blob: 1149b8980ba393a9e7932d5e1d4c4c5df0a3358e [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002 * Copyright (C) 2010 The Android Open Source Project
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Surface"
Mathias Agopiane3c697f2013-02-14 17:11:02 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Mathias Agopian05debe12017-02-08 17:04:18 -080021#include <gui/Surface.h>
Mathias Agopianb0e76f42012-03-23 14:15:44 -070022
Mathias Agopian05debe12017-02-08 17:04:18 -080023#include <android/native_window.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080024
Mathias Agopian9cce3252010-02-09 17:46:37 -080025#include <utils/Log.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080026#include <utils/Trace.h>
Rachad7cb0d392014-07-29 17:53:53 -070027#include <utils/NativeHandle.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080028
Ian Elliott62c48c92017-01-20 13:13:20 -070029#include <ui/DisplayStatInfo.h>
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -070030#include <ui/Fence.h>
31#include <ui/HdrCapabilities.h>
32#include <ui/Region.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070033
Mathias Agopian2b5dd402017-02-07 17:36:19 -080034#include <gui/BufferItem.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070035#include <gui/IProducerListener.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080036
Mathias Agopian2b5dd402017-02-07 17:36:19 -080037#include <gui/ISurfaceComposer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080038#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -060040#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
41#include <configstore/Utils.h>
42
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043namespace android {
44
Mathias Agopiane3c697f2013-02-14 17:11:02 -080045Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070046 const sp<IGraphicBufferProducer>& bufferProducer,
47 bool controlledByApp)
Dan Stoza812ed062015-06-02 15:45:22 -070048 : mGraphicBufferProducer(bufferProducer),
Pablo Ceballos60d69222015-08-07 14:47:20 -070049 mCrop(Rect::EMPTY_RECT),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080050 mGenerationNumber(0),
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070051 mSharedBufferMode(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080052 mAutoRefresh(false),
53 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
Pablo Ceballosbc8c1922016-07-01 14:15:41 -070054 mSharedBufferHasBeenQueued(false),
Brian Anderson3da8d272016-07-28 16:20:47 -070055 mEnableFrameTimestamps(false),
56 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>())
Mathias Agopian62185b72009-04-16 16:19:50 -070057{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080058 // Initialize the ANativeWindow function pointers.
59 ANativeWindow::setSwapInterval = hook_setSwapInterval;
60 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
61 ANativeWindow::cancelBuffer = hook_cancelBuffer;
62 ANativeWindow::queueBuffer = hook_queueBuffer;
63 ANativeWindow::query = hook_query;
64 ANativeWindow::perform = hook_perform;
65
66 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
67 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
68 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
69 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
70
71 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
72 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
73
74 mReqWidth = 0;
75 mReqHeight = 0;
76 mReqFormat = 0;
77 mReqUsage = 0;
78 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080079 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080080 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
81 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070082 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080083 mDefaultWidth = 0;
84 mDefaultHeight = 0;
85 mUserWidth = 0;
86 mUserHeight = 0;
87 mTransformHint = 0;
88 mConsumerRunningBehind = false;
89 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070090 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070091 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070092}
93
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070094Surface::~Surface() {
95 if (mConnectedToCpu) {
96 Surface::disconnect(NATIVE_WINDOW_API_CPU);
97 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -080098}
99
Brian Anderson3da8d272016-07-28 16:20:47 -0700100sp<ISurfaceComposer> Surface::composerService() const {
101 return ComposerService::getComposerService();
102}
103
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800104nsecs_t Surface::now() const {
105 return systemTime();
106}
107
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800108sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
109 return mGraphicBufferProducer;
110}
111
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900112void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
113 mGraphicBufferProducer->setSidebandStream(stream);
114}
115
Dan Stoza29a3e902014-06-20 13:13:57 -0700116void Surface::allocateBuffers() {
117 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
118 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700119 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
120 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700121}
122
Dan Stoza812ed062015-06-02 15:45:22 -0700123status_t Surface::setGenerationNumber(uint32_t generation) {
124 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
125 if (result == NO_ERROR) {
126 mGenerationNumber = generation;
127 }
128 return result;
129}
130
Dan Stoza7dde5992015-05-22 09:51:44 -0700131uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700132 Mutex::Autolock lock(mMutex);
133 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700134}
135
Dan Stozac6f30bd2015-06-08 09:32:50 -0700136String8 Surface::getConsumerName() const {
137 return mGraphicBufferProducer->getConsumerName();
138}
139
Dan Stoza127fc632015-06-30 13:43:32 -0700140status_t Surface::setDequeueTimeout(nsecs_t timeout) {
141 return mGraphicBufferProducer->setDequeueTimeout(timeout);
142}
143
Dan Stoza50101d02016-04-07 16:53:23 -0700144status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700145 sp<Fence>* outFence, float outTransformMatrix[16]) {
146 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
147 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700148}
149
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800150status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
151 ATRACE_CALL();
152
153 DisplayStatInfo stats;
154 status_t err = composerService()->getDisplayStats(NULL, &stats);
155
156 *outRefreshDuration = stats.vsyncPeriod;
157
158 return NO_ERROR;
159}
160
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700161void Surface::enableFrameTimestamps(bool enable) {
162 Mutex::Autolock lock(mMutex);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800163 // If going from disabled to enabled, get the initial values for
164 // compositor and display timing.
165 if (!mEnableFrameTimestamps && enable) {
166 FrameEventHistoryDelta delta;
167 mGraphicBufferProducer->getFrameTimestamps(&delta);
168 mFrameEventHistory->applyDelta(delta);
169 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700170 mEnableFrameTimestamps = enable;
171}
172
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800173status_t Surface::getCompositorTiming(
174 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
175 nsecs_t* compositeToPresentLatency) {
176 Mutex::Autolock lock(mMutex);
177 if (!mEnableFrameTimestamps) {
178 return INVALID_OPERATION;
179 }
180
181 if (compositeDeadline != nullptr) {
182 *compositeDeadline =
183 mFrameEventHistory->getNextCompositeDeadline(now());
184 }
185 if (compositeInterval != nullptr) {
186 *compositeInterval = mFrameEventHistory->getCompositeInterval();
187 }
188 if (compositeToPresentLatency != nullptr) {
189 *compositeToPresentLatency =
190 mFrameEventHistory->getCompositeToPresentLatency();
191 }
192 return NO_ERROR;
193}
194
Brian Anderson50143b32016-09-30 14:01:24 -0700195static bool checkConsumerForUpdates(
196 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700197 const nsecs_t* outLatchTime,
198 const nsecs_t* outFirstRefreshStartTime,
199 const nsecs_t* outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700200 const nsecs_t* outGpuCompositionDoneTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700201 const nsecs_t* outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700202 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700203 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700204 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
205 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700206 !e->hasFirstRefreshStartInfo();
Brian Andersonb04c6f02016-10-21 12:57:46 -0700207 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700208 !e->hasGpuCompositionDoneInfo();
209 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
210 !e->hasDisplayPresentInfo();
211
Brian Anderson4e606e32017-03-16 15:34:57 -0700212 // LastRefreshStart, DequeueReady, and Release are never
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700213 // available for the last frame.
214 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
215 !e->hasLastRefreshStartInfo() &&
216 (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700217 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
218 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700219 bool checkForRelease = (outReleaseTime != nullptr) &&
220 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
221
222 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700223 return checkForLatch || checkForFirstRefreshStart ||
Brian Andersonb04c6f02016-10-21 12:57:46 -0700224 checkForLastRefreshStart || checkForGpuCompositionDone ||
Brian Anderson4e606e32017-03-16 15:34:57 -0700225 checkForDisplayPresent || checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700226}
227
Brian Anderson3d4039d2016-09-23 16:31:30 -0700228static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
229 if (dst != nullptr) {
Brian Andersoned816e62016-10-26 16:12:21 -0700230 *dst = FrameEvents::isValidTimestamp(src) ? src : 0;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700231 }
232}
233
234static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) {
235 if (dst != nullptr) {
236 nsecs_t signalTime = src->getSignalTime();
237 *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0;
238 }
239}
240
Brian Anderson069b3652016-07-22 10:32:47 -0700241status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700242 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700243 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700244 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700245 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
246 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800247 ATRACE_CALL();
248
Brian Anderson3890c392016-07-25 12:48:08 -0700249 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700250
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700251 if (!mEnableFrameTimestamps) {
252 return INVALID_OPERATION;
253 }
254
Brian Anderson3da8d272016-07-28 16:20:47 -0700255 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700256 if (events == nullptr) {
257 // If the entry isn't available in the producer, it's definitely not
258 // available in the consumer.
259 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700260 }
261
Brian Anderson50143b32016-09-30 14:01:24 -0700262 // Update our cache of events if the requested events are not available.
263 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700264 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700265 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700266 outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700267 FrameEventHistoryDelta delta;
268 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700269 mFrameEventHistory->applyDelta(delta);
270 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700271 }
272
Brian Anderson3890c392016-07-25 12:48:08 -0700273 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700274 // The entry was available before the update, but was overwritten
275 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700276 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800277 }
Brian Anderson069b3652016-07-22 10:32:47 -0700278
Brian Anderson3d4039d2016-09-23 16:31:30 -0700279 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700280 getFrameTimestamp(outLatchTime, events->latchTime);
281 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
282 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
283 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700284
Brian Anderson3d4039d2016-09-23 16:31:30 -0700285 getFrameTimestampFence(outAcquireTime, events->acquireFence);
286 getFrameTimestampFence(
Brian Andersonb04c6f02016-10-21 12:57:46 -0700287 outGpuCompositionDoneTime, events->gpuCompositionDoneFence);
Brian Anderson3d4039d2016-09-23 16:31:30 -0700288 getFrameTimestampFence(
289 outDisplayPresentTime, events->displayPresentFence);
Brian Anderson3d4039d2016-09-23 16:31:30 -0700290 getFrameTimestampFence(outReleaseTime, events->releaseFence);
Brian Anderson069b3652016-07-22 10:32:47 -0700291
292 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800293}
294
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600295using namespace android::hardware::configstore;
296using namespace android::hardware::configstore::V1_0;
297
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700298status_t Surface::getWideColorSupport(bool* supported) {
299 ATRACE_CALL();
300
301 sp<IBinder> display(
302 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
303 Vector<android_color_mode_t> colorModes;
304 status_t err =
305 composerService()->getDisplayColorModes(display, &colorModes);
306
307 if (err)
308 return err;
309
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600310 bool wideColorBoardConfig =
311 getBool<ISurfaceFlingerConfigs,
312 &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
313
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700314 *supported = false;
315 for (android_color_mode_t colorMode : colorModes) {
316 switch (colorMode) {
317 case HAL_COLOR_MODE_DISPLAY_P3:
318 case HAL_COLOR_MODE_ADOBE_RGB:
319 case HAL_COLOR_MODE_DCI_P3:
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600320 if (wideColorBoardConfig) {
321 *supported = true;
322 }
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700323 break;
324 default:
325 break;
326 }
327 }
328
329 return NO_ERROR;
330}
331
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700332status_t Surface::getHdrSupport(bool* supported) {
333 ATRACE_CALL();
334
335 sp<IBinder> display(
336 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
337 HdrCapabilities hdrCapabilities;
338 status_t err =
339 composerService()->getHdrCapabilities(display, &hdrCapabilities);
340
341 if (err)
342 return err;
343
344 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
345
346 return NO_ERROR;
347}
348
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800349int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
350 Surface* c = getSelf(window);
351 return c->setSwapInterval(interval);
352}
353
354int Surface::hook_dequeueBuffer(ANativeWindow* window,
355 ANativeWindowBuffer** buffer, int* fenceFd) {
356 Surface* c = getSelf(window);
357 return c->dequeueBuffer(buffer, fenceFd);
358}
359
360int Surface::hook_cancelBuffer(ANativeWindow* window,
361 ANativeWindowBuffer* buffer, int fenceFd) {
362 Surface* c = getSelf(window);
363 return c->cancelBuffer(buffer, fenceFd);
364}
365
366int Surface::hook_queueBuffer(ANativeWindow* window,
367 ANativeWindowBuffer* buffer, int fenceFd) {
368 Surface* c = getSelf(window);
369 return c->queueBuffer(buffer, fenceFd);
370}
371
372int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
373 ANativeWindowBuffer** buffer) {
374 Surface* c = getSelf(window);
375 ANativeWindowBuffer* buf;
376 int fenceFd = -1;
377 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600378 if (result != OK) {
379 return result;
380 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800381 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700382 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800383 if (waitResult != OK) {
384 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
385 waitResult);
386 c->cancelBuffer(buf, -1);
387 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700388 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800389 *buffer = buf;
390 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700391}
392
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800393int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
394 ANativeWindowBuffer* buffer) {
395 Surface* c = getSelf(window);
396 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700397}
398
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800399int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
400 ANativeWindowBuffer* buffer) {
401 Surface* c = getSelf(window);
402 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700403}
404
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800405int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
406 ANativeWindowBuffer* buffer) {
407 Surface* c = getSelf(window);
408 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700409}
Mathias Agopian62185b72009-04-16 16:19:50 -0700410
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800411int Surface::hook_query(const ANativeWindow* window,
412 int what, int* value) {
413 const Surface* c = getSelf(window);
414 return c->query(what, value);
415}
416
417int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
418 va_list args;
419 va_start(args, operation);
420 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700421 int result = c->perform(operation, args);
422 va_end(args);
423 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800424}
425
426int Surface::setSwapInterval(int interval) {
427 ATRACE_CALL();
428 // EGL specification states:
429 // interval is silently clamped to minimum and maximum implementation
430 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800431
432 if (interval < minSwapInterval)
433 interval = minSwapInterval;
434
435 if (interval > maxSwapInterval)
436 interval = maxSwapInterval;
437
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700438 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700439 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800440
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700441 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800442}
443
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700444int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800445 ATRACE_CALL();
446 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800447
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800448 uint32_t reqWidth;
449 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800450 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800451 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700452 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800453
454 {
455 Mutex::Autolock lock(mMutex);
456
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800457 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
458 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800459
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800460 reqFormat = mReqFormat;
461 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800462
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700463 enableFrameTimestamps = mEnableFrameTimestamps;
464
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700465 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800466 BufferItem::INVALID_BUFFER_SLOT) {
467 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
468 if (gbuf != NULL) {
469 *buffer = gbuf.get();
470 *fenceFd = -1;
471 return OK;
472 }
473 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800474 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
475
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800476 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800477 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700478 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700479
480 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700481 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700482 reqWidth, reqHeight, reqFormat, reqUsage,
483 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700484 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800485
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800486 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700487 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
488 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
489 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800490 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700491 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800492
493 Mutex::Autolock lock(mMutex);
494
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800495 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700496
497 // this should never happen
498 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
499
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800500 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
501 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700502 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700503
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700504 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700505 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700506 }
507
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700508 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
509 if (mReportRemovedBuffers && (gbuf != nullptr)) {
510 mRemovedBuffers.clear();
511 mRemovedBuffers.push_back(gbuf);
512 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800513 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
514 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700515 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700516 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800517 return result;
518 }
519 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700520
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800521 if (fence->isValid()) {
522 *fenceFd = fence->dup();
523 if (*fenceFd == -1) {
524 ALOGE("dequeueBuffer: error duping fence: %d", errno);
525 // dup() should never fail; something is badly wrong. Soldier on
526 // and hope for the best; the worst that should happen is some
527 // visible corruption that lasts until the next frame.
528 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700529 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800530 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700531 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800532
533 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800534
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700535 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800536 mSharedBufferSlot = buf;
537 mSharedBufferHasBeenQueued = false;
538 } else if (mSharedBufferSlot == buf) {
539 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
540 mSharedBufferHasBeenQueued = false;
541 }
542
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800543 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700544}
545
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800546int Surface::cancelBuffer(android_native_buffer_t* buffer,
547 int fenceFd) {
548 ATRACE_CALL();
549 ALOGV("Surface::cancelBuffer");
550 Mutex::Autolock lock(mMutex);
551 int i = getSlotFromBufferLocked(buffer);
552 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900553 if (fenceFd >= 0) {
554 close(fenceFd);
555 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800556 return i;
557 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800558 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
559 if (fenceFd >= 0) {
560 close(fenceFd);
561 }
562 return OK;
563 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800564 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
565 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800566
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700567 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800568 mSharedBufferHasBeenQueued = true;
569 }
570
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800571 return OK;
572}
573
574int Surface::getSlotFromBufferLocked(
575 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800576 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
577 if (mSlots[i].buffer != NULL &&
578 mSlots[i].buffer->handle == buffer->handle) {
579 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700580 }
581 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800582 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
583 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700584}
585
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800586int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800587 ALOGV("Surface::lockBuffer");
588 Mutex::Autolock lock(mMutex);
589 return OK;
590}
Mathias Agopian631f3582010-05-25 17:51:34 -0700591
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800592int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
593 ATRACE_CALL();
594 ALOGV("Surface::queueBuffer");
595 Mutex::Autolock lock(mMutex);
596 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700597 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800598
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800599 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700600 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700601 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700602 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700603 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800604 } else {
605 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700606 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800607 int i = getSlotFromBufferLocked(buffer);
608 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900609 if (fenceFd >= 0) {
610 close(fenceFd);
611 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800612 return i;
613 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800614 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
615 if (fenceFd >= 0) {
616 close(fenceFd);
617 }
618 return OK;
619 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800622 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700623 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800624 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800626 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
627 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700628 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800629 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700630 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700631
Dan Stozac62acbd2015-04-21 16:42:49 -0700632 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700633 input.setSurfaceDamage(Region::INVALID_REGION);
634 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700635 // Here we do two things:
636 // 1) The surface damage was specified using the OpenGL ES convention of
637 // the origin being in the bottom-left corner. Here we flip to the
638 // convention that the rest of the system uses (top-left corner) by
639 // subtracting all top/bottom coordinates from the buffer height.
640 // 2) If the buffer is coming in rotated (for example, because the EGL
641 // implementation is reacting to the transform hint coming back from
642 // SurfaceFlinger), the surface damage needs to be rotated the
643 // opposite direction, since it was generated assuming an unrotated
644 // buffer (the app doesn't know that the EGL implementation is
645 // reacting to the transform hint behind its back). The
646 // transformations in the switch statement below apply those
647 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
648
649 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700650 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700651 bool rotated90 = (mTransform ^ mStickyTransform) &
652 NATIVE_WINDOW_TRANSFORM_ROT_90;
653 if (rotated90) {
654 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700655 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700656
Dan Stoza5065a552015-03-17 16:23:42 -0700657 Region flippedRegion;
658 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700659 int left = rect.left;
660 int right = rect.right;
661 int top = height - rect.bottom; // Flip from OpenGL convention
662 int bottom = height - rect.top; // Flip from OpenGL convention
663 switch (mTransform ^ mStickyTransform) {
664 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
665 // Rotate 270 degrees
666 Rect flippedRect{top, width - right, bottom, width - left};
667 flippedRegion.orSelf(flippedRect);
668 break;
669 }
670 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
671 // Rotate 180 degrees
672 Rect flippedRect{width - right, height - bottom,
673 width - left, height - top};
674 flippedRegion.orSelf(flippedRect);
675 break;
676 }
677 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
678 // Rotate 90 degrees
679 Rect flippedRect{height - bottom, left,
680 height - top, right};
681 flippedRegion.orSelf(flippedRect);
682 break;
683 }
684 default: {
685 Rect flippedRect{left, top, right, bottom};
686 flippedRegion.orSelf(flippedRect);
687 break;
688 }
689 }
Dan Stoza5065a552015-03-17 16:23:42 -0700690 }
691
692 input.setSurfaceDamage(flippedRegion);
693 }
694
Dan Stoza70ccba52016-07-01 14:00:40 -0700695 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800696 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700697 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800698 if (err != OK) {
699 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
700 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800701
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700702 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700703 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700704 // Update timestamps with the local acquire fence.
705 // The consumer doesn't send it back to prevent us from having two
706 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700707 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700708 std::make_shared<FenceTime>(std::move(fence)));
709
710 // Cache timestamps of signaled fences so we can close their file
711 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700712 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700713 }
714
Brian Anderson50143b32016-09-30 14:01:24 -0700715 mLastFrameNumber = mNextFrameNumber;
716
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700717 mDefaultWidth = output.width;
718 mDefaultHeight = output.height;
719 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700720
Ruben Brunk1681d952014-06-27 15:51:55 -0700721 // Disable transform hint if sticky transform is set.
722 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700723 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700724 }
725
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700726 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700727
Dan Stozac62acbd2015-04-21 16:42:49 -0700728 if (!mConnectedToCpu) {
729 // Clear surface damage back to full-buffer
730 mDirtyRegion = Region::INVALID_REGION;
731 }
Dan Stoza5065a552015-03-17 16:23:42 -0700732
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700733 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800734 mSharedBufferHasBeenQueued = true;
735 }
736
Robert Carr9f31e292016-04-11 11:15:32 -0700737 mQueueBufferCondition.broadcast();
738
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800739 return err;
740}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700741
Mathias Agopiana67932f2011-04-20 14:20:59 -0700742int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800743 ATRACE_CALL();
744 ALOGV("Surface::query");
745 { // scope for the lock
746 Mutex::Autolock lock(mMutex);
747 switch (what) {
748 case NATIVE_WINDOW_FORMAT:
749 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800750 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800751 return NO_ERROR;
752 }
753 break;
754 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700755 if (composerService()->authenticateSurfaceTexture(
756 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800757 *value = 1;
758 } else {
759 *value = 0;
760 }
761 return NO_ERROR;
762 }
763 case NATIVE_WINDOW_CONCRETE_TYPE:
764 *value = NATIVE_WINDOW_SURFACE;
765 return NO_ERROR;
766 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800767 *value = static_cast<int>(
768 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800769 return NO_ERROR;
770 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800771 *value = static_cast<int>(
772 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800773 return NO_ERROR;
774 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800775 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800776 return NO_ERROR;
777 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
778 status_t err = NO_ERROR;
779 if (!mConsumerRunningBehind) {
780 *value = 0;
781 } else {
782 err = mGraphicBufferProducer->query(what, value);
783 if (err == NO_ERROR) {
784 mConsumerRunningBehind = *value;
785 }
786 }
787 return err;
788 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700789 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
790 int64_t durationUs = mLastDequeueDuration / 1000;
791 *value = durationUs > std::numeric_limits<int>::max() ?
792 std::numeric_limits<int>::max() :
793 static_cast<int>(durationUs);
794 return NO_ERROR;
795 }
796 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
797 int64_t durationUs = mLastQueueDuration / 1000;
798 *value = durationUs > std::numeric_limits<int>::max() ?
799 std::numeric_limits<int>::max() :
800 static_cast<int>(durationUs);
801 return NO_ERROR;
802 }
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800803 case NATIVE_WINDOW_IS_VALID: {
804 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
805 return NO_ERROR;
806 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800807 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700808 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800809 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800810}
811
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800812int Surface::perform(int operation, va_list args)
813{
814 int res = NO_ERROR;
815 switch (operation) {
816 case NATIVE_WINDOW_CONNECT:
817 // deprecated. must return NO_ERROR.
818 break;
819 case NATIVE_WINDOW_DISCONNECT:
820 // deprecated. must return NO_ERROR.
821 break;
822 case NATIVE_WINDOW_SET_USAGE:
823 res = dispatchSetUsage(args);
824 break;
825 case NATIVE_WINDOW_SET_CROP:
826 res = dispatchSetCrop(args);
827 break;
828 case NATIVE_WINDOW_SET_BUFFER_COUNT:
829 res = dispatchSetBufferCount(args);
830 break;
831 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
832 res = dispatchSetBuffersGeometry(args);
833 break;
834 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
835 res = dispatchSetBuffersTransform(args);
836 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700837 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
838 res = dispatchSetBuffersStickyTransform(args);
839 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800840 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
841 res = dispatchSetBuffersTimestamp(args);
842 break;
843 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
844 res = dispatchSetBuffersDimensions(args);
845 break;
846 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
847 res = dispatchSetBuffersUserDimensions(args);
848 break;
849 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
850 res = dispatchSetBuffersFormat(args);
851 break;
852 case NATIVE_WINDOW_LOCK:
853 res = dispatchLock(args);
854 break;
855 case NATIVE_WINDOW_UNLOCK_AND_POST:
856 res = dispatchUnlockAndPost(args);
857 break;
858 case NATIVE_WINDOW_SET_SCALING_MODE:
859 res = dispatchSetScalingMode(args);
860 break;
861 case NATIVE_WINDOW_API_CONNECT:
862 res = dispatchConnect(args);
863 break;
864 case NATIVE_WINDOW_API_DISCONNECT:
865 res = dispatchDisconnect(args);
866 break;
Rachad7cb0d392014-07-29 17:53:53 -0700867 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
868 res = dispatchSetSidebandStream(args);
869 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800870 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
871 res = dispatchSetBuffersDataSpace(args);
872 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700873 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
874 res = dispatchSetSurfaceDamage(args);
875 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700876 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
877 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700878 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800879 case NATIVE_WINDOW_SET_AUTO_REFRESH:
880 res = dispatchSetAutoRefresh(args);
881 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800882 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
883 res = dispatchGetDisplayRefreshCycleDuration(args);
884 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800885 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
886 res = dispatchGetNextFrameId(args);
887 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700888 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
889 res = dispatchEnableFrameTimestamps(args);
890 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800891 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
892 res = dispatchGetCompositorTiming(args);
893 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800894 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
895 res = dispatchGetFrameTimestamps(args);
896 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700897 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
898 res = dispatchGetWideColorSupport(args);
899 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700900 case NATIVE_WINDOW_GET_HDR_SUPPORT:
901 res = dispatchGetHdrSupport(args);
902 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800903 default:
904 res = NAME_NOT_FOUND;
905 break;
906 }
907 return res;
908}
Mathias Agopiana138f892010-05-21 17:24:35 -0700909
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800910int Surface::dispatchConnect(va_list args) {
911 int api = va_arg(args, int);
912 return connect(api);
913}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800914
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800915int Surface::dispatchDisconnect(va_list args) {
916 int api = va_arg(args, int);
917 return disconnect(api);
918}
919
920int Surface::dispatchSetUsage(va_list args) {
921 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800922 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800923}
924
925int Surface::dispatchSetCrop(va_list args) {
926 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
927 return setCrop(reinterpret_cast<Rect const*>(rect));
928}
929
930int Surface::dispatchSetBufferCount(va_list args) {
931 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800932 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800933}
934
935int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800936 uint32_t width = va_arg(args, uint32_t);
937 uint32_t height = va_arg(args, uint32_t);
938 PixelFormat format = va_arg(args, PixelFormat);
939 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800940 if (err != 0) {
941 return err;
942 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800943 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800944}
945
946int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800947 uint32_t width = va_arg(args, uint32_t);
948 uint32_t height = va_arg(args, uint32_t);
949 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800950}
951
952int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800953 uint32_t width = va_arg(args, uint32_t);
954 uint32_t height = va_arg(args, uint32_t);
955 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800956}
957
958int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800959 PixelFormat format = va_arg(args, PixelFormat);
960 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800961}
962
963int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800964 int mode = va_arg(args, int);
965 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800966}
967
968int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800969 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800970 return setBuffersTransform(transform);
971}
972
Ruben Brunk1681d952014-06-27 15:51:55 -0700973int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800974 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700975 return setBuffersStickyTransform(transform);
976}
977
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800978int Surface::dispatchSetBuffersTimestamp(va_list args) {
979 int64_t timestamp = va_arg(args, int64_t);
980 return setBuffersTimestamp(timestamp);
981}
982
983int Surface::dispatchLock(va_list args) {
984 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
985 ARect* inOutDirtyBounds = va_arg(args, ARect*);
986 return lock(outBuffer, inOutDirtyBounds);
987}
988
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800989int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800990 return unlockAndPost();
991}
992
Rachad7cb0d392014-07-29 17:53:53 -0700993int Surface::dispatchSetSidebandStream(va_list args) {
994 native_handle_t* sH = va_arg(args, native_handle_t*);
995 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
996 setSidebandStream(sidebandHandle);
997 return OK;
998}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800999
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001000int Surface::dispatchSetBuffersDataSpace(va_list args) {
1001 android_dataspace dataspace =
1002 static_cast<android_dataspace>(va_arg(args, int));
1003 return setBuffersDataSpace(dataspace);
1004}
1005
Dan Stoza5065a552015-03-17 16:23:42 -07001006int Surface::dispatchSetSurfaceDamage(va_list args) {
1007 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1008 size_t numRects = va_arg(args, size_t);
1009 setSurfaceDamage(rects, numRects);
1010 return NO_ERROR;
1011}
1012
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001013int Surface::dispatchSetSharedBufferMode(va_list args) {
1014 bool sharedBufferMode = va_arg(args, int);
1015 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001016}
1017
1018int Surface::dispatchSetAutoRefresh(va_list args) {
1019 bool autoRefresh = va_arg(args, int);
1020 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001021}
1022
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001023int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1024 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1025 return getDisplayRefreshCycleDuration(outRefreshDuration);
1026}
1027
Brian Anderson1049d1d2016-12-16 17:25:57 -08001028int Surface::dispatchGetNextFrameId(va_list args) {
1029 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1030 *nextFrameId = getNextFrameNumber();
1031 return NO_ERROR;
1032}
1033
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001034int Surface::dispatchEnableFrameTimestamps(va_list args) {
1035 bool enable = va_arg(args, int);
1036 enableFrameTimestamps(enable);
1037 return NO_ERROR;
1038}
1039
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001040int Surface::dispatchGetCompositorTiming(va_list args) {
1041 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1042 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1043 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1044 return getCompositorTiming(compositeDeadline, compositeInterval,
1045 compositeToPresentLatency);
1046}
1047
Pablo Ceballosce796e72016-02-04 19:10:51 -08001048int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001049 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001050 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001051 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001052 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1053 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1054 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001055 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001056 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001057 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001058 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001059 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001060 outRequestedPresentTime, outAcquireTime, outLatchTime,
1061 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001062 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001063 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001064}
1065
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001066int Surface::dispatchGetWideColorSupport(va_list args) {
1067 bool* outSupport = va_arg(args, bool*);
1068 return getWideColorSupport(outSupport);
1069}
1070
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001071int Surface::dispatchGetHdrSupport(va_list args) {
1072 bool* outSupport = va_arg(args, bool*);
1073 return getHdrSupport(outSupport);
1074}
1075
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001076int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001077 static sp<IProducerListener> listener = new DummyProducerListener();
1078 return connect(api, listener);
1079}
1080
1081int Surface::connect(int api, const sp<IProducerListener>& listener) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001082 return connect(api, listener, false);
1083}
1084
1085int Surface::connect(
1086 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001087 ATRACE_CALL();
1088 ALOGV("Surface::connect");
1089 Mutex::Autolock lock(mMutex);
1090 IGraphicBufferProducer::QueueBufferOutput output;
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001091 mReportRemovedBuffers = reportBufferRemoval;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001092 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001093 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001094 mDefaultWidth = output.width;
1095 mDefaultHeight = output.height;
1096 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001097
1098 // Disable transform hint if sticky transform is set.
1099 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001100 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001101 }
1102
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001103 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001104 }
1105 if (!err && api == NATIVE_WINDOW_API_CPU) {
1106 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001107 // Clear the dirty region in case we're switching from a non-CPU API
1108 mDirtyRegion.clear();
1109 } else if (!err) {
1110 // Initialize the dirty region for tracking surface damage
1111 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001112 }
Dan Stoza5065a552015-03-17 16:23:42 -07001113
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001114 return err;
1115}
1116
Mathias Agopian365857d2013-09-11 19:35:45 -07001117
Robert Carr97b9c862016-09-08 13:54:35 -07001118int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001119 ATRACE_CALL();
1120 ALOGV("Surface::disconnect");
1121 Mutex::Autolock lock(mMutex);
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001122 mRemovedBuffers.clear();
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001123 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1124 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001125 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001126 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001127 if (!err) {
1128 mReqFormat = 0;
1129 mReqWidth = 0;
1130 mReqHeight = 0;
1131 mReqUsage = 0;
1132 mCrop.clear();
1133 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1134 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001135 mStickyTransform = 0;
1136
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001137 if (api == NATIVE_WINDOW_API_CPU) {
1138 mConnectedToCpu = false;
1139 }
1140 }
1141 return err;
1142}
1143
Dan Stozad9c49712015-04-27 11:06:01 -07001144int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001145 sp<Fence>* outFence) {
1146 ATRACE_CALL();
1147 ALOGV("Surface::detachNextBuffer");
1148
1149 if (outBuffer == NULL || outFence == NULL) {
1150 return BAD_VALUE;
1151 }
1152
1153 Mutex::Autolock lock(mMutex);
1154
1155 sp<GraphicBuffer> buffer(NULL);
1156 sp<Fence> fence(NULL);
1157 status_t result = mGraphicBufferProducer->detachNextBuffer(
1158 &buffer, &fence);
1159 if (result != NO_ERROR) {
1160 return result;
1161 }
1162
Dan Stozad9c49712015-04-27 11:06:01 -07001163 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001164 if (fence != NULL && fence->isValid()) {
1165 *outFence = fence;
1166 } else {
1167 *outFence = Fence::NO_FENCE;
1168 }
1169
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001170 if (mReportRemovedBuffers) {
1171 mRemovedBuffers.clear();
1172 }
1173
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001174 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1175 if (mSlots[i].buffer != NULL &&
1176 mSlots[i].buffer->handle == buffer->handle) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001177 if (mReportRemovedBuffers) {
1178 mRemovedBuffers.push_back(mSlots[i].buffer);
1179 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001180 mSlots[i].buffer = NULL;
1181 }
1182 }
1183
Dan Stoza231832e2015-03-11 11:55:01 -07001184 return NO_ERROR;
1185}
1186
1187int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1188{
1189 ATRACE_CALL();
1190 ALOGV("Surface::attachBuffer");
1191
1192 Mutex::Autolock lock(mMutex);
1193
1194 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001195 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1196 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001197 int32_t attachedSlot = -1;
1198 status_t result = mGraphicBufferProducer->attachBuffer(
1199 &attachedSlot, graphicBuffer);
1200 if (result != NO_ERROR) {
1201 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001202 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001203 return result;
1204 }
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001205 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
1206 mRemovedBuffers.clear();
1207 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1208 }
Dan Stoza231832e2015-03-11 11:55:01 -07001209 mSlots[attachedSlot].buffer = graphicBuffer;
1210
1211 return NO_ERROR;
1212}
1213
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001214int Surface::setUsage(uint32_t reqUsage)
1215{
1216 ALOGV("Surface::setUsage");
1217 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001218 if (reqUsage != mReqUsage) {
1219 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1220 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001221 mReqUsage = reqUsage;
1222 return OK;
1223}
1224
1225int Surface::setCrop(Rect const* rect)
1226{
1227 ATRACE_CALL();
1228
Pablo Ceballos60d69222015-08-07 14:47:20 -07001229 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001230 if (rect == NULL || rect->isEmpty()) {
1231 realRect.clear();
1232 } else {
1233 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001234 }
1235
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001236 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1237 realRect.left, realRect.top, realRect.right, realRect.bottom);
1238
1239 Mutex::Autolock lock(mMutex);
1240 mCrop = realRect;
1241 return NO_ERROR;
1242}
1243
1244int Surface::setBufferCount(int bufferCount)
1245{
1246 ATRACE_CALL();
1247 ALOGV("Surface::setBufferCount");
1248 Mutex::Autolock lock(mMutex);
1249
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001250 status_t err = NO_ERROR;
1251 if (bufferCount == 0) {
1252 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1253 } else {
1254 int minUndequeuedBuffers = 0;
1255 err = mGraphicBufferProducer->query(
1256 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1257 if (err == NO_ERROR) {
1258 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1259 bufferCount - minUndequeuedBuffers);
1260 }
1261 }
Mathias Agopian90147262010-01-22 11:47:55 -08001262
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001263 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1264 bufferCount, strerror(-err));
1265
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001266 return err;
1267}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001268
Pablo Ceballosfa455352015-08-12 17:47:47 -07001269int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1270 ATRACE_CALL();
1271 ALOGV("Surface::setMaxDequeuedBufferCount");
1272 Mutex::Autolock lock(mMutex);
1273
1274 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1275 maxDequeuedBuffers);
1276 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1277 "returned %s", maxDequeuedBuffers, strerror(-err));
1278
Pablo Ceballosfa455352015-08-12 17:47:47 -07001279 return err;
1280}
1281
1282int Surface::setAsyncMode(bool async) {
1283 ATRACE_CALL();
1284 ALOGV("Surface::setAsyncMode");
1285 Mutex::Autolock lock(mMutex);
1286
1287 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1288 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1289 async, strerror(-err));
1290
Pablo Ceballosfa455352015-08-12 17:47:47 -07001291 return err;
1292}
1293
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001294int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001295 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001296 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001297 Mutex::Autolock lock(mMutex);
1298
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001299 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1300 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001301 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001302 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001303 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001304 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1305 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001306
1307 return err;
1308}
1309
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001310int Surface::setAutoRefresh(bool autoRefresh) {
1311 ATRACE_CALL();
1312 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1313 Mutex::Autolock lock(mMutex);
1314
1315 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1316 if (err == NO_ERROR) {
1317 mAutoRefresh = autoRefresh;
1318 }
1319 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1320 autoRefresh, strerror(-err));
1321 return err;
1322}
1323
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001324int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001325{
1326 ATRACE_CALL();
1327 ALOGV("Surface::setBuffersDimensions");
1328
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001329 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001330 return BAD_VALUE;
1331
1332 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001333 if (width != mReqWidth || height != mReqHeight) {
1334 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1335 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001336 mReqWidth = width;
1337 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001338 return NO_ERROR;
1339}
1340
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001341int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001342{
1343 ATRACE_CALL();
1344 ALOGV("Surface::setBuffersUserDimensions");
1345
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001346 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001347 return BAD_VALUE;
1348
1349 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001350 if (width != mUserWidth || height != mUserHeight) {
1351 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1352 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001353 mUserWidth = width;
1354 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001355 return NO_ERROR;
1356}
1357
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001358int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001359{
1360 ALOGV("Surface::setBuffersFormat");
1361
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001362 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001363 if (format != mReqFormat) {
1364 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1365 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001366 mReqFormat = format;
1367 return NO_ERROR;
1368}
1369
1370int Surface::setScalingMode(int mode)
1371{
1372 ATRACE_CALL();
1373 ALOGV("Surface::setScalingMode(%d)", mode);
1374
1375 switch (mode) {
1376 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1377 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1378 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001379 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001380 break;
1381 default:
1382 ALOGE("unknown scaling mode: %d", mode);
1383 return BAD_VALUE;
1384 }
1385
1386 Mutex::Autolock lock(mMutex);
1387 mScalingMode = mode;
1388 return NO_ERROR;
1389}
1390
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001391int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001392{
1393 ATRACE_CALL();
1394 ALOGV("Surface::setBuffersTransform");
1395 Mutex::Autolock lock(mMutex);
1396 mTransform = transform;
1397 return NO_ERROR;
1398}
1399
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001400int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001401{
1402 ATRACE_CALL();
1403 ALOGV("Surface::setBuffersStickyTransform");
1404 Mutex::Autolock lock(mMutex);
1405 mStickyTransform = transform;
1406 return NO_ERROR;
1407}
1408
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001409int Surface::setBuffersTimestamp(int64_t timestamp)
1410{
1411 ALOGV("Surface::setBuffersTimestamp");
1412 Mutex::Autolock lock(mMutex);
1413 mTimestamp = timestamp;
1414 return NO_ERROR;
1415}
1416
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001417int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1418{
1419 ALOGV("Surface::setBuffersDataSpace");
1420 Mutex::Autolock lock(mMutex);
1421 mDataSpace = dataSpace;
1422 return NO_ERROR;
1423}
1424
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001425void Surface::freeAllBuffers() {
1426 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1427 mSlots[i].buffer = 0;
1428 }
1429}
1430
Dan Stoza5065a552015-03-17 16:23:42 -07001431void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1432 ATRACE_CALL();
1433 ALOGV("Surface::setSurfaceDamage");
1434 Mutex::Autolock lock(mMutex);
1435
Dan Stozac62acbd2015-04-21 16:42:49 -07001436 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001437 mDirtyRegion = Region::INVALID_REGION;
1438 return;
1439 }
1440
1441 mDirtyRegion.clear();
1442 for (size_t r = 0; r < numRects; ++r) {
1443 // We intentionally flip top and bottom here, since because they're
1444 // specified with a bottom-left origin, top > bottom, which fails
1445 // validation in the Region class. We will fix this up when we flip to a
1446 // top-left origin in queueBuffer.
1447 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1448 mDirtyRegion.orSelf(rect);
1449 }
1450}
1451
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001452// ----------------------------------------------------------------------
1453// the lock/unlock APIs must be used from the same thread
1454
1455static status_t copyBlt(
1456 const sp<GraphicBuffer>& dst,
1457 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001458 const Region& reg,
1459 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001460{
1461 // src and dst with, height and format must be identical. no verification
1462 // is done here.
1463 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001464 uint8_t* src_bits = NULL;
1465 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1466 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001467 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1468
1469 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001470 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1471 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001472 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001473 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001474
1475 Region::const_iterator head(reg.begin());
1476 Region::const_iterator tail(reg.end());
1477 if (head != tail && src_bits && dst_bits) {
1478 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001479 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1480 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001481
1482 while (head != tail) {
1483 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001484 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001485 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001486 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1487 uint8_t const * s = src_bits +
1488 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1489 uint8_t * d = dst_bits +
1490 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001491 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001492 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001493 h = 1;
1494 }
1495 do {
1496 memcpy(d, s, size);
1497 d += dbpr;
1498 s += sbpr;
1499 } while (--h > 0);
1500 }
1501 }
1502
1503 if (src_bits)
1504 src->unlock();
1505
1506 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001507 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001508
1509 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510}
1511
Mathias Agopiana138f892010-05-21 17:24:35 -07001512// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001513
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001514status_t Surface::lock(
1515 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1516{
1517 if (mLockedBuffer != 0) {
1518 ALOGE("Surface::lock failed, already locked");
1519 return INVALID_OPERATION;
1520 }
1521
1522 if (!mConnectedToCpu) {
1523 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1524 if (err) {
1525 return err;
1526 }
1527 // we're intending to do software rendering from this point
1528 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1529 }
1530
1531 ANativeWindowBuffer* out;
1532 int fenceFd = -1;
1533 status_t err = dequeueBuffer(&out, &fenceFd);
1534 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1535 if (err == NO_ERROR) {
1536 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001537 const Rect bounds(backBuffer->width, backBuffer->height);
1538
1539 Region newDirtyRegion;
1540 if (inOutDirtyBounds) {
1541 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1542 newDirtyRegion.andSelf(bounds);
1543 } else {
1544 newDirtyRegion.set(bounds);
1545 }
1546
1547 // figure out if we can copy the frontbuffer back
1548 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1549 const bool canCopyBack = (frontBuffer != 0 &&
1550 backBuffer->width == frontBuffer->width &&
1551 backBuffer->height == frontBuffer->height &&
1552 backBuffer->format == frontBuffer->format);
1553
1554 if (canCopyBack) {
1555 // copy the area that is invalid and not repainted this round
1556 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001557 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001558 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001559 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001560 } else {
1561 // if we can't copy-back anything, modify the user's dirty
1562 // region to make sure they redraw the whole buffer
1563 newDirtyRegion.set(bounds);
1564 mDirtyRegion.clear();
1565 Mutex::Autolock lock(mMutex);
1566 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1567 mSlots[i].dirtyRegion.clear();
1568 }
1569 }
1570
1571
1572 { // scope for the lock
1573 Mutex::Autolock lock(mMutex);
1574 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1575 if (backBufferSlot >= 0) {
1576 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1577 mDirtyRegion.subtract(dirtyRegion);
1578 dirtyRegion = newDirtyRegion;
1579 }
1580 }
1581
1582 mDirtyRegion.orSelf(newDirtyRegion);
1583 if (inOutDirtyBounds) {
1584 *inOutDirtyBounds = newDirtyRegion.getBounds();
1585 }
1586
1587 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001588 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001589 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001590 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001591
1592 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1593 backBuffer->handle);
1594
1595 if (res != 0) {
1596 err = INVALID_OPERATION;
1597 } else {
1598 mLockedBuffer = backBuffer;
1599 outBuffer->width = backBuffer->width;
1600 outBuffer->height = backBuffer->height;
1601 outBuffer->stride = backBuffer->stride;
1602 outBuffer->format = backBuffer->format;
1603 outBuffer->bits = vaddr;
1604 }
1605 }
1606 return err;
1607}
1608
1609status_t Surface::unlockAndPost()
1610{
1611 if (mLockedBuffer == 0) {
1612 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1613 return INVALID_OPERATION;
1614 }
1615
Francis Hart8f396012014-04-01 15:30:53 +03001616 int fd = -1;
1617 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001618 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1619
Francis Hart8f396012014-04-01 15:30:53 +03001620 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001621 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1622 mLockedBuffer->handle, strerror(-err));
1623
1624 mPostedBuffer = mLockedBuffer;
1625 mLockedBuffer = 0;
1626 return err;
1627}
1628
Robert Carr9f31e292016-04-11 11:15:32 -07001629bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1630 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001631 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001632 return true;
1633 }
1634 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1635}
1636
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001637status_t Surface::getUniqueId(uint64_t* outId) const {
1638 Mutex::Autolock lock(mMutex);
1639 return mGraphicBufferProducer->getUniqueId(outId);
1640}
1641
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001642status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1643 if (out == nullptr) {
1644 ALOGE("%s: out must not be null!", __FUNCTION__);
1645 return BAD_VALUE;
1646 }
1647
1648 Mutex::Autolock lock(mMutex);
1649 *out = mRemovedBuffers;
1650 mRemovedBuffers.clear();
1651 return OK;
1652}
1653
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654}; // namespace android