blob: 409a3cb0761015a2a87f610c791c509027992e39 [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 Stoza932f0082017-05-31 13:50:16 -0700506 nsecs_t startTime = 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 Stoza932f0082017-05-31 13:50:16 -0700512 mLastDequeueDuration = systemTime() - startTime;
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
Dan Stoza932f0082017-05-31 13:50:16 -0700529 // Write this while holding the mutex
530 mLastDequeueStartTime = startTime;
531
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800532 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700533
534 // this should never happen
535 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
536
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800537 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
538 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700539 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700540
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700541 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700542 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700543 }
544
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700545 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
546 if (mReportRemovedBuffers && (gbuf != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700547 mRemovedBuffers.push_back(gbuf);
548 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800549 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
550 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700551 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700552 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800553 return result;
554 }
555 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700556
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800557 if (fence->isValid()) {
558 *fenceFd = fence->dup();
559 if (*fenceFd == -1) {
560 ALOGE("dequeueBuffer: error duping fence: %d", errno);
561 // dup() should never fail; something is badly wrong. Soldier on
562 // and hope for the best; the worst that should happen is some
563 // visible corruption that lasts until the next frame.
564 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700565 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800566 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700567 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800568
569 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800570
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700571 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800572 mSharedBufferSlot = buf;
573 mSharedBufferHasBeenQueued = false;
574 } else if (mSharedBufferSlot == buf) {
575 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
576 mSharedBufferHasBeenQueued = false;
577 }
578
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800579 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700580}
581
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800582int Surface::cancelBuffer(android_native_buffer_t* buffer,
583 int fenceFd) {
584 ATRACE_CALL();
585 ALOGV("Surface::cancelBuffer");
586 Mutex::Autolock lock(mMutex);
587 int i = getSlotFromBufferLocked(buffer);
588 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900589 if (fenceFd >= 0) {
590 close(fenceFd);
591 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800592 return i;
593 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800594 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
595 if (fenceFd >= 0) {
596 close(fenceFd);
597 }
598 return OK;
599 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800600 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
601 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800602
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700603 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800604 mSharedBufferHasBeenQueued = true;
605 }
606
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800607 return OK;
608}
609
610int Surface::getSlotFromBufferLocked(
611 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800612 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
613 if (mSlots[i].buffer != NULL &&
614 mSlots[i].buffer->handle == buffer->handle) {
615 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700616 }
617 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800618 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
619 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700620}
621
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800622int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800623 ALOGV("Surface::lockBuffer");
624 Mutex::Autolock lock(mMutex);
625 return OK;
626}
Mathias Agopian631f3582010-05-25 17:51:34 -0700627
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800628int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
629 ATRACE_CALL();
630 ALOGV("Surface::queueBuffer");
631 Mutex::Autolock lock(mMutex);
632 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700633 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800634
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800635 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700636 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700637 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700638 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700639 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800640 } else {
641 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700642 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800643 int i = getSlotFromBufferLocked(buffer);
644 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900645 if (fenceFd >= 0) {
646 close(fenceFd);
647 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800648 return i;
649 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800650 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
651 if (fenceFd >= 0) {
652 close(fenceFd);
653 }
654 return OK;
655 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800658 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700659 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800660 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800662 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
663 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700664 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800665 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700666 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700667
Dan Stozac62acbd2015-04-21 16:42:49 -0700668 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700669 input.setSurfaceDamage(Region::INVALID_REGION);
670 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700671 // Here we do two things:
672 // 1) The surface damage was specified using the OpenGL ES convention of
673 // the origin being in the bottom-left corner. Here we flip to the
674 // convention that the rest of the system uses (top-left corner) by
675 // subtracting all top/bottom coordinates from the buffer height.
676 // 2) If the buffer is coming in rotated (for example, because the EGL
677 // implementation is reacting to the transform hint coming back from
678 // SurfaceFlinger), the surface damage needs to be rotated the
679 // opposite direction, since it was generated assuming an unrotated
680 // buffer (the app doesn't know that the EGL implementation is
681 // reacting to the transform hint behind its back). The
682 // transformations in the switch statement below apply those
683 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
684
685 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700686 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700687 bool rotated90 = (mTransform ^ mStickyTransform) &
688 NATIVE_WINDOW_TRANSFORM_ROT_90;
689 if (rotated90) {
690 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700691 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700692
Dan Stoza5065a552015-03-17 16:23:42 -0700693 Region flippedRegion;
694 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700695 int left = rect.left;
696 int right = rect.right;
697 int top = height - rect.bottom; // Flip from OpenGL convention
698 int bottom = height - rect.top; // Flip from OpenGL convention
699 switch (mTransform ^ mStickyTransform) {
700 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
701 // Rotate 270 degrees
702 Rect flippedRect{top, width - right, bottom, width - left};
703 flippedRegion.orSelf(flippedRect);
704 break;
705 }
706 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
707 // Rotate 180 degrees
708 Rect flippedRect{width - right, height - bottom,
709 width - left, height - top};
710 flippedRegion.orSelf(flippedRect);
711 break;
712 }
713 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
714 // Rotate 90 degrees
715 Rect flippedRect{height - bottom, left,
716 height - top, right};
717 flippedRegion.orSelf(flippedRect);
718 break;
719 }
720 default: {
721 Rect flippedRect{left, top, right, bottom};
722 flippedRegion.orSelf(flippedRect);
723 break;
724 }
725 }
Dan Stoza5065a552015-03-17 16:23:42 -0700726 }
727
728 input.setSurfaceDamage(flippedRegion);
729 }
730
Dan Stoza70ccba52016-07-01 14:00:40 -0700731 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800732 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700733 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800734 if (err != OK) {
735 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
736 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800737
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700738 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700739 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700740 // Update timestamps with the local acquire fence.
741 // The consumer doesn't send it back to prevent us from having two
742 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700743 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700744 std::make_shared<FenceTime>(std::move(fence)));
745
746 // Cache timestamps of signaled fences so we can close their file
747 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700748 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700749 }
750
Brian Anderson50143b32016-09-30 14:01:24 -0700751 mLastFrameNumber = mNextFrameNumber;
752
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700753 mDefaultWidth = output.width;
754 mDefaultHeight = output.height;
755 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700756
Ruben Brunk1681d952014-06-27 15:51:55 -0700757 // Disable transform hint if sticky transform is set.
758 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700759 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700760 }
761
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700762 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700763
Dan Stozac62acbd2015-04-21 16:42:49 -0700764 if (!mConnectedToCpu) {
765 // Clear surface damage back to full-buffer
766 mDirtyRegion = Region::INVALID_REGION;
767 }
Dan Stoza5065a552015-03-17 16:23:42 -0700768
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700769 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800770 mSharedBufferHasBeenQueued = true;
771 }
772
Robert Carr9f31e292016-04-11 11:15:32 -0700773 mQueueBufferCondition.broadcast();
774
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800775 return err;
776}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700777
Brian Anderson6b376712017-04-04 10:51:39 -0700778void Surface::querySupportedTimestampsLocked() const {
779 // mMutex must be locked when calling this method.
780
781 if (mQueriedSupportedTimestamps) {
782 return;
783 }
784 mQueriedSupportedTimestamps = true;
785
786 std::vector<FrameEvent> supportedFrameTimestamps;
787 status_t err = composerService()->getSupportedFrameTimestamps(
788 &supportedFrameTimestamps);
789
790 if (err != NO_ERROR) {
791 return;
792 }
793
794 for (auto sft : supportedFrameTimestamps) {
795 if (sft == FrameEvent::DISPLAY_PRESENT) {
796 mFrameTimestampsSupportsPresent = true;
797 }
798 }
799}
800
Mathias Agopiana67932f2011-04-20 14:20:59 -0700801int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800802 ATRACE_CALL();
803 ALOGV("Surface::query");
804 { // scope for the lock
805 Mutex::Autolock lock(mMutex);
806 switch (what) {
807 case NATIVE_WINDOW_FORMAT:
808 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800809 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800810 return NO_ERROR;
811 }
812 break;
813 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700814 if (composerService()->authenticateSurfaceTexture(
815 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800816 *value = 1;
817 } else {
818 *value = 0;
819 }
820 return NO_ERROR;
821 }
822 case NATIVE_WINDOW_CONCRETE_TYPE:
823 *value = NATIVE_WINDOW_SURFACE;
824 return NO_ERROR;
825 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800826 *value = static_cast<int>(
827 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800828 return NO_ERROR;
829 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800830 *value = static_cast<int>(
831 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800832 return NO_ERROR;
833 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800834 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800835 return NO_ERROR;
836 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
837 status_t err = NO_ERROR;
838 if (!mConsumerRunningBehind) {
839 *value = 0;
840 } else {
841 err = mGraphicBufferProducer->query(what, value);
842 if (err == NO_ERROR) {
843 mConsumerRunningBehind = *value;
844 }
845 }
846 return err;
847 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700848 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
849 int64_t durationUs = mLastDequeueDuration / 1000;
850 *value = durationUs > std::numeric_limits<int>::max() ?
851 std::numeric_limits<int>::max() :
852 static_cast<int>(durationUs);
853 return NO_ERROR;
854 }
855 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
856 int64_t durationUs = mLastQueueDuration / 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 }
Brian Anderson6b376712017-04-04 10:51:39 -0700862 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
863 querySupportedTimestampsLocked();
864 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
865 return NO_ERROR;
866 }
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800867 case NATIVE_WINDOW_IS_VALID: {
868 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
869 return NO_ERROR;
870 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800871 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700872 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800873 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800874}
875
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800876int Surface::perform(int operation, va_list args)
877{
878 int res = NO_ERROR;
879 switch (operation) {
880 case NATIVE_WINDOW_CONNECT:
881 // deprecated. must return NO_ERROR.
882 break;
883 case NATIVE_WINDOW_DISCONNECT:
884 // deprecated. must return NO_ERROR.
885 break;
886 case NATIVE_WINDOW_SET_USAGE:
887 res = dispatchSetUsage(args);
888 break;
889 case NATIVE_WINDOW_SET_CROP:
890 res = dispatchSetCrop(args);
891 break;
892 case NATIVE_WINDOW_SET_BUFFER_COUNT:
893 res = dispatchSetBufferCount(args);
894 break;
895 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
896 res = dispatchSetBuffersGeometry(args);
897 break;
898 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
899 res = dispatchSetBuffersTransform(args);
900 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700901 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
902 res = dispatchSetBuffersStickyTransform(args);
903 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800904 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
905 res = dispatchSetBuffersTimestamp(args);
906 break;
907 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
908 res = dispatchSetBuffersDimensions(args);
909 break;
910 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
911 res = dispatchSetBuffersUserDimensions(args);
912 break;
913 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
914 res = dispatchSetBuffersFormat(args);
915 break;
916 case NATIVE_WINDOW_LOCK:
917 res = dispatchLock(args);
918 break;
919 case NATIVE_WINDOW_UNLOCK_AND_POST:
920 res = dispatchUnlockAndPost(args);
921 break;
922 case NATIVE_WINDOW_SET_SCALING_MODE:
923 res = dispatchSetScalingMode(args);
924 break;
925 case NATIVE_WINDOW_API_CONNECT:
926 res = dispatchConnect(args);
927 break;
928 case NATIVE_WINDOW_API_DISCONNECT:
929 res = dispatchDisconnect(args);
930 break;
Rachad7cb0d392014-07-29 17:53:53 -0700931 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
932 res = dispatchSetSidebandStream(args);
933 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800934 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
935 res = dispatchSetBuffersDataSpace(args);
936 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700937 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
938 res = dispatchSetSurfaceDamage(args);
939 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700940 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
941 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700942 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800943 case NATIVE_WINDOW_SET_AUTO_REFRESH:
944 res = dispatchSetAutoRefresh(args);
945 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800946 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
947 res = dispatchGetDisplayRefreshCycleDuration(args);
948 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800949 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
950 res = dispatchGetNextFrameId(args);
951 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700952 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
953 res = dispatchEnableFrameTimestamps(args);
954 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800955 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
956 res = dispatchGetCompositorTiming(args);
957 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800958 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
959 res = dispatchGetFrameTimestamps(args);
960 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700961 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
962 res = dispatchGetWideColorSupport(args);
963 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700964 case NATIVE_WINDOW_GET_HDR_SUPPORT:
965 res = dispatchGetHdrSupport(args);
966 break;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700967 case NATIVE_WINDOW_SET_USAGE64:
968 res = dispatchSetUsage64(args);
969 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800970 default:
971 res = NAME_NOT_FOUND;
972 break;
973 }
974 return res;
975}
Mathias Agopiana138f892010-05-21 17:24:35 -0700976
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800977int Surface::dispatchConnect(va_list args) {
978 int api = va_arg(args, int);
979 return connect(api);
980}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800981
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800982int Surface::dispatchDisconnect(va_list args) {
983 int api = va_arg(args, int);
984 return disconnect(api);
985}
986
987int Surface::dispatchSetUsage(va_list args) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700988 uint64_t usage = va_arg(args, uint32_t);
989 return setUsage(usage);
990}
991
992int Surface::dispatchSetUsage64(va_list args) {
993 uint64_t usage = va_arg(args, uint64_t);
994 return setUsage(usage);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800995}
996
997int Surface::dispatchSetCrop(va_list args) {
998 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
999 return setCrop(reinterpret_cast<Rect const*>(rect));
1000}
1001
1002int Surface::dispatchSetBufferCount(va_list args) {
1003 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001004 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001005}
1006
1007int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001008 uint32_t width = va_arg(args, uint32_t);
1009 uint32_t height = va_arg(args, uint32_t);
1010 PixelFormat format = va_arg(args, PixelFormat);
1011 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001012 if (err != 0) {
1013 return err;
1014 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001015 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001016}
1017
1018int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001019 uint32_t width = va_arg(args, uint32_t);
1020 uint32_t height = va_arg(args, uint32_t);
1021 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001022}
1023
1024int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001025 uint32_t width = va_arg(args, uint32_t);
1026 uint32_t height = va_arg(args, uint32_t);
1027 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001028}
1029
1030int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001031 PixelFormat format = va_arg(args, PixelFormat);
1032 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001033}
1034
1035int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001036 int mode = va_arg(args, int);
1037 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001038}
1039
1040int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001041 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001042 return setBuffersTransform(transform);
1043}
1044
Ruben Brunk1681d952014-06-27 15:51:55 -07001045int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001046 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -07001047 return setBuffersStickyTransform(transform);
1048}
1049
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001050int Surface::dispatchSetBuffersTimestamp(va_list args) {
1051 int64_t timestamp = va_arg(args, int64_t);
1052 return setBuffersTimestamp(timestamp);
1053}
1054
1055int Surface::dispatchLock(va_list args) {
1056 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1057 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1058 return lock(outBuffer, inOutDirtyBounds);
1059}
1060
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001061int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001062 return unlockAndPost();
1063}
1064
Rachad7cb0d392014-07-29 17:53:53 -07001065int Surface::dispatchSetSidebandStream(va_list args) {
1066 native_handle_t* sH = va_arg(args, native_handle_t*);
1067 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1068 setSidebandStream(sidebandHandle);
1069 return OK;
1070}
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001071
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001072int Surface::dispatchSetBuffersDataSpace(va_list args) {
1073 android_dataspace dataspace =
1074 static_cast<android_dataspace>(va_arg(args, int));
1075 return setBuffersDataSpace(dataspace);
1076}
1077
Dan Stoza5065a552015-03-17 16:23:42 -07001078int Surface::dispatchSetSurfaceDamage(va_list args) {
1079 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1080 size_t numRects = va_arg(args, size_t);
1081 setSurfaceDamage(rects, numRects);
1082 return NO_ERROR;
1083}
1084
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001085int Surface::dispatchSetSharedBufferMode(va_list args) {
1086 bool sharedBufferMode = va_arg(args, int);
1087 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001088}
1089
1090int Surface::dispatchSetAutoRefresh(va_list args) {
1091 bool autoRefresh = va_arg(args, int);
1092 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001093}
1094
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001095int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1096 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1097 return getDisplayRefreshCycleDuration(outRefreshDuration);
1098}
1099
Brian Anderson1049d1d2016-12-16 17:25:57 -08001100int Surface::dispatchGetNextFrameId(va_list args) {
1101 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1102 *nextFrameId = getNextFrameNumber();
1103 return NO_ERROR;
1104}
1105
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001106int Surface::dispatchEnableFrameTimestamps(va_list args) {
1107 bool enable = va_arg(args, int);
1108 enableFrameTimestamps(enable);
1109 return NO_ERROR;
1110}
1111
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001112int Surface::dispatchGetCompositorTiming(va_list args) {
1113 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1114 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1115 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1116 return getCompositorTiming(compositeDeadline, compositeInterval,
1117 compositeToPresentLatency);
1118}
1119
Pablo Ceballosce796e72016-02-04 19:10:51 -08001120int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001121 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001122 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001123 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001124 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1125 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1126 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001127 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001128 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001129 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001130 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001131 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001132 outRequestedPresentTime, outAcquireTime, outLatchTime,
1133 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001134 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001135 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001136}
1137
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001138int Surface::dispatchGetWideColorSupport(va_list args) {
1139 bool* outSupport = va_arg(args, bool*);
1140 return getWideColorSupport(outSupport);
1141}
1142
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001143int Surface::dispatchGetHdrSupport(va_list args) {
1144 bool* outSupport = va_arg(args, bool*);
1145 return getHdrSupport(outSupport);
1146}
1147
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001148int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001149 static sp<IProducerListener> listener = new DummyProducerListener();
1150 return connect(api, listener);
1151}
1152
1153int Surface::connect(int api, const sp<IProducerListener>& listener) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001154 return connect(api, listener, false);
1155}
1156
1157int Surface::connect(
1158 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001159 ATRACE_CALL();
1160 ALOGV("Surface::connect");
1161 Mutex::Autolock lock(mMutex);
1162 IGraphicBufferProducer::QueueBufferOutput output;
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001163 mReportRemovedBuffers = reportBufferRemoval;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001164 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001165 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001166 mDefaultWidth = output.width;
1167 mDefaultHeight = output.height;
1168 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001169
1170 // Disable transform hint if sticky transform is set.
1171 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001172 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001173 }
1174
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001175 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001176 }
1177 if (!err && api == NATIVE_WINDOW_API_CPU) {
1178 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001179 // Clear the dirty region in case we're switching from a non-CPU API
1180 mDirtyRegion.clear();
1181 } else if (!err) {
1182 // Initialize the dirty region for tracking surface damage
1183 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001184 }
Dan Stoza5065a552015-03-17 16:23:42 -07001185
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001186 return err;
1187}
1188
Mathias Agopian365857d2013-09-11 19:35:45 -07001189
Robert Carr97b9c862016-09-08 13:54:35 -07001190int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001191 ATRACE_CALL();
1192 ALOGV("Surface::disconnect");
1193 Mutex::Autolock lock(mMutex);
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001194 mRemovedBuffers.clear();
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001195 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1196 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001197 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001198 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001199 if (!err) {
1200 mReqFormat = 0;
1201 mReqWidth = 0;
1202 mReqHeight = 0;
1203 mReqUsage = 0;
1204 mCrop.clear();
1205 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1206 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001207 mStickyTransform = 0;
1208
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001209 if (api == NATIVE_WINDOW_API_CPU) {
1210 mConnectedToCpu = false;
1211 }
1212 }
1213 return err;
1214}
1215
Dan Stozad9c49712015-04-27 11:06:01 -07001216int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001217 sp<Fence>* outFence) {
1218 ATRACE_CALL();
1219 ALOGV("Surface::detachNextBuffer");
1220
1221 if (outBuffer == NULL || outFence == NULL) {
1222 return BAD_VALUE;
1223 }
1224
1225 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001226 if (mReportRemovedBuffers) {
1227 mRemovedBuffers.clear();
1228 }
Dan Stoza231832e2015-03-11 11:55:01 -07001229
1230 sp<GraphicBuffer> buffer(NULL);
1231 sp<Fence> fence(NULL);
1232 status_t result = mGraphicBufferProducer->detachNextBuffer(
1233 &buffer, &fence);
1234 if (result != NO_ERROR) {
1235 return result;
1236 }
1237
Dan Stozad9c49712015-04-27 11:06:01 -07001238 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001239 if (fence != NULL && fence->isValid()) {
1240 *outFence = fence;
1241 } else {
1242 *outFence = Fence::NO_FENCE;
1243 }
1244
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001245 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1246 if (mSlots[i].buffer != NULL &&
Shuzhen Wang55be5052017-05-23 09:41:54 -07001247 mSlots[i].buffer->getId() == buffer->getId()) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001248 if (mReportRemovedBuffers) {
1249 mRemovedBuffers.push_back(mSlots[i].buffer);
1250 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001251 mSlots[i].buffer = NULL;
1252 }
1253 }
1254
Dan Stoza231832e2015-03-11 11:55:01 -07001255 return NO_ERROR;
1256}
1257
1258int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1259{
1260 ATRACE_CALL();
1261 ALOGV("Surface::attachBuffer");
1262
1263 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001264 if (mReportRemovedBuffers) {
1265 mRemovedBuffers.clear();
1266 }
Dan Stoza231832e2015-03-11 11:55:01 -07001267
1268 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001269 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1270 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001271 int32_t attachedSlot = -1;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001272 status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer);
Dan Stoza231832e2015-03-11 11:55:01 -07001273 if (result != NO_ERROR) {
1274 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001275 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001276 return result;
1277 }
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001278 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001279 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1280 }
Dan Stoza231832e2015-03-11 11:55:01 -07001281 mSlots[attachedSlot].buffer = graphicBuffer;
1282
1283 return NO_ERROR;
1284}
1285
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001286int Surface::setUsage(uint64_t reqUsage)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001287{
1288 ALOGV("Surface::setUsage");
1289 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001290 if (reqUsage != mReqUsage) {
1291 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1292 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001293 mReqUsage = reqUsage;
1294 return OK;
1295}
1296
1297int Surface::setCrop(Rect const* rect)
1298{
1299 ATRACE_CALL();
1300
Pablo Ceballos60d69222015-08-07 14:47:20 -07001301 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001302 if (rect == NULL || rect->isEmpty()) {
1303 realRect.clear();
1304 } else {
1305 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001306 }
1307
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001308 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1309 realRect.left, realRect.top, realRect.right, realRect.bottom);
1310
1311 Mutex::Autolock lock(mMutex);
1312 mCrop = realRect;
1313 return NO_ERROR;
1314}
1315
1316int Surface::setBufferCount(int bufferCount)
1317{
1318 ATRACE_CALL();
1319 ALOGV("Surface::setBufferCount");
1320 Mutex::Autolock lock(mMutex);
1321
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001322 status_t err = NO_ERROR;
1323 if (bufferCount == 0) {
1324 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1325 } else {
1326 int minUndequeuedBuffers = 0;
1327 err = mGraphicBufferProducer->query(
1328 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1329 if (err == NO_ERROR) {
1330 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1331 bufferCount - minUndequeuedBuffers);
1332 }
1333 }
Mathias Agopian90147262010-01-22 11:47:55 -08001334
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001335 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1336 bufferCount, strerror(-err));
1337
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001338 return err;
1339}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001340
Pablo Ceballosfa455352015-08-12 17:47:47 -07001341int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1342 ATRACE_CALL();
1343 ALOGV("Surface::setMaxDequeuedBufferCount");
1344 Mutex::Autolock lock(mMutex);
1345
1346 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1347 maxDequeuedBuffers);
1348 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1349 "returned %s", maxDequeuedBuffers, strerror(-err));
1350
Pablo Ceballosfa455352015-08-12 17:47:47 -07001351 return err;
1352}
1353
1354int Surface::setAsyncMode(bool async) {
1355 ATRACE_CALL();
1356 ALOGV("Surface::setAsyncMode");
1357 Mutex::Autolock lock(mMutex);
1358
1359 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1360 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1361 async, strerror(-err));
1362
Pablo Ceballosfa455352015-08-12 17:47:47 -07001363 return err;
1364}
1365
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001366int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001367 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001368 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001369 Mutex::Autolock lock(mMutex);
1370
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001371 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1372 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001373 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001374 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001375 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001376 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1377 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001378
1379 return err;
1380}
1381
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001382int Surface::setAutoRefresh(bool autoRefresh) {
1383 ATRACE_CALL();
1384 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1385 Mutex::Autolock lock(mMutex);
1386
1387 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1388 if (err == NO_ERROR) {
1389 mAutoRefresh = autoRefresh;
1390 }
1391 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1392 autoRefresh, strerror(-err));
1393 return err;
1394}
1395
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001396int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001397{
1398 ATRACE_CALL();
1399 ALOGV("Surface::setBuffersDimensions");
1400
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001401 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001402 return BAD_VALUE;
1403
1404 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001405 if (width != mReqWidth || height != mReqHeight) {
1406 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1407 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001408 mReqWidth = width;
1409 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001410 return NO_ERROR;
1411}
1412
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001413int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001414{
1415 ATRACE_CALL();
1416 ALOGV("Surface::setBuffersUserDimensions");
1417
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001418 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001419 return BAD_VALUE;
1420
1421 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001422 if (width != mUserWidth || height != mUserHeight) {
1423 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1424 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001425 mUserWidth = width;
1426 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001427 return NO_ERROR;
1428}
1429
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001430int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001431{
1432 ALOGV("Surface::setBuffersFormat");
1433
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001434 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001435 if (format != mReqFormat) {
1436 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1437 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001438 mReqFormat = format;
1439 return NO_ERROR;
1440}
1441
1442int Surface::setScalingMode(int mode)
1443{
1444 ATRACE_CALL();
1445 ALOGV("Surface::setScalingMode(%d)", mode);
1446
1447 switch (mode) {
1448 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1449 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1450 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001451 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001452 break;
1453 default:
1454 ALOGE("unknown scaling mode: %d", mode);
1455 return BAD_VALUE;
1456 }
1457
1458 Mutex::Autolock lock(mMutex);
1459 mScalingMode = mode;
1460 return NO_ERROR;
1461}
1462
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001463int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001464{
1465 ATRACE_CALL();
1466 ALOGV("Surface::setBuffersTransform");
1467 Mutex::Autolock lock(mMutex);
1468 mTransform = transform;
1469 return NO_ERROR;
1470}
1471
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001472int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001473{
1474 ATRACE_CALL();
1475 ALOGV("Surface::setBuffersStickyTransform");
1476 Mutex::Autolock lock(mMutex);
1477 mStickyTransform = transform;
1478 return NO_ERROR;
1479}
1480
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001481int Surface::setBuffersTimestamp(int64_t timestamp)
1482{
1483 ALOGV("Surface::setBuffersTimestamp");
1484 Mutex::Autolock lock(mMutex);
1485 mTimestamp = timestamp;
1486 return NO_ERROR;
1487}
1488
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001489int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1490{
1491 ALOGV("Surface::setBuffersDataSpace");
1492 Mutex::Autolock lock(mMutex);
1493 mDataSpace = dataSpace;
1494 return NO_ERROR;
1495}
1496
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001497void Surface::freeAllBuffers() {
1498 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1499 mSlots[i].buffer = 0;
1500 }
1501}
1502
Dan Stoza5065a552015-03-17 16:23:42 -07001503void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1504 ATRACE_CALL();
1505 ALOGV("Surface::setSurfaceDamage");
1506 Mutex::Autolock lock(mMutex);
1507
Dan Stozac62acbd2015-04-21 16:42:49 -07001508 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001509 mDirtyRegion = Region::INVALID_REGION;
1510 return;
1511 }
1512
1513 mDirtyRegion.clear();
1514 for (size_t r = 0; r < numRects; ++r) {
1515 // We intentionally flip top and bottom here, since because they're
1516 // specified with a bottom-left origin, top > bottom, which fails
1517 // validation in the Region class. We will fix this up when we flip to a
1518 // top-left origin in queueBuffer.
1519 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1520 mDirtyRegion.orSelf(rect);
1521 }
1522}
1523
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001524// ----------------------------------------------------------------------
1525// the lock/unlock APIs must be used from the same thread
1526
1527static status_t copyBlt(
1528 const sp<GraphicBuffer>& dst,
1529 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001530 const Region& reg,
1531 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001532{
Dennis Kempinadd458f2017-05-17 19:16:31 -07001533 if (dst->getId() == src->getId())
1534 return OK;
1535
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001536 // src and dst with, height and format must be identical. no verification
1537 // is done here.
1538 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001539 uint8_t* src_bits = NULL;
1540 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1541 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001542 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1543
1544 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001545 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1546 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001547 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001548 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001549
1550 Region::const_iterator head(reg.begin());
1551 Region::const_iterator tail(reg.end());
1552 if (head != tail && src_bits && dst_bits) {
1553 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001554 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1555 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001556
1557 while (head != tail) {
1558 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001559 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001560 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001561 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1562 uint8_t const * s = src_bits +
1563 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1564 uint8_t * d = dst_bits +
1565 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001566 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001567 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001568 h = 1;
1569 }
1570 do {
1571 memcpy(d, s, size);
1572 d += dbpr;
1573 s += sbpr;
1574 } while (--h > 0);
1575 }
1576 }
1577
1578 if (src_bits)
1579 src->unlock();
1580
1581 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001582 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001583
1584 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001585}
1586
Mathias Agopiana138f892010-05-21 17:24:35 -07001587// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001588
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001589status_t Surface::lock(
1590 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1591{
1592 if (mLockedBuffer != 0) {
1593 ALOGE("Surface::lock failed, already locked");
1594 return INVALID_OPERATION;
1595 }
1596
1597 if (!mConnectedToCpu) {
1598 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1599 if (err) {
1600 return err;
1601 }
1602 // we're intending to do software rendering from this point
1603 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1604 }
1605
1606 ANativeWindowBuffer* out;
1607 int fenceFd = -1;
1608 status_t err = dequeueBuffer(&out, &fenceFd);
1609 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1610 if (err == NO_ERROR) {
1611 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001612 const Rect bounds(backBuffer->width, backBuffer->height);
1613
1614 Region newDirtyRegion;
1615 if (inOutDirtyBounds) {
1616 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1617 newDirtyRegion.andSelf(bounds);
1618 } else {
1619 newDirtyRegion.set(bounds);
1620 }
1621
1622 // figure out if we can copy the frontbuffer back
1623 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1624 const bool canCopyBack = (frontBuffer != 0 &&
1625 backBuffer->width == frontBuffer->width &&
1626 backBuffer->height == frontBuffer->height &&
1627 backBuffer->format == frontBuffer->format);
1628
1629 if (canCopyBack) {
1630 // copy the area that is invalid and not repainted this round
1631 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001632 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001633 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001634 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001635 } else {
1636 // if we can't copy-back anything, modify the user's dirty
1637 // region to make sure they redraw the whole buffer
1638 newDirtyRegion.set(bounds);
1639 mDirtyRegion.clear();
1640 Mutex::Autolock lock(mMutex);
1641 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1642 mSlots[i].dirtyRegion.clear();
1643 }
1644 }
1645
1646
1647 { // scope for the lock
1648 Mutex::Autolock lock(mMutex);
1649 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1650 if (backBufferSlot >= 0) {
1651 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1652 mDirtyRegion.subtract(dirtyRegion);
1653 dirtyRegion = newDirtyRegion;
1654 }
1655 }
1656
1657 mDirtyRegion.orSelf(newDirtyRegion);
1658 if (inOutDirtyBounds) {
1659 *inOutDirtyBounds = newDirtyRegion.getBounds();
1660 }
1661
1662 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001663 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001664 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001665 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001666
1667 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1668 backBuffer->handle);
1669
1670 if (res != 0) {
1671 err = INVALID_OPERATION;
1672 } else {
1673 mLockedBuffer = backBuffer;
1674 outBuffer->width = backBuffer->width;
1675 outBuffer->height = backBuffer->height;
1676 outBuffer->stride = backBuffer->stride;
1677 outBuffer->format = backBuffer->format;
1678 outBuffer->bits = vaddr;
1679 }
1680 }
1681 return err;
1682}
1683
1684status_t Surface::unlockAndPost()
1685{
1686 if (mLockedBuffer == 0) {
1687 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1688 return INVALID_OPERATION;
1689 }
1690
Francis Hart8f396012014-04-01 15:30:53 +03001691 int fd = -1;
1692 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001693 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1694
Francis Hart8f396012014-04-01 15:30:53 +03001695 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001696 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1697 mLockedBuffer->handle, strerror(-err));
1698
1699 mPostedBuffer = mLockedBuffer;
1700 mLockedBuffer = 0;
1701 return err;
1702}
1703
Robert Carr9f31e292016-04-11 11:15:32 -07001704bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1705 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001706 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001707 return true;
1708 }
1709 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1710}
1711
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001712status_t Surface::getUniqueId(uint64_t* outId) const {
1713 Mutex::Autolock lock(mMutex);
1714 return mGraphicBufferProducer->getUniqueId(outId);
1715}
1716
Dan Stoza932f0082017-05-31 13:50:16 -07001717nsecs_t Surface::getLastDequeueStartTime() const {
1718 Mutex::Autolock lock(mMutex);
1719 return mLastDequeueStartTime;
1720}
1721
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001722status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1723 if (out == nullptr) {
1724 ALOGE("%s: out must not be null!", __FUNCTION__);
1725 return BAD_VALUE;
1726 }
1727
1728 Mutex::Autolock lock(mMutex);
1729 *out = mRemovedBuffers;
1730 mRemovedBuffers.clear();
1731 return OK;
1732}
1733
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734}; // namespace android