blob: c2ed91a81c805e43771fcc782d35dfcacc737755 [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 Elliottbe833a22017-01-25 13:09:20 -0700265status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700266 ATRACE_CALL();
267
268 DisplayStatInfo stats;
269 status_t err = composerService()->getDisplayStats(NULL, &stats);
270
Ian Elliottbe833a22017-01-25 13:09:20 -0700271 *outRefreshDuration = stats.vsyncPeriod;
Ian Elliott62c48c92017-01-20 13:13:20 -0700272
273 return NO_ERROR;
274}
Pablo Ceballosce796e72016-02-04 19:10:51 -0800275
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800276int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
277 Surface* c = getSelf(window);
278 return c->setSwapInterval(interval);
279}
280
281int Surface::hook_dequeueBuffer(ANativeWindow* window,
282 ANativeWindowBuffer** buffer, int* fenceFd) {
283 Surface* c = getSelf(window);
284 return c->dequeueBuffer(buffer, fenceFd);
285}
286
287int Surface::hook_cancelBuffer(ANativeWindow* window,
288 ANativeWindowBuffer* buffer, int fenceFd) {
289 Surface* c = getSelf(window);
290 return c->cancelBuffer(buffer, fenceFd);
291}
292
293int Surface::hook_queueBuffer(ANativeWindow* window,
294 ANativeWindowBuffer* buffer, int fenceFd) {
295 Surface* c = getSelf(window);
296 return c->queueBuffer(buffer, fenceFd);
297}
298
299int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
300 ANativeWindowBuffer** buffer) {
301 Surface* c = getSelf(window);
302 ANativeWindowBuffer* buf;
303 int fenceFd = -1;
304 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600305 if (result != OK) {
306 return result;
307 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800308 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700309 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800310 if (waitResult != OK) {
311 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
312 waitResult);
313 c->cancelBuffer(buf, -1);
314 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700315 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800316 *buffer = buf;
317 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700318}
319
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800320int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
321 ANativeWindowBuffer* buffer) {
322 Surface* c = getSelf(window);
323 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700324}
325
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800326int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
327 ANativeWindowBuffer* buffer) {
328 Surface* c = getSelf(window);
329 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700330}
331
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800332int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
333 ANativeWindowBuffer* buffer) {
334 Surface* c = getSelf(window);
335 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700336}
Mathias Agopian62185b72009-04-16 16:19:50 -0700337
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800338int Surface::hook_query(const ANativeWindow* window,
339 int what, int* value) {
340 const Surface* c = getSelf(window);
341 return c->query(what, value);
342}
343
344int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
345 va_list args;
346 va_start(args, operation);
347 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700348 int result = c->perform(operation, args);
349 va_end(args);
350 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800351}
352
353int Surface::setSwapInterval(int interval) {
354 ATRACE_CALL();
355 // EGL specification states:
356 // interval is silently clamped to minimum and maximum implementation
357 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800358
359 if (interval < minSwapInterval)
360 interval = minSwapInterval;
361
362 if (interval > maxSwapInterval)
363 interval = maxSwapInterval;
364
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700365 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700366 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800367
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700368 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800369}
370
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700371int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800372 ATRACE_CALL();
373 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800374
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800375 uint32_t reqWidth;
376 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800377 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800378 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700379 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800380
381 {
382 Mutex::Autolock lock(mMutex);
383
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800384 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
385 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800386
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800387 reqFormat = mReqFormat;
388 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800389
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700390 enableFrameTimestamps = mEnableFrameTimestamps;
391
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700392 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800393 BufferItem::INVALID_BUFFER_SLOT) {
394 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
395 if (gbuf != NULL) {
396 *buffer = gbuf.get();
397 *fenceFd = -1;
398 return OK;
399 }
400 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800401 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
402
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800403 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800404 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700405 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700406
407 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700408 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700409 reqWidth, reqHeight, reqFormat, reqUsage,
410 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700411 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800412
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800413 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700414 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
415 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
416 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800417 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700418 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800419
420 Mutex::Autolock lock(mMutex);
421
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800422 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700423
424 // this should never happen
425 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
426
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800427 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
428 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700429 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700430
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700431 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700432 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700433 }
434
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800435 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
436 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
437 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700438 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700439 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800440 return result;
441 }
442 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700443
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800444 if (fence->isValid()) {
445 *fenceFd = fence->dup();
446 if (*fenceFd == -1) {
447 ALOGE("dequeueBuffer: error duping fence: %d", errno);
448 // dup() should never fail; something is badly wrong. Soldier on
449 // and hope for the best; the worst that should happen is some
450 // visible corruption that lasts until the next frame.
451 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700452 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800453 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700454 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800455
456 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800457
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700458 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800459 mSharedBufferSlot = buf;
460 mSharedBufferHasBeenQueued = false;
461 } else if (mSharedBufferSlot == buf) {
462 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
463 mSharedBufferHasBeenQueued = false;
464 }
465
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800466 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700467}
468
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800469int Surface::cancelBuffer(android_native_buffer_t* buffer,
470 int fenceFd) {
471 ATRACE_CALL();
472 ALOGV("Surface::cancelBuffer");
473 Mutex::Autolock lock(mMutex);
474 int i = getSlotFromBufferLocked(buffer);
475 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900476 if (fenceFd >= 0) {
477 close(fenceFd);
478 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800479 return i;
480 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800481 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
482 if (fenceFd >= 0) {
483 close(fenceFd);
484 }
485 return OK;
486 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800487 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
488 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800489
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700490 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800491 mSharedBufferHasBeenQueued = true;
492 }
493
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800494 return OK;
495}
496
497int Surface::getSlotFromBufferLocked(
498 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800499 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
500 if (mSlots[i].buffer != NULL &&
501 mSlots[i].buffer->handle == buffer->handle) {
502 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700503 }
504 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800505 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
506 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700507}
508
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800509int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800510 ALOGV("Surface::lockBuffer");
511 Mutex::Autolock lock(mMutex);
512 return OK;
513}
Mathias Agopian631f3582010-05-25 17:51:34 -0700514
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800515int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
516 ATRACE_CALL();
517 ALOGV("Surface::queueBuffer");
518 Mutex::Autolock lock(mMutex);
519 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700520 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800521
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800522 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700523 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700524 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700525 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700526 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800527 } else {
528 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700529 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800530 int i = getSlotFromBufferLocked(buffer);
531 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900532 if (fenceFd >= 0) {
533 close(fenceFd);
534 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800535 return i;
536 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800537 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
538 if (fenceFd >= 0) {
539 close(fenceFd);
540 }
541 return OK;
542 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800545 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700546 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800547 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800549 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
550 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700551 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800552 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700553 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700554
Dan Stozac62acbd2015-04-21 16:42:49 -0700555 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700556 input.setSurfaceDamage(Region::INVALID_REGION);
557 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700558 // Here we do two things:
559 // 1) The surface damage was specified using the OpenGL ES convention of
560 // the origin being in the bottom-left corner. Here we flip to the
561 // convention that the rest of the system uses (top-left corner) by
562 // subtracting all top/bottom coordinates from the buffer height.
563 // 2) If the buffer is coming in rotated (for example, because the EGL
564 // implementation is reacting to the transform hint coming back from
565 // SurfaceFlinger), the surface damage needs to be rotated the
566 // opposite direction, since it was generated assuming an unrotated
567 // buffer (the app doesn't know that the EGL implementation is
568 // reacting to the transform hint behind its back). The
569 // transformations in the switch statement below apply those
570 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
571
572 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700573 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700574 bool rotated90 = (mTransform ^ mStickyTransform) &
575 NATIVE_WINDOW_TRANSFORM_ROT_90;
576 if (rotated90) {
577 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700578 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700579
Dan Stoza5065a552015-03-17 16:23:42 -0700580 Region flippedRegion;
581 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700582 int left = rect.left;
583 int right = rect.right;
584 int top = height - rect.bottom; // Flip from OpenGL convention
585 int bottom = height - rect.top; // Flip from OpenGL convention
586 switch (mTransform ^ mStickyTransform) {
587 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
588 // Rotate 270 degrees
589 Rect flippedRect{top, width - right, bottom, width - left};
590 flippedRegion.orSelf(flippedRect);
591 break;
592 }
593 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
594 // Rotate 180 degrees
595 Rect flippedRect{width - right, height - bottom,
596 width - left, height - top};
597 flippedRegion.orSelf(flippedRect);
598 break;
599 }
600 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
601 // Rotate 90 degrees
602 Rect flippedRect{height - bottom, left,
603 height - top, right};
604 flippedRegion.orSelf(flippedRect);
605 break;
606 }
607 default: {
608 Rect flippedRect{left, top, right, bottom};
609 flippedRegion.orSelf(flippedRect);
610 break;
611 }
612 }
Dan Stoza5065a552015-03-17 16:23:42 -0700613 }
614
615 input.setSurfaceDamage(flippedRegion);
616 }
617
Dan Stoza70ccba52016-07-01 14:00:40 -0700618 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800619 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700620 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800621 if (err != OK) {
622 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
623 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800624
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700625 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700626 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700627 // Update timestamps with the local acquire fence.
628 // The consumer doesn't send it back to prevent us from having two
629 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700630 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700631 std::make_shared<FenceTime>(std::move(fence)));
632
633 // Cache timestamps of signaled fences so we can close their file
634 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700635 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700636 }
637
Brian Anderson50143b32016-09-30 14:01:24 -0700638 mLastFrameNumber = mNextFrameNumber;
639
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700640 mDefaultWidth = output.width;
641 mDefaultHeight = output.height;
642 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700643
Ruben Brunk1681d952014-06-27 15:51:55 -0700644 // Disable transform hint if sticky transform is set.
645 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700646 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700647 }
648
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700649 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700650
Dan Stozac62acbd2015-04-21 16:42:49 -0700651 if (!mConnectedToCpu) {
652 // Clear surface damage back to full-buffer
653 mDirtyRegion = Region::INVALID_REGION;
654 }
Dan Stoza5065a552015-03-17 16:23:42 -0700655
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700656 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800657 mSharedBufferHasBeenQueued = true;
658 }
659
Robert Carr9f31e292016-04-11 11:15:32 -0700660 mQueueBufferCondition.broadcast();
661
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800662 return err;
663}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700664
Brian Anderson069b3652016-07-22 10:32:47 -0700665void Surface::querySupportedTimestampsLocked() const {
666 // mMutex must be locked when calling this method.
667
668 if (mQueriedSupportedTimestamps) {
669 return;
670 }
671 mQueriedSupportedTimestamps = true;
672
Brian Anderson3890c392016-07-25 12:48:08 -0700673 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson3da8d272016-07-28 16:20:47 -0700674 status_t err = composerService()->getSupportedFrameTimestamps(
Brian Anderson069b3652016-07-22 10:32:47 -0700675 &supportedFrameTimestamps);
676
677 if (err != NO_ERROR) {
678 return;
679 }
680
681 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700682 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700683 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700684 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700685 mFrameTimestampsSupportsRetire = true;
686 }
687 }
688}
689
Mathias Agopiana67932f2011-04-20 14:20:59 -0700690int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800691 ATRACE_CALL();
692 ALOGV("Surface::query");
693 { // scope for the lock
694 Mutex::Autolock lock(mMutex);
695 switch (what) {
696 case NATIVE_WINDOW_FORMAT:
697 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800698 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800699 return NO_ERROR;
700 }
701 break;
702 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700703 if (composerService()->authenticateSurfaceTexture(
704 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800705 *value = 1;
706 } else {
707 *value = 0;
708 }
709 return NO_ERROR;
710 }
711 case NATIVE_WINDOW_CONCRETE_TYPE:
712 *value = NATIVE_WINDOW_SURFACE;
713 return NO_ERROR;
714 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800715 *value = static_cast<int>(
716 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800717 return NO_ERROR;
718 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800719 *value = static_cast<int>(
720 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800721 return NO_ERROR;
722 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800723 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800724 return NO_ERROR;
725 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
726 status_t err = NO_ERROR;
727 if (!mConsumerRunningBehind) {
728 *value = 0;
729 } else {
730 err = mGraphicBufferProducer->query(what, value);
731 if (err == NO_ERROR) {
732 mConsumerRunningBehind = *value;
733 }
734 }
735 return err;
736 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700737 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
738 int64_t durationUs = mLastDequeueDuration / 1000;
739 *value = durationUs > std::numeric_limits<int>::max() ?
740 std::numeric_limits<int>::max() :
741 static_cast<int>(durationUs);
742 return NO_ERROR;
743 }
744 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
745 int64_t durationUs = mLastQueueDuration / 1000;
746 *value = durationUs > std::numeric_limits<int>::max() ?
747 std::numeric_limits<int>::max() :
748 static_cast<int>(durationUs);
749 return NO_ERROR;
750 }
Brian Anderson069b3652016-07-22 10:32:47 -0700751 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
752 querySupportedTimestampsLocked();
753 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
754 return NO_ERROR;
755 }
756 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
757 querySupportedTimestampsLocked();
758 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
759 return NO_ERROR;
760 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800761 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700762 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800763 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800764}
765
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800766int Surface::perform(int operation, va_list args)
767{
768 int res = NO_ERROR;
769 switch (operation) {
770 case NATIVE_WINDOW_CONNECT:
771 // deprecated. must return NO_ERROR.
772 break;
773 case NATIVE_WINDOW_DISCONNECT:
774 // deprecated. must return NO_ERROR.
775 break;
776 case NATIVE_WINDOW_SET_USAGE:
777 res = dispatchSetUsage(args);
778 break;
779 case NATIVE_WINDOW_SET_CROP:
780 res = dispatchSetCrop(args);
781 break;
782 case NATIVE_WINDOW_SET_BUFFER_COUNT:
783 res = dispatchSetBufferCount(args);
784 break;
785 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
786 res = dispatchSetBuffersGeometry(args);
787 break;
788 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
789 res = dispatchSetBuffersTransform(args);
790 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700791 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
792 res = dispatchSetBuffersStickyTransform(args);
793 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800794 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
795 res = dispatchSetBuffersTimestamp(args);
796 break;
797 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
798 res = dispatchSetBuffersDimensions(args);
799 break;
800 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
801 res = dispatchSetBuffersUserDimensions(args);
802 break;
803 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
804 res = dispatchSetBuffersFormat(args);
805 break;
806 case NATIVE_WINDOW_LOCK:
807 res = dispatchLock(args);
808 break;
809 case NATIVE_WINDOW_UNLOCK_AND_POST:
810 res = dispatchUnlockAndPost(args);
811 break;
812 case NATIVE_WINDOW_SET_SCALING_MODE:
813 res = dispatchSetScalingMode(args);
814 break;
815 case NATIVE_WINDOW_API_CONNECT:
816 res = dispatchConnect(args);
817 break;
818 case NATIVE_WINDOW_API_DISCONNECT:
819 res = dispatchDisconnect(args);
820 break;
Rachad7cb0d392014-07-29 17:53:53 -0700821 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
822 res = dispatchSetSidebandStream(args);
823 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800824 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
825 res = dispatchSetBuffersDataSpace(args);
826 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700827 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
828 res = dispatchSetSurfaceDamage(args);
829 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700830 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
831 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700832 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800833 case NATIVE_WINDOW_SET_AUTO_REFRESH:
834 res = dispatchSetAutoRefresh(args);
835 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700836 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
837 res = dispatchEnableFrameTimestamps(args);
838 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800839 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
840 res = dispatchGetFrameTimestamps(args);
841 break;
Ian Elliottbe833a22017-01-25 13:09:20 -0700842 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
843 res = dispatchGetDisplayRefreshCycleDuration(args);
Ian Elliott62c48c92017-01-20 13:13:20 -0700844 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800845 default:
846 res = NAME_NOT_FOUND;
847 break;
848 }
849 return res;
850}
Mathias Agopiana138f892010-05-21 17:24:35 -0700851
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800852int Surface::dispatchConnect(va_list args) {
853 int api = va_arg(args, int);
854 return connect(api);
855}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800856
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800857int Surface::dispatchDisconnect(va_list args) {
858 int api = va_arg(args, int);
859 return disconnect(api);
860}
861
862int Surface::dispatchSetUsage(va_list args) {
863 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800864 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800865}
866
867int Surface::dispatchSetCrop(va_list args) {
868 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
869 return setCrop(reinterpret_cast<Rect const*>(rect));
870}
871
872int Surface::dispatchSetBufferCount(va_list args) {
873 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800874 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800875}
876
877int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800878 uint32_t width = va_arg(args, uint32_t);
879 uint32_t height = va_arg(args, uint32_t);
880 PixelFormat format = va_arg(args, PixelFormat);
881 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800882 if (err != 0) {
883 return err;
884 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800885 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800886}
887
888int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800889 uint32_t width = va_arg(args, uint32_t);
890 uint32_t height = va_arg(args, uint32_t);
891 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800892}
893
894int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800895 uint32_t width = va_arg(args, uint32_t);
896 uint32_t height = va_arg(args, uint32_t);
897 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800898}
899
900int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800901 PixelFormat format = va_arg(args, PixelFormat);
902 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800903}
904
905int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800906 int mode = va_arg(args, int);
907 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800908}
909
910int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800911 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800912 return setBuffersTransform(transform);
913}
914
Ruben Brunk1681d952014-06-27 15:51:55 -0700915int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800916 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700917 return setBuffersStickyTransform(transform);
918}
919
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800920int Surface::dispatchSetBuffersTimestamp(va_list args) {
921 int64_t timestamp = va_arg(args, int64_t);
922 return setBuffersTimestamp(timestamp);
923}
924
925int Surface::dispatchLock(va_list args) {
926 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
927 ARect* inOutDirtyBounds = va_arg(args, ARect*);
928 return lock(outBuffer, inOutDirtyBounds);
929}
930
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800931int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800932 return unlockAndPost();
933}
934
Rachad7cb0d392014-07-29 17:53:53 -0700935int Surface::dispatchSetSidebandStream(va_list args) {
936 native_handle_t* sH = va_arg(args, native_handle_t*);
937 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
938 setSidebandStream(sidebandHandle);
939 return OK;
940}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800941
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800942int Surface::dispatchSetBuffersDataSpace(va_list args) {
943 android_dataspace dataspace =
944 static_cast<android_dataspace>(va_arg(args, int));
945 return setBuffersDataSpace(dataspace);
946}
947
Dan Stoza5065a552015-03-17 16:23:42 -0700948int Surface::dispatchSetSurfaceDamage(va_list args) {
949 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
950 size_t numRects = va_arg(args, size_t);
951 setSurfaceDamage(rects, numRects);
952 return NO_ERROR;
953}
954
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700955int Surface::dispatchSetSharedBufferMode(va_list args) {
956 bool sharedBufferMode = va_arg(args, int);
957 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800958}
959
960int Surface::dispatchSetAutoRefresh(va_list args) {
961 bool autoRefresh = va_arg(args, int);
962 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700963}
964
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700965int Surface::dispatchEnableFrameTimestamps(va_list args) {
966 bool enable = va_arg(args, int);
967 enableFrameTimestamps(enable);
968 return NO_ERROR;
969}
970
Pablo Ceballosce796e72016-02-04 19:10:51 -0800971int Surface::dispatchGetFrameTimestamps(va_list args) {
972 uint32_t framesAgo = va_arg(args, uint32_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -0700973 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800974 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700975 nsecs_t* outLatchTime = va_arg(args, int64_t*);
976 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
977 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800978 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700979 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800980 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700981 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800982 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700983 return getFrameTimestamps(getNextFrameNumber() - 1 - framesAgo,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700984 outRequestedPresentTime, outAcquireTime, outLatchTime,
985 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700986 outGlCompositionDoneTime, outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700987 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800988}
989
Ian Elliottbe833a22017-01-25 13:09:20 -0700990int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
991 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
992 return getDisplayRefreshCycleDuration(outRefreshDuration);
Ian Elliott62c48c92017-01-20 13:13:20 -0700993}
994
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800995int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -0800996 static sp<IProducerListener> listener = new DummyProducerListener();
997 return connect(api, listener);
998}
999
1000int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001001 ATRACE_CALL();
1002 ALOGV("Surface::connect");
1003 Mutex::Autolock lock(mMutex);
1004 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001005 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001006 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001007 mDefaultWidth = output.width;
1008 mDefaultHeight = output.height;
1009 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001010
1011 // Disable transform hint if sticky transform is set.
1012 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001013 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001014 }
1015
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001016 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001017 }
1018 if (!err && api == NATIVE_WINDOW_API_CPU) {
1019 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001020 // Clear the dirty region in case we're switching from a non-CPU API
1021 mDirtyRegion.clear();
1022 } else if (!err) {
1023 // Initialize the dirty region for tracking surface damage
1024 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001025 }
Dan Stoza5065a552015-03-17 16:23:42 -07001026
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001027 return err;
1028}
1029
Mathias Agopian365857d2013-09-11 19:35:45 -07001030
Robert Carr97b9c862016-09-08 13:54:35 -07001031int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001032 ATRACE_CALL();
1033 ALOGV("Surface::disconnect");
1034 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001035 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1036 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001037 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001038 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001039 if (!err) {
1040 mReqFormat = 0;
1041 mReqWidth = 0;
1042 mReqHeight = 0;
1043 mReqUsage = 0;
1044 mCrop.clear();
1045 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1046 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001047 mStickyTransform = 0;
1048
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001049 if (api == NATIVE_WINDOW_API_CPU) {
1050 mConnectedToCpu = false;
1051 }
1052 }
1053 return err;
1054}
1055
Dan Stozad9c49712015-04-27 11:06:01 -07001056int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001057 sp<Fence>* outFence) {
1058 ATRACE_CALL();
1059 ALOGV("Surface::detachNextBuffer");
1060
1061 if (outBuffer == NULL || outFence == NULL) {
1062 return BAD_VALUE;
1063 }
1064
1065 Mutex::Autolock lock(mMutex);
1066
1067 sp<GraphicBuffer> buffer(NULL);
1068 sp<Fence> fence(NULL);
1069 status_t result = mGraphicBufferProducer->detachNextBuffer(
1070 &buffer, &fence);
1071 if (result != NO_ERROR) {
1072 return result;
1073 }
1074
Dan Stozad9c49712015-04-27 11:06:01 -07001075 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001076 if (fence != NULL && fence->isValid()) {
1077 *outFence = fence;
1078 } else {
1079 *outFence = Fence::NO_FENCE;
1080 }
1081
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001082 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1083 if (mSlots[i].buffer != NULL &&
1084 mSlots[i].buffer->handle == buffer->handle) {
1085 mSlots[i].buffer = NULL;
1086 }
1087 }
1088
Dan Stoza231832e2015-03-11 11:55:01 -07001089 return NO_ERROR;
1090}
1091
1092int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1093{
1094 ATRACE_CALL();
1095 ALOGV("Surface::attachBuffer");
1096
1097 Mutex::Autolock lock(mMutex);
1098
1099 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001100 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1101 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001102 int32_t attachedSlot = -1;
1103 status_t result = mGraphicBufferProducer->attachBuffer(
1104 &attachedSlot, graphicBuffer);
1105 if (result != NO_ERROR) {
1106 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001107 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001108 return result;
1109 }
1110 mSlots[attachedSlot].buffer = graphicBuffer;
1111
1112 return NO_ERROR;
1113}
1114
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001115int Surface::setUsage(uint32_t reqUsage)
1116{
1117 ALOGV("Surface::setUsage");
1118 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001119 if (reqUsage != mReqUsage) {
1120 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1121 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001122 mReqUsage = reqUsage;
1123 return OK;
1124}
1125
1126int Surface::setCrop(Rect const* rect)
1127{
1128 ATRACE_CALL();
1129
Pablo Ceballos60d69222015-08-07 14:47:20 -07001130 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001131 if (rect == NULL || rect->isEmpty()) {
1132 realRect.clear();
1133 } else {
1134 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001135 }
1136
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001137 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1138 realRect.left, realRect.top, realRect.right, realRect.bottom);
1139
1140 Mutex::Autolock lock(mMutex);
1141 mCrop = realRect;
1142 return NO_ERROR;
1143}
1144
1145int Surface::setBufferCount(int bufferCount)
1146{
1147 ATRACE_CALL();
1148 ALOGV("Surface::setBufferCount");
1149 Mutex::Autolock lock(mMutex);
1150
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001151 status_t err = NO_ERROR;
1152 if (bufferCount == 0) {
1153 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1154 } else {
1155 int minUndequeuedBuffers = 0;
1156 err = mGraphicBufferProducer->query(
1157 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1158 if (err == NO_ERROR) {
1159 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1160 bufferCount - minUndequeuedBuffers);
1161 }
1162 }
Mathias Agopian90147262010-01-22 11:47:55 -08001163
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001164 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1165 bufferCount, strerror(-err));
1166
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001167 return err;
1168}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001169
Pablo Ceballosfa455352015-08-12 17:47:47 -07001170int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1171 ATRACE_CALL();
1172 ALOGV("Surface::setMaxDequeuedBufferCount");
1173 Mutex::Autolock lock(mMutex);
1174
1175 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1176 maxDequeuedBuffers);
1177 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1178 "returned %s", maxDequeuedBuffers, strerror(-err));
1179
Pablo Ceballosfa455352015-08-12 17:47:47 -07001180 return err;
1181}
1182
1183int Surface::setAsyncMode(bool async) {
1184 ATRACE_CALL();
1185 ALOGV("Surface::setAsyncMode");
1186 Mutex::Autolock lock(mMutex);
1187
1188 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1189 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1190 async, strerror(-err));
1191
Pablo Ceballosfa455352015-08-12 17:47:47 -07001192 return err;
1193}
1194
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001195int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001196 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001197 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001198 Mutex::Autolock lock(mMutex);
1199
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001200 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1201 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001202 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001203 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001204 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001205 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1206 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001207
1208 return err;
1209}
1210
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001211int Surface::setAutoRefresh(bool autoRefresh) {
1212 ATRACE_CALL();
1213 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1214 Mutex::Autolock lock(mMutex);
1215
1216 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1217 if (err == NO_ERROR) {
1218 mAutoRefresh = autoRefresh;
1219 }
1220 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1221 autoRefresh, strerror(-err));
1222 return err;
1223}
1224
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001225int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001226{
1227 ATRACE_CALL();
1228 ALOGV("Surface::setBuffersDimensions");
1229
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001230 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001231 return BAD_VALUE;
1232
1233 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001234 if (width != mReqWidth || height != mReqHeight) {
1235 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1236 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001237 mReqWidth = width;
1238 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001239 return NO_ERROR;
1240}
1241
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001242int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001243{
1244 ATRACE_CALL();
1245 ALOGV("Surface::setBuffersUserDimensions");
1246
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001247 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001248 return BAD_VALUE;
1249
1250 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001251 if (width != mUserWidth || height != mUserHeight) {
1252 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1253 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001254 mUserWidth = width;
1255 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001256 return NO_ERROR;
1257}
1258
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001259int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001260{
1261 ALOGV("Surface::setBuffersFormat");
1262
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001263 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001264 if (format != mReqFormat) {
1265 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1266 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001267 mReqFormat = format;
1268 return NO_ERROR;
1269}
1270
1271int Surface::setScalingMode(int mode)
1272{
1273 ATRACE_CALL();
1274 ALOGV("Surface::setScalingMode(%d)", mode);
1275
1276 switch (mode) {
1277 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1278 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1279 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001280 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001281 break;
1282 default:
1283 ALOGE("unknown scaling mode: %d", mode);
1284 return BAD_VALUE;
1285 }
1286
1287 Mutex::Autolock lock(mMutex);
1288 mScalingMode = mode;
1289 return NO_ERROR;
1290}
1291
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001292int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001293{
1294 ATRACE_CALL();
1295 ALOGV("Surface::setBuffersTransform");
1296 Mutex::Autolock lock(mMutex);
1297 mTransform = transform;
1298 return NO_ERROR;
1299}
1300
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001301int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001302{
1303 ATRACE_CALL();
1304 ALOGV("Surface::setBuffersStickyTransform");
1305 Mutex::Autolock lock(mMutex);
1306 mStickyTransform = transform;
1307 return NO_ERROR;
1308}
1309
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001310int Surface::setBuffersTimestamp(int64_t timestamp)
1311{
1312 ALOGV("Surface::setBuffersTimestamp");
1313 Mutex::Autolock lock(mMutex);
1314 mTimestamp = timestamp;
1315 return NO_ERROR;
1316}
1317
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001318int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1319{
1320 ALOGV("Surface::setBuffersDataSpace");
1321 Mutex::Autolock lock(mMutex);
1322 mDataSpace = dataSpace;
1323 return NO_ERROR;
1324}
1325
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001326void Surface::freeAllBuffers() {
1327 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1328 mSlots[i].buffer = 0;
1329 }
1330}
1331
Dan Stoza5065a552015-03-17 16:23:42 -07001332void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1333 ATRACE_CALL();
1334 ALOGV("Surface::setSurfaceDamage");
1335 Mutex::Autolock lock(mMutex);
1336
Dan Stozac62acbd2015-04-21 16:42:49 -07001337 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001338 mDirtyRegion = Region::INVALID_REGION;
1339 return;
1340 }
1341
1342 mDirtyRegion.clear();
1343 for (size_t r = 0; r < numRects; ++r) {
1344 // We intentionally flip top and bottom here, since because they're
1345 // specified with a bottom-left origin, top > bottom, which fails
1346 // validation in the Region class. We will fix this up when we flip to a
1347 // top-left origin in queueBuffer.
1348 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1349 mDirtyRegion.orSelf(rect);
1350 }
1351}
1352
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001353// ----------------------------------------------------------------------
1354// the lock/unlock APIs must be used from the same thread
1355
1356static status_t copyBlt(
1357 const sp<GraphicBuffer>& dst,
1358 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001359 const Region& reg,
1360 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001361{
1362 // src and dst with, height and format must be identical. no verification
1363 // is done here.
1364 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001365 uint8_t* src_bits = NULL;
1366 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1367 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001368 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1369
1370 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001371 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1372 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001373 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001374 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001375
1376 Region::const_iterator head(reg.begin());
1377 Region::const_iterator tail(reg.end());
1378 if (head != tail && src_bits && dst_bits) {
1379 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001380 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1381 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001382
1383 while (head != tail) {
1384 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001385 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001386 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001387 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1388 uint8_t const * s = src_bits +
1389 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1390 uint8_t * d = dst_bits +
1391 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001392 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001393 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001394 h = 1;
1395 }
1396 do {
1397 memcpy(d, s, size);
1398 d += dbpr;
1399 s += sbpr;
1400 } while (--h > 0);
1401 }
1402 }
1403
1404 if (src_bits)
1405 src->unlock();
1406
1407 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001408 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001409
1410 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411}
1412
Mathias Agopiana138f892010-05-21 17:24:35 -07001413// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001414
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001415status_t Surface::lock(
1416 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1417{
1418 if (mLockedBuffer != 0) {
1419 ALOGE("Surface::lock failed, already locked");
1420 return INVALID_OPERATION;
1421 }
1422
1423 if (!mConnectedToCpu) {
1424 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1425 if (err) {
1426 return err;
1427 }
1428 // we're intending to do software rendering from this point
1429 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1430 }
1431
1432 ANativeWindowBuffer* out;
1433 int fenceFd = -1;
1434 status_t err = dequeueBuffer(&out, &fenceFd);
1435 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1436 if (err == NO_ERROR) {
1437 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001438 const Rect bounds(backBuffer->width, backBuffer->height);
1439
1440 Region newDirtyRegion;
1441 if (inOutDirtyBounds) {
1442 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1443 newDirtyRegion.andSelf(bounds);
1444 } else {
1445 newDirtyRegion.set(bounds);
1446 }
1447
1448 // figure out if we can copy the frontbuffer back
1449 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1450 const bool canCopyBack = (frontBuffer != 0 &&
1451 backBuffer->width == frontBuffer->width &&
1452 backBuffer->height == frontBuffer->height &&
1453 backBuffer->format == frontBuffer->format);
1454
1455 if (canCopyBack) {
1456 // copy the area that is invalid and not repainted this round
1457 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001458 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001459 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001460 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001461 } else {
1462 // if we can't copy-back anything, modify the user's dirty
1463 // region to make sure they redraw the whole buffer
1464 newDirtyRegion.set(bounds);
1465 mDirtyRegion.clear();
1466 Mutex::Autolock lock(mMutex);
1467 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1468 mSlots[i].dirtyRegion.clear();
1469 }
1470 }
1471
1472
1473 { // scope for the lock
1474 Mutex::Autolock lock(mMutex);
1475 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1476 if (backBufferSlot >= 0) {
1477 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1478 mDirtyRegion.subtract(dirtyRegion);
1479 dirtyRegion = newDirtyRegion;
1480 }
1481 }
1482
1483 mDirtyRegion.orSelf(newDirtyRegion);
1484 if (inOutDirtyBounds) {
1485 *inOutDirtyBounds = newDirtyRegion.getBounds();
1486 }
1487
1488 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001489 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001490 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001491 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001492
1493 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1494 backBuffer->handle);
1495
1496 if (res != 0) {
1497 err = INVALID_OPERATION;
1498 } else {
1499 mLockedBuffer = backBuffer;
1500 outBuffer->width = backBuffer->width;
1501 outBuffer->height = backBuffer->height;
1502 outBuffer->stride = backBuffer->stride;
1503 outBuffer->format = backBuffer->format;
1504 outBuffer->bits = vaddr;
1505 }
1506 }
1507 return err;
1508}
1509
1510status_t Surface::unlockAndPost()
1511{
1512 if (mLockedBuffer == 0) {
1513 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1514 return INVALID_OPERATION;
1515 }
1516
Francis Hart8f396012014-04-01 15:30:53 +03001517 int fd = -1;
1518 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001519 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1520
Francis Hart8f396012014-04-01 15:30:53 +03001521 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001522 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1523 mLockedBuffer->handle, strerror(-err));
1524
1525 mPostedBuffer = mLockedBuffer;
1526 mLockedBuffer = 0;
1527 return err;
1528}
1529
Robert Carr9f31e292016-04-11 11:15:32 -07001530bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1531 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001532 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001533 return true;
1534 }
1535 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1536}
1537
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001538status_t Surface::getUniqueId(uint64_t* outId) const {
1539 Mutex::Autolock lock(mMutex);
1540 return mGraphicBufferProducer->getUniqueId(outId);
1541}
1542
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001543namespace view {
1544
1545status_t Surface::writeToParcel(Parcel* parcel) const {
1546 return writeToParcel(parcel, false);
1547}
1548
1549status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const {
1550 if (parcel == nullptr) return BAD_VALUE;
1551
1552 status_t res = OK;
1553
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001554 if (!nameAlreadyWritten) {
1555 res = parcel->writeString16(name);
1556 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001557
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001558 /* isSingleBuffered defaults to no */
1559 res = parcel->writeInt32(0);
1560 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001561 }
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001562
1563 res = parcel->writeStrongBinder(
1564 IGraphicBufferProducer::asBinder(graphicBufferProducer));
1565
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001566 return res;
1567}
1568
1569status_t Surface::readFromParcel(const Parcel* parcel) {
1570 return readFromParcel(parcel, false);
1571}
1572
1573status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) {
1574 if (parcel == nullptr) return BAD_VALUE;
1575
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001576 status_t res = OK;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001577 if (!nameAlreadyRead) {
1578 name = readMaybeEmptyString16(parcel);
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001579 // Discard this for now
1580 int isSingleBuffered;
1581 res = parcel->readInt32(&isSingleBuffered);
1582 if (res != OK) {
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001583 ALOGE("Can't read isSingleBuffered");
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001584 return res;
1585 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001586 }
1587
1588 sp<IBinder> binder;
1589
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001590 res = parcel->readNullableStrongBinder(&binder);
1591 if (res != OK) {
1592 ALOGE("%s: Can't read strong binder", __FUNCTION__);
1593 return res;
1594 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001595
1596 graphicBufferProducer = interface_cast<IGraphicBufferProducer>(binder);
1597
1598 return OK;
1599}
1600
1601String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
1602 size_t len;
1603 const char16_t* str = parcel->readString16Inplace(&len);
1604 if (str != nullptr) {
1605 return String16(str, len);
1606 } else {
1607 return String16();
1608 }
1609}
1610
1611} // namespace view
1612
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613}; // namespace android