blob: b250f5d4b72a1cf8d758446fc95b5bcc77e93986 [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>
Ian Elliott62c48c92017-01-20 13:13:20 -070031#include <ui/DisplayStatInfo.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070032
Dan Stozaf0eaf252014-03-21 13:05:51 -070033#include <gui/IProducerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080034#include <gui/ISurfaceComposer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080035#include <gui/SurfaceComposerClient.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080036#include <gui/GLConsumer.h>
37#include <gui/Surface.h>
38
39#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070040
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041namespace android {
42
Mathias Agopiane3c697f2013-02-14 17:11:02 -080043Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070044 const sp<IGraphicBufferProducer>& bufferProducer,
45 bool controlledByApp)
Dan Stoza812ed062015-06-02 15:45:22 -070046 : mGraphicBufferProducer(bufferProducer),
Pablo Ceballos60d69222015-08-07 14:47:20 -070047 mCrop(Rect::EMPTY_RECT),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080048 mGenerationNumber(0),
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070049 mSharedBufferMode(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080050 mAutoRefresh(false),
51 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
Pablo Ceballosbc8c1922016-07-01 14:15:41 -070052 mSharedBufferHasBeenQueued(false),
Brian Anderson069b3652016-07-22 10:32:47 -070053 mQueriedSupportedTimestamps(false),
54 mFrameTimestampsSupportsPresent(false),
Brian Anderson7c3ba8a2016-07-25 12:48:08 -070055 mFrameTimestampsSupportsRetire(false),
Brian Anderson3da8d272016-07-28 16:20:47 -070056 mEnableFrameTimestamps(false),
57 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>())
Mathias Agopian62185b72009-04-16 16:19:50 -070058{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080059 // Initialize the ANativeWindow function pointers.
60 ANativeWindow::setSwapInterval = hook_setSwapInterval;
61 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
62 ANativeWindow::cancelBuffer = hook_cancelBuffer;
63 ANativeWindow::queueBuffer = hook_queueBuffer;
64 ANativeWindow::query = hook_query;
65 ANativeWindow::perform = hook_perform;
66
67 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
68 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
69 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
70 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
71
72 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
73 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
74
75 mReqWidth = 0;
76 mReqHeight = 0;
77 mReqFormat = 0;
78 mReqUsage = 0;
79 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080080 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080081 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
82 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070083 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080084 mDefaultWidth = 0;
85 mDefaultHeight = 0;
86 mUserWidth = 0;
87 mUserHeight = 0;
88 mTransformHint = 0;
89 mConsumerRunningBehind = false;
90 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070091 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070092 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070093}
94
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070095Surface::~Surface() {
96 if (mConnectedToCpu) {
97 Surface::disconnect(NATIVE_WINDOW_API_CPU);
98 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -080099}
100
Brian Anderson3da8d272016-07-28 16:20:47 -0700101sp<ISurfaceComposer> Surface::composerService() const {
102 return ComposerService::getComposerService();
103}
104
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800105nsecs_t Surface::now() const {
106 return systemTime();
107}
108
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800109sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
110 return mGraphicBufferProducer;
111}
112
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900113void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
114 mGraphicBufferProducer->setSidebandStream(stream);
115}
116
Dan Stoza29a3e902014-06-20 13:13:57 -0700117void Surface::allocateBuffers() {
118 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
119 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700120 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
121 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700122}
123
Dan Stoza812ed062015-06-02 15:45:22 -0700124status_t Surface::setGenerationNumber(uint32_t generation) {
125 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
126 if (result == NO_ERROR) {
127 mGenerationNumber = generation;
128 }
129 return result;
130}
131
Dan Stoza7dde5992015-05-22 09:51:44 -0700132uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700133 Mutex::Autolock lock(mMutex);
134 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700135}
136
Dan Stozac6f30bd2015-06-08 09:32:50 -0700137String8 Surface::getConsumerName() const {
138 return mGraphicBufferProducer->getConsumerName();
139}
140
Dan Stoza127fc632015-06-30 13:43:32 -0700141status_t Surface::setDequeueTimeout(nsecs_t timeout) {
142 return mGraphicBufferProducer->setDequeueTimeout(timeout);
143}
144
Dan Stoza50101d02016-04-07 16:53:23 -0700145status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700146 sp<Fence>* outFence, float outTransformMatrix[16]) {
147 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
148 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700149}
150
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800151status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
152 ATRACE_CALL();
153
154 DisplayStatInfo stats;
155 status_t err = composerService()->getDisplayStats(NULL, &stats);
156
157 *outRefreshDuration = stats.vsyncPeriod;
158
159 return NO_ERROR;
160}
161
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700162void Surface::enableFrameTimestamps(bool enable) {
163 Mutex::Autolock lock(mMutex);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800164 // If going from disabled to enabled, get the initial values for
165 // compositor and display timing.
166 if (!mEnableFrameTimestamps && enable) {
167 FrameEventHistoryDelta delta;
168 mGraphicBufferProducer->getFrameTimestamps(&delta);
169 mFrameEventHistory->applyDelta(delta);
170 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700171 mEnableFrameTimestamps = enable;
172}
173
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800174status_t Surface::getCompositorTiming(
175 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
176 nsecs_t* compositeToPresentLatency) {
177 Mutex::Autolock lock(mMutex);
178 if (!mEnableFrameTimestamps) {
179 return INVALID_OPERATION;
180 }
181
182 if (compositeDeadline != nullptr) {
183 *compositeDeadline =
184 mFrameEventHistory->getNextCompositeDeadline(now());
185 }
186 if (compositeInterval != nullptr) {
187 *compositeInterval = mFrameEventHistory->getCompositeInterval();
188 }
189 if (compositeToPresentLatency != nullptr) {
190 *compositeToPresentLatency =
191 mFrameEventHistory->getCompositeToPresentLatency();
192 }
193 return NO_ERROR;
194}
195
Brian Anderson50143b32016-09-30 14:01:24 -0700196static bool checkConsumerForUpdates(
197 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700198 const nsecs_t* outLatchTime,
199 const nsecs_t* outFirstRefreshStartTime,
200 const nsecs_t* outLastRefreshStartTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700201 const nsecs_t* outGlCompositionDoneTime,
202 const nsecs_t* outDisplayPresentTime,
203 const nsecs_t* outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700204 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700205 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700206 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
207 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700208 !e->hasFirstRefreshStartInfo();
209 bool checkForGlCompositionDone = (outGlCompositionDoneTime != nullptr) &&
210 !e->hasGpuCompositionDoneInfo();
211 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
212 !e->hasDisplayPresentInfo();
213
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700214 // LastRefreshStart, DisplayRetire, DequeueReady, and Release are never
215 // available for the last frame.
216 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
217 !e->hasLastRefreshStartInfo() &&
218 (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700219 bool checkForDisplayRetire = (outDisplayRetireTime != nullptr) &&
220 !e->hasDisplayRetireInfo() && (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700221 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
222 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700223 bool checkForRelease = (outReleaseTime != nullptr) &&
224 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
225
226 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700227 return checkForLatch || checkForFirstRefreshStart ||
228 checkForLastRefreshStart || checkForGlCompositionDone ||
229 checkForDisplayPresent || checkForDisplayRetire ||
230 checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700231}
232
Brian Anderson3d4039d2016-09-23 16:31:30 -0700233static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
234 if (dst != nullptr) {
235 *dst = Fence::isValidTimestamp(src) ? src : 0;
236 }
237}
238
239static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) {
240 if (dst != nullptr) {
241 nsecs_t signalTime = src->getSignalTime();
242 *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0;
243 }
244}
245
Brian Anderson069b3652016-07-22 10:32:47 -0700246status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700247 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700248 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
249 nsecs_t* outLastRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700250 nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700251 nsecs_t* outDequeueReadyTime, nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800252 ATRACE_CALL();
253
Brian Anderson3890c392016-07-25 12:48:08 -0700254 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700255
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700256 if (!mEnableFrameTimestamps) {
257 return INVALID_OPERATION;
258 }
259
Brian Anderson3890c392016-07-25 12:48:08 -0700260 // Verify the requested timestamps are supported.
261 querySupportedTimestampsLocked();
262 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
263 return BAD_VALUE;
264 }
265 if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
266 return BAD_VALUE;
Brian Anderson069b3652016-07-22 10:32:47 -0700267 }
268
Brian Anderson3da8d272016-07-28 16:20:47 -0700269 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700270 if (events == nullptr) {
271 // If the entry isn't available in the producer, it's definitely not
272 // available in the consumer.
273 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700274 }
275
Brian Anderson50143b32016-09-30 14:01:24 -0700276 // Update our cache of events if the requested events are not available.
277 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700278 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
279 outGlCompositionDoneTime, outDisplayPresentTime,
280 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700281 FrameEventHistoryDelta delta;
282 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700283 mFrameEventHistory->applyDelta(delta);
284 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700285 }
286
Brian Anderson3890c392016-07-25 12:48:08 -0700287 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700288 // The entry was available before the update, but was overwritten
289 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700290 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800291 }
Brian Anderson069b3652016-07-22 10:32:47 -0700292
Brian Anderson3d4039d2016-09-23 16:31:30 -0700293 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700294 getFrameTimestamp(outLatchTime, events->latchTime);
295 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
296 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
297 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700298
Brian Anderson3d4039d2016-09-23 16:31:30 -0700299 getFrameTimestampFence(outAcquireTime, events->acquireFence);
300 getFrameTimestampFence(
301 outGlCompositionDoneTime, events->gpuCompositionDoneFence);
302 getFrameTimestampFence(
303 outDisplayPresentTime, events->displayPresentFence);
304 getFrameTimestampFence(outDisplayRetireTime, events->displayRetireFence);
305 getFrameTimestampFence(outReleaseTime, events->releaseFence);
Brian Anderson069b3652016-07-22 10:32:47 -0700306
307 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800308}
309
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800310int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
311 Surface* c = getSelf(window);
312 return c->setSwapInterval(interval);
313}
314
315int Surface::hook_dequeueBuffer(ANativeWindow* window,
316 ANativeWindowBuffer** buffer, int* fenceFd) {
317 Surface* c = getSelf(window);
318 return c->dequeueBuffer(buffer, fenceFd);
319}
320
321int Surface::hook_cancelBuffer(ANativeWindow* window,
322 ANativeWindowBuffer* buffer, int fenceFd) {
323 Surface* c = getSelf(window);
324 return c->cancelBuffer(buffer, fenceFd);
325}
326
327int Surface::hook_queueBuffer(ANativeWindow* window,
328 ANativeWindowBuffer* buffer, int fenceFd) {
329 Surface* c = getSelf(window);
330 return c->queueBuffer(buffer, fenceFd);
331}
332
333int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
334 ANativeWindowBuffer** buffer) {
335 Surface* c = getSelf(window);
336 ANativeWindowBuffer* buf;
337 int fenceFd = -1;
338 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600339 if (result != OK) {
340 return result;
341 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800342 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700343 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800344 if (waitResult != OK) {
345 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
346 waitResult);
347 c->cancelBuffer(buf, -1);
348 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700349 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800350 *buffer = buf;
351 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700352}
353
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800354int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
355 ANativeWindowBuffer* buffer) {
356 Surface* c = getSelf(window);
357 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700358}
359
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800360int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
361 ANativeWindowBuffer* buffer) {
362 Surface* c = getSelf(window);
363 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700364}
365
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800366int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
367 ANativeWindowBuffer* buffer) {
368 Surface* c = getSelf(window);
369 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700370}
Mathias Agopian62185b72009-04-16 16:19:50 -0700371
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800372int Surface::hook_query(const ANativeWindow* window,
373 int what, int* value) {
374 const Surface* c = getSelf(window);
375 return c->query(what, value);
376}
377
378int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
379 va_list args;
380 va_start(args, operation);
381 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700382 int result = c->perform(operation, args);
383 va_end(args);
384 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800385}
386
387int Surface::setSwapInterval(int interval) {
388 ATRACE_CALL();
389 // EGL specification states:
390 // interval is silently clamped to minimum and maximum implementation
391 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800392
393 if (interval < minSwapInterval)
394 interval = minSwapInterval;
395
396 if (interval > maxSwapInterval)
397 interval = maxSwapInterval;
398
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700399 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700400 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800401
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700402 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800403}
404
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700405int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800406 ATRACE_CALL();
407 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800408
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800409 uint32_t reqWidth;
410 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800411 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800412 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700413 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800414
415 {
416 Mutex::Autolock lock(mMutex);
417
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800418 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
419 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800420
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800421 reqFormat = mReqFormat;
422 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800423
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700424 enableFrameTimestamps = mEnableFrameTimestamps;
425
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700426 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800427 BufferItem::INVALID_BUFFER_SLOT) {
428 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
429 if (gbuf != NULL) {
430 *buffer = gbuf.get();
431 *fenceFd = -1;
432 return OK;
433 }
434 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800435 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
436
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800437 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800438 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700439 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700440
441 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700442 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700443 reqWidth, reqHeight, reqFormat, reqUsage,
444 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700445 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800446
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800447 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700448 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
449 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
450 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800451 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700452 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800453
454 Mutex::Autolock lock(mMutex);
455
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800456 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700457
458 // this should never happen
459 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
460
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800461 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
462 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700463 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700464
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700465 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700466 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700467 }
468
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800469 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
470 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
471 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700472 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700473 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800474 return result;
475 }
476 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700477
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800478 if (fence->isValid()) {
479 *fenceFd = fence->dup();
480 if (*fenceFd == -1) {
481 ALOGE("dequeueBuffer: error duping fence: %d", errno);
482 // dup() should never fail; something is badly wrong. Soldier on
483 // and hope for the best; the worst that should happen is some
484 // visible corruption that lasts until the next frame.
485 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700486 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800487 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700488 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800489
490 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800491
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700492 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800493 mSharedBufferSlot = buf;
494 mSharedBufferHasBeenQueued = false;
495 } else if (mSharedBufferSlot == buf) {
496 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
497 mSharedBufferHasBeenQueued = false;
498 }
499
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800500 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700501}
502
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800503int Surface::cancelBuffer(android_native_buffer_t* buffer,
504 int fenceFd) {
505 ATRACE_CALL();
506 ALOGV("Surface::cancelBuffer");
507 Mutex::Autolock lock(mMutex);
508 int i = getSlotFromBufferLocked(buffer);
509 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900510 if (fenceFd >= 0) {
511 close(fenceFd);
512 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800513 return i;
514 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800515 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
516 if (fenceFd >= 0) {
517 close(fenceFd);
518 }
519 return OK;
520 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800521 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
522 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800523
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700524 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800525 mSharedBufferHasBeenQueued = true;
526 }
527
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800528 return OK;
529}
530
531int Surface::getSlotFromBufferLocked(
532 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800533 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
534 if (mSlots[i].buffer != NULL &&
535 mSlots[i].buffer->handle == buffer->handle) {
536 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700537 }
538 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800539 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
540 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700541}
542
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800543int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800544 ALOGV("Surface::lockBuffer");
545 Mutex::Autolock lock(mMutex);
546 return OK;
547}
Mathias Agopian631f3582010-05-25 17:51:34 -0700548
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800549int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
550 ATRACE_CALL();
551 ALOGV("Surface::queueBuffer");
552 Mutex::Autolock lock(mMutex);
553 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700554 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800555
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800556 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700557 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700558 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700559 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700560 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800561 } else {
562 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700563 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800564 int i = getSlotFromBufferLocked(buffer);
565 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900566 if (fenceFd >= 0) {
567 close(fenceFd);
568 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800569 return i;
570 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800571 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
572 if (fenceFd >= 0) {
573 close(fenceFd);
574 }
575 return OK;
576 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800579 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700580 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800581 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800583 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
584 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700585 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800586 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700587 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700588
Dan Stozac62acbd2015-04-21 16:42:49 -0700589 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700590 input.setSurfaceDamage(Region::INVALID_REGION);
591 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700592 // Here we do two things:
593 // 1) The surface damage was specified using the OpenGL ES convention of
594 // the origin being in the bottom-left corner. Here we flip to the
595 // convention that the rest of the system uses (top-left corner) by
596 // subtracting all top/bottom coordinates from the buffer height.
597 // 2) If the buffer is coming in rotated (for example, because the EGL
598 // implementation is reacting to the transform hint coming back from
599 // SurfaceFlinger), the surface damage needs to be rotated the
600 // opposite direction, since it was generated assuming an unrotated
601 // buffer (the app doesn't know that the EGL implementation is
602 // reacting to the transform hint behind its back). The
603 // transformations in the switch statement below apply those
604 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
605
606 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700607 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700608 bool rotated90 = (mTransform ^ mStickyTransform) &
609 NATIVE_WINDOW_TRANSFORM_ROT_90;
610 if (rotated90) {
611 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700612 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700613
Dan Stoza5065a552015-03-17 16:23:42 -0700614 Region flippedRegion;
615 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700616 int left = rect.left;
617 int right = rect.right;
618 int top = height - rect.bottom; // Flip from OpenGL convention
619 int bottom = height - rect.top; // Flip from OpenGL convention
620 switch (mTransform ^ mStickyTransform) {
621 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
622 // Rotate 270 degrees
623 Rect flippedRect{top, width - right, bottom, width - left};
624 flippedRegion.orSelf(flippedRect);
625 break;
626 }
627 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
628 // Rotate 180 degrees
629 Rect flippedRect{width - right, height - bottom,
630 width - left, height - top};
631 flippedRegion.orSelf(flippedRect);
632 break;
633 }
634 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
635 // Rotate 90 degrees
636 Rect flippedRect{height - bottom, left,
637 height - top, right};
638 flippedRegion.orSelf(flippedRect);
639 break;
640 }
641 default: {
642 Rect flippedRect{left, top, right, bottom};
643 flippedRegion.orSelf(flippedRect);
644 break;
645 }
646 }
Dan Stoza5065a552015-03-17 16:23:42 -0700647 }
648
649 input.setSurfaceDamage(flippedRegion);
650 }
651
Dan Stoza70ccba52016-07-01 14:00:40 -0700652 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800653 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700654 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800655 if (err != OK) {
656 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
657 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800658
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700659 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700660 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700661 // Update timestamps with the local acquire fence.
662 // The consumer doesn't send it back to prevent us from having two
663 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700664 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700665 std::make_shared<FenceTime>(std::move(fence)));
666
667 // Cache timestamps of signaled fences so we can close their file
668 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700669 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700670 }
671
Brian Anderson50143b32016-09-30 14:01:24 -0700672 mLastFrameNumber = mNextFrameNumber;
673
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700674 mDefaultWidth = output.width;
675 mDefaultHeight = output.height;
676 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700677
Ruben Brunk1681d952014-06-27 15:51:55 -0700678 // Disable transform hint if sticky transform is set.
679 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700680 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700681 }
682
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700683 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700684
Dan Stozac62acbd2015-04-21 16:42:49 -0700685 if (!mConnectedToCpu) {
686 // Clear surface damage back to full-buffer
687 mDirtyRegion = Region::INVALID_REGION;
688 }
Dan Stoza5065a552015-03-17 16:23:42 -0700689
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700690 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800691 mSharedBufferHasBeenQueued = true;
692 }
693
Robert Carr9f31e292016-04-11 11:15:32 -0700694 mQueueBufferCondition.broadcast();
695
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800696 return err;
697}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700698
Brian Anderson069b3652016-07-22 10:32:47 -0700699void Surface::querySupportedTimestampsLocked() const {
700 // mMutex must be locked when calling this method.
701
702 if (mQueriedSupportedTimestamps) {
703 return;
704 }
705 mQueriedSupportedTimestamps = true;
706
Brian Anderson3890c392016-07-25 12:48:08 -0700707 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson3da8d272016-07-28 16:20:47 -0700708 status_t err = composerService()->getSupportedFrameTimestamps(
Brian Anderson069b3652016-07-22 10:32:47 -0700709 &supportedFrameTimestamps);
710
711 if (err != NO_ERROR) {
712 return;
713 }
714
715 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700716 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700717 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700718 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700719 mFrameTimestampsSupportsRetire = true;
720 }
721 }
722}
723
Mathias Agopiana67932f2011-04-20 14:20:59 -0700724int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800725 ATRACE_CALL();
726 ALOGV("Surface::query");
727 { // scope for the lock
728 Mutex::Autolock lock(mMutex);
729 switch (what) {
730 case NATIVE_WINDOW_FORMAT:
731 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800732 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800733 return NO_ERROR;
734 }
735 break;
736 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700737 if (composerService()->authenticateSurfaceTexture(
738 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800739 *value = 1;
740 } else {
741 *value = 0;
742 }
743 return NO_ERROR;
744 }
745 case NATIVE_WINDOW_CONCRETE_TYPE:
746 *value = NATIVE_WINDOW_SURFACE;
747 return NO_ERROR;
748 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800749 *value = static_cast<int>(
750 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800751 return NO_ERROR;
752 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800753 *value = static_cast<int>(
754 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800755 return NO_ERROR;
756 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800757 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800758 return NO_ERROR;
759 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
760 status_t err = NO_ERROR;
761 if (!mConsumerRunningBehind) {
762 *value = 0;
763 } else {
764 err = mGraphicBufferProducer->query(what, value);
765 if (err == NO_ERROR) {
766 mConsumerRunningBehind = *value;
767 }
768 }
769 return err;
770 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700771 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
772 int64_t durationUs = mLastDequeueDuration / 1000;
773 *value = durationUs > std::numeric_limits<int>::max() ?
774 std::numeric_limits<int>::max() :
775 static_cast<int>(durationUs);
776 return NO_ERROR;
777 }
778 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
779 int64_t durationUs = mLastQueueDuration / 1000;
780 *value = durationUs > std::numeric_limits<int>::max() ?
781 std::numeric_limits<int>::max() :
782 static_cast<int>(durationUs);
783 return NO_ERROR;
784 }
Brian Anderson069b3652016-07-22 10:32:47 -0700785 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
786 querySupportedTimestampsLocked();
787 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
788 return NO_ERROR;
789 }
790 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
791 querySupportedTimestampsLocked();
792 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
793 return NO_ERROR;
794 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800795 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700796 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800797 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800798}
799
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800800int Surface::perform(int operation, va_list args)
801{
802 int res = NO_ERROR;
803 switch (operation) {
804 case NATIVE_WINDOW_CONNECT:
805 // deprecated. must return NO_ERROR.
806 break;
807 case NATIVE_WINDOW_DISCONNECT:
808 // deprecated. must return NO_ERROR.
809 break;
810 case NATIVE_WINDOW_SET_USAGE:
811 res = dispatchSetUsage(args);
812 break;
813 case NATIVE_WINDOW_SET_CROP:
814 res = dispatchSetCrop(args);
815 break;
816 case NATIVE_WINDOW_SET_BUFFER_COUNT:
817 res = dispatchSetBufferCount(args);
818 break;
819 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
820 res = dispatchSetBuffersGeometry(args);
821 break;
822 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
823 res = dispatchSetBuffersTransform(args);
824 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700825 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
826 res = dispatchSetBuffersStickyTransform(args);
827 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800828 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
829 res = dispatchSetBuffersTimestamp(args);
830 break;
831 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
832 res = dispatchSetBuffersDimensions(args);
833 break;
834 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
835 res = dispatchSetBuffersUserDimensions(args);
836 break;
837 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
838 res = dispatchSetBuffersFormat(args);
839 break;
840 case NATIVE_WINDOW_LOCK:
841 res = dispatchLock(args);
842 break;
843 case NATIVE_WINDOW_UNLOCK_AND_POST:
844 res = dispatchUnlockAndPost(args);
845 break;
846 case NATIVE_WINDOW_SET_SCALING_MODE:
847 res = dispatchSetScalingMode(args);
848 break;
849 case NATIVE_WINDOW_API_CONNECT:
850 res = dispatchConnect(args);
851 break;
852 case NATIVE_WINDOW_API_DISCONNECT:
853 res = dispatchDisconnect(args);
854 break;
Rachad7cb0d392014-07-29 17:53:53 -0700855 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
856 res = dispatchSetSidebandStream(args);
857 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800858 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
859 res = dispatchSetBuffersDataSpace(args);
860 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700861 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
862 res = dispatchSetSurfaceDamage(args);
863 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700864 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
865 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700866 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800867 case NATIVE_WINDOW_SET_AUTO_REFRESH:
868 res = dispatchSetAutoRefresh(args);
869 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800870 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
871 res = dispatchGetDisplayRefreshCycleDuration(args);
872 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800873 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
874 res = dispatchGetNextFrameId(args);
875 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700876 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
877 res = dispatchEnableFrameTimestamps(args);
878 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800879 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
880 res = dispatchGetCompositorTiming(args);
881 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800882 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
883 res = dispatchGetFrameTimestamps(args);
884 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800885 default:
886 res = NAME_NOT_FOUND;
887 break;
888 }
889 return res;
890}
Mathias Agopiana138f892010-05-21 17:24:35 -0700891
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800892int Surface::dispatchConnect(va_list args) {
893 int api = va_arg(args, int);
894 return connect(api);
895}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800896
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800897int Surface::dispatchDisconnect(va_list args) {
898 int api = va_arg(args, int);
899 return disconnect(api);
900}
901
902int Surface::dispatchSetUsage(va_list args) {
903 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800904 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800905}
906
907int Surface::dispatchSetCrop(va_list args) {
908 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
909 return setCrop(reinterpret_cast<Rect const*>(rect));
910}
911
912int Surface::dispatchSetBufferCount(va_list args) {
913 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800914 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800915}
916
917int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800918 uint32_t width = va_arg(args, uint32_t);
919 uint32_t height = va_arg(args, uint32_t);
920 PixelFormat format = va_arg(args, PixelFormat);
921 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800922 if (err != 0) {
923 return err;
924 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800925 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800926}
927
928int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800929 uint32_t width = va_arg(args, uint32_t);
930 uint32_t height = va_arg(args, uint32_t);
931 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800932}
933
934int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800935 uint32_t width = va_arg(args, uint32_t);
936 uint32_t height = va_arg(args, uint32_t);
937 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800938}
939
940int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800941 PixelFormat format = va_arg(args, PixelFormat);
942 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800943}
944
945int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800946 int mode = va_arg(args, int);
947 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800948}
949
950int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800951 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800952 return setBuffersTransform(transform);
953}
954
Ruben Brunk1681d952014-06-27 15:51:55 -0700955int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800956 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700957 return setBuffersStickyTransform(transform);
958}
959
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800960int Surface::dispatchSetBuffersTimestamp(va_list args) {
961 int64_t timestamp = va_arg(args, int64_t);
962 return setBuffersTimestamp(timestamp);
963}
964
965int Surface::dispatchLock(va_list args) {
966 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
967 ARect* inOutDirtyBounds = va_arg(args, ARect*);
968 return lock(outBuffer, inOutDirtyBounds);
969}
970
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800971int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800972 return unlockAndPost();
973}
974
Rachad7cb0d392014-07-29 17:53:53 -0700975int Surface::dispatchSetSidebandStream(va_list args) {
976 native_handle_t* sH = va_arg(args, native_handle_t*);
977 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
978 setSidebandStream(sidebandHandle);
979 return OK;
980}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800981
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800982int Surface::dispatchSetBuffersDataSpace(va_list args) {
983 android_dataspace dataspace =
984 static_cast<android_dataspace>(va_arg(args, int));
985 return setBuffersDataSpace(dataspace);
986}
987
Dan Stoza5065a552015-03-17 16:23:42 -0700988int Surface::dispatchSetSurfaceDamage(va_list args) {
989 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
990 size_t numRects = va_arg(args, size_t);
991 setSurfaceDamage(rects, numRects);
992 return NO_ERROR;
993}
994
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700995int Surface::dispatchSetSharedBufferMode(va_list args) {
996 bool sharedBufferMode = va_arg(args, int);
997 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800998}
999
1000int Surface::dispatchSetAutoRefresh(va_list args) {
1001 bool autoRefresh = va_arg(args, int);
1002 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001003}
1004
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001005int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1006 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1007 return getDisplayRefreshCycleDuration(outRefreshDuration);
1008}
1009
Brian Anderson1049d1d2016-12-16 17:25:57 -08001010int Surface::dispatchGetNextFrameId(va_list args) {
1011 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1012 *nextFrameId = getNextFrameNumber();
1013 return NO_ERROR;
1014}
1015
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001016int Surface::dispatchEnableFrameTimestamps(va_list args) {
1017 bool enable = va_arg(args, int);
1018 enableFrameTimestamps(enable);
1019 return NO_ERROR;
1020}
1021
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001022int Surface::dispatchGetCompositorTiming(va_list args) {
1023 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1024 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1025 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1026 return getCompositorTiming(compositeDeadline, compositeInterval,
1027 compositeToPresentLatency);
1028}
1029
Pablo Ceballosce796e72016-02-04 19:10:51 -08001030int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001031 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001032 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001033 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001034 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1035 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1036 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001037 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001038 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001039 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001040 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001041 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001042 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001043 outRequestedPresentTime, outAcquireTime, outLatchTime,
1044 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -07001045 outGlCompositionDoneTime, outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001046 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001047}
1048
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001049int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001050 static sp<IProducerListener> listener = new DummyProducerListener();
1051 return connect(api, listener);
1052}
1053
1054int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001055 ATRACE_CALL();
1056 ALOGV("Surface::connect");
1057 Mutex::Autolock lock(mMutex);
1058 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001059 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001060 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001061 mDefaultWidth = output.width;
1062 mDefaultHeight = output.height;
1063 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001064
1065 // Disable transform hint if sticky transform is set.
1066 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001067 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001068 }
1069
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001070 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001071 }
1072 if (!err && api == NATIVE_WINDOW_API_CPU) {
1073 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001074 // Clear the dirty region in case we're switching from a non-CPU API
1075 mDirtyRegion.clear();
1076 } else if (!err) {
1077 // Initialize the dirty region for tracking surface damage
1078 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001079 }
Dan Stoza5065a552015-03-17 16:23:42 -07001080
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001081 return err;
1082}
1083
Mathias Agopian365857d2013-09-11 19:35:45 -07001084
Robert Carr97b9c862016-09-08 13:54:35 -07001085int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001086 ATRACE_CALL();
1087 ALOGV("Surface::disconnect");
1088 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001089 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1090 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001091 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001092 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001093 if (!err) {
1094 mReqFormat = 0;
1095 mReqWidth = 0;
1096 mReqHeight = 0;
1097 mReqUsage = 0;
1098 mCrop.clear();
1099 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1100 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001101 mStickyTransform = 0;
1102
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001103 if (api == NATIVE_WINDOW_API_CPU) {
1104 mConnectedToCpu = false;
1105 }
1106 }
1107 return err;
1108}
1109
Dan Stozad9c49712015-04-27 11:06:01 -07001110int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001111 sp<Fence>* outFence) {
1112 ATRACE_CALL();
1113 ALOGV("Surface::detachNextBuffer");
1114
1115 if (outBuffer == NULL || outFence == NULL) {
1116 return BAD_VALUE;
1117 }
1118
1119 Mutex::Autolock lock(mMutex);
1120
1121 sp<GraphicBuffer> buffer(NULL);
1122 sp<Fence> fence(NULL);
1123 status_t result = mGraphicBufferProducer->detachNextBuffer(
1124 &buffer, &fence);
1125 if (result != NO_ERROR) {
1126 return result;
1127 }
1128
Dan Stozad9c49712015-04-27 11:06:01 -07001129 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001130 if (fence != NULL && fence->isValid()) {
1131 *outFence = fence;
1132 } else {
1133 *outFence = Fence::NO_FENCE;
1134 }
1135
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001136 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1137 if (mSlots[i].buffer != NULL &&
1138 mSlots[i].buffer->handle == buffer->handle) {
1139 mSlots[i].buffer = NULL;
1140 }
1141 }
1142
Dan Stoza231832e2015-03-11 11:55:01 -07001143 return NO_ERROR;
1144}
1145
1146int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1147{
1148 ATRACE_CALL();
1149 ALOGV("Surface::attachBuffer");
1150
1151 Mutex::Autolock lock(mMutex);
1152
1153 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001154 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1155 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001156 int32_t attachedSlot = -1;
1157 status_t result = mGraphicBufferProducer->attachBuffer(
1158 &attachedSlot, graphicBuffer);
1159 if (result != NO_ERROR) {
1160 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001161 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001162 return result;
1163 }
1164 mSlots[attachedSlot].buffer = graphicBuffer;
1165
1166 return NO_ERROR;
1167}
1168
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001169int Surface::setUsage(uint32_t reqUsage)
1170{
1171 ALOGV("Surface::setUsage");
1172 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001173 if (reqUsage != mReqUsage) {
1174 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1175 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001176 mReqUsage = reqUsage;
1177 return OK;
1178}
1179
1180int Surface::setCrop(Rect const* rect)
1181{
1182 ATRACE_CALL();
1183
Pablo Ceballos60d69222015-08-07 14:47:20 -07001184 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001185 if (rect == NULL || rect->isEmpty()) {
1186 realRect.clear();
1187 } else {
1188 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001189 }
1190
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001191 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1192 realRect.left, realRect.top, realRect.right, realRect.bottom);
1193
1194 Mutex::Autolock lock(mMutex);
1195 mCrop = realRect;
1196 return NO_ERROR;
1197}
1198
1199int Surface::setBufferCount(int bufferCount)
1200{
1201 ATRACE_CALL();
1202 ALOGV("Surface::setBufferCount");
1203 Mutex::Autolock lock(mMutex);
1204
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001205 status_t err = NO_ERROR;
1206 if (bufferCount == 0) {
1207 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1208 } else {
1209 int minUndequeuedBuffers = 0;
1210 err = mGraphicBufferProducer->query(
1211 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1212 if (err == NO_ERROR) {
1213 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1214 bufferCount - minUndequeuedBuffers);
1215 }
1216 }
Mathias Agopian90147262010-01-22 11:47:55 -08001217
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001218 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1219 bufferCount, strerror(-err));
1220
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001221 return err;
1222}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001223
Pablo Ceballosfa455352015-08-12 17:47:47 -07001224int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1225 ATRACE_CALL();
1226 ALOGV("Surface::setMaxDequeuedBufferCount");
1227 Mutex::Autolock lock(mMutex);
1228
1229 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1230 maxDequeuedBuffers);
1231 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1232 "returned %s", maxDequeuedBuffers, strerror(-err));
1233
Pablo Ceballosfa455352015-08-12 17:47:47 -07001234 return err;
1235}
1236
1237int Surface::setAsyncMode(bool async) {
1238 ATRACE_CALL();
1239 ALOGV("Surface::setAsyncMode");
1240 Mutex::Autolock lock(mMutex);
1241
1242 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1243 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1244 async, strerror(-err));
1245
Pablo Ceballosfa455352015-08-12 17:47:47 -07001246 return err;
1247}
1248
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001249int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001250 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001251 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001252 Mutex::Autolock lock(mMutex);
1253
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001254 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1255 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001256 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001257 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001258 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001259 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1260 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001261
1262 return err;
1263}
1264
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001265int Surface::setAutoRefresh(bool autoRefresh) {
1266 ATRACE_CALL();
1267 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1268 Mutex::Autolock lock(mMutex);
1269
1270 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1271 if (err == NO_ERROR) {
1272 mAutoRefresh = autoRefresh;
1273 }
1274 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1275 autoRefresh, strerror(-err));
1276 return err;
1277}
1278
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001279int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001280{
1281 ATRACE_CALL();
1282 ALOGV("Surface::setBuffersDimensions");
1283
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001284 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001285 return BAD_VALUE;
1286
1287 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001288 if (width != mReqWidth || height != mReqHeight) {
1289 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1290 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001291 mReqWidth = width;
1292 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001293 return NO_ERROR;
1294}
1295
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001296int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001297{
1298 ATRACE_CALL();
1299 ALOGV("Surface::setBuffersUserDimensions");
1300
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001301 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001302 return BAD_VALUE;
1303
1304 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001305 if (width != mUserWidth || height != mUserHeight) {
1306 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1307 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001308 mUserWidth = width;
1309 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001310 return NO_ERROR;
1311}
1312
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001313int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001314{
1315 ALOGV("Surface::setBuffersFormat");
1316
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001317 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001318 if (format != mReqFormat) {
1319 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1320 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001321 mReqFormat = format;
1322 return NO_ERROR;
1323}
1324
1325int Surface::setScalingMode(int mode)
1326{
1327 ATRACE_CALL();
1328 ALOGV("Surface::setScalingMode(%d)", mode);
1329
1330 switch (mode) {
1331 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1332 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1333 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001334 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001335 break;
1336 default:
1337 ALOGE("unknown scaling mode: %d", mode);
1338 return BAD_VALUE;
1339 }
1340
1341 Mutex::Autolock lock(mMutex);
1342 mScalingMode = mode;
1343 return NO_ERROR;
1344}
1345
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001346int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001347{
1348 ATRACE_CALL();
1349 ALOGV("Surface::setBuffersTransform");
1350 Mutex::Autolock lock(mMutex);
1351 mTransform = transform;
1352 return NO_ERROR;
1353}
1354
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001355int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001356{
1357 ATRACE_CALL();
1358 ALOGV("Surface::setBuffersStickyTransform");
1359 Mutex::Autolock lock(mMutex);
1360 mStickyTransform = transform;
1361 return NO_ERROR;
1362}
1363
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001364int Surface::setBuffersTimestamp(int64_t timestamp)
1365{
1366 ALOGV("Surface::setBuffersTimestamp");
1367 Mutex::Autolock lock(mMutex);
1368 mTimestamp = timestamp;
1369 return NO_ERROR;
1370}
1371
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001372int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1373{
1374 ALOGV("Surface::setBuffersDataSpace");
1375 Mutex::Autolock lock(mMutex);
1376 mDataSpace = dataSpace;
1377 return NO_ERROR;
1378}
1379
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001380void Surface::freeAllBuffers() {
1381 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1382 mSlots[i].buffer = 0;
1383 }
1384}
1385
Dan Stoza5065a552015-03-17 16:23:42 -07001386void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1387 ATRACE_CALL();
1388 ALOGV("Surface::setSurfaceDamage");
1389 Mutex::Autolock lock(mMutex);
1390
Dan Stozac62acbd2015-04-21 16:42:49 -07001391 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001392 mDirtyRegion = Region::INVALID_REGION;
1393 return;
1394 }
1395
1396 mDirtyRegion.clear();
1397 for (size_t r = 0; r < numRects; ++r) {
1398 // We intentionally flip top and bottom here, since because they're
1399 // specified with a bottom-left origin, top > bottom, which fails
1400 // validation in the Region class. We will fix this up when we flip to a
1401 // top-left origin in queueBuffer.
1402 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1403 mDirtyRegion.orSelf(rect);
1404 }
1405}
1406
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001407// ----------------------------------------------------------------------
1408// the lock/unlock APIs must be used from the same thread
1409
1410static status_t copyBlt(
1411 const sp<GraphicBuffer>& dst,
1412 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001413 const Region& reg,
1414 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001415{
1416 // src and dst with, height and format must be identical. no verification
1417 // is done here.
1418 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001419 uint8_t* src_bits = NULL;
1420 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1421 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001422 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1423
1424 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001425 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1426 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001427 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001428 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001429
1430 Region::const_iterator head(reg.begin());
1431 Region::const_iterator tail(reg.end());
1432 if (head != tail && src_bits && dst_bits) {
1433 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001434 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1435 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001436
1437 while (head != tail) {
1438 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001439 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001440 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001441 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1442 uint8_t const * s = src_bits +
1443 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1444 uint8_t * d = dst_bits +
1445 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001446 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001447 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001448 h = 1;
1449 }
1450 do {
1451 memcpy(d, s, size);
1452 d += dbpr;
1453 s += sbpr;
1454 } while (--h > 0);
1455 }
1456 }
1457
1458 if (src_bits)
1459 src->unlock();
1460
1461 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001462 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001463
1464 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001465}
1466
Mathias Agopiana138f892010-05-21 17:24:35 -07001467// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001468
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001469status_t Surface::lock(
1470 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1471{
1472 if (mLockedBuffer != 0) {
1473 ALOGE("Surface::lock failed, already locked");
1474 return INVALID_OPERATION;
1475 }
1476
1477 if (!mConnectedToCpu) {
1478 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1479 if (err) {
1480 return err;
1481 }
1482 // we're intending to do software rendering from this point
1483 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1484 }
1485
1486 ANativeWindowBuffer* out;
1487 int fenceFd = -1;
1488 status_t err = dequeueBuffer(&out, &fenceFd);
1489 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1490 if (err == NO_ERROR) {
1491 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001492 const Rect bounds(backBuffer->width, backBuffer->height);
1493
1494 Region newDirtyRegion;
1495 if (inOutDirtyBounds) {
1496 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1497 newDirtyRegion.andSelf(bounds);
1498 } else {
1499 newDirtyRegion.set(bounds);
1500 }
1501
1502 // figure out if we can copy the frontbuffer back
1503 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1504 const bool canCopyBack = (frontBuffer != 0 &&
1505 backBuffer->width == frontBuffer->width &&
1506 backBuffer->height == frontBuffer->height &&
1507 backBuffer->format == frontBuffer->format);
1508
1509 if (canCopyBack) {
1510 // copy the area that is invalid and not repainted this round
1511 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001512 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001513 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001514 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001515 } else {
1516 // if we can't copy-back anything, modify the user's dirty
1517 // region to make sure they redraw the whole buffer
1518 newDirtyRegion.set(bounds);
1519 mDirtyRegion.clear();
1520 Mutex::Autolock lock(mMutex);
1521 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1522 mSlots[i].dirtyRegion.clear();
1523 }
1524 }
1525
1526
1527 { // scope for the lock
1528 Mutex::Autolock lock(mMutex);
1529 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1530 if (backBufferSlot >= 0) {
1531 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1532 mDirtyRegion.subtract(dirtyRegion);
1533 dirtyRegion = newDirtyRegion;
1534 }
1535 }
1536
1537 mDirtyRegion.orSelf(newDirtyRegion);
1538 if (inOutDirtyBounds) {
1539 *inOutDirtyBounds = newDirtyRegion.getBounds();
1540 }
1541
1542 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001543 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001544 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001545 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001546
1547 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1548 backBuffer->handle);
1549
1550 if (res != 0) {
1551 err = INVALID_OPERATION;
1552 } else {
1553 mLockedBuffer = backBuffer;
1554 outBuffer->width = backBuffer->width;
1555 outBuffer->height = backBuffer->height;
1556 outBuffer->stride = backBuffer->stride;
1557 outBuffer->format = backBuffer->format;
1558 outBuffer->bits = vaddr;
1559 }
1560 }
1561 return err;
1562}
1563
1564status_t Surface::unlockAndPost()
1565{
1566 if (mLockedBuffer == 0) {
1567 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1568 return INVALID_OPERATION;
1569 }
1570
Francis Hart8f396012014-04-01 15:30:53 +03001571 int fd = -1;
1572 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001573 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1574
Francis Hart8f396012014-04-01 15:30:53 +03001575 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001576 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1577 mLockedBuffer->handle, strerror(-err));
1578
1579 mPostedBuffer = mLockedBuffer;
1580 mLockedBuffer = 0;
1581 return err;
1582}
1583
Robert Carr9f31e292016-04-11 11:15:32 -07001584bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1585 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001586 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001587 return true;
1588 }
1589 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1590}
1591
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001592status_t Surface::getUniqueId(uint64_t* outId) const {
1593 Mutex::Autolock lock(mMutex);
1594 return mGraphicBufferProducer->getUniqueId(outId);
1595}
1596
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001597namespace view {
1598
1599status_t Surface::writeToParcel(Parcel* parcel) const {
1600 return writeToParcel(parcel, false);
1601}
1602
1603status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const {
1604 if (parcel == nullptr) return BAD_VALUE;
1605
1606 status_t res = OK;
1607
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001608 if (!nameAlreadyWritten) {
1609 res = parcel->writeString16(name);
1610 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001611
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001612 /* isSingleBuffered defaults to no */
1613 res = parcel->writeInt32(0);
1614 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001615 }
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001616
1617 res = parcel->writeStrongBinder(
1618 IGraphicBufferProducer::asBinder(graphicBufferProducer));
1619
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001620 return res;
1621}
1622
1623status_t Surface::readFromParcel(const Parcel* parcel) {
1624 return readFromParcel(parcel, false);
1625}
1626
1627status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) {
1628 if (parcel == nullptr) return BAD_VALUE;
1629
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001630 status_t res = OK;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001631 if (!nameAlreadyRead) {
1632 name = readMaybeEmptyString16(parcel);
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001633 // Discard this for now
1634 int isSingleBuffered;
1635 res = parcel->readInt32(&isSingleBuffered);
1636 if (res != OK) {
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001637 ALOGE("Can't read isSingleBuffered");
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001638 return res;
1639 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001640 }
1641
1642 sp<IBinder> binder;
1643
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001644 res = parcel->readNullableStrongBinder(&binder);
1645 if (res != OK) {
1646 ALOGE("%s: Can't read strong binder", __FUNCTION__);
1647 return res;
1648 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001649
1650 graphicBufferProducer = interface_cast<IGraphicBufferProducer>(binder);
1651
1652 return OK;
1653}
1654
1655String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
1656 size_t len;
1657 const char16_t* str = parcel->readString16Inplace(&len);
1658 if (str != nullptr) {
1659 return String16(str, len);
1660 } else {
1661 return String16();
1662 }
1663}
1664
1665} // namespace view
1666
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667}; // namespace android