blob: c66362074e8337abbb37f0392d180183ee9186ad [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 Agopianb0e76f42012-03-23 14:15:44 -070021#include <android/native_window.h>
22
Mathias Agopiane3c697f2013-02-14 17:11:02 -080023#include <binder/Parcel.h>
24
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#include <gui/Surface.h>
36
Mathias Agopian2b5dd402017-02-07 17:36:19 -080037#include <gui/ISurfaceComposer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080038#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040namespace android {
41
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070043 const sp<IGraphicBufferProducer>& bufferProducer,
44 bool controlledByApp)
Dan Stoza812ed062015-06-02 15:45:22 -070045 : mGraphicBufferProducer(bufferProducer),
Pablo Ceballos60d69222015-08-07 14:47:20 -070046 mCrop(Rect::EMPTY_RECT),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080047 mGenerationNumber(0),
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070048 mSharedBufferMode(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080049 mAutoRefresh(false),
50 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
Pablo Ceballosbc8c1922016-07-01 14:15:41 -070051 mSharedBufferHasBeenQueued(false),
Brian Anderson069b3652016-07-22 10:32:47 -070052 mQueriedSupportedTimestamps(false),
53 mFrameTimestampsSupportsPresent(false),
Brian Anderson7c3ba8a2016-07-25 12:48:08 -070054 mFrameTimestampsSupportsRetire(false),
Brian Anderson3da8d272016-07-28 16:20:47 -070055 mEnableFrameTimestamps(false),
56 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>())
Mathias Agopian62185b72009-04-16 16:19:50 -070057{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080058 // Initialize the ANativeWindow function pointers.
59 ANativeWindow::setSwapInterval = hook_setSwapInterval;
60 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
61 ANativeWindow::cancelBuffer = hook_cancelBuffer;
62 ANativeWindow::queueBuffer = hook_queueBuffer;
63 ANativeWindow::query = hook_query;
64 ANativeWindow::perform = hook_perform;
65
66 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
67 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
68 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
69 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
70
71 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
72 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
73
74 mReqWidth = 0;
75 mReqHeight = 0;
76 mReqFormat = 0;
77 mReqUsage = 0;
78 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080079 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080080 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
81 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070082 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080083 mDefaultWidth = 0;
84 mDefaultHeight = 0;
85 mUserWidth = 0;
86 mUserHeight = 0;
87 mTransformHint = 0;
88 mConsumerRunningBehind = false;
89 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070090 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070091 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070092}
93
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070094Surface::~Surface() {
95 if (mConnectedToCpu) {
96 Surface::disconnect(NATIVE_WINDOW_API_CPU);
97 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -080098}
99
Brian Anderson3da8d272016-07-28 16:20:47 -0700100sp<ISurfaceComposer> Surface::composerService() const {
101 return ComposerService::getComposerService();
102}
103
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800104sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
105 return mGraphicBufferProducer;
106}
107
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900108void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
109 mGraphicBufferProducer->setSidebandStream(stream);
110}
111
Dan Stoza29a3e902014-06-20 13:13:57 -0700112void Surface::allocateBuffers() {
113 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
114 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700115 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
116 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700117}
118
Dan Stoza812ed062015-06-02 15:45:22 -0700119status_t Surface::setGenerationNumber(uint32_t generation) {
120 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
121 if (result == NO_ERROR) {
122 mGenerationNumber = generation;
123 }
124 return result;
125}
126
Dan Stoza7dde5992015-05-22 09:51:44 -0700127uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700128 Mutex::Autolock lock(mMutex);
129 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700130}
131
Dan Stozac6f30bd2015-06-08 09:32:50 -0700132String8 Surface::getConsumerName() const {
133 return mGraphicBufferProducer->getConsumerName();
134}
135
Dan Stoza127fc632015-06-30 13:43:32 -0700136status_t Surface::setDequeueTimeout(nsecs_t timeout) {
137 return mGraphicBufferProducer->setDequeueTimeout(timeout);
138}
139
Dan Stoza50101d02016-04-07 16:53:23 -0700140status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700141 sp<Fence>* outFence, float outTransformMatrix[16]) {
142 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
143 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700144}
145
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700146void Surface::enableFrameTimestamps(bool enable) {
147 Mutex::Autolock lock(mMutex);
148 mEnableFrameTimestamps = enable;
149}
150
Brian Anderson50143b32016-09-30 14:01:24 -0700151static bool checkConsumerForUpdates(
152 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700153 const nsecs_t* outLatchTime,
154 const nsecs_t* outFirstRefreshStartTime,
155 const nsecs_t* outLastRefreshStartTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700156 const nsecs_t* outGlCompositionDoneTime,
157 const nsecs_t* outDisplayPresentTime,
158 const nsecs_t* outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700159 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700160 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700161 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
162 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700163 !e->hasFirstRefreshStartInfo();
164 bool checkForGlCompositionDone = (outGlCompositionDoneTime != nullptr) &&
165 !e->hasGpuCompositionDoneInfo();
166 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
167 !e->hasDisplayPresentInfo();
168
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700169 // LastRefreshStart, DisplayRetire, DequeueReady, and Release are never
170 // available for the last frame.
171 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
172 !e->hasLastRefreshStartInfo() &&
173 (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700174 bool checkForDisplayRetire = (outDisplayRetireTime != nullptr) &&
175 !e->hasDisplayRetireInfo() && (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700176 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
177 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700178 bool checkForRelease = (outReleaseTime != nullptr) &&
179 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
180
181 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700182 return checkForLatch || checkForFirstRefreshStart ||
183 checkForLastRefreshStart || checkForGlCompositionDone ||
184 checkForDisplayPresent || checkForDisplayRetire ||
185 checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700186}
187
Brian Anderson3d4039d2016-09-23 16:31:30 -0700188static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
189 if (dst != nullptr) {
190 *dst = Fence::isValidTimestamp(src) ? src : 0;
191 }
192}
193
194static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) {
195 if (dst != nullptr) {
196 nsecs_t signalTime = src->getSignalTime();
197 *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0;
198 }
199}
200
Brian Anderson069b3652016-07-22 10:32:47 -0700201status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700202 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700203 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
204 nsecs_t* outLastRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700205 nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700206 nsecs_t* outDequeueReadyTime, nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800207 ATRACE_CALL();
208
Brian Anderson3890c392016-07-25 12:48:08 -0700209 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700210
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700211 if (!mEnableFrameTimestamps) {
212 return INVALID_OPERATION;
213 }
214
Brian Anderson3890c392016-07-25 12:48:08 -0700215 // Verify the requested timestamps are supported.
216 querySupportedTimestampsLocked();
217 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
218 return BAD_VALUE;
219 }
220 if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
221 return BAD_VALUE;
Brian Anderson069b3652016-07-22 10:32:47 -0700222 }
223
Brian Anderson3da8d272016-07-28 16:20:47 -0700224 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700225 if (events == nullptr) {
226 // If the entry isn't available in the producer, it's definitely not
227 // available in the consumer.
228 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700229 }
230
Brian Anderson50143b32016-09-30 14:01:24 -0700231 // Update our cache of events if the requested events are not available.
232 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700233 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
234 outGlCompositionDoneTime, outDisplayPresentTime,
235 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700236 FrameEventHistoryDelta delta;
237 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700238 mFrameEventHistory->applyDelta(delta);
239 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700240 }
241
Brian Anderson3890c392016-07-25 12:48:08 -0700242 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700243 // The entry was available before the update, but was overwritten
244 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700245 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800246 }
Brian Anderson069b3652016-07-22 10:32:47 -0700247
Brian Anderson3d4039d2016-09-23 16:31:30 -0700248 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700249 getFrameTimestamp(outLatchTime, events->latchTime);
250 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
251 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
252 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700253
Brian Anderson3d4039d2016-09-23 16:31:30 -0700254 getFrameTimestampFence(outAcquireTime, events->acquireFence);
255 getFrameTimestampFence(
256 outGlCompositionDoneTime, events->gpuCompositionDoneFence);
257 getFrameTimestampFence(
258 outDisplayPresentTime, events->displayPresentFence);
259 getFrameTimestampFence(outDisplayRetireTime, events->displayRetireFence);
260 getFrameTimestampFence(outReleaseTime, events->releaseFence);
Brian Anderson069b3652016-07-22 10:32:47 -0700261
262 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800263}
Ian Elliottbe833a22017-01-25 13:09:20 -0700264status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700265 ATRACE_CALL();
266
267 DisplayStatInfo stats;
268 status_t err = composerService()->getDisplayStats(NULL, &stats);
269
Ian Elliottbe833a22017-01-25 13:09:20 -0700270 *outRefreshDuration = stats.vsyncPeriod;
Ian Elliott62c48c92017-01-20 13:13:20 -0700271
272 return NO_ERROR;
273}
Pablo Ceballosce796e72016-02-04 19:10:51 -0800274
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800275int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
276 Surface* c = getSelf(window);
277 return c->setSwapInterval(interval);
278}
279
280int Surface::hook_dequeueBuffer(ANativeWindow* window,
281 ANativeWindowBuffer** buffer, int* fenceFd) {
282 Surface* c = getSelf(window);
283 return c->dequeueBuffer(buffer, fenceFd);
284}
285
286int Surface::hook_cancelBuffer(ANativeWindow* window,
287 ANativeWindowBuffer* buffer, int fenceFd) {
288 Surface* c = getSelf(window);
289 return c->cancelBuffer(buffer, fenceFd);
290}
291
292int Surface::hook_queueBuffer(ANativeWindow* window,
293 ANativeWindowBuffer* buffer, int fenceFd) {
294 Surface* c = getSelf(window);
295 return c->queueBuffer(buffer, fenceFd);
296}
297
298int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
299 ANativeWindowBuffer** buffer) {
300 Surface* c = getSelf(window);
301 ANativeWindowBuffer* buf;
302 int fenceFd = -1;
303 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600304 if (result != OK) {
305 return result;
306 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800307 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700308 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800309 if (waitResult != OK) {
310 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
311 waitResult);
312 c->cancelBuffer(buf, -1);
313 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700314 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800315 *buffer = buf;
316 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700317}
318
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800319int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
320 ANativeWindowBuffer* buffer) {
321 Surface* c = getSelf(window);
322 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700323}
324
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800325int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
326 ANativeWindowBuffer* buffer) {
327 Surface* c = getSelf(window);
328 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700329}
330
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800331int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
332 ANativeWindowBuffer* buffer) {
333 Surface* c = getSelf(window);
334 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700335}
Mathias Agopian62185b72009-04-16 16:19:50 -0700336
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800337int Surface::hook_query(const ANativeWindow* window,
338 int what, int* value) {
339 const Surface* c = getSelf(window);
340 return c->query(what, value);
341}
342
343int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
344 va_list args;
345 va_start(args, operation);
346 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700347 int result = c->perform(operation, args);
348 va_end(args);
349 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800350}
351
352int Surface::setSwapInterval(int interval) {
353 ATRACE_CALL();
354 // EGL specification states:
355 // interval is silently clamped to minimum and maximum implementation
356 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800357
358 if (interval < minSwapInterval)
359 interval = minSwapInterval;
360
361 if (interval > maxSwapInterval)
362 interval = maxSwapInterval;
363
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700364 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700365 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800366
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700367 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800368}
369
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700370int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800371 ATRACE_CALL();
372 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800373
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800374 uint32_t reqWidth;
375 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800376 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800377 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700378 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800379
380 {
381 Mutex::Autolock lock(mMutex);
382
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800383 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
384 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800385
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800386 reqFormat = mReqFormat;
387 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800388
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700389 enableFrameTimestamps = mEnableFrameTimestamps;
390
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700391 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800392 BufferItem::INVALID_BUFFER_SLOT) {
393 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
394 if (gbuf != NULL) {
395 *buffer = gbuf.get();
396 *fenceFd = -1;
397 return OK;
398 }
399 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800400 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
401
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800402 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800403 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700404 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700405
406 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700407 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700408 reqWidth, reqHeight, reqFormat, reqUsage,
409 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700410 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800411
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800412 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700413 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
414 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
415 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800416 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700417 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800418
419 Mutex::Autolock lock(mMutex);
420
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800421 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700422
423 // this should never happen
424 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
425
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800426 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
427 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700428 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700429
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700430 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700431 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700432 }
433
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800434 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
435 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
436 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700437 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700438 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800439 return result;
440 }
441 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700442
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800443 if (fence->isValid()) {
444 *fenceFd = fence->dup();
445 if (*fenceFd == -1) {
446 ALOGE("dequeueBuffer: error duping fence: %d", errno);
447 // dup() should never fail; something is badly wrong. Soldier on
448 // and hope for the best; the worst that should happen is some
449 // visible corruption that lasts until the next frame.
450 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700451 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800452 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700453 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800454
455 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800456
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700457 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800458 mSharedBufferSlot = buf;
459 mSharedBufferHasBeenQueued = false;
460 } else if (mSharedBufferSlot == buf) {
461 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
462 mSharedBufferHasBeenQueued = false;
463 }
464
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800465 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700466}
467
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800468int Surface::cancelBuffer(android_native_buffer_t* buffer,
469 int fenceFd) {
470 ATRACE_CALL();
471 ALOGV("Surface::cancelBuffer");
472 Mutex::Autolock lock(mMutex);
473 int i = getSlotFromBufferLocked(buffer);
474 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900475 if (fenceFd >= 0) {
476 close(fenceFd);
477 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800478 return i;
479 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800480 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
481 if (fenceFd >= 0) {
482 close(fenceFd);
483 }
484 return OK;
485 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800486 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
487 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800488
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700489 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800490 mSharedBufferHasBeenQueued = true;
491 }
492
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800493 return OK;
494}
495
496int Surface::getSlotFromBufferLocked(
497 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800498 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
499 if (mSlots[i].buffer != NULL &&
500 mSlots[i].buffer->handle == buffer->handle) {
501 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700502 }
503 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800504 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
505 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700506}
507
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800508int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800509 ALOGV("Surface::lockBuffer");
510 Mutex::Autolock lock(mMutex);
511 return OK;
512}
Mathias Agopian631f3582010-05-25 17:51:34 -0700513
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800514int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
515 ATRACE_CALL();
516 ALOGV("Surface::queueBuffer");
517 Mutex::Autolock lock(mMutex);
518 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700519 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800520
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800521 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700522 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700523 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700524 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700525 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800526 } else {
527 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700528 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800529 int i = getSlotFromBufferLocked(buffer);
530 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900531 if (fenceFd >= 0) {
532 close(fenceFd);
533 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800534 return i;
535 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800536 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
537 if (fenceFd >= 0) {
538 close(fenceFd);
539 }
540 return OK;
541 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800544 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700545 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800546 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800548 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
549 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700550 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800551 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700552 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700553
Dan Stozac62acbd2015-04-21 16:42:49 -0700554 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700555 input.setSurfaceDamage(Region::INVALID_REGION);
556 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700557 // Here we do two things:
558 // 1) The surface damage was specified using the OpenGL ES convention of
559 // the origin being in the bottom-left corner. Here we flip to the
560 // convention that the rest of the system uses (top-left corner) by
561 // subtracting all top/bottom coordinates from the buffer height.
562 // 2) If the buffer is coming in rotated (for example, because the EGL
563 // implementation is reacting to the transform hint coming back from
564 // SurfaceFlinger), the surface damage needs to be rotated the
565 // opposite direction, since it was generated assuming an unrotated
566 // buffer (the app doesn't know that the EGL implementation is
567 // reacting to the transform hint behind its back). The
568 // transformations in the switch statement below apply those
569 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
570
571 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700572 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700573 bool rotated90 = (mTransform ^ mStickyTransform) &
574 NATIVE_WINDOW_TRANSFORM_ROT_90;
575 if (rotated90) {
576 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700577 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700578
Dan Stoza5065a552015-03-17 16:23:42 -0700579 Region flippedRegion;
580 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700581 int left = rect.left;
582 int right = rect.right;
583 int top = height - rect.bottom; // Flip from OpenGL convention
584 int bottom = height - rect.top; // Flip from OpenGL convention
585 switch (mTransform ^ mStickyTransform) {
586 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
587 // Rotate 270 degrees
588 Rect flippedRect{top, width - right, bottom, width - left};
589 flippedRegion.orSelf(flippedRect);
590 break;
591 }
592 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
593 // Rotate 180 degrees
594 Rect flippedRect{width - right, height - bottom,
595 width - left, height - top};
596 flippedRegion.orSelf(flippedRect);
597 break;
598 }
599 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
600 // Rotate 90 degrees
601 Rect flippedRect{height - bottom, left,
602 height - top, right};
603 flippedRegion.orSelf(flippedRect);
604 break;
605 }
606 default: {
607 Rect flippedRect{left, top, right, bottom};
608 flippedRegion.orSelf(flippedRect);
609 break;
610 }
611 }
Dan Stoza5065a552015-03-17 16:23:42 -0700612 }
613
614 input.setSurfaceDamage(flippedRegion);
615 }
616
Dan Stoza70ccba52016-07-01 14:00:40 -0700617 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800618 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700619 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800620 if (err != OK) {
621 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
622 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800623
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700624 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700625 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700626 // Update timestamps with the local acquire fence.
627 // The consumer doesn't send it back to prevent us from having two
628 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700629 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700630 std::make_shared<FenceTime>(std::move(fence)));
631
632 // Cache timestamps of signaled fences so we can close their file
633 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700634 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700635 }
636
Brian Anderson50143b32016-09-30 14:01:24 -0700637 mLastFrameNumber = mNextFrameNumber;
638
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700639 mDefaultWidth = output.width;
640 mDefaultHeight = output.height;
641 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700642
Ruben Brunk1681d952014-06-27 15:51:55 -0700643 // Disable transform hint if sticky transform is set.
644 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700645 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700646 }
647
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700648 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700649
Dan Stozac62acbd2015-04-21 16:42:49 -0700650 if (!mConnectedToCpu) {
651 // Clear surface damage back to full-buffer
652 mDirtyRegion = Region::INVALID_REGION;
653 }
Dan Stoza5065a552015-03-17 16:23:42 -0700654
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700655 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800656 mSharedBufferHasBeenQueued = true;
657 }
658
Robert Carr9f31e292016-04-11 11:15:32 -0700659 mQueueBufferCondition.broadcast();
660
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800661 return err;
662}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700663
Brian Anderson069b3652016-07-22 10:32:47 -0700664void Surface::querySupportedTimestampsLocked() const {
665 // mMutex must be locked when calling this method.
666
667 if (mQueriedSupportedTimestamps) {
668 return;
669 }
670 mQueriedSupportedTimestamps = true;
671
Brian Anderson3890c392016-07-25 12:48:08 -0700672 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson3da8d272016-07-28 16:20:47 -0700673 status_t err = composerService()->getSupportedFrameTimestamps(
Brian Anderson069b3652016-07-22 10:32:47 -0700674 &supportedFrameTimestamps);
675
676 if (err != NO_ERROR) {
677 return;
678 }
679
680 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700681 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700682 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700683 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700684 mFrameTimestampsSupportsRetire = true;
685 }
686 }
687}
688
Mathias Agopiana67932f2011-04-20 14:20:59 -0700689int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800690 ATRACE_CALL();
691 ALOGV("Surface::query");
692 { // scope for the lock
693 Mutex::Autolock lock(mMutex);
694 switch (what) {
695 case NATIVE_WINDOW_FORMAT:
696 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800697 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800698 return NO_ERROR;
699 }
700 break;
701 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700702 if (composerService()->authenticateSurfaceTexture(
703 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800704 *value = 1;
705 } else {
706 *value = 0;
707 }
708 return NO_ERROR;
709 }
710 case NATIVE_WINDOW_CONCRETE_TYPE:
711 *value = NATIVE_WINDOW_SURFACE;
712 return NO_ERROR;
713 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800714 *value = static_cast<int>(
715 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800716 return NO_ERROR;
717 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800718 *value = static_cast<int>(
719 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800720 return NO_ERROR;
721 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800722 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800723 return NO_ERROR;
724 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
725 status_t err = NO_ERROR;
726 if (!mConsumerRunningBehind) {
727 *value = 0;
728 } else {
729 err = mGraphicBufferProducer->query(what, value);
730 if (err == NO_ERROR) {
731 mConsumerRunningBehind = *value;
732 }
733 }
734 return err;
735 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700736 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
737 int64_t durationUs = mLastDequeueDuration / 1000;
738 *value = durationUs > std::numeric_limits<int>::max() ?
739 std::numeric_limits<int>::max() :
740 static_cast<int>(durationUs);
741 return NO_ERROR;
742 }
743 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
744 int64_t durationUs = mLastQueueDuration / 1000;
745 *value = durationUs > std::numeric_limits<int>::max() ?
746 std::numeric_limits<int>::max() :
747 static_cast<int>(durationUs);
748 return NO_ERROR;
749 }
Brian Anderson069b3652016-07-22 10:32:47 -0700750 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
751 querySupportedTimestampsLocked();
752 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
753 return NO_ERROR;
754 }
755 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
756 querySupportedTimestampsLocked();
757 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
758 return NO_ERROR;
759 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800760 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700761 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800762 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800763}
764
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800765int Surface::perform(int operation, va_list args)
766{
767 int res = NO_ERROR;
768 switch (operation) {
769 case NATIVE_WINDOW_CONNECT:
770 // deprecated. must return NO_ERROR.
771 break;
772 case NATIVE_WINDOW_DISCONNECT:
773 // deprecated. must return NO_ERROR.
774 break;
775 case NATIVE_WINDOW_SET_USAGE:
776 res = dispatchSetUsage(args);
777 break;
778 case NATIVE_WINDOW_SET_CROP:
779 res = dispatchSetCrop(args);
780 break;
781 case NATIVE_WINDOW_SET_BUFFER_COUNT:
782 res = dispatchSetBufferCount(args);
783 break;
784 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
785 res = dispatchSetBuffersGeometry(args);
786 break;
787 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
788 res = dispatchSetBuffersTransform(args);
789 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700790 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
791 res = dispatchSetBuffersStickyTransform(args);
792 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800793 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
794 res = dispatchSetBuffersTimestamp(args);
795 break;
796 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
797 res = dispatchSetBuffersDimensions(args);
798 break;
799 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
800 res = dispatchSetBuffersUserDimensions(args);
801 break;
802 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
803 res = dispatchSetBuffersFormat(args);
804 break;
805 case NATIVE_WINDOW_LOCK:
806 res = dispatchLock(args);
807 break;
808 case NATIVE_WINDOW_UNLOCK_AND_POST:
809 res = dispatchUnlockAndPost(args);
810 break;
811 case NATIVE_WINDOW_SET_SCALING_MODE:
812 res = dispatchSetScalingMode(args);
813 break;
814 case NATIVE_WINDOW_API_CONNECT:
815 res = dispatchConnect(args);
816 break;
817 case NATIVE_WINDOW_API_DISCONNECT:
818 res = dispatchDisconnect(args);
819 break;
Rachad7cb0d392014-07-29 17:53:53 -0700820 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
821 res = dispatchSetSidebandStream(args);
822 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800823 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
824 res = dispatchSetBuffersDataSpace(args);
825 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700826 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
827 res = dispatchSetSurfaceDamage(args);
828 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700829 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
830 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700831 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800832 case NATIVE_WINDOW_SET_AUTO_REFRESH:
833 res = dispatchSetAutoRefresh(args);
834 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700835 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
836 res = dispatchEnableFrameTimestamps(args);
837 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800838 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
839 res = dispatchGetFrameTimestamps(args);
840 break;
Ian Elliottbe833a22017-01-25 13:09:20 -0700841 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
842 res = dispatchGetDisplayRefreshCycleDuration(args);
Ian Elliott62c48c92017-01-20 13:13:20 -0700843 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800844 default:
845 res = NAME_NOT_FOUND;
846 break;
847 }
848 return res;
849}
Mathias Agopiana138f892010-05-21 17:24:35 -0700850
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800851int Surface::dispatchConnect(va_list args) {
852 int api = va_arg(args, int);
853 return connect(api);
854}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800855
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800856int Surface::dispatchDisconnect(va_list args) {
857 int api = va_arg(args, int);
858 return disconnect(api);
859}
860
861int Surface::dispatchSetUsage(va_list args) {
862 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800863 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800864}
865
866int Surface::dispatchSetCrop(va_list args) {
867 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
868 return setCrop(reinterpret_cast<Rect const*>(rect));
869}
870
871int Surface::dispatchSetBufferCount(va_list args) {
872 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800873 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800874}
875
876int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800877 uint32_t width = va_arg(args, uint32_t);
878 uint32_t height = va_arg(args, uint32_t);
879 PixelFormat format = va_arg(args, PixelFormat);
880 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800881 if (err != 0) {
882 return err;
883 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800884 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800885}
886
887int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800888 uint32_t width = va_arg(args, uint32_t);
889 uint32_t height = va_arg(args, uint32_t);
890 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800891}
892
893int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800894 uint32_t width = va_arg(args, uint32_t);
895 uint32_t height = va_arg(args, uint32_t);
896 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800897}
898
899int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800900 PixelFormat format = va_arg(args, PixelFormat);
901 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800902}
903
904int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800905 int mode = va_arg(args, int);
906 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800907}
908
909int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800910 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800911 return setBuffersTransform(transform);
912}
913
Ruben Brunk1681d952014-06-27 15:51:55 -0700914int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800915 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700916 return setBuffersStickyTransform(transform);
917}
918
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800919int Surface::dispatchSetBuffersTimestamp(va_list args) {
920 int64_t timestamp = va_arg(args, int64_t);
921 return setBuffersTimestamp(timestamp);
922}
923
924int Surface::dispatchLock(va_list args) {
925 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
926 ARect* inOutDirtyBounds = va_arg(args, ARect*);
927 return lock(outBuffer, inOutDirtyBounds);
928}
929
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800930int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800931 return unlockAndPost();
932}
933
Rachad7cb0d392014-07-29 17:53:53 -0700934int Surface::dispatchSetSidebandStream(va_list args) {
935 native_handle_t* sH = va_arg(args, native_handle_t*);
936 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
937 setSidebandStream(sidebandHandle);
938 return OK;
939}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800940
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800941int Surface::dispatchSetBuffersDataSpace(va_list args) {
942 android_dataspace dataspace =
943 static_cast<android_dataspace>(va_arg(args, int));
944 return setBuffersDataSpace(dataspace);
945}
946
Dan Stoza5065a552015-03-17 16:23:42 -0700947int Surface::dispatchSetSurfaceDamage(va_list args) {
948 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
949 size_t numRects = va_arg(args, size_t);
950 setSurfaceDamage(rects, numRects);
951 return NO_ERROR;
952}
953
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700954int Surface::dispatchSetSharedBufferMode(va_list args) {
955 bool sharedBufferMode = va_arg(args, int);
956 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800957}
958
959int Surface::dispatchSetAutoRefresh(va_list args) {
960 bool autoRefresh = va_arg(args, int);
961 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700962}
963
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700964int Surface::dispatchEnableFrameTimestamps(va_list args) {
965 bool enable = va_arg(args, int);
966 enableFrameTimestamps(enable);
967 return NO_ERROR;
968}
969
Pablo Ceballosce796e72016-02-04 19:10:51 -0800970int Surface::dispatchGetFrameTimestamps(va_list args) {
971 uint32_t framesAgo = va_arg(args, uint32_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -0700972 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800973 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700974 nsecs_t* outLatchTime = va_arg(args, int64_t*);
975 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
976 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800977 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700978 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800979 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700980 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800981 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700982 return getFrameTimestamps(getNextFrameNumber() - 1 - framesAgo,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700983 outRequestedPresentTime, outAcquireTime, outLatchTime,
984 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700985 outGlCompositionDoneTime, outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700986 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800987}
988
Ian Elliottbe833a22017-01-25 13:09:20 -0700989int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
990 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
991 return getDisplayRefreshCycleDuration(outRefreshDuration);
Ian Elliott62c48c92017-01-20 13:13:20 -0700992}
993
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800994int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -0800995 static sp<IProducerListener> listener = new DummyProducerListener();
996 return connect(api, listener);
997}
998
999int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001000 ATRACE_CALL();
1001 ALOGV("Surface::connect");
1002 Mutex::Autolock lock(mMutex);
1003 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001004 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001005 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001006 mDefaultWidth = output.width;
1007 mDefaultHeight = output.height;
1008 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001009
1010 // Disable transform hint if sticky transform is set.
1011 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001012 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001013 }
1014
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001015 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001016 }
1017 if (!err && api == NATIVE_WINDOW_API_CPU) {
1018 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001019 // Clear the dirty region in case we're switching from a non-CPU API
1020 mDirtyRegion.clear();
1021 } else if (!err) {
1022 // Initialize the dirty region for tracking surface damage
1023 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001024 }
Dan Stoza5065a552015-03-17 16:23:42 -07001025
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001026 return err;
1027}
1028
Mathias Agopian365857d2013-09-11 19:35:45 -07001029
Robert Carr97b9c862016-09-08 13:54:35 -07001030int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001031 ATRACE_CALL();
1032 ALOGV("Surface::disconnect");
1033 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001034 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1035 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001036 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001037 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001038 if (!err) {
1039 mReqFormat = 0;
1040 mReqWidth = 0;
1041 mReqHeight = 0;
1042 mReqUsage = 0;
1043 mCrop.clear();
1044 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1045 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001046 mStickyTransform = 0;
1047
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001048 if (api == NATIVE_WINDOW_API_CPU) {
1049 mConnectedToCpu = false;
1050 }
1051 }
1052 return err;
1053}
1054
Dan Stozad9c49712015-04-27 11:06:01 -07001055int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001056 sp<Fence>* outFence) {
1057 ATRACE_CALL();
1058 ALOGV("Surface::detachNextBuffer");
1059
1060 if (outBuffer == NULL || outFence == NULL) {
1061 return BAD_VALUE;
1062 }
1063
1064 Mutex::Autolock lock(mMutex);
1065
1066 sp<GraphicBuffer> buffer(NULL);
1067 sp<Fence> fence(NULL);
1068 status_t result = mGraphicBufferProducer->detachNextBuffer(
1069 &buffer, &fence);
1070 if (result != NO_ERROR) {
1071 return result;
1072 }
1073
Dan Stozad9c49712015-04-27 11:06:01 -07001074 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001075 if (fence != NULL && fence->isValid()) {
1076 *outFence = fence;
1077 } else {
1078 *outFence = Fence::NO_FENCE;
1079 }
1080
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001081 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1082 if (mSlots[i].buffer != NULL &&
1083 mSlots[i].buffer->handle == buffer->handle) {
1084 mSlots[i].buffer = NULL;
1085 }
1086 }
1087
Dan Stoza231832e2015-03-11 11:55:01 -07001088 return NO_ERROR;
1089}
1090
1091int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1092{
1093 ATRACE_CALL();
1094 ALOGV("Surface::attachBuffer");
1095
1096 Mutex::Autolock lock(mMutex);
1097
1098 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001099 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1100 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001101 int32_t attachedSlot = -1;
1102 status_t result = mGraphicBufferProducer->attachBuffer(
1103 &attachedSlot, graphicBuffer);
1104 if (result != NO_ERROR) {
1105 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001106 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001107 return result;
1108 }
1109 mSlots[attachedSlot].buffer = graphicBuffer;
1110
1111 return NO_ERROR;
1112}
1113
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001114int Surface::setUsage(uint32_t reqUsage)
1115{
1116 ALOGV("Surface::setUsage");
1117 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001118 if (reqUsage != mReqUsage) {
1119 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1120 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001121 mReqUsage = reqUsage;
1122 return OK;
1123}
1124
1125int Surface::setCrop(Rect const* rect)
1126{
1127 ATRACE_CALL();
1128
Pablo Ceballos60d69222015-08-07 14:47:20 -07001129 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001130 if (rect == NULL || rect->isEmpty()) {
1131 realRect.clear();
1132 } else {
1133 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001134 }
1135
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001136 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1137 realRect.left, realRect.top, realRect.right, realRect.bottom);
1138
1139 Mutex::Autolock lock(mMutex);
1140 mCrop = realRect;
1141 return NO_ERROR;
1142}
1143
1144int Surface::setBufferCount(int bufferCount)
1145{
1146 ATRACE_CALL();
1147 ALOGV("Surface::setBufferCount");
1148 Mutex::Autolock lock(mMutex);
1149
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001150 status_t err = NO_ERROR;
1151 if (bufferCount == 0) {
1152 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1153 } else {
1154 int minUndequeuedBuffers = 0;
1155 err = mGraphicBufferProducer->query(
1156 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1157 if (err == NO_ERROR) {
1158 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1159 bufferCount - minUndequeuedBuffers);
1160 }
1161 }
Mathias Agopian90147262010-01-22 11:47:55 -08001162
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001163 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1164 bufferCount, strerror(-err));
1165
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001166 return err;
1167}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001168
Pablo Ceballosfa455352015-08-12 17:47:47 -07001169int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1170 ATRACE_CALL();
1171 ALOGV("Surface::setMaxDequeuedBufferCount");
1172 Mutex::Autolock lock(mMutex);
1173
1174 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1175 maxDequeuedBuffers);
1176 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1177 "returned %s", maxDequeuedBuffers, strerror(-err));
1178
Pablo Ceballosfa455352015-08-12 17:47:47 -07001179 return err;
1180}
1181
1182int Surface::setAsyncMode(bool async) {
1183 ATRACE_CALL();
1184 ALOGV("Surface::setAsyncMode");
1185 Mutex::Autolock lock(mMutex);
1186
1187 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1188 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1189 async, strerror(-err));
1190
Pablo Ceballosfa455352015-08-12 17:47:47 -07001191 return err;
1192}
1193
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001194int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001195 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001196 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001197 Mutex::Autolock lock(mMutex);
1198
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001199 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1200 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001201 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001202 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001203 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001204 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1205 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001206
1207 return err;
1208}
1209
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001210int Surface::setAutoRefresh(bool autoRefresh) {
1211 ATRACE_CALL();
1212 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1213 Mutex::Autolock lock(mMutex);
1214
1215 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1216 if (err == NO_ERROR) {
1217 mAutoRefresh = autoRefresh;
1218 }
1219 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1220 autoRefresh, strerror(-err));
1221 return err;
1222}
1223
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001224int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001225{
1226 ATRACE_CALL();
1227 ALOGV("Surface::setBuffersDimensions");
1228
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001229 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001230 return BAD_VALUE;
1231
1232 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001233 if (width != mReqWidth || height != mReqHeight) {
1234 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1235 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001236 mReqWidth = width;
1237 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001238 return NO_ERROR;
1239}
1240
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001241int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001242{
1243 ATRACE_CALL();
1244 ALOGV("Surface::setBuffersUserDimensions");
1245
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001246 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001247 return BAD_VALUE;
1248
1249 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001250 if (width != mUserWidth || height != mUserHeight) {
1251 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1252 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001253 mUserWidth = width;
1254 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001255 return NO_ERROR;
1256}
1257
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001258int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001259{
1260 ALOGV("Surface::setBuffersFormat");
1261
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001262 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001263 if (format != mReqFormat) {
1264 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1265 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001266 mReqFormat = format;
1267 return NO_ERROR;
1268}
1269
1270int Surface::setScalingMode(int mode)
1271{
1272 ATRACE_CALL();
1273 ALOGV("Surface::setScalingMode(%d)", mode);
1274
1275 switch (mode) {
1276 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1277 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1278 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001279 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001280 break;
1281 default:
1282 ALOGE("unknown scaling mode: %d", mode);
1283 return BAD_VALUE;
1284 }
1285
1286 Mutex::Autolock lock(mMutex);
1287 mScalingMode = mode;
1288 return NO_ERROR;
1289}
1290
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001291int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001292{
1293 ATRACE_CALL();
1294 ALOGV("Surface::setBuffersTransform");
1295 Mutex::Autolock lock(mMutex);
1296 mTransform = transform;
1297 return NO_ERROR;
1298}
1299
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001300int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001301{
1302 ATRACE_CALL();
1303 ALOGV("Surface::setBuffersStickyTransform");
1304 Mutex::Autolock lock(mMutex);
1305 mStickyTransform = transform;
1306 return NO_ERROR;
1307}
1308
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001309int Surface::setBuffersTimestamp(int64_t timestamp)
1310{
1311 ALOGV("Surface::setBuffersTimestamp");
1312 Mutex::Autolock lock(mMutex);
1313 mTimestamp = timestamp;
1314 return NO_ERROR;
1315}
1316
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001317int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1318{
1319 ALOGV("Surface::setBuffersDataSpace");
1320 Mutex::Autolock lock(mMutex);
1321 mDataSpace = dataSpace;
1322 return NO_ERROR;
1323}
1324
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001325void Surface::freeAllBuffers() {
1326 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1327 mSlots[i].buffer = 0;
1328 }
1329}
1330
Dan Stoza5065a552015-03-17 16:23:42 -07001331void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1332 ATRACE_CALL();
1333 ALOGV("Surface::setSurfaceDamage");
1334 Mutex::Autolock lock(mMutex);
1335
Dan Stozac62acbd2015-04-21 16:42:49 -07001336 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001337 mDirtyRegion = Region::INVALID_REGION;
1338 return;
1339 }
1340
1341 mDirtyRegion.clear();
1342 for (size_t r = 0; r < numRects; ++r) {
1343 // We intentionally flip top and bottom here, since because they're
1344 // specified with a bottom-left origin, top > bottom, which fails
1345 // validation in the Region class. We will fix this up when we flip to a
1346 // top-left origin in queueBuffer.
1347 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1348 mDirtyRegion.orSelf(rect);
1349 }
1350}
1351
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001352// ----------------------------------------------------------------------
1353// the lock/unlock APIs must be used from the same thread
1354
1355static status_t copyBlt(
1356 const sp<GraphicBuffer>& dst,
1357 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001358 const Region& reg,
1359 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001360{
1361 // src and dst with, height and format must be identical. no verification
1362 // is done here.
1363 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001364 uint8_t* src_bits = NULL;
1365 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1366 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001367 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1368
1369 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001370 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1371 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001372 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001373 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001374
1375 Region::const_iterator head(reg.begin());
1376 Region::const_iterator tail(reg.end());
1377 if (head != tail && src_bits && dst_bits) {
1378 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001379 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1380 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001381
1382 while (head != tail) {
1383 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001384 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001385 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001386 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1387 uint8_t const * s = src_bits +
1388 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1389 uint8_t * d = dst_bits +
1390 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001391 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001392 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001393 h = 1;
1394 }
1395 do {
1396 memcpy(d, s, size);
1397 d += dbpr;
1398 s += sbpr;
1399 } while (--h > 0);
1400 }
1401 }
1402
1403 if (src_bits)
1404 src->unlock();
1405
1406 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001407 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001408
1409 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001410}
1411
Mathias Agopiana138f892010-05-21 17:24:35 -07001412// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001413
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001414status_t Surface::lock(
1415 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1416{
1417 if (mLockedBuffer != 0) {
1418 ALOGE("Surface::lock failed, already locked");
1419 return INVALID_OPERATION;
1420 }
1421
1422 if (!mConnectedToCpu) {
1423 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1424 if (err) {
1425 return err;
1426 }
1427 // we're intending to do software rendering from this point
1428 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1429 }
1430
1431 ANativeWindowBuffer* out;
1432 int fenceFd = -1;
1433 status_t err = dequeueBuffer(&out, &fenceFd);
1434 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1435 if (err == NO_ERROR) {
1436 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001437 const Rect bounds(backBuffer->width, backBuffer->height);
1438
1439 Region newDirtyRegion;
1440 if (inOutDirtyBounds) {
1441 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1442 newDirtyRegion.andSelf(bounds);
1443 } else {
1444 newDirtyRegion.set(bounds);
1445 }
1446
1447 // figure out if we can copy the frontbuffer back
1448 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1449 const bool canCopyBack = (frontBuffer != 0 &&
1450 backBuffer->width == frontBuffer->width &&
1451 backBuffer->height == frontBuffer->height &&
1452 backBuffer->format == frontBuffer->format);
1453
1454 if (canCopyBack) {
1455 // copy the area that is invalid and not repainted this round
1456 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001457 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001458 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001459 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001460 } else {
1461 // if we can't copy-back anything, modify the user's dirty
1462 // region to make sure they redraw the whole buffer
1463 newDirtyRegion.set(bounds);
1464 mDirtyRegion.clear();
1465 Mutex::Autolock lock(mMutex);
1466 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1467 mSlots[i].dirtyRegion.clear();
1468 }
1469 }
1470
1471
1472 { // scope for the lock
1473 Mutex::Autolock lock(mMutex);
1474 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1475 if (backBufferSlot >= 0) {
1476 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1477 mDirtyRegion.subtract(dirtyRegion);
1478 dirtyRegion = newDirtyRegion;
1479 }
1480 }
1481
1482 mDirtyRegion.orSelf(newDirtyRegion);
1483 if (inOutDirtyBounds) {
1484 *inOutDirtyBounds = newDirtyRegion.getBounds();
1485 }
1486
1487 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001488 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001489 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001490 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001491
1492 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1493 backBuffer->handle);
1494
1495 if (res != 0) {
1496 err = INVALID_OPERATION;
1497 } else {
1498 mLockedBuffer = backBuffer;
1499 outBuffer->width = backBuffer->width;
1500 outBuffer->height = backBuffer->height;
1501 outBuffer->stride = backBuffer->stride;
1502 outBuffer->format = backBuffer->format;
1503 outBuffer->bits = vaddr;
1504 }
1505 }
1506 return err;
1507}
1508
1509status_t Surface::unlockAndPost()
1510{
1511 if (mLockedBuffer == 0) {
1512 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1513 return INVALID_OPERATION;
1514 }
1515
Francis Hart8f396012014-04-01 15:30:53 +03001516 int fd = -1;
1517 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001518 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1519
Francis Hart8f396012014-04-01 15:30:53 +03001520 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001521 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1522 mLockedBuffer->handle, strerror(-err));
1523
1524 mPostedBuffer = mLockedBuffer;
1525 mLockedBuffer = 0;
1526 return err;
1527}
1528
Robert Carr9f31e292016-04-11 11:15:32 -07001529bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1530 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001531 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001532 return true;
1533 }
1534 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1535}
1536
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001537status_t Surface::getUniqueId(uint64_t* outId) const {
1538 Mutex::Autolock lock(mMutex);
1539 return mGraphicBufferProducer->getUniqueId(outId);
1540}
1541
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001542namespace view {
1543
1544status_t Surface::writeToParcel(Parcel* parcel) const {
1545 return writeToParcel(parcel, false);
1546}
1547
1548status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const {
1549 if (parcel == nullptr) return BAD_VALUE;
1550
1551 status_t res = OK;
1552
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001553 if (!nameAlreadyWritten) {
1554 res = parcel->writeString16(name);
1555 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001556
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001557 /* isSingleBuffered defaults to no */
1558 res = parcel->writeInt32(0);
1559 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001560 }
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001561
1562 res = parcel->writeStrongBinder(
1563 IGraphicBufferProducer::asBinder(graphicBufferProducer));
1564
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001565 return res;
1566}
1567
1568status_t Surface::readFromParcel(const Parcel* parcel) {
1569 return readFromParcel(parcel, false);
1570}
1571
1572status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) {
1573 if (parcel == nullptr) return BAD_VALUE;
1574
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001575 status_t res = OK;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001576 if (!nameAlreadyRead) {
1577 name = readMaybeEmptyString16(parcel);
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001578 // Discard this for now
1579 int isSingleBuffered;
1580 res = parcel->readInt32(&isSingleBuffered);
1581 if (res != OK) {
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001582 ALOGE("Can't read isSingleBuffered");
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001583 return res;
1584 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001585 }
1586
1587 sp<IBinder> binder;
1588
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001589 res = parcel->readNullableStrongBinder(&binder);
1590 if (res != OK) {
1591 ALOGE("%s: Can't read strong binder", __FUNCTION__);
1592 return res;
1593 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001594
1595 graphicBufferProducer = interface_cast<IGraphicBufferProducer>(binder);
1596
1597 return OK;
1598}
1599
1600String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
1601 size_t len;
1602 const char16_t* str = parcel->readString16Inplace(&len);
1603 if (str != nullptr) {
1604 return String16(str, len);
1605 } else {
1606 return String16();
1607 }
1608}
1609
1610} // namespace view
1611
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001612}; // namespace android