blob: c8598287cc5090af2ba9b308bf78e7a1921ea3c4 [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>
Mathias Agopiana67932f2011-04-20 14:20:59 -070031
Dan Stozaf0eaf252014-03-21 13:05:51 -070032#include <gui/IProducerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080033#include <gui/ISurfaceComposer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080034#include <gui/SurfaceComposerClient.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080035#include <gui/GLConsumer.h>
36#include <gui/Surface.h>
37
38#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}
264
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800265int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
266 Surface* c = getSelf(window);
267 return c->setSwapInterval(interval);
268}
269
270int Surface::hook_dequeueBuffer(ANativeWindow* window,
271 ANativeWindowBuffer** buffer, int* fenceFd) {
272 Surface* c = getSelf(window);
273 return c->dequeueBuffer(buffer, fenceFd);
274}
275
276int Surface::hook_cancelBuffer(ANativeWindow* window,
277 ANativeWindowBuffer* buffer, int fenceFd) {
278 Surface* c = getSelf(window);
279 return c->cancelBuffer(buffer, fenceFd);
280}
281
282int Surface::hook_queueBuffer(ANativeWindow* window,
283 ANativeWindowBuffer* buffer, int fenceFd) {
284 Surface* c = getSelf(window);
285 return c->queueBuffer(buffer, fenceFd);
286}
287
288int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
289 ANativeWindowBuffer** buffer) {
290 Surface* c = getSelf(window);
291 ANativeWindowBuffer* buf;
292 int fenceFd = -1;
293 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600294 if (result != OK) {
295 return result;
296 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800297 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700298 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800299 if (waitResult != OK) {
300 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
301 waitResult);
302 c->cancelBuffer(buf, -1);
303 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700304 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800305 *buffer = buf;
306 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700307}
308
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800309int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
310 ANativeWindowBuffer* buffer) {
311 Surface* c = getSelf(window);
312 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700313}
314
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800315int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
316 ANativeWindowBuffer* buffer) {
317 Surface* c = getSelf(window);
318 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700319}
320
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800321int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
322 ANativeWindowBuffer* buffer) {
323 Surface* c = getSelf(window);
324 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700325}
Mathias Agopian62185b72009-04-16 16:19:50 -0700326
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800327int Surface::hook_query(const ANativeWindow* window,
328 int what, int* value) {
329 const Surface* c = getSelf(window);
330 return c->query(what, value);
331}
332
333int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
334 va_list args;
335 va_start(args, operation);
336 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700337 int result = c->perform(operation, args);
338 va_end(args);
339 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800340}
341
342int Surface::setSwapInterval(int interval) {
343 ATRACE_CALL();
344 // EGL specification states:
345 // interval is silently clamped to minimum and maximum implementation
346 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800347
348 if (interval < minSwapInterval)
349 interval = minSwapInterval;
350
351 if (interval > maxSwapInterval)
352 interval = maxSwapInterval;
353
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700354 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700355 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800356
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700357 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800358}
359
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700360int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800361 ATRACE_CALL();
362 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800363
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800364 uint32_t reqWidth;
365 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800366 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800367 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700368 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800369
370 {
371 Mutex::Autolock lock(mMutex);
372
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800373 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
374 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800375
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800376 reqFormat = mReqFormat;
377 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800378
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700379 enableFrameTimestamps = mEnableFrameTimestamps;
380
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700381 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800382 BufferItem::INVALID_BUFFER_SLOT) {
383 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
384 if (gbuf != NULL) {
385 *buffer = gbuf.get();
386 *fenceFd = -1;
387 return OK;
388 }
389 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800390 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
391
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800392 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800393 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700394 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700395
396 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700397 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700398 reqWidth, reqHeight, reqFormat, reqUsage,
399 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700400 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800401
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800402 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700403 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
404 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
405 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800406 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700407 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800408
409 Mutex::Autolock lock(mMutex);
410
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800411 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700412
413 // this should never happen
414 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
415
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800416 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
417 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700418 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700419
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700420 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700421 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700422 }
423
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800424 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
425 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
426 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700427 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700428 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800429 return result;
430 }
431 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700432
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800433 if (fence->isValid()) {
434 *fenceFd = fence->dup();
435 if (*fenceFd == -1) {
436 ALOGE("dequeueBuffer: error duping fence: %d", errno);
437 // dup() should never fail; something is badly wrong. Soldier on
438 // and hope for the best; the worst that should happen is some
439 // visible corruption that lasts until the next frame.
440 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700441 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800442 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700443 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800444
445 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800446
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700447 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800448 mSharedBufferSlot = buf;
449 mSharedBufferHasBeenQueued = false;
450 } else if (mSharedBufferSlot == buf) {
451 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
452 mSharedBufferHasBeenQueued = false;
453 }
454
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800455 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700456}
457
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800458int Surface::cancelBuffer(android_native_buffer_t* buffer,
459 int fenceFd) {
460 ATRACE_CALL();
461 ALOGV("Surface::cancelBuffer");
462 Mutex::Autolock lock(mMutex);
463 int i = getSlotFromBufferLocked(buffer);
464 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900465 if (fenceFd >= 0) {
466 close(fenceFd);
467 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800468 return i;
469 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800470 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
471 if (fenceFd >= 0) {
472 close(fenceFd);
473 }
474 return OK;
475 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800476 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
477 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800478
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700479 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800480 mSharedBufferHasBeenQueued = true;
481 }
482
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800483 return OK;
484}
485
486int Surface::getSlotFromBufferLocked(
487 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800488 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
489 if (mSlots[i].buffer != NULL &&
490 mSlots[i].buffer->handle == buffer->handle) {
491 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700492 }
493 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800494 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
495 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700496}
497
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800498int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800499 ALOGV("Surface::lockBuffer");
500 Mutex::Autolock lock(mMutex);
501 return OK;
502}
Mathias Agopian631f3582010-05-25 17:51:34 -0700503
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800504int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
505 ATRACE_CALL();
506 ALOGV("Surface::queueBuffer");
507 Mutex::Autolock lock(mMutex);
508 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700509 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800510
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800511 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700512 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700513 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700514 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700515 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800516 } else {
517 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700518 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800519 int i = getSlotFromBufferLocked(buffer);
520 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900521 if (fenceFd >= 0) {
522 close(fenceFd);
523 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800524 return i;
525 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800526 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
527 if (fenceFd >= 0) {
528 close(fenceFd);
529 }
530 return OK;
531 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800534 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700535 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800536 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800538 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
539 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700540 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800541 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700542 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700543
Dan Stozac62acbd2015-04-21 16:42:49 -0700544 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700545 input.setSurfaceDamage(Region::INVALID_REGION);
546 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700547 // Here we do two things:
548 // 1) The surface damage was specified using the OpenGL ES convention of
549 // the origin being in the bottom-left corner. Here we flip to the
550 // convention that the rest of the system uses (top-left corner) by
551 // subtracting all top/bottom coordinates from the buffer height.
552 // 2) If the buffer is coming in rotated (for example, because the EGL
553 // implementation is reacting to the transform hint coming back from
554 // SurfaceFlinger), the surface damage needs to be rotated the
555 // opposite direction, since it was generated assuming an unrotated
556 // buffer (the app doesn't know that the EGL implementation is
557 // reacting to the transform hint behind its back). The
558 // transformations in the switch statement below apply those
559 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
560
561 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700562 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700563 bool rotated90 = (mTransform ^ mStickyTransform) &
564 NATIVE_WINDOW_TRANSFORM_ROT_90;
565 if (rotated90) {
566 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700567 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700568
Dan Stoza5065a552015-03-17 16:23:42 -0700569 Region flippedRegion;
570 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700571 int left = rect.left;
572 int right = rect.right;
573 int top = height - rect.bottom; // Flip from OpenGL convention
574 int bottom = height - rect.top; // Flip from OpenGL convention
575 switch (mTransform ^ mStickyTransform) {
576 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
577 // Rotate 270 degrees
578 Rect flippedRect{top, width - right, bottom, width - left};
579 flippedRegion.orSelf(flippedRect);
580 break;
581 }
582 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
583 // Rotate 180 degrees
584 Rect flippedRect{width - right, height - bottom,
585 width - left, height - top};
586 flippedRegion.orSelf(flippedRect);
587 break;
588 }
589 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
590 // Rotate 90 degrees
591 Rect flippedRect{height - bottom, left,
592 height - top, right};
593 flippedRegion.orSelf(flippedRect);
594 break;
595 }
596 default: {
597 Rect flippedRect{left, top, right, bottom};
598 flippedRegion.orSelf(flippedRect);
599 break;
600 }
601 }
Dan Stoza5065a552015-03-17 16:23:42 -0700602 }
603
604 input.setSurfaceDamage(flippedRegion);
605 }
606
Dan Stoza70ccba52016-07-01 14:00:40 -0700607 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800608 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700609 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800610 if (err != OK) {
611 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
612 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800613
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700614 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700615 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700616 // Update timestamps with the local acquire fence.
617 // The consumer doesn't send it back to prevent us from having two
618 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700619 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700620 std::make_shared<FenceTime>(std::move(fence)));
621
622 // Cache timestamps of signaled fences so we can close their file
623 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700624 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700625 }
626
Brian Anderson50143b32016-09-30 14:01:24 -0700627 mLastFrameNumber = mNextFrameNumber;
628
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700629 mDefaultWidth = output.width;
630 mDefaultHeight = output.height;
631 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700632
Ruben Brunk1681d952014-06-27 15:51:55 -0700633 // Disable transform hint if sticky transform is set.
634 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700635 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700636 }
637
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700638 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700639
Dan Stozac62acbd2015-04-21 16:42:49 -0700640 if (!mConnectedToCpu) {
641 // Clear surface damage back to full-buffer
642 mDirtyRegion = Region::INVALID_REGION;
643 }
Dan Stoza5065a552015-03-17 16:23:42 -0700644
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700645 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800646 mSharedBufferHasBeenQueued = true;
647 }
648
Robert Carr9f31e292016-04-11 11:15:32 -0700649 mQueueBufferCondition.broadcast();
650
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800651 return err;
652}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700653
Brian Anderson069b3652016-07-22 10:32:47 -0700654void Surface::querySupportedTimestampsLocked() const {
655 // mMutex must be locked when calling this method.
656
657 if (mQueriedSupportedTimestamps) {
658 return;
659 }
660 mQueriedSupportedTimestamps = true;
661
Brian Anderson3890c392016-07-25 12:48:08 -0700662 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson3da8d272016-07-28 16:20:47 -0700663 status_t err = composerService()->getSupportedFrameTimestamps(
Brian Anderson069b3652016-07-22 10:32:47 -0700664 &supportedFrameTimestamps);
665
666 if (err != NO_ERROR) {
667 return;
668 }
669
670 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700671 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700672 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700673 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700674 mFrameTimestampsSupportsRetire = true;
675 }
676 }
677}
678
Mathias Agopiana67932f2011-04-20 14:20:59 -0700679int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800680 ATRACE_CALL();
681 ALOGV("Surface::query");
682 { // scope for the lock
683 Mutex::Autolock lock(mMutex);
684 switch (what) {
685 case NATIVE_WINDOW_FORMAT:
686 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800687 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800688 return NO_ERROR;
689 }
690 break;
691 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700692 if (composerService()->authenticateSurfaceTexture(
693 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800694 *value = 1;
695 } else {
696 *value = 0;
697 }
698 return NO_ERROR;
699 }
700 case NATIVE_WINDOW_CONCRETE_TYPE:
701 *value = NATIVE_WINDOW_SURFACE;
702 return NO_ERROR;
703 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800704 *value = static_cast<int>(
705 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800706 return NO_ERROR;
707 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800708 *value = static_cast<int>(
709 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800710 return NO_ERROR;
711 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800712 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800713 return NO_ERROR;
714 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
715 status_t err = NO_ERROR;
716 if (!mConsumerRunningBehind) {
717 *value = 0;
718 } else {
719 err = mGraphicBufferProducer->query(what, value);
720 if (err == NO_ERROR) {
721 mConsumerRunningBehind = *value;
722 }
723 }
724 return err;
725 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700726 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
727 int64_t durationUs = mLastDequeueDuration / 1000;
728 *value = durationUs > std::numeric_limits<int>::max() ?
729 std::numeric_limits<int>::max() :
730 static_cast<int>(durationUs);
731 return NO_ERROR;
732 }
733 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
734 int64_t durationUs = mLastQueueDuration / 1000;
735 *value = durationUs > std::numeric_limits<int>::max() ?
736 std::numeric_limits<int>::max() :
737 static_cast<int>(durationUs);
738 return NO_ERROR;
739 }
Brian Anderson069b3652016-07-22 10:32:47 -0700740 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
741 querySupportedTimestampsLocked();
742 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
743 return NO_ERROR;
744 }
745 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
746 querySupportedTimestampsLocked();
747 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
748 return NO_ERROR;
749 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800750 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700751 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800752 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800753}
754
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800755int Surface::perform(int operation, va_list args)
756{
757 int res = NO_ERROR;
758 switch (operation) {
759 case NATIVE_WINDOW_CONNECT:
760 // deprecated. must return NO_ERROR.
761 break;
762 case NATIVE_WINDOW_DISCONNECT:
763 // deprecated. must return NO_ERROR.
764 break;
765 case NATIVE_WINDOW_SET_USAGE:
766 res = dispatchSetUsage(args);
767 break;
768 case NATIVE_WINDOW_SET_CROP:
769 res = dispatchSetCrop(args);
770 break;
771 case NATIVE_WINDOW_SET_BUFFER_COUNT:
772 res = dispatchSetBufferCount(args);
773 break;
774 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
775 res = dispatchSetBuffersGeometry(args);
776 break;
777 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
778 res = dispatchSetBuffersTransform(args);
779 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700780 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
781 res = dispatchSetBuffersStickyTransform(args);
782 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800783 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
784 res = dispatchSetBuffersTimestamp(args);
785 break;
786 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
787 res = dispatchSetBuffersDimensions(args);
788 break;
789 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
790 res = dispatchSetBuffersUserDimensions(args);
791 break;
792 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
793 res = dispatchSetBuffersFormat(args);
794 break;
795 case NATIVE_WINDOW_LOCK:
796 res = dispatchLock(args);
797 break;
798 case NATIVE_WINDOW_UNLOCK_AND_POST:
799 res = dispatchUnlockAndPost(args);
800 break;
801 case NATIVE_WINDOW_SET_SCALING_MODE:
802 res = dispatchSetScalingMode(args);
803 break;
804 case NATIVE_WINDOW_API_CONNECT:
805 res = dispatchConnect(args);
806 break;
807 case NATIVE_WINDOW_API_DISCONNECT:
808 res = dispatchDisconnect(args);
809 break;
Rachad7cb0d392014-07-29 17:53:53 -0700810 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
811 res = dispatchSetSidebandStream(args);
812 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800813 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
814 res = dispatchSetBuffersDataSpace(args);
815 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700816 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
817 res = dispatchSetSurfaceDamage(args);
818 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700819 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
820 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700821 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800822 case NATIVE_WINDOW_SET_AUTO_REFRESH:
823 res = dispatchSetAutoRefresh(args);
824 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700825 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
826 res = dispatchEnableFrameTimestamps(args);
827 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800828 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
829 res = dispatchGetFrameTimestamps(args);
830 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800831 default:
832 res = NAME_NOT_FOUND;
833 break;
834 }
835 return res;
836}
Mathias Agopiana138f892010-05-21 17:24:35 -0700837
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800838int Surface::dispatchConnect(va_list args) {
839 int api = va_arg(args, int);
840 return connect(api);
841}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800842
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800843int Surface::dispatchDisconnect(va_list args) {
844 int api = va_arg(args, int);
845 return disconnect(api);
846}
847
848int Surface::dispatchSetUsage(va_list args) {
849 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800850 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800851}
852
853int Surface::dispatchSetCrop(va_list args) {
854 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
855 return setCrop(reinterpret_cast<Rect const*>(rect));
856}
857
858int Surface::dispatchSetBufferCount(va_list args) {
859 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800860 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800861}
862
863int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800864 uint32_t width = va_arg(args, uint32_t);
865 uint32_t height = va_arg(args, uint32_t);
866 PixelFormat format = va_arg(args, PixelFormat);
867 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800868 if (err != 0) {
869 return err;
870 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800871 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800872}
873
874int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800875 uint32_t width = va_arg(args, uint32_t);
876 uint32_t height = va_arg(args, uint32_t);
877 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800878}
879
880int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800881 uint32_t width = va_arg(args, uint32_t);
882 uint32_t height = va_arg(args, uint32_t);
883 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800884}
885
886int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800887 PixelFormat format = va_arg(args, PixelFormat);
888 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800889}
890
891int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800892 int mode = va_arg(args, int);
893 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800894}
895
896int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800897 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800898 return setBuffersTransform(transform);
899}
900
Ruben Brunk1681d952014-06-27 15:51:55 -0700901int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800902 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700903 return setBuffersStickyTransform(transform);
904}
905
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800906int Surface::dispatchSetBuffersTimestamp(va_list args) {
907 int64_t timestamp = va_arg(args, int64_t);
908 return setBuffersTimestamp(timestamp);
909}
910
911int Surface::dispatchLock(va_list args) {
912 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
913 ARect* inOutDirtyBounds = va_arg(args, ARect*);
914 return lock(outBuffer, inOutDirtyBounds);
915}
916
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800917int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800918 return unlockAndPost();
919}
920
Rachad7cb0d392014-07-29 17:53:53 -0700921int Surface::dispatchSetSidebandStream(va_list args) {
922 native_handle_t* sH = va_arg(args, native_handle_t*);
923 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
924 setSidebandStream(sidebandHandle);
925 return OK;
926}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800927
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800928int Surface::dispatchSetBuffersDataSpace(va_list args) {
929 android_dataspace dataspace =
930 static_cast<android_dataspace>(va_arg(args, int));
931 return setBuffersDataSpace(dataspace);
932}
933
Dan Stoza5065a552015-03-17 16:23:42 -0700934int Surface::dispatchSetSurfaceDamage(va_list args) {
935 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
936 size_t numRects = va_arg(args, size_t);
937 setSurfaceDamage(rects, numRects);
938 return NO_ERROR;
939}
940
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700941int Surface::dispatchSetSharedBufferMode(va_list args) {
942 bool sharedBufferMode = va_arg(args, int);
943 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800944}
945
946int Surface::dispatchSetAutoRefresh(va_list args) {
947 bool autoRefresh = va_arg(args, int);
948 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700949}
950
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700951int Surface::dispatchEnableFrameTimestamps(va_list args) {
952 bool enable = va_arg(args, int);
953 enableFrameTimestamps(enable);
954 return NO_ERROR;
955}
956
Pablo Ceballosce796e72016-02-04 19:10:51 -0800957int Surface::dispatchGetFrameTimestamps(va_list args) {
958 uint32_t framesAgo = va_arg(args, uint32_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -0700959 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800960 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700961 nsecs_t* outLatchTime = va_arg(args, int64_t*);
962 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
963 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800964 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700965 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800966 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700967 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800968 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700969 return getFrameTimestamps(getNextFrameNumber() - 1 - framesAgo,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700970 outRequestedPresentTime, outAcquireTime, outLatchTime,
971 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700972 outGlCompositionDoneTime, outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700973 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800974}
975
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800976int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -0800977 static sp<IProducerListener> listener = new DummyProducerListener();
978 return connect(api, listener);
979}
980
981int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800982 ATRACE_CALL();
983 ALOGV("Surface::connect");
984 Mutex::Autolock lock(mMutex);
985 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700986 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800987 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700988 mDefaultWidth = output.width;
989 mDefaultHeight = output.height;
990 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -0700991
992 // Disable transform hint if sticky transform is set.
993 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700994 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700995 }
996
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700997 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800998 }
999 if (!err && api == NATIVE_WINDOW_API_CPU) {
1000 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001001 // Clear the dirty region in case we're switching from a non-CPU API
1002 mDirtyRegion.clear();
1003 } else if (!err) {
1004 // Initialize the dirty region for tracking surface damage
1005 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001006 }
Dan Stoza5065a552015-03-17 16:23:42 -07001007
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001008 return err;
1009}
1010
Mathias Agopian365857d2013-09-11 19:35:45 -07001011
Robert Carr97b9c862016-09-08 13:54:35 -07001012int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001013 ATRACE_CALL();
1014 ALOGV("Surface::disconnect");
1015 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001016 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1017 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001018 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001019 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001020 if (!err) {
1021 mReqFormat = 0;
1022 mReqWidth = 0;
1023 mReqHeight = 0;
1024 mReqUsage = 0;
1025 mCrop.clear();
1026 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1027 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001028 mStickyTransform = 0;
1029
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001030 if (api == NATIVE_WINDOW_API_CPU) {
1031 mConnectedToCpu = false;
1032 }
1033 }
1034 return err;
1035}
1036
Dan Stozad9c49712015-04-27 11:06:01 -07001037int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001038 sp<Fence>* outFence) {
1039 ATRACE_CALL();
1040 ALOGV("Surface::detachNextBuffer");
1041
1042 if (outBuffer == NULL || outFence == NULL) {
1043 return BAD_VALUE;
1044 }
1045
1046 Mutex::Autolock lock(mMutex);
1047
1048 sp<GraphicBuffer> buffer(NULL);
1049 sp<Fence> fence(NULL);
1050 status_t result = mGraphicBufferProducer->detachNextBuffer(
1051 &buffer, &fence);
1052 if (result != NO_ERROR) {
1053 return result;
1054 }
1055
Dan Stozad9c49712015-04-27 11:06:01 -07001056 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001057 if (fence != NULL && fence->isValid()) {
1058 *outFence = fence;
1059 } else {
1060 *outFence = Fence::NO_FENCE;
1061 }
1062
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001063 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1064 if (mSlots[i].buffer != NULL &&
1065 mSlots[i].buffer->handle == buffer->handle) {
1066 mSlots[i].buffer = NULL;
1067 }
1068 }
1069
Dan Stoza231832e2015-03-11 11:55:01 -07001070 return NO_ERROR;
1071}
1072
1073int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1074{
1075 ATRACE_CALL();
1076 ALOGV("Surface::attachBuffer");
1077
1078 Mutex::Autolock lock(mMutex);
1079
1080 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001081 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1082 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001083 int32_t attachedSlot = -1;
1084 status_t result = mGraphicBufferProducer->attachBuffer(
1085 &attachedSlot, graphicBuffer);
1086 if (result != NO_ERROR) {
1087 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001088 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001089 return result;
1090 }
1091 mSlots[attachedSlot].buffer = graphicBuffer;
1092
1093 return NO_ERROR;
1094}
1095
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001096int Surface::setUsage(uint32_t reqUsage)
1097{
1098 ALOGV("Surface::setUsage");
1099 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001100 if (reqUsage != mReqUsage) {
1101 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1102 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001103 mReqUsage = reqUsage;
1104 return OK;
1105}
1106
1107int Surface::setCrop(Rect const* rect)
1108{
1109 ATRACE_CALL();
1110
Pablo Ceballos60d69222015-08-07 14:47:20 -07001111 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001112 if (rect == NULL || rect->isEmpty()) {
1113 realRect.clear();
1114 } else {
1115 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001116 }
1117
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001118 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1119 realRect.left, realRect.top, realRect.right, realRect.bottom);
1120
1121 Mutex::Autolock lock(mMutex);
1122 mCrop = realRect;
1123 return NO_ERROR;
1124}
1125
1126int Surface::setBufferCount(int bufferCount)
1127{
1128 ATRACE_CALL();
1129 ALOGV("Surface::setBufferCount");
1130 Mutex::Autolock lock(mMutex);
1131
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001132 status_t err = NO_ERROR;
1133 if (bufferCount == 0) {
1134 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1135 } else {
1136 int minUndequeuedBuffers = 0;
1137 err = mGraphicBufferProducer->query(
1138 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1139 if (err == NO_ERROR) {
1140 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1141 bufferCount - minUndequeuedBuffers);
1142 }
1143 }
Mathias Agopian90147262010-01-22 11:47:55 -08001144
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001145 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1146 bufferCount, strerror(-err));
1147
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001148 return err;
1149}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001150
Pablo Ceballosfa455352015-08-12 17:47:47 -07001151int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1152 ATRACE_CALL();
1153 ALOGV("Surface::setMaxDequeuedBufferCount");
1154 Mutex::Autolock lock(mMutex);
1155
1156 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1157 maxDequeuedBuffers);
1158 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1159 "returned %s", maxDequeuedBuffers, strerror(-err));
1160
Pablo Ceballosfa455352015-08-12 17:47:47 -07001161 return err;
1162}
1163
1164int Surface::setAsyncMode(bool async) {
1165 ATRACE_CALL();
1166 ALOGV("Surface::setAsyncMode");
1167 Mutex::Autolock lock(mMutex);
1168
1169 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1170 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1171 async, strerror(-err));
1172
Pablo Ceballosfa455352015-08-12 17:47:47 -07001173 return err;
1174}
1175
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001176int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001177 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001178 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001179 Mutex::Autolock lock(mMutex);
1180
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001181 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1182 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001183 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001184 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001185 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001186 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1187 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001188
1189 return err;
1190}
1191
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001192int Surface::setAutoRefresh(bool autoRefresh) {
1193 ATRACE_CALL();
1194 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1195 Mutex::Autolock lock(mMutex);
1196
1197 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1198 if (err == NO_ERROR) {
1199 mAutoRefresh = autoRefresh;
1200 }
1201 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1202 autoRefresh, strerror(-err));
1203 return err;
1204}
1205
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001206int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001207{
1208 ATRACE_CALL();
1209 ALOGV("Surface::setBuffersDimensions");
1210
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001211 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001212 return BAD_VALUE;
1213
1214 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001215 if (width != mReqWidth || height != mReqHeight) {
1216 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1217 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001218 mReqWidth = width;
1219 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001220 return NO_ERROR;
1221}
1222
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001223int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001224{
1225 ATRACE_CALL();
1226 ALOGV("Surface::setBuffersUserDimensions");
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 != mUserWidth || height != mUserHeight) {
1233 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1234 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001235 mUserWidth = width;
1236 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001237 return NO_ERROR;
1238}
1239
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001240int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001241{
1242 ALOGV("Surface::setBuffersFormat");
1243
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001244 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001245 if (format != mReqFormat) {
1246 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1247 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001248 mReqFormat = format;
1249 return NO_ERROR;
1250}
1251
1252int Surface::setScalingMode(int mode)
1253{
1254 ATRACE_CALL();
1255 ALOGV("Surface::setScalingMode(%d)", mode);
1256
1257 switch (mode) {
1258 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1259 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1260 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001261 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001262 break;
1263 default:
1264 ALOGE("unknown scaling mode: %d", mode);
1265 return BAD_VALUE;
1266 }
1267
1268 Mutex::Autolock lock(mMutex);
1269 mScalingMode = mode;
1270 return NO_ERROR;
1271}
1272
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001273int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001274{
1275 ATRACE_CALL();
1276 ALOGV("Surface::setBuffersTransform");
1277 Mutex::Autolock lock(mMutex);
1278 mTransform = transform;
1279 return NO_ERROR;
1280}
1281
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001282int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001283{
1284 ATRACE_CALL();
1285 ALOGV("Surface::setBuffersStickyTransform");
1286 Mutex::Autolock lock(mMutex);
1287 mStickyTransform = transform;
1288 return NO_ERROR;
1289}
1290
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001291int Surface::setBuffersTimestamp(int64_t timestamp)
1292{
1293 ALOGV("Surface::setBuffersTimestamp");
1294 Mutex::Autolock lock(mMutex);
1295 mTimestamp = timestamp;
1296 return NO_ERROR;
1297}
1298
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001299int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1300{
1301 ALOGV("Surface::setBuffersDataSpace");
1302 Mutex::Autolock lock(mMutex);
1303 mDataSpace = dataSpace;
1304 return NO_ERROR;
1305}
1306
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001307void Surface::freeAllBuffers() {
1308 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1309 mSlots[i].buffer = 0;
1310 }
1311}
1312
Dan Stoza5065a552015-03-17 16:23:42 -07001313void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1314 ATRACE_CALL();
1315 ALOGV("Surface::setSurfaceDamage");
1316 Mutex::Autolock lock(mMutex);
1317
Dan Stozac62acbd2015-04-21 16:42:49 -07001318 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001319 mDirtyRegion = Region::INVALID_REGION;
1320 return;
1321 }
1322
1323 mDirtyRegion.clear();
1324 for (size_t r = 0; r < numRects; ++r) {
1325 // We intentionally flip top and bottom here, since because they're
1326 // specified with a bottom-left origin, top > bottom, which fails
1327 // validation in the Region class. We will fix this up when we flip to a
1328 // top-left origin in queueBuffer.
1329 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1330 mDirtyRegion.orSelf(rect);
1331 }
1332}
1333
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001334// ----------------------------------------------------------------------
1335// the lock/unlock APIs must be used from the same thread
1336
1337static status_t copyBlt(
1338 const sp<GraphicBuffer>& dst,
1339 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001340 const Region& reg,
1341 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001342{
1343 // src and dst with, height and format must be identical. no verification
1344 // is done here.
1345 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001346 uint8_t* src_bits = NULL;
1347 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1348 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001349 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1350
1351 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001352 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1353 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001354 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001355 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001356
1357 Region::const_iterator head(reg.begin());
1358 Region::const_iterator tail(reg.end());
1359 if (head != tail && src_bits && dst_bits) {
1360 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001361 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1362 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001363
1364 while (head != tail) {
1365 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001366 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001367 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001368 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1369 uint8_t const * s = src_bits +
1370 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1371 uint8_t * d = dst_bits +
1372 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001373 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001374 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001375 h = 1;
1376 }
1377 do {
1378 memcpy(d, s, size);
1379 d += dbpr;
1380 s += sbpr;
1381 } while (--h > 0);
1382 }
1383 }
1384
1385 if (src_bits)
1386 src->unlock();
1387
1388 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001389 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001390
1391 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392}
1393
Mathias Agopiana138f892010-05-21 17:24:35 -07001394// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001395
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001396status_t Surface::lock(
1397 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1398{
1399 if (mLockedBuffer != 0) {
1400 ALOGE("Surface::lock failed, already locked");
1401 return INVALID_OPERATION;
1402 }
1403
1404 if (!mConnectedToCpu) {
1405 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1406 if (err) {
1407 return err;
1408 }
1409 // we're intending to do software rendering from this point
1410 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1411 }
1412
1413 ANativeWindowBuffer* out;
1414 int fenceFd = -1;
1415 status_t err = dequeueBuffer(&out, &fenceFd);
1416 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1417 if (err == NO_ERROR) {
1418 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001419 const Rect bounds(backBuffer->width, backBuffer->height);
1420
1421 Region newDirtyRegion;
1422 if (inOutDirtyBounds) {
1423 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1424 newDirtyRegion.andSelf(bounds);
1425 } else {
1426 newDirtyRegion.set(bounds);
1427 }
1428
1429 // figure out if we can copy the frontbuffer back
1430 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1431 const bool canCopyBack = (frontBuffer != 0 &&
1432 backBuffer->width == frontBuffer->width &&
1433 backBuffer->height == frontBuffer->height &&
1434 backBuffer->format == frontBuffer->format);
1435
1436 if (canCopyBack) {
1437 // copy the area that is invalid and not repainted this round
1438 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001439 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001440 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001441 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001442 } else {
1443 // if we can't copy-back anything, modify the user's dirty
1444 // region to make sure they redraw the whole buffer
1445 newDirtyRegion.set(bounds);
1446 mDirtyRegion.clear();
1447 Mutex::Autolock lock(mMutex);
1448 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1449 mSlots[i].dirtyRegion.clear();
1450 }
1451 }
1452
1453
1454 { // scope for the lock
1455 Mutex::Autolock lock(mMutex);
1456 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1457 if (backBufferSlot >= 0) {
1458 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1459 mDirtyRegion.subtract(dirtyRegion);
1460 dirtyRegion = newDirtyRegion;
1461 }
1462 }
1463
1464 mDirtyRegion.orSelf(newDirtyRegion);
1465 if (inOutDirtyBounds) {
1466 *inOutDirtyBounds = newDirtyRegion.getBounds();
1467 }
1468
1469 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001470 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001471 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001472 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001473
1474 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1475 backBuffer->handle);
1476
1477 if (res != 0) {
1478 err = INVALID_OPERATION;
1479 } else {
1480 mLockedBuffer = backBuffer;
1481 outBuffer->width = backBuffer->width;
1482 outBuffer->height = backBuffer->height;
1483 outBuffer->stride = backBuffer->stride;
1484 outBuffer->format = backBuffer->format;
1485 outBuffer->bits = vaddr;
1486 }
1487 }
1488 return err;
1489}
1490
1491status_t Surface::unlockAndPost()
1492{
1493 if (mLockedBuffer == 0) {
1494 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1495 return INVALID_OPERATION;
1496 }
1497
Francis Hart8f396012014-04-01 15:30:53 +03001498 int fd = -1;
1499 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001500 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1501
Francis Hart8f396012014-04-01 15:30:53 +03001502 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001503 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1504 mLockedBuffer->handle, strerror(-err));
1505
1506 mPostedBuffer = mLockedBuffer;
1507 mLockedBuffer = 0;
1508 return err;
1509}
1510
Robert Carr9f31e292016-04-11 11:15:32 -07001511bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1512 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001513 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001514 return true;
1515 }
1516 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1517}
1518
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001519status_t Surface::getUniqueId(uint64_t* outId) const {
1520 Mutex::Autolock lock(mMutex);
1521 return mGraphicBufferProducer->getUniqueId(outId);
1522}
1523
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001524namespace view {
1525
1526status_t Surface::writeToParcel(Parcel* parcel) const {
1527 return writeToParcel(parcel, false);
1528}
1529
1530status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const {
1531 if (parcel == nullptr) return BAD_VALUE;
1532
1533 status_t res = OK;
1534
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001535 if (!nameAlreadyWritten) {
1536 res = parcel->writeString16(name);
1537 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001538
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001539 /* isSingleBuffered defaults to no */
1540 res = parcel->writeInt32(0);
1541 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001542 }
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001543
1544 res = parcel->writeStrongBinder(
1545 IGraphicBufferProducer::asBinder(graphicBufferProducer));
1546
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001547 return res;
1548}
1549
1550status_t Surface::readFromParcel(const Parcel* parcel) {
1551 return readFromParcel(parcel, false);
1552}
1553
1554status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) {
1555 if (parcel == nullptr) return BAD_VALUE;
1556
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001557 status_t res = OK;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001558 if (!nameAlreadyRead) {
1559 name = readMaybeEmptyString16(parcel);
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001560 // Discard this for now
1561 int isSingleBuffered;
1562 res = parcel->readInt32(&isSingleBuffered);
1563 if (res != OK) {
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001564 ALOGE("Can't read isSingleBuffered");
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001565 return res;
1566 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001567 }
1568
1569 sp<IBinder> binder;
1570
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001571 res = parcel->readNullableStrongBinder(&binder);
1572 if (res != OK) {
1573 ALOGE("%s: Can't read strong binder", __FUNCTION__);
1574 return res;
1575 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001576
1577 graphicBufferProducer = interface_cast<IGraphicBufferProducer>(binder);
1578
1579 return OK;
1580}
1581
1582String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
1583 size_t len;
1584 const char16_t* str = parcel->readString16Inplace(&len);
1585 if (str != nullptr) {
1586 return String16(str, len);
1587 } else {
1588 return String16();
1589 }
1590}
1591
1592} // namespace view
1593
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594}; // namespace android