blob: 2de14c8846a4001c2463c3888d6f1492b70deb27 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002 * Copyright (C) 2010 The Android Open Source Project
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Surface"
Mathias Agopiane3c697f2013-02-14 17:11:02 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Mathias Agopian05debe12017-02-08 17:04:18 -080021#include <gui/Surface.h>
Mathias Agopianb0e76f42012-03-23 14:15:44 -070022
Mathias Agopiancb496ac2017-05-22 14:21:00 -070023#include <inttypes.h>
24
Mathias Agopian05debe12017-02-08 17:04:18 -080025#include <android/native_window.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080026
Mathias Agopian9cce3252010-02-09 17:46:37 -080027#include <utils/Log.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080028#include <utils/Trace.h>
Rachad7cb0d392014-07-29 17:53:53 -070029#include <utils/NativeHandle.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080030
Ian Elliott62c48c92017-01-20 13:13:20 -070031#include <ui/DisplayStatInfo.h>
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -070032#include <ui/Fence.h>
33#include <ui/HdrCapabilities.h>
34#include <ui/Region.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070035
Mathias Agopian2b5dd402017-02-07 17:36:19 -080036#include <gui/BufferItem.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070037#include <gui/IProducerListener.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080038
Mathias Agopian2b5dd402017-02-07 17:36:19 -080039#include <gui/ISurfaceComposer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080040#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -060042#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
43#include <configstore/Utils.h>
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045namespace android {
46
Peiyong Linfd997e02018-03-28 15:29:00 -070047using ui::ColorMode;
Peiyong Lin34beb7a2018-03-28 11:57:12 -070048using ui::Dataspace;
Peiyong Linfd997e02018-03-28 15:29:00 -070049
Ian Elliotta2eb34c2017-07-18 11:05:49 -060050Surface::Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp)
51 : mGraphicBufferProducer(bufferProducer),
52 mCrop(Rect::EMPTY_RECT),
53 mBufferAge(0),
54 mGenerationNumber(0),
55 mSharedBufferMode(false),
56 mAutoRefresh(false),
57 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
58 mSharedBufferHasBeenQueued(false),
59 mQueriedSupportedTimestamps(false),
60 mFrameTimestampsSupportsPresent(false),
61 mEnableFrameTimestamps(false),
62 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>()) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -080063 // Initialize the ANativeWindow function pointers.
64 ANativeWindow::setSwapInterval = hook_setSwapInterval;
65 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
66 ANativeWindow::cancelBuffer = hook_cancelBuffer;
67 ANativeWindow::queueBuffer = hook_queueBuffer;
68 ANativeWindow::query = hook_query;
69 ANativeWindow::perform = hook_perform;
70
71 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
72 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
73 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
74 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
75
76 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
77 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
78
79 mReqWidth = 0;
80 mReqHeight = 0;
81 mReqFormat = 0;
82 mReqUsage = 0;
83 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Peiyong Lin34beb7a2018-03-28 11:57:12 -070084 mDataSpace = Dataspace::UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080085 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
86 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070087 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080088 mDefaultWidth = 0;
89 mDefaultHeight = 0;
90 mUserWidth = 0;
91 mUserHeight = 0;
92 mTransformHint = 0;
93 mConsumerRunningBehind = false;
94 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070095 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070096 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070097}
98
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070099Surface::~Surface() {
100 if (mConnectedToCpu) {
101 Surface::disconnect(NATIVE_WINDOW_API_CPU);
102 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800103}
104
Brian Anderson3da8d272016-07-28 16:20:47 -0700105sp<ISurfaceComposer> Surface::composerService() const {
106 return ComposerService::getComposerService();
107}
108
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800109nsecs_t Surface::now() const {
110 return systemTime();
111}
112
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800113sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
114 return mGraphicBufferProducer;
115}
116
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900117void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
118 mGraphicBufferProducer->setSidebandStream(stream);
119}
120
Dan Stoza29a3e902014-06-20 13:13:57 -0700121void Surface::allocateBuffers() {
122 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
123 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700124 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
125 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700126}
127
Dan Stoza812ed062015-06-02 15:45:22 -0700128status_t Surface::setGenerationNumber(uint32_t generation) {
129 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
130 if (result == NO_ERROR) {
131 mGenerationNumber = generation;
132 }
133 return result;
134}
135
Dan Stoza7dde5992015-05-22 09:51:44 -0700136uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700137 Mutex::Autolock lock(mMutex);
138 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700139}
140
Dan Stozac6f30bd2015-06-08 09:32:50 -0700141String8 Surface::getConsumerName() const {
142 return mGraphicBufferProducer->getConsumerName();
143}
144
Dan Stoza127fc632015-06-30 13:43:32 -0700145status_t Surface::setDequeueTimeout(nsecs_t timeout) {
146 return mGraphicBufferProducer->setDequeueTimeout(timeout);
147}
148
Dan Stoza50101d02016-04-07 16:53:23 -0700149status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700150 sp<Fence>* outFence, float outTransformMatrix[16]) {
151 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
152 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700153}
154
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800155status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
156 ATRACE_CALL();
157
158 DisplayStatInfo stats;
Chih-Hung Hsiehd66be0a2017-10-05 13:51:32 -0700159 status_t result = composerService()->getDisplayStats(NULL, &stats);
160 if (result != NO_ERROR) {
161 return result;
162 }
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800163
164 *outRefreshDuration = stats.vsyncPeriod;
165
166 return NO_ERROR;
167}
168
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700169void Surface::enableFrameTimestamps(bool enable) {
170 Mutex::Autolock lock(mMutex);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800171 // If going from disabled to enabled, get the initial values for
172 // compositor and display timing.
173 if (!mEnableFrameTimestamps && enable) {
174 FrameEventHistoryDelta delta;
175 mGraphicBufferProducer->getFrameTimestamps(&delta);
176 mFrameEventHistory->applyDelta(delta);
177 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700178 mEnableFrameTimestamps = enable;
179}
180
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800181status_t Surface::getCompositorTiming(
182 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
183 nsecs_t* compositeToPresentLatency) {
184 Mutex::Autolock lock(mMutex);
185 if (!mEnableFrameTimestamps) {
186 return INVALID_OPERATION;
187 }
188
189 if (compositeDeadline != nullptr) {
190 *compositeDeadline =
191 mFrameEventHistory->getNextCompositeDeadline(now());
192 }
193 if (compositeInterval != nullptr) {
194 *compositeInterval = mFrameEventHistory->getCompositeInterval();
195 }
196 if (compositeToPresentLatency != nullptr) {
197 *compositeToPresentLatency =
198 mFrameEventHistory->getCompositeToPresentLatency();
199 }
200 return NO_ERROR;
201}
202
Brian Anderson50143b32016-09-30 14:01:24 -0700203static bool checkConsumerForUpdates(
204 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700205 const nsecs_t* outLatchTime,
206 const nsecs_t* outFirstRefreshStartTime,
207 const nsecs_t* outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700208 const nsecs_t* outGpuCompositionDoneTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700209 const nsecs_t* outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700210 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700211 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700212 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
213 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700214 !e->hasFirstRefreshStartInfo();
Brian Andersonb04c6f02016-10-21 12:57:46 -0700215 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700216 !e->hasGpuCompositionDoneInfo();
217 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
218 !e->hasDisplayPresentInfo();
219
Brian Anderson6b376712017-04-04 10:51:39 -0700220 // LastRefreshStart, DequeueReady, and Release are never available for the
221 // last frame.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700222 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
223 !e->hasLastRefreshStartInfo() &&
224 (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700225 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
226 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700227 bool checkForRelease = (outReleaseTime != nullptr) &&
228 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
229
230 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700231 return checkForLatch || checkForFirstRefreshStart ||
Brian Andersonb04c6f02016-10-21 12:57:46 -0700232 checkForLastRefreshStart || checkForGpuCompositionDone ||
Brian Anderson4e606e32017-03-16 15:34:57 -0700233 checkForDisplayPresent || checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700234}
235
Brian Anderson3d4039d2016-09-23 16:31:30 -0700236static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
237 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700238 // We always get valid timestamps for these eventually.
239 *dst = (src == FrameEvents::TIMESTAMP_PENDING) ?
240 NATIVE_WINDOW_TIMESTAMP_PENDING : src;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700241 }
242}
243
Brian Andersondc96fdf2017-03-20 16:54:25 -0700244static void getFrameTimestampFence(nsecs_t *dst,
245 const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700246 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700247 if (!fenceShouldBeKnown) {
248 *dst = NATIVE_WINDOW_TIMESTAMP_PENDING;
249 return;
250 }
251
Brian Anderson3d4039d2016-09-23 16:31:30 -0700252 nsecs_t signalTime = src->getSignalTime();
Brian Andersondc96fdf2017-03-20 16:54:25 -0700253 *dst = (signalTime == Fence::SIGNAL_TIME_PENDING) ?
254 NATIVE_WINDOW_TIMESTAMP_PENDING :
255 (signalTime == Fence::SIGNAL_TIME_INVALID) ?
256 NATIVE_WINDOW_TIMESTAMP_INVALID :
257 signalTime;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700258 }
259}
260
Brian Anderson069b3652016-07-22 10:32:47 -0700261status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700262 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700263 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700264 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700265 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
266 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800267 ATRACE_CALL();
268
Brian Anderson3890c392016-07-25 12:48:08 -0700269 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700270
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700271 if (!mEnableFrameTimestamps) {
272 return INVALID_OPERATION;
273 }
274
Brian Anderson6b376712017-04-04 10:51:39 -0700275 // Verify the requested timestamps are supported.
276 querySupportedTimestampsLocked();
277 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
278 return BAD_VALUE;
279 }
280
Brian Anderson3da8d272016-07-28 16:20:47 -0700281 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700282 if (events == nullptr) {
283 // If the entry isn't available in the producer, it's definitely not
284 // available in the consumer.
285 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700286 }
287
Brian Anderson50143b32016-09-30 14:01:24 -0700288 // Update our cache of events if the requested events are not available.
289 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700290 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700291 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700292 outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700293 FrameEventHistoryDelta delta;
294 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700295 mFrameEventHistory->applyDelta(delta);
296 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700297 }
298
Brian Anderson3890c392016-07-25 12:48:08 -0700299 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700300 // The entry was available before the update, but was overwritten
301 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700302 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800303 }
Brian Anderson069b3652016-07-22 10:32:47 -0700304
Brian Anderson3d4039d2016-09-23 16:31:30 -0700305 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700306 getFrameTimestamp(outLatchTime, events->latchTime);
307 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
308 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
309 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700310
Brian Andersondc96fdf2017-03-20 16:54:25 -0700311 getFrameTimestampFence(outAcquireTime, events->acquireFence,
312 events->hasAcquireInfo());
313 getFrameTimestampFence(outGpuCompositionDoneTime,
314 events->gpuCompositionDoneFence,
315 events->hasGpuCompositionDoneInfo());
316 getFrameTimestampFence(outDisplayPresentTime, events->displayPresentFence,
317 events->hasDisplayPresentInfo());
318 getFrameTimestampFence(outReleaseTime, events->releaseFence,
319 events->hasReleaseInfo());
Brian Anderson069b3652016-07-22 10:32:47 -0700320
321 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800322}
323
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600324using namespace android::hardware::configstore;
325using namespace android::hardware::configstore::V1_0;
326
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700327status_t Surface::getWideColorSupport(bool* supported) {
328 ATRACE_CALL();
329
330 sp<IBinder> display(
331 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Peiyong Lina52f0292018-03-14 17:26:31 -0700332 Vector<ColorMode> colorModes;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700333 status_t err =
334 composerService()->getDisplayColorModes(display, &colorModes);
335
336 if (err)
337 return err;
338
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600339 bool wideColorBoardConfig =
340 getBool<ISurfaceFlingerConfigs,
341 &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
342
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700343 *supported = false;
Peiyong Lina52f0292018-03-14 17:26:31 -0700344 for (ColorMode colorMode : colorModes) {
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700345 switch (colorMode) {
Peiyong Lina52f0292018-03-14 17:26:31 -0700346 case ColorMode::DISPLAY_P3:
347 case ColorMode::ADOBE_RGB:
348 case ColorMode::DCI_P3:
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600349 if (wideColorBoardConfig) {
350 *supported = true;
351 }
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700352 break;
353 default:
354 break;
355 }
356 }
357
358 return NO_ERROR;
359}
360
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700361status_t Surface::getHdrSupport(bool* supported) {
362 ATRACE_CALL();
363
364 sp<IBinder> display(
365 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
366 HdrCapabilities hdrCapabilities;
367 status_t err =
368 composerService()->getHdrCapabilities(display, &hdrCapabilities);
369
370 if (err)
371 return err;
372
373 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
374
375 return NO_ERROR;
376}
377
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800378int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
379 Surface* c = getSelf(window);
380 return c->setSwapInterval(interval);
381}
382
383int Surface::hook_dequeueBuffer(ANativeWindow* window,
384 ANativeWindowBuffer** buffer, int* fenceFd) {
385 Surface* c = getSelf(window);
386 return c->dequeueBuffer(buffer, fenceFd);
387}
388
389int Surface::hook_cancelBuffer(ANativeWindow* window,
390 ANativeWindowBuffer* buffer, int fenceFd) {
391 Surface* c = getSelf(window);
392 return c->cancelBuffer(buffer, fenceFd);
393}
394
395int Surface::hook_queueBuffer(ANativeWindow* window,
396 ANativeWindowBuffer* buffer, int fenceFd) {
397 Surface* c = getSelf(window);
398 return c->queueBuffer(buffer, fenceFd);
399}
400
401int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
402 ANativeWindowBuffer** buffer) {
403 Surface* c = getSelf(window);
404 ANativeWindowBuffer* buf;
405 int fenceFd = -1;
406 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600407 if (result != OK) {
408 return result;
409 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800410 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700411 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800412 if (waitResult != OK) {
413 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
414 waitResult);
415 c->cancelBuffer(buf, -1);
416 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700417 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800418 *buffer = buf;
419 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700420}
421
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800422int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
423 ANativeWindowBuffer* buffer) {
424 Surface* c = getSelf(window);
425 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700426}
427
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800428int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
429 ANativeWindowBuffer* buffer) {
430 Surface* c = getSelf(window);
431 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700432}
433
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800434int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
435 ANativeWindowBuffer* buffer) {
436 Surface* c = getSelf(window);
437 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700438}
Mathias Agopian62185b72009-04-16 16:19:50 -0700439
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800440int Surface::hook_query(const ANativeWindow* window,
441 int what, int* value) {
442 const Surface* c = getSelf(window);
443 return c->query(what, value);
444}
445
446int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
447 va_list args;
448 va_start(args, operation);
449 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700450 int result = c->perform(operation, args);
451 va_end(args);
452 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800453}
454
455int Surface::setSwapInterval(int interval) {
456 ATRACE_CALL();
457 // EGL specification states:
458 // interval is silently clamped to minimum and maximum implementation
459 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800460
461 if (interval < minSwapInterval)
462 interval = minSwapInterval;
463
464 if (interval > maxSwapInterval)
465 interval = maxSwapInterval;
466
Jorim Jaggi0a3e7842018-07-17 13:48:33 +0200467 const bool wasSwapIntervalZero = mSwapIntervalZero;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700468 mSwapIntervalZero = (interval == 0);
Jorim Jaggi0a3e7842018-07-17 13:48:33 +0200469
470 if (mSwapIntervalZero != wasSwapIntervalZero) {
471 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
472 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800473
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700474 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800475}
476
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700477int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800478 ATRACE_CALL();
479 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800480
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800481 uint32_t reqWidth;
482 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800483 PixelFormat reqFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700484 uint64_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700485 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800486
487 {
488 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700489 if (mReportRemovedBuffers) {
490 mRemovedBuffers.clear();
491 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800492
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800493 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
494 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800495
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800496 reqFormat = mReqFormat;
497 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800498
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700499 enableFrameTimestamps = mEnableFrameTimestamps;
500
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700501 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800502 BufferItem::INVALID_BUFFER_SLOT) {
503 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
504 if (gbuf != NULL) {
505 *buffer = gbuf.get();
506 *fenceFd = -1;
507 return OK;
508 }
509 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800510 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
511
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800512 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800513 sp<Fence> fence;
Dan Stoza932f0082017-05-31 13:50:16 -0700514 nsecs_t startTime = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700515
516 FrameEventHistoryDelta frameTimestamps;
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600517 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, reqWidth, reqHeight,
518 reqFormat, reqUsage, &mBufferAge,
519 enableFrameTimestamps ? &frameTimestamps
520 : nullptr);
Dan Stoza932f0082017-05-31 13:50:16 -0700521 mLastDequeueDuration = systemTime() - startTime;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800522
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800523 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700524 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700525 "(%d, %d, %d, %#" PRIx64 ") failed: %d",
526 reqWidth, reqHeight, reqFormat, reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800527 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700528 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800529
Dan Stoza90ce2a92017-05-01 16:31:53 -0700530 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
531 ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf);
532 android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging
533 return FAILED_TRANSACTION;
534 }
535
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800536 Mutex::Autolock lock(mMutex);
537
Dan Stoza932f0082017-05-31 13:50:16 -0700538 // Write this while holding the mutex
539 mLastDequeueStartTime = startTime;
540
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800541 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700542
543 // this should never happen
544 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
545
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800546 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
547 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700548 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700549
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700550 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700551 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700552 }
553
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700554 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
555 if (mReportRemovedBuffers && (gbuf != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700556 mRemovedBuffers.push_back(gbuf);
557 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800558 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
559 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700560 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700561 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800562 return result;
563 }
564 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700565
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800566 if (fence->isValid()) {
567 *fenceFd = fence->dup();
568 if (*fenceFd == -1) {
569 ALOGE("dequeueBuffer: error duping fence: %d", errno);
570 // dup() should never fail; something is badly wrong. Soldier on
571 // and hope for the best; the worst that should happen is some
572 // visible corruption that lasts until the next frame.
573 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700574 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800575 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700576 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800577
578 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800579
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700580 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800581 mSharedBufferSlot = buf;
582 mSharedBufferHasBeenQueued = false;
583 } else if (mSharedBufferSlot == buf) {
584 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
585 mSharedBufferHasBeenQueued = false;
586 }
587
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800588 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700589}
590
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800591int Surface::cancelBuffer(android_native_buffer_t* buffer,
592 int fenceFd) {
593 ATRACE_CALL();
594 ALOGV("Surface::cancelBuffer");
595 Mutex::Autolock lock(mMutex);
596 int i = getSlotFromBufferLocked(buffer);
597 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900598 if (fenceFd >= 0) {
599 close(fenceFd);
600 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800601 return i;
602 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800603 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
604 if (fenceFd >= 0) {
605 close(fenceFd);
606 }
607 return OK;
608 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800609 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
610 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800611
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700612 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800613 mSharedBufferHasBeenQueued = true;
614 }
615
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800616 return OK;
617}
618
619int Surface::getSlotFromBufferLocked(
620 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800621 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
622 if (mSlots[i].buffer != NULL &&
623 mSlots[i].buffer->handle == buffer->handle) {
624 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700625 }
626 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800627 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
628 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700629}
630
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800631int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800632 ALOGV("Surface::lockBuffer");
633 Mutex::Autolock lock(mMutex);
634 return OK;
635}
Mathias Agopian631f3582010-05-25 17:51:34 -0700636
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800637int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
638 ATRACE_CALL();
639 ALOGV("Surface::queueBuffer");
640 Mutex::Autolock lock(mMutex);
641 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700642 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800643
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800644 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700645 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700646 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700647 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700648 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800649 } else {
650 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700651 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800652 int i = getSlotFromBufferLocked(buffer);
653 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900654 if (fenceFd >= 0) {
655 close(fenceFd);
656 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800657 return i;
658 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800659 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
660 if (fenceFd >= 0) {
661 close(fenceFd);
662 }
663 return OK;
664 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800667 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700668 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800669 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800671 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
672 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700673 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700674 static_cast<android_dataspace>(mDataSpace), crop, mScalingMode,
675 mTransform ^ mStickyTransform, fence, mStickyTransform,
676 mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700677
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700678 // we should send HDR metadata as needed if this becomes a bottleneck
679 input.setHdrMetadata(mHdrMetadata);
680
Dan Stozac62acbd2015-04-21 16:42:49 -0700681 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700682 input.setSurfaceDamage(Region::INVALID_REGION);
683 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700684 // Here we do two things:
685 // 1) The surface damage was specified using the OpenGL ES convention of
686 // the origin being in the bottom-left corner. Here we flip to the
687 // convention that the rest of the system uses (top-left corner) by
688 // subtracting all top/bottom coordinates from the buffer height.
689 // 2) If the buffer is coming in rotated (for example, because the EGL
690 // implementation is reacting to the transform hint coming back from
691 // SurfaceFlinger), the surface damage needs to be rotated the
692 // opposite direction, since it was generated assuming an unrotated
693 // buffer (the app doesn't know that the EGL implementation is
694 // reacting to the transform hint behind its back). The
695 // transformations in the switch statement below apply those
696 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
697
698 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700699 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700700 bool rotated90 = (mTransform ^ mStickyTransform) &
701 NATIVE_WINDOW_TRANSFORM_ROT_90;
702 if (rotated90) {
703 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700704 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700705
Dan Stoza5065a552015-03-17 16:23:42 -0700706 Region flippedRegion;
707 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700708 int left = rect.left;
709 int right = rect.right;
710 int top = height - rect.bottom; // Flip from OpenGL convention
711 int bottom = height - rect.top; // Flip from OpenGL convention
712 switch (mTransform ^ mStickyTransform) {
713 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
714 // Rotate 270 degrees
715 Rect flippedRect{top, width - right, bottom, width - left};
716 flippedRegion.orSelf(flippedRect);
717 break;
718 }
719 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
720 // Rotate 180 degrees
721 Rect flippedRect{width - right, height - bottom,
722 width - left, height - top};
723 flippedRegion.orSelf(flippedRect);
724 break;
725 }
726 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
727 // Rotate 90 degrees
728 Rect flippedRect{height - bottom, left,
729 height - top, right};
730 flippedRegion.orSelf(flippedRect);
731 break;
732 }
733 default: {
734 Rect flippedRect{left, top, right, bottom};
735 flippedRegion.orSelf(flippedRect);
736 break;
737 }
738 }
Dan Stoza5065a552015-03-17 16:23:42 -0700739 }
740
741 input.setSurfaceDamage(flippedRegion);
742 }
743
Dan Stoza70ccba52016-07-01 14:00:40 -0700744 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800745 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700746 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800747 if (err != OK) {
748 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
749 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800750
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700751 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700752 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700753 // Update timestamps with the local acquire fence.
754 // The consumer doesn't send it back to prevent us from having two
755 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700756 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700757 std::make_shared<FenceTime>(std::move(fence)));
758
759 // Cache timestamps of signaled fences so we can close their file
760 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700761 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700762 }
763
Brian Anderson50143b32016-09-30 14:01:24 -0700764 mLastFrameNumber = mNextFrameNumber;
765
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700766 mDefaultWidth = output.width;
767 mDefaultHeight = output.height;
768 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700769
Ruben Brunk1681d952014-06-27 15:51:55 -0700770 // Disable transform hint if sticky transform is set.
771 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700772 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700773 }
774
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700775 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700776
Dan Stozac62acbd2015-04-21 16:42:49 -0700777 if (!mConnectedToCpu) {
778 // Clear surface damage back to full-buffer
779 mDirtyRegion = Region::INVALID_REGION;
780 }
Dan Stoza5065a552015-03-17 16:23:42 -0700781
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700782 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800783 mSharedBufferHasBeenQueued = true;
784 }
785
Robert Carr9f31e292016-04-11 11:15:32 -0700786 mQueueBufferCondition.broadcast();
787
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800788 return err;
789}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700790
Brian Anderson6b376712017-04-04 10:51:39 -0700791void Surface::querySupportedTimestampsLocked() const {
792 // mMutex must be locked when calling this method.
793
794 if (mQueriedSupportedTimestamps) {
795 return;
796 }
797 mQueriedSupportedTimestamps = true;
798
799 std::vector<FrameEvent> supportedFrameTimestamps;
800 status_t err = composerService()->getSupportedFrameTimestamps(
801 &supportedFrameTimestamps);
802
803 if (err != NO_ERROR) {
804 return;
805 }
806
807 for (auto sft : supportedFrameTimestamps) {
808 if (sft == FrameEvent::DISPLAY_PRESENT) {
809 mFrameTimestampsSupportsPresent = true;
810 }
811 }
812}
813
Mathias Agopiana67932f2011-04-20 14:20:59 -0700814int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800815 ATRACE_CALL();
816 ALOGV("Surface::query");
817 { // scope for the lock
818 Mutex::Autolock lock(mMutex);
819 switch (what) {
820 case NATIVE_WINDOW_FORMAT:
821 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800822 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800823 return NO_ERROR;
824 }
825 break;
826 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700827 if (composerService()->authenticateSurfaceTexture(
828 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800829 *value = 1;
830 } else {
831 *value = 0;
832 }
833 return NO_ERROR;
834 }
835 case NATIVE_WINDOW_CONCRETE_TYPE:
836 *value = NATIVE_WINDOW_SURFACE;
837 return NO_ERROR;
838 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800839 *value = static_cast<int>(
840 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800841 return NO_ERROR;
842 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800843 *value = static_cast<int>(
844 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800845 return NO_ERROR;
846 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800847 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800848 return NO_ERROR;
849 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
850 status_t err = NO_ERROR;
851 if (!mConsumerRunningBehind) {
852 *value = 0;
853 } else {
854 err = mGraphicBufferProducer->query(what, value);
855 if (err == NO_ERROR) {
856 mConsumerRunningBehind = *value;
857 }
858 }
859 return err;
860 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600861 case NATIVE_WINDOW_BUFFER_AGE: {
862 if (mBufferAge > INT32_MAX) {
863 *value = 0;
864 } else {
865 *value = static_cast<int32_t>(mBufferAge);
866 }
867 return NO_ERROR;
868 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700869 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
870 int64_t durationUs = mLastDequeueDuration / 1000;
871 *value = durationUs > std::numeric_limits<int>::max() ?
872 std::numeric_limits<int>::max() :
873 static_cast<int>(durationUs);
874 return NO_ERROR;
875 }
876 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
877 int64_t durationUs = mLastQueueDuration / 1000;
878 *value = durationUs > std::numeric_limits<int>::max() ?
879 std::numeric_limits<int>::max() :
880 static_cast<int>(durationUs);
881 return NO_ERROR;
882 }
Brian Anderson6b376712017-04-04 10:51:39 -0700883 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
884 querySupportedTimestampsLocked();
885 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
886 return NO_ERROR;
887 }
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800888 case NATIVE_WINDOW_IS_VALID: {
889 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
890 return NO_ERROR;
891 }
Peiyong Lin654f87b2018-01-30 14:21:33 -0800892 case NATIVE_WINDOW_DATASPACE: {
893 *value = static_cast<int>(mDataSpace);
894 return NO_ERROR;
895 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800896 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700897 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800898 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800899}
900
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800901int Surface::perform(int operation, va_list args)
902{
903 int res = NO_ERROR;
904 switch (operation) {
905 case NATIVE_WINDOW_CONNECT:
906 // deprecated. must return NO_ERROR.
907 break;
908 case NATIVE_WINDOW_DISCONNECT:
909 // deprecated. must return NO_ERROR.
910 break;
911 case NATIVE_WINDOW_SET_USAGE:
912 res = dispatchSetUsage(args);
913 break;
914 case NATIVE_WINDOW_SET_CROP:
915 res = dispatchSetCrop(args);
916 break;
917 case NATIVE_WINDOW_SET_BUFFER_COUNT:
918 res = dispatchSetBufferCount(args);
919 break;
920 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
921 res = dispatchSetBuffersGeometry(args);
922 break;
923 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
924 res = dispatchSetBuffersTransform(args);
925 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700926 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
927 res = dispatchSetBuffersStickyTransform(args);
928 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800929 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
930 res = dispatchSetBuffersTimestamp(args);
931 break;
932 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
933 res = dispatchSetBuffersDimensions(args);
934 break;
935 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
936 res = dispatchSetBuffersUserDimensions(args);
937 break;
938 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
939 res = dispatchSetBuffersFormat(args);
940 break;
941 case NATIVE_WINDOW_LOCK:
942 res = dispatchLock(args);
943 break;
944 case NATIVE_WINDOW_UNLOCK_AND_POST:
945 res = dispatchUnlockAndPost(args);
946 break;
947 case NATIVE_WINDOW_SET_SCALING_MODE:
948 res = dispatchSetScalingMode(args);
949 break;
950 case NATIVE_WINDOW_API_CONNECT:
951 res = dispatchConnect(args);
952 break;
953 case NATIVE_WINDOW_API_DISCONNECT:
954 res = dispatchDisconnect(args);
955 break;
Rachad7cb0d392014-07-29 17:53:53 -0700956 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
957 res = dispatchSetSidebandStream(args);
958 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800959 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
960 res = dispatchSetBuffersDataSpace(args);
961 break;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700962 case NATIVE_WINDOW_SET_BUFFERS_SMPTE2086_METADATA:
963 res = dispatchSetBuffersSmpte2086Metadata(args);
964 break;
965 case NATIVE_WINDOW_SET_BUFFERS_CTA861_3_METADATA:
966 res = dispatchSetBuffersCta8613Metadata(args);
967 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700968 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
969 res = dispatchSetSurfaceDamage(args);
970 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700971 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
972 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700973 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800974 case NATIVE_WINDOW_SET_AUTO_REFRESH:
975 res = dispatchSetAutoRefresh(args);
976 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800977 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
978 res = dispatchGetDisplayRefreshCycleDuration(args);
979 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800980 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
981 res = dispatchGetNextFrameId(args);
982 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700983 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
984 res = dispatchEnableFrameTimestamps(args);
985 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800986 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
987 res = dispatchGetCompositorTiming(args);
988 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800989 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
990 res = dispatchGetFrameTimestamps(args);
991 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700992 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
993 res = dispatchGetWideColorSupport(args);
994 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700995 case NATIVE_WINDOW_GET_HDR_SUPPORT:
996 res = dispatchGetHdrSupport(args);
997 break;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700998 case NATIVE_WINDOW_SET_USAGE64:
999 res = dispatchSetUsage64(args);
1000 break;
Chia-I Wue2786ea2017-08-07 10:36:08 -07001001 case NATIVE_WINDOW_GET_CONSUMER_USAGE64:
1002 res = dispatchGetConsumerUsage64(args);
1003 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001004 default:
1005 res = NAME_NOT_FOUND;
1006 break;
1007 }
1008 return res;
1009}
Mathias Agopiana138f892010-05-21 17:24:35 -07001010
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001011int Surface::dispatchConnect(va_list args) {
1012 int api = va_arg(args, int);
1013 return connect(api);
1014}
Mathias Agopian55fa2512010-03-11 15:06:54 -08001015
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001016int Surface::dispatchDisconnect(va_list args) {
1017 int api = va_arg(args, int);
1018 return disconnect(api);
1019}
1020
1021int Surface::dispatchSetUsage(va_list args) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001022 uint64_t usage = va_arg(args, uint32_t);
1023 return setUsage(usage);
1024}
1025
1026int Surface::dispatchSetUsage64(va_list args) {
1027 uint64_t usage = va_arg(args, uint64_t);
1028 return setUsage(usage);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001029}
1030
1031int Surface::dispatchSetCrop(va_list args) {
1032 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
1033 return setCrop(reinterpret_cast<Rect const*>(rect));
1034}
1035
1036int Surface::dispatchSetBufferCount(va_list args) {
1037 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001038 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001039}
1040
1041int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001042 uint32_t width = va_arg(args, uint32_t);
1043 uint32_t height = va_arg(args, uint32_t);
1044 PixelFormat format = va_arg(args, PixelFormat);
1045 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001046 if (err != 0) {
1047 return err;
1048 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001049 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001050}
1051
1052int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001053 uint32_t width = va_arg(args, uint32_t);
1054 uint32_t height = va_arg(args, uint32_t);
1055 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001056}
1057
1058int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001059 uint32_t width = va_arg(args, uint32_t);
1060 uint32_t height = va_arg(args, uint32_t);
1061 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001062}
1063
1064int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001065 PixelFormat format = va_arg(args, PixelFormat);
1066 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001067}
1068
1069int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001070 int mode = va_arg(args, int);
1071 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001072}
1073
1074int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001075 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001076 return setBuffersTransform(transform);
1077}
1078
Ruben Brunk1681d952014-06-27 15:51:55 -07001079int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001080 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -07001081 return setBuffersStickyTransform(transform);
1082}
1083
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001084int Surface::dispatchSetBuffersTimestamp(va_list args) {
1085 int64_t timestamp = va_arg(args, int64_t);
1086 return setBuffersTimestamp(timestamp);
1087}
1088
1089int Surface::dispatchLock(va_list args) {
1090 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1091 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1092 return lock(outBuffer, inOutDirtyBounds);
1093}
1094
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001095int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001096 return unlockAndPost();
1097}
1098
Rachad7cb0d392014-07-29 17:53:53 -07001099int Surface::dispatchSetSidebandStream(va_list args) {
1100 native_handle_t* sH = va_arg(args, native_handle_t*);
1101 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1102 setSidebandStream(sidebandHandle);
1103 return OK;
1104}
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001105
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001106int Surface::dispatchSetBuffersDataSpace(va_list args) {
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001107 Dataspace dataspace = static_cast<Dataspace>(va_arg(args, int));
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001108 return setBuffersDataSpace(dataspace);
1109}
1110
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001111int Surface::dispatchSetBuffersSmpte2086Metadata(va_list args) {
1112 const android_smpte2086_metadata* metadata =
1113 va_arg(args, const android_smpte2086_metadata*);
1114 return setBuffersSmpte2086Metadata(metadata);
1115}
1116
1117int Surface::dispatchSetBuffersCta8613Metadata(va_list args) {
1118 const android_cta861_3_metadata* metadata =
1119 va_arg(args, const android_cta861_3_metadata*);
1120 return setBuffersCta8613Metadata(metadata);
1121}
1122
Dan Stoza5065a552015-03-17 16:23:42 -07001123int Surface::dispatchSetSurfaceDamage(va_list args) {
1124 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1125 size_t numRects = va_arg(args, size_t);
1126 setSurfaceDamage(rects, numRects);
1127 return NO_ERROR;
1128}
1129
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001130int Surface::dispatchSetSharedBufferMode(va_list args) {
1131 bool sharedBufferMode = va_arg(args, int);
1132 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001133}
1134
1135int Surface::dispatchSetAutoRefresh(va_list args) {
1136 bool autoRefresh = va_arg(args, int);
1137 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001138}
1139
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001140int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1141 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1142 return getDisplayRefreshCycleDuration(outRefreshDuration);
1143}
1144
Brian Anderson1049d1d2016-12-16 17:25:57 -08001145int Surface::dispatchGetNextFrameId(va_list args) {
1146 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1147 *nextFrameId = getNextFrameNumber();
1148 return NO_ERROR;
1149}
1150
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001151int Surface::dispatchEnableFrameTimestamps(va_list args) {
1152 bool enable = va_arg(args, int);
1153 enableFrameTimestamps(enable);
1154 return NO_ERROR;
1155}
1156
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001157int Surface::dispatchGetCompositorTiming(va_list args) {
1158 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1159 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1160 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1161 return getCompositorTiming(compositeDeadline, compositeInterval,
1162 compositeToPresentLatency);
1163}
1164
Pablo Ceballosce796e72016-02-04 19:10:51 -08001165int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001166 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001167 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001168 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001169 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1170 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1171 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001172 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001173 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001174 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001175 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001176 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001177 outRequestedPresentTime, outAcquireTime, outLatchTime,
1178 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001179 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001180 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001181}
1182
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001183int Surface::dispatchGetWideColorSupport(va_list args) {
1184 bool* outSupport = va_arg(args, bool*);
1185 return getWideColorSupport(outSupport);
1186}
1187
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001188int Surface::dispatchGetHdrSupport(va_list args) {
1189 bool* outSupport = va_arg(args, bool*);
1190 return getHdrSupport(outSupport);
1191}
1192
Chia-I Wue2786ea2017-08-07 10:36:08 -07001193int Surface::dispatchGetConsumerUsage64(va_list args) {
1194 uint64_t* usage = va_arg(args, uint64_t*);
1195 return getConsumerUsage(usage);
1196}
1197
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001198int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001199 static sp<IProducerListener> listener = new DummyProducerListener();
1200 return connect(api, listener);
1201}
1202
1203int Surface::connect(int api, const sp<IProducerListener>& listener) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001204 return connect(api, listener, false);
1205}
1206
1207int Surface::connect(
1208 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001209 ATRACE_CALL();
1210 ALOGV("Surface::connect");
1211 Mutex::Autolock lock(mMutex);
1212 IGraphicBufferProducer::QueueBufferOutput output;
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001213 mReportRemovedBuffers = reportBufferRemoval;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001214 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001215 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001216 mDefaultWidth = output.width;
1217 mDefaultHeight = output.height;
1218 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001219
1220 // Disable transform hint if sticky transform is set.
1221 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001222 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001223 }
1224
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001225 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001226 }
1227 if (!err && api == NATIVE_WINDOW_API_CPU) {
1228 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001229 // Clear the dirty region in case we're switching from a non-CPU API
1230 mDirtyRegion.clear();
1231 } else if (!err) {
1232 // Initialize the dirty region for tracking surface damage
1233 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001234 }
Dan Stoza5065a552015-03-17 16:23:42 -07001235
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001236 return err;
1237}
1238
Mathias Agopian365857d2013-09-11 19:35:45 -07001239
Robert Carr97b9c862016-09-08 13:54:35 -07001240int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001241 ATRACE_CALL();
1242 ALOGV("Surface::disconnect");
1243 Mutex::Autolock lock(mMutex);
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001244 mRemovedBuffers.clear();
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001245 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1246 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001247 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001248 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001249 if (!err) {
1250 mReqFormat = 0;
1251 mReqWidth = 0;
1252 mReqHeight = 0;
1253 mReqUsage = 0;
1254 mCrop.clear();
1255 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1256 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001257 mStickyTransform = 0;
1258
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001259 if (api == NATIVE_WINDOW_API_CPU) {
1260 mConnectedToCpu = false;
1261 }
1262 }
1263 return err;
1264}
1265
Dan Stozad9c49712015-04-27 11:06:01 -07001266int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001267 sp<Fence>* outFence) {
1268 ATRACE_CALL();
1269 ALOGV("Surface::detachNextBuffer");
1270
1271 if (outBuffer == NULL || outFence == NULL) {
1272 return BAD_VALUE;
1273 }
1274
1275 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001276 if (mReportRemovedBuffers) {
1277 mRemovedBuffers.clear();
1278 }
Dan Stoza231832e2015-03-11 11:55:01 -07001279
1280 sp<GraphicBuffer> buffer(NULL);
1281 sp<Fence> fence(NULL);
1282 status_t result = mGraphicBufferProducer->detachNextBuffer(
1283 &buffer, &fence);
1284 if (result != NO_ERROR) {
1285 return result;
1286 }
1287
Dan Stozad9c49712015-04-27 11:06:01 -07001288 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001289 if (fence != NULL && fence->isValid()) {
1290 *outFence = fence;
1291 } else {
1292 *outFence = Fence::NO_FENCE;
1293 }
1294
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001295 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1296 if (mSlots[i].buffer != NULL &&
Shuzhen Wang55be5052017-05-23 09:41:54 -07001297 mSlots[i].buffer->getId() == buffer->getId()) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001298 if (mReportRemovedBuffers) {
1299 mRemovedBuffers.push_back(mSlots[i].buffer);
1300 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001301 mSlots[i].buffer = NULL;
1302 }
1303 }
1304
Dan Stoza231832e2015-03-11 11:55:01 -07001305 return NO_ERROR;
1306}
1307
1308int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1309{
1310 ATRACE_CALL();
1311 ALOGV("Surface::attachBuffer");
1312
1313 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001314 if (mReportRemovedBuffers) {
1315 mRemovedBuffers.clear();
1316 }
Dan Stoza231832e2015-03-11 11:55:01 -07001317
1318 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001319 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1320 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001321 int32_t attachedSlot = -1;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001322 status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer);
Dan Stoza231832e2015-03-11 11:55:01 -07001323 if (result != NO_ERROR) {
1324 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001325 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001326 return result;
1327 }
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001328 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001329 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1330 }
Dan Stoza231832e2015-03-11 11:55:01 -07001331 mSlots[attachedSlot].buffer = graphicBuffer;
1332
1333 return NO_ERROR;
1334}
1335
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001336int Surface::setUsage(uint64_t reqUsage)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001337{
1338 ALOGV("Surface::setUsage");
1339 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001340 if (reqUsage != mReqUsage) {
1341 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1342 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001343 mReqUsage = reqUsage;
1344 return OK;
1345}
1346
1347int Surface::setCrop(Rect const* rect)
1348{
1349 ATRACE_CALL();
1350
Pablo Ceballos60d69222015-08-07 14:47:20 -07001351 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001352 if (rect == NULL || rect->isEmpty()) {
1353 realRect.clear();
1354 } else {
1355 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001356 }
1357
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001358 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1359 realRect.left, realRect.top, realRect.right, realRect.bottom);
1360
1361 Mutex::Autolock lock(mMutex);
1362 mCrop = realRect;
1363 return NO_ERROR;
1364}
1365
1366int Surface::setBufferCount(int bufferCount)
1367{
1368 ATRACE_CALL();
1369 ALOGV("Surface::setBufferCount");
1370 Mutex::Autolock lock(mMutex);
1371
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001372 status_t err = NO_ERROR;
1373 if (bufferCount == 0) {
1374 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1375 } else {
1376 int minUndequeuedBuffers = 0;
1377 err = mGraphicBufferProducer->query(
1378 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1379 if (err == NO_ERROR) {
1380 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1381 bufferCount - minUndequeuedBuffers);
1382 }
1383 }
Mathias Agopian90147262010-01-22 11:47:55 -08001384
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001385 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1386 bufferCount, strerror(-err));
1387
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001388 return err;
1389}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001390
Pablo Ceballosfa455352015-08-12 17:47:47 -07001391int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1392 ATRACE_CALL();
1393 ALOGV("Surface::setMaxDequeuedBufferCount");
1394 Mutex::Autolock lock(mMutex);
1395
1396 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1397 maxDequeuedBuffers);
1398 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1399 "returned %s", maxDequeuedBuffers, strerror(-err));
1400
Pablo Ceballosfa455352015-08-12 17:47:47 -07001401 return err;
1402}
1403
1404int Surface::setAsyncMode(bool async) {
1405 ATRACE_CALL();
1406 ALOGV("Surface::setAsyncMode");
1407 Mutex::Autolock lock(mMutex);
1408
1409 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1410 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1411 async, strerror(-err));
1412
Pablo Ceballosfa455352015-08-12 17:47:47 -07001413 return err;
1414}
1415
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001416int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001417 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001418 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001419 Mutex::Autolock lock(mMutex);
1420
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001421 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1422 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001423 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001424 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001425 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001426 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1427 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001428
1429 return err;
1430}
1431
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001432int Surface::setAutoRefresh(bool autoRefresh) {
1433 ATRACE_CALL();
1434 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1435 Mutex::Autolock lock(mMutex);
1436
1437 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1438 if (err == NO_ERROR) {
1439 mAutoRefresh = autoRefresh;
1440 }
1441 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1442 autoRefresh, strerror(-err));
1443 return err;
1444}
1445
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001446int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001447{
1448 ATRACE_CALL();
1449 ALOGV("Surface::setBuffersDimensions");
1450
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001451 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001452 return BAD_VALUE;
1453
1454 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001455 if (width != mReqWidth || height != mReqHeight) {
1456 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1457 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001458 mReqWidth = width;
1459 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001460 return NO_ERROR;
1461}
1462
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001463int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001464{
1465 ATRACE_CALL();
1466 ALOGV("Surface::setBuffersUserDimensions");
1467
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001468 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001469 return BAD_VALUE;
1470
1471 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001472 if (width != mUserWidth || height != mUserHeight) {
1473 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1474 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001475 mUserWidth = width;
1476 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001477 return NO_ERROR;
1478}
1479
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001480int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001481{
1482 ALOGV("Surface::setBuffersFormat");
1483
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001484 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001485 if (format != mReqFormat) {
1486 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1487 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001488 mReqFormat = format;
1489 return NO_ERROR;
1490}
1491
1492int Surface::setScalingMode(int mode)
1493{
1494 ATRACE_CALL();
1495 ALOGV("Surface::setScalingMode(%d)", mode);
1496
1497 switch (mode) {
1498 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1499 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1500 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001501 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001502 break;
1503 default:
1504 ALOGE("unknown scaling mode: %d", mode);
1505 return BAD_VALUE;
1506 }
1507
1508 Mutex::Autolock lock(mMutex);
1509 mScalingMode = mode;
1510 return NO_ERROR;
1511}
1512
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001513int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001514{
1515 ATRACE_CALL();
1516 ALOGV("Surface::setBuffersTransform");
1517 Mutex::Autolock lock(mMutex);
1518 mTransform = transform;
1519 return NO_ERROR;
1520}
1521
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001522int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001523{
1524 ATRACE_CALL();
1525 ALOGV("Surface::setBuffersStickyTransform");
1526 Mutex::Autolock lock(mMutex);
1527 mStickyTransform = transform;
1528 return NO_ERROR;
1529}
1530
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001531int Surface::setBuffersTimestamp(int64_t timestamp)
1532{
1533 ALOGV("Surface::setBuffersTimestamp");
1534 Mutex::Autolock lock(mMutex);
1535 mTimestamp = timestamp;
1536 return NO_ERROR;
1537}
1538
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001539int Surface::setBuffersDataSpace(Dataspace dataSpace)
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001540{
1541 ALOGV("Surface::setBuffersDataSpace");
1542 Mutex::Autolock lock(mMutex);
1543 mDataSpace = dataSpace;
1544 return NO_ERROR;
1545}
1546
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001547int Surface::setBuffersSmpte2086Metadata(const android_smpte2086_metadata* metadata) {
1548 ALOGV("Surface::setBuffersSmpte2086Metadata");
1549 Mutex::Autolock lock(mMutex);
1550 if (metadata) {
1551 mHdrMetadata.smpte2086 = *metadata;
1552 mHdrMetadata.validTypes |= HdrMetadata::SMPTE2086;
1553 } else {
1554 mHdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086;
1555 }
1556 return NO_ERROR;
1557}
1558
1559int Surface::setBuffersCta8613Metadata(const android_cta861_3_metadata* metadata) {
1560 ALOGV("Surface::setBuffersCta8613Metadata");
1561 Mutex::Autolock lock(mMutex);
1562 if (metadata) {
1563 mHdrMetadata.cta8613 = *metadata;
1564 mHdrMetadata.validTypes |= HdrMetadata::CTA861_3;
1565 } else {
1566 mHdrMetadata.validTypes &= ~HdrMetadata::CTA861_3;
1567 }
1568 return NO_ERROR;
1569}
1570
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001571Dataspace Surface::getBuffersDataSpace() {
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -06001572 ALOGV("Surface::getBuffersDataSpace");
1573 Mutex::Autolock lock(mMutex);
1574 return mDataSpace;
1575}
1576
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001577void Surface::freeAllBuffers() {
1578 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1579 mSlots[i].buffer = 0;
1580 }
1581}
1582
Dan Stoza5065a552015-03-17 16:23:42 -07001583void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1584 ATRACE_CALL();
1585 ALOGV("Surface::setSurfaceDamage");
1586 Mutex::Autolock lock(mMutex);
1587
Dan Stozac62acbd2015-04-21 16:42:49 -07001588 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001589 mDirtyRegion = Region::INVALID_REGION;
1590 return;
1591 }
1592
1593 mDirtyRegion.clear();
1594 for (size_t r = 0; r < numRects; ++r) {
1595 // We intentionally flip top and bottom here, since because they're
1596 // specified with a bottom-left origin, top > bottom, which fails
1597 // validation in the Region class. We will fix this up when we flip to a
1598 // top-left origin in queueBuffer.
1599 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1600 mDirtyRegion.orSelf(rect);
1601 }
1602}
1603
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001604// ----------------------------------------------------------------------
1605// the lock/unlock APIs must be used from the same thread
1606
1607static status_t copyBlt(
1608 const sp<GraphicBuffer>& dst,
1609 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001610 const Region& reg,
1611 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001612{
Dennis Kempinadd458f2017-05-17 19:16:31 -07001613 if (dst->getId() == src->getId())
1614 return OK;
1615
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001616 // src and dst with, height and format must be identical. no verification
1617 // is done here.
1618 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001619 uint8_t* src_bits = NULL;
1620 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1621 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001622 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1623
1624 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001625 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1626 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001627 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001628 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001629
1630 Region::const_iterator head(reg.begin());
1631 Region::const_iterator tail(reg.end());
1632 if (head != tail && src_bits && dst_bits) {
1633 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001634 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1635 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001636
1637 while (head != tail) {
1638 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001639 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001640 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001641 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1642 uint8_t const * s = src_bits +
1643 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1644 uint8_t * d = dst_bits +
1645 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001646 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001647 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001648 h = 1;
1649 }
1650 do {
1651 memcpy(d, s, size);
1652 d += dbpr;
1653 s += sbpr;
1654 } while (--h > 0);
1655 }
1656 }
1657
1658 if (src_bits)
1659 src->unlock();
1660
1661 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001662 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001663
1664 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665}
1666
Mathias Agopiana138f892010-05-21 17:24:35 -07001667// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001668
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001669status_t Surface::lock(
1670 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1671{
1672 if (mLockedBuffer != 0) {
1673 ALOGE("Surface::lock failed, already locked");
1674 return INVALID_OPERATION;
1675 }
1676
1677 if (!mConnectedToCpu) {
1678 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1679 if (err) {
1680 return err;
1681 }
1682 // we're intending to do software rendering from this point
1683 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1684 }
1685
1686 ANativeWindowBuffer* out;
1687 int fenceFd = -1;
1688 status_t err = dequeueBuffer(&out, &fenceFd);
1689 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1690 if (err == NO_ERROR) {
1691 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001692 const Rect bounds(backBuffer->width, backBuffer->height);
1693
1694 Region newDirtyRegion;
1695 if (inOutDirtyBounds) {
1696 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1697 newDirtyRegion.andSelf(bounds);
1698 } else {
1699 newDirtyRegion.set(bounds);
1700 }
1701
1702 // figure out if we can copy the frontbuffer back
1703 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1704 const bool canCopyBack = (frontBuffer != 0 &&
1705 backBuffer->width == frontBuffer->width &&
1706 backBuffer->height == frontBuffer->height &&
1707 backBuffer->format == frontBuffer->format);
1708
1709 if (canCopyBack) {
1710 // copy the area that is invalid and not repainted this round
1711 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001712 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001713 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001714 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001715 } else {
1716 // if we can't copy-back anything, modify the user's dirty
1717 // region to make sure they redraw the whole buffer
1718 newDirtyRegion.set(bounds);
1719 mDirtyRegion.clear();
1720 Mutex::Autolock lock(mMutex);
1721 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1722 mSlots[i].dirtyRegion.clear();
1723 }
1724 }
1725
1726
1727 { // scope for the lock
1728 Mutex::Autolock lock(mMutex);
1729 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1730 if (backBufferSlot >= 0) {
1731 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1732 mDirtyRegion.subtract(dirtyRegion);
1733 dirtyRegion = newDirtyRegion;
1734 }
1735 }
1736
1737 mDirtyRegion.orSelf(newDirtyRegion);
1738 if (inOutDirtyBounds) {
1739 *inOutDirtyBounds = newDirtyRegion.getBounds();
1740 }
1741
1742 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001743 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001744 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001745 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001746
1747 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1748 backBuffer->handle);
1749
1750 if (res != 0) {
1751 err = INVALID_OPERATION;
1752 } else {
1753 mLockedBuffer = backBuffer;
1754 outBuffer->width = backBuffer->width;
1755 outBuffer->height = backBuffer->height;
1756 outBuffer->stride = backBuffer->stride;
1757 outBuffer->format = backBuffer->format;
1758 outBuffer->bits = vaddr;
1759 }
1760 }
1761 return err;
1762}
1763
1764status_t Surface::unlockAndPost()
1765{
1766 if (mLockedBuffer == 0) {
1767 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1768 return INVALID_OPERATION;
1769 }
1770
Francis Hart8f396012014-04-01 15:30:53 +03001771 int fd = -1;
1772 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001773 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1774
Francis Hart8f396012014-04-01 15:30:53 +03001775 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001776 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1777 mLockedBuffer->handle, strerror(-err));
1778
1779 mPostedBuffer = mLockedBuffer;
1780 mLockedBuffer = 0;
1781 return err;
1782}
1783
Robert Carr9f31e292016-04-11 11:15:32 -07001784bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1785 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001786 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001787 return true;
1788 }
1789 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1790}
1791
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001792status_t Surface::getUniqueId(uint64_t* outId) const {
1793 Mutex::Autolock lock(mMutex);
1794 return mGraphicBufferProducer->getUniqueId(outId);
1795}
1796
Chia-I Wue2786ea2017-08-07 10:36:08 -07001797int Surface::getConsumerUsage(uint64_t* outUsage) const {
1798 Mutex::Autolock lock(mMutex);
1799 return mGraphicBufferProducer->getConsumerUsage(outUsage);
1800}
1801
Dan Stoza932f0082017-05-31 13:50:16 -07001802nsecs_t Surface::getLastDequeueStartTime() const {
1803 Mutex::Autolock lock(mMutex);
1804 return mLastDequeueStartTime;
1805}
1806
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001807status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1808 if (out == nullptr) {
1809 ALOGE("%s: out must not be null!", __FUNCTION__);
1810 return BAD_VALUE;
1811 }
1812
1813 Mutex::Autolock lock(mMutex);
1814 *out = mRemovedBuffers;
1815 mRemovedBuffers.clear();
1816 return OK;
1817}
1818
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001819status_t Surface::attachAndQueueBuffer(Surface* surface, sp<GraphicBuffer> buffer) {
1820 if (buffer == nullptr) {
1821 return BAD_VALUE;
1822 }
1823 int err = static_cast<ANativeWindow*>(surface)->perform(surface, NATIVE_WINDOW_API_CONNECT,
1824 NATIVE_WINDOW_API_CPU);
1825 if (err != OK) {
1826 return err;
1827 }
1828 err = surface->attachBuffer(buffer->getNativeBuffer());
1829 if (err != OK) {
1830 return err;
1831 }
1832 err = static_cast<ANativeWindow*>(surface)->queueBuffer(surface, buffer->getNativeBuffer(), -1);
1833 if (err != OK) {
1834 return err;
1835 }
1836 err = surface->disconnect(NATIVE_WINDOW_API_CPU);
1837 return err;
1838}
1839
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001840}; // namespace android