blob: 78eb69d13235a9ac675243f1fb62351ffe18f411 [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 Agopiancb496ac2017-05-22 14:21:00 -070023#include <inttypes.h>
24
Mathias Agopian05debe12017-02-08 17:04:18 -080025#include <android/native_window.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080026
Mathias Agopian9cce3252010-02-09 17:46:37 -080027#include <utils/Log.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080028#include <utils/Trace.h>
Rachad7cb0d392014-07-29 17:53:53 -070029#include <utils/NativeHandle.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080030
Ian Elliott62c48c92017-01-20 13:13:20 -070031#include <ui/DisplayStatInfo.h>
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -070032#include <ui/Fence.h>
33#include <ui/HdrCapabilities.h>
34#include <ui/Region.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070035
Mathias Agopian2b5dd402017-02-07 17:36:19 -080036#include <gui/BufferItem.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070037#include <gui/IProducerListener.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080038
Mathias Agopian2b5dd402017-02-07 17:36:19 -080039#include <gui/ISurfaceComposer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080040#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -060042#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
43#include <configstore/Utils.h>
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045namespace android {
46
Ian Elliotta2eb34c2017-07-18 11:05:49 -060047Surface::Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp)
48 : mGraphicBufferProducer(bufferProducer),
49 mCrop(Rect::EMPTY_RECT),
50 mBufferAge(0),
51 mGenerationNumber(0),
52 mSharedBufferMode(false),
53 mAutoRefresh(false),
54 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
55 mSharedBufferHasBeenQueued(false),
56 mQueriedSupportedTimestamps(false),
57 mFrameTimestampsSupportsPresent(false),
58 mEnableFrameTimestamps(false),
59 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>()) {
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 Andersondc96fdf2017-03-20 16:54:25 -0700232 // We always get valid timestamps for these eventually.
233 *dst = (src == FrameEvents::TIMESTAMP_PENDING) ?
234 NATIVE_WINDOW_TIMESTAMP_PENDING : src;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700235 }
236}
237
Brian Andersondc96fdf2017-03-20 16:54:25 -0700238static void getFrameTimestampFence(nsecs_t *dst,
239 const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700240 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700241 if (!fenceShouldBeKnown) {
242 *dst = NATIVE_WINDOW_TIMESTAMP_PENDING;
243 return;
244 }
245
Brian Anderson3d4039d2016-09-23 16:31:30 -0700246 nsecs_t signalTime = src->getSignalTime();
Brian Andersondc96fdf2017-03-20 16:54:25 -0700247 *dst = (signalTime == Fence::SIGNAL_TIME_PENDING) ?
248 NATIVE_WINDOW_TIMESTAMP_PENDING :
249 (signalTime == Fence::SIGNAL_TIME_INVALID) ?
250 NATIVE_WINDOW_TIMESTAMP_INVALID :
251 signalTime;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700252 }
253}
254
Brian Anderson069b3652016-07-22 10:32:47 -0700255status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700256 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700257 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700258 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700259 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
260 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800261 ATRACE_CALL();
262
Brian Anderson3890c392016-07-25 12:48:08 -0700263 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700264
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700265 if (!mEnableFrameTimestamps) {
266 return INVALID_OPERATION;
267 }
268
Brian Anderson6b376712017-04-04 10:51:39 -0700269 // Verify the requested timestamps are supported.
270 querySupportedTimestampsLocked();
271 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
272 return BAD_VALUE;
273 }
274
Brian Anderson3da8d272016-07-28 16:20:47 -0700275 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700276 if (events == nullptr) {
277 // If the entry isn't available in the producer, it's definitely not
278 // available in the consumer.
279 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700280 }
281
Brian Anderson50143b32016-09-30 14:01:24 -0700282 // Update our cache of events if the requested events are not available.
283 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700284 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700285 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700286 outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700287 FrameEventHistoryDelta delta;
288 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700289 mFrameEventHistory->applyDelta(delta);
290 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700291 }
292
Brian Anderson3890c392016-07-25 12:48:08 -0700293 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700294 // The entry was available before the update, but was overwritten
295 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700296 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800297 }
Brian Anderson069b3652016-07-22 10:32:47 -0700298
Brian Anderson3d4039d2016-09-23 16:31:30 -0700299 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700300 getFrameTimestamp(outLatchTime, events->latchTime);
301 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
302 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
303 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700304
Brian Andersondc96fdf2017-03-20 16:54:25 -0700305 getFrameTimestampFence(outAcquireTime, events->acquireFence,
306 events->hasAcquireInfo());
307 getFrameTimestampFence(outGpuCompositionDoneTime,
308 events->gpuCompositionDoneFence,
309 events->hasGpuCompositionDoneInfo());
310 getFrameTimestampFence(outDisplayPresentTime, events->displayPresentFence,
311 events->hasDisplayPresentInfo());
312 getFrameTimestampFence(outReleaseTime, events->releaseFence,
313 events->hasReleaseInfo());
Brian Anderson069b3652016-07-22 10:32:47 -0700314
315 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800316}
317
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600318using namespace android::hardware::configstore;
319using namespace android::hardware::configstore::V1_0;
320
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700321status_t Surface::getWideColorSupport(bool* supported) {
322 ATRACE_CALL();
323
324 sp<IBinder> display(
325 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
326 Vector<android_color_mode_t> colorModes;
327 status_t err =
328 composerService()->getDisplayColorModes(display, &colorModes);
329
330 if (err)
331 return err;
332
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600333 bool wideColorBoardConfig =
334 getBool<ISurfaceFlingerConfigs,
335 &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
336
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700337 *supported = false;
338 for (android_color_mode_t colorMode : colorModes) {
339 switch (colorMode) {
340 case HAL_COLOR_MODE_DISPLAY_P3:
341 case HAL_COLOR_MODE_ADOBE_RGB:
342 case HAL_COLOR_MODE_DCI_P3:
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600343 if (wideColorBoardConfig) {
344 *supported = true;
345 }
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700346 break;
347 default:
348 break;
349 }
350 }
351
352 return NO_ERROR;
353}
354
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700355status_t Surface::getHdrSupport(bool* supported) {
356 ATRACE_CALL();
357
358 sp<IBinder> display(
359 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
360 HdrCapabilities hdrCapabilities;
361 status_t err =
362 composerService()->getHdrCapabilities(display, &hdrCapabilities);
363
364 if (err)
365 return err;
366
367 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
368
369 return NO_ERROR;
370}
371
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800372int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
373 Surface* c = getSelf(window);
374 return c->setSwapInterval(interval);
375}
376
377int Surface::hook_dequeueBuffer(ANativeWindow* window,
378 ANativeWindowBuffer** buffer, int* fenceFd) {
379 Surface* c = getSelf(window);
380 return c->dequeueBuffer(buffer, fenceFd);
381}
382
383int Surface::hook_cancelBuffer(ANativeWindow* window,
384 ANativeWindowBuffer* buffer, int fenceFd) {
385 Surface* c = getSelf(window);
386 return c->cancelBuffer(buffer, fenceFd);
387}
388
389int Surface::hook_queueBuffer(ANativeWindow* window,
390 ANativeWindowBuffer* buffer, int fenceFd) {
391 Surface* c = getSelf(window);
392 return c->queueBuffer(buffer, fenceFd);
393}
394
395int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
396 ANativeWindowBuffer** buffer) {
397 Surface* c = getSelf(window);
398 ANativeWindowBuffer* buf;
399 int fenceFd = -1;
400 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600401 if (result != OK) {
402 return result;
403 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800404 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700405 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800406 if (waitResult != OK) {
407 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
408 waitResult);
409 c->cancelBuffer(buf, -1);
410 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700411 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800412 *buffer = buf;
413 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700414}
415
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800416int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
417 ANativeWindowBuffer* buffer) {
418 Surface* c = getSelf(window);
419 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700420}
421
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800422int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
423 ANativeWindowBuffer* buffer) {
424 Surface* c = getSelf(window);
425 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700426}
427
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800428int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
429 ANativeWindowBuffer* buffer) {
430 Surface* c = getSelf(window);
431 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700432}
Mathias Agopian62185b72009-04-16 16:19:50 -0700433
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800434int Surface::hook_query(const ANativeWindow* window,
435 int what, int* value) {
436 const Surface* c = getSelf(window);
437 return c->query(what, value);
438}
439
440int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
441 va_list args;
442 va_start(args, operation);
443 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700444 int result = c->perform(operation, args);
445 va_end(args);
446 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800447}
448
449int Surface::setSwapInterval(int interval) {
450 ATRACE_CALL();
451 // EGL specification states:
452 // interval is silently clamped to minimum and maximum implementation
453 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800454
455 if (interval < minSwapInterval)
456 interval = minSwapInterval;
457
458 if (interval > maxSwapInterval)
459 interval = maxSwapInterval;
460
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700461 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700462 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800463
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700464 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800465}
466
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700467int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800468 ATRACE_CALL();
469 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800470
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800471 uint32_t reqWidth;
472 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800473 PixelFormat reqFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700474 uint64_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700475 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800476
477 {
478 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700479 if (mReportRemovedBuffers) {
480 mRemovedBuffers.clear();
481 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800482
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800483 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
484 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800485
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800486 reqFormat = mReqFormat;
487 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800488
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700489 enableFrameTimestamps = mEnableFrameTimestamps;
490
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700491 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800492 BufferItem::INVALID_BUFFER_SLOT) {
493 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
494 if (gbuf != NULL) {
495 *buffer = gbuf.get();
496 *fenceFd = -1;
497 return OK;
498 }
499 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800500 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
501
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800502 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800503 sp<Fence> fence;
Dan Stoza932f0082017-05-31 13:50:16 -0700504 nsecs_t startTime = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700505
506 FrameEventHistoryDelta frameTimestamps;
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600507 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, reqWidth, reqHeight,
508 reqFormat, reqUsage, &mBufferAge,
509 enableFrameTimestamps ? &frameTimestamps
510 : nullptr);
Dan Stoza932f0082017-05-31 13:50:16 -0700511 mLastDequeueDuration = systemTime() - startTime;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800512
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800513 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700514 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700515 "(%d, %d, %d, %#" PRIx64 ") failed: %d",
516 reqWidth, reqHeight, reqFormat, reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800517 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700518 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800519
Dan Stoza90ce2a92017-05-01 16:31:53 -0700520 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
521 ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf);
522 android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging
523 return FAILED_TRANSACTION;
524 }
525
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800526 Mutex::Autolock lock(mMutex);
527
Dan Stoza932f0082017-05-31 13:50:16 -0700528 // Write this while holding the mutex
529 mLastDequeueStartTime = startTime;
530
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800531 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700532
533 // this should never happen
534 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
535
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800536 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
537 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700538 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700539
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700540 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700541 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700542 }
543
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700544 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
545 if (mReportRemovedBuffers && (gbuf != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700546 mRemovedBuffers.push_back(gbuf);
547 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800548 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
549 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700550 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700551 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800552 return result;
553 }
554 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700555
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800556 if (fence->isValid()) {
557 *fenceFd = fence->dup();
558 if (*fenceFd == -1) {
559 ALOGE("dequeueBuffer: error duping fence: %d", errno);
560 // dup() should never fail; something is badly wrong. Soldier on
561 // and hope for the best; the worst that should happen is some
562 // visible corruption that lasts until the next frame.
563 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700564 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800565 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700566 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800567
568 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800569
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700570 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800571 mSharedBufferSlot = buf;
572 mSharedBufferHasBeenQueued = false;
573 } else if (mSharedBufferSlot == buf) {
574 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
575 mSharedBufferHasBeenQueued = false;
576 }
577
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800578 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700579}
580
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800581int Surface::cancelBuffer(android_native_buffer_t* buffer,
582 int fenceFd) {
583 ATRACE_CALL();
584 ALOGV("Surface::cancelBuffer");
585 Mutex::Autolock lock(mMutex);
586 int i = getSlotFromBufferLocked(buffer);
587 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900588 if (fenceFd >= 0) {
589 close(fenceFd);
590 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800591 return i;
592 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800593 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
594 if (fenceFd >= 0) {
595 close(fenceFd);
596 }
597 return OK;
598 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800599 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
600 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800601
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700602 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800603 mSharedBufferHasBeenQueued = true;
604 }
605
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800606 return OK;
607}
608
609int Surface::getSlotFromBufferLocked(
610 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800611 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
612 if (mSlots[i].buffer != NULL &&
613 mSlots[i].buffer->handle == buffer->handle) {
614 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700615 }
616 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800617 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
618 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700619}
620
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800621int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800622 ALOGV("Surface::lockBuffer");
623 Mutex::Autolock lock(mMutex);
624 return OK;
625}
Mathias Agopian631f3582010-05-25 17:51:34 -0700626
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800627int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
628 ATRACE_CALL();
629 ALOGV("Surface::queueBuffer");
630 Mutex::Autolock lock(mMutex);
631 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700632 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800633
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800634 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700635 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700636 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700637 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700638 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800639 } else {
640 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700641 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800642 int i = getSlotFromBufferLocked(buffer);
643 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900644 if (fenceFd >= 0) {
645 close(fenceFd);
646 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800647 return i;
648 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800649 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
650 if (fenceFd >= 0) {
651 close(fenceFd);
652 }
653 return OK;
654 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800657 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700658 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800659 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800661 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
662 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700663 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800664 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700665 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700666
Dan Stozac62acbd2015-04-21 16:42:49 -0700667 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700668 input.setSurfaceDamage(Region::INVALID_REGION);
669 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700670 // Here we do two things:
671 // 1) The surface damage was specified using the OpenGL ES convention of
672 // the origin being in the bottom-left corner. Here we flip to the
673 // convention that the rest of the system uses (top-left corner) by
674 // subtracting all top/bottom coordinates from the buffer height.
675 // 2) If the buffer is coming in rotated (for example, because the EGL
676 // implementation is reacting to the transform hint coming back from
677 // SurfaceFlinger), the surface damage needs to be rotated the
678 // opposite direction, since it was generated assuming an unrotated
679 // buffer (the app doesn't know that the EGL implementation is
680 // reacting to the transform hint behind its back). The
681 // transformations in the switch statement below apply those
682 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
683
684 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700685 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700686 bool rotated90 = (mTransform ^ mStickyTransform) &
687 NATIVE_WINDOW_TRANSFORM_ROT_90;
688 if (rotated90) {
689 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700690 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700691
Dan Stoza5065a552015-03-17 16:23:42 -0700692 Region flippedRegion;
693 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700694 int left = rect.left;
695 int right = rect.right;
696 int top = height - rect.bottom; // Flip from OpenGL convention
697 int bottom = height - rect.top; // Flip from OpenGL convention
698 switch (mTransform ^ mStickyTransform) {
699 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
700 // Rotate 270 degrees
701 Rect flippedRect{top, width - right, bottom, width - left};
702 flippedRegion.orSelf(flippedRect);
703 break;
704 }
705 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
706 // Rotate 180 degrees
707 Rect flippedRect{width - right, height - bottom,
708 width - left, height - top};
709 flippedRegion.orSelf(flippedRect);
710 break;
711 }
712 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
713 // Rotate 90 degrees
714 Rect flippedRect{height - bottom, left,
715 height - top, right};
716 flippedRegion.orSelf(flippedRect);
717 break;
718 }
719 default: {
720 Rect flippedRect{left, top, right, bottom};
721 flippedRegion.orSelf(flippedRect);
722 break;
723 }
724 }
Dan Stoza5065a552015-03-17 16:23:42 -0700725 }
726
727 input.setSurfaceDamage(flippedRegion);
728 }
729
Dan Stoza70ccba52016-07-01 14:00:40 -0700730 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800731 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700732 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800733 if (err != OK) {
734 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
735 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800736
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700737 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700738 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700739 // Update timestamps with the local acquire fence.
740 // The consumer doesn't send it back to prevent us from having two
741 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700742 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700743 std::make_shared<FenceTime>(std::move(fence)));
744
745 // Cache timestamps of signaled fences so we can close their file
746 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700747 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700748 }
749
Brian Anderson50143b32016-09-30 14:01:24 -0700750 mLastFrameNumber = mNextFrameNumber;
751
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700752 mDefaultWidth = output.width;
753 mDefaultHeight = output.height;
754 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700755
Ruben Brunk1681d952014-06-27 15:51:55 -0700756 // Disable transform hint if sticky transform is set.
757 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700758 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700759 }
760
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700761 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700762
Dan Stozac62acbd2015-04-21 16:42:49 -0700763 if (!mConnectedToCpu) {
764 // Clear surface damage back to full-buffer
765 mDirtyRegion = Region::INVALID_REGION;
766 }
Dan Stoza5065a552015-03-17 16:23:42 -0700767
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700768 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800769 mSharedBufferHasBeenQueued = true;
770 }
771
Robert Carr9f31e292016-04-11 11:15:32 -0700772 mQueueBufferCondition.broadcast();
773
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800774 return err;
775}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700776
Brian Anderson6b376712017-04-04 10:51:39 -0700777void Surface::querySupportedTimestampsLocked() const {
778 // mMutex must be locked when calling this method.
779
780 if (mQueriedSupportedTimestamps) {
781 return;
782 }
783 mQueriedSupportedTimestamps = true;
784
785 std::vector<FrameEvent> supportedFrameTimestamps;
786 status_t err = composerService()->getSupportedFrameTimestamps(
787 &supportedFrameTimestamps);
788
789 if (err != NO_ERROR) {
790 return;
791 }
792
793 for (auto sft : supportedFrameTimestamps) {
794 if (sft == FrameEvent::DISPLAY_PRESENT) {
795 mFrameTimestampsSupportsPresent = true;
796 }
797 }
798}
799
Mathias Agopiana67932f2011-04-20 14:20:59 -0700800int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800801 ATRACE_CALL();
802 ALOGV("Surface::query");
803 { // scope for the lock
804 Mutex::Autolock lock(mMutex);
805 switch (what) {
806 case NATIVE_WINDOW_FORMAT:
807 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800808 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800809 return NO_ERROR;
810 }
811 break;
812 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700813 if (composerService()->authenticateSurfaceTexture(
814 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800815 *value = 1;
816 } else {
817 *value = 0;
818 }
819 return NO_ERROR;
820 }
821 case NATIVE_WINDOW_CONCRETE_TYPE:
822 *value = NATIVE_WINDOW_SURFACE;
823 return NO_ERROR;
824 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800825 *value = static_cast<int>(
826 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800827 return NO_ERROR;
828 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800829 *value = static_cast<int>(
830 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800831 return NO_ERROR;
832 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800833 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800834 return NO_ERROR;
835 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
836 status_t err = NO_ERROR;
837 if (!mConsumerRunningBehind) {
838 *value = 0;
839 } else {
840 err = mGraphicBufferProducer->query(what, value);
841 if (err == NO_ERROR) {
842 mConsumerRunningBehind = *value;
843 }
844 }
845 return err;
846 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600847 case NATIVE_WINDOW_BUFFER_AGE: {
848 if (mBufferAge > INT32_MAX) {
849 *value = 0;
850 } else {
851 *value = static_cast<int32_t>(mBufferAge);
852 }
853 return NO_ERROR;
854 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700855 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
856 int64_t durationUs = mLastDequeueDuration / 1000;
857 *value = durationUs > std::numeric_limits<int>::max() ?
858 std::numeric_limits<int>::max() :
859 static_cast<int>(durationUs);
860 return NO_ERROR;
861 }
862 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
863 int64_t durationUs = mLastQueueDuration / 1000;
864 *value = durationUs > std::numeric_limits<int>::max() ?
865 std::numeric_limits<int>::max() :
866 static_cast<int>(durationUs);
867 return NO_ERROR;
868 }
Brian Anderson6b376712017-04-04 10:51:39 -0700869 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
870 querySupportedTimestampsLocked();
871 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
872 return NO_ERROR;
873 }
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800874 case NATIVE_WINDOW_IS_VALID: {
875 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
876 return NO_ERROR;
877 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800878 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700879 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800880 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800881}
882
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800883int Surface::perform(int operation, va_list args)
884{
885 int res = NO_ERROR;
886 switch (operation) {
887 case NATIVE_WINDOW_CONNECT:
888 // deprecated. must return NO_ERROR.
889 break;
890 case NATIVE_WINDOW_DISCONNECT:
891 // deprecated. must return NO_ERROR.
892 break;
893 case NATIVE_WINDOW_SET_USAGE:
894 res = dispatchSetUsage(args);
895 break;
896 case NATIVE_WINDOW_SET_CROP:
897 res = dispatchSetCrop(args);
898 break;
899 case NATIVE_WINDOW_SET_BUFFER_COUNT:
900 res = dispatchSetBufferCount(args);
901 break;
902 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
903 res = dispatchSetBuffersGeometry(args);
904 break;
905 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
906 res = dispatchSetBuffersTransform(args);
907 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700908 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
909 res = dispatchSetBuffersStickyTransform(args);
910 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800911 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
912 res = dispatchSetBuffersTimestamp(args);
913 break;
914 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
915 res = dispatchSetBuffersDimensions(args);
916 break;
917 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
918 res = dispatchSetBuffersUserDimensions(args);
919 break;
920 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
921 res = dispatchSetBuffersFormat(args);
922 break;
923 case NATIVE_WINDOW_LOCK:
924 res = dispatchLock(args);
925 break;
926 case NATIVE_WINDOW_UNLOCK_AND_POST:
927 res = dispatchUnlockAndPost(args);
928 break;
929 case NATIVE_WINDOW_SET_SCALING_MODE:
930 res = dispatchSetScalingMode(args);
931 break;
932 case NATIVE_WINDOW_API_CONNECT:
933 res = dispatchConnect(args);
934 break;
935 case NATIVE_WINDOW_API_DISCONNECT:
936 res = dispatchDisconnect(args);
937 break;
Rachad7cb0d392014-07-29 17:53:53 -0700938 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
939 res = dispatchSetSidebandStream(args);
940 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800941 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
942 res = dispatchSetBuffersDataSpace(args);
943 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700944 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
945 res = dispatchSetSurfaceDamage(args);
946 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700947 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
948 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700949 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800950 case NATIVE_WINDOW_SET_AUTO_REFRESH:
951 res = dispatchSetAutoRefresh(args);
952 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800953 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
954 res = dispatchGetDisplayRefreshCycleDuration(args);
955 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800956 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
957 res = dispatchGetNextFrameId(args);
958 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700959 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
960 res = dispatchEnableFrameTimestamps(args);
961 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800962 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
963 res = dispatchGetCompositorTiming(args);
964 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800965 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
966 res = dispatchGetFrameTimestamps(args);
967 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700968 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
969 res = dispatchGetWideColorSupport(args);
970 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700971 case NATIVE_WINDOW_GET_HDR_SUPPORT:
972 res = dispatchGetHdrSupport(args);
973 break;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700974 case NATIVE_WINDOW_SET_USAGE64:
975 res = dispatchSetUsage64(args);
976 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800977 default:
978 res = NAME_NOT_FOUND;
979 break;
980 }
981 return res;
982}
Mathias Agopiana138f892010-05-21 17:24:35 -0700983
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800984int Surface::dispatchConnect(va_list args) {
985 int api = va_arg(args, int);
986 return connect(api);
987}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800988
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800989int Surface::dispatchDisconnect(va_list args) {
990 int api = va_arg(args, int);
991 return disconnect(api);
992}
993
994int Surface::dispatchSetUsage(va_list args) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700995 uint64_t usage = va_arg(args, uint32_t);
996 return setUsage(usage);
997}
998
999int Surface::dispatchSetUsage64(va_list args) {
1000 uint64_t usage = va_arg(args, uint64_t);
1001 return setUsage(usage);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001002}
1003
1004int Surface::dispatchSetCrop(va_list args) {
1005 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
1006 return setCrop(reinterpret_cast<Rect const*>(rect));
1007}
1008
1009int Surface::dispatchSetBufferCount(va_list args) {
1010 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001011 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001012}
1013
1014int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001015 uint32_t width = va_arg(args, uint32_t);
1016 uint32_t height = va_arg(args, uint32_t);
1017 PixelFormat format = va_arg(args, PixelFormat);
1018 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001019 if (err != 0) {
1020 return err;
1021 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001022 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001023}
1024
1025int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001026 uint32_t width = va_arg(args, uint32_t);
1027 uint32_t height = va_arg(args, uint32_t);
1028 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001029}
1030
1031int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001032 uint32_t width = va_arg(args, uint32_t);
1033 uint32_t height = va_arg(args, uint32_t);
1034 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001035}
1036
1037int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001038 PixelFormat format = va_arg(args, PixelFormat);
1039 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001040}
1041
1042int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001043 int mode = va_arg(args, int);
1044 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001045}
1046
1047int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001048 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001049 return setBuffersTransform(transform);
1050}
1051
Ruben Brunk1681d952014-06-27 15:51:55 -07001052int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001053 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -07001054 return setBuffersStickyTransform(transform);
1055}
1056
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001057int Surface::dispatchSetBuffersTimestamp(va_list args) {
1058 int64_t timestamp = va_arg(args, int64_t);
1059 return setBuffersTimestamp(timestamp);
1060}
1061
1062int Surface::dispatchLock(va_list args) {
1063 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1064 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1065 return lock(outBuffer, inOutDirtyBounds);
1066}
1067
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001068int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001069 return unlockAndPost();
1070}
1071
Rachad7cb0d392014-07-29 17:53:53 -07001072int Surface::dispatchSetSidebandStream(va_list args) {
1073 native_handle_t* sH = va_arg(args, native_handle_t*);
1074 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1075 setSidebandStream(sidebandHandle);
1076 return OK;
1077}
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001078
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001079int Surface::dispatchSetBuffersDataSpace(va_list args) {
1080 android_dataspace dataspace =
1081 static_cast<android_dataspace>(va_arg(args, int));
1082 return setBuffersDataSpace(dataspace);
1083}
1084
Dan Stoza5065a552015-03-17 16:23:42 -07001085int Surface::dispatchSetSurfaceDamage(va_list args) {
1086 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1087 size_t numRects = va_arg(args, size_t);
1088 setSurfaceDamage(rects, numRects);
1089 return NO_ERROR;
1090}
1091
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001092int Surface::dispatchSetSharedBufferMode(va_list args) {
1093 bool sharedBufferMode = va_arg(args, int);
1094 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001095}
1096
1097int Surface::dispatchSetAutoRefresh(va_list args) {
1098 bool autoRefresh = va_arg(args, int);
1099 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001100}
1101
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001102int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1103 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1104 return getDisplayRefreshCycleDuration(outRefreshDuration);
1105}
1106
Brian Anderson1049d1d2016-12-16 17:25:57 -08001107int Surface::dispatchGetNextFrameId(va_list args) {
1108 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1109 *nextFrameId = getNextFrameNumber();
1110 return NO_ERROR;
1111}
1112
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001113int Surface::dispatchEnableFrameTimestamps(va_list args) {
1114 bool enable = va_arg(args, int);
1115 enableFrameTimestamps(enable);
1116 return NO_ERROR;
1117}
1118
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001119int Surface::dispatchGetCompositorTiming(va_list args) {
1120 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1121 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1122 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1123 return getCompositorTiming(compositeDeadline, compositeInterval,
1124 compositeToPresentLatency);
1125}
1126
Pablo Ceballosce796e72016-02-04 19:10:51 -08001127int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001128 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001129 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001130 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001131 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1132 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1133 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001134 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001135 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001136 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001137 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001138 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001139 outRequestedPresentTime, outAcquireTime, outLatchTime,
1140 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001141 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001142 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001143}
1144
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001145int Surface::dispatchGetWideColorSupport(va_list args) {
1146 bool* outSupport = va_arg(args, bool*);
1147 return getWideColorSupport(outSupport);
1148}
1149
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001150int Surface::dispatchGetHdrSupport(va_list args) {
1151 bool* outSupport = va_arg(args, bool*);
1152 return getHdrSupport(outSupport);
1153}
1154
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001155int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001156 static sp<IProducerListener> listener = new DummyProducerListener();
1157 return connect(api, listener);
1158}
1159
1160int Surface::connect(int api, const sp<IProducerListener>& listener) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001161 return connect(api, listener, false);
1162}
1163
1164int Surface::connect(
1165 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001166 ATRACE_CALL();
1167 ALOGV("Surface::connect");
1168 Mutex::Autolock lock(mMutex);
1169 IGraphicBufferProducer::QueueBufferOutput output;
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001170 mReportRemovedBuffers = reportBufferRemoval;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001171 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001172 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001173 mDefaultWidth = output.width;
1174 mDefaultHeight = output.height;
1175 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001176
1177 // Disable transform hint if sticky transform is set.
1178 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001179 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001180 }
1181
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001182 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001183 }
1184 if (!err && api == NATIVE_WINDOW_API_CPU) {
1185 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001186 // Clear the dirty region in case we're switching from a non-CPU API
1187 mDirtyRegion.clear();
1188 } else if (!err) {
1189 // Initialize the dirty region for tracking surface damage
1190 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001191 }
Dan Stoza5065a552015-03-17 16:23:42 -07001192
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001193 return err;
1194}
1195
Mathias Agopian365857d2013-09-11 19:35:45 -07001196
Robert Carr97b9c862016-09-08 13:54:35 -07001197int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001198 ATRACE_CALL();
1199 ALOGV("Surface::disconnect");
1200 Mutex::Autolock lock(mMutex);
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001201 mRemovedBuffers.clear();
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001202 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1203 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001204 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001205 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001206 if (!err) {
1207 mReqFormat = 0;
1208 mReqWidth = 0;
1209 mReqHeight = 0;
1210 mReqUsage = 0;
1211 mCrop.clear();
1212 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1213 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001214 mStickyTransform = 0;
1215
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001216 if (api == NATIVE_WINDOW_API_CPU) {
1217 mConnectedToCpu = false;
1218 }
1219 }
1220 return err;
1221}
1222
Dan Stozad9c49712015-04-27 11:06:01 -07001223int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001224 sp<Fence>* outFence) {
1225 ATRACE_CALL();
1226 ALOGV("Surface::detachNextBuffer");
1227
1228 if (outBuffer == NULL || outFence == NULL) {
1229 return BAD_VALUE;
1230 }
1231
1232 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001233 if (mReportRemovedBuffers) {
1234 mRemovedBuffers.clear();
1235 }
Dan Stoza231832e2015-03-11 11:55:01 -07001236
1237 sp<GraphicBuffer> buffer(NULL);
1238 sp<Fence> fence(NULL);
1239 status_t result = mGraphicBufferProducer->detachNextBuffer(
1240 &buffer, &fence);
1241 if (result != NO_ERROR) {
1242 return result;
1243 }
1244
Dan Stozad9c49712015-04-27 11:06:01 -07001245 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001246 if (fence != NULL && fence->isValid()) {
1247 *outFence = fence;
1248 } else {
1249 *outFence = Fence::NO_FENCE;
1250 }
1251
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001252 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1253 if (mSlots[i].buffer != NULL &&
Shuzhen Wang55be5052017-05-23 09:41:54 -07001254 mSlots[i].buffer->getId() == buffer->getId()) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001255 if (mReportRemovedBuffers) {
1256 mRemovedBuffers.push_back(mSlots[i].buffer);
1257 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001258 mSlots[i].buffer = NULL;
1259 }
1260 }
1261
Dan Stoza231832e2015-03-11 11:55:01 -07001262 return NO_ERROR;
1263}
1264
1265int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1266{
1267 ATRACE_CALL();
1268 ALOGV("Surface::attachBuffer");
1269
1270 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001271 if (mReportRemovedBuffers) {
1272 mRemovedBuffers.clear();
1273 }
Dan Stoza231832e2015-03-11 11:55:01 -07001274
1275 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001276 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1277 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001278 int32_t attachedSlot = -1;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001279 status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer);
Dan Stoza231832e2015-03-11 11:55:01 -07001280 if (result != NO_ERROR) {
1281 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001282 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001283 return result;
1284 }
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001285 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001286 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1287 }
Dan Stoza231832e2015-03-11 11:55:01 -07001288 mSlots[attachedSlot].buffer = graphicBuffer;
1289
1290 return NO_ERROR;
1291}
1292
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001293int Surface::setUsage(uint64_t reqUsage)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001294{
1295 ALOGV("Surface::setUsage");
1296 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001297 if (reqUsage != mReqUsage) {
1298 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1299 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001300 mReqUsage = reqUsage;
1301 return OK;
1302}
1303
1304int Surface::setCrop(Rect const* rect)
1305{
1306 ATRACE_CALL();
1307
Pablo Ceballos60d69222015-08-07 14:47:20 -07001308 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001309 if (rect == NULL || rect->isEmpty()) {
1310 realRect.clear();
1311 } else {
1312 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001313 }
1314
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001315 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1316 realRect.left, realRect.top, realRect.right, realRect.bottom);
1317
1318 Mutex::Autolock lock(mMutex);
1319 mCrop = realRect;
1320 return NO_ERROR;
1321}
1322
1323int Surface::setBufferCount(int bufferCount)
1324{
1325 ATRACE_CALL();
1326 ALOGV("Surface::setBufferCount");
1327 Mutex::Autolock lock(mMutex);
1328
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001329 status_t err = NO_ERROR;
1330 if (bufferCount == 0) {
1331 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1332 } else {
1333 int minUndequeuedBuffers = 0;
1334 err = mGraphicBufferProducer->query(
1335 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1336 if (err == NO_ERROR) {
1337 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1338 bufferCount - minUndequeuedBuffers);
1339 }
1340 }
Mathias Agopian90147262010-01-22 11:47:55 -08001341
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001342 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1343 bufferCount, strerror(-err));
1344
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001345 return err;
1346}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001347
Pablo Ceballosfa455352015-08-12 17:47:47 -07001348int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1349 ATRACE_CALL();
1350 ALOGV("Surface::setMaxDequeuedBufferCount");
1351 Mutex::Autolock lock(mMutex);
1352
1353 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1354 maxDequeuedBuffers);
1355 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1356 "returned %s", maxDequeuedBuffers, strerror(-err));
1357
Pablo Ceballosfa455352015-08-12 17:47:47 -07001358 return err;
1359}
1360
1361int Surface::setAsyncMode(bool async) {
1362 ATRACE_CALL();
1363 ALOGV("Surface::setAsyncMode");
1364 Mutex::Autolock lock(mMutex);
1365
1366 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1367 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1368 async, strerror(-err));
1369
Pablo Ceballosfa455352015-08-12 17:47:47 -07001370 return err;
1371}
1372
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001373int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001374 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001375 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001376 Mutex::Autolock lock(mMutex);
1377
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001378 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1379 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001380 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001381 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001382 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001383 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1384 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001385
1386 return err;
1387}
1388
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001389int Surface::setAutoRefresh(bool autoRefresh) {
1390 ATRACE_CALL();
1391 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1392 Mutex::Autolock lock(mMutex);
1393
1394 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1395 if (err == NO_ERROR) {
1396 mAutoRefresh = autoRefresh;
1397 }
1398 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1399 autoRefresh, strerror(-err));
1400 return err;
1401}
1402
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001403int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001404{
1405 ATRACE_CALL();
1406 ALOGV("Surface::setBuffersDimensions");
1407
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001408 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001409 return BAD_VALUE;
1410
1411 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001412 if (width != mReqWidth || height != mReqHeight) {
1413 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1414 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001415 mReqWidth = width;
1416 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001417 return NO_ERROR;
1418}
1419
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001420int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001421{
1422 ATRACE_CALL();
1423 ALOGV("Surface::setBuffersUserDimensions");
1424
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001425 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001426 return BAD_VALUE;
1427
1428 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001429 if (width != mUserWidth || height != mUserHeight) {
1430 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1431 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001432 mUserWidth = width;
1433 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001434 return NO_ERROR;
1435}
1436
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001437int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001438{
1439 ALOGV("Surface::setBuffersFormat");
1440
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001441 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001442 if (format != mReqFormat) {
1443 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1444 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001445 mReqFormat = format;
1446 return NO_ERROR;
1447}
1448
1449int Surface::setScalingMode(int mode)
1450{
1451 ATRACE_CALL();
1452 ALOGV("Surface::setScalingMode(%d)", mode);
1453
1454 switch (mode) {
1455 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1456 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1457 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001458 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001459 break;
1460 default:
1461 ALOGE("unknown scaling mode: %d", mode);
1462 return BAD_VALUE;
1463 }
1464
1465 Mutex::Autolock lock(mMutex);
1466 mScalingMode = mode;
1467 return NO_ERROR;
1468}
1469
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001470int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001471{
1472 ATRACE_CALL();
1473 ALOGV("Surface::setBuffersTransform");
1474 Mutex::Autolock lock(mMutex);
1475 mTransform = transform;
1476 return NO_ERROR;
1477}
1478
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001479int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001480{
1481 ATRACE_CALL();
1482 ALOGV("Surface::setBuffersStickyTransform");
1483 Mutex::Autolock lock(mMutex);
1484 mStickyTransform = transform;
1485 return NO_ERROR;
1486}
1487
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001488int Surface::setBuffersTimestamp(int64_t timestamp)
1489{
1490 ALOGV("Surface::setBuffersTimestamp");
1491 Mutex::Autolock lock(mMutex);
1492 mTimestamp = timestamp;
1493 return NO_ERROR;
1494}
1495
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001496int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1497{
1498 ALOGV("Surface::setBuffersDataSpace");
1499 Mutex::Autolock lock(mMutex);
1500 mDataSpace = dataSpace;
1501 return NO_ERROR;
1502}
1503
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001504void Surface::freeAllBuffers() {
1505 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1506 mSlots[i].buffer = 0;
1507 }
1508}
1509
Dan Stoza5065a552015-03-17 16:23:42 -07001510void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1511 ATRACE_CALL();
1512 ALOGV("Surface::setSurfaceDamage");
1513 Mutex::Autolock lock(mMutex);
1514
Dan Stozac62acbd2015-04-21 16:42:49 -07001515 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001516 mDirtyRegion = Region::INVALID_REGION;
1517 return;
1518 }
1519
1520 mDirtyRegion.clear();
1521 for (size_t r = 0; r < numRects; ++r) {
1522 // We intentionally flip top and bottom here, since because they're
1523 // specified with a bottom-left origin, top > bottom, which fails
1524 // validation in the Region class. We will fix this up when we flip to a
1525 // top-left origin in queueBuffer.
1526 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1527 mDirtyRegion.orSelf(rect);
1528 }
1529}
1530
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001531// ----------------------------------------------------------------------
1532// the lock/unlock APIs must be used from the same thread
1533
1534static status_t copyBlt(
1535 const sp<GraphicBuffer>& dst,
1536 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001537 const Region& reg,
1538 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001539{
Dennis Kempinadd458f2017-05-17 19:16:31 -07001540 if (dst->getId() == src->getId())
1541 return OK;
1542
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001543 // src and dst with, height and format must be identical. no verification
1544 // is done here.
1545 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001546 uint8_t* src_bits = NULL;
1547 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1548 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001549 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1550
1551 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001552 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1553 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001554 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001555 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001556
1557 Region::const_iterator head(reg.begin());
1558 Region::const_iterator tail(reg.end());
1559 if (head != tail && src_bits && dst_bits) {
1560 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001561 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1562 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001563
1564 while (head != tail) {
1565 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001566 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001567 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001568 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1569 uint8_t const * s = src_bits +
1570 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1571 uint8_t * d = dst_bits +
1572 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001573 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001574 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001575 h = 1;
1576 }
1577 do {
1578 memcpy(d, s, size);
1579 d += dbpr;
1580 s += sbpr;
1581 } while (--h > 0);
1582 }
1583 }
1584
1585 if (src_bits)
1586 src->unlock();
1587
1588 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001589 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001590
1591 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001592}
1593
Mathias Agopiana138f892010-05-21 17:24:35 -07001594// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001595
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001596status_t Surface::lock(
1597 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1598{
1599 if (mLockedBuffer != 0) {
1600 ALOGE("Surface::lock failed, already locked");
1601 return INVALID_OPERATION;
1602 }
1603
1604 if (!mConnectedToCpu) {
1605 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1606 if (err) {
1607 return err;
1608 }
1609 // we're intending to do software rendering from this point
1610 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1611 }
1612
1613 ANativeWindowBuffer* out;
1614 int fenceFd = -1;
1615 status_t err = dequeueBuffer(&out, &fenceFd);
1616 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1617 if (err == NO_ERROR) {
1618 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001619 const Rect bounds(backBuffer->width, backBuffer->height);
1620
1621 Region newDirtyRegion;
1622 if (inOutDirtyBounds) {
1623 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1624 newDirtyRegion.andSelf(bounds);
1625 } else {
1626 newDirtyRegion.set(bounds);
1627 }
1628
1629 // figure out if we can copy the frontbuffer back
1630 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1631 const bool canCopyBack = (frontBuffer != 0 &&
1632 backBuffer->width == frontBuffer->width &&
1633 backBuffer->height == frontBuffer->height &&
1634 backBuffer->format == frontBuffer->format);
1635
1636 if (canCopyBack) {
1637 // copy the area that is invalid and not repainted this round
1638 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001639 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001640 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001641 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001642 } else {
1643 // if we can't copy-back anything, modify the user's dirty
1644 // region to make sure they redraw the whole buffer
1645 newDirtyRegion.set(bounds);
1646 mDirtyRegion.clear();
1647 Mutex::Autolock lock(mMutex);
1648 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1649 mSlots[i].dirtyRegion.clear();
1650 }
1651 }
1652
1653
1654 { // scope for the lock
1655 Mutex::Autolock lock(mMutex);
1656 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1657 if (backBufferSlot >= 0) {
1658 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1659 mDirtyRegion.subtract(dirtyRegion);
1660 dirtyRegion = newDirtyRegion;
1661 }
1662 }
1663
1664 mDirtyRegion.orSelf(newDirtyRegion);
1665 if (inOutDirtyBounds) {
1666 *inOutDirtyBounds = newDirtyRegion.getBounds();
1667 }
1668
1669 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001670 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001671 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001672 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001673
1674 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1675 backBuffer->handle);
1676
1677 if (res != 0) {
1678 err = INVALID_OPERATION;
1679 } else {
1680 mLockedBuffer = backBuffer;
1681 outBuffer->width = backBuffer->width;
1682 outBuffer->height = backBuffer->height;
1683 outBuffer->stride = backBuffer->stride;
1684 outBuffer->format = backBuffer->format;
1685 outBuffer->bits = vaddr;
1686 }
1687 }
1688 return err;
1689}
1690
1691status_t Surface::unlockAndPost()
1692{
1693 if (mLockedBuffer == 0) {
1694 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1695 return INVALID_OPERATION;
1696 }
1697
Francis Hart8f396012014-04-01 15:30:53 +03001698 int fd = -1;
1699 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001700 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1701
Francis Hart8f396012014-04-01 15:30:53 +03001702 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001703 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1704 mLockedBuffer->handle, strerror(-err));
1705
1706 mPostedBuffer = mLockedBuffer;
1707 mLockedBuffer = 0;
1708 return err;
1709}
1710
Robert Carr9f31e292016-04-11 11:15:32 -07001711bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1712 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001713 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001714 return true;
1715 }
1716 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1717}
1718
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001719status_t Surface::getUniqueId(uint64_t* outId) const {
1720 Mutex::Autolock lock(mMutex);
1721 return mGraphicBufferProducer->getUniqueId(outId);
1722}
1723
Dan Stoza932f0082017-05-31 13:50:16 -07001724nsecs_t Surface::getLastDequeueStartTime() const {
1725 Mutex::Autolock lock(mMutex);
1726 return mLastDequeueStartTime;
1727}
1728
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001729status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1730 if (out == nullptr) {
1731 ALOGE("%s: out must not be null!", __FUNCTION__);
1732 return BAD_VALUE;
1733 }
1734
1735 Mutex::Autolock lock(mMutex);
1736 *out = mRemovedBuffers;
1737 mRemovedBuffers.clear();
1738 return OK;
1739}
1740
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001741}; // namespace android