blob: 1e79e06d8be505af73d160af8e87c15825347c51 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002 * Copyright (C) 2010 The Android Open Source Project
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Surface"
Mathias Agopiane3c697f2013-02-14 17:11:02 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Mathias Agopianb0e76f42012-03-23 14:15:44 -070021#include <android/native_window.h>
22
Mathias Agopiane3c697f2013-02-14 17:11:02 -080023#include <binder/Parcel.h>
24
Mathias Agopian9cce3252010-02-09 17:46:37 -080025#include <utils/Log.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080026#include <utils/Trace.h>
Rachad7cb0d392014-07-29 17:53:53 -070027#include <utils/NativeHandle.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080028
Mathias Agopiane3c697f2013-02-14 17:11:02 -080029#include <ui/Fence.h>
Dan Stoza5065a552015-03-17 16:23:42 -070030#include <ui/Region.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070031
Dan Stozaf0eaf252014-03-21 13:05:51 -070032#include <gui/IProducerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080033#include <gui/ISurfaceComposer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080034#include <gui/SurfaceComposerClient.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080035#include <gui/GLConsumer.h>
36#include <gui/Surface.h>
37
38#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040namespace android {
41
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070043 const sp<IGraphicBufferProducer>& bufferProducer,
44 bool controlledByApp)
Dan Stoza812ed062015-06-02 15:45:22 -070045 : mGraphicBufferProducer(bufferProducer),
Pablo Ceballos60d69222015-08-07 14:47:20 -070046 mCrop(Rect::EMPTY_RECT),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080047 mGenerationNumber(0),
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070048 mSharedBufferMode(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080049 mAutoRefresh(false),
50 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
Pablo Ceballosbc8c1922016-07-01 14:15:41 -070051 mSharedBufferHasBeenQueued(false),
Brian Anderson069b3652016-07-22 10:32:47 -070052 mNextFrameNumber(1),
53 mQueriedSupportedTimestamps(false),
54 mFrameTimestampsSupportsPresent(false),
Brian Anderson7c3ba8a2016-07-25 12:48:08 -070055 mFrameTimestampsSupportsRetire(false),
56 mEnableFrameTimestamps(false)
Mathias Agopian62185b72009-04-16 16:19:50 -070057{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080058 // Initialize the ANativeWindow function pointers.
59 ANativeWindow::setSwapInterval = hook_setSwapInterval;
60 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
61 ANativeWindow::cancelBuffer = hook_cancelBuffer;
62 ANativeWindow::queueBuffer = hook_queueBuffer;
63 ANativeWindow::query = hook_query;
64 ANativeWindow::perform = hook_perform;
65
66 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
67 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
68 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
69 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
70
71 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
72 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
73
74 mReqWidth = 0;
75 mReqHeight = 0;
76 mReqFormat = 0;
77 mReqUsage = 0;
78 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080079 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080080 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
81 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070082 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080083 mDefaultWidth = 0;
84 mDefaultHeight = 0;
85 mUserWidth = 0;
86 mUserHeight = 0;
87 mTransformHint = 0;
88 mConsumerRunningBehind = false;
89 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070090 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070091 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070092}
93
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070094Surface::~Surface() {
95 if (mConnectedToCpu) {
96 Surface::disconnect(NATIVE_WINDOW_API_CPU);
97 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -080098}
99
100sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
101 return mGraphicBufferProducer;
102}
103
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900104void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
105 mGraphicBufferProducer->setSidebandStream(stream);
106}
107
Dan Stoza29a3e902014-06-20 13:13:57 -0700108void Surface::allocateBuffers() {
109 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
110 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700111 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
112 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700113}
114
Dan Stoza812ed062015-06-02 15:45:22 -0700115status_t Surface::setGenerationNumber(uint32_t generation) {
116 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
117 if (result == NO_ERROR) {
118 mGenerationNumber = generation;
119 }
120 return result;
121}
122
Dan Stoza7dde5992015-05-22 09:51:44 -0700123uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700124 Mutex::Autolock lock(mMutex);
125 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700126}
127
Dan Stozac6f30bd2015-06-08 09:32:50 -0700128String8 Surface::getConsumerName() const {
129 return mGraphicBufferProducer->getConsumerName();
130}
131
Dan Stoza127fc632015-06-30 13:43:32 -0700132status_t Surface::setDequeueTimeout(nsecs_t timeout) {
133 return mGraphicBufferProducer->setDequeueTimeout(timeout);
134}
135
Dan Stoza50101d02016-04-07 16:53:23 -0700136status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700137 sp<Fence>* outFence, float outTransformMatrix[16]) {
138 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
139 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700140}
141
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700142void Surface::enableFrameTimestamps(bool enable) {
143 Mutex::Autolock lock(mMutex);
144 mEnableFrameTimestamps = enable;
145}
146
Brian Anderson3d4039d2016-09-23 16:31:30 -0700147static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
148 if (dst != nullptr) {
149 *dst = Fence::isValidTimestamp(src) ? src : 0;
150 }
151}
152
153static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) {
154 if (dst != nullptr) {
155 nsecs_t signalTime = src->getSignalTime();
156 *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0;
157 }
158}
159
Brian Anderson069b3652016-07-22 10:32:47 -0700160status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700161 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
162 nsecs_t* outRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700163 nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime,
164 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800165 ATRACE_CALL();
166
Brian Anderson3890c392016-07-25 12:48:08 -0700167 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700168
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700169 if (!mEnableFrameTimestamps) {
170 return INVALID_OPERATION;
171 }
172
Brian Anderson3890c392016-07-25 12:48:08 -0700173 // Verify the requested timestamps are supported.
174 querySupportedTimestampsLocked();
175 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
176 return BAD_VALUE;
177 }
178 if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
179 return BAD_VALUE;
Brian Anderson069b3652016-07-22 10:32:47 -0700180 }
181
Brian Anderson3890c392016-07-25 12:48:08 -0700182 FrameEvents* events = mFrameEventHistory.getFrame(frameNumber);
Brian Anderson069b3652016-07-22 10:32:47 -0700183
Brian Anderson3890c392016-07-25 12:48:08 -0700184 // Update our cache of events if the requested events are not available.
185 if (events == nullptr ||
186 (outRequestedPresentTime && !events->hasRequestedPresentInfo()) ||
187 (outAcquireTime && !events->hasAcquireInfo()) ||
188 (outRefreshStartTime && !events->hasFirstRefreshStartInfo()) ||
189 (outGlCompositionDoneTime && !events->hasGpuCompositionDoneInfo()) ||
190 (outDisplayPresentTime && !events->hasDisplayPresentInfo()) ||
191 (outDisplayRetireTime && !events->hasDisplayRetireInfo()) ||
192 (outReleaseTime && !events->hasReleaseInfo())) {
193 FrameEventHistoryDelta delta;
194 mGraphicBufferProducer->getFrameTimestamps(&delta);
195 mFrameEventHistory.applyDelta(delta);
196 events = mFrameEventHistory.getFrame(frameNumber);
197 }
198
199 // A record for the requested frame does not exist.
200 if (events == nullptr) {
Brian Anderson069b3652016-07-22 10:32:47 -0700201 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800202 }
Brian Anderson069b3652016-07-22 10:32:47 -0700203
Brian Anderson3d4039d2016-09-23 16:31:30 -0700204 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
205 getFrameTimestamp(outRefreshStartTime, events->firstRefreshStartTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700206
Brian Anderson3d4039d2016-09-23 16:31:30 -0700207 getFrameTimestampFence(outAcquireTime, events->acquireFence);
208 getFrameTimestampFence(
209 outGlCompositionDoneTime, events->gpuCompositionDoneFence);
210 getFrameTimestampFence(
211 outDisplayPresentTime, events->displayPresentFence);
212 getFrameTimestampFence(outDisplayRetireTime, events->displayRetireFence);
213 getFrameTimestampFence(outReleaseTime, events->releaseFence);
Brian Anderson069b3652016-07-22 10:32:47 -0700214
215 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800216}
217
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800218int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
219 Surface* c = getSelf(window);
220 return c->setSwapInterval(interval);
221}
222
223int Surface::hook_dequeueBuffer(ANativeWindow* window,
224 ANativeWindowBuffer** buffer, int* fenceFd) {
225 Surface* c = getSelf(window);
226 return c->dequeueBuffer(buffer, fenceFd);
227}
228
229int Surface::hook_cancelBuffer(ANativeWindow* window,
230 ANativeWindowBuffer* buffer, int fenceFd) {
231 Surface* c = getSelf(window);
232 return c->cancelBuffer(buffer, fenceFd);
233}
234
235int Surface::hook_queueBuffer(ANativeWindow* window,
236 ANativeWindowBuffer* buffer, int fenceFd) {
237 Surface* c = getSelf(window);
238 return c->queueBuffer(buffer, fenceFd);
239}
240
241int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
242 ANativeWindowBuffer** buffer) {
243 Surface* c = getSelf(window);
244 ANativeWindowBuffer* buf;
245 int fenceFd = -1;
246 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600247 if (result != OK) {
248 return result;
249 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800250 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700251 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800252 if (waitResult != OK) {
253 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
254 waitResult);
255 c->cancelBuffer(buf, -1);
256 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700257 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800258 *buffer = buf;
259 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700260}
261
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800262int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
263 ANativeWindowBuffer* buffer) {
264 Surface* c = getSelf(window);
265 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700266}
267
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800268int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
269 ANativeWindowBuffer* buffer) {
270 Surface* c = getSelf(window);
271 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700272}
273
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800274int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
275 ANativeWindowBuffer* buffer) {
276 Surface* c = getSelf(window);
277 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700278}
Mathias Agopian62185b72009-04-16 16:19:50 -0700279
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800280int Surface::hook_query(const ANativeWindow* window,
281 int what, int* value) {
282 const Surface* c = getSelf(window);
283 return c->query(what, value);
284}
285
286int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
287 va_list args;
288 va_start(args, operation);
289 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700290 int result = c->perform(operation, args);
291 va_end(args);
292 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800293}
294
295int Surface::setSwapInterval(int interval) {
296 ATRACE_CALL();
297 // EGL specification states:
298 // interval is silently clamped to minimum and maximum implementation
299 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800300
301 if (interval < minSwapInterval)
302 interval = minSwapInterval;
303
304 if (interval > maxSwapInterval)
305 interval = maxSwapInterval;
306
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700307 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700308 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800309
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700310 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800311}
312
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700313int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800314 ATRACE_CALL();
315 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800316
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800317 uint32_t reqWidth;
318 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800319 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800320 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700321 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800322
323 {
324 Mutex::Autolock lock(mMutex);
325
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800326 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
327 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800328
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800329 reqFormat = mReqFormat;
330 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800331
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700332 enableFrameTimestamps = mEnableFrameTimestamps;
333
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700334 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800335 BufferItem::INVALID_BUFFER_SLOT) {
336 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
337 if (gbuf != NULL) {
338 *buffer = gbuf.get();
339 *fenceFd = -1;
340 return OK;
341 }
342 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800343 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
344
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800345 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800346 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700347 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700348
349 FrameEventHistoryDelta frameTimestamps;
350 FrameEventHistoryDelta* frameTimestampsOrNull =
351 enableFrameTimestamps ? &frameTimestamps : nullptr;
352
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700353 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700354 reqWidth, reqHeight, reqFormat, reqUsage, frameTimestampsOrNull);
Dan Stoza70ccba52016-07-01 14:00:40 -0700355 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800356
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800357 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700358 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
359 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
360 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800361 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700362 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800363
364 Mutex::Autolock lock(mMutex);
365
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800366 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700367
368 // this should never happen
369 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
370
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800371 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
372 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700373 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700374
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700375 if (enableFrameTimestamps) {
376 mFrameEventHistory.applyDelta(frameTimestamps);
377 }
378
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800379 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
380 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
381 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700382 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700383 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800384 return result;
385 }
386 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700387
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800388 if (fence->isValid()) {
389 *fenceFd = fence->dup();
390 if (*fenceFd == -1) {
391 ALOGE("dequeueBuffer: error duping fence: %d", errno);
392 // dup() should never fail; something is badly wrong. Soldier on
393 // and hope for the best; the worst that should happen is some
394 // visible corruption that lasts until the next frame.
395 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700396 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800397 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700398 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800399
400 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800401
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700402 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800403 mSharedBufferSlot = buf;
404 mSharedBufferHasBeenQueued = false;
405 } else if (mSharedBufferSlot == buf) {
406 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
407 mSharedBufferHasBeenQueued = false;
408 }
409
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800410 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700411}
412
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800413int Surface::cancelBuffer(android_native_buffer_t* buffer,
414 int fenceFd) {
415 ATRACE_CALL();
416 ALOGV("Surface::cancelBuffer");
417 Mutex::Autolock lock(mMutex);
418 int i = getSlotFromBufferLocked(buffer);
419 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900420 if (fenceFd >= 0) {
421 close(fenceFd);
422 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800423 return i;
424 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800425 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
426 if (fenceFd >= 0) {
427 close(fenceFd);
428 }
429 return OK;
430 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800431 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
432 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800433
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700434 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800435 mSharedBufferHasBeenQueued = true;
436 }
437
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800438 return OK;
439}
440
441int Surface::getSlotFromBufferLocked(
442 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800443 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
444 if (mSlots[i].buffer != NULL &&
445 mSlots[i].buffer->handle == buffer->handle) {
446 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700447 }
448 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800449 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
450 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700451}
452
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800453int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800454 ALOGV("Surface::lockBuffer");
455 Mutex::Autolock lock(mMutex);
456 return OK;
457}
Mathias Agopian631f3582010-05-25 17:51:34 -0700458
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800459int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
460 ATRACE_CALL();
461 ALOGV("Surface::queueBuffer");
462 Mutex::Autolock lock(mMutex);
463 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700464 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800465
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800466 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700467 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700468 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700469 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700470 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800471 } else {
472 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700473 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800474 int i = getSlotFromBufferLocked(buffer);
475 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900476 if (fenceFd >= 0) {
477 close(fenceFd);
478 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800479 return i;
480 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800481 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
482 if (fenceFd >= 0) {
483 close(fenceFd);
484 }
485 return OK;
486 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800489 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700490 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800491 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800493 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
494 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700495 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800496 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700497 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700498
Dan Stozac62acbd2015-04-21 16:42:49 -0700499 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700500 input.setSurfaceDamage(Region::INVALID_REGION);
501 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700502 // Here we do two things:
503 // 1) The surface damage was specified using the OpenGL ES convention of
504 // the origin being in the bottom-left corner. Here we flip to the
505 // convention that the rest of the system uses (top-left corner) by
506 // subtracting all top/bottom coordinates from the buffer height.
507 // 2) If the buffer is coming in rotated (for example, because the EGL
508 // implementation is reacting to the transform hint coming back from
509 // SurfaceFlinger), the surface damage needs to be rotated the
510 // opposite direction, since it was generated assuming an unrotated
511 // buffer (the app doesn't know that the EGL implementation is
512 // reacting to the transform hint behind its back). The
513 // transformations in the switch statement below apply those
514 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
515
516 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700517 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700518 bool rotated90 = (mTransform ^ mStickyTransform) &
519 NATIVE_WINDOW_TRANSFORM_ROT_90;
520 if (rotated90) {
521 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700522 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700523
Dan Stoza5065a552015-03-17 16:23:42 -0700524 Region flippedRegion;
525 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700526 int left = rect.left;
527 int right = rect.right;
528 int top = height - rect.bottom; // Flip from OpenGL convention
529 int bottom = height - rect.top; // Flip from OpenGL convention
530 switch (mTransform ^ mStickyTransform) {
531 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
532 // Rotate 270 degrees
533 Rect flippedRect{top, width - right, bottom, width - left};
534 flippedRegion.orSelf(flippedRect);
535 break;
536 }
537 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
538 // Rotate 180 degrees
539 Rect flippedRect{width - right, height - bottom,
540 width - left, height - top};
541 flippedRegion.orSelf(flippedRect);
542 break;
543 }
544 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
545 // Rotate 90 degrees
546 Rect flippedRect{height - bottom, left,
547 height - top, right};
548 flippedRegion.orSelf(flippedRect);
549 break;
550 }
551 default: {
552 Rect flippedRect{left, top, right, bottom};
553 flippedRegion.orSelf(flippedRect);
554 break;
555 }
556 }
Dan Stoza5065a552015-03-17 16:23:42 -0700557 }
558
559 input.setSurfaceDamage(flippedRegion);
560 }
561
Dan Stoza70ccba52016-07-01 14:00:40 -0700562 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800563 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700564 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800565 if (err != OK) {
566 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
567 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800568
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700569 if (mEnableFrameTimestamps) {
570 mFrameEventHistory.applyDelta(output.frameTimestamps);
571 // Update timestamps with the local acquire fence.
572 // The consumer doesn't send it back to prevent us from having two
573 // file descriptors of the same fence.
Brian Anderson3d4039d2016-09-23 16:31:30 -0700574 mFrameEventHistory.updateAcquireFence(mNextFrameNumber,
575 std::make_shared<FenceTime>(std::move(fence)));
576
577 // Cache timestamps of signaled fences so we can close their file
578 // descriptors.
579 mFrameEventHistory.updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700580 }
581
582 mDefaultWidth = output.width;
583 mDefaultHeight = output.height;
584 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700585
Ruben Brunk1681d952014-06-27 15:51:55 -0700586 // Disable transform hint if sticky transform is set.
587 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700588 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700589 }
590
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700591 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700592
Dan Stozac62acbd2015-04-21 16:42:49 -0700593 if (!mConnectedToCpu) {
594 // Clear surface damage back to full-buffer
595 mDirtyRegion = Region::INVALID_REGION;
596 }
Dan Stoza5065a552015-03-17 16:23:42 -0700597
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700598 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800599 mSharedBufferHasBeenQueued = true;
600 }
601
Robert Carr9f31e292016-04-11 11:15:32 -0700602 mQueueBufferCondition.broadcast();
603
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800604 return err;
605}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700606
Brian Anderson069b3652016-07-22 10:32:47 -0700607void Surface::querySupportedTimestampsLocked() const {
608 // mMutex must be locked when calling this method.
609
610 if (mQueriedSupportedTimestamps) {
611 return;
612 }
613 mQueriedSupportedTimestamps = true;
614
Brian Anderson3890c392016-07-25 12:48:08 -0700615 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson069b3652016-07-22 10:32:47 -0700616 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
617 status_t err = composer->getSupportedFrameTimestamps(
618 &supportedFrameTimestamps);
619
620 if (err != NO_ERROR) {
621 return;
622 }
623
624 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700625 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700626 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700627 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700628 mFrameTimestampsSupportsRetire = true;
629 }
630 }
631}
632
Mathias Agopiana67932f2011-04-20 14:20:59 -0700633int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800634 ATRACE_CALL();
635 ALOGV("Surface::query");
636 { // scope for the lock
637 Mutex::Autolock lock(mMutex);
638 switch (what) {
639 case NATIVE_WINDOW_FORMAT:
640 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800641 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800642 return NO_ERROR;
643 }
644 break;
645 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
646 sp<ISurfaceComposer> composer(
647 ComposerService::getComposerService());
648 if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) {
649 *value = 1;
650 } else {
651 *value = 0;
652 }
653 return NO_ERROR;
654 }
655 case NATIVE_WINDOW_CONCRETE_TYPE:
656 *value = NATIVE_WINDOW_SURFACE;
657 return NO_ERROR;
658 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800659 *value = static_cast<int>(
660 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800661 return NO_ERROR;
662 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800663 *value = static_cast<int>(
664 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800665 return NO_ERROR;
666 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800667 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800668 return NO_ERROR;
669 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
670 status_t err = NO_ERROR;
671 if (!mConsumerRunningBehind) {
672 *value = 0;
673 } else {
674 err = mGraphicBufferProducer->query(what, value);
675 if (err == NO_ERROR) {
676 mConsumerRunningBehind = *value;
677 }
678 }
679 return err;
680 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700681 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
682 int64_t durationUs = mLastDequeueDuration / 1000;
683 *value = durationUs > std::numeric_limits<int>::max() ?
684 std::numeric_limits<int>::max() :
685 static_cast<int>(durationUs);
686 return NO_ERROR;
687 }
688 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
689 int64_t durationUs = mLastQueueDuration / 1000;
690 *value = durationUs > std::numeric_limits<int>::max() ?
691 std::numeric_limits<int>::max() :
692 static_cast<int>(durationUs);
693 return NO_ERROR;
694 }
Brian Anderson069b3652016-07-22 10:32:47 -0700695 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
696 querySupportedTimestampsLocked();
697 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
698 return NO_ERROR;
699 }
700 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
701 querySupportedTimestampsLocked();
702 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
703 return NO_ERROR;
704 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800705 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700706 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800707 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800708}
709
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800710int Surface::perform(int operation, va_list args)
711{
712 int res = NO_ERROR;
713 switch (operation) {
714 case NATIVE_WINDOW_CONNECT:
715 // deprecated. must return NO_ERROR.
716 break;
717 case NATIVE_WINDOW_DISCONNECT:
718 // deprecated. must return NO_ERROR.
719 break;
720 case NATIVE_WINDOW_SET_USAGE:
721 res = dispatchSetUsage(args);
722 break;
723 case NATIVE_WINDOW_SET_CROP:
724 res = dispatchSetCrop(args);
725 break;
726 case NATIVE_WINDOW_SET_BUFFER_COUNT:
727 res = dispatchSetBufferCount(args);
728 break;
729 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
730 res = dispatchSetBuffersGeometry(args);
731 break;
732 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
733 res = dispatchSetBuffersTransform(args);
734 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700735 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
736 res = dispatchSetBuffersStickyTransform(args);
737 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800738 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
739 res = dispatchSetBuffersTimestamp(args);
740 break;
741 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
742 res = dispatchSetBuffersDimensions(args);
743 break;
744 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
745 res = dispatchSetBuffersUserDimensions(args);
746 break;
747 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
748 res = dispatchSetBuffersFormat(args);
749 break;
750 case NATIVE_WINDOW_LOCK:
751 res = dispatchLock(args);
752 break;
753 case NATIVE_WINDOW_UNLOCK_AND_POST:
754 res = dispatchUnlockAndPost(args);
755 break;
756 case NATIVE_WINDOW_SET_SCALING_MODE:
757 res = dispatchSetScalingMode(args);
758 break;
759 case NATIVE_WINDOW_API_CONNECT:
760 res = dispatchConnect(args);
761 break;
762 case NATIVE_WINDOW_API_DISCONNECT:
763 res = dispatchDisconnect(args);
764 break;
Rachad7cb0d392014-07-29 17:53:53 -0700765 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
766 res = dispatchSetSidebandStream(args);
767 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800768 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
769 res = dispatchSetBuffersDataSpace(args);
770 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700771 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
772 res = dispatchSetSurfaceDamage(args);
773 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700774 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
775 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700776 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800777 case NATIVE_WINDOW_SET_AUTO_REFRESH:
778 res = dispatchSetAutoRefresh(args);
779 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700780 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
781 res = dispatchEnableFrameTimestamps(args);
782 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800783 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
784 res = dispatchGetFrameTimestamps(args);
785 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800786 default:
787 res = NAME_NOT_FOUND;
788 break;
789 }
790 return res;
791}
Mathias Agopiana138f892010-05-21 17:24:35 -0700792
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800793int Surface::dispatchConnect(va_list args) {
794 int api = va_arg(args, int);
795 return connect(api);
796}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800797
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800798int Surface::dispatchDisconnect(va_list args) {
799 int api = va_arg(args, int);
800 return disconnect(api);
801}
802
803int Surface::dispatchSetUsage(va_list args) {
804 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800805 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800806}
807
808int Surface::dispatchSetCrop(va_list args) {
809 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
810 return setCrop(reinterpret_cast<Rect const*>(rect));
811}
812
813int Surface::dispatchSetBufferCount(va_list args) {
814 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800815 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800816}
817
818int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800819 uint32_t width = va_arg(args, uint32_t);
820 uint32_t height = va_arg(args, uint32_t);
821 PixelFormat format = va_arg(args, PixelFormat);
822 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800823 if (err != 0) {
824 return err;
825 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800826 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800827}
828
829int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800830 uint32_t width = va_arg(args, uint32_t);
831 uint32_t height = va_arg(args, uint32_t);
832 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800833}
834
835int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800836 uint32_t width = va_arg(args, uint32_t);
837 uint32_t height = va_arg(args, uint32_t);
838 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800839}
840
841int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800842 PixelFormat format = va_arg(args, PixelFormat);
843 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800844}
845
846int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800847 int mode = va_arg(args, int);
848 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800849}
850
851int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800852 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800853 return setBuffersTransform(transform);
854}
855
Ruben Brunk1681d952014-06-27 15:51:55 -0700856int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800857 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700858 return setBuffersStickyTransform(transform);
859}
860
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800861int Surface::dispatchSetBuffersTimestamp(va_list args) {
862 int64_t timestamp = va_arg(args, int64_t);
863 return setBuffersTimestamp(timestamp);
864}
865
866int Surface::dispatchLock(va_list args) {
867 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
868 ARect* inOutDirtyBounds = va_arg(args, ARect*);
869 return lock(outBuffer, inOutDirtyBounds);
870}
871
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800872int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800873 return unlockAndPost();
874}
875
Rachad7cb0d392014-07-29 17:53:53 -0700876int Surface::dispatchSetSidebandStream(va_list args) {
877 native_handle_t* sH = va_arg(args, native_handle_t*);
878 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
879 setSidebandStream(sidebandHandle);
880 return OK;
881}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800882
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800883int Surface::dispatchSetBuffersDataSpace(va_list args) {
884 android_dataspace dataspace =
885 static_cast<android_dataspace>(va_arg(args, int));
886 return setBuffersDataSpace(dataspace);
887}
888
Dan Stoza5065a552015-03-17 16:23:42 -0700889int Surface::dispatchSetSurfaceDamage(va_list args) {
890 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
891 size_t numRects = va_arg(args, size_t);
892 setSurfaceDamage(rects, numRects);
893 return NO_ERROR;
894}
895
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700896int Surface::dispatchSetSharedBufferMode(va_list args) {
897 bool sharedBufferMode = va_arg(args, int);
898 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800899}
900
901int Surface::dispatchSetAutoRefresh(va_list args) {
902 bool autoRefresh = va_arg(args, int);
903 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700904}
905
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700906int Surface::dispatchEnableFrameTimestamps(va_list args) {
907 bool enable = va_arg(args, int);
908 enableFrameTimestamps(enable);
909 return NO_ERROR;
910}
911
Pablo Ceballosce796e72016-02-04 19:10:51 -0800912int Surface::dispatchGetFrameTimestamps(va_list args) {
913 uint32_t framesAgo = va_arg(args, uint32_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -0700914 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800915 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
916 nsecs_t* outRefreshStartTime = va_arg(args, int64_t*);
917 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700918 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800919 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
920 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700921 return getFrameTimestamps(getNextFrameNumber() - 1 - framesAgo,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700922 outRequestedPresentTime, outAcquireTime, outRefreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700923 outGlCompositionDoneTime, outDisplayPresentTime,
924 outDisplayRetireTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800925}
926
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800927int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -0800928 static sp<IProducerListener> listener = new DummyProducerListener();
929 return connect(api, listener);
930}
931
932int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800933 ATRACE_CALL();
934 ALOGV("Surface::connect");
935 Mutex::Autolock lock(mMutex);
936 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700937 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800938 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700939 mDefaultWidth = output.width;
940 mDefaultHeight = output.height;
941 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -0700942
943 // Disable transform hint if sticky transform is set.
944 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700945 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700946 }
947
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700948 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800949 }
950 if (!err && api == NATIVE_WINDOW_API_CPU) {
951 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -0700952 // Clear the dirty region in case we're switching from a non-CPU API
953 mDirtyRegion.clear();
954 } else if (!err) {
955 // Initialize the dirty region for tracking surface damage
956 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800957 }
Dan Stoza5065a552015-03-17 16:23:42 -0700958
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800959 return err;
960}
961
Mathias Agopian365857d2013-09-11 19:35:45 -0700962
Robert Carr97b9c862016-09-08 13:54:35 -0700963int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800964 ATRACE_CALL();
965 ALOGV("Surface::disconnect");
966 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800967 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
968 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800969 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -0700970 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800971 if (!err) {
972 mReqFormat = 0;
973 mReqWidth = 0;
974 mReqHeight = 0;
975 mReqUsage = 0;
976 mCrop.clear();
977 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
978 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -0700979 mStickyTransform = 0;
980
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800981 if (api == NATIVE_WINDOW_API_CPU) {
982 mConnectedToCpu = false;
983 }
984 }
985 return err;
986}
987
Dan Stozad9c49712015-04-27 11:06:01 -0700988int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -0700989 sp<Fence>* outFence) {
990 ATRACE_CALL();
991 ALOGV("Surface::detachNextBuffer");
992
993 if (outBuffer == NULL || outFence == NULL) {
994 return BAD_VALUE;
995 }
996
997 Mutex::Autolock lock(mMutex);
998
999 sp<GraphicBuffer> buffer(NULL);
1000 sp<Fence> fence(NULL);
1001 status_t result = mGraphicBufferProducer->detachNextBuffer(
1002 &buffer, &fence);
1003 if (result != NO_ERROR) {
1004 return result;
1005 }
1006
Dan Stozad9c49712015-04-27 11:06:01 -07001007 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001008 if (fence != NULL && fence->isValid()) {
1009 *outFence = fence;
1010 } else {
1011 *outFence = Fence::NO_FENCE;
1012 }
1013
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001014 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1015 if (mSlots[i].buffer != NULL &&
1016 mSlots[i].buffer->handle == buffer->handle) {
1017 mSlots[i].buffer = NULL;
1018 }
1019 }
1020
Dan Stoza231832e2015-03-11 11:55:01 -07001021 return NO_ERROR;
1022}
1023
1024int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1025{
1026 ATRACE_CALL();
1027 ALOGV("Surface::attachBuffer");
1028
1029 Mutex::Autolock lock(mMutex);
1030
1031 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001032 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1033 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001034 int32_t attachedSlot = -1;
1035 status_t result = mGraphicBufferProducer->attachBuffer(
1036 &attachedSlot, graphicBuffer);
1037 if (result != NO_ERROR) {
1038 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001039 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001040 return result;
1041 }
1042 mSlots[attachedSlot].buffer = graphicBuffer;
1043
1044 return NO_ERROR;
1045}
1046
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001047int Surface::setUsage(uint32_t reqUsage)
1048{
1049 ALOGV("Surface::setUsage");
1050 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001051 if (reqUsage != mReqUsage) {
1052 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1053 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001054 mReqUsage = reqUsage;
1055 return OK;
1056}
1057
1058int Surface::setCrop(Rect const* rect)
1059{
1060 ATRACE_CALL();
1061
Pablo Ceballos60d69222015-08-07 14:47:20 -07001062 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001063 if (rect == NULL || rect->isEmpty()) {
1064 realRect.clear();
1065 } else {
1066 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001067 }
1068
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001069 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1070 realRect.left, realRect.top, realRect.right, realRect.bottom);
1071
1072 Mutex::Autolock lock(mMutex);
1073 mCrop = realRect;
1074 return NO_ERROR;
1075}
1076
1077int Surface::setBufferCount(int bufferCount)
1078{
1079 ATRACE_CALL();
1080 ALOGV("Surface::setBufferCount");
1081 Mutex::Autolock lock(mMutex);
1082
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001083 status_t err = NO_ERROR;
1084 if (bufferCount == 0) {
1085 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1086 } else {
1087 int minUndequeuedBuffers = 0;
1088 err = mGraphicBufferProducer->query(
1089 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1090 if (err == NO_ERROR) {
1091 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1092 bufferCount - minUndequeuedBuffers);
1093 }
1094 }
Mathias Agopian90147262010-01-22 11:47:55 -08001095
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001096 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1097 bufferCount, strerror(-err));
1098
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001099 return err;
1100}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001101
Pablo Ceballosfa455352015-08-12 17:47:47 -07001102int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1103 ATRACE_CALL();
1104 ALOGV("Surface::setMaxDequeuedBufferCount");
1105 Mutex::Autolock lock(mMutex);
1106
1107 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1108 maxDequeuedBuffers);
1109 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1110 "returned %s", maxDequeuedBuffers, strerror(-err));
1111
Pablo Ceballosfa455352015-08-12 17:47:47 -07001112 return err;
1113}
1114
1115int Surface::setAsyncMode(bool async) {
1116 ATRACE_CALL();
1117 ALOGV("Surface::setAsyncMode");
1118 Mutex::Autolock lock(mMutex);
1119
1120 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1121 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1122 async, strerror(-err));
1123
Pablo Ceballosfa455352015-08-12 17:47:47 -07001124 return err;
1125}
1126
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001127int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001128 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001129 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001130 Mutex::Autolock lock(mMutex);
1131
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001132 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1133 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001134 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001135 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001136 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001137 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1138 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001139
1140 return err;
1141}
1142
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001143int Surface::setAutoRefresh(bool autoRefresh) {
1144 ATRACE_CALL();
1145 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1146 Mutex::Autolock lock(mMutex);
1147
1148 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1149 if (err == NO_ERROR) {
1150 mAutoRefresh = autoRefresh;
1151 }
1152 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1153 autoRefresh, strerror(-err));
1154 return err;
1155}
1156
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001157int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001158{
1159 ATRACE_CALL();
1160 ALOGV("Surface::setBuffersDimensions");
1161
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001162 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001163 return BAD_VALUE;
1164
1165 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001166 if (width != mReqWidth || height != mReqHeight) {
1167 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1168 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001169 mReqWidth = width;
1170 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001171 return NO_ERROR;
1172}
1173
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001174int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001175{
1176 ATRACE_CALL();
1177 ALOGV("Surface::setBuffersUserDimensions");
1178
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001179 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001180 return BAD_VALUE;
1181
1182 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001183 if (width != mUserWidth || height != mUserHeight) {
1184 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1185 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001186 mUserWidth = width;
1187 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001188 return NO_ERROR;
1189}
1190
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001191int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001192{
1193 ALOGV("Surface::setBuffersFormat");
1194
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001195 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001196 if (format != mReqFormat) {
1197 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1198 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001199 mReqFormat = format;
1200 return NO_ERROR;
1201}
1202
1203int Surface::setScalingMode(int mode)
1204{
1205 ATRACE_CALL();
1206 ALOGV("Surface::setScalingMode(%d)", mode);
1207
1208 switch (mode) {
1209 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1210 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1211 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001212 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001213 break;
1214 default:
1215 ALOGE("unknown scaling mode: %d", mode);
1216 return BAD_VALUE;
1217 }
1218
1219 Mutex::Autolock lock(mMutex);
1220 mScalingMode = mode;
1221 return NO_ERROR;
1222}
1223
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001224int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001225{
1226 ATRACE_CALL();
1227 ALOGV("Surface::setBuffersTransform");
1228 Mutex::Autolock lock(mMutex);
1229 mTransform = transform;
1230 return NO_ERROR;
1231}
1232
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001233int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001234{
1235 ATRACE_CALL();
1236 ALOGV("Surface::setBuffersStickyTransform");
1237 Mutex::Autolock lock(mMutex);
1238 mStickyTransform = transform;
1239 return NO_ERROR;
1240}
1241
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001242int Surface::setBuffersTimestamp(int64_t timestamp)
1243{
1244 ALOGV("Surface::setBuffersTimestamp");
1245 Mutex::Autolock lock(mMutex);
1246 mTimestamp = timestamp;
1247 return NO_ERROR;
1248}
1249
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001250int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1251{
1252 ALOGV("Surface::setBuffersDataSpace");
1253 Mutex::Autolock lock(mMutex);
1254 mDataSpace = dataSpace;
1255 return NO_ERROR;
1256}
1257
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001258void Surface::freeAllBuffers() {
1259 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1260 mSlots[i].buffer = 0;
1261 }
1262}
1263
Dan Stoza5065a552015-03-17 16:23:42 -07001264void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1265 ATRACE_CALL();
1266 ALOGV("Surface::setSurfaceDamage");
1267 Mutex::Autolock lock(mMutex);
1268
Dan Stozac62acbd2015-04-21 16:42:49 -07001269 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001270 mDirtyRegion = Region::INVALID_REGION;
1271 return;
1272 }
1273
1274 mDirtyRegion.clear();
1275 for (size_t r = 0; r < numRects; ++r) {
1276 // We intentionally flip top and bottom here, since because they're
1277 // specified with a bottom-left origin, top > bottom, which fails
1278 // validation in the Region class. We will fix this up when we flip to a
1279 // top-left origin in queueBuffer.
1280 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1281 mDirtyRegion.orSelf(rect);
1282 }
1283}
1284
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001285// ----------------------------------------------------------------------
1286// the lock/unlock APIs must be used from the same thread
1287
1288static status_t copyBlt(
1289 const sp<GraphicBuffer>& dst,
1290 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001291 const Region& reg,
1292 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001293{
1294 // src and dst with, height and format must be identical. no verification
1295 // is done here.
1296 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001297 uint8_t* src_bits = NULL;
1298 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1299 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001300 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1301
1302 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001303 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1304 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001305 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001306 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001307
1308 Region::const_iterator head(reg.begin());
1309 Region::const_iterator tail(reg.end());
1310 if (head != tail && src_bits && dst_bits) {
1311 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001312 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1313 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001314
1315 while (head != tail) {
1316 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001317 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001318 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001319 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1320 uint8_t const * s = src_bits +
1321 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1322 uint8_t * d = dst_bits +
1323 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001324 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001325 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001326 h = 1;
1327 }
1328 do {
1329 memcpy(d, s, size);
1330 d += dbpr;
1331 s += sbpr;
1332 } while (--h > 0);
1333 }
1334 }
1335
1336 if (src_bits)
1337 src->unlock();
1338
1339 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001340 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001341
1342 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343}
1344
Mathias Agopiana138f892010-05-21 17:24:35 -07001345// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001346
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001347status_t Surface::lock(
1348 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1349{
1350 if (mLockedBuffer != 0) {
1351 ALOGE("Surface::lock failed, already locked");
1352 return INVALID_OPERATION;
1353 }
1354
1355 if (!mConnectedToCpu) {
1356 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1357 if (err) {
1358 return err;
1359 }
1360 // we're intending to do software rendering from this point
1361 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1362 }
1363
1364 ANativeWindowBuffer* out;
1365 int fenceFd = -1;
1366 status_t err = dequeueBuffer(&out, &fenceFd);
1367 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1368 if (err == NO_ERROR) {
1369 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001370 const Rect bounds(backBuffer->width, backBuffer->height);
1371
1372 Region newDirtyRegion;
1373 if (inOutDirtyBounds) {
1374 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1375 newDirtyRegion.andSelf(bounds);
1376 } else {
1377 newDirtyRegion.set(bounds);
1378 }
1379
1380 // figure out if we can copy the frontbuffer back
1381 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1382 const bool canCopyBack = (frontBuffer != 0 &&
1383 backBuffer->width == frontBuffer->width &&
1384 backBuffer->height == frontBuffer->height &&
1385 backBuffer->format == frontBuffer->format);
1386
1387 if (canCopyBack) {
1388 // copy the area that is invalid and not repainted this round
1389 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001390 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001391 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001392 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001393 } else {
1394 // if we can't copy-back anything, modify the user's dirty
1395 // region to make sure they redraw the whole buffer
1396 newDirtyRegion.set(bounds);
1397 mDirtyRegion.clear();
1398 Mutex::Autolock lock(mMutex);
1399 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1400 mSlots[i].dirtyRegion.clear();
1401 }
1402 }
1403
1404
1405 { // scope for the lock
1406 Mutex::Autolock lock(mMutex);
1407 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1408 if (backBufferSlot >= 0) {
1409 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1410 mDirtyRegion.subtract(dirtyRegion);
1411 dirtyRegion = newDirtyRegion;
1412 }
1413 }
1414
1415 mDirtyRegion.orSelf(newDirtyRegion);
1416 if (inOutDirtyBounds) {
1417 *inOutDirtyBounds = newDirtyRegion.getBounds();
1418 }
1419
1420 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001421 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001422 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001423 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001424
1425 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1426 backBuffer->handle);
1427
1428 if (res != 0) {
1429 err = INVALID_OPERATION;
1430 } else {
1431 mLockedBuffer = backBuffer;
1432 outBuffer->width = backBuffer->width;
1433 outBuffer->height = backBuffer->height;
1434 outBuffer->stride = backBuffer->stride;
1435 outBuffer->format = backBuffer->format;
1436 outBuffer->bits = vaddr;
1437 }
1438 }
1439 return err;
1440}
1441
1442status_t Surface::unlockAndPost()
1443{
1444 if (mLockedBuffer == 0) {
1445 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1446 return INVALID_OPERATION;
1447 }
1448
Francis Hart8f396012014-04-01 15:30:53 +03001449 int fd = -1;
1450 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001451 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1452
Francis Hart8f396012014-04-01 15:30:53 +03001453 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001454 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1455 mLockedBuffer->handle, strerror(-err));
1456
1457 mPostedBuffer = mLockedBuffer;
1458 mLockedBuffer = 0;
1459 return err;
1460}
1461
Robert Carr9f31e292016-04-11 11:15:32 -07001462bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1463 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001464 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001465 return true;
1466 }
1467 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1468}
1469
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001470status_t Surface::getUniqueId(uint64_t* outId) const {
1471 Mutex::Autolock lock(mMutex);
1472 return mGraphicBufferProducer->getUniqueId(outId);
1473}
1474
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001475namespace view {
1476
1477status_t Surface::writeToParcel(Parcel* parcel) const {
1478 return writeToParcel(parcel, false);
1479}
1480
1481status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const {
1482 if (parcel == nullptr) return BAD_VALUE;
1483
1484 status_t res = OK;
1485
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001486 if (!nameAlreadyWritten) {
1487 res = parcel->writeString16(name);
1488 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001489
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001490 /* isSingleBuffered defaults to no */
1491 res = parcel->writeInt32(0);
1492 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001493 }
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001494
1495 res = parcel->writeStrongBinder(
1496 IGraphicBufferProducer::asBinder(graphicBufferProducer));
1497
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001498 return res;
1499}
1500
1501status_t Surface::readFromParcel(const Parcel* parcel) {
1502 return readFromParcel(parcel, false);
1503}
1504
1505status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) {
1506 if (parcel == nullptr) return BAD_VALUE;
1507
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001508 status_t res = OK;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001509 if (!nameAlreadyRead) {
1510 name = readMaybeEmptyString16(parcel);
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001511 // Discard this for now
1512 int isSingleBuffered;
1513 res = parcel->readInt32(&isSingleBuffered);
1514 if (res != OK) {
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001515 ALOGE("Can't read isSingleBuffered");
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001516 return res;
1517 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001518 }
1519
1520 sp<IBinder> binder;
1521
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001522 res = parcel->readNullableStrongBinder(&binder);
1523 if (res != OK) {
1524 ALOGE("%s: Can't read strong binder", __FUNCTION__);
1525 return res;
1526 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001527
1528 graphicBufferProducer = interface_cast<IGraphicBufferProducer>(binder);
1529
1530 return OK;
1531}
1532
1533String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
1534 size_t len;
1535 const char16_t* str = parcel->readString16Inplace(&len);
1536 if (str != nullptr) {
1537 return String16(str, len);
1538 } else {
1539 return String16();
1540 }
1541}
1542
1543} // namespace view
1544
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001545}; // namespace android