blob: d285ef0ce90452e89030fd2b669b83d40244371c [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 Agopian05debe12017-02-08 17:04:18 -080021#include <gui/Surface.h>
Mathias Agopianb0e76f42012-03-23 14:15:44 -070022
Mathias Agopian05debe12017-02-08 17:04:18 -080023#include <android/native_window.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080024
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
Mathias Agopian2b5dd402017-02-07 17:36:19 -080033#include <gui/BufferItem.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070034#include <gui/IProducerListener.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080035
Mathias Agopian2b5dd402017-02-07 17:36:19 -080036#include <gui/ISurfaceComposer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080037#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039namespace android {
40
Mathias Agopiane3c697f2013-02-14 17:11:02 -080041Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070042 const sp<IGraphicBufferProducer>& bufferProducer,
43 bool controlledByApp)
Dan Stoza812ed062015-06-02 15:45:22 -070044 : mGraphicBufferProducer(bufferProducer),
Pablo Ceballos60d69222015-08-07 14:47:20 -070045 mCrop(Rect::EMPTY_RECT),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080046 mGenerationNumber(0),
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070047 mSharedBufferMode(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080048 mAutoRefresh(false),
49 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
Pablo Ceballosbc8c1922016-07-01 14:15:41 -070050 mSharedBufferHasBeenQueued(false),
Brian Anderson069b3652016-07-22 10:32:47 -070051 mQueriedSupportedTimestamps(false),
52 mFrameTimestampsSupportsPresent(false),
Brian Anderson7c3ba8a2016-07-25 12:48:08 -070053 mFrameTimestampsSupportsRetire(false),
Brian Anderson3da8d272016-07-28 16:20:47 -070054 mEnableFrameTimestamps(false),
55 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>())
Mathias Agopian62185b72009-04-16 16:19:50 -070056{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080057 // Initialize the ANativeWindow function pointers.
58 ANativeWindow::setSwapInterval = hook_setSwapInterval;
59 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
60 ANativeWindow::cancelBuffer = hook_cancelBuffer;
61 ANativeWindow::queueBuffer = hook_queueBuffer;
62 ANativeWindow::query = hook_query;
63 ANativeWindow::perform = hook_perform;
64
65 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
66 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
67 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
68 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
69
70 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
71 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
72
73 mReqWidth = 0;
74 mReqHeight = 0;
75 mReqFormat = 0;
76 mReqUsage = 0;
77 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080078 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080079 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
80 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070081 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080082 mDefaultWidth = 0;
83 mDefaultHeight = 0;
84 mUserWidth = 0;
85 mUserHeight = 0;
86 mTransformHint = 0;
87 mConsumerRunningBehind = false;
88 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070089 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070090 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070091}
92
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070093Surface::~Surface() {
94 if (mConnectedToCpu) {
95 Surface::disconnect(NATIVE_WINDOW_API_CPU);
96 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -080097}
98
Brian Anderson3da8d272016-07-28 16:20:47 -070099sp<ISurfaceComposer> Surface::composerService() const {
100 return ComposerService::getComposerService();
101}
102
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800103nsecs_t Surface::now() const {
104 return systemTime();
105}
106
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800107sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
108 return mGraphicBufferProducer;
109}
110
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900111void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
112 mGraphicBufferProducer->setSidebandStream(stream);
113}
114
Dan Stoza29a3e902014-06-20 13:13:57 -0700115void Surface::allocateBuffers() {
116 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
117 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700118 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
119 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700120}
121
Dan Stoza812ed062015-06-02 15:45:22 -0700122status_t Surface::setGenerationNumber(uint32_t generation) {
123 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
124 if (result == NO_ERROR) {
125 mGenerationNumber = generation;
126 }
127 return result;
128}
129
Dan Stoza7dde5992015-05-22 09:51:44 -0700130uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700131 Mutex::Autolock lock(mMutex);
132 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700133}
134
Dan Stozac6f30bd2015-06-08 09:32:50 -0700135String8 Surface::getConsumerName() const {
136 return mGraphicBufferProducer->getConsumerName();
137}
138
Dan Stoza127fc632015-06-30 13:43:32 -0700139status_t Surface::setDequeueTimeout(nsecs_t timeout) {
140 return mGraphicBufferProducer->setDequeueTimeout(timeout);
141}
142
Dan Stoza50101d02016-04-07 16:53:23 -0700143status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700144 sp<Fence>* outFence, float outTransformMatrix[16]) {
145 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
146 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700147}
148
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800149status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
150 ATRACE_CALL();
151
152 DisplayStatInfo stats;
153 status_t err = composerService()->getDisplayStats(NULL, &stats);
154
155 *outRefreshDuration = stats.vsyncPeriod;
156
157 return NO_ERROR;
158}
159
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700160void Surface::enableFrameTimestamps(bool enable) {
161 Mutex::Autolock lock(mMutex);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800162 // If going from disabled to enabled, get the initial values for
163 // compositor and display timing.
164 if (!mEnableFrameTimestamps && enable) {
165 FrameEventHistoryDelta delta;
166 mGraphicBufferProducer->getFrameTimestamps(&delta);
167 mFrameEventHistory->applyDelta(delta);
168 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700169 mEnableFrameTimestamps = enable;
170}
171
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800172status_t Surface::getCompositorTiming(
173 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
174 nsecs_t* compositeToPresentLatency) {
175 Mutex::Autolock lock(mMutex);
176 if (!mEnableFrameTimestamps) {
177 return INVALID_OPERATION;
178 }
179
180 if (compositeDeadline != nullptr) {
181 *compositeDeadline =
182 mFrameEventHistory->getNextCompositeDeadline(now());
183 }
184 if (compositeInterval != nullptr) {
185 *compositeInterval = mFrameEventHistory->getCompositeInterval();
186 }
187 if (compositeToPresentLatency != nullptr) {
188 *compositeToPresentLatency =
189 mFrameEventHistory->getCompositeToPresentLatency();
190 }
191 return NO_ERROR;
192}
193
Brian Anderson50143b32016-09-30 14:01:24 -0700194static bool checkConsumerForUpdates(
195 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700196 const nsecs_t* outLatchTime,
197 const nsecs_t* outFirstRefreshStartTime,
198 const nsecs_t* outLastRefreshStartTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700199 const nsecs_t* outGlCompositionDoneTime,
200 const nsecs_t* outDisplayPresentTime,
201 const nsecs_t* outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700202 const nsecs_t* outDequeueReadyTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700203 const nsecs_t* outReleaseTime) {
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700204 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
205 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700206 !e->hasFirstRefreshStartInfo();
207 bool checkForGlCompositionDone = (outGlCompositionDoneTime != nullptr) &&
208 !e->hasGpuCompositionDoneInfo();
209 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
210 !e->hasDisplayPresentInfo();
211
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700212 // LastRefreshStart, DisplayRetire, DequeueReady, and Release are never
213 // available for the last frame.
214 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
215 !e->hasLastRefreshStartInfo() &&
216 (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700217 bool checkForDisplayRetire = (outDisplayRetireTime != nullptr) &&
218 !e->hasDisplayRetireInfo() && (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700219 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
220 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700221 bool checkForRelease = (outReleaseTime != nullptr) &&
222 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
223
224 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700225 return checkForLatch || checkForFirstRefreshStart ||
226 checkForLastRefreshStart || checkForGlCompositionDone ||
227 checkForDisplayPresent || checkForDisplayRetire ||
228 checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700229}
230
Brian Anderson3d4039d2016-09-23 16:31:30 -0700231static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
232 if (dst != nullptr) {
233 *dst = Fence::isValidTimestamp(src) ? src : 0;
234 }
235}
236
237static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) {
238 if (dst != nullptr) {
239 nsecs_t signalTime = src->getSignalTime();
240 *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0;
241 }
242}
243
Brian Anderson069b3652016-07-22 10:32:47 -0700244status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700245 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700246 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
247 nsecs_t* outLastRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700248 nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700249 nsecs_t* outDequeueReadyTime, nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800250 ATRACE_CALL();
251
Brian Anderson3890c392016-07-25 12:48:08 -0700252 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700253
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700254 if (!mEnableFrameTimestamps) {
255 return INVALID_OPERATION;
256 }
257
Brian Anderson3890c392016-07-25 12:48:08 -0700258 // Verify the requested timestamps are supported.
259 querySupportedTimestampsLocked();
260 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
261 return BAD_VALUE;
262 }
263 if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
264 return BAD_VALUE;
Brian Anderson069b3652016-07-22 10:32:47 -0700265 }
266
Brian Anderson3da8d272016-07-28 16:20:47 -0700267 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700268 if (events == nullptr) {
269 // If the entry isn't available in the producer, it's definitely not
270 // available in the consumer.
271 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700272 }
273
Brian Anderson50143b32016-09-30 14:01:24 -0700274 // Update our cache of events if the requested events are not available.
275 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700276 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
277 outGlCompositionDoneTime, outDisplayPresentTime,
278 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700279 FrameEventHistoryDelta delta;
280 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700281 mFrameEventHistory->applyDelta(delta);
282 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700283 }
284
Brian Anderson3890c392016-07-25 12:48:08 -0700285 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700286 // The entry was available before the update, but was overwritten
287 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700288 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800289 }
Brian Anderson069b3652016-07-22 10:32:47 -0700290
Brian Anderson3d4039d2016-09-23 16:31:30 -0700291 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700292 getFrameTimestamp(outLatchTime, events->latchTime);
293 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
294 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
295 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700296
Brian Anderson3d4039d2016-09-23 16:31:30 -0700297 getFrameTimestampFence(outAcquireTime, events->acquireFence);
298 getFrameTimestampFence(
299 outGlCompositionDoneTime, events->gpuCompositionDoneFence);
300 getFrameTimestampFence(
301 outDisplayPresentTime, events->displayPresentFence);
302 getFrameTimestampFence(outDisplayRetireTime, events->displayRetireFence);
303 getFrameTimestampFence(outReleaseTime, events->releaseFence);
Brian Anderson069b3652016-07-22 10:32:47 -0700304
305 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800306}
307
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800308int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
309 Surface* c = getSelf(window);
310 return c->setSwapInterval(interval);
311}
312
313int Surface::hook_dequeueBuffer(ANativeWindow* window,
314 ANativeWindowBuffer** buffer, int* fenceFd) {
315 Surface* c = getSelf(window);
316 return c->dequeueBuffer(buffer, fenceFd);
317}
318
319int Surface::hook_cancelBuffer(ANativeWindow* window,
320 ANativeWindowBuffer* buffer, int fenceFd) {
321 Surface* c = getSelf(window);
322 return c->cancelBuffer(buffer, fenceFd);
323}
324
325int Surface::hook_queueBuffer(ANativeWindow* window,
326 ANativeWindowBuffer* buffer, int fenceFd) {
327 Surface* c = getSelf(window);
328 return c->queueBuffer(buffer, fenceFd);
329}
330
331int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
332 ANativeWindowBuffer** buffer) {
333 Surface* c = getSelf(window);
334 ANativeWindowBuffer* buf;
335 int fenceFd = -1;
336 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600337 if (result != OK) {
338 return result;
339 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800340 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700341 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800342 if (waitResult != OK) {
343 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
344 waitResult);
345 c->cancelBuffer(buf, -1);
346 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700347 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800348 *buffer = buf;
349 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700350}
351
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800352int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
353 ANativeWindowBuffer* buffer) {
354 Surface* c = getSelf(window);
355 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700356}
357
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800358int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
359 ANativeWindowBuffer* buffer) {
360 Surface* c = getSelf(window);
361 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700362}
363
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800364int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
365 ANativeWindowBuffer* buffer) {
366 Surface* c = getSelf(window);
367 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700368}
Mathias Agopian62185b72009-04-16 16:19:50 -0700369
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800370int Surface::hook_query(const ANativeWindow* window,
371 int what, int* value) {
372 const Surface* c = getSelf(window);
373 return c->query(what, value);
374}
375
376int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
377 va_list args;
378 va_start(args, operation);
379 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700380 int result = c->perform(operation, args);
381 va_end(args);
382 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800383}
384
385int Surface::setSwapInterval(int interval) {
386 ATRACE_CALL();
387 // EGL specification states:
388 // interval is silently clamped to minimum and maximum implementation
389 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800390
391 if (interval < minSwapInterval)
392 interval = minSwapInterval;
393
394 if (interval > maxSwapInterval)
395 interval = maxSwapInterval;
396
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700397 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700398 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800399
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700400 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800401}
402
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700403int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800404 ATRACE_CALL();
405 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800406
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800407 uint32_t reqWidth;
408 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800409 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800410 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700411 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800412
413 {
414 Mutex::Autolock lock(mMutex);
415
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800416 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
417 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800418
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800419 reqFormat = mReqFormat;
420 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800421
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700422 enableFrameTimestamps = mEnableFrameTimestamps;
423
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700424 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800425 BufferItem::INVALID_BUFFER_SLOT) {
426 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
427 if (gbuf != NULL) {
428 *buffer = gbuf.get();
429 *fenceFd = -1;
430 return OK;
431 }
432 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800433 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
434
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800435 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800436 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700437 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700438
439 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700440 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700441 reqWidth, reqHeight, reqFormat, reqUsage,
442 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700443 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800444
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800445 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700446 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
447 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
448 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800449 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700450 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800451
452 Mutex::Autolock lock(mMutex);
453
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800454 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700455
456 // this should never happen
457 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
458
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800459 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
460 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700461 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700462
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700463 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700464 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700465 }
466
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800467 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
468 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
469 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700470 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700471 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800472 return result;
473 }
474 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700475
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800476 if (fence->isValid()) {
477 *fenceFd = fence->dup();
478 if (*fenceFd == -1) {
479 ALOGE("dequeueBuffer: error duping fence: %d", errno);
480 // dup() should never fail; something is badly wrong. Soldier on
481 // and hope for the best; the worst that should happen is some
482 // visible corruption that lasts until the next frame.
483 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700484 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800485 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700486 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800487
488 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800489
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700490 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800491 mSharedBufferSlot = buf;
492 mSharedBufferHasBeenQueued = false;
493 } else if (mSharedBufferSlot == buf) {
494 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
495 mSharedBufferHasBeenQueued = false;
496 }
497
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800498 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700499}
500
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800501int Surface::cancelBuffer(android_native_buffer_t* buffer,
502 int fenceFd) {
503 ATRACE_CALL();
504 ALOGV("Surface::cancelBuffer");
505 Mutex::Autolock lock(mMutex);
506 int i = getSlotFromBufferLocked(buffer);
507 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900508 if (fenceFd >= 0) {
509 close(fenceFd);
510 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800511 return i;
512 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800513 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
514 if (fenceFd >= 0) {
515 close(fenceFd);
516 }
517 return OK;
518 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800519 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
520 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800521
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700522 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800523 mSharedBufferHasBeenQueued = true;
524 }
525
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800526 return OK;
527}
528
529int Surface::getSlotFromBufferLocked(
530 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800531 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
532 if (mSlots[i].buffer != NULL &&
533 mSlots[i].buffer->handle == buffer->handle) {
534 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700535 }
536 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800537 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
538 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700539}
540
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800541int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800542 ALOGV("Surface::lockBuffer");
543 Mutex::Autolock lock(mMutex);
544 return OK;
545}
Mathias Agopian631f3582010-05-25 17:51:34 -0700546
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800547int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
548 ATRACE_CALL();
549 ALOGV("Surface::queueBuffer");
550 Mutex::Autolock lock(mMutex);
551 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700552 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800553
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800554 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700555 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700556 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700557 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700558 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800559 } else {
560 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700561 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800562 int i = getSlotFromBufferLocked(buffer);
563 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900564 if (fenceFd >= 0) {
565 close(fenceFd);
566 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800567 return i;
568 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800569 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
570 if (fenceFd >= 0) {
571 close(fenceFd);
572 }
573 return OK;
574 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800577 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700578 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800579 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800581 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
582 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700583 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800584 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700585 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700586
Dan Stozac62acbd2015-04-21 16:42:49 -0700587 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700588 input.setSurfaceDamage(Region::INVALID_REGION);
589 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700590 // Here we do two things:
591 // 1) The surface damage was specified using the OpenGL ES convention of
592 // the origin being in the bottom-left corner. Here we flip to the
593 // convention that the rest of the system uses (top-left corner) by
594 // subtracting all top/bottom coordinates from the buffer height.
595 // 2) If the buffer is coming in rotated (for example, because the EGL
596 // implementation is reacting to the transform hint coming back from
597 // SurfaceFlinger), the surface damage needs to be rotated the
598 // opposite direction, since it was generated assuming an unrotated
599 // buffer (the app doesn't know that the EGL implementation is
600 // reacting to the transform hint behind its back). The
601 // transformations in the switch statement below apply those
602 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
603
604 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700605 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700606 bool rotated90 = (mTransform ^ mStickyTransform) &
607 NATIVE_WINDOW_TRANSFORM_ROT_90;
608 if (rotated90) {
609 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700610 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700611
Dan Stoza5065a552015-03-17 16:23:42 -0700612 Region flippedRegion;
613 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700614 int left = rect.left;
615 int right = rect.right;
616 int top = height - rect.bottom; // Flip from OpenGL convention
617 int bottom = height - rect.top; // Flip from OpenGL convention
618 switch (mTransform ^ mStickyTransform) {
619 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
620 // Rotate 270 degrees
621 Rect flippedRect{top, width - right, bottom, width - left};
622 flippedRegion.orSelf(flippedRect);
623 break;
624 }
625 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
626 // Rotate 180 degrees
627 Rect flippedRect{width - right, height - bottom,
628 width - left, height - top};
629 flippedRegion.orSelf(flippedRect);
630 break;
631 }
632 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
633 // Rotate 90 degrees
634 Rect flippedRect{height - bottom, left,
635 height - top, right};
636 flippedRegion.orSelf(flippedRect);
637 break;
638 }
639 default: {
640 Rect flippedRect{left, top, right, bottom};
641 flippedRegion.orSelf(flippedRect);
642 break;
643 }
644 }
Dan Stoza5065a552015-03-17 16:23:42 -0700645 }
646
647 input.setSurfaceDamage(flippedRegion);
648 }
649
Dan Stoza70ccba52016-07-01 14:00:40 -0700650 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800651 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700652 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800653 if (err != OK) {
654 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
655 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800656
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700657 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700658 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700659 // Update timestamps with the local acquire fence.
660 // The consumer doesn't send it back to prevent us from having two
661 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700662 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700663 std::make_shared<FenceTime>(std::move(fence)));
664
665 // Cache timestamps of signaled fences so we can close their file
666 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700667 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700668 }
669
Brian Anderson50143b32016-09-30 14:01:24 -0700670 mLastFrameNumber = mNextFrameNumber;
671
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700672 mDefaultWidth = output.width;
673 mDefaultHeight = output.height;
674 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700675
Ruben Brunk1681d952014-06-27 15:51:55 -0700676 // Disable transform hint if sticky transform is set.
677 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700678 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700679 }
680
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700681 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700682
Dan Stozac62acbd2015-04-21 16:42:49 -0700683 if (!mConnectedToCpu) {
684 // Clear surface damage back to full-buffer
685 mDirtyRegion = Region::INVALID_REGION;
686 }
Dan Stoza5065a552015-03-17 16:23:42 -0700687
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700688 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800689 mSharedBufferHasBeenQueued = true;
690 }
691
Robert Carr9f31e292016-04-11 11:15:32 -0700692 mQueueBufferCondition.broadcast();
693
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800694 return err;
695}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700696
Brian Anderson069b3652016-07-22 10:32:47 -0700697void Surface::querySupportedTimestampsLocked() const {
698 // mMutex must be locked when calling this method.
699
700 if (mQueriedSupportedTimestamps) {
701 return;
702 }
703 mQueriedSupportedTimestamps = true;
704
Brian Anderson3890c392016-07-25 12:48:08 -0700705 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson3da8d272016-07-28 16:20:47 -0700706 status_t err = composerService()->getSupportedFrameTimestamps(
Brian Anderson069b3652016-07-22 10:32:47 -0700707 &supportedFrameTimestamps);
708
709 if (err != NO_ERROR) {
710 return;
711 }
712
713 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700714 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700715 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700716 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700717 mFrameTimestampsSupportsRetire = true;
718 }
719 }
720}
721
Mathias Agopiana67932f2011-04-20 14:20:59 -0700722int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800723 ATRACE_CALL();
724 ALOGV("Surface::query");
725 { // scope for the lock
726 Mutex::Autolock lock(mMutex);
727 switch (what) {
728 case NATIVE_WINDOW_FORMAT:
729 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800730 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800731 return NO_ERROR;
732 }
733 break;
734 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700735 if (composerService()->authenticateSurfaceTexture(
736 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800737 *value = 1;
738 } else {
739 *value = 0;
740 }
741 return NO_ERROR;
742 }
743 case NATIVE_WINDOW_CONCRETE_TYPE:
744 *value = NATIVE_WINDOW_SURFACE;
745 return NO_ERROR;
746 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800747 *value = static_cast<int>(
748 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800749 return NO_ERROR;
750 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800751 *value = static_cast<int>(
752 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800753 return NO_ERROR;
754 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800755 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800756 return NO_ERROR;
757 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
758 status_t err = NO_ERROR;
759 if (!mConsumerRunningBehind) {
760 *value = 0;
761 } else {
762 err = mGraphicBufferProducer->query(what, value);
763 if (err == NO_ERROR) {
764 mConsumerRunningBehind = *value;
765 }
766 }
767 return err;
768 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700769 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
770 int64_t durationUs = mLastDequeueDuration / 1000;
771 *value = durationUs > std::numeric_limits<int>::max() ?
772 std::numeric_limits<int>::max() :
773 static_cast<int>(durationUs);
774 return NO_ERROR;
775 }
776 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
777 int64_t durationUs = mLastQueueDuration / 1000;
778 *value = durationUs > std::numeric_limits<int>::max() ?
779 std::numeric_limits<int>::max() :
780 static_cast<int>(durationUs);
781 return NO_ERROR;
782 }
Brian Anderson069b3652016-07-22 10:32:47 -0700783 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
784 querySupportedTimestampsLocked();
785 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
786 return NO_ERROR;
787 }
788 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
789 querySupportedTimestampsLocked();
790 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
791 return NO_ERROR;
792 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800793 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700794 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800795 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800796}
797
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800798int Surface::perform(int operation, va_list args)
799{
800 int res = NO_ERROR;
801 switch (operation) {
802 case NATIVE_WINDOW_CONNECT:
803 // deprecated. must return NO_ERROR.
804 break;
805 case NATIVE_WINDOW_DISCONNECT:
806 // deprecated. must return NO_ERROR.
807 break;
808 case NATIVE_WINDOW_SET_USAGE:
809 res = dispatchSetUsage(args);
810 break;
811 case NATIVE_WINDOW_SET_CROP:
812 res = dispatchSetCrop(args);
813 break;
814 case NATIVE_WINDOW_SET_BUFFER_COUNT:
815 res = dispatchSetBufferCount(args);
816 break;
817 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
818 res = dispatchSetBuffersGeometry(args);
819 break;
820 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
821 res = dispatchSetBuffersTransform(args);
822 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700823 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
824 res = dispatchSetBuffersStickyTransform(args);
825 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800826 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
827 res = dispatchSetBuffersTimestamp(args);
828 break;
829 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
830 res = dispatchSetBuffersDimensions(args);
831 break;
832 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
833 res = dispatchSetBuffersUserDimensions(args);
834 break;
835 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
836 res = dispatchSetBuffersFormat(args);
837 break;
838 case NATIVE_WINDOW_LOCK:
839 res = dispatchLock(args);
840 break;
841 case NATIVE_WINDOW_UNLOCK_AND_POST:
842 res = dispatchUnlockAndPost(args);
843 break;
844 case NATIVE_WINDOW_SET_SCALING_MODE:
845 res = dispatchSetScalingMode(args);
846 break;
847 case NATIVE_WINDOW_API_CONNECT:
848 res = dispatchConnect(args);
849 break;
850 case NATIVE_WINDOW_API_DISCONNECT:
851 res = dispatchDisconnect(args);
852 break;
Rachad7cb0d392014-07-29 17:53:53 -0700853 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
854 res = dispatchSetSidebandStream(args);
855 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800856 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
857 res = dispatchSetBuffersDataSpace(args);
858 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700859 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
860 res = dispatchSetSurfaceDamage(args);
861 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700862 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
863 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700864 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800865 case NATIVE_WINDOW_SET_AUTO_REFRESH:
866 res = dispatchSetAutoRefresh(args);
867 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800868 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
869 res = dispatchGetDisplayRefreshCycleDuration(args);
870 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800871 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
872 res = dispatchGetNextFrameId(args);
873 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700874 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
875 res = dispatchEnableFrameTimestamps(args);
876 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800877 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
878 res = dispatchGetCompositorTiming(args);
879 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800880 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
881 res = dispatchGetFrameTimestamps(args);
882 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800883 default:
884 res = NAME_NOT_FOUND;
885 break;
886 }
887 return res;
888}
Mathias Agopiana138f892010-05-21 17:24:35 -0700889
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800890int Surface::dispatchConnect(va_list args) {
891 int api = va_arg(args, int);
892 return connect(api);
893}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800894
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800895int Surface::dispatchDisconnect(va_list args) {
896 int api = va_arg(args, int);
897 return disconnect(api);
898}
899
900int Surface::dispatchSetUsage(va_list args) {
901 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800902 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800903}
904
905int Surface::dispatchSetCrop(va_list args) {
906 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
907 return setCrop(reinterpret_cast<Rect const*>(rect));
908}
909
910int Surface::dispatchSetBufferCount(va_list args) {
911 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800912 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800913}
914
915int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800916 uint32_t width = va_arg(args, uint32_t);
917 uint32_t height = va_arg(args, uint32_t);
918 PixelFormat format = va_arg(args, PixelFormat);
919 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800920 if (err != 0) {
921 return err;
922 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800923 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800924}
925
926int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800927 uint32_t width = va_arg(args, uint32_t);
928 uint32_t height = va_arg(args, uint32_t);
929 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800930}
931
932int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800933 uint32_t width = va_arg(args, uint32_t);
934 uint32_t height = va_arg(args, uint32_t);
935 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800936}
937
938int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800939 PixelFormat format = va_arg(args, PixelFormat);
940 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800941}
942
943int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800944 int mode = va_arg(args, int);
945 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800946}
947
948int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800949 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800950 return setBuffersTransform(transform);
951}
952
Ruben Brunk1681d952014-06-27 15:51:55 -0700953int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800954 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700955 return setBuffersStickyTransform(transform);
956}
957
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800958int Surface::dispatchSetBuffersTimestamp(va_list args) {
959 int64_t timestamp = va_arg(args, int64_t);
960 return setBuffersTimestamp(timestamp);
961}
962
963int Surface::dispatchLock(va_list args) {
964 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
965 ARect* inOutDirtyBounds = va_arg(args, ARect*);
966 return lock(outBuffer, inOutDirtyBounds);
967}
968
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800969int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800970 return unlockAndPost();
971}
972
Rachad7cb0d392014-07-29 17:53:53 -0700973int Surface::dispatchSetSidebandStream(va_list args) {
974 native_handle_t* sH = va_arg(args, native_handle_t*);
975 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
976 setSidebandStream(sidebandHandle);
977 return OK;
978}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800979
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800980int Surface::dispatchSetBuffersDataSpace(va_list args) {
981 android_dataspace dataspace =
982 static_cast<android_dataspace>(va_arg(args, int));
983 return setBuffersDataSpace(dataspace);
984}
985
Dan Stoza5065a552015-03-17 16:23:42 -0700986int Surface::dispatchSetSurfaceDamage(va_list args) {
987 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
988 size_t numRects = va_arg(args, size_t);
989 setSurfaceDamage(rects, numRects);
990 return NO_ERROR;
991}
992
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700993int Surface::dispatchSetSharedBufferMode(va_list args) {
994 bool sharedBufferMode = va_arg(args, int);
995 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800996}
997
998int Surface::dispatchSetAutoRefresh(va_list args) {
999 bool autoRefresh = va_arg(args, int);
1000 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001001}
1002
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001003int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1004 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1005 return getDisplayRefreshCycleDuration(outRefreshDuration);
1006}
1007
Brian Anderson1049d1d2016-12-16 17:25:57 -08001008int Surface::dispatchGetNextFrameId(va_list args) {
1009 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1010 *nextFrameId = getNextFrameNumber();
1011 return NO_ERROR;
1012}
1013
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001014int Surface::dispatchEnableFrameTimestamps(va_list args) {
1015 bool enable = va_arg(args, int);
1016 enableFrameTimestamps(enable);
1017 return NO_ERROR;
1018}
1019
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001020int Surface::dispatchGetCompositorTiming(va_list args) {
1021 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1022 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1023 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1024 return getCompositorTiming(compositeDeadline, compositeInterval,
1025 compositeToPresentLatency);
1026}
1027
Pablo Ceballosce796e72016-02-04 19:10:51 -08001028int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001029 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001030 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001031 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001032 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1033 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1034 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001035 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001036 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001037 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001038 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001039 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001040 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001041 outRequestedPresentTime, outAcquireTime, outLatchTime,
1042 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -07001043 outGlCompositionDoneTime, outDisplayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001044 outDisplayRetireTime, outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001045}
1046
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001047int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001048 static sp<IProducerListener> listener = new DummyProducerListener();
1049 return connect(api, listener);
1050}
1051
1052int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001053 ATRACE_CALL();
1054 ALOGV("Surface::connect");
1055 Mutex::Autolock lock(mMutex);
1056 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001057 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001058 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001059 mDefaultWidth = output.width;
1060 mDefaultHeight = output.height;
1061 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001062
1063 // Disable transform hint if sticky transform is set.
1064 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001065 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001066 }
1067
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001068 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001069 }
1070 if (!err && api == NATIVE_WINDOW_API_CPU) {
1071 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001072 // Clear the dirty region in case we're switching from a non-CPU API
1073 mDirtyRegion.clear();
1074 } else if (!err) {
1075 // Initialize the dirty region for tracking surface damage
1076 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001077 }
Dan Stoza5065a552015-03-17 16:23:42 -07001078
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001079 return err;
1080}
1081
Mathias Agopian365857d2013-09-11 19:35:45 -07001082
Robert Carr97b9c862016-09-08 13:54:35 -07001083int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001084 ATRACE_CALL();
1085 ALOGV("Surface::disconnect");
1086 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001087 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1088 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001089 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001090 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001091 if (!err) {
1092 mReqFormat = 0;
1093 mReqWidth = 0;
1094 mReqHeight = 0;
1095 mReqUsage = 0;
1096 mCrop.clear();
1097 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1098 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001099 mStickyTransform = 0;
1100
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001101 if (api == NATIVE_WINDOW_API_CPU) {
1102 mConnectedToCpu = false;
1103 }
1104 }
1105 return err;
1106}
1107
Dan Stozad9c49712015-04-27 11:06:01 -07001108int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001109 sp<Fence>* outFence) {
1110 ATRACE_CALL();
1111 ALOGV("Surface::detachNextBuffer");
1112
1113 if (outBuffer == NULL || outFence == NULL) {
1114 return BAD_VALUE;
1115 }
1116
1117 Mutex::Autolock lock(mMutex);
1118
1119 sp<GraphicBuffer> buffer(NULL);
1120 sp<Fence> fence(NULL);
1121 status_t result = mGraphicBufferProducer->detachNextBuffer(
1122 &buffer, &fence);
1123 if (result != NO_ERROR) {
1124 return result;
1125 }
1126
Dan Stozad9c49712015-04-27 11:06:01 -07001127 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001128 if (fence != NULL && fence->isValid()) {
1129 *outFence = fence;
1130 } else {
1131 *outFence = Fence::NO_FENCE;
1132 }
1133
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001134 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1135 if (mSlots[i].buffer != NULL &&
1136 mSlots[i].buffer->handle == buffer->handle) {
1137 mSlots[i].buffer = NULL;
1138 }
1139 }
1140
Dan Stoza231832e2015-03-11 11:55:01 -07001141 return NO_ERROR;
1142}
1143
1144int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1145{
1146 ATRACE_CALL();
1147 ALOGV("Surface::attachBuffer");
1148
1149 Mutex::Autolock lock(mMutex);
1150
1151 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001152 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1153 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001154 int32_t attachedSlot = -1;
1155 status_t result = mGraphicBufferProducer->attachBuffer(
1156 &attachedSlot, graphicBuffer);
1157 if (result != NO_ERROR) {
1158 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001159 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001160 return result;
1161 }
1162 mSlots[attachedSlot].buffer = graphicBuffer;
1163
1164 return NO_ERROR;
1165}
1166
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001167int Surface::setUsage(uint32_t reqUsage)
1168{
1169 ALOGV("Surface::setUsage");
1170 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001171 if (reqUsage != mReqUsage) {
1172 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1173 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001174 mReqUsage = reqUsage;
1175 return OK;
1176}
1177
1178int Surface::setCrop(Rect const* rect)
1179{
1180 ATRACE_CALL();
1181
Pablo Ceballos60d69222015-08-07 14:47:20 -07001182 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001183 if (rect == NULL || rect->isEmpty()) {
1184 realRect.clear();
1185 } else {
1186 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001187 }
1188
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001189 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1190 realRect.left, realRect.top, realRect.right, realRect.bottom);
1191
1192 Mutex::Autolock lock(mMutex);
1193 mCrop = realRect;
1194 return NO_ERROR;
1195}
1196
1197int Surface::setBufferCount(int bufferCount)
1198{
1199 ATRACE_CALL();
1200 ALOGV("Surface::setBufferCount");
1201 Mutex::Autolock lock(mMutex);
1202
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001203 status_t err = NO_ERROR;
1204 if (bufferCount == 0) {
1205 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1206 } else {
1207 int minUndequeuedBuffers = 0;
1208 err = mGraphicBufferProducer->query(
1209 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1210 if (err == NO_ERROR) {
1211 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1212 bufferCount - minUndequeuedBuffers);
1213 }
1214 }
Mathias Agopian90147262010-01-22 11:47:55 -08001215
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001216 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1217 bufferCount, strerror(-err));
1218
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001219 return err;
1220}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001221
Pablo Ceballosfa455352015-08-12 17:47:47 -07001222int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1223 ATRACE_CALL();
1224 ALOGV("Surface::setMaxDequeuedBufferCount");
1225 Mutex::Autolock lock(mMutex);
1226
1227 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1228 maxDequeuedBuffers);
1229 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1230 "returned %s", maxDequeuedBuffers, strerror(-err));
1231
Pablo Ceballosfa455352015-08-12 17:47:47 -07001232 return err;
1233}
1234
1235int Surface::setAsyncMode(bool async) {
1236 ATRACE_CALL();
1237 ALOGV("Surface::setAsyncMode");
1238 Mutex::Autolock lock(mMutex);
1239
1240 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1241 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1242 async, strerror(-err));
1243
Pablo Ceballosfa455352015-08-12 17:47:47 -07001244 return err;
1245}
1246
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001247int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001248 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001249 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001250 Mutex::Autolock lock(mMutex);
1251
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001252 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1253 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001254 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001255 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001256 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001257 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1258 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001259
1260 return err;
1261}
1262
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001263int Surface::setAutoRefresh(bool autoRefresh) {
1264 ATRACE_CALL();
1265 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1266 Mutex::Autolock lock(mMutex);
1267
1268 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1269 if (err == NO_ERROR) {
1270 mAutoRefresh = autoRefresh;
1271 }
1272 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1273 autoRefresh, strerror(-err));
1274 return err;
1275}
1276
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001277int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001278{
1279 ATRACE_CALL();
1280 ALOGV("Surface::setBuffersDimensions");
1281
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001282 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001283 return BAD_VALUE;
1284
1285 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001286 if (width != mReqWidth || height != mReqHeight) {
1287 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1288 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001289 mReqWidth = width;
1290 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001291 return NO_ERROR;
1292}
1293
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001294int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001295{
1296 ATRACE_CALL();
1297 ALOGV("Surface::setBuffersUserDimensions");
1298
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001299 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001300 return BAD_VALUE;
1301
1302 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001303 if (width != mUserWidth || height != mUserHeight) {
1304 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1305 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001306 mUserWidth = width;
1307 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001308 return NO_ERROR;
1309}
1310
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001311int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001312{
1313 ALOGV("Surface::setBuffersFormat");
1314
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001315 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001316 if (format != mReqFormat) {
1317 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1318 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001319 mReqFormat = format;
1320 return NO_ERROR;
1321}
1322
1323int Surface::setScalingMode(int mode)
1324{
1325 ATRACE_CALL();
1326 ALOGV("Surface::setScalingMode(%d)", mode);
1327
1328 switch (mode) {
1329 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1330 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1331 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001332 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001333 break;
1334 default:
1335 ALOGE("unknown scaling mode: %d", mode);
1336 return BAD_VALUE;
1337 }
1338
1339 Mutex::Autolock lock(mMutex);
1340 mScalingMode = mode;
1341 return NO_ERROR;
1342}
1343
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001344int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001345{
1346 ATRACE_CALL();
1347 ALOGV("Surface::setBuffersTransform");
1348 Mutex::Autolock lock(mMutex);
1349 mTransform = transform;
1350 return NO_ERROR;
1351}
1352
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001353int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001354{
1355 ATRACE_CALL();
1356 ALOGV("Surface::setBuffersStickyTransform");
1357 Mutex::Autolock lock(mMutex);
1358 mStickyTransform = transform;
1359 return NO_ERROR;
1360}
1361
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001362int Surface::setBuffersTimestamp(int64_t timestamp)
1363{
1364 ALOGV("Surface::setBuffersTimestamp");
1365 Mutex::Autolock lock(mMutex);
1366 mTimestamp = timestamp;
1367 return NO_ERROR;
1368}
1369
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001370int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1371{
1372 ALOGV("Surface::setBuffersDataSpace");
1373 Mutex::Autolock lock(mMutex);
1374 mDataSpace = dataSpace;
1375 return NO_ERROR;
1376}
1377
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001378void Surface::freeAllBuffers() {
1379 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1380 mSlots[i].buffer = 0;
1381 }
1382}
1383
Dan Stoza5065a552015-03-17 16:23:42 -07001384void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1385 ATRACE_CALL();
1386 ALOGV("Surface::setSurfaceDamage");
1387 Mutex::Autolock lock(mMutex);
1388
Dan Stozac62acbd2015-04-21 16:42:49 -07001389 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001390 mDirtyRegion = Region::INVALID_REGION;
1391 return;
1392 }
1393
1394 mDirtyRegion.clear();
1395 for (size_t r = 0; r < numRects; ++r) {
1396 // We intentionally flip top and bottom here, since because they're
1397 // specified with a bottom-left origin, top > bottom, which fails
1398 // validation in the Region class. We will fix this up when we flip to a
1399 // top-left origin in queueBuffer.
1400 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1401 mDirtyRegion.orSelf(rect);
1402 }
1403}
1404
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001405// ----------------------------------------------------------------------
1406// the lock/unlock APIs must be used from the same thread
1407
1408static status_t copyBlt(
1409 const sp<GraphicBuffer>& dst,
1410 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001411 const Region& reg,
1412 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001413{
1414 // src and dst with, height and format must be identical. no verification
1415 // is done here.
1416 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001417 uint8_t* src_bits = NULL;
1418 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1419 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001420 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1421
1422 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001423 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1424 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001425 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001426 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001427
1428 Region::const_iterator head(reg.begin());
1429 Region::const_iterator tail(reg.end());
1430 if (head != tail && src_bits && dst_bits) {
1431 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001432 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1433 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001434
1435 while (head != tail) {
1436 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001437 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001438 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001439 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1440 uint8_t const * s = src_bits +
1441 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1442 uint8_t * d = dst_bits +
1443 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001444 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001445 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001446 h = 1;
1447 }
1448 do {
1449 memcpy(d, s, size);
1450 d += dbpr;
1451 s += sbpr;
1452 } while (--h > 0);
1453 }
1454 }
1455
1456 if (src_bits)
1457 src->unlock();
1458
1459 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001460 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001461
1462 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463}
1464
Mathias Agopiana138f892010-05-21 17:24:35 -07001465// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001466
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001467status_t Surface::lock(
1468 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1469{
1470 if (mLockedBuffer != 0) {
1471 ALOGE("Surface::lock failed, already locked");
1472 return INVALID_OPERATION;
1473 }
1474
1475 if (!mConnectedToCpu) {
1476 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1477 if (err) {
1478 return err;
1479 }
1480 // we're intending to do software rendering from this point
1481 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1482 }
1483
1484 ANativeWindowBuffer* out;
1485 int fenceFd = -1;
1486 status_t err = dequeueBuffer(&out, &fenceFd);
1487 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1488 if (err == NO_ERROR) {
1489 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001490 const Rect bounds(backBuffer->width, backBuffer->height);
1491
1492 Region newDirtyRegion;
1493 if (inOutDirtyBounds) {
1494 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1495 newDirtyRegion.andSelf(bounds);
1496 } else {
1497 newDirtyRegion.set(bounds);
1498 }
1499
1500 // figure out if we can copy the frontbuffer back
1501 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1502 const bool canCopyBack = (frontBuffer != 0 &&
1503 backBuffer->width == frontBuffer->width &&
1504 backBuffer->height == frontBuffer->height &&
1505 backBuffer->format == frontBuffer->format);
1506
1507 if (canCopyBack) {
1508 // copy the area that is invalid and not repainted this round
1509 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001510 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001511 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001512 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001513 } else {
1514 // if we can't copy-back anything, modify the user's dirty
1515 // region to make sure they redraw the whole buffer
1516 newDirtyRegion.set(bounds);
1517 mDirtyRegion.clear();
1518 Mutex::Autolock lock(mMutex);
1519 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1520 mSlots[i].dirtyRegion.clear();
1521 }
1522 }
1523
1524
1525 { // scope for the lock
1526 Mutex::Autolock lock(mMutex);
1527 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1528 if (backBufferSlot >= 0) {
1529 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1530 mDirtyRegion.subtract(dirtyRegion);
1531 dirtyRegion = newDirtyRegion;
1532 }
1533 }
1534
1535 mDirtyRegion.orSelf(newDirtyRegion);
1536 if (inOutDirtyBounds) {
1537 *inOutDirtyBounds = newDirtyRegion.getBounds();
1538 }
1539
1540 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001541 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001542 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001543 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001544
1545 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1546 backBuffer->handle);
1547
1548 if (res != 0) {
1549 err = INVALID_OPERATION;
1550 } else {
1551 mLockedBuffer = backBuffer;
1552 outBuffer->width = backBuffer->width;
1553 outBuffer->height = backBuffer->height;
1554 outBuffer->stride = backBuffer->stride;
1555 outBuffer->format = backBuffer->format;
1556 outBuffer->bits = vaddr;
1557 }
1558 }
1559 return err;
1560}
1561
1562status_t Surface::unlockAndPost()
1563{
1564 if (mLockedBuffer == 0) {
1565 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1566 return INVALID_OPERATION;
1567 }
1568
Francis Hart8f396012014-04-01 15:30:53 +03001569 int fd = -1;
1570 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001571 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1572
Francis Hart8f396012014-04-01 15:30:53 +03001573 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001574 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1575 mLockedBuffer->handle, strerror(-err));
1576
1577 mPostedBuffer = mLockedBuffer;
1578 mLockedBuffer = 0;
1579 return err;
1580}
1581
Robert Carr9f31e292016-04-11 11:15:32 -07001582bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1583 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001584 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001585 return true;
1586 }
1587 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1588}
1589
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001590status_t Surface::getUniqueId(uint64_t* outId) const {
1591 Mutex::Autolock lock(mMutex);
1592 return mGraphicBufferProducer->getUniqueId(outId);
1593}
1594
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001595}; // namespace android