blob: a2e12f7735b285c4e38ce0a1922b46e8f13bcb1b [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 Agopian10e9ab52017-03-08 15:02:55 -0800839 case NATIVE_WINDOW_IS_VALID: {
840 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
841 return NO_ERROR;
842 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800843 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700844 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800845 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800846}
847
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800848int Surface::perform(int operation, va_list args)
849{
850 int res = NO_ERROR;
851 switch (operation) {
852 case NATIVE_WINDOW_CONNECT:
853 // deprecated. must return NO_ERROR.
854 break;
855 case NATIVE_WINDOW_DISCONNECT:
856 // deprecated. must return NO_ERROR.
857 break;
858 case NATIVE_WINDOW_SET_USAGE:
859 res = dispatchSetUsage(args);
860 break;
861 case NATIVE_WINDOW_SET_CROP:
862 res = dispatchSetCrop(args);
863 break;
864 case NATIVE_WINDOW_SET_BUFFER_COUNT:
865 res = dispatchSetBufferCount(args);
866 break;
867 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
868 res = dispatchSetBuffersGeometry(args);
869 break;
870 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
871 res = dispatchSetBuffersTransform(args);
872 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700873 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
874 res = dispatchSetBuffersStickyTransform(args);
875 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800876 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
877 res = dispatchSetBuffersTimestamp(args);
878 break;
879 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
880 res = dispatchSetBuffersDimensions(args);
881 break;
882 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
883 res = dispatchSetBuffersUserDimensions(args);
884 break;
885 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
886 res = dispatchSetBuffersFormat(args);
887 break;
888 case NATIVE_WINDOW_LOCK:
889 res = dispatchLock(args);
890 break;
891 case NATIVE_WINDOW_UNLOCK_AND_POST:
892 res = dispatchUnlockAndPost(args);
893 break;
894 case NATIVE_WINDOW_SET_SCALING_MODE:
895 res = dispatchSetScalingMode(args);
896 break;
897 case NATIVE_WINDOW_API_CONNECT:
898 res = dispatchConnect(args);
899 break;
900 case NATIVE_WINDOW_API_DISCONNECT:
901 res = dispatchDisconnect(args);
902 break;
Rachad7cb0d392014-07-29 17:53:53 -0700903 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
904 res = dispatchSetSidebandStream(args);
905 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800906 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
907 res = dispatchSetBuffersDataSpace(args);
908 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700909 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
910 res = dispatchSetSurfaceDamage(args);
911 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700912 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
913 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700914 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800915 case NATIVE_WINDOW_SET_AUTO_REFRESH:
916 res = dispatchSetAutoRefresh(args);
917 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800918 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
919 res = dispatchGetDisplayRefreshCycleDuration(args);
920 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800921 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
922 res = dispatchGetNextFrameId(args);
923 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700924 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
925 res = dispatchEnableFrameTimestamps(args);
926 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800927 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
928 res = dispatchGetCompositorTiming(args);
929 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800930 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
931 res = dispatchGetFrameTimestamps(args);
932 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700933 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
934 res = dispatchGetWideColorSupport(args);
935 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700936 case NATIVE_WINDOW_GET_HDR_SUPPORT:
937 res = dispatchGetHdrSupport(args);
938 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800939 default:
940 res = NAME_NOT_FOUND;
941 break;
942 }
943 return res;
944}
Mathias Agopiana138f892010-05-21 17:24:35 -0700945
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800946int Surface::dispatchConnect(va_list args) {
947 int api = va_arg(args, int);
948 return connect(api);
949}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800950
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800951int Surface::dispatchDisconnect(va_list args) {
952 int api = va_arg(args, int);
953 return disconnect(api);
954}
955
956int Surface::dispatchSetUsage(va_list args) {
957 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800958 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800959}
960
961int Surface::dispatchSetCrop(va_list args) {
962 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
963 return setCrop(reinterpret_cast<Rect const*>(rect));
964}
965
966int Surface::dispatchSetBufferCount(va_list args) {
967 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800968 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800969}
970
971int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800972 uint32_t width = va_arg(args, uint32_t);
973 uint32_t height = va_arg(args, uint32_t);
974 PixelFormat format = va_arg(args, PixelFormat);
975 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800976 if (err != 0) {
977 return err;
978 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800979 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800980}
981
982int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800983 uint32_t width = va_arg(args, uint32_t);
984 uint32_t height = va_arg(args, uint32_t);
985 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800986}
987
988int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800989 uint32_t width = va_arg(args, uint32_t);
990 uint32_t height = va_arg(args, uint32_t);
991 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800992}
993
994int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800995 PixelFormat format = va_arg(args, PixelFormat);
996 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800997}
998
999int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001000 int mode = va_arg(args, int);
1001 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001002}
1003
1004int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001005 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001006 return setBuffersTransform(transform);
1007}
1008
Ruben Brunk1681d952014-06-27 15:51:55 -07001009int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001010 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -07001011 return setBuffersStickyTransform(transform);
1012}
1013
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001014int Surface::dispatchSetBuffersTimestamp(va_list args) {
1015 int64_t timestamp = va_arg(args, int64_t);
1016 return setBuffersTimestamp(timestamp);
1017}
1018
1019int Surface::dispatchLock(va_list args) {
1020 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1021 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1022 return lock(outBuffer, inOutDirtyBounds);
1023}
1024
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001025int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001026 return unlockAndPost();
1027}
1028
Rachad7cb0d392014-07-29 17:53:53 -07001029int Surface::dispatchSetSidebandStream(va_list args) {
1030 native_handle_t* sH = va_arg(args, native_handle_t*);
1031 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1032 setSidebandStream(sidebandHandle);
1033 return OK;
1034}
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001035
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001036int Surface::dispatchSetBuffersDataSpace(va_list args) {
1037 android_dataspace dataspace =
1038 static_cast<android_dataspace>(va_arg(args, int));
1039 return setBuffersDataSpace(dataspace);
1040}
1041
Dan Stoza5065a552015-03-17 16:23:42 -07001042int Surface::dispatchSetSurfaceDamage(va_list args) {
1043 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1044 size_t numRects = va_arg(args, size_t);
1045 setSurfaceDamage(rects, numRects);
1046 return NO_ERROR;
1047}
1048
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001049int Surface::dispatchSetSharedBufferMode(va_list args) {
1050 bool sharedBufferMode = va_arg(args, int);
1051 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001052}
1053
1054int Surface::dispatchSetAutoRefresh(va_list args) {
1055 bool autoRefresh = va_arg(args, int);
1056 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001057}
1058
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001059int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1060 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1061 return getDisplayRefreshCycleDuration(outRefreshDuration);
1062}
1063
Brian Anderson1049d1d2016-12-16 17:25:57 -08001064int Surface::dispatchGetNextFrameId(va_list args) {
1065 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1066 *nextFrameId = getNextFrameNumber();
1067 return NO_ERROR;
1068}
1069
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001070int Surface::dispatchEnableFrameTimestamps(va_list args) {
1071 bool enable = va_arg(args, int);
1072 enableFrameTimestamps(enable);
1073 return NO_ERROR;
1074}
1075
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001076int Surface::dispatchGetCompositorTiming(va_list args) {
1077 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1078 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1079 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1080 return getCompositorTiming(compositeDeadline, compositeInterval,
1081 compositeToPresentLatency);
1082}
1083
Pablo Ceballosce796e72016-02-04 19:10:51 -08001084int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001085 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001086 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001087 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001088 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1089 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1090 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001091 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001092 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001093 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001094 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001095 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001096 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001097 outRequestedPresentTime, outAcquireTime, outLatchTime,
1098 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001099 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001100 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001101}
1102
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001103int Surface::dispatchGetWideColorSupport(va_list args) {
1104 bool* outSupport = va_arg(args, bool*);
1105 return getWideColorSupport(outSupport);
1106}
1107
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001108int Surface::dispatchGetHdrSupport(va_list args) {
1109 bool* outSupport = va_arg(args, bool*);
1110 return getHdrSupport(outSupport);
1111}
1112
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001113int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001114 static sp<IProducerListener> listener = new DummyProducerListener();
1115 return connect(api, listener);
1116}
1117
1118int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001119 ATRACE_CALL();
1120 ALOGV("Surface::connect");
1121 Mutex::Autolock lock(mMutex);
1122 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001123 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001124 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001125 mDefaultWidth = output.width;
1126 mDefaultHeight = output.height;
1127 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001128
1129 // Disable transform hint if sticky transform is set.
1130 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001131 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001132 }
1133
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001134 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001135 }
1136 if (!err && api == NATIVE_WINDOW_API_CPU) {
1137 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001138 // Clear the dirty region in case we're switching from a non-CPU API
1139 mDirtyRegion.clear();
1140 } else if (!err) {
1141 // Initialize the dirty region for tracking surface damage
1142 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001143 }
Dan Stoza5065a552015-03-17 16:23:42 -07001144
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001145 return err;
1146}
1147
Mathias Agopian365857d2013-09-11 19:35:45 -07001148
Robert Carr97b9c862016-09-08 13:54:35 -07001149int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001150 ATRACE_CALL();
1151 ALOGV("Surface::disconnect");
1152 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001153 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1154 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001155 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001156 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001157 if (!err) {
1158 mReqFormat = 0;
1159 mReqWidth = 0;
1160 mReqHeight = 0;
1161 mReqUsage = 0;
1162 mCrop.clear();
1163 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1164 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001165 mStickyTransform = 0;
1166
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001167 if (api == NATIVE_WINDOW_API_CPU) {
1168 mConnectedToCpu = false;
1169 }
1170 }
1171 return err;
1172}
1173
Dan Stozad9c49712015-04-27 11:06:01 -07001174int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001175 sp<Fence>* outFence) {
1176 ATRACE_CALL();
1177 ALOGV("Surface::detachNextBuffer");
1178
1179 if (outBuffer == NULL || outFence == NULL) {
1180 return BAD_VALUE;
1181 }
1182
1183 Mutex::Autolock lock(mMutex);
1184
1185 sp<GraphicBuffer> buffer(NULL);
1186 sp<Fence> fence(NULL);
1187 status_t result = mGraphicBufferProducer->detachNextBuffer(
1188 &buffer, &fence);
1189 if (result != NO_ERROR) {
1190 return result;
1191 }
1192
Dan Stozad9c49712015-04-27 11:06:01 -07001193 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001194 if (fence != NULL && fence->isValid()) {
1195 *outFence = fence;
1196 } else {
1197 *outFence = Fence::NO_FENCE;
1198 }
1199
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001200 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1201 if (mSlots[i].buffer != NULL &&
1202 mSlots[i].buffer->handle == buffer->handle) {
1203 mSlots[i].buffer = NULL;
1204 }
1205 }
1206
Dan Stoza231832e2015-03-11 11:55:01 -07001207 return NO_ERROR;
1208}
1209
1210int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1211{
1212 ATRACE_CALL();
1213 ALOGV("Surface::attachBuffer");
1214
1215 Mutex::Autolock lock(mMutex);
1216
1217 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001218 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1219 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001220 int32_t attachedSlot = -1;
1221 status_t result = mGraphicBufferProducer->attachBuffer(
1222 &attachedSlot, graphicBuffer);
1223 if (result != NO_ERROR) {
1224 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001225 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001226 return result;
1227 }
1228 mSlots[attachedSlot].buffer = graphicBuffer;
1229
1230 return NO_ERROR;
1231}
1232
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001233int Surface::setUsage(uint32_t reqUsage)
1234{
1235 ALOGV("Surface::setUsage");
1236 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001237 if (reqUsage != mReqUsage) {
1238 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1239 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001240 mReqUsage = reqUsage;
1241 return OK;
1242}
1243
1244int Surface::setCrop(Rect const* rect)
1245{
1246 ATRACE_CALL();
1247
Pablo Ceballos60d69222015-08-07 14:47:20 -07001248 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001249 if (rect == NULL || rect->isEmpty()) {
1250 realRect.clear();
1251 } else {
1252 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001253 }
1254
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001255 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1256 realRect.left, realRect.top, realRect.right, realRect.bottom);
1257
1258 Mutex::Autolock lock(mMutex);
1259 mCrop = realRect;
1260 return NO_ERROR;
1261}
1262
1263int Surface::setBufferCount(int bufferCount)
1264{
1265 ATRACE_CALL();
1266 ALOGV("Surface::setBufferCount");
1267 Mutex::Autolock lock(mMutex);
1268
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001269 status_t err = NO_ERROR;
1270 if (bufferCount == 0) {
1271 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1272 } else {
1273 int minUndequeuedBuffers = 0;
1274 err = mGraphicBufferProducer->query(
1275 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1276 if (err == NO_ERROR) {
1277 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1278 bufferCount - minUndequeuedBuffers);
1279 }
1280 }
Mathias Agopian90147262010-01-22 11:47:55 -08001281
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001282 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1283 bufferCount, strerror(-err));
1284
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001285 return err;
1286}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001287
Pablo Ceballosfa455352015-08-12 17:47:47 -07001288int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1289 ATRACE_CALL();
1290 ALOGV("Surface::setMaxDequeuedBufferCount");
1291 Mutex::Autolock lock(mMutex);
1292
1293 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1294 maxDequeuedBuffers);
1295 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1296 "returned %s", maxDequeuedBuffers, strerror(-err));
1297
Pablo Ceballosfa455352015-08-12 17:47:47 -07001298 return err;
1299}
1300
1301int Surface::setAsyncMode(bool async) {
1302 ATRACE_CALL();
1303 ALOGV("Surface::setAsyncMode");
1304 Mutex::Autolock lock(mMutex);
1305
1306 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1307 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1308 async, strerror(-err));
1309
Pablo Ceballosfa455352015-08-12 17:47:47 -07001310 return err;
1311}
1312
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001313int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001314 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001315 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001316 Mutex::Autolock lock(mMutex);
1317
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001318 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1319 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001320 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001321 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001322 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001323 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1324 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001325
1326 return err;
1327}
1328
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001329int Surface::setAutoRefresh(bool autoRefresh) {
1330 ATRACE_CALL();
1331 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1332 Mutex::Autolock lock(mMutex);
1333
1334 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1335 if (err == NO_ERROR) {
1336 mAutoRefresh = autoRefresh;
1337 }
1338 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1339 autoRefresh, strerror(-err));
1340 return err;
1341}
1342
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001343int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001344{
1345 ATRACE_CALL();
1346 ALOGV("Surface::setBuffersDimensions");
1347
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001348 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001349 return BAD_VALUE;
1350
1351 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001352 if (width != mReqWidth || height != mReqHeight) {
1353 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1354 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001355 mReqWidth = width;
1356 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001357 return NO_ERROR;
1358}
1359
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001360int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001361{
1362 ATRACE_CALL();
1363 ALOGV("Surface::setBuffersUserDimensions");
1364
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001365 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001366 return BAD_VALUE;
1367
1368 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001369 if (width != mUserWidth || height != mUserHeight) {
1370 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1371 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001372 mUserWidth = width;
1373 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001374 return NO_ERROR;
1375}
1376
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001377int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001378{
1379 ALOGV("Surface::setBuffersFormat");
1380
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001381 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001382 if (format != mReqFormat) {
1383 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1384 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001385 mReqFormat = format;
1386 return NO_ERROR;
1387}
1388
1389int Surface::setScalingMode(int mode)
1390{
1391 ATRACE_CALL();
1392 ALOGV("Surface::setScalingMode(%d)", mode);
1393
1394 switch (mode) {
1395 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1396 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1397 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001398 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001399 break;
1400 default:
1401 ALOGE("unknown scaling mode: %d", mode);
1402 return BAD_VALUE;
1403 }
1404
1405 Mutex::Autolock lock(mMutex);
1406 mScalingMode = mode;
1407 return NO_ERROR;
1408}
1409
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001410int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001411{
1412 ATRACE_CALL();
1413 ALOGV("Surface::setBuffersTransform");
1414 Mutex::Autolock lock(mMutex);
1415 mTransform = transform;
1416 return NO_ERROR;
1417}
1418
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001419int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001420{
1421 ATRACE_CALL();
1422 ALOGV("Surface::setBuffersStickyTransform");
1423 Mutex::Autolock lock(mMutex);
1424 mStickyTransform = transform;
1425 return NO_ERROR;
1426}
1427
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001428int Surface::setBuffersTimestamp(int64_t timestamp)
1429{
1430 ALOGV("Surface::setBuffersTimestamp");
1431 Mutex::Autolock lock(mMutex);
1432 mTimestamp = timestamp;
1433 return NO_ERROR;
1434}
1435
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001436int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1437{
1438 ALOGV("Surface::setBuffersDataSpace");
1439 Mutex::Autolock lock(mMutex);
1440 mDataSpace = dataSpace;
1441 return NO_ERROR;
1442}
1443
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001444void Surface::freeAllBuffers() {
1445 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1446 mSlots[i].buffer = 0;
1447 }
1448}
1449
Dan Stoza5065a552015-03-17 16:23:42 -07001450void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1451 ATRACE_CALL();
1452 ALOGV("Surface::setSurfaceDamage");
1453 Mutex::Autolock lock(mMutex);
1454
Dan Stozac62acbd2015-04-21 16:42:49 -07001455 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001456 mDirtyRegion = Region::INVALID_REGION;
1457 return;
1458 }
1459
1460 mDirtyRegion.clear();
1461 for (size_t r = 0; r < numRects; ++r) {
1462 // We intentionally flip top and bottom here, since because they're
1463 // specified with a bottom-left origin, top > bottom, which fails
1464 // validation in the Region class. We will fix this up when we flip to a
1465 // top-left origin in queueBuffer.
1466 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1467 mDirtyRegion.orSelf(rect);
1468 }
1469}
1470
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001471// ----------------------------------------------------------------------
1472// the lock/unlock APIs must be used from the same thread
1473
1474static status_t copyBlt(
1475 const sp<GraphicBuffer>& dst,
1476 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001477 const Region& reg,
1478 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001479{
1480 // src and dst with, height and format must be identical. no verification
1481 // is done here.
1482 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001483 uint8_t* src_bits = NULL;
1484 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1485 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001486 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1487
1488 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001489 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1490 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001491 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001492 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001493
1494 Region::const_iterator head(reg.begin());
1495 Region::const_iterator tail(reg.end());
1496 if (head != tail && src_bits && dst_bits) {
1497 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001498 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1499 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001500
1501 while (head != tail) {
1502 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001503 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001504 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001505 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1506 uint8_t const * s = src_bits +
1507 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1508 uint8_t * d = dst_bits +
1509 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001510 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001511 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001512 h = 1;
1513 }
1514 do {
1515 memcpy(d, s, size);
1516 d += dbpr;
1517 s += sbpr;
1518 } while (--h > 0);
1519 }
1520 }
1521
1522 if (src_bits)
1523 src->unlock();
1524
1525 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001526 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001527
1528 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001529}
1530
Mathias Agopiana138f892010-05-21 17:24:35 -07001531// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001532
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001533status_t Surface::lock(
1534 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1535{
1536 if (mLockedBuffer != 0) {
1537 ALOGE("Surface::lock failed, already locked");
1538 return INVALID_OPERATION;
1539 }
1540
1541 if (!mConnectedToCpu) {
1542 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1543 if (err) {
1544 return err;
1545 }
1546 // we're intending to do software rendering from this point
1547 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1548 }
1549
1550 ANativeWindowBuffer* out;
1551 int fenceFd = -1;
1552 status_t err = dequeueBuffer(&out, &fenceFd);
1553 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1554 if (err == NO_ERROR) {
1555 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001556 const Rect bounds(backBuffer->width, backBuffer->height);
1557
1558 Region newDirtyRegion;
1559 if (inOutDirtyBounds) {
1560 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1561 newDirtyRegion.andSelf(bounds);
1562 } else {
1563 newDirtyRegion.set(bounds);
1564 }
1565
1566 // figure out if we can copy the frontbuffer back
1567 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1568 const bool canCopyBack = (frontBuffer != 0 &&
1569 backBuffer->width == frontBuffer->width &&
1570 backBuffer->height == frontBuffer->height &&
1571 backBuffer->format == frontBuffer->format);
1572
1573 if (canCopyBack) {
1574 // copy the area that is invalid and not repainted this round
1575 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001576 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001577 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001578 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001579 } else {
1580 // if we can't copy-back anything, modify the user's dirty
1581 // region to make sure they redraw the whole buffer
1582 newDirtyRegion.set(bounds);
1583 mDirtyRegion.clear();
1584 Mutex::Autolock lock(mMutex);
1585 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1586 mSlots[i].dirtyRegion.clear();
1587 }
1588 }
1589
1590
1591 { // scope for the lock
1592 Mutex::Autolock lock(mMutex);
1593 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1594 if (backBufferSlot >= 0) {
1595 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1596 mDirtyRegion.subtract(dirtyRegion);
1597 dirtyRegion = newDirtyRegion;
1598 }
1599 }
1600
1601 mDirtyRegion.orSelf(newDirtyRegion);
1602 if (inOutDirtyBounds) {
1603 *inOutDirtyBounds = newDirtyRegion.getBounds();
1604 }
1605
1606 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001607 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001608 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001609 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001610
1611 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1612 backBuffer->handle);
1613
1614 if (res != 0) {
1615 err = INVALID_OPERATION;
1616 } else {
1617 mLockedBuffer = backBuffer;
1618 outBuffer->width = backBuffer->width;
1619 outBuffer->height = backBuffer->height;
1620 outBuffer->stride = backBuffer->stride;
1621 outBuffer->format = backBuffer->format;
1622 outBuffer->bits = vaddr;
1623 }
1624 }
1625 return err;
1626}
1627
1628status_t Surface::unlockAndPost()
1629{
1630 if (mLockedBuffer == 0) {
1631 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1632 return INVALID_OPERATION;
1633 }
1634
Francis Hart8f396012014-04-01 15:30:53 +03001635 int fd = -1;
1636 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001637 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1638
Francis Hart8f396012014-04-01 15:30:53 +03001639 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001640 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1641 mLockedBuffer->handle, strerror(-err));
1642
1643 mPostedBuffer = mLockedBuffer;
1644 mLockedBuffer = 0;
1645 return err;
1646}
1647
Robert Carr9f31e292016-04-11 11:15:32 -07001648bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1649 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001650 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001651 return true;
1652 }
1653 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1654}
1655
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001656status_t Surface::getUniqueId(uint64_t* outId) const {
1657 Mutex::Autolock lock(mMutex);
1658 return mGraphicBufferProducer->getUniqueId(outId);
1659}
1660
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661}; // namespace android