blob: 41221aab8ddf6d51681eafe6c82836a7ba969853 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002 * Copyright (C) 2010 The Android Open Source Project
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Surface"
Mathias Agopiane3c697f2013-02-14 17:11:02 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Mathias Agopian05debe12017-02-08 17:04:18 -080021#include <gui/Surface.h>
Mathias Agopianb0e76f42012-03-23 14:15:44 -070022
Mathias Agopian05debe12017-02-08 17:04:18 -080023#include <android/native_window.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080024
Mathias Agopian9cce3252010-02-09 17:46:37 -080025#include <utils/Log.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080026#include <utils/Trace.h>
Rachad7cb0d392014-07-29 17:53:53 -070027#include <utils/NativeHandle.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080028
Ian Elliott62c48c92017-01-20 13:13:20 -070029#include <ui/DisplayStatInfo.h>
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -070030#include <ui/Fence.h>
31#include <ui/HdrCapabilities.h>
32#include <ui/Region.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070033
Mathias Agopian2b5dd402017-02-07 17:36:19 -080034#include <gui/BufferItem.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070035#include <gui/IProducerListener.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080036
Mathias Agopian2b5dd402017-02-07 17:36:19 -080037#include <gui/ISurfaceComposer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080038#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -060040#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
41#include <configstore/Utils.h>
42
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043namespace android {
44
Mathias Agopiane3c697f2013-02-14 17:11:02 -080045Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070046 const sp<IGraphicBufferProducer>& bufferProducer,
47 bool controlledByApp)
Dan Stoza812ed062015-06-02 15:45:22 -070048 : mGraphicBufferProducer(bufferProducer),
Pablo Ceballos60d69222015-08-07 14:47:20 -070049 mCrop(Rect::EMPTY_RECT),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080050 mGenerationNumber(0),
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070051 mSharedBufferMode(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080052 mAutoRefresh(false),
53 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
Pablo Ceballosbc8c1922016-07-01 14:15:41 -070054 mSharedBufferHasBeenQueued(false),
Brian Anderson6b376712017-04-04 10:51:39 -070055 mQueriedSupportedTimestamps(false),
56 mFrameTimestampsSupportsPresent(false),
Brian Anderson3da8d272016-07-28 16:20:47 -070057 mEnableFrameTimestamps(false),
58 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>())
Mathias Agopian62185b72009-04-16 16:19:50 -070059{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080060 // Initialize the ANativeWindow function pointers.
61 ANativeWindow::setSwapInterval = hook_setSwapInterval;
62 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
63 ANativeWindow::cancelBuffer = hook_cancelBuffer;
64 ANativeWindow::queueBuffer = hook_queueBuffer;
65 ANativeWindow::query = hook_query;
66 ANativeWindow::perform = hook_perform;
67
68 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
69 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
70 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
71 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
72
73 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
74 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
75
76 mReqWidth = 0;
77 mReqHeight = 0;
78 mReqFormat = 0;
79 mReqUsage = 0;
80 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080081 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080082 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
83 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070084 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080085 mDefaultWidth = 0;
86 mDefaultHeight = 0;
87 mUserWidth = 0;
88 mUserHeight = 0;
89 mTransformHint = 0;
90 mConsumerRunningBehind = false;
91 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070092 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070093 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070094}
95
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070096Surface::~Surface() {
97 if (mConnectedToCpu) {
98 Surface::disconnect(NATIVE_WINDOW_API_CPU);
99 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800100}
101
Brian Anderson3da8d272016-07-28 16:20:47 -0700102sp<ISurfaceComposer> Surface::composerService() const {
103 return ComposerService::getComposerService();
104}
105
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800106nsecs_t Surface::now() const {
107 return systemTime();
108}
109
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800110sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
111 return mGraphicBufferProducer;
112}
113
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900114void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
115 mGraphicBufferProducer->setSidebandStream(stream);
116}
117
Dan Stoza29a3e902014-06-20 13:13:57 -0700118void Surface::allocateBuffers() {
119 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
120 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700121 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
122 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700123}
124
Dan Stoza812ed062015-06-02 15:45:22 -0700125status_t Surface::setGenerationNumber(uint32_t generation) {
126 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
127 if (result == NO_ERROR) {
128 mGenerationNumber = generation;
129 }
130 return result;
131}
132
Dan Stoza7dde5992015-05-22 09:51:44 -0700133uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700134 Mutex::Autolock lock(mMutex);
135 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700136}
137
Dan Stozac6f30bd2015-06-08 09:32:50 -0700138String8 Surface::getConsumerName() const {
139 return mGraphicBufferProducer->getConsumerName();
140}
141
Dan Stoza127fc632015-06-30 13:43:32 -0700142status_t Surface::setDequeueTimeout(nsecs_t timeout) {
143 return mGraphicBufferProducer->setDequeueTimeout(timeout);
144}
145
Dan Stoza50101d02016-04-07 16:53:23 -0700146status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700147 sp<Fence>* outFence, float outTransformMatrix[16]) {
148 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
149 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700150}
151
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800152status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
153 ATRACE_CALL();
154
155 DisplayStatInfo stats;
156 status_t err = composerService()->getDisplayStats(NULL, &stats);
157
158 *outRefreshDuration = stats.vsyncPeriod;
159
160 return NO_ERROR;
161}
162
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700163void Surface::enableFrameTimestamps(bool enable) {
164 Mutex::Autolock lock(mMutex);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800165 // If going from disabled to enabled, get the initial values for
166 // compositor and display timing.
167 if (!mEnableFrameTimestamps && enable) {
168 FrameEventHistoryDelta delta;
169 mGraphicBufferProducer->getFrameTimestamps(&delta);
170 mFrameEventHistory->applyDelta(delta);
171 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700172 mEnableFrameTimestamps = enable;
173}
174
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800175status_t Surface::getCompositorTiming(
176 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
177 nsecs_t* compositeToPresentLatency) {
178 Mutex::Autolock lock(mMutex);
179 if (!mEnableFrameTimestamps) {
180 return INVALID_OPERATION;
181 }
182
183 if (compositeDeadline != nullptr) {
184 *compositeDeadline =
185 mFrameEventHistory->getNextCompositeDeadline(now());
186 }
187 if (compositeInterval != nullptr) {
188 *compositeInterval = mFrameEventHistory->getCompositeInterval();
189 }
190 if (compositeToPresentLatency != nullptr) {
191 *compositeToPresentLatency =
192 mFrameEventHistory->getCompositeToPresentLatency();
193 }
194 return NO_ERROR;
195}
196
Brian Anderson50143b32016-09-30 14:01:24 -0700197static bool checkConsumerForUpdates(
198 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700199 const nsecs_t* outLatchTime,
200 const nsecs_t* outFirstRefreshStartTime,
201 const nsecs_t* outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700202 const nsecs_t* outGpuCompositionDoneTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700203 const nsecs_t* outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700204 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700205 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700206 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
207 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700208 !e->hasFirstRefreshStartInfo();
Brian Andersonb04c6f02016-10-21 12:57:46 -0700209 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700210 !e->hasGpuCompositionDoneInfo();
211 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
212 !e->hasDisplayPresentInfo();
213
Brian Anderson6b376712017-04-04 10:51:39 -0700214 // LastRefreshStart, DequeueReady, and Release are never available for the
215 // last frame.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700216 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
217 !e->hasLastRefreshStartInfo() &&
218 (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700219 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
220 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700221 bool checkForRelease = (outReleaseTime != nullptr) &&
222 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
223
224 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700225 return checkForLatch || checkForFirstRefreshStart ||
Brian Andersonb04c6f02016-10-21 12:57:46 -0700226 checkForLastRefreshStart || checkForGpuCompositionDone ||
Brian Anderson4e606e32017-03-16 15:34:57 -0700227 checkForDisplayPresent || checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700228}
229
Brian Anderson3d4039d2016-09-23 16:31:30 -0700230static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
231 if (dst != nullptr) {
Brian Andersoned816e62016-10-26 16:12:21 -0700232 *dst = FrameEvents::isValidTimestamp(src) ? src : 0;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700233 }
234}
235
236static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) {
237 if (dst != nullptr) {
238 nsecs_t signalTime = src->getSignalTime();
239 *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0;
240 }
241}
242
Brian Anderson069b3652016-07-22 10:32:47 -0700243status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700244 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700245 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700246 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700247 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
248 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800249 ATRACE_CALL();
250
Brian Anderson3890c392016-07-25 12:48:08 -0700251 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700252
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700253 if (!mEnableFrameTimestamps) {
254 return INVALID_OPERATION;
255 }
256
Brian Anderson6b376712017-04-04 10:51:39 -0700257 // Verify the requested timestamps are supported.
258 querySupportedTimestampsLocked();
259 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
260 return BAD_VALUE;
261 }
262
Brian Anderson3da8d272016-07-28 16:20:47 -0700263 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700264 if (events == nullptr) {
265 // If the entry isn't available in the producer, it's definitely not
266 // available in the consumer.
267 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700268 }
269
Brian Anderson50143b32016-09-30 14:01:24 -0700270 // Update our cache of events if the requested events are not available.
271 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700272 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700273 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700274 outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700275 FrameEventHistoryDelta delta;
276 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700277 mFrameEventHistory->applyDelta(delta);
278 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700279 }
280
Brian Anderson3890c392016-07-25 12:48:08 -0700281 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700282 // The entry was available before the update, but was overwritten
283 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700284 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800285 }
Brian Anderson069b3652016-07-22 10:32:47 -0700286
Brian Anderson3d4039d2016-09-23 16:31:30 -0700287 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700288 getFrameTimestamp(outLatchTime, events->latchTime);
289 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
290 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
291 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700292
Brian Anderson3d4039d2016-09-23 16:31:30 -0700293 getFrameTimestampFence(outAcquireTime, events->acquireFence);
294 getFrameTimestampFence(
Brian Andersonb04c6f02016-10-21 12:57:46 -0700295 outGpuCompositionDoneTime, events->gpuCompositionDoneFence);
Brian Anderson3d4039d2016-09-23 16:31:30 -0700296 getFrameTimestampFence(
297 outDisplayPresentTime, events->displayPresentFence);
Brian Anderson3d4039d2016-09-23 16:31:30 -0700298 getFrameTimestampFence(outReleaseTime, events->releaseFence);
Brian Anderson069b3652016-07-22 10:32:47 -0700299
300 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800301}
302
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600303using namespace android::hardware::configstore;
304using namespace android::hardware::configstore::V1_0;
305
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700306status_t Surface::getWideColorSupport(bool* supported) {
307 ATRACE_CALL();
308
309 sp<IBinder> display(
310 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
311 Vector<android_color_mode_t> colorModes;
312 status_t err =
313 composerService()->getDisplayColorModes(display, &colorModes);
314
315 if (err)
316 return err;
317
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600318 bool wideColorBoardConfig =
319 getBool<ISurfaceFlingerConfigs,
320 &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
321
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700322 *supported = false;
323 for (android_color_mode_t colorMode : colorModes) {
324 switch (colorMode) {
325 case HAL_COLOR_MODE_DISPLAY_P3:
326 case HAL_COLOR_MODE_ADOBE_RGB:
327 case HAL_COLOR_MODE_DCI_P3:
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600328 if (wideColorBoardConfig) {
329 *supported = true;
330 }
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700331 break;
332 default:
333 break;
334 }
335 }
336
337 return NO_ERROR;
338}
339
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700340status_t Surface::getHdrSupport(bool* supported) {
341 ATRACE_CALL();
342
343 sp<IBinder> display(
344 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
345 HdrCapabilities hdrCapabilities;
346 status_t err =
347 composerService()->getHdrCapabilities(display, &hdrCapabilities);
348
349 if (err)
350 return err;
351
352 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
353
354 return NO_ERROR;
355}
356
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800357int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
358 Surface* c = getSelf(window);
359 return c->setSwapInterval(interval);
360}
361
362int Surface::hook_dequeueBuffer(ANativeWindow* window,
363 ANativeWindowBuffer** buffer, int* fenceFd) {
364 Surface* c = getSelf(window);
365 return c->dequeueBuffer(buffer, fenceFd);
366}
367
368int Surface::hook_cancelBuffer(ANativeWindow* window,
369 ANativeWindowBuffer* buffer, int fenceFd) {
370 Surface* c = getSelf(window);
371 return c->cancelBuffer(buffer, fenceFd);
372}
373
374int Surface::hook_queueBuffer(ANativeWindow* window,
375 ANativeWindowBuffer* buffer, int fenceFd) {
376 Surface* c = getSelf(window);
377 return c->queueBuffer(buffer, fenceFd);
378}
379
380int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
381 ANativeWindowBuffer** buffer) {
382 Surface* c = getSelf(window);
383 ANativeWindowBuffer* buf;
384 int fenceFd = -1;
385 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600386 if (result != OK) {
387 return result;
388 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800389 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700390 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800391 if (waitResult != OK) {
392 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
393 waitResult);
394 c->cancelBuffer(buf, -1);
395 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700396 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800397 *buffer = buf;
398 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700399}
400
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800401int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
402 ANativeWindowBuffer* buffer) {
403 Surface* c = getSelf(window);
404 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700405}
406
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800407int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
408 ANativeWindowBuffer* buffer) {
409 Surface* c = getSelf(window);
410 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700411}
412
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800413int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
414 ANativeWindowBuffer* buffer) {
415 Surface* c = getSelf(window);
416 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700417}
Mathias Agopian62185b72009-04-16 16:19:50 -0700418
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800419int Surface::hook_query(const ANativeWindow* window,
420 int what, int* value) {
421 const Surface* c = getSelf(window);
422 return c->query(what, value);
423}
424
425int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
426 va_list args;
427 va_start(args, operation);
428 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700429 int result = c->perform(operation, args);
430 va_end(args);
431 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800432}
433
434int Surface::setSwapInterval(int interval) {
435 ATRACE_CALL();
436 // EGL specification states:
437 // interval is silently clamped to minimum and maximum implementation
438 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800439
440 if (interval < minSwapInterval)
441 interval = minSwapInterval;
442
443 if (interval > maxSwapInterval)
444 interval = maxSwapInterval;
445
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700446 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700447 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800448
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700449 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800450}
451
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700452int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800453 ATRACE_CALL();
454 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800455
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800456 uint32_t reqWidth;
457 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800458 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800459 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700460 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800461
462 {
463 Mutex::Autolock lock(mMutex);
464
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800465 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
466 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800467
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800468 reqFormat = mReqFormat;
469 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800470
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700471 enableFrameTimestamps = mEnableFrameTimestamps;
472
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700473 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800474 BufferItem::INVALID_BUFFER_SLOT) {
475 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
476 if (gbuf != NULL) {
477 *buffer = gbuf.get();
478 *fenceFd = -1;
479 return OK;
480 }
481 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800482 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
483
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800484 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800485 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700486 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700487
488 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700489 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700490 reqWidth, reqHeight, reqFormat, reqUsage,
491 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700492 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800493
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800494 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700495 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
496 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
497 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800498 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700499 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800500
501 Mutex::Autolock lock(mMutex);
502
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800503 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700504
505 // this should never happen
506 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
507
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800508 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
509 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700510 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700511
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700512 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700513 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700514 }
515
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700516 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
517 if (mReportRemovedBuffers && (gbuf != nullptr)) {
518 mRemovedBuffers.clear();
519 mRemovedBuffers.push_back(gbuf);
520 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800521 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
522 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700523 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700524 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800525 return result;
526 }
527 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700528
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800529 if (fence->isValid()) {
530 *fenceFd = fence->dup();
531 if (*fenceFd == -1) {
532 ALOGE("dequeueBuffer: error duping fence: %d", errno);
533 // dup() should never fail; something is badly wrong. Soldier on
534 // and hope for the best; the worst that should happen is some
535 // visible corruption that lasts until the next frame.
536 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700537 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800538 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700539 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800540
541 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800542
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700543 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800544 mSharedBufferSlot = buf;
545 mSharedBufferHasBeenQueued = false;
546 } else if (mSharedBufferSlot == buf) {
547 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
548 mSharedBufferHasBeenQueued = false;
549 }
550
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800551 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700552}
553
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800554int Surface::cancelBuffer(android_native_buffer_t* buffer,
555 int fenceFd) {
556 ATRACE_CALL();
557 ALOGV("Surface::cancelBuffer");
558 Mutex::Autolock lock(mMutex);
559 int i = getSlotFromBufferLocked(buffer);
560 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900561 if (fenceFd >= 0) {
562 close(fenceFd);
563 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800564 return i;
565 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800566 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
567 if (fenceFd >= 0) {
568 close(fenceFd);
569 }
570 return OK;
571 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800572 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
573 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800574
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700575 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800576 mSharedBufferHasBeenQueued = true;
577 }
578
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800579 return OK;
580}
581
582int Surface::getSlotFromBufferLocked(
583 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800584 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
585 if (mSlots[i].buffer != NULL &&
586 mSlots[i].buffer->handle == buffer->handle) {
587 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700588 }
589 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800590 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
591 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700592}
593
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800594int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800595 ALOGV("Surface::lockBuffer");
596 Mutex::Autolock lock(mMutex);
597 return OK;
598}
Mathias Agopian631f3582010-05-25 17:51:34 -0700599
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800600int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
601 ATRACE_CALL();
602 ALOGV("Surface::queueBuffer");
603 Mutex::Autolock lock(mMutex);
604 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700605 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800606
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800607 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700608 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700609 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700610 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700611 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800612 } else {
613 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700614 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800615 int i = getSlotFromBufferLocked(buffer);
616 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900617 if (fenceFd >= 0) {
618 close(fenceFd);
619 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800620 return i;
621 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800622 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
623 if (fenceFd >= 0) {
624 close(fenceFd);
625 }
626 return OK;
627 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800630 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700631 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800632 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800634 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
635 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700636 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800637 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700638 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700639
Dan Stozac62acbd2015-04-21 16:42:49 -0700640 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700641 input.setSurfaceDamage(Region::INVALID_REGION);
642 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700643 // Here we do two things:
644 // 1) The surface damage was specified using the OpenGL ES convention of
645 // the origin being in the bottom-left corner. Here we flip to the
646 // convention that the rest of the system uses (top-left corner) by
647 // subtracting all top/bottom coordinates from the buffer height.
648 // 2) If the buffer is coming in rotated (for example, because the EGL
649 // implementation is reacting to the transform hint coming back from
650 // SurfaceFlinger), the surface damage needs to be rotated the
651 // opposite direction, since it was generated assuming an unrotated
652 // buffer (the app doesn't know that the EGL implementation is
653 // reacting to the transform hint behind its back). The
654 // transformations in the switch statement below apply those
655 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
656
657 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700658 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700659 bool rotated90 = (mTransform ^ mStickyTransform) &
660 NATIVE_WINDOW_TRANSFORM_ROT_90;
661 if (rotated90) {
662 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700663 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700664
Dan Stoza5065a552015-03-17 16:23:42 -0700665 Region flippedRegion;
666 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700667 int left = rect.left;
668 int right = rect.right;
669 int top = height - rect.bottom; // Flip from OpenGL convention
670 int bottom = height - rect.top; // Flip from OpenGL convention
671 switch (mTransform ^ mStickyTransform) {
672 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
673 // Rotate 270 degrees
674 Rect flippedRect{top, width - right, bottom, width - left};
675 flippedRegion.orSelf(flippedRect);
676 break;
677 }
678 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
679 // Rotate 180 degrees
680 Rect flippedRect{width - right, height - bottom,
681 width - left, height - top};
682 flippedRegion.orSelf(flippedRect);
683 break;
684 }
685 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
686 // Rotate 90 degrees
687 Rect flippedRect{height - bottom, left,
688 height - top, right};
689 flippedRegion.orSelf(flippedRect);
690 break;
691 }
692 default: {
693 Rect flippedRect{left, top, right, bottom};
694 flippedRegion.orSelf(flippedRect);
695 break;
696 }
697 }
Dan Stoza5065a552015-03-17 16:23:42 -0700698 }
699
700 input.setSurfaceDamage(flippedRegion);
701 }
702
Dan Stoza70ccba52016-07-01 14:00:40 -0700703 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800704 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700705 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800706 if (err != OK) {
707 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
708 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800709
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700710 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700711 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700712 // Update timestamps with the local acquire fence.
713 // The consumer doesn't send it back to prevent us from having two
714 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700715 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700716 std::make_shared<FenceTime>(std::move(fence)));
717
718 // Cache timestamps of signaled fences so we can close their file
719 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700720 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700721 }
722
Brian Anderson50143b32016-09-30 14:01:24 -0700723 mLastFrameNumber = mNextFrameNumber;
724
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700725 mDefaultWidth = output.width;
726 mDefaultHeight = output.height;
727 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700728
Ruben Brunk1681d952014-06-27 15:51:55 -0700729 // Disable transform hint if sticky transform is set.
730 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700731 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700732 }
733
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700734 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700735
Dan Stozac62acbd2015-04-21 16:42:49 -0700736 if (!mConnectedToCpu) {
737 // Clear surface damage back to full-buffer
738 mDirtyRegion = Region::INVALID_REGION;
739 }
Dan Stoza5065a552015-03-17 16:23:42 -0700740
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700741 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800742 mSharedBufferHasBeenQueued = true;
743 }
744
Robert Carr9f31e292016-04-11 11:15:32 -0700745 mQueueBufferCondition.broadcast();
746
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800747 return err;
748}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700749
Brian Anderson6b376712017-04-04 10:51:39 -0700750void Surface::querySupportedTimestampsLocked() const {
751 // mMutex must be locked when calling this method.
752
753 if (mQueriedSupportedTimestamps) {
754 return;
755 }
756 mQueriedSupportedTimestamps = true;
757
758 std::vector<FrameEvent> supportedFrameTimestamps;
759 status_t err = composerService()->getSupportedFrameTimestamps(
760 &supportedFrameTimestamps);
761
762 if (err != NO_ERROR) {
763 return;
764 }
765
766 for (auto sft : supportedFrameTimestamps) {
767 if (sft == FrameEvent::DISPLAY_PRESENT) {
768 mFrameTimestampsSupportsPresent = true;
769 }
770 }
771}
772
Mathias Agopiana67932f2011-04-20 14:20:59 -0700773int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800774 ATRACE_CALL();
775 ALOGV("Surface::query");
776 { // scope for the lock
777 Mutex::Autolock lock(mMutex);
778 switch (what) {
779 case NATIVE_WINDOW_FORMAT:
780 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800781 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800782 return NO_ERROR;
783 }
784 break;
785 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700786 if (composerService()->authenticateSurfaceTexture(
787 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800788 *value = 1;
789 } else {
790 *value = 0;
791 }
792 return NO_ERROR;
793 }
794 case NATIVE_WINDOW_CONCRETE_TYPE:
795 *value = NATIVE_WINDOW_SURFACE;
796 return NO_ERROR;
797 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800798 *value = static_cast<int>(
799 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800800 return NO_ERROR;
801 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800802 *value = static_cast<int>(
803 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800804 return NO_ERROR;
805 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800806 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800807 return NO_ERROR;
808 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
809 status_t err = NO_ERROR;
810 if (!mConsumerRunningBehind) {
811 *value = 0;
812 } else {
813 err = mGraphicBufferProducer->query(what, value);
814 if (err == NO_ERROR) {
815 mConsumerRunningBehind = *value;
816 }
817 }
818 return err;
819 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700820 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
821 int64_t durationUs = mLastDequeueDuration / 1000;
822 *value = durationUs > std::numeric_limits<int>::max() ?
823 std::numeric_limits<int>::max() :
824 static_cast<int>(durationUs);
825 return NO_ERROR;
826 }
827 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
828 int64_t durationUs = mLastQueueDuration / 1000;
829 *value = durationUs > std::numeric_limits<int>::max() ?
830 std::numeric_limits<int>::max() :
831 static_cast<int>(durationUs);
832 return NO_ERROR;
833 }
Brian Anderson6b376712017-04-04 10:51:39 -0700834 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
835 querySupportedTimestampsLocked();
836 *value = mFrameTimestampsSupportsPresent ? 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*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001093 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001094 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001095 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001096 outRequestedPresentTime, outAcquireTime, outLatchTime,
1097 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001098 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001099 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001100}
1101
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001102int Surface::dispatchGetWideColorSupport(va_list args) {
1103 bool* outSupport = va_arg(args, bool*);
1104 return getWideColorSupport(outSupport);
1105}
1106
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001107int Surface::dispatchGetHdrSupport(va_list args) {
1108 bool* outSupport = va_arg(args, bool*);
1109 return getHdrSupport(outSupport);
1110}
1111
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001112int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001113 static sp<IProducerListener> listener = new DummyProducerListener();
1114 return connect(api, listener);
1115}
1116
1117int Surface::connect(int api, const sp<IProducerListener>& listener) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001118 return connect(api, listener, false);
1119}
1120
1121int Surface::connect(
1122 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001123 ATRACE_CALL();
1124 ALOGV("Surface::connect");
1125 Mutex::Autolock lock(mMutex);
1126 IGraphicBufferProducer::QueueBufferOutput output;
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001127 mReportRemovedBuffers = reportBufferRemoval;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001128 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001129 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001130 mDefaultWidth = output.width;
1131 mDefaultHeight = output.height;
1132 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001133
1134 // Disable transform hint if sticky transform is set.
1135 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001136 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001137 }
1138
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001139 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001140 }
1141 if (!err && api == NATIVE_WINDOW_API_CPU) {
1142 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001143 // Clear the dirty region in case we're switching from a non-CPU API
1144 mDirtyRegion.clear();
1145 } else if (!err) {
1146 // Initialize the dirty region for tracking surface damage
1147 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001148 }
Dan Stoza5065a552015-03-17 16:23:42 -07001149
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001150 return err;
1151}
1152
Mathias Agopian365857d2013-09-11 19:35:45 -07001153
Robert Carr97b9c862016-09-08 13:54:35 -07001154int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001155 ATRACE_CALL();
1156 ALOGV("Surface::disconnect");
1157 Mutex::Autolock lock(mMutex);
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001158 mRemovedBuffers.clear();
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001159 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1160 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001161 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001162 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001163 if (!err) {
1164 mReqFormat = 0;
1165 mReqWidth = 0;
1166 mReqHeight = 0;
1167 mReqUsage = 0;
1168 mCrop.clear();
1169 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1170 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001171 mStickyTransform = 0;
1172
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001173 if (api == NATIVE_WINDOW_API_CPU) {
1174 mConnectedToCpu = false;
1175 }
1176 }
1177 return err;
1178}
1179
Dan Stozad9c49712015-04-27 11:06:01 -07001180int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001181 sp<Fence>* outFence) {
1182 ATRACE_CALL();
1183 ALOGV("Surface::detachNextBuffer");
1184
1185 if (outBuffer == NULL || outFence == NULL) {
1186 return BAD_VALUE;
1187 }
1188
1189 Mutex::Autolock lock(mMutex);
1190
1191 sp<GraphicBuffer> buffer(NULL);
1192 sp<Fence> fence(NULL);
1193 status_t result = mGraphicBufferProducer->detachNextBuffer(
1194 &buffer, &fence);
1195 if (result != NO_ERROR) {
1196 return result;
1197 }
1198
Dan Stozad9c49712015-04-27 11:06:01 -07001199 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001200 if (fence != NULL && fence->isValid()) {
1201 *outFence = fence;
1202 } else {
1203 *outFence = Fence::NO_FENCE;
1204 }
1205
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001206 if (mReportRemovedBuffers) {
1207 mRemovedBuffers.clear();
1208 }
1209
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001210 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1211 if (mSlots[i].buffer != NULL &&
1212 mSlots[i].buffer->handle == buffer->handle) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001213 if (mReportRemovedBuffers) {
1214 mRemovedBuffers.push_back(mSlots[i].buffer);
1215 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001216 mSlots[i].buffer = NULL;
1217 }
1218 }
1219
Dan Stoza231832e2015-03-11 11:55:01 -07001220 return NO_ERROR;
1221}
1222
1223int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1224{
1225 ATRACE_CALL();
1226 ALOGV("Surface::attachBuffer");
1227
1228 Mutex::Autolock lock(mMutex);
1229
1230 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001231 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1232 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001233 int32_t attachedSlot = -1;
1234 status_t result = mGraphicBufferProducer->attachBuffer(
1235 &attachedSlot, graphicBuffer);
1236 if (result != NO_ERROR) {
1237 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001238 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001239 return result;
1240 }
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001241 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
1242 mRemovedBuffers.clear();
1243 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1244 }
Dan Stoza231832e2015-03-11 11:55:01 -07001245 mSlots[attachedSlot].buffer = graphicBuffer;
1246
1247 return NO_ERROR;
1248}
1249
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001250int Surface::setUsage(uint32_t reqUsage)
1251{
1252 ALOGV("Surface::setUsage");
1253 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001254 if (reqUsage != mReqUsage) {
1255 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1256 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001257 mReqUsage = reqUsage;
1258 return OK;
1259}
1260
1261int Surface::setCrop(Rect const* rect)
1262{
1263 ATRACE_CALL();
1264
Pablo Ceballos60d69222015-08-07 14:47:20 -07001265 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001266 if (rect == NULL || rect->isEmpty()) {
1267 realRect.clear();
1268 } else {
1269 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001270 }
1271
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001272 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1273 realRect.left, realRect.top, realRect.right, realRect.bottom);
1274
1275 Mutex::Autolock lock(mMutex);
1276 mCrop = realRect;
1277 return NO_ERROR;
1278}
1279
1280int Surface::setBufferCount(int bufferCount)
1281{
1282 ATRACE_CALL();
1283 ALOGV("Surface::setBufferCount");
1284 Mutex::Autolock lock(mMutex);
1285
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001286 status_t err = NO_ERROR;
1287 if (bufferCount == 0) {
1288 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1289 } else {
1290 int minUndequeuedBuffers = 0;
1291 err = mGraphicBufferProducer->query(
1292 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1293 if (err == NO_ERROR) {
1294 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1295 bufferCount - minUndequeuedBuffers);
1296 }
1297 }
Mathias Agopian90147262010-01-22 11:47:55 -08001298
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001299 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1300 bufferCount, strerror(-err));
1301
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001302 return err;
1303}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001304
Pablo Ceballosfa455352015-08-12 17:47:47 -07001305int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1306 ATRACE_CALL();
1307 ALOGV("Surface::setMaxDequeuedBufferCount");
1308 Mutex::Autolock lock(mMutex);
1309
1310 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1311 maxDequeuedBuffers);
1312 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1313 "returned %s", maxDequeuedBuffers, strerror(-err));
1314
Pablo Ceballosfa455352015-08-12 17:47:47 -07001315 return err;
1316}
1317
1318int Surface::setAsyncMode(bool async) {
1319 ATRACE_CALL();
1320 ALOGV("Surface::setAsyncMode");
1321 Mutex::Autolock lock(mMutex);
1322
1323 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1324 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1325 async, strerror(-err));
1326
Pablo Ceballosfa455352015-08-12 17:47:47 -07001327 return err;
1328}
1329
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001330int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001331 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001332 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001333 Mutex::Autolock lock(mMutex);
1334
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001335 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1336 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001337 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001338 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001339 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001340 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1341 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001342
1343 return err;
1344}
1345
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001346int Surface::setAutoRefresh(bool autoRefresh) {
1347 ATRACE_CALL();
1348 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1349 Mutex::Autolock lock(mMutex);
1350
1351 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1352 if (err == NO_ERROR) {
1353 mAutoRefresh = autoRefresh;
1354 }
1355 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1356 autoRefresh, strerror(-err));
1357 return err;
1358}
1359
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001360int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001361{
1362 ATRACE_CALL();
1363 ALOGV("Surface::setBuffersDimensions");
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 != mReqWidth || height != mReqHeight) {
1370 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1371 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001372 mReqWidth = width;
1373 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001374 return NO_ERROR;
1375}
1376
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001377int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001378{
1379 ATRACE_CALL();
1380 ALOGV("Surface::setBuffersUserDimensions");
1381
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001382 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001383 return BAD_VALUE;
1384
1385 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001386 if (width != mUserWidth || height != mUserHeight) {
1387 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1388 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001389 mUserWidth = width;
1390 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001391 return NO_ERROR;
1392}
1393
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001394int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001395{
1396 ALOGV("Surface::setBuffersFormat");
1397
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001398 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001399 if (format != mReqFormat) {
1400 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1401 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001402 mReqFormat = format;
1403 return NO_ERROR;
1404}
1405
1406int Surface::setScalingMode(int mode)
1407{
1408 ATRACE_CALL();
1409 ALOGV("Surface::setScalingMode(%d)", mode);
1410
1411 switch (mode) {
1412 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1413 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1414 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001415 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001416 break;
1417 default:
1418 ALOGE("unknown scaling mode: %d", mode);
1419 return BAD_VALUE;
1420 }
1421
1422 Mutex::Autolock lock(mMutex);
1423 mScalingMode = mode;
1424 return NO_ERROR;
1425}
1426
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001427int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001428{
1429 ATRACE_CALL();
1430 ALOGV("Surface::setBuffersTransform");
1431 Mutex::Autolock lock(mMutex);
1432 mTransform = transform;
1433 return NO_ERROR;
1434}
1435
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001436int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001437{
1438 ATRACE_CALL();
1439 ALOGV("Surface::setBuffersStickyTransform");
1440 Mutex::Autolock lock(mMutex);
1441 mStickyTransform = transform;
1442 return NO_ERROR;
1443}
1444
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001445int Surface::setBuffersTimestamp(int64_t timestamp)
1446{
1447 ALOGV("Surface::setBuffersTimestamp");
1448 Mutex::Autolock lock(mMutex);
1449 mTimestamp = timestamp;
1450 return NO_ERROR;
1451}
1452
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001453int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1454{
1455 ALOGV("Surface::setBuffersDataSpace");
1456 Mutex::Autolock lock(mMutex);
1457 mDataSpace = dataSpace;
1458 return NO_ERROR;
1459}
1460
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001461void Surface::freeAllBuffers() {
1462 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1463 mSlots[i].buffer = 0;
1464 }
1465}
1466
Dan Stoza5065a552015-03-17 16:23:42 -07001467void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1468 ATRACE_CALL();
1469 ALOGV("Surface::setSurfaceDamage");
1470 Mutex::Autolock lock(mMutex);
1471
Dan Stozac62acbd2015-04-21 16:42:49 -07001472 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001473 mDirtyRegion = Region::INVALID_REGION;
1474 return;
1475 }
1476
1477 mDirtyRegion.clear();
1478 for (size_t r = 0; r < numRects; ++r) {
1479 // We intentionally flip top and bottom here, since because they're
1480 // specified with a bottom-left origin, top > bottom, which fails
1481 // validation in the Region class. We will fix this up when we flip to a
1482 // top-left origin in queueBuffer.
1483 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1484 mDirtyRegion.orSelf(rect);
1485 }
1486}
1487
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001488// ----------------------------------------------------------------------
1489// the lock/unlock APIs must be used from the same thread
1490
1491static status_t copyBlt(
1492 const sp<GraphicBuffer>& dst,
1493 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001494 const Region& reg,
1495 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001496{
1497 // src and dst with, height and format must be identical. no verification
1498 // is done here.
1499 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001500 uint8_t* src_bits = NULL;
1501 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1502 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001503 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1504
1505 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001506 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1507 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001508 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001509 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001510
1511 Region::const_iterator head(reg.begin());
1512 Region::const_iterator tail(reg.end());
1513 if (head != tail && src_bits && dst_bits) {
1514 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001515 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1516 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001517
1518 while (head != tail) {
1519 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001520 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001521 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001522 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1523 uint8_t const * s = src_bits +
1524 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1525 uint8_t * d = dst_bits +
1526 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001527 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001528 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001529 h = 1;
1530 }
1531 do {
1532 memcpy(d, s, size);
1533 d += dbpr;
1534 s += sbpr;
1535 } while (--h > 0);
1536 }
1537 }
1538
1539 if (src_bits)
1540 src->unlock();
1541
1542 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001543 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001544
1545 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546}
1547
Mathias Agopiana138f892010-05-21 17:24:35 -07001548// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001549
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001550status_t Surface::lock(
1551 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1552{
1553 if (mLockedBuffer != 0) {
1554 ALOGE("Surface::lock failed, already locked");
1555 return INVALID_OPERATION;
1556 }
1557
1558 if (!mConnectedToCpu) {
1559 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1560 if (err) {
1561 return err;
1562 }
1563 // we're intending to do software rendering from this point
1564 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1565 }
1566
1567 ANativeWindowBuffer* out;
1568 int fenceFd = -1;
1569 status_t err = dequeueBuffer(&out, &fenceFd);
1570 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1571 if (err == NO_ERROR) {
1572 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001573 const Rect bounds(backBuffer->width, backBuffer->height);
1574
1575 Region newDirtyRegion;
1576 if (inOutDirtyBounds) {
1577 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1578 newDirtyRegion.andSelf(bounds);
1579 } else {
1580 newDirtyRegion.set(bounds);
1581 }
1582
1583 // figure out if we can copy the frontbuffer back
1584 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1585 const bool canCopyBack = (frontBuffer != 0 &&
1586 backBuffer->width == frontBuffer->width &&
1587 backBuffer->height == frontBuffer->height &&
1588 backBuffer->format == frontBuffer->format);
1589
1590 if (canCopyBack) {
1591 // copy the area that is invalid and not repainted this round
1592 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001593 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001594 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001595 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001596 } else {
1597 // if we can't copy-back anything, modify the user's dirty
1598 // region to make sure they redraw the whole buffer
1599 newDirtyRegion.set(bounds);
1600 mDirtyRegion.clear();
1601 Mutex::Autolock lock(mMutex);
1602 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1603 mSlots[i].dirtyRegion.clear();
1604 }
1605 }
1606
1607
1608 { // scope for the lock
1609 Mutex::Autolock lock(mMutex);
1610 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1611 if (backBufferSlot >= 0) {
1612 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1613 mDirtyRegion.subtract(dirtyRegion);
1614 dirtyRegion = newDirtyRegion;
1615 }
1616 }
1617
1618 mDirtyRegion.orSelf(newDirtyRegion);
1619 if (inOutDirtyBounds) {
1620 *inOutDirtyBounds = newDirtyRegion.getBounds();
1621 }
1622
1623 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001624 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001625 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001626 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001627
1628 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1629 backBuffer->handle);
1630
1631 if (res != 0) {
1632 err = INVALID_OPERATION;
1633 } else {
1634 mLockedBuffer = backBuffer;
1635 outBuffer->width = backBuffer->width;
1636 outBuffer->height = backBuffer->height;
1637 outBuffer->stride = backBuffer->stride;
1638 outBuffer->format = backBuffer->format;
1639 outBuffer->bits = vaddr;
1640 }
1641 }
1642 return err;
1643}
1644
1645status_t Surface::unlockAndPost()
1646{
1647 if (mLockedBuffer == 0) {
1648 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1649 return INVALID_OPERATION;
1650 }
1651
Francis Hart8f396012014-04-01 15:30:53 +03001652 int fd = -1;
1653 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001654 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1655
Francis Hart8f396012014-04-01 15:30:53 +03001656 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001657 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1658 mLockedBuffer->handle, strerror(-err));
1659
1660 mPostedBuffer = mLockedBuffer;
1661 mLockedBuffer = 0;
1662 return err;
1663}
1664
Robert Carr9f31e292016-04-11 11:15:32 -07001665bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1666 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001667 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001668 return true;
1669 }
1670 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1671}
1672
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001673status_t Surface::getUniqueId(uint64_t* outId) const {
1674 Mutex::Autolock lock(mMutex);
1675 return mGraphicBufferProducer->getUniqueId(outId);
1676}
1677
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001678status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1679 if (out == nullptr) {
1680 ALOGE("%s: out must not be null!", __FUNCTION__);
1681 return BAD_VALUE;
1682 }
1683
1684 Mutex::Autolock lock(mMutex);
1685 *out = mRemovedBuffers;
1686 mRemovedBuffers.clear();
1687 return OK;
1688}
1689
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001690}; // namespace android