blob: ead8ecb3b4a707923415d5c836f794dcb4bd64e2 [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,
153 const nsecs_t* outRefreshStartTime,
154 const nsecs_t* outGlCompositionDoneTime,
155 const nsecs_t* outDisplayPresentTime,
156 const nsecs_t* outDisplayRetireTime,
157 const nsecs_t* outReleaseTime) {
158 bool checkForRefreshStart = (outRefreshStartTime != nullptr) &&
159 !e->hasFirstRefreshStartInfo();
160 bool checkForGlCompositionDone = (outGlCompositionDoneTime != nullptr) &&
161 !e->hasGpuCompositionDoneInfo();
162 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
163 !e->hasDisplayPresentInfo();
164
165 // DisplayRetire and Release are never available for the last frame.
166 bool checkForDisplayRetire = (outDisplayRetireTime != nullptr) &&
167 !e->hasDisplayRetireInfo() && (e->frameNumber != lastFrameNumber);
168 bool checkForRelease = (outReleaseTime != nullptr) &&
169 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
170
171 // RequestedPresent and Acquire info are always available producer-side.
172 return checkForRefreshStart || checkForGlCompositionDone ||
173 checkForDisplayPresent || checkForDisplayRetire || checkForRelease;
174}
175
Brian Anderson3d4039d2016-09-23 16:31:30 -0700176static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
177 if (dst != nullptr) {
178 *dst = Fence::isValidTimestamp(src) ? src : 0;
179 }
180}
181
182static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) {
183 if (dst != nullptr) {
184 nsecs_t signalTime = src->getSignalTime();
185 *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0;
186 }
187}
188
Brian Anderson069b3652016-07-22 10:32:47 -0700189status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700190 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
191 nsecs_t* outRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700192 nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime,
193 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800194 ATRACE_CALL();
195
Brian Anderson3890c392016-07-25 12:48:08 -0700196 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700197
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700198 if (!mEnableFrameTimestamps) {
199 return INVALID_OPERATION;
200 }
201
Brian Anderson3890c392016-07-25 12:48:08 -0700202 // Verify the requested timestamps are supported.
203 querySupportedTimestampsLocked();
204 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
205 return BAD_VALUE;
206 }
207 if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
208 return BAD_VALUE;
Brian Anderson069b3652016-07-22 10:32:47 -0700209 }
210
Brian Anderson3da8d272016-07-28 16:20:47 -0700211 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700212 if (events == nullptr) {
213 // If the entry isn't available in the producer, it's definitely not
214 // available in the consumer.
215 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700216 }
217
Brian Anderson50143b32016-09-30 14:01:24 -0700218 // Update our cache of events if the requested events are not available.
219 if (checkConsumerForUpdates(events, mLastFrameNumber,
220 outRefreshStartTime, outGlCompositionDoneTime,
221 outDisplayPresentTime, outDisplayRetireTime, outReleaseTime)) {
222 FrameEventHistoryDelta delta;
223 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700224 mFrameEventHistory->applyDelta(delta);
225 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700226 }
227
Brian Anderson3890c392016-07-25 12:48:08 -0700228 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700229 // The entry was available before the update, but was overwritten
230 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700231 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800232 }
Brian Anderson069b3652016-07-22 10:32:47 -0700233
Brian Anderson3d4039d2016-09-23 16:31:30 -0700234 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
235 getFrameTimestamp(outRefreshStartTime, events->firstRefreshStartTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700236
Brian Anderson3d4039d2016-09-23 16:31:30 -0700237 getFrameTimestampFence(outAcquireTime, events->acquireFence);
238 getFrameTimestampFence(
239 outGlCompositionDoneTime, events->gpuCompositionDoneFence);
240 getFrameTimestampFence(
241 outDisplayPresentTime, events->displayPresentFence);
242 getFrameTimestampFence(outDisplayRetireTime, events->displayRetireFence);
243 getFrameTimestampFence(outReleaseTime, events->releaseFence);
Brian Anderson069b3652016-07-22 10:32:47 -0700244
245 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800246}
247
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800248int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
249 Surface* c = getSelf(window);
250 return c->setSwapInterval(interval);
251}
252
253int Surface::hook_dequeueBuffer(ANativeWindow* window,
254 ANativeWindowBuffer** buffer, int* fenceFd) {
255 Surface* c = getSelf(window);
256 return c->dequeueBuffer(buffer, fenceFd);
257}
258
259int Surface::hook_cancelBuffer(ANativeWindow* window,
260 ANativeWindowBuffer* buffer, int fenceFd) {
261 Surface* c = getSelf(window);
262 return c->cancelBuffer(buffer, fenceFd);
263}
264
265int Surface::hook_queueBuffer(ANativeWindow* window,
266 ANativeWindowBuffer* buffer, int fenceFd) {
267 Surface* c = getSelf(window);
268 return c->queueBuffer(buffer, fenceFd);
269}
270
271int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
272 ANativeWindowBuffer** buffer) {
273 Surface* c = getSelf(window);
274 ANativeWindowBuffer* buf;
275 int fenceFd = -1;
276 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600277 if (result != OK) {
278 return result;
279 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800280 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700281 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800282 if (waitResult != OK) {
283 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
284 waitResult);
285 c->cancelBuffer(buf, -1);
286 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700287 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800288 *buffer = buf;
289 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700290}
291
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800292int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
293 ANativeWindowBuffer* buffer) {
294 Surface* c = getSelf(window);
295 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700296}
297
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800298int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
299 ANativeWindowBuffer* buffer) {
300 Surface* c = getSelf(window);
301 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700302}
303
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800304int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
305 ANativeWindowBuffer* buffer) {
306 Surface* c = getSelf(window);
307 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700308}
Mathias Agopian62185b72009-04-16 16:19:50 -0700309
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800310int Surface::hook_query(const ANativeWindow* window,
311 int what, int* value) {
312 const Surface* c = getSelf(window);
313 return c->query(what, value);
314}
315
316int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
317 va_list args;
318 va_start(args, operation);
319 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700320 int result = c->perform(operation, args);
321 va_end(args);
322 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800323}
324
325int Surface::setSwapInterval(int interval) {
326 ATRACE_CALL();
327 // EGL specification states:
328 // interval is silently clamped to minimum and maximum implementation
329 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800330
331 if (interval < minSwapInterval)
332 interval = minSwapInterval;
333
334 if (interval > maxSwapInterval)
335 interval = maxSwapInterval;
336
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700337 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700338 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800339
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700340 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800341}
342
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700343int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800344 ATRACE_CALL();
345 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800346
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800347 uint32_t reqWidth;
348 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800349 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800350 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700351 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800352
353 {
354 Mutex::Autolock lock(mMutex);
355
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800356 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
357 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800358
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800359 reqFormat = mReqFormat;
360 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800361
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700362 enableFrameTimestamps = mEnableFrameTimestamps;
363
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700364 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800365 BufferItem::INVALID_BUFFER_SLOT) {
366 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
367 if (gbuf != NULL) {
368 *buffer = gbuf.get();
369 *fenceFd = -1;
370 return OK;
371 }
372 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800373 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
374
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800375 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800376 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700377 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700378
379 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700380 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700381 reqWidth, reqHeight, reqFormat, reqUsage,
382 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700383 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800384
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800385 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700386 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
387 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
388 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800389 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700390 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800391
392 Mutex::Autolock lock(mMutex);
393
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800394 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700395
396 // this should never happen
397 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
398
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800399 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
400 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700401 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700402
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700403 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700404 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700405 }
406
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800407 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
408 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
409 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700410 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700411 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800412 return result;
413 }
414 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700415
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800416 if (fence->isValid()) {
417 *fenceFd = fence->dup();
418 if (*fenceFd == -1) {
419 ALOGE("dequeueBuffer: error duping fence: %d", errno);
420 // dup() should never fail; something is badly wrong. Soldier on
421 // and hope for the best; the worst that should happen is some
422 // visible corruption that lasts until the next frame.
423 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700424 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800425 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700426 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800427
428 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800429
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700430 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800431 mSharedBufferSlot = buf;
432 mSharedBufferHasBeenQueued = false;
433 } else if (mSharedBufferSlot == buf) {
434 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
435 mSharedBufferHasBeenQueued = false;
436 }
437
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800438 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700439}
440
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800441int Surface::cancelBuffer(android_native_buffer_t* buffer,
442 int fenceFd) {
443 ATRACE_CALL();
444 ALOGV("Surface::cancelBuffer");
445 Mutex::Autolock lock(mMutex);
446 int i = getSlotFromBufferLocked(buffer);
447 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900448 if (fenceFd >= 0) {
449 close(fenceFd);
450 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800451 return i;
452 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800453 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
454 if (fenceFd >= 0) {
455 close(fenceFd);
456 }
457 return OK;
458 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800459 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
460 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800461
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700462 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800463 mSharedBufferHasBeenQueued = true;
464 }
465
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800466 return OK;
467}
468
469int Surface::getSlotFromBufferLocked(
470 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800471 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
472 if (mSlots[i].buffer != NULL &&
473 mSlots[i].buffer->handle == buffer->handle) {
474 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700475 }
476 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800477 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
478 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700479}
480
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800481int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800482 ALOGV("Surface::lockBuffer");
483 Mutex::Autolock lock(mMutex);
484 return OK;
485}
Mathias Agopian631f3582010-05-25 17:51:34 -0700486
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800487int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
488 ATRACE_CALL();
489 ALOGV("Surface::queueBuffer");
490 Mutex::Autolock lock(mMutex);
491 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700492 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800493
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800494 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700495 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700496 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700497 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700498 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800499 } else {
500 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700501 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800502 int i = getSlotFromBufferLocked(buffer);
503 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900504 if (fenceFd >= 0) {
505 close(fenceFd);
506 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800507 return i;
508 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800509 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
510 if (fenceFd >= 0) {
511 close(fenceFd);
512 }
513 return OK;
514 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800517 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700518 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800519 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800521 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
522 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700523 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800524 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700525 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700526
Dan Stozac62acbd2015-04-21 16:42:49 -0700527 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700528 input.setSurfaceDamage(Region::INVALID_REGION);
529 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700530 // Here we do two things:
531 // 1) The surface damage was specified using the OpenGL ES convention of
532 // the origin being in the bottom-left corner. Here we flip to the
533 // convention that the rest of the system uses (top-left corner) by
534 // subtracting all top/bottom coordinates from the buffer height.
535 // 2) If the buffer is coming in rotated (for example, because the EGL
536 // implementation is reacting to the transform hint coming back from
537 // SurfaceFlinger), the surface damage needs to be rotated the
538 // opposite direction, since it was generated assuming an unrotated
539 // buffer (the app doesn't know that the EGL implementation is
540 // reacting to the transform hint behind its back). The
541 // transformations in the switch statement below apply those
542 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
543
544 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700545 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700546 bool rotated90 = (mTransform ^ mStickyTransform) &
547 NATIVE_WINDOW_TRANSFORM_ROT_90;
548 if (rotated90) {
549 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700550 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700551
Dan Stoza5065a552015-03-17 16:23:42 -0700552 Region flippedRegion;
553 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700554 int left = rect.left;
555 int right = rect.right;
556 int top = height - rect.bottom; // Flip from OpenGL convention
557 int bottom = height - rect.top; // Flip from OpenGL convention
558 switch (mTransform ^ mStickyTransform) {
559 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
560 // Rotate 270 degrees
561 Rect flippedRect{top, width - right, bottom, width - left};
562 flippedRegion.orSelf(flippedRect);
563 break;
564 }
565 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
566 // Rotate 180 degrees
567 Rect flippedRect{width - right, height - bottom,
568 width - left, height - top};
569 flippedRegion.orSelf(flippedRect);
570 break;
571 }
572 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
573 // Rotate 90 degrees
574 Rect flippedRect{height - bottom, left,
575 height - top, right};
576 flippedRegion.orSelf(flippedRect);
577 break;
578 }
579 default: {
580 Rect flippedRect{left, top, right, bottom};
581 flippedRegion.orSelf(flippedRect);
582 break;
583 }
584 }
Dan Stoza5065a552015-03-17 16:23:42 -0700585 }
586
587 input.setSurfaceDamage(flippedRegion);
588 }
589
Dan Stoza70ccba52016-07-01 14:00:40 -0700590 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800591 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700592 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800593 if (err != OK) {
594 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
595 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800596
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700597 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700598 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700599 // Update timestamps with the local acquire fence.
600 // The consumer doesn't send it back to prevent us from having two
601 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700602 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700603 std::make_shared<FenceTime>(std::move(fence)));
604
605 // Cache timestamps of signaled fences so we can close their file
606 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700607 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700608 }
609
Brian Anderson50143b32016-09-30 14:01:24 -0700610 mLastFrameNumber = mNextFrameNumber;
611
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700612 mDefaultWidth = output.width;
613 mDefaultHeight = output.height;
614 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700615
Ruben Brunk1681d952014-06-27 15:51:55 -0700616 // Disable transform hint if sticky transform is set.
617 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700618 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700619 }
620
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700621 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700622
Dan Stozac62acbd2015-04-21 16:42:49 -0700623 if (!mConnectedToCpu) {
624 // Clear surface damage back to full-buffer
625 mDirtyRegion = Region::INVALID_REGION;
626 }
Dan Stoza5065a552015-03-17 16:23:42 -0700627
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700628 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800629 mSharedBufferHasBeenQueued = true;
630 }
631
Robert Carr9f31e292016-04-11 11:15:32 -0700632 mQueueBufferCondition.broadcast();
633
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800634 return err;
635}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700636
Brian Anderson069b3652016-07-22 10:32:47 -0700637void Surface::querySupportedTimestampsLocked() const {
638 // mMutex must be locked when calling this method.
639
640 if (mQueriedSupportedTimestamps) {
641 return;
642 }
643 mQueriedSupportedTimestamps = true;
644
Brian Anderson3890c392016-07-25 12:48:08 -0700645 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson3da8d272016-07-28 16:20:47 -0700646 status_t err = composerService()->getSupportedFrameTimestamps(
Brian Anderson069b3652016-07-22 10:32:47 -0700647 &supportedFrameTimestamps);
648
649 if (err != NO_ERROR) {
650 return;
651 }
652
653 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700654 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700655 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700656 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700657 mFrameTimestampsSupportsRetire = true;
658 }
659 }
660}
661
Mathias Agopiana67932f2011-04-20 14:20:59 -0700662int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800663 ATRACE_CALL();
664 ALOGV("Surface::query");
665 { // scope for the lock
666 Mutex::Autolock lock(mMutex);
667 switch (what) {
668 case NATIVE_WINDOW_FORMAT:
669 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800670 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800671 return NO_ERROR;
672 }
673 break;
674 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700675 if (composerService()->authenticateSurfaceTexture(
676 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800677 *value = 1;
678 } else {
679 *value = 0;
680 }
681 return NO_ERROR;
682 }
683 case NATIVE_WINDOW_CONCRETE_TYPE:
684 *value = NATIVE_WINDOW_SURFACE;
685 return NO_ERROR;
686 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800687 *value = static_cast<int>(
688 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800689 return NO_ERROR;
690 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800691 *value = static_cast<int>(
692 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800693 return NO_ERROR;
694 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800695 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800696 return NO_ERROR;
697 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
698 status_t err = NO_ERROR;
699 if (!mConsumerRunningBehind) {
700 *value = 0;
701 } else {
702 err = mGraphicBufferProducer->query(what, value);
703 if (err == NO_ERROR) {
704 mConsumerRunningBehind = *value;
705 }
706 }
707 return err;
708 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700709 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
710 int64_t durationUs = mLastDequeueDuration / 1000;
711 *value = durationUs > std::numeric_limits<int>::max() ?
712 std::numeric_limits<int>::max() :
713 static_cast<int>(durationUs);
714 return NO_ERROR;
715 }
716 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
717 int64_t durationUs = mLastQueueDuration / 1000;
718 *value = durationUs > std::numeric_limits<int>::max() ?
719 std::numeric_limits<int>::max() :
720 static_cast<int>(durationUs);
721 return NO_ERROR;
722 }
Brian Anderson069b3652016-07-22 10:32:47 -0700723 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
724 querySupportedTimestampsLocked();
725 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
726 return NO_ERROR;
727 }
728 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
729 querySupportedTimestampsLocked();
730 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
731 return NO_ERROR;
732 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800733 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700734 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800735 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800736}
737
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800738int Surface::perform(int operation, va_list args)
739{
740 int res = NO_ERROR;
741 switch (operation) {
742 case NATIVE_WINDOW_CONNECT:
743 // deprecated. must return NO_ERROR.
744 break;
745 case NATIVE_WINDOW_DISCONNECT:
746 // deprecated. must return NO_ERROR.
747 break;
748 case NATIVE_WINDOW_SET_USAGE:
749 res = dispatchSetUsage(args);
750 break;
751 case NATIVE_WINDOW_SET_CROP:
752 res = dispatchSetCrop(args);
753 break;
754 case NATIVE_WINDOW_SET_BUFFER_COUNT:
755 res = dispatchSetBufferCount(args);
756 break;
757 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
758 res = dispatchSetBuffersGeometry(args);
759 break;
760 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
761 res = dispatchSetBuffersTransform(args);
762 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700763 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
764 res = dispatchSetBuffersStickyTransform(args);
765 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800766 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
767 res = dispatchSetBuffersTimestamp(args);
768 break;
769 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
770 res = dispatchSetBuffersDimensions(args);
771 break;
772 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
773 res = dispatchSetBuffersUserDimensions(args);
774 break;
775 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
776 res = dispatchSetBuffersFormat(args);
777 break;
778 case NATIVE_WINDOW_LOCK:
779 res = dispatchLock(args);
780 break;
781 case NATIVE_WINDOW_UNLOCK_AND_POST:
782 res = dispatchUnlockAndPost(args);
783 break;
784 case NATIVE_WINDOW_SET_SCALING_MODE:
785 res = dispatchSetScalingMode(args);
786 break;
787 case NATIVE_WINDOW_API_CONNECT:
788 res = dispatchConnect(args);
789 break;
790 case NATIVE_WINDOW_API_DISCONNECT:
791 res = dispatchDisconnect(args);
792 break;
Rachad7cb0d392014-07-29 17:53:53 -0700793 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
794 res = dispatchSetSidebandStream(args);
795 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800796 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
797 res = dispatchSetBuffersDataSpace(args);
798 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700799 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
800 res = dispatchSetSurfaceDamage(args);
801 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700802 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
803 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700804 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800805 case NATIVE_WINDOW_SET_AUTO_REFRESH:
806 res = dispatchSetAutoRefresh(args);
807 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700808 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
809 res = dispatchEnableFrameTimestamps(args);
810 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800811 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
812 res = dispatchGetFrameTimestamps(args);
813 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800814 default:
815 res = NAME_NOT_FOUND;
816 break;
817 }
818 return res;
819}
Mathias Agopiana138f892010-05-21 17:24:35 -0700820
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800821int Surface::dispatchConnect(va_list args) {
822 int api = va_arg(args, int);
823 return connect(api);
824}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800825
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800826int Surface::dispatchDisconnect(va_list args) {
827 int api = va_arg(args, int);
828 return disconnect(api);
829}
830
831int Surface::dispatchSetUsage(va_list args) {
832 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800833 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800834}
835
836int Surface::dispatchSetCrop(va_list args) {
837 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
838 return setCrop(reinterpret_cast<Rect const*>(rect));
839}
840
841int Surface::dispatchSetBufferCount(va_list args) {
842 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800843 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800844}
845
846int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800847 uint32_t width = va_arg(args, uint32_t);
848 uint32_t height = va_arg(args, uint32_t);
849 PixelFormat format = va_arg(args, PixelFormat);
850 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800851 if (err != 0) {
852 return err;
853 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800854 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800855}
856
857int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800858 uint32_t width = va_arg(args, uint32_t);
859 uint32_t height = va_arg(args, uint32_t);
860 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800861}
862
863int Surface::dispatchSetBuffersUserDimensions(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 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800867}
868
869int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800870 PixelFormat format = va_arg(args, PixelFormat);
871 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800872}
873
874int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800875 int mode = va_arg(args, int);
876 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800877}
878
879int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800880 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800881 return setBuffersTransform(transform);
882}
883
Ruben Brunk1681d952014-06-27 15:51:55 -0700884int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800885 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700886 return setBuffersStickyTransform(transform);
887}
888
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800889int Surface::dispatchSetBuffersTimestamp(va_list args) {
890 int64_t timestamp = va_arg(args, int64_t);
891 return setBuffersTimestamp(timestamp);
892}
893
894int Surface::dispatchLock(va_list args) {
895 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
896 ARect* inOutDirtyBounds = va_arg(args, ARect*);
897 return lock(outBuffer, inOutDirtyBounds);
898}
899
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800900int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800901 return unlockAndPost();
902}
903
Rachad7cb0d392014-07-29 17:53:53 -0700904int Surface::dispatchSetSidebandStream(va_list args) {
905 native_handle_t* sH = va_arg(args, native_handle_t*);
906 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
907 setSidebandStream(sidebandHandle);
908 return OK;
909}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800910
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800911int Surface::dispatchSetBuffersDataSpace(va_list args) {
912 android_dataspace dataspace =
913 static_cast<android_dataspace>(va_arg(args, int));
914 return setBuffersDataSpace(dataspace);
915}
916
Dan Stoza5065a552015-03-17 16:23:42 -0700917int Surface::dispatchSetSurfaceDamage(va_list args) {
918 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
919 size_t numRects = va_arg(args, size_t);
920 setSurfaceDamage(rects, numRects);
921 return NO_ERROR;
922}
923
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700924int Surface::dispatchSetSharedBufferMode(va_list args) {
925 bool sharedBufferMode = va_arg(args, int);
926 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800927}
928
929int Surface::dispatchSetAutoRefresh(va_list args) {
930 bool autoRefresh = va_arg(args, int);
931 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700932}
933
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700934int Surface::dispatchEnableFrameTimestamps(va_list args) {
935 bool enable = va_arg(args, int);
936 enableFrameTimestamps(enable);
937 return NO_ERROR;
938}
939
Pablo Ceballosce796e72016-02-04 19:10:51 -0800940int Surface::dispatchGetFrameTimestamps(va_list args) {
941 uint32_t framesAgo = va_arg(args, uint32_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -0700942 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800943 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
944 nsecs_t* outRefreshStartTime = va_arg(args, int64_t*);
945 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700946 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800947 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
948 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700949 return getFrameTimestamps(getNextFrameNumber() - 1 - framesAgo,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700950 outRequestedPresentTime, outAcquireTime, outRefreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700951 outGlCompositionDoneTime, outDisplayPresentTime,
952 outDisplayRetireTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800953}
954
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800955int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -0800956 static sp<IProducerListener> listener = new DummyProducerListener();
957 return connect(api, listener);
958}
959
960int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800961 ATRACE_CALL();
962 ALOGV("Surface::connect");
963 Mutex::Autolock lock(mMutex);
964 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700965 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800966 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700967 mDefaultWidth = output.width;
968 mDefaultHeight = output.height;
969 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -0700970
971 // Disable transform hint if sticky transform is set.
972 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700973 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700974 }
975
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700976 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800977 }
978 if (!err && api == NATIVE_WINDOW_API_CPU) {
979 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -0700980 // Clear the dirty region in case we're switching from a non-CPU API
981 mDirtyRegion.clear();
982 } else if (!err) {
983 // Initialize the dirty region for tracking surface damage
984 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800985 }
Dan Stoza5065a552015-03-17 16:23:42 -0700986
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800987 return err;
988}
989
Mathias Agopian365857d2013-09-11 19:35:45 -0700990
Robert Carr97b9c862016-09-08 13:54:35 -0700991int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800992 ATRACE_CALL();
993 ALOGV("Surface::disconnect");
994 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800995 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
996 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800997 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -0700998 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800999 if (!err) {
1000 mReqFormat = 0;
1001 mReqWidth = 0;
1002 mReqHeight = 0;
1003 mReqUsage = 0;
1004 mCrop.clear();
1005 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1006 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001007 mStickyTransform = 0;
1008
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001009 if (api == NATIVE_WINDOW_API_CPU) {
1010 mConnectedToCpu = false;
1011 }
1012 }
1013 return err;
1014}
1015
Dan Stozad9c49712015-04-27 11:06:01 -07001016int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001017 sp<Fence>* outFence) {
1018 ATRACE_CALL();
1019 ALOGV("Surface::detachNextBuffer");
1020
1021 if (outBuffer == NULL || outFence == NULL) {
1022 return BAD_VALUE;
1023 }
1024
1025 Mutex::Autolock lock(mMutex);
1026
1027 sp<GraphicBuffer> buffer(NULL);
1028 sp<Fence> fence(NULL);
1029 status_t result = mGraphicBufferProducer->detachNextBuffer(
1030 &buffer, &fence);
1031 if (result != NO_ERROR) {
1032 return result;
1033 }
1034
Dan Stozad9c49712015-04-27 11:06:01 -07001035 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001036 if (fence != NULL && fence->isValid()) {
1037 *outFence = fence;
1038 } else {
1039 *outFence = Fence::NO_FENCE;
1040 }
1041
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001042 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1043 if (mSlots[i].buffer != NULL &&
1044 mSlots[i].buffer->handle == buffer->handle) {
1045 mSlots[i].buffer = NULL;
1046 }
1047 }
1048
Dan Stoza231832e2015-03-11 11:55:01 -07001049 return NO_ERROR;
1050}
1051
1052int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1053{
1054 ATRACE_CALL();
1055 ALOGV("Surface::attachBuffer");
1056
1057 Mutex::Autolock lock(mMutex);
1058
1059 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001060 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1061 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001062 int32_t attachedSlot = -1;
1063 status_t result = mGraphicBufferProducer->attachBuffer(
1064 &attachedSlot, graphicBuffer);
1065 if (result != NO_ERROR) {
1066 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001067 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001068 return result;
1069 }
1070 mSlots[attachedSlot].buffer = graphicBuffer;
1071
1072 return NO_ERROR;
1073}
1074
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001075int Surface::setUsage(uint32_t reqUsage)
1076{
1077 ALOGV("Surface::setUsage");
1078 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001079 if (reqUsage != mReqUsage) {
1080 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1081 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001082 mReqUsage = reqUsage;
1083 return OK;
1084}
1085
1086int Surface::setCrop(Rect const* rect)
1087{
1088 ATRACE_CALL();
1089
Pablo Ceballos60d69222015-08-07 14:47:20 -07001090 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001091 if (rect == NULL || rect->isEmpty()) {
1092 realRect.clear();
1093 } else {
1094 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001095 }
1096
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001097 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1098 realRect.left, realRect.top, realRect.right, realRect.bottom);
1099
1100 Mutex::Autolock lock(mMutex);
1101 mCrop = realRect;
1102 return NO_ERROR;
1103}
1104
1105int Surface::setBufferCount(int bufferCount)
1106{
1107 ATRACE_CALL();
1108 ALOGV("Surface::setBufferCount");
1109 Mutex::Autolock lock(mMutex);
1110
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001111 status_t err = NO_ERROR;
1112 if (bufferCount == 0) {
1113 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1114 } else {
1115 int minUndequeuedBuffers = 0;
1116 err = mGraphicBufferProducer->query(
1117 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1118 if (err == NO_ERROR) {
1119 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1120 bufferCount - minUndequeuedBuffers);
1121 }
1122 }
Mathias Agopian90147262010-01-22 11:47:55 -08001123
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001124 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1125 bufferCount, strerror(-err));
1126
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001127 return err;
1128}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001129
Pablo Ceballosfa455352015-08-12 17:47:47 -07001130int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1131 ATRACE_CALL();
1132 ALOGV("Surface::setMaxDequeuedBufferCount");
1133 Mutex::Autolock lock(mMutex);
1134
1135 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1136 maxDequeuedBuffers);
1137 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1138 "returned %s", maxDequeuedBuffers, strerror(-err));
1139
Pablo Ceballosfa455352015-08-12 17:47:47 -07001140 return err;
1141}
1142
1143int Surface::setAsyncMode(bool async) {
1144 ATRACE_CALL();
1145 ALOGV("Surface::setAsyncMode");
1146 Mutex::Autolock lock(mMutex);
1147
1148 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1149 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1150 async, strerror(-err));
1151
Pablo Ceballosfa455352015-08-12 17:47:47 -07001152 return err;
1153}
1154
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001155int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001156 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001157 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001158 Mutex::Autolock lock(mMutex);
1159
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001160 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1161 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001162 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001163 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001164 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001165 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1166 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001167
1168 return err;
1169}
1170
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001171int Surface::setAutoRefresh(bool autoRefresh) {
1172 ATRACE_CALL();
1173 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1174 Mutex::Autolock lock(mMutex);
1175
1176 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1177 if (err == NO_ERROR) {
1178 mAutoRefresh = autoRefresh;
1179 }
1180 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1181 autoRefresh, strerror(-err));
1182 return err;
1183}
1184
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001185int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001186{
1187 ATRACE_CALL();
1188 ALOGV("Surface::setBuffersDimensions");
1189
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001190 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001191 return BAD_VALUE;
1192
1193 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001194 if (width != mReqWidth || height != mReqHeight) {
1195 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1196 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001197 mReqWidth = width;
1198 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001199 return NO_ERROR;
1200}
1201
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001202int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001203{
1204 ATRACE_CALL();
1205 ALOGV("Surface::setBuffersUserDimensions");
1206
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001207 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001208 return BAD_VALUE;
1209
1210 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001211 if (width != mUserWidth || height != mUserHeight) {
1212 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1213 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001214 mUserWidth = width;
1215 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001216 return NO_ERROR;
1217}
1218
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001219int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001220{
1221 ALOGV("Surface::setBuffersFormat");
1222
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001223 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001224 if (format != mReqFormat) {
1225 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1226 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001227 mReqFormat = format;
1228 return NO_ERROR;
1229}
1230
1231int Surface::setScalingMode(int mode)
1232{
1233 ATRACE_CALL();
1234 ALOGV("Surface::setScalingMode(%d)", mode);
1235
1236 switch (mode) {
1237 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1238 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1239 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001240 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001241 break;
1242 default:
1243 ALOGE("unknown scaling mode: %d", mode);
1244 return BAD_VALUE;
1245 }
1246
1247 Mutex::Autolock lock(mMutex);
1248 mScalingMode = mode;
1249 return NO_ERROR;
1250}
1251
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001252int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001253{
1254 ATRACE_CALL();
1255 ALOGV("Surface::setBuffersTransform");
1256 Mutex::Autolock lock(mMutex);
1257 mTransform = transform;
1258 return NO_ERROR;
1259}
1260
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001261int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001262{
1263 ATRACE_CALL();
1264 ALOGV("Surface::setBuffersStickyTransform");
1265 Mutex::Autolock lock(mMutex);
1266 mStickyTransform = transform;
1267 return NO_ERROR;
1268}
1269
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001270int Surface::setBuffersTimestamp(int64_t timestamp)
1271{
1272 ALOGV("Surface::setBuffersTimestamp");
1273 Mutex::Autolock lock(mMutex);
1274 mTimestamp = timestamp;
1275 return NO_ERROR;
1276}
1277
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001278int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1279{
1280 ALOGV("Surface::setBuffersDataSpace");
1281 Mutex::Autolock lock(mMutex);
1282 mDataSpace = dataSpace;
1283 return NO_ERROR;
1284}
1285
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001286void Surface::freeAllBuffers() {
1287 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1288 mSlots[i].buffer = 0;
1289 }
1290}
1291
Dan Stoza5065a552015-03-17 16:23:42 -07001292void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1293 ATRACE_CALL();
1294 ALOGV("Surface::setSurfaceDamage");
1295 Mutex::Autolock lock(mMutex);
1296
Dan Stozac62acbd2015-04-21 16:42:49 -07001297 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001298 mDirtyRegion = Region::INVALID_REGION;
1299 return;
1300 }
1301
1302 mDirtyRegion.clear();
1303 for (size_t r = 0; r < numRects; ++r) {
1304 // We intentionally flip top and bottom here, since because they're
1305 // specified with a bottom-left origin, top > bottom, which fails
1306 // validation in the Region class. We will fix this up when we flip to a
1307 // top-left origin in queueBuffer.
1308 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1309 mDirtyRegion.orSelf(rect);
1310 }
1311}
1312
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001313// ----------------------------------------------------------------------
1314// the lock/unlock APIs must be used from the same thread
1315
1316static status_t copyBlt(
1317 const sp<GraphicBuffer>& dst,
1318 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001319 const Region& reg,
1320 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001321{
1322 // src and dst with, height and format must be identical. no verification
1323 // is done here.
1324 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001325 uint8_t* src_bits = NULL;
1326 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1327 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001328 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1329
1330 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001331 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1332 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001333 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001334 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001335
1336 Region::const_iterator head(reg.begin());
1337 Region::const_iterator tail(reg.end());
1338 if (head != tail && src_bits && dst_bits) {
1339 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001340 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1341 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001342
1343 while (head != tail) {
1344 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001345 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001346 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001347 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1348 uint8_t const * s = src_bits +
1349 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1350 uint8_t * d = dst_bits +
1351 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001352 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001353 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001354 h = 1;
1355 }
1356 do {
1357 memcpy(d, s, size);
1358 d += dbpr;
1359 s += sbpr;
1360 } while (--h > 0);
1361 }
1362 }
1363
1364 if (src_bits)
1365 src->unlock();
1366
1367 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001368 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001369
1370 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371}
1372
Mathias Agopiana138f892010-05-21 17:24:35 -07001373// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001374
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001375status_t Surface::lock(
1376 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1377{
1378 if (mLockedBuffer != 0) {
1379 ALOGE("Surface::lock failed, already locked");
1380 return INVALID_OPERATION;
1381 }
1382
1383 if (!mConnectedToCpu) {
1384 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1385 if (err) {
1386 return err;
1387 }
1388 // we're intending to do software rendering from this point
1389 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1390 }
1391
1392 ANativeWindowBuffer* out;
1393 int fenceFd = -1;
1394 status_t err = dequeueBuffer(&out, &fenceFd);
1395 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1396 if (err == NO_ERROR) {
1397 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001398 const Rect bounds(backBuffer->width, backBuffer->height);
1399
1400 Region newDirtyRegion;
1401 if (inOutDirtyBounds) {
1402 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1403 newDirtyRegion.andSelf(bounds);
1404 } else {
1405 newDirtyRegion.set(bounds);
1406 }
1407
1408 // figure out if we can copy the frontbuffer back
1409 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1410 const bool canCopyBack = (frontBuffer != 0 &&
1411 backBuffer->width == frontBuffer->width &&
1412 backBuffer->height == frontBuffer->height &&
1413 backBuffer->format == frontBuffer->format);
1414
1415 if (canCopyBack) {
1416 // copy the area that is invalid and not repainted this round
1417 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001418 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001419 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001420 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001421 } else {
1422 // if we can't copy-back anything, modify the user's dirty
1423 // region to make sure they redraw the whole buffer
1424 newDirtyRegion.set(bounds);
1425 mDirtyRegion.clear();
1426 Mutex::Autolock lock(mMutex);
1427 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1428 mSlots[i].dirtyRegion.clear();
1429 }
1430 }
1431
1432
1433 { // scope for the lock
1434 Mutex::Autolock lock(mMutex);
1435 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1436 if (backBufferSlot >= 0) {
1437 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1438 mDirtyRegion.subtract(dirtyRegion);
1439 dirtyRegion = newDirtyRegion;
1440 }
1441 }
1442
1443 mDirtyRegion.orSelf(newDirtyRegion);
1444 if (inOutDirtyBounds) {
1445 *inOutDirtyBounds = newDirtyRegion.getBounds();
1446 }
1447
1448 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001449 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001450 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001451 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001452
1453 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1454 backBuffer->handle);
1455
1456 if (res != 0) {
1457 err = INVALID_OPERATION;
1458 } else {
1459 mLockedBuffer = backBuffer;
1460 outBuffer->width = backBuffer->width;
1461 outBuffer->height = backBuffer->height;
1462 outBuffer->stride = backBuffer->stride;
1463 outBuffer->format = backBuffer->format;
1464 outBuffer->bits = vaddr;
1465 }
1466 }
1467 return err;
1468}
1469
1470status_t Surface::unlockAndPost()
1471{
1472 if (mLockedBuffer == 0) {
1473 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1474 return INVALID_OPERATION;
1475 }
1476
Francis Hart8f396012014-04-01 15:30:53 +03001477 int fd = -1;
1478 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001479 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1480
Francis Hart8f396012014-04-01 15:30:53 +03001481 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001482 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1483 mLockedBuffer->handle, strerror(-err));
1484
1485 mPostedBuffer = mLockedBuffer;
1486 mLockedBuffer = 0;
1487 return err;
1488}
1489
Robert Carr9f31e292016-04-11 11:15:32 -07001490bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1491 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001492 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001493 return true;
1494 }
1495 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1496}
1497
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001498status_t Surface::getUniqueId(uint64_t* outId) const {
1499 Mutex::Autolock lock(mMutex);
1500 return mGraphicBufferProducer->getUniqueId(outId);
1501}
1502
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001503namespace view {
1504
1505status_t Surface::writeToParcel(Parcel* parcel) const {
1506 return writeToParcel(parcel, false);
1507}
1508
1509status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const {
1510 if (parcel == nullptr) return BAD_VALUE;
1511
1512 status_t res = OK;
1513
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001514 if (!nameAlreadyWritten) {
1515 res = parcel->writeString16(name);
1516 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001517
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001518 /* isSingleBuffered defaults to no */
1519 res = parcel->writeInt32(0);
1520 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001521 }
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001522
1523 res = parcel->writeStrongBinder(
1524 IGraphicBufferProducer::asBinder(graphicBufferProducer));
1525
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001526 return res;
1527}
1528
1529status_t Surface::readFromParcel(const Parcel* parcel) {
1530 return readFromParcel(parcel, false);
1531}
1532
1533status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) {
1534 if (parcel == nullptr) return BAD_VALUE;
1535
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001536 status_t res = OK;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001537 if (!nameAlreadyRead) {
1538 name = readMaybeEmptyString16(parcel);
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001539 // Discard this for now
1540 int isSingleBuffered;
1541 res = parcel->readInt32(&isSingleBuffered);
1542 if (res != OK) {
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001543 ALOGE("Can't read isSingleBuffered");
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001544 return res;
1545 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001546 }
1547
1548 sp<IBinder> binder;
1549
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001550 res = parcel->readNullableStrongBinder(&binder);
1551 if (res != OK) {
1552 ALOGE("%s: Can't read strong binder", __FUNCTION__);
1553 return res;
1554 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001555
1556 graphicBufferProducer = interface_cast<IGraphicBufferProducer>(binder);
1557
1558 return OK;
1559}
1560
1561String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
1562 size_t len;
1563 const char16_t* str = parcel->readString16Inplace(&len);
1564 if (str != nullptr) {
1565 return String16(str, len);
1566 } else {
1567 return String16();
1568 }
1569}
1570
1571} // namespace view
1572
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573}; // namespace android