blob: 6a1aebd6d69c3dca3aad8a2d789442242375a627 [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;
48
Ian Elliotta2eb34c2017-07-18 11:05:49 -060049Surface::Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp)
50 : mGraphicBufferProducer(bufferProducer),
51 mCrop(Rect::EMPTY_RECT),
52 mBufferAge(0),
53 mGenerationNumber(0),
54 mSharedBufferMode(false),
55 mAutoRefresh(false),
56 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
57 mSharedBufferHasBeenQueued(false),
58 mQueriedSupportedTimestamps(false),
59 mFrameTimestampsSupportsPresent(false),
60 mEnableFrameTimestamps(false),
61 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>()) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -080062 // Initialize the ANativeWindow function pointers.
63 ANativeWindow::setSwapInterval = hook_setSwapInterval;
64 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
65 ANativeWindow::cancelBuffer = hook_cancelBuffer;
66 ANativeWindow::queueBuffer = hook_queueBuffer;
67 ANativeWindow::query = hook_query;
68 ANativeWindow::perform = hook_perform;
69
70 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
71 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
72 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
73 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
74
75 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
76 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
77
78 mReqWidth = 0;
79 mReqHeight = 0;
80 mReqFormat = 0;
81 mReqUsage = 0;
82 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080083 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080084 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
85 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070086 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080087 mDefaultWidth = 0;
88 mDefaultHeight = 0;
89 mUserWidth = 0;
90 mUserHeight = 0;
91 mTransformHint = 0;
92 mConsumerRunningBehind = false;
93 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070094 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070095 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070096}
97
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070098Surface::~Surface() {
99 if (mConnectedToCpu) {
100 Surface::disconnect(NATIVE_WINDOW_API_CPU);
101 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800102}
103
Brian Anderson3da8d272016-07-28 16:20:47 -0700104sp<ISurfaceComposer> Surface::composerService() const {
105 return ComposerService::getComposerService();
106}
107
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800108nsecs_t Surface::now() const {
109 return systemTime();
110}
111
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800112sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
113 return mGraphicBufferProducer;
114}
115
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900116void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
117 mGraphicBufferProducer->setSidebandStream(stream);
118}
119
Dan Stoza29a3e902014-06-20 13:13:57 -0700120void Surface::allocateBuffers() {
121 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
122 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700123 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
124 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700125}
126
Dan Stoza812ed062015-06-02 15:45:22 -0700127status_t Surface::setGenerationNumber(uint32_t generation) {
128 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
129 if (result == NO_ERROR) {
130 mGenerationNumber = generation;
131 }
132 return result;
133}
134
Dan Stoza7dde5992015-05-22 09:51:44 -0700135uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700136 Mutex::Autolock lock(mMutex);
137 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700138}
139
Dan Stozac6f30bd2015-06-08 09:32:50 -0700140String8 Surface::getConsumerName() const {
141 return mGraphicBufferProducer->getConsumerName();
142}
143
Dan Stoza127fc632015-06-30 13:43:32 -0700144status_t Surface::setDequeueTimeout(nsecs_t timeout) {
145 return mGraphicBufferProducer->setDequeueTimeout(timeout);
146}
147
Dan Stoza50101d02016-04-07 16:53:23 -0700148status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700149 sp<Fence>* outFence, float outTransformMatrix[16]) {
150 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
151 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700152}
153
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800154status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
155 ATRACE_CALL();
156
157 DisplayStatInfo stats;
Chih-Hung Hsiehd66be0a2017-10-05 13:51:32 -0700158 status_t result = composerService()->getDisplayStats(NULL, &stats);
159 if (result != NO_ERROR) {
160 return result;
161 }
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800162
163 *outRefreshDuration = stats.vsyncPeriod;
164
165 return NO_ERROR;
166}
167
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700168void Surface::enableFrameTimestamps(bool enable) {
169 Mutex::Autolock lock(mMutex);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800170 // If going from disabled to enabled, get the initial values for
171 // compositor and display timing.
172 if (!mEnableFrameTimestamps && enable) {
173 FrameEventHistoryDelta delta;
174 mGraphicBufferProducer->getFrameTimestamps(&delta);
175 mFrameEventHistory->applyDelta(delta);
176 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700177 mEnableFrameTimestamps = enable;
178}
179
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800180status_t Surface::getCompositorTiming(
181 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
182 nsecs_t* compositeToPresentLatency) {
183 Mutex::Autolock lock(mMutex);
184 if (!mEnableFrameTimestamps) {
185 return INVALID_OPERATION;
186 }
187
188 if (compositeDeadline != nullptr) {
189 *compositeDeadline =
190 mFrameEventHistory->getNextCompositeDeadline(now());
191 }
192 if (compositeInterval != nullptr) {
193 *compositeInterval = mFrameEventHistory->getCompositeInterval();
194 }
195 if (compositeToPresentLatency != nullptr) {
196 *compositeToPresentLatency =
197 mFrameEventHistory->getCompositeToPresentLatency();
198 }
199 return NO_ERROR;
200}
201
Brian Anderson50143b32016-09-30 14:01:24 -0700202static bool checkConsumerForUpdates(
203 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700204 const nsecs_t* outLatchTime,
205 const nsecs_t* outFirstRefreshStartTime,
206 const nsecs_t* outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700207 const nsecs_t* outGpuCompositionDoneTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700208 const nsecs_t* outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700209 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700210 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700211 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
212 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700213 !e->hasFirstRefreshStartInfo();
Brian Andersonb04c6f02016-10-21 12:57:46 -0700214 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700215 !e->hasGpuCompositionDoneInfo();
216 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
217 !e->hasDisplayPresentInfo();
218
Brian Anderson6b376712017-04-04 10:51:39 -0700219 // LastRefreshStart, DequeueReady, and Release are never available for the
220 // last frame.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700221 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
222 !e->hasLastRefreshStartInfo() &&
223 (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700224 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
225 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700226 bool checkForRelease = (outReleaseTime != nullptr) &&
227 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
228
229 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700230 return checkForLatch || checkForFirstRefreshStart ||
Brian Andersonb04c6f02016-10-21 12:57:46 -0700231 checkForLastRefreshStart || checkForGpuCompositionDone ||
Brian Anderson4e606e32017-03-16 15:34:57 -0700232 checkForDisplayPresent || checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700233}
234
Brian Anderson3d4039d2016-09-23 16:31:30 -0700235static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
236 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700237 // We always get valid timestamps for these eventually.
238 *dst = (src == FrameEvents::TIMESTAMP_PENDING) ?
239 NATIVE_WINDOW_TIMESTAMP_PENDING : src;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700240 }
241}
242
Brian Andersondc96fdf2017-03-20 16:54:25 -0700243static void getFrameTimestampFence(nsecs_t *dst,
244 const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700245 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700246 if (!fenceShouldBeKnown) {
247 *dst = NATIVE_WINDOW_TIMESTAMP_PENDING;
248 return;
249 }
250
Brian Anderson3d4039d2016-09-23 16:31:30 -0700251 nsecs_t signalTime = src->getSignalTime();
Brian Andersondc96fdf2017-03-20 16:54:25 -0700252 *dst = (signalTime == Fence::SIGNAL_TIME_PENDING) ?
253 NATIVE_WINDOW_TIMESTAMP_PENDING :
254 (signalTime == Fence::SIGNAL_TIME_INVALID) ?
255 NATIVE_WINDOW_TIMESTAMP_INVALID :
256 signalTime;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700257 }
258}
259
Brian Anderson069b3652016-07-22 10:32:47 -0700260status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700261 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700262 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700263 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700264 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
265 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800266 ATRACE_CALL();
267
Brian Anderson3890c392016-07-25 12:48:08 -0700268 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700269
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700270 if (!mEnableFrameTimestamps) {
271 return INVALID_OPERATION;
272 }
273
Brian Anderson6b376712017-04-04 10:51:39 -0700274 // Verify the requested timestamps are supported.
275 querySupportedTimestampsLocked();
276 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
277 return BAD_VALUE;
278 }
279
Brian Anderson3da8d272016-07-28 16:20:47 -0700280 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700281 if (events == nullptr) {
282 // If the entry isn't available in the producer, it's definitely not
283 // available in the consumer.
284 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700285 }
286
Brian Anderson50143b32016-09-30 14:01:24 -0700287 // Update our cache of events if the requested events are not available.
288 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700289 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700290 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700291 outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700292 FrameEventHistoryDelta delta;
293 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700294 mFrameEventHistory->applyDelta(delta);
295 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700296 }
297
Brian Anderson3890c392016-07-25 12:48:08 -0700298 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700299 // The entry was available before the update, but was overwritten
300 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700301 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800302 }
Brian Anderson069b3652016-07-22 10:32:47 -0700303
Brian Anderson3d4039d2016-09-23 16:31:30 -0700304 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700305 getFrameTimestamp(outLatchTime, events->latchTime);
306 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
307 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
308 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700309
Brian Andersondc96fdf2017-03-20 16:54:25 -0700310 getFrameTimestampFence(outAcquireTime, events->acquireFence,
311 events->hasAcquireInfo());
312 getFrameTimestampFence(outGpuCompositionDoneTime,
313 events->gpuCompositionDoneFence,
314 events->hasGpuCompositionDoneInfo());
315 getFrameTimestampFence(outDisplayPresentTime, events->displayPresentFence,
316 events->hasDisplayPresentInfo());
317 getFrameTimestampFence(outReleaseTime, events->releaseFence,
318 events->hasReleaseInfo());
Brian Anderson069b3652016-07-22 10:32:47 -0700319
320 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800321}
322
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600323using namespace android::hardware::configstore;
324using namespace android::hardware::configstore::V1_0;
325
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700326status_t Surface::getWideColorSupport(bool* supported) {
327 ATRACE_CALL();
328
329 sp<IBinder> display(
330 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
Peiyong Lina52f0292018-03-14 17:26:31 -0700331 Vector<ColorMode> colorModes;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700332 status_t err =
333 composerService()->getDisplayColorModes(display, &colorModes);
334
335 if (err)
336 return err;
337
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600338 bool wideColorBoardConfig =
339 getBool<ISurfaceFlingerConfigs,
340 &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
341
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700342 *supported = false;
Peiyong Lina52f0292018-03-14 17:26:31 -0700343 for (ColorMode colorMode : colorModes) {
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700344 switch (colorMode) {
Peiyong Lina52f0292018-03-14 17:26:31 -0700345 case ColorMode::DISPLAY_P3:
346 case ColorMode::ADOBE_RGB:
347 case ColorMode::DCI_P3:
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600348 if (wideColorBoardConfig) {
349 *supported = true;
350 }
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700351 break;
352 default:
353 break;
354 }
355 }
356
357 return NO_ERROR;
358}
359
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700360status_t Surface::getHdrSupport(bool* supported) {
361 ATRACE_CALL();
362
363 sp<IBinder> display(
364 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
365 HdrCapabilities hdrCapabilities;
366 status_t err =
367 composerService()->getHdrCapabilities(display, &hdrCapabilities);
368
369 if (err)
370 return err;
371
372 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
373
374 return NO_ERROR;
375}
376
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800377int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
378 Surface* c = getSelf(window);
379 return c->setSwapInterval(interval);
380}
381
382int Surface::hook_dequeueBuffer(ANativeWindow* window,
383 ANativeWindowBuffer** buffer, int* fenceFd) {
384 Surface* c = getSelf(window);
385 return c->dequeueBuffer(buffer, fenceFd);
386}
387
388int Surface::hook_cancelBuffer(ANativeWindow* window,
389 ANativeWindowBuffer* buffer, int fenceFd) {
390 Surface* c = getSelf(window);
391 return c->cancelBuffer(buffer, fenceFd);
392}
393
394int Surface::hook_queueBuffer(ANativeWindow* window,
395 ANativeWindowBuffer* buffer, int fenceFd) {
396 Surface* c = getSelf(window);
397 return c->queueBuffer(buffer, fenceFd);
398}
399
400int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
401 ANativeWindowBuffer** buffer) {
402 Surface* c = getSelf(window);
403 ANativeWindowBuffer* buf;
404 int fenceFd = -1;
405 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600406 if (result != OK) {
407 return result;
408 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800409 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700410 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800411 if (waitResult != OK) {
412 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
413 waitResult);
414 c->cancelBuffer(buf, -1);
415 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700416 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800417 *buffer = buf;
418 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700419}
420
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800421int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
422 ANativeWindowBuffer* buffer) {
423 Surface* c = getSelf(window);
424 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700425}
426
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800427int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
428 ANativeWindowBuffer* buffer) {
429 Surface* c = getSelf(window);
430 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700431}
432
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800433int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
434 ANativeWindowBuffer* buffer) {
435 Surface* c = getSelf(window);
436 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700437}
Mathias Agopian62185b72009-04-16 16:19:50 -0700438
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800439int Surface::hook_query(const ANativeWindow* window,
440 int what, int* value) {
441 const Surface* c = getSelf(window);
442 return c->query(what, value);
443}
444
445int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
446 va_list args;
447 va_start(args, operation);
448 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700449 int result = c->perform(operation, args);
450 va_end(args);
451 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800452}
453
454int Surface::setSwapInterval(int interval) {
455 ATRACE_CALL();
456 // EGL specification states:
457 // interval is silently clamped to minimum and maximum implementation
458 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800459
460 if (interval < minSwapInterval)
461 interval = minSwapInterval;
462
463 if (interval > maxSwapInterval)
464 interval = maxSwapInterval;
465
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700466 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700467 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800468
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700469 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800470}
471
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700472int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800473 ATRACE_CALL();
474 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800475
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800476 uint32_t reqWidth;
477 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800478 PixelFormat reqFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700479 uint64_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700480 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800481
482 {
483 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700484 if (mReportRemovedBuffers) {
485 mRemovedBuffers.clear();
486 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800487
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800488 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
489 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800490
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800491 reqFormat = mReqFormat;
492 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800493
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700494 enableFrameTimestamps = mEnableFrameTimestamps;
495
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700496 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800497 BufferItem::INVALID_BUFFER_SLOT) {
498 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
499 if (gbuf != NULL) {
500 *buffer = gbuf.get();
501 *fenceFd = -1;
502 return OK;
503 }
504 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800505 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
506
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800507 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800508 sp<Fence> fence;
Dan Stoza932f0082017-05-31 13:50:16 -0700509 nsecs_t startTime = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700510
511 FrameEventHistoryDelta frameTimestamps;
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600512 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, reqWidth, reqHeight,
513 reqFormat, reqUsage, &mBufferAge,
514 enableFrameTimestamps ? &frameTimestamps
515 : nullptr);
Dan Stoza932f0082017-05-31 13:50:16 -0700516 mLastDequeueDuration = systemTime() - startTime;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800517
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800518 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700519 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700520 "(%d, %d, %d, %#" PRIx64 ") failed: %d",
521 reqWidth, reqHeight, reqFormat, reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800522 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700523 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800524
Dan Stoza90ce2a92017-05-01 16:31:53 -0700525 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
526 ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf);
527 android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging
528 return FAILED_TRANSACTION;
529 }
530
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800531 Mutex::Autolock lock(mMutex);
532
Dan Stoza932f0082017-05-31 13:50:16 -0700533 // Write this while holding the mutex
534 mLastDequeueStartTime = startTime;
535
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800536 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700537
538 // this should never happen
539 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
540
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800541 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
542 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700543 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700544
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700545 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700546 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700547 }
548
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700549 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
550 if (mReportRemovedBuffers && (gbuf != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700551 mRemovedBuffers.push_back(gbuf);
552 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800553 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
554 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700555 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700556 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800557 return result;
558 }
559 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700560
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800561 if (fence->isValid()) {
562 *fenceFd = fence->dup();
563 if (*fenceFd == -1) {
564 ALOGE("dequeueBuffer: error duping fence: %d", errno);
565 // dup() should never fail; something is badly wrong. Soldier on
566 // and hope for the best; the worst that should happen is some
567 // visible corruption that lasts until the next frame.
568 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700569 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800570 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700571 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800572
573 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800574
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700575 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800576 mSharedBufferSlot = buf;
577 mSharedBufferHasBeenQueued = false;
578 } else if (mSharedBufferSlot == buf) {
579 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
580 mSharedBufferHasBeenQueued = false;
581 }
582
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800583 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700584}
585
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800586int Surface::cancelBuffer(android_native_buffer_t* buffer,
587 int fenceFd) {
588 ATRACE_CALL();
589 ALOGV("Surface::cancelBuffer");
590 Mutex::Autolock lock(mMutex);
591 int i = getSlotFromBufferLocked(buffer);
592 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900593 if (fenceFd >= 0) {
594 close(fenceFd);
595 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800596 return i;
597 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800598 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
599 if (fenceFd >= 0) {
600 close(fenceFd);
601 }
602 return OK;
603 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800604 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
605 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800606
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700607 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800608 mSharedBufferHasBeenQueued = true;
609 }
610
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800611 return OK;
612}
613
614int Surface::getSlotFromBufferLocked(
615 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800616 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
617 if (mSlots[i].buffer != NULL &&
618 mSlots[i].buffer->handle == buffer->handle) {
619 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700620 }
621 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800622 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
623 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700624}
625
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800626int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800627 ALOGV("Surface::lockBuffer");
628 Mutex::Autolock lock(mMutex);
629 return OK;
630}
Mathias Agopian631f3582010-05-25 17:51:34 -0700631
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800632int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
633 ATRACE_CALL();
634 ALOGV("Surface::queueBuffer");
635 Mutex::Autolock lock(mMutex);
636 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700637 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800638
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800639 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700640 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700641 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700642 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700643 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800644 } else {
645 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700646 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800647 int i = getSlotFromBufferLocked(buffer);
648 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900649 if (fenceFd >= 0) {
650 close(fenceFd);
651 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800652 return i;
653 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800654 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
655 if (fenceFd >= 0) {
656 close(fenceFd);
657 }
658 return OK;
659 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800662 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700663 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800664 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800666 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
667 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700668 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800669 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700670 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700671
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700672 // we should send HDR metadata as needed if this becomes a bottleneck
673 input.setHdrMetadata(mHdrMetadata);
674
Dan Stozac62acbd2015-04-21 16:42:49 -0700675 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700676 input.setSurfaceDamage(Region::INVALID_REGION);
677 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700678 // Here we do two things:
679 // 1) The surface damage was specified using the OpenGL ES convention of
680 // the origin being in the bottom-left corner. Here we flip to the
681 // convention that the rest of the system uses (top-left corner) by
682 // subtracting all top/bottom coordinates from the buffer height.
683 // 2) If the buffer is coming in rotated (for example, because the EGL
684 // implementation is reacting to the transform hint coming back from
685 // SurfaceFlinger), the surface damage needs to be rotated the
686 // opposite direction, since it was generated assuming an unrotated
687 // buffer (the app doesn't know that the EGL implementation is
688 // reacting to the transform hint behind its back). The
689 // transformations in the switch statement below apply those
690 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
691
692 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700693 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700694 bool rotated90 = (mTransform ^ mStickyTransform) &
695 NATIVE_WINDOW_TRANSFORM_ROT_90;
696 if (rotated90) {
697 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700698 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700699
Dan Stoza5065a552015-03-17 16:23:42 -0700700 Region flippedRegion;
701 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700702 int left = rect.left;
703 int right = rect.right;
704 int top = height - rect.bottom; // Flip from OpenGL convention
705 int bottom = height - rect.top; // Flip from OpenGL convention
706 switch (mTransform ^ mStickyTransform) {
707 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
708 // Rotate 270 degrees
709 Rect flippedRect{top, width - right, bottom, width - left};
710 flippedRegion.orSelf(flippedRect);
711 break;
712 }
713 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
714 // Rotate 180 degrees
715 Rect flippedRect{width - right, height - bottom,
716 width - left, height - top};
717 flippedRegion.orSelf(flippedRect);
718 break;
719 }
720 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
721 // Rotate 90 degrees
722 Rect flippedRect{height - bottom, left,
723 height - top, right};
724 flippedRegion.orSelf(flippedRect);
725 break;
726 }
727 default: {
728 Rect flippedRect{left, top, right, bottom};
729 flippedRegion.orSelf(flippedRect);
730 break;
731 }
732 }
Dan Stoza5065a552015-03-17 16:23:42 -0700733 }
734
735 input.setSurfaceDamage(flippedRegion);
736 }
737
Dan Stoza70ccba52016-07-01 14:00:40 -0700738 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800739 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700740 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800741 if (err != OK) {
742 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
743 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800744
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700745 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700746 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700747 // Update timestamps with the local acquire fence.
748 // The consumer doesn't send it back to prevent us from having two
749 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700750 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700751 std::make_shared<FenceTime>(std::move(fence)));
752
753 // Cache timestamps of signaled fences so we can close their file
754 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700755 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700756 }
757
Brian Anderson50143b32016-09-30 14:01:24 -0700758 mLastFrameNumber = mNextFrameNumber;
759
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700760 mDefaultWidth = output.width;
761 mDefaultHeight = output.height;
762 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700763
Ruben Brunk1681d952014-06-27 15:51:55 -0700764 // Disable transform hint if sticky transform is set.
765 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700766 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700767 }
768
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700769 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700770
Dan Stozac62acbd2015-04-21 16:42:49 -0700771 if (!mConnectedToCpu) {
772 // Clear surface damage back to full-buffer
773 mDirtyRegion = Region::INVALID_REGION;
774 }
Dan Stoza5065a552015-03-17 16:23:42 -0700775
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700776 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800777 mSharedBufferHasBeenQueued = true;
778 }
779
Robert Carr9f31e292016-04-11 11:15:32 -0700780 mQueueBufferCondition.broadcast();
781
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800782 return err;
783}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700784
Brian Anderson6b376712017-04-04 10:51:39 -0700785void Surface::querySupportedTimestampsLocked() const {
786 // mMutex must be locked when calling this method.
787
788 if (mQueriedSupportedTimestamps) {
789 return;
790 }
791 mQueriedSupportedTimestamps = true;
792
793 std::vector<FrameEvent> supportedFrameTimestamps;
794 status_t err = composerService()->getSupportedFrameTimestamps(
795 &supportedFrameTimestamps);
796
797 if (err != NO_ERROR) {
798 return;
799 }
800
801 for (auto sft : supportedFrameTimestamps) {
802 if (sft == FrameEvent::DISPLAY_PRESENT) {
803 mFrameTimestampsSupportsPresent = true;
804 }
805 }
806}
807
Mathias Agopiana67932f2011-04-20 14:20:59 -0700808int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800809 ATRACE_CALL();
810 ALOGV("Surface::query");
811 { // scope for the lock
812 Mutex::Autolock lock(mMutex);
813 switch (what) {
814 case NATIVE_WINDOW_FORMAT:
815 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800816 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800817 return NO_ERROR;
818 }
819 break;
820 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700821 if (composerService()->authenticateSurfaceTexture(
822 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800823 *value = 1;
824 } else {
825 *value = 0;
826 }
827 return NO_ERROR;
828 }
829 case NATIVE_WINDOW_CONCRETE_TYPE:
830 *value = NATIVE_WINDOW_SURFACE;
831 return NO_ERROR;
832 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800833 *value = static_cast<int>(
834 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800835 return NO_ERROR;
836 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800837 *value = static_cast<int>(
838 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800839 return NO_ERROR;
840 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800841 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800842 return NO_ERROR;
843 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
844 status_t err = NO_ERROR;
845 if (!mConsumerRunningBehind) {
846 *value = 0;
847 } else {
848 err = mGraphicBufferProducer->query(what, value);
849 if (err == NO_ERROR) {
850 mConsumerRunningBehind = *value;
851 }
852 }
853 return err;
854 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600855 case NATIVE_WINDOW_BUFFER_AGE: {
856 if (mBufferAge > INT32_MAX) {
857 *value = 0;
858 } else {
859 *value = static_cast<int32_t>(mBufferAge);
860 }
861 return NO_ERROR;
862 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700863 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
864 int64_t durationUs = mLastDequeueDuration / 1000;
865 *value = durationUs > std::numeric_limits<int>::max() ?
866 std::numeric_limits<int>::max() :
867 static_cast<int>(durationUs);
868 return NO_ERROR;
869 }
870 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
871 int64_t durationUs = mLastQueueDuration / 1000;
872 *value = durationUs > std::numeric_limits<int>::max() ?
873 std::numeric_limits<int>::max() :
874 static_cast<int>(durationUs);
875 return NO_ERROR;
876 }
Brian Anderson6b376712017-04-04 10:51:39 -0700877 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
878 querySupportedTimestampsLocked();
879 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
880 return NO_ERROR;
881 }
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800882 case NATIVE_WINDOW_IS_VALID: {
883 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
884 return NO_ERROR;
885 }
Peiyong Lin654f87b2018-01-30 14:21:33 -0800886 case NATIVE_WINDOW_DATASPACE: {
887 *value = static_cast<int>(mDataSpace);
888 return NO_ERROR;
889 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800890 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700891 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800892 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800893}
894
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800895int Surface::perform(int operation, va_list args)
896{
897 int res = NO_ERROR;
898 switch (operation) {
899 case NATIVE_WINDOW_CONNECT:
900 // deprecated. must return NO_ERROR.
901 break;
902 case NATIVE_WINDOW_DISCONNECT:
903 // deprecated. must return NO_ERROR.
904 break;
905 case NATIVE_WINDOW_SET_USAGE:
906 res = dispatchSetUsage(args);
907 break;
908 case NATIVE_WINDOW_SET_CROP:
909 res = dispatchSetCrop(args);
910 break;
911 case NATIVE_WINDOW_SET_BUFFER_COUNT:
912 res = dispatchSetBufferCount(args);
913 break;
914 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
915 res = dispatchSetBuffersGeometry(args);
916 break;
917 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
918 res = dispatchSetBuffersTransform(args);
919 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700920 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
921 res = dispatchSetBuffersStickyTransform(args);
922 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800923 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
924 res = dispatchSetBuffersTimestamp(args);
925 break;
926 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
927 res = dispatchSetBuffersDimensions(args);
928 break;
929 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
930 res = dispatchSetBuffersUserDimensions(args);
931 break;
932 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
933 res = dispatchSetBuffersFormat(args);
934 break;
935 case NATIVE_WINDOW_LOCK:
936 res = dispatchLock(args);
937 break;
938 case NATIVE_WINDOW_UNLOCK_AND_POST:
939 res = dispatchUnlockAndPost(args);
940 break;
941 case NATIVE_WINDOW_SET_SCALING_MODE:
942 res = dispatchSetScalingMode(args);
943 break;
944 case NATIVE_WINDOW_API_CONNECT:
945 res = dispatchConnect(args);
946 break;
947 case NATIVE_WINDOW_API_DISCONNECT:
948 res = dispatchDisconnect(args);
949 break;
Rachad7cb0d392014-07-29 17:53:53 -0700950 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
951 res = dispatchSetSidebandStream(args);
952 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800953 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
954 res = dispatchSetBuffersDataSpace(args);
955 break;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700956 case NATIVE_WINDOW_SET_BUFFERS_SMPTE2086_METADATA:
957 res = dispatchSetBuffersSmpte2086Metadata(args);
958 break;
959 case NATIVE_WINDOW_SET_BUFFERS_CTA861_3_METADATA:
960 res = dispatchSetBuffersCta8613Metadata(args);
961 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700962 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
963 res = dispatchSetSurfaceDamage(args);
964 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700965 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
966 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700967 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800968 case NATIVE_WINDOW_SET_AUTO_REFRESH:
969 res = dispatchSetAutoRefresh(args);
970 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800971 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
972 res = dispatchGetDisplayRefreshCycleDuration(args);
973 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800974 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
975 res = dispatchGetNextFrameId(args);
976 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700977 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
978 res = dispatchEnableFrameTimestamps(args);
979 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800980 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
981 res = dispatchGetCompositorTiming(args);
982 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800983 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
984 res = dispatchGetFrameTimestamps(args);
985 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700986 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
987 res = dispatchGetWideColorSupport(args);
988 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700989 case NATIVE_WINDOW_GET_HDR_SUPPORT:
990 res = dispatchGetHdrSupport(args);
991 break;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700992 case NATIVE_WINDOW_SET_USAGE64:
993 res = dispatchSetUsage64(args);
994 break;
Chia-I Wue2786ea2017-08-07 10:36:08 -0700995 case NATIVE_WINDOW_GET_CONSUMER_USAGE64:
996 res = dispatchGetConsumerUsage64(args);
997 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800998 default:
999 res = NAME_NOT_FOUND;
1000 break;
1001 }
1002 return res;
1003}
Mathias Agopiana138f892010-05-21 17:24:35 -07001004
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001005int Surface::dispatchConnect(va_list args) {
1006 int api = va_arg(args, int);
1007 return connect(api);
1008}
Mathias Agopian55fa2512010-03-11 15:06:54 -08001009
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001010int Surface::dispatchDisconnect(va_list args) {
1011 int api = va_arg(args, int);
1012 return disconnect(api);
1013}
1014
1015int Surface::dispatchSetUsage(va_list args) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001016 uint64_t usage = va_arg(args, uint32_t);
1017 return setUsage(usage);
1018}
1019
1020int Surface::dispatchSetUsage64(va_list args) {
1021 uint64_t usage = va_arg(args, uint64_t);
1022 return setUsage(usage);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001023}
1024
1025int Surface::dispatchSetCrop(va_list args) {
1026 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
1027 return setCrop(reinterpret_cast<Rect const*>(rect));
1028}
1029
1030int Surface::dispatchSetBufferCount(va_list args) {
1031 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001032 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001033}
1034
1035int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001036 uint32_t width = va_arg(args, uint32_t);
1037 uint32_t height = va_arg(args, uint32_t);
1038 PixelFormat format = va_arg(args, PixelFormat);
1039 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001040 if (err != 0) {
1041 return err;
1042 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001043 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001044}
1045
1046int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001047 uint32_t width = va_arg(args, uint32_t);
1048 uint32_t height = va_arg(args, uint32_t);
1049 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001050}
1051
1052int Surface::dispatchSetBuffersUserDimensions(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 setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001056}
1057
1058int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001059 PixelFormat format = va_arg(args, PixelFormat);
1060 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001061}
1062
1063int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001064 int mode = va_arg(args, int);
1065 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001066}
1067
1068int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001069 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001070 return setBuffersTransform(transform);
1071}
1072
Ruben Brunk1681d952014-06-27 15:51:55 -07001073int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001074 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -07001075 return setBuffersStickyTransform(transform);
1076}
1077
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001078int Surface::dispatchSetBuffersTimestamp(va_list args) {
1079 int64_t timestamp = va_arg(args, int64_t);
1080 return setBuffersTimestamp(timestamp);
1081}
1082
1083int Surface::dispatchLock(va_list args) {
1084 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1085 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1086 return lock(outBuffer, inOutDirtyBounds);
1087}
1088
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001089int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001090 return unlockAndPost();
1091}
1092
Rachad7cb0d392014-07-29 17:53:53 -07001093int Surface::dispatchSetSidebandStream(va_list args) {
1094 native_handle_t* sH = va_arg(args, native_handle_t*);
1095 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1096 setSidebandStream(sidebandHandle);
1097 return OK;
1098}
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001099
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001100int Surface::dispatchSetBuffersDataSpace(va_list args) {
1101 android_dataspace dataspace =
1102 static_cast<android_dataspace>(va_arg(args, int));
1103 return setBuffersDataSpace(dataspace);
1104}
1105
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001106int Surface::dispatchSetBuffersSmpte2086Metadata(va_list args) {
1107 const android_smpte2086_metadata* metadata =
1108 va_arg(args, const android_smpte2086_metadata*);
1109 return setBuffersSmpte2086Metadata(metadata);
1110}
1111
1112int Surface::dispatchSetBuffersCta8613Metadata(va_list args) {
1113 const android_cta861_3_metadata* metadata =
1114 va_arg(args, const android_cta861_3_metadata*);
1115 return setBuffersCta8613Metadata(metadata);
1116}
1117
Dan Stoza5065a552015-03-17 16:23:42 -07001118int Surface::dispatchSetSurfaceDamage(va_list args) {
1119 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1120 size_t numRects = va_arg(args, size_t);
1121 setSurfaceDamage(rects, numRects);
1122 return NO_ERROR;
1123}
1124
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001125int Surface::dispatchSetSharedBufferMode(va_list args) {
1126 bool sharedBufferMode = va_arg(args, int);
1127 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001128}
1129
1130int Surface::dispatchSetAutoRefresh(va_list args) {
1131 bool autoRefresh = va_arg(args, int);
1132 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001133}
1134
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001135int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1136 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1137 return getDisplayRefreshCycleDuration(outRefreshDuration);
1138}
1139
Brian Anderson1049d1d2016-12-16 17:25:57 -08001140int Surface::dispatchGetNextFrameId(va_list args) {
1141 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1142 *nextFrameId = getNextFrameNumber();
1143 return NO_ERROR;
1144}
1145
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001146int Surface::dispatchEnableFrameTimestamps(va_list args) {
1147 bool enable = va_arg(args, int);
1148 enableFrameTimestamps(enable);
1149 return NO_ERROR;
1150}
1151
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001152int Surface::dispatchGetCompositorTiming(va_list args) {
1153 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1154 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1155 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1156 return getCompositorTiming(compositeDeadline, compositeInterval,
1157 compositeToPresentLatency);
1158}
1159
Pablo Ceballosce796e72016-02-04 19:10:51 -08001160int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001161 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001162 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001163 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001164 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1165 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1166 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001167 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001168 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001169 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001170 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001171 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001172 outRequestedPresentTime, outAcquireTime, outLatchTime,
1173 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001174 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001175 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001176}
1177
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001178int Surface::dispatchGetWideColorSupport(va_list args) {
1179 bool* outSupport = va_arg(args, bool*);
1180 return getWideColorSupport(outSupport);
1181}
1182
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001183int Surface::dispatchGetHdrSupport(va_list args) {
1184 bool* outSupport = va_arg(args, bool*);
1185 return getHdrSupport(outSupport);
1186}
1187
Chia-I Wue2786ea2017-08-07 10:36:08 -07001188int Surface::dispatchGetConsumerUsage64(va_list args) {
1189 uint64_t* usage = va_arg(args, uint64_t*);
1190 return getConsumerUsage(usage);
1191}
1192
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001193int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001194 static sp<IProducerListener> listener = new DummyProducerListener();
1195 return connect(api, listener);
1196}
1197
1198int Surface::connect(int api, const sp<IProducerListener>& listener) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001199 return connect(api, listener, false);
1200}
1201
1202int Surface::connect(
1203 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001204 ATRACE_CALL();
1205 ALOGV("Surface::connect");
1206 Mutex::Autolock lock(mMutex);
1207 IGraphicBufferProducer::QueueBufferOutput output;
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001208 mReportRemovedBuffers = reportBufferRemoval;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001209 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001210 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001211 mDefaultWidth = output.width;
1212 mDefaultHeight = output.height;
1213 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001214
1215 // Disable transform hint if sticky transform is set.
1216 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001217 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001218 }
1219
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001220 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001221 }
1222 if (!err && api == NATIVE_WINDOW_API_CPU) {
1223 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001224 // Clear the dirty region in case we're switching from a non-CPU API
1225 mDirtyRegion.clear();
1226 } else if (!err) {
1227 // Initialize the dirty region for tracking surface damage
1228 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001229 }
Dan Stoza5065a552015-03-17 16:23:42 -07001230
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001231 return err;
1232}
1233
Mathias Agopian365857d2013-09-11 19:35:45 -07001234
Robert Carr97b9c862016-09-08 13:54:35 -07001235int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001236 ATRACE_CALL();
1237 ALOGV("Surface::disconnect");
1238 Mutex::Autolock lock(mMutex);
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001239 mRemovedBuffers.clear();
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001240 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1241 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001242 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001243 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001244 if (!err) {
1245 mReqFormat = 0;
1246 mReqWidth = 0;
1247 mReqHeight = 0;
1248 mReqUsage = 0;
1249 mCrop.clear();
1250 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1251 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001252 mStickyTransform = 0;
1253
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001254 if (api == NATIVE_WINDOW_API_CPU) {
1255 mConnectedToCpu = false;
1256 }
1257 }
1258 return err;
1259}
1260
Dan Stozad9c49712015-04-27 11:06:01 -07001261int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001262 sp<Fence>* outFence) {
1263 ATRACE_CALL();
1264 ALOGV("Surface::detachNextBuffer");
1265
1266 if (outBuffer == NULL || outFence == NULL) {
1267 return BAD_VALUE;
1268 }
1269
1270 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001271 if (mReportRemovedBuffers) {
1272 mRemovedBuffers.clear();
1273 }
Dan Stoza231832e2015-03-11 11:55:01 -07001274
1275 sp<GraphicBuffer> buffer(NULL);
1276 sp<Fence> fence(NULL);
1277 status_t result = mGraphicBufferProducer->detachNextBuffer(
1278 &buffer, &fence);
1279 if (result != NO_ERROR) {
1280 return result;
1281 }
1282
Dan Stozad9c49712015-04-27 11:06:01 -07001283 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001284 if (fence != NULL && fence->isValid()) {
1285 *outFence = fence;
1286 } else {
1287 *outFence = Fence::NO_FENCE;
1288 }
1289
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001290 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1291 if (mSlots[i].buffer != NULL &&
Shuzhen Wang55be5052017-05-23 09:41:54 -07001292 mSlots[i].buffer->getId() == buffer->getId()) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001293 if (mReportRemovedBuffers) {
1294 mRemovedBuffers.push_back(mSlots[i].buffer);
1295 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001296 mSlots[i].buffer = NULL;
1297 }
1298 }
1299
Dan Stoza231832e2015-03-11 11:55:01 -07001300 return NO_ERROR;
1301}
1302
1303int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1304{
1305 ATRACE_CALL();
1306 ALOGV("Surface::attachBuffer");
1307
1308 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001309 if (mReportRemovedBuffers) {
1310 mRemovedBuffers.clear();
1311 }
Dan Stoza231832e2015-03-11 11:55:01 -07001312
1313 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001314 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1315 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001316 int32_t attachedSlot = -1;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001317 status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer);
Dan Stoza231832e2015-03-11 11:55:01 -07001318 if (result != NO_ERROR) {
1319 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001320 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001321 return result;
1322 }
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001323 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001324 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1325 }
Dan Stoza231832e2015-03-11 11:55:01 -07001326 mSlots[attachedSlot].buffer = graphicBuffer;
1327
1328 return NO_ERROR;
1329}
1330
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001331int Surface::setUsage(uint64_t reqUsage)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001332{
1333 ALOGV("Surface::setUsage");
1334 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001335 if (reqUsage != mReqUsage) {
1336 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1337 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001338 mReqUsage = reqUsage;
1339 return OK;
1340}
1341
1342int Surface::setCrop(Rect const* rect)
1343{
1344 ATRACE_CALL();
1345
Pablo Ceballos60d69222015-08-07 14:47:20 -07001346 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001347 if (rect == NULL || rect->isEmpty()) {
1348 realRect.clear();
1349 } else {
1350 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001351 }
1352
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001353 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1354 realRect.left, realRect.top, realRect.right, realRect.bottom);
1355
1356 Mutex::Autolock lock(mMutex);
1357 mCrop = realRect;
1358 return NO_ERROR;
1359}
1360
1361int Surface::setBufferCount(int bufferCount)
1362{
1363 ATRACE_CALL();
1364 ALOGV("Surface::setBufferCount");
1365 Mutex::Autolock lock(mMutex);
1366
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001367 status_t err = NO_ERROR;
1368 if (bufferCount == 0) {
1369 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1370 } else {
1371 int minUndequeuedBuffers = 0;
1372 err = mGraphicBufferProducer->query(
1373 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1374 if (err == NO_ERROR) {
1375 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1376 bufferCount - minUndequeuedBuffers);
1377 }
1378 }
Mathias Agopian90147262010-01-22 11:47:55 -08001379
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001380 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1381 bufferCount, strerror(-err));
1382
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001383 return err;
1384}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001385
Pablo Ceballosfa455352015-08-12 17:47:47 -07001386int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1387 ATRACE_CALL();
1388 ALOGV("Surface::setMaxDequeuedBufferCount");
1389 Mutex::Autolock lock(mMutex);
1390
1391 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1392 maxDequeuedBuffers);
1393 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1394 "returned %s", maxDequeuedBuffers, strerror(-err));
1395
Pablo Ceballosfa455352015-08-12 17:47:47 -07001396 return err;
1397}
1398
1399int Surface::setAsyncMode(bool async) {
1400 ATRACE_CALL();
1401 ALOGV("Surface::setAsyncMode");
1402 Mutex::Autolock lock(mMutex);
1403
1404 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1405 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1406 async, strerror(-err));
1407
Pablo Ceballosfa455352015-08-12 17:47:47 -07001408 return err;
1409}
1410
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001411int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001412 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001413 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001414 Mutex::Autolock lock(mMutex);
1415
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001416 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1417 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001418 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001419 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001420 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001421 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1422 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001423
1424 return err;
1425}
1426
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001427int Surface::setAutoRefresh(bool autoRefresh) {
1428 ATRACE_CALL();
1429 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1430 Mutex::Autolock lock(mMutex);
1431
1432 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1433 if (err == NO_ERROR) {
1434 mAutoRefresh = autoRefresh;
1435 }
1436 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1437 autoRefresh, strerror(-err));
1438 return err;
1439}
1440
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001441int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001442{
1443 ATRACE_CALL();
1444 ALOGV("Surface::setBuffersDimensions");
1445
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001446 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001447 return BAD_VALUE;
1448
1449 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001450 if (width != mReqWidth || height != mReqHeight) {
1451 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1452 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001453 mReqWidth = width;
1454 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001455 return NO_ERROR;
1456}
1457
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001458int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001459{
1460 ATRACE_CALL();
1461 ALOGV("Surface::setBuffersUserDimensions");
1462
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001463 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001464 return BAD_VALUE;
1465
1466 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001467 if (width != mUserWidth || height != mUserHeight) {
1468 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1469 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001470 mUserWidth = width;
1471 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001472 return NO_ERROR;
1473}
1474
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001475int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001476{
1477 ALOGV("Surface::setBuffersFormat");
1478
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001479 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001480 if (format != mReqFormat) {
1481 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1482 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001483 mReqFormat = format;
1484 return NO_ERROR;
1485}
1486
1487int Surface::setScalingMode(int mode)
1488{
1489 ATRACE_CALL();
1490 ALOGV("Surface::setScalingMode(%d)", mode);
1491
1492 switch (mode) {
1493 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1494 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1495 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001496 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001497 break;
1498 default:
1499 ALOGE("unknown scaling mode: %d", mode);
1500 return BAD_VALUE;
1501 }
1502
1503 Mutex::Autolock lock(mMutex);
1504 mScalingMode = mode;
1505 return NO_ERROR;
1506}
1507
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001508int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001509{
1510 ATRACE_CALL();
1511 ALOGV("Surface::setBuffersTransform");
1512 Mutex::Autolock lock(mMutex);
1513 mTransform = transform;
1514 return NO_ERROR;
1515}
1516
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001517int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001518{
1519 ATRACE_CALL();
1520 ALOGV("Surface::setBuffersStickyTransform");
1521 Mutex::Autolock lock(mMutex);
1522 mStickyTransform = transform;
1523 return NO_ERROR;
1524}
1525
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001526int Surface::setBuffersTimestamp(int64_t timestamp)
1527{
1528 ALOGV("Surface::setBuffersTimestamp");
1529 Mutex::Autolock lock(mMutex);
1530 mTimestamp = timestamp;
1531 return NO_ERROR;
1532}
1533
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001534int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1535{
1536 ALOGV("Surface::setBuffersDataSpace");
1537 Mutex::Autolock lock(mMutex);
1538 mDataSpace = dataSpace;
1539 return NO_ERROR;
1540}
1541
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001542int Surface::setBuffersSmpte2086Metadata(const android_smpte2086_metadata* metadata) {
1543 ALOGV("Surface::setBuffersSmpte2086Metadata");
1544 Mutex::Autolock lock(mMutex);
1545 if (metadata) {
1546 mHdrMetadata.smpte2086 = *metadata;
1547 mHdrMetadata.validTypes |= HdrMetadata::SMPTE2086;
1548 } else {
1549 mHdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086;
1550 }
1551 return NO_ERROR;
1552}
1553
1554int Surface::setBuffersCta8613Metadata(const android_cta861_3_metadata* metadata) {
1555 ALOGV("Surface::setBuffersCta8613Metadata");
1556 Mutex::Autolock lock(mMutex);
1557 if (metadata) {
1558 mHdrMetadata.cta8613 = *metadata;
1559 mHdrMetadata.validTypes |= HdrMetadata::CTA861_3;
1560 } else {
1561 mHdrMetadata.validTypes &= ~HdrMetadata::CTA861_3;
1562 }
1563 return NO_ERROR;
1564}
1565
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -06001566android_dataspace_t Surface::getBuffersDataSpace() {
1567 ALOGV("Surface::getBuffersDataSpace");
1568 Mutex::Autolock lock(mMutex);
1569 return mDataSpace;
1570}
1571
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001572void Surface::freeAllBuffers() {
1573 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1574 mSlots[i].buffer = 0;
1575 }
1576}
1577
Dan Stoza5065a552015-03-17 16:23:42 -07001578void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1579 ATRACE_CALL();
1580 ALOGV("Surface::setSurfaceDamage");
1581 Mutex::Autolock lock(mMutex);
1582
Dan Stozac62acbd2015-04-21 16:42:49 -07001583 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001584 mDirtyRegion = Region::INVALID_REGION;
1585 return;
1586 }
1587
1588 mDirtyRegion.clear();
1589 for (size_t r = 0; r < numRects; ++r) {
1590 // We intentionally flip top and bottom here, since because they're
1591 // specified with a bottom-left origin, top > bottom, which fails
1592 // validation in the Region class. We will fix this up when we flip to a
1593 // top-left origin in queueBuffer.
1594 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1595 mDirtyRegion.orSelf(rect);
1596 }
1597}
1598
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001599// ----------------------------------------------------------------------
1600// the lock/unlock APIs must be used from the same thread
1601
1602static status_t copyBlt(
1603 const sp<GraphicBuffer>& dst,
1604 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001605 const Region& reg,
1606 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001607{
Dennis Kempinadd458f2017-05-17 19:16:31 -07001608 if (dst->getId() == src->getId())
1609 return OK;
1610
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001611 // src and dst with, height and format must be identical. no verification
1612 // is done here.
1613 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001614 uint8_t* src_bits = NULL;
1615 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1616 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001617 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1618
1619 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001620 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1621 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001622 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001623 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001624
1625 Region::const_iterator head(reg.begin());
1626 Region::const_iterator tail(reg.end());
1627 if (head != tail && src_bits && dst_bits) {
1628 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001629 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1630 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001631
1632 while (head != tail) {
1633 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001634 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001635 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001636 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1637 uint8_t const * s = src_bits +
1638 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1639 uint8_t * d = dst_bits +
1640 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001641 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001642 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001643 h = 1;
1644 }
1645 do {
1646 memcpy(d, s, size);
1647 d += dbpr;
1648 s += sbpr;
1649 } while (--h > 0);
1650 }
1651 }
1652
1653 if (src_bits)
1654 src->unlock();
1655
1656 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001657 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001658
1659 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660}
1661
Mathias Agopiana138f892010-05-21 17:24:35 -07001662// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001663
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001664status_t Surface::lock(
1665 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1666{
1667 if (mLockedBuffer != 0) {
1668 ALOGE("Surface::lock failed, already locked");
1669 return INVALID_OPERATION;
1670 }
1671
1672 if (!mConnectedToCpu) {
1673 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1674 if (err) {
1675 return err;
1676 }
1677 // we're intending to do software rendering from this point
1678 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1679 }
1680
1681 ANativeWindowBuffer* out;
1682 int fenceFd = -1;
1683 status_t err = dequeueBuffer(&out, &fenceFd);
1684 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1685 if (err == NO_ERROR) {
1686 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001687 const Rect bounds(backBuffer->width, backBuffer->height);
1688
1689 Region newDirtyRegion;
1690 if (inOutDirtyBounds) {
1691 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1692 newDirtyRegion.andSelf(bounds);
1693 } else {
1694 newDirtyRegion.set(bounds);
1695 }
1696
1697 // figure out if we can copy the frontbuffer back
1698 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1699 const bool canCopyBack = (frontBuffer != 0 &&
1700 backBuffer->width == frontBuffer->width &&
1701 backBuffer->height == frontBuffer->height &&
1702 backBuffer->format == frontBuffer->format);
1703
1704 if (canCopyBack) {
1705 // copy the area that is invalid and not repainted this round
1706 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001707 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001708 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001709 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001710 } else {
1711 // if we can't copy-back anything, modify the user's dirty
1712 // region to make sure they redraw the whole buffer
1713 newDirtyRegion.set(bounds);
1714 mDirtyRegion.clear();
1715 Mutex::Autolock lock(mMutex);
1716 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1717 mSlots[i].dirtyRegion.clear();
1718 }
1719 }
1720
1721
1722 { // scope for the lock
1723 Mutex::Autolock lock(mMutex);
1724 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1725 if (backBufferSlot >= 0) {
1726 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1727 mDirtyRegion.subtract(dirtyRegion);
1728 dirtyRegion = newDirtyRegion;
1729 }
1730 }
1731
1732 mDirtyRegion.orSelf(newDirtyRegion);
1733 if (inOutDirtyBounds) {
1734 *inOutDirtyBounds = newDirtyRegion.getBounds();
1735 }
1736
1737 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001738 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001739 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001740 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001741
1742 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1743 backBuffer->handle);
1744
1745 if (res != 0) {
1746 err = INVALID_OPERATION;
1747 } else {
1748 mLockedBuffer = backBuffer;
1749 outBuffer->width = backBuffer->width;
1750 outBuffer->height = backBuffer->height;
1751 outBuffer->stride = backBuffer->stride;
1752 outBuffer->format = backBuffer->format;
1753 outBuffer->bits = vaddr;
1754 }
1755 }
1756 return err;
1757}
1758
1759status_t Surface::unlockAndPost()
1760{
1761 if (mLockedBuffer == 0) {
1762 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1763 return INVALID_OPERATION;
1764 }
1765
Francis Hart8f396012014-04-01 15:30:53 +03001766 int fd = -1;
1767 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001768 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1769
Francis Hart8f396012014-04-01 15:30:53 +03001770 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001771 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1772 mLockedBuffer->handle, strerror(-err));
1773
1774 mPostedBuffer = mLockedBuffer;
1775 mLockedBuffer = 0;
1776 return err;
1777}
1778
Robert Carr9f31e292016-04-11 11:15:32 -07001779bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1780 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001781 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001782 return true;
1783 }
1784 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1785}
1786
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001787status_t Surface::getUniqueId(uint64_t* outId) const {
1788 Mutex::Autolock lock(mMutex);
1789 return mGraphicBufferProducer->getUniqueId(outId);
1790}
1791
Chia-I Wue2786ea2017-08-07 10:36:08 -07001792int Surface::getConsumerUsage(uint64_t* outUsage) const {
1793 Mutex::Autolock lock(mMutex);
1794 return mGraphicBufferProducer->getConsumerUsage(outUsage);
1795}
1796
Dan Stoza932f0082017-05-31 13:50:16 -07001797nsecs_t Surface::getLastDequeueStartTime() const {
1798 Mutex::Autolock lock(mMutex);
1799 return mLastDequeueStartTime;
1800}
1801
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001802status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1803 if (out == nullptr) {
1804 ALOGE("%s: out must not be null!", __FUNCTION__);
1805 return BAD_VALUE;
1806 }
1807
1808 Mutex::Autolock lock(mMutex);
1809 *out = mRemovedBuffers;
1810 mRemovedBuffers.clear();
1811 return OK;
1812}
1813
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001814status_t Surface::attachAndQueueBuffer(Surface* surface, sp<GraphicBuffer> buffer) {
1815 if (buffer == nullptr) {
1816 return BAD_VALUE;
1817 }
1818 int err = static_cast<ANativeWindow*>(surface)->perform(surface, NATIVE_WINDOW_API_CONNECT,
1819 NATIVE_WINDOW_API_CPU);
1820 if (err != OK) {
1821 return err;
1822 }
1823 err = surface->attachBuffer(buffer->getNativeBuffer());
1824 if (err != OK) {
1825 return err;
1826 }
1827 err = static_cast<ANativeWindow*>(surface)->queueBuffer(surface, buffer->getNativeBuffer(), -1);
1828 if (err != OK) {
1829 return err;
1830 }
1831 err = surface->disconnect(NATIVE_WINDOW_API_CPU);
1832 return err;
1833}
1834
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001835}; // namespace android