blob: 72fa843cc293d610bb2b945be77cb7e4aaf8f415 [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
Mathias Agopiane3c697f2013-02-14 17:11:02 -080029#include <ui/Fence.h>
Dan Stoza5065a552015-03-17 16:23:42 -070030#include <ui/Region.h>
Ian Elliott62c48c92017-01-20 13:13:20 -070031#include <ui/DisplayStatInfo.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070032
Mathias Agopian2b5dd402017-02-07 17:36:19 -080033#include <gui/BufferItem.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070034#include <gui/IProducerListener.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080035
Mathias Agopian2b5dd402017-02-07 17:36:19 -080036#include <gui/ISurfaceComposer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080037#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039namespace android {
40
Mathias Agopiane3c697f2013-02-14 17:11:02 -080041Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070042 const sp<IGraphicBufferProducer>& bufferProducer,
43 bool controlledByApp)
Dan Stoza812ed062015-06-02 15:45:22 -070044 : mGraphicBufferProducer(bufferProducer),
Pablo Ceballos60d69222015-08-07 14:47:20 -070045 mCrop(Rect::EMPTY_RECT),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080046 mGenerationNumber(0),
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070047 mSharedBufferMode(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080048 mAutoRefresh(false),
49 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
Pablo Ceballosbc8c1922016-07-01 14:15:41 -070050 mSharedBufferHasBeenQueued(false),
Brian Anderson069b3652016-07-22 10:32:47 -070051 mQueriedSupportedTimestamps(false),
52 mFrameTimestampsSupportsPresent(false),
Brian Anderson7c3ba8a2016-07-25 12:48:08 -070053 mFrameTimestampsSupportsRetire(false),
Brian Anderson3da8d272016-07-28 16:20:47 -070054 mEnableFrameTimestamps(false),
55 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>())
Mathias Agopian62185b72009-04-16 16:19:50 -070056{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080057 // Initialize the ANativeWindow function pointers.
58 ANativeWindow::setSwapInterval = hook_setSwapInterval;
59 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
60 ANativeWindow::cancelBuffer = hook_cancelBuffer;
61 ANativeWindow::queueBuffer = hook_queueBuffer;
62 ANativeWindow::query = hook_query;
63 ANativeWindow::perform = hook_perform;
64
65 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
66 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
67 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
68 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
69
70 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
71 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
72
73 mReqWidth = 0;
74 mReqHeight = 0;
75 mReqFormat = 0;
76 mReqUsage = 0;
77 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080078 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080079 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
80 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070081 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080082 mDefaultWidth = 0;
83 mDefaultHeight = 0;
84 mUserWidth = 0;
85 mUserHeight = 0;
86 mTransformHint = 0;
87 mConsumerRunningBehind = false;
88 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070089 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070090 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070091}
92
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070093Surface::~Surface() {
94 if (mConnectedToCpu) {
95 Surface::disconnect(NATIVE_WINDOW_API_CPU);
96 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -080097}
98
Brian Anderson3da8d272016-07-28 16:20:47 -070099sp<ISurfaceComposer> Surface::composerService() const {
100 return ComposerService::getComposerService();
101}
102
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800103sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
104 return mGraphicBufferProducer;
105}
106
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900107void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
108 mGraphicBufferProducer->setSidebandStream(stream);
109}
110
Dan Stoza29a3e902014-06-20 13:13:57 -0700111void Surface::allocateBuffers() {
112 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
113 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700114 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
115 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700116}
117
Dan Stoza812ed062015-06-02 15:45:22 -0700118status_t Surface::setGenerationNumber(uint32_t generation) {
119 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
120 if (result == NO_ERROR) {
121 mGenerationNumber = generation;
122 }
123 return result;
124}
125
Dan Stoza7dde5992015-05-22 09:51:44 -0700126uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700127 Mutex::Autolock lock(mMutex);
128 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700129}
130
Dan Stozac6f30bd2015-06-08 09:32:50 -0700131String8 Surface::getConsumerName() const {
132 return mGraphicBufferProducer->getConsumerName();
133}
134
Dan Stoza127fc632015-06-30 13:43:32 -0700135status_t Surface::setDequeueTimeout(nsecs_t timeout) {
136 return mGraphicBufferProducer->setDequeueTimeout(timeout);
137}
138
Dan Stoza50101d02016-04-07 16:53:23 -0700139status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700140 sp<Fence>* outFence, float outTransformMatrix[16]) {
141 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
142 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700143}
144
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700145void Surface::enableFrameTimestamps(bool enable) {
146 Mutex::Autolock lock(mMutex);
147 mEnableFrameTimestamps = enable;
148}
149
Brian Anderson50143b32016-09-30 14:01:24 -0700150static bool checkConsumerForUpdates(
151 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700152 const nsecs_t* outLatchTime,
153 const nsecs_t* outFirstRefreshStartTime,
154 const nsecs_t* outLastRefreshStartTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700155 const nsecs_t* outGlCompositionDoneTime,
156 const nsecs_t* outDisplayPresentTime,
157 const nsecs_t* outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700158 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700159 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700160 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
161 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700162 !e->hasFirstRefreshStartInfo();
163 bool checkForGlCompositionDone = (outGlCompositionDoneTime != nullptr) &&
164 !e->hasGpuCompositionDoneInfo();
165 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
166 !e->hasDisplayPresentInfo();
167
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700168 // LastRefreshStart, DisplayRetire, DequeueReady, and Release are never
169 // available for the last frame.
170 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
171 !e->hasLastRefreshStartInfo() &&
172 (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700173 bool checkForDisplayRetire = (outDisplayRetireTime != nullptr) &&
174 !e->hasDisplayRetireInfo() && (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700175 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
176 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700177 bool checkForRelease = (outReleaseTime != nullptr) &&
178 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
179
180 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700181 return checkForLatch || checkForFirstRefreshStart ||
182 checkForLastRefreshStart || checkForGlCompositionDone ||
183 checkForDisplayPresent || checkForDisplayRetire ||
184 checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700185}
186
Brian Anderson3d4039d2016-09-23 16:31:30 -0700187static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
188 if (dst != nullptr) {
189 *dst = Fence::isValidTimestamp(src) ? src : 0;
190 }
191}
192
193static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) {
194 if (dst != nullptr) {
195 nsecs_t signalTime = src->getSignalTime();
196 *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0;
197 }
198}
199
Brian Anderson069b3652016-07-22 10:32:47 -0700200status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700201 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700202 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
203 nsecs_t* outLastRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700204 nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700205 nsecs_t* outDequeueReadyTime, nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800206 ATRACE_CALL();
207
Brian Anderson3890c392016-07-25 12:48:08 -0700208 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700209
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700210 if (!mEnableFrameTimestamps) {
211 return INVALID_OPERATION;
212 }
213
Brian Anderson3890c392016-07-25 12:48:08 -0700214 // Verify the requested timestamps are supported.
215 querySupportedTimestampsLocked();
216 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
217 return BAD_VALUE;
218 }
219 if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
220 return BAD_VALUE;
Brian Anderson069b3652016-07-22 10:32:47 -0700221 }
222
Brian Anderson3da8d272016-07-28 16:20:47 -0700223 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700224 if (events == nullptr) {
225 // If the entry isn't available in the producer, it's definitely not
226 // available in the consumer.
227 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700228 }
229
Brian Anderson50143b32016-09-30 14:01:24 -0700230 // Update our cache of events if the requested events are not available.
231 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700232 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
233 outGlCompositionDoneTime, outDisplayPresentTime,
234 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700235 FrameEventHistoryDelta delta;
236 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700237 mFrameEventHistory->applyDelta(delta);
238 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700239 }
240
Brian Anderson3890c392016-07-25 12:48:08 -0700241 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700242 // The entry was available before the update, but was overwritten
243 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700244 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800245 }
Brian Anderson069b3652016-07-22 10:32:47 -0700246
Brian Anderson3d4039d2016-09-23 16:31:30 -0700247 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700248 getFrameTimestamp(outLatchTime, events->latchTime);
249 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
250 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
251 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700252
Brian Anderson3d4039d2016-09-23 16:31:30 -0700253 getFrameTimestampFence(outAcquireTime, events->acquireFence);
254 getFrameTimestampFence(
255 outGlCompositionDoneTime, events->gpuCompositionDoneFence);
256 getFrameTimestampFence(
257 outDisplayPresentTime, events->displayPresentFence);
258 getFrameTimestampFence(outDisplayRetireTime, events->displayRetireFence);
259 getFrameTimestampFence(outReleaseTime, events->releaseFence);
Brian Anderson069b3652016-07-22 10:32:47 -0700260
261 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800262}
Ian Elliottbe833a22017-01-25 13:09:20 -0700263status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700264 ATRACE_CALL();
265
266 DisplayStatInfo stats;
267 status_t err = composerService()->getDisplayStats(NULL, &stats);
268
Ian Elliottbe833a22017-01-25 13:09:20 -0700269 *outRefreshDuration = stats.vsyncPeriod;
Ian Elliott62c48c92017-01-20 13:13:20 -0700270
271 return NO_ERROR;
272}
Pablo Ceballosce796e72016-02-04 19:10:51 -0800273
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800274int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
275 Surface* c = getSelf(window);
276 return c->setSwapInterval(interval);
277}
278
279int Surface::hook_dequeueBuffer(ANativeWindow* window,
280 ANativeWindowBuffer** buffer, int* fenceFd) {
281 Surface* c = getSelf(window);
282 return c->dequeueBuffer(buffer, fenceFd);
283}
284
285int Surface::hook_cancelBuffer(ANativeWindow* window,
286 ANativeWindowBuffer* buffer, int fenceFd) {
287 Surface* c = getSelf(window);
288 return c->cancelBuffer(buffer, fenceFd);
289}
290
291int Surface::hook_queueBuffer(ANativeWindow* window,
292 ANativeWindowBuffer* buffer, int fenceFd) {
293 Surface* c = getSelf(window);
294 return c->queueBuffer(buffer, fenceFd);
295}
296
297int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
298 ANativeWindowBuffer** buffer) {
299 Surface* c = getSelf(window);
300 ANativeWindowBuffer* buf;
301 int fenceFd = -1;
302 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600303 if (result != OK) {
304 return result;
305 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800306 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700307 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800308 if (waitResult != OK) {
309 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
310 waitResult);
311 c->cancelBuffer(buf, -1);
312 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700313 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800314 *buffer = buf;
315 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700316}
317
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800318int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
319 ANativeWindowBuffer* buffer) {
320 Surface* c = getSelf(window);
321 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700322}
323
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800324int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
325 ANativeWindowBuffer* buffer) {
326 Surface* c = getSelf(window);
327 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700328}
329
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800330int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
331 ANativeWindowBuffer* buffer) {
332 Surface* c = getSelf(window);
333 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700334}
Mathias Agopian62185b72009-04-16 16:19:50 -0700335
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800336int Surface::hook_query(const ANativeWindow* window,
337 int what, int* value) {
338 const Surface* c = getSelf(window);
339 return c->query(what, value);
340}
341
342int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
343 va_list args;
344 va_start(args, operation);
345 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700346 int result = c->perform(operation, args);
347 va_end(args);
348 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800349}
350
351int Surface::setSwapInterval(int interval) {
352 ATRACE_CALL();
353 // EGL specification states:
354 // interval is silently clamped to minimum and maximum implementation
355 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800356
357 if (interval < minSwapInterval)
358 interval = minSwapInterval;
359
360 if (interval > maxSwapInterval)
361 interval = maxSwapInterval;
362
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700363 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700364 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800365
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700366 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800367}
368
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700369int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800370 ATRACE_CALL();
371 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800372
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800373 uint32_t reqWidth;
374 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800375 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800376 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700377 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800378
379 {
380 Mutex::Autolock lock(mMutex);
381
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800382 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
383 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800384
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800385 reqFormat = mReqFormat;
386 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800387
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700388 enableFrameTimestamps = mEnableFrameTimestamps;
389
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700390 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800391 BufferItem::INVALID_BUFFER_SLOT) {
392 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
393 if (gbuf != NULL) {
394 *buffer = gbuf.get();
395 *fenceFd = -1;
396 return OK;
397 }
398 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800399 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
400
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800401 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800402 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700403 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700404
405 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700406 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700407 reqWidth, reqHeight, reqFormat, reqUsage,
408 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700409 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800410
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800411 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700412 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
413 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
414 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800415 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700416 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800417
418 Mutex::Autolock lock(mMutex);
419
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800420 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700421
422 // this should never happen
423 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
424
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800425 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
426 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700427 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700428
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700429 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700430 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700431 }
432
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800433 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
434 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
435 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700436 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700437 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800438 return result;
439 }
440 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700441
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800442 if (fence->isValid()) {
443 *fenceFd = fence->dup();
444 if (*fenceFd == -1) {
445 ALOGE("dequeueBuffer: error duping fence: %d", errno);
446 // dup() should never fail; something is badly wrong. Soldier on
447 // and hope for the best; the worst that should happen is some
448 // visible corruption that lasts until the next frame.
449 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700450 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800451 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700452 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800453
454 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800455
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700456 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800457 mSharedBufferSlot = buf;
458 mSharedBufferHasBeenQueued = false;
459 } else if (mSharedBufferSlot == buf) {
460 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
461 mSharedBufferHasBeenQueued = false;
462 }
463
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800464 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700465}
466
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800467int Surface::cancelBuffer(android_native_buffer_t* buffer,
468 int fenceFd) {
469 ATRACE_CALL();
470 ALOGV("Surface::cancelBuffer");
471 Mutex::Autolock lock(mMutex);
472 int i = getSlotFromBufferLocked(buffer);
473 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900474 if (fenceFd >= 0) {
475 close(fenceFd);
476 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800477 return i;
478 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800479 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
480 if (fenceFd >= 0) {
481 close(fenceFd);
482 }
483 return OK;
484 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800485 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
486 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800487
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700488 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800489 mSharedBufferHasBeenQueued = true;
490 }
491
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800492 return OK;
493}
494
495int Surface::getSlotFromBufferLocked(
496 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800497 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
498 if (mSlots[i].buffer != NULL &&
499 mSlots[i].buffer->handle == buffer->handle) {
500 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700501 }
502 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800503 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
504 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700505}
506
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800507int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800508 ALOGV("Surface::lockBuffer");
509 Mutex::Autolock lock(mMutex);
510 return OK;
511}
Mathias Agopian631f3582010-05-25 17:51:34 -0700512
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800513int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
514 ATRACE_CALL();
515 ALOGV("Surface::queueBuffer");
516 Mutex::Autolock lock(mMutex);
517 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700518 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800519
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800520 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700521 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700522 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700523 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700524 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800525 } else {
526 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700527 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800528 int i = getSlotFromBufferLocked(buffer);
529 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900530 if (fenceFd >= 0) {
531 close(fenceFd);
532 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800533 return i;
534 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800535 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
536 if (fenceFd >= 0) {
537 close(fenceFd);
538 }
539 return OK;
540 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800543 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700544 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800545 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800547 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
548 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700549 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800550 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700551 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700552
Dan Stozac62acbd2015-04-21 16:42:49 -0700553 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700554 input.setSurfaceDamage(Region::INVALID_REGION);
555 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700556 // Here we do two things:
557 // 1) The surface damage was specified using the OpenGL ES convention of
558 // the origin being in the bottom-left corner. Here we flip to the
559 // convention that the rest of the system uses (top-left corner) by
560 // subtracting all top/bottom coordinates from the buffer height.
561 // 2) If the buffer is coming in rotated (for example, because the EGL
562 // implementation is reacting to the transform hint coming back from
563 // SurfaceFlinger), the surface damage needs to be rotated the
564 // opposite direction, since it was generated assuming an unrotated
565 // buffer (the app doesn't know that the EGL implementation is
566 // reacting to the transform hint behind its back). The
567 // transformations in the switch statement below apply those
568 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
569
570 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700571 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700572 bool rotated90 = (mTransform ^ mStickyTransform) &
573 NATIVE_WINDOW_TRANSFORM_ROT_90;
574 if (rotated90) {
575 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700576 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700577
Dan Stoza5065a552015-03-17 16:23:42 -0700578 Region flippedRegion;
579 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700580 int left = rect.left;
581 int right = rect.right;
582 int top = height - rect.bottom; // Flip from OpenGL convention
583 int bottom = height - rect.top; // Flip from OpenGL convention
584 switch (mTransform ^ mStickyTransform) {
585 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
586 // Rotate 270 degrees
587 Rect flippedRect{top, width - right, bottom, width - left};
588 flippedRegion.orSelf(flippedRect);
589 break;
590 }
591 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
592 // Rotate 180 degrees
593 Rect flippedRect{width - right, height - bottom,
594 width - left, height - top};
595 flippedRegion.orSelf(flippedRect);
596 break;
597 }
598 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
599 // Rotate 90 degrees
600 Rect flippedRect{height - bottom, left,
601 height - top, right};
602 flippedRegion.orSelf(flippedRect);
603 break;
604 }
605 default: {
606 Rect flippedRect{left, top, right, bottom};
607 flippedRegion.orSelf(flippedRect);
608 break;
609 }
610 }
Dan Stoza5065a552015-03-17 16:23:42 -0700611 }
612
613 input.setSurfaceDamage(flippedRegion);
614 }
615
Dan Stoza70ccba52016-07-01 14:00:40 -0700616 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800617 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700618 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800619 if (err != OK) {
620 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
621 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800622
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700623 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700624 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700625 // Update timestamps with the local acquire fence.
626 // The consumer doesn't send it back to prevent us from having two
627 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700628 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700629 std::make_shared<FenceTime>(std::move(fence)));
630
631 // Cache timestamps of signaled fences so we can close their file
632 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700633 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700634 }
635
Brian Anderson50143b32016-09-30 14:01:24 -0700636 mLastFrameNumber = mNextFrameNumber;
637
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700638 mDefaultWidth = output.width;
639 mDefaultHeight = output.height;
640 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700641
Ruben Brunk1681d952014-06-27 15:51:55 -0700642 // Disable transform hint if sticky transform is set.
643 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700644 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700645 }
646
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700647 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700648
Dan Stozac62acbd2015-04-21 16:42:49 -0700649 if (!mConnectedToCpu) {
650 // Clear surface damage back to full-buffer
651 mDirtyRegion = Region::INVALID_REGION;
652 }
Dan Stoza5065a552015-03-17 16:23:42 -0700653
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700654 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800655 mSharedBufferHasBeenQueued = true;
656 }
657
Robert Carr9f31e292016-04-11 11:15:32 -0700658 mQueueBufferCondition.broadcast();
659
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800660 return err;
661}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700662
Brian Anderson069b3652016-07-22 10:32:47 -0700663void Surface::querySupportedTimestampsLocked() const {
664 // mMutex must be locked when calling this method.
665
666 if (mQueriedSupportedTimestamps) {
667 return;
668 }
669 mQueriedSupportedTimestamps = true;
670
Brian Anderson3890c392016-07-25 12:48:08 -0700671 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson3da8d272016-07-28 16:20:47 -0700672 status_t err = composerService()->getSupportedFrameTimestamps(
Brian Anderson069b3652016-07-22 10:32:47 -0700673 &supportedFrameTimestamps);
674
675 if (err != NO_ERROR) {
676 return;
677 }
678
679 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700680 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700681 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700682 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700683 mFrameTimestampsSupportsRetire = true;
684 }
685 }
686}
687
Mathias Agopiana67932f2011-04-20 14:20:59 -0700688int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800689 ATRACE_CALL();
690 ALOGV("Surface::query");
691 { // scope for the lock
692 Mutex::Autolock lock(mMutex);
693 switch (what) {
694 case NATIVE_WINDOW_FORMAT:
695 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800696 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800697 return NO_ERROR;
698 }
699 break;
700 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700701 if (composerService()->authenticateSurfaceTexture(
702 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800703 *value = 1;
704 } else {
705 *value = 0;
706 }
707 return NO_ERROR;
708 }
709 case NATIVE_WINDOW_CONCRETE_TYPE:
710 *value = NATIVE_WINDOW_SURFACE;
711 return NO_ERROR;
712 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800713 *value = static_cast<int>(
714 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800715 return NO_ERROR;
716 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800717 *value = static_cast<int>(
718 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800719 return NO_ERROR;
720 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800721 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800722 return NO_ERROR;
723 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
724 status_t err = NO_ERROR;
725 if (!mConsumerRunningBehind) {
726 *value = 0;
727 } else {
728 err = mGraphicBufferProducer->query(what, value);
729 if (err == NO_ERROR) {
730 mConsumerRunningBehind = *value;
731 }
732 }
733 return err;
734 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700735 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
736 int64_t durationUs = mLastDequeueDuration / 1000;
737 *value = durationUs > std::numeric_limits<int>::max() ?
738 std::numeric_limits<int>::max() :
739 static_cast<int>(durationUs);
740 return NO_ERROR;
741 }
742 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
743 int64_t durationUs = mLastQueueDuration / 1000;
744 *value = durationUs > std::numeric_limits<int>::max() ?
745 std::numeric_limits<int>::max() :
746 static_cast<int>(durationUs);
747 return NO_ERROR;
748 }
Brian Anderson069b3652016-07-22 10:32:47 -0700749 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
750 querySupportedTimestampsLocked();
751 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
752 return NO_ERROR;
753 }
754 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
755 querySupportedTimestampsLocked();
756 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
757 return NO_ERROR;
758 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800759 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700760 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800761 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800762}
763
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800764int Surface::perform(int operation, va_list args)
765{
766 int res = NO_ERROR;
767 switch (operation) {
768 case NATIVE_WINDOW_CONNECT:
769 // deprecated. must return NO_ERROR.
770 break;
771 case NATIVE_WINDOW_DISCONNECT:
772 // deprecated. must return NO_ERROR.
773 break;
774 case NATIVE_WINDOW_SET_USAGE:
775 res = dispatchSetUsage(args);
776 break;
777 case NATIVE_WINDOW_SET_CROP:
778 res = dispatchSetCrop(args);
779 break;
780 case NATIVE_WINDOW_SET_BUFFER_COUNT:
781 res = dispatchSetBufferCount(args);
782 break;
783 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
784 res = dispatchSetBuffersGeometry(args);
785 break;
786 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
787 res = dispatchSetBuffersTransform(args);
788 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700789 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
790 res = dispatchSetBuffersStickyTransform(args);
791 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800792 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
793 res = dispatchSetBuffersTimestamp(args);
794 break;
795 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
796 res = dispatchSetBuffersDimensions(args);
797 break;
798 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
799 res = dispatchSetBuffersUserDimensions(args);
800 break;
801 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
802 res = dispatchSetBuffersFormat(args);
803 break;
804 case NATIVE_WINDOW_LOCK:
805 res = dispatchLock(args);
806 break;
807 case NATIVE_WINDOW_UNLOCK_AND_POST:
808 res = dispatchUnlockAndPost(args);
809 break;
810 case NATIVE_WINDOW_SET_SCALING_MODE:
811 res = dispatchSetScalingMode(args);
812 break;
813 case NATIVE_WINDOW_API_CONNECT:
814 res = dispatchConnect(args);
815 break;
816 case NATIVE_WINDOW_API_DISCONNECT:
817 res = dispatchDisconnect(args);
818 break;
Rachad7cb0d392014-07-29 17:53:53 -0700819 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
820 res = dispatchSetSidebandStream(args);
821 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800822 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
823 res = dispatchSetBuffersDataSpace(args);
824 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700825 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
826 res = dispatchSetSurfaceDamage(args);
827 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700828 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
829 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700830 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800831 case NATIVE_WINDOW_SET_AUTO_REFRESH:
832 res = dispatchSetAutoRefresh(args);
833 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700834 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
835 res = dispatchEnableFrameTimestamps(args);
836 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800837 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
838 res = dispatchGetFrameTimestamps(args);
839 break;
Ian Elliottbe833a22017-01-25 13:09:20 -0700840 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
841 res = dispatchGetDisplayRefreshCycleDuration(args);
Ian Elliott62c48c92017-01-20 13:13:20 -0700842 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800843 default:
844 res = NAME_NOT_FOUND;
845 break;
846 }
847 return res;
848}
Mathias Agopiana138f892010-05-21 17:24:35 -0700849
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800850int Surface::dispatchConnect(va_list args) {
851 int api = va_arg(args, int);
852 return connect(api);
853}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800854
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800855int Surface::dispatchDisconnect(va_list args) {
856 int api = va_arg(args, int);
857 return disconnect(api);
858}
859
860int Surface::dispatchSetUsage(va_list args) {
861 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800862 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800863}
864
865int Surface::dispatchSetCrop(va_list args) {
866 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
867 return setCrop(reinterpret_cast<Rect const*>(rect));
868}
869
870int Surface::dispatchSetBufferCount(va_list args) {
871 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800872 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800873}
874
875int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800876 uint32_t width = va_arg(args, uint32_t);
877 uint32_t height = va_arg(args, uint32_t);
878 PixelFormat format = va_arg(args, PixelFormat);
879 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800880 if (err != 0) {
881 return err;
882 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800883 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800884}
885
886int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800887 uint32_t width = va_arg(args, uint32_t);
888 uint32_t height = va_arg(args, uint32_t);
889 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800890}
891
892int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800893 uint32_t width = va_arg(args, uint32_t);
894 uint32_t height = va_arg(args, uint32_t);
895 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800896}
897
898int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800899 PixelFormat format = va_arg(args, PixelFormat);
900 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800901}
902
903int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800904 int mode = va_arg(args, int);
905 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800906}
907
908int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800909 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800910 return setBuffersTransform(transform);
911}
912
Ruben Brunk1681d952014-06-27 15:51:55 -0700913int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800914 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700915 return setBuffersStickyTransform(transform);
916}
917
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800918int Surface::dispatchSetBuffersTimestamp(va_list args) {
919 int64_t timestamp = va_arg(args, int64_t);
920 return setBuffersTimestamp(timestamp);
921}
922
923int Surface::dispatchLock(va_list args) {
924 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
925 ARect* inOutDirtyBounds = va_arg(args, ARect*);
926 return lock(outBuffer, inOutDirtyBounds);
927}
928
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800929int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800930 return unlockAndPost();
931}
932
Rachad7cb0d392014-07-29 17:53:53 -0700933int Surface::dispatchSetSidebandStream(va_list args) {
934 native_handle_t* sH = va_arg(args, native_handle_t*);
935 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
936 setSidebandStream(sidebandHandle);
937 return OK;
938}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800939
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800940int Surface::dispatchSetBuffersDataSpace(va_list args) {
941 android_dataspace dataspace =
942 static_cast<android_dataspace>(va_arg(args, int));
943 return setBuffersDataSpace(dataspace);
944}
945
Dan Stoza5065a552015-03-17 16:23:42 -0700946int Surface::dispatchSetSurfaceDamage(va_list args) {
947 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
948 size_t numRects = va_arg(args, size_t);
949 setSurfaceDamage(rects, numRects);
950 return NO_ERROR;
951}
952
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700953int Surface::dispatchSetSharedBufferMode(va_list args) {
954 bool sharedBufferMode = va_arg(args, int);
955 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800956}
957
958int Surface::dispatchSetAutoRefresh(va_list args) {
959 bool autoRefresh = va_arg(args, int);
960 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700961}
962
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700963int Surface::dispatchEnableFrameTimestamps(va_list args) {
964 bool enable = va_arg(args, int);
965 enableFrameTimestamps(enable);
966 return NO_ERROR;
967}
968
Pablo Ceballosce796e72016-02-04 19:10:51 -0800969int Surface::dispatchGetFrameTimestamps(va_list args) {
970 uint32_t framesAgo = va_arg(args, uint32_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -0700971 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800972 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700973 nsecs_t* outLatchTime = va_arg(args, int64_t*);
974 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
975 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800976 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700977 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800978 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700979 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800980 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700981 return getFrameTimestamps(getNextFrameNumber() - 1 - framesAgo,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700982 outRequestedPresentTime, outAcquireTime, outLatchTime,
983 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700984 outGlCompositionDoneTime, outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700985 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800986}
987
Ian Elliottbe833a22017-01-25 13:09:20 -0700988int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
989 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
990 return getDisplayRefreshCycleDuration(outRefreshDuration);
Ian Elliott62c48c92017-01-20 13:13:20 -0700991}
992
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800993int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -0800994 static sp<IProducerListener> listener = new DummyProducerListener();
995 return connect(api, listener);
996}
997
998int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800999 ATRACE_CALL();
1000 ALOGV("Surface::connect");
1001 Mutex::Autolock lock(mMutex);
1002 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001003 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001004 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001005 mDefaultWidth = output.width;
1006 mDefaultHeight = output.height;
1007 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001008
1009 // Disable transform hint if sticky transform is set.
1010 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001011 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001012 }
1013
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001014 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001015 }
1016 if (!err && api == NATIVE_WINDOW_API_CPU) {
1017 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001018 // Clear the dirty region in case we're switching from a non-CPU API
1019 mDirtyRegion.clear();
1020 } else if (!err) {
1021 // Initialize the dirty region for tracking surface damage
1022 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001023 }
Dan Stoza5065a552015-03-17 16:23:42 -07001024
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001025 return err;
1026}
1027
Mathias Agopian365857d2013-09-11 19:35:45 -07001028
Robert Carr97b9c862016-09-08 13:54:35 -07001029int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001030 ATRACE_CALL();
1031 ALOGV("Surface::disconnect");
1032 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001033 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1034 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001035 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001036 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001037 if (!err) {
1038 mReqFormat = 0;
1039 mReqWidth = 0;
1040 mReqHeight = 0;
1041 mReqUsage = 0;
1042 mCrop.clear();
1043 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1044 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001045 mStickyTransform = 0;
1046
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001047 if (api == NATIVE_WINDOW_API_CPU) {
1048 mConnectedToCpu = false;
1049 }
1050 }
1051 return err;
1052}
1053
Dan Stozad9c49712015-04-27 11:06:01 -07001054int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001055 sp<Fence>* outFence) {
1056 ATRACE_CALL();
1057 ALOGV("Surface::detachNextBuffer");
1058
1059 if (outBuffer == NULL || outFence == NULL) {
1060 return BAD_VALUE;
1061 }
1062
1063 Mutex::Autolock lock(mMutex);
1064
1065 sp<GraphicBuffer> buffer(NULL);
1066 sp<Fence> fence(NULL);
1067 status_t result = mGraphicBufferProducer->detachNextBuffer(
1068 &buffer, &fence);
1069 if (result != NO_ERROR) {
1070 return result;
1071 }
1072
Dan Stozad9c49712015-04-27 11:06:01 -07001073 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001074 if (fence != NULL && fence->isValid()) {
1075 *outFence = fence;
1076 } else {
1077 *outFence = Fence::NO_FENCE;
1078 }
1079
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001080 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1081 if (mSlots[i].buffer != NULL &&
1082 mSlots[i].buffer->handle == buffer->handle) {
1083 mSlots[i].buffer = NULL;
1084 }
1085 }
1086
Dan Stoza231832e2015-03-11 11:55:01 -07001087 return NO_ERROR;
1088}
1089
1090int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1091{
1092 ATRACE_CALL();
1093 ALOGV("Surface::attachBuffer");
1094
1095 Mutex::Autolock lock(mMutex);
1096
1097 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001098 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1099 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001100 int32_t attachedSlot = -1;
1101 status_t result = mGraphicBufferProducer->attachBuffer(
1102 &attachedSlot, graphicBuffer);
1103 if (result != NO_ERROR) {
1104 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001105 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001106 return result;
1107 }
1108 mSlots[attachedSlot].buffer = graphicBuffer;
1109
1110 return NO_ERROR;
1111}
1112
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001113int Surface::setUsage(uint32_t reqUsage)
1114{
1115 ALOGV("Surface::setUsage");
1116 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001117 if (reqUsage != mReqUsage) {
1118 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1119 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001120 mReqUsage = reqUsage;
1121 return OK;
1122}
1123
1124int Surface::setCrop(Rect const* rect)
1125{
1126 ATRACE_CALL();
1127
Pablo Ceballos60d69222015-08-07 14:47:20 -07001128 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001129 if (rect == NULL || rect->isEmpty()) {
1130 realRect.clear();
1131 } else {
1132 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001133 }
1134
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001135 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1136 realRect.left, realRect.top, realRect.right, realRect.bottom);
1137
1138 Mutex::Autolock lock(mMutex);
1139 mCrop = realRect;
1140 return NO_ERROR;
1141}
1142
1143int Surface::setBufferCount(int bufferCount)
1144{
1145 ATRACE_CALL();
1146 ALOGV("Surface::setBufferCount");
1147 Mutex::Autolock lock(mMutex);
1148
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001149 status_t err = NO_ERROR;
1150 if (bufferCount == 0) {
1151 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1152 } else {
1153 int minUndequeuedBuffers = 0;
1154 err = mGraphicBufferProducer->query(
1155 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1156 if (err == NO_ERROR) {
1157 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1158 bufferCount - minUndequeuedBuffers);
1159 }
1160 }
Mathias Agopian90147262010-01-22 11:47:55 -08001161
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001162 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1163 bufferCount, strerror(-err));
1164
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001165 return err;
1166}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001167
Pablo Ceballosfa455352015-08-12 17:47:47 -07001168int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1169 ATRACE_CALL();
1170 ALOGV("Surface::setMaxDequeuedBufferCount");
1171 Mutex::Autolock lock(mMutex);
1172
1173 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1174 maxDequeuedBuffers);
1175 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1176 "returned %s", maxDequeuedBuffers, strerror(-err));
1177
Pablo Ceballosfa455352015-08-12 17:47:47 -07001178 return err;
1179}
1180
1181int Surface::setAsyncMode(bool async) {
1182 ATRACE_CALL();
1183 ALOGV("Surface::setAsyncMode");
1184 Mutex::Autolock lock(mMutex);
1185
1186 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1187 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1188 async, strerror(-err));
1189
Pablo Ceballosfa455352015-08-12 17:47:47 -07001190 return err;
1191}
1192
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001193int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001194 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001195 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001196 Mutex::Autolock lock(mMutex);
1197
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001198 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1199 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001200 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001201 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001202 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001203 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1204 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001205
1206 return err;
1207}
1208
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001209int Surface::setAutoRefresh(bool autoRefresh) {
1210 ATRACE_CALL();
1211 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1212 Mutex::Autolock lock(mMutex);
1213
1214 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1215 if (err == NO_ERROR) {
1216 mAutoRefresh = autoRefresh;
1217 }
1218 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1219 autoRefresh, strerror(-err));
1220 return err;
1221}
1222
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001223int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001224{
1225 ATRACE_CALL();
1226 ALOGV("Surface::setBuffersDimensions");
1227
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001228 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001229 return BAD_VALUE;
1230
1231 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001232 if (width != mReqWidth || height != mReqHeight) {
1233 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1234 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001235 mReqWidth = width;
1236 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001237 return NO_ERROR;
1238}
1239
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001240int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001241{
1242 ATRACE_CALL();
1243 ALOGV("Surface::setBuffersUserDimensions");
1244
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001245 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001246 return BAD_VALUE;
1247
1248 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001249 if (width != mUserWidth || height != mUserHeight) {
1250 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1251 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001252 mUserWidth = width;
1253 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001254 return NO_ERROR;
1255}
1256
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001257int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001258{
1259 ALOGV("Surface::setBuffersFormat");
1260
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001261 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001262 if (format != mReqFormat) {
1263 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1264 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001265 mReqFormat = format;
1266 return NO_ERROR;
1267}
1268
1269int Surface::setScalingMode(int mode)
1270{
1271 ATRACE_CALL();
1272 ALOGV("Surface::setScalingMode(%d)", mode);
1273
1274 switch (mode) {
1275 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1276 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1277 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001278 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001279 break;
1280 default:
1281 ALOGE("unknown scaling mode: %d", mode);
1282 return BAD_VALUE;
1283 }
1284
1285 Mutex::Autolock lock(mMutex);
1286 mScalingMode = mode;
1287 return NO_ERROR;
1288}
1289
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001290int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001291{
1292 ATRACE_CALL();
1293 ALOGV("Surface::setBuffersTransform");
1294 Mutex::Autolock lock(mMutex);
1295 mTransform = transform;
1296 return NO_ERROR;
1297}
1298
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001299int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001300{
1301 ATRACE_CALL();
1302 ALOGV("Surface::setBuffersStickyTransform");
1303 Mutex::Autolock lock(mMutex);
1304 mStickyTransform = transform;
1305 return NO_ERROR;
1306}
1307
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001308int Surface::setBuffersTimestamp(int64_t timestamp)
1309{
1310 ALOGV("Surface::setBuffersTimestamp");
1311 Mutex::Autolock lock(mMutex);
1312 mTimestamp = timestamp;
1313 return NO_ERROR;
1314}
1315
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001316int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1317{
1318 ALOGV("Surface::setBuffersDataSpace");
1319 Mutex::Autolock lock(mMutex);
1320 mDataSpace = dataSpace;
1321 return NO_ERROR;
1322}
1323
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001324void Surface::freeAllBuffers() {
1325 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1326 mSlots[i].buffer = 0;
1327 }
1328}
1329
Dan Stoza5065a552015-03-17 16:23:42 -07001330void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1331 ATRACE_CALL();
1332 ALOGV("Surface::setSurfaceDamage");
1333 Mutex::Autolock lock(mMutex);
1334
Dan Stozac62acbd2015-04-21 16:42:49 -07001335 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001336 mDirtyRegion = Region::INVALID_REGION;
1337 return;
1338 }
1339
1340 mDirtyRegion.clear();
1341 for (size_t r = 0; r < numRects; ++r) {
1342 // We intentionally flip top and bottom here, since because they're
1343 // specified with a bottom-left origin, top > bottom, which fails
1344 // validation in the Region class. We will fix this up when we flip to a
1345 // top-left origin in queueBuffer.
1346 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1347 mDirtyRegion.orSelf(rect);
1348 }
1349}
1350
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001351// ----------------------------------------------------------------------
1352// the lock/unlock APIs must be used from the same thread
1353
1354static status_t copyBlt(
1355 const sp<GraphicBuffer>& dst,
1356 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001357 const Region& reg,
1358 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001359{
1360 // src and dst with, height and format must be identical. no verification
1361 // is done here.
1362 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001363 uint8_t* src_bits = NULL;
1364 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1365 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001366 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1367
1368 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001369 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1370 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001371 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001372 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001373
1374 Region::const_iterator head(reg.begin());
1375 Region::const_iterator tail(reg.end());
1376 if (head != tail && src_bits && dst_bits) {
1377 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001378 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1379 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001380
1381 while (head != tail) {
1382 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001383 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001384 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001385 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1386 uint8_t const * s = src_bits +
1387 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1388 uint8_t * d = dst_bits +
1389 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001390 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001391 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001392 h = 1;
1393 }
1394 do {
1395 memcpy(d, s, size);
1396 d += dbpr;
1397 s += sbpr;
1398 } while (--h > 0);
1399 }
1400 }
1401
1402 if (src_bits)
1403 src->unlock();
1404
1405 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001406 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001407
1408 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409}
1410
Mathias Agopiana138f892010-05-21 17:24:35 -07001411// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001412
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001413status_t Surface::lock(
1414 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1415{
1416 if (mLockedBuffer != 0) {
1417 ALOGE("Surface::lock failed, already locked");
1418 return INVALID_OPERATION;
1419 }
1420
1421 if (!mConnectedToCpu) {
1422 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1423 if (err) {
1424 return err;
1425 }
1426 // we're intending to do software rendering from this point
1427 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1428 }
1429
1430 ANativeWindowBuffer* out;
1431 int fenceFd = -1;
1432 status_t err = dequeueBuffer(&out, &fenceFd);
1433 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1434 if (err == NO_ERROR) {
1435 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001436 const Rect bounds(backBuffer->width, backBuffer->height);
1437
1438 Region newDirtyRegion;
1439 if (inOutDirtyBounds) {
1440 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1441 newDirtyRegion.andSelf(bounds);
1442 } else {
1443 newDirtyRegion.set(bounds);
1444 }
1445
1446 // figure out if we can copy the frontbuffer back
1447 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1448 const bool canCopyBack = (frontBuffer != 0 &&
1449 backBuffer->width == frontBuffer->width &&
1450 backBuffer->height == frontBuffer->height &&
1451 backBuffer->format == frontBuffer->format);
1452
1453 if (canCopyBack) {
1454 // copy the area that is invalid and not repainted this round
1455 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001456 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001457 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001458 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001459 } else {
1460 // if we can't copy-back anything, modify the user's dirty
1461 // region to make sure they redraw the whole buffer
1462 newDirtyRegion.set(bounds);
1463 mDirtyRegion.clear();
1464 Mutex::Autolock lock(mMutex);
1465 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1466 mSlots[i].dirtyRegion.clear();
1467 }
1468 }
1469
1470
1471 { // scope for the lock
1472 Mutex::Autolock lock(mMutex);
1473 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1474 if (backBufferSlot >= 0) {
1475 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1476 mDirtyRegion.subtract(dirtyRegion);
1477 dirtyRegion = newDirtyRegion;
1478 }
1479 }
1480
1481 mDirtyRegion.orSelf(newDirtyRegion);
1482 if (inOutDirtyBounds) {
1483 *inOutDirtyBounds = newDirtyRegion.getBounds();
1484 }
1485
1486 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001487 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001488 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001489 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001490
1491 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1492 backBuffer->handle);
1493
1494 if (res != 0) {
1495 err = INVALID_OPERATION;
1496 } else {
1497 mLockedBuffer = backBuffer;
1498 outBuffer->width = backBuffer->width;
1499 outBuffer->height = backBuffer->height;
1500 outBuffer->stride = backBuffer->stride;
1501 outBuffer->format = backBuffer->format;
1502 outBuffer->bits = vaddr;
1503 }
1504 }
1505 return err;
1506}
1507
1508status_t Surface::unlockAndPost()
1509{
1510 if (mLockedBuffer == 0) {
1511 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1512 return INVALID_OPERATION;
1513 }
1514
Francis Hart8f396012014-04-01 15:30:53 +03001515 int fd = -1;
1516 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001517 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1518
Francis Hart8f396012014-04-01 15:30:53 +03001519 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001520 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1521 mLockedBuffer->handle, strerror(-err));
1522
1523 mPostedBuffer = mLockedBuffer;
1524 mLockedBuffer = 0;
1525 return err;
1526}
1527
Robert Carr9f31e292016-04-11 11:15:32 -07001528bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1529 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001530 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001531 return true;
1532 }
1533 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1534}
1535
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001536status_t Surface::getUniqueId(uint64_t* outId) const {
1537 Mutex::Autolock lock(mMutex);
1538 return mGraphicBufferProducer->getUniqueId(outId);
1539}
1540
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001541}; // namespace android