blob: 339bd0fa4e41c2beba65edec1f3308e0c2abc48d [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
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700467 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700468 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800469
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700470 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800471}
472
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700473int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800474 ATRACE_CALL();
475 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800476
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800477 uint32_t reqWidth;
478 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800479 PixelFormat reqFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700480 uint64_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700481 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800482
483 {
484 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700485 if (mReportRemovedBuffers) {
486 mRemovedBuffers.clear();
487 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800488
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800489 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
490 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800491
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800492 reqFormat = mReqFormat;
493 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800494
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700495 enableFrameTimestamps = mEnableFrameTimestamps;
496
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700497 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800498 BufferItem::INVALID_BUFFER_SLOT) {
499 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
500 if (gbuf != NULL) {
501 *buffer = gbuf.get();
502 *fenceFd = -1;
503 return OK;
504 }
505 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800506 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
507
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800508 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800509 sp<Fence> fence;
Dan Stoza932f0082017-05-31 13:50:16 -0700510 nsecs_t startTime = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700511
512 FrameEventHistoryDelta frameTimestamps;
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600513 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, reqWidth, reqHeight,
514 reqFormat, reqUsage, &mBufferAge,
515 enableFrameTimestamps ? &frameTimestamps
516 : nullptr);
Dan Stoza932f0082017-05-31 13:50:16 -0700517 mLastDequeueDuration = systemTime() - startTime;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800518
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800519 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700520 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700521 "(%d, %d, %d, %#" PRIx64 ") failed: %d",
522 reqWidth, reqHeight, reqFormat, reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800523 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700524 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800525
Dan Stoza90ce2a92017-05-01 16:31:53 -0700526 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
527 ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf);
528 android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging
529 return FAILED_TRANSACTION;
530 }
531
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800532 Mutex::Autolock lock(mMutex);
533
Dan Stoza932f0082017-05-31 13:50:16 -0700534 // Write this while holding the mutex
535 mLastDequeueStartTime = startTime;
536
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800537 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700538
539 // this should never happen
540 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
541
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800542 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
543 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700544 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700545
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700546 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700547 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700548 }
549
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700550 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
551 if (mReportRemovedBuffers && (gbuf != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700552 mRemovedBuffers.push_back(gbuf);
553 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800554 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
555 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700556 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700557 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800558 return result;
559 }
560 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700561
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800562 if (fence->isValid()) {
563 *fenceFd = fence->dup();
564 if (*fenceFd == -1) {
565 ALOGE("dequeueBuffer: error duping fence: %d", errno);
566 // dup() should never fail; something is badly wrong. Soldier on
567 // and hope for the best; the worst that should happen is some
568 // visible corruption that lasts until the next frame.
569 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700570 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800571 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700572 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800573
574 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800575
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700576 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800577 mSharedBufferSlot = buf;
578 mSharedBufferHasBeenQueued = false;
579 } else if (mSharedBufferSlot == buf) {
580 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
581 mSharedBufferHasBeenQueued = false;
582 }
583
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800584 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700585}
586
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800587int Surface::cancelBuffer(android_native_buffer_t* buffer,
588 int fenceFd) {
589 ATRACE_CALL();
590 ALOGV("Surface::cancelBuffer");
591 Mutex::Autolock lock(mMutex);
592 int i = getSlotFromBufferLocked(buffer);
593 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900594 if (fenceFd >= 0) {
595 close(fenceFd);
596 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800597 return i;
598 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800599 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
600 if (fenceFd >= 0) {
601 close(fenceFd);
602 }
603 return OK;
604 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800605 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
606 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800607
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700608 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800609 mSharedBufferHasBeenQueued = true;
610 }
611
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800612 return OK;
613}
614
615int Surface::getSlotFromBufferLocked(
616 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800617 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
618 if (mSlots[i].buffer != NULL &&
619 mSlots[i].buffer->handle == buffer->handle) {
620 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700621 }
622 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800623 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
624 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700625}
626
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800627int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800628 ALOGV("Surface::lockBuffer");
629 Mutex::Autolock lock(mMutex);
630 return OK;
631}
Mathias Agopian631f3582010-05-25 17:51:34 -0700632
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800633int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
634 ATRACE_CALL();
635 ALOGV("Surface::queueBuffer");
636 Mutex::Autolock lock(mMutex);
637 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700638 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800639
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800640 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700641 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700642 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700643 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700644 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800645 } else {
646 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700647 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800648 int i = getSlotFromBufferLocked(buffer);
649 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900650 if (fenceFd >= 0) {
651 close(fenceFd);
652 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800653 return i;
654 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800655 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
656 if (fenceFd >= 0) {
657 close(fenceFd);
658 }
659 return OK;
660 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800663 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700664 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800665 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800667 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
668 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700669 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700670 static_cast<android_dataspace>(mDataSpace), crop, mScalingMode,
671 mTransform ^ mStickyTransform, fence, mStickyTransform,
672 mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700673
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700674 // we should send HDR metadata as needed if this becomes a bottleneck
675 input.setHdrMetadata(mHdrMetadata);
676
Dan Stozac62acbd2015-04-21 16:42:49 -0700677 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700678 input.setSurfaceDamage(Region::INVALID_REGION);
679 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700680 // Here we do two things:
681 // 1) The surface damage was specified using the OpenGL ES convention of
682 // the origin being in the bottom-left corner. Here we flip to the
683 // convention that the rest of the system uses (top-left corner) by
684 // subtracting all top/bottom coordinates from the buffer height.
685 // 2) If the buffer is coming in rotated (for example, because the EGL
686 // implementation is reacting to the transform hint coming back from
687 // SurfaceFlinger), the surface damage needs to be rotated the
688 // opposite direction, since it was generated assuming an unrotated
689 // buffer (the app doesn't know that the EGL implementation is
690 // reacting to the transform hint behind its back). The
691 // transformations in the switch statement below apply those
692 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
693
694 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700695 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700696 bool rotated90 = (mTransform ^ mStickyTransform) &
697 NATIVE_WINDOW_TRANSFORM_ROT_90;
698 if (rotated90) {
699 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700700 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700701
Dan Stoza5065a552015-03-17 16:23:42 -0700702 Region flippedRegion;
703 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700704 int left = rect.left;
705 int right = rect.right;
706 int top = height - rect.bottom; // Flip from OpenGL convention
707 int bottom = height - rect.top; // Flip from OpenGL convention
708 switch (mTransform ^ mStickyTransform) {
709 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
710 // Rotate 270 degrees
711 Rect flippedRect{top, width - right, bottom, width - left};
712 flippedRegion.orSelf(flippedRect);
713 break;
714 }
715 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
716 // Rotate 180 degrees
717 Rect flippedRect{width - right, height - bottom,
718 width - left, height - top};
719 flippedRegion.orSelf(flippedRect);
720 break;
721 }
722 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
723 // Rotate 90 degrees
724 Rect flippedRect{height - bottom, left,
725 height - top, right};
726 flippedRegion.orSelf(flippedRect);
727 break;
728 }
729 default: {
730 Rect flippedRect{left, top, right, bottom};
731 flippedRegion.orSelf(flippedRect);
732 break;
733 }
734 }
Dan Stoza5065a552015-03-17 16:23:42 -0700735 }
736
737 input.setSurfaceDamage(flippedRegion);
738 }
739
Dan Stoza70ccba52016-07-01 14:00:40 -0700740 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800741 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700742 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800743 if (err != OK) {
744 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
745 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800746
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700747 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700748 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700749 // Update timestamps with the local acquire fence.
750 // The consumer doesn't send it back to prevent us from having two
751 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700752 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700753 std::make_shared<FenceTime>(std::move(fence)));
754
755 // Cache timestamps of signaled fences so we can close their file
756 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700757 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700758 }
759
Brian Anderson50143b32016-09-30 14:01:24 -0700760 mLastFrameNumber = mNextFrameNumber;
761
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700762 mDefaultWidth = output.width;
763 mDefaultHeight = output.height;
764 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700765
Ruben Brunk1681d952014-06-27 15:51:55 -0700766 // Disable transform hint if sticky transform is set.
767 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700768 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700769 }
770
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700771 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700772
Dan Stozac62acbd2015-04-21 16:42:49 -0700773 if (!mConnectedToCpu) {
774 // Clear surface damage back to full-buffer
775 mDirtyRegion = Region::INVALID_REGION;
776 }
Dan Stoza5065a552015-03-17 16:23:42 -0700777
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700778 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800779 mSharedBufferHasBeenQueued = true;
780 }
781
Robert Carr9f31e292016-04-11 11:15:32 -0700782 mQueueBufferCondition.broadcast();
783
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800784 return err;
785}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700786
Brian Anderson6b376712017-04-04 10:51:39 -0700787void Surface::querySupportedTimestampsLocked() const {
788 // mMutex must be locked when calling this method.
789
790 if (mQueriedSupportedTimestamps) {
791 return;
792 }
793 mQueriedSupportedTimestamps = true;
794
795 std::vector<FrameEvent> supportedFrameTimestamps;
796 status_t err = composerService()->getSupportedFrameTimestamps(
797 &supportedFrameTimestamps);
798
799 if (err != NO_ERROR) {
800 return;
801 }
802
803 for (auto sft : supportedFrameTimestamps) {
804 if (sft == FrameEvent::DISPLAY_PRESENT) {
805 mFrameTimestampsSupportsPresent = true;
806 }
807 }
808}
809
Mathias Agopiana67932f2011-04-20 14:20:59 -0700810int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800811 ATRACE_CALL();
812 ALOGV("Surface::query");
813 { // scope for the lock
814 Mutex::Autolock lock(mMutex);
815 switch (what) {
816 case NATIVE_WINDOW_FORMAT:
817 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800818 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800819 return NO_ERROR;
820 }
821 break;
822 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700823 if (composerService()->authenticateSurfaceTexture(
824 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800825 *value = 1;
826 } else {
827 *value = 0;
828 }
829 return NO_ERROR;
830 }
831 case NATIVE_WINDOW_CONCRETE_TYPE:
832 *value = NATIVE_WINDOW_SURFACE;
833 return NO_ERROR;
834 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800835 *value = static_cast<int>(
836 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800837 return NO_ERROR;
838 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800839 *value = static_cast<int>(
840 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800841 return NO_ERROR;
842 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800843 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800844 return NO_ERROR;
845 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
846 status_t err = NO_ERROR;
847 if (!mConsumerRunningBehind) {
848 *value = 0;
849 } else {
850 err = mGraphicBufferProducer->query(what, value);
851 if (err == NO_ERROR) {
852 mConsumerRunningBehind = *value;
853 }
854 }
855 return err;
856 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600857 case NATIVE_WINDOW_BUFFER_AGE: {
858 if (mBufferAge > INT32_MAX) {
859 *value = 0;
860 } else {
861 *value = static_cast<int32_t>(mBufferAge);
862 }
863 return NO_ERROR;
864 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700865 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
866 int64_t durationUs = mLastDequeueDuration / 1000;
867 *value = durationUs > std::numeric_limits<int>::max() ?
868 std::numeric_limits<int>::max() :
869 static_cast<int>(durationUs);
870 return NO_ERROR;
871 }
872 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
873 int64_t durationUs = mLastQueueDuration / 1000;
874 *value = durationUs > std::numeric_limits<int>::max() ?
875 std::numeric_limits<int>::max() :
876 static_cast<int>(durationUs);
877 return NO_ERROR;
878 }
Brian Anderson6b376712017-04-04 10:51:39 -0700879 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
880 querySupportedTimestampsLocked();
881 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
882 return NO_ERROR;
883 }
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800884 case NATIVE_WINDOW_IS_VALID: {
885 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
886 return NO_ERROR;
887 }
Peiyong Lin654f87b2018-01-30 14:21:33 -0800888 case NATIVE_WINDOW_DATASPACE: {
889 *value = static_cast<int>(mDataSpace);
890 return NO_ERROR;
891 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800892 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700893 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800894 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800895}
896
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800897int Surface::perform(int operation, va_list args)
898{
899 int res = NO_ERROR;
900 switch (operation) {
901 case NATIVE_WINDOW_CONNECT:
902 // deprecated. must return NO_ERROR.
903 break;
904 case NATIVE_WINDOW_DISCONNECT:
905 // deprecated. must return NO_ERROR.
906 break;
907 case NATIVE_WINDOW_SET_USAGE:
908 res = dispatchSetUsage(args);
909 break;
910 case NATIVE_WINDOW_SET_CROP:
911 res = dispatchSetCrop(args);
912 break;
913 case NATIVE_WINDOW_SET_BUFFER_COUNT:
914 res = dispatchSetBufferCount(args);
915 break;
916 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
917 res = dispatchSetBuffersGeometry(args);
918 break;
919 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
920 res = dispatchSetBuffersTransform(args);
921 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700922 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
923 res = dispatchSetBuffersStickyTransform(args);
924 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800925 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
926 res = dispatchSetBuffersTimestamp(args);
927 break;
928 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
929 res = dispatchSetBuffersDimensions(args);
930 break;
931 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
932 res = dispatchSetBuffersUserDimensions(args);
933 break;
934 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
935 res = dispatchSetBuffersFormat(args);
936 break;
937 case NATIVE_WINDOW_LOCK:
938 res = dispatchLock(args);
939 break;
940 case NATIVE_WINDOW_UNLOCK_AND_POST:
941 res = dispatchUnlockAndPost(args);
942 break;
943 case NATIVE_WINDOW_SET_SCALING_MODE:
944 res = dispatchSetScalingMode(args);
945 break;
946 case NATIVE_WINDOW_API_CONNECT:
947 res = dispatchConnect(args);
948 break;
949 case NATIVE_WINDOW_API_DISCONNECT:
950 res = dispatchDisconnect(args);
951 break;
Rachad7cb0d392014-07-29 17:53:53 -0700952 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
953 res = dispatchSetSidebandStream(args);
954 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800955 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
956 res = dispatchSetBuffersDataSpace(args);
957 break;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700958 case NATIVE_WINDOW_SET_BUFFERS_SMPTE2086_METADATA:
959 res = dispatchSetBuffersSmpte2086Metadata(args);
960 break;
961 case NATIVE_WINDOW_SET_BUFFERS_CTA861_3_METADATA:
962 res = dispatchSetBuffersCta8613Metadata(args);
963 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700964 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
965 res = dispatchSetSurfaceDamage(args);
966 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700967 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
968 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700969 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800970 case NATIVE_WINDOW_SET_AUTO_REFRESH:
971 res = dispatchSetAutoRefresh(args);
972 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800973 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
974 res = dispatchGetDisplayRefreshCycleDuration(args);
975 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800976 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
977 res = dispatchGetNextFrameId(args);
978 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700979 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
980 res = dispatchEnableFrameTimestamps(args);
981 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800982 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
983 res = dispatchGetCompositorTiming(args);
984 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800985 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
986 res = dispatchGetFrameTimestamps(args);
987 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700988 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
989 res = dispatchGetWideColorSupport(args);
990 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700991 case NATIVE_WINDOW_GET_HDR_SUPPORT:
992 res = dispatchGetHdrSupport(args);
993 break;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700994 case NATIVE_WINDOW_SET_USAGE64:
995 res = dispatchSetUsage64(args);
996 break;
Chia-I Wue2786ea2017-08-07 10:36:08 -0700997 case NATIVE_WINDOW_GET_CONSUMER_USAGE64:
998 res = dispatchGetConsumerUsage64(args);
999 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001000 default:
1001 res = NAME_NOT_FOUND;
1002 break;
1003 }
1004 return res;
1005}
Mathias Agopiana138f892010-05-21 17:24:35 -07001006
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001007int Surface::dispatchConnect(va_list args) {
1008 int api = va_arg(args, int);
1009 return connect(api);
1010}
Mathias Agopian55fa2512010-03-11 15:06:54 -08001011
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001012int Surface::dispatchDisconnect(va_list args) {
1013 int api = va_arg(args, int);
1014 return disconnect(api);
1015}
1016
1017int Surface::dispatchSetUsage(va_list args) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001018 uint64_t usage = va_arg(args, uint32_t);
1019 return setUsage(usage);
1020}
1021
1022int Surface::dispatchSetUsage64(va_list args) {
1023 uint64_t usage = va_arg(args, uint64_t);
1024 return setUsage(usage);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001025}
1026
1027int Surface::dispatchSetCrop(va_list args) {
1028 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
1029 return setCrop(reinterpret_cast<Rect const*>(rect));
1030}
1031
1032int Surface::dispatchSetBufferCount(va_list args) {
1033 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001034 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001035}
1036
1037int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001038 uint32_t width = va_arg(args, uint32_t);
1039 uint32_t height = va_arg(args, uint32_t);
1040 PixelFormat format = va_arg(args, PixelFormat);
1041 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001042 if (err != 0) {
1043 return err;
1044 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001045 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001046}
1047
1048int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001049 uint32_t width = va_arg(args, uint32_t);
1050 uint32_t height = va_arg(args, uint32_t);
1051 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001052}
1053
1054int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001055 uint32_t width = va_arg(args, uint32_t);
1056 uint32_t height = va_arg(args, uint32_t);
1057 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001058}
1059
1060int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001061 PixelFormat format = va_arg(args, PixelFormat);
1062 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001063}
1064
1065int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001066 int mode = va_arg(args, int);
1067 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001068}
1069
1070int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001071 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001072 return setBuffersTransform(transform);
1073}
1074
Ruben Brunk1681d952014-06-27 15:51:55 -07001075int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001076 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -07001077 return setBuffersStickyTransform(transform);
1078}
1079
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001080int Surface::dispatchSetBuffersTimestamp(va_list args) {
1081 int64_t timestamp = va_arg(args, int64_t);
1082 return setBuffersTimestamp(timestamp);
1083}
1084
1085int Surface::dispatchLock(va_list args) {
1086 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1087 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1088 return lock(outBuffer, inOutDirtyBounds);
1089}
1090
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001091int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001092 return unlockAndPost();
1093}
1094
Rachad7cb0d392014-07-29 17:53:53 -07001095int Surface::dispatchSetSidebandStream(va_list args) {
1096 native_handle_t* sH = va_arg(args, native_handle_t*);
1097 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1098 setSidebandStream(sidebandHandle);
1099 return OK;
1100}
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001101
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001102int Surface::dispatchSetBuffersDataSpace(va_list args) {
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001103 Dataspace dataspace = static_cast<Dataspace>(va_arg(args, int));
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001104 return setBuffersDataSpace(dataspace);
1105}
1106
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001107int Surface::dispatchSetBuffersSmpte2086Metadata(va_list args) {
1108 const android_smpte2086_metadata* metadata =
1109 va_arg(args, const android_smpte2086_metadata*);
1110 return setBuffersSmpte2086Metadata(metadata);
1111}
1112
1113int Surface::dispatchSetBuffersCta8613Metadata(va_list args) {
1114 const android_cta861_3_metadata* metadata =
1115 va_arg(args, const android_cta861_3_metadata*);
1116 return setBuffersCta8613Metadata(metadata);
1117}
1118
Dan Stoza5065a552015-03-17 16:23:42 -07001119int Surface::dispatchSetSurfaceDamage(va_list args) {
1120 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1121 size_t numRects = va_arg(args, size_t);
1122 setSurfaceDamage(rects, numRects);
1123 return NO_ERROR;
1124}
1125
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001126int Surface::dispatchSetSharedBufferMode(va_list args) {
1127 bool sharedBufferMode = va_arg(args, int);
1128 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001129}
1130
1131int Surface::dispatchSetAutoRefresh(va_list args) {
1132 bool autoRefresh = va_arg(args, int);
1133 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001134}
1135
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001136int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1137 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1138 return getDisplayRefreshCycleDuration(outRefreshDuration);
1139}
1140
Brian Anderson1049d1d2016-12-16 17:25:57 -08001141int Surface::dispatchGetNextFrameId(va_list args) {
1142 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1143 *nextFrameId = getNextFrameNumber();
1144 return NO_ERROR;
1145}
1146
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001147int Surface::dispatchEnableFrameTimestamps(va_list args) {
1148 bool enable = va_arg(args, int);
1149 enableFrameTimestamps(enable);
1150 return NO_ERROR;
1151}
1152
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001153int Surface::dispatchGetCompositorTiming(va_list args) {
1154 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1155 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1156 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1157 return getCompositorTiming(compositeDeadline, compositeInterval,
1158 compositeToPresentLatency);
1159}
1160
Pablo Ceballosce796e72016-02-04 19:10:51 -08001161int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001162 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001163 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001164 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001165 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1166 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1167 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001168 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001169 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001170 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001171 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001172 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001173 outRequestedPresentTime, outAcquireTime, outLatchTime,
1174 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001175 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001176 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001177}
1178
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001179int Surface::dispatchGetWideColorSupport(va_list args) {
1180 bool* outSupport = va_arg(args, bool*);
1181 return getWideColorSupport(outSupport);
1182}
1183
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001184int Surface::dispatchGetHdrSupport(va_list args) {
1185 bool* outSupport = va_arg(args, bool*);
1186 return getHdrSupport(outSupport);
1187}
1188
Chia-I Wue2786ea2017-08-07 10:36:08 -07001189int Surface::dispatchGetConsumerUsage64(va_list args) {
1190 uint64_t* usage = va_arg(args, uint64_t*);
1191 return getConsumerUsage(usage);
1192}
1193
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001194int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001195 static sp<IProducerListener> listener = new DummyProducerListener();
1196 return connect(api, listener);
1197}
1198
1199int Surface::connect(int api, const sp<IProducerListener>& listener) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001200 return connect(api, listener, false);
1201}
1202
1203int Surface::connect(
1204 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001205 ATRACE_CALL();
1206 ALOGV("Surface::connect");
1207 Mutex::Autolock lock(mMutex);
1208 IGraphicBufferProducer::QueueBufferOutput output;
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001209 mReportRemovedBuffers = reportBufferRemoval;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001210 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001211 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001212 mDefaultWidth = output.width;
1213 mDefaultHeight = output.height;
1214 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001215
1216 // Disable transform hint if sticky transform is set.
1217 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001218 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001219 }
1220
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001221 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001222 }
1223 if (!err && api == NATIVE_WINDOW_API_CPU) {
1224 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001225 // Clear the dirty region in case we're switching from a non-CPU API
1226 mDirtyRegion.clear();
1227 } else if (!err) {
1228 // Initialize the dirty region for tracking surface damage
1229 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001230 }
Dan Stoza5065a552015-03-17 16:23:42 -07001231
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001232 return err;
1233}
1234
Mathias Agopian365857d2013-09-11 19:35:45 -07001235
Robert Carr97b9c862016-09-08 13:54:35 -07001236int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001237 ATRACE_CALL();
1238 ALOGV("Surface::disconnect");
1239 Mutex::Autolock lock(mMutex);
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001240 mRemovedBuffers.clear();
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001241 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1242 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001243 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001244 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001245 if (!err) {
1246 mReqFormat = 0;
1247 mReqWidth = 0;
1248 mReqHeight = 0;
1249 mReqUsage = 0;
1250 mCrop.clear();
1251 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1252 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001253 mStickyTransform = 0;
1254
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001255 if (api == NATIVE_WINDOW_API_CPU) {
1256 mConnectedToCpu = false;
1257 }
1258 }
1259 return err;
1260}
1261
Dan Stozad9c49712015-04-27 11:06:01 -07001262int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001263 sp<Fence>* outFence) {
1264 ATRACE_CALL();
1265 ALOGV("Surface::detachNextBuffer");
1266
1267 if (outBuffer == NULL || outFence == NULL) {
1268 return BAD_VALUE;
1269 }
1270
1271 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001272 if (mReportRemovedBuffers) {
1273 mRemovedBuffers.clear();
1274 }
Dan Stoza231832e2015-03-11 11:55:01 -07001275
1276 sp<GraphicBuffer> buffer(NULL);
1277 sp<Fence> fence(NULL);
1278 status_t result = mGraphicBufferProducer->detachNextBuffer(
1279 &buffer, &fence);
1280 if (result != NO_ERROR) {
1281 return result;
1282 }
1283
Dan Stozad9c49712015-04-27 11:06:01 -07001284 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001285 if (fence != NULL && fence->isValid()) {
1286 *outFence = fence;
1287 } else {
1288 *outFence = Fence::NO_FENCE;
1289 }
1290
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001291 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1292 if (mSlots[i].buffer != NULL &&
Shuzhen Wang55be5052017-05-23 09:41:54 -07001293 mSlots[i].buffer->getId() == buffer->getId()) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001294 if (mReportRemovedBuffers) {
1295 mRemovedBuffers.push_back(mSlots[i].buffer);
1296 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001297 mSlots[i].buffer = NULL;
1298 }
1299 }
1300
Dan Stoza231832e2015-03-11 11:55:01 -07001301 return NO_ERROR;
1302}
1303
1304int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1305{
1306 ATRACE_CALL();
1307 ALOGV("Surface::attachBuffer");
1308
1309 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001310 if (mReportRemovedBuffers) {
1311 mRemovedBuffers.clear();
1312 }
Dan Stoza231832e2015-03-11 11:55:01 -07001313
1314 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001315 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1316 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001317 int32_t attachedSlot = -1;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001318 status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer);
Dan Stoza231832e2015-03-11 11:55:01 -07001319 if (result != NO_ERROR) {
1320 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001321 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001322 return result;
1323 }
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001324 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001325 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1326 }
Dan Stoza231832e2015-03-11 11:55:01 -07001327 mSlots[attachedSlot].buffer = graphicBuffer;
1328
1329 return NO_ERROR;
1330}
1331
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001332int Surface::setUsage(uint64_t reqUsage)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001333{
1334 ALOGV("Surface::setUsage");
1335 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001336 if (reqUsage != mReqUsage) {
1337 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1338 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001339 mReqUsage = reqUsage;
1340 return OK;
1341}
1342
1343int Surface::setCrop(Rect const* rect)
1344{
1345 ATRACE_CALL();
1346
Pablo Ceballos60d69222015-08-07 14:47:20 -07001347 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001348 if (rect == NULL || rect->isEmpty()) {
1349 realRect.clear();
1350 } else {
1351 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001352 }
1353
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001354 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1355 realRect.left, realRect.top, realRect.right, realRect.bottom);
1356
1357 Mutex::Autolock lock(mMutex);
1358 mCrop = realRect;
1359 return NO_ERROR;
1360}
1361
1362int Surface::setBufferCount(int bufferCount)
1363{
1364 ATRACE_CALL();
1365 ALOGV("Surface::setBufferCount");
1366 Mutex::Autolock lock(mMutex);
1367
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001368 status_t err = NO_ERROR;
1369 if (bufferCount == 0) {
1370 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1371 } else {
1372 int minUndequeuedBuffers = 0;
1373 err = mGraphicBufferProducer->query(
1374 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1375 if (err == NO_ERROR) {
1376 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1377 bufferCount - minUndequeuedBuffers);
1378 }
1379 }
Mathias Agopian90147262010-01-22 11:47:55 -08001380
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001381 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1382 bufferCount, strerror(-err));
1383
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001384 return err;
1385}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001386
Pablo Ceballosfa455352015-08-12 17:47:47 -07001387int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1388 ATRACE_CALL();
1389 ALOGV("Surface::setMaxDequeuedBufferCount");
1390 Mutex::Autolock lock(mMutex);
1391
1392 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1393 maxDequeuedBuffers);
1394 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1395 "returned %s", maxDequeuedBuffers, strerror(-err));
1396
Pablo Ceballosfa455352015-08-12 17:47:47 -07001397 return err;
1398}
1399
1400int Surface::setAsyncMode(bool async) {
1401 ATRACE_CALL();
1402 ALOGV("Surface::setAsyncMode");
1403 Mutex::Autolock lock(mMutex);
1404
1405 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1406 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1407 async, strerror(-err));
1408
Pablo Ceballosfa455352015-08-12 17:47:47 -07001409 return err;
1410}
1411
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001412int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001413 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001414 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001415 Mutex::Autolock lock(mMutex);
1416
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001417 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1418 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001419 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001420 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001421 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001422 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1423 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001424
1425 return err;
1426}
1427
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001428int Surface::setAutoRefresh(bool autoRefresh) {
1429 ATRACE_CALL();
1430 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1431 Mutex::Autolock lock(mMutex);
1432
1433 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1434 if (err == NO_ERROR) {
1435 mAutoRefresh = autoRefresh;
1436 }
1437 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1438 autoRefresh, strerror(-err));
1439 return err;
1440}
1441
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001442int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001443{
1444 ATRACE_CALL();
1445 ALOGV("Surface::setBuffersDimensions");
1446
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001447 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001448 return BAD_VALUE;
1449
1450 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001451 if (width != mReqWidth || height != mReqHeight) {
1452 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1453 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001454 mReqWidth = width;
1455 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001456 return NO_ERROR;
1457}
1458
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001459int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001460{
1461 ATRACE_CALL();
1462 ALOGV("Surface::setBuffersUserDimensions");
1463
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001464 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001465 return BAD_VALUE;
1466
1467 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001468 if (width != mUserWidth || height != mUserHeight) {
1469 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1470 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001471 mUserWidth = width;
1472 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001473 return NO_ERROR;
1474}
1475
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001476int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001477{
1478 ALOGV("Surface::setBuffersFormat");
1479
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001480 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001481 if (format != mReqFormat) {
1482 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1483 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001484 mReqFormat = format;
1485 return NO_ERROR;
1486}
1487
1488int Surface::setScalingMode(int mode)
1489{
1490 ATRACE_CALL();
1491 ALOGV("Surface::setScalingMode(%d)", mode);
1492
1493 switch (mode) {
1494 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1495 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1496 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001497 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001498 break;
1499 default:
1500 ALOGE("unknown scaling mode: %d", mode);
1501 return BAD_VALUE;
1502 }
1503
1504 Mutex::Autolock lock(mMutex);
1505 mScalingMode = mode;
1506 return NO_ERROR;
1507}
1508
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001509int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001510{
1511 ATRACE_CALL();
1512 ALOGV("Surface::setBuffersTransform");
1513 Mutex::Autolock lock(mMutex);
1514 mTransform = transform;
1515 return NO_ERROR;
1516}
1517
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001518int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001519{
1520 ATRACE_CALL();
1521 ALOGV("Surface::setBuffersStickyTransform");
1522 Mutex::Autolock lock(mMutex);
1523 mStickyTransform = transform;
1524 return NO_ERROR;
1525}
1526
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001527int Surface::setBuffersTimestamp(int64_t timestamp)
1528{
1529 ALOGV("Surface::setBuffersTimestamp");
1530 Mutex::Autolock lock(mMutex);
1531 mTimestamp = timestamp;
1532 return NO_ERROR;
1533}
1534
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001535int Surface::setBuffersDataSpace(Dataspace dataSpace)
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001536{
1537 ALOGV("Surface::setBuffersDataSpace");
1538 Mutex::Autolock lock(mMutex);
1539 mDataSpace = dataSpace;
1540 return NO_ERROR;
1541}
1542
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001543int Surface::setBuffersSmpte2086Metadata(const android_smpte2086_metadata* metadata) {
1544 ALOGV("Surface::setBuffersSmpte2086Metadata");
1545 Mutex::Autolock lock(mMutex);
1546 if (metadata) {
1547 mHdrMetadata.smpte2086 = *metadata;
1548 mHdrMetadata.validTypes |= HdrMetadata::SMPTE2086;
1549 } else {
1550 mHdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086;
1551 }
1552 return NO_ERROR;
1553}
1554
1555int Surface::setBuffersCta8613Metadata(const android_cta861_3_metadata* metadata) {
1556 ALOGV("Surface::setBuffersCta8613Metadata");
1557 Mutex::Autolock lock(mMutex);
1558 if (metadata) {
1559 mHdrMetadata.cta8613 = *metadata;
1560 mHdrMetadata.validTypes |= HdrMetadata::CTA861_3;
1561 } else {
1562 mHdrMetadata.validTypes &= ~HdrMetadata::CTA861_3;
1563 }
1564 return NO_ERROR;
1565}
1566
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001567Dataspace Surface::getBuffersDataSpace() {
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -06001568 ALOGV("Surface::getBuffersDataSpace");
1569 Mutex::Autolock lock(mMutex);
1570 return mDataSpace;
1571}
1572
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001573void Surface::freeAllBuffers() {
1574 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1575 mSlots[i].buffer = 0;
1576 }
1577}
1578
Dan Stoza5065a552015-03-17 16:23:42 -07001579void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1580 ATRACE_CALL();
1581 ALOGV("Surface::setSurfaceDamage");
1582 Mutex::Autolock lock(mMutex);
1583
Dan Stozac62acbd2015-04-21 16:42:49 -07001584 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001585 mDirtyRegion = Region::INVALID_REGION;
1586 return;
1587 }
1588
1589 mDirtyRegion.clear();
1590 for (size_t r = 0; r < numRects; ++r) {
1591 // We intentionally flip top and bottom here, since because they're
1592 // specified with a bottom-left origin, top > bottom, which fails
1593 // validation in the Region class. We will fix this up when we flip to a
1594 // top-left origin in queueBuffer.
1595 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1596 mDirtyRegion.orSelf(rect);
1597 }
1598}
1599
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001600// ----------------------------------------------------------------------
1601// the lock/unlock APIs must be used from the same thread
1602
1603static status_t copyBlt(
1604 const sp<GraphicBuffer>& dst,
1605 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001606 const Region& reg,
1607 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001608{
Dennis Kempinadd458f2017-05-17 19:16:31 -07001609 if (dst->getId() == src->getId())
1610 return OK;
1611
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001612 // src and dst with, height and format must be identical. no verification
1613 // is done here.
1614 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001615 uint8_t* src_bits = NULL;
1616 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1617 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001618 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1619
1620 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001621 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1622 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001623 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001624 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001625
1626 Region::const_iterator head(reg.begin());
1627 Region::const_iterator tail(reg.end());
1628 if (head != tail && src_bits && dst_bits) {
1629 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001630 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1631 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001632
1633 while (head != tail) {
1634 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001635 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001636 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001637 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1638 uint8_t const * s = src_bits +
1639 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1640 uint8_t * d = dst_bits +
1641 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001642 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001643 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001644 h = 1;
1645 }
1646 do {
1647 memcpy(d, s, size);
1648 d += dbpr;
1649 s += sbpr;
1650 } while (--h > 0);
1651 }
1652 }
1653
1654 if (src_bits)
1655 src->unlock();
1656
1657 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001658 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001659
1660 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661}
1662
Mathias Agopiana138f892010-05-21 17:24:35 -07001663// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001664
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001665status_t Surface::lock(
1666 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1667{
1668 if (mLockedBuffer != 0) {
1669 ALOGE("Surface::lock failed, already locked");
1670 return INVALID_OPERATION;
1671 }
1672
1673 if (!mConnectedToCpu) {
1674 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1675 if (err) {
1676 return err;
1677 }
1678 // we're intending to do software rendering from this point
1679 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1680 }
1681
1682 ANativeWindowBuffer* out;
1683 int fenceFd = -1;
1684 status_t err = dequeueBuffer(&out, &fenceFd);
1685 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1686 if (err == NO_ERROR) {
1687 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001688 const Rect bounds(backBuffer->width, backBuffer->height);
1689
1690 Region newDirtyRegion;
1691 if (inOutDirtyBounds) {
1692 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1693 newDirtyRegion.andSelf(bounds);
1694 } else {
1695 newDirtyRegion.set(bounds);
1696 }
1697
1698 // figure out if we can copy the frontbuffer back
1699 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1700 const bool canCopyBack = (frontBuffer != 0 &&
1701 backBuffer->width == frontBuffer->width &&
1702 backBuffer->height == frontBuffer->height &&
1703 backBuffer->format == frontBuffer->format);
1704
1705 if (canCopyBack) {
1706 // copy the area that is invalid and not repainted this round
1707 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001708 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001709 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001710 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001711 } else {
1712 // if we can't copy-back anything, modify the user's dirty
1713 // region to make sure they redraw the whole buffer
1714 newDirtyRegion.set(bounds);
1715 mDirtyRegion.clear();
1716 Mutex::Autolock lock(mMutex);
1717 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1718 mSlots[i].dirtyRegion.clear();
1719 }
1720 }
1721
1722
1723 { // scope for the lock
1724 Mutex::Autolock lock(mMutex);
1725 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1726 if (backBufferSlot >= 0) {
1727 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1728 mDirtyRegion.subtract(dirtyRegion);
1729 dirtyRegion = newDirtyRegion;
1730 }
1731 }
1732
1733 mDirtyRegion.orSelf(newDirtyRegion);
1734 if (inOutDirtyBounds) {
1735 *inOutDirtyBounds = newDirtyRegion.getBounds();
1736 }
1737
1738 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001739 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001740 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001741 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001742
1743 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1744 backBuffer->handle);
1745
1746 if (res != 0) {
1747 err = INVALID_OPERATION;
1748 } else {
1749 mLockedBuffer = backBuffer;
1750 outBuffer->width = backBuffer->width;
1751 outBuffer->height = backBuffer->height;
1752 outBuffer->stride = backBuffer->stride;
1753 outBuffer->format = backBuffer->format;
1754 outBuffer->bits = vaddr;
1755 }
1756 }
1757 return err;
1758}
1759
1760status_t Surface::unlockAndPost()
1761{
1762 if (mLockedBuffer == 0) {
1763 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1764 return INVALID_OPERATION;
1765 }
1766
Francis Hart8f396012014-04-01 15:30:53 +03001767 int fd = -1;
1768 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001769 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1770
Francis Hart8f396012014-04-01 15:30:53 +03001771 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001772 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1773 mLockedBuffer->handle, strerror(-err));
1774
1775 mPostedBuffer = mLockedBuffer;
1776 mLockedBuffer = 0;
1777 return err;
1778}
1779
Robert Carr9f31e292016-04-11 11:15:32 -07001780bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1781 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001782 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001783 return true;
1784 }
1785 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1786}
1787
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001788status_t Surface::getUniqueId(uint64_t* outId) const {
1789 Mutex::Autolock lock(mMutex);
1790 return mGraphicBufferProducer->getUniqueId(outId);
1791}
1792
Chia-I Wue2786ea2017-08-07 10:36:08 -07001793int Surface::getConsumerUsage(uint64_t* outUsage) const {
1794 Mutex::Autolock lock(mMutex);
1795 return mGraphicBufferProducer->getConsumerUsage(outUsage);
1796}
1797
Dan Stoza932f0082017-05-31 13:50:16 -07001798nsecs_t Surface::getLastDequeueStartTime() const {
1799 Mutex::Autolock lock(mMutex);
1800 return mLastDequeueStartTime;
1801}
1802
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001803status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1804 if (out == nullptr) {
1805 ALOGE("%s: out must not be null!", __FUNCTION__);
1806 return BAD_VALUE;
1807 }
1808
1809 Mutex::Autolock lock(mMutex);
1810 *out = mRemovedBuffers;
1811 mRemovedBuffers.clear();
1812 return OK;
1813}
1814
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001815status_t Surface::attachAndQueueBuffer(Surface* surface, sp<GraphicBuffer> buffer) {
1816 if (buffer == nullptr) {
1817 return BAD_VALUE;
1818 }
1819 int err = static_cast<ANativeWindow*>(surface)->perform(surface, NATIVE_WINDOW_API_CONNECT,
1820 NATIVE_WINDOW_API_CPU);
1821 if (err != OK) {
1822 return err;
1823 }
1824 err = surface->attachBuffer(buffer->getNativeBuffer());
1825 if (err != OK) {
1826 return err;
1827 }
1828 err = static_cast<ANativeWindow*>(surface)->queueBuffer(surface, buffer->getNativeBuffer(), -1);
1829 if (err != OK) {
1830 return err;
1831 }
1832 err = surface->disconnect(NATIVE_WINDOW_API_CPU);
1833 return err;
1834}
1835
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001836}; // namespace android