blob: 0754461923fb7035054d698173cbc81506cf18d8 [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),
Brian Anderson069b3652016-07-22 10:32:47 -070059 mNextFrameNumber(1),
60 mQueriedSupportedTimestamps(false),
61 mFrameTimestampsSupportsPresent(false),
Brian Anderson7c3ba8a2016-07-25 12:48:08 -070062 mFrameTimestampsSupportsRetire(false),
63 mEnableFrameTimestamps(false)
Mathias Agopian62185b72009-04-16 16:19:50 -070064{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080065 // Initialize the ANativeWindow function pointers.
66 ANativeWindow::setSwapInterval = hook_setSwapInterval;
67 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
68 ANativeWindow::cancelBuffer = hook_cancelBuffer;
69 ANativeWindow::queueBuffer = hook_queueBuffer;
70 ANativeWindow::query = hook_query;
71 ANativeWindow::perform = hook_perform;
72
73 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
74 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
75 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
76 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
77
78 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
79 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
80
81 mReqWidth = 0;
82 mReqHeight = 0;
83 mReqFormat = 0;
84 mReqUsage = 0;
85 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080086 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080087 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
88 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070089 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080090 mDefaultWidth = 0;
91 mDefaultHeight = 0;
92 mUserWidth = 0;
93 mUserHeight = 0;
94 mTransformHint = 0;
95 mConsumerRunningBehind = false;
96 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070097 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070098 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070099}
100
Mathias Agopian35ffa6a2013-03-12 18:45:09 -0700101Surface::~Surface() {
102 if (mConnectedToCpu) {
103 Surface::disconnect(NATIVE_WINDOW_API_CPU);
104 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800105}
106
107sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
108 return mGraphicBufferProducer;
109}
110
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900111void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
112 mGraphicBufferProducer->setSidebandStream(stream);
113}
114
Dan Stoza29a3e902014-06-20 13:13:57 -0700115void Surface::allocateBuffers() {
116 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
117 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700118 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
119 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700120}
121
Dan Stoza812ed062015-06-02 15:45:22 -0700122status_t Surface::setGenerationNumber(uint32_t generation) {
123 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
124 if (result == NO_ERROR) {
125 mGenerationNumber = generation;
126 }
127 return result;
128}
129
Dan Stoza7dde5992015-05-22 09:51:44 -0700130uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700131 Mutex::Autolock lock(mMutex);
132 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700133}
134
Dan Stozac6f30bd2015-06-08 09:32:50 -0700135String8 Surface::getConsumerName() const {
136 return mGraphicBufferProducer->getConsumerName();
137}
138
Dan Stoza127fc632015-06-30 13:43:32 -0700139status_t Surface::setDequeueTimeout(nsecs_t timeout) {
140 return mGraphicBufferProducer->setDequeueTimeout(timeout);
141}
142
Dan Stoza50101d02016-04-07 16:53:23 -0700143status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700144 sp<Fence>* outFence, float outTransformMatrix[16]) {
145 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
146 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700147}
148
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700149void Surface::enableFrameTimestamps(bool enable) {
150 Mutex::Autolock lock(mMutex);
151 mEnableFrameTimestamps = enable;
152}
153
Brian Anderson3d4039d2016-09-23 16:31:30 -0700154static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
155 if (dst != nullptr) {
156 *dst = Fence::isValidTimestamp(src) ? src : 0;
157 }
158}
159
160static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) {
161 if (dst != nullptr) {
162 nsecs_t signalTime = src->getSignalTime();
163 *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0;
164 }
165}
166
Brian Anderson069b3652016-07-22 10:32:47 -0700167status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700168 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
169 nsecs_t* outRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700170 nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime,
171 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800172 ATRACE_CALL();
173
Brian Anderson3890c392016-07-25 12:48:08 -0700174 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700175
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700176 if (!mEnableFrameTimestamps) {
177 return INVALID_OPERATION;
178 }
179
Brian Anderson3890c392016-07-25 12:48:08 -0700180 // Verify the requested timestamps are supported.
181 querySupportedTimestampsLocked();
182 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
183 return BAD_VALUE;
184 }
185 if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
186 return BAD_VALUE;
Brian Anderson069b3652016-07-22 10:32:47 -0700187 }
188
Brian Anderson3890c392016-07-25 12:48:08 -0700189 FrameEvents* events = mFrameEventHistory.getFrame(frameNumber);
Brian Anderson069b3652016-07-22 10:32:47 -0700190
Brian Anderson3890c392016-07-25 12:48:08 -0700191 // Update our cache of events if the requested events are not available.
192 if (events == nullptr ||
193 (outRequestedPresentTime && !events->hasRequestedPresentInfo()) ||
194 (outAcquireTime && !events->hasAcquireInfo()) ||
195 (outRefreshStartTime && !events->hasFirstRefreshStartInfo()) ||
196 (outGlCompositionDoneTime && !events->hasGpuCompositionDoneInfo()) ||
197 (outDisplayPresentTime && !events->hasDisplayPresentInfo()) ||
198 (outDisplayRetireTime && !events->hasDisplayRetireInfo()) ||
199 (outReleaseTime && !events->hasReleaseInfo())) {
200 FrameEventHistoryDelta delta;
201 mGraphicBufferProducer->getFrameTimestamps(&delta);
202 mFrameEventHistory.applyDelta(delta);
203 events = mFrameEventHistory.getFrame(frameNumber);
204 }
205
206 // A record for the requested frame does not exist.
207 if (events == nullptr) {
Brian Anderson069b3652016-07-22 10:32:47 -0700208 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800209 }
Brian Anderson069b3652016-07-22 10:32:47 -0700210
Brian Anderson3d4039d2016-09-23 16:31:30 -0700211 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
212 getFrameTimestamp(outRefreshStartTime, events->firstRefreshStartTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700213
Brian Anderson3d4039d2016-09-23 16:31:30 -0700214 getFrameTimestampFence(outAcquireTime, events->acquireFence);
215 getFrameTimestampFence(
216 outGlCompositionDoneTime, events->gpuCompositionDoneFence);
217 getFrameTimestampFence(
218 outDisplayPresentTime, events->displayPresentFence);
219 getFrameTimestampFence(outDisplayRetireTime, events->displayRetireFence);
220 getFrameTimestampFence(outReleaseTime, events->releaseFence);
Brian Anderson069b3652016-07-22 10:32:47 -0700221
222 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800223}
224
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800225int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
226 Surface* c = getSelf(window);
227 return c->setSwapInterval(interval);
228}
229
230int Surface::hook_dequeueBuffer(ANativeWindow* window,
231 ANativeWindowBuffer** buffer, int* fenceFd) {
232 Surface* c = getSelf(window);
233 return c->dequeueBuffer(buffer, fenceFd);
234}
235
236int Surface::hook_cancelBuffer(ANativeWindow* window,
237 ANativeWindowBuffer* buffer, int fenceFd) {
238 Surface* c = getSelf(window);
239 return c->cancelBuffer(buffer, fenceFd);
240}
241
242int Surface::hook_queueBuffer(ANativeWindow* window,
243 ANativeWindowBuffer* buffer, int fenceFd) {
244 Surface* c = getSelf(window);
245 return c->queueBuffer(buffer, fenceFd);
246}
247
248int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
249 ANativeWindowBuffer** buffer) {
250 Surface* c = getSelf(window);
251 ANativeWindowBuffer* buf;
252 int fenceFd = -1;
253 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600254 if (result != OK) {
255 return result;
256 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800257 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700258 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800259 if (waitResult != OK) {
260 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
261 waitResult);
262 c->cancelBuffer(buf, -1);
263 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700264 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800265 *buffer = buf;
266 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700267}
268
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800269int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
270 ANativeWindowBuffer* buffer) {
271 Surface* c = getSelf(window);
272 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700273}
274
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800275int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
276 ANativeWindowBuffer* buffer) {
277 Surface* c = getSelf(window);
278 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700279}
280
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800281int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
282 ANativeWindowBuffer* buffer) {
283 Surface* c = getSelf(window);
284 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700285}
Mathias Agopian62185b72009-04-16 16:19:50 -0700286
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800287int Surface::hook_query(const ANativeWindow* window,
288 int what, int* value) {
289 const Surface* c = getSelf(window);
290 return c->query(what, value);
291}
292
293int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
294 va_list args;
295 va_start(args, operation);
296 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700297 int result = c->perform(operation, args);
298 va_end(args);
299 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800300}
301
302int Surface::setSwapInterval(int interval) {
303 ATRACE_CALL();
304 // EGL specification states:
305 // interval is silently clamped to minimum and maximum implementation
306 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800307
308 if (interval < minSwapInterval)
309 interval = minSwapInterval;
310
311 if (interval > maxSwapInterval)
312 interval = maxSwapInterval;
313
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700314 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700315 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800316
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700317 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800318}
319
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700320int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800321 ATRACE_CALL();
322 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800323
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800324 uint32_t reqWidth;
325 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800326 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800327 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700328 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800329
330 {
331 Mutex::Autolock lock(mMutex);
332
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800333 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
334 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800335
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800336 reqFormat = mReqFormat;
337 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800338
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700339 enableFrameTimestamps = mEnableFrameTimestamps;
340
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700341 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800342 BufferItem::INVALID_BUFFER_SLOT) {
343 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
344 if (gbuf != NULL) {
345 *buffer = gbuf.get();
346 *fenceFd = -1;
347 return OK;
348 }
349 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800350 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
351
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800352 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800353 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700354 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700355
356 FrameEventHistoryDelta frameTimestamps;
357 FrameEventHistoryDelta* frameTimestampsOrNull =
358 enableFrameTimestamps ? &frameTimestamps : nullptr;
359
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700360 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700361 reqWidth, reqHeight, reqFormat, reqUsage, frameTimestampsOrNull);
Dan Stoza70ccba52016-07-01 14:00:40 -0700362 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800363
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800364 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700365 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
366 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
367 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800368 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700369 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800370
371 Mutex::Autolock lock(mMutex);
372
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800373 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700374
375 // this should never happen
376 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
377
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800378 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
379 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700380 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700381
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700382 if (enableFrameTimestamps) {
383 mFrameEventHistory.applyDelta(frameTimestamps);
384 }
385
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800386 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
387 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
388 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700389 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700390 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800391 return result;
392 }
393 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700394
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800395 if (fence->isValid()) {
396 *fenceFd = fence->dup();
397 if (*fenceFd == -1) {
398 ALOGE("dequeueBuffer: error duping fence: %d", errno);
399 // dup() should never fail; something is badly wrong. Soldier on
400 // and hope for the best; the worst that should happen is some
401 // visible corruption that lasts until the next frame.
402 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700403 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800404 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700405 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800406
407 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800408
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700409 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800410 mSharedBufferSlot = buf;
411 mSharedBufferHasBeenQueued = false;
412 } else if (mSharedBufferSlot == buf) {
413 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
414 mSharedBufferHasBeenQueued = false;
415 }
416
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800417 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700418}
419
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800420int Surface::cancelBuffer(android_native_buffer_t* buffer,
421 int fenceFd) {
422 ATRACE_CALL();
423 ALOGV("Surface::cancelBuffer");
424 Mutex::Autolock lock(mMutex);
425 int i = getSlotFromBufferLocked(buffer);
426 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900427 if (fenceFd >= 0) {
428 close(fenceFd);
429 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800430 return i;
431 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800432 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
433 if (fenceFd >= 0) {
434 close(fenceFd);
435 }
436 return OK;
437 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800438 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
439 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800440
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700441 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800442 mSharedBufferHasBeenQueued = true;
443 }
444
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800445 return OK;
446}
447
448int Surface::getSlotFromBufferLocked(
449 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800450 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
451 if (mSlots[i].buffer != NULL &&
452 mSlots[i].buffer->handle == buffer->handle) {
453 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700454 }
455 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800456 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
457 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700458}
459
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800460int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800461 ALOGV("Surface::lockBuffer");
462 Mutex::Autolock lock(mMutex);
463 return OK;
464}
Mathias Agopian631f3582010-05-25 17:51:34 -0700465
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800466int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
467 ATRACE_CALL();
468 ALOGV("Surface::queueBuffer");
469 Mutex::Autolock lock(mMutex);
470 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700471 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800472
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800473 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700474 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700475 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700476 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700477 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800478 } else {
479 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700480 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800481 int i = getSlotFromBufferLocked(buffer);
482 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900483 if (fenceFd >= 0) {
484 close(fenceFd);
485 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800486 return i;
487 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800488 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
489 if (fenceFd >= 0) {
490 close(fenceFd);
491 }
492 return OK;
493 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800496 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700497 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800498 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800500 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
501 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700502 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800503 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700504 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700505
Dan Stozac62acbd2015-04-21 16:42:49 -0700506 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700507 input.setSurfaceDamage(Region::INVALID_REGION);
508 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700509 // Here we do two things:
510 // 1) The surface damage was specified using the OpenGL ES convention of
511 // the origin being in the bottom-left corner. Here we flip to the
512 // convention that the rest of the system uses (top-left corner) by
513 // subtracting all top/bottom coordinates from the buffer height.
514 // 2) If the buffer is coming in rotated (for example, because the EGL
515 // implementation is reacting to the transform hint coming back from
516 // SurfaceFlinger), the surface damage needs to be rotated the
517 // opposite direction, since it was generated assuming an unrotated
518 // buffer (the app doesn't know that the EGL implementation is
519 // reacting to the transform hint behind its back). The
520 // transformations in the switch statement below apply those
521 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
522
523 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700524 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700525 bool rotated90 = (mTransform ^ mStickyTransform) &
526 NATIVE_WINDOW_TRANSFORM_ROT_90;
527 if (rotated90) {
528 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700529 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700530
Dan Stoza5065a552015-03-17 16:23:42 -0700531 Region flippedRegion;
532 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700533 int left = rect.left;
534 int right = rect.right;
535 int top = height - rect.bottom; // Flip from OpenGL convention
536 int bottom = height - rect.top; // Flip from OpenGL convention
537 switch (mTransform ^ mStickyTransform) {
538 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
539 // Rotate 270 degrees
540 Rect flippedRect{top, width - right, bottom, width - left};
541 flippedRegion.orSelf(flippedRect);
542 break;
543 }
544 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
545 // Rotate 180 degrees
546 Rect flippedRect{width - right, height - bottom,
547 width - left, height - top};
548 flippedRegion.orSelf(flippedRect);
549 break;
550 }
551 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
552 // Rotate 90 degrees
553 Rect flippedRect{height - bottom, left,
554 height - top, right};
555 flippedRegion.orSelf(flippedRect);
556 break;
557 }
558 default: {
559 Rect flippedRect{left, top, right, bottom};
560 flippedRegion.orSelf(flippedRect);
561 break;
562 }
563 }
Dan Stoza5065a552015-03-17 16:23:42 -0700564 }
565
566 input.setSurfaceDamage(flippedRegion);
567 }
568
Dan Stoza70ccba52016-07-01 14:00:40 -0700569 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800570 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700571 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800572 if (err != OK) {
573 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
574 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800575
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700576 if (mEnableFrameTimestamps) {
577 mFrameEventHistory.applyDelta(output.frameTimestamps);
578 // Update timestamps with the local acquire fence.
579 // The consumer doesn't send it back to prevent us from having two
580 // file descriptors of the same fence.
Brian Anderson3d4039d2016-09-23 16:31:30 -0700581 mFrameEventHistory.updateAcquireFence(mNextFrameNumber,
582 std::make_shared<FenceTime>(std::move(fence)));
583
584 // Cache timestamps of signaled fences so we can close their file
585 // descriptors.
586 mFrameEventHistory.updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700587 }
588
589 mDefaultWidth = output.width;
590 mDefaultHeight = output.height;
591 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700592
Ruben Brunk1681d952014-06-27 15:51:55 -0700593 // Disable transform hint if sticky transform is set.
594 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700595 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700596 }
597
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700598 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700599
Dan Stozac62acbd2015-04-21 16:42:49 -0700600 if (!mConnectedToCpu) {
601 // Clear surface damage back to full-buffer
602 mDirtyRegion = Region::INVALID_REGION;
603 }
Dan Stoza5065a552015-03-17 16:23:42 -0700604
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700605 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800606 mSharedBufferHasBeenQueued = true;
607 }
608
Robert Carr9f31e292016-04-11 11:15:32 -0700609 mQueueBufferCondition.broadcast();
610
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800611 return err;
612}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700613
Brian Anderson069b3652016-07-22 10:32:47 -0700614void Surface::querySupportedTimestampsLocked() const {
615 // mMutex must be locked when calling this method.
616
617 if (mQueriedSupportedTimestamps) {
618 return;
619 }
620 mQueriedSupportedTimestamps = true;
621
Brian Anderson3890c392016-07-25 12:48:08 -0700622 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson069b3652016-07-22 10:32:47 -0700623 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
624 status_t err = composer->getSupportedFrameTimestamps(
625 &supportedFrameTimestamps);
626
627 if (err != NO_ERROR) {
628 return;
629 }
630
631 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700632 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700633 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700634 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700635 mFrameTimestampsSupportsRetire = true;
636 }
637 }
638}
639
Mathias Agopiana67932f2011-04-20 14:20:59 -0700640int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800641 ATRACE_CALL();
642 ALOGV("Surface::query");
643 { // scope for the lock
644 Mutex::Autolock lock(mMutex);
645 switch (what) {
646 case NATIVE_WINDOW_FORMAT:
647 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800648 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800649 return NO_ERROR;
650 }
651 break;
652 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
653 sp<ISurfaceComposer> composer(
654 ComposerService::getComposerService());
655 if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) {
656 *value = 1;
657 } else {
658 *value = 0;
659 }
660 return NO_ERROR;
661 }
662 case NATIVE_WINDOW_CONCRETE_TYPE:
663 *value = NATIVE_WINDOW_SURFACE;
664 return NO_ERROR;
665 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800666 *value = static_cast<int>(
667 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800668 return NO_ERROR;
669 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800670 *value = static_cast<int>(
671 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800672 return NO_ERROR;
673 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800674 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800675 return NO_ERROR;
676 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
677 status_t err = NO_ERROR;
678 if (!mConsumerRunningBehind) {
679 *value = 0;
680 } else {
681 err = mGraphicBufferProducer->query(what, value);
682 if (err == NO_ERROR) {
683 mConsumerRunningBehind = *value;
684 }
685 }
686 return err;
687 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700688 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
689 int64_t durationUs = mLastDequeueDuration / 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 }
695 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
696 int64_t durationUs = mLastQueueDuration / 1000;
697 *value = durationUs > std::numeric_limits<int>::max() ?
698 std::numeric_limits<int>::max() :
699 static_cast<int>(durationUs);
700 return NO_ERROR;
701 }
Brian Anderson069b3652016-07-22 10:32:47 -0700702 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
703 querySupportedTimestampsLocked();
704 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
705 return NO_ERROR;
706 }
707 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
708 querySupportedTimestampsLocked();
709 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
710 return NO_ERROR;
711 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800712 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700713 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800714 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800715}
716
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800717int Surface::perform(int operation, va_list args)
718{
719 int res = NO_ERROR;
720 switch (operation) {
721 case NATIVE_WINDOW_CONNECT:
722 // deprecated. must return NO_ERROR.
723 break;
724 case NATIVE_WINDOW_DISCONNECT:
725 // deprecated. must return NO_ERROR.
726 break;
727 case NATIVE_WINDOW_SET_USAGE:
728 res = dispatchSetUsage(args);
729 break;
730 case NATIVE_WINDOW_SET_CROP:
731 res = dispatchSetCrop(args);
732 break;
733 case NATIVE_WINDOW_SET_BUFFER_COUNT:
734 res = dispatchSetBufferCount(args);
735 break;
736 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
737 res = dispatchSetBuffersGeometry(args);
738 break;
739 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
740 res = dispatchSetBuffersTransform(args);
741 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700742 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
743 res = dispatchSetBuffersStickyTransform(args);
744 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800745 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
746 res = dispatchSetBuffersTimestamp(args);
747 break;
748 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
749 res = dispatchSetBuffersDimensions(args);
750 break;
751 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
752 res = dispatchSetBuffersUserDimensions(args);
753 break;
754 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
755 res = dispatchSetBuffersFormat(args);
756 break;
757 case NATIVE_WINDOW_LOCK:
758 res = dispatchLock(args);
759 break;
760 case NATIVE_WINDOW_UNLOCK_AND_POST:
761 res = dispatchUnlockAndPost(args);
762 break;
763 case NATIVE_WINDOW_SET_SCALING_MODE:
764 res = dispatchSetScalingMode(args);
765 break;
766 case NATIVE_WINDOW_API_CONNECT:
767 res = dispatchConnect(args);
768 break;
769 case NATIVE_WINDOW_API_DISCONNECT:
770 res = dispatchDisconnect(args);
771 break;
Rachad7cb0d392014-07-29 17:53:53 -0700772 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
773 res = dispatchSetSidebandStream(args);
774 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800775 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
776 res = dispatchSetBuffersDataSpace(args);
777 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700778 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
779 res = dispatchSetSurfaceDamage(args);
780 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700781 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
782 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700783 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800784 case NATIVE_WINDOW_SET_AUTO_REFRESH:
785 res = dispatchSetAutoRefresh(args);
786 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700787 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
788 res = dispatchEnableFrameTimestamps(args);
789 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800790 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
791 res = dispatchGetFrameTimestamps(args);
792 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800793 default:
794 res = NAME_NOT_FOUND;
795 break;
796 }
797 return res;
798}
Mathias Agopiana138f892010-05-21 17:24:35 -0700799
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800800int Surface::dispatchConnect(va_list args) {
801 int api = va_arg(args, int);
802 return connect(api);
803}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800804
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800805int Surface::dispatchDisconnect(va_list args) {
806 int api = va_arg(args, int);
807 return disconnect(api);
808}
809
810int Surface::dispatchSetUsage(va_list args) {
811 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800812 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800813}
814
815int Surface::dispatchSetCrop(va_list args) {
816 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
817 return setCrop(reinterpret_cast<Rect const*>(rect));
818}
819
820int Surface::dispatchSetBufferCount(va_list args) {
821 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800822 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800823}
824
825int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800826 uint32_t width = va_arg(args, uint32_t);
827 uint32_t height = va_arg(args, uint32_t);
828 PixelFormat format = va_arg(args, PixelFormat);
829 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800830 if (err != 0) {
831 return err;
832 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800833 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800834}
835
836int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800837 uint32_t width = va_arg(args, uint32_t);
838 uint32_t height = va_arg(args, uint32_t);
839 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800840}
841
842int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800843 uint32_t width = va_arg(args, uint32_t);
844 uint32_t height = va_arg(args, uint32_t);
845 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800846}
847
848int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800849 PixelFormat format = va_arg(args, PixelFormat);
850 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800851}
852
853int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800854 int mode = va_arg(args, int);
855 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800856}
857
858int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800859 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800860 return setBuffersTransform(transform);
861}
862
Ruben Brunk1681d952014-06-27 15:51:55 -0700863int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800864 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700865 return setBuffersStickyTransform(transform);
866}
867
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800868int Surface::dispatchSetBuffersTimestamp(va_list args) {
869 int64_t timestamp = va_arg(args, int64_t);
870 return setBuffersTimestamp(timestamp);
871}
872
873int Surface::dispatchLock(va_list args) {
874 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
875 ARect* inOutDirtyBounds = va_arg(args, ARect*);
876 return lock(outBuffer, inOutDirtyBounds);
877}
878
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800879int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800880 return unlockAndPost();
881}
882
Rachad7cb0d392014-07-29 17:53:53 -0700883int Surface::dispatchSetSidebandStream(va_list args) {
884 native_handle_t* sH = va_arg(args, native_handle_t*);
885 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
886 setSidebandStream(sidebandHandle);
887 return OK;
888}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800889
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800890int Surface::dispatchSetBuffersDataSpace(va_list args) {
891 android_dataspace dataspace =
892 static_cast<android_dataspace>(va_arg(args, int));
893 return setBuffersDataSpace(dataspace);
894}
895
Dan Stoza5065a552015-03-17 16:23:42 -0700896int Surface::dispatchSetSurfaceDamage(va_list args) {
897 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
898 size_t numRects = va_arg(args, size_t);
899 setSurfaceDamage(rects, numRects);
900 return NO_ERROR;
901}
902
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700903int Surface::dispatchSetSharedBufferMode(va_list args) {
904 bool sharedBufferMode = va_arg(args, int);
905 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800906}
907
908int Surface::dispatchSetAutoRefresh(va_list args) {
909 bool autoRefresh = va_arg(args, int);
910 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700911}
912
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700913int Surface::dispatchEnableFrameTimestamps(va_list args) {
914 bool enable = va_arg(args, int);
915 enableFrameTimestamps(enable);
916 return NO_ERROR;
917}
918
Pablo Ceballosce796e72016-02-04 19:10:51 -0800919int Surface::dispatchGetFrameTimestamps(va_list args) {
920 uint32_t framesAgo = va_arg(args, uint32_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -0700921 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800922 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
923 nsecs_t* outRefreshStartTime = va_arg(args, int64_t*);
924 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700925 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800926 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
927 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700928 return getFrameTimestamps(getNextFrameNumber() - 1 - framesAgo,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700929 outRequestedPresentTime, outAcquireTime, outRefreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700930 outGlCompositionDoneTime, outDisplayPresentTime,
931 outDisplayRetireTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800932}
933
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800934int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -0800935 static sp<IProducerListener> listener = new DummyProducerListener();
936 return connect(api, listener);
937}
938
939int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800940 ATRACE_CALL();
941 ALOGV("Surface::connect");
942 Mutex::Autolock lock(mMutex);
943 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700944 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800945 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700946 mDefaultWidth = output.width;
947 mDefaultHeight = output.height;
948 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -0700949
950 // Disable transform hint if sticky transform is set.
951 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700952 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700953 }
954
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700955 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800956 }
957 if (!err && api == NATIVE_WINDOW_API_CPU) {
958 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -0700959 // Clear the dirty region in case we're switching from a non-CPU API
960 mDirtyRegion.clear();
961 } else if (!err) {
962 // Initialize the dirty region for tracking surface damage
963 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800964 }
Dan Stoza5065a552015-03-17 16:23:42 -0700965
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800966 return err;
967}
968
Mathias Agopian365857d2013-09-11 19:35:45 -0700969
Robert Carr97b9c862016-09-08 13:54:35 -0700970int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800971 ATRACE_CALL();
972 ALOGV("Surface::disconnect");
973 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800974 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
975 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800976 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -0700977 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800978 if (!err) {
979 mReqFormat = 0;
980 mReqWidth = 0;
981 mReqHeight = 0;
982 mReqUsage = 0;
983 mCrop.clear();
984 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
985 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -0700986 mStickyTransform = 0;
987
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800988 if (api == NATIVE_WINDOW_API_CPU) {
989 mConnectedToCpu = false;
990 }
991 }
992 return err;
993}
994
Dan Stozad9c49712015-04-27 11:06:01 -0700995int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -0700996 sp<Fence>* outFence) {
997 ATRACE_CALL();
998 ALOGV("Surface::detachNextBuffer");
999
1000 if (outBuffer == NULL || outFence == NULL) {
1001 return BAD_VALUE;
1002 }
1003
1004 Mutex::Autolock lock(mMutex);
1005
1006 sp<GraphicBuffer> buffer(NULL);
1007 sp<Fence> fence(NULL);
1008 status_t result = mGraphicBufferProducer->detachNextBuffer(
1009 &buffer, &fence);
1010 if (result != NO_ERROR) {
1011 return result;
1012 }
1013
Dan Stozad9c49712015-04-27 11:06:01 -07001014 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001015 if (fence != NULL && fence->isValid()) {
1016 *outFence = fence;
1017 } else {
1018 *outFence = Fence::NO_FENCE;
1019 }
1020
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001021 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1022 if (mSlots[i].buffer != NULL &&
1023 mSlots[i].buffer->handle == buffer->handle) {
1024 mSlots[i].buffer = NULL;
1025 }
1026 }
1027
Dan Stoza231832e2015-03-11 11:55:01 -07001028 return NO_ERROR;
1029}
1030
1031int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1032{
1033 ATRACE_CALL();
1034 ALOGV("Surface::attachBuffer");
1035
1036 Mutex::Autolock lock(mMutex);
1037
1038 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001039 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1040 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001041 int32_t attachedSlot = -1;
1042 status_t result = mGraphicBufferProducer->attachBuffer(
1043 &attachedSlot, graphicBuffer);
1044 if (result != NO_ERROR) {
1045 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001046 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001047 return result;
1048 }
1049 mSlots[attachedSlot].buffer = graphicBuffer;
1050
1051 return NO_ERROR;
1052}
1053
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001054int Surface::setUsage(uint32_t reqUsage)
1055{
1056 ALOGV("Surface::setUsage");
1057 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001058 if (reqUsage != mReqUsage) {
1059 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1060 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001061 mReqUsage = reqUsage;
1062 return OK;
1063}
1064
1065int Surface::setCrop(Rect const* rect)
1066{
1067 ATRACE_CALL();
1068
Pablo Ceballos60d69222015-08-07 14:47:20 -07001069 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001070 if (rect == NULL || rect->isEmpty()) {
1071 realRect.clear();
1072 } else {
1073 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001074 }
1075
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001076 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1077 realRect.left, realRect.top, realRect.right, realRect.bottom);
1078
1079 Mutex::Autolock lock(mMutex);
1080 mCrop = realRect;
1081 return NO_ERROR;
1082}
1083
1084int Surface::setBufferCount(int bufferCount)
1085{
1086 ATRACE_CALL();
1087 ALOGV("Surface::setBufferCount");
1088 Mutex::Autolock lock(mMutex);
1089
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001090 status_t err = NO_ERROR;
1091 if (bufferCount == 0) {
1092 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1093 } else {
1094 int minUndequeuedBuffers = 0;
1095 err = mGraphicBufferProducer->query(
1096 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1097 if (err == NO_ERROR) {
1098 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1099 bufferCount - minUndequeuedBuffers);
1100 }
1101 }
Mathias Agopian90147262010-01-22 11:47:55 -08001102
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001103 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1104 bufferCount, strerror(-err));
1105
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001106 return err;
1107}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001108
Pablo Ceballosfa455352015-08-12 17:47:47 -07001109int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1110 ATRACE_CALL();
1111 ALOGV("Surface::setMaxDequeuedBufferCount");
1112 Mutex::Autolock lock(mMutex);
1113
1114 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1115 maxDequeuedBuffers);
1116 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1117 "returned %s", maxDequeuedBuffers, strerror(-err));
1118
Pablo Ceballosfa455352015-08-12 17:47:47 -07001119 return err;
1120}
1121
1122int Surface::setAsyncMode(bool async) {
1123 ATRACE_CALL();
1124 ALOGV("Surface::setAsyncMode");
1125 Mutex::Autolock lock(mMutex);
1126
1127 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1128 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1129 async, strerror(-err));
1130
Pablo Ceballosfa455352015-08-12 17:47:47 -07001131 return err;
1132}
1133
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001134int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001135 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001136 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001137 Mutex::Autolock lock(mMutex);
1138
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001139 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1140 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001141 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001142 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001143 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001144 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1145 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001146
1147 return err;
1148}
1149
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001150int Surface::setAutoRefresh(bool autoRefresh) {
1151 ATRACE_CALL();
1152 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1153 Mutex::Autolock lock(mMutex);
1154
1155 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1156 if (err == NO_ERROR) {
1157 mAutoRefresh = autoRefresh;
1158 }
1159 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1160 autoRefresh, strerror(-err));
1161 return err;
1162}
1163
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001164int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001165{
1166 ATRACE_CALL();
1167 ALOGV("Surface::setBuffersDimensions");
1168
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001169 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001170 return BAD_VALUE;
1171
1172 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001173 if (width != mReqWidth || height != mReqHeight) {
1174 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1175 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001176 mReqWidth = width;
1177 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001178 return NO_ERROR;
1179}
1180
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001181int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001182{
1183 ATRACE_CALL();
1184 ALOGV("Surface::setBuffersUserDimensions");
1185
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001186 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001187 return BAD_VALUE;
1188
1189 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001190 if (width != mUserWidth || height != mUserHeight) {
1191 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1192 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001193 mUserWidth = width;
1194 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001195 return NO_ERROR;
1196}
1197
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001198int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001199{
1200 ALOGV("Surface::setBuffersFormat");
1201
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001202 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001203 if (format != mReqFormat) {
1204 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1205 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001206 mReqFormat = format;
1207 return NO_ERROR;
1208}
1209
1210int Surface::setScalingMode(int mode)
1211{
1212 ATRACE_CALL();
1213 ALOGV("Surface::setScalingMode(%d)", mode);
1214
1215 switch (mode) {
1216 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1217 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1218 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001219 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001220 break;
1221 default:
1222 ALOGE("unknown scaling mode: %d", mode);
1223 return BAD_VALUE;
1224 }
1225
1226 Mutex::Autolock lock(mMutex);
1227 mScalingMode = mode;
1228 return NO_ERROR;
1229}
1230
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001231int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001232{
1233 ATRACE_CALL();
1234 ALOGV("Surface::setBuffersTransform");
1235 Mutex::Autolock lock(mMutex);
1236 mTransform = transform;
1237 return NO_ERROR;
1238}
1239
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001240int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001241{
1242 ATRACE_CALL();
1243 ALOGV("Surface::setBuffersStickyTransform");
1244 Mutex::Autolock lock(mMutex);
1245 mStickyTransform = transform;
1246 return NO_ERROR;
1247}
1248
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001249int Surface::setBuffersTimestamp(int64_t timestamp)
1250{
1251 ALOGV("Surface::setBuffersTimestamp");
1252 Mutex::Autolock lock(mMutex);
1253 mTimestamp = timestamp;
1254 return NO_ERROR;
1255}
1256
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001257int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1258{
1259 ALOGV("Surface::setBuffersDataSpace");
1260 Mutex::Autolock lock(mMutex);
1261 mDataSpace = dataSpace;
1262 return NO_ERROR;
1263}
1264
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001265void Surface::freeAllBuffers() {
1266 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1267 mSlots[i].buffer = 0;
1268 }
1269}
1270
Dan Stoza5065a552015-03-17 16:23:42 -07001271void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1272 ATRACE_CALL();
1273 ALOGV("Surface::setSurfaceDamage");
1274 Mutex::Autolock lock(mMutex);
1275
Dan Stozac62acbd2015-04-21 16:42:49 -07001276 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001277 mDirtyRegion = Region::INVALID_REGION;
1278 return;
1279 }
1280
1281 mDirtyRegion.clear();
1282 for (size_t r = 0; r < numRects; ++r) {
1283 // We intentionally flip top and bottom here, since because they're
1284 // specified with a bottom-left origin, top > bottom, which fails
1285 // validation in the Region class. We will fix this up when we flip to a
1286 // top-left origin in queueBuffer.
1287 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1288 mDirtyRegion.orSelf(rect);
1289 }
1290}
1291
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001292// ----------------------------------------------------------------------
1293// the lock/unlock APIs must be used from the same thread
1294
1295static status_t copyBlt(
1296 const sp<GraphicBuffer>& dst,
1297 const sp<GraphicBuffer>& src,
1298 const Region& reg)
1299{
1300 // src and dst with, height and format must be identical. no verification
1301 // is done here.
1302 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001303 uint8_t* src_bits = NULL;
1304 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1305 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001306 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1307
1308 uint8_t* dst_bits = NULL;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001309 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1310 reinterpret_cast<void**>(&dst_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001311 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
1312
1313 Region::const_iterator head(reg.begin());
1314 Region::const_iterator tail(reg.end());
1315 if (head != tail && src_bits && dst_bits) {
1316 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001317 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1318 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001319
1320 while (head != tail) {
1321 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001322 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001323 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001324 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1325 uint8_t const * s = src_bits +
1326 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1327 uint8_t * d = dst_bits +
1328 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001329 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001330 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001331 h = 1;
1332 }
1333 do {
1334 memcpy(d, s, size);
1335 d += dbpr;
1336 s += sbpr;
1337 } while (--h > 0);
1338 }
1339 }
1340
1341 if (src_bits)
1342 src->unlock();
1343
1344 if (dst_bits)
1345 dst->unlock();
1346
1347 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348}
1349
Mathias Agopiana138f892010-05-21 17:24:35 -07001350// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001351
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001352status_t Surface::lock(
1353 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1354{
1355 if (mLockedBuffer != 0) {
1356 ALOGE("Surface::lock failed, already locked");
1357 return INVALID_OPERATION;
1358 }
1359
1360 if (!mConnectedToCpu) {
1361 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1362 if (err) {
1363 return err;
1364 }
1365 // we're intending to do software rendering from this point
1366 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1367 }
1368
1369 ANativeWindowBuffer* out;
1370 int fenceFd = -1;
1371 status_t err = dequeueBuffer(&out, &fenceFd);
1372 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1373 if (err == NO_ERROR) {
1374 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001375 const Rect bounds(backBuffer->width, backBuffer->height);
1376
1377 Region newDirtyRegion;
1378 if (inOutDirtyBounds) {
1379 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1380 newDirtyRegion.andSelf(bounds);
1381 } else {
1382 newDirtyRegion.set(bounds);
1383 }
1384
1385 // figure out if we can copy the frontbuffer back
1386 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1387 const bool canCopyBack = (frontBuffer != 0 &&
1388 backBuffer->width == frontBuffer->width &&
1389 backBuffer->height == frontBuffer->height &&
1390 backBuffer->format == frontBuffer->format);
1391
1392 if (canCopyBack) {
1393 // copy the area that is invalid and not repainted this round
1394 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001395 if (!copyback.isEmpty()) {
1396 if (fenceFd >= 0) {
1397 sync_wait(fenceFd, -1);
1398 close(fenceFd);
1399 fenceFd = -1;
1400 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001401 copyBlt(backBuffer, frontBuffer, copyback);
Francis Hartdc10f842014-12-01 16:04:49 +02001402 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001403 } else {
1404 // if we can't copy-back anything, modify the user's dirty
1405 // region to make sure they redraw the whole buffer
1406 newDirtyRegion.set(bounds);
1407 mDirtyRegion.clear();
1408 Mutex::Autolock lock(mMutex);
1409 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1410 mSlots[i].dirtyRegion.clear();
1411 }
1412 }
1413
1414
1415 { // scope for the lock
1416 Mutex::Autolock lock(mMutex);
1417 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1418 if (backBufferSlot >= 0) {
1419 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1420 mDirtyRegion.subtract(dirtyRegion);
1421 dirtyRegion = newDirtyRegion;
1422 }
1423 }
1424
1425 mDirtyRegion.orSelf(newDirtyRegion);
1426 if (inOutDirtyBounds) {
1427 *inOutDirtyBounds = newDirtyRegion.getBounds();
1428 }
1429
1430 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001431 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001432 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001433 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001434
1435 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1436 backBuffer->handle);
1437
1438 if (res != 0) {
1439 err = INVALID_OPERATION;
1440 } else {
1441 mLockedBuffer = backBuffer;
1442 outBuffer->width = backBuffer->width;
1443 outBuffer->height = backBuffer->height;
1444 outBuffer->stride = backBuffer->stride;
1445 outBuffer->format = backBuffer->format;
1446 outBuffer->bits = vaddr;
1447 }
1448 }
1449 return err;
1450}
1451
1452status_t Surface::unlockAndPost()
1453{
1454 if (mLockedBuffer == 0) {
1455 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1456 return INVALID_OPERATION;
1457 }
1458
Francis Hart8f396012014-04-01 15:30:53 +03001459 int fd = -1;
1460 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001461 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1462
Francis Hart8f396012014-04-01 15:30:53 +03001463 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001464 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1465 mLockedBuffer->handle, strerror(-err));
1466
1467 mPostedBuffer = mLockedBuffer;
1468 mLockedBuffer = 0;
1469 return err;
1470}
1471
Robert Carr9f31e292016-04-11 11:15:32 -07001472bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1473 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001474 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001475 return true;
1476 }
1477 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1478}
1479
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001480status_t Surface::getUniqueId(uint64_t* outId) const {
1481 Mutex::Autolock lock(mMutex);
1482 return mGraphicBufferProducer->getUniqueId(outId);
1483}
1484
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001485namespace view {
1486
1487status_t Surface::writeToParcel(Parcel* parcel) const {
1488 return writeToParcel(parcel, false);
1489}
1490
1491status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const {
1492 if (parcel == nullptr) return BAD_VALUE;
1493
1494 status_t res = OK;
1495
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001496 if (!nameAlreadyWritten) {
1497 res = parcel->writeString16(name);
1498 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001499
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001500 /* isSingleBuffered defaults to no */
1501 res = parcel->writeInt32(0);
1502 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001503 }
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001504
1505 res = parcel->writeStrongBinder(
1506 IGraphicBufferProducer::asBinder(graphicBufferProducer));
1507
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001508 return res;
1509}
1510
1511status_t Surface::readFromParcel(const Parcel* parcel) {
1512 return readFromParcel(parcel, false);
1513}
1514
1515status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) {
1516 if (parcel == nullptr) return BAD_VALUE;
1517
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001518 status_t res = OK;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001519 if (!nameAlreadyRead) {
1520 name = readMaybeEmptyString16(parcel);
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001521 // Discard this for now
1522 int isSingleBuffered;
1523 res = parcel->readInt32(&isSingleBuffered);
1524 if (res != OK) {
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001525 ALOGE("Can't read isSingleBuffered");
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001526 return res;
1527 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001528 }
1529
1530 sp<IBinder> binder;
1531
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001532 res = parcel->readNullableStrongBinder(&binder);
1533 if (res != OK) {
1534 ALOGE("%s: Can't read strong binder", __FUNCTION__);
1535 return res;
1536 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001537
1538 graphicBufferProducer = interface_cast<IGraphicBufferProducer>(binder);
1539
1540 return OK;
1541}
1542
1543String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
1544 size_t len;
1545 const char16_t* str = parcel->readString16Inplace(&len);
1546 if (str != nullptr) {
1547 return String16(str, len);
1548 } else {
1549 return String16();
1550 }
1551}
1552
1553} // namespace view
1554
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001555}; // namespace android