blob: d471dbf04776f2c340371eb55e326e54569a50e0 [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
Mathias Agopiane3c697f2013-02-14 17:11:02 -080047Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070048 const sp<IGraphicBufferProducer>& bufferProducer,
49 bool controlledByApp)
Dan Stoza812ed062015-06-02 15:45:22 -070050 : mGraphicBufferProducer(bufferProducer),
Pablo Ceballos60d69222015-08-07 14:47:20 -070051 mCrop(Rect::EMPTY_RECT),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080052 mGenerationNumber(0),
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070053 mSharedBufferMode(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080054 mAutoRefresh(false),
55 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
Pablo Ceballosbc8c1922016-07-01 14:15:41 -070056 mSharedBufferHasBeenQueued(false),
Brian Anderson6b376712017-04-04 10:51:39 -070057 mQueriedSupportedTimestamps(false),
58 mFrameTimestampsSupportsPresent(false),
Brian Anderson3da8d272016-07-28 16:20:47 -070059 mEnableFrameTimestamps(false),
60 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>())
Mathias Agopian62185b72009-04-16 16:19:50 -070061{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080062 // Initialize the ANativeWindow function pointers.
63 ANativeWindow::setSwapInterval = hook_setSwapInterval;
64 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
65 ANativeWindow::cancelBuffer = hook_cancelBuffer;
66 ANativeWindow::queueBuffer = hook_queueBuffer;
67 ANativeWindow::query = hook_query;
68 ANativeWindow::perform = hook_perform;
69
70 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
71 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
72 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
73 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
74
75 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
76 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
77
78 mReqWidth = 0;
79 mReqHeight = 0;
80 mReqFormat = 0;
81 mReqUsage = 0;
82 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080083 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080084 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
85 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070086 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080087 mDefaultWidth = 0;
88 mDefaultHeight = 0;
89 mUserWidth = 0;
90 mUserHeight = 0;
91 mTransformHint = 0;
92 mConsumerRunningBehind = false;
93 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070094 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070095 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070096}
97
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070098Surface::~Surface() {
99 if (mConnectedToCpu) {
100 Surface::disconnect(NATIVE_WINDOW_API_CPU);
101 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800102}
103
Brian Anderson3da8d272016-07-28 16:20:47 -0700104sp<ISurfaceComposer> Surface::composerService() const {
105 return ComposerService::getComposerService();
106}
107
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800108nsecs_t Surface::now() const {
109 return systemTime();
110}
111
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800112sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
113 return mGraphicBufferProducer;
114}
115
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900116void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
117 mGraphicBufferProducer->setSidebandStream(stream);
118}
119
Dan Stoza29a3e902014-06-20 13:13:57 -0700120void Surface::allocateBuffers() {
121 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
122 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700123 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
124 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700125}
126
Dan Stoza812ed062015-06-02 15:45:22 -0700127status_t Surface::setGenerationNumber(uint32_t generation) {
128 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
129 if (result == NO_ERROR) {
130 mGenerationNumber = generation;
131 }
132 return result;
133}
134
Dan Stoza7dde5992015-05-22 09:51:44 -0700135uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700136 Mutex::Autolock lock(mMutex);
137 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700138}
139
Dan Stozac6f30bd2015-06-08 09:32:50 -0700140String8 Surface::getConsumerName() const {
141 return mGraphicBufferProducer->getConsumerName();
142}
143
Dan Stoza127fc632015-06-30 13:43:32 -0700144status_t Surface::setDequeueTimeout(nsecs_t timeout) {
145 return mGraphicBufferProducer->setDequeueTimeout(timeout);
146}
147
Dan Stoza50101d02016-04-07 16:53:23 -0700148status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700149 sp<Fence>* outFence, float outTransformMatrix[16]) {
150 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
151 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700152}
153
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800154status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
155 ATRACE_CALL();
156
157 DisplayStatInfo stats;
158 status_t err = composerService()->getDisplayStats(NULL, &stats);
159
160 *outRefreshDuration = stats.vsyncPeriod;
161
162 return NO_ERROR;
163}
164
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700165void Surface::enableFrameTimestamps(bool enable) {
166 Mutex::Autolock lock(mMutex);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800167 // If going from disabled to enabled, get the initial values for
168 // compositor and display timing.
169 if (!mEnableFrameTimestamps && enable) {
170 FrameEventHistoryDelta delta;
171 mGraphicBufferProducer->getFrameTimestamps(&delta);
172 mFrameEventHistory->applyDelta(delta);
173 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700174 mEnableFrameTimestamps = enable;
175}
176
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800177status_t Surface::getCompositorTiming(
178 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
179 nsecs_t* compositeToPresentLatency) {
180 Mutex::Autolock lock(mMutex);
181 if (!mEnableFrameTimestamps) {
182 return INVALID_OPERATION;
183 }
184
185 if (compositeDeadline != nullptr) {
186 *compositeDeadline =
187 mFrameEventHistory->getNextCompositeDeadline(now());
188 }
189 if (compositeInterval != nullptr) {
190 *compositeInterval = mFrameEventHistory->getCompositeInterval();
191 }
192 if (compositeToPresentLatency != nullptr) {
193 *compositeToPresentLatency =
194 mFrameEventHistory->getCompositeToPresentLatency();
195 }
196 return NO_ERROR;
197}
198
Brian Anderson50143b32016-09-30 14:01:24 -0700199static bool checkConsumerForUpdates(
200 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700201 const nsecs_t* outLatchTime,
202 const nsecs_t* outFirstRefreshStartTime,
203 const nsecs_t* outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700204 const nsecs_t* outGpuCompositionDoneTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700205 const nsecs_t* outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700206 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700207 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700208 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
209 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700210 !e->hasFirstRefreshStartInfo();
Brian Andersonb04c6f02016-10-21 12:57:46 -0700211 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700212 !e->hasGpuCompositionDoneInfo();
213 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
214 !e->hasDisplayPresentInfo();
215
Brian Anderson6b376712017-04-04 10:51:39 -0700216 // LastRefreshStart, DequeueReady, and Release are never available for the
217 // last frame.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700218 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
219 !e->hasLastRefreshStartInfo() &&
220 (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700221 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
222 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700223 bool checkForRelease = (outReleaseTime != nullptr) &&
224 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
225
226 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700227 return checkForLatch || checkForFirstRefreshStart ||
Brian Andersonb04c6f02016-10-21 12:57:46 -0700228 checkForLastRefreshStart || checkForGpuCompositionDone ||
Brian Anderson4e606e32017-03-16 15:34:57 -0700229 checkForDisplayPresent || checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700230}
231
Brian Anderson3d4039d2016-09-23 16:31:30 -0700232static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
233 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700234 // We always get valid timestamps for these eventually.
235 *dst = (src == FrameEvents::TIMESTAMP_PENDING) ?
236 NATIVE_WINDOW_TIMESTAMP_PENDING : src;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700237 }
238}
239
Brian Andersondc96fdf2017-03-20 16:54:25 -0700240static void getFrameTimestampFence(nsecs_t *dst,
241 const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700242 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700243 if (!fenceShouldBeKnown) {
244 *dst = NATIVE_WINDOW_TIMESTAMP_PENDING;
245 return;
246 }
247
Brian Anderson3d4039d2016-09-23 16:31:30 -0700248 nsecs_t signalTime = src->getSignalTime();
Brian Andersondc96fdf2017-03-20 16:54:25 -0700249 *dst = (signalTime == Fence::SIGNAL_TIME_PENDING) ?
250 NATIVE_WINDOW_TIMESTAMP_PENDING :
251 (signalTime == Fence::SIGNAL_TIME_INVALID) ?
252 NATIVE_WINDOW_TIMESTAMP_INVALID :
253 signalTime;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700254 }
255}
256
Brian Anderson069b3652016-07-22 10:32:47 -0700257status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700258 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700259 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700260 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700261 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
262 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800263 ATRACE_CALL();
264
Brian Anderson3890c392016-07-25 12:48:08 -0700265 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700266
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700267 if (!mEnableFrameTimestamps) {
268 return INVALID_OPERATION;
269 }
270
Brian Anderson6b376712017-04-04 10:51:39 -0700271 // Verify the requested timestamps are supported.
272 querySupportedTimestampsLocked();
273 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
274 return BAD_VALUE;
275 }
276
Brian Anderson3da8d272016-07-28 16:20:47 -0700277 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700278 if (events == nullptr) {
279 // If the entry isn't available in the producer, it's definitely not
280 // available in the consumer.
281 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700282 }
283
Brian Anderson50143b32016-09-30 14:01:24 -0700284 // Update our cache of events if the requested events are not available.
285 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700286 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700287 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700288 outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700289 FrameEventHistoryDelta delta;
290 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700291 mFrameEventHistory->applyDelta(delta);
292 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700293 }
294
Brian Anderson3890c392016-07-25 12:48:08 -0700295 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700296 // The entry was available before the update, but was overwritten
297 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700298 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800299 }
Brian Anderson069b3652016-07-22 10:32:47 -0700300
Brian Anderson3d4039d2016-09-23 16:31:30 -0700301 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700302 getFrameTimestamp(outLatchTime, events->latchTime);
303 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
304 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
305 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700306
Brian Andersondc96fdf2017-03-20 16:54:25 -0700307 getFrameTimestampFence(outAcquireTime, events->acquireFence,
308 events->hasAcquireInfo());
309 getFrameTimestampFence(outGpuCompositionDoneTime,
310 events->gpuCompositionDoneFence,
311 events->hasGpuCompositionDoneInfo());
312 getFrameTimestampFence(outDisplayPresentTime, events->displayPresentFence,
313 events->hasDisplayPresentInfo());
314 getFrameTimestampFence(outReleaseTime, events->releaseFence,
315 events->hasReleaseInfo());
Brian Anderson069b3652016-07-22 10:32:47 -0700316
317 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800318}
319
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600320using namespace android::hardware::configstore;
321using namespace android::hardware::configstore::V1_0;
322
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700323status_t Surface::getWideColorSupport(bool* supported) {
324 ATRACE_CALL();
325
326 sp<IBinder> display(
327 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
328 Vector<android_color_mode_t> colorModes;
329 status_t err =
330 composerService()->getDisplayColorModes(display, &colorModes);
331
332 if (err)
333 return err;
334
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600335 bool wideColorBoardConfig =
336 getBool<ISurfaceFlingerConfigs,
337 &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
338
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700339 *supported = false;
340 for (android_color_mode_t colorMode : colorModes) {
341 switch (colorMode) {
342 case HAL_COLOR_MODE_DISPLAY_P3:
343 case HAL_COLOR_MODE_ADOBE_RGB:
344 case HAL_COLOR_MODE_DCI_P3:
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600345 if (wideColorBoardConfig) {
346 *supported = true;
347 }
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700348 break;
349 default:
350 break;
351 }
352 }
353
354 return NO_ERROR;
355}
356
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700357status_t Surface::getHdrSupport(bool* supported) {
358 ATRACE_CALL();
359
360 sp<IBinder> display(
361 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
362 HdrCapabilities hdrCapabilities;
363 status_t err =
364 composerService()->getHdrCapabilities(display, &hdrCapabilities);
365
366 if (err)
367 return err;
368
369 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
370
371 return NO_ERROR;
372}
373
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800374int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
375 Surface* c = getSelf(window);
376 return c->setSwapInterval(interval);
377}
378
379int Surface::hook_dequeueBuffer(ANativeWindow* window,
380 ANativeWindowBuffer** buffer, int* fenceFd) {
381 Surface* c = getSelf(window);
382 return c->dequeueBuffer(buffer, fenceFd);
383}
384
385int Surface::hook_cancelBuffer(ANativeWindow* window,
386 ANativeWindowBuffer* buffer, int fenceFd) {
387 Surface* c = getSelf(window);
388 return c->cancelBuffer(buffer, fenceFd);
389}
390
391int Surface::hook_queueBuffer(ANativeWindow* window,
392 ANativeWindowBuffer* buffer, int fenceFd) {
393 Surface* c = getSelf(window);
394 return c->queueBuffer(buffer, fenceFd);
395}
396
397int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
398 ANativeWindowBuffer** buffer) {
399 Surface* c = getSelf(window);
400 ANativeWindowBuffer* buf;
401 int fenceFd = -1;
402 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600403 if (result != OK) {
404 return result;
405 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800406 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700407 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800408 if (waitResult != OK) {
409 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
410 waitResult);
411 c->cancelBuffer(buf, -1);
412 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700413 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800414 *buffer = buf;
415 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700416}
417
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800418int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
419 ANativeWindowBuffer* buffer) {
420 Surface* c = getSelf(window);
421 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700422}
423
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800424int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
425 ANativeWindowBuffer* buffer) {
426 Surface* c = getSelf(window);
427 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700428}
429
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800430int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
431 ANativeWindowBuffer* buffer) {
432 Surface* c = getSelf(window);
433 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700434}
Mathias Agopian62185b72009-04-16 16:19:50 -0700435
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800436int Surface::hook_query(const ANativeWindow* window,
437 int what, int* value) {
438 const Surface* c = getSelf(window);
439 return c->query(what, value);
440}
441
442int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
443 va_list args;
444 va_start(args, operation);
445 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700446 int result = c->perform(operation, args);
447 va_end(args);
448 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800449}
450
451int Surface::setSwapInterval(int interval) {
452 ATRACE_CALL();
453 // EGL specification states:
454 // interval is silently clamped to minimum and maximum implementation
455 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800456
457 if (interval < minSwapInterval)
458 interval = minSwapInterval;
459
460 if (interval > maxSwapInterval)
461 interval = maxSwapInterval;
462
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700463 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700464 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800465
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700466 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800467}
468
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700469int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800470 ATRACE_CALL();
471 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800472
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800473 uint32_t reqWidth;
474 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800475 PixelFormat reqFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700476 uint64_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700477 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800478
479 {
480 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700481 if (mReportRemovedBuffers) {
482 mRemovedBuffers.clear();
483 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800484
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800485 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
486 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800487
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800488 reqFormat = mReqFormat;
489 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800490
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700491 enableFrameTimestamps = mEnableFrameTimestamps;
492
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700493 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800494 BufferItem::INVALID_BUFFER_SLOT) {
495 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
496 if (gbuf != NULL) {
497 *buffer = gbuf.get();
498 *fenceFd = -1;
499 return OK;
500 }
501 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800502 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
503
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800504 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800505 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700506 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700507
508 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700509 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700510 reqWidth, reqHeight, reqFormat, reqUsage,
511 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700512 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800513
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800514 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700515 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700516 "(%d, %d, %d, %#" PRIx64 ") failed: %d",
517 reqWidth, reqHeight, reqFormat, reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800518 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700519 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800520
Dan Stoza90ce2a92017-05-01 16:31:53 -0700521 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
522 ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf);
523 android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging
524 return FAILED_TRANSACTION;
525 }
526
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800527 Mutex::Autolock lock(mMutex);
528
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800529 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700530
531 // this should never happen
532 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
533
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800534 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
535 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700536 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700537
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700538 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700539 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700540 }
541
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700542 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
543 if (mReportRemovedBuffers && (gbuf != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700544 mRemovedBuffers.push_back(gbuf);
545 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800546 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
547 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700548 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700549 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800550 return result;
551 }
552 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700553
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800554 if (fence->isValid()) {
555 *fenceFd = fence->dup();
556 if (*fenceFd == -1) {
557 ALOGE("dequeueBuffer: error duping fence: %d", errno);
558 // dup() should never fail; something is badly wrong. Soldier on
559 // and hope for the best; the worst that should happen is some
560 // visible corruption that lasts until the next frame.
561 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700562 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800563 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700564 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800565
566 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800567
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700568 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800569 mSharedBufferSlot = buf;
570 mSharedBufferHasBeenQueued = false;
571 } else if (mSharedBufferSlot == buf) {
572 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
573 mSharedBufferHasBeenQueued = false;
574 }
575
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800576 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700577}
578
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800579int Surface::cancelBuffer(android_native_buffer_t* buffer,
580 int fenceFd) {
581 ATRACE_CALL();
582 ALOGV("Surface::cancelBuffer");
583 Mutex::Autolock lock(mMutex);
584 int i = getSlotFromBufferLocked(buffer);
585 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900586 if (fenceFd >= 0) {
587 close(fenceFd);
588 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800589 return i;
590 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800591 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
592 if (fenceFd >= 0) {
593 close(fenceFd);
594 }
595 return OK;
596 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800597 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
598 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800599
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700600 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800601 mSharedBufferHasBeenQueued = true;
602 }
603
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800604 return OK;
605}
606
607int Surface::getSlotFromBufferLocked(
608 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800609 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
610 if (mSlots[i].buffer != NULL &&
611 mSlots[i].buffer->handle == buffer->handle) {
612 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700613 }
614 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800615 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
616 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700617}
618
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800619int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800620 ALOGV("Surface::lockBuffer");
621 Mutex::Autolock lock(mMutex);
622 return OK;
623}
Mathias Agopian631f3582010-05-25 17:51:34 -0700624
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800625int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
626 ATRACE_CALL();
627 ALOGV("Surface::queueBuffer");
628 Mutex::Autolock lock(mMutex);
629 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700630 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800631
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800632 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700633 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700634 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700635 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700636 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800637 } else {
638 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700639 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800640 int i = getSlotFromBufferLocked(buffer);
641 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900642 if (fenceFd >= 0) {
643 close(fenceFd);
644 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800645 return i;
646 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800647 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
648 if (fenceFd >= 0) {
649 close(fenceFd);
650 }
651 return OK;
652 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800655 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700656 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800657 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800659 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
660 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700661 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800662 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700663 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700664
Dan Stozac62acbd2015-04-21 16:42:49 -0700665 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700666 input.setSurfaceDamage(Region::INVALID_REGION);
667 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700668 // Here we do two things:
669 // 1) The surface damage was specified using the OpenGL ES convention of
670 // the origin being in the bottom-left corner. Here we flip to the
671 // convention that the rest of the system uses (top-left corner) by
672 // subtracting all top/bottom coordinates from the buffer height.
673 // 2) If the buffer is coming in rotated (for example, because the EGL
674 // implementation is reacting to the transform hint coming back from
675 // SurfaceFlinger), the surface damage needs to be rotated the
676 // opposite direction, since it was generated assuming an unrotated
677 // buffer (the app doesn't know that the EGL implementation is
678 // reacting to the transform hint behind its back). The
679 // transformations in the switch statement below apply those
680 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
681
682 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700683 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700684 bool rotated90 = (mTransform ^ mStickyTransform) &
685 NATIVE_WINDOW_TRANSFORM_ROT_90;
686 if (rotated90) {
687 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700688 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700689
Dan Stoza5065a552015-03-17 16:23:42 -0700690 Region flippedRegion;
691 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700692 int left = rect.left;
693 int right = rect.right;
694 int top = height - rect.bottom; // Flip from OpenGL convention
695 int bottom = height - rect.top; // Flip from OpenGL convention
696 switch (mTransform ^ mStickyTransform) {
697 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
698 // Rotate 270 degrees
699 Rect flippedRect{top, width - right, bottom, width - left};
700 flippedRegion.orSelf(flippedRect);
701 break;
702 }
703 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
704 // Rotate 180 degrees
705 Rect flippedRect{width - right, height - bottom,
706 width - left, height - top};
707 flippedRegion.orSelf(flippedRect);
708 break;
709 }
710 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
711 // Rotate 90 degrees
712 Rect flippedRect{height - bottom, left,
713 height - top, right};
714 flippedRegion.orSelf(flippedRect);
715 break;
716 }
717 default: {
718 Rect flippedRect{left, top, right, bottom};
719 flippedRegion.orSelf(flippedRect);
720 break;
721 }
722 }
Dan Stoza5065a552015-03-17 16:23:42 -0700723 }
724
725 input.setSurfaceDamage(flippedRegion);
726 }
727
Dan Stoza70ccba52016-07-01 14:00:40 -0700728 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800729 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700730 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800731 if (err != OK) {
732 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
733 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800734
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700735 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700736 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700737 // Update timestamps with the local acquire fence.
738 // The consumer doesn't send it back to prevent us from having two
739 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700740 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700741 std::make_shared<FenceTime>(std::move(fence)));
742
743 // Cache timestamps of signaled fences so we can close their file
744 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700745 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700746 }
747
Brian Anderson50143b32016-09-30 14:01:24 -0700748 mLastFrameNumber = mNextFrameNumber;
749
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700750 mDefaultWidth = output.width;
751 mDefaultHeight = output.height;
752 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700753
Ruben Brunk1681d952014-06-27 15:51:55 -0700754 // Disable transform hint if sticky transform is set.
755 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700756 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700757 }
758
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700759 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700760
Dan Stozac62acbd2015-04-21 16:42:49 -0700761 if (!mConnectedToCpu) {
762 // Clear surface damage back to full-buffer
763 mDirtyRegion = Region::INVALID_REGION;
764 }
Dan Stoza5065a552015-03-17 16:23:42 -0700765
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700766 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800767 mSharedBufferHasBeenQueued = true;
768 }
769
Robert Carr9f31e292016-04-11 11:15:32 -0700770 mQueueBufferCondition.broadcast();
771
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800772 return err;
773}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700774
Brian Anderson6b376712017-04-04 10:51:39 -0700775void Surface::querySupportedTimestampsLocked() const {
776 // mMutex must be locked when calling this method.
777
778 if (mQueriedSupportedTimestamps) {
779 return;
780 }
781 mQueriedSupportedTimestamps = true;
782
783 std::vector<FrameEvent> supportedFrameTimestamps;
784 status_t err = composerService()->getSupportedFrameTimestamps(
785 &supportedFrameTimestamps);
786
787 if (err != NO_ERROR) {
788 return;
789 }
790
791 for (auto sft : supportedFrameTimestamps) {
792 if (sft == FrameEvent::DISPLAY_PRESENT) {
793 mFrameTimestampsSupportsPresent = true;
794 }
795 }
796}
797
Mathias Agopiana67932f2011-04-20 14:20:59 -0700798int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800799 ATRACE_CALL();
800 ALOGV("Surface::query");
801 { // scope for the lock
802 Mutex::Autolock lock(mMutex);
803 switch (what) {
804 case NATIVE_WINDOW_FORMAT:
805 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800806 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800807 return NO_ERROR;
808 }
809 break;
810 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700811 if (composerService()->authenticateSurfaceTexture(
812 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800813 *value = 1;
814 } else {
815 *value = 0;
816 }
817 return NO_ERROR;
818 }
819 case NATIVE_WINDOW_CONCRETE_TYPE:
820 *value = NATIVE_WINDOW_SURFACE;
821 return NO_ERROR;
822 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800823 *value = static_cast<int>(
824 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800825 return NO_ERROR;
826 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800827 *value = static_cast<int>(
828 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800829 return NO_ERROR;
830 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800831 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800832 return NO_ERROR;
833 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
834 status_t err = NO_ERROR;
835 if (!mConsumerRunningBehind) {
836 *value = 0;
837 } else {
838 err = mGraphicBufferProducer->query(what, value);
839 if (err == NO_ERROR) {
840 mConsumerRunningBehind = *value;
841 }
842 }
843 return err;
844 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700845 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
846 int64_t durationUs = mLastDequeueDuration / 1000;
847 *value = durationUs > std::numeric_limits<int>::max() ?
848 std::numeric_limits<int>::max() :
849 static_cast<int>(durationUs);
850 return NO_ERROR;
851 }
852 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
853 int64_t durationUs = mLastQueueDuration / 1000;
854 *value = durationUs > std::numeric_limits<int>::max() ?
855 std::numeric_limits<int>::max() :
856 static_cast<int>(durationUs);
857 return NO_ERROR;
858 }
Brian Anderson6b376712017-04-04 10:51:39 -0700859 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
860 querySupportedTimestampsLocked();
861 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
862 return NO_ERROR;
863 }
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800864 case NATIVE_WINDOW_IS_VALID: {
865 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
866 return NO_ERROR;
867 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800868 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700869 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800870 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800871}
872
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800873int Surface::perform(int operation, va_list args)
874{
875 int res = NO_ERROR;
876 switch (operation) {
877 case NATIVE_WINDOW_CONNECT:
878 // deprecated. must return NO_ERROR.
879 break;
880 case NATIVE_WINDOW_DISCONNECT:
881 // deprecated. must return NO_ERROR.
882 break;
883 case NATIVE_WINDOW_SET_USAGE:
884 res = dispatchSetUsage(args);
885 break;
886 case NATIVE_WINDOW_SET_CROP:
887 res = dispatchSetCrop(args);
888 break;
889 case NATIVE_WINDOW_SET_BUFFER_COUNT:
890 res = dispatchSetBufferCount(args);
891 break;
892 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
893 res = dispatchSetBuffersGeometry(args);
894 break;
895 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
896 res = dispatchSetBuffersTransform(args);
897 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700898 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
899 res = dispatchSetBuffersStickyTransform(args);
900 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800901 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
902 res = dispatchSetBuffersTimestamp(args);
903 break;
904 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
905 res = dispatchSetBuffersDimensions(args);
906 break;
907 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
908 res = dispatchSetBuffersUserDimensions(args);
909 break;
910 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
911 res = dispatchSetBuffersFormat(args);
912 break;
913 case NATIVE_WINDOW_LOCK:
914 res = dispatchLock(args);
915 break;
916 case NATIVE_WINDOW_UNLOCK_AND_POST:
917 res = dispatchUnlockAndPost(args);
918 break;
919 case NATIVE_WINDOW_SET_SCALING_MODE:
920 res = dispatchSetScalingMode(args);
921 break;
922 case NATIVE_WINDOW_API_CONNECT:
923 res = dispatchConnect(args);
924 break;
925 case NATIVE_WINDOW_API_DISCONNECT:
926 res = dispatchDisconnect(args);
927 break;
Rachad7cb0d392014-07-29 17:53:53 -0700928 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
929 res = dispatchSetSidebandStream(args);
930 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800931 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
932 res = dispatchSetBuffersDataSpace(args);
933 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700934 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
935 res = dispatchSetSurfaceDamage(args);
936 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700937 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
938 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700939 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800940 case NATIVE_WINDOW_SET_AUTO_REFRESH:
941 res = dispatchSetAutoRefresh(args);
942 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800943 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
944 res = dispatchGetDisplayRefreshCycleDuration(args);
945 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800946 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
947 res = dispatchGetNextFrameId(args);
948 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700949 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
950 res = dispatchEnableFrameTimestamps(args);
951 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800952 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
953 res = dispatchGetCompositorTiming(args);
954 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800955 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
956 res = dispatchGetFrameTimestamps(args);
957 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700958 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
959 res = dispatchGetWideColorSupport(args);
960 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700961 case NATIVE_WINDOW_GET_HDR_SUPPORT:
962 res = dispatchGetHdrSupport(args);
963 break;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700964 case NATIVE_WINDOW_SET_USAGE64:
965 res = dispatchSetUsage64(args);
966 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800967 default:
968 res = NAME_NOT_FOUND;
969 break;
970 }
971 return res;
972}
Mathias Agopiana138f892010-05-21 17:24:35 -0700973
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800974int Surface::dispatchConnect(va_list args) {
975 int api = va_arg(args, int);
976 return connect(api);
977}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800978
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800979int Surface::dispatchDisconnect(va_list args) {
980 int api = va_arg(args, int);
981 return disconnect(api);
982}
983
984int Surface::dispatchSetUsage(va_list args) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700985 uint64_t usage = va_arg(args, uint32_t);
986 return setUsage(usage);
987}
988
989int Surface::dispatchSetUsage64(va_list args) {
990 uint64_t usage = va_arg(args, uint64_t);
991 return setUsage(usage);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800992}
993
994int Surface::dispatchSetCrop(va_list args) {
995 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
996 return setCrop(reinterpret_cast<Rect const*>(rect));
997}
998
999int Surface::dispatchSetBufferCount(va_list args) {
1000 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001001 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001002}
1003
1004int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001005 uint32_t width = va_arg(args, uint32_t);
1006 uint32_t height = va_arg(args, uint32_t);
1007 PixelFormat format = va_arg(args, PixelFormat);
1008 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001009 if (err != 0) {
1010 return err;
1011 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001012 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001013}
1014
1015int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001016 uint32_t width = va_arg(args, uint32_t);
1017 uint32_t height = va_arg(args, uint32_t);
1018 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001019}
1020
1021int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001022 uint32_t width = va_arg(args, uint32_t);
1023 uint32_t height = va_arg(args, uint32_t);
1024 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001025}
1026
1027int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001028 PixelFormat format = va_arg(args, PixelFormat);
1029 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001030}
1031
1032int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001033 int mode = va_arg(args, int);
1034 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001035}
1036
1037int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001038 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001039 return setBuffersTransform(transform);
1040}
1041
Ruben Brunk1681d952014-06-27 15:51:55 -07001042int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001043 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -07001044 return setBuffersStickyTransform(transform);
1045}
1046
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001047int Surface::dispatchSetBuffersTimestamp(va_list args) {
1048 int64_t timestamp = va_arg(args, int64_t);
1049 return setBuffersTimestamp(timestamp);
1050}
1051
1052int Surface::dispatchLock(va_list args) {
1053 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1054 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1055 return lock(outBuffer, inOutDirtyBounds);
1056}
1057
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001058int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001059 return unlockAndPost();
1060}
1061
Rachad7cb0d392014-07-29 17:53:53 -07001062int Surface::dispatchSetSidebandStream(va_list args) {
1063 native_handle_t* sH = va_arg(args, native_handle_t*);
1064 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1065 setSidebandStream(sidebandHandle);
1066 return OK;
1067}
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001068
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001069int Surface::dispatchSetBuffersDataSpace(va_list args) {
1070 android_dataspace dataspace =
1071 static_cast<android_dataspace>(va_arg(args, int));
1072 return setBuffersDataSpace(dataspace);
1073}
1074
Dan Stoza5065a552015-03-17 16:23:42 -07001075int Surface::dispatchSetSurfaceDamage(va_list args) {
1076 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1077 size_t numRects = va_arg(args, size_t);
1078 setSurfaceDamage(rects, numRects);
1079 return NO_ERROR;
1080}
1081
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001082int Surface::dispatchSetSharedBufferMode(va_list args) {
1083 bool sharedBufferMode = va_arg(args, int);
1084 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001085}
1086
1087int Surface::dispatchSetAutoRefresh(va_list args) {
1088 bool autoRefresh = va_arg(args, int);
1089 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001090}
1091
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001092int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1093 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1094 return getDisplayRefreshCycleDuration(outRefreshDuration);
1095}
1096
Brian Anderson1049d1d2016-12-16 17:25:57 -08001097int Surface::dispatchGetNextFrameId(va_list args) {
1098 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1099 *nextFrameId = getNextFrameNumber();
1100 return NO_ERROR;
1101}
1102
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001103int Surface::dispatchEnableFrameTimestamps(va_list args) {
1104 bool enable = va_arg(args, int);
1105 enableFrameTimestamps(enable);
1106 return NO_ERROR;
1107}
1108
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001109int Surface::dispatchGetCompositorTiming(va_list args) {
1110 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1111 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1112 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1113 return getCompositorTiming(compositeDeadline, compositeInterval,
1114 compositeToPresentLatency);
1115}
1116
Pablo Ceballosce796e72016-02-04 19:10:51 -08001117int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001118 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001119 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001120 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001121 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1122 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1123 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001124 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001125 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001126 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001127 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001128 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001129 outRequestedPresentTime, outAcquireTime, outLatchTime,
1130 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001131 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001132 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001133}
1134
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001135int Surface::dispatchGetWideColorSupport(va_list args) {
1136 bool* outSupport = va_arg(args, bool*);
1137 return getWideColorSupport(outSupport);
1138}
1139
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001140int Surface::dispatchGetHdrSupport(va_list args) {
1141 bool* outSupport = va_arg(args, bool*);
1142 return getHdrSupport(outSupport);
1143}
1144
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001145int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001146 static sp<IProducerListener> listener = new DummyProducerListener();
1147 return connect(api, listener);
1148}
1149
1150int Surface::connect(int api, const sp<IProducerListener>& listener) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001151 return connect(api, listener, false);
1152}
1153
1154int Surface::connect(
1155 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001156 ATRACE_CALL();
1157 ALOGV("Surface::connect");
1158 Mutex::Autolock lock(mMutex);
1159 IGraphicBufferProducer::QueueBufferOutput output;
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001160 mReportRemovedBuffers = reportBufferRemoval;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001161 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001162 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001163 mDefaultWidth = output.width;
1164 mDefaultHeight = output.height;
1165 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001166
1167 // Disable transform hint if sticky transform is set.
1168 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001169 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001170 }
1171
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001172 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001173 }
1174 if (!err && api == NATIVE_WINDOW_API_CPU) {
1175 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001176 // Clear the dirty region in case we're switching from a non-CPU API
1177 mDirtyRegion.clear();
1178 } else if (!err) {
1179 // Initialize the dirty region for tracking surface damage
1180 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001181 }
Dan Stoza5065a552015-03-17 16:23:42 -07001182
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001183 return err;
1184}
1185
Mathias Agopian365857d2013-09-11 19:35:45 -07001186
Robert Carr97b9c862016-09-08 13:54:35 -07001187int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001188 ATRACE_CALL();
1189 ALOGV("Surface::disconnect");
1190 Mutex::Autolock lock(mMutex);
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001191 mRemovedBuffers.clear();
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001192 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1193 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001194 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001195 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001196 if (!err) {
1197 mReqFormat = 0;
1198 mReqWidth = 0;
1199 mReqHeight = 0;
1200 mReqUsage = 0;
1201 mCrop.clear();
1202 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1203 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001204 mStickyTransform = 0;
1205
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001206 if (api == NATIVE_WINDOW_API_CPU) {
1207 mConnectedToCpu = false;
1208 }
1209 }
1210 return err;
1211}
1212
Dan Stozad9c49712015-04-27 11:06:01 -07001213int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001214 sp<Fence>* outFence) {
1215 ATRACE_CALL();
1216 ALOGV("Surface::detachNextBuffer");
1217
1218 if (outBuffer == NULL || outFence == NULL) {
1219 return BAD_VALUE;
1220 }
1221
1222 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001223 if (mReportRemovedBuffers) {
1224 mRemovedBuffers.clear();
1225 }
Dan Stoza231832e2015-03-11 11:55:01 -07001226
1227 sp<GraphicBuffer> buffer(NULL);
1228 sp<Fence> fence(NULL);
1229 status_t result = mGraphicBufferProducer->detachNextBuffer(
1230 &buffer, &fence);
1231 if (result != NO_ERROR) {
1232 return result;
1233 }
1234
Dan Stozad9c49712015-04-27 11:06:01 -07001235 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001236 if (fence != NULL && fence->isValid()) {
1237 *outFence = fence;
1238 } else {
1239 *outFence = Fence::NO_FENCE;
1240 }
1241
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001242 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1243 if (mSlots[i].buffer != NULL &&
Shuzhen Wang55be5052017-05-23 09:41:54 -07001244 mSlots[i].buffer->getId() == buffer->getId()) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001245 if (mReportRemovedBuffers) {
1246 mRemovedBuffers.push_back(mSlots[i].buffer);
1247 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001248 mSlots[i].buffer = NULL;
1249 }
1250 }
1251
Dan Stoza231832e2015-03-11 11:55:01 -07001252 return NO_ERROR;
1253}
1254
1255int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1256{
1257 ATRACE_CALL();
1258 ALOGV("Surface::attachBuffer");
1259
1260 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001261 if (mReportRemovedBuffers) {
1262 mRemovedBuffers.clear();
1263 }
Dan Stoza231832e2015-03-11 11:55:01 -07001264
1265 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001266 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1267 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001268 int32_t attachedSlot = -1;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001269 status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer);
Dan Stoza231832e2015-03-11 11:55:01 -07001270 if (result != NO_ERROR) {
1271 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001272 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001273 return result;
1274 }
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001275 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001276 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1277 }
Dan Stoza231832e2015-03-11 11:55:01 -07001278 mSlots[attachedSlot].buffer = graphicBuffer;
1279
1280 return NO_ERROR;
1281}
1282
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001283int Surface::setUsage(uint64_t reqUsage)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001284{
1285 ALOGV("Surface::setUsage");
1286 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001287 if (reqUsage != mReqUsage) {
1288 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1289 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001290 mReqUsage = reqUsage;
1291 return OK;
1292}
1293
1294int Surface::setCrop(Rect const* rect)
1295{
1296 ATRACE_CALL();
1297
Pablo Ceballos60d69222015-08-07 14:47:20 -07001298 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001299 if (rect == NULL || rect->isEmpty()) {
1300 realRect.clear();
1301 } else {
1302 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001303 }
1304
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001305 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1306 realRect.left, realRect.top, realRect.right, realRect.bottom);
1307
1308 Mutex::Autolock lock(mMutex);
1309 mCrop = realRect;
1310 return NO_ERROR;
1311}
1312
1313int Surface::setBufferCount(int bufferCount)
1314{
1315 ATRACE_CALL();
1316 ALOGV("Surface::setBufferCount");
1317 Mutex::Autolock lock(mMutex);
1318
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001319 status_t err = NO_ERROR;
1320 if (bufferCount == 0) {
1321 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1322 } else {
1323 int minUndequeuedBuffers = 0;
1324 err = mGraphicBufferProducer->query(
1325 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1326 if (err == NO_ERROR) {
1327 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1328 bufferCount - minUndequeuedBuffers);
1329 }
1330 }
Mathias Agopian90147262010-01-22 11:47:55 -08001331
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001332 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1333 bufferCount, strerror(-err));
1334
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001335 return err;
1336}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001337
Pablo Ceballosfa455352015-08-12 17:47:47 -07001338int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1339 ATRACE_CALL();
1340 ALOGV("Surface::setMaxDequeuedBufferCount");
1341 Mutex::Autolock lock(mMutex);
1342
1343 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1344 maxDequeuedBuffers);
1345 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1346 "returned %s", maxDequeuedBuffers, strerror(-err));
1347
Pablo Ceballosfa455352015-08-12 17:47:47 -07001348 return err;
1349}
1350
1351int Surface::setAsyncMode(bool async) {
1352 ATRACE_CALL();
1353 ALOGV("Surface::setAsyncMode");
1354 Mutex::Autolock lock(mMutex);
1355
1356 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1357 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1358 async, strerror(-err));
1359
Pablo Ceballosfa455352015-08-12 17:47:47 -07001360 return err;
1361}
1362
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001363int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001364 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001365 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001366 Mutex::Autolock lock(mMutex);
1367
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001368 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1369 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001370 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001371 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001372 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001373 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1374 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001375
1376 return err;
1377}
1378
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001379int Surface::setAutoRefresh(bool autoRefresh) {
1380 ATRACE_CALL();
1381 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1382 Mutex::Autolock lock(mMutex);
1383
1384 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1385 if (err == NO_ERROR) {
1386 mAutoRefresh = autoRefresh;
1387 }
1388 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1389 autoRefresh, strerror(-err));
1390 return err;
1391}
1392
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001393int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001394{
1395 ATRACE_CALL();
1396 ALOGV("Surface::setBuffersDimensions");
1397
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001398 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001399 return BAD_VALUE;
1400
1401 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001402 if (width != mReqWidth || height != mReqHeight) {
1403 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1404 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001405 mReqWidth = width;
1406 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001407 return NO_ERROR;
1408}
1409
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001410int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001411{
1412 ATRACE_CALL();
1413 ALOGV("Surface::setBuffersUserDimensions");
1414
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001415 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001416 return BAD_VALUE;
1417
1418 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001419 if (width != mUserWidth || height != mUserHeight) {
1420 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1421 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001422 mUserWidth = width;
1423 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001424 return NO_ERROR;
1425}
1426
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001427int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001428{
1429 ALOGV("Surface::setBuffersFormat");
1430
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001431 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001432 if (format != mReqFormat) {
1433 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1434 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001435 mReqFormat = format;
1436 return NO_ERROR;
1437}
1438
1439int Surface::setScalingMode(int mode)
1440{
1441 ATRACE_CALL();
1442 ALOGV("Surface::setScalingMode(%d)", mode);
1443
1444 switch (mode) {
1445 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1446 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1447 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001448 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001449 break;
1450 default:
1451 ALOGE("unknown scaling mode: %d", mode);
1452 return BAD_VALUE;
1453 }
1454
1455 Mutex::Autolock lock(mMutex);
1456 mScalingMode = mode;
1457 return NO_ERROR;
1458}
1459
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001460int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001461{
1462 ATRACE_CALL();
1463 ALOGV("Surface::setBuffersTransform");
1464 Mutex::Autolock lock(mMutex);
1465 mTransform = transform;
1466 return NO_ERROR;
1467}
1468
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001469int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001470{
1471 ATRACE_CALL();
1472 ALOGV("Surface::setBuffersStickyTransform");
1473 Mutex::Autolock lock(mMutex);
1474 mStickyTransform = transform;
1475 return NO_ERROR;
1476}
1477
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001478int Surface::setBuffersTimestamp(int64_t timestamp)
1479{
1480 ALOGV("Surface::setBuffersTimestamp");
1481 Mutex::Autolock lock(mMutex);
1482 mTimestamp = timestamp;
1483 return NO_ERROR;
1484}
1485
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001486int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1487{
1488 ALOGV("Surface::setBuffersDataSpace");
1489 Mutex::Autolock lock(mMutex);
1490 mDataSpace = dataSpace;
1491 return NO_ERROR;
1492}
1493
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001494void Surface::freeAllBuffers() {
1495 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1496 mSlots[i].buffer = 0;
1497 }
1498}
1499
Dan Stoza5065a552015-03-17 16:23:42 -07001500void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1501 ATRACE_CALL();
1502 ALOGV("Surface::setSurfaceDamage");
1503 Mutex::Autolock lock(mMutex);
1504
Dan Stozac62acbd2015-04-21 16:42:49 -07001505 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001506 mDirtyRegion = Region::INVALID_REGION;
1507 return;
1508 }
1509
1510 mDirtyRegion.clear();
1511 for (size_t r = 0; r < numRects; ++r) {
1512 // We intentionally flip top and bottom here, since because they're
1513 // specified with a bottom-left origin, top > bottom, which fails
1514 // validation in the Region class. We will fix this up when we flip to a
1515 // top-left origin in queueBuffer.
1516 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1517 mDirtyRegion.orSelf(rect);
1518 }
1519}
1520
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001521// ----------------------------------------------------------------------
1522// the lock/unlock APIs must be used from the same thread
1523
1524static status_t copyBlt(
1525 const sp<GraphicBuffer>& dst,
1526 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001527 const Region& reg,
1528 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001529{
Dennis Kempinadd458f2017-05-17 19:16:31 -07001530 if (dst->getId() == src->getId())
1531 return OK;
1532
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001533 // src and dst with, height and format must be identical. no verification
1534 // is done here.
1535 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001536 uint8_t* src_bits = NULL;
1537 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1538 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001539 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1540
1541 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001542 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1543 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001544 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001545 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001546
1547 Region::const_iterator head(reg.begin());
1548 Region::const_iterator tail(reg.end());
1549 if (head != tail && src_bits && dst_bits) {
1550 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001551 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1552 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001553
1554 while (head != tail) {
1555 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001556 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001557 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001558 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1559 uint8_t const * s = src_bits +
1560 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1561 uint8_t * d = dst_bits +
1562 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001563 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001564 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001565 h = 1;
1566 }
1567 do {
1568 memcpy(d, s, size);
1569 d += dbpr;
1570 s += sbpr;
1571 } while (--h > 0);
1572 }
1573 }
1574
1575 if (src_bits)
1576 src->unlock();
1577
1578 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001579 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001580
1581 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582}
1583
Mathias Agopiana138f892010-05-21 17:24:35 -07001584// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001585
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001586status_t Surface::lock(
1587 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1588{
1589 if (mLockedBuffer != 0) {
1590 ALOGE("Surface::lock failed, already locked");
1591 return INVALID_OPERATION;
1592 }
1593
1594 if (!mConnectedToCpu) {
1595 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1596 if (err) {
1597 return err;
1598 }
1599 // we're intending to do software rendering from this point
1600 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1601 }
1602
1603 ANativeWindowBuffer* out;
1604 int fenceFd = -1;
1605 status_t err = dequeueBuffer(&out, &fenceFd);
1606 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1607 if (err == NO_ERROR) {
1608 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001609 const Rect bounds(backBuffer->width, backBuffer->height);
1610
1611 Region newDirtyRegion;
1612 if (inOutDirtyBounds) {
1613 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1614 newDirtyRegion.andSelf(bounds);
1615 } else {
1616 newDirtyRegion.set(bounds);
1617 }
1618
1619 // figure out if we can copy the frontbuffer back
1620 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1621 const bool canCopyBack = (frontBuffer != 0 &&
1622 backBuffer->width == frontBuffer->width &&
1623 backBuffer->height == frontBuffer->height &&
1624 backBuffer->format == frontBuffer->format);
1625
1626 if (canCopyBack) {
1627 // copy the area that is invalid and not repainted this round
1628 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001629 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001630 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001631 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001632 } else {
1633 // if we can't copy-back anything, modify the user's dirty
1634 // region to make sure they redraw the whole buffer
1635 newDirtyRegion.set(bounds);
1636 mDirtyRegion.clear();
1637 Mutex::Autolock lock(mMutex);
1638 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1639 mSlots[i].dirtyRegion.clear();
1640 }
1641 }
1642
1643
1644 { // scope for the lock
1645 Mutex::Autolock lock(mMutex);
1646 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1647 if (backBufferSlot >= 0) {
1648 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1649 mDirtyRegion.subtract(dirtyRegion);
1650 dirtyRegion = newDirtyRegion;
1651 }
1652 }
1653
1654 mDirtyRegion.orSelf(newDirtyRegion);
1655 if (inOutDirtyBounds) {
1656 *inOutDirtyBounds = newDirtyRegion.getBounds();
1657 }
1658
1659 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001660 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001661 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001662 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001663
1664 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1665 backBuffer->handle);
1666
1667 if (res != 0) {
1668 err = INVALID_OPERATION;
1669 } else {
1670 mLockedBuffer = backBuffer;
1671 outBuffer->width = backBuffer->width;
1672 outBuffer->height = backBuffer->height;
1673 outBuffer->stride = backBuffer->stride;
1674 outBuffer->format = backBuffer->format;
1675 outBuffer->bits = vaddr;
1676 }
1677 }
1678 return err;
1679}
1680
1681status_t Surface::unlockAndPost()
1682{
1683 if (mLockedBuffer == 0) {
1684 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1685 return INVALID_OPERATION;
1686 }
1687
Francis Hart8f396012014-04-01 15:30:53 +03001688 int fd = -1;
1689 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001690 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1691
Francis Hart8f396012014-04-01 15:30:53 +03001692 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001693 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1694 mLockedBuffer->handle, strerror(-err));
1695
1696 mPostedBuffer = mLockedBuffer;
1697 mLockedBuffer = 0;
1698 return err;
1699}
1700
Robert Carr9f31e292016-04-11 11:15:32 -07001701bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1702 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001703 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001704 return true;
1705 }
1706 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1707}
1708
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001709status_t Surface::getUniqueId(uint64_t* outId) const {
1710 Mutex::Autolock lock(mMutex);
1711 return mGraphicBufferProducer->getUniqueId(outId);
1712}
1713
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001714status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1715 if (out == nullptr) {
1716 ALOGE("%s: out must not be null!", __FUNCTION__);
1717 return BAD_VALUE;
1718 }
1719
1720 Mutex::Autolock lock(mMutex);
1721 *out = mRemovedBuffers;
1722 mRemovedBuffers.clear();
1723 return OK;
1724}
1725
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726}; // namespace android