blob: 851a49579ba5ef47e2083e75c825f1f27e53e581 [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
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800105sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
106 return mGraphicBufferProducer;
107}
108
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900109void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
110 mGraphicBufferProducer->setSidebandStream(stream);
111}
112
Dan Stoza29a3e902014-06-20 13:13:57 -0700113void Surface::allocateBuffers() {
114 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
115 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700116 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
117 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700118}
119
Dan Stoza812ed062015-06-02 15:45:22 -0700120status_t Surface::setGenerationNumber(uint32_t generation) {
121 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
122 if (result == NO_ERROR) {
123 mGenerationNumber = generation;
124 }
125 return result;
126}
127
Dan Stoza7dde5992015-05-22 09:51:44 -0700128uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700129 Mutex::Autolock lock(mMutex);
130 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700131}
132
Dan Stozac6f30bd2015-06-08 09:32:50 -0700133String8 Surface::getConsumerName() const {
134 return mGraphicBufferProducer->getConsumerName();
135}
136
Dan Stoza127fc632015-06-30 13:43:32 -0700137status_t Surface::setDequeueTimeout(nsecs_t timeout) {
138 return mGraphicBufferProducer->setDequeueTimeout(timeout);
139}
140
Dan Stoza50101d02016-04-07 16:53:23 -0700141status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700142 sp<Fence>* outFence, float outTransformMatrix[16]) {
143 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
144 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700145}
146
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700147void Surface::enableFrameTimestamps(bool enable) {
148 Mutex::Autolock lock(mMutex);
149 mEnableFrameTimestamps = enable;
150}
151
Brian Anderson50143b32016-09-30 14:01:24 -0700152static bool checkConsumerForUpdates(
153 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700154 const nsecs_t* outLatchTime,
155 const nsecs_t* outFirstRefreshStartTime,
156 const nsecs_t* outLastRefreshStartTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700157 const nsecs_t* outGlCompositionDoneTime,
158 const nsecs_t* outDisplayPresentTime,
159 const nsecs_t* outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700160 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700161 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700162 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
163 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700164 !e->hasFirstRefreshStartInfo();
165 bool checkForGlCompositionDone = (outGlCompositionDoneTime != nullptr) &&
166 !e->hasGpuCompositionDoneInfo();
167 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
168 !e->hasDisplayPresentInfo();
169
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700170 // LastRefreshStart, DisplayRetire, DequeueReady, and Release are never
171 // available for the last frame.
172 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
173 !e->hasLastRefreshStartInfo() &&
174 (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700175 bool checkForDisplayRetire = (outDisplayRetireTime != nullptr) &&
176 !e->hasDisplayRetireInfo() && (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700177 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
178 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700179 bool checkForRelease = (outReleaseTime != nullptr) &&
180 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
181
182 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700183 return checkForLatch || checkForFirstRefreshStart ||
184 checkForLastRefreshStart || checkForGlCompositionDone ||
185 checkForDisplayPresent || checkForDisplayRetire ||
186 checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700187}
188
Brian Anderson3d4039d2016-09-23 16:31:30 -0700189static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
190 if (dst != nullptr) {
191 *dst = Fence::isValidTimestamp(src) ? src : 0;
192 }
193}
194
195static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) {
196 if (dst != nullptr) {
197 nsecs_t signalTime = src->getSignalTime();
198 *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0;
199 }
200}
201
Brian Anderson069b3652016-07-22 10:32:47 -0700202status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700203 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700204 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
205 nsecs_t* outLastRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700206 nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700207 nsecs_t* outDequeueReadyTime, nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800208 ATRACE_CALL();
209
Brian Anderson3890c392016-07-25 12:48:08 -0700210 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700211
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700212 if (!mEnableFrameTimestamps) {
213 return INVALID_OPERATION;
214 }
215
Brian Anderson3890c392016-07-25 12:48:08 -0700216 // Verify the requested timestamps are supported.
217 querySupportedTimestampsLocked();
218 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
219 return BAD_VALUE;
220 }
221 if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
222 return BAD_VALUE;
Brian Anderson069b3652016-07-22 10:32:47 -0700223 }
224
Brian Anderson3da8d272016-07-28 16:20:47 -0700225 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700226 if (events == nullptr) {
227 // If the entry isn't available in the producer, it's definitely not
228 // available in the consumer.
229 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700230 }
231
Brian Anderson50143b32016-09-30 14:01:24 -0700232 // Update our cache of events if the requested events are not available.
233 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700234 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
235 outGlCompositionDoneTime, outDisplayPresentTime,
236 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700237 FrameEventHistoryDelta delta;
238 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700239 mFrameEventHistory->applyDelta(delta);
240 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700241 }
242
Brian Anderson3890c392016-07-25 12:48:08 -0700243 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700244 // The entry was available before the update, but was overwritten
245 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700246 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800247 }
Brian Anderson069b3652016-07-22 10:32:47 -0700248
Brian Anderson3d4039d2016-09-23 16:31:30 -0700249 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700250 getFrameTimestamp(outLatchTime, events->latchTime);
251 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
252 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
253 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700254
Brian Anderson3d4039d2016-09-23 16:31:30 -0700255 getFrameTimestampFence(outAcquireTime, events->acquireFence);
256 getFrameTimestampFence(
257 outGlCompositionDoneTime, events->gpuCompositionDoneFence);
258 getFrameTimestampFence(
259 outDisplayPresentTime, events->displayPresentFence);
260 getFrameTimestampFence(outDisplayRetireTime, events->displayRetireFence);
261 getFrameTimestampFence(outReleaseTime, events->releaseFence);
Brian Anderson069b3652016-07-22 10:32:47 -0700262
263 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800264}
Ian Elliott62c48c92017-01-20 13:13:20 -0700265status_t Surface::getDisplayRefreshCyclePeriod(nsecs_t* outMinRefreshDuration,
266 nsecs_t* outMaxRefreshDuration) {
267 ATRACE_CALL();
268
269 DisplayStatInfo stats;
270 status_t err = composerService()->getDisplayStats(NULL, &stats);
271
272 *outMinRefreshDuration = stats.vsyncPeriod;
273 *outMaxRefreshDuration = stats.vsyncPeriod;
274
275 return NO_ERROR;
276}
Pablo Ceballosce796e72016-02-04 19:10:51 -0800277
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800278int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
279 Surface* c = getSelf(window);
280 return c->setSwapInterval(interval);
281}
282
283int Surface::hook_dequeueBuffer(ANativeWindow* window,
284 ANativeWindowBuffer** buffer, int* fenceFd) {
285 Surface* c = getSelf(window);
286 return c->dequeueBuffer(buffer, fenceFd);
287}
288
289int Surface::hook_cancelBuffer(ANativeWindow* window,
290 ANativeWindowBuffer* buffer, int fenceFd) {
291 Surface* c = getSelf(window);
292 return c->cancelBuffer(buffer, fenceFd);
293}
294
295int Surface::hook_queueBuffer(ANativeWindow* window,
296 ANativeWindowBuffer* buffer, int fenceFd) {
297 Surface* c = getSelf(window);
298 return c->queueBuffer(buffer, fenceFd);
299}
300
301int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
302 ANativeWindowBuffer** buffer) {
303 Surface* c = getSelf(window);
304 ANativeWindowBuffer* buf;
305 int fenceFd = -1;
306 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600307 if (result != OK) {
308 return result;
309 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800310 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700311 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800312 if (waitResult != OK) {
313 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
314 waitResult);
315 c->cancelBuffer(buf, -1);
316 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700317 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800318 *buffer = buf;
319 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700320}
321
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800322int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
323 ANativeWindowBuffer* buffer) {
324 Surface* c = getSelf(window);
325 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700326}
327
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800328int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
329 ANativeWindowBuffer* buffer) {
330 Surface* c = getSelf(window);
331 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700332}
333
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800334int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
335 ANativeWindowBuffer* buffer) {
336 Surface* c = getSelf(window);
337 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700338}
Mathias Agopian62185b72009-04-16 16:19:50 -0700339
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800340int Surface::hook_query(const ANativeWindow* window,
341 int what, int* value) {
342 const Surface* c = getSelf(window);
343 return c->query(what, value);
344}
345
346int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
347 va_list args;
348 va_start(args, operation);
349 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700350 int result = c->perform(operation, args);
351 va_end(args);
352 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800353}
354
355int Surface::setSwapInterval(int interval) {
356 ATRACE_CALL();
357 // EGL specification states:
358 // interval is silently clamped to minimum and maximum implementation
359 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800360
361 if (interval < minSwapInterval)
362 interval = minSwapInterval;
363
364 if (interval > maxSwapInterval)
365 interval = maxSwapInterval;
366
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700367 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700368 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800369
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700370 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800371}
372
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700373int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800374 ATRACE_CALL();
375 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800376
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800377 uint32_t reqWidth;
378 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800379 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800380 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700381 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800382
383 {
384 Mutex::Autolock lock(mMutex);
385
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800386 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
387 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800388
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800389 reqFormat = mReqFormat;
390 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800391
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700392 enableFrameTimestamps = mEnableFrameTimestamps;
393
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700394 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800395 BufferItem::INVALID_BUFFER_SLOT) {
396 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
397 if (gbuf != NULL) {
398 *buffer = gbuf.get();
399 *fenceFd = -1;
400 return OK;
401 }
402 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800403 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
404
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800405 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800406 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700407 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700408
409 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700410 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700411 reqWidth, reqHeight, reqFormat, reqUsage,
412 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700413 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800414
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800415 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700416 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
417 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
418 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800419 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700420 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800421
422 Mutex::Autolock lock(mMutex);
423
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800424 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700425
426 // this should never happen
427 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
428
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800429 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
430 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700431 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700432
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700433 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700434 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700435 }
436
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800437 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
438 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
439 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700440 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700441 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800442 return result;
443 }
444 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700445
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800446 if (fence->isValid()) {
447 *fenceFd = fence->dup();
448 if (*fenceFd == -1) {
449 ALOGE("dequeueBuffer: error duping fence: %d", errno);
450 // dup() should never fail; something is badly wrong. Soldier on
451 // and hope for the best; the worst that should happen is some
452 // visible corruption that lasts until the next frame.
453 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700454 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800455 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700456 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800457
458 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800459
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700460 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800461 mSharedBufferSlot = buf;
462 mSharedBufferHasBeenQueued = false;
463 } else if (mSharedBufferSlot == buf) {
464 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
465 mSharedBufferHasBeenQueued = false;
466 }
467
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800468 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700469}
470
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800471int Surface::cancelBuffer(android_native_buffer_t* buffer,
472 int fenceFd) {
473 ATRACE_CALL();
474 ALOGV("Surface::cancelBuffer");
475 Mutex::Autolock lock(mMutex);
476 int i = getSlotFromBufferLocked(buffer);
477 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900478 if (fenceFd >= 0) {
479 close(fenceFd);
480 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800481 return i;
482 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800483 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
484 if (fenceFd >= 0) {
485 close(fenceFd);
486 }
487 return OK;
488 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800489 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
490 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800491
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700492 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800493 mSharedBufferHasBeenQueued = true;
494 }
495
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800496 return OK;
497}
498
499int Surface::getSlotFromBufferLocked(
500 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800501 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
502 if (mSlots[i].buffer != NULL &&
503 mSlots[i].buffer->handle == buffer->handle) {
504 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700505 }
506 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800507 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
508 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700509}
510
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800511int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800512 ALOGV("Surface::lockBuffer");
513 Mutex::Autolock lock(mMutex);
514 return OK;
515}
Mathias Agopian631f3582010-05-25 17:51:34 -0700516
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800517int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
518 ATRACE_CALL();
519 ALOGV("Surface::queueBuffer");
520 Mutex::Autolock lock(mMutex);
521 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700522 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800523
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800524 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700525 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700526 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700527 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700528 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800529 } else {
530 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700531 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800532 int i = getSlotFromBufferLocked(buffer);
533 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900534 if (fenceFd >= 0) {
535 close(fenceFd);
536 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800537 return i;
538 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800539 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
540 if (fenceFd >= 0) {
541 close(fenceFd);
542 }
543 return OK;
544 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800547 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700548 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800549 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800551 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
552 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700553 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800554 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700555 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700556
Dan Stozac62acbd2015-04-21 16:42:49 -0700557 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700558 input.setSurfaceDamage(Region::INVALID_REGION);
559 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700560 // Here we do two things:
561 // 1) The surface damage was specified using the OpenGL ES convention of
562 // the origin being in the bottom-left corner. Here we flip to the
563 // convention that the rest of the system uses (top-left corner) by
564 // subtracting all top/bottom coordinates from the buffer height.
565 // 2) If the buffer is coming in rotated (for example, because the EGL
566 // implementation is reacting to the transform hint coming back from
567 // SurfaceFlinger), the surface damage needs to be rotated the
568 // opposite direction, since it was generated assuming an unrotated
569 // buffer (the app doesn't know that the EGL implementation is
570 // reacting to the transform hint behind its back). The
571 // transformations in the switch statement below apply those
572 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
573
574 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700575 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700576 bool rotated90 = (mTransform ^ mStickyTransform) &
577 NATIVE_WINDOW_TRANSFORM_ROT_90;
578 if (rotated90) {
579 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700580 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700581
Dan Stoza5065a552015-03-17 16:23:42 -0700582 Region flippedRegion;
583 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700584 int left = rect.left;
585 int right = rect.right;
586 int top = height - rect.bottom; // Flip from OpenGL convention
587 int bottom = height - rect.top; // Flip from OpenGL convention
588 switch (mTransform ^ mStickyTransform) {
589 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
590 // Rotate 270 degrees
591 Rect flippedRect{top, width - right, bottom, width - left};
592 flippedRegion.orSelf(flippedRect);
593 break;
594 }
595 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
596 // Rotate 180 degrees
597 Rect flippedRect{width - right, height - bottom,
598 width - left, height - top};
599 flippedRegion.orSelf(flippedRect);
600 break;
601 }
602 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
603 // Rotate 90 degrees
604 Rect flippedRect{height - bottom, left,
605 height - top, right};
606 flippedRegion.orSelf(flippedRect);
607 break;
608 }
609 default: {
610 Rect flippedRect{left, top, right, bottom};
611 flippedRegion.orSelf(flippedRect);
612 break;
613 }
614 }
Dan Stoza5065a552015-03-17 16:23:42 -0700615 }
616
617 input.setSurfaceDamage(flippedRegion);
618 }
619
Dan Stoza70ccba52016-07-01 14:00:40 -0700620 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800621 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700622 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800623 if (err != OK) {
624 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
625 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800626
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700627 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700628 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700629 // Update timestamps with the local acquire fence.
630 // The consumer doesn't send it back to prevent us from having two
631 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700632 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700633 std::make_shared<FenceTime>(std::move(fence)));
634
635 // Cache timestamps of signaled fences so we can close their file
636 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700637 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700638 }
639
Brian Anderson50143b32016-09-30 14:01:24 -0700640 mLastFrameNumber = mNextFrameNumber;
641
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700642 mDefaultWidth = output.width;
643 mDefaultHeight = output.height;
644 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700645
Ruben Brunk1681d952014-06-27 15:51:55 -0700646 // Disable transform hint if sticky transform is set.
647 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700648 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700649 }
650
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700651 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700652
Dan Stozac62acbd2015-04-21 16:42:49 -0700653 if (!mConnectedToCpu) {
654 // Clear surface damage back to full-buffer
655 mDirtyRegion = Region::INVALID_REGION;
656 }
Dan Stoza5065a552015-03-17 16:23:42 -0700657
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700658 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800659 mSharedBufferHasBeenQueued = true;
660 }
661
Robert Carr9f31e292016-04-11 11:15:32 -0700662 mQueueBufferCondition.broadcast();
663
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800664 return err;
665}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700666
Brian Anderson069b3652016-07-22 10:32:47 -0700667void Surface::querySupportedTimestampsLocked() const {
668 // mMutex must be locked when calling this method.
669
670 if (mQueriedSupportedTimestamps) {
671 return;
672 }
673 mQueriedSupportedTimestamps = true;
674
Brian Anderson3890c392016-07-25 12:48:08 -0700675 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson3da8d272016-07-28 16:20:47 -0700676 status_t err = composerService()->getSupportedFrameTimestamps(
Brian Anderson069b3652016-07-22 10:32:47 -0700677 &supportedFrameTimestamps);
678
679 if (err != NO_ERROR) {
680 return;
681 }
682
683 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700684 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700685 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700686 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700687 mFrameTimestampsSupportsRetire = true;
688 }
689 }
690}
691
Mathias Agopiana67932f2011-04-20 14:20:59 -0700692int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800693 ATRACE_CALL();
694 ALOGV("Surface::query");
695 { // scope for the lock
696 Mutex::Autolock lock(mMutex);
697 switch (what) {
698 case NATIVE_WINDOW_FORMAT:
699 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800700 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800701 return NO_ERROR;
702 }
703 break;
704 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700705 if (composerService()->authenticateSurfaceTexture(
706 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800707 *value = 1;
708 } else {
709 *value = 0;
710 }
711 return NO_ERROR;
712 }
713 case NATIVE_WINDOW_CONCRETE_TYPE:
714 *value = NATIVE_WINDOW_SURFACE;
715 return NO_ERROR;
716 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800717 *value = static_cast<int>(
718 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800719 return NO_ERROR;
720 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800721 *value = static_cast<int>(
722 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800723 return NO_ERROR;
724 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800725 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800726 return NO_ERROR;
727 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
728 status_t err = NO_ERROR;
729 if (!mConsumerRunningBehind) {
730 *value = 0;
731 } else {
732 err = mGraphicBufferProducer->query(what, value);
733 if (err == NO_ERROR) {
734 mConsumerRunningBehind = *value;
735 }
736 }
737 return err;
738 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700739 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
740 int64_t durationUs = mLastDequeueDuration / 1000;
741 *value = durationUs > std::numeric_limits<int>::max() ?
742 std::numeric_limits<int>::max() :
743 static_cast<int>(durationUs);
744 return NO_ERROR;
745 }
746 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
747 int64_t durationUs = mLastQueueDuration / 1000;
748 *value = durationUs > std::numeric_limits<int>::max() ?
749 std::numeric_limits<int>::max() :
750 static_cast<int>(durationUs);
751 return NO_ERROR;
752 }
Brian Anderson069b3652016-07-22 10:32:47 -0700753 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
754 querySupportedTimestampsLocked();
755 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
756 return NO_ERROR;
757 }
758 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
759 querySupportedTimestampsLocked();
760 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
761 return NO_ERROR;
762 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800763 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700764 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800765 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800766}
767
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800768int Surface::perform(int operation, va_list args)
769{
770 int res = NO_ERROR;
771 switch (operation) {
772 case NATIVE_WINDOW_CONNECT:
773 // deprecated. must return NO_ERROR.
774 break;
775 case NATIVE_WINDOW_DISCONNECT:
776 // deprecated. must return NO_ERROR.
777 break;
778 case NATIVE_WINDOW_SET_USAGE:
779 res = dispatchSetUsage(args);
780 break;
781 case NATIVE_WINDOW_SET_CROP:
782 res = dispatchSetCrop(args);
783 break;
784 case NATIVE_WINDOW_SET_BUFFER_COUNT:
785 res = dispatchSetBufferCount(args);
786 break;
787 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
788 res = dispatchSetBuffersGeometry(args);
789 break;
790 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
791 res = dispatchSetBuffersTransform(args);
792 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700793 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
794 res = dispatchSetBuffersStickyTransform(args);
795 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800796 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
797 res = dispatchSetBuffersTimestamp(args);
798 break;
799 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
800 res = dispatchSetBuffersDimensions(args);
801 break;
802 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
803 res = dispatchSetBuffersUserDimensions(args);
804 break;
805 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
806 res = dispatchSetBuffersFormat(args);
807 break;
808 case NATIVE_WINDOW_LOCK:
809 res = dispatchLock(args);
810 break;
811 case NATIVE_WINDOW_UNLOCK_AND_POST:
812 res = dispatchUnlockAndPost(args);
813 break;
814 case NATIVE_WINDOW_SET_SCALING_MODE:
815 res = dispatchSetScalingMode(args);
816 break;
817 case NATIVE_WINDOW_API_CONNECT:
818 res = dispatchConnect(args);
819 break;
820 case NATIVE_WINDOW_API_DISCONNECT:
821 res = dispatchDisconnect(args);
822 break;
Rachad7cb0d392014-07-29 17:53:53 -0700823 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
824 res = dispatchSetSidebandStream(args);
825 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800826 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
827 res = dispatchSetBuffersDataSpace(args);
828 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700829 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
830 res = dispatchSetSurfaceDamage(args);
831 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700832 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
833 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700834 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800835 case NATIVE_WINDOW_SET_AUTO_REFRESH:
836 res = dispatchSetAutoRefresh(args);
837 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700838 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
839 res = dispatchEnableFrameTimestamps(args);
840 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800841 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
842 res = dispatchGetFrameTimestamps(args);
843 break;
Ian Elliott62c48c92017-01-20 13:13:20 -0700844 case NATIVE_WINDOW_GET_REFRESH_CYCLE_PERIOD:
845 res = dispatchGetDisplayRefreshCyclePeriod(args);
846 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800847 default:
848 res = NAME_NOT_FOUND;
849 break;
850 }
851 return res;
852}
Mathias Agopiana138f892010-05-21 17:24:35 -0700853
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800854int Surface::dispatchConnect(va_list args) {
855 int api = va_arg(args, int);
856 return connect(api);
857}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800858
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800859int Surface::dispatchDisconnect(va_list args) {
860 int api = va_arg(args, int);
861 return disconnect(api);
862}
863
864int Surface::dispatchSetUsage(va_list args) {
865 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800866 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800867}
868
869int Surface::dispatchSetCrop(va_list args) {
870 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
871 return setCrop(reinterpret_cast<Rect const*>(rect));
872}
873
874int Surface::dispatchSetBufferCount(va_list args) {
875 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800876 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800877}
878
879int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800880 uint32_t width = va_arg(args, uint32_t);
881 uint32_t height = va_arg(args, uint32_t);
882 PixelFormat format = va_arg(args, PixelFormat);
883 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800884 if (err != 0) {
885 return err;
886 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800887 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800888}
889
890int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800891 uint32_t width = va_arg(args, uint32_t);
892 uint32_t height = va_arg(args, uint32_t);
893 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800894}
895
896int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800897 uint32_t width = va_arg(args, uint32_t);
898 uint32_t height = va_arg(args, uint32_t);
899 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800900}
901
902int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800903 PixelFormat format = va_arg(args, PixelFormat);
904 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800905}
906
907int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800908 int mode = va_arg(args, int);
909 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800910}
911
912int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800913 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800914 return setBuffersTransform(transform);
915}
916
Ruben Brunk1681d952014-06-27 15:51:55 -0700917int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800918 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700919 return setBuffersStickyTransform(transform);
920}
921
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800922int Surface::dispatchSetBuffersTimestamp(va_list args) {
923 int64_t timestamp = va_arg(args, int64_t);
924 return setBuffersTimestamp(timestamp);
925}
926
927int Surface::dispatchLock(va_list args) {
928 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
929 ARect* inOutDirtyBounds = va_arg(args, ARect*);
930 return lock(outBuffer, inOutDirtyBounds);
931}
932
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800933int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800934 return unlockAndPost();
935}
936
Rachad7cb0d392014-07-29 17:53:53 -0700937int Surface::dispatchSetSidebandStream(va_list args) {
938 native_handle_t* sH = va_arg(args, native_handle_t*);
939 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
940 setSidebandStream(sidebandHandle);
941 return OK;
942}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800943
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800944int Surface::dispatchSetBuffersDataSpace(va_list args) {
945 android_dataspace dataspace =
946 static_cast<android_dataspace>(va_arg(args, int));
947 return setBuffersDataSpace(dataspace);
948}
949
Dan Stoza5065a552015-03-17 16:23:42 -0700950int Surface::dispatchSetSurfaceDamage(va_list args) {
951 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
952 size_t numRects = va_arg(args, size_t);
953 setSurfaceDamage(rects, numRects);
954 return NO_ERROR;
955}
956
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700957int Surface::dispatchSetSharedBufferMode(va_list args) {
958 bool sharedBufferMode = va_arg(args, int);
959 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800960}
961
962int Surface::dispatchSetAutoRefresh(va_list args) {
963 bool autoRefresh = va_arg(args, int);
964 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700965}
966
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700967int Surface::dispatchEnableFrameTimestamps(va_list args) {
968 bool enable = va_arg(args, int);
969 enableFrameTimestamps(enable);
970 return NO_ERROR;
971}
972
Pablo Ceballosce796e72016-02-04 19:10:51 -0800973int Surface::dispatchGetFrameTimestamps(va_list args) {
974 uint32_t framesAgo = va_arg(args, uint32_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -0700975 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800976 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700977 nsecs_t* outLatchTime = va_arg(args, int64_t*);
978 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
979 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800980 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700981 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800982 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700983 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800984 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700985 return getFrameTimestamps(getNextFrameNumber() - 1 - framesAgo,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700986 outRequestedPresentTime, outAcquireTime, outLatchTime,
987 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700988 outGlCompositionDoneTime, outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700989 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800990}
991
Ian Elliott62c48c92017-01-20 13:13:20 -0700992int Surface::dispatchGetDisplayRefreshCyclePeriod(va_list args) {
993 nsecs_t* outMinRefreshDuration = va_arg(args, int64_t*);
994 nsecs_t* outMaxRefreshDuration = va_arg(args, int64_t*);
995 return getDisplayRefreshCyclePeriod(outMinRefreshDuration,
996 outMaxRefreshDuration);
997}
998
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800999int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001000 static sp<IProducerListener> listener = new DummyProducerListener();
1001 return connect(api, listener);
1002}
1003
1004int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001005 ATRACE_CALL();
1006 ALOGV("Surface::connect");
1007 Mutex::Autolock lock(mMutex);
1008 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001009 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001010 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001011 mDefaultWidth = output.width;
1012 mDefaultHeight = output.height;
1013 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001014
1015 // Disable transform hint if sticky transform is set.
1016 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001017 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001018 }
1019
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001020 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001021 }
1022 if (!err && api == NATIVE_WINDOW_API_CPU) {
1023 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001024 // Clear the dirty region in case we're switching from a non-CPU API
1025 mDirtyRegion.clear();
1026 } else if (!err) {
1027 // Initialize the dirty region for tracking surface damage
1028 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001029 }
Dan Stoza5065a552015-03-17 16:23:42 -07001030
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001031 return err;
1032}
1033
Mathias Agopian365857d2013-09-11 19:35:45 -07001034
Robert Carr97b9c862016-09-08 13:54:35 -07001035int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001036 ATRACE_CALL();
1037 ALOGV("Surface::disconnect");
1038 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001039 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1040 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001041 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001042 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001043 if (!err) {
1044 mReqFormat = 0;
1045 mReqWidth = 0;
1046 mReqHeight = 0;
1047 mReqUsage = 0;
1048 mCrop.clear();
1049 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1050 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001051 mStickyTransform = 0;
1052
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001053 if (api == NATIVE_WINDOW_API_CPU) {
1054 mConnectedToCpu = false;
1055 }
1056 }
1057 return err;
1058}
1059
Dan Stozad9c49712015-04-27 11:06:01 -07001060int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001061 sp<Fence>* outFence) {
1062 ATRACE_CALL();
1063 ALOGV("Surface::detachNextBuffer");
1064
1065 if (outBuffer == NULL || outFence == NULL) {
1066 return BAD_VALUE;
1067 }
1068
1069 Mutex::Autolock lock(mMutex);
1070
1071 sp<GraphicBuffer> buffer(NULL);
1072 sp<Fence> fence(NULL);
1073 status_t result = mGraphicBufferProducer->detachNextBuffer(
1074 &buffer, &fence);
1075 if (result != NO_ERROR) {
1076 return result;
1077 }
1078
Dan Stozad9c49712015-04-27 11:06:01 -07001079 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001080 if (fence != NULL && fence->isValid()) {
1081 *outFence = fence;
1082 } else {
1083 *outFence = Fence::NO_FENCE;
1084 }
1085
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001086 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1087 if (mSlots[i].buffer != NULL &&
1088 mSlots[i].buffer->handle == buffer->handle) {
1089 mSlots[i].buffer = NULL;
1090 }
1091 }
1092
Dan Stoza231832e2015-03-11 11:55:01 -07001093 return NO_ERROR;
1094}
1095
1096int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1097{
1098 ATRACE_CALL();
1099 ALOGV("Surface::attachBuffer");
1100
1101 Mutex::Autolock lock(mMutex);
1102
1103 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001104 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1105 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001106 int32_t attachedSlot = -1;
1107 status_t result = mGraphicBufferProducer->attachBuffer(
1108 &attachedSlot, graphicBuffer);
1109 if (result != NO_ERROR) {
1110 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001111 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001112 return result;
1113 }
1114 mSlots[attachedSlot].buffer = graphicBuffer;
1115
1116 return NO_ERROR;
1117}
1118
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001119int Surface::setUsage(uint32_t reqUsage)
1120{
1121 ALOGV("Surface::setUsage");
1122 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001123 if (reqUsage != mReqUsage) {
1124 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1125 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001126 mReqUsage = reqUsage;
1127 return OK;
1128}
1129
1130int Surface::setCrop(Rect const* rect)
1131{
1132 ATRACE_CALL();
1133
Pablo Ceballos60d69222015-08-07 14:47:20 -07001134 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001135 if (rect == NULL || rect->isEmpty()) {
1136 realRect.clear();
1137 } else {
1138 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001139 }
1140
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001141 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1142 realRect.left, realRect.top, realRect.right, realRect.bottom);
1143
1144 Mutex::Autolock lock(mMutex);
1145 mCrop = realRect;
1146 return NO_ERROR;
1147}
1148
1149int Surface::setBufferCount(int bufferCount)
1150{
1151 ATRACE_CALL();
1152 ALOGV("Surface::setBufferCount");
1153 Mutex::Autolock lock(mMutex);
1154
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001155 status_t err = NO_ERROR;
1156 if (bufferCount == 0) {
1157 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1158 } else {
1159 int minUndequeuedBuffers = 0;
1160 err = mGraphicBufferProducer->query(
1161 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1162 if (err == NO_ERROR) {
1163 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1164 bufferCount - minUndequeuedBuffers);
1165 }
1166 }
Mathias Agopian90147262010-01-22 11:47:55 -08001167
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001168 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1169 bufferCount, strerror(-err));
1170
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001171 return err;
1172}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001173
Pablo Ceballosfa455352015-08-12 17:47:47 -07001174int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1175 ATRACE_CALL();
1176 ALOGV("Surface::setMaxDequeuedBufferCount");
1177 Mutex::Autolock lock(mMutex);
1178
1179 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1180 maxDequeuedBuffers);
1181 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1182 "returned %s", maxDequeuedBuffers, strerror(-err));
1183
Pablo Ceballosfa455352015-08-12 17:47:47 -07001184 return err;
1185}
1186
1187int Surface::setAsyncMode(bool async) {
1188 ATRACE_CALL();
1189 ALOGV("Surface::setAsyncMode");
1190 Mutex::Autolock lock(mMutex);
1191
1192 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1193 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1194 async, strerror(-err));
1195
Pablo Ceballosfa455352015-08-12 17:47:47 -07001196 return err;
1197}
1198
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001199int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001200 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001201 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001202 Mutex::Autolock lock(mMutex);
1203
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001204 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1205 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001206 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001207 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001208 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001209 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1210 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001211
1212 return err;
1213}
1214
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001215int Surface::setAutoRefresh(bool autoRefresh) {
1216 ATRACE_CALL();
1217 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1218 Mutex::Autolock lock(mMutex);
1219
1220 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1221 if (err == NO_ERROR) {
1222 mAutoRefresh = autoRefresh;
1223 }
1224 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1225 autoRefresh, strerror(-err));
1226 return err;
1227}
1228
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001229int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001230{
1231 ATRACE_CALL();
1232 ALOGV("Surface::setBuffersDimensions");
1233
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001234 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001235 return BAD_VALUE;
1236
1237 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001238 if (width != mReqWidth || height != mReqHeight) {
1239 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1240 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001241 mReqWidth = width;
1242 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001243 return NO_ERROR;
1244}
1245
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001246int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001247{
1248 ATRACE_CALL();
1249 ALOGV("Surface::setBuffersUserDimensions");
1250
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001251 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001252 return BAD_VALUE;
1253
1254 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001255 if (width != mUserWidth || height != mUserHeight) {
1256 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1257 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001258 mUserWidth = width;
1259 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001260 return NO_ERROR;
1261}
1262
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001263int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001264{
1265 ALOGV("Surface::setBuffersFormat");
1266
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001267 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001268 if (format != mReqFormat) {
1269 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1270 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001271 mReqFormat = format;
1272 return NO_ERROR;
1273}
1274
1275int Surface::setScalingMode(int mode)
1276{
1277 ATRACE_CALL();
1278 ALOGV("Surface::setScalingMode(%d)", mode);
1279
1280 switch (mode) {
1281 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1282 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1283 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001284 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001285 break;
1286 default:
1287 ALOGE("unknown scaling mode: %d", mode);
1288 return BAD_VALUE;
1289 }
1290
1291 Mutex::Autolock lock(mMutex);
1292 mScalingMode = mode;
1293 return NO_ERROR;
1294}
1295
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001296int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001297{
1298 ATRACE_CALL();
1299 ALOGV("Surface::setBuffersTransform");
1300 Mutex::Autolock lock(mMutex);
1301 mTransform = transform;
1302 return NO_ERROR;
1303}
1304
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001305int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001306{
1307 ATRACE_CALL();
1308 ALOGV("Surface::setBuffersStickyTransform");
1309 Mutex::Autolock lock(mMutex);
1310 mStickyTransform = transform;
1311 return NO_ERROR;
1312}
1313
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001314int Surface::setBuffersTimestamp(int64_t timestamp)
1315{
1316 ALOGV("Surface::setBuffersTimestamp");
1317 Mutex::Autolock lock(mMutex);
1318 mTimestamp = timestamp;
1319 return NO_ERROR;
1320}
1321
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001322int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1323{
1324 ALOGV("Surface::setBuffersDataSpace");
1325 Mutex::Autolock lock(mMutex);
1326 mDataSpace = dataSpace;
1327 return NO_ERROR;
1328}
1329
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001330void Surface::freeAllBuffers() {
1331 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1332 mSlots[i].buffer = 0;
1333 }
1334}
1335
Dan Stoza5065a552015-03-17 16:23:42 -07001336void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1337 ATRACE_CALL();
1338 ALOGV("Surface::setSurfaceDamage");
1339 Mutex::Autolock lock(mMutex);
1340
Dan Stozac62acbd2015-04-21 16:42:49 -07001341 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001342 mDirtyRegion = Region::INVALID_REGION;
1343 return;
1344 }
1345
1346 mDirtyRegion.clear();
1347 for (size_t r = 0; r < numRects; ++r) {
1348 // We intentionally flip top and bottom here, since because they're
1349 // specified with a bottom-left origin, top > bottom, which fails
1350 // validation in the Region class. We will fix this up when we flip to a
1351 // top-left origin in queueBuffer.
1352 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1353 mDirtyRegion.orSelf(rect);
1354 }
1355}
1356
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001357// ----------------------------------------------------------------------
1358// the lock/unlock APIs must be used from the same thread
1359
1360static status_t copyBlt(
1361 const sp<GraphicBuffer>& dst,
1362 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001363 const Region& reg,
1364 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001365{
1366 // src and dst with, height and format must be identical. no verification
1367 // is done here.
1368 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001369 uint8_t* src_bits = NULL;
1370 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1371 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001372 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1373
1374 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001375 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1376 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001377 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001378 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001379
1380 Region::const_iterator head(reg.begin());
1381 Region::const_iterator tail(reg.end());
1382 if (head != tail && src_bits && dst_bits) {
1383 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001384 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1385 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001386
1387 while (head != tail) {
1388 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001389 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001390 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001391 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1392 uint8_t const * s = src_bits +
1393 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1394 uint8_t * d = dst_bits +
1395 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001396 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001397 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001398 h = 1;
1399 }
1400 do {
1401 memcpy(d, s, size);
1402 d += dbpr;
1403 s += sbpr;
1404 } while (--h > 0);
1405 }
1406 }
1407
1408 if (src_bits)
1409 src->unlock();
1410
1411 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001412 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001413
1414 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415}
1416
Mathias Agopiana138f892010-05-21 17:24:35 -07001417// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001418
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001419status_t Surface::lock(
1420 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1421{
1422 if (mLockedBuffer != 0) {
1423 ALOGE("Surface::lock failed, already locked");
1424 return INVALID_OPERATION;
1425 }
1426
1427 if (!mConnectedToCpu) {
1428 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1429 if (err) {
1430 return err;
1431 }
1432 // we're intending to do software rendering from this point
1433 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1434 }
1435
1436 ANativeWindowBuffer* out;
1437 int fenceFd = -1;
1438 status_t err = dequeueBuffer(&out, &fenceFd);
1439 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1440 if (err == NO_ERROR) {
1441 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001442 const Rect bounds(backBuffer->width, backBuffer->height);
1443
1444 Region newDirtyRegion;
1445 if (inOutDirtyBounds) {
1446 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1447 newDirtyRegion.andSelf(bounds);
1448 } else {
1449 newDirtyRegion.set(bounds);
1450 }
1451
1452 // figure out if we can copy the frontbuffer back
1453 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1454 const bool canCopyBack = (frontBuffer != 0 &&
1455 backBuffer->width == frontBuffer->width &&
1456 backBuffer->height == frontBuffer->height &&
1457 backBuffer->format == frontBuffer->format);
1458
1459 if (canCopyBack) {
1460 // copy the area that is invalid and not repainted this round
1461 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001462 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001463 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001464 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001465 } else {
1466 // if we can't copy-back anything, modify the user's dirty
1467 // region to make sure they redraw the whole buffer
1468 newDirtyRegion.set(bounds);
1469 mDirtyRegion.clear();
1470 Mutex::Autolock lock(mMutex);
1471 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1472 mSlots[i].dirtyRegion.clear();
1473 }
1474 }
1475
1476
1477 { // scope for the lock
1478 Mutex::Autolock lock(mMutex);
1479 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1480 if (backBufferSlot >= 0) {
1481 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1482 mDirtyRegion.subtract(dirtyRegion);
1483 dirtyRegion = newDirtyRegion;
1484 }
1485 }
1486
1487 mDirtyRegion.orSelf(newDirtyRegion);
1488 if (inOutDirtyBounds) {
1489 *inOutDirtyBounds = newDirtyRegion.getBounds();
1490 }
1491
1492 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001493 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001494 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001495 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001496
1497 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1498 backBuffer->handle);
1499
1500 if (res != 0) {
1501 err = INVALID_OPERATION;
1502 } else {
1503 mLockedBuffer = backBuffer;
1504 outBuffer->width = backBuffer->width;
1505 outBuffer->height = backBuffer->height;
1506 outBuffer->stride = backBuffer->stride;
1507 outBuffer->format = backBuffer->format;
1508 outBuffer->bits = vaddr;
1509 }
1510 }
1511 return err;
1512}
1513
1514status_t Surface::unlockAndPost()
1515{
1516 if (mLockedBuffer == 0) {
1517 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1518 return INVALID_OPERATION;
1519 }
1520
Francis Hart8f396012014-04-01 15:30:53 +03001521 int fd = -1;
1522 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001523 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1524
Francis Hart8f396012014-04-01 15:30:53 +03001525 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001526 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1527 mLockedBuffer->handle, strerror(-err));
1528
1529 mPostedBuffer = mLockedBuffer;
1530 mLockedBuffer = 0;
1531 return err;
1532}
1533
Robert Carr9f31e292016-04-11 11:15:32 -07001534bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1535 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001536 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001537 return true;
1538 }
1539 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1540}
1541
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001542status_t Surface::getUniqueId(uint64_t* outId) const {
1543 Mutex::Autolock lock(mMutex);
1544 return mGraphicBufferProducer->getUniqueId(outId);
1545}
1546
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001547namespace view {
1548
1549status_t Surface::writeToParcel(Parcel* parcel) const {
1550 return writeToParcel(parcel, false);
1551}
1552
1553status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const {
1554 if (parcel == nullptr) return BAD_VALUE;
1555
1556 status_t res = OK;
1557
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001558 if (!nameAlreadyWritten) {
1559 res = parcel->writeString16(name);
1560 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001561
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001562 /* isSingleBuffered defaults to no */
1563 res = parcel->writeInt32(0);
1564 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001565 }
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001566
1567 res = parcel->writeStrongBinder(
1568 IGraphicBufferProducer::asBinder(graphicBufferProducer));
1569
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001570 return res;
1571}
1572
1573status_t Surface::readFromParcel(const Parcel* parcel) {
1574 return readFromParcel(parcel, false);
1575}
1576
1577status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) {
1578 if (parcel == nullptr) return BAD_VALUE;
1579
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001580 status_t res = OK;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001581 if (!nameAlreadyRead) {
1582 name = readMaybeEmptyString16(parcel);
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001583 // Discard this for now
1584 int isSingleBuffered;
1585 res = parcel->readInt32(&isSingleBuffered);
1586 if (res != OK) {
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001587 ALOGE("Can't read isSingleBuffered");
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001588 return res;
1589 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001590 }
1591
1592 sp<IBinder> binder;
1593
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001594 res = parcel->readNullableStrongBinder(&binder);
1595 if (res != OK) {
1596 ALOGE("%s: Can't read strong binder", __FUNCTION__);
1597 return res;
1598 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001599
1600 graphicBufferProducer = interface_cast<IGraphicBufferProducer>(binder);
1601
1602 return OK;
1603}
1604
1605String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
1606 size_t len;
1607 const char16_t* str = parcel->readString16Inplace(&len);
1608 if (str != nullptr) {
1609 return String16(str, len);
1610 } else {
1611 return String16();
1612 }
1613}
1614
1615} // namespace view
1616
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617}; // namespace android