blob: 05ae7bb63fe167d5cfeb50bedf85aa9a08a57364 [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;
Chih-Hung Hsiehd66be0a2017-10-05 13:51:32 -0700158 status_t result = composerService()->getDisplayStats(NULL, &stats);
159 if (result != NO_ERROR) {
160 return result;
161 }
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800162
163 *outRefreshDuration = stats.vsyncPeriod;
164
165 return NO_ERROR;
166}
167
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700168void Surface::enableFrameTimestamps(bool enable) {
169 Mutex::Autolock lock(mMutex);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800170 // If going from disabled to enabled, get the initial values for
171 // compositor and display timing.
172 if (!mEnableFrameTimestamps && enable) {
173 FrameEventHistoryDelta delta;
174 mGraphicBufferProducer->getFrameTimestamps(&delta);
175 mFrameEventHistory->applyDelta(delta);
176 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700177 mEnableFrameTimestamps = enable;
178}
179
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800180status_t Surface::getCompositorTiming(
181 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
182 nsecs_t* compositeToPresentLatency) {
183 Mutex::Autolock lock(mMutex);
184 if (!mEnableFrameTimestamps) {
185 return INVALID_OPERATION;
186 }
187
188 if (compositeDeadline != nullptr) {
189 *compositeDeadline =
190 mFrameEventHistory->getNextCompositeDeadline(now());
191 }
192 if (compositeInterval != nullptr) {
193 *compositeInterval = mFrameEventHistory->getCompositeInterval();
194 }
195 if (compositeToPresentLatency != nullptr) {
196 *compositeToPresentLatency =
197 mFrameEventHistory->getCompositeToPresentLatency();
198 }
199 return NO_ERROR;
200}
201
Brian Anderson50143b32016-09-30 14:01:24 -0700202static bool checkConsumerForUpdates(
203 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700204 const nsecs_t* outLatchTime,
205 const nsecs_t* outFirstRefreshStartTime,
206 const nsecs_t* outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700207 const nsecs_t* outGpuCompositionDoneTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700208 const nsecs_t* outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700209 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700210 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700211 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
212 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700213 !e->hasFirstRefreshStartInfo();
Brian Andersonb04c6f02016-10-21 12:57:46 -0700214 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700215 !e->hasGpuCompositionDoneInfo();
216 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
217 !e->hasDisplayPresentInfo();
218
Brian Anderson6b376712017-04-04 10:51:39 -0700219 // LastRefreshStart, DequeueReady, and Release are never available for the
220 // last frame.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700221 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
222 !e->hasLastRefreshStartInfo() &&
223 (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700224 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
225 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700226 bool checkForRelease = (outReleaseTime != nullptr) &&
227 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
228
229 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700230 return checkForLatch || checkForFirstRefreshStart ||
Brian Andersonb04c6f02016-10-21 12:57:46 -0700231 checkForLastRefreshStart || checkForGpuCompositionDone ||
Brian Anderson4e606e32017-03-16 15:34:57 -0700232 checkForDisplayPresent || checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700233}
234
Brian Anderson3d4039d2016-09-23 16:31:30 -0700235static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
236 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700237 // We always get valid timestamps for these eventually.
238 *dst = (src == FrameEvents::TIMESTAMP_PENDING) ?
239 NATIVE_WINDOW_TIMESTAMP_PENDING : src;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700240 }
241}
242
Brian Andersondc96fdf2017-03-20 16:54:25 -0700243static void getFrameTimestampFence(nsecs_t *dst,
244 const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700245 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700246 if (!fenceShouldBeKnown) {
247 *dst = NATIVE_WINDOW_TIMESTAMP_PENDING;
248 return;
249 }
250
Brian Anderson3d4039d2016-09-23 16:31:30 -0700251 nsecs_t signalTime = src->getSignalTime();
Brian Andersondc96fdf2017-03-20 16:54:25 -0700252 *dst = (signalTime == Fence::SIGNAL_TIME_PENDING) ?
253 NATIVE_WINDOW_TIMESTAMP_PENDING :
254 (signalTime == Fence::SIGNAL_TIME_INVALID) ?
255 NATIVE_WINDOW_TIMESTAMP_INVALID :
256 signalTime;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700257 }
258}
259
Brian Anderson069b3652016-07-22 10:32:47 -0700260status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700261 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700262 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700263 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700264 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
265 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800266 ATRACE_CALL();
267
Brian Anderson3890c392016-07-25 12:48:08 -0700268 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700269
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700270 if (!mEnableFrameTimestamps) {
271 return INVALID_OPERATION;
272 }
273
Brian Anderson6b376712017-04-04 10:51:39 -0700274 // Verify the requested timestamps are supported.
275 querySupportedTimestampsLocked();
276 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
277 return BAD_VALUE;
278 }
279
Brian Anderson3da8d272016-07-28 16:20:47 -0700280 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700281 if (events == nullptr) {
282 // If the entry isn't available in the producer, it's definitely not
283 // available in the consumer.
284 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700285 }
286
Brian Anderson50143b32016-09-30 14:01:24 -0700287 // Update our cache of events if the requested events are not available.
288 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700289 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700290 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700291 outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700292 FrameEventHistoryDelta delta;
293 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700294 mFrameEventHistory->applyDelta(delta);
295 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700296 }
297
Brian Anderson3890c392016-07-25 12:48:08 -0700298 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700299 // The entry was available before the update, but was overwritten
300 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700301 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800302 }
Brian Anderson069b3652016-07-22 10:32:47 -0700303
Brian Anderson3d4039d2016-09-23 16:31:30 -0700304 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700305 getFrameTimestamp(outLatchTime, events->latchTime);
306 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
307 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
308 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700309
Brian Andersondc96fdf2017-03-20 16:54:25 -0700310 getFrameTimestampFence(outAcquireTime, events->acquireFence,
311 events->hasAcquireInfo());
312 getFrameTimestampFence(outGpuCompositionDoneTime,
313 events->gpuCompositionDoneFence,
314 events->hasGpuCompositionDoneInfo());
315 getFrameTimestampFence(outDisplayPresentTime, events->displayPresentFence,
316 events->hasDisplayPresentInfo());
317 getFrameTimestampFence(outReleaseTime, events->releaseFence,
318 events->hasReleaseInfo());
Brian Anderson069b3652016-07-22 10:32:47 -0700319
320 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800321}
322
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600323using namespace android::hardware::configstore;
324using namespace android::hardware::configstore::V1_0;
325
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700326status_t Surface::getWideColorSupport(bool* supported) {
327 ATRACE_CALL();
328
329 sp<IBinder> display(
330 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
331 Vector<android_color_mode_t> colorModes;
332 status_t err =
333 composerService()->getDisplayColorModes(display, &colorModes);
334
335 if (err)
336 return err;
337
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600338 bool wideColorBoardConfig =
339 getBool<ISurfaceFlingerConfigs,
340 &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
341
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700342 *supported = false;
343 for (android_color_mode_t colorMode : colorModes) {
344 switch (colorMode) {
345 case HAL_COLOR_MODE_DISPLAY_P3:
346 case HAL_COLOR_MODE_ADOBE_RGB:
347 case HAL_COLOR_MODE_DCI_P3:
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600348 if (wideColorBoardConfig) {
349 *supported = true;
350 }
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700351 break;
352 default:
353 break;
354 }
355 }
356
357 return NO_ERROR;
358}
359
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700360status_t Surface::getHdrSupport(bool* supported) {
361 ATRACE_CALL();
362
363 sp<IBinder> display(
364 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
365 HdrCapabilities hdrCapabilities;
366 status_t err =
367 composerService()->getHdrCapabilities(display, &hdrCapabilities);
368
369 if (err)
370 return err;
371
372 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
373
374 return NO_ERROR;
375}
376
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800377int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
378 Surface* c = getSelf(window);
379 return c->setSwapInterval(interval);
380}
381
382int Surface::hook_dequeueBuffer(ANativeWindow* window,
383 ANativeWindowBuffer** buffer, int* fenceFd) {
384 Surface* c = getSelf(window);
385 return c->dequeueBuffer(buffer, fenceFd);
386}
387
388int Surface::hook_cancelBuffer(ANativeWindow* window,
389 ANativeWindowBuffer* buffer, int fenceFd) {
390 Surface* c = getSelf(window);
391 return c->cancelBuffer(buffer, fenceFd);
392}
393
394int Surface::hook_queueBuffer(ANativeWindow* window,
395 ANativeWindowBuffer* buffer, int fenceFd) {
396 Surface* c = getSelf(window);
397 return c->queueBuffer(buffer, fenceFd);
398}
399
400int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
401 ANativeWindowBuffer** buffer) {
402 Surface* c = getSelf(window);
403 ANativeWindowBuffer* buf;
404 int fenceFd = -1;
405 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600406 if (result != OK) {
407 return result;
408 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800409 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700410 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800411 if (waitResult != OK) {
412 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
413 waitResult);
414 c->cancelBuffer(buf, -1);
415 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700416 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800417 *buffer = buf;
418 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700419}
420
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800421int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
422 ANativeWindowBuffer* buffer) {
423 Surface* c = getSelf(window);
424 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700425}
426
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800427int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
428 ANativeWindowBuffer* buffer) {
429 Surface* c = getSelf(window);
430 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700431}
432
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800433int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
434 ANativeWindowBuffer* buffer) {
435 Surface* c = getSelf(window);
436 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700437}
Mathias Agopian62185b72009-04-16 16:19:50 -0700438
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800439int Surface::hook_query(const ANativeWindow* window,
440 int what, int* value) {
441 const Surface* c = getSelf(window);
442 return c->query(what, value);
443}
444
445int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
446 va_list args;
447 va_start(args, operation);
448 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700449 int result = c->perform(operation, args);
450 va_end(args);
451 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800452}
453
454int Surface::setSwapInterval(int interval) {
455 ATRACE_CALL();
456 // EGL specification states:
457 // interval is silently clamped to minimum and maximum implementation
458 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800459
460 if (interval < minSwapInterval)
461 interval = minSwapInterval;
462
463 if (interval > maxSwapInterval)
464 interval = maxSwapInterval;
465
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700466 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700467 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800468
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700469 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800470}
471
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700472int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800473 ATRACE_CALL();
474 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800475
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800476 uint32_t reqWidth;
477 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800478 PixelFormat reqFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700479 uint64_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700480 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800481
482 {
483 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700484 if (mReportRemovedBuffers) {
485 mRemovedBuffers.clear();
486 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800487
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800488 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
489 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800490
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800491 reqFormat = mReqFormat;
492 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800493
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700494 enableFrameTimestamps = mEnableFrameTimestamps;
495
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700496 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800497 BufferItem::INVALID_BUFFER_SLOT) {
498 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
499 if (gbuf != NULL) {
500 *buffer = gbuf.get();
501 *fenceFd = -1;
502 return OK;
503 }
504 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800505 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
506
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800507 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800508 sp<Fence> fence;
Dan Stoza932f0082017-05-31 13:50:16 -0700509 nsecs_t startTime = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700510
511 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700512 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700513 reqWidth, reqHeight, reqFormat, reqUsage,
514 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza932f0082017-05-31 13:50:16 -0700515 mLastDequeueDuration = systemTime() - startTime;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800516
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800517 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700518 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700519 "(%d, %d, %d, %#" PRIx64 ") failed: %d",
520 reqWidth, reqHeight, reqFormat, reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800521 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700522 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800523
Dan Stoza90ce2a92017-05-01 16:31:53 -0700524 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
525 ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf);
526 android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging
527 return FAILED_TRANSACTION;
528 }
529
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800530 Mutex::Autolock lock(mMutex);
531
Dan Stoza932f0082017-05-31 13:50:16 -0700532 // Write this while holding the mutex
533 mLastDequeueStartTime = startTime;
534
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800535 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700536
537 // this should never happen
538 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
539
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800540 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
541 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700542 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700543
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700544 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700545 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700546 }
547
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700548 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
549 if (mReportRemovedBuffers && (gbuf != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700550 mRemovedBuffers.push_back(gbuf);
551 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800552 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
553 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700554 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700555 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800556 return result;
557 }
558 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700559
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800560 if (fence->isValid()) {
561 *fenceFd = fence->dup();
562 if (*fenceFd == -1) {
563 ALOGE("dequeueBuffer: error duping fence: %d", errno);
564 // dup() should never fail; something is badly wrong. Soldier on
565 // and hope for the best; the worst that should happen is some
566 // visible corruption that lasts until the next frame.
567 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700568 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800569 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700570 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800571
572 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800573
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700574 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800575 mSharedBufferSlot = buf;
576 mSharedBufferHasBeenQueued = false;
577 } else if (mSharedBufferSlot == buf) {
578 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
579 mSharedBufferHasBeenQueued = false;
580 }
581
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800582 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700583}
584
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800585int Surface::cancelBuffer(android_native_buffer_t* buffer,
586 int fenceFd) {
587 ATRACE_CALL();
588 ALOGV("Surface::cancelBuffer");
589 Mutex::Autolock lock(mMutex);
590 int i = getSlotFromBufferLocked(buffer);
591 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900592 if (fenceFd >= 0) {
593 close(fenceFd);
594 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800595 return i;
596 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800597 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
598 if (fenceFd >= 0) {
599 close(fenceFd);
600 }
601 return OK;
602 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800603 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
604 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800605
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700606 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800607 mSharedBufferHasBeenQueued = true;
608 }
609
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800610 return OK;
611}
612
613int Surface::getSlotFromBufferLocked(
614 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800615 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
616 if (mSlots[i].buffer != NULL &&
617 mSlots[i].buffer->handle == buffer->handle) {
618 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700619 }
620 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800621 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
622 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700623}
624
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800625int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800626 ALOGV("Surface::lockBuffer");
627 Mutex::Autolock lock(mMutex);
628 return OK;
629}
Mathias Agopian631f3582010-05-25 17:51:34 -0700630
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800631int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
632 ATRACE_CALL();
633 ALOGV("Surface::queueBuffer");
634 Mutex::Autolock lock(mMutex);
635 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700636 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800637
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800638 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700639 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700640 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700641 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700642 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800643 } else {
644 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700645 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800646 int i = getSlotFromBufferLocked(buffer);
647 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900648 if (fenceFd >= 0) {
649 close(fenceFd);
650 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800651 return i;
652 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800653 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
654 if (fenceFd >= 0) {
655 close(fenceFd);
656 }
657 return OK;
658 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800661 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700662 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800663 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800665 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
666 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700667 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800668 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700669 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700670
Dan Stozac62acbd2015-04-21 16:42:49 -0700671 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700672 input.setSurfaceDamage(Region::INVALID_REGION);
673 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700674 // Here we do two things:
675 // 1) The surface damage was specified using the OpenGL ES convention of
676 // the origin being in the bottom-left corner. Here we flip to the
677 // convention that the rest of the system uses (top-left corner) by
678 // subtracting all top/bottom coordinates from the buffer height.
679 // 2) If the buffer is coming in rotated (for example, because the EGL
680 // implementation is reacting to the transform hint coming back from
681 // SurfaceFlinger), the surface damage needs to be rotated the
682 // opposite direction, since it was generated assuming an unrotated
683 // buffer (the app doesn't know that the EGL implementation is
684 // reacting to the transform hint behind its back). The
685 // transformations in the switch statement below apply those
686 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
687
688 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700689 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700690 bool rotated90 = (mTransform ^ mStickyTransform) &
691 NATIVE_WINDOW_TRANSFORM_ROT_90;
692 if (rotated90) {
693 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700694 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700695
Dan Stoza5065a552015-03-17 16:23:42 -0700696 Region flippedRegion;
697 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700698 int left = rect.left;
699 int right = rect.right;
700 int top = height - rect.bottom; // Flip from OpenGL convention
701 int bottom = height - rect.top; // Flip from OpenGL convention
702 switch (mTransform ^ mStickyTransform) {
703 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
704 // Rotate 270 degrees
705 Rect flippedRect{top, width - right, bottom, width - left};
706 flippedRegion.orSelf(flippedRect);
707 break;
708 }
709 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
710 // Rotate 180 degrees
711 Rect flippedRect{width - right, height - bottom,
712 width - left, height - top};
713 flippedRegion.orSelf(flippedRect);
714 break;
715 }
716 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
717 // Rotate 90 degrees
718 Rect flippedRect{height - bottom, left,
719 height - top, right};
720 flippedRegion.orSelf(flippedRect);
721 break;
722 }
723 default: {
724 Rect flippedRect{left, top, right, bottom};
725 flippedRegion.orSelf(flippedRect);
726 break;
727 }
728 }
Dan Stoza5065a552015-03-17 16:23:42 -0700729 }
730
731 input.setSurfaceDamage(flippedRegion);
732 }
733
Dan Stoza70ccba52016-07-01 14:00:40 -0700734 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800735 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700736 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800737 if (err != OK) {
738 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
739 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800740
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700741 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700742 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700743 // Update timestamps with the local acquire fence.
744 // The consumer doesn't send it back to prevent us from having two
745 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700746 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700747 std::make_shared<FenceTime>(std::move(fence)));
748
749 // Cache timestamps of signaled fences so we can close their file
750 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700751 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700752 }
753
Brian Anderson50143b32016-09-30 14:01:24 -0700754 mLastFrameNumber = mNextFrameNumber;
755
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700756 mDefaultWidth = output.width;
757 mDefaultHeight = output.height;
758 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700759
Ruben Brunk1681d952014-06-27 15:51:55 -0700760 // Disable transform hint if sticky transform is set.
761 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700762 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700763 }
764
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700765 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700766
Dan Stozac62acbd2015-04-21 16:42:49 -0700767 if (!mConnectedToCpu) {
768 // Clear surface damage back to full-buffer
769 mDirtyRegion = Region::INVALID_REGION;
770 }
Dan Stoza5065a552015-03-17 16:23:42 -0700771
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700772 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800773 mSharedBufferHasBeenQueued = true;
774 }
775
Robert Carr9f31e292016-04-11 11:15:32 -0700776 mQueueBufferCondition.broadcast();
777
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800778 return err;
779}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700780
Brian Anderson6b376712017-04-04 10:51:39 -0700781void Surface::querySupportedTimestampsLocked() const {
782 // mMutex must be locked when calling this method.
783
784 if (mQueriedSupportedTimestamps) {
785 return;
786 }
787 mQueriedSupportedTimestamps = true;
788
789 std::vector<FrameEvent> supportedFrameTimestamps;
790 status_t err = composerService()->getSupportedFrameTimestamps(
791 &supportedFrameTimestamps);
792
793 if (err != NO_ERROR) {
794 return;
795 }
796
797 for (auto sft : supportedFrameTimestamps) {
798 if (sft == FrameEvent::DISPLAY_PRESENT) {
799 mFrameTimestampsSupportsPresent = true;
800 }
801 }
802}
803
Mathias Agopiana67932f2011-04-20 14:20:59 -0700804int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800805 ATRACE_CALL();
806 ALOGV("Surface::query");
807 { // scope for the lock
808 Mutex::Autolock lock(mMutex);
809 switch (what) {
810 case NATIVE_WINDOW_FORMAT:
811 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800812 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800813 return NO_ERROR;
814 }
815 break;
816 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700817 if (composerService()->authenticateSurfaceTexture(
818 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800819 *value = 1;
820 } else {
821 *value = 0;
822 }
823 return NO_ERROR;
824 }
825 case NATIVE_WINDOW_CONCRETE_TYPE:
826 *value = NATIVE_WINDOW_SURFACE;
827 return NO_ERROR;
828 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800829 *value = static_cast<int>(
830 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800831 return NO_ERROR;
832 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800833 *value = static_cast<int>(
834 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800835 return NO_ERROR;
836 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800837 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800838 return NO_ERROR;
839 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
840 status_t err = NO_ERROR;
841 if (!mConsumerRunningBehind) {
842 *value = 0;
843 } else {
844 err = mGraphicBufferProducer->query(what, value);
845 if (err == NO_ERROR) {
846 mConsumerRunningBehind = *value;
847 }
848 }
849 return err;
850 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700851 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
852 int64_t durationUs = mLastDequeueDuration / 1000;
853 *value = durationUs > std::numeric_limits<int>::max() ?
854 std::numeric_limits<int>::max() :
855 static_cast<int>(durationUs);
856 return NO_ERROR;
857 }
858 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
859 int64_t durationUs = mLastQueueDuration / 1000;
860 *value = durationUs > std::numeric_limits<int>::max() ?
861 std::numeric_limits<int>::max() :
862 static_cast<int>(durationUs);
863 return NO_ERROR;
864 }
Brian Anderson6b376712017-04-04 10:51:39 -0700865 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
866 querySupportedTimestampsLocked();
867 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
868 return NO_ERROR;
869 }
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800870 case NATIVE_WINDOW_IS_VALID: {
871 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
872 return NO_ERROR;
873 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800874 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700875 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800876 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800877}
878
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800879int Surface::perform(int operation, va_list args)
880{
881 int res = NO_ERROR;
882 switch (operation) {
883 case NATIVE_WINDOW_CONNECT:
884 // deprecated. must return NO_ERROR.
885 break;
886 case NATIVE_WINDOW_DISCONNECT:
887 // deprecated. must return NO_ERROR.
888 break;
889 case NATIVE_WINDOW_SET_USAGE:
890 res = dispatchSetUsage(args);
891 break;
892 case NATIVE_WINDOW_SET_CROP:
893 res = dispatchSetCrop(args);
894 break;
895 case NATIVE_WINDOW_SET_BUFFER_COUNT:
896 res = dispatchSetBufferCount(args);
897 break;
898 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
899 res = dispatchSetBuffersGeometry(args);
900 break;
901 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
902 res = dispatchSetBuffersTransform(args);
903 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700904 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
905 res = dispatchSetBuffersStickyTransform(args);
906 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800907 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
908 res = dispatchSetBuffersTimestamp(args);
909 break;
910 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
911 res = dispatchSetBuffersDimensions(args);
912 break;
913 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
914 res = dispatchSetBuffersUserDimensions(args);
915 break;
916 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
917 res = dispatchSetBuffersFormat(args);
918 break;
919 case NATIVE_WINDOW_LOCK:
920 res = dispatchLock(args);
921 break;
922 case NATIVE_WINDOW_UNLOCK_AND_POST:
923 res = dispatchUnlockAndPost(args);
924 break;
925 case NATIVE_WINDOW_SET_SCALING_MODE:
926 res = dispatchSetScalingMode(args);
927 break;
928 case NATIVE_WINDOW_API_CONNECT:
929 res = dispatchConnect(args);
930 break;
931 case NATIVE_WINDOW_API_DISCONNECT:
932 res = dispatchDisconnect(args);
933 break;
Rachad7cb0d392014-07-29 17:53:53 -0700934 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
935 res = dispatchSetSidebandStream(args);
936 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800937 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
938 res = dispatchSetBuffersDataSpace(args);
939 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700940 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
941 res = dispatchSetSurfaceDamage(args);
942 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700943 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
944 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700945 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800946 case NATIVE_WINDOW_SET_AUTO_REFRESH:
947 res = dispatchSetAutoRefresh(args);
948 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800949 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
950 res = dispatchGetDisplayRefreshCycleDuration(args);
951 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800952 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
953 res = dispatchGetNextFrameId(args);
954 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700955 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
956 res = dispatchEnableFrameTimestamps(args);
957 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800958 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
959 res = dispatchGetCompositorTiming(args);
960 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800961 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
962 res = dispatchGetFrameTimestamps(args);
963 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700964 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
965 res = dispatchGetWideColorSupport(args);
966 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700967 case NATIVE_WINDOW_GET_HDR_SUPPORT:
968 res = dispatchGetHdrSupport(args);
969 break;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700970 case NATIVE_WINDOW_SET_USAGE64:
971 res = dispatchSetUsage64(args);
972 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800973 default:
974 res = NAME_NOT_FOUND;
975 break;
976 }
977 return res;
978}
Mathias Agopiana138f892010-05-21 17:24:35 -0700979
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800980int Surface::dispatchConnect(va_list args) {
981 int api = va_arg(args, int);
982 return connect(api);
983}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800984
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800985int Surface::dispatchDisconnect(va_list args) {
986 int api = va_arg(args, int);
987 return disconnect(api);
988}
989
990int Surface::dispatchSetUsage(va_list args) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700991 uint64_t usage = va_arg(args, uint32_t);
992 return setUsage(usage);
993}
994
995int Surface::dispatchSetUsage64(va_list args) {
996 uint64_t usage = va_arg(args, uint64_t);
997 return setUsage(usage);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800998}
999
1000int Surface::dispatchSetCrop(va_list args) {
1001 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
1002 return setCrop(reinterpret_cast<Rect const*>(rect));
1003}
1004
1005int Surface::dispatchSetBufferCount(va_list args) {
1006 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001007 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001008}
1009
1010int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001011 uint32_t width = va_arg(args, uint32_t);
1012 uint32_t height = va_arg(args, uint32_t);
1013 PixelFormat format = va_arg(args, PixelFormat);
1014 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001015 if (err != 0) {
1016 return err;
1017 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001018 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001019}
1020
1021int Surface::dispatchSetBuffersDimensions(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 setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001025}
1026
1027int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001028 uint32_t width = va_arg(args, uint32_t);
1029 uint32_t height = va_arg(args, uint32_t);
1030 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001031}
1032
1033int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001034 PixelFormat format = va_arg(args, PixelFormat);
1035 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001036}
1037
1038int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001039 int mode = va_arg(args, int);
1040 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001041}
1042
1043int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001044 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001045 return setBuffersTransform(transform);
1046}
1047
Ruben Brunk1681d952014-06-27 15:51:55 -07001048int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001049 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -07001050 return setBuffersStickyTransform(transform);
1051}
1052
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001053int Surface::dispatchSetBuffersTimestamp(va_list args) {
1054 int64_t timestamp = va_arg(args, int64_t);
1055 return setBuffersTimestamp(timestamp);
1056}
1057
1058int Surface::dispatchLock(va_list args) {
1059 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1060 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1061 return lock(outBuffer, inOutDirtyBounds);
1062}
1063
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001064int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001065 return unlockAndPost();
1066}
1067
Rachad7cb0d392014-07-29 17:53:53 -07001068int Surface::dispatchSetSidebandStream(va_list args) {
1069 native_handle_t* sH = va_arg(args, native_handle_t*);
1070 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1071 setSidebandStream(sidebandHandle);
1072 return OK;
1073}
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001074
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001075int Surface::dispatchSetBuffersDataSpace(va_list args) {
1076 android_dataspace dataspace =
1077 static_cast<android_dataspace>(va_arg(args, int));
1078 return setBuffersDataSpace(dataspace);
1079}
1080
Dan Stoza5065a552015-03-17 16:23:42 -07001081int Surface::dispatchSetSurfaceDamage(va_list args) {
1082 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1083 size_t numRects = va_arg(args, size_t);
1084 setSurfaceDamage(rects, numRects);
1085 return NO_ERROR;
1086}
1087
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001088int Surface::dispatchSetSharedBufferMode(va_list args) {
1089 bool sharedBufferMode = va_arg(args, int);
1090 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001091}
1092
1093int Surface::dispatchSetAutoRefresh(va_list args) {
1094 bool autoRefresh = va_arg(args, int);
1095 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001096}
1097
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001098int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1099 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1100 return getDisplayRefreshCycleDuration(outRefreshDuration);
1101}
1102
Brian Anderson1049d1d2016-12-16 17:25:57 -08001103int Surface::dispatchGetNextFrameId(va_list args) {
1104 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1105 *nextFrameId = getNextFrameNumber();
1106 return NO_ERROR;
1107}
1108
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001109int Surface::dispatchEnableFrameTimestamps(va_list args) {
1110 bool enable = va_arg(args, int);
1111 enableFrameTimestamps(enable);
1112 return NO_ERROR;
1113}
1114
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001115int Surface::dispatchGetCompositorTiming(va_list args) {
1116 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1117 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1118 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1119 return getCompositorTiming(compositeDeadline, compositeInterval,
1120 compositeToPresentLatency);
1121}
1122
Pablo Ceballosce796e72016-02-04 19:10:51 -08001123int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001124 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001125 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001126 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001127 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1128 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1129 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001130 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001131 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001132 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001133 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001134 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001135 outRequestedPresentTime, outAcquireTime, outLatchTime,
1136 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001137 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001138 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001139}
1140
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001141int Surface::dispatchGetWideColorSupport(va_list args) {
1142 bool* outSupport = va_arg(args, bool*);
1143 return getWideColorSupport(outSupport);
1144}
1145
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001146int Surface::dispatchGetHdrSupport(va_list args) {
1147 bool* outSupport = va_arg(args, bool*);
1148 return getHdrSupport(outSupport);
1149}
1150
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001151int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001152 static sp<IProducerListener> listener = new DummyProducerListener();
1153 return connect(api, listener);
1154}
1155
1156int Surface::connect(int api, const sp<IProducerListener>& listener) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001157 return connect(api, listener, false);
1158}
1159
1160int Surface::connect(
1161 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001162 ATRACE_CALL();
1163 ALOGV("Surface::connect");
1164 Mutex::Autolock lock(mMutex);
1165 IGraphicBufferProducer::QueueBufferOutput output;
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001166 mReportRemovedBuffers = reportBufferRemoval;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001167 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001168 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001169 mDefaultWidth = output.width;
1170 mDefaultHeight = output.height;
1171 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001172
1173 // Disable transform hint if sticky transform is set.
1174 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001175 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001176 }
1177
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001178 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001179 }
1180 if (!err && api == NATIVE_WINDOW_API_CPU) {
1181 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001182 // Clear the dirty region in case we're switching from a non-CPU API
1183 mDirtyRegion.clear();
1184 } else if (!err) {
1185 // Initialize the dirty region for tracking surface damage
1186 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001187 }
Dan Stoza5065a552015-03-17 16:23:42 -07001188
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001189 return err;
1190}
1191
Mathias Agopian365857d2013-09-11 19:35:45 -07001192
Robert Carr97b9c862016-09-08 13:54:35 -07001193int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001194 ATRACE_CALL();
1195 ALOGV("Surface::disconnect");
1196 Mutex::Autolock lock(mMutex);
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001197 mRemovedBuffers.clear();
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001198 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1199 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001200 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001201 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001202 if (!err) {
1203 mReqFormat = 0;
1204 mReqWidth = 0;
1205 mReqHeight = 0;
1206 mReqUsage = 0;
1207 mCrop.clear();
1208 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1209 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001210 mStickyTransform = 0;
1211
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001212 if (api == NATIVE_WINDOW_API_CPU) {
1213 mConnectedToCpu = false;
1214 }
1215 }
1216 return err;
1217}
1218
Dan Stozad9c49712015-04-27 11:06:01 -07001219int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001220 sp<Fence>* outFence) {
1221 ATRACE_CALL();
1222 ALOGV("Surface::detachNextBuffer");
1223
1224 if (outBuffer == NULL || outFence == NULL) {
1225 return BAD_VALUE;
1226 }
1227
1228 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001229 if (mReportRemovedBuffers) {
1230 mRemovedBuffers.clear();
1231 }
Dan Stoza231832e2015-03-11 11:55:01 -07001232
1233 sp<GraphicBuffer> buffer(NULL);
1234 sp<Fence> fence(NULL);
1235 status_t result = mGraphicBufferProducer->detachNextBuffer(
1236 &buffer, &fence);
1237 if (result != NO_ERROR) {
1238 return result;
1239 }
1240
Dan Stozad9c49712015-04-27 11:06:01 -07001241 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001242 if (fence != NULL && fence->isValid()) {
1243 *outFence = fence;
1244 } else {
1245 *outFence = Fence::NO_FENCE;
1246 }
1247
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001248 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1249 if (mSlots[i].buffer != NULL &&
Shuzhen Wang55be5052017-05-23 09:41:54 -07001250 mSlots[i].buffer->getId() == buffer->getId()) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001251 if (mReportRemovedBuffers) {
1252 mRemovedBuffers.push_back(mSlots[i].buffer);
1253 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001254 mSlots[i].buffer = NULL;
1255 }
1256 }
1257
Dan Stoza231832e2015-03-11 11:55:01 -07001258 return NO_ERROR;
1259}
1260
1261int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1262{
1263 ATRACE_CALL();
1264 ALOGV("Surface::attachBuffer");
1265
1266 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001267 if (mReportRemovedBuffers) {
1268 mRemovedBuffers.clear();
1269 }
Dan Stoza231832e2015-03-11 11:55:01 -07001270
1271 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001272 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1273 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001274 int32_t attachedSlot = -1;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001275 status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer);
Dan Stoza231832e2015-03-11 11:55:01 -07001276 if (result != NO_ERROR) {
1277 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001278 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001279 return result;
1280 }
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001281 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001282 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1283 }
Dan Stoza231832e2015-03-11 11:55:01 -07001284 mSlots[attachedSlot].buffer = graphicBuffer;
1285
1286 return NO_ERROR;
1287}
1288
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001289int Surface::setUsage(uint64_t reqUsage)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001290{
1291 ALOGV("Surface::setUsage");
1292 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001293 if (reqUsage != mReqUsage) {
1294 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1295 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001296 mReqUsage = reqUsage;
1297 return OK;
1298}
1299
1300int Surface::setCrop(Rect const* rect)
1301{
1302 ATRACE_CALL();
1303
Pablo Ceballos60d69222015-08-07 14:47:20 -07001304 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001305 if (rect == NULL || rect->isEmpty()) {
1306 realRect.clear();
1307 } else {
1308 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001309 }
1310
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001311 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1312 realRect.left, realRect.top, realRect.right, realRect.bottom);
1313
1314 Mutex::Autolock lock(mMutex);
1315 mCrop = realRect;
1316 return NO_ERROR;
1317}
1318
1319int Surface::setBufferCount(int bufferCount)
1320{
1321 ATRACE_CALL();
1322 ALOGV("Surface::setBufferCount");
1323 Mutex::Autolock lock(mMutex);
1324
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001325 status_t err = NO_ERROR;
1326 if (bufferCount == 0) {
1327 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1328 } else {
1329 int minUndequeuedBuffers = 0;
1330 err = mGraphicBufferProducer->query(
1331 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1332 if (err == NO_ERROR) {
1333 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1334 bufferCount - minUndequeuedBuffers);
1335 }
1336 }
Mathias Agopian90147262010-01-22 11:47:55 -08001337
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001338 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1339 bufferCount, strerror(-err));
1340
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001341 return err;
1342}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001343
Pablo Ceballosfa455352015-08-12 17:47:47 -07001344int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1345 ATRACE_CALL();
1346 ALOGV("Surface::setMaxDequeuedBufferCount");
1347 Mutex::Autolock lock(mMutex);
1348
1349 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1350 maxDequeuedBuffers);
1351 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1352 "returned %s", maxDequeuedBuffers, strerror(-err));
1353
Pablo Ceballosfa455352015-08-12 17:47:47 -07001354 return err;
1355}
1356
1357int Surface::setAsyncMode(bool async) {
1358 ATRACE_CALL();
1359 ALOGV("Surface::setAsyncMode");
1360 Mutex::Autolock lock(mMutex);
1361
1362 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1363 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1364 async, strerror(-err));
1365
Pablo Ceballosfa455352015-08-12 17:47:47 -07001366 return err;
1367}
1368
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001369int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001370 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001371 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001372 Mutex::Autolock lock(mMutex);
1373
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001374 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1375 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001376 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001377 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001378 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001379 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1380 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001381
1382 return err;
1383}
1384
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001385int Surface::setAutoRefresh(bool autoRefresh) {
1386 ATRACE_CALL();
1387 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1388 Mutex::Autolock lock(mMutex);
1389
1390 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1391 if (err == NO_ERROR) {
1392 mAutoRefresh = autoRefresh;
1393 }
1394 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1395 autoRefresh, strerror(-err));
1396 return err;
1397}
1398
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001399int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001400{
1401 ATRACE_CALL();
1402 ALOGV("Surface::setBuffersDimensions");
1403
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001404 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001405 return BAD_VALUE;
1406
1407 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001408 if (width != mReqWidth || height != mReqHeight) {
1409 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1410 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001411 mReqWidth = width;
1412 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001413 return NO_ERROR;
1414}
1415
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001416int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001417{
1418 ATRACE_CALL();
1419 ALOGV("Surface::setBuffersUserDimensions");
1420
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001421 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001422 return BAD_VALUE;
1423
1424 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001425 if (width != mUserWidth || height != mUserHeight) {
1426 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1427 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001428 mUserWidth = width;
1429 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001430 return NO_ERROR;
1431}
1432
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001433int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001434{
1435 ALOGV("Surface::setBuffersFormat");
1436
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001437 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001438 if (format != mReqFormat) {
1439 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1440 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001441 mReqFormat = format;
1442 return NO_ERROR;
1443}
1444
1445int Surface::setScalingMode(int mode)
1446{
1447 ATRACE_CALL();
1448 ALOGV("Surface::setScalingMode(%d)", mode);
1449
1450 switch (mode) {
1451 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1452 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1453 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001454 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001455 break;
1456 default:
1457 ALOGE("unknown scaling mode: %d", mode);
1458 return BAD_VALUE;
1459 }
1460
1461 Mutex::Autolock lock(mMutex);
1462 mScalingMode = mode;
1463 return NO_ERROR;
1464}
1465
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001466int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001467{
1468 ATRACE_CALL();
1469 ALOGV("Surface::setBuffersTransform");
1470 Mutex::Autolock lock(mMutex);
1471 mTransform = transform;
1472 return NO_ERROR;
1473}
1474
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001475int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001476{
1477 ATRACE_CALL();
1478 ALOGV("Surface::setBuffersStickyTransform");
1479 Mutex::Autolock lock(mMutex);
1480 mStickyTransform = transform;
1481 return NO_ERROR;
1482}
1483
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001484int Surface::setBuffersTimestamp(int64_t timestamp)
1485{
1486 ALOGV("Surface::setBuffersTimestamp");
1487 Mutex::Autolock lock(mMutex);
1488 mTimestamp = timestamp;
1489 return NO_ERROR;
1490}
1491
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001492int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1493{
1494 ALOGV("Surface::setBuffersDataSpace");
1495 Mutex::Autolock lock(mMutex);
1496 mDataSpace = dataSpace;
1497 return NO_ERROR;
1498}
1499
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001500void Surface::freeAllBuffers() {
1501 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1502 mSlots[i].buffer = 0;
1503 }
1504}
1505
Dan Stoza5065a552015-03-17 16:23:42 -07001506void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1507 ATRACE_CALL();
1508 ALOGV("Surface::setSurfaceDamage");
1509 Mutex::Autolock lock(mMutex);
1510
Dan Stozac62acbd2015-04-21 16:42:49 -07001511 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001512 mDirtyRegion = Region::INVALID_REGION;
1513 return;
1514 }
1515
1516 mDirtyRegion.clear();
1517 for (size_t r = 0; r < numRects; ++r) {
1518 // We intentionally flip top and bottom here, since because they're
1519 // specified with a bottom-left origin, top > bottom, which fails
1520 // validation in the Region class. We will fix this up when we flip to a
1521 // top-left origin in queueBuffer.
1522 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1523 mDirtyRegion.orSelf(rect);
1524 }
1525}
1526
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001527// ----------------------------------------------------------------------
1528// the lock/unlock APIs must be used from the same thread
1529
1530static status_t copyBlt(
1531 const sp<GraphicBuffer>& dst,
1532 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001533 const Region& reg,
1534 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001535{
Dennis Kempinadd458f2017-05-17 19:16:31 -07001536 if (dst->getId() == src->getId())
1537 return OK;
1538
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001539 // src and dst with, height and format must be identical. no verification
1540 // is done here.
1541 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001542 uint8_t* src_bits = NULL;
1543 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1544 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001545 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1546
1547 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001548 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1549 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001550 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001551 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001552
1553 Region::const_iterator head(reg.begin());
1554 Region::const_iterator tail(reg.end());
1555 if (head != tail && src_bits && dst_bits) {
1556 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001557 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1558 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001559
1560 while (head != tail) {
1561 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001562 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001563 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001564 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1565 uint8_t const * s = src_bits +
1566 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1567 uint8_t * d = dst_bits +
1568 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001569 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001570 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001571 h = 1;
1572 }
1573 do {
1574 memcpy(d, s, size);
1575 d += dbpr;
1576 s += sbpr;
1577 } while (--h > 0);
1578 }
1579 }
1580
1581 if (src_bits)
1582 src->unlock();
1583
1584 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001585 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001586
1587 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588}
1589
Mathias Agopiana138f892010-05-21 17:24:35 -07001590// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001591
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001592status_t Surface::lock(
1593 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1594{
1595 if (mLockedBuffer != 0) {
1596 ALOGE("Surface::lock failed, already locked");
1597 return INVALID_OPERATION;
1598 }
1599
1600 if (!mConnectedToCpu) {
1601 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1602 if (err) {
1603 return err;
1604 }
1605 // we're intending to do software rendering from this point
1606 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1607 }
1608
1609 ANativeWindowBuffer* out;
1610 int fenceFd = -1;
1611 status_t err = dequeueBuffer(&out, &fenceFd);
1612 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1613 if (err == NO_ERROR) {
1614 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001615 const Rect bounds(backBuffer->width, backBuffer->height);
1616
1617 Region newDirtyRegion;
1618 if (inOutDirtyBounds) {
1619 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1620 newDirtyRegion.andSelf(bounds);
1621 } else {
1622 newDirtyRegion.set(bounds);
1623 }
1624
1625 // figure out if we can copy the frontbuffer back
1626 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1627 const bool canCopyBack = (frontBuffer != 0 &&
1628 backBuffer->width == frontBuffer->width &&
1629 backBuffer->height == frontBuffer->height &&
1630 backBuffer->format == frontBuffer->format);
1631
1632 if (canCopyBack) {
1633 // copy the area that is invalid and not repainted this round
1634 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001635 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001636 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001637 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001638 } else {
1639 // if we can't copy-back anything, modify the user's dirty
1640 // region to make sure they redraw the whole buffer
1641 newDirtyRegion.set(bounds);
1642 mDirtyRegion.clear();
1643 Mutex::Autolock lock(mMutex);
1644 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1645 mSlots[i].dirtyRegion.clear();
1646 }
1647 }
1648
1649
1650 { // scope for the lock
1651 Mutex::Autolock lock(mMutex);
1652 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1653 if (backBufferSlot >= 0) {
1654 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1655 mDirtyRegion.subtract(dirtyRegion);
1656 dirtyRegion = newDirtyRegion;
1657 }
1658 }
1659
1660 mDirtyRegion.orSelf(newDirtyRegion);
1661 if (inOutDirtyBounds) {
1662 *inOutDirtyBounds = newDirtyRegion.getBounds();
1663 }
1664
1665 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001666 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001667 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001668 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001669
1670 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1671 backBuffer->handle);
1672
1673 if (res != 0) {
1674 err = INVALID_OPERATION;
1675 } else {
1676 mLockedBuffer = backBuffer;
1677 outBuffer->width = backBuffer->width;
1678 outBuffer->height = backBuffer->height;
1679 outBuffer->stride = backBuffer->stride;
1680 outBuffer->format = backBuffer->format;
1681 outBuffer->bits = vaddr;
1682 }
1683 }
1684 return err;
1685}
1686
1687status_t Surface::unlockAndPost()
1688{
1689 if (mLockedBuffer == 0) {
1690 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1691 return INVALID_OPERATION;
1692 }
1693
Francis Hart8f396012014-04-01 15:30:53 +03001694 int fd = -1;
1695 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001696 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1697
Francis Hart8f396012014-04-01 15:30:53 +03001698 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001699 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1700 mLockedBuffer->handle, strerror(-err));
1701
1702 mPostedBuffer = mLockedBuffer;
1703 mLockedBuffer = 0;
1704 return err;
1705}
1706
Robert Carr9f31e292016-04-11 11:15:32 -07001707bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1708 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001709 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001710 return true;
1711 }
1712 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1713}
1714
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001715status_t Surface::getUniqueId(uint64_t* outId) const {
1716 Mutex::Autolock lock(mMutex);
1717 return mGraphicBufferProducer->getUniqueId(outId);
1718}
1719
Dan Stoza932f0082017-05-31 13:50:16 -07001720nsecs_t Surface::getLastDequeueStartTime() const {
1721 Mutex::Autolock lock(mMutex);
1722 return mLastDequeueStartTime;
1723}
1724
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001725status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1726 if (out == nullptr) {
1727 ALOGE("%s: out must not be null!", __FUNCTION__);
1728 return BAD_VALUE;
1729 }
1730
1731 Mutex::Autolock lock(mMutex);
1732 *out = mRemovedBuffers;
1733 mRemovedBuffers.clear();
1734 return OK;
1735}
1736
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001737}; // namespace android