blob: 6a02a775929e636f27753713d07a6561dd8ebc36 [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 mNextFrameNumber(1),
53 mQueriedSupportedTimestamps(false),
54 mFrameTimestampsSupportsPresent(false),
Brian Anderson7c3ba8a2016-07-25 12:48:08 -070055 mFrameTimestampsSupportsRetire(false),
56 mEnableFrameTimestamps(false)
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
100sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
101 return mGraphicBufferProducer;
102}
103
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900104void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
105 mGraphicBufferProducer->setSidebandStream(stream);
106}
107
Dan Stoza29a3e902014-06-20 13:13:57 -0700108void Surface::allocateBuffers() {
109 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
110 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700111 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
112 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700113}
114
Dan Stoza812ed062015-06-02 15:45:22 -0700115status_t Surface::setGenerationNumber(uint32_t generation) {
116 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
117 if (result == NO_ERROR) {
118 mGenerationNumber = generation;
119 }
120 return result;
121}
122
Dan Stoza7dde5992015-05-22 09:51:44 -0700123uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700124 Mutex::Autolock lock(mMutex);
125 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700126}
127
Dan Stozac6f30bd2015-06-08 09:32:50 -0700128String8 Surface::getConsumerName() const {
129 return mGraphicBufferProducer->getConsumerName();
130}
131
Dan Stoza127fc632015-06-30 13:43:32 -0700132status_t Surface::setDequeueTimeout(nsecs_t timeout) {
133 return mGraphicBufferProducer->setDequeueTimeout(timeout);
134}
135
Dan Stoza50101d02016-04-07 16:53:23 -0700136status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700137 sp<Fence>* outFence, float outTransformMatrix[16]) {
138 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
139 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700140}
141
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700142void Surface::enableFrameTimestamps(bool enable) {
143 Mutex::Autolock lock(mMutex);
144 mEnableFrameTimestamps = enable;
145}
146
Brian Anderson069b3652016-07-22 10:32:47 -0700147status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700148 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
149 nsecs_t* outRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700150 nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime,
151 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800152 ATRACE_CALL();
153
Brian Anderson3890c392016-07-25 12:48:08 -0700154 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700155
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700156 if (!mEnableFrameTimestamps) {
157 return INVALID_OPERATION;
158 }
159
Brian Anderson3890c392016-07-25 12:48:08 -0700160 // Verify the requested timestamps are supported.
161 querySupportedTimestampsLocked();
162 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
163 return BAD_VALUE;
164 }
165 if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
166 return BAD_VALUE;
Brian Anderson069b3652016-07-22 10:32:47 -0700167 }
168
Brian Anderson3890c392016-07-25 12:48:08 -0700169 FrameEvents* events = mFrameEventHistory.getFrame(frameNumber);
Brian Anderson069b3652016-07-22 10:32:47 -0700170
Brian Anderson3890c392016-07-25 12:48:08 -0700171 // Update our cache of events if the requested events are not available.
172 if (events == nullptr ||
173 (outRequestedPresentTime && !events->hasRequestedPresentInfo()) ||
174 (outAcquireTime && !events->hasAcquireInfo()) ||
175 (outRefreshStartTime && !events->hasFirstRefreshStartInfo()) ||
176 (outGlCompositionDoneTime && !events->hasGpuCompositionDoneInfo()) ||
177 (outDisplayPresentTime && !events->hasDisplayPresentInfo()) ||
178 (outDisplayRetireTime && !events->hasDisplayRetireInfo()) ||
179 (outReleaseTime && !events->hasReleaseInfo())) {
180 FrameEventHistoryDelta delta;
181 mGraphicBufferProducer->getFrameTimestamps(&delta);
182 mFrameEventHistory.applyDelta(delta);
183 events = mFrameEventHistory.getFrame(frameNumber);
184 }
185
186 // A record for the requested frame does not exist.
187 if (events == nullptr) {
Brian Anderson069b3652016-07-22 10:32:47 -0700188 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800189 }
Brian Anderson069b3652016-07-22 10:32:47 -0700190
Brian Anderson3890c392016-07-25 12:48:08 -0700191 events->checkFencesForCompletion();
192
Brian Anderson069b3652016-07-22 10:32:47 -0700193 if (outRequestedPresentTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700194 *outRequestedPresentTime = events->requestedPresentTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700195 }
196 if (outAcquireTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700197 *outAcquireTime = events->acquireTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700198 }
199 if (outRefreshStartTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700200 *outRefreshStartTime = events->firstRefreshStartTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700201 }
202 if (outGlCompositionDoneTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700203 *outGlCompositionDoneTime = events->gpuCompositionDoneTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700204 }
205 if (outDisplayPresentTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700206 *outDisplayPresentTime = events->displayPresentTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700207 }
208 if (outDisplayRetireTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700209 *outDisplayRetireTime = events->displayRetireTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700210 }
211 if (outReleaseTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700212 *outReleaseTime = events->releaseTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700213 }
214
215 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800216}
217
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800218int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
219 Surface* c = getSelf(window);
220 return c->setSwapInterval(interval);
221}
222
223int Surface::hook_dequeueBuffer(ANativeWindow* window,
224 ANativeWindowBuffer** buffer, int* fenceFd) {
225 Surface* c = getSelf(window);
226 return c->dequeueBuffer(buffer, fenceFd);
227}
228
229int Surface::hook_cancelBuffer(ANativeWindow* window,
230 ANativeWindowBuffer* buffer, int fenceFd) {
231 Surface* c = getSelf(window);
232 return c->cancelBuffer(buffer, fenceFd);
233}
234
235int Surface::hook_queueBuffer(ANativeWindow* window,
236 ANativeWindowBuffer* buffer, int fenceFd) {
237 Surface* c = getSelf(window);
238 return c->queueBuffer(buffer, fenceFd);
239}
240
241int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
242 ANativeWindowBuffer** buffer) {
243 Surface* c = getSelf(window);
244 ANativeWindowBuffer* buf;
245 int fenceFd = -1;
246 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600247 if (result != OK) {
248 return result;
249 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800250 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700251 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800252 if (waitResult != OK) {
253 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
254 waitResult);
255 c->cancelBuffer(buf, -1);
256 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700257 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800258 *buffer = buf;
259 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700260}
261
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800262int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
263 ANativeWindowBuffer* buffer) {
264 Surface* c = getSelf(window);
265 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700266}
267
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800268int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
269 ANativeWindowBuffer* buffer) {
270 Surface* c = getSelf(window);
271 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700272}
273
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800274int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
275 ANativeWindowBuffer* buffer) {
276 Surface* c = getSelf(window);
277 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700278}
Mathias Agopian62185b72009-04-16 16:19:50 -0700279
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800280int Surface::hook_query(const ANativeWindow* window,
281 int what, int* value) {
282 const Surface* c = getSelf(window);
283 return c->query(what, value);
284}
285
286int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
287 va_list args;
288 va_start(args, operation);
289 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700290 int result = c->perform(operation, args);
291 va_end(args);
292 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800293}
294
295int Surface::setSwapInterval(int interval) {
296 ATRACE_CALL();
297 // EGL specification states:
298 // interval is silently clamped to minimum and maximum implementation
299 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800300
301 if (interval < minSwapInterval)
302 interval = minSwapInterval;
303
304 if (interval > maxSwapInterval)
305 interval = maxSwapInterval;
306
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700307 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700308 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800309
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700310 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800311}
312
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700313int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800314 ATRACE_CALL();
315 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800316
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800317 uint32_t reqWidth;
318 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800319 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800320 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700321 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800322
323 {
324 Mutex::Autolock lock(mMutex);
325
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800326 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
327 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800328
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800329 reqFormat = mReqFormat;
330 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800331
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700332 enableFrameTimestamps = mEnableFrameTimestamps;
333
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700334 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800335 BufferItem::INVALID_BUFFER_SLOT) {
336 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
337 if (gbuf != NULL) {
338 *buffer = gbuf.get();
339 *fenceFd = -1;
340 return OK;
341 }
342 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800343 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
344
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800345 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800346 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700347 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700348
349 FrameEventHistoryDelta frameTimestamps;
350 FrameEventHistoryDelta* frameTimestampsOrNull =
351 enableFrameTimestamps ? &frameTimestamps : nullptr;
352
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700353 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700354 reqWidth, reqHeight, reqFormat, reqUsage, frameTimestampsOrNull);
Dan Stoza70ccba52016-07-01 14:00:40 -0700355 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800356
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800357 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700358 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
359 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
360 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800361 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700362 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800363
364 Mutex::Autolock lock(mMutex);
365
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800366 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700367
368 // this should never happen
369 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
370
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800371 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
372 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700373 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700374
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700375 if (enableFrameTimestamps) {
376 mFrameEventHistory.applyDelta(frameTimestamps);
377 }
378
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800379 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
380 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
381 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700382 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700383 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800384 return result;
385 }
386 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700387
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800388 if (fence->isValid()) {
389 *fenceFd = fence->dup();
390 if (*fenceFd == -1) {
391 ALOGE("dequeueBuffer: error duping fence: %d", errno);
392 // dup() should never fail; something is badly wrong. Soldier on
393 // and hope for the best; the worst that should happen is some
394 // visible corruption that lasts until the next frame.
395 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700396 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800397 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700398 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800399
400 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800401
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700402 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800403 mSharedBufferSlot = buf;
404 mSharedBufferHasBeenQueued = false;
405 } else if (mSharedBufferSlot == buf) {
406 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
407 mSharedBufferHasBeenQueued = false;
408 }
409
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800410 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700411}
412
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800413int Surface::cancelBuffer(android_native_buffer_t* buffer,
414 int fenceFd) {
415 ATRACE_CALL();
416 ALOGV("Surface::cancelBuffer");
417 Mutex::Autolock lock(mMutex);
418 int i = getSlotFromBufferLocked(buffer);
419 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900420 if (fenceFd >= 0) {
421 close(fenceFd);
422 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800423 return i;
424 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800425 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
426 if (fenceFd >= 0) {
427 close(fenceFd);
428 }
429 return OK;
430 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800431 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
432 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800433
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700434 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800435 mSharedBufferHasBeenQueued = true;
436 }
437
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800438 return OK;
439}
440
441int Surface::getSlotFromBufferLocked(
442 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800443 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
444 if (mSlots[i].buffer != NULL &&
445 mSlots[i].buffer->handle == buffer->handle) {
446 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700447 }
448 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800449 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
450 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700451}
452
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800453int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800454 ALOGV("Surface::lockBuffer");
455 Mutex::Autolock lock(mMutex);
456 return OK;
457}
Mathias Agopian631f3582010-05-25 17:51:34 -0700458
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800459int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
460 ATRACE_CALL();
461 ALOGV("Surface::queueBuffer");
462 Mutex::Autolock lock(mMutex);
463 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700464 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800465
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800466 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700467 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700468 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700469 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700470 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800471 } else {
472 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700473 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800474 int i = getSlotFromBufferLocked(buffer);
475 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900476 if (fenceFd >= 0) {
477 close(fenceFd);
478 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800479 return i;
480 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800481 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
482 if (fenceFd >= 0) {
483 close(fenceFd);
484 }
485 return OK;
486 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800489 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700490 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800491 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800493 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
494 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700495 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800496 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700497 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700498
Dan Stozac62acbd2015-04-21 16:42:49 -0700499 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700500 input.setSurfaceDamage(Region::INVALID_REGION);
501 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700502 // Here we do two things:
503 // 1) The surface damage was specified using the OpenGL ES convention of
504 // the origin being in the bottom-left corner. Here we flip to the
505 // convention that the rest of the system uses (top-left corner) by
506 // subtracting all top/bottom coordinates from the buffer height.
507 // 2) If the buffer is coming in rotated (for example, because the EGL
508 // implementation is reacting to the transform hint coming back from
509 // SurfaceFlinger), the surface damage needs to be rotated the
510 // opposite direction, since it was generated assuming an unrotated
511 // buffer (the app doesn't know that the EGL implementation is
512 // reacting to the transform hint behind its back). The
513 // transformations in the switch statement below apply those
514 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
515
516 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700517 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700518 bool rotated90 = (mTransform ^ mStickyTransform) &
519 NATIVE_WINDOW_TRANSFORM_ROT_90;
520 if (rotated90) {
521 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700522 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700523
Dan Stoza5065a552015-03-17 16:23:42 -0700524 Region flippedRegion;
525 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700526 int left = rect.left;
527 int right = rect.right;
528 int top = height - rect.bottom; // Flip from OpenGL convention
529 int bottom = height - rect.top; // Flip from OpenGL convention
530 switch (mTransform ^ mStickyTransform) {
531 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
532 // Rotate 270 degrees
533 Rect flippedRect{top, width - right, bottom, width - left};
534 flippedRegion.orSelf(flippedRect);
535 break;
536 }
537 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
538 // Rotate 180 degrees
539 Rect flippedRect{width - right, height - bottom,
540 width - left, height - top};
541 flippedRegion.orSelf(flippedRect);
542 break;
543 }
544 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
545 // Rotate 90 degrees
546 Rect flippedRect{height - bottom, left,
547 height - top, right};
548 flippedRegion.orSelf(flippedRect);
549 break;
550 }
551 default: {
552 Rect flippedRect{left, top, right, bottom};
553 flippedRegion.orSelf(flippedRect);
554 break;
555 }
556 }
Dan Stoza5065a552015-03-17 16:23:42 -0700557 }
558
559 input.setSurfaceDamage(flippedRegion);
560 }
561
Dan Stoza70ccba52016-07-01 14:00:40 -0700562 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800563 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700564 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800565 if (err != OK) {
566 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
567 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800568
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700569 if (mEnableFrameTimestamps) {
570 mFrameEventHistory.applyDelta(output.frameTimestamps);
571 // Update timestamps with the local acquire fence.
572 // The consumer doesn't send it back to prevent us from having two
573 // file descriptors of the same fence.
574 mFrameEventHistory.updateAcquireFence(mNextFrameNumber, fence);
575 }
576
577 mDefaultWidth = output.width;
578 mDefaultHeight = output.height;
579 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700580
Ruben Brunk1681d952014-06-27 15:51:55 -0700581 // Disable transform hint if sticky transform is set.
582 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700583 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700584 }
585
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700586 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700587
Dan Stozac62acbd2015-04-21 16:42:49 -0700588 if (!mConnectedToCpu) {
589 // Clear surface damage back to full-buffer
590 mDirtyRegion = Region::INVALID_REGION;
591 }
Dan Stoza5065a552015-03-17 16:23:42 -0700592
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700593 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800594 mSharedBufferHasBeenQueued = true;
595 }
596
Robert Carr9f31e292016-04-11 11:15:32 -0700597 mQueueBufferCondition.broadcast();
598
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800599 return err;
600}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700601
Brian Anderson069b3652016-07-22 10:32:47 -0700602void Surface::querySupportedTimestampsLocked() const {
603 // mMutex must be locked when calling this method.
604
605 if (mQueriedSupportedTimestamps) {
606 return;
607 }
608 mQueriedSupportedTimestamps = true;
609
Brian Anderson3890c392016-07-25 12:48:08 -0700610 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson069b3652016-07-22 10:32:47 -0700611 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
612 status_t err = composer->getSupportedFrameTimestamps(
613 &supportedFrameTimestamps);
614
615 if (err != NO_ERROR) {
616 return;
617 }
618
619 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700620 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700621 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700622 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700623 mFrameTimestampsSupportsRetire = true;
624 }
625 }
626}
627
Mathias Agopiana67932f2011-04-20 14:20:59 -0700628int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800629 ATRACE_CALL();
630 ALOGV("Surface::query");
631 { // scope for the lock
632 Mutex::Autolock lock(mMutex);
633 switch (what) {
634 case NATIVE_WINDOW_FORMAT:
635 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800636 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800637 return NO_ERROR;
638 }
639 break;
640 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
641 sp<ISurfaceComposer> composer(
642 ComposerService::getComposerService());
643 if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) {
644 *value = 1;
645 } else {
646 *value = 0;
647 }
648 return NO_ERROR;
649 }
650 case NATIVE_WINDOW_CONCRETE_TYPE:
651 *value = NATIVE_WINDOW_SURFACE;
652 return NO_ERROR;
653 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800654 *value = static_cast<int>(
655 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800656 return NO_ERROR;
657 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800658 *value = static_cast<int>(
659 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800660 return NO_ERROR;
661 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800662 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800663 return NO_ERROR;
664 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
665 status_t err = NO_ERROR;
666 if (!mConsumerRunningBehind) {
667 *value = 0;
668 } else {
669 err = mGraphicBufferProducer->query(what, value);
670 if (err == NO_ERROR) {
671 mConsumerRunningBehind = *value;
672 }
673 }
674 return err;
675 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700676 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
677 int64_t durationUs = mLastDequeueDuration / 1000;
678 *value = durationUs > std::numeric_limits<int>::max() ?
679 std::numeric_limits<int>::max() :
680 static_cast<int>(durationUs);
681 return NO_ERROR;
682 }
683 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
684 int64_t durationUs = mLastQueueDuration / 1000;
685 *value = durationUs > std::numeric_limits<int>::max() ?
686 std::numeric_limits<int>::max() :
687 static_cast<int>(durationUs);
688 return NO_ERROR;
689 }
Brian Anderson069b3652016-07-22 10:32:47 -0700690 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
691 querySupportedTimestampsLocked();
692 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
693 return NO_ERROR;
694 }
695 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
696 querySupportedTimestampsLocked();
697 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
698 return NO_ERROR;
699 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800700 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700701 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800702 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800703}
704
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800705int Surface::perform(int operation, va_list args)
706{
707 int res = NO_ERROR;
708 switch (operation) {
709 case NATIVE_WINDOW_CONNECT:
710 // deprecated. must return NO_ERROR.
711 break;
712 case NATIVE_WINDOW_DISCONNECT:
713 // deprecated. must return NO_ERROR.
714 break;
715 case NATIVE_WINDOW_SET_USAGE:
716 res = dispatchSetUsage(args);
717 break;
718 case NATIVE_WINDOW_SET_CROP:
719 res = dispatchSetCrop(args);
720 break;
721 case NATIVE_WINDOW_SET_BUFFER_COUNT:
722 res = dispatchSetBufferCount(args);
723 break;
724 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
725 res = dispatchSetBuffersGeometry(args);
726 break;
727 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
728 res = dispatchSetBuffersTransform(args);
729 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700730 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
731 res = dispatchSetBuffersStickyTransform(args);
732 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800733 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
734 res = dispatchSetBuffersTimestamp(args);
735 break;
736 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
737 res = dispatchSetBuffersDimensions(args);
738 break;
739 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
740 res = dispatchSetBuffersUserDimensions(args);
741 break;
742 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
743 res = dispatchSetBuffersFormat(args);
744 break;
745 case NATIVE_WINDOW_LOCK:
746 res = dispatchLock(args);
747 break;
748 case NATIVE_WINDOW_UNLOCK_AND_POST:
749 res = dispatchUnlockAndPost(args);
750 break;
751 case NATIVE_WINDOW_SET_SCALING_MODE:
752 res = dispatchSetScalingMode(args);
753 break;
754 case NATIVE_WINDOW_API_CONNECT:
755 res = dispatchConnect(args);
756 break;
757 case NATIVE_WINDOW_API_DISCONNECT:
758 res = dispatchDisconnect(args);
759 break;
Rachad7cb0d392014-07-29 17:53:53 -0700760 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
761 res = dispatchSetSidebandStream(args);
762 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800763 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
764 res = dispatchSetBuffersDataSpace(args);
765 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700766 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
767 res = dispatchSetSurfaceDamage(args);
768 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700769 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
770 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700771 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800772 case NATIVE_WINDOW_SET_AUTO_REFRESH:
773 res = dispatchSetAutoRefresh(args);
774 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700775 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
776 res = dispatchEnableFrameTimestamps(args);
777 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800778 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
779 res = dispatchGetFrameTimestamps(args);
780 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800781 default:
782 res = NAME_NOT_FOUND;
783 break;
784 }
785 return res;
786}
Mathias Agopiana138f892010-05-21 17:24:35 -0700787
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800788int Surface::dispatchConnect(va_list args) {
789 int api = va_arg(args, int);
790 return connect(api);
791}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800792
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800793int Surface::dispatchDisconnect(va_list args) {
794 int api = va_arg(args, int);
795 return disconnect(api);
796}
797
798int Surface::dispatchSetUsage(va_list args) {
799 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800800 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800801}
802
803int Surface::dispatchSetCrop(va_list args) {
804 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
805 return setCrop(reinterpret_cast<Rect const*>(rect));
806}
807
808int Surface::dispatchSetBufferCount(va_list args) {
809 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800810 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800811}
812
813int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800814 uint32_t width = va_arg(args, uint32_t);
815 uint32_t height = va_arg(args, uint32_t);
816 PixelFormat format = va_arg(args, PixelFormat);
817 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800818 if (err != 0) {
819 return err;
820 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800821 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800822}
823
824int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800825 uint32_t width = va_arg(args, uint32_t);
826 uint32_t height = va_arg(args, uint32_t);
827 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800828}
829
830int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800831 uint32_t width = va_arg(args, uint32_t);
832 uint32_t height = va_arg(args, uint32_t);
833 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800834}
835
836int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800837 PixelFormat format = va_arg(args, PixelFormat);
838 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800839}
840
841int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800842 int mode = va_arg(args, int);
843 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800844}
845
846int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800847 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800848 return setBuffersTransform(transform);
849}
850
Ruben Brunk1681d952014-06-27 15:51:55 -0700851int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800852 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700853 return setBuffersStickyTransform(transform);
854}
855
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800856int Surface::dispatchSetBuffersTimestamp(va_list args) {
857 int64_t timestamp = va_arg(args, int64_t);
858 return setBuffersTimestamp(timestamp);
859}
860
861int Surface::dispatchLock(va_list args) {
862 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
863 ARect* inOutDirtyBounds = va_arg(args, ARect*);
864 return lock(outBuffer, inOutDirtyBounds);
865}
866
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800867int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800868 return unlockAndPost();
869}
870
Rachad7cb0d392014-07-29 17:53:53 -0700871int Surface::dispatchSetSidebandStream(va_list args) {
872 native_handle_t* sH = va_arg(args, native_handle_t*);
873 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
874 setSidebandStream(sidebandHandle);
875 return OK;
876}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800877
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800878int Surface::dispatchSetBuffersDataSpace(va_list args) {
879 android_dataspace dataspace =
880 static_cast<android_dataspace>(va_arg(args, int));
881 return setBuffersDataSpace(dataspace);
882}
883
Dan Stoza5065a552015-03-17 16:23:42 -0700884int Surface::dispatchSetSurfaceDamage(va_list args) {
885 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
886 size_t numRects = va_arg(args, size_t);
887 setSurfaceDamage(rects, numRects);
888 return NO_ERROR;
889}
890
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700891int Surface::dispatchSetSharedBufferMode(va_list args) {
892 bool sharedBufferMode = va_arg(args, int);
893 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800894}
895
896int Surface::dispatchSetAutoRefresh(va_list args) {
897 bool autoRefresh = va_arg(args, int);
898 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700899}
900
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700901int Surface::dispatchEnableFrameTimestamps(va_list args) {
902 bool enable = va_arg(args, int);
903 enableFrameTimestamps(enable);
904 return NO_ERROR;
905}
906
Pablo Ceballosce796e72016-02-04 19:10:51 -0800907int Surface::dispatchGetFrameTimestamps(va_list args) {
908 uint32_t framesAgo = va_arg(args, uint32_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -0700909 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800910 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
911 nsecs_t* outRefreshStartTime = va_arg(args, int64_t*);
912 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700913 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800914 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
915 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700916 return getFrameTimestamps(getNextFrameNumber() - 1 - framesAgo,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700917 outRequestedPresentTime, outAcquireTime, outRefreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700918 outGlCompositionDoneTime, outDisplayPresentTime,
919 outDisplayRetireTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800920}
921
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800922int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -0800923 static sp<IProducerListener> listener = new DummyProducerListener();
924 return connect(api, listener);
925}
926
927int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800928 ATRACE_CALL();
929 ALOGV("Surface::connect");
930 Mutex::Autolock lock(mMutex);
931 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700932 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800933 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700934 mDefaultWidth = output.width;
935 mDefaultHeight = output.height;
936 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -0700937
938 // Disable transform hint if sticky transform is set.
939 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700940 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700941 }
942
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700943 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800944 }
945 if (!err && api == NATIVE_WINDOW_API_CPU) {
946 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -0700947 // Clear the dirty region in case we're switching from a non-CPU API
948 mDirtyRegion.clear();
949 } else if (!err) {
950 // Initialize the dirty region for tracking surface damage
951 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800952 }
Dan Stoza5065a552015-03-17 16:23:42 -0700953
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800954 return err;
955}
956
Mathias Agopian365857d2013-09-11 19:35:45 -0700957
Robert Carr97b9c862016-09-08 13:54:35 -0700958int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800959 ATRACE_CALL();
960 ALOGV("Surface::disconnect");
961 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800962 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
963 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800964 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -0700965 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800966 if (!err) {
967 mReqFormat = 0;
968 mReqWidth = 0;
969 mReqHeight = 0;
970 mReqUsage = 0;
971 mCrop.clear();
972 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
973 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -0700974 mStickyTransform = 0;
975
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800976 if (api == NATIVE_WINDOW_API_CPU) {
977 mConnectedToCpu = false;
978 }
979 }
980 return err;
981}
982
Dan Stozad9c49712015-04-27 11:06:01 -0700983int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -0700984 sp<Fence>* outFence) {
985 ATRACE_CALL();
986 ALOGV("Surface::detachNextBuffer");
987
988 if (outBuffer == NULL || outFence == NULL) {
989 return BAD_VALUE;
990 }
991
992 Mutex::Autolock lock(mMutex);
993
994 sp<GraphicBuffer> buffer(NULL);
995 sp<Fence> fence(NULL);
996 status_t result = mGraphicBufferProducer->detachNextBuffer(
997 &buffer, &fence);
998 if (result != NO_ERROR) {
999 return result;
1000 }
1001
Dan Stozad9c49712015-04-27 11:06:01 -07001002 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001003 if (fence != NULL && fence->isValid()) {
1004 *outFence = fence;
1005 } else {
1006 *outFence = Fence::NO_FENCE;
1007 }
1008
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001009 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1010 if (mSlots[i].buffer != NULL &&
1011 mSlots[i].buffer->handle == buffer->handle) {
1012 mSlots[i].buffer = NULL;
1013 }
1014 }
1015
Dan Stoza231832e2015-03-11 11:55:01 -07001016 return NO_ERROR;
1017}
1018
1019int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1020{
1021 ATRACE_CALL();
1022 ALOGV("Surface::attachBuffer");
1023
1024 Mutex::Autolock lock(mMutex);
1025
1026 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001027 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1028 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001029 int32_t attachedSlot = -1;
1030 status_t result = mGraphicBufferProducer->attachBuffer(
1031 &attachedSlot, graphicBuffer);
1032 if (result != NO_ERROR) {
1033 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001034 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001035 return result;
1036 }
1037 mSlots[attachedSlot].buffer = graphicBuffer;
1038
1039 return NO_ERROR;
1040}
1041
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001042int Surface::setUsage(uint32_t reqUsage)
1043{
1044 ALOGV("Surface::setUsage");
1045 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001046 if (reqUsage != mReqUsage) {
1047 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1048 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001049 mReqUsage = reqUsage;
1050 return OK;
1051}
1052
1053int Surface::setCrop(Rect const* rect)
1054{
1055 ATRACE_CALL();
1056
Pablo Ceballos60d69222015-08-07 14:47:20 -07001057 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001058 if (rect == NULL || rect->isEmpty()) {
1059 realRect.clear();
1060 } else {
1061 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001062 }
1063
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001064 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1065 realRect.left, realRect.top, realRect.right, realRect.bottom);
1066
1067 Mutex::Autolock lock(mMutex);
1068 mCrop = realRect;
1069 return NO_ERROR;
1070}
1071
1072int Surface::setBufferCount(int bufferCount)
1073{
1074 ATRACE_CALL();
1075 ALOGV("Surface::setBufferCount");
1076 Mutex::Autolock lock(mMutex);
1077
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001078 status_t err = NO_ERROR;
1079 if (bufferCount == 0) {
1080 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1081 } else {
1082 int minUndequeuedBuffers = 0;
1083 err = mGraphicBufferProducer->query(
1084 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1085 if (err == NO_ERROR) {
1086 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1087 bufferCount - minUndequeuedBuffers);
1088 }
1089 }
Mathias Agopian90147262010-01-22 11:47:55 -08001090
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001091 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1092 bufferCount, strerror(-err));
1093
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001094 return err;
1095}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001096
Pablo Ceballosfa455352015-08-12 17:47:47 -07001097int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1098 ATRACE_CALL();
1099 ALOGV("Surface::setMaxDequeuedBufferCount");
1100 Mutex::Autolock lock(mMutex);
1101
1102 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1103 maxDequeuedBuffers);
1104 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1105 "returned %s", maxDequeuedBuffers, strerror(-err));
1106
Pablo Ceballosfa455352015-08-12 17:47:47 -07001107 return err;
1108}
1109
1110int Surface::setAsyncMode(bool async) {
1111 ATRACE_CALL();
1112 ALOGV("Surface::setAsyncMode");
1113 Mutex::Autolock lock(mMutex);
1114
1115 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1116 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1117 async, strerror(-err));
1118
Pablo Ceballosfa455352015-08-12 17:47:47 -07001119 return err;
1120}
1121
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001122int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001123 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001124 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001125 Mutex::Autolock lock(mMutex);
1126
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001127 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1128 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001129 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001130 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001131 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001132 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1133 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001134
1135 return err;
1136}
1137
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001138int Surface::setAutoRefresh(bool autoRefresh) {
1139 ATRACE_CALL();
1140 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1141 Mutex::Autolock lock(mMutex);
1142
1143 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1144 if (err == NO_ERROR) {
1145 mAutoRefresh = autoRefresh;
1146 }
1147 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1148 autoRefresh, strerror(-err));
1149 return err;
1150}
1151
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001152int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001153{
1154 ATRACE_CALL();
1155 ALOGV("Surface::setBuffersDimensions");
1156
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001157 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001158 return BAD_VALUE;
1159
1160 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001161 if (width != mReqWidth || height != mReqHeight) {
1162 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1163 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001164 mReqWidth = width;
1165 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001166 return NO_ERROR;
1167}
1168
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001169int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001170{
1171 ATRACE_CALL();
1172 ALOGV("Surface::setBuffersUserDimensions");
1173
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001174 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001175 return BAD_VALUE;
1176
1177 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001178 if (width != mUserWidth || height != mUserHeight) {
1179 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1180 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001181 mUserWidth = width;
1182 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001183 return NO_ERROR;
1184}
1185
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001186int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001187{
1188 ALOGV("Surface::setBuffersFormat");
1189
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001190 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001191 if (format != mReqFormat) {
1192 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1193 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001194 mReqFormat = format;
1195 return NO_ERROR;
1196}
1197
1198int Surface::setScalingMode(int mode)
1199{
1200 ATRACE_CALL();
1201 ALOGV("Surface::setScalingMode(%d)", mode);
1202
1203 switch (mode) {
1204 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1205 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1206 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001207 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001208 break;
1209 default:
1210 ALOGE("unknown scaling mode: %d", mode);
1211 return BAD_VALUE;
1212 }
1213
1214 Mutex::Autolock lock(mMutex);
1215 mScalingMode = mode;
1216 return NO_ERROR;
1217}
1218
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001219int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001220{
1221 ATRACE_CALL();
1222 ALOGV("Surface::setBuffersTransform");
1223 Mutex::Autolock lock(mMutex);
1224 mTransform = transform;
1225 return NO_ERROR;
1226}
1227
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001228int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001229{
1230 ATRACE_CALL();
1231 ALOGV("Surface::setBuffersStickyTransform");
1232 Mutex::Autolock lock(mMutex);
1233 mStickyTransform = transform;
1234 return NO_ERROR;
1235}
1236
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001237int Surface::setBuffersTimestamp(int64_t timestamp)
1238{
1239 ALOGV("Surface::setBuffersTimestamp");
1240 Mutex::Autolock lock(mMutex);
1241 mTimestamp = timestamp;
1242 return NO_ERROR;
1243}
1244
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001245int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1246{
1247 ALOGV("Surface::setBuffersDataSpace");
1248 Mutex::Autolock lock(mMutex);
1249 mDataSpace = dataSpace;
1250 return NO_ERROR;
1251}
1252
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001253void Surface::freeAllBuffers() {
1254 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1255 mSlots[i].buffer = 0;
1256 }
1257}
1258
Dan Stoza5065a552015-03-17 16:23:42 -07001259void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1260 ATRACE_CALL();
1261 ALOGV("Surface::setSurfaceDamage");
1262 Mutex::Autolock lock(mMutex);
1263
Dan Stozac62acbd2015-04-21 16:42:49 -07001264 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001265 mDirtyRegion = Region::INVALID_REGION;
1266 return;
1267 }
1268
1269 mDirtyRegion.clear();
1270 for (size_t r = 0; r < numRects; ++r) {
1271 // We intentionally flip top and bottom here, since because they're
1272 // specified with a bottom-left origin, top > bottom, which fails
1273 // validation in the Region class. We will fix this up when we flip to a
1274 // top-left origin in queueBuffer.
1275 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1276 mDirtyRegion.orSelf(rect);
1277 }
1278}
1279
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001280// ----------------------------------------------------------------------
1281// the lock/unlock APIs must be used from the same thread
1282
1283static status_t copyBlt(
1284 const sp<GraphicBuffer>& dst,
1285 const sp<GraphicBuffer>& src,
1286 const Region& reg)
1287{
1288 // src and dst with, height and format must be identical. no verification
1289 // is done here.
1290 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001291 uint8_t* src_bits = NULL;
1292 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1293 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001294 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1295
1296 uint8_t* dst_bits = NULL;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001297 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1298 reinterpret_cast<void**>(&dst_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001299 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
1300
1301 Region::const_iterator head(reg.begin());
1302 Region::const_iterator tail(reg.end());
1303 if (head != tail && src_bits && dst_bits) {
1304 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001305 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1306 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001307
1308 while (head != tail) {
1309 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001310 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001311 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001312 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1313 uint8_t const * s = src_bits +
1314 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1315 uint8_t * d = dst_bits +
1316 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001317 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001318 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001319 h = 1;
1320 }
1321 do {
1322 memcpy(d, s, size);
1323 d += dbpr;
1324 s += sbpr;
1325 } while (--h > 0);
1326 }
1327 }
1328
1329 if (src_bits)
1330 src->unlock();
1331
1332 if (dst_bits)
1333 dst->unlock();
1334
1335 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336}
1337
Mathias Agopiana138f892010-05-21 17:24:35 -07001338// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001339
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001340status_t Surface::lock(
1341 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1342{
1343 if (mLockedBuffer != 0) {
1344 ALOGE("Surface::lock failed, already locked");
1345 return INVALID_OPERATION;
1346 }
1347
1348 if (!mConnectedToCpu) {
1349 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1350 if (err) {
1351 return err;
1352 }
1353 // we're intending to do software rendering from this point
1354 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1355 }
1356
1357 ANativeWindowBuffer* out;
1358 int fenceFd = -1;
1359 status_t err = dequeueBuffer(&out, &fenceFd);
1360 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1361 if (err == NO_ERROR) {
1362 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001363 const Rect bounds(backBuffer->width, backBuffer->height);
1364
1365 Region newDirtyRegion;
1366 if (inOutDirtyBounds) {
1367 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1368 newDirtyRegion.andSelf(bounds);
1369 } else {
1370 newDirtyRegion.set(bounds);
1371 }
1372
1373 // figure out if we can copy the frontbuffer back
1374 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1375 const bool canCopyBack = (frontBuffer != 0 &&
1376 backBuffer->width == frontBuffer->width &&
1377 backBuffer->height == frontBuffer->height &&
1378 backBuffer->format == frontBuffer->format);
1379
1380 if (canCopyBack) {
1381 // copy the area that is invalid and not repainted this round
1382 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
1383 if (!copyback.isEmpty())
1384 copyBlt(backBuffer, frontBuffer, copyback);
1385 } else {
1386 // if we can't copy-back anything, modify the user's dirty
1387 // region to make sure they redraw the whole buffer
1388 newDirtyRegion.set(bounds);
1389 mDirtyRegion.clear();
1390 Mutex::Autolock lock(mMutex);
1391 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1392 mSlots[i].dirtyRegion.clear();
1393 }
1394 }
1395
1396
1397 { // scope for the lock
1398 Mutex::Autolock lock(mMutex);
1399 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1400 if (backBufferSlot >= 0) {
1401 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1402 mDirtyRegion.subtract(dirtyRegion);
1403 dirtyRegion = newDirtyRegion;
1404 }
1405 }
1406
1407 mDirtyRegion.orSelf(newDirtyRegion);
1408 if (inOutDirtyBounds) {
1409 *inOutDirtyBounds = newDirtyRegion.getBounds();
1410 }
1411
1412 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001413 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001414 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001415 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001416
1417 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1418 backBuffer->handle);
1419
1420 if (res != 0) {
1421 err = INVALID_OPERATION;
1422 } else {
1423 mLockedBuffer = backBuffer;
1424 outBuffer->width = backBuffer->width;
1425 outBuffer->height = backBuffer->height;
1426 outBuffer->stride = backBuffer->stride;
1427 outBuffer->format = backBuffer->format;
1428 outBuffer->bits = vaddr;
1429 }
1430 }
1431 return err;
1432}
1433
1434status_t Surface::unlockAndPost()
1435{
1436 if (mLockedBuffer == 0) {
1437 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1438 return INVALID_OPERATION;
1439 }
1440
Francis Hart8f396012014-04-01 15:30:53 +03001441 int fd = -1;
1442 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001443 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1444
Francis Hart8f396012014-04-01 15:30:53 +03001445 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001446 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1447 mLockedBuffer->handle, strerror(-err));
1448
1449 mPostedBuffer = mLockedBuffer;
1450 mLockedBuffer = 0;
1451 return err;
1452}
1453
Robert Carr9f31e292016-04-11 11:15:32 -07001454bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1455 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001456 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001457 return true;
1458 }
1459 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1460}
1461
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001462status_t Surface::getUniqueId(uint64_t* outId) const {
1463 Mutex::Autolock lock(mMutex);
1464 return mGraphicBufferProducer->getUniqueId(outId);
1465}
1466
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001467namespace view {
1468
1469status_t Surface::writeToParcel(Parcel* parcel) const {
1470 return writeToParcel(parcel, false);
1471}
1472
1473status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const {
1474 if (parcel == nullptr) return BAD_VALUE;
1475
1476 status_t res = OK;
1477
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001478 if (!nameAlreadyWritten) {
1479 res = parcel->writeString16(name);
1480 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001481
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001482 /* isSingleBuffered defaults to no */
1483 res = parcel->writeInt32(0);
1484 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001485 }
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001486
1487 res = parcel->writeStrongBinder(
1488 IGraphicBufferProducer::asBinder(graphicBufferProducer));
1489
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001490 return res;
1491}
1492
1493status_t Surface::readFromParcel(const Parcel* parcel) {
1494 return readFromParcel(parcel, false);
1495}
1496
1497status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) {
1498 if (parcel == nullptr) return BAD_VALUE;
1499
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001500 status_t res = OK;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001501 if (!nameAlreadyRead) {
1502 name = readMaybeEmptyString16(parcel);
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001503 // Discard this for now
1504 int isSingleBuffered;
1505 res = parcel->readInt32(&isSingleBuffered);
1506 if (res != OK) {
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001507 ALOGE("Can't read isSingleBuffered");
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001508 return res;
1509 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001510 }
1511
1512 sp<IBinder> binder;
1513
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001514 res = parcel->readNullableStrongBinder(&binder);
1515 if (res != OK) {
1516 ALOGE("%s: Can't read strong binder", __FUNCTION__);
1517 return res;
1518 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001519
1520 graphicBufferProducer = interface_cast<IGraphicBufferProducer>(binder);
1521
1522 return OK;
1523}
1524
1525String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
1526 size_t len;
1527 const char16_t* str = parcel->readString16Inplace(&len);
1528 if (str != nullptr) {
1529 return String16(str, len);
1530 } else {
1531 return String16();
1532 }
1533}
1534
1535} // namespace view
1536
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537}; // namespace android