blob: ad1984a5072791b8602fd4e302ac9470e3c0bbc6 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040namespace android {
41
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070043 const sp<IGraphicBufferProducer>& bufferProducer,
44 bool controlledByApp)
Dan Stoza812ed062015-06-02 15:45:22 -070045 : mGraphicBufferProducer(bufferProducer),
Pablo Ceballos60d69222015-08-07 14:47:20 -070046 mCrop(Rect::EMPTY_RECT),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080047 mGenerationNumber(0),
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070048 mSharedBufferMode(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080049 mAutoRefresh(false),
50 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
Pablo Ceballosbc8c1922016-07-01 14:15:41 -070051 mSharedBufferHasBeenQueued(false),
Brian Anderson069b3652016-07-22 10:32:47 -070052 mQueriedSupportedTimestamps(false),
53 mFrameTimestampsSupportsPresent(false),
Brian Anderson7c3ba8a2016-07-25 12:48:08 -070054 mFrameTimestampsSupportsRetire(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,
202 const nsecs_t* outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700203 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700204 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700205 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
206 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700207 !e->hasFirstRefreshStartInfo();
Brian Andersonb04c6f02016-10-21 12:57:46 -0700208 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700209 !e->hasGpuCompositionDoneInfo();
210 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
211 !e->hasDisplayPresentInfo();
212
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700213 // LastRefreshStart, DisplayRetire, DequeueReady, and Release are never
214 // available for the last frame.
215 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
216 !e->hasLastRefreshStartInfo() &&
217 (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700218 bool checkForDisplayRetire = (outDisplayRetireTime != nullptr) &&
219 !e->hasDisplayRetireInfo() && (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700220 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
221 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700222 bool checkForRelease = (outReleaseTime != nullptr) &&
223 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
224
225 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700226 return checkForLatch || checkForFirstRefreshStart ||
Brian Andersonb04c6f02016-10-21 12:57:46 -0700227 checkForLastRefreshStart || checkForGpuCompositionDone ||
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700228 checkForDisplayPresent || checkForDisplayRetire ||
229 checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700230}
231
Brian Anderson3d4039d2016-09-23 16:31:30 -0700232static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
233 if (dst != nullptr) {
Brian Andersoned816e62016-10-26 16:12:21 -0700234 *dst = FrameEvents::isValidTimestamp(src) ? src : 0;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700235 }
236}
237
238static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) {
239 if (dst != nullptr) {
240 nsecs_t signalTime = src->getSignalTime();
241 *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0;
242 }
243}
244
Brian Anderson069b3652016-07-22 10:32:47 -0700245status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700246 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700247 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700248 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700249 nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700250 nsecs_t* outDequeueReadyTime, nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800251 ATRACE_CALL();
252
Brian Anderson3890c392016-07-25 12:48:08 -0700253 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700254
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700255 if (!mEnableFrameTimestamps) {
256 return INVALID_OPERATION;
257 }
258
Brian Anderson3890c392016-07-25 12:48:08 -0700259 // Verify the requested timestamps are supported.
260 querySupportedTimestampsLocked();
261 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
262 return BAD_VALUE;
263 }
264 if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
265 return BAD_VALUE;
Brian Anderson069b3652016-07-22 10:32:47 -0700266 }
267
Brian Anderson3da8d272016-07-28 16:20:47 -0700268 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700269 if (events == nullptr) {
270 // If the entry isn't available in the producer, it's definitely not
271 // available in the consumer.
272 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700273 }
274
Brian Anderson50143b32016-09-30 14:01:24 -0700275 // Update our cache of events if the requested events are not available.
276 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700277 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700278 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700279 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700280 FrameEventHistoryDelta delta;
281 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700282 mFrameEventHistory->applyDelta(delta);
283 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700284 }
285
Brian Anderson3890c392016-07-25 12:48:08 -0700286 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700287 // The entry was available before the update, but was overwritten
288 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700289 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800290 }
Brian Anderson069b3652016-07-22 10:32:47 -0700291
Brian Anderson3d4039d2016-09-23 16:31:30 -0700292 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700293 getFrameTimestamp(outLatchTime, events->latchTime);
294 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
295 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
296 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700297
Brian Anderson3d4039d2016-09-23 16:31:30 -0700298 getFrameTimestampFence(outAcquireTime, events->acquireFence);
299 getFrameTimestampFence(
Brian Andersonb04c6f02016-10-21 12:57:46 -0700300 outGpuCompositionDoneTime, events->gpuCompositionDoneFence);
Brian Anderson3d4039d2016-09-23 16:31:30 -0700301 getFrameTimestampFence(
302 outDisplayPresentTime, events->displayPresentFence);
303 getFrameTimestampFence(outDisplayRetireTime, events->displayRetireFence);
304 getFrameTimestampFence(outReleaseTime, events->releaseFence);
Brian Anderson069b3652016-07-22 10:32:47 -0700305
306 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800307}
308
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700309status_t Surface::getWideColorSupport(bool* supported) {
310 ATRACE_CALL();
311
312 sp<IBinder> display(
313 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
314 Vector<android_color_mode_t> colorModes;
315 status_t err =
316 composerService()->getDisplayColorModes(display, &colorModes);
317
318 if (err)
319 return err;
320
321 *supported = false;
322 for (android_color_mode_t colorMode : colorModes) {
323 switch (colorMode) {
324 case HAL_COLOR_MODE_DISPLAY_P3:
325 case HAL_COLOR_MODE_ADOBE_RGB:
326 case HAL_COLOR_MODE_DCI_P3:
327 *supported = true;
328 break;
329 default:
330 break;
331 }
332 }
333
334 return NO_ERROR;
335}
336
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700337status_t Surface::getHdrSupport(bool* supported) {
338 ATRACE_CALL();
339
340 sp<IBinder> display(
341 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
342 HdrCapabilities hdrCapabilities;
343 status_t err =
344 composerService()->getHdrCapabilities(display, &hdrCapabilities);
345
346 if (err)
347 return err;
348
349 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
350
351 return NO_ERROR;
352}
353
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800354int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
355 Surface* c = getSelf(window);
356 return c->setSwapInterval(interval);
357}
358
359int Surface::hook_dequeueBuffer(ANativeWindow* window,
360 ANativeWindowBuffer** buffer, int* fenceFd) {
361 Surface* c = getSelf(window);
362 return c->dequeueBuffer(buffer, fenceFd);
363}
364
365int Surface::hook_cancelBuffer(ANativeWindow* window,
366 ANativeWindowBuffer* buffer, int fenceFd) {
367 Surface* c = getSelf(window);
368 return c->cancelBuffer(buffer, fenceFd);
369}
370
371int Surface::hook_queueBuffer(ANativeWindow* window,
372 ANativeWindowBuffer* buffer, int fenceFd) {
373 Surface* c = getSelf(window);
374 return c->queueBuffer(buffer, fenceFd);
375}
376
377int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
378 ANativeWindowBuffer** buffer) {
379 Surface* c = getSelf(window);
380 ANativeWindowBuffer* buf;
381 int fenceFd = -1;
382 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600383 if (result != OK) {
384 return result;
385 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800386 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700387 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800388 if (waitResult != OK) {
389 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
390 waitResult);
391 c->cancelBuffer(buf, -1);
392 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700393 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800394 *buffer = buf;
395 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700396}
397
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800398int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
399 ANativeWindowBuffer* buffer) {
400 Surface* c = getSelf(window);
401 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700402}
403
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800404int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
405 ANativeWindowBuffer* buffer) {
406 Surface* c = getSelf(window);
407 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700408}
409
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800410int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
411 ANativeWindowBuffer* buffer) {
412 Surface* c = getSelf(window);
413 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700414}
Mathias Agopian62185b72009-04-16 16:19:50 -0700415
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800416int Surface::hook_query(const ANativeWindow* window,
417 int what, int* value) {
418 const Surface* c = getSelf(window);
419 return c->query(what, value);
420}
421
422int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
423 va_list args;
424 va_start(args, operation);
425 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700426 int result = c->perform(operation, args);
427 va_end(args);
428 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800429}
430
431int Surface::setSwapInterval(int interval) {
432 ATRACE_CALL();
433 // EGL specification states:
434 // interval is silently clamped to minimum and maximum implementation
435 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800436
437 if (interval < minSwapInterval)
438 interval = minSwapInterval;
439
440 if (interval > maxSwapInterval)
441 interval = maxSwapInterval;
442
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700443 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700444 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800445
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700446 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800447}
448
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700449int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800450 ATRACE_CALL();
451 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800452
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800453 uint32_t reqWidth;
454 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800455 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800456 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700457 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800458
459 {
460 Mutex::Autolock lock(mMutex);
461
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800462 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
463 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800464
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800465 reqFormat = mReqFormat;
466 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800467
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700468 enableFrameTimestamps = mEnableFrameTimestamps;
469
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700470 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800471 BufferItem::INVALID_BUFFER_SLOT) {
472 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
473 if (gbuf != NULL) {
474 *buffer = gbuf.get();
475 *fenceFd = -1;
476 return OK;
477 }
478 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800479 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
480
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800481 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800482 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700483 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700484
485 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700486 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700487 reqWidth, reqHeight, reqFormat, reqUsage,
488 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700489 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800490
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800491 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700492 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
493 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
494 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800495 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700496 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800497
498 Mutex::Autolock lock(mMutex);
499
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800500 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700501
502 // this should never happen
503 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
504
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800505 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
506 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700507 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700508
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700509 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700510 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700511 }
512
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800513 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
514 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
515 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700516 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700517 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800518 return result;
519 }
520 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700521
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800522 if (fence->isValid()) {
523 *fenceFd = fence->dup();
524 if (*fenceFd == -1) {
525 ALOGE("dequeueBuffer: error duping fence: %d", errno);
526 // dup() should never fail; something is badly wrong. Soldier on
527 // and hope for the best; the worst that should happen is some
528 // visible corruption that lasts until the next frame.
529 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700530 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800531 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700532 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800533
534 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800535
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700536 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800537 mSharedBufferSlot = buf;
538 mSharedBufferHasBeenQueued = false;
539 } else if (mSharedBufferSlot == buf) {
540 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
541 mSharedBufferHasBeenQueued = false;
542 }
543
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800544 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700545}
546
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800547int Surface::cancelBuffer(android_native_buffer_t* buffer,
548 int fenceFd) {
549 ATRACE_CALL();
550 ALOGV("Surface::cancelBuffer");
551 Mutex::Autolock lock(mMutex);
552 int i = getSlotFromBufferLocked(buffer);
553 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900554 if (fenceFd >= 0) {
555 close(fenceFd);
556 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800557 return i;
558 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800559 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
560 if (fenceFd >= 0) {
561 close(fenceFd);
562 }
563 return OK;
564 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800565 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
566 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800567
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700568 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800569 mSharedBufferHasBeenQueued = true;
570 }
571
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800572 return OK;
573}
574
575int Surface::getSlotFromBufferLocked(
576 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800577 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
578 if (mSlots[i].buffer != NULL &&
579 mSlots[i].buffer->handle == buffer->handle) {
580 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700581 }
582 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800583 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
584 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700585}
586
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800587int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800588 ALOGV("Surface::lockBuffer");
589 Mutex::Autolock lock(mMutex);
590 return OK;
591}
Mathias Agopian631f3582010-05-25 17:51:34 -0700592
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800593int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
594 ATRACE_CALL();
595 ALOGV("Surface::queueBuffer");
596 Mutex::Autolock lock(mMutex);
597 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700598 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800599
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800600 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700601 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700602 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700603 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700604 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800605 } else {
606 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700607 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800608 int i = getSlotFromBufferLocked(buffer);
609 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900610 if (fenceFd >= 0) {
611 close(fenceFd);
612 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800613 return i;
614 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800615 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
616 if (fenceFd >= 0) {
617 close(fenceFd);
618 }
619 return OK;
620 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800623 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700624 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800625 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800627 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
628 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700629 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800630 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700631 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700632
Dan Stozac62acbd2015-04-21 16:42:49 -0700633 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700634 input.setSurfaceDamage(Region::INVALID_REGION);
635 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700636 // Here we do two things:
637 // 1) The surface damage was specified using the OpenGL ES convention of
638 // the origin being in the bottom-left corner. Here we flip to the
639 // convention that the rest of the system uses (top-left corner) by
640 // subtracting all top/bottom coordinates from the buffer height.
641 // 2) If the buffer is coming in rotated (for example, because the EGL
642 // implementation is reacting to the transform hint coming back from
643 // SurfaceFlinger), the surface damage needs to be rotated the
644 // opposite direction, since it was generated assuming an unrotated
645 // buffer (the app doesn't know that the EGL implementation is
646 // reacting to the transform hint behind its back). The
647 // transformations in the switch statement below apply those
648 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
649
650 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700651 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700652 bool rotated90 = (mTransform ^ mStickyTransform) &
653 NATIVE_WINDOW_TRANSFORM_ROT_90;
654 if (rotated90) {
655 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700656 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700657
Dan Stoza5065a552015-03-17 16:23:42 -0700658 Region flippedRegion;
659 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700660 int left = rect.left;
661 int right = rect.right;
662 int top = height - rect.bottom; // Flip from OpenGL convention
663 int bottom = height - rect.top; // Flip from OpenGL convention
664 switch (mTransform ^ mStickyTransform) {
665 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
666 // Rotate 270 degrees
667 Rect flippedRect{top, width - right, bottom, width - left};
668 flippedRegion.orSelf(flippedRect);
669 break;
670 }
671 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
672 // Rotate 180 degrees
673 Rect flippedRect{width - right, height - bottom,
674 width - left, height - top};
675 flippedRegion.orSelf(flippedRect);
676 break;
677 }
678 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
679 // Rotate 90 degrees
680 Rect flippedRect{height - bottom, left,
681 height - top, right};
682 flippedRegion.orSelf(flippedRect);
683 break;
684 }
685 default: {
686 Rect flippedRect{left, top, right, bottom};
687 flippedRegion.orSelf(flippedRect);
688 break;
689 }
690 }
Dan Stoza5065a552015-03-17 16:23:42 -0700691 }
692
693 input.setSurfaceDamage(flippedRegion);
694 }
695
Dan Stoza70ccba52016-07-01 14:00:40 -0700696 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800697 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700698 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800699 if (err != OK) {
700 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
701 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800702
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700703 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700704 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700705 // Update timestamps with the local acquire fence.
706 // The consumer doesn't send it back to prevent us from having two
707 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700708 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700709 std::make_shared<FenceTime>(std::move(fence)));
710
711 // Cache timestamps of signaled fences so we can close their file
712 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700713 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700714 }
715
Brian Anderson50143b32016-09-30 14:01:24 -0700716 mLastFrameNumber = mNextFrameNumber;
717
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700718 mDefaultWidth = output.width;
719 mDefaultHeight = output.height;
720 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700721
Ruben Brunk1681d952014-06-27 15:51:55 -0700722 // Disable transform hint if sticky transform is set.
723 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700724 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700725 }
726
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700727 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700728
Dan Stozac62acbd2015-04-21 16:42:49 -0700729 if (!mConnectedToCpu) {
730 // Clear surface damage back to full-buffer
731 mDirtyRegion = Region::INVALID_REGION;
732 }
Dan Stoza5065a552015-03-17 16:23:42 -0700733
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700734 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800735 mSharedBufferHasBeenQueued = true;
736 }
737
Robert Carr9f31e292016-04-11 11:15:32 -0700738 mQueueBufferCondition.broadcast();
739
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800740 return err;
741}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700742
Brian Anderson069b3652016-07-22 10:32:47 -0700743void Surface::querySupportedTimestampsLocked() const {
744 // mMutex must be locked when calling this method.
745
746 if (mQueriedSupportedTimestamps) {
747 return;
748 }
749 mQueriedSupportedTimestamps = true;
750
Brian Anderson3890c392016-07-25 12:48:08 -0700751 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson3da8d272016-07-28 16:20:47 -0700752 status_t err = composerService()->getSupportedFrameTimestamps(
Brian Anderson069b3652016-07-22 10:32:47 -0700753 &supportedFrameTimestamps);
754
755 if (err != NO_ERROR) {
756 return;
757 }
758
759 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700760 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700761 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700762 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700763 mFrameTimestampsSupportsRetire = true;
764 }
765 }
766}
767
Mathias Agopiana67932f2011-04-20 14:20:59 -0700768int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800769 ATRACE_CALL();
770 ALOGV("Surface::query");
771 { // scope for the lock
772 Mutex::Autolock lock(mMutex);
773 switch (what) {
774 case NATIVE_WINDOW_FORMAT:
775 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800776 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800777 return NO_ERROR;
778 }
779 break;
780 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700781 if (composerService()->authenticateSurfaceTexture(
782 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800783 *value = 1;
784 } else {
785 *value = 0;
786 }
787 return NO_ERROR;
788 }
789 case NATIVE_WINDOW_CONCRETE_TYPE:
790 *value = NATIVE_WINDOW_SURFACE;
791 return NO_ERROR;
792 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800793 *value = static_cast<int>(
794 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800795 return NO_ERROR;
796 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800797 *value = static_cast<int>(
798 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800799 return NO_ERROR;
800 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800801 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800802 return NO_ERROR;
803 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
804 status_t err = NO_ERROR;
805 if (!mConsumerRunningBehind) {
806 *value = 0;
807 } else {
808 err = mGraphicBufferProducer->query(what, value);
809 if (err == NO_ERROR) {
810 mConsumerRunningBehind = *value;
811 }
812 }
813 return err;
814 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700815 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
816 int64_t durationUs = mLastDequeueDuration / 1000;
817 *value = durationUs > std::numeric_limits<int>::max() ?
818 std::numeric_limits<int>::max() :
819 static_cast<int>(durationUs);
820 return NO_ERROR;
821 }
822 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
823 int64_t durationUs = mLastQueueDuration / 1000;
824 *value = durationUs > std::numeric_limits<int>::max() ?
825 std::numeric_limits<int>::max() :
826 static_cast<int>(durationUs);
827 return NO_ERROR;
828 }
Brian Anderson069b3652016-07-22 10:32:47 -0700829 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
830 querySupportedTimestampsLocked();
831 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
832 return NO_ERROR;
833 }
834 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
835 querySupportedTimestampsLocked();
836 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
837 return NO_ERROR;
838 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800839 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700840 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800841 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800842}
843
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800844int Surface::perform(int operation, va_list args)
845{
846 int res = NO_ERROR;
847 switch (operation) {
848 case NATIVE_WINDOW_CONNECT:
849 // deprecated. must return NO_ERROR.
850 break;
851 case NATIVE_WINDOW_DISCONNECT:
852 // deprecated. must return NO_ERROR.
853 break;
854 case NATIVE_WINDOW_SET_USAGE:
855 res = dispatchSetUsage(args);
856 break;
857 case NATIVE_WINDOW_SET_CROP:
858 res = dispatchSetCrop(args);
859 break;
860 case NATIVE_WINDOW_SET_BUFFER_COUNT:
861 res = dispatchSetBufferCount(args);
862 break;
863 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
864 res = dispatchSetBuffersGeometry(args);
865 break;
866 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
867 res = dispatchSetBuffersTransform(args);
868 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700869 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
870 res = dispatchSetBuffersStickyTransform(args);
871 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800872 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
873 res = dispatchSetBuffersTimestamp(args);
874 break;
875 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
876 res = dispatchSetBuffersDimensions(args);
877 break;
878 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
879 res = dispatchSetBuffersUserDimensions(args);
880 break;
881 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
882 res = dispatchSetBuffersFormat(args);
883 break;
884 case NATIVE_WINDOW_LOCK:
885 res = dispatchLock(args);
886 break;
887 case NATIVE_WINDOW_UNLOCK_AND_POST:
888 res = dispatchUnlockAndPost(args);
889 break;
890 case NATIVE_WINDOW_SET_SCALING_MODE:
891 res = dispatchSetScalingMode(args);
892 break;
893 case NATIVE_WINDOW_API_CONNECT:
894 res = dispatchConnect(args);
895 break;
896 case NATIVE_WINDOW_API_DISCONNECT:
897 res = dispatchDisconnect(args);
898 break;
Rachad7cb0d392014-07-29 17:53:53 -0700899 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
900 res = dispatchSetSidebandStream(args);
901 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800902 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
903 res = dispatchSetBuffersDataSpace(args);
904 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700905 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
906 res = dispatchSetSurfaceDamage(args);
907 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700908 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
909 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700910 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800911 case NATIVE_WINDOW_SET_AUTO_REFRESH:
912 res = dispatchSetAutoRefresh(args);
913 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800914 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
915 res = dispatchGetDisplayRefreshCycleDuration(args);
916 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800917 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
918 res = dispatchGetNextFrameId(args);
919 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700920 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
921 res = dispatchEnableFrameTimestamps(args);
922 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800923 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
924 res = dispatchGetCompositorTiming(args);
925 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800926 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
927 res = dispatchGetFrameTimestamps(args);
928 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700929 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
930 res = dispatchGetWideColorSupport(args);
931 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700932 case NATIVE_WINDOW_GET_HDR_SUPPORT:
933 res = dispatchGetHdrSupport(args);
934 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800935 default:
936 res = NAME_NOT_FOUND;
937 break;
938 }
939 return res;
940}
Mathias Agopiana138f892010-05-21 17:24:35 -0700941
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800942int Surface::dispatchConnect(va_list args) {
943 int api = va_arg(args, int);
944 return connect(api);
945}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800946
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800947int Surface::dispatchDisconnect(va_list args) {
948 int api = va_arg(args, int);
949 return disconnect(api);
950}
951
952int Surface::dispatchSetUsage(va_list args) {
953 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800954 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800955}
956
957int Surface::dispatchSetCrop(va_list args) {
958 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
959 return setCrop(reinterpret_cast<Rect const*>(rect));
960}
961
962int Surface::dispatchSetBufferCount(va_list args) {
963 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800964 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800965}
966
967int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800968 uint32_t width = va_arg(args, uint32_t);
969 uint32_t height = va_arg(args, uint32_t);
970 PixelFormat format = va_arg(args, PixelFormat);
971 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800972 if (err != 0) {
973 return err;
974 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800975 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800976}
977
978int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800979 uint32_t width = va_arg(args, uint32_t);
980 uint32_t height = va_arg(args, uint32_t);
981 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800982}
983
984int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800985 uint32_t width = va_arg(args, uint32_t);
986 uint32_t height = va_arg(args, uint32_t);
987 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800988}
989
990int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800991 PixelFormat format = va_arg(args, PixelFormat);
992 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800993}
994
995int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800996 int mode = va_arg(args, int);
997 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800998}
999
1000int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001001 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001002 return setBuffersTransform(transform);
1003}
1004
Ruben Brunk1681d952014-06-27 15:51:55 -07001005int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001006 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -07001007 return setBuffersStickyTransform(transform);
1008}
1009
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001010int Surface::dispatchSetBuffersTimestamp(va_list args) {
1011 int64_t timestamp = va_arg(args, int64_t);
1012 return setBuffersTimestamp(timestamp);
1013}
1014
1015int Surface::dispatchLock(va_list args) {
1016 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1017 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1018 return lock(outBuffer, inOutDirtyBounds);
1019}
1020
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001021int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001022 return unlockAndPost();
1023}
1024
Rachad7cb0d392014-07-29 17:53:53 -07001025int Surface::dispatchSetSidebandStream(va_list args) {
1026 native_handle_t* sH = va_arg(args, native_handle_t*);
1027 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1028 setSidebandStream(sidebandHandle);
1029 return OK;
1030}
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001031
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001032int Surface::dispatchSetBuffersDataSpace(va_list args) {
1033 android_dataspace dataspace =
1034 static_cast<android_dataspace>(va_arg(args, int));
1035 return setBuffersDataSpace(dataspace);
1036}
1037
Dan Stoza5065a552015-03-17 16:23:42 -07001038int Surface::dispatchSetSurfaceDamage(va_list args) {
1039 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1040 size_t numRects = va_arg(args, size_t);
1041 setSurfaceDamage(rects, numRects);
1042 return NO_ERROR;
1043}
1044
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001045int Surface::dispatchSetSharedBufferMode(va_list args) {
1046 bool sharedBufferMode = va_arg(args, int);
1047 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001048}
1049
1050int Surface::dispatchSetAutoRefresh(va_list args) {
1051 bool autoRefresh = va_arg(args, int);
1052 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001053}
1054
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001055int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1056 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1057 return getDisplayRefreshCycleDuration(outRefreshDuration);
1058}
1059
Brian Anderson1049d1d2016-12-16 17:25:57 -08001060int Surface::dispatchGetNextFrameId(va_list args) {
1061 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1062 *nextFrameId = getNextFrameNumber();
1063 return NO_ERROR;
1064}
1065
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001066int Surface::dispatchEnableFrameTimestamps(va_list args) {
1067 bool enable = va_arg(args, int);
1068 enableFrameTimestamps(enable);
1069 return NO_ERROR;
1070}
1071
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001072int Surface::dispatchGetCompositorTiming(va_list args) {
1073 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1074 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1075 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1076 return getCompositorTiming(compositeDeadline, compositeInterval,
1077 compositeToPresentLatency);
1078}
1079
Pablo Ceballosce796e72016-02-04 19:10:51 -08001080int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001081 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001082 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001083 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001084 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1085 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1086 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001087 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001088 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001089 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001090 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001091 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001092 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001093 outRequestedPresentTime, outAcquireTime, outLatchTime,
1094 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001095 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001096 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001097}
1098
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001099int Surface::dispatchGetWideColorSupport(va_list args) {
1100 bool* outSupport = va_arg(args, bool*);
1101 return getWideColorSupport(outSupport);
1102}
1103
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001104int Surface::dispatchGetHdrSupport(va_list args) {
1105 bool* outSupport = va_arg(args, bool*);
1106 return getHdrSupport(outSupport);
1107}
1108
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001109int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001110 static sp<IProducerListener> listener = new DummyProducerListener();
1111 return connect(api, listener);
1112}
1113
1114int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001115 ATRACE_CALL();
1116 ALOGV("Surface::connect");
1117 Mutex::Autolock lock(mMutex);
1118 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001119 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001120 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001121 mDefaultWidth = output.width;
1122 mDefaultHeight = output.height;
1123 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001124
1125 // Disable transform hint if sticky transform is set.
1126 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001127 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001128 }
1129
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001130 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001131 }
1132 if (!err && api == NATIVE_WINDOW_API_CPU) {
1133 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001134 // Clear the dirty region in case we're switching from a non-CPU API
1135 mDirtyRegion.clear();
1136 } else if (!err) {
1137 // Initialize the dirty region for tracking surface damage
1138 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001139 }
Dan Stoza5065a552015-03-17 16:23:42 -07001140
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001141 return err;
1142}
1143
Mathias Agopian365857d2013-09-11 19:35:45 -07001144
Robert Carr97b9c862016-09-08 13:54:35 -07001145int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001146 ATRACE_CALL();
1147 ALOGV("Surface::disconnect");
1148 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001149 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1150 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001151 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001152 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001153 if (!err) {
1154 mReqFormat = 0;
1155 mReqWidth = 0;
1156 mReqHeight = 0;
1157 mReqUsage = 0;
1158 mCrop.clear();
1159 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1160 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001161 mStickyTransform = 0;
1162
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001163 if (api == NATIVE_WINDOW_API_CPU) {
1164 mConnectedToCpu = false;
1165 }
1166 }
1167 return err;
1168}
1169
Dan Stozad9c49712015-04-27 11:06:01 -07001170int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001171 sp<Fence>* outFence) {
1172 ATRACE_CALL();
1173 ALOGV("Surface::detachNextBuffer");
1174
1175 if (outBuffer == NULL || outFence == NULL) {
1176 return BAD_VALUE;
1177 }
1178
1179 Mutex::Autolock lock(mMutex);
1180
1181 sp<GraphicBuffer> buffer(NULL);
1182 sp<Fence> fence(NULL);
1183 status_t result = mGraphicBufferProducer->detachNextBuffer(
1184 &buffer, &fence);
1185 if (result != NO_ERROR) {
1186 return result;
1187 }
1188
Dan Stozad9c49712015-04-27 11:06:01 -07001189 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001190 if (fence != NULL && fence->isValid()) {
1191 *outFence = fence;
1192 } else {
1193 *outFence = Fence::NO_FENCE;
1194 }
1195
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001196 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1197 if (mSlots[i].buffer != NULL &&
1198 mSlots[i].buffer->handle == buffer->handle) {
1199 mSlots[i].buffer = NULL;
1200 }
1201 }
1202
Dan Stoza231832e2015-03-11 11:55:01 -07001203 return NO_ERROR;
1204}
1205
1206int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1207{
1208 ATRACE_CALL();
1209 ALOGV("Surface::attachBuffer");
1210
1211 Mutex::Autolock lock(mMutex);
1212
1213 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001214 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1215 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001216 int32_t attachedSlot = -1;
1217 status_t result = mGraphicBufferProducer->attachBuffer(
1218 &attachedSlot, graphicBuffer);
1219 if (result != NO_ERROR) {
1220 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001221 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001222 return result;
1223 }
1224 mSlots[attachedSlot].buffer = graphicBuffer;
1225
1226 return NO_ERROR;
1227}
1228
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001229int Surface::setUsage(uint32_t reqUsage)
1230{
1231 ALOGV("Surface::setUsage");
1232 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001233 if (reqUsage != mReqUsage) {
1234 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1235 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001236 mReqUsage = reqUsage;
1237 return OK;
1238}
1239
1240int Surface::setCrop(Rect const* rect)
1241{
1242 ATRACE_CALL();
1243
Pablo Ceballos60d69222015-08-07 14:47:20 -07001244 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001245 if (rect == NULL || rect->isEmpty()) {
1246 realRect.clear();
1247 } else {
1248 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001249 }
1250
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001251 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1252 realRect.left, realRect.top, realRect.right, realRect.bottom);
1253
1254 Mutex::Autolock lock(mMutex);
1255 mCrop = realRect;
1256 return NO_ERROR;
1257}
1258
1259int Surface::setBufferCount(int bufferCount)
1260{
1261 ATRACE_CALL();
1262 ALOGV("Surface::setBufferCount");
1263 Mutex::Autolock lock(mMutex);
1264
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001265 status_t err = NO_ERROR;
1266 if (bufferCount == 0) {
1267 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1268 } else {
1269 int minUndequeuedBuffers = 0;
1270 err = mGraphicBufferProducer->query(
1271 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1272 if (err == NO_ERROR) {
1273 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1274 bufferCount - minUndequeuedBuffers);
1275 }
1276 }
Mathias Agopian90147262010-01-22 11:47:55 -08001277
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001278 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1279 bufferCount, strerror(-err));
1280
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001281 return err;
1282}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001283
Pablo Ceballosfa455352015-08-12 17:47:47 -07001284int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1285 ATRACE_CALL();
1286 ALOGV("Surface::setMaxDequeuedBufferCount");
1287 Mutex::Autolock lock(mMutex);
1288
1289 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1290 maxDequeuedBuffers);
1291 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1292 "returned %s", maxDequeuedBuffers, strerror(-err));
1293
Pablo Ceballosfa455352015-08-12 17:47:47 -07001294 return err;
1295}
1296
1297int Surface::setAsyncMode(bool async) {
1298 ATRACE_CALL();
1299 ALOGV("Surface::setAsyncMode");
1300 Mutex::Autolock lock(mMutex);
1301
1302 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1303 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1304 async, strerror(-err));
1305
Pablo Ceballosfa455352015-08-12 17:47:47 -07001306 return err;
1307}
1308
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001309int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001310 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001311 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001312 Mutex::Autolock lock(mMutex);
1313
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001314 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1315 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001316 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001317 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001318 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001319 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1320 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001321
1322 return err;
1323}
1324
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001325int Surface::setAutoRefresh(bool autoRefresh) {
1326 ATRACE_CALL();
1327 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1328 Mutex::Autolock lock(mMutex);
1329
1330 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1331 if (err == NO_ERROR) {
1332 mAutoRefresh = autoRefresh;
1333 }
1334 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1335 autoRefresh, strerror(-err));
1336 return err;
1337}
1338
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001339int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001340{
1341 ATRACE_CALL();
1342 ALOGV("Surface::setBuffersDimensions");
1343
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001344 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001345 return BAD_VALUE;
1346
1347 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001348 if (width != mReqWidth || height != mReqHeight) {
1349 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1350 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001351 mReqWidth = width;
1352 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001353 return NO_ERROR;
1354}
1355
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001356int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001357{
1358 ATRACE_CALL();
1359 ALOGV("Surface::setBuffersUserDimensions");
1360
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001361 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001362 return BAD_VALUE;
1363
1364 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001365 if (width != mUserWidth || height != mUserHeight) {
1366 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1367 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001368 mUserWidth = width;
1369 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001370 return NO_ERROR;
1371}
1372
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001373int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001374{
1375 ALOGV("Surface::setBuffersFormat");
1376
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001377 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001378 if (format != mReqFormat) {
1379 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1380 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001381 mReqFormat = format;
1382 return NO_ERROR;
1383}
1384
1385int Surface::setScalingMode(int mode)
1386{
1387 ATRACE_CALL();
1388 ALOGV("Surface::setScalingMode(%d)", mode);
1389
1390 switch (mode) {
1391 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1392 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1393 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001394 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001395 break;
1396 default:
1397 ALOGE("unknown scaling mode: %d", mode);
1398 return BAD_VALUE;
1399 }
1400
1401 Mutex::Autolock lock(mMutex);
1402 mScalingMode = mode;
1403 return NO_ERROR;
1404}
1405
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001406int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001407{
1408 ATRACE_CALL();
1409 ALOGV("Surface::setBuffersTransform");
1410 Mutex::Autolock lock(mMutex);
1411 mTransform = transform;
1412 return NO_ERROR;
1413}
1414
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001415int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001416{
1417 ATRACE_CALL();
1418 ALOGV("Surface::setBuffersStickyTransform");
1419 Mutex::Autolock lock(mMutex);
1420 mStickyTransform = transform;
1421 return NO_ERROR;
1422}
1423
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001424int Surface::setBuffersTimestamp(int64_t timestamp)
1425{
1426 ALOGV("Surface::setBuffersTimestamp");
1427 Mutex::Autolock lock(mMutex);
1428 mTimestamp = timestamp;
1429 return NO_ERROR;
1430}
1431
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001432int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1433{
1434 ALOGV("Surface::setBuffersDataSpace");
1435 Mutex::Autolock lock(mMutex);
1436 mDataSpace = dataSpace;
1437 return NO_ERROR;
1438}
1439
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001440void Surface::freeAllBuffers() {
1441 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1442 mSlots[i].buffer = 0;
1443 }
1444}
1445
Dan Stoza5065a552015-03-17 16:23:42 -07001446void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1447 ATRACE_CALL();
1448 ALOGV("Surface::setSurfaceDamage");
1449 Mutex::Autolock lock(mMutex);
1450
Dan Stozac62acbd2015-04-21 16:42:49 -07001451 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001452 mDirtyRegion = Region::INVALID_REGION;
1453 return;
1454 }
1455
1456 mDirtyRegion.clear();
1457 for (size_t r = 0; r < numRects; ++r) {
1458 // We intentionally flip top and bottom here, since because they're
1459 // specified with a bottom-left origin, top > bottom, which fails
1460 // validation in the Region class. We will fix this up when we flip to a
1461 // top-left origin in queueBuffer.
1462 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1463 mDirtyRegion.orSelf(rect);
1464 }
1465}
1466
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001467// ----------------------------------------------------------------------
1468// the lock/unlock APIs must be used from the same thread
1469
1470static status_t copyBlt(
1471 const sp<GraphicBuffer>& dst,
1472 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001473 const Region& reg,
1474 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001475{
1476 // src and dst with, height and format must be identical. no verification
1477 // is done here.
1478 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001479 uint8_t* src_bits = NULL;
1480 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1481 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001482 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1483
1484 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001485 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1486 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001487 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001488 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001489
1490 Region::const_iterator head(reg.begin());
1491 Region::const_iterator tail(reg.end());
1492 if (head != tail && src_bits && dst_bits) {
1493 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001494 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1495 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001496
1497 while (head != tail) {
1498 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001499 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001500 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001501 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1502 uint8_t const * s = src_bits +
1503 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1504 uint8_t * d = dst_bits +
1505 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001506 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001507 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001508 h = 1;
1509 }
1510 do {
1511 memcpy(d, s, size);
1512 d += dbpr;
1513 s += sbpr;
1514 } while (--h > 0);
1515 }
1516 }
1517
1518 if (src_bits)
1519 src->unlock();
1520
1521 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001522 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001523
1524 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525}
1526
Mathias Agopiana138f892010-05-21 17:24:35 -07001527// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001528
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001529status_t Surface::lock(
1530 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1531{
1532 if (mLockedBuffer != 0) {
1533 ALOGE("Surface::lock failed, already locked");
1534 return INVALID_OPERATION;
1535 }
1536
1537 if (!mConnectedToCpu) {
1538 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1539 if (err) {
1540 return err;
1541 }
1542 // we're intending to do software rendering from this point
1543 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1544 }
1545
1546 ANativeWindowBuffer* out;
1547 int fenceFd = -1;
1548 status_t err = dequeueBuffer(&out, &fenceFd);
1549 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1550 if (err == NO_ERROR) {
1551 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001552 const Rect bounds(backBuffer->width, backBuffer->height);
1553
1554 Region newDirtyRegion;
1555 if (inOutDirtyBounds) {
1556 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1557 newDirtyRegion.andSelf(bounds);
1558 } else {
1559 newDirtyRegion.set(bounds);
1560 }
1561
1562 // figure out if we can copy the frontbuffer back
1563 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1564 const bool canCopyBack = (frontBuffer != 0 &&
1565 backBuffer->width == frontBuffer->width &&
1566 backBuffer->height == frontBuffer->height &&
1567 backBuffer->format == frontBuffer->format);
1568
1569 if (canCopyBack) {
1570 // copy the area that is invalid and not repainted this round
1571 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001572 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001573 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001574 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001575 } else {
1576 // if we can't copy-back anything, modify the user's dirty
1577 // region to make sure they redraw the whole buffer
1578 newDirtyRegion.set(bounds);
1579 mDirtyRegion.clear();
1580 Mutex::Autolock lock(mMutex);
1581 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1582 mSlots[i].dirtyRegion.clear();
1583 }
1584 }
1585
1586
1587 { // scope for the lock
1588 Mutex::Autolock lock(mMutex);
1589 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1590 if (backBufferSlot >= 0) {
1591 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1592 mDirtyRegion.subtract(dirtyRegion);
1593 dirtyRegion = newDirtyRegion;
1594 }
1595 }
1596
1597 mDirtyRegion.orSelf(newDirtyRegion);
1598 if (inOutDirtyBounds) {
1599 *inOutDirtyBounds = newDirtyRegion.getBounds();
1600 }
1601
1602 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001603 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001604 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001605 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001606
1607 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1608 backBuffer->handle);
1609
1610 if (res != 0) {
1611 err = INVALID_OPERATION;
1612 } else {
1613 mLockedBuffer = backBuffer;
1614 outBuffer->width = backBuffer->width;
1615 outBuffer->height = backBuffer->height;
1616 outBuffer->stride = backBuffer->stride;
1617 outBuffer->format = backBuffer->format;
1618 outBuffer->bits = vaddr;
1619 }
1620 }
1621 return err;
1622}
1623
1624status_t Surface::unlockAndPost()
1625{
1626 if (mLockedBuffer == 0) {
1627 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1628 return INVALID_OPERATION;
1629 }
1630
Francis Hart8f396012014-04-01 15:30:53 +03001631 int fd = -1;
1632 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001633 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1634
Francis Hart8f396012014-04-01 15:30:53 +03001635 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001636 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1637 mLockedBuffer->handle, strerror(-err));
1638
1639 mPostedBuffer = mLockedBuffer;
1640 mLockedBuffer = 0;
1641 return err;
1642}
1643
Robert Carr9f31e292016-04-11 11:15:32 -07001644bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1645 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001646 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001647 return true;
1648 }
1649 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1650}
1651
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001652status_t Surface::getUniqueId(uint64_t* outId) const {
1653 Mutex::Autolock lock(mMutex);
1654 return mGraphicBufferProducer->getUniqueId(outId);
1655}
1656
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657}; // namespace android