blob: 00e23f0df13027724f3b379d2ae577a7fb723893 [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
Peiyong Linfd997e02018-03-28 15:29:00 -070047using ui::ColorMode;
Peiyong Lin34beb7a2018-03-28 11:57:12 -070048using ui::Dataspace;
Peiyong Linfd997e02018-03-28 15:29:00 -070049
Ian Elliotta2eb34c2017-07-18 11:05:49 -060050Surface::Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp)
51 : mGraphicBufferProducer(bufferProducer),
52 mCrop(Rect::EMPTY_RECT),
53 mBufferAge(0),
54 mGenerationNumber(0),
55 mSharedBufferMode(false),
56 mAutoRefresh(false),
57 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
58 mSharedBufferHasBeenQueued(false),
59 mQueriedSupportedTimestamps(false),
60 mFrameTimestampsSupportsPresent(false),
61 mEnableFrameTimestamps(false),
62 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>()) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -080063 // Initialize the ANativeWindow function pointers.
64 ANativeWindow::setSwapInterval = hook_setSwapInterval;
65 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
66 ANativeWindow::cancelBuffer = hook_cancelBuffer;
67 ANativeWindow::queueBuffer = hook_queueBuffer;
68 ANativeWindow::query = hook_query;
69 ANativeWindow::perform = hook_perform;
70
71 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
72 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
73 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
74 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
75
76 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
77 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
78
79 mReqWidth = 0;
80 mReqHeight = 0;
81 mReqFormat = 0;
82 mReqUsage = 0;
83 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Peiyong Lin34beb7a2018-03-28 11:57:12 -070084 mDataSpace = Dataspace::UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080085 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
86 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070087 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080088 mDefaultWidth = 0;
89 mDefaultHeight = 0;
90 mUserWidth = 0;
91 mUserHeight = 0;
92 mTransformHint = 0;
93 mConsumerRunningBehind = false;
94 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070095 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070096 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070097}
98
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070099Surface::~Surface() {
100 if (mConnectedToCpu) {
101 Surface::disconnect(NATIVE_WINDOW_API_CPU);
102 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800103}
104
Brian Anderson3da8d272016-07-28 16:20:47 -0700105sp<ISurfaceComposer> Surface::composerService() const {
106 return ComposerService::getComposerService();
107}
108
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800109nsecs_t Surface::now() const {
110 return systemTime();
111}
112
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800113sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
114 return mGraphicBufferProducer;
115}
116
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900117void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
118 mGraphicBufferProducer->setSidebandStream(stream);
119}
120
Dan Stoza29a3e902014-06-20 13:13:57 -0700121void Surface::allocateBuffers() {
122 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
123 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700124 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
125 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700126}
127
Dan Stoza812ed062015-06-02 15:45:22 -0700128status_t Surface::setGenerationNumber(uint32_t generation) {
129 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
130 if (result == NO_ERROR) {
131 mGenerationNumber = generation;
132 }
133 return result;
134}
135
Dan Stoza7dde5992015-05-22 09:51:44 -0700136uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700137 Mutex::Autolock lock(mMutex);
138 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700139}
140
Dan Stozac6f30bd2015-06-08 09:32:50 -0700141String8 Surface::getConsumerName() const {
142 return mGraphicBufferProducer->getConsumerName();
143}
144
Dan Stoza127fc632015-06-30 13:43:32 -0700145status_t Surface::setDequeueTimeout(nsecs_t timeout) {
146 return mGraphicBufferProducer->setDequeueTimeout(timeout);
147}
148
Dan Stoza50101d02016-04-07 16:53:23 -0700149status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700150 sp<Fence>* outFence, float outTransformMatrix[16]) {
151 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
152 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700153}
154
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800155status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
156 ATRACE_CALL();
157
158 DisplayStatInfo stats;
Yi Kong48a619f2018-06-05 16:34:59 -0700159 status_t result = composerService()->getDisplayStats(nullptr, &stats);
Chih-Hung Hsiehd66be0a2017-10-05 13:51:32 -0700160 if (result != NO_ERROR) {
161 return result;
162 }
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800163
164 *outRefreshDuration = stats.vsyncPeriod;
165
166 return NO_ERROR;
167}
168
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700169void Surface::enableFrameTimestamps(bool enable) {
170 Mutex::Autolock lock(mMutex);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800171 // If going from disabled to enabled, get the initial values for
172 // compositor and display timing.
173 if (!mEnableFrameTimestamps && enable) {
174 FrameEventHistoryDelta delta;
175 mGraphicBufferProducer->getFrameTimestamps(&delta);
176 mFrameEventHistory->applyDelta(delta);
177 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700178 mEnableFrameTimestamps = enable;
179}
180
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800181status_t Surface::getCompositorTiming(
182 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
183 nsecs_t* compositeToPresentLatency) {
184 Mutex::Autolock lock(mMutex);
185 if (!mEnableFrameTimestamps) {
186 return INVALID_OPERATION;
187 }
188
189 if (compositeDeadline != nullptr) {
190 *compositeDeadline =
191 mFrameEventHistory->getNextCompositeDeadline(now());
192 }
193 if (compositeInterval != nullptr) {
194 *compositeInterval = mFrameEventHistory->getCompositeInterval();
195 }
196 if (compositeToPresentLatency != nullptr) {
197 *compositeToPresentLatency =
198 mFrameEventHistory->getCompositeToPresentLatency();
199 }
200 return NO_ERROR;
201}
202
Brian Anderson50143b32016-09-30 14:01:24 -0700203static bool checkConsumerForUpdates(
204 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700205 const nsecs_t* outLatchTime,
206 const nsecs_t* outFirstRefreshStartTime,
207 const nsecs_t* outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700208 const nsecs_t* outGpuCompositionDoneTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700209 const nsecs_t* outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700210 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700211 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700212 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
213 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700214 !e->hasFirstRefreshStartInfo();
Brian Andersonb04c6f02016-10-21 12:57:46 -0700215 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700216 !e->hasGpuCompositionDoneInfo();
217 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
218 !e->hasDisplayPresentInfo();
219
Brian Anderson6b376712017-04-04 10:51:39 -0700220 // LastRefreshStart, DequeueReady, and Release are never available for the
221 // last frame.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700222 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
223 !e->hasLastRefreshStartInfo() &&
224 (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700225 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
226 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700227 bool checkForRelease = (outReleaseTime != nullptr) &&
228 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
229
230 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700231 return checkForLatch || checkForFirstRefreshStart ||
Brian Andersonb04c6f02016-10-21 12:57:46 -0700232 checkForLastRefreshStart || checkForGpuCompositionDone ||
Brian Anderson4e606e32017-03-16 15:34:57 -0700233 checkForDisplayPresent || checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700234}
235
Brian Anderson3d4039d2016-09-23 16:31:30 -0700236static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
237 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700238 // We always get valid timestamps for these eventually.
239 *dst = (src == FrameEvents::TIMESTAMP_PENDING) ?
240 NATIVE_WINDOW_TIMESTAMP_PENDING : src;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700241 }
242}
243
Brian Andersondc96fdf2017-03-20 16:54:25 -0700244static void getFrameTimestampFence(nsecs_t *dst,
245 const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700246 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700247 if (!fenceShouldBeKnown) {
248 *dst = NATIVE_WINDOW_TIMESTAMP_PENDING;
249 return;
250 }
251
Brian Anderson3d4039d2016-09-23 16:31:30 -0700252 nsecs_t signalTime = src->getSignalTime();
Brian Andersondc96fdf2017-03-20 16:54:25 -0700253 *dst = (signalTime == Fence::SIGNAL_TIME_PENDING) ?
254 NATIVE_WINDOW_TIMESTAMP_PENDING :
255 (signalTime == Fence::SIGNAL_TIME_INVALID) ?
256 NATIVE_WINDOW_TIMESTAMP_INVALID :
257 signalTime;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700258 }
259}
260
Brian Anderson069b3652016-07-22 10:32:47 -0700261status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700262 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700263 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700264 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700265 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
266 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800267 ATRACE_CALL();
268
Brian Anderson3890c392016-07-25 12:48:08 -0700269 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700270
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700271 if (!mEnableFrameTimestamps) {
272 return INVALID_OPERATION;
273 }
274
Brian Anderson6b376712017-04-04 10:51:39 -0700275 // Verify the requested timestamps are supported.
276 querySupportedTimestampsLocked();
277 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
278 return BAD_VALUE;
279 }
280
Brian Anderson3da8d272016-07-28 16:20:47 -0700281 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700282 if (events == nullptr) {
283 // If the entry isn't available in the producer, it's definitely not
284 // available in the consumer.
285 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700286 }
287
Brian Anderson50143b32016-09-30 14:01:24 -0700288 // Update our cache of events if the requested events are not available.
289 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700290 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700291 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700292 outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700293 FrameEventHistoryDelta delta;
294 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700295 mFrameEventHistory->applyDelta(delta);
296 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700297 }
298
Brian Anderson3890c392016-07-25 12:48:08 -0700299 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700300 // The entry was available before the update, but was overwritten
301 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700302 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800303 }
Brian Anderson069b3652016-07-22 10:32:47 -0700304
Brian Anderson3d4039d2016-09-23 16:31:30 -0700305 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700306 getFrameTimestamp(outLatchTime, events->latchTime);
307 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
308 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
309 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700310
Brian Andersondc96fdf2017-03-20 16:54:25 -0700311 getFrameTimestampFence(outAcquireTime, events->acquireFence,
312 events->hasAcquireInfo());
313 getFrameTimestampFence(outGpuCompositionDoneTime,
314 events->gpuCompositionDoneFence,
315 events->hasGpuCompositionDoneInfo());
316 getFrameTimestampFence(outDisplayPresentTime, events->displayPresentFence,
317 events->hasDisplayPresentInfo());
318 getFrameTimestampFence(outReleaseTime, events->releaseFence,
319 events->hasReleaseInfo());
Brian Anderson069b3652016-07-22 10:32:47 -0700320
321 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800322}
323
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600324using namespace android::hardware::configstore;
325using namespace android::hardware::configstore::V1_0;
326
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700327status_t Surface::getWideColorSupport(bool* supported) {
328 ATRACE_CALL();
329
330 sp<IBinder> display(
331 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Peiyong Lina52f0292018-03-14 17:26:31 -0700332 Vector<ColorMode> colorModes;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700333 status_t err =
334 composerService()->getDisplayColorModes(display, &colorModes);
335
336 if (err)
337 return err;
338
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600339 bool wideColorBoardConfig =
340 getBool<ISurfaceFlingerConfigs,
341 &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
342
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700343 *supported = false;
Peiyong Lina52f0292018-03-14 17:26:31 -0700344 for (ColorMode colorMode : colorModes) {
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700345 switch (colorMode) {
Peiyong Lina52f0292018-03-14 17:26:31 -0700346 case ColorMode::DISPLAY_P3:
347 case ColorMode::ADOBE_RGB:
348 case ColorMode::DCI_P3:
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600349 if (wideColorBoardConfig) {
350 *supported = true;
351 }
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700352 break;
353 default:
354 break;
355 }
356 }
357
358 return NO_ERROR;
359}
360
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700361status_t Surface::getHdrSupport(bool* supported) {
362 ATRACE_CALL();
363
364 sp<IBinder> display(
365 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
366 HdrCapabilities hdrCapabilities;
367 status_t err =
368 composerService()->getHdrCapabilities(display, &hdrCapabilities);
369
370 if (err)
371 return err;
372
373 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
374
375 return NO_ERROR;
376}
377
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800378int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
379 Surface* c = getSelf(window);
380 return c->setSwapInterval(interval);
381}
382
383int Surface::hook_dequeueBuffer(ANativeWindow* window,
384 ANativeWindowBuffer** buffer, int* fenceFd) {
385 Surface* c = getSelf(window);
386 return c->dequeueBuffer(buffer, fenceFd);
387}
388
389int Surface::hook_cancelBuffer(ANativeWindow* window,
390 ANativeWindowBuffer* buffer, int fenceFd) {
391 Surface* c = getSelf(window);
392 return c->cancelBuffer(buffer, fenceFd);
393}
394
395int Surface::hook_queueBuffer(ANativeWindow* window,
396 ANativeWindowBuffer* buffer, int fenceFd) {
397 Surface* c = getSelf(window);
398 return c->queueBuffer(buffer, fenceFd);
399}
400
401int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
402 ANativeWindowBuffer** buffer) {
403 Surface* c = getSelf(window);
404 ANativeWindowBuffer* buf;
405 int fenceFd = -1;
406 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600407 if (result != OK) {
408 return result;
409 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800410 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700411 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800412 if (waitResult != OK) {
413 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
414 waitResult);
415 c->cancelBuffer(buf, -1);
416 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700417 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800418 *buffer = buf;
419 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700420}
421
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800422int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
423 ANativeWindowBuffer* buffer) {
424 Surface* c = getSelf(window);
425 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700426}
427
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800428int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
429 ANativeWindowBuffer* buffer) {
430 Surface* c = getSelf(window);
431 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700432}
433
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800434int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
435 ANativeWindowBuffer* buffer) {
436 Surface* c = getSelf(window);
437 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700438}
Mathias Agopian62185b72009-04-16 16:19:50 -0700439
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800440int Surface::hook_query(const ANativeWindow* window,
441 int what, int* value) {
442 const Surface* c = getSelf(window);
443 return c->query(what, value);
444}
445
446int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
447 va_list args;
448 va_start(args, operation);
449 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700450 int result = c->perform(operation, args);
451 va_end(args);
452 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800453}
454
455int Surface::setSwapInterval(int interval) {
456 ATRACE_CALL();
457 // EGL specification states:
458 // interval is silently clamped to minimum and maximum implementation
459 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800460
461 if (interval < minSwapInterval)
462 interval = minSwapInterval;
463
464 if (interval > maxSwapInterval)
465 interval = maxSwapInterval;
466
Jorim Jaggi0a3e7842018-07-17 13:48:33 +0200467 const bool wasSwapIntervalZero = mSwapIntervalZero;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700468 mSwapIntervalZero = (interval == 0);
Jorim Jaggi0a3e7842018-07-17 13:48:33 +0200469
470 if (mSwapIntervalZero != wasSwapIntervalZero) {
471 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
472 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800473
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700474 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800475}
476
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700477int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800478 ATRACE_CALL();
479 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800480
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800481 uint32_t reqWidth;
482 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800483 PixelFormat reqFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700484 uint64_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700485 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800486
487 {
488 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700489 if (mReportRemovedBuffers) {
490 mRemovedBuffers.clear();
491 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800492
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800493 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
494 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800495
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800496 reqFormat = mReqFormat;
497 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800498
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700499 enableFrameTimestamps = mEnableFrameTimestamps;
500
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700501 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800502 BufferItem::INVALID_BUFFER_SLOT) {
503 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
Yi Kong48a619f2018-06-05 16:34:59 -0700504 if (gbuf != nullptr) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800505 *buffer = gbuf.get();
506 *fenceFd = -1;
507 return OK;
508 }
509 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800510 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
511
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800512 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800513 sp<Fence> fence;
Dan Stoza932f0082017-05-31 13:50:16 -0700514 nsecs_t startTime = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700515
516 FrameEventHistoryDelta frameTimestamps;
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600517 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, reqWidth, reqHeight,
518 reqFormat, reqUsage, &mBufferAge,
519 enableFrameTimestamps ? &frameTimestamps
520 : nullptr);
Dan Stoza932f0082017-05-31 13:50:16 -0700521 mLastDequeueDuration = systemTime() - startTime;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800522
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800523 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700524 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700525 "(%d, %d, %d, %#" PRIx64 ") failed: %d",
526 reqWidth, reqHeight, reqFormat, reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800527 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700528 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800529
Dan Stoza90ce2a92017-05-01 16:31:53 -0700530 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
531 ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf);
532 android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging
533 return FAILED_TRANSACTION;
534 }
535
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800536 Mutex::Autolock lock(mMutex);
537
Dan Stoza932f0082017-05-31 13:50:16 -0700538 // Write this while holding the mutex
539 mLastDequeueStartTime = startTime;
540
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800541 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700542
543 // this should never happen
Yi Kong48a619f2018-06-05 16:34:59 -0700544 ALOGE_IF(fence == nullptr, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700545
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800546 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
547 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700548 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700549
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700550 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700551 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700552 }
553
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700554 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
555 if (mReportRemovedBuffers && (gbuf != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700556 mRemovedBuffers.push_back(gbuf);
557 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800558 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
559 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700560 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700561 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800562 return result;
563 }
564 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700565
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800566 if (fence->isValid()) {
567 *fenceFd = fence->dup();
568 if (*fenceFd == -1) {
569 ALOGE("dequeueBuffer: error duping fence: %d", errno);
570 // dup() should never fail; something is badly wrong. Soldier on
571 // and hope for the best; the worst that should happen is some
572 // visible corruption that lasts until the next frame.
573 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700574 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800575 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700576 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800577
578 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800579
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700580 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800581 mSharedBufferSlot = buf;
582 mSharedBufferHasBeenQueued = false;
583 } else if (mSharedBufferSlot == buf) {
584 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
585 mSharedBufferHasBeenQueued = false;
586 }
587
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800588 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700589}
590
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800591int Surface::cancelBuffer(android_native_buffer_t* buffer,
592 int fenceFd) {
593 ATRACE_CALL();
594 ALOGV("Surface::cancelBuffer");
595 Mutex::Autolock lock(mMutex);
596 int i = getSlotFromBufferLocked(buffer);
597 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900598 if (fenceFd >= 0) {
599 close(fenceFd);
600 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800601 return i;
602 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800603 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
604 if (fenceFd >= 0) {
605 close(fenceFd);
606 }
607 return OK;
608 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800609 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
610 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800611
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700612 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800613 mSharedBufferHasBeenQueued = true;
614 }
615
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800616 return OK;
617}
618
619int Surface::getSlotFromBufferLocked(
620 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800621 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Yi Kong48a619f2018-06-05 16:34:59 -0700622 if (mSlots[i].buffer != nullptr &&
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800623 mSlots[i].buffer->handle == buffer->handle) {
624 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700625 }
626 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800627 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
628 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700629}
630
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800631int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800632 ALOGV("Surface::lockBuffer");
633 Mutex::Autolock lock(mMutex);
634 return OK;
635}
Mathias Agopian631f3582010-05-25 17:51:34 -0700636
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800637int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
638 ATRACE_CALL();
639 ALOGV("Surface::queueBuffer");
640 Mutex::Autolock lock(mMutex);
641 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700642 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800643
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800644 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700645 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700646 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700647 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700648 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800649 } else {
650 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700651 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800652 int i = getSlotFromBufferLocked(buffer);
653 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900654 if (fenceFd >= 0) {
655 close(fenceFd);
656 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800657 return i;
658 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800659 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
660 if (fenceFd >= 0) {
661 close(fenceFd);
662 }
663 return OK;
664 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800667 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700668 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800669 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800671 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
672 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700673 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700674 static_cast<android_dataspace>(mDataSpace), crop, mScalingMode,
675 mTransform ^ mStickyTransform, fence, mStickyTransform,
676 mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700677
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700678 // we should send HDR metadata as needed if this becomes a bottleneck
679 input.setHdrMetadata(mHdrMetadata);
680
Dan Stozac62acbd2015-04-21 16:42:49 -0700681 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700682 input.setSurfaceDamage(Region::INVALID_REGION);
683 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700684 // Here we do two things:
685 // 1) The surface damage was specified using the OpenGL ES convention of
686 // the origin being in the bottom-left corner. Here we flip to the
687 // convention that the rest of the system uses (top-left corner) by
688 // subtracting all top/bottom coordinates from the buffer height.
689 // 2) If the buffer is coming in rotated (for example, because the EGL
690 // implementation is reacting to the transform hint coming back from
691 // SurfaceFlinger), the surface damage needs to be rotated the
692 // opposite direction, since it was generated assuming an unrotated
693 // buffer (the app doesn't know that the EGL implementation is
694 // reacting to the transform hint behind its back). The
695 // transformations in the switch statement below apply those
696 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
697
698 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700699 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700700 bool rotated90 = (mTransform ^ mStickyTransform) &
701 NATIVE_WINDOW_TRANSFORM_ROT_90;
702 if (rotated90) {
703 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700704 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700705
Dan Stoza5065a552015-03-17 16:23:42 -0700706 Region flippedRegion;
707 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700708 int left = rect.left;
709 int right = rect.right;
710 int top = height - rect.bottom; // Flip from OpenGL convention
711 int bottom = height - rect.top; // Flip from OpenGL convention
712 switch (mTransform ^ mStickyTransform) {
713 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
714 // Rotate 270 degrees
715 Rect flippedRect{top, width - right, bottom, width - left};
716 flippedRegion.orSelf(flippedRect);
717 break;
718 }
719 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
720 // Rotate 180 degrees
721 Rect flippedRect{width - right, height - bottom,
722 width - left, height - top};
723 flippedRegion.orSelf(flippedRect);
724 break;
725 }
726 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
727 // Rotate 90 degrees
728 Rect flippedRect{height - bottom, left,
729 height - top, right};
730 flippedRegion.orSelf(flippedRect);
731 break;
732 }
733 default: {
734 Rect flippedRect{left, top, right, bottom};
735 flippedRegion.orSelf(flippedRect);
736 break;
737 }
738 }
Dan Stoza5065a552015-03-17 16:23:42 -0700739 }
740
741 input.setSurfaceDamage(flippedRegion);
742 }
743
Dan Stoza70ccba52016-07-01 14:00:40 -0700744 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800745 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700746 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800747 if (err != OK) {
748 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
749 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800750
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700751 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700752 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700753 // Update timestamps with the local acquire fence.
754 // The consumer doesn't send it back to prevent us from having two
755 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700756 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700757 std::make_shared<FenceTime>(std::move(fence)));
758
759 // Cache timestamps of signaled fences so we can close their file
760 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700761 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700762 }
763
Brian Anderson50143b32016-09-30 14:01:24 -0700764 mLastFrameNumber = mNextFrameNumber;
765
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700766 mDefaultWidth = output.width;
767 mDefaultHeight = output.height;
768 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700769
Ruben Brunk1681d952014-06-27 15:51:55 -0700770 // Disable transform hint if sticky transform is set.
771 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700772 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700773 }
774
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700775 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700776
Dan Stozac62acbd2015-04-21 16:42:49 -0700777 if (!mConnectedToCpu) {
778 // Clear surface damage back to full-buffer
779 mDirtyRegion = Region::INVALID_REGION;
780 }
Dan Stoza5065a552015-03-17 16:23:42 -0700781
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700782 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800783 mSharedBufferHasBeenQueued = true;
784 }
785
Robert Carr9f31e292016-04-11 11:15:32 -0700786 mQueueBufferCondition.broadcast();
787
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800788 return err;
789}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700790
Brian Anderson6b376712017-04-04 10:51:39 -0700791void Surface::querySupportedTimestampsLocked() const {
792 // mMutex must be locked when calling this method.
793
794 if (mQueriedSupportedTimestamps) {
795 return;
796 }
797 mQueriedSupportedTimestamps = true;
798
799 std::vector<FrameEvent> supportedFrameTimestamps;
800 status_t err = composerService()->getSupportedFrameTimestamps(
801 &supportedFrameTimestamps);
802
803 if (err != NO_ERROR) {
804 return;
805 }
806
807 for (auto sft : supportedFrameTimestamps) {
808 if (sft == FrameEvent::DISPLAY_PRESENT) {
809 mFrameTimestampsSupportsPresent = true;
810 }
811 }
812}
813
Mathias Agopiana67932f2011-04-20 14:20:59 -0700814int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800815 ATRACE_CALL();
816 ALOGV("Surface::query");
817 { // scope for the lock
818 Mutex::Autolock lock(mMutex);
819 switch (what) {
820 case NATIVE_WINDOW_FORMAT:
821 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800822 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800823 return NO_ERROR;
824 }
825 break;
826 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700827 if (composerService()->authenticateSurfaceTexture(
828 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800829 *value = 1;
830 } else {
831 *value = 0;
832 }
833 return NO_ERROR;
834 }
835 case NATIVE_WINDOW_CONCRETE_TYPE:
836 *value = NATIVE_WINDOW_SURFACE;
837 return NO_ERROR;
838 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800839 *value = static_cast<int>(
840 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800841 return NO_ERROR;
842 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800843 *value = static_cast<int>(
844 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800845 return NO_ERROR;
846 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800847 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800848 return NO_ERROR;
849 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
850 status_t err = NO_ERROR;
851 if (!mConsumerRunningBehind) {
852 *value = 0;
853 } else {
854 err = mGraphicBufferProducer->query(what, value);
855 if (err == NO_ERROR) {
856 mConsumerRunningBehind = *value;
857 }
858 }
859 return err;
860 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600861 case NATIVE_WINDOW_BUFFER_AGE: {
862 if (mBufferAge > INT32_MAX) {
863 *value = 0;
864 } else {
865 *value = static_cast<int32_t>(mBufferAge);
866 }
867 return NO_ERROR;
868 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700869 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
870 int64_t durationUs = mLastDequeueDuration / 1000;
871 *value = durationUs > std::numeric_limits<int>::max() ?
872 std::numeric_limits<int>::max() :
873 static_cast<int>(durationUs);
874 return NO_ERROR;
875 }
876 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
877 int64_t durationUs = mLastQueueDuration / 1000;
878 *value = durationUs > std::numeric_limits<int>::max() ?
879 std::numeric_limits<int>::max() :
880 static_cast<int>(durationUs);
881 return NO_ERROR;
882 }
Brian Anderson6b376712017-04-04 10:51:39 -0700883 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
884 querySupportedTimestampsLocked();
885 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
886 return NO_ERROR;
887 }
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800888 case NATIVE_WINDOW_IS_VALID: {
889 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
890 return NO_ERROR;
891 }
Peiyong Lin654f87b2018-01-30 14:21:33 -0800892 case NATIVE_WINDOW_DATASPACE: {
893 *value = static_cast<int>(mDataSpace);
894 return NO_ERROR;
895 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800896 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700897 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800898 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800899}
900
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800901int Surface::perform(int operation, va_list args)
902{
903 int res = NO_ERROR;
904 switch (operation) {
905 case NATIVE_WINDOW_CONNECT:
906 // deprecated. must return NO_ERROR.
907 break;
908 case NATIVE_WINDOW_DISCONNECT:
909 // deprecated. must return NO_ERROR.
910 break;
911 case NATIVE_WINDOW_SET_USAGE:
912 res = dispatchSetUsage(args);
913 break;
914 case NATIVE_WINDOW_SET_CROP:
915 res = dispatchSetCrop(args);
916 break;
917 case NATIVE_WINDOW_SET_BUFFER_COUNT:
918 res = dispatchSetBufferCount(args);
919 break;
920 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
921 res = dispatchSetBuffersGeometry(args);
922 break;
923 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
924 res = dispatchSetBuffersTransform(args);
925 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700926 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
927 res = dispatchSetBuffersStickyTransform(args);
928 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800929 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
930 res = dispatchSetBuffersTimestamp(args);
931 break;
932 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
933 res = dispatchSetBuffersDimensions(args);
934 break;
935 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
936 res = dispatchSetBuffersUserDimensions(args);
937 break;
938 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
939 res = dispatchSetBuffersFormat(args);
940 break;
941 case NATIVE_WINDOW_LOCK:
942 res = dispatchLock(args);
943 break;
944 case NATIVE_WINDOW_UNLOCK_AND_POST:
945 res = dispatchUnlockAndPost(args);
946 break;
947 case NATIVE_WINDOW_SET_SCALING_MODE:
948 res = dispatchSetScalingMode(args);
949 break;
950 case NATIVE_WINDOW_API_CONNECT:
951 res = dispatchConnect(args);
952 break;
953 case NATIVE_WINDOW_API_DISCONNECT:
954 res = dispatchDisconnect(args);
955 break;
Rachad7cb0d392014-07-29 17:53:53 -0700956 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
957 res = dispatchSetSidebandStream(args);
958 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800959 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
960 res = dispatchSetBuffersDataSpace(args);
961 break;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700962 case NATIVE_WINDOW_SET_BUFFERS_SMPTE2086_METADATA:
963 res = dispatchSetBuffersSmpte2086Metadata(args);
964 break;
965 case NATIVE_WINDOW_SET_BUFFERS_CTA861_3_METADATA:
966 res = dispatchSetBuffersCta8613Metadata(args);
967 break;
Valerie Haua82679d2018-11-21 09:31:43 -0800968 case NATIVE_WINDOW_SET_BUFFERS_HDR10_PLUS_METADATA:
969 res = dispatchSetBuffersHdr10PlusMetadata(args);
970 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700971 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
972 res = dispatchSetSurfaceDamage(args);
973 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700974 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
975 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700976 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800977 case NATIVE_WINDOW_SET_AUTO_REFRESH:
978 res = dispatchSetAutoRefresh(args);
979 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800980 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
981 res = dispatchGetDisplayRefreshCycleDuration(args);
982 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800983 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
984 res = dispatchGetNextFrameId(args);
985 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700986 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
987 res = dispatchEnableFrameTimestamps(args);
988 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800989 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
990 res = dispatchGetCompositorTiming(args);
991 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800992 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
993 res = dispatchGetFrameTimestamps(args);
994 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700995 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
996 res = dispatchGetWideColorSupport(args);
997 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700998 case NATIVE_WINDOW_GET_HDR_SUPPORT:
999 res = dispatchGetHdrSupport(args);
1000 break;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001001 case NATIVE_WINDOW_SET_USAGE64:
1002 res = dispatchSetUsage64(args);
1003 break;
Chia-I Wue2786ea2017-08-07 10:36:08 -07001004 case NATIVE_WINDOW_GET_CONSUMER_USAGE64:
1005 res = dispatchGetConsumerUsage64(args);
1006 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001007 default:
1008 res = NAME_NOT_FOUND;
1009 break;
1010 }
1011 return res;
1012}
Mathias Agopiana138f892010-05-21 17:24:35 -07001013
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001014int Surface::dispatchConnect(va_list args) {
1015 int api = va_arg(args, int);
1016 return connect(api);
1017}
Mathias Agopian55fa2512010-03-11 15:06:54 -08001018
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001019int Surface::dispatchDisconnect(va_list args) {
1020 int api = va_arg(args, int);
1021 return disconnect(api);
1022}
1023
1024int Surface::dispatchSetUsage(va_list args) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001025 uint64_t usage = va_arg(args, uint32_t);
1026 return setUsage(usage);
1027}
1028
1029int Surface::dispatchSetUsage64(va_list args) {
1030 uint64_t usage = va_arg(args, uint64_t);
1031 return setUsage(usage);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001032}
1033
1034int Surface::dispatchSetCrop(va_list args) {
1035 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
1036 return setCrop(reinterpret_cast<Rect const*>(rect));
1037}
1038
1039int Surface::dispatchSetBufferCount(va_list args) {
1040 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001041 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001042}
1043
1044int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001045 uint32_t width = va_arg(args, uint32_t);
1046 uint32_t height = va_arg(args, uint32_t);
1047 PixelFormat format = va_arg(args, PixelFormat);
1048 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001049 if (err != 0) {
1050 return err;
1051 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001052 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001053}
1054
1055int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001056 uint32_t width = va_arg(args, uint32_t);
1057 uint32_t height = va_arg(args, uint32_t);
1058 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001059}
1060
1061int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001062 uint32_t width = va_arg(args, uint32_t);
1063 uint32_t height = va_arg(args, uint32_t);
1064 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001065}
1066
1067int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001068 PixelFormat format = va_arg(args, PixelFormat);
1069 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001070}
1071
1072int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001073 int mode = va_arg(args, int);
1074 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001075}
1076
1077int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001078 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001079 return setBuffersTransform(transform);
1080}
1081
Ruben Brunk1681d952014-06-27 15:51:55 -07001082int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001083 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -07001084 return setBuffersStickyTransform(transform);
1085}
1086
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001087int Surface::dispatchSetBuffersTimestamp(va_list args) {
1088 int64_t timestamp = va_arg(args, int64_t);
1089 return setBuffersTimestamp(timestamp);
1090}
1091
1092int Surface::dispatchLock(va_list args) {
1093 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1094 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1095 return lock(outBuffer, inOutDirtyBounds);
1096}
1097
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001098int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001099 return unlockAndPost();
1100}
1101
Rachad7cb0d392014-07-29 17:53:53 -07001102int Surface::dispatchSetSidebandStream(va_list args) {
1103 native_handle_t* sH = va_arg(args, native_handle_t*);
1104 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1105 setSidebandStream(sidebandHandle);
1106 return OK;
1107}
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001108
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001109int Surface::dispatchSetBuffersDataSpace(va_list args) {
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001110 Dataspace dataspace = static_cast<Dataspace>(va_arg(args, int));
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001111 return setBuffersDataSpace(dataspace);
1112}
1113
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001114int Surface::dispatchSetBuffersSmpte2086Metadata(va_list args) {
1115 const android_smpte2086_metadata* metadata =
1116 va_arg(args, const android_smpte2086_metadata*);
1117 return setBuffersSmpte2086Metadata(metadata);
1118}
1119
1120int Surface::dispatchSetBuffersCta8613Metadata(va_list args) {
1121 const android_cta861_3_metadata* metadata =
1122 va_arg(args, const android_cta861_3_metadata*);
1123 return setBuffersCta8613Metadata(metadata);
1124}
1125
Valerie Haua82679d2018-11-21 09:31:43 -08001126int Surface::dispatchSetBuffersHdr10PlusMetadata(va_list args) {
1127 const size_t size = va_arg(args, size_t);
1128 const uint8_t* metadata = va_arg(args, const uint8_t*);
1129 return setBuffersHdr10PlusMetadata(size, metadata);
1130}
1131
Dan Stoza5065a552015-03-17 16:23:42 -07001132int Surface::dispatchSetSurfaceDamage(va_list args) {
1133 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1134 size_t numRects = va_arg(args, size_t);
1135 setSurfaceDamage(rects, numRects);
1136 return NO_ERROR;
1137}
1138
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001139int Surface::dispatchSetSharedBufferMode(va_list args) {
1140 bool sharedBufferMode = va_arg(args, int);
1141 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001142}
1143
1144int Surface::dispatchSetAutoRefresh(va_list args) {
1145 bool autoRefresh = va_arg(args, int);
1146 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001147}
1148
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001149int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1150 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1151 return getDisplayRefreshCycleDuration(outRefreshDuration);
1152}
1153
Brian Anderson1049d1d2016-12-16 17:25:57 -08001154int Surface::dispatchGetNextFrameId(va_list args) {
1155 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1156 *nextFrameId = getNextFrameNumber();
1157 return NO_ERROR;
1158}
1159
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001160int Surface::dispatchEnableFrameTimestamps(va_list args) {
1161 bool enable = va_arg(args, int);
1162 enableFrameTimestamps(enable);
1163 return NO_ERROR;
1164}
1165
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001166int Surface::dispatchGetCompositorTiming(va_list args) {
1167 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1168 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1169 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1170 return getCompositorTiming(compositeDeadline, compositeInterval,
1171 compositeToPresentLatency);
1172}
1173
Pablo Ceballosce796e72016-02-04 19:10:51 -08001174int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001175 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001176 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001177 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001178 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1179 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1180 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001181 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001182 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001183 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001184 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001185 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001186 outRequestedPresentTime, outAcquireTime, outLatchTime,
1187 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001188 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001189 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001190}
1191
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001192int Surface::dispatchGetWideColorSupport(va_list args) {
1193 bool* outSupport = va_arg(args, bool*);
1194 return getWideColorSupport(outSupport);
1195}
1196
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001197int Surface::dispatchGetHdrSupport(va_list args) {
1198 bool* outSupport = va_arg(args, bool*);
1199 return getHdrSupport(outSupport);
1200}
1201
Chia-I Wue2786ea2017-08-07 10:36:08 -07001202int Surface::dispatchGetConsumerUsage64(va_list args) {
1203 uint64_t* usage = va_arg(args, uint64_t*);
1204 return getConsumerUsage(usage);
1205}
1206
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001207int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001208 static sp<IProducerListener> listener = new DummyProducerListener();
1209 return connect(api, listener);
1210}
1211
1212int Surface::connect(int api, const sp<IProducerListener>& listener) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001213 return connect(api, listener, false);
1214}
1215
1216int Surface::connect(
1217 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001218 ATRACE_CALL();
1219 ALOGV("Surface::connect");
1220 Mutex::Autolock lock(mMutex);
1221 IGraphicBufferProducer::QueueBufferOutput output;
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001222 mReportRemovedBuffers = reportBufferRemoval;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001223 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001224 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001225 mDefaultWidth = output.width;
1226 mDefaultHeight = output.height;
1227 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001228
1229 // Disable transform hint if sticky transform is set.
1230 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001231 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001232 }
1233
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001234 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001235 }
1236 if (!err && api == NATIVE_WINDOW_API_CPU) {
1237 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001238 // Clear the dirty region in case we're switching from a non-CPU API
1239 mDirtyRegion.clear();
1240 } else if (!err) {
1241 // Initialize the dirty region for tracking surface damage
1242 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001243 }
Dan Stoza5065a552015-03-17 16:23:42 -07001244
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001245 return err;
1246}
1247
Mathias Agopian365857d2013-09-11 19:35:45 -07001248
Robert Carr97b9c862016-09-08 13:54:35 -07001249int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001250 ATRACE_CALL();
1251 ALOGV("Surface::disconnect");
1252 Mutex::Autolock lock(mMutex);
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001253 mRemovedBuffers.clear();
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001254 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1255 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001256 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001257 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001258 if (!err) {
1259 mReqFormat = 0;
1260 mReqWidth = 0;
1261 mReqHeight = 0;
1262 mReqUsage = 0;
1263 mCrop.clear();
1264 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1265 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001266 mStickyTransform = 0;
1267
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001268 if (api == NATIVE_WINDOW_API_CPU) {
1269 mConnectedToCpu = false;
1270 }
1271 }
1272 return err;
1273}
1274
Dan Stozad9c49712015-04-27 11:06:01 -07001275int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001276 sp<Fence>* outFence) {
1277 ATRACE_CALL();
1278 ALOGV("Surface::detachNextBuffer");
1279
Yi Kong48a619f2018-06-05 16:34:59 -07001280 if (outBuffer == nullptr || outFence == nullptr) {
Dan Stoza231832e2015-03-11 11:55:01 -07001281 return BAD_VALUE;
1282 }
1283
1284 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001285 if (mReportRemovedBuffers) {
1286 mRemovedBuffers.clear();
1287 }
Dan Stoza231832e2015-03-11 11:55:01 -07001288
Yi Kong48a619f2018-06-05 16:34:59 -07001289 sp<GraphicBuffer> buffer(nullptr);
1290 sp<Fence> fence(nullptr);
Dan Stoza231832e2015-03-11 11:55:01 -07001291 status_t result = mGraphicBufferProducer->detachNextBuffer(
1292 &buffer, &fence);
1293 if (result != NO_ERROR) {
1294 return result;
1295 }
1296
Dan Stozad9c49712015-04-27 11:06:01 -07001297 *outBuffer = buffer;
Yi Kong48a619f2018-06-05 16:34:59 -07001298 if (fence != nullptr && fence->isValid()) {
Dan Stoza231832e2015-03-11 11:55:01 -07001299 *outFence = fence;
1300 } else {
1301 *outFence = Fence::NO_FENCE;
1302 }
1303
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001304 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Yi Kong48a619f2018-06-05 16:34:59 -07001305 if (mSlots[i].buffer != nullptr &&
Shuzhen Wang55be5052017-05-23 09:41:54 -07001306 mSlots[i].buffer->getId() == buffer->getId()) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001307 if (mReportRemovedBuffers) {
1308 mRemovedBuffers.push_back(mSlots[i].buffer);
1309 }
Yi Kong48a619f2018-06-05 16:34:59 -07001310 mSlots[i].buffer = nullptr;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001311 }
1312 }
1313
Dan Stoza231832e2015-03-11 11:55:01 -07001314 return NO_ERROR;
1315}
1316
1317int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1318{
1319 ATRACE_CALL();
1320 ALOGV("Surface::attachBuffer");
1321
1322 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001323 if (mReportRemovedBuffers) {
1324 mRemovedBuffers.clear();
1325 }
Dan Stoza231832e2015-03-11 11:55:01 -07001326
1327 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001328 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1329 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001330 int32_t attachedSlot = -1;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001331 status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer);
Dan Stoza231832e2015-03-11 11:55:01 -07001332 if (result != NO_ERROR) {
1333 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001334 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001335 return result;
1336 }
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001337 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001338 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1339 }
Dan Stoza231832e2015-03-11 11:55:01 -07001340 mSlots[attachedSlot].buffer = graphicBuffer;
1341
1342 return NO_ERROR;
1343}
1344
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001345int Surface::setUsage(uint64_t reqUsage)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001346{
1347 ALOGV("Surface::setUsage");
1348 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001349 if (reqUsage != mReqUsage) {
1350 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1351 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001352 mReqUsage = reqUsage;
1353 return OK;
1354}
1355
1356int Surface::setCrop(Rect const* rect)
1357{
1358 ATRACE_CALL();
1359
Pablo Ceballos60d69222015-08-07 14:47:20 -07001360 Rect realRect(Rect::EMPTY_RECT);
Yi Kong48a619f2018-06-05 16:34:59 -07001361 if (rect == nullptr || rect->isEmpty()) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001362 realRect.clear();
1363 } else {
1364 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001365 }
1366
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001367 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1368 realRect.left, realRect.top, realRect.right, realRect.bottom);
1369
1370 Mutex::Autolock lock(mMutex);
1371 mCrop = realRect;
1372 return NO_ERROR;
1373}
1374
1375int Surface::setBufferCount(int bufferCount)
1376{
1377 ATRACE_CALL();
1378 ALOGV("Surface::setBufferCount");
1379 Mutex::Autolock lock(mMutex);
1380
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001381 status_t err = NO_ERROR;
1382 if (bufferCount == 0) {
1383 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1384 } else {
1385 int minUndequeuedBuffers = 0;
1386 err = mGraphicBufferProducer->query(
1387 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1388 if (err == NO_ERROR) {
1389 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1390 bufferCount - minUndequeuedBuffers);
1391 }
1392 }
Mathias Agopian90147262010-01-22 11:47:55 -08001393
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001394 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1395 bufferCount, strerror(-err));
1396
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001397 return err;
1398}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001399
Pablo Ceballosfa455352015-08-12 17:47:47 -07001400int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1401 ATRACE_CALL();
1402 ALOGV("Surface::setMaxDequeuedBufferCount");
1403 Mutex::Autolock lock(mMutex);
1404
1405 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1406 maxDequeuedBuffers);
1407 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1408 "returned %s", maxDequeuedBuffers, strerror(-err));
1409
Pablo Ceballosfa455352015-08-12 17:47:47 -07001410 return err;
1411}
1412
1413int Surface::setAsyncMode(bool async) {
1414 ATRACE_CALL();
1415 ALOGV("Surface::setAsyncMode");
1416 Mutex::Autolock lock(mMutex);
1417
1418 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1419 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1420 async, strerror(-err));
1421
Pablo Ceballosfa455352015-08-12 17:47:47 -07001422 return err;
1423}
1424
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001425int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001426 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001427 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001428 Mutex::Autolock lock(mMutex);
1429
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001430 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1431 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001432 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001433 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001434 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001435 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1436 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001437
1438 return err;
1439}
1440
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001441int Surface::setAutoRefresh(bool autoRefresh) {
1442 ATRACE_CALL();
1443 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1444 Mutex::Autolock lock(mMutex);
1445
1446 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1447 if (err == NO_ERROR) {
1448 mAutoRefresh = autoRefresh;
1449 }
1450 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1451 autoRefresh, strerror(-err));
1452 return err;
1453}
1454
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001455int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001456{
1457 ATRACE_CALL();
1458 ALOGV("Surface::setBuffersDimensions");
1459
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001460 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001461 return BAD_VALUE;
1462
1463 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001464 if (width != mReqWidth || height != mReqHeight) {
1465 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1466 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001467 mReqWidth = width;
1468 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001469 return NO_ERROR;
1470}
1471
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001472int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001473{
1474 ATRACE_CALL();
1475 ALOGV("Surface::setBuffersUserDimensions");
1476
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001477 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001478 return BAD_VALUE;
1479
1480 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001481 if (width != mUserWidth || height != mUserHeight) {
1482 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1483 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001484 mUserWidth = width;
1485 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001486 return NO_ERROR;
1487}
1488
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001489int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001490{
1491 ALOGV("Surface::setBuffersFormat");
1492
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001493 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001494 if (format != mReqFormat) {
1495 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1496 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001497 mReqFormat = format;
1498 return NO_ERROR;
1499}
1500
1501int Surface::setScalingMode(int mode)
1502{
1503 ATRACE_CALL();
1504 ALOGV("Surface::setScalingMode(%d)", mode);
1505
1506 switch (mode) {
1507 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1508 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1509 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001510 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001511 break;
1512 default:
1513 ALOGE("unknown scaling mode: %d", mode);
1514 return BAD_VALUE;
1515 }
1516
1517 Mutex::Autolock lock(mMutex);
1518 mScalingMode = mode;
1519 return NO_ERROR;
1520}
1521
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001522int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001523{
1524 ATRACE_CALL();
1525 ALOGV("Surface::setBuffersTransform");
1526 Mutex::Autolock lock(mMutex);
1527 mTransform = transform;
1528 return NO_ERROR;
1529}
1530
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001531int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001532{
1533 ATRACE_CALL();
1534 ALOGV("Surface::setBuffersStickyTransform");
1535 Mutex::Autolock lock(mMutex);
1536 mStickyTransform = transform;
1537 return NO_ERROR;
1538}
1539
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001540int Surface::setBuffersTimestamp(int64_t timestamp)
1541{
1542 ALOGV("Surface::setBuffersTimestamp");
1543 Mutex::Autolock lock(mMutex);
1544 mTimestamp = timestamp;
1545 return NO_ERROR;
1546}
1547
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001548int Surface::setBuffersDataSpace(Dataspace dataSpace)
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001549{
1550 ALOGV("Surface::setBuffersDataSpace");
1551 Mutex::Autolock lock(mMutex);
1552 mDataSpace = dataSpace;
1553 return NO_ERROR;
1554}
1555
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001556int Surface::setBuffersSmpte2086Metadata(const android_smpte2086_metadata* metadata) {
1557 ALOGV("Surface::setBuffersSmpte2086Metadata");
1558 Mutex::Autolock lock(mMutex);
1559 if (metadata) {
1560 mHdrMetadata.smpte2086 = *metadata;
1561 mHdrMetadata.validTypes |= HdrMetadata::SMPTE2086;
1562 } else {
1563 mHdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086;
1564 }
1565 return NO_ERROR;
1566}
1567
1568int Surface::setBuffersCta8613Metadata(const android_cta861_3_metadata* metadata) {
1569 ALOGV("Surface::setBuffersCta8613Metadata");
1570 Mutex::Autolock lock(mMutex);
1571 if (metadata) {
1572 mHdrMetadata.cta8613 = *metadata;
1573 mHdrMetadata.validTypes |= HdrMetadata::CTA861_3;
1574 } else {
1575 mHdrMetadata.validTypes &= ~HdrMetadata::CTA861_3;
1576 }
1577 return NO_ERROR;
1578}
1579
Valerie Haua82679d2018-11-21 09:31:43 -08001580int Surface::setBuffersHdr10PlusMetadata(const size_t size, const uint8_t* metadata) {
1581 ALOGV("Surface::setBuffersBlobMetadata");
1582 Mutex::Autolock lock(mMutex);
1583 if (size > 0) {
1584 mHdrMetadata.hdr10plus.assign(metadata, metadata + size);
1585 mHdrMetadata.validTypes |= HdrMetadata::HDR10PLUS;
1586 } else {
1587 mHdrMetadata.validTypes &= ~HdrMetadata::HDR10PLUS;
1588 mHdrMetadata.hdr10plus.clear();
1589 }
1590 return NO_ERROR;
1591}
1592
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001593Dataspace Surface::getBuffersDataSpace() {
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -06001594 ALOGV("Surface::getBuffersDataSpace");
1595 Mutex::Autolock lock(mMutex);
1596 return mDataSpace;
1597}
1598
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001599void Surface::freeAllBuffers() {
1600 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Yi Kong48a619f2018-06-05 16:34:59 -07001601 mSlots[i].buffer = nullptr;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001602 }
1603}
1604
Dan Stoza5065a552015-03-17 16:23:42 -07001605void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1606 ATRACE_CALL();
1607 ALOGV("Surface::setSurfaceDamage");
1608 Mutex::Autolock lock(mMutex);
1609
Dan Stozac62acbd2015-04-21 16:42:49 -07001610 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001611 mDirtyRegion = Region::INVALID_REGION;
1612 return;
1613 }
1614
1615 mDirtyRegion.clear();
1616 for (size_t r = 0; r < numRects; ++r) {
1617 // We intentionally flip top and bottom here, since because they're
1618 // specified with a bottom-left origin, top > bottom, which fails
1619 // validation in the Region class. We will fix this up when we flip to a
1620 // top-left origin in queueBuffer.
1621 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1622 mDirtyRegion.orSelf(rect);
1623 }
1624}
1625
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001626// ----------------------------------------------------------------------
1627// the lock/unlock APIs must be used from the same thread
1628
1629static status_t copyBlt(
1630 const sp<GraphicBuffer>& dst,
1631 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001632 const Region& reg,
1633 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001634{
Dennis Kempinadd458f2017-05-17 19:16:31 -07001635 if (dst->getId() == src->getId())
1636 return OK;
1637
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001638 // src and dst with, height and format must be identical. no verification
1639 // is done here.
1640 status_t err;
Yi Kong48a619f2018-06-05 16:34:59 -07001641 uint8_t* src_bits = nullptr;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001642 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1643 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001644 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1645
Yi Kong48a619f2018-06-05 16:34:59 -07001646 uint8_t* dst_bits = nullptr;
Francis Hart7b09e792015-01-09 11:10:54 +02001647 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1648 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001649 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001650 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001651
1652 Region::const_iterator head(reg.begin());
1653 Region::const_iterator tail(reg.end());
1654 if (head != tail && src_bits && dst_bits) {
1655 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001656 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1657 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001658
1659 while (head != tail) {
1660 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001661 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001662 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001663 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1664 uint8_t const * s = src_bits +
1665 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1666 uint8_t * d = dst_bits +
1667 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001668 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001669 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001670 h = 1;
1671 }
1672 do {
1673 memcpy(d, s, size);
1674 d += dbpr;
1675 s += sbpr;
1676 } while (--h > 0);
1677 }
1678 }
1679
1680 if (src_bits)
1681 src->unlock();
1682
1683 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001684 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001685
1686 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687}
1688
Mathias Agopiana138f892010-05-21 17:24:35 -07001689// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001690
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001691status_t Surface::lock(
1692 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1693{
Yi Kong48a619f2018-06-05 16:34:59 -07001694 if (mLockedBuffer != nullptr) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001695 ALOGE("Surface::lock failed, already locked");
1696 return INVALID_OPERATION;
1697 }
1698
1699 if (!mConnectedToCpu) {
1700 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1701 if (err) {
1702 return err;
1703 }
1704 // we're intending to do software rendering from this point
1705 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1706 }
1707
1708 ANativeWindowBuffer* out;
1709 int fenceFd = -1;
1710 status_t err = dequeueBuffer(&out, &fenceFd);
1711 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1712 if (err == NO_ERROR) {
1713 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001714 const Rect bounds(backBuffer->width, backBuffer->height);
1715
1716 Region newDirtyRegion;
1717 if (inOutDirtyBounds) {
1718 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1719 newDirtyRegion.andSelf(bounds);
1720 } else {
1721 newDirtyRegion.set(bounds);
1722 }
1723
1724 // figure out if we can copy the frontbuffer back
1725 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
Yi Kong48a619f2018-06-05 16:34:59 -07001726 const bool canCopyBack = (frontBuffer != nullptr &&
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001727 backBuffer->width == frontBuffer->width &&
1728 backBuffer->height == frontBuffer->height &&
1729 backBuffer->format == frontBuffer->format);
1730
1731 if (canCopyBack) {
1732 // copy the area that is invalid and not repainted this round
1733 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001734 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001735 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001736 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001737 } else {
1738 // if we can't copy-back anything, modify the user's dirty
1739 // region to make sure they redraw the whole buffer
1740 newDirtyRegion.set(bounds);
1741 mDirtyRegion.clear();
1742 Mutex::Autolock lock(mMutex);
1743 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1744 mSlots[i].dirtyRegion.clear();
1745 }
1746 }
1747
1748
1749 { // scope for the lock
1750 Mutex::Autolock lock(mMutex);
1751 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1752 if (backBufferSlot >= 0) {
1753 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1754 mDirtyRegion.subtract(dirtyRegion);
1755 dirtyRegion = newDirtyRegion;
1756 }
1757 }
1758
1759 mDirtyRegion.orSelf(newDirtyRegion);
1760 if (inOutDirtyBounds) {
1761 *inOutDirtyBounds = newDirtyRegion.getBounds();
1762 }
1763
1764 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001765 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001766 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001767 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001768
1769 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1770 backBuffer->handle);
1771
1772 if (res != 0) {
1773 err = INVALID_OPERATION;
1774 } else {
1775 mLockedBuffer = backBuffer;
1776 outBuffer->width = backBuffer->width;
1777 outBuffer->height = backBuffer->height;
1778 outBuffer->stride = backBuffer->stride;
1779 outBuffer->format = backBuffer->format;
1780 outBuffer->bits = vaddr;
1781 }
1782 }
1783 return err;
1784}
1785
1786status_t Surface::unlockAndPost()
1787{
Yi Kong48a619f2018-06-05 16:34:59 -07001788 if (mLockedBuffer == nullptr) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001789 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1790 return INVALID_OPERATION;
1791 }
1792
Francis Hart8f396012014-04-01 15:30:53 +03001793 int fd = -1;
1794 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001795 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1796
Francis Hart8f396012014-04-01 15:30:53 +03001797 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001798 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1799 mLockedBuffer->handle, strerror(-err));
1800
1801 mPostedBuffer = mLockedBuffer;
Yi Kong48a619f2018-06-05 16:34:59 -07001802 mLockedBuffer = nullptr;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001803 return err;
1804}
1805
Robert Carr9f31e292016-04-11 11:15:32 -07001806bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1807 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001808 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001809 return true;
1810 }
1811 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1812}
1813
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001814status_t Surface::getUniqueId(uint64_t* outId) const {
1815 Mutex::Autolock lock(mMutex);
1816 return mGraphicBufferProducer->getUniqueId(outId);
1817}
1818
Chia-I Wue2786ea2017-08-07 10:36:08 -07001819int Surface::getConsumerUsage(uint64_t* outUsage) const {
1820 Mutex::Autolock lock(mMutex);
1821 return mGraphicBufferProducer->getConsumerUsage(outUsage);
1822}
1823
Dan Stoza932f0082017-05-31 13:50:16 -07001824nsecs_t Surface::getLastDequeueStartTime() const {
1825 Mutex::Autolock lock(mMutex);
1826 return mLastDequeueStartTime;
1827}
1828
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001829status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1830 if (out == nullptr) {
1831 ALOGE("%s: out must not be null!", __FUNCTION__);
1832 return BAD_VALUE;
1833 }
1834
1835 Mutex::Autolock lock(mMutex);
1836 *out = mRemovedBuffers;
1837 mRemovedBuffers.clear();
1838 return OK;
1839}
1840
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001841status_t Surface::attachAndQueueBuffer(Surface* surface, sp<GraphicBuffer> buffer) {
1842 if (buffer == nullptr) {
1843 return BAD_VALUE;
1844 }
1845 int err = static_cast<ANativeWindow*>(surface)->perform(surface, NATIVE_WINDOW_API_CONNECT,
1846 NATIVE_WINDOW_API_CPU);
1847 if (err != OK) {
1848 return err;
1849 }
1850 err = surface->attachBuffer(buffer->getNativeBuffer());
1851 if (err != OK) {
1852 return err;
1853 }
1854 err = static_cast<ANativeWindow*>(surface)->queueBuffer(surface, buffer->getNativeBuffer(), -1);
1855 if (err != OK) {
1856 return err;
1857 }
1858 err = surface->disconnect(NATIVE_WINDOW_API_CPU);
1859 return err;
1860}
1861
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001862}; // namespace android