blob: 7607b42b818454ba075bfd42a33e68a0be79499c [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
Francis Hartdc10f842014-12-01 16:04:49 +020023// We would eliminate the non-conforming zero-length array, but we can't since
24// this is effectively included from the Linux kernel
25#pragma clang diagnostic push
26#pragma clang diagnostic ignored "-Wzero-length-array"
27#include <sync/sync.h>
28#pragma clang diagnostic pop
29
Mathias Agopiane3c697f2013-02-14 17:11:02 -080030#include <binder/Parcel.h>
31
Mathias Agopian9cce3252010-02-09 17:46:37 -080032#include <utils/Log.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080033#include <utils/Trace.h>
Rachad7cb0d392014-07-29 17:53:53 -070034#include <utils/NativeHandle.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080035
Mathias Agopiane3c697f2013-02-14 17:11:02 -080036#include <ui/Fence.h>
Dan Stoza5065a552015-03-17 16:23:42 -070037#include <ui/Region.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070038
Dan Stozaf0eaf252014-03-21 13:05:51 -070039#include <gui/IProducerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080040#include <gui/ISurfaceComposer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080041#include <gui/SurfaceComposerClient.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042#include <gui/GLConsumer.h>
43#include <gui/Surface.h>
44
45#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070046
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047namespace android {
48
Mathias Agopiane3c697f2013-02-14 17:11:02 -080049Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070050 const sp<IGraphicBufferProducer>& bufferProducer,
51 bool controlledByApp)
Dan Stoza812ed062015-06-02 15:45:22 -070052 : mGraphicBufferProducer(bufferProducer),
Pablo Ceballos60d69222015-08-07 14:47:20 -070053 mCrop(Rect::EMPTY_RECT),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080054 mGenerationNumber(0),
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070055 mSharedBufferMode(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080056 mAutoRefresh(false),
57 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
Pablo Ceballosbc8c1922016-07-01 14:15:41 -070058 mSharedBufferHasBeenQueued(false),
59 mNextFrameNumber(1)
Mathias Agopian62185b72009-04-16 16:19:50 -070060{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080061 // Initialize the ANativeWindow function pointers.
62 ANativeWindow::setSwapInterval = hook_setSwapInterval;
63 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
64 ANativeWindow::cancelBuffer = hook_cancelBuffer;
65 ANativeWindow::queueBuffer = hook_queueBuffer;
66 ANativeWindow::query = hook_query;
67 ANativeWindow::perform = hook_perform;
68
69 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
70 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
71 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
72 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
73
74 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
75 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
76
77 mReqWidth = 0;
78 mReqHeight = 0;
79 mReqFormat = 0;
80 mReqUsage = 0;
81 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080082 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080083 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
84 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070085 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080086 mDefaultWidth = 0;
87 mDefaultHeight = 0;
88 mUserWidth = 0;
89 mUserHeight = 0;
90 mTransformHint = 0;
91 mConsumerRunningBehind = false;
92 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070093 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070094 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070095}
96
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070097Surface::~Surface() {
98 if (mConnectedToCpu) {
99 Surface::disconnect(NATIVE_WINDOW_API_CPU);
100 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800101}
102
103sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
104 return mGraphicBufferProducer;
105}
106
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900107void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
108 mGraphicBufferProducer->setSidebandStream(stream);
109}
110
Dan Stoza29a3e902014-06-20 13:13:57 -0700111void Surface::allocateBuffers() {
112 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
113 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700114 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
115 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700116}
117
Dan Stoza812ed062015-06-02 15:45:22 -0700118status_t Surface::setGenerationNumber(uint32_t generation) {
119 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
120 if (result == NO_ERROR) {
121 mGenerationNumber = generation;
122 }
123 return result;
124}
125
Dan Stoza7dde5992015-05-22 09:51:44 -0700126uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700127 Mutex::Autolock lock(mMutex);
128 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700129}
130
Dan Stozac6f30bd2015-06-08 09:32:50 -0700131String8 Surface::getConsumerName() const {
132 return mGraphicBufferProducer->getConsumerName();
133}
134
Dan Stoza127fc632015-06-30 13:43:32 -0700135status_t Surface::setDequeueTimeout(nsecs_t timeout) {
136 return mGraphicBufferProducer->setDequeueTimeout(timeout);
137}
138
Dan Stoza50101d02016-04-07 16:53:23 -0700139status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700140 sp<Fence>* outFence, float outTransformMatrix[16]) {
141 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
142 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700143}
144
Pablo Ceballosce796e72016-02-04 19:10:51 -0800145bool Surface::getFrameTimestamps(uint64_t frameNumber, nsecs_t* outPostedTime,
146 nsecs_t* outAcquireTime, nsecs_t* outRefreshStartTime,
147 nsecs_t* outGlCompositionDoneTime, nsecs_t* outDisplayRetireTime,
148 nsecs_t* outReleaseTime) {
149 ATRACE_CALL();
150
151 FrameTimestamps timestamps;
152 bool found = mGraphicBufferProducer->getFrameTimestamps(frameNumber,
153 &timestamps);
154 if (found) {
155 if (outPostedTime) {
156 *outPostedTime = timestamps.postedTime;
157 }
158 if (outAcquireTime) {
159 *outAcquireTime = timestamps.acquireTime;
160 }
161 if (outRefreshStartTime) {
162 *outRefreshStartTime = timestamps.refreshStartTime;
163 }
164 if (outGlCompositionDoneTime) {
165 *outGlCompositionDoneTime = timestamps.glCompositionDoneTime;
166 }
167 if (outDisplayRetireTime) {
168 *outDisplayRetireTime = timestamps.displayRetireTime;
169 }
170 if (outReleaseTime) {
171 *outReleaseTime = timestamps.releaseTime;
172 }
173 return true;
174 }
175 return false;
176}
177
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800178int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
179 Surface* c = getSelf(window);
180 return c->setSwapInterval(interval);
181}
182
183int Surface::hook_dequeueBuffer(ANativeWindow* window,
184 ANativeWindowBuffer** buffer, int* fenceFd) {
185 Surface* c = getSelf(window);
186 return c->dequeueBuffer(buffer, fenceFd);
187}
188
189int Surface::hook_cancelBuffer(ANativeWindow* window,
190 ANativeWindowBuffer* buffer, int fenceFd) {
191 Surface* c = getSelf(window);
192 return c->cancelBuffer(buffer, fenceFd);
193}
194
195int Surface::hook_queueBuffer(ANativeWindow* window,
196 ANativeWindowBuffer* buffer, int fenceFd) {
197 Surface* c = getSelf(window);
198 return c->queueBuffer(buffer, fenceFd);
199}
200
201int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
202 ANativeWindowBuffer** buffer) {
203 Surface* c = getSelf(window);
204 ANativeWindowBuffer* buf;
205 int fenceFd = -1;
206 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600207 if (result != OK) {
208 return result;
209 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800210 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700211 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800212 if (waitResult != OK) {
213 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
214 waitResult);
215 c->cancelBuffer(buf, -1);
216 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700217 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800218 *buffer = buf;
219 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700220}
221
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800222int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
223 ANativeWindowBuffer* buffer) {
224 Surface* c = getSelf(window);
225 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700226}
227
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800228int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
229 ANativeWindowBuffer* buffer) {
230 Surface* c = getSelf(window);
231 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700232}
233
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800234int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
235 ANativeWindowBuffer* buffer) {
236 Surface* c = getSelf(window);
237 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700238}
Mathias Agopian62185b72009-04-16 16:19:50 -0700239
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800240int Surface::hook_query(const ANativeWindow* window,
241 int what, int* value) {
242 const Surface* c = getSelf(window);
243 return c->query(what, value);
244}
245
246int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
247 va_list args;
248 va_start(args, operation);
249 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700250 int result = c->perform(operation, args);
251 va_end(args);
252 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800253}
254
255int Surface::setSwapInterval(int interval) {
256 ATRACE_CALL();
257 // EGL specification states:
258 // interval is silently clamped to minimum and maximum implementation
259 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800260
261 if (interval < minSwapInterval)
262 interval = minSwapInterval;
263
264 if (interval > maxSwapInterval)
265 interval = maxSwapInterval;
266
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700267 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700268 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800269
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700270 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800271}
272
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700273int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800274 ATRACE_CALL();
275 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800276
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800277 uint32_t reqWidth;
278 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800279 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800280 uint32_t reqUsage;
281
282 {
283 Mutex::Autolock lock(mMutex);
284
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800285 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
286 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800287
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800288 reqFormat = mReqFormat;
289 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800290
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700291 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800292 BufferItem::INVALID_BUFFER_SLOT) {
293 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
294 if (gbuf != NULL) {
295 *buffer = gbuf.get();
296 *fenceFd = -1;
297 return OK;
298 }
299 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800300 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
301
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800302 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800303 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700304 nsecs_t now = systemTime();
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700305 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800306 reqWidth, reqHeight, reqFormat, reqUsage);
Dan Stoza70ccba52016-07-01 14:00:40 -0700307 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800308
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800309 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700310 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
311 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
312 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800313 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700314 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800315
316 Mutex::Autolock lock(mMutex);
317
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800318 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700319
320 // this should never happen
321 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
322
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800323 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
324 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700325 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700326
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800327 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
328 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
329 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700330 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700331 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800332 return result;
333 }
334 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700335
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800336 if (fence->isValid()) {
337 *fenceFd = fence->dup();
338 if (*fenceFd == -1) {
339 ALOGE("dequeueBuffer: error duping fence: %d", errno);
340 // dup() should never fail; something is badly wrong. Soldier on
341 // and hope for the best; the worst that should happen is some
342 // visible corruption that lasts until the next frame.
343 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700344 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800345 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700346 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800347
348 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800349
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700350 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800351 mSharedBufferSlot = buf;
352 mSharedBufferHasBeenQueued = false;
353 } else if (mSharedBufferSlot == buf) {
354 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
355 mSharedBufferHasBeenQueued = false;
356 }
357
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800358 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700359}
360
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800361int Surface::cancelBuffer(android_native_buffer_t* buffer,
362 int fenceFd) {
363 ATRACE_CALL();
364 ALOGV("Surface::cancelBuffer");
365 Mutex::Autolock lock(mMutex);
366 int i = getSlotFromBufferLocked(buffer);
367 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900368 if (fenceFd >= 0) {
369 close(fenceFd);
370 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800371 return i;
372 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800373 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
374 if (fenceFd >= 0) {
375 close(fenceFd);
376 }
377 return OK;
378 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800379 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
380 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800381
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700382 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800383 mSharedBufferHasBeenQueued = true;
384 }
385
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800386 return OK;
387}
388
389int Surface::getSlotFromBufferLocked(
390 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800391 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
392 if (mSlots[i].buffer != NULL &&
393 mSlots[i].buffer->handle == buffer->handle) {
394 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700395 }
396 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800397 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
398 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700399}
400
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800401int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800402 ALOGV("Surface::lockBuffer");
403 Mutex::Autolock lock(mMutex);
404 return OK;
405}
Mathias Agopian631f3582010-05-25 17:51:34 -0700406
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800407int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
408 ATRACE_CALL();
409 ALOGV("Surface::queueBuffer");
410 Mutex::Autolock lock(mMutex);
411 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700412 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800413
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800414 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700415 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700416 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700417 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross6c5a17d2016-09-27 14:12:48 -0700418 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800419 } else {
420 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700421 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800422 int i = getSlotFromBufferLocked(buffer);
423 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900424 if (fenceFd >= 0) {
425 close(fenceFd);
426 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800427 return i;
428 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800429 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
430 if (fenceFd >= 0) {
431 close(fenceFd);
432 }
433 return OK;
434 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800437 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700438 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800439 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800441 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
442 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700443 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800444 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700445 fence, mStickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700446
Dan Stozac62acbd2015-04-21 16:42:49 -0700447 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700448 input.setSurfaceDamage(Region::INVALID_REGION);
449 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700450 // Here we do two things:
451 // 1) The surface damage was specified using the OpenGL ES convention of
452 // the origin being in the bottom-left corner. Here we flip to the
453 // convention that the rest of the system uses (top-left corner) by
454 // subtracting all top/bottom coordinates from the buffer height.
455 // 2) If the buffer is coming in rotated (for example, because the EGL
456 // implementation is reacting to the transform hint coming back from
457 // SurfaceFlinger), the surface damage needs to be rotated the
458 // opposite direction, since it was generated assuming an unrotated
459 // buffer (the app doesn't know that the EGL implementation is
460 // reacting to the transform hint behind its back). The
461 // transformations in the switch statement below apply those
462 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
463
464 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700465 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700466 bool rotated90 = (mTransform ^ mStickyTransform) &
467 NATIVE_WINDOW_TRANSFORM_ROT_90;
468 if (rotated90) {
469 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700470 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700471
Dan Stoza5065a552015-03-17 16:23:42 -0700472 Region flippedRegion;
473 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700474 int left = rect.left;
475 int right = rect.right;
476 int top = height - rect.bottom; // Flip from OpenGL convention
477 int bottom = height - rect.top; // Flip from OpenGL convention
478 switch (mTransform ^ mStickyTransform) {
479 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
480 // Rotate 270 degrees
481 Rect flippedRect{top, width - right, bottom, width - left};
482 flippedRegion.orSelf(flippedRect);
483 break;
484 }
485 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
486 // Rotate 180 degrees
487 Rect flippedRect{width - right, height - bottom,
488 width - left, height - top};
489 flippedRegion.orSelf(flippedRect);
490 break;
491 }
492 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
493 // Rotate 90 degrees
494 Rect flippedRect{height - bottom, left,
495 height - top, right};
496 flippedRegion.orSelf(flippedRect);
497 break;
498 }
499 default: {
500 Rect flippedRect{left, top, right, bottom};
501 flippedRegion.orSelf(flippedRect);
502 break;
503 }
504 }
Dan Stoza5065a552015-03-17 16:23:42 -0700505 }
506
507 input.setSurfaceDamage(flippedRegion);
508 }
509
Dan Stoza70ccba52016-07-01 14:00:40 -0700510 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800511 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700512 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800513 if (err != OK) {
514 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
515 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800516
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800517 uint32_t numPendingBuffers = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -0700518 uint32_t hint = 0;
519 output.deflate(&mDefaultWidth, &mDefaultHeight, &hint,
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700520 &numPendingBuffers, &mNextFrameNumber);
tedbo1e7fa9e2011-06-22 15:52:53 -0700521
Ruben Brunk1681d952014-06-27 15:51:55 -0700522 // Disable transform hint if sticky transform is set.
523 if (mStickyTransform == 0) {
524 mTransformHint = hint;
525 }
526
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800527 mConsumerRunningBehind = (numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700528
Dan Stozac62acbd2015-04-21 16:42:49 -0700529 if (!mConnectedToCpu) {
530 // Clear surface damage back to full-buffer
531 mDirtyRegion = Region::INVALID_REGION;
532 }
Dan Stoza5065a552015-03-17 16:23:42 -0700533
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700534 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800535 mSharedBufferHasBeenQueued = true;
536 }
537
Robert Carr9f31e292016-04-11 11:15:32 -0700538 mQueueBufferCondition.broadcast();
539
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800540 return err;
541}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700542
Mathias Agopiana67932f2011-04-20 14:20:59 -0700543int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800544 ATRACE_CALL();
545 ALOGV("Surface::query");
546 { // scope for the lock
547 Mutex::Autolock lock(mMutex);
548 switch (what) {
549 case NATIVE_WINDOW_FORMAT:
550 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800551 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800552 return NO_ERROR;
553 }
554 break;
555 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
556 sp<ISurfaceComposer> composer(
557 ComposerService::getComposerService());
558 if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) {
559 *value = 1;
560 } else {
561 *value = 0;
562 }
563 return NO_ERROR;
564 }
565 case NATIVE_WINDOW_CONCRETE_TYPE:
566 *value = NATIVE_WINDOW_SURFACE;
567 return NO_ERROR;
568 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800569 *value = static_cast<int>(
570 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800571 return NO_ERROR;
572 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800573 *value = static_cast<int>(
574 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800575 return NO_ERROR;
576 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800577 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800578 return NO_ERROR;
579 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
580 status_t err = NO_ERROR;
581 if (!mConsumerRunningBehind) {
582 *value = 0;
583 } else {
584 err = mGraphicBufferProducer->query(what, value);
585 if (err == NO_ERROR) {
586 mConsumerRunningBehind = *value;
587 }
588 }
589 return err;
590 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700591 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
592 int64_t durationUs = mLastDequeueDuration / 1000;
593 *value = durationUs > std::numeric_limits<int>::max() ?
594 std::numeric_limits<int>::max() :
595 static_cast<int>(durationUs);
596 return NO_ERROR;
597 }
598 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
599 int64_t durationUs = mLastQueueDuration / 1000;
600 *value = durationUs > std::numeric_limits<int>::max() ?
601 std::numeric_limits<int>::max() :
602 static_cast<int>(durationUs);
603 return NO_ERROR;
604 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800605 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700606 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800607 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800608}
609
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800610int Surface::perform(int operation, va_list args)
611{
612 int res = NO_ERROR;
613 switch (operation) {
614 case NATIVE_WINDOW_CONNECT:
615 // deprecated. must return NO_ERROR.
616 break;
617 case NATIVE_WINDOW_DISCONNECT:
618 // deprecated. must return NO_ERROR.
619 break;
620 case NATIVE_WINDOW_SET_USAGE:
621 res = dispatchSetUsage(args);
622 break;
623 case NATIVE_WINDOW_SET_CROP:
624 res = dispatchSetCrop(args);
625 break;
626 case NATIVE_WINDOW_SET_BUFFER_COUNT:
627 res = dispatchSetBufferCount(args);
628 break;
629 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
630 res = dispatchSetBuffersGeometry(args);
631 break;
632 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
633 res = dispatchSetBuffersTransform(args);
634 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700635 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
636 res = dispatchSetBuffersStickyTransform(args);
637 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800638 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
639 res = dispatchSetBuffersTimestamp(args);
640 break;
641 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
642 res = dispatchSetBuffersDimensions(args);
643 break;
644 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
645 res = dispatchSetBuffersUserDimensions(args);
646 break;
647 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
648 res = dispatchSetBuffersFormat(args);
649 break;
650 case NATIVE_WINDOW_LOCK:
651 res = dispatchLock(args);
652 break;
653 case NATIVE_WINDOW_UNLOCK_AND_POST:
654 res = dispatchUnlockAndPost(args);
655 break;
656 case NATIVE_WINDOW_SET_SCALING_MODE:
657 res = dispatchSetScalingMode(args);
658 break;
659 case NATIVE_WINDOW_API_CONNECT:
660 res = dispatchConnect(args);
661 break;
662 case NATIVE_WINDOW_API_DISCONNECT:
663 res = dispatchDisconnect(args);
664 break;
Rachad7cb0d392014-07-29 17:53:53 -0700665 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
666 res = dispatchSetSidebandStream(args);
667 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800668 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
669 res = dispatchSetBuffersDataSpace(args);
670 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700671 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
672 res = dispatchSetSurfaceDamage(args);
673 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700674 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
675 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700676 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800677 case NATIVE_WINDOW_SET_AUTO_REFRESH:
678 res = dispatchSetAutoRefresh(args);
679 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800680 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
681 res = dispatchGetFrameTimestamps(args);
682 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800683 default:
684 res = NAME_NOT_FOUND;
685 break;
686 }
687 return res;
688}
Mathias Agopiana138f892010-05-21 17:24:35 -0700689
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800690int Surface::dispatchConnect(va_list args) {
691 int api = va_arg(args, int);
692 return connect(api);
693}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800694
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800695int Surface::dispatchDisconnect(va_list args) {
696 int api = va_arg(args, int);
697 return disconnect(api);
698}
699
700int Surface::dispatchSetUsage(va_list args) {
701 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800702 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800703}
704
705int Surface::dispatchSetCrop(va_list args) {
706 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
707 return setCrop(reinterpret_cast<Rect const*>(rect));
708}
709
710int Surface::dispatchSetBufferCount(va_list args) {
711 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800712 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800713}
714
715int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800716 uint32_t width = va_arg(args, uint32_t);
717 uint32_t height = va_arg(args, uint32_t);
718 PixelFormat format = va_arg(args, PixelFormat);
719 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800720 if (err != 0) {
721 return err;
722 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800723 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800724}
725
726int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800727 uint32_t width = va_arg(args, uint32_t);
728 uint32_t height = va_arg(args, uint32_t);
729 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800730}
731
732int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800733 uint32_t width = va_arg(args, uint32_t);
734 uint32_t height = va_arg(args, uint32_t);
735 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800736}
737
738int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800739 PixelFormat format = va_arg(args, PixelFormat);
740 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800741}
742
743int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800744 int mode = va_arg(args, int);
745 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800746}
747
748int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800749 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800750 return setBuffersTransform(transform);
751}
752
Ruben Brunk1681d952014-06-27 15:51:55 -0700753int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800754 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700755 return setBuffersStickyTransform(transform);
756}
757
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800758int Surface::dispatchSetBuffersTimestamp(va_list args) {
759 int64_t timestamp = va_arg(args, int64_t);
760 return setBuffersTimestamp(timestamp);
761}
762
763int Surface::dispatchLock(va_list args) {
764 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
765 ARect* inOutDirtyBounds = va_arg(args, ARect*);
766 return lock(outBuffer, inOutDirtyBounds);
767}
768
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800769int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800770 return unlockAndPost();
771}
772
Rachad7cb0d392014-07-29 17:53:53 -0700773int Surface::dispatchSetSidebandStream(va_list args) {
774 native_handle_t* sH = va_arg(args, native_handle_t*);
775 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
776 setSidebandStream(sidebandHandle);
777 return OK;
778}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800779
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800780int Surface::dispatchSetBuffersDataSpace(va_list args) {
781 android_dataspace dataspace =
782 static_cast<android_dataspace>(va_arg(args, int));
783 return setBuffersDataSpace(dataspace);
784}
785
Dan Stoza5065a552015-03-17 16:23:42 -0700786int Surface::dispatchSetSurfaceDamage(va_list args) {
787 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
788 size_t numRects = va_arg(args, size_t);
789 setSurfaceDamage(rects, numRects);
790 return NO_ERROR;
791}
792
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700793int Surface::dispatchSetSharedBufferMode(va_list args) {
794 bool sharedBufferMode = va_arg(args, int);
795 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800796}
797
798int Surface::dispatchSetAutoRefresh(va_list args) {
799 bool autoRefresh = va_arg(args, int);
800 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700801}
802
Pablo Ceballosce796e72016-02-04 19:10:51 -0800803int Surface::dispatchGetFrameTimestamps(va_list args) {
804 uint32_t framesAgo = va_arg(args, uint32_t);
805 nsecs_t* outPostedTime = va_arg(args, int64_t*);
806 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
807 nsecs_t* outRefreshStartTime = va_arg(args, int64_t*);
808 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
809 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
810 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
811 bool ret = getFrameTimestamps(getNextFrameNumber() - 1 - framesAgo,
812 outPostedTime, outAcquireTime, outRefreshStartTime,
813 outGlCompositionDoneTime, outDisplayRetireTime, outReleaseTime);
814 return ret ? NO_ERROR : BAD_VALUE;
815}
816
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800817int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -0800818 static sp<IProducerListener> listener = new DummyProducerListener();
819 return connect(api, listener);
820}
821
822int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800823 ATRACE_CALL();
824 ALOGV("Surface::connect");
825 Mutex::Autolock lock(mMutex);
826 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700827 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800828 if (err == NO_ERROR) {
829 uint32_t numPendingBuffers = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -0700830 uint32_t hint = 0;
831 output.deflate(&mDefaultWidth, &mDefaultHeight, &hint,
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700832 &numPendingBuffers, &mNextFrameNumber);
Ruben Brunk1681d952014-06-27 15:51:55 -0700833
834 // Disable transform hint if sticky transform is set.
835 if (mStickyTransform == 0) {
836 mTransformHint = hint;
837 }
838
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800839 mConsumerRunningBehind = (numPendingBuffers >= 2);
840 }
841 if (!err && api == NATIVE_WINDOW_API_CPU) {
842 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -0700843 // Clear the dirty region in case we're switching from a non-CPU API
844 mDirtyRegion.clear();
845 } else if (!err) {
846 // Initialize the dirty region for tracking surface damage
847 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800848 }
Dan Stoza5065a552015-03-17 16:23:42 -0700849
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800850 return err;
851}
852
Mathias Agopian365857d2013-09-11 19:35:45 -0700853
Robert Carr97b9c862016-09-08 13:54:35 -0700854int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800855 ATRACE_CALL();
856 ALOGV("Surface::disconnect");
857 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800858 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
859 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800860 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -0700861 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800862 if (!err) {
863 mReqFormat = 0;
864 mReqWidth = 0;
865 mReqHeight = 0;
866 mReqUsage = 0;
867 mCrop.clear();
868 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
869 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -0700870 mStickyTransform = 0;
871
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800872 if (api == NATIVE_WINDOW_API_CPU) {
873 mConnectedToCpu = false;
874 }
875 }
876 return err;
877}
878
Dan Stozad9c49712015-04-27 11:06:01 -0700879int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -0700880 sp<Fence>* outFence) {
881 ATRACE_CALL();
882 ALOGV("Surface::detachNextBuffer");
883
884 if (outBuffer == NULL || outFence == NULL) {
885 return BAD_VALUE;
886 }
887
888 Mutex::Autolock lock(mMutex);
889
890 sp<GraphicBuffer> buffer(NULL);
891 sp<Fence> fence(NULL);
892 status_t result = mGraphicBufferProducer->detachNextBuffer(
893 &buffer, &fence);
894 if (result != NO_ERROR) {
895 return result;
896 }
897
Dan Stozad9c49712015-04-27 11:06:01 -0700898 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -0700899 if (fence != NULL && fence->isValid()) {
900 *outFence = fence;
901 } else {
902 *outFence = Fence::NO_FENCE;
903 }
904
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800905 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
906 if (mSlots[i].buffer != NULL &&
907 mSlots[i].buffer->handle == buffer->handle) {
908 mSlots[i].buffer = NULL;
909 }
910 }
911
Dan Stoza231832e2015-03-11 11:55:01 -0700912 return NO_ERROR;
913}
914
915int Surface::attachBuffer(ANativeWindowBuffer* buffer)
916{
917 ATRACE_CALL();
918 ALOGV("Surface::attachBuffer");
919
920 Mutex::Autolock lock(mMutex);
921
922 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -0700923 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
924 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -0700925 int32_t attachedSlot = -1;
926 status_t result = mGraphicBufferProducer->attachBuffer(
927 &attachedSlot, graphicBuffer);
928 if (result != NO_ERROR) {
929 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -0700930 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -0700931 return result;
932 }
933 mSlots[attachedSlot].buffer = graphicBuffer;
934
935 return NO_ERROR;
936}
937
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800938int Surface::setUsage(uint32_t reqUsage)
939{
940 ALOGV("Surface::setUsage");
941 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800942 if (reqUsage != mReqUsage) {
943 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
944 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800945 mReqUsage = reqUsage;
946 return OK;
947}
948
949int Surface::setCrop(Rect const* rect)
950{
951 ATRACE_CALL();
952
Pablo Ceballos60d69222015-08-07 14:47:20 -0700953 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800954 if (rect == NULL || rect->isEmpty()) {
955 realRect.clear();
956 } else {
957 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800958 }
959
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800960 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
961 realRect.left, realRect.top, realRect.right, realRect.bottom);
962
963 Mutex::Autolock lock(mMutex);
964 mCrop = realRect;
965 return NO_ERROR;
966}
967
968int Surface::setBufferCount(int bufferCount)
969{
970 ATRACE_CALL();
971 ALOGV("Surface::setBufferCount");
972 Mutex::Autolock lock(mMutex);
973
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700974 status_t err = NO_ERROR;
975 if (bufferCount == 0) {
976 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
977 } else {
978 int minUndequeuedBuffers = 0;
979 err = mGraphicBufferProducer->query(
980 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
981 if (err == NO_ERROR) {
982 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
983 bufferCount - minUndequeuedBuffers);
984 }
985 }
Mathias Agopian90147262010-01-22 11:47:55 -0800986
Pablo Ceballose5b755a2015-08-13 16:18:19 -0700987 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
988 bufferCount, strerror(-err));
989
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700990 return err;
991}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700992
Pablo Ceballosfa455352015-08-12 17:47:47 -0700993int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
994 ATRACE_CALL();
995 ALOGV("Surface::setMaxDequeuedBufferCount");
996 Mutex::Autolock lock(mMutex);
997
998 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
999 maxDequeuedBuffers);
1000 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1001 "returned %s", maxDequeuedBuffers, strerror(-err));
1002
Pablo Ceballosfa455352015-08-12 17:47:47 -07001003 return err;
1004}
1005
1006int Surface::setAsyncMode(bool async) {
1007 ATRACE_CALL();
1008 ALOGV("Surface::setAsyncMode");
1009 Mutex::Autolock lock(mMutex);
1010
1011 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1012 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1013 async, strerror(-err));
1014
Pablo Ceballosfa455352015-08-12 17:47:47 -07001015 return err;
1016}
1017
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001018int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001019 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001020 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001021 Mutex::Autolock lock(mMutex);
1022
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001023 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1024 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001025 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001026 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001027 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001028 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1029 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001030
1031 return err;
1032}
1033
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001034int Surface::setAutoRefresh(bool autoRefresh) {
1035 ATRACE_CALL();
1036 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1037 Mutex::Autolock lock(mMutex);
1038
1039 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1040 if (err == NO_ERROR) {
1041 mAutoRefresh = autoRefresh;
1042 }
1043 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1044 autoRefresh, strerror(-err));
1045 return err;
1046}
1047
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001048int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001049{
1050 ATRACE_CALL();
1051 ALOGV("Surface::setBuffersDimensions");
1052
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001053 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001054 return BAD_VALUE;
1055
1056 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001057 if (width != mReqWidth || height != mReqHeight) {
1058 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1059 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001060 mReqWidth = width;
1061 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001062 return NO_ERROR;
1063}
1064
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001065int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001066{
1067 ATRACE_CALL();
1068 ALOGV("Surface::setBuffersUserDimensions");
1069
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001070 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001071 return BAD_VALUE;
1072
1073 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001074 if (width != mUserWidth || height != mUserHeight) {
1075 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1076 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001077 mUserWidth = width;
1078 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001079 return NO_ERROR;
1080}
1081
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001082int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001083{
1084 ALOGV("Surface::setBuffersFormat");
1085
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001086 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001087 if (format != mReqFormat) {
1088 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1089 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001090 mReqFormat = format;
1091 return NO_ERROR;
1092}
1093
1094int Surface::setScalingMode(int mode)
1095{
1096 ATRACE_CALL();
1097 ALOGV("Surface::setScalingMode(%d)", mode);
1098
1099 switch (mode) {
1100 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1101 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1102 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001103 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001104 break;
1105 default:
1106 ALOGE("unknown scaling mode: %d", mode);
1107 return BAD_VALUE;
1108 }
1109
1110 Mutex::Autolock lock(mMutex);
1111 mScalingMode = mode;
1112 return NO_ERROR;
1113}
1114
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001115int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001116{
1117 ATRACE_CALL();
1118 ALOGV("Surface::setBuffersTransform");
1119 Mutex::Autolock lock(mMutex);
1120 mTransform = transform;
1121 return NO_ERROR;
1122}
1123
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001124int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001125{
1126 ATRACE_CALL();
1127 ALOGV("Surface::setBuffersStickyTransform");
1128 Mutex::Autolock lock(mMutex);
1129 mStickyTransform = transform;
1130 return NO_ERROR;
1131}
1132
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001133int Surface::setBuffersTimestamp(int64_t timestamp)
1134{
1135 ALOGV("Surface::setBuffersTimestamp");
1136 Mutex::Autolock lock(mMutex);
1137 mTimestamp = timestamp;
1138 return NO_ERROR;
1139}
1140
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001141int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1142{
1143 ALOGV("Surface::setBuffersDataSpace");
1144 Mutex::Autolock lock(mMutex);
1145 mDataSpace = dataSpace;
1146 return NO_ERROR;
1147}
1148
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001149void Surface::freeAllBuffers() {
1150 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1151 mSlots[i].buffer = 0;
1152 }
1153}
1154
Dan Stoza5065a552015-03-17 16:23:42 -07001155void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1156 ATRACE_CALL();
1157 ALOGV("Surface::setSurfaceDamage");
1158 Mutex::Autolock lock(mMutex);
1159
Dan Stozac62acbd2015-04-21 16:42:49 -07001160 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001161 mDirtyRegion = Region::INVALID_REGION;
1162 return;
1163 }
1164
1165 mDirtyRegion.clear();
1166 for (size_t r = 0; r < numRects; ++r) {
1167 // We intentionally flip top and bottom here, since because they're
1168 // specified with a bottom-left origin, top > bottom, which fails
1169 // validation in the Region class. We will fix this up when we flip to a
1170 // top-left origin in queueBuffer.
1171 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1172 mDirtyRegion.orSelf(rect);
1173 }
1174}
1175
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001176// ----------------------------------------------------------------------
1177// the lock/unlock APIs must be used from the same thread
1178
1179static status_t copyBlt(
1180 const sp<GraphicBuffer>& dst,
1181 const sp<GraphicBuffer>& src,
1182 const Region& reg)
1183{
1184 // src and dst with, height and format must be identical. no verification
1185 // is done here.
1186 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001187 uint8_t* src_bits = NULL;
1188 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1189 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001190 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1191
1192 uint8_t* dst_bits = NULL;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001193 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1194 reinterpret_cast<void**>(&dst_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001195 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
1196
1197 Region::const_iterator head(reg.begin());
1198 Region::const_iterator tail(reg.end());
1199 if (head != tail && src_bits && dst_bits) {
1200 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001201 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1202 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001203
1204 while (head != tail) {
1205 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001206 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001207 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001208 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1209 uint8_t const * s = src_bits +
1210 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1211 uint8_t * d = dst_bits +
1212 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001213 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001214 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001215 h = 1;
1216 }
1217 do {
1218 memcpy(d, s, size);
1219 d += dbpr;
1220 s += sbpr;
1221 } while (--h > 0);
1222 }
1223 }
1224
1225 if (src_bits)
1226 src->unlock();
1227
1228 if (dst_bits)
1229 dst->unlock();
1230
1231 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232}
1233
Mathias Agopiana138f892010-05-21 17:24:35 -07001234// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001235
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001236status_t Surface::lock(
1237 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1238{
1239 if (mLockedBuffer != 0) {
1240 ALOGE("Surface::lock failed, already locked");
1241 return INVALID_OPERATION;
1242 }
1243
1244 if (!mConnectedToCpu) {
1245 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1246 if (err) {
1247 return err;
1248 }
1249 // we're intending to do software rendering from this point
1250 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1251 }
1252
1253 ANativeWindowBuffer* out;
1254 int fenceFd = -1;
1255 status_t err = dequeueBuffer(&out, &fenceFd);
1256 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1257 if (err == NO_ERROR) {
1258 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001259 const Rect bounds(backBuffer->width, backBuffer->height);
1260
1261 Region newDirtyRegion;
1262 if (inOutDirtyBounds) {
1263 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1264 newDirtyRegion.andSelf(bounds);
1265 } else {
1266 newDirtyRegion.set(bounds);
1267 }
1268
1269 // figure out if we can copy the frontbuffer back
1270 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1271 const bool canCopyBack = (frontBuffer != 0 &&
1272 backBuffer->width == frontBuffer->width &&
1273 backBuffer->height == frontBuffer->height &&
1274 backBuffer->format == frontBuffer->format);
1275
1276 if (canCopyBack) {
1277 // copy the area that is invalid and not repainted this round
1278 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001279 if (!copyback.isEmpty()) {
1280 if (fenceFd >= 0) {
1281 sync_wait(fenceFd, -1);
1282 close(fenceFd);
1283 fenceFd = -1;
1284 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001285 copyBlt(backBuffer, frontBuffer, copyback);
Francis Hartdc10f842014-12-01 16:04:49 +02001286 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001287 } else {
1288 // if we can't copy-back anything, modify the user's dirty
1289 // region to make sure they redraw the whole buffer
1290 newDirtyRegion.set(bounds);
1291 mDirtyRegion.clear();
1292 Mutex::Autolock lock(mMutex);
1293 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1294 mSlots[i].dirtyRegion.clear();
1295 }
1296 }
1297
1298
1299 { // scope for the lock
1300 Mutex::Autolock lock(mMutex);
1301 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1302 if (backBufferSlot >= 0) {
1303 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1304 mDirtyRegion.subtract(dirtyRegion);
1305 dirtyRegion = newDirtyRegion;
1306 }
1307 }
1308
1309 mDirtyRegion.orSelf(newDirtyRegion);
1310 if (inOutDirtyBounds) {
1311 *inOutDirtyBounds = newDirtyRegion.getBounds();
1312 }
1313
1314 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001315 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001316 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001317 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001318
1319 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1320 backBuffer->handle);
1321
1322 if (res != 0) {
1323 err = INVALID_OPERATION;
1324 } else {
1325 mLockedBuffer = backBuffer;
1326 outBuffer->width = backBuffer->width;
1327 outBuffer->height = backBuffer->height;
1328 outBuffer->stride = backBuffer->stride;
1329 outBuffer->format = backBuffer->format;
1330 outBuffer->bits = vaddr;
1331 }
1332 }
1333 return err;
1334}
1335
1336status_t Surface::unlockAndPost()
1337{
1338 if (mLockedBuffer == 0) {
1339 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1340 return INVALID_OPERATION;
1341 }
1342
Francis Hart8f396012014-04-01 15:30:53 +03001343 int fd = -1;
1344 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001345 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1346
Francis Hart8f396012014-04-01 15:30:53 +03001347 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001348 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1349 mLockedBuffer->handle, strerror(-err));
1350
1351 mPostedBuffer = mLockedBuffer;
1352 mLockedBuffer = 0;
1353 return err;
1354}
1355
Robert Carr9f31e292016-04-11 11:15:32 -07001356bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1357 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001358 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001359 return true;
1360 }
1361 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1362}
1363
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001364status_t Surface::getUniqueId(uint64_t* outId) const {
1365 Mutex::Autolock lock(mMutex);
1366 return mGraphicBufferProducer->getUniqueId(outId);
1367}
1368
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001369namespace view {
1370
1371status_t Surface::writeToParcel(Parcel* parcel) const {
1372 return writeToParcel(parcel, false);
1373}
1374
1375status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const {
1376 if (parcel == nullptr) return BAD_VALUE;
1377
1378 status_t res = OK;
1379
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001380 if (!nameAlreadyWritten) {
1381 res = parcel->writeString16(name);
1382 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001383
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001384 /* isSingleBuffered defaults to no */
1385 res = parcel->writeInt32(0);
1386 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001387 }
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001388
1389 res = parcel->writeStrongBinder(
1390 IGraphicBufferProducer::asBinder(graphicBufferProducer));
1391
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001392 return res;
1393}
1394
1395status_t Surface::readFromParcel(const Parcel* parcel) {
1396 return readFromParcel(parcel, false);
1397}
1398
1399status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) {
1400 if (parcel == nullptr) return BAD_VALUE;
1401
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001402 status_t res = OK;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001403 if (!nameAlreadyRead) {
1404 name = readMaybeEmptyString16(parcel);
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001405 // Discard this for now
1406 int isSingleBuffered;
1407 res = parcel->readInt32(&isSingleBuffered);
1408 if (res != OK) {
1409 return res;
1410 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001411 }
1412
1413 sp<IBinder> binder;
1414
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001415 res = parcel->readStrongBinder(&binder);
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001416 if (res != OK) return res;
1417
1418 graphicBufferProducer = interface_cast<IGraphicBufferProducer>(binder);
1419
1420 return OK;
1421}
1422
1423String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
1424 size_t len;
1425 const char16_t* str = parcel->readString16Inplace(&len);
1426 if (str != nullptr) {
1427 return String16(str, len);
1428 } else {
1429 return String16();
1430 }
1431}
1432
1433} // namespace view
1434
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435}; // namespace android