blob: c29101e4a02cbe8ca514216da921ba14edf8f25f [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),
55 mFrameTimestampsSupportsRetire(false)
Mathias Agopian62185b72009-04-16 16:19:50 -070056{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080057 // Initialize the ANativeWindow function pointers.
58 ANativeWindow::setSwapInterval = hook_setSwapInterval;
59 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
60 ANativeWindow::cancelBuffer = hook_cancelBuffer;
61 ANativeWindow::queueBuffer = hook_queueBuffer;
62 ANativeWindow::query = hook_query;
63 ANativeWindow::perform = hook_perform;
64
65 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
66 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
67 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
68 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
69
70 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
71 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
72
73 mReqWidth = 0;
74 mReqHeight = 0;
75 mReqFormat = 0;
76 mReqUsage = 0;
77 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080078 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080079 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
80 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070081 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080082 mDefaultWidth = 0;
83 mDefaultHeight = 0;
84 mUserWidth = 0;
85 mUserHeight = 0;
86 mTransformHint = 0;
87 mConsumerRunningBehind = false;
88 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070089 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070090 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070091}
92
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070093Surface::~Surface() {
94 if (mConnectedToCpu) {
95 Surface::disconnect(NATIVE_WINDOW_API_CPU);
96 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -080097}
98
99sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
100 return mGraphicBufferProducer;
101}
102
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900103void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
104 mGraphicBufferProducer->setSidebandStream(stream);
105}
106
Dan Stoza29a3e902014-06-20 13:13:57 -0700107void Surface::allocateBuffers() {
108 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
109 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700110 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
111 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700112}
113
Dan Stoza812ed062015-06-02 15:45:22 -0700114status_t Surface::setGenerationNumber(uint32_t generation) {
115 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
116 if (result == NO_ERROR) {
117 mGenerationNumber = generation;
118 }
119 return result;
120}
121
Dan Stoza7dde5992015-05-22 09:51:44 -0700122uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700123 Mutex::Autolock lock(mMutex);
124 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700125}
126
Dan Stozac6f30bd2015-06-08 09:32:50 -0700127String8 Surface::getConsumerName() const {
128 return mGraphicBufferProducer->getConsumerName();
129}
130
Dan Stoza127fc632015-06-30 13:43:32 -0700131status_t Surface::setDequeueTimeout(nsecs_t timeout) {
132 return mGraphicBufferProducer->setDequeueTimeout(timeout);
133}
134
Dan Stoza50101d02016-04-07 16:53:23 -0700135status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700136 sp<Fence>* outFence, float outTransformMatrix[16]) {
137 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
138 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700139}
140
Brian Anderson069b3652016-07-22 10:32:47 -0700141status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700142 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
143 nsecs_t* outRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700144 nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime,
145 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800146 ATRACE_CALL();
147
Brian Anderson3890c392016-07-25 12:48:08 -0700148 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700149
Brian Anderson3890c392016-07-25 12:48:08 -0700150 // Verify the requested timestamps are supported.
151 querySupportedTimestampsLocked();
152 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
153 return BAD_VALUE;
154 }
155 if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
156 return BAD_VALUE;
Brian Anderson069b3652016-07-22 10:32:47 -0700157 }
158
Brian Anderson3890c392016-07-25 12:48:08 -0700159 FrameEvents* events = mFrameEventHistory.getFrame(frameNumber);
Brian Anderson069b3652016-07-22 10:32:47 -0700160
Brian Anderson3890c392016-07-25 12:48:08 -0700161 // Update our cache of events if the requested events are not available.
162 if (events == nullptr ||
163 (outRequestedPresentTime && !events->hasRequestedPresentInfo()) ||
164 (outAcquireTime && !events->hasAcquireInfo()) ||
165 (outRefreshStartTime && !events->hasFirstRefreshStartInfo()) ||
166 (outGlCompositionDoneTime && !events->hasGpuCompositionDoneInfo()) ||
167 (outDisplayPresentTime && !events->hasDisplayPresentInfo()) ||
168 (outDisplayRetireTime && !events->hasDisplayRetireInfo()) ||
169 (outReleaseTime && !events->hasReleaseInfo())) {
170 FrameEventHistoryDelta delta;
171 mGraphicBufferProducer->getFrameTimestamps(&delta);
172 mFrameEventHistory.applyDelta(delta);
173 events = mFrameEventHistory.getFrame(frameNumber);
174 }
175
176 // A record for the requested frame does not exist.
177 if (events == nullptr) {
Brian Anderson069b3652016-07-22 10:32:47 -0700178 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800179 }
Brian Anderson069b3652016-07-22 10:32:47 -0700180
Brian Anderson3890c392016-07-25 12:48:08 -0700181 events->checkFencesForCompletion();
182
Brian Anderson069b3652016-07-22 10:32:47 -0700183 if (outRequestedPresentTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700184 *outRequestedPresentTime = events->requestedPresentTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700185 }
186 if (outAcquireTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700187 *outAcquireTime = events->acquireTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700188 }
189 if (outRefreshStartTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700190 *outRefreshStartTime = events->firstRefreshStartTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700191 }
192 if (outGlCompositionDoneTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700193 *outGlCompositionDoneTime = events->gpuCompositionDoneTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700194 }
195 if (outDisplayPresentTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700196 *outDisplayPresentTime = events->displayPresentTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700197 }
198 if (outDisplayRetireTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700199 *outDisplayRetireTime = events->displayRetireTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700200 }
201 if (outReleaseTime) {
Brian Anderson3890c392016-07-25 12:48:08 -0700202 *outReleaseTime = events->releaseTime;
Brian Anderson069b3652016-07-22 10:32:47 -0700203 }
204
205 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800206}
207
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800208int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
209 Surface* c = getSelf(window);
210 return c->setSwapInterval(interval);
211}
212
213int Surface::hook_dequeueBuffer(ANativeWindow* window,
214 ANativeWindowBuffer** buffer, int* fenceFd) {
215 Surface* c = getSelf(window);
216 return c->dequeueBuffer(buffer, fenceFd);
217}
218
219int Surface::hook_cancelBuffer(ANativeWindow* window,
220 ANativeWindowBuffer* buffer, int fenceFd) {
221 Surface* c = getSelf(window);
222 return c->cancelBuffer(buffer, fenceFd);
223}
224
225int Surface::hook_queueBuffer(ANativeWindow* window,
226 ANativeWindowBuffer* buffer, int fenceFd) {
227 Surface* c = getSelf(window);
228 return c->queueBuffer(buffer, fenceFd);
229}
230
231int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
232 ANativeWindowBuffer** buffer) {
233 Surface* c = getSelf(window);
234 ANativeWindowBuffer* buf;
235 int fenceFd = -1;
236 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600237 if (result != OK) {
238 return result;
239 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800240 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700241 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800242 if (waitResult != OK) {
243 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
244 waitResult);
245 c->cancelBuffer(buf, -1);
246 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700247 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800248 *buffer = buf;
249 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700250}
251
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800252int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
253 ANativeWindowBuffer* buffer) {
254 Surface* c = getSelf(window);
255 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700256}
257
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800258int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
259 ANativeWindowBuffer* buffer) {
260 Surface* c = getSelf(window);
261 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700262}
263
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800264int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
265 ANativeWindowBuffer* buffer) {
266 Surface* c = getSelf(window);
267 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700268}
Mathias Agopian62185b72009-04-16 16:19:50 -0700269
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800270int Surface::hook_query(const ANativeWindow* window,
271 int what, int* value) {
272 const Surface* c = getSelf(window);
273 return c->query(what, value);
274}
275
276int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
277 va_list args;
278 va_start(args, operation);
279 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700280 int result = c->perform(operation, args);
281 va_end(args);
282 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800283}
284
285int Surface::setSwapInterval(int interval) {
286 ATRACE_CALL();
287 // EGL specification states:
288 // interval is silently clamped to minimum and maximum implementation
289 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800290
291 if (interval < minSwapInterval)
292 interval = minSwapInterval;
293
294 if (interval > maxSwapInterval)
295 interval = maxSwapInterval;
296
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700297 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700298 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800299
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700300 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800301}
302
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700303int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800304 ATRACE_CALL();
305 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800306
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800307 uint32_t reqWidth;
308 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800309 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800310 uint32_t reqUsage;
311
312 {
313 Mutex::Autolock lock(mMutex);
314
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800315 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
316 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800317
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800318 reqFormat = mReqFormat;
319 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800320
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700321 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800322 BufferItem::INVALID_BUFFER_SLOT) {
323 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
324 if (gbuf != NULL) {
325 *buffer = gbuf.get();
326 *fenceFd = -1;
327 return OK;
328 }
329 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800330 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
331
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800332 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800333 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700334 nsecs_t now = systemTime();
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700335 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800336 reqWidth, reqHeight, reqFormat, reqUsage);
Dan Stoza70ccba52016-07-01 14:00:40 -0700337 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800338
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800339 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700340 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
341 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
342 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800343 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700344 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800345
346 Mutex::Autolock lock(mMutex);
347
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800348 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700349
350 // this should never happen
351 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
352
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800353 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
354 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700355 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700356
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800357 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
358 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
359 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700360 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700361 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800362 return result;
363 }
364 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700365
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800366 if (fence->isValid()) {
367 *fenceFd = fence->dup();
368 if (*fenceFd == -1) {
369 ALOGE("dequeueBuffer: error duping fence: %d", errno);
370 // dup() should never fail; something is badly wrong. Soldier on
371 // and hope for the best; the worst that should happen is some
372 // visible corruption that lasts until the next frame.
373 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700374 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800375 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700376 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800377
378 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800379
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700380 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800381 mSharedBufferSlot = buf;
382 mSharedBufferHasBeenQueued = false;
383 } else if (mSharedBufferSlot == buf) {
384 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
385 mSharedBufferHasBeenQueued = false;
386 }
387
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800388 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700389}
390
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800391int Surface::cancelBuffer(android_native_buffer_t* buffer,
392 int fenceFd) {
393 ATRACE_CALL();
394 ALOGV("Surface::cancelBuffer");
395 Mutex::Autolock lock(mMutex);
396 int i = getSlotFromBufferLocked(buffer);
397 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900398 if (fenceFd >= 0) {
399 close(fenceFd);
400 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800401 return i;
402 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800403 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
404 if (fenceFd >= 0) {
405 close(fenceFd);
406 }
407 return OK;
408 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800409 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
410 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800411
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700412 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800413 mSharedBufferHasBeenQueued = true;
414 }
415
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800416 return OK;
417}
418
419int Surface::getSlotFromBufferLocked(
420 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800421 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
422 if (mSlots[i].buffer != NULL &&
423 mSlots[i].buffer->handle == buffer->handle) {
424 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700425 }
426 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800427 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
428 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700429}
430
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800431int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800432 ALOGV("Surface::lockBuffer");
433 Mutex::Autolock lock(mMutex);
434 return OK;
435}
Mathias Agopian631f3582010-05-25 17:51:34 -0700436
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800437int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
438 ATRACE_CALL();
439 ALOGV("Surface::queueBuffer");
440 Mutex::Autolock lock(mMutex);
441 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700442 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800443
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800444 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700445 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700446 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700447 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700448 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800449 } else {
450 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700451 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800452 int i = getSlotFromBufferLocked(buffer);
453 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900454 if (fenceFd >= 0) {
455 close(fenceFd);
456 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800457 return i;
458 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800459 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
460 if (fenceFd >= 0) {
461 close(fenceFd);
462 }
463 return OK;
464 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800465
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800467 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700468 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800469 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800471 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
472 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700473 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800474 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700475 fence, mStickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700476
Dan Stozac62acbd2015-04-21 16:42:49 -0700477 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700478 input.setSurfaceDamage(Region::INVALID_REGION);
479 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700480 // Here we do two things:
481 // 1) The surface damage was specified using the OpenGL ES convention of
482 // the origin being in the bottom-left corner. Here we flip to the
483 // convention that the rest of the system uses (top-left corner) by
484 // subtracting all top/bottom coordinates from the buffer height.
485 // 2) If the buffer is coming in rotated (for example, because the EGL
486 // implementation is reacting to the transform hint coming back from
487 // SurfaceFlinger), the surface damage needs to be rotated the
488 // opposite direction, since it was generated assuming an unrotated
489 // buffer (the app doesn't know that the EGL implementation is
490 // reacting to the transform hint behind its back). The
491 // transformations in the switch statement below apply those
492 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
493
494 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700495 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700496 bool rotated90 = (mTransform ^ mStickyTransform) &
497 NATIVE_WINDOW_TRANSFORM_ROT_90;
498 if (rotated90) {
499 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700500 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700501
Dan Stoza5065a552015-03-17 16:23:42 -0700502 Region flippedRegion;
503 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700504 int left = rect.left;
505 int right = rect.right;
506 int top = height - rect.bottom; // Flip from OpenGL convention
507 int bottom = height - rect.top; // Flip from OpenGL convention
508 switch (mTransform ^ mStickyTransform) {
509 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
510 // Rotate 270 degrees
511 Rect flippedRect{top, width - right, bottom, width - left};
512 flippedRegion.orSelf(flippedRect);
513 break;
514 }
515 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
516 // Rotate 180 degrees
517 Rect flippedRect{width - right, height - bottom,
518 width - left, height - top};
519 flippedRegion.orSelf(flippedRect);
520 break;
521 }
522 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
523 // Rotate 90 degrees
524 Rect flippedRect{height - bottom, left,
525 height - top, right};
526 flippedRegion.orSelf(flippedRect);
527 break;
528 }
529 default: {
530 Rect flippedRect{left, top, right, bottom};
531 flippedRegion.orSelf(flippedRect);
532 break;
533 }
534 }
Dan Stoza5065a552015-03-17 16:23:42 -0700535 }
536
537 input.setSurfaceDamage(flippedRegion);
538 }
539
Dan Stoza70ccba52016-07-01 14:00:40 -0700540 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800541 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700542 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800543 if (err != OK) {
544 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
545 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800546
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800547 uint32_t numPendingBuffers = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -0700548 uint32_t hint = 0;
549 output.deflate(&mDefaultWidth, &mDefaultHeight, &hint,
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700550 &numPendingBuffers, &mNextFrameNumber);
tedbo1e7fa9e2011-06-22 15:52:53 -0700551
Ruben Brunk1681d952014-06-27 15:51:55 -0700552 // Disable transform hint if sticky transform is set.
553 if (mStickyTransform == 0) {
554 mTransformHint = hint;
555 }
556
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800557 mConsumerRunningBehind = (numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700558
Dan Stozac62acbd2015-04-21 16:42:49 -0700559 if (!mConnectedToCpu) {
560 // Clear surface damage back to full-buffer
561 mDirtyRegion = Region::INVALID_REGION;
562 }
Dan Stoza5065a552015-03-17 16:23:42 -0700563
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700564 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800565 mSharedBufferHasBeenQueued = true;
566 }
567
Robert Carr9f31e292016-04-11 11:15:32 -0700568 mQueueBufferCondition.broadcast();
569
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800570 return err;
571}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700572
Brian Anderson069b3652016-07-22 10:32:47 -0700573void Surface::querySupportedTimestampsLocked() const {
574 // mMutex must be locked when calling this method.
575
576 if (mQueriedSupportedTimestamps) {
577 return;
578 }
579 mQueriedSupportedTimestamps = true;
580
Brian Anderson3890c392016-07-25 12:48:08 -0700581 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson069b3652016-07-22 10:32:47 -0700582 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
583 status_t err = composer->getSupportedFrameTimestamps(
584 &supportedFrameTimestamps);
585
586 if (err != NO_ERROR) {
587 return;
588 }
589
590 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700591 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700592 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700593 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700594 mFrameTimestampsSupportsRetire = true;
595 }
596 }
597}
598
Mathias Agopiana67932f2011-04-20 14:20:59 -0700599int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800600 ATRACE_CALL();
601 ALOGV("Surface::query");
602 { // scope for the lock
603 Mutex::Autolock lock(mMutex);
604 switch (what) {
605 case NATIVE_WINDOW_FORMAT:
606 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800607 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800608 return NO_ERROR;
609 }
610 break;
611 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
612 sp<ISurfaceComposer> composer(
613 ComposerService::getComposerService());
614 if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) {
615 *value = 1;
616 } else {
617 *value = 0;
618 }
619 return NO_ERROR;
620 }
621 case NATIVE_WINDOW_CONCRETE_TYPE:
622 *value = NATIVE_WINDOW_SURFACE;
623 return NO_ERROR;
624 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800625 *value = static_cast<int>(
626 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800627 return NO_ERROR;
628 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800629 *value = static_cast<int>(
630 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800631 return NO_ERROR;
632 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800633 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800634 return NO_ERROR;
635 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
636 status_t err = NO_ERROR;
637 if (!mConsumerRunningBehind) {
638 *value = 0;
639 } else {
640 err = mGraphicBufferProducer->query(what, value);
641 if (err == NO_ERROR) {
642 mConsumerRunningBehind = *value;
643 }
644 }
645 return err;
646 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700647 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
648 int64_t durationUs = mLastDequeueDuration / 1000;
649 *value = durationUs > std::numeric_limits<int>::max() ?
650 std::numeric_limits<int>::max() :
651 static_cast<int>(durationUs);
652 return NO_ERROR;
653 }
654 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
655 int64_t durationUs = mLastQueueDuration / 1000;
656 *value = durationUs > std::numeric_limits<int>::max() ?
657 std::numeric_limits<int>::max() :
658 static_cast<int>(durationUs);
659 return NO_ERROR;
660 }
Brian Anderson069b3652016-07-22 10:32:47 -0700661 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
662 querySupportedTimestampsLocked();
663 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
664 return NO_ERROR;
665 }
666 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
667 querySupportedTimestampsLocked();
668 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
669 return NO_ERROR;
670 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800671 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700672 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800673 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800674}
675
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800676int Surface::perform(int operation, va_list args)
677{
678 int res = NO_ERROR;
679 switch (operation) {
680 case NATIVE_WINDOW_CONNECT:
681 // deprecated. must return NO_ERROR.
682 break;
683 case NATIVE_WINDOW_DISCONNECT:
684 // deprecated. must return NO_ERROR.
685 break;
686 case NATIVE_WINDOW_SET_USAGE:
687 res = dispatchSetUsage(args);
688 break;
689 case NATIVE_WINDOW_SET_CROP:
690 res = dispatchSetCrop(args);
691 break;
692 case NATIVE_WINDOW_SET_BUFFER_COUNT:
693 res = dispatchSetBufferCount(args);
694 break;
695 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
696 res = dispatchSetBuffersGeometry(args);
697 break;
698 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
699 res = dispatchSetBuffersTransform(args);
700 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700701 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
702 res = dispatchSetBuffersStickyTransform(args);
703 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800704 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
705 res = dispatchSetBuffersTimestamp(args);
706 break;
707 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
708 res = dispatchSetBuffersDimensions(args);
709 break;
710 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
711 res = dispatchSetBuffersUserDimensions(args);
712 break;
713 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
714 res = dispatchSetBuffersFormat(args);
715 break;
716 case NATIVE_WINDOW_LOCK:
717 res = dispatchLock(args);
718 break;
719 case NATIVE_WINDOW_UNLOCK_AND_POST:
720 res = dispatchUnlockAndPost(args);
721 break;
722 case NATIVE_WINDOW_SET_SCALING_MODE:
723 res = dispatchSetScalingMode(args);
724 break;
725 case NATIVE_WINDOW_API_CONNECT:
726 res = dispatchConnect(args);
727 break;
728 case NATIVE_WINDOW_API_DISCONNECT:
729 res = dispatchDisconnect(args);
730 break;
Rachad7cb0d392014-07-29 17:53:53 -0700731 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
732 res = dispatchSetSidebandStream(args);
733 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800734 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
735 res = dispatchSetBuffersDataSpace(args);
736 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700737 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
738 res = dispatchSetSurfaceDamage(args);
739 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700740 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
741 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700742 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800743 case NATIVE_WINDOW_SET_AUTO_REFRESH:
744 res = dispatchSetAutoRefresh(args);
745 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800746 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
747 res = dispatchGetFrameTimestamps(args);
748 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800749 default:
750 res = NAME_NOT_FOUND;
751 break;
752 }
753 return res;
754}
Mathias Agopiana138f892010-05-21 17:24:35 -0700755
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800756int Surface::dispatchConnect(va_list args) {
757 int api = va_arg(args, int);
758 return connect(api);
759}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800760
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800761int Surface::dispatchDisconnect(va_list args) {
762 int api = va_arg(args, int);
763 return disconnect(api);
764}
765
766int Surface::dispatchSetUsage(va_list args) {
767 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800768 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800769}
770
771int Surface::dispatchSetCrop(va_list args) {
772 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
773 return setCrop(reinterpret_cast<Rect const*>(rect));
774}
775
776int Surface::dispatchSetBufferCount(va_list args) {
777 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800778 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800779}
780
781int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800782 uint32_t width = va_arg(args, uint32_t);
783 uint32_t height = va_arg(args, uint32_t);
784 PixelFormat format = va_arg(args, PixelFormat);
785 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800786 if (err != 0) {
787 return err;
788 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800789 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800790}
791
792int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800793 uint32_t width = va_arg(args, uint32_t);
794 uint32_t height = va_arg(args, uint32_t);
795 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800796}
797
798int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800799 uint32_t width = va_arg(args, uint32_t);
800 uint32_t height = va_arg(args, uint32_t);
801 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800802}
803
804int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800805 PixelFormat format = va_arg(args, PixelFormat);
806 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800807}
808
809int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800810 int mode = va_arg(args, int);
811 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800812}
813
814int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800815 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800816 return setBuffersTransform(transform);
817}
818
Ruben Brunk1681d952014-06-27 15:51:55 -0700819int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800820 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700821 return setBuffersStickyTransform(transform);
822}
823
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800824int Surface::dispatchSetBuffersTimestamp(va_list args) {
825 int64_t timestamp = va_arg(args, int64_t);
826 return setBuffersTimestamp(timestamp);
827}
828
829int Surface::dispatchLock(va_list args) {
830 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
831 ARect* inOutDirtyBounds = va_arg(args, ARect*);
832 return lock(outBuffer, inOutDirtyBounds);
833}
834
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800835int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800836 return unlockAndPost();
837}
838
Rachad7cb0d392014-07-29 17:53:53 -0700839int Surface::dispatchSetSidebandStream(va_list args) {
840 native_handle_t* sH = va_arg(args, native_handle_t*);
841 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
842 setSidebandStream(sidebandHandle);
843 return OK;
844}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800845
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800846int Surface::dispatchSetBuffersDataSpace(va_list args) {
847 android_dataspace dataspace =
848 static_cast<android_dataspace>(va_arg(args, int));
849 return setBuffersDataSpace(dataspace);
850}
851
Dan Stoza5065a552015-03-17 16:23:42 -0700852int Surface::dispatchSetSurfaceDamage(va_list args) {
853 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
854 size_t numRects = va_arg(args, size_t);
855 setSurfaceDamage(rects, numRects);
856 return NO_ERROR;
857}
858
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700859int Surface::dispatchSetSharedBufferMode(va_list args) {
860 bool sharedBufferMode = va_arg(args, int);
861 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800862}
863
864int Surface::dispatchSetAutoRefresh(va_list args) {
865 bool autoRefresh = va_arg(args, int);
866 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700867}
868
Pablo Ceballosce796e72016-02-04 19:10:51 -0800869int Surface::dispatchGetFrameTimestamps(va_list args) {
870 uint32_t framesAgo = va_arg(args, uint32_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -0700871 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800872 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
873 nsecs_t* outRefreshStartTime = va_arg(args, int64_t*);
874 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700875 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800876 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
877 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700878 return getFrameTimestamps(getNextFrameNumber() - 1 - framesAgo,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700879 outRequestedPresentTime, outAcquireTime, outRefreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700880 outGlCompositionDoneTime, outDisplayPresentTime,
881 outDisplayRetireTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800882}
883
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800884int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -0800885 static sp<IProducerListener> listener = new DummyProducerListener();
886 return connect(api, listener);
887}
888
889int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800890 ATRACE_CALL();
891 ALOGV("Surface::connect");
892 Mutex::Autolock lock(mMutex);
893 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700894 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800895 if (err == NO_ERROR) {
896 uint32_t numPendingBuffers = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -0700897 uint32_t hint = 0;
898 output.deflate(&mDefaultWidth, &mDefaultHeight, &hint,
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700899 &numPendingBuffers, &mNextFrameNumber);
Ruben Brunk1681d952014-06-27 15:51:55 -0700900
901 // Disable transform hint if sticky transform is set.
902 if (mStickyTransform == 0) {
903 mTransformHint = hint;
904 }
905
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800906 mConsumerRunningBehind = (numPendingBuffers >= 2);
907 }
908 if (!err && api == NATIVE_WINDOW_API_CPU) {
909 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -0700910 // Clear the dirty region in case we're switching from a non-CPU API
911 mDirtyRegion.clear();
912 } else if (!err) {
913 // Initialize the dirty region for tracking surface damage
914 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800915 }
Dan Stoza5065a552015-03-17 16:23:42 -0700916
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800917 return err;
918}
919
Mathias Agopian365857d2013-09-11 19:35:45 -0700920
Robert Carr97b9c862016-09-08 13:54:35 -0700921int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800922 ATRACE_CALL();
923 ALOGV("Surface::disconnect");
924 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800925 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
926 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800927 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -0700928 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800929 if (!err) {
930 mReqFormat = 0;
931 mReqWidth = 0;
932 mReqHeight = 0;
933 mReqUsage = 0;
934 mCrop.clear();
935 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
936 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -0700937 mStickyTransform = 0;
938
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800939 if (api == NATIVE_WINDOW_API_CPU) {
940 mConnectedToCpu = false;
941 }
942 }
943 return err;
944}
945
Dan Stozad9c49712015-04-27 11:06:01 -0700946int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -0700947 sp<Fence>* outFence) {
948 ATRACE_CALL();
949 ALOGV("Surface::detachNextBuffer");
950
951 if (outBuffer == NULL || outFence == NULL) {
952 return BAD_VALUE;
953 }
954
955 Mutex::Autolock lock(mMutex);
956
957 sp<GraphicBuffer> buffer(NULL);
958 sp<Fence> fence(NULL);
959 status_t result = mGraphicBufferProducer->detachNextBuffer(
960 &buffer, &fence);
961 if (result != NO_ERROR) {
962 return result;
963 }
964
Dan Stozad9c49712015-04-27 11:06:01 -0700965 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -0700966 if (fence != NULL && fence->isValid()) {
967 *outFence = fence;
968 } else {
969 *outFence = Fence::NO_FENCE;
970 }
971
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800972 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
973 if (mSlots[i].buffer != NULL &&
974 mSlots[i].buffer->handle == buffer->handle) {
975 mSlots[i].buffer = NULL;
976 }
977 }
978
Dan Stoza231832e2015-03-11 11:55:01 -0700979 return NO_ERROR;
980}
981
982int Surface::attachBuffer(ANativeWindowBuffer* buffer)
983{
984 ATRACE_CALL();
985 ALOGV("Surface::attachBuffer");
986
987 Mutex::Autolock lock(mMutex);
988
989 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -0700990 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
991 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -0700992 int32_t attachedSlot = -1;
993 status_t result = mGraphicBufferProducer->attachBuffer(
994 &attachedSlot, graphicBuffer);
995 if (result != NO_ERROR) {
996 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -0700997 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -0700998 return result;
999 }
1000 mSlots[attachedSlot].buffer = graphicBuffer;
1001
1002 return NO_ERROR;
1003}
1004
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001005int Surface::setUsage(uint32_t reqUsage)
1006{
1007 ALOGV("Surface::setUsage");
1008 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001009 if (reqUsage != mReqUsage) {
1010 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1011 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001012 mReqUsage = reqUsage;
1013 return OK;
1014}
1015
1016int Surface::setCrop(Rect const* rect)
1017{
1018 ATRACE_CALL();
1019
Pablo Ceballos60d69222015-08-07 14:47:20 -07001020 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001021 if (rect == NULL || rect->isEmpty()) {
1022 realRect.clear();
1023 } else {
1024 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001025 }
1026
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001027 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1028 realRect.left, realRect.top, realRect.right, realRect.bottom);
1029
1030 Mutex::Autolock lock(mMutex);
1031 mCrop = realRect;
1032 return NO_ERROR;
1033}
1034
1035int Surface::setBufferCount(int bufferCount)
1036{
1037 ATRACE_CALL();
1038 ALOGV("Surface::setBufferCount");
1039 Mutex::Autolock lock(mMutex);
1040
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001041 status_t err = NO_ERROR;
1042 if (bufferCount == 0) {
1043 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1044 } else {
1045 int minUndequeuedBuffers = 0;
1046 err = mGraphicBufferProducer->query(
1047 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1048 if (err == NO_ERROR) {
1049 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1050 bufferCount - minUndequeuedBuffers);
1051 }
1052 }
Mathias Agopian90147262010-01-22 11:47:55 -08001053
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001054 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1055 bufferCount, strerror(-err));
1056
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001057 return err;
1058}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001059
Pablo Ceballosfa455352015-08-12 17:47:47 -07001060int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1061 ATRACE_CALL();
1062 ALOGV("Surface::setMaxDequeuedBufferCount");
1063 Mutex::Autolock lock(mMutex);
1064
1065 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1066 maxDequeuedBuffers);
1067 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1068 "returned %s", maxDequeuedBuffers, strerror(-err));
1069
Pablo Ceballosfa455352015-08-12 17:47:47 -07001070 return err;
1071}
1072
1073int Surface::setAsyncMode(bool async) {
1074 ATRACE_CALL();
1075 ALOGV("Surface::setAsyncMode");
1076 Mutex::Autolock lock(mMutex);
1077
1078 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1079 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1080 async, strerror(-err));
1081
Pablo Ceballosfa455352015-08-12 17:47:47 -07001082 return err;
1083}
1084
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001085int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001086 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001087 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001088 Mutex::Autolock lock(mMutex);
1089
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001090 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1091 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001092 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001093 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001094 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001095 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1096 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001097
1098 return err;
1099}
1100
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001101int Surface::setAutoRefresh(bool autoRefresh) {
1102 ATRACE_CALL();
1103 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1104 Mutex::Autolock lock(mMutex);
1105
1106 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1107 if (err == NO_ERROR) {
1108 mAutoRefresh = autoRefresh;
1109 }
1110 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1111 autoRefresh, strerror(-err));
1112 return err;
1113}
1114
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001115int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001116{
1117 ATRACE_CALL();
1118 ALOGV("Surface::setBuffersDimensions");
1119
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001120 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001121 return BAD_VALUE;
1122
1123 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001124 if (width != mReqWidth || height != mReqHeight) {
1125 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1126 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001127 mReqWidth = width;
1128 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001129 return NO_ERROR;
1130}
1131
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001132int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001133{
1134 ATRACE_CALL();
1135 ALOGV("Surface::setBuffersUserDimensions");
1136
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001137 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001138 return BAD_VALUE;
1139
1140 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001141 if (width != mUserWidth || height != mUserHeight) {
1142 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1143 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001144 mUserWidth = width;
1145 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001146 return NO_ERROR;
1147}
1148
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001149int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001150{
1151 ALOGV("Surface::setBuffersFormat");
1152
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001153 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001154 if (format != mReqFormat) {
1155 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1156 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001157 mReqFormat = format;
1158 return NO_ERROR;
1159}
1160
1161int Surface::setScalingMode(int mode)
1162{
1163 ATRACE_CALL();
1164 ALOGV("Surface::setScalingMode(%d)", mode);
1165
1166 switch (mode) {
1167 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1168 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1169 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001170 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001171 break;
1172 default:
1173 ALOGE("unknown scaling mode: %d", mode);
1174 return BAD_VALUE;
1175 }
1176
1177 Mutex::Autolock lock(mMutex);
1178 mScalingMode = mode;
1179 return NO_ERROR;
1180}
1181
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001182int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001183{
1184 ATRACE_CALL();
1185 ALOGV("Surface::setBuffersTransform");
1186 Mutex::Autolock lock(mMutex);
1187 mTransform = transform;
1188 return NO_ERROR;
1189}
1190
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001191int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001192{
1193 ATRACE_CALL();
1194 ALOGV("Surface::setBuffersStickyTransform");
1195 Mutex::Autolock lock(mMutex);
1196 mStickyTransform = transform;
1197 return NO_ERROR;
1198}
1199
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001200int Surface::setBuffersTimestamp(int64_t timestamp)
1201{
1202 ALOGV("Surface::setBuffersTimestamp");
1203 Mutex::Autolock lock(mMutex);
1204 mTimestamp = timestamp;
1205 return NO_ERROR;
1206}
1207
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001208int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1209{
1210 ALOGV("Surface::setBuffersDataSpace");
1211 Mutex::Autolock lock(mMutex);
1212 mDataSpace = dataSpace;
1213 return NO_ERROR;
1214}
1215
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001216void Surface::freeAllBuffers() {
1217 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1218 mSlots[i].buffer = 0;
1219 }
1220}
1221
Dan Stoza5065a552015-03-17 16:23:42 -07001222void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1223 ATRACE_CALL();
1224 ALOGV("Surface::setSurfaceDamage");
1225 Mutex::Autolock lock(mMutex);
1226
Dan Stozac62acbd2015-04-21 16:42:49 -07001227 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001228 mDirtyRegion = Region::INVALID_REGION;
1229 return;
1230 }
1231
1232 mDirtyRegion.clear();
1233 for (size_t r = 0; r < numRects; ++r) {
1234 // We intentionally flip top and bottom here, since because they're
1235 // specified with a bottom-left origin, top > bottom, which fails
1236 // validation in the Region class. We will fix this up when we flip to a
1237 // top-left origin in queueBuffer.
1238 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1239 mDirtyRegion.orSelf(rect);
1240 }
1241}
1242
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001243// ----------------------------------------------------------------------
1244// the lock/unlock APIs must be used from the same thread
1245
1246static status_t copyBlt(
1247 const sp<GraphicBuffer>& dst,
1248 const sp<GraphicBuffer>& src,
1249 const Region& reg)
1250{
1251 // src and dst with, height and format must be identical. no verification
1252 // is done here.
1253 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001254 uint8_t* src_bits = NULL;
1255 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1256 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001257 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1258
1259 uint8_t* dst_bits = NULL;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001260 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1261 reinterpret_cast<void**>(&dst_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001262 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
1263
1264 Region::const_iterator head(reg.begin());
1265 Region::const_iterator tail(reg.end());
1266 if (head != tail && src_bits && dst_bits) {
1267 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001268 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1269 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001270
1271 while (head != tail) {
1272 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001273 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001274 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001275 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1276 uint8_t const * s = src_bits +
1277 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1278 uint8_t * d = dst_bits +
1279 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001280 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001281 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001282 h = 1;
1283 }
1284 do {
1285 memcpy(d, s, size);
1286 d += dbpr;
1287 s += sbpr;
1288 } while (--h > 0);
1289 }
1290 }
1291
1292 if (src_bits)
1293 src->unlock();
1294
1295 if (dst_bits)
1296 dst->unlock();
1297
1298 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299}
1300
Mathias Agopiana138f892010-05-21 17:24:35 -07001301// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001302
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001303status_t Surface::lock(
1304 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1305{
1306 if (mLockedBuffer != 0) {
1307 ALOGE("Surface::lock failed, already locked");
1308 return INVALID_OPERATION;
1309 }
1310
1311 if (!mConnectedToCpu) {
1312 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1313 if (err) {
1314 return err;
1315 }
1316 // we're intending to do software rendering from this point
1317 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1318 }
1319
1320 ANativeWindowBuffer* out;
1321 int fenceFd = -1;
1322 status_t err = dequeueBuffer(&out, &fenceFd);
1323 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1324 if (err == NO_ERROR) {
1325 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001326 const Rect bounds(backBuffer->width, backBuffer->height);
1327
1328 Region newDirtyRegion;
1329 if (inOutDirtyBounds) {
1330 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1331 newDirtyRegion.andSelf(bounds);
1332 } else {
1333 newDirtyRegion.set(bounds);
1334 }
1335
1336 // figure out if we can copy the frontbuffer back
1337 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1338 const bool canCopyBack = (frontBuffer != 0 &&
1339 backBuffer->width == frontBuffer->width &&
1340 backBuffer->height == frontBuffer->height &&
1341 backBuffer->format == frontBuffer->format);
1342
1343 if (canCopyBack) {
1344 // copy the area that is invalid and not repainted this round
1345 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
1346 if (!copyback.isEmpty())
1347 copyBlt(backBuffer, frontBuffer, copyback);
1348 } else {
1349 // if we can't copy-back anything, modify the user's dirty
1350 // region to make sure they redraw the whole buffer
1351 newDirtyRegion.set(bounds);
1352 mDirtyRegion.clear();
1353 Mutex::Autolock lock(mMutex);
1354 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1355 mSlots[i].dirtyRegion.clear();
1356 }
1357 }
1358
1359
1360 { // scope for the lock
1361 Mutex::Autolock lock(mMutex);
1362 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1363 if (backBufferSlot >= 0) {
1364 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1365 mDirtyRegion.subtract(dirtyRegion);
1366 dirtyRegion = newDirtyRegion;
1367 }
1368 }
1369
1370 mDirtyRegion.orSelf(newDirtyRegion);
1371 if (inOutDirtyBounds) {
1372 *inOutDirtyBounds = newDirtyRegion.getBounds();
1373 }
1374
1375 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001376 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001377 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001378 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001379
1380 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1381 backBuffer->handle);
1382
1383 if (res != 0) {
1384 err = INVALID_OPERATION;
1385 } else {
1386 mLockedBuffer = backBuffer;
1387 outBuffer->width = backBuffer->width;
1388 outBuffer->height = backBuffer->height;
1389 outBuffer->stride = backBuffer->stride;
1390 outBuffer->format = backBuffer->format;
1391 outBuffer->bits = vaddr;
1392 }
1393 }
1394 return err;
1395}
1396
1397status_t Surface::unlockAndPost()
1398{
1399 if (mLockedBuffer == 0) {
1400 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1401 return INVALID_OPERATION;
1402 }
1403
Francis Hart8f396012014-04-01 15:30:53 +03001404 int fd = -1;
1405 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001406 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1407
Francis Hart8f396012014-04-01 15:30:53 +03001408 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001409 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1410 mLockedBuffer->handle, strerror(-err));
1411
1412 mPostedBuffer = mLockedBuffer;
1413 mLockedBuffer = 0;
1414 return err;
1415}
1416
Robert Carr9f31e292016-04-11 11:15:32 -07001417bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1418 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001419 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001420 return true;
1421 }
1422 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1423}
1424
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001425status_t Surface::getUniqueId(uint64_t* outId) const {
1426 Mutex::Autolock lock(mMutex);
1427 return mGraphicBufferProducer->getUniqueId(outId);
1428}
1429
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001430namespace view {
1431
1432status_t Surface::writeToParcel(Parcel* parcel) const {
1433 return writeToParcel(parcel, false);
1434}
1435
1436status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const {
1437 if (parcel == nullptr) return BAD_VALUE;
1438
1439 status_t res = OK;
1440
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001441 if (!nameAlreadyWritten) {
1442 res = parcel->writeString16(name);
1443 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001444
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001445 /* isSingleBuffered defaults to no */
1446 res = parcel->writeInt32(0);
1447 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001448 }
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001449
1450 res = parcel->writeStrongBinder(
1451 IGraphicBufferProducer::asBinder(graphicBufferProducer));
1452
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001453 return res;
1454}
1455
1456status_t Surface::readFromParcel(const Parcel* parcel) {
1457 return readFromParcel(parcel, false);
1458}
1459
1460status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) {
1461 if (parcel == nullptr) return BAD_VALUE;
1462
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001463 status_t res = OK;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001464 if (!nameAlreadyRead) {
1465 name = readMaybeEmptyString16(parcel);
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001466 // Discard this for now
1467 int isSingleBuffered;
1468 res = parcel->readInt32(&isSingleBuffered);
1469 if (res != OK) {
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001470 ALOGE("Can't read isSingleBuffered");
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001471 return res;
1472 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001473 }
1474
1475 sp<IBinder> binder;
1476
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001477 res = parcel->readNullableStrongBinder(&binder);
1478 if (res != OK) {
1479 ALOGE("%s: Can't read strong binder", __FUNCTION__);
1480 return res;
1481 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001482
1483 graphicBufferProducer = interface_cast<IGraphicBufferProducer>(binder);
1484
1485 return OK;
1486}
1487
1488String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
1489 size_t len;
1490 const char16_t* str = parcel->readString16Inplace(&len);
1491 if (str != nullptr) {
1492 return String16(str, len);
1493 } else {
1494 return String16();
1495 }
1496}
1497
1498} // namespace view
1499
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500}; // namespace android