blob: a4aec6e6aa64a772019795f605c22cb324f0c529 [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
Ian Elliotta2eb34c2017-07-18 11:05:49 -060047Surface::Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp)
48 : mGraphicBufferProducer(bufferProducer),
49 mCrop(Rect::EMPTY_RECT),
50 mBufferAge(0),
51 mGenerationNumber(0),
52 mSharedBufferMode(false),
53 mAutoRefresh(false),
54 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
55 mSharedBufferHasBeenQueued(false),
56 mQueriedSupportedTimestamps(false),
57 mFrameTimestampsSupportsPresent(false),
58 mEnableFrameTimestamps(false),
59 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>()) {
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;
Chih-Hung Hsiehd66be0a2017-10-05 13:51:32 -0700156 status_t result = composerService()->getDisplayStats(NULL, &stats);
157 if (result != NO_ERROR) {
158 return result;
159 }
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800160
161 *outRefreshDuration = stats.vsyncPeriod;
162
163 return NO_ERROR;
164}
165
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700166void Surface::enableFrameTimestamps(bool enable) {
167 Mutex::Autolock lock(mMutex);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800168 // If going from disabled to enabled, get the initial values for
169 // compositor and display timing.
170 if (!mEnableFrameTimestamps && enable) {
171 FrameEventHistoryDelta delta;
172 mGraphicBufferProducer->getFrameTimestamps(&delta);
173 mFrameEventHistory->applyDelta(delta);
174 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700175 mEnableFrameTimestamps = enable;
176}
177
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800178status_t Surface::getCompositorTiming(
179 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
180 nsecs_t* compositeToPresentLatency) {
181 Mutex::Autolock lock(mMutex);
182 if (!mEnableFrameTimestamps) {
183 return INVALID_OPERATION;
184 }
185
186 if (compositeDeadline != nullptr) {
187 *compositeDeadline =
188 mFrameEventHistory->getNextCompositeDeadline(now());
189 }
190 if (compositeInterval != nullptr) {
191 *compositeInterval = mFrameEventHistory->getCompositeInterval();
192 }
193 if (compositeToPresentLatency != nullptr) {
194 *compositeToPresentLatency =
195 mFrameEventHistory->getCompositeToPresentLatency();
196 }
197 return NO_ERROR;
198}
199
Brian Anderson50143b32016-09-30 14:01:24 -0700200static bool checkConsumerForUpdates(
201 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700202 const nsecs_t* outLatchTime,
203 const nsecs_t* outFirstRefreshStartTime,
204 const nsecs_t* outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700205 const nsecs_t* outGpuCompositionDoneTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700206 const nsecs_t* outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700207 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700208 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700209 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
210 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700211 !e->hasFirstRefreshStartInfo();
Brian Andersonb04c6f02016-10-21 12:57:46 -0700212 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700213 !e->hasGpuCompositionDoneInfo();
214 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
215 !e->hasDisplayPresentInfo();
216
Brian Anderson6b376712017-04-04 10:51:39 -0700217 // LastRefreshStart, DequeueReady, and Release are never available for the
218 // last frame.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700219 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
220 !e->hasLastRefreshStartInfo() &&
221 (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700222 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
223 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700224 bool checkForRelease = (outReleaseTime != nullptr) &&
225 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
226
227 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700228 return checkForLatch || checkForFirstRefreshStart ||
Brian Andersonb04c6f02016-10-21 12:57:46 -0700229 checkForLastRefreshStart || checkForGpuCompositionDone ||
Brian Anderson4e606e32017-03-16 15:34:57 -0700230 checkForDisplayPresent || checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700231}
232
Brian Anderson3d4039d2016-09-23 16:31:30 -0700233static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
234 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700235 // We always get valid timestamps for these eventually.
236 *dst = (src == FrameEvents::TIMESTAMP_PENDING) ?
237 NATIVE_WINDOW_TIMESTAMP_PENDING : src;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700238 }
239}
240
Brian Andersondc96fdf2017-03-20 16:54:25 -0700241static void getFrameTimestampFence(nsecs_t *dst,
242 const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700243 if (dst != nullptr) {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700244 if (!fenceShouldBeKnown) {
245 *dst = NATIVE_WINDOW_TIMESTAMP_PENDING;
246 return;
247 }
248
Brian Anderson3d4039d2016-09-23 16:31:30 -0700249 nsecs_t signalTime = src->getSignalTime();
Brian Andersondc96fdf2017-03-20 16:54:25 -0700250 *dst = (signalTime == Fence::SIGNAL_TIME_PENDING) ?
251 NATIVE_WINDOW_TIMESTAMP_PENDING :
252 (signalTime == Fence::SIGNAL_TIME_INVALID) ?
253 NATIVE_WINDOW_TIMESTAMP_INVALID :
254 signalTime;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700255 }
256}
257
Brian Anderson069b3652016-07-22 10:32:47 -0700258status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700259 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700260 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700261 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700262 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
263 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800264 ATRACE_CALL();
265
Brian Anderson3890c392016-07-25 12:48:08 -0700266 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700267
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700268 if (!mEnableFrameTimestamps) {
269 return INVALID_OPERATION;
270 }
271
Brian Anderson6b376712017-04-04 10:51:39 -0700272 // Verify the requested timestamps are supported.
273 querySupportedTimestampsLocked();
274 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
275 return BAD_VALUE;
276 }
277
Brian Anderson3da8d272016-07-28 16:20:47 -0700278 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700279 if (events == nullptr) {
280 // If the entry isn't available in the producer, it's definitely not
281 // available in the consumer.
282 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700283 }
284
Brian Anderson50143b32016-09-30 14:01:24 -0700285 // Update our cache of events if the requested events are not available.
286 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700287 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700288 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700289 outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700290 FrameEventHistoryDelta delta;
291 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700292 mFrameEventHistory->applyDelta(delta);
293 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700294 }
295
Brian Anderson3890c392016-07-25 12:48:08 -0700296 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700297 // The entry was available before the update, but was overwritten
298 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700299 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800300 }
Brian Anderson069b3652016-07-22 10:32:47 -0700301
Brian Anderson3d4039d2016-09-23 16:31:30 -0700302 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700303 getFrameTimestamp(outLatchTime, events->latchTime);
304 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
305 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
306 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700307
Brian Andersondc96fdf2017-03-20 16:54:25 -0700308 getFrameTimestampFence(outAcquireTime, events->acquireFence,
309 events->hasAcquireInfo());
310 getFrameTimestampFence(outGpuCompositionDoneTime,
311 events->gpuCompositionDoneFence,
312 events->hasGpuCompositionDoneInfo());
313 getFrameTimestampFence(outDisplayPresentTime, events->displayPresentFence,
314 events->hasDisplayPresentInfo());
315 getFrameTimestampFence(outReleaseTime, events->releaseFence,
316 events->hasReleaseInfo());
Brian Anderson069b3652016-07-22 10:32:47 -0700317
318 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800319}
320
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600321using namespace android::hardware::configstore;
322using namespace android::hardware::configstore::V1_0;
323
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700324status_t Surface::getWideColorSupport(bool* supported) {
325 ATRACE_CALL();
326
327 sp<IBinder> display(
328 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
329 Vector<android_color_mode_t> colorModes;
330 status_t err =
331 composerService()->getDisplayColorModes(display, &colorModes);
332
333 if (err)
334 return err;
335
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600336 bool wideColorBoardConfig =
337 getBool<ISurfaceFlingerConfigs,
338 &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
339
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700340 *supported = false;
341 for (android_color_mode_t colorMode : colorModes) {
342 switch (colorMode) {
343 case HAL_COLOR_MODE_DISPLAY_P3:
344 case HAL_COLOR_MODE_ADOBE_RGB:
345 case HAL_COLOR_MODE_DCI_P3:
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600346 if (wideColorBoardConfig) {
347 *supported = true;
348 }
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700349 break;
350 default:
351 break;
352 }
353 }
354
355 return NO_ERROR;
356}
357
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700358status_t Surface::getHdrSupport(bool* supported) {
359 ATRACE_CALL();
360
361 sp<IBinder> display(
362 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
363 HdrCapabilities hdrCapabilities;
364 status_t err =
365 composerService()->getHdrCapabilities(display, &hdrCapabilities);
366
367 if (err)
368 return err;
369
370 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
371
372 return NO_ERROR;
373}
374
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800375int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
376 Surface* c = getSelf(window);
377 return c->setSwapInterval(interval);
378}
379
380int Surface::hook_dequeueBuffer(ANativeWindow* window,
381 ANativeWindowBuffer** buffer, int* fenceFd) {
382 Surface* c = getSelf(window);
383 return c->dequeueBuffer(buffer, fenceFd);
384}
385
386int Surface::hook_cancelBuffer(ANativeWindow* window,
387 ANativeWindowBuffer* buffer, int fenceFd) {
388 Surface* c = getSelf(window);
389 return c->cancelBuffer(buffer, fenceFd);
390}
391
392int Surface::hook_queueBuffer(ANativeWindow* window,
393 ANativeWindowBuffer* buffer, int fenceFd) {
394 Surface* c = getSelf(window);
395 return c->queueBuffer(buffer, fenceFd);
396}
397
398int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
399 ANativeWindowBuffer** buffer) {
400 Surface* c = getSelf(window);
401 ANativeWindowBuffer* buf;
402 int fenceFd = -1;
403 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600404 if (result != OK) {
405 return result;
406 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800407 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700408 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800409 if (waitResult != OK) {
410 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
411 waitResult);
412 c->cancelBuffer(buf, -1);
413 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700414 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800415 *buffer = buf;
416 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700417}
418
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800419int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
420 ANativeWindowBuffer* buffer) {
421 Surface* c = getSelf(window);
422 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700423}
424
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800425int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
426 ANativeWindowBuffer* buffer) {
427 Surface* c = getSelf(window);
428 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700429}
430
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800431int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
432 ANativeWindowBuffer* buffer) {
433 Surface* c = getSelf(window);
434 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700435}
Mathias Agopian62185b72009-04-16 16:19:50 -0700436
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800437int Surface::hook_query(const ANativeWindow* window,
438 int what, int* value) {
439 const Surface* c = getSelf(window);
440 return c->query(what, value);
441}
442
443int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
444 va_list args;
445 va_start(args, operation);
446 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700447 int result = c->perform(operation, args);
448 va_end(args);
449 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800450}
451
452int Surface::setSwapInterval(int interval) {
453 ATRACE_CALL();
454 // EGL specification states:
455 // interval is silently clamped to minimum and maximum implementation
456 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800457
458 if (interval < minSwapInterval)
459 interval = minSwapInterval;
460
461 if (interval > maxSwapInterval)
462 interval = maxSwapInterval;
463
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700464 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700465 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800466
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700467 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800468}
469
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700470int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800471 ATRACE_CALL();
472 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800473
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800474 uint32_t reqWidth;
475 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800476 PixelFormat reqFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700477 uint64_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700478 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800479
480 {
481 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -0700482 if (mReportRemovedBuffers) {
483 mRemovedBuffers.clear();
484 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800485
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800486 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
487 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800488
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800489 reqFormat = mReqFormat;
490 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800491
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700492 enableFrameTimestamps = mEnableFrameTimestamps;
493
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700494 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800495 BufferItem::INVALID_BUFFER_SLOT) {
496 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
497 if (gbuf != NULL) {
498 *buffer = gbuf.get();
499 *fenceFd = -1;
500 return OK;
501 }
502 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800503 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
504
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800505 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800506 sp<Fence> fence;
Dan Stoza932f0082017-05-31 13:50:16 -0700507 nsecs_t startTime = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700508
509 FrameEventHistoryDelta frameTimestamps;
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600510 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, reqWidth, reqHeight,
511 reqFormat, reqUsage, &mBufferAge,
512 enableFrameTimestamps ? &frameTimestamps
513 : nullptr);
Dan Stoza932f0082017-05-31 13:50:16 -0700514 mLastDequeueDuration = systemTime() - startTime;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800515
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800516 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700517 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700518 "(%d, %d, %d, %#" PRIx64 ") failed: %d",
519 reqWidth, reqHeight, reqFormat, reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800520 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700521 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800522
Dan Stoza90ce2a92017-05-01 16:31:53 -0700523 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
524 ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf);
525 android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging
526 return FAILED_TRANSACTION;
527 }
528
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800529 Mutex::Autolock lock(mMutex);
530
Dan Stoza932f0082017-05-31 13:50:16 -0700531 // Write this while holding the mutex
532 mLastDequeueStartTime = startTime;
533
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800534 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700535
536 // this should never happen
537 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
538
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800539 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
540 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700541 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700542
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700543 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700544 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700545 }
546
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700547 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
548 if (mReportRemovedBuffers && (gbuf != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -0700549 mRemovedBuffers.push_back(gbuf);
550 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800551 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
552 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700553 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700554 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800555 return result;
556 }
557 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700558
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800559 if (fence->isValid()) {
560 *fenceFd = fence->dup();
561 if (*fenceFd == -1) {
562 ALOGE("dequeueBuffer: error duping fence: %d", errno);
563 // dup() should never fail; something is badly wrong. Soldier on
564 // and hope for the best; the worst that should happen is some
565 // visible corruption that lasts until the next frame.
566 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700567 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800568 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700569 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800570
571 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800572
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700573 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800574 mSharedBufferSlot = buf;
575 mSharedBufferHasBeenQueued = false;
576 } else if (mSharedBufferSlot == buf) {
577 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
578 mSharedBufferHasBeenQueued = false;
579 }
580
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800581 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700582}
583
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800584int Surface::cancelBuffer(android_native_buffer_t* buffer,
585 int fenceFd) {
586 ATRACE_CALL();
587 ALOGV("Surface::cancelBuffer");
588 Mutex::Autolock lock(mMutex);
589 int i = getSlotFromBufferLocked(buffer);
590 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900591 if (fenceFd >= 0) {
592 close(fenceFd);
593 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800594 return i;
595 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800596 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
597 if (fenceFd >= 0) {
598 close(fenceFd);
599 }
600 return OK;
601 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800602 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
603 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800604
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700605 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800606 mSharedBufferHasBeenQueued = true;
607 }
608
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800609 return OK;
610}
611
612int Surface::getSlotFromBufferLocked(
613 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800614 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
615 if (mSlots[i].buffer != NULL &&
616 mSlots[i].buffer->handle == buffer->handle) {
617 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700618 }
619 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800620 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
621 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700622}
623
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800624int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800625 ALOGV("Surface::lockBuffer");
626 Mutex::Autolock lock(mMutex);
627 return OK;
628}
Mathias Agopian631f3582010-05-25 17:51:34 -0700629
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800630int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
631 ATRACE_CALL();
632 ALOGV("Surface::queueBuffer");
633 Mutex::Autolock lock(mMutex);
634 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700635 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800636
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800637 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700638 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700639 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700640 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700641 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800642 } else {
643 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700644 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800645 int i = getSlotFromBufferLocked(buffer);
646 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900647 if (fenceFd >= 0) {
648 close(fenceFd);
649 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800650 return i;
651 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800652 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
653 if (fenceFd >= 0) {
654 close(fenceFd);
655 }
656 return OK;
657 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800660 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700661 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800662 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800664 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
665 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700666 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800667 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700668 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700669
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700670 // we should send HDR metadata as needed if this becomes a bottleneck
671 input.setHdrMetadata(mHdrMetadata);
672
Dan Stozac62acbd2015-04-21 16:42:49 -0700673 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700674 input.setSurfaceDamage(Region::INVALID_REGION);
675 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700676 // Here we do two things:
677 // 1) The surface damage was specified using the OpenGL ES convention of
678 // the origin being in the bottom-left corner. Here we flip to the
679 // convention that the rest of the system uses (top-left corner) by
680 // subtracting all top/bottom coordinates from the buffer height.
681 // 2) If the buffer is coming in rotated (for example, because the EGL
682 // implementation is reacting to the transform hint coming back from
683 // SurfaceFlinger), the surface damage needs to be rotated the
684 // opposite direction, since it was generated assuming an unrotated
685 // buffer (the app doesn't know that the EGL implementation is
686 // reacting to the transform hint behind its back). The
687 // transformations in the switch statement below apply those
688 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
689
690 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700691 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700692 bool rotated90 = (mTransform ^ mStickyTransform) &
693 NATIVE_WINDOW_TRANSFORM_ROT_90;
694 if (rotated90) {
695 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700696 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700697
Dan Stoza5065a552015-03-17 16:23:42 -0700698 Region flippedRegion;
699 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700700 int left = rect.left;
701 int right = rect.right;
702 int top = height - rect.bottom; // Flip from OpenGL convention
703 int bottom = height - rect.top; // Flip from OpenGL convention
704 switch (mTransform ^ mStickyTransform) {
705 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
706 // Rotate 270 degrees
707 Rect flippedRect{top, width - right, bottom, width - left};
708 flippedRegion.orSelf(flippedRect);
709 break;
710 }
711 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
712 // Rotate 180 degrees
713 Rect flippedRect{width - right, height - bottom,
714 width - left, height - top};
715 flippedRegion.orSelf(flippedRect);
716 break;
717 }
718 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
719 // Rotate 90 degrees
720 Rect flippedRect{height - bottom, left,
721 height - top, right};
722 flippedRegion.orSelf(flippedRect);
723 break;
724 }
725 default: {
726 Rect flippedRect{left, top, right, bottom};
727 flippedRegion.orSelf(flippedRect);
728 break;
729 }
730 }
Dan Stoza5065a552015-03-17 16:23:42 -0700731 }
732
733 input.setSurfaceDamage(flippedRegion);
734 }
735
Dan Stoza70ccba52016-07-01 14:00:40 -0700736 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800737 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700738 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800739 if (err != OK) {
740 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
741 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800742
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700743 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700744 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700745 // Update timestamps with the local acquire fence.
746 // The consumer doesn't send it back to prevent us from having two
747 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700748 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700749 std::make_shared<FenceTime>(std::move(fence)));
750
751 // Cache timestamps of signaled fences so we can close their file
752 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700753 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700754 }
755
Brian Anderson50143b32016-09-30 14:01:24 -0700756 mLastFrameNumber = mNextFrameNumber;
757
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700758 mDefaultWidth = output.width;
759 mDefaultHeight = output.height;
760 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700761
Ruben Brunk1681d952014-06-27 15:51:55 -0700762 // Disable transform hint if sticky transform is set.
763 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700764 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700765 }
766
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700767 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700768
Dan Stozac62acbd2015-04-21 16:42:49 -0700769 if (!mConnectedToCpu) {
770 // Clear surface damage back to full-buffer
771 mDirtyRegion = Region::INVALID_REGION;
772 }
Dan Stoza5065a552015-03-17 16:23:42 -0700773
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700774 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800775 mSharedBufferHasBeenQueued = true;
776 }
777
Robert Carr9f31e292016-04-11 11:15:32 -0700778 mQueueBufferCondition.broadcast();
779
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800780 return err;
781}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700782
Brian Anderson6b376712017-04-04 10:51:39 -0700783void Surface::querySupportedTimestampsLocked() const {
784 // mMutex must be locked when calling this method.
785
786 if (mQueriedSupportedTimestamps) {
787 return;
788 }
789 mQueriedSupportedTimestamps = true;
790
791 std::vector<FrameEvent> supportedFrameTimestamps;
792 status_t err = composerService()->getSupportedFrameTimestamps(
793 &supportedFrameTimestamps);
794
795 if (err != NO_ERROR) {
796 return;
797 }
798
799 for (auto sft : supportedFrameTimestamps) {
800 if (sft == FrameEvent::DISPLAY_PRESENT) {
801 mFrameTimestampsSupportsPresent = true;
802 }
803 }
804}
805
Mathias Agopiana67932f2011-04-20 14:20:59 -0700806int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800807 ATRACE_CALL();
808 ALOGV("Surface::query");
809 { // scope for the lock
810 Mutex::Autolock lock(mMutex);
811 switch (what) {
812 case NATIVE_WINDOW_FORMAT:
813 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800814 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800815 return NO_ERROR;
816 }
817 break;
818 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700819 if (composerService()->authenticateSurfaceTexture(
820 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800821 *value = 1;
822 } else {
823 *value = 0;
824 }
825 return NO_ERROR;
826 }
827 case NATIVE_WINDOW_CONCRETE_TYPE:
828 *value = NATIVE_WINDOW_SURFACE;
829 return NO_ERROR;
830 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800831 *value = static_cast<int>(
832 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800833 return NO_ERROR;
834 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800835 *value = static_cast<int>(
836 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800837 return NO_ERROR;
838 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800839 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800840 return NO_ERROR;
841 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
842 status_t err = NO_ERROR;
843 if (!mConsumerRunningBehind) {
844 *value = 0;
845 } else {
846 err = mGraphicBufferProducer->query(what, value);
847 if (err == NO_ERROR) {
848 mConsumerRunningBehind = *value;
849 }
850 }
851 return err;
852 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600853 case NATIVE_WINDOW_BUFFER_AGE: {
854 if (mBufferAge > INT32_MAX) {
855 *value = 0;
856 } else {
857 *value = static_cast<int32_t>(mBufferAge);
858 }
859 return NO_ERROR;
860 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700861 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
862 int64_t durationUs = mLastDequeueDuration / 1000;
863 *value = durationUs > std::numeric_limits<int>::max() ?
864 std::numeric_limits<int>::max() :
865 static_cast<int>(durationUs);
866 return NO_ERROR;
867 }
868 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
869 int64_t durationUs = mLastQueueDuration / 1000;
870 *value = durationUs > std::numeric_limits<int>::max() ?
871 std::numeric_limits<int>::max() :
872 static_cast<int>(durationUs);
873 return NO_ERROR;
874 }
Brian Anderson6b376712017-04-04 10:51:39 -0700875 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
876 querySupportedTimestampsLocked();
877 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
878 return NO_ERROR;
879 }
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800880 case NATIVE_WINDOW_IS_VALID: {
881 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
882 return NO_ERROR;
883 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800884 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700885 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800886 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800887}
888
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800889int Surface::perform(int operation, va_list args)
890{
891 int res = NO_ERROR;
892 switch (operation) {
893 case NATIVE_WINDOW_CONNECT:
894 // deprecated. must return NO_ERROR.
895 break;
896 case NATIVE_WINDOW_DISCONNECT:
897 // deprecated. must return NO_ERROR.
898 break;
899 case NATIVE_WINDOW_SET_USAGE:
900 res = dispatchSetUsage(args);
901 break;
902 case NATIVE_WINDOW_SET_CROP:
903 res = dispatchSetCrop(args);
904 break;
905 case NATIVE_WINDOW_SET_BUFFER_COUNT:
906 res = dispatchSetBufferCount(args);
907 break;
908 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
909 res = dispatchSetBuffersGeometry(args);
910 break;
911 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
912 res = dispatchSetBuffersTransform(args);
913 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700914 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
915 res = dispatchSetBuffersStickyTransform(args);
916 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800917 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
918 res = dispatchSetBuffersTimestamp(args);
919 break;
920 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
921 res = dispatchSetBuffersDimensions(args);
922 break;
923 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
924 res = dispatchSetBuffersUserDimensions(args);
925 break;
926 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
927 res = dispatchSetBuffersFormat(args);
928 break;
929 case NATIVE_WINDOW_LOCK:
930 res = dispatchLock(args);
931 break;
932 case NATIVE_WINDOW_UNLOCK_AND_POST:
933 res = dispatchUnlockAndPost(args);
934 break;
935 case NATIVE_WINDOW_SET_SCALING_MODE:
936 res = dispatchSetScalingMode(args);
937 break;
938 case NATIVE_WINDOW_API_CONNECT:
939 res = dispatchConnect(args);
940 break;
941 case NATIVE_WINDOW_API_DISCONNECT:
942 res = dispatchDisconnect(args);
943 break;
Rachad7cb0d392014-07-29 17:53:53 -0700944 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
945 res = dispatchSetSidebandStream(args);
946 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800947 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
948 res = dispatchSetBuffersDataSpace(args);
949 break;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700950 case NATIVE_WINDOW_SET_BUFFERS_SMPTE2086_METADATA:
951 res = dispatchSetBuffersSmpte2086Metadata(args);
952 break;
953 case NATIVE_WINDOW_SET_BUFFERS_CTA861_3_METADATA:
954 res = dispatchSetBuffersCta8613Metadata(args);
955 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700956 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
957 res = dispatchSetSurfaceDamage(args);
958 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700959 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
960 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700961 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800962 case NATIVE_WINDOW_SET_AUTO_REFRESH:
963 res = dispatchSetAutoRefresh(args);
964 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800965 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
966 res = dispatchGetDisplayRefreshCycleDuration(args);
967 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800968 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
969 res = dispatchGetNextFrameId(args);
970 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700971 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
972 res = dispatchEnableFrameTimestamps(args);
973 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800974 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
975 res = dispatchGetCompositorTiming(args);
976 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800977 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
978 res = dispatchGetFrameTimestamps(args);
979 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700980 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
981 res = dispatchGetWideColorSupport(args);
982 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700983 case NATIVE_WINDOW_GET_HDR_SUPPORT:
984 res = dispatchGetHdrSupport(args);
985 break;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700986 case NATIVE_WINDOW_SET_USAGE64:
987 res = dispatchSetUsage64(args);
988 break;
Chia-I Wue2786ea2017-08-07 10:36:08 -0700989 case NATIVE_WINDOW_GET_CONSUMER_USAGE64:
990 res = dispatchGetConsumerUsage64(args);
991 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800992 default:
993 res = NAME_NOT_FOUND;
994 break;
995 }
996 return res;
997}
Mathias Agopiana138f892010-05-21 17:24:35 -0700998
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800999int Surface::dispatchConnect(va_list args) {
1000 int api = va_arg(args, int);
1001 return connect(api);
1002}
Mathias Agopian55fa2512010-03-11 15:06:54 -08001003
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001004int Surface::dispatchDisconnect(va_list args) {
1005 int api = va_arg(args, int);
1006 return disconnect(api);
1007}
1008
1009int Surface::dispatchSetUsage(va_list args) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001010 uint64_t usage = va_arg(args, uint32_t);
1011 return setUsage(usage);
1012}
1013
1014int Surface::dispatchSetUsage64(va_list args) {
1015 uint64_t usage = va_arg(args, uint64_t);
1016 return setUsage(usage);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001017}
1018
1019int Surface::dispatchSetCrop(va_list args) {
1020 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
1021 return setCrop(reinterpret_cast<Rect const*>(rect));
1022}
1023
1024int Surface::dispatchSetBufferCount(va_list args) {
1025 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001026 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001027}
1028
1029int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001030 uint32_t width = va_arg(args, uint32_t);
1031 uint32_t height = va_arg(args, uint32_t);
1032 PixelFormat format = va_arg(args, PixelFormat);
1033 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001034 if (err != 0) {
1035 return err;
1036 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001037 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001038}
1039
1040int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001041 uint32_t width = va_arg(args, uint32_t);
1042 uint32_t height = va_arg(args, uint32_t);
1043 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001044}
1045
1046int Surface::dispatchSetBuffersUserDimensions(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 setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001050}
1051
1052int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001053 PixelFormat format = va_arg(args, PixelFormat);
1054 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001055}
1056
1057int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001058 int mode = va_arg(args, int);
1059 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001060}
1061
1062int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001063 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001064 return setBuffersTransform(transform);
1065}
1066
Ruben Brunk1681d952014-06-27 15:51:55 -07001067int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001068 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -07001069 return setBuffersStickyTransform(transform);
1070}
1071
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001072int Surface::dispatchSetBuffersTimestamp(va_list args) {
1073 int64_t timestamp = va_arg(args, int64_t);
1074 return setBuffersTimestamp(timestamp);
1075}
1076
1077int Surface::dispatchLock(va_list args) {
1078 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1079 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1080 return lock(outBuffer, inOutDirtyBounds);
1081}
1082
Igor Murashkin7d2d1602013-11-12 18:02:20 -08001083int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001084 return unlockAndPost();
1085}
1086
Rachad7cb0d392014-07-29 17:53:53 -07001087int Surface::dispatchSetSidebandStream(va_list args) {
1088 native_handle_t* sH = va_arg(args, native_handle_t*);
1089 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1090 setSidebandStream(sidebandHandle);
1091 return OK;
1092}
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001093
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001094int Surface::dispatchSetBuffersDataSpace(va_list args) {
1095 android_dataspace dataspace =
1096 static_cast<android_dataspace>(va_arg(args, int));
1097 return setBuffersDataSpace(dataspace);
1098}
1099
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001100int Surface::dispatchSetBuffersSmpte2086Metadata(va_list args) {
1101 const android_smpte2086_metadata* metadata =
1102 va_arg(args, const android_smpte2086_metadata*);
1103 return setBuffersSmpte2086Metadata(metadata);
1104}
1105
1106int Surface::dispatchSetBuffersCta8613Metadata(va_list args) {
1107 const android_cta861_3_metadata* metadata =
1108 va_arg(args, const android_cta861_3_metadata*);
1109 return setBuffersCta8613Metadata(metadata);
1110}
1111
Dan Stoza5065a552015-03-17 16:23:42 -07001112int Surface::dispatchSetSurfaceDamage(va_list args) {
1113 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1114 size_t numRects = va_arg(args, size_t);
1115 setSurfaceDamage(rects, numRects);
1116 return NO_ERROR;
1117}
1118
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001119int Surface::dispatchSetSharedBufferMode(va_list args) {
1120 bool sharedBufferMode = va_arg(args, int);
1121 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001122}
1123
1124int Surface::dispatchSetAutoRefresh(va_list args) {
1125 bool autoRefresh = va_arg(args, int);
1126 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001127}
1128
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001129int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1130 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1131 return getDisplayRefreshCycleDuration(outRefreshDuration);
1132}
1133
Brian Anderson1049d1d2016-12-16 17:25:57 -08001134int Surface::dispatchGetNextFrameId(va_list args) {
1135 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1136 *nextFrameId = getNextFrameNumber();
1137 return NO_ERROR;
1138}
1139
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001140int Surface::dispatchEnableFrameTimestamps(va_list args) {
1141 bool enable = va_arg(args, int);
1142 enableFrameTimestamps(enable);
1143 return NO_ERROR;
1144}
1145
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001146int Surface::dispatchGetCompositorTiming(va_list args) {
1147 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1148 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1149 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1150 return getCompositorTiming(compositeDeadline, compositeInterval,
1151 compositeToPresentLatency);
1152}
1153
Pablo Ceballosce796e72016-02-04 19:10:51 -08001154int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001155 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001156 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001157 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001158 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1159 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1160 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001161 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001162 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001163 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001164 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001165 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001166 outRequestedPresentTime, outAcquireTime, outLatchTime,
1167 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001168 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001169 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001170}
1171
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001172int Surface::dispatchGetWideColorSupport(va_list args) {
1173 bool* outSupport = va_arg(args, bool*);
1174 return getWideColorSupport(outSupport);
1175}
1176
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001177int Surface::dispatchGetHdrSupport(va_list args) {
1178 bool* outSupport = va_arg(args, bool*);
1179 return getHdrSupport(outSupport);
1180}
1181
Chia-I Wue2786ea2017-08-07 10:36:08 -07001182int Surface::dispatchGetConsumerUsage64(va_list args) {
1183 uint64_t* usage = va_arg(args, uint64_t*);
1184 return getConsumerUsage(usage);
1185}
1186
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001187int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001188 static sp<IProducerListener> listener = new DummyProducerListener();
1189 return connect(api, listener);
1190}
1191
1192int Surface::connect(int api, const sp<IProducerListener>& listener) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001193 return connect(api, listener, false);
1194}
1195
1196int Surface::connect(
1197 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001198 ATRACE_CALL();
1199 ALOGV("Surface::connect");
1200 Mutex::Autolock lock(mMutex);
1201 IGraphicBufferProducer::QueueBufferOutput output;
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001202 mReportRemovedBuffers = reportBufferRemoval;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001203 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001204 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001205 mDefaultWidth = output.width;
1206 mDefaultHeight = output.height;
1207 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001208
1209 // Disable transform hint if sticky transform is set.
1210 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001211 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001212 }
1213
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001214 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001215 }
1216 if (!err && api == NATIVE_WINDOW_API_CPU) {
1217 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001218 // Clear the dirty region in case we're switching from a non-CPU API
1219 mDirtyRegion.clear();
1220 } else if (!err) {
1221 // Initialize the dirty region for tracking surface damage
1222 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001223 }
Dan Stoza5065a552015-03-17 16:23:42 -07001224
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001225 return err;
1226}
1227
Mathias Agopian365857d2013-09-11 19:35:45 -07001228
Robert Carr97b9c862016-09-08 13:54:35 -07001229int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001230 ATRACE_CALL();
1231 ALOGV("Surface::disconnect");
1232 Mutex::Autolock lock(mMutex);
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001233 mRemovedBuffers.clear();
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001234 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1235 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001236 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001237 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001238 if (!err) {
1239 mReqFormat = 0;
1240 mReqWidth = 0;
1241 mReqHeight = 0;
1242 mReqUsage = 0;
1243 mCrop.clear();
1244 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1245 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001246 mStickyTransform = 0;
1247
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001248 if (api == NATIVE_WINDOW_API_CPU) {
1249 mConnectedToCpu = false;
1250 }
1251 }
1252 return err;
1253}
1254
Dan Stozad9c49712015-04-27 11:06:01 -07001255int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001256 sp<Fence>* outFence) {
1257 ATRACE_CALL();
1258 ALOGV("Surface::detachNextBuffer");
1259
1260 if (outBuffer == NULL || outFence == NULL) {
1261 return BAD_VALUE;
1262 }
1263
1264 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001265 if (mReportRemovedBuffers) {
1266 mRemovedBuffers.clear();
1267 }
Dan Stoza231832e2015-03-11 11:55:01 -07001268
1269 sp<GraphicBuffer> buffer(NULL);
1270 sp<Fence> fence(NULL);
1271 status_t result = mGraphicBufferProducer->detachNextBuffer(
1272 &buffer, &fence);
1273 if (result != NO_ERROR) {
1274 return result;
1275 }
1276
Dan Stozad9c49712015-04-27 11:06:01 -07001277 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001278 if (fence != NULL && fence->isValid()) {
1279 *outFence = fence;
1280 } else {
1281 *outFence = Fence::NO_FENCE;
1282 }
1283
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001284 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1285 if (mSlots[i].buffer != NULL &&
Shuzhen Wang55be5052017-05-23 09:41:54 -07001286 mSlots[i].buffer->getId() == buffer->getId()) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001287 if (mReportRemovedBuffers) {
1288 mRemovedBuffers.push_back(mSlots[i].buffer);
1289 }
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001290 mSlots[i].buffer = NULL;
1291 }
1292 }
1293
Dan Stoza231832e2015-03-11 11:55:01 -07001294 return NO_ERROR;
1295}
1296
1297int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1298{
1299 ATRACE_CALL();
1300 ALOGV("Surface::attachBuffer");
1301
1302 Mutex::Autolock lock(mMutex);
Yin-Chia Yeh1f2af5c2017-05-11 16:54:04 -07001303 if (mReportRemovedBuffers) {
1304 mRemovedBuffers.clear();
1305 }
Dan Stoza231832e2015-03-11 11:55:01 -07001306
1307 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001308 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1309 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001310 int32_t attachedSlot = -1;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001311 status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer);
Dan Stoza231832e2015-03-11 11:55:01 -07001312 if (result != NO_ERROR) {
1313 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001314 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001315 return result;
1316 }
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001317 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001318 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1319 }
Dan Stoza231832e2015-03-11 11:55:01 -07001320 mSlots[attachedSlot].buffer = graphicBuffer;
1321
1322 return NO_ERROR;
1323}
1324
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001325int Surface::setUsage(uint64_t reqUsage)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001326{
1327 ALOGV("Surface::setUsage");
1328 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001329 if (reqUsage != mReqUsage) {
1330 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1331 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001332 mReqUsage = reqUsage;
1333 return OK;
1334}
1335
1336int Surface::setCrop(Rect const* rect)
1337{
1338 ATRACE_CALL();
1339
Pablo Ceballos60d69222015-08-07 14:47:20 -07001340 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001341 if (rect == NULL || rect->isEmpty()) {
1342 realRect.clear();
1343 } else {
1344 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001345 }
1346
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001347 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1348 realRect.left, realRect.top, realRect.right, realRect.bottom);
1349
1350 Mutex::Autolock lock(mMutex);
1351 mCrop = realRect;
1352 return NO_ERROR;
1353}
1354
1355int Surface::setBufferCount(int bufferCount)
1356{
1357 ATRACE_CALL();
1358 ALOGV("Surface::setBufferCount");
1359 Mutex::Autolock lock(mMutex);
1360
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001361 status_t err = NO_ERROR;
1362 if (bufferCount == 0) {
1363 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1364 } else {
1365 int minUndequeuedBuffers = 0;
1366 err = mGraphicBufferProducer->query(
1367 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1368 if (err == NO_ERROR) {
1369 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1370 bufferCount - minUndequeuedBuffers);
1371 }
1372 }
Mathias Agopian90147262010-01-22 11:47:55 -08001373
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001374 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1375 bufferCount, strerror(-err));
1376
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001377 return err;
1378}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001379
Pablo Ceballosfa455352015-08-12 17:47:47 -07001380int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1381 ATRACE_CALL();
1382 ALOGV("Surface::setMaxDequeuedBufferCount");
1383 Mutex::Autolock lock(mMutex);
1384
1385 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1386 maxDequeuedBuffers);
1387 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1388 "returned %s", maxDequeuedBuffers, strerror(-err));
1389
Pablo Ceballosfa455352015-08-12 17:47:47 -07001390 return err;
1391}
1392
1393int Surface::setAsyncMode(bool async) {
1394 ATRACE_CALL();
1395 ALOGV("Surface::setAsyncMode");
1396 Mutex::Autolock lock(mMutex);
1397
1398 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1399 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1400 async, strerror(-err));
1401
Pablo Ceballosfa455352015-08-12 17:47:47 -07001402 return err;
1403}
1404
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001405int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001406 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001407 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001408 Mutex::Autolock lock(mMutex);
1409
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001410 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1411 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001412 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001413 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001414 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001415 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1416 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001417
1418 return err;
1419}
1420
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001421int Surface::setAutoRefresh(bool autoRefresh) {
1422 ATRACE_CALL();
1423 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1424 Mutex::Autolock lock(mMutex);
1425
1426 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1427 if (err == NO_ERROR) {
1428 mAutoRefresh = autoRefresh;
1429 }
1430 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1431 autoRefresh, strerror(-err));
1432 return err;
1433}
1434
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001435int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001436{
1437 ATRACE_CALL();
1438 ALOGV("Surface::setBuffersDimensions");
1439
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001440 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001441 return BAD_VALUE;
1442
1443 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001444 if (width != mReqWidth || height != mReqHeight) {
1445 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1446 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001447 mReqWidth = width;
1448 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001449 return NO_ERROR;
1450}
1451
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001452int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001453{
1454 ATRACE_CALL();
1455 ALOGV("Surface::setBuffersUserDimensions");
1456
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001457 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001458 return BAD_VALUE;
1459
1460 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001461 if (width != mUserWidth || height != mUserHeight) {
1462 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1463 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001464 mUserWidth = width;
1465 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001466 return NO_ERROR;
1467}
1468
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001469int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001470{
1471 ALOGV("Surface::setBuffersFormat");
1472
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001473 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001474 if (format != mReqFormat) {
1475 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1476 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001477 mReqFormat = format;
1478 return NO_ERROR;
1479}
1480
1481int Surface::setScalingMode(int mode)
1482{
1483 ATRACE_CALL();
1484 ALOGV("Surface::setScalingMode(%d)", mode);
1485
1486 switch (mode) {
1487 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1488 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1489 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001490 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001491 break;
1492 default:
1493 ALOGE("unknown scaling mode: %d", mode);
1494 return BAD_VALUE;
1495 }
1496
1497 Mutex::Autolock lock(mMutex);
1498 mScalingMode = mode;
1499 return NO_ERROR;
1500}
1501
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001502int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001503{
1504 ATRACE_CALL();
1505 ALOGV("Surface::setBuffersTransform");
1506 Mutex::Autolock lock(mMutex);
1507 mTransform = transform;
1508 return NO_ERROR;
1509}
1510
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001511int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001512{
1513 ATRACE_CALL();
1514 ALOGV("Surface::setBuffersStickyTransform");
1515 Mutex::Autolock lock(mMutex);
1516 mStickyTransform = transform;
1517 return NO_ERROR;
1518}
1519
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001520int Surface::setBuffersTimestamp(int64_t timestamp)
1521{
1522 ALOGV("Surface::setBuffersTimestamp");
1523 Mutex::Autolock lock(mMutex);
1524 mTimestamp = timestamp;
1525 return NO_ERROR;
1526}
1527
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001528int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1529{
1530 ALOGV("Surface::setBuffersDataSpace");
1531 Mutex::Autolock lock(mMutex);
1532 mDataSpace = dataSpace;
1533 return NO_ERROR;
1534}
1535
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -07001536int Surface::setBuffersSmpte2086Metadata(const android_smpte2086_metadata* metadata) {
1537 ALOGV("Surface::setBuffersSmpte2086Metadata");
1538 Mutex::Autolock lock(mMutex);
1539 if (metadata) {
1540 mHdrMetadata.smpte2086 = *metadata;
1541 mHdrMetadata.validTypes |= HdrMetadata::SMPTE2086;
1542 } else {
1543 mHdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086;
1544 }
1545 return NO_ERROR;
1546}
1547
1548int Surface::setBuffersCta8613Metadata(const android_cta861_3_metadata* metadata) {
1549 ALOGV("Surface::setBuffersCta8613Metadata");
1550 Mutex::Autolock lock(mMutex);
1551 if (metadata) {
1552 mHdrMetadata.cta8613 = *metadata;
1553 mHdrMetadata.validTypes |= HdrMetadata::CTA861_3;
1554 } else {
1555 mHdrMetadata.validTypes &= ~HdrMetadata::CTA861_3;
1556 }
1557 return NO_ERROR;
1558}
1559
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -06001560android_dataspace_t Surface::getBuffersDataSpace() {
1561 ALOGV("Surface::getBuffersDataSpace");
1562 Mutex::Autolock lock(mMutex);
1563 return mDataSpace;
1564}
1565
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001566void Surface::freeAllBuffers() {
1567 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1568 mSlots[i].buffer = 0;
1569 }
1570}
1571
Dan Stoza5065a552015-03-17 16:23:42 -07001572void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1573 ATRACE_CALL();
1574 ALOGV("Surface::setSurfaceDamage");
1575 Mutex::Autolock lock(mMutex);
1576
Dan Stozac62acbd2015-04-21 16:42:49 -07001577 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001578 mDirtyRegion = Region::INVALID_REGION;
1579 return;
1580 }
1581
1582 mDirtyRegion.clear();
1583 for (size_t r = 0; r < numRects; ++r) {
1584 // We intentionally flip top and bottom here, since because they're
1585 // specified with a bottom-left origin, top > bottom, which fails
1586 // validation in the Region class. We will fix this up when we flip to a
1587 // top-left origin in queueBuffer.
1588 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1589 mDirtyRegion.orSelf(rect);
1590 }
1591}
1592
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001593// ----------------------------------------------------------------------
1594// the lock/unlock APIs must be used from the same thread
1595
1596static status_t copyBlt(
1597 const sp<GraphicBuffer>& dst,
1598 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001599 const Region& reg,
1600 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001601{
Dennis Kempinadd458f2017-05-17 19:16:31 -07001602 if (dst->getId() == src->getId())
1603 return OK;
1604
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001605 // src and dst with, height and format must be identical. no verification
1606 // is done here.
1607 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001608 uint8_t* src_bits = NULL;
1609 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1610 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001611 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1612
1613 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001614 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1615 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001616 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001617 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001618
1619 Region::const_iterator head(reg.begin());
1620 Region::const_iterator tail(reg.end());
1621 if (head != tail && src_bits && dst_bits) {
1622 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001623 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1624 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001625
1626 while (head != tail) {
1627 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001628 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001629 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001630 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1631 uint8_t const * s = src_bits +
1632 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1633 uint8_t * d = dst_bits +
1634 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001635 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001636 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001637 h = 1;
1638 }
1639 do {
1640 memcpy(d, s, size);
1641 d += dbpr;
1642 s += sbpr;
1643 } while (--h > 0);
1644 }
1645 }
1646
1647 if (src_bits)
1648 src->unlock();
1649
1650 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001651 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001652
1653 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654}
1655
Mathias Agopiana138f892010-05-21 17:24:35 -07001656// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001657
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001658status_t Surface::lock(
1659 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1660{
1661 if (mLockedBuffer != 0) {
1662 ALOGE("Surface::lock failed, already locked");
1663 return INVALID_OPERATION;
1664 }
1665
1666 if (!mConnectedToCpu) {
1667 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1668 if (err) {
1669 return err;
1670 }
1671 // we're intending to do software rendering from this point
1672 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1673 }
1674
1675 ANativeWindowBuffer* out;
1676 int fenceFd = -1;
1677 status_t err = dequeueBuffer(&out, &fenceFd);
1678 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1679 if (err == NO_ERROR) {
1680 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001681 const Rect bounds(backBuffer->width, backBuffer->height);
1682
1683 Region newDirtyRegion;
1684 if (inOutDirtyBounds) {
1685 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1686 newDirtyRegion.andSelf(bounds);
1687 } else {
1688 newDirtyRegion.set(bounds);
1689 }
1690
1691 // figure out if we can copy the frontbuffer back
1692 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1693 const bool canCopyBack = (frontBuffer != 0 &&
1694 backBuffer->width == frontBuffer->width &&
1695 backBuffer->height == frontBuffer->height &&
1696 backBuffer->format == frontBuffer->format);
1697
1698 if (canCopyBack) {
1699 // copy the area that is invalid and not repainted this round
1700 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001701 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001702 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001703 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001704 } else {
1705 // if we can't copy-back anything, modify the user's dirty
1706 // region to make sure they redraw the whole buffer
1707 newDirtyRegion.set(bounds);
1708 mDirtyRegion.clear();
1709 Mutex::Autolock lock(mMutex);
1710 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1711 mSlots[i].dirtyRegion.clear();
1712 }
1713 }
1714
1715
1716 { // scope for the lock
1717 Mutex::Autolock lock(mMutex);
1718 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1719 if (backBufferSlot >= 0) {
1720 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1721 mDirtyRegion.subtract(dirtyRegion);
1722 dirtyRegion = newDirtyRegion;
1723 }
1724 }
1725
1726 mDirtyRegion.orSelf(newDirtyRegion);
1727 if (inOutDirtyBounds) {
1728 *inOutDirtyBounds = newDirtyRegion.getBounds();
1729 }
1730
1731 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001732 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001733 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001734 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001735
1736 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1737 backBuffer->handle);
1738
1739 if (res != 0) {
1740 err = INVALID_OPERATION;
1741 } else {
1742 mLockedBuffer = backBuffer;
1743 outBuffer->width = backBuffer->width;
1744 outBuffer->height = backBuffer->height;
1745 outBuffer->stride = backBuffer->stride;
1746 outBuffer->format = backBuffer->format;
1747 outBuffer->bits = vaddr;
1748 }
1749 }
1750 return err;
1751}
1752
1753status_t Surface::unlockAndPost()
1754{
1755 if (mLockedBuffer == 0) {
1756 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1757 return INVALID_OPERATION;
1758 }
1759
Francis Hart8f396012014-04-01 15:30:53 +03001760 int fd = -1;
1761 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001762 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1763
Francis Hart8f396012014-04-01 15:30:53 +03001764 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001765 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1766 mLockedBuffer->handle, strerror(-err));
1767
1768 mPostedBuffer = mLockedBuffer;
1769 mLockedBuffer = 0;
1770 return err;
1771}
1772
Robert Carr9f31e292016-04-11 11:15:32 -07001773bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1774 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001775 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001776 return true;
1777 }
1778 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1779}
1780
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001781status_t Surface::getUniqueId(uint64_t* outId) const {
1782 Mutex::Autolock lock(mMutex);
1783 return mGraphicBufferProducer->getUniqueId(outId);
1784}
1785
Chia-I Wue2786ea2017-08-07 10:36:08 -07001786int Surface::getConsumerUsage(uint64_t* outUsage) const {
1787 Mutex::Autolock lock(mMutex);
1788 return mGraphicBufferProducer->getConsumerUsage(outUsage);
1789}
1790
Dan Stoza932f0082017-05-31 13:50:16 -07001791nsecs_t Surface::getLastDequeueStartTime() const {
1792 Mutex::Autolock lock(mMutex);
1793 return mLastDequeueStartTime;
1794}
1795
Yin-Chia Yehe572fd72017-03-28 19:07:39 -07001796status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1797 if (out == nullptr) {
1798 ALOGE("%s: out must not be null!", __FUNCTION__);
1799 return BAD_VALUE;
1800 }
1801
1802 Mutex::Autolock lock(mMutex);
1803 *out = mRemovedBuffers;
1804 mRemovedBuffers.clear();
1805 return OK;
1806}
1807
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001808status_t Surface::attachAndQueueBuffer(Surface* surface, sp<GraphicBuffer> buffer) {
1809 if (buffer == nullptr) {
1810 return BAD_VALUE;
1811 }
1812 int err = static_cast<ANativeWindow*>(surface)->perform(surface, NATIVE_WINDOW_API_CONNECT,
1813 NATIVE_WINDOW_API_CPU);
1814 if (err != OK) {
1815 return err;
1816 }
1817 err = surface->attachBuffer(buffer->getNativeBuffer());
1818 if (err != OK) {
1819 return err;
1820 }
1821 err = static_cast<ANativeWindow*>(surface)->queueBuffer(surface, buffer->getNativeBuffer(), -1);
1822 if (err != OK) {
1823 return err;
1824 }
1825 err = surface->disconnect(NATIVE_WINDOW_API_CPU);
1826 return err;
1827}
1828
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001829}; // namespace android