blob: a6d9e6682826a860537ebb645a3d207600dd8e8b [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 Agopian05debe12017-02-08 17:04:18 -080023#include <android/native_window.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080024
Mathias Agopian9cce3252010-02-09 17:46:37 -080025#include <utils/Log.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080026#include <utils/Trace.h>
Rachad7cb0d392014-07-29 17:53:53 -070027#include <utils/NativeHandle.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080028
Ian Elliott62c48c92017-01-20 13:13:20 -070029#include <ui/DisplayStatInfo.h>
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -070030#include <ui/Fence.h>
31#include <ui/HdrCapabilities.h>
32#include <ui/Region.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070033
Mathias Agopian2b5dd402017-02-07 17:36:19 -080034#include <gui/BufferItem.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070035#include <gui/IProducerListener.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080036
Mathias Agopian2b5dd402017-02-07 17:36:19 -080037#include <gui/ISurfaceComposer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080038#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -060040#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
41#include <configstore/Utils.h>
42
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043namespace android {
44
Mathias Agopiane3c697f2013-02-14 17:11:02 -080045Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070046 const sp<IGraphicBufferProducer>& bufferProducer,
47 bool controlledByApp)
Dan Stoza812ed062015-06-02 15:45:22 -070048 : mGraphicBufferProducer(bufferProducer),
Pablo Ceballos60d69222015-08-07 14:47:20 -070049 mCrop(Rect::EMPTY_RECT),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080050 mGenerationNumber(0),
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070051 mSharedBufferMode(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080052 mAutoRefresh(false),
53 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
Pablo Ceballosbc8c1922016-07-01 14:15:41 -070054 mSharedBufferHasBeenQueued(false),
Brian Anderson6b376712017-04-04 10:51:39 -070055 mQueriedSupportedTimestamps(false),
56 mFrameTimestampsSupportsPresent(false),
Brian Anderson3da8d272016-07-28 16:20:47 -070057 mEnableFrameTimestamps(false),
58 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>())
Mathias Agopian62185b72009-04-16 16:19:50 -070059{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080060 // Initialize the ANativeWindow function pointers.
61 ANativeWindow::setSwapInterval = hook_setSwapInterval;
62 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
63 ANativeWindow::cancelBuffer = hook_cancelBuffer;
64 ANativeWindow::queueBuffer = hook_queueBuffer;
65 ANativeWindow::query = hook_query;
66 ANativeWindow::perform = hook_perform;
67
68 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
69 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
70 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
71 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
72
73 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
74 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
75
76 mReqWidth = 0;
77 mReqHeight = 0;
78 mReqFormat = 0;
79 mReqUsage = 0;
80 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080081 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080082 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
83 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070084 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080085 mDefaultWidth = 0;
86 mDefaultHeight = 0;
87 mUserWidth = 0;
88 mUserHeight = 0;
89 mTransformHint = 0;
90 mConsumerRunningBehind = false;
91 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070092 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070093 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070094}
95
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070096Surface::~Surface() {
97 if (mConnectedToCpu) {
98 Surface::disconnect(NATIVE_WINDOW_API_CPU);
99 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800100}
101
Brian Anderson3da8d272016-07-28 16:20:47 -0700102sp<ISurfaceComposer> Surface::composerService() const {
103 return ComposerService::getComposerService();
104}
105
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800106nsecs_t Surface::now() const {
107 return systemTime();
108}
109
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800110sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
111 return mGraphicBufferProducer;
112}
113
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900114void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
115 mGraphicBufferProducer->setSidebandStream(stream);
116}
117
Dan Stoza29a3e902014-06-20 13:13:57 -0700118void Surface::allocateBuffers() {
119 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
120 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700121 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
122 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700123}
124
Dan Stoza812ed062015-06-02 15:45:22 -0700125status_t Surface::setGenerationNumber(uint32_t generation) {
126 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
127 if (result == NO_ERROR) {
128 mGenerationNumber = generation;
129 }
130 return result;
131}
132
Dan Stoza7dde5992015-05-22 09:51:44 -0700133uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700134 Mutex::Autolock lock(mMutex);
135 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700136}
137
Dan Stozac6f30bd2015-06-08 09:32:50 -0700138String8 Surface::getConsumerName() const {
139 return mGraphicBufferProducer->getConsumerName();
140}
141
Dan Stoza127fc632015-06-30 13:43:32 -0700142status_t Surface::setDequeueTimeout(nsecs_t timeout) {
143 return mGraphicBufferProducer->setDequeueTimeout(timeout);
144}
145
Dan Stoza50101d02016-04-07 16:53:23 -0700146status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700147 sp<Fence>* outFence, float outTransformMatrix[16]) {
148 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
149 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700150}
151
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800152status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
153 ATRACE_CALL();
154
155 DisplayStatInfo stats;
156 status_t err = composerService()->getDisplayStats(NULL, &stats);
157
158 *outRefreshDuration = stats.vsyncPeriod;
159
160 return NO_ERROR;
161}
162
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700163void Surface::enableFrameTimestamps(bool enable) {
164 Mutex::Autolock lock(mMutex);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800165 // If going from disabled to enabled, get the initial values for
166 // compositor and display timing.
167 if (!mEnableFrameTimestamps && enable) {
168 FrameEventHistoryDelta delta;
169 mGraphicBufferProducer->getFrameTimestamps(&delta);
170 mFrameEventHistory->applyDelta(delta);
171 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700172 mEnableFrameTimestamps = enable;
173}
174
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800175status_t Surface::getCompositorTiming(
176 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
177 nsecs_t* compositeToPresentLatency) {
178 Mutex::Autolock lock(mMutex);
179 if (!mEnableFrameTimestamps) {
180 return INVALID_OPERATION;
181 }
182
183 if (compositeDeadline != nullptr) {
184 *compositeDeadline =
185 mFrameEventHistory->getNextCompositeDeadline(now());
186 }
187 if (compositeInterval != nullptr) {
188 *compositeInterval = mFrameEventHistory->getCompositeInterval();
189 }
190 if (compositeToPresentLatency != nullptr) {
191 *compositeToPresentLatency =
192 mFrameEventHistory->getCompositeToPresentLatency();
193 }
194 return NO_ERROR;
195}
196
Brian Anderson50143b32016-09-30 14:01:24 -0700197static bool checkConsumerForUpdates(
198 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700199 const nsecs_t* outLatchTime,
200 const nsecs_t* outFirstRefreshStartTime,
201 const nsecs_t* outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700202 const nsecs_t* outGpuCompositionDoneTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700203 const nsecs_t* outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700204 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700205 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700206 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
207 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700208 !e->hasFirstRefreshStartInfo();
Brian Andersonb04c6f02016-10-21 12:57:46 -0700209 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700210 !e->hasGpuCompositionDoneInfo();
211 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
212 !e->hasDisplayPresentInfo();
213
Brian Anderson6b376712017-04-04 10:51:39 -0700214 // LastRefreshStart, DequeueReady, and Release are never available for the
215 // last frame.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700216 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
217 !e->hasLastRefreshStartInfo() &&
218 (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700219 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
220 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700221 bool checkForRelease = (outReleaseTime != nullptr) &&
222 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
223
224 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700225 return checkForLatch || checkForFirstRefreshStart ||
Brian Andersonb04c6f02016-10-21 12:57:46 -0700226 checkForLastRefreshStart || checkForGpuCompositionDone ||
Brian Anderson4e606e32017-03-16 15:34:57 -0700227 checkForDisplayPresent || checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700228}
229
Brian Anderson3d4039d2016-09-23 16:31:30 -0700230static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
231 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700232 // We always get valid timestamps for these eventually.
233 *dst = (src == FrameEvents::TIMESTAMP_PENDING) ?
234 NATIVE_WINDOW_TIMESTAMP_PENDING : src;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700235 }
236}
237
Brian Andersondc96fdf2017-03-20 16:54:25 -0700238static void getFrameTimestampFence(nsecs_t *dst,
239 const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700240 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700241 if (!fenceShouldBeKnown) {
242 *dst = NATIVE_WINDOW_TIMESTAMP_PENDING;
243 return;
244 }
245
Brian Anderson3d4039d2016-09-23 16:31:30 -0700246 nsecs_t signalTime = src->getSignalTime();
Brian Andersondc96fdf2017-03-20 16:54:25 -0700247 *dst = (signalTime == Fence::SIGNAL_TIME_PENDING) ?
248 NATIVE_WINDOW_TIMESTAMP_PENDING :
249 (signalTime == Fence::SIGNAL_TIME_INVALID) ?
250 NATIVE_WINDOW_TIMESTAMP_INVALID :
251 signalTime;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700252 }
253}
254
Brian Anderson069b3652016-07-22 10:32:47 -0700255status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700256 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700257 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700258 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700259 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
260 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800261 ATRACE_CALL();
262
Brian Anderson3890c392016-07-25 12:48:08 -0700263 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700264
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700265 if (!mEnableFrameTimestamps) {
266 return INVALID_OPERATION;
267 }
268
Brian Anderson6b376712017-04-04 10:51:39 -0700269 // Verify the requested timestamps are supported.
270 querySupportedTimestampsLocked();
271 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
272 return BAD_VALUE;
273 }
274
Brian Anderson3da8d272016-07-28 16:20:47 -0700275 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700276 if (events == nullptr) {
277 // If the entry isn't available in the producer, it's definitely not
278 // available in the consumer.
279 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700280 }
281
Brian Anderson50143b32016-09-30 14:01:24 -0700282 // Update our cache of events if the requested events are not available.
283 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700284 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700285 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700286 outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700287 FrameEventHistoryDelta delta;
288 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700289 mFrameEventHistory->applyDelta(delta);
290 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700291 }
292
Brian Anderson3890c392016-07-25 12:48:08 -0700293 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700294 // The entry was available before the update, but was overwritten
295 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700296 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800297 }
Brian Anderson069b3652016-07-22 10:32:47 -0700298
Brian Anderson3d4039d2016-09-23 16:31:30 -0700299 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700300 getFrameTimestamp(outLatchTime, events->latchTime);
301 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
302 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
303 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700304
Brian Andersondc96fdf2017-03-20 16:54:25 -0700305 getFrameTimestampFence(outAcquireTime, events->acquireFence,
306 events->hasAcquireInfo());
307 getFrameTimestampFence(outGpuCompositionDoneTime,
308 events->gpuCompositionDoneFence,
309 events->hasGpuCompositionDoneInfo());
310 getFrameTimestampFence(outDisplayPresentTime, events->displayPresentFence,
311 events->hasDisplayPresentInfo());
312 getFrameTimestampFence(outReleaseTime, events->releaseFence,
313 events->hasReleaseInfo());
Brian Anderson069b3652016-07-22 10:32:47 -0700314
315 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800316}
317
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600318using namespace android::hardware::configstore;
319using namespace android::hardware::configstore::V1_0;
320
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700321status_t Surface::getWideColorSupport(bool* supported) {
322 ATRACE_CALL();
323
324 sp<IBinder> display(
325 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
326 Vector<android_color_mode_t> colorModes;
327 status_t err =
328 composerService()->getDisplayColorModes(display, &colorModes);
329
330 if (err)
331 return err;
332
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600333 bool wideColorBoardConfig =
334 getBool<ISurfaceFlingerConfigs,
335 &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
336
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700337 *supported = false;
338 for (android_color_mode_t colorMode : colorModes) {
339 switch (colorMode) {
340 case HAL_COLOR_MODE_DISPLAY_P3:
341 case HAL_COLOR_MODE_ADOBE_RGB:
342 case HAL_COLOR_MODE_DCI_P3:
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600343 if (wideColorBoardConfig) {
344 *supported = true;
345 }
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700346 break;
347 default:
348 break;
349 }
350 }
351
352 return NO_ERROR;
353}
354
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700355status_t Surface::getHdrSupport(bool* supported) {
356 ATRACE_CALL();
357
358 sp<IBinder> display(
359 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
360 HdrCapabilities hdrCapabilities;
361 status_t err =
362 composerService()->getHdrCapabilities(display, &hdrCapabilities);
363
364 if (err)
365 return err;
366
367 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
368
369 return NO_ERROR;
370}
371
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800372int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
373 Surface* c = getSelf(window);
374 return c->setSwapInterval(interval);
375}
376
377int Surface::hook_dequeueBuffer(ANativeWindow* window,
378 ANativeWindowBuffer** buffer, int* fenceFd) {
379 Surface* c = getSelf(window);
380 return c->dequeueBuffer(buffer, fenceFd);
381}
382
383int Surface::hook_cancelBuffer(ANativeWindow* window,
384 ANativeWindowBuffer* buffer, int fenceFd) {
385 Surface* c = getSelf(window);
386 return c->cancelBuffer(buffer, fenceFd);
387}
388
389int Surface::hook_queueBuffer(ANativeWindow* window,
390 ANativeWindowBuffer* buffer, int fenceFd) {
391 Surface* c = getSelf(window);
392 return c->queueBuffer(buffer, fenceFd);
393}
394
395int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
396 ANativeWindowBuffer** buffer) {
397 Surface* c = getSelf(window);
398 ANativeWindowBuffer* buf;
399 int fenceFd = -1;
400 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600401 if (result != OK) {
402 return result;
403 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800404 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700405 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800406 if (waitResult != OK) {
407 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
408 waitResult);
409 c->cancelBuffer(buf, -1);
410 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700411 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800412 *buffer = buf;
413 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700414}
415
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800416int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
417 ANativeWindowBuffer* buffer) {
418 Surface* c = getSelf(window);
419 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700420}
421
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800422int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
423 ANativeWindowBuffer* buffer) {
424 Surface* c = getSelf(window);
425 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700426}
427
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800428int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
429 ANativeWindowBuffer* buffer) {
430 Surface* c = getSelf(window);
431 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700432}
Mathias Agopian62185b72009-04-16 16:19:50 -0700433
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800434int Surface::hook_query(const ANativeWindow* window,
435 int what, int* value) {
436 const Surface* c = getSelf(window);
437 return c->query(what, value);
438}
439
440int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
441 va_list args;
442 va_start(args, operation);
443 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700444 int result = c->perform(operation, args);
445 va_end(args);
446 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800447}
448
449int Surface::setSwapInterval(int interval) {
450 ATRACE_CALL();
451 // EGL specification states:
452 // interval is silently clamped to minimum and maximum implementation
453 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800454
455 if (interval < minSwapInterval)
456 interval = minSwapInterval;
457
458 if (interval > maxSwapInterval)
459 interval = maxSwapInterval;
460
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700461 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700462 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800463
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700464 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800465}
466
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700467int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800468 ATRACE_CALL();
469 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800470
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800471 uint32_t reqWidth;
472 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800473 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800474 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700475 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800476
477 {
478 Mutex::Autolock lock(mMutex);
479
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800480 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
481 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800482
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800483 reqFormat = mReqFormat;
484 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800485
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700486 enableFrameTimestamps = mEnableFrameTimestamps;
487
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700488 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800489 BufferItem::INVALID_BUFFER_SLOT) {
490 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
491 if (gbuf != NULL) {
492 *buffer = gbuf.get();
493 *fenceFd = -1;
494 return OK;
495 }
496 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800497 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
498
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800499 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800500 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700501 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700502
503 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700504 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700505 reqWidth, reqHeight, reqFormat, reqUsage,
506 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700507 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800508
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800509 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700510 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
511 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
512 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800513 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700514 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800515
516 Mutex::Autolock lock(mMutex);
517
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800518 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700519
520 // this should never happen
521 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
522
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800523 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
524 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700525 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700526
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700527 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700528 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700529 }
530
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700531 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
532 if (mReportRemovedBuffers && (gbuf != nullptr)) {
533 mRemovedBuffers.clear();
534 mRemovedBuffers.push_back(gbuf);
535 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800536 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
537 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700538 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700539 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800540 return result;
541 }
542 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700543
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800544 if (fence->isValid()) {
545 *fenceFd = fence->dup();
546 if (*fenceFd == -1) {
547 ALOGE("dequeueBuffer: error duping fence: %d", errno);
548 // dup() should never fail; something is badly wrong. Soldier on
549 // and hope for the best; the worst that should happen is some
550 // visible corruption that lasts until the next frame.
551 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700552 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800553 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700554 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800555
556 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800557
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700558 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800559 mSharedBufferSlot = buf;
560 mSharedBufferHasBeenQueued = false;
561 } else if (mSharedBufferSlot == buf) {
562 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
563 mSharedBufferHasBeenQueued = false;
564 }
565
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800566 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700567}
568
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800569int Surface::cancelBuffer(android_native_buffer_t* buffer,
570 int fenceFd) {
571 ATRACE_CALL();
572 ALOGV("Surface::cancelBuffer");
573 Mutex::Autolock lock(mMutex);
574 int i = getSlotFromBufferLocked(buffer);
575 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900576 if (fenceFd >= 0) {
577 close(fenceFd);
578 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800579 return i;
580 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800581 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
582 if (fenceFd >= 0) {
583 close(fenceFd);
584 }
585 return OK;
586 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800587 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
588 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800589
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700590 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800591 mSharedBufferHasBeenQueued = true;
592 }
593
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800594 return OK;
595}
596
597int Surface::getSlotFromBufferLocked(
598 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800599 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
600 if (mSlots[i].buffer != NULL &&
601 mSlots[i].buffer->handle == buffer->handle) {
602 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700603 }
604 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800605 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
606 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700607}
608
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800609int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800610 ALOGV("Surface::lockBuffer");
611 Mutex::Autolock lock(mMutex);
612 return OK;
613}
Mathias Agopian631f3582010-05-25 17:51:34 -0700614
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800615int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
616 ATRACE_CALL();
617 ALOGV("Surface::queueBuffer");
618 Mutex::Autolock lock(mMutex);
619 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700620 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800621
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800622 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700623 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700624 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700625 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700626 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800627 } else {
628 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700629 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800630 int i = getSlotFromBufferLocked(buffer);
631 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900632 if (fenceFd >= 0) {
633 close(fenceFd);
634 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800635 return i;
636 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800637 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
638 if (fenceFd >= 0) {
639 close(fenceFd);
640 }
641 return OK;
642 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800645 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700646 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800647 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800649 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
650 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700651 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800652 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700653 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700654
Dan Stozac62acbd2015-04-21 16:42:49 -0700655 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700656 input.setSurfaceDamage(Region::INVALID_REGION);
657 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700658 // Here we do two things:
659 // 1) The surface damage was specified using the OpenGL ES convention of
660 // the origin being in the bottom-left corner. Here we flip to the
661 // convention that the rest of the system uses (top-left corner) by
662 // subtracting all top/bottom coordinates from the buffer height.
663 // 2) If the buffer is coming in rotated (for example, because the EGL
664 // implementation is reacting to the transform hint coming back from
665 // SurfaceFlinger), the surface damage needs to be rotated the
666 // opposite direction, since it was generated assuming an unrotated
667 // buffer (the app doesn't know that the EGL implementation is
668 // reacting to the transform hint behind its back). The
669 // transformations in the switch statement below apply those
670 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
671
672 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700673 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700674 bool rotated90 = (mTransform ^ mStickyTransform) &
675 NATIVE_WINDOW_TRANSFORM_ROT_90;
676 if (rotated90) {
677 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700678 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700679
Dan Stoza5065a552015-03-17 16:23:42 -0700680 Region flippedRegion;
681 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700682 int left = rect.left;
683 int right = rect.right;
684 int top = height - rect.bottom; // Flip from OpenGL convention
685 int bottom = height - rect.top; // Flip from OpenGL convention
686 switch (mTransform ^ mStickyTransform) {
687 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
688 // Rotate 270 degrees
689 Rect flippedRect{top, width - right, bottom, width - left};
690 flippedRegion.orSelf(flippedRect);
691 break;
692 }
693 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
694 // Rotate 180 degrees
695 Rect flippedRect{width - right, height - bottom,
696 width - left, height - top};
697 flippedRegion.orSelf(flippedRect);
698 break;
699 }
700 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
701 // Rotate 90 degrees
702 Rect flippedRect{height - bottom, left,
703 height - top, right};
704 flippedRegion.orSelf(flippedRect);
705 break;
706 }
707 default: {
708 Rect flippedRect{left, top, right, bottom};
709 flippedRegion.orSelf(flippedRect);
710 break;
711 }
712 }
Dan Stoza5065a552015-03-17 16:23:42 -0700713 }
714
715 input.setSurfaceDamage(flippedRegion);
716 }
717
Dan Stoza70ccba52016-07-01 14:00:40 -0700718 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800719 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700720 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800721 if (err != OK) {
722 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
723 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800724
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700725 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700726 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700727 // Update timestamps with the local acquire fence.
728 // The consumer doesn't send it back to prevent us from having two
729 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700730 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700731 std::make_shared<FenceTime>(std::move(fence)));
732
733 // Cache timestamps of signaled fences so we can close their file
734 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700735 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700736 }
737
Brian Anderson50143b32016-09-30 14:01:24 -0700738 mLastFrameNumber = mNextFrameNumber;
739
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700740 mDefaultWidth = output.width;
741 mDefaultHeight = output.height;
742 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700743
Ruben Brunk1681d952014-06-27 15:51:55 -0700744 // Disable transform hint if sticky transform is set.
745 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700746 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700747 }
748
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700749 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700750
Dan Stozac62acbd2015-04-21 16:42:49 -0700751 if (!mConnectedToCpu) {
752 // Clear surface damage back to full-buffer
753 mDirtyRegion = Region::INVALID_REGION;
754 }
Dan Stoza5065a552015-03-17 16:23:42 -0700755
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700756 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800757 mSharedBufferHasBeenQueued = true;
758 }
759
Robert Carr9f31e292016-04-11 11:15:32 -0700760 mQueueBufferCondition.broadcast();
761
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800762 return err;
763}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700764
Brian Anderson6b376712017-04-04 10:51:39 -0700765void Surface::querySupportedTimestampsLocked() const {
766 // mMutex must be locked when calling this method.
767
768 if (mQueriedSupportedTimestamps) {
769 return;
770 }
771 mQueriedSupportedTimestamps = true;
772
773 std::vector<FrameEvent> supportedFrameTimestamps;
774 status_t err = composerService()->getSupportedFrameTimestamps(
775 &supportedFrameTimestamps);
776
777 if (err != NO_ERROR) {
778 return;
779 }
780
781 for (auto sft : supportedFrameTimestamps) {
782 if (sft == FrameEvent::DISPLAY_PRESENT) {
783 mFrameTimestampsSupportsPresent = true;
784 }
785 }
786}
787
Mathias Agopiana67932f2011-04-20 14:20:59 -0700788int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800789 ATRACE_CALL();
790 ALOGV("Surface::query");
791 { // scope for the lock
792 Mutex::Autolock lock(mMutex);
793 switch (what) {
794 case NATIVE_WINDOW_FORMAT:
795 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800796 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800797 return NO_ERROR;
798 }
799 break;
800 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700801 if (composerService()->authenticateSurfaceTexture(
802 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800803 *value = 1;
804 } else {
805 *value = 0;
806 }
807 return NO_ERROR;
808 }
809 case NATIVE_WINDOW_CONCRETE_TYPE:
810 *value = NATIVE_WINDOW_SURFACE;
811 return NO_ERROR;
812 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800813 *value = static_cast<int>(
814 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800815 return NO_ERROR;
816 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800817 *value = static_cast<int>(
818 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800819 return NO_ERROR;
820 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800821 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800822 return NO_ERROR;
823 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
824 status_t err = NO_ERROR;
825 if (!mConsumerRunningBehind) {
826 *value = 0;
827 } else {
828 err = mGraphicBufferProducer->query(what, value);
829 if (err == NO_ERROR) {
830 mConsumerRunningBehind = *value;
831 }
832 }
833 return err;
834 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700835 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
836 int64_t durationUs = mLastDequeueDuration / 1000;
837 *value = durationUs > std::numeric_limits<int>::max() ?
838 std::numeric_limits<int>::max() :
839 static_cast<int>(durationUs);
840 return NO_ERROR;
841 }
842 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
843 int64_t durationUs = mLastQueueDuration / 1000;
844 *value = durationUs > std::numeric_limits<int>::max() ?
845 std::numeric_limits<int>::max() :
846 static_cast<int>(durationUs);
847 return NO_ERROR;
848 }
Brian Anderson6b376712017-04-04 10:51:39 -0700849 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
850 querySupportedTimestampsLocked();
851 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
852 return NO_ERROR;
853 }
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800854 case NATIVE_WINDOW_IS_VALID: {
855 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
856 return NO_ERROR;
857 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800858 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700859 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800860 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800861}
862
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800863int Surface::perform(int operation, va_list args)
864{
865 int res = NO_ERROR;
866 switch (operation) {
867 case NATIVE_WINDOW_CONNECT:
868 // deprecated. must return NO_ERROR.
869 break;
870 case NATIVE_WINDOW_DISCONNECT:
871 // deprecated. must return NO_ERROR.
872 break;
873 case NATIVE_WINDOW_SET_USAGE:
874 res = dispatchSetUsage(args);
875 break;
876 case NATIVE_WINDOW_SET_CROP:
877 res = dispatchSetCrop(args);
878 break;
879 case NATIVE_WINDOW_SET_BUFFER_COUNT:
880 res = dispatchSetBufferCount(args);
881 break;
882 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
883 res = dispatchSetBuffersGeometry(args);
884 break;
885 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
886 res = dispatchSetBuffersTransform(args);
887 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700888 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
889 res = dispatchSetBuffersStickyTransform(args);
890 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800891 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
892 res = dispatchSetBuffersTimestamp(args);
893 break;
894 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
895 res = dispatchSetBuffersDimensions(args);
896 break;
897 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
898 res = dispatchSetBuffersUserDimensions(args);
899 break;
900 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
901 res = dispatchSetBuffersFormat(args);
902 break;
903 case NATIVE_WINDOW_LOCK:
904 res = dispatchLock(args);
905 break;
906 case NATIVE_WINDOW_UNLOCK_AND_POST:
907 res = dispatchUnlockAndPost(args);
908 break;
909 case NATIVE_WINDOW_SET_SCALING_MODE:
910 res = dispatchSetScalingMode(args);
911 break;
912 case NATIVE_WINDOW_API_CONNECT:
913 res = dispatchConnect(args);
914 break;
915 case NATIVE_WINDOW_API_DISCONNECT:
916 res = dispatchDisconnect(args);
917 break;
Rachad7cb0d392014-07-29 17:53:53 -0700918 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
919 res = dispatchSetSidebandStream(args);
920 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800921 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
922 res = dispatchSetBuffersDataSpace(args);
923 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700924 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
925 res = dispatchSetSurfaceDamage(args);
926 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700927 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
928 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700929 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800930 case NATIVE_WINDOW_SET_AUTO_REFRESH:
931 res = dispatchSetAutoRefresh(args);
932 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800933 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
934 res = dispatchGetDisplayRefreshCycleDuration(args);
935 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800936 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
937 res = dispatchGetNextFrameId(args);
938 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700939 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
940 res = dispatchEnableFrameTimestamps(args);
941 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800942 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
943 res = dispatchGetCompositorTiming(args);
944 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800945 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
946 res = dispatchGetFrameTimestamps(args);
947 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700948 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
949 res = dispatchGetWideColorSupport(args);
950 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700951 case NATIVE_WINDOW_GET_HDR_SUPPORT:
952 res = dispatchGetHdrSupport(args);
953 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800954 default:
955 res = NAME_NOT_FOUND;
956 break;
957 }
958 return res;
959}
Mathias Agopiana138f892010-05-21 17:24:35 -0700960
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800961int Surface::dispatchConnect(va_list args) {
962 int api = va_arg(args, int);
963 return connect(api);
964}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800965
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800966int Surface::dispatchDisconnect(va_list args) {
967 int api = va_arg(args, int);
968 return disconnect(api);
969}
970
971int Surface::dispatchSetUsage(va_list args) {
972 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800973 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800974}
975
976int Surface::dispatchSetCrop(va_list args) {
977 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
978 return setCrop(reinterpret_cast<Rect const*>(rect));
979}
980
981int Surface::dispatchSetBufferCount(va_list args) {
982 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800983 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800984}
985
986int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800987 uint32_t width = va_arg(args, uint32_t);
988 uint32_t height = va_arg(args, uint32_t);
989 PixelFormat format = va_arg(args, PixelFormat);
990 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800991 if (err != 0) {
992 return err;
993 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800994 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800995}
996
997int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800998 uint32_t width = va_arg(args, uint32_t);
999 uint32_t height = va_arg(args, uint32_t);
1000 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001001}
1002
1003int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001004 uint32_t width = va_arg(args, uint32_t);
1005 uint32_t height = va_arg(args, uint32_t);
1006 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001007}
1008
1009int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001010 PixelFormat format = va_arg(args, PixelFormat);
1011 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001012}
1013
1014int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001015 int mode = va_arg(args, int);
1016 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001017}
1018
1019int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001020 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001021 return setBuffersTransform(transform);
1022}
1023
Ruben Brunk1681d952014-06-27 15:51:55 -07001024int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001025 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -07001026 return setBuffersStickyTransform(transform);
1027}
1028
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001029int Surface::dispatchSetBuffersTimestamp(va_list args) {
1030 int64_t timestamp = va_arg(args, int64_t);
1031 return setBuffersTimestamp(timestamp);
1032}
1033
1034int Surface::dispatchLock(va_list args) {
1035 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1036 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1037 return lock(outBuffer, inOutDirtyBounds);
1038}
1039
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001040int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001041 return unlockAndPost();
1042}
1043
Rachad7cb0d392014-07-29 17:53:53 -07001044int Surface::dispatchSetSidebandStream(va_list args) {
1045 native_handle_t* sH = va_arg(args, native_handle_t*);
1046 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1047 setSidebandStream(sidebandHandle);
1048 return OK;
1049}
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001050
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001051int Surface::dispatchSetBuffersDataSpace(va_list args) {
1052 android_dataspace dataspace =
1053 static_cast<android_dataspace>(va_arg(args, int));
1054 return setBuffersDataSpace(dataspace);
1055}
1056
Dan Stoza5065a552015-03-17 16:23:42 -07001057int Surface::dispatchSetSurfaceDamage(va_list args) {
1058 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1059 size_t numRects = va_arg(args, size_t);
1060 setSurfaceDamage(rects, numRects);
1061 return NO_ERROR;
1062}
1063
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001064int Surface::dispatchSetSharedBufferMode(va_list args) {
1065 bool sharedBufferMode = va_arg(args, int);
1066 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001067}
1068
1069int Surface::dispatchSetAutoRefresh(va_list args) {
1070 bool autoRefresh = va_arg(args, int);
1071 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001072}
1073
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001074int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1075 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1076 return getDisplayRefreshCycleDuration(outRefreshDuration);
1077}
1078
Brian Anderson1049d1d2016-12-16 17:25:57 -08001079int Surface::dispatchGetNextFrameId(va_list args) {
1080 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1081 *nextFrameId = getNextFrameNumber();
1082 return NO_ERROR;
1083}
1084
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001085int Surface::dispatchEnableFrameTimestamps(va_list args) {
1086 bool enable = va_arg(args, int);
1087 enableFrameTimestamps(enable);
1088 return NO_ERROR;
1089}
1090
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001091int Surface::dispatchGetCompositorTiming(va_list args) {
1092 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1093 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1094 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1095 return getCompositorTiming(compositeDeadline, compositeInterval,
1096 compositeToPresentLatency);
1097}
1098
Pablo Ceballosce796e72016-02-04 19:10:51 -08001099int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001100 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001101 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001102 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001103 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1104 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1105 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001106 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001107 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001108 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001109 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001110 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001111 outRequestedPresentTime, outAcquireTime, outLatchTime,
1112 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001113 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001114 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001115}
1116
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001117int Surface::dispatchGetWideColorSupport(va_list args) {
1118 bool* outSupport = va_arg(args, bool*);
1119 return getWideColorSupport(outSupport);
1120}
1121
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001122int Surface::dispatchGetHdrSupport(va_list args) {
1123 bool* outSupport = va_arg(args, bool*);
1124 return getHdrSupport(outSupport);
1125}
1126
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001127int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001128 static sp<IProducerListener> listener = new DummyProducerListener();
1129 return connect(api, listener);
1130}
1131
1132int Surface::connect(int api, const sp<IProducerListener>& listener) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001133 return connect(api, listener, false);
1134}
1135
1136int Surface::connect(
1137 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001138 ATRACE_CALL();
1139 ALOGV("Surface::connect");
1140 Mutex::Autolock lock(mMutex);
1141 IGraphicBufferProducer::QueueBufferOutput output;
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001142 mReportRemovedBuffers = reportBufferRemoval;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001143 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001144 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001145 mDefaultWidth = output.width;
1146 mDefaultHeight = output.height;
1147 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001148
1149 // Disable transform hint if sticky transform is set.
1150 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001151 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001152 }
1153
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001154 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001155 }
1156 if (!err && api == NATIVE_WINDOW_API_CPU) {
1157 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001158 // Clear the dirty region in case we're switching from a non-CPU API
1159 mDirtyRegion.clear();
1160 } else if (!err) {
1161 // Initialize the dirty region for tracking surface damage
1162 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001163 }
Dan Stoza5065a552015-03-17 16:23:42 -07001164
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001165 return err;
1166}
1167
Mathias Agopian365857d2013-09-11 19:35:45 -07001168
Robert Carr97b9c862016-09-08 13:54:35 -07001169int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001170 ATRACE_CALL();
1171 ALOGV("Surface::disconnect");
1172 Mutex::Autolock lock(mMutex);
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001173 mRemovedBuffers.clear();
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001174 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1175 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001176 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001177 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001178 if (!err) {
1179 mReqFormat = 0;
1180 mReqWidth = 0;
1181 mReqHeight = 0;
1182 mReqUsage = 0;
1183 mCrop.clear();
1184 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1185 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001186 mStickyTransform = 0;
1187
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001188 if (api == NATIVE_WINDOW_API_CPU) {
1189 mConnectedToCpu = false;
1190 }
1191 }
1192 return err;
1193}
1194
Dan Stozad9c49712015-04-27 11:06:01 -07001195int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001196 sp<Fence>* outFence) {
1197 ATRACE_CALL();
1198 ALOGV("Surface::detachNextBuffer");
1199
1200 if (outBuffer == NULL || outFence == NULL) {
1201 return BAD_VALUE;
1202 }
1203
1204 Mutex::Autolock lock(mMutex);
1205
1206 sp<GraphicBuffer> buffer(NULL);
1207 sp<Fence> fence(NULL);
1208 status_t result = mGraphicBufferProducer->detachNextBuffer(
1209 &buffer, &fence);
1210 if (result != NO_ERROR) {
1211 return result;
1212 }
1213
Dan Stozad9c49712015-04-27 11:06:01 -07001214 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001215 if (fence != NULL && fence->isValid()) {
1216 *outFence = fence;
1217 } else {
1218 *outFence = Fence::NO_FENCE;
1219 }
1220
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001221 if (mReportRemovedBuffers) {
1222 mRemovedBuffers.clear();
1223 }
1224
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001225 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1226 if (mSlots[i].buffer != NULL &&
1227 mSlots[i].buffer->handle == buffer->handle) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001228 if (mReportRemovedBuffers) {
1229 mRemovedBuffers.push_back(mSlots[i].buffer);
1230 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001231 mSlots[i].buffer = NULL;
1232 }
1233 }
1234
Dan Stoza231832e2015-03-11 11:55:01 -07001235 return NO_ERROR;
1236}
1237
1238int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1239{
1240 ATRACE_CALL();
1241 ALOGV("Surface::attachBuffer");
1242
1243 Mutex::Autolock lock(mMutex);
1244
1245 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001246 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1247 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001248 int32_t attachedSlot = -1;
1249 status_t result = mGraphicBufferProducer->attachBuffer(
1250 &attachedSlot, graphicBuffer);
1251 if (result != NO_ERROR) {
1252 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001253 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001254 return result;
1255 }
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001256 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
1257 mRemovedBuffers.clear();
1258 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1259 }
Dan Stoza231832e2015-03-11 11:55:01 -07001260 mSlots[attachedSlot].buffer = graphicBuffer;
1261
1262 return NO_ERROR;
1263}
1264
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001265int Surface::setUsage(uint32_t reqUsage)
1266{
1267 ALOGV("Surface::setUsage");
1268 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001269 if (reqUsage != mReqUsage) {
1270 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1271 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001272 mReqUsage = reqUsage;
1273 return OK;
1274}
1275
1276int Surface::setCrop(Rect const* rect)
1277{
1278 ATRACE_CALL();
1279
Pablo Ceballos60d69222015-08-07 14:47:20 -07001280 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001281 if (rect == NULL || rect->isEmpty()) {
1282 realRect.clear();
1283 } else {
1284 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001285 }
1286
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001287 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1288 realRect.left, realRect.top, realRect.right, realRect.bottom);
1289
1290 Mutex::Autolock lock(mMutex);
1291 mCrop = realRect;
1292 return NO_ERROR;
1293}
1294
1295int Surface::setBufferCount(int bufferCount)
1296{
1297 ATRACE_CALL();
1298 ALOGV("Surface::setBufferCount");
1299 Mutex::Autolock lock(mMutex);
1300
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001301 status_t err = NO_ERROR;
1302 if (bufferCount == 0) {
1303 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1304 } else {
1305 int minUndequeuedBuffers = 0;
1306 err = mGraphicBufferProducer->query(
1307 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1308 if (err == NO_ERROR) {
1309 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1310 bufferCount - minUndequeuedBuffers);
1311 }
1312 }
Mathias Agopian90147262010-01-22 11:47:55 -08001313
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001314 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1315 bufferCount, strerror(-err));
1316
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001317 return err;
1318}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001319
Pablo Ceballosfa455352015-08-12 17:47:47 -07001320int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1321 ATRACE_CALL();
1322 ALOGV("Surface::setMaxDequeuedBufferCount");
1323 Mutex::Autolock lock(mMutex);
1324
1325 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1326 maxDequeuedBuffers);
1327 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1328 "returned %s", maxDequeuedBuffers, strerror(-err));
1329
Pablo Ceballosfa455352015-08-12 17:47:47 -07001330 return err;
1331}
1332
1333int Surface::setAsyncMode(bool async) {
1334 ATRACE_CALL();
1335 ALOGV("Surface::setAsyncMode");
1336 Mutex::Autolock lock(mMutex);
1337
1338 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1339 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1340 async, strerror(-err));
1341
Pablo Ceballosfa455352015-08-12 17:47:47 -07001342 return err;
1343}
1344
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001345int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001346 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001347 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001348 Mutex::Autolock lock(mMutex);
1349
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001350 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1351 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001352 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001353 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001354 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001355 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1356 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001357
1358 return err;
1359}
1360
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001361int Surface::setAutoRefresh(bool autoRefresh) {
1362 ATRACE_CALL();
1363 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1364 Mutex::Autolock lock(mMutex);
1365
1366 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1367 if (err == NO_ERROR) {
1368 mAutoRefresh = autoRefresh;
1369 }
1370 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1371 autoRefresh, strerror(-err));
1372 return err;
1373}
1374
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001375int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001376{
1377 ATRACE_CALL();
1378 ALOGV("Surface::setBuffersDimensions");
1379
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001380 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001381 return BAD_VALUE;
1382
1383 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001384 if (width != mReqWidth || height != mReqHeight) {
1385 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1386 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001387 mReqWidth = width;
1388 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001389 return NO_ERROR;
1390}
1391
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001392int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001393{
1394 ATRACE_CALL();
1395 ALOGV("Surface::setBuffersUserDimensions");
1396
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001397 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001398 return BAD_VALUE;
1399
1400 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001401 if (width != mUserWidth || height != mUserHeight) {
1402 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1403 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001404 mUserWidth = width;
1405 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001406 return NO_ERROR;
1407}
1408
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001409int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001410{
1411 ALOGV("Surface::setBuffersFormat");
1412
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001413 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001414 if (format != mReqFormat) {
1415 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1416 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001417 mReqFormat = format;
1418 return NO_ERROR;
1419}
1420
1421int Surface::setScalingMode(int mode)
1422{
1423 ATRACE_CALL();
1424 ALOGV("Surface::setScalingMode(%d)", mode);
1425
1426 switch (mode) {
1427 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1428 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1429 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001430 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001431 break;
1432 default:
1433 ALOGE("unknown scaling mode: %d", mode);
1434 return BAD_VALUE;
1435 }
1436
1437 Mutex::Autolock lock(mMutex);
1438 mScalingMode = mode;
1439 return NO_ERROR;
1440}
1441
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001442int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001443{
1444 ATRACE_CALL();
1445 ALOGV("Surface::setBuffersTransform");
1446 Mutex::Autolock lock(mMutex);
1447 mTransform = transform;
1448 return NO_ERROR;
1449}
1450
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001451int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001452{
1453 ATRACE_CALL();
1454 ALOGV("Surface::setBuffersStickyTransform");
1455 Mutex::Autolock lock(mMutex);
1456 mStickyTransform = transform;
1457 return NO_ERROR;
1458}
1459
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001460int Surface::setBuffersTimestamp(int64_t timestamp)
1461{
1462 ALOGV("Surface::setBuffersTimestamp");
1463 Mutex::Autolock lock(mMutex);
1464 mTimestamp = timestamp;
1465 return NO_ERROR;
1466}
1467
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001468int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1469{
1470 ALOGV("Surface::setBuffersDataSpace");
1471 Mutex::Autolock lock(mMutex);
1472 mDataSpace = dataSpace;
1473 return NO_ERROR;
1474}
1475
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001476void Surface::freeAllBuffers() {
1477 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1478 mSlots[i].buffer = 0;
1479 }
1480}
1481
Dan Stoza5065a552015-03-17 16:23:42 -07001482void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1483 ATRACE_CALL();
1484 ALOGV("Surface::setSurfaceDamage");
1485 Mutex::Autolock lock(mMutex);
1486
Dan Stozac62acbd2015-04-21 16:42:49 -07001487 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001488 mDirtyRegion = Region::INVALID_REGION;
1489 return;
1490 }
1491
1492 mDirtyRegion.clear();
1493 for (size_t r = 0; r < numRects; ++r) {
1494 // We intentionally flip top and bottom here, since because they're
1495 // specified with a bottom-left origin, top > bottom, which fails
1496 // validation in the Region class. We will fix this up when we flip to a
1497 // top-left origin in queueBuffer.
1498 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1499 mDirtyRegion.orSelf(rect);
1500 }
1501}
1502
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001503// ----------------------------------------------------------------------
1504// the lock/unlock APIs must be used from the same thread
1505
1506static status_t copyBlt(
1507 const sp<GraphicBuffer>& dst,
1508 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001509 const Region& reg,
1510 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001511{
1512 // src and dst with, height and format must be identical. no verification
1513 // is done here.
1514 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001515 uint8_t* src_bits = NULL;
1516 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1517 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001518 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1519
1520 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001521 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1522 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001523 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001524 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001525
1526 Region::const_iterator head(reg.begin());
1527 Region::const_iterator tail(reg.end());
1528 if (head != tail && src_bits && dst_bits) {
1529 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001530 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1531 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001532
1533 while (head != tail) {
1534 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001535 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001536 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001537 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1538 uint8_t const * s = src_bits +
1539 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1540 uint8_t * d = dst_bits +
1541 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001542 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001543 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001544 h = 1;
1545 }
1546 do {
1547 memcpy(d, s, size);
1548 d += dbpr;
1549 s += sbpr;
1550 } while (--h > 0);
1551 }
1552 }
1553
1554 if (src_bits)
1555 src->unlock();
1556
1557 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001558 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001559
1560 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001561}
1562
Mathias Agopiana138f892010-05-21 17:24:35 -07001563// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001564
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001565status_t Surface::lock(
1566 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1567{
1568 if (mLockedBuffer != 0) {
1569 ALOGE("Surface::lock failed, already locked");
1570 return INVALID_OPERATION;
1571 }
1572
1573 if (!mConnectedToCpu) {
1574 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1575 if (err) {
1576 return err;
1577 }
1578 // we're intending to do software rendering from this point
1579 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1580 }
1581
1582 ANativeWindowBuffer* out;
1583 int fenceFd = -1;
1584 status_t err = dequeueBuffer(&out, &fenceFd);
1585 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1586 if (err == NO_ERROR) {
1587 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001588 const Rect bounds(backBuffer->width, backBuffer->height);
1589
1590 Region newDirtyRegion;
1591 if (inOutDirtyBounds) {
1592 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1593 newDirtyRegion.andSelf(bounds);
1594 } else {
1595 newDirtyRegion.set(bounds);
1596 }
1597
1598 // figure out if we can copy the frontbuffer back
1599 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1600 const bool canCopyBack = (frontBuffer != 0 &&
1601 backBuffer->width == frontBuffer->width &&
1602 backBuffer->height == frontBuffer->height &&
1603 backBuffer->format == frontBuffer->format);
1604
1605 if (canCopyBack) {
1606 // copy the area that is invalid and not repainted this round
1607 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001608 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001609 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001610 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001611 } else {
1612 // if we can't copy-back anything, modify the user's dirty
1613 // region to make sure they redraw the whole buffer
1614 newDirtyRegion.set(bounds);
1615 mDirtyRegion.clear();
1616 Mutex::Autolock lock(mMutex);
1617 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1618 mSlots[i].dirtyRegion.clear();
1619 }
1620 }
1621
1622
1623 { // scope for the lock
1624 Mutex::Autolock lock(mMutex);
1625 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1626 if (backBufferSlot >= 0) {
1627 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1628 mDirtyRegion.subtract(dirtyRegion);
1629 dirtyRegion = newDirtyRegion;
1630 }
1631 }
1632
1633 mDirtyRegion.orSelf(newDirtyRegion);
1634 if (inOutDirtyBounds) {
1635 *inOutDirtyBounds = newDirtyRegion.getBounds();
1636 }
1637
1638 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001639 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001640 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001641 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001642
1643 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1644 backBuffer->handle);
1645
1646 if (res != 0) {
1647 err = INVALID_OPERATION;
1648 } else {
1649 mLockedBuffer = backBuffer;
1650 outBuffer->width = backBuffer->width;
1651 outBuffer->height = backBuffer->height;
1652 outBuffer->stride = backBuffer->stride;
1653 outBuffer->format = backBuffer->format;
1654 outBuffer->bits = vaddr;
1655 }
1656 }
1657 return err;
1658}
1659
1660status_t Surface::unlockAndPost()
1661{
1662 if (mLockedBuffer == 0) {
1663 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1664 return INVALID_OPERATION;
1665 }
1666
Francis Hart8f396012014-04-01 15:30:53 +03001667 int fd = -1;
1668 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001669 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1670
Francis Hart8f396012014-04-01 15:30:53 +03001671 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001672 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1673 mLockedBuffer->handle, strerror(-err));
1674
1675 mPostedBuffer = mLockedBuffer;
1676 mLockedBuffer = 0;
1677 return err;
1678}
1679
Robert Carr9f31e292016-04-11 11:15:32 -07001680bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1681 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001682 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001683 return true;
1684 }
1685 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1686}
1687
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001688status_t Surface::getUniqueId(uint64_t* outId) const {
1689 Mutex::Autolock lock(mMutex);
1690 return mGraphicBufferProducer->getUniqueId(outId);
1691}
1692
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001693status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1694 if (out == nullptr) {
1695 ALOGE("%s: out must not be null!", __FUNCTION__);
1696 return BAD_VALUE;
1697 }
1698
1699 Mutex::Autolock lock(mMutex);
1700 *out = mRemovedBuffers;
1701 mRemovedBuffers.clear();
1702 return OK;
1703}
1704
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705}; // namespace android