blob: e82f0cc9e91a78f4741a8e2be0a5dedd96854057 [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
Yiwei Zhang9e919eb2019-03-07 23:21:40 -080023#include <condition_variable>
24#include <deque>
25#include <mutex>
26#include <thread>
27
Mathias Agopiancb496ac2017-05-22 14:21:00 -070028#include <inttypes.h>
29
Mathias Agopian05debe12017-02-08 17:04:18 -080030#include <android/native_window.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080031
Mathias Agopian9cce3252010-02-09 17:46:37 -080032#include <utils/Log.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080033#include <utils/Trace.h>
Rachad7cb0d392014-07-29 17:53:53 -070034#include <utils/NativeHandle.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080035
Ian Elliott62c48c92017-01-20 13:13:20 -070036#include <ui/DisplayStatInfo.h>
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -070037#include <ui/Fence.h>
Shuzhen Wang067fcd32019-08-14 10:41:12 -070038#include <ui/GraphicBuffer.h>
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -070039#include <ui/HdrCapabilities.h>
40#include <ui/Region.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070041
Mathias Agopian2b5dd402017-02-07 17:36:19 -080042#include <gui/BufferItem.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070043#include <gui/IProducerListener.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080044
Mathias Agopian2b5dd402017-02-07 17:36:19 -080045#include <gui/ISurfaceComposer.h>
Steven Thomas62a4cf82020-01-31 12:04:03 -080046#include <gui/LayerState.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080047#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049namespace android {
50
Peiyong Linfd997e02018-03-28 15:29:00 -070051using ui::ColorMode;
Peiyong Lin34beb7a2018-03-28 11:57:12 -070052using ui::Dataspace;
Peiyong Linfd997e02018-03-28 15:29:00 -070053
Alec Mouri09d122a2019-11-25 10:00:53 -080054namespace {
55
56bool isInterceptorRegistrationOp(int op) {
57 return op == NATIVE_WINDOW_SET_CANCEL_INTERCEPTOR ||
58 op == NATIVE_WINDOW_SET_DEQUEUE_INTERCEPTOR ||
59 op == NATIVE_WINDOW_SET_PERFORM_INTERCEPTOR ||
John Reck401cda62020-05-07 16:04:41 -070060 op == NATIVE_WINDOW_SET_QUEUE_INTERCEPTOR ||
61 op == NATIVE_WINDOW_SET_QUERY_INTERCEPTOR;
Alec Mouri09d122a2019-11-25 10:00:53 -080062}
63
64} // namespace
65
Vishnu Nair992496b2020-10-22 17:27:21 -070066Surface::Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp,
67 const sp<IBinder>& surfaceControlHandle)
Ian Elliotta2eb34c2017-07-18 11:05:49 -060068 : mGraphicBufferProducer(bufferProducer),
69 mCrop(Rect::EMPTY_RECT),
70 mBufferAge(0),
71 mGenerationNumber(0),
72 mSharedBufferMode(false),
73 mAutoRefresh(false),
74 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
75 mSharedBufferHasBeenQueued(false),
76 mQueriedSupportedTimestamps(false),
77 mFrameTimestampsSupportsPresent(false),
78 mEnableFrameTimestamps(false),
79 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>()) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -080080 // Initialize the ANativeWindow function pointers.
81 ANativeWindow::setSwapInterval = hook_setSwapInterval;
82 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
83 ANativeWindow::cancelBuffer = hook_cancelBuffer;
84 ANativeWindow::queueBuffer = hook_queueBuffer;
85 ANativeWindow::query = hook_query;
86 ANativeWindow::perform = hook_perform;
87
88 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
89 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
90 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
91 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
92
93 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
94 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
95
96 mReqWidth = 0;
97 mReqHeight = 0;
98 mReqFormat = 0;
99 mReqUsage = 0;
100 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700101 mDataSpace = Dataspace::UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800102 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
103 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -0700104 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800105 mDefaultWidth = 0;
106 mDefaultHeight = 0;
107 mUserWidth = 0;
108 mUserHeight = 0;
109 mTransformHint = 0;
110 mConsumerRunningBehind = false;
111 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -0700112 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700113 mSwapIntervalZero = false;
Yiwei Zhang74c9cc32020-06-20 03:35:17 -0700114 mMaxBufferCount = NUM_BUFFER_SLOTS;
Vishnu Nair992496b2020-10-22 17:27:21 -0700115 mSurfaceControlHandle = surfaceControlHandle;
Mathias Agopian62185b72009-04-16 16:19:50 -0700116}
117
Mathias Agopian35ffa6a2013-03-12 18:45:09 -0700118Surface::~Surface() {
119 if (mConnectedToCpu) {
120 Surface::disconnect(NATIVE_WINDOW_API_CPU);
121 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800122}
123
Brian Anderson3da8d272016-07-28 16:20:47 -0700124sp<ISurfaceComposer> Surface::composerService() const {
125 return ComposerService::getComposerService();
126}
127
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800128nsecs_t Surface::now() const {
129 return systemTime();
130}
131
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800132sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
133 return mGraphicBufferProducer;
134}
135
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900136void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
137 mGraphicBufferProducer->setSidebandStream(stream);
138}
139
Dan Stoza29a3e902014-06-20 13:13:57 -0700140void Surface::allocateBuffers() {
141 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
142 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700143 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
144 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700145}
146
Dan Stoza812ed062015-06-02 15:45:22 -0700147status_t Surface::setGenerationNumber(uint32_t generation) {
148 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
149 if (result == NO_ERROR) {
150 mGenerationNumber = generation;
151 }
152 return result;
153}
154
Dan Stoza7dde5992015-05-22 09:51:44 -0700155uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700156 Mutex::Autolock lock(mMutex);
157 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700158}
159
Dan Stozac6f30bd2015-06-08 09:32:50 -0700160String8 Surface::getConsumerName() const {
161 return mGraphicBufferProducer->getConsumerName();
162}
163
Dan Stoza127fc632015-06-30 13:43:32 -0700164status_t Surface::setDequeueTimeout(nsecs_t timeout) {
165 return mGraphicBufferProducer->setDequeueTimeout(timeout);
166}
167
Dan Stoza50101d02016-04-07 16:53:23 -0700168status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700169 sp<Fence>* outFence, float outTransformMatrix[16]) {
170 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
171 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700172}
173
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800174status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
175 ATRACE_CALL();
176
177 DisplayStatInfo stats;
Yi Kong48a619f2018-06-05 16:34:59 -0700178 status_t result = composerService()->getDisplayStats(nullptr, &stats);
Chih-Hung Hsiehd66be0a2017-10-05 13:51:32 -0700179 if (result != NO_ERROR) {
180 return result;
181 }
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800182
183 *outRefreshDuration = stats.vsyncPeriod;
184
185 return NO_ERROR;
186}
187
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700188void Surface::enableFrameTimestamps(bool enable) {
189 Mutex::Autolock lock(mMutex);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800190 // If going from disabled to enabled, get the initial values for
191 // compositor and display timing.
192 if (!mEnableFrameTimestamps && enable) {
193 FrameEventHistoryDelta delta;
194 mGraphicBufferProducer->getFrameTimestamps(&delta);
195 mFrameEventHistory->applyDelta(delta);
196 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700197 mEnableFrameTimestamps = enable;
198}
199
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800200status_t Surface::getCompositorTiming(
201 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
202 nsecs_t* compositeToPresentLatency) {
203 Mutex::Autolock lock(mMutex);
204 if (!mEnableFrameTimestamps) {
205 return INVALID_OPERATION;
206 }
207
208 if (compositeDeadline != nullptr) {
209 *compositeDeadline =
210 mFrameEventHistory->getNextCompositeDeadline(now());
211 }
212 if (compositeInterval != nullptr) {
213 *compositeInterval = mFrameEventHistory->getCompositeInterval();
214 }
215 if (compositeToPresentLatency != nullptr) {
216 *compositeToPresentLatency =
217 mFrameEventHistory->getCompositeToPresentLatency();
218 }
219 return NO_ERROR;
220}
221
Brian Anderson50143b32016-09-30 14:01:24 -0700222static bool checkConsumerForUpdates(
223 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700224 const nsecs_t* outLatchTime,
225 const nsecs_t* outFirstRefreshStartTime,
226 const nsecs_t* outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700227 const nsecs_t* outGpuCompositionDoneTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700228 const nsecs_t* outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700229 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700230 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700231 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
232 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700233 !e->hasFirstRefreshStartInfo();
Brian Andersonb04c6f02016-10-21 12:57:46 -0700234 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700235 !e->hasGpuCompositionDoneInfo();
236 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
237 !e->hasDisplayPresentInfo();
238
Brian Anderson6b376712017-04-04 10:51:39 -0700239 // LastRefreshStart, DequeueReady, and Release are never available for the
240 // last frame.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700241 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
242 !e->hasLastRefreshStartInfo() &&
243 (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700244 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
245 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700246 bool checkForRelease = (outReleaseTime != nullptr) &&
247 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
248
249 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700250 return checkForLatch || checkForFirstRefreshStart ||
Brian Andersonb04c6f02016-10-21 12:57:46 -0700251 checkForLastRefreshStart || checkForGpuCompositionDone ||
Brian Anderson4e606e32017-03-16 15:34:57 -0700252 checkForDisplayPresent || checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700253}
254
Brian Anderson3d4039d2016-09-23 16:31:30 -0700255static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
256 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700257 // We always get valid timestamps for these eventually.
258 *dst = (src == FrameEvents::TIMESTAMP_PENDING) ?
259 NATIVE_WINDOW_TIMESTAMP_PENDING : src;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700260 }
261}
262
Brian Andersondc96fdf2017-03-20 16:54:25 -0700263static void getFrameTimestampFence(nsecs_t *dst,
264 const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700265 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700266 if (!fenceShouldBeKnown) {
267 *dst = NATIVE_WINDOW_TIMESTAMP_PENDING;
268 return;
269 }
270
Brian Anderson3d4039d2016-09-23 16:31:30 -0700271 nsecs_t signalTime = src->getSignalTime();
Brian Andersondc96fdf2017-03-20 16:54:25 -0700272 *dst = (signalTime == Fence::SIGNAL_TIME_PENDING) ?
273 NATIVE_WINDOW_TIMESTAMP_PENDING :
274 (signalTime == Fence::SIGNAL_TIME_INVALID) ?
275 NATIVE_WINDOW_TIMESTAMP_INVALID :
276 signalTime;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700277 }
278}
279
Brian Anderson069b3652016-07-22 10:32:47 -0700280status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700281 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700282 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700283 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700284 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
285 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800286 ATRACE_CALL();
287
Brian Anderson3890c392016-07-25 12:48:08 -0700288 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700289
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700290 if (!mEnableFrameTimestamps) {
291 return INVALID_OPERATION;
292 }
293
Brian Anderson6b376712017-04-04 10:51:39 -0700294 // Verify the requested timestamps are supported.
295 querySupportedTimestampsLocked();
296 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
297 return BAD_VALUE;
298 }
299
Brian Anderson3da8d272016-07-28 16:20:47 -0700300 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700301 if (events == nullptr) {
302 // If the entry isn't available in the producer, it's definitely not
303 // available in the consumer.
304 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700305 }
306
Brian Anderson50143b32016-09-30 14:01:24 -0700307 // Update our cache of events if the requested events are not available.
308 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700309 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700310 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700311 outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700312 FrameEventHistoryDelta delta;
313 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700314 mFrameEventHistory->applyDelta(delta);
315 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700316 }
317
Brian Anderson3890c392016-07-25 12:48:08 -0700318 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700319 // The entry was available before the update, but was overwritten
320 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700321 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800322 }
Brian Anderson069b3652016-07-22 10:32:47 -0700323
Brian Anderson3d4039d2016-09-23 16:31:30 -0700324 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700325 getFrameTimestamp(outLatchTime, events->latchTime);
326 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
327 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
328 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700329
Brian Andersondc96fdf2017-03-20 16:54:25 -0700330 getFrameTimestampFence(outAcquireTime, events->acquireFence,
331 events->hasAcquireInfo());
332 getFrameTimestampFence(outGpuCompositionDoneTime,
333 events->gpuCompositionDoneFence,
334 events->hasGpuCompositionDoneInfo());
335 getFrameTimestampFence(outDisplayPresentTime, events->displayPresentFence,
336 events->hasDisplayPresentInfo());
337 getFrameTimestampFence(outReleaseTime, events->releaseFence,
338 events->hasReleaseInfo());
Brian Anderson069b3652016-07-22 10:32:47 -0700339
340 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800341}
342
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700343status_t Surface::getWideColorSupport(bool* supported) {
344 ATRACE_CALL();
345
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800346 const sp<IBinder> display = composerService()->getInternalDisplayToken();
347 if (display == nullptr) {
348 return NAME_NOT_FOUND;
349 }
350
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700351 *supported = false;
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800352 status_t error = composerService()->isWideColorDisplay(display, supported);
353 return error;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700354}
355
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700356status_t Surface::getHdrSupport(bool* supported) {
357 ATRACE_CALL();
358
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800359 const sp<IBinder> display = composerService()->getInternalDisplayToken();
360 if (display == nullptr) {
361 return NAME_NOT_FOUND;
362 }
363
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700364 HdrCapabilities hdrCapabilities;
365 status_t err =
366 composerService()->getHdrCapabilities(display, &hdrCapabilities);
367
368 if (err)
369 return err;
370
371 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
372
373 return NO_ERROR;
374}
375
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800376int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
377 Surface* c = getSelf(window);
378 return c->setSwapInterval(interval);
379}
380
381int Surface::hook_dequeueBuffer(ANativeWindow* window,
382 ANativeWindowBuffer** buffer, int* fenceFd) {
383 Surface* c = getSelf(window);
Alec Mouri09d122a2019-11-25 10:00:53 -0800384 {
385 std::shared_lock<std::shared_mutex> lock(c->mInterceptorMutex);
386 if (c->mDequeueInterceptor != nullptr) {
387 auto interceptor = c->mDequeueInterceptor;
388 auto data = c->mDequeueInterceptorData;
389 return interceptor(window, Surface::dequeueBufferInternal, data, buffer, fenceFd);
390 }
391 }
392 return c->dequeueBuffer(buffer, fenceFd);
393}
394
395int Surface::dequeueBufferInternal(ANativeWindow* window, ANativeWindowBuffer** buffer,
396 int* fenceFd) {
397 Surface* c = getSelf(window);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800398 return c->dequeueBuffer(buffer, fenceFd);
399}
400
401int Surface::hook_cancelBuffer(ANativeWindow* window,
402 ANativeWindowBuffer* buffer, int fenceFd) {
403 Surface* c = getSelf(window);
Alec Mouri09d122a2019-11-25 10:00:53 -0800404 {
405 std::shared_lock<std::shared_mutex> lock(c->mInterceptorMutex);
406 if (c->mCancelInterceptor != nullptr) {
407 auto interceptor = c->mCancelInterceptor;
408 auto data = c->mCancelInterceptorData;
409 return interceptor(window, Surface::cancelBufferInternal, data, buffer, fenceFd);
410 }
411 }
412 return c->cancelBuffer(buffer, fenceFd);
413}
414
415int Surface::cancelBufferInternal(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
416 Surface* c = getSelf(window);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800417 return c->cancelBuffer(buffer, fenceFd);
418}
419
420int Surface::hook_queueBuffer(ANativeWindow* window,
421 ANativeWindowBuffer* buffer, int fenceFd) {
422 Surface* c = getSelf(window);
Alec Mouri09d122a2019-11-25 10:00:53 -0800423 {
424 std::shared_lock<std::shared_mutex> lock(c->mInterceptorMutex);
425 if (c->mQueueInterceptor != nullptr) {
426 auto interceptor = c->mQueueInterceptor;
427 auto data = c->mQueueInterceptorData;
428 return interceptor(window, Surface::queueBufferInternal, data, buffer, fenceFd);
429 }
430 }
431 return c->queueBuffer(buffer, fenceFd);
432}
433
434int Surface::queueBufferInternal(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
435 Surface* c = getSelf(window);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800436 return c->queueBuffer(buffer, fenceFd);
437}
438
439int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
440 ANativeWindowBuffer** buffer) {
441 Surface* c = getSelf(window);
442 ANativeWindowBuffer* buf;
443 int fenceFd = -1;
444 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600445 if (result != OK) {
446 return result;
447 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800448 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700449 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800450 if (waitResult != OK) {
451 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
452 waitResult);
453 c->cancelBuffer(buf, -1);
454 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700455 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800456 *buffer = buf;
457 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700458}
459
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800460int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
461 ANativeWindowBuffer* buffer) {
462 Surface* c = getSelf(window);
463 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700464}
465
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800466int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
467 ANativeWindowBuffer* buffer) {
468 Surface* c = getSelf(window);
469 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700470}
471
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800472int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
473 ANativeWindowBuffer* buffer) {
474 Surface* c = getSelf(window);
475 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700476}
Mathias Agopian62185b72009-04-16 16:19:50 -0700477
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800478int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
479 va_list args;
480 va_start(args, operation);
481 Surface* c = getSelf(window);
Alec Mouri09d122a2019-11-25 10:00:53 -0800482 int result;
483 // Don't acquire shared ownership of the interceptor mutex if we're going to
484 // do interceptor registration, as otherwise we'll deadlock on acquiring
485 // exclusive ownership.
486 if (!isInterceptorRegistrationOp(operation)) {
487 std::shared_lock<std::shared_mutex> lock(c->mInterceptorMutex);
488 if (c->mPerformInterceptor != nullptr) {
489 result = c->mPerformInterceptor(window, Surface::performInternal,
490 c->mPerformInterceptorData, operation, args);
491 va_end(args);
492 return result;
493 }
494 }
495 result = c->perform(operation, args);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700496 va_end(args);
497 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800498}
499
Alec Mouri09d122a2019-11-25 10:00:53 -0800500int Surface::performInternal(ANativeWindow* window, int operation, va_list args) {
501 Surface* c = getSelf(window);
502 return c->perform(operation, args);
503}
504
505int Surface::hook_query(const ANativeWindow* window, int what, int* value) {
506 const Surface* c = getSelf(window);
John Reck401cda62020-05-07 16:04:41 -0700507 {
508 std::shared_lock<std::shared_mutex> lock(c->mInterceptorMutex);
509 if (c->mQueryInterceptor != nullptr) {
510 auto interceptor = c->mQueryInterceptor;
511 auto data = c->mQueryInterceptorData;
512 return interceptor(window, Surface::queryInternal, data, what, value);
513 }
514 }
515 return c->query(what, value);
516}
517
518int Surface::queryInternal(const ANativeWindow* window, int what, int* value) {
519 const Surface* c = getSelf(window);
Alec Mouri09d122a2019-11-25 10:00:53 -0800520 return c->query(what, value);
521}
522
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800523int Surface::setSwapInterval(int interval) {
524 ATRACE_CALL();
525 // EGL specification states:
526 // interval is silently clamped to minimum and maximum implementation
527 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800528
529 if (interval < minSwapInterval)
530 interval = minSwapInterval;
531
532 if (interval > maxSwapInterval)
533 interval = maxSwapInterval;
534
Jorim Jaggi0a3e7842018-07-17 13:48:33 +0200535 const bool wasSwapIntervalZero = mSwapIntervalZero;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700536 mSwapIntervalZero = (interval == 0);
Jorim Jaggi0a3e7842018-07-17 13:48:33 +0200537
538 if (mSwapIntervalZero != wasSwapIntervalZero) {
539 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
540 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800541
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700542 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800543}
544
Yiwei Zhang9e919eb2019-03-07 23:21:40 -0800545class FenceMonitor {
546public:
547 explicit FenceMonitor(const char* name) : mName(name), mFencesQueued(0), mFencesSignaled(0) {
548 std::thread thread(&FenceMonitor::loop, this);
549 pthread_setname_np(thread.native_handle(), mName);
550 thread.detach();
551 }
552
553 void queueFence(const sp<Fence>& fence) {
554 char message[64];
555
556 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang28178132019-03-08 17:14:53 -0800557 if (fence->getSignalTime() != Fence::SIGNAL_TIME_PENDING) {
558 snprintf(message, sizeof(message), "%s fence %u has signaled", mName, mFencesQueued);
559 ATRACE_NAME(message);
560 // Need an increment on both to make the trace number correct.
561 mFencesQueued++;
562 mFencesSignaled++;
563 return;
564 }
565 snprintf(message, sizeof(message), "Trace %s fence %u", mName, mFencesQueued);
Yiwei Zhang9e919eb2019-03-07 23:21:40 -0800566 ATRACE_NAME(message);
567
568 mQueue.push_back(fence);
569 mCondition.notify_one();
570 mFencesQueued++;
571 ATRACE_INT(mName, int32_t(mQueue.size()));
572 }
573
574private:
575#pragma clang diagnostic push
576#pragma clang diagnostic ignored "-Wmissing-noreturn"
577 void loop() {
578 while (true) {
579 threadLoop();
580 }
581 }
582#pragma clang diagnostic pop
583
584 void threadLoop() {
585 sp<Fence> fence;
586 uint32_t fenceNum;
587 {
588 std::unique_lock<std::mutex> lock(mMutex);
589 while (mQueue.empty()) {
590 mCondition.wait(lock);
591 }
592 fence = mQueue[0];
593 fenceNum = mFencesSignaled;
594 }
595 {
596 char message[64];
597 snprintf(message, sizeof(message), "waiting for %s %u", mName, fenceNum);
598 ATRACE_NAME(message);
599
600 status_t result = fence->waitForever(message);
601 if (result != OK) {
602 ALOGE("Error waiting for fence: %d", result);
603 }
604 }
605 {
606 std::lock_guard<std::mutex> lock(mMutex);
607 mQueue.pop_front();
608 mFencesSignaled++;
609 ATRACE_INT(mName, int32_t(mQueue.size()));
610 }
611 }
612
613 const char* mName;
614 uint32_t mFencesQueued;
615 uint32_t mFencesSignaled;
616 std::deque<sp<Fence>> mQueue;
617 std::condition_variable mCondition;
618 std::mutex mMutex;
619};
620
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -0800621void Surface::getDequeueBufferInputLocked(
622 IGraphicBufferProducer::DequeueBufferInput* dequeueInput) {
623 LOG_ALWAYS_FATAL_IF(dequeueInput == nullptr, "input is null");
624
625 dequeueInput->width = mReqWidth ? mReqWidth : mUserWidth;
626 dequeueInput->height = mReqHeight ? mReqHeight : mUserHeight;
627
628 dequeueInput->format = mReqFormat;
629 dequeueInput->usage = mReqUsage;
630
631 dequeueInput->getTimestamps = mEnableFrameTimestamps;
632}
633
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700634int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800635 ATRACE_CALL();
636 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800637
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -0800638 IGraphicBufferProducer::DequeueBufferInput dqInput;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800639 {
640 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700641 if (mReportRemovedBuffers) {
642 mRemovedBuffers.clear();
643 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800644
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -0800645 getDequeueBufferInputLocked(&dqInput);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700646
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700647 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800648 BufferItem::INVALID_BUFFER_SLOT) {
649 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
Yi Kong48a619f2018-06-05 16:34:59 -0700650 if (gbuf != nullptr) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800651 *buffer = gbuf.get();
652 *fenceFd = -1;
653 return OK;
654 }
655 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800656 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
657
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800658 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800659 sp<Fence> fence;
Dan Stoza932f0082017-05-31 13:50:16 -0700660 nsecs_t startTime = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700661
662 FrameEventHistoryDelta frameTimestamps;
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -0800663 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, dqInput.width,
664 dqInput.height, dqInput.format,
665 dqInput.usage, &mBufferAge,
666 dqInput.getTimestamps ?
667 &frameTimestamps : nullptr);
Dan Stoza932f0082017-05-31 13:50:16 -0700668 mLastDequeueDuration = systemTime() - startTime;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800669
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800670 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700671 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700672 "(%d, %d, %d, %#" PRIx64 ") failed: %d",
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -0800673 dqInput.width, dqInput.height, dqInput.format, dqInput.usage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800674 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700675 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800676
Dan Stoza90ce2a92017-05-01 16:31:53 -0700677 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
678 ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf);
679 android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging
680 return FAILED_TRANSACTION;
681 }
682
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800683 Mutex::Autolock lock(mMutex);
684
Dan Stoza932f0082017-05-31 13:50:16 -0700685 // Write this while holding the mutex
686 mLastDequeueStartTime = startTime;
687
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800688 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700689
690 // this should never happen
Yi Kong48a619f2018-06-05 16:34:59 -0700691 ALOGE_IF(fence == nullptr, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700692
Yiwei Zhang9e919eb2019-03-07 23:21:40 -0800693 if (CC_UNLIKELY(atrace_is_tag_enabled(ATRACE_TAG_GRAPHICS))) {
694 static FenceMonitor hwcReleaseThread("HWC release");
695 hwcReleaseThread.queueFence(fence);
696 }
697
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800698 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
699 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700700 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700701
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -0800702 if (dqInput.getTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700703 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700704 }
705
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700706 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
707 if (mReportRemovedBuffers && (gbuf != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700708 mRemovedBuffers.push_back(gbuf);
709 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800710 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
711 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700712 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700713 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800714 return result;
715 }
716 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700717
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800718 if (fence->isValid()) {
719 *fenceFd = fence->dup();
720 if (*fenceFd == -1) {
721 ALOGE("dequeueBuffer: error duping fence: %d", errno);
722 // dup() should never fail; something is badly wrong. Soldier on
723 // and hope for the best; the worst that should happen is some
724 // visible corruption that lasts until the next frame.
725 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700726 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800727 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700728 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800729
730 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800731
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700732 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800733 mSharedBufferSlot = buf;
734 mSharedBufferHasBeenQueued = false;
735 } else if (mSharedBufferSlot == buf) {
736 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
737 mSharedBufferHasBeenQueued = false;
738 }
739
Yin-Chia Yehda2e6092020-07-29 15:00:07 -0700740 mDequeuedSlots.insert(buf);
741
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800742 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700743}
744
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -0800745int Surface::dequeueBuffers(std::vector<BatchBuffer>* buffers) {
746 using DequeueBufferInput = IGraphicBufferProducer::DequeueBufferInput;
747 using DequeueBufferOutput = IGraphicBufferProducer::DequeueBufferOutput;
748 using CancelBufferInput = IGraphicBufferProducer::CancelBufferInput;
749 using RequestBufferOutput = IGraphicBufferProducer::RequestBufferOutput;
750
751 ATRACE_CALL();
752 ALOGV("Surface::dequeueBuffers");
753
754 if (buffers->size() == 0) {
755 ALOGE("%s: must dequeue at least 1 buffer!", __FUNCTION__);
756 return BAD_VALUE;
757 }
758
759 if (mSharedBufferMode) {
760 ALOGE("%s: batch operation is not supported in shared buffer mode!",
761 __FUNCTION__);
762 return INVALID_OPERATION;
763 }
764
765 size_t numBufferRequested = buffers->size();
766 DequeueBufferInput input;
767
768 {
769 Mutex::Autolock lock(mMutex);
770 if (mReportRemovedBuffers) {
771 mRemovedBuffers.clear();
772 }
773
774 getDequeueBufferInputLocked(&input);
775 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffers
776
777 std::vector<DequeueBufferInput> dequeueInput(numBufferRequested, input);
778 std::vector<DequeueBufferOutput> dequeueOutput;
779
780 nsecs_t startTime = systemTime();
781
782 status_t result = mGraphicBufferProducer->dequeueBuffers(dequeueInput, &dequeueOutput);
783
784 mLastDequeueDuration = systemTime() - startTime;
785
786 if (result < 0) {
787 ALOGV("%s: IGraphicBufferProducer::dequeueBuffers"
788 "(%d, %d, %d, %#" PRIx64 ") failed: %d",
789 __FUNCTION__, input.width, input.height, input.format, input.usage, result);
790 return result;
791 }
792
793 std::vector<CancelBufferInput> cancelBufferInputs(numBufferRequested);
794 std::vector<status_t> cancelBufferOutputs;
795 for (size_t i = 0; i < numBufferRequested; i++) {
796 cancelBufferInputs[i].slot = dequeueOutput[i].slot;
797 cancelBufferInputs[i].fence = dequeueOutput[i].fence;
798 }
799
800 for (const auto& output : dequeueOutput) {
801 if (output.result < 0) {
802 mGraphicBufferProducer->cancelBuffers(cancelBufferInputs, &cancelBufferOutputs);
803 ALOGV("%s: IGraphicBufferProducer::dequeueBuffers"
804 "(%d, %d, %d, %#" PRIx64 ") failed: %d",
805 __FUNCTION__, input.width, input.height, input.format, input.usage,
806 output.result);
807 return output.result;
808 }
809
810 if (output.slot < 0 || output.slot >= NUM_BUFFER_SLOTS) {
811 mGraphicBufferProducer->cancelBuffers(cancelBufferInputs, &cancelBufferOutputs);
812 ALOGE("%s: IGraphicBufferProducer returned invalid slot number %d",
813 __FUNCTION__, output.slot);
814 android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging
815 return FAILED_TRANSACTION;
816 }
817
818 if (input.getTimestamps && !output.timestamps.has_value()) {
819 mGraphicBufferProducer->cancelBuffers(cancelBufferInputs, &cancelBufferOutputs);
820 ALOGE("%s: no frame timestamp returns!", __FUNCTION__);
821 return FAILED_TRANSACTION;
822 }
823
824 // this should never happen
825 ALOGE_IF(output.fence == nullptr,
826 "%s: received null Fence! slot=%d", __FUNCTION__, output.slot);
827 }
828
829 Mutex::Autolock lock(mMutex);
830
831 // Write this while holding the mutex
832 mLastDequeueStartTime = startTime;
833
834 std::vector<int32_t> requestBufferSlots;
835 requestBufferSlots.reserve(numBufferRequested);
836 // handle release all buffers and request buffers
837 for (const auto& output : dequeueOutput) {
838 if (output.result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
839 ALOGV("%s: RELEASE_ALL_BUFFERS during batch operation", __FUNCTION__);
840 freeAllBuffers();
841 break;
842 }
843 }
844
845 for (const auto& output : dequeueOutput) {
846 // Collect slots that needs requesting buffer
847 sp<GraphicBuffer>& gbuf(mSlots[output.slot].buffer);
848 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
849 if (mReportRemovedBuffers && (gbuf != nullptr)) {
850 mRemovedBuffers.push_back(gbuf);
851 }
852 requestBufferSlots.push_back(output.slot);
853 }
854 }
855
856 // Batch request Buffer
857 std::vector<RequestBufferOutput> reqBufferOutput;
858 if (requestBufferSlots.size() > 0) {
859 result = mGraphicBufferProducer->requestBuffers(requestBufferSlots, &reqBufferOutput);
860 if (result != NO_ERROR) {
861 ALOGE("%s: IGraphicBufferProducer::requestBuffers failed: %d",
862 __FUNCTION__, result);
863 mGraphicBufferProducer->cancelBuffers(cancelBufferInputs, &cancelBufferOutputs);
864 return result;
865 }
866
867 // Check if we have any single failure
868 for (size_t i = 0; i < requestBufferSlots.size(); i++) {
869 if (reqBufferOutput[i].result != OK) {
870 ALOGE("%s: IGraphicBufferProducer::requestBuffers failed at %zu-th buffer, slot %d",
871 __FUNCTION__, i, requestBufferSlots[i]);
872 mGraphicBufferProducer->cancelBuffers(cancelBufferInputs, &cancelBufferOutputs);
873 return reqBufferOutput[i].result;
874 }
875 }
876
877 // Fill request buffer results to mSlots
878 for (size_t i = 0; i < requestBufferSlots.size(); i++) {
879 mSlots[requestBufferSlots[i]].buffer = reqBufferOutput[i].buffer;
880 }
881 }
882
883 for (size_t batchIdx = 0; batchIdx < numBufferRequested; batchIdx++) {
884 const auto& output = dequeueOutput[batchIdx];
885 int slot = output.slot;
886 sp<GraphicBuffer>& gbuf(mSlots[slot].buffer);
887
888 if (CC_UNLIKELY(atrace_is_tag_enabled(ATRACE_TAG_GRAPHICS))) {
889 static FenceMonitor hwcReleaseThread("HWC release");
890 hwcReleaseThread.queueFence(output.fence);
891 }
892
893 if (input.getTimestamps) {
894 mFrameEventHistory->applyDelta(output.timestamps.value());
895 }
896
897 if (output.fence->isValid()) {
898 buffers->at(batchIdx).fenceFd = output.fence->dup();
899 if (buffers->at(batchIdx).fenceFd == -1) {
900 ALOGE("%s: error duping fence: %d", __FUNCTION__, errno);
901 // dup() should never fail; something is badly wrong. Soldier on
902 // and hope for the best; the worst that should happen is some
903 // visible corruption that lasts until the next frame.
904 }
905 } else {
906 buffers->at(batchIdx).fenceFd = -1;
907 }
908
909 buffers->at(batchIdx).buffer = gbuf.get();
910 mDequeuedSlots.insert(slot);
911 }
912 return OK;
913}
914
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800915int Surface::cancelBuffer(android_native_buffer_t* buffer,
916 int fenceFd) {
917 ATRACE_CALL();
918 ALOGV("Surface::cancelBuffer");
919 Mutex::Autolock lock(mMutex);
920 int i = getSlotFromBufferLocked(buffer);
921 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900922 if (fenceFd >= 0) {
923 close(fenceFd);
924 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800925 return i;
926 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800927 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
928 if (fenceFd >= 0) {
929 close(fenceFd);
930 }
931 return OK;
932 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800933 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
934 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800935
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700936 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800937 mSharedBufferHasBeenQueued = true;
938 }
939
Yin-Chia Yehda2e6092020-07-29 15:00:07 -0700940 mDequeuedSlots.erase(i);
941
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800942 return OK;
943}
944
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -0800945int Surface::cancelBuffers(const std::vector<BatchBuffer>& buffers) {
946 using CancelBufferInput = IGraphicBufferProducer::CancelBufferInput;
947 ATRACE_CALL();
948 ALOGV("Surface::cancelBuffers");
949
950 if (mSharedBufferMode) {
951 ALOGE("%s: batch operation is not supported in shared buffer mode!",
952 __FUNCTION__);
953 return INVALID_OPERATION;
954 }
955
956 size_t numBuffers = buffers.size();
957 std::vector<CancelBufferInput> cancelBufferInputs(numBuffers);
958 std::vector<status_t> cancelBufferOutputs;
959 size_t numBuffersCancelled = 0;
960 int badSlotResult = 0;
961 for (size_t i = 0; i < numBuffers; i++) {
962 int slot = getSlotFromBufferLocked(buffers[i].buffer);
963 int fenceFd = buffers[i].fenceFd;
964 if (slot < 0) {
965 if (fenceFd >= 0) {
966 close(fenceFd);
967 }
968 ALOGE("%s: cannot find slot number for cancelled buffer", __FUNCTION__);
969 badSlotResult = slot;
970 } else {
971 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
972 cancelBufferInputs[numBuffersCancelled].slot = slot;
973 cancelBufferInputs[numBuffersCancelled++].fence = fence;
974 }
975 }
976 cancelBufferInputs.resize(numBuffersCancelled);
977 mGraphicBufferProducer->cancelBuffers(cancelBufferInputs, &cancelBufferOutputs);
978
979
980 for (size_t i = 0; i < numBuffersCancelled; i++) {
981 mDequeuedSlots.erase(cancelBufferInputs[i].slot);
982 }
983
984 if (badSlotResult != 0) {
985 return badSlotResult;
986 }
987 return OK;
988}
989
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800990int Surface::getSlotFromBufferLocked(
991 android_native_buffer_t* buffer) const {
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -0800992 if (buffer == nullptr) {
993 ALOGE("%s: input buffer is null!", __FUNCTION__);
994 return BAD_VALUE;
995 }
996
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800997 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Yi Kong48a619f2018-06-05 16:34:59 -0700998 if (mSlots[i].buffer != nullptr &&
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800999 mSlots[i].buffer->handle == buffer->handle) {
1000 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -07001001 }
1002 }
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -08001003 ALOGE("%s: unknown buffer: %p", __FUNCTION__, buffer->handle);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001004 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -07001005}
1006
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001007int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001008 ALOGV("Surface::lockBuffer");
1009 Mutex::Autolock lock(mMutex);
1010 return OK;
1011}
Mathias Agopian631f3582010-05-25 17:51:34 -07001012
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -08001013void Surface::getQueueBufferInputLocked(android_native_buffer_t* buffer, int fenceFd,
1014 nsecs_t timestamp, IGraphicBufferProducer::QueueBufferInput* out) {
Andy McFadden3c256212013-08-16 14:55:39 -07001015 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001016
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -08001017 if (timestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -07001018 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -07001019 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -07001020 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -07001021 timestamp / 1000000.0);
Mathias Agopian631f3582010-05-25 17:51:34 -07001022 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001024 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -07001025 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001026 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001028 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
Andy McFadden3c256212013-08-16 14:55:39 -07001029 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001030 static_cast<android_dataspace>(mDataSpace), crop, mScalingMode,
1031 mTransform ^ mStickyTransform, fence, mStickyTransform,
1032 mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -07001033
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001034 // we should send HDR metadata as needed if this becomes a bottleneck
1035 input.setHdrMetadata(mHdrMetadata);
1036
Dan Stozac62acbd2015-04-21 16:42:49 -07001037 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -07001038 input.setSurfaceDamage(Region::INVALID_REGION);
1039 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -07001040 // Here we do two things:
1041 // 1) The surface damage was specified using the OpenGL ES convention of
1042 // the origin being in the bottom-left corner. Here we flip to the
1043 // convention that the rest of the system uses (top-left corner) by
1044 // subtracting all top/bottom coordinates from the buffer height.
1045 // 2) If the buffer is coming in rotated (for example, because the EGL
1046 // implementation is reacting to the transform hint coming back from
1047 // SurfaceFlinger), the surface damage needs to be rotated the
1048 // opposite direction, since it was generated assuming an unrotated
1049 // buffer (the app doesn't know that the EGL implementation is
1050 // reacting to the transform hint behind its back). The
1051 // transformations in the switch statement below apply those
1052 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
1053
1054 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -07001055 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -07001056 bool rotated90 = (mTransform ^ mStickyTransform) &
1057 NATIVE_WINDOW_TRANSFORM_ROT_90;
1058 if (rotated90) {
1059 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -07001060 }
Dan Stozadb4850c2015-06-25 16:10:18 -07001061
Dan Stoza5065a552015-03-17 16:23:42 -07001062 Region flippedRegion;
1063 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -07001064 int left = rect.left;
1065 int right = rect.right;
1066 int top = height - rect.bottom; // Flip from OpenGL convention
1067 int bottom = height - rect.top; // Flip from OpenGL convention
1068 switch (mTransform ^ mStickyTransform) {
1069 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
1070 // Rotate 270 degrees
1071 Rect flippedRect{top, width - right, bottom, width - left};
1072 flippedRegion.orSelf(flippedRect);
1073 break;
1074 }
1075 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
1076 // Rotate 180 degrees
1077 Rect flippedRect{width - right, height - bottom,
1078 width - left, height - top};
1079 flippedRegion.orSelf(flippedRect);
1080 break;
1081 }
1082 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
1083 // Rotate 90 degrees
1084 Rect flippedRect{height - bottom, left,
1085 height - top, right};
1086 flippedRegion.orSelf(flippedRect);
1087 break;
1088 }
1089 default: {
1090 Rect flippedRect{left, top, right, bottom};
1091 flippedRegion.orSelf(flippedRect);
1092 break;
1093 }
1094 }
Dan Stoza5065a552015-03-17 16:23:42 -07001095 }
1096
1097 input.setSurfaceDamage(flippedRegion);
1098 }
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -08001099 *out = input;
1100}
Dan Stoza5065a552015-03-17 16:23:42 -07001101
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -08001102void Surface::onBufferQueuedLocked(int slot, sp<Fence> fence,
1103 const IGraphicBufferProducer::QueueBufferOutput& output) {
1104 mDequeuedSlots.erase(slot);
Yin-Chia Yehda2e6092020-07-29 15:00:07 -07001105
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001106 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -07001107 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001108 // Update timestamps with the local acquire fence.
1109 // The consumer doesn't send it back to prevent us from having two
1110 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -07001111 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Yiwei Zhangf0c12502019-03-13 16:09:05 -07001112 std::make_shared<FenceTime>(fence));
Brian Anderson3d4039d2016-09-23 16:31:30 -07001113
1114 // Cache timestamps of signaled fences so we can close their file
1115 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -07001116 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001117 }
1118
Brian Anderson50143b32016-09-30 14:01:24 -07001119 mLastFrameNumber = mNextFrameNumber;
1120
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001121 mDefaultWidth = output.width;
1122 mDefaultHeight = output.height;
1123 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -07001124
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001125 // Ignore transform hint if sticky transform is set or transform to display inverse flag is
1126 // set.
1127 if (mStickyTransform == 0 && !transformToDisplayInverse()) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001128 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001129 }
1130
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001131 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -07001132
Dan Stozac62acbd2015-04-21 16:42:49 -07001133 if (!mConnectedToCpu) {
1134 // Clear surface damage back to full-buffer
1135 mDirtyRegion = Region::INVALID_REGION;
1136 }
Dan Stoza5065a552015-03-17 16:23:42 -07001137
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -08001138 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == slot) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001139 mSharedBufferHasBeenQueued = true;
1140 }
1141
Robert Carr9f31e292016-04-11 11:15:32 -07001142 mQueueBufferCondition.broadcast();
1143
Yiwei Zhang9e919eb2019-03-07 23:21:40 -08001144 if (CC_UNLIKELY(atrace_is_tag_enabled(ATRACE_TAG_GRAPHICS))) {
1145 static FenceMonitor gpuCompletionThread("GPU completion");
1146 gpuCompletionThread.queueFence(fence);
1147 }
Yin-Chia Yeh78c0ad72020-02-07 14:05:35 -08001148}
1149
1150int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
1151 ATRACE_CALL();
1152 ALOGV("Surface::queueBuffer");
1153 Mutex::Autolock lock(mMutex);
1154
1155 int i = getSlotFromBufferLocked(buffer);
1156 if (i < 0) {
1157 if (fenceFd >= 0) {
1158 close(fenceFd);
1159 }
1160 return i;
1161 }
1162 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
1163 if (fenceFd >= 0) {
1164 close(fenceFd);
1165 }
1166 return OK;
1167 }
1168
1169 IGraphicBufferProducer::QueueBufferOutput output;
1170 IGraphicBufferProducer::QueueBufferInput input;
1171 getQueueBufferInputLocked(buffer, fenceFd, mTimestamp, &input);
1172 sp<Fence> fence = input.fence;
1173
1174 nsecs_t now = systemTime();
1175 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
1176 mLastQueueDuration = systemTime() - now;
1177 if (err != OK) {
1178 ALOGE("queueBuffer: error queuing buffer, %d", err);
1179 }
1180
1181 onBufferQueuedLocked(i, fence, output);
1182 return err;
1183}
1184
1185int Surface::queueBuffers(const std::vector<BatchQueuedBuffer>& buffers) {
1186 ATRACE_CALL();
1187 ALOGV("Surface::queueBuffers");
1188 Mutex::Autolock lock(mMutex);
1189
1190 if (mSharedBufferMode) {
1191 ALOGE("%s: batched operation is not supported in shared buffer mode", __FUNCTION__);
1192 return INVALID_OPERATION;
1193 }
1194
1195 size_t numBuffers = buffers.size();
1196 std::vector<IGraphicBufferProducer::QueueBufferInput> queueBufferInputs(numBuffers);
1197 std::vector<IGraphicBufferProducer::QueueBufferOutput> queueBufferOutputs;
1198 std::vector<int> bufferSlots(numBuffers, -1);
1199 std::vector<sp<Fence>> bufferFences(numBuffers);
1200
1201 for (size_t batchIdx = 0; batchIdx < numBuffers; batchIdx++) {
1202 int i = getSlotFromBufferLocked(buffers[batchIdx].buffer);
1203 if (i < 0) {
1204 if (buffers[batchIdx].fenceFd >= 0) {
1205 close(buffers[batchIdx].fenceFd);
1206 }
1207 return i;
1208 }
1209 bufferSlots[batchIdx] = i;
1210
1211 IGraphicBufferProducer::QueueBufferInput input;
1212 getQueueBufferInputLocked(
1213 buffers[batchIdx].buffer, buffers[batchIdx].fenceFd, buffers[batchIdx].timestamp,
1214 &input);
1215 bufferFences[batchIdx] = input.fence;
1216 queueBufferInputs[batchIdx] = input;
1217 }
1218
1219 nsecs_t now = systemTime();
1220 status_t err = mGraphicBufferProducer->queueBuffers(queueBufferInputs, &queueBufferOutputs);
1221 mLastQueueDuration = systemTime() - now;
1222 if (err != OK) {
1223 ALOGE("%s: error queuing buffer, %d", __FUNCTION__, err);
1224 }
1225
1226
1227 for (size_t batchIdx = 0; batchIdx < numBuffers; batchIdx++) {
1228 onBufferQueuedLocked(bufferSlots[batchIdx], bufferFences[batchIdx],
1229 queueBufferOutputs[batchIdx]);
1230 }
Yiwei Zhang9e919eb2019-03-07 23:21:40 -08001231
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001232 return err;
1233}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001234
Brian Anderson6b376712017-04-04 10:51:39 -07001235void Surface::querySupportedTimestampsLocked() const {
1236 // mMutex must be locked when calling this method.
1237
1238 if (mQueriedSupportedTimestamps) {
1239 return;
1240 }
1241 mQueriedSupportedTimestamps = true;
1242
1243 std::vector<FrameEvent> supportedFrameTimestamps;
1244 status_t err = composerService()->getSupportedFrameTimestamps(
1245 &supportedFrameTimestamps);
1246
1247 if (err != NO_ERROR) {
1248 return;
1249 }
1250
1251 for (auto sft : supportedFrameTimestamps) {
1252 if (sft == FrameEvent::DISPLAY_PRESENT) {
1253 mFrameTimestampsSupportsPresent = true;
1254 }
1255 }
1256}
1257
Mathias Agopiana67932f2011-04-20 14:20:59 -07001258int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001259 ATRACE_CALL();
1260 ALOGV("Surface::query");
1261 { // scope for the lock
1262 Mutex::Autolock lock(mMutex);
1263 switch (what) {
1264 case NATIVE_WINDOW_FORMAT:
1265 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001266 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001267 return NO_ERROR;
1268 }
1269 break;
1270 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Vishnu Nair17dde612020-12-28 11:39:59 -08001271 status_t err = mGraphicBufferProducer->query(what, value);
1272 if (err == NO_ERROR) {
1273 return NO_ERROR;
1274 }
Brian Anderson3da8d272016-07-28 16:20:47 -07001275 if (composerService()->authenticateSurfaceTexture(
1276 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001277 *value = 1;
1278 } else {
1279 *value = 0;
1280 }
1281 return NO_ERROR;
1282 }
1283 case NATIVE_WINDOW_CONCRETE_TYPE:
1284 *value = NATIVE_WINDOW_SURFACE;
1285 return NO_ERROR;
1286 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001287 *value = static_cast<int>(
1288 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001289 return NO_ERROR;
1290 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001291 *value = static_cast<int>(
1292 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001293 return NO_ERROR;
1294 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001295 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001296 return NO_ERROR;
1297 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
1298 status_t err = NO_ERROR;
1299 if (!mConsumerRunningBehind) {
1300 *value = 0;
1301 } else {
1302 err = mGraphicBufferProducer->query(what, value);
1303 if (err == NO_ERROR) {
1304 mConsumerRunningBehind = *value;
1305 }
1306 }
1307 return err;
1308 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -06001309 case NATIVE_WINDOW_BUFFER_AGE: {
1310 if (mBufferAge > INT32_MAX) {
1311 *value = 0;
1312 } else {
1313 *value = static_cast<int32_t>(mBufferAge);
1314 }
1315 return NO_ERROR;
1316 }
Dan Stoza70ccba52016-07-01 14:00:40 -07001317 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
1318 int64_t durationUs = mLastDequeueDuration / 1000;
1319 *value = durationUs > std::numeric_limits<int>::max() ?
1320 std::numeric_limits<int>::max() :
1321 static_cast<int>(durationUs);
1322 return NO_ERROR;
1323 }
1324 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
1325 int64_t durationUs = mLastQueueDuration / 1000;
1326 *value = durationUs > std::numeric_limits<int>::max() ?
1327 std::numeric_limits<int>::max() :
1328 static_cast<int>(durationUs);
1329 return NO_ERROR;
1330 }
Brian Anderson6b376712017-04-04 10:51:39 -07001331 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
1332 querySupportedTimestampsLocked();
1333 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
1334 return NO_ERROR;
1335 }
Mathias Agopian10e9ab52017-03-08 15:02:55 -08001336 case NATIVE_WINDOW_IS_VALID: {
1337 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
1338 return NO_ERROR;
1339 }
Peiyong Lin654f87b2018-01-30 14:21:33 -08001340 case NATIVE_WINDOW_DATASPACE: {
1341 *value = static_cast<int>(mDataSpace);
1342 return NO_ERROR;
1343 }
silence_dogoode9d092a2019-06-19 16:14:53 -07001344 case NATIVE_WINDOW_MAX_BUFFER_COUNT: {
1345 *value = mMaxBufferCount;
1346 return NO_ERROR;
1347 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001348 }
Jamie Gennis391bbe22011-03-14 15:00:06 -07001349 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001350 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -08001351}
1352
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001353int Surface::perform(int operation, va_list args)
1354{
1355 int res = NO_ERROR;
1356 switch (operation) {
1357 case NATIVE_WINDOW_CONNECT:
1358 // deprecated. must return NO_ERROR.
1359 break;
1360 case NATIVE_WINDOW_DISCONNECT:
1361 // deprecated. must return NO_ERROR.
1362 break;
1363 case NATIVE_WINDOW_SET_USAGE:
1364 res = dispatchSetUsage(args);
1365 break;
1366 case NATIVE_WINDOW_SET_CROP:
1367 res = dispatchSetCrop(args);
1368 break;
1369 case NATIVE_WINDOW_SET_BUFFER_COUNT:
1370 res = dispatchSetBufferCount(args);
1371 break;
1372 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
1373 res = dispatchSetBuffersGeometry(args);
1374 break;
1375 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
1376 res = dispatchSetBuffersTransform(args);
1377 break;
Ruben Brunk1681d952014-06-27 15:51:55 -07001378 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
1379 res = dispatchSetBuffersStickyTransform(args);
1380 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001381 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
1382 res = dispatchSetBuffersTimestamp(args);
1383 break;
1384 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
1385 res = dispatchSetBuffersDimensions(args);
1386 break;
1387 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
1388 res = dispatchSetBuffersUserDimensions(args);
1389 break;
1390 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
1391 res = dispatchSetBuffersFormat(args);
1392 break;
1393 case NATIVE_WINDOW_LOCK:
1394 res = dispatchLock(args);
1395 break;
1396 case NATIVE_WINDOW_UNLOCK_AND_POST:
1397 res = dispatchUnlockAndPost(args);
1398 break;
1399 case NATIVE_WINDOW_SET_SCALING_MODE:
1400 res = dispatchSetScalingMode(args);
1401 break;
1402 case NATIVE_WINDOW_API_CONNECT:
1403 res = dispatchConnect(args);
1404 break;
1405 case NATIVE_WINDOW_API_DISCONNECT:
1406 res = dispatchDisconnect(args);
1407 break;
Rachad7cb0d392014-07-29 17:53:53 -07001408 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
1409 res = dispatchSetSidebandStream(args);
1410 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001411 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
1412 res = dispatchSetBuffersDataSpace(args);
1413 break;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001414 case NATIVE_WINDOW_SET_BUFFERS_SMPTE2086_METADATA:
1415 res = dispatchSetBuffersSmpte2086Metadata(args);
1416 break;
1417 case NATIVE_WINDOW_SET_BUFFERS_CTA861_3_METADATA:
1418 res = dispatchSetBuffersCta8613Metadata(args);
1419 break;
Valerie Haua82679d2018-11-21 09:31:43 -08001420 case NATIVE_WINDOW_SET_BUFFERS_HDR10_PLUS_METADATA:
1421 res = dispatchSetBuffersHdr10PlusMetadata(args);
1422 break;
Dan Stoza5065a552015-03-17 16:23:42 -07001423 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
1424 res = dispatchSetSurfaceDamage(args);
1425 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001426 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
1427 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001428 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001429 case NATIVE_WINDOW_SET_AUTO_REFRESH:
1430 res = dispatchSetAutoRefresh(args);
1431 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001432 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
1433 res = dispatchGetDisplayRefreshCycleDuration(args);
1434 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -08001435 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
1436 res = dispatchGetNextFrameId(args);
1437 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001438 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
1439 res = dispatchEnableFrameTimestamps(args);
1440 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001441 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
1442 res = dispatchGetCompositorTiming(args);
1443 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -08001444 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
1445 res = dispatchGetFrameTimestamps(args);
1446 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001447 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
1448 res = dispatchGetWideColorSupport(args);
1449 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001450 case NATIVE_WINDOW_GET_HDR_SUPPORT:
1451 res = dispatchGetHdrSupport(args);
1452 break;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001453 case NATIVE_WINDOW_SET_USAGE64:
1454 res = dispatchSetUsage64(args);
1455 break;
Chia-I Wue2786ea2017-08-07 10:36:08 -07001456 case NATIVE_WINDOW_GET_CONSUMER_USAGE64:
1457 res = dispatchGetConsumerUsage64(args);
1458 break;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001459 case NATIVE_WINDOW_SET_AUTO_PREROTATION:
1460 res = dispatchSetAutoPrerotation(args);
1461 break;
Alec Mouria1619662019-08-21 19:30:48 -07001462 case NATIVE_WINDOW_GET_LAST_DEQUEUE_START:
1463 res = dispatchGetLastDequeueStartTime(args);
1464 break;
Alec Mouri04fdb602019-08-23 19:41:43 -07001465 case NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT:
1466 res = dispatchSetDequeueTimeout(args);
1467 break;
Alec Mouri72670c52019-08-31 01:54:33 -07001468 case NATIVE_WINDOW_GET_LAST_DEQUEUE_DURATION:
1469 res = dispatchGetLastDequeueDuration(args);
1470 break;
1471 case NATIVE_WINDOW_GET_LAST_QUEUE_DURATION:
1472 res = dispatchGetLastQueueDuration(args);
1473 break;
Steven Thomas3172e202020-01-06 19:25:30 -08001474 case NATIVE_WINDOW_SET_FRAME_RATE:
1475 res = dispatchSetFrameRate(args);
1476 break;
Alec Mouri09d122a2019-11-25 10:00:53 -08001477 case NATIVE_WINDOW_SET_CANCEL_INTERCEPTOR:
1478 res = dispatchAddCancelInterceptor(args);
1479 break;
1480 case NATIVE_WINDOW_SET_DEQUEUE_INTERCEPTOR:
1481 res = dispatchAddDequeueInterceptor(args);
1482 break;
1483 case NATIVE_WINDOW_SET_PERFORM_INTERCEPTOR:
1484 res = dispatchAddPerformInterceptor(args);
1485 break;
1486 case NATIVE_WINDOW_SET_QUEUE_INTERCEPTOR:
1487 res = dispatchAddQueueInterceptor(args);
1488 break;
John Reck401cda62020-05-07 16:04:41 -07001489 case NATIVE_WINDOW_SET_QUERY_INTERCEPTOR:
1490 res = dispatchAddQueryInterceptor(args);
1491 break;
Alec Mouri74aef6d2019-12-09 17:10:24 -08001492 case NATIVE_WINDOW_ALLOCATE_BUFFERS:
1493 allocateBuffers();
1494 res = NO_ERROR;
1495 break;
Alec Mourief0b1532019-12-17 09:39:07 -08001496 case NATIVE_WINDOW_GET_LAST_QUEUED_BUFFER:
1497 res = dispatchGetLastQueuedBuffer(args);
1498 break;
Ady Abraham74e17562020-08-24 18:18:19 -07001499 case NATIVE_WINDOW_SET_FRAME_TIMELINE_VSYNC:
1500 res = dispatchSetFrameTimelineVsync(args);
1501 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001502 default:
1503 res = NAME_NOT_FOUND;
1504 break;
1505 }
1506 return res;
1507}
Mathias Agopiana138f892010-05-21 17:24:35 -07001508
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001509int Surface::dispatchConnect(va_list args) {
1510 int api = va_arg(args, int);
1511 return connect(api);
1512}
Mathias Agopian55fa2512010-03-11 15:06:54 -08001513
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001514int Surface::dispatchDisconnect(va_list args) {
1515 int api = va_arg(args, int);
1516 return disconnect(api);
1517}
1518
1519int Surface::dispatchSetUsage(va_list args) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001520 uint64_t usage = va_arg(args, uint32_t);
1521 return setUsage(usage);
1522}
1523
1524int Surface::dispatchSetUsage64(va_list args) {
1525 uint64_t usage = va_arg(args, uint64_t);
1526 return setUsage(usage);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001527}
1528
1529int Surface::dispatchSetCrop(va_list args) {
1530 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
1531 return setCrop(reinterpret_cast<Rect const*>(rect));
1532}
1533
1534int Surface::dispatchSetBufferCount(va_list args) {
1535 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001536 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001537}
1538
1539int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001540 uint32_t width = va_arg(args, uint32_t);
1541 uint32_t height = va_arg(args, uint32_t);
1542 PixelFormat format = va_arg(args, PixelFormat);
1543 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001544 if (err != 0) {
1545 return err;
1546 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001547 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001548}
1549
1550int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001551 uint32_t width = va_arg(args, uint32_t);
1552 uint32_t height = va_arg(args, uint32_t);
1553 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001554}
1555
1556int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001557 uint32_t width = va_arg(args, uint32_t);
1558 uint32_t height = va_arg(args, uint32_t);
1559 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001560}
1561
1562int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001563 PixelFormat format = va_arg(args, PixelFormat);
1564 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001565}
1566
1567int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001568 int mode = va_arg(args, int);
1569 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001570}
1571
1572int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001573 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001574 return setBuffersTransform(transform);
1575}
1576
Ruben Brunk1681d952014-06-27 15:51:55 -07001577int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001578 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -07001579 return setBuffersStickyTransform(transform);
1580}
1581
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001582int Surface::dispatchSetBuffersTimestamp(va_list args) {
1583 int64_t timestamp = va_arg(args, int64_t);
1584 return setBuffersTimestamp(timestamp);
1585}
1586
1587int Surface::dispatchLock(va_list args) {
1588 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1589 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1590 return lock(outBuffer, inOutDirtyBounds);
1591}
1592
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001593int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001594 return unlockAndPost();
1595}
1596
Rachad7cb0d392014-07-29 17:53:53 -07001597int Surface::dispatchSetSidebandStream(va_list args) {
1598 native_handle_t* sH = va_arg(args, native_handle_t*);
1599 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1600 setSidebandStream(sidebandHandle);
1601 return OK;
1602}
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001603
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001604int Surface::dispatchSetBuffersDataSpace(va_list args) {
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001605 Dataspace dataspace = static_cast<Dataspace>(va_arg(args, int));
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001606 return setBuffersDataSpace(dataspace);
1607}
1608
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001609int Surface::dispatchSetBuffersSmpte2086Metadata(va_list args) {
1610 const android_smpte2086_metadata* metadata =
1611 va_arg(args, const android_smpte2086_metadata*);
1612 return setBuffersSmpte2086Metadata(metadata);
1613}
1614
1615int Surface::dispatchSetBuffersCta8613Metadata(va_list args) {
1616 const android_cta861_3_metadata* metadata =
1617 va_arg(args, const android_cta861_3_metadata*);
1618 return setBuffersCta8613Metadata(metadata);
1619}
1620
Valerie Haua82679d2018-11-21 09:31:43 -08001621int Surface::dispatchSetBuffersHdr10PlusMetadata(va_list args) {
1622 const size_t size = va_arg(args, size_t);
1623 const uint8_t* metadata = va_arg(args, const uint8_t*);
1624 return setBuffersHdr10PlusMetadata(size, metadata);
1625}
1626
Dan Stoza5065a552015-03-17 16:23:42 -07001627int Surface::dispatchSetSurfaceDamage(va_list args) {
1628 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1629 size_t numRects = va_arg(args, size_t);
1630 setSurfaceDamage(rects, numRects);
1631 return NO_ERROR;
1632}
1633
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001634int Surface::dispatchSetSharedBufferMode(va_list args) {
1635 bool sharedBufferMode = va_arg(args, int);
1636 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001637}
1638
1639int Surface::dispatchSetAutoRefresh(va_list args) {
1640 bool autoRefresh = va_arg(args, int);
1641 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001642}
1643
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001644int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1645 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1646 return getDisplayRefreshCycleDuration(outRefreshDuration);
1647}
1648
Brian Anderson1049d1d2016-12-16 17:25:57 -08001649int Surface::dispatchGetNextFrameId(va_list args) {
1650 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1651 *nextFrameId = getNextFrameNumber();
1652 return NO_ERROR;
1653}
1654
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001655int Surface::dispatchEnableFrameTimestamps(va_list args) {
1656 bool enable = va_arg(args, int);
1657 enableFrameTimestamps(enable);
1658 return NO_ERROR;
1659}
1660
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001661int Surface::dispatchGetCompositorTiming(va_list args) {
1662 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1663 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1664 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1665 return getCompositorTiming(compositeDeadline, compositeInterval,
1666 compositeToPresentLatency);
1667}
1668
Pablo Ceballosce796e72016-02-04 19:10:51 -08001669int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001670 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001671 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001672 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001673 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1674 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1675 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001676 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001677 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001678 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001679 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001680 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001681 outRequestedPresentTime, outAcquireTime, outLatchTime,
1682 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001683 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001684 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001685}
1686
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001687int Surface::dispatchGetWideColorSupport(va_list args) {
1688 bool* outSupport = va_arg(args, bool*);
1689 return getWideColorSupport(outSupport);
1690}
1691
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001692int Surface::dispatchGetHdrSupport(va_list args) {
1693 bool* outSupport = va_arg(args, bool*);
1694 return getHdrSupport(outSupport);
1695}
1696
Chia-I Wue2786ea2017-08-07 10:36:08 -07001697int Surface::dispatchGetConsumerUsage64(va_list args) {
1698 uint64_t* usage = va_arg(args, uint64_t*);
1699 return getConsumerUsage(usage);
1700}
1701
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001702int Surface::dispatchSetAutoPrerotation(va_list args) {
1703 bool autoPrerotation = va_arg(args, int);
1704 return setAutoPrerotation(autoPrerotation);
1705}
1706
Alec Mouria1619662019-08-21 19:30:48 -07001707int Surface::dispatchGetLastDequeueStartTime(va_list args) {
1708 int64_t* lastDequeueStartTime = va_arg(args, int64_t*);
1709 *lastDequeueStartTime = mLastDequeueStartTime;
1710 return NO_ERROR;
1711}
1712
Alec Mouri04fdb602019-08-23 19:41:43 -07001713int Surface::dispatchSetDequeueTimeout(va_list args) {
1714 nsecs_t timeout = va_arg(args, int64_t);
1715 return setDequeueTimeout(timeout);
1716}
1717
Alec Mouri72670c52019-08-31 01:54:33 -07001718int Surface::dispatchGetLastDequeueDuration(va_list args) {
1719 int64_t* lastDequeueDuration = va_arg(args, int64_t*);
1720 *lastDequeueDuration = mLastDequeueDuration;
1721 return NO_ERROR;
1722}
1723
1724int Surface::dispatchGetLastQueueDuration(va_list args) {
1725 int64_t* lastQueueDuration = va_arg(args, int64_t*);
1726 *lastQueueDuration = mLastQueueDuration;
1727 return NO_ERROR;
1728}
1729
Steven Thomas3172e202020-01-06 19:25:30 -08001730int Surface::dispatchSetFrameRate(va_list args) {
1731 float frameRate = static_cast<float>(va_arg(args, double));
Steven Thomas62a4cf82020-01-31 12:04:03 -08001732 int8_t compatibility = static_cast<int8_t>(va_arg(args, int));
Marin Shalamanov46084422020-10-13 12:33:42 +02001733 bool shouldBeSeamless = static_cast<bool>(va_arg(args, int));
1734 return setFrameRate(frameRate, compatibility, shouldBeSeamless);
Steven Thomas3172e202020-01-06 19:25:30 -08001735}
1736
Alec Mouri09d122a2019-11-25 10:00:53 -08001737int Surface::dispatchAddCancelInterceptor(va_list args) {
1738 ANativeWindow_cancelBufferInterceptor interceptor =
1739 va_arg(args, ANativeWindow_cancelBufferInterceptor);
1740 void* data = va_arg(args, void*);
1741 std::lock_guard<std::shared_mutex> lock(mInterceptorMutex);
1742 mCancelInterceptor = interceptor;
1743 mCancelInterceptorData = data;
1744 return NO_ERROR;
1745}
1746
1747int Surface::dispatchAddDequeueInterceptor(va_list args) {
1748 ANativeWindow_dequeueBufferInterceptor interceptor =
1749 va_arg(args, ANativeWindow_dequeueBufferInterceptor);
1750 void* data = va_arg(args, void*);
1751 std::lock_guard<std::shared_mutex> lock(mInterceptorMutex);
1752 mDequeueInterceptor = interceptor;
1753 mDequeueInterceptorData = data;
1754 return NO_ERROR;
1755}
1756
1757int Surface::dispatchAddPerformInterceptor(va_list args) {
1758 ANativeWindow_performInterceptor interceptor = va_arg(args, ANativeWindow_performInterceptor);
1759 void* data = va_arg(args, void*);
1760 std::lock_guard<std::shared_mutex> lock(mInterceptorMutex);
1761 mPerformInterceptor = interceptor;
1762 mPerformInterceptorData = data;
1763 return NO_ERROR;
1764}
1765
1766int Surface::dispatchAddQueueInterceptor(va_list args) {
1767 ANativeWindow_queueBufferInterceptor interceptor =
1768 va_arg(args, ANativeWindow_queueBufferInterceptor);
1769 void* data = va_arg(args, void*);
1770 std::lock_guard<std::shared_mutex> lock(mInterceptorMutex);
1771 mQueueInterceptor = interceptor;
1772 mQueueInterceptorData = data;
1773 return NO_ERROR;
1774}
1775
John Reck401cda62020-05-07 16:04:41 -07001776int Surface::dispatchAddQueryInterceptor(va_list args) {
1777 ANativeWindow_queryInterceptor interceptor = va_arg(args, ANativeWindow_queryInterceptor);
1778 void* data = va_arg(args, void*);
1779 std::lock_guard<std::shared_mutex> lock(mInterceptorMutex);
1780 mQueryInterceptor = interceptor;
1781 mQueryInterceptorData = data;
1782 return NO_ERROR;
1783}
1784
Alec Mourief0b1532019-12-17 09:39:07 -08001785int Surface::dispatchGetLastQueuedBuffer(va_list args) {
1786 AHardwareBuffer** buffer = va_arg(args, AHardwareBuffer**);
1787 int* fence = va_arg(args, int*);
1788 float* matrix = va_arg(args, float*);
1789 sp<GraphicBuffer> graphicBuffer;
1790 sp<Fence> spFence;
1791
1792 int result = mGraphicBufferProducer->getLastQueuedBuffer(&graphicBuffer, &spFence, matrix);
1793
1794 if (graphicBuffer != nullptr) {
Yiwei Zhang03fbf9c2020-06-03 10:01:34 -07001795 *buffer = graphicBuffer->toAHardwareBuffer();
Alec Mourief0b1532019-12-17 09:39:07 -08001796 AHardwareBuffer_acquire(*buffer);
1797 } else {
1798 *buffer = nullptr;
1799 }
1800
1801 if (spFence != nullptr) {
1802 *fence = spFence->dup();
1803 } else {
1804 *fence = -1;
1805 }
1806 return result;
1807}
1808
Ady Abraham74e17562020-08-24 18:18:19 -07001809int Surface::dispatchSetFrameTimelineVsync(va_list args) {
1810 ATRACE_CALL();
1811 auto frameTimelineVsyncId = static_cast<int64_t>(va_arg(args, int64_t));
1812
1813 ALOGV("Surface::dispatchSetFrameTimelineVsync");
Robert Carr9b611b72020-10-19 12:00:23 -07001814 return setFrameTimelineVsync(frameTimelineVsyncId);
Ady Abraham74e17562020-08-24 18:18:19 -07001815}
1816
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001817bool Surface::transformToDisplayInverse() {
1818 return (mTransform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) ==
1819 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
1820}
1821
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001822int Surface::connect(int api) {
Peiyong Lind8460c82020-07-28 16:04:22 -07001823 static sp<IProducerListener> listener = new StubProducerListener();
Dan Stoza966b98b2015-03-02 22:12:37 -08001824 return connect(api, listener);
1825}
1826
1827int Surface::connect(int api, const sp<IProducerListener>& listener) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001828 return connect(api, listener, false);
1829}
1830
1831int Surface::connect(
Shuzhen Wang067fcd32019-08-14 10:41:12 -07001832 int api, bool reportBufferRemoval, const sp<SurfaceListener>& sListener) {
1833 if (sListener != nullptr) {
1834 mListenerProxy = new ProducerListenerProxy(this, sListener);
1835 }
1836 return connect(api, mListenerProxy, reportBufferRemoval);
1837}
1838
1839int Surface::connect(
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001840 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001841 ATRACE_CALL();
1842 ALOGV("Surface::connect");
1843 Mutex::Autolock lock(mMutex);
1844 IGraphicBufferProducer::QueueBufferOutput output;
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001845 mReportRemovedBuffers = reportBufferRemoval;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001846 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001847 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001848 mDefaultWidth = output.width;
1849 mDefaultHeight = output.height;
1850 mNextFrameNumber = output.nextFrameNumber;
silence_dogoode9d092a2019-06-19 16:14:53 -07001851 mMaxBufferCount = output.maxBufferCount;
Ruben Brunk1681d952014-06-27 15:51:55 -07001852
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001853 // Ignore transform hint if sticky transform is set or transform to display inverse flag is
1854 // set. Transform hint should be ignored if the client is expected to always submit buffers
1855 // in the same orientation.
1856 if (mStickyTransform == 0 && !transformToDisplayInverse()) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001857 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001858 }
1859
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001860 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001861 }
1862 if (!err && api == NATIVE_WINDOW_API_CPU) {
1863 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001864 // Clear the dirty region in case we're switching from a non-CPU API
1865 mDirtyRegion.clear();
1866 } else if (!err) {
1867 // Initialize the dirty region for tracking surface damage
1868 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001869 }
Dan Stoza5065a552015-03-17 16:23:42 -07001870
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001871 return err;
1872}
1873
Mathias Agopian365857d2013-09-11 19:35:45 -07001874
Robert Carr97b9c862016-09-08 13:54:35 -07001875int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001876 ATRACE_CALL();
1877 ALOGV("Surface::disconnect");
1878 Mutex::Autolock lock(mMutex);
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001879 mRemovedBuffers.clear();
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001880 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1881 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001882 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001883 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001884 if (!err) {
1885 mReqFormat = 0;
1886 mReqWidth = 0;
1887 mReqHeight = 0;
1888 mReqUsage = 0;
1889 mCrop.clear();
1890 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1891 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001892 mStickyTransform = 0;
Yiwei Zhang538cedc2019-06-24 19:35:03 -07001893 mAutoPrerotation = false;
Yiwei Zhangc1851922019-08-21 14:02:45 -07001894 mEnableFrameTimestamps = false;
Yiwei Zhang74c9cc32020-06-20 03:35:17 -07001895 mMaxBufferCount = NUM_BUFFER_SLOTS;
Ruben Brunk1681d952014-06-27 15:51:55 -07001896
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001897 if (api == NATIVE_WINDOW_API_CPU) {
1898 mConnectedToCpu = false;
1899 }
1900 }
1901 return err;
1902}
1903
Dan Stozad9c49712015-04-27 11:06:01 -07001904int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001905 sp<Fence>* outFence) {
1906 ATRACE_CALL();
1907 ALOGV("Surface::detachNextBuffer");
1908
Yi Kong48a619f2018-06-05 16:34:59 -07001909 if (outBuffer == nullptr || outFence == nullptr) {
Dan Stoza231832e2015-03-11 11:55:01 -07001910 return BAD_VALUE;
1911 }
1912
1913 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001914 if (mReportRemovedBuffers) {
1915 mRemovedBuffers.clear();
1916 }
Dan Stoza231832e2015-03-11 11:55:01 -07001917
Yi Kong48a619f2018-06-05 16:34:59 -07001918 sp<GraphicBuffer> buffer(nullptr);
1919 sp<Fence> fence(nullptr);
Dan Stoza231832e2015-03-11 11:55:01 -07001920 status_t result = mGraphicBufferProducer->detachNextBuffer(
1921 &buffer, &fence);
1922 if (result != NO_ERROR) {
1923 return result;
1924 }
1925
Dan Stozad9c49712015-04-27 11:06:01 -07001926 *outBuffer = buffer;
Yi Kong48a619f2018-06-05 16:34:59 -07001927 if (fence != nullptr && fence->isValid()) {
Dan Stoza231832e2015-03-11 11:55:01 -07001928 *outFence = fence;
1929 } else {
1930 *outFence = Fence::NO_FENCE;
1931 }
1932
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001933 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Yi Kong48a619f2018-06-05 16:34:59 -07001934 if (mSlots[i].buffer != nullptr &&
Shuzhen Wang55be5052017-05-23 09:41:54 -07001935 mSlots[i].buffer->getId() == buffer->getId()) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001936 if (mReportRemovedBuffers) {
1937 mRemovedBuffers.push_back(mSlots[i].buffer);
1938 }
Yi Kong48a619f2018-06-05 16:34:59 -07001939 mSlots[i].buffer = nullptr;
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001940 }
1941 }
1942
Dan Stoza231832e2015-03-11 11:55:01 -07001943 return NO_ERROR;
1944}
1945
1946int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1947{
1948 ATRACE_CALL();
1949 ALOGV("Surface::attachBuffer");
1950
1951 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001952 if (mReportRemovedBuffers) {
1953 mRemovedBuffers.clear();
1954 }
Dan Stoza231832e2015-03-11 11:55:01 -07001955
1956 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001957 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1958 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001959 int32_t attachedSlot = -1;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001960 status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer);
Dan Stoza231832e2015-03-11 11:55:01 -07001961 if (result != NO_ERROR) {
1962 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001963 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001964 return result;
1965 }
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001966 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001967 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1968 }
Dan Stoza231832e2015-03-11 11:55:01 -07001969 mSlots[attachedSlot].buffer = graphicBuffer;
Yin-Chia Yehda2e6092020-07-29 15:00:07 -07001970 mDequeuedSlots.insert(attachedSlot);
Dan Stoza231832e2015-03-11 11:55:01 -07001971
1972 return NO_ERROR;
1973}
1974
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001975int Surface::setUsage(uint64_t reqUsage)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001976{
1977 ALOGV("Surface::setUsage");
1978 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001979 if (reqUsage != mReqUsage) {
1980 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1981 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001982 mReqUsage = reqUsage;
1983 return OK;
1984}
1985
1986int Surface::setCrop(Rect const* rect)
1987{
1988 ATRACE_CALL();
1989
Pablo Ceballos60d69222015-08-07 14:47:20 -07001990 Rect realRect(Rect::EMPTY_RECT);
Yi Kong48a619f2018-06-05 16:34:59 -07001991 if (rect == nullptr || rect->isEmpty()) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001992 realRect.clear();
1993 } else {
1994 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001995 }
1996
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001997 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1998 realRect.left, realRect.top, realRect.right, realRect.bottom);
1999
2000 Mutex::Autolock lock(mMutex);
2001 mCrop = realRect;
2002 return NO_ERROR;
2003}
2004
2005int Surface::setBufferCount(int bufferCount)
2006{
2007 ATRACE_CALL();
2008 ALOGV("Surface::setBufferCount");
2009 Mutex::Autolock lock(mMutex);
2010
Pablo Ceballose5b755a2015-08-13 16:18:19 -07002011 status_t err = NO_ERROR;
2012 if (bufferCount == 0) {
2013 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
2014 } else {
2015 int minUndequeuedBuffers = 0;
2016 err = mGraphicBufferProducer->query(
2017 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
2018 if (err == NO_ERROR) {
2019 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
2020 bufferCount - minUndequeuedBuffers);
2021 }
2022 }
Mathias Agopian90147262010-01-22 11:47:55 -08002023
Pablo Ceballose5b755a2015-08-13 16:18:19 -07002024 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
2025 bufferCount, strerror(-err));
2026
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002027 return err;
2028}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002029
Pablo Ceballosfa455352015-08-12 17:47:47 -07002030int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
2031 ATRACE_CALL();
2032 ALOGV("Surface::setMaxDequeuedBufferCount");
2033 Mutex::Autolock lock(mMutex);
2034
2035 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
2036 maxDequeuedBuffers);
2037 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
2038 "returned %s", maxDequeuedBuffers, strerror(-err));
2039
Pablo Ceballosfa455352015-08-12 17:47:47 -07002040 return err;
2041}
2042
2043int Surface::setAsyncMode(bool async) {
2044 ATRACE_CALL();
2045 ALOGV("Surface::setAsyncMode");
2046 Mutex::Autolock lock(mMutex);
2047
2048 status_t err = mGraphicBufferProducer->setAsyncMode(async);
2049 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
2050 async, strerror(-err));
2051
Pablo Ceballosfa455352015-08-12 17:47:47 -07002052 return err;
2053}
2054
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07002055int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07002056 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07002057 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07002058 Mutex::Autolock lock(mMutex);
2059
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07002060 status_t err = mGraphicBufferProducer->setSharedBufferMode(
2061 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002062 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07002063 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002064 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07002065 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
2066 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07002067
2068 return err;
2069}
2070
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002071int Surface::setAutoRefresh(bool autoRefresh) {
2072 ATRACE_CALL();
2073 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
2074 Mutex::Autolock lock(mMutex);
2075
2076 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
2077 if (err == NO_ERROR) {
2078 mAutoRefresh = autoRefresh;
2079 }
2080 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
2081 autoRefresh, strerror(-err));
2082 return err;
2083}
2084
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002085int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002086{
2087 ATRACE_CALL();
2088 ALOGV("Surface::setBuffersDimensions");
2089
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002090 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002091 return BAD_VALUE;
2092
2093 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002094 if (width != mReqWidth || height != mReqHeight) {
2095 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
2096 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002097 mReqWidth = width;
2098 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002099 return NO_ERROR;
2100}
2101
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002102int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002103{
2104 ATRACE_CALL();
2105 ALOGV("Surface::setBuffersUserDimensions");
2106
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002107 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002108 return BAD_VALUE;
2109
2110 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002111 if (width != mUserWidth || height != mUserHeight) {
2112 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
2113 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002114 mUserWidth = width;
2115 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002116 return NO_ERROR;
2117}
2118
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002119int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002120{
2121 ALOGV("Surface::setBuffersFormat");
2122
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002123 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08002124 if (format != mReqFormat) {
2125 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
2126 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002127 mReqFormat = format;
2128 return NO_ERROR;
2129}
2130
2131int Surface::setScalingMode(int mode)
2132{
2133 ATRACE_CALL();
2134 ALOGV("Surface::setScalingMode(%d)", mode);
2135
2136 switch (mode) {
2137 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
2138 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
2139 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08002140 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002141 break;
2142 default:
2143 ALOGE("unknown scaling mode: %d", mode);
2144 return BAD_VALUE;
2145 }
2146
2147 Mutex::Autolock lock(mMutex);
2148 mScalingMode = mode;
2149 return NO_ERROR;
2150}
2151
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002152int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002153{
2154 ATRACE_CALL();
2155 ALOGV("Surface::setBuffersTransform");
2156 Mutex::Autolock lock(mMutex);
Vishnu Nair5eb3f062019-04-08 08:21:03 -07002157 // Ensure NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY is sticky. If the client sets the flag, do not
2158 // override it until the surface is disconnected. This is a temporary workaround for camera
2159 // until they switch to using Buffer State Layers. Currently if client sets the buffer transform
2160 // it may be overriden by the buffer producer when the producer sets the buffer transform.
2161 if (transformToDisplayInverse()) {
2162 transform |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
2163 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002164 mTransform = transform;
2165 return NO_ERROR;
2166}
2167
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002168int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07002169{
2170 ATRACE_CALL();
2171 ALOGV("Surface::setBuffersStickyTransform");
2172 Mutex::Autolock lock(mMutex);
2173 mStickyTransform = transform;
2174 return NO_ERROR;
2175}
2176
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002177int Surface::setBuffersTimestamp(int64_t timestamp)
2178{
2179 ALOGV("Surface::setBuffersTimestamp");
2180 Mutex::Autolock lock(mMutex);
2181 mTimestamp = timestamp;
2182 return NO_ERROR;
2183}
2184
Peiyong Lin34beb7a2018-03-28 11:57:12 -07002185int Surface::setBuffersDataSpace(Dataspace dataSpace)
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08002186{
2187 ALOGV("Surface::setBuffersDataSpace");
2188 Mutex::Autolock lock(mMutex);
2189 mDataSpace = dataSpace;
2190 return NO_ERROR;
2191}
2192
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07002193int Surface::setBuffersSmpte2086Metadata(const android_smpte2086_metadata* metadata) {
2194 ALOGV("Surface::setBuffersSmpte2086Metadata");
2195 Mutex::Autolock lock(mMutex);
2196 if (metadata) {
2197 mHdrMetadata.smpte2086 = *metadata;
2198 mHdrMetadata.validTypes |= HdrMetadata::SMPTE2086;
2199 } else {
2200 mHdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086;
2201 }
2202 return NO_ERROR;
2203}
2204
2205int Surface::setBuffersCta8613Metadata(const android_cta861_3_metadata* metadata) {
2206 ALOGV("Surface::setBuffersCta8613Metadata");
2207 Mutex::Autolock lock(mMutex);
2208 if (metadata) {
2209 mHdrMetadata.cta8613 = *metadata;
2210 mHdrMetadata.validTypes |= HdrMetadata::CTA861_3;
2211 } else {
2212 mHdrMetadata.validTypes &= ~HdrMetadata::CTA861_3;
2213 }
2214 return NO_ERROR;
2215}
2216
Valerie Haua82679d2018-11-21 09:31:43 -08002217int Surface::setBuffersHdr10PlusMetadata(const size_t size, const uint8_t* metadata) {
2218 ALOGV("Surface::setBuffersBlobMetadata");
2219 Mutex::Autolock lock(mMutex);
2220 if (size > 0) {
2221 mHdrMetadata.hdr10plus.assign(metadata, metadata + size);
2222 mHdrMetadata.validTypes |= HdrMetadata::HDR10PLUS;
2223 } else {
2224 mHdrMetadata.validTypes &= ~HdrMetadata::HDR10PLUS;
2225 mHdrMetadata.hdr10plus.clear();
2226 }
2227 return NO_ERROR;
2228}
2229
Peiyong Lin34beb7a2018-03-28 11:57:12 -07002230Dataspace Surface::getBuffersDataSpace() {
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -06002231 ALOGV("Surface::getBuffersDataSpace");
2232 Mutex::Autolock lock(mMutex);
2233 return mDataSpace;
2234}
2235
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002236void Surface::freeAllBuffers() {
Yin-Chia Yehda2e6092020-07-29 15:00:07 -07002237 if (!mDequeuedSlots.empty()) {
2238 ALOGE("%s: %zu buffers were freed while being dequeued!",
2239 __FUNCTION__, mDequeuedSlots.size());
2240 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002241 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Yi Kong48a619f2018-06-05 16:34:59 -07002242 mSlots[i].buffer = nullptr;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002243 }
2244}
2245
Shuzhen Wang067fcd32019-08-14 10:41:12 -07002246status_t Surface::getAndFlushBuffersFromSlots(const std::vector<int32_t>& slots,
2247 std::vector<sp<GraphicBuffer>>* outBuffers) {
2248 ALOGV("Surface::getAndFlushBuffersFromSlots");
2249 for (int32_t i : slots) {
2250 if (i < 0 || i >= NUM_BUFFER_SLOTS) {
2251 ALOGE("%s: Invalid slotIndex: %d", __FUNCTION__, i);
2252 return BAD_VALUE;
2253 }
2254 }
2255
2256 Mutex::Autolock lock(mMutex);
2257 for (int32_t i : slots) {
2258 if (mSlots[i].buffer == nullptr) {
2259 ALOGW("%s: Discarded slot %d doesn't contain buffer!", __FUNCTION__, i);
2260 continue;
2261 }
Yin-Chia Yehda2e6092020-07-29 15:00:07 -07002262 // Don't flush currently dequeued buffers
2263 if (mDequeuedSlots.count(i) > 0) {
2264 continue;
2265 }
Shuzhen Wang067fcd32019-08-14 10:41:12 -07002266 outBuffers->push_back(mSlots[i].buffer);
2267 mSlots[i].buffer = nullptr;
2268 }
2269 return OK;
2270}
2271
Dan Stoza5065a552015-03-17 16:23:42 -07002272void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
2273 ATRACE_CALL();
2274 ALOGV("Surface::setSurfaceDamage");
2275 Mutex::Autolock lock(mMutex);
2276
Dan Stozac62acbd2015-04-21 16:42:49 -07002277 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07002278 mDirtyRegion = Region::INVALID_REGION;
2279 return;
2280 }
2281
2282 mDirtyRegion.clear();
2283 for (size_t r = 0; r < numRects; ++r) {
2284 // We intentionally flip top and bottom here, since because they're
2285 // specified with a bottom-left origin, top > bottom, which fails
2286 // validation in the Region class. We will fix this up when we flip to a
2287 // top-left origin in queueBuffer.
2288 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
2289 mDirtyRegion.orSelf(rect);
2290 }
2291}
2292
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002293// ----------------------------------------------------------------------
2294// the lock/unlock APIs must be used from the same thread
2295
2296static status_t copyBlt(
2297 const sp<GraphicBuffer>& dst,
2298 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02002299 const Region& reg,
2300 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002301{
Dennis Kempinadd458f2017-05-17 19:16:31 -07002302 if (dst->getId() == src->getId())
2303 return OK;
2304
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002305 // src and dst with, height and format must be identical. no verification
2306 // is done here.
2307 status_t err;
Yi Kong48a619f2018-06-05 16:34:59 -07002308 uint8_t* src_bits = nullptr;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002309 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
2310 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002311 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
2312
Yi Kong48a619f2018-06-05 16:34:59 -07002313 uint8_t* dst_bits = nullptr;
Francis Hart7b09e792015-01-09 11:10:54 +02002314 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
2315 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002316 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02002317 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002318
2319 Region::const_iterator head(reg.begin());
2320 Region::const_iterator tail(reg.end());
2321 if (head != tail && src_bits && dst_bits) {
2322 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002323 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
2324 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002325
2326 while (head != tail) {
2327 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002328 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002329 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002330 size_t size = static_cast<uint32_t>(r.width()) * bpp;
2331 uint8_t const * s = src_bits +
2332 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
2333 uint8_t * d = dst_bits +
2334 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002335 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08002336 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002337 h = 1;
2338 }
2339 do {
2340 memcpy(d, s, size);
2341 d += dbpr;
2342 s += sbpr;
2343 } while (--h > 0);
2344 }
2345 }
2346
2347 if (src_bits)
2348 src->unlock();
2349
2350 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02002351 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002352
2353 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002354}
2355
Mathias Agopiana138f892010-05-21 17:24:35 -07002356// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002357
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002358status_t Surface::lock(
2359 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
2360{
Yi Kong48a619f2018-06-05 16:34:59 -07002361 if (mLockedBuffer != nullptr) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002362 ALOGE("Surface::lock failed, already locked");
2363 return INVALID_OPERATION;
2364 }
2365
2366 if (!mConnectedToCpu) {
2367 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
2368 if (err) {
2369 return err;
2370 }
2371 // we're intending to do software rendering from this point
2372 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
2373 }
2374
2375 ANativeWindowBuffer* out;
2376 int fenceFd = -1;
2377 status_t err = dequeueBuffer(&out, &fenceFd);
2378 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
2379 if (err == NO_ERROR) {
2380 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002381 const Rect bounds(backBuffer->width, backBuffer->height);
2382
2383 Region newDirtyRegion;
2384 if (inOutDirtyBounds) {
2385 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
2386 newDirtyRegion.andSelf(bounds);
2387 } else {
2388 newDirtyRegion.set(bounds);
2389 }
2390
2391 // figure out if we can copy the frontbuffer back
2392 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
Yi Kong48a619f2018-06-05 16:34:59 -07002393 const bool canCopyBack = (frontBuffer != nullptr &&
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002394 backBuffer->width == frontBuffer->width &&
2395 backBuffer->height == frontBuffer->height &&
2396 backBuffer->format == frontBuffer->format);
2397
2398 if (canCopyBack) {
2399 // copy the area that is invalid and not repainted this round
2400 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02002401 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02002402 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02002403 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002404 } else {
2405 // if we can't copy-back anything, modify the user's dirty
2406 // region to make sure they redraw the whole buffer
2407 newDirtyRegion.set(bounds);
2408 mDirtyRegion.clear();
2409 Mutex::Autolock lock(mMutex);
2410 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
2411 mSlots[i].dirtyRegion.clear();
2412 }
2413 }
2414
2415
2416 { // scope for the lock
2417 Mutex::Autolock lock(mMutex);
2418 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
2419 if (backBufferSlot >= 0) {
2420 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
2421 mDirtyRegion.subtract(dirtyRegion);
2422 dirtyRegion = newDirtyRegion;
2423 }
2424 }
2425
2426 mDirtyRegion.orSelf(newDirtyRegion);
2427 if (inOutDirtyBounds) {
2428 *inOutDirtyBounds = newDirtyRegion.getBounds();
2429 }
2430
2431 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03002432 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002433 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03002434 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002435
2436 ALOGW_IF(res, "failed locking buffer (handle = %p)",
2437 backBuffer->handle);
2438
2439 if (res != 0) {
2440 err = INVALID_OPERATION;
2441 } else {
2442 mLockedBuffer = backBuffer;
2443 outBuffer->width = backBuffer->width;
2444 outBuffer->height = backBuffer->height;
2445 outBuffer->stride = backBuffer->stride;
2446 outBuffer->format = backBuffer->format;
2447 outBuffer->bits = vaddr;
2448 }
2449 }
2450 return err;
2451}
2452
2453status_t Surface::unlockAndPost()
2454{
Yi Kong48a619f2018-06-05 16:34:59 -07002455 if (mLockedBuffer == nullptr) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002456 ALOGE("Surface::unlockAndPost failed, no locked buffer");
2457 return INVALID_OPERATION;
2458 }
2459
Francis Hart8f396012014-04-01 15:30:53 +03002460 int fd = -1;
2461 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002462 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
2463
Francis Hart8f396012014-04-01 15:30:53 +03002464 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002465 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
2466 mLockedBuffer->handle, strerror(-err));
2467
2468 mPostedBuffer = mLockedBuffer;
Yi Kong48a619f2018-06-05 16:34:59 -07002469 mLockedBuffer = nullptr;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002470 return err;
2471}
2472
Robert Carr9f31e292016-04-11 11:15:32 -07002473bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
2474 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07002475 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07002476 return true;
2477 }
2478 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
2479}
2480
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07002481status_t Surface::getUniqueId(uint64_t* outId) const {
2482 Mutex::Autolock lock(mMutex);
2483 return mGraphicBufferProducer->getUniqueId(outId);
2484}
2485
Chia-I Wue2786ea2017-08-07 10:36:08 -07002486int Surface::getConsumerUsage(uint64_t* outUsage) const {
2487 Mutex::Autolock lock(mMutex);
2488 return mGraphicBufferProducer->getConsumerUsage(outUsage);
2489}
2490
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07002491status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
2492 if (out == nullptr) {
2493 ALOGE("%s: out must not be null!", __FUNCTION__);
2494 return BAD_VALUE;
2495 }
2496
2497 Mutex::Autolock lock(mMutex);
2498 *out = mRemovedBuffers;
2499 mRemovedBuffers.clear();
2500 return OK;
2501}
2502
Peiyong Lin0f6a4122019-06-25 17:25:02 -07002503status_t Surface::attachAndQueueBufferWithDataspace(Surface* surface, sp<GraphicBuffer> buffer,
2504 Dataspace dataspace) {
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002505 if (buffer == nullptr) {
2506 return BAD_VALUE;
2507 }
2508 int err = static_cast<ANativeWindow*>(surface)->perform(surface, NATIVE_WINDOW_API_CONNECT,
2509 NATIVE_WINDOW_API_CPU);
2510 if (err != OK) {
2511 return err;
2512 }
Peiyong Lin0f6a4122019-06-25 17:25:02 -07002513 ui::Dataspace tmpDataspace = surface->getBuffersDataSpace();
2514 err = surface->setBuffersDataSpace(dataspace);
2515 if (err != OK) {
2516 return err;
2517 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002518 err = surface->attachBuffer(buffer->getNativeBuffer());
2519 if (err != OK) {
2520 return err;
2521 }
2522 err = static_cast<ANativeWindow*>(surface)->queueBuffer(surface, buffer->getNativeBuffer(), -1);
2523 if (err != OK) {
2524 return err;
2525 }
Peiyong Lin0f6a4122019-06-25 17:25:02 -07002526 err = surface->setBuffersDataSpace(tmpDataspace);
2527 if (err != OK) {
2528 return err;
2529 }
Chavi Weingarten40482ff2017-11-30 01:51:40 +00002530 err = surface->disconnect(NATIVE_WINDOW_API_CPU);
2531 return err;
2532}
2533
Yiwei Zhang538cedc2019-06-24 19:35:03 -07002534int Surface::setAutoPrerotation(bool autoPrerotation) {
2535 ATRACE_CALL();
2536 ALOGV("Surface::setAutoPrerotation (%d)", autoPrerotation);
2537 Mutex::Autolock lock(mMutex);
2538
2539 if (mAutoPrerotation == autoPrerotation) {
2540 return OK;
2541 }
2542
2543 status_t err = mGraphicBufferProducer->setAutoPrerotation(autoPrerotation);
2544 if (err == NO_ERROR) {
2545 mAutoPrerotation = autoPrerotation;
2546 }
2547 ALOGE_IF(err, "IGraphicBufferProducer::setAutoPrerotation(%d) returned %s", autoPrerotation,
2548 strerror(-err));
2549 return err;
2550}
2551
Shuzhen Wang067fcd32019-08-14 10:41:12 -07002552void Surface::ProducerListenerProxy::onBuffersDiscarded(const std::vector<int32_t>& slots) {
2553 ATRACE_CALL();
2554 sp<Surface> parent = mParent.promote();
2555 if (parent == nullptr) {
2556 return;
2557 }
2558
2559 std::vector<sp<GraphicBuffer>> discardedBufs;
2560 status_t res = parent->getAndFlushBuffersFromSlots(slots, &discardedBufs);
2561 if (res != OK) {
2562 ALOGE("%s: Failed to get buffers from slots: %s(%d)", __FUNCTION__,
2563 strerror(-res), res);
2564 return;
2565 }
2566
2567 mSurfaceListener->onBuffersDiscarded(discardedBufs);
2568}
2569
Marin Shalamanov46084422020-10-13 12:33:42 +02002570status_t Surface::setFrameRate(float frameRate, int8_t compatibility, bool shouldBeSeamless) {
Steven Thomas3172e202020-01-06 19:25:30 -08002571 ATRACE_CALL();
Steven Thomas62a4cf82020-01-31 12:04:03 -08002572 ALOGV("Surface::setFrameRate");
2573
2574 if (!ValidateFrameRate(frameRate, compatibility, "Surface::setFrameRate")) {
2575 return BAD_VALUE;
2576 }
2577
Marin Shalamanov46084422020-10-13 12:33:42 +02002578 return composerService()->setFrameRate(mGraphicBufferProducer, frameRate, compatibility,
2579 shouldBeSeamless);
Steven Thomas3172e202020-01-06 19:25:30 -08002580}
2581
Robert Carr9b611b72020-10-19 12:00:23 -07002582status_t Surface::setFrameTimelineVsync(int64_t frameTimelineVsyncId) {
2583 return composerService()->setFrameTimelineVsync(mGraphicBufferProducer,
2584 frameTimelineVsyncId);
2585}
2586
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002587}; // namespace android