blob: a172c325228cf01da0c4fe40f3cf2d063b9e04dc [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>
Mathias Agopiana67932f2011-04-20 14:20:59 -070031
Dan Stozaf0eaf252014-03-21 13:05:51 -070032#include <gui/IProducerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080033#include <gui/ISurfaceComposer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080034#include <gui/SurfaceComposerClient.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080035#include <gui/GLConsumer.h>
36#include <gui/Surface.h>
37
38#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040namespace android {
41
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070043 const sp<IGraphicBufferProducer>& bufferProducer,
44 bool controlledByApp)
Dan Stoza812ed062015-06-02 15:45:22 -070045 : mGraphicBufferProducer(bufferProducer),
Pablo Ceballos60d69222015-08-07 14:47:20 -070046 mCrop(Rect::EMPTY_RECT),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080047 mGenerationNumber(0),
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070048 mSharedBufferMode(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080049 mAutoRefresh(false),
50 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
Pablo Ceballosbc8c1922016-07-01 14:15:41 -070051 mSharedBufferHasBeenQueued(false),
Brian Anderson069b3652016-07-22 10:32:47 -070052 mQueriedSupportedTimestamps(false),
53 mFrameTimestampsSupportsPresent(false),
Brian Anderson7c3ba8a2016-07-25 12:48:08 -070054 mFrameTimestampsSupportsRetire(false),
55 mEnableFrameTimestamps(false)
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
99sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
100 return mGraphicBufferProducer;
101}
102
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900103void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
104 mGraphicBufferProducer->setSidebandStream(stream);
105}
106
Dan Stoza29a3e902014-06-20 13:13:57 -0700107void Surface::allocateBuffers() {
108 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
109 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700110 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
111 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700112}
113
Dan Stoza812ed062015-06-02 15:45:22 -0700114status_t Surface::setGenerationNumber(uint32_t generation) {
115 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
116 if (result == NO_ERROR) {
117 mGenerationNumber = generation;
118 }
119 return result;
120}
121
Dan Stoza7dde5992015-05-22 09:51:44 -0700122uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700123 Mutex::Autolock lock(mMutex);
124 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700125}
126
Dan Stozac6f30bd2015-06-08 09:32:50 -0700127String8 Surface::getConsumerName() const {
128 return mGraphicBufferProducer->getConsumerName();
129}
130
Dan Stoza127fc632015-06-30 13:43:32 -0700131status_t Surface::setDequeueTimeout(nsecs_t timeout) {
132 return mGraphicBufferProducer->setDequeueTimeout(timeout);
133}
134
Dan Stoza50101d02016-04-07 16:53:23 -0700135status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700136 sp<Fence>* outFence, float outTransformMatrix[16]) {
137 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
138 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700139}
140
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700141void Surface::enableFrameTimestamps(bool enable) {
142 Mutex::Autolock lock(mMutex);
143 mEnableFrameTimestamps = enable;
144}
145
Brian Anderson50143b32016-09-30 14:01:24 -0700146static bool checkConsumerForUpdates(
147 const FrameEvents* e, const uint64_t lastFrameNumber,
148 const nsecs_t* outRefreshStartTime,
149 const nsecs_t* outGlCompositionDoneTime,
150 const nsecs_t* outDisplayPresentTime,
151 const nsecs_t* outDisplayRetireTime,
152 const nsecs_t* outReleaseTime) {
153 bool checkForRefreshStart = (outRefreshStartTime != nullptr) &&
154 !e->hasFirstRefreshStartInfo();
155 bool checkForGlCompositionDone = (outGlCompositionDoneTime != nullptr) &&
156 !e->hasGpuCompositionDoneInfo();
157 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
158 !e->hasDisplayPresentInfo();
159
160 // DisplayRetire and Release are never available for the last frame.
161 bool checkForDisplayRetire = (outDisplayRetireTime != nullptr) &&
162 !e->hasDisplayRetireInfo() && (e->frameNumber != lastFrameNumber);
163 bool checkForRelease = (outReleaseTime != nullptr) &&
164 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
165
166 // RequestedPresent and Acquire info are always available producer-side.
167 return checkForRefreshStart || checkForGlCompositionDone ||
168 checkForDisplayPresent || checkForDisplayRetire || checkForRelease;
169}
170
Brian Anderson3d4039d2016-09-23 16:31:30 -0700171static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
172 if (dst != nullptr) {
173 *dst = Fence::isValidTimestamp(src) ? src : 0;
174 }
175}
176
177static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) {
178 if (dst != nullptr) {
179 nsecs_t signalTime = src->getSignalTime();
180 *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0;
181 }
182}
183
Brian Anderson069b3652016-07-22 10:32:47 -0700184status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700185 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
186 nsecs_t* outRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700187 nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime,
188 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800189 ATRACE_CALL();
190
Brian Anderson3890c392016-07-25 12:48:08 -0700191 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700192
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700193 if (!mEnableFrameTimestamps) {
194 return INVALID_OPERATION;
195 }
196
Brian Anderson3890c392016-07-25 12:48:08 -0700197 // Verify the requested timestamps are supported.
198 querySupportedTimestampsLocked();
199 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
200 return BAD_VALUE;
201 }
202 if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
203 return BAD_VALUE;
Brian Anderson069b3652016-07-22 10:32:47 -0700204 }
205
Brian Anderson3890c392016-07-25 12:48:08 -0700206 FrameEvents* events = mFrameEventHistory.getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700207 if (events == nullptr) {
208 // If the entry isn't available in the producer, it's definitely not
209 // available in the consumer.
210 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700211 }
212
Brian Anderson50143b32016-09-30 14:01:24 -0700213 // Update our cache of events if the requested events are not available.
214 if (checkConsumerForUpdates(events, mLastFrameNumber,
215 outRefreshStartTime, outGlCompositionDoneTime,
216 outDisplayPresentTime, outDisplayRetireTime, outReleaseTime)) {
217 FrameEventHistoryDelta delta;
218 mGraphicBufferProducer->getFrameTimestamps(&delta);
219 mFrameEventHistory.applyDelta(delta);
220 events = mFrameEventHistory.getFrame(frameNumber);
221 }
222
Brian Anderson3890c392016-07-25 12:48:08 -0700223 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700224 // The entry was available before the update, but was overwritten
225 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700226 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800227 }
Brian Anderson069b3652016-07-22 10:32:47 -0700228
Brian Anderson3d4039d2016-09-23 16:31:30 -0700229 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
230 getFrameTimestamp(outRefreshStartTime, events->firstRefreshStartTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700231
Brian Anderson3d4039d2016-09-23 16:31:30 -0700232 getFrameTimestampFence(outAcquireTime, events->acquireFence);
233 getFrameTimestampFence(
234 outGlCompositionDoneTime, events->gpuCompositionDoneFence);
235 getFrameTimestampFence(
236 outDisplayPresentTime, events->displayPresentFence);
237 getFrameTimestampFence(outDisplayRetireTime, events->displayRetireFence);
238 getFrameTimestampFence(outReleaseTime, events->releaseFence);
Brian Anderson069b3652016-07-22 10:32:47 -0700239
240 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800241}
242
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800243int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
244 Surface* c = getSelf(window);
245 return c->setSwapInterval(interval);
246}
247
248int Surface::hook_dequeueBuffer(ANativeWindow* window,
249 ANativeWindowBuffer** buffer, int* fenceFd) {
250 Surface* c = getSelf(window);
251 return c->dequeueBuffer(buffer, fenceFd);
252}
253
254int Surface::hook_cancelBuffer(ANativeWindow* window,
255 ANativeWindowBuffer* buffer, int fenceFd) {
256 Surface* c = getSelf(window);
257 return c->cancelBuffer(buffer, fenceFd);
258}
259
260int Surface::hook_queueBuffer(ANativeWindow* window,
261 ANativeWindowBuffer* buffer, int fenceFd) {
262 Surface* c = getSelf(window);
263 return c->queueBuffer(buffer, fenceFd);
264}
265
266int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
267 ANativeWindowBuffer** buffer) {
268 Surface* c = getSelf(window);
269 ANativeWindowBuffer* buf;
270 int fenceFd = -1;
271 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600272 if (result != OK) {
273 return result;
274 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800275 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700276 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800277 if (waitResult != OK) {
278 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
279 waitResult);
280 c->cancelBuffer(buf, -1);
281 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700282 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800283 *buffer = buf;
284 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700285}
286
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800287int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
288 ANativeWindowBuffer* buffer) {
289 Surface* c = getSelf(window);
290 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700291}
292
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800293int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
294 ANativeWindowBuffer* buffer) {
295 Surface* c = getSelf(window);
296 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700297}
298
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800299int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
300 ANativeWindowBuffer* buffer) {
301 Surface* c = getSelf(window);
302 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700303}
Mathias Agopian62185b72009-04-16 16:19:50 -0700304
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800305int Surface::hook_query(const ANativeWindow* window,
306 int what, int* value) {
307 const Surface* c = getSelf(window);
308 return c->query(what, value);
309}
310
311int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
312 va_list args;
313 va_start(args, operation);
314 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700315 int result = c->perform(operation, args);
316 va_end(args);
317 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800318}
319
320int Surface::setSwapInterval(int interval) {
321 ATRACE_CALL();
322 // EGL specification states:
323 // interval is silently clamped to minimum and maximum implementation
324 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800325
326 if (interval < minSwapInterval)
327 interval = minSwapInterval;
328
329 if (interval > maxSwapInterval)
330 interval = maxSwapInterval;
331
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700332 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700333 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800334
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700335 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800336}
337
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700338int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800339 ATRACE_CALL();
340 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800341
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800342 uint32_t reqWidth;
343 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800344 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800345 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700346 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800347
348 {
349 Mutex::Autolock lock(mMutex);
350
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800351 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
352 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800353
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800354 reqFormat = mReqFormat;
355 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800356
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700357 enableFrameTimestamps = mEnableFrameTimestamps;
358
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700359 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800360 BufferItem::INVALID_BUFFER_SLOT) {
361 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
362 if (gbuf != NULL) {
363 *buffer = gbuf.get();
364 *fenceFd = -1;
365 return OK;
366 }
367 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800368 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
369
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800370 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800371 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700372 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700373
374 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700375 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700376 reqWidth, reqHeight, reqFormat, reqUsage,
377 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700378 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800379
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800380 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700381 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
382 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
383 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800384 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700385 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800386
387 Mutex::Autolock lock(mMutex);
388
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800389 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700390
391 // this should never happen
392 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
393
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800394 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
395 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700396 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700397
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700398 if (enableFrameTimestamps) {
399 mFrameEventHistory.applyDelta(frameTimestamps);
400 }
401
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800402 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
403 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
404 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700405 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700406 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800407 return result;
408 }
409 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700410
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800411 if (fence->isValid()) {
412 *fenceFd = fence->dup();
413 if (*fenceFd == -1) {
414 ALOGE("dequeueBuffer: error duping fence: %d", errno);
415 // dup() should never fail; something is badly wrong. Soldier on
416 // and hope for the best; the worst that should happen is some
417 // visible corruption that lasts until the next frame.
418 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700419 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800420 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700421 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800422
423 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800424
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700425 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800426 mSharedBufferSlot = buf;
427 mSharedBufferHasBeenQueued = false;
428 } else if (mSharedBufferSlot == buf) {
429 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
430 mSharedBufferHasBeenQueued = false;
431 }
432
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800433 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700434}
435
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800436int Surface::cancelBuffer(android_native_buffer_t* buffer,
437 int fenceFd) {
438 ATRACE_CALL();
439 ALOGV("Surface::cancelBuffer");
440 Mutex::Autolock lock(mMutex);
441 int i = getSlotFromBufferLocked(buffer);
442 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900443 if (fenceFd >= 0) {
444 close(fenceFd);
445 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800446 return i;
447 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800448 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
449 if (fenceFd >= 0) {
450 close(fenceFd);
451 }
452 return OK;
453 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800454 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
455 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800456
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700457 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800458 mSharedBufferHasBeenQueued = true;
459 }
460
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800461 return OK;
462}
463
464int Surface::getSlotFromBufferLocked(
465 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800466 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
467 if (mSlots[i].buffer != NULL &&
468 mSlots[i].buffer->handle == buffer->handle) {
469 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700470 }
471 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800472 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
473 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700474}
475
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800476int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800477 ALOGV("Surface::lockBuffer");
478 Mutex::Autolock lock(mMutex);
479 return OK;
480}
Mathias Agopian631f3582010-05-25 17:51:34 -0700481
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800482int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
483 ATRACE_CALL();
484 ALOGV("Surface::queueBuffer");
485 Mutex::Autolock lock(mMutex);
486 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700487 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800488
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800489 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700490 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700491 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700492 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700493 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800494 } else {
495 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700496 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800497 int i = getSlotFromBufferLocked(buffer);
498 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900499 if (fenceFd >= 0) {
500 close(fenceFd);
501 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800502 return i;
503 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800504 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
505 if (fenceFd >= 0) {
506 close(fenceFd);
507 }
508 return OK;
509 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800512 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700513 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800514 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800516 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
517 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700518 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800519 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700520 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700521
Dan Stozac62acbd2015-04-21 16:42:49 -0700522 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700523 input.setSurfaceDamage(Region::INVALID_REGION);
524 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700525 // Here we do two things:
526 // 1) The surface damage was specified using the OpenGL ES convention of
527 // the origin being in the bottom-left corner. Here we flip to the
528 // convention that the rest of the system uses (top-left corner) by
529 // subtracting all top/bottom coordinates from the buffer height.
530 // 2) If the buffer is coming in rotated (for example, because the EGL
531 // implementation is reacting to the transform hint coming back from
532 // SurfaceFlinger), the surface damage needs to be rotated the
533 // opposite direction, since it was generated assuming an unrotated
534 // buffer (the app doesn't know that the EGL implementation is
535 // reacting to the transform hint behind its back). The
536 // transformations in the switch statement below apply those
537 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
538
539 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700540 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700541 bool rotated90 = (mTransform ^ mStickyTransform) &
542 NATIVE_WINDOW_TRANSFORM_ROT_90;
543 if (rotated90) {
544 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700545 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700546
Dan Stoza5065a552015-03-17 16:23:42 -0700547 Region flippedRegion;
548 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700549 int left = rect.left;
550 int right = rect.right;
551 int top = height - rect.bottom; // Flip from OpenGL convention
552 int bottom = height - rect.top; // Flip from OpenGL convention
553 switch (mTransform ^ mStickyTransform) {
554 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
555 // Rotate 270 degrees
556 Rect flippedRect{top, width - right, bottom, width - left};
557 flippedRegion.orSelf(flippedRect);
558 break;
559 }
560 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
561 // Rotate 180 degrees
562 Rect flippedRect{width - right, height - bottom,
563 width - left, height - top};
564 flippedRegion.orSelf(flippedRect);
565 break;
566 }
567 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
568 // Rotate 90 degrees
569 Rect flippedRect{height - bottom, left,
570 height - top, right};
571 flippedRegion.orSelf(flippedRect);
572 break;
573 }
574 default: {
575 Rect flippedRect{left, top, right, bottom};
576 flippedRegion.orSelf(flippedRect);
577 break;
578 }
579 }
Dan Stoza5065a552015-03-17 16:23:42 -0700580 }
581
582 input.setSurfaceDamage(flippedRegion);
583 }
584
Dan Stoza70ccba52016-07-01 14:00:40 -0700585 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800586 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700587 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800588 if (err != OK) {
589 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
590 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800591
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700592 if (mEnableFrameTimestamps) {
593 mFrameEventHistory.applyDelta(output.frameTimestamps);
594 // Update timestamps with the local acquire fence.
595 // The consumer doesn't send it back to prevent us from having two
596 // file descriptors of the same fence.
Brian Anderson3d4039d2016-09-23 16:31:30 -0700597 mFrameEventHistory.updateAcquireFence(mNextFrameNumber,
598 std::make_shared<FenceTime>(std::move(fence)));
599
600 // Cache timestamps of signaled fences so we can close their file
601 // descriptors.
602 mFrameEventHistory.updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700603 }
604
Brian Anderson50143b32016-09-30 14:01:24 -0700605 mLastFrameNumber = mNextFrameNumber;
606
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700607 mDefaultWidth = output.width;
608 mDefaultHeight = output.height;
609 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700610
Ruben Brunk1681d952014-06-27 15:51:55 -0700611 // Disable transform hint if sticky transform is set.
612 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700613 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700614 }
615
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700616 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700617
Dan Stozac62acbd2015-04-21 16:42:49 -0700618 if (!mConnectedToCpu) {
619 // Clear surface damage back to full-buffer
620 mDirtyRegion = Region::INVALID_REGION;
621 }
Dan Stoza5065a552015-03-17 16:23:42 -0700622
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700623 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800624 mSharedBufferHasBeenQueued = true;
625 }
626
Robert Carr9f31e292016-04-11 11:15:32 -0700627 mQueueBufferCondition.broadcast();
628
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800629 return err;
630}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700631
Brian Anderson069b3652016-07-22 10:32:47 -0700632void Surface::querySupportedTimestampsLocked() const {
633 // mMutex must be locked when calling this method.
634
635 if (mQueriedSupportedTimestamps) {
636 return;
637 }
638 mQueriedSupportedTimestamps = true;
639
Brian Anderson3890c392016-07-25 12:48:08 -0700640 std::vector<FrameEvent> supportedFrameTimestamps;
Brian Anderson069b3652016-07-22 10:32:47 -0700641 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
642 status_t err = composer->getSupportedFrameTimestamps(
643 &supportedFrameTimestamps);
644
645 if (err != NO_ERROR) {
646 return;
647 }
648
649 for (auto sft : supportedFrameTimestamps) {
Brian Anderson3890c392016-07-25 12:48:08 -0700650 if (sft == FrameEvent::DISPLAY_PRESENT) {
Brian Anderson069b3652016-07-22 10:32:47 -0700651 mFrameTimestampsSupportsPresent = true;
Brian Anderson3890c392016-07-25 12:48:08 -0700652 } else if (sft == FrameEvent::DISPLAY_RETIRE) {
Brian Anderson069b3652016-07-22 10:32:47 -0700653 mFrameTimestampsSupportsRetire = true;
654 }
655 }
656}
657
Mathias Agopiana67932f2011-04-20 14:20:59 -0700658int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800659 ATRACE_CALL();
660 ALOGV("Surface::query");
661 { // scope for the lock
662 Mutex::Autolock lock(mMutex);
663 switch (what) {
664 case NATIVE_WINDOW_FORMAT:
665 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800666 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800667 return NO_ERROR;
668 }
669 break;
670 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
671 sp<ISurfaceComposer> composer(
672 ComposerService::getComposerService());
673 if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) {
674 *value = 1;
675 } else {
676 *value = 0;
677 }
678 return NO_ERROR;
679 }
680 case NATIVE_WINDOW_CONCRETE_TYPE:
681 *value = NATIVE_WINDOW_SURFACE;
682 return NO_ERROR;
683 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800684 *value = static_cast<int>(
685 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800686 return NO_ERROR;
687 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800688 *value = static_cast<int>(
689 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800690 return NO_ERROR;
691 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800692 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800693 return NO_ERROR;
694 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
695 status_t err = NO_ERROR;
696 if (!mConsumerRunningBehind) {
697 *value = 0;
698 } else {
699 err = mGraphicBufferProducer->query(what, value);
700 if (err == NO_ERROR) {
701 mConsumerRunningBehind = *value;
702 }
703 }
704 return err;
705 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700706 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
707 int64_t durationUs = mLastDequeueDuration / 1000;
708 *value = durationUs > std::numeric_limits<int>::max() ?
709 std::numeric_limits<int>::max() :
710 static_cast<int>(durationUs);
711 return NO_ERROR;
712 }
713 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
714 int64_t durationUs = mLastQueueDuration / 1000;
715 *value = durationUs > std::numeric_limits<int>::max() ?
716 std::numeric_limits<int>::max() :
717 static_cast<int>(durationUs);
718 return NO_ERROR;
719 }
Brian Anderson069b3652016-07-22 10:32:47 -0700720 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
721 querySupportedTimestampsLocked();
722 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
723 return NO_ERROR;
724 }
725 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: {
726 querySupportedTimestampsLocked();
727 *value = mFrameTimestampsSupportsRetire ? 1 : 0;
728 return NO_ERROR;
729 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800730 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700731 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800732 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800733}
734
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800735int Surface::perform(int operation, va_list args)
736{
737 int res = NO_ERROR;
738 switch (operation) {
739 case NATIVE_WINDOW_CONNECT:
740 // deprecated. must return NO_ERROR.
741 break;
742 case NATIVE_WINDOW_DISCONNECT:
743 // deprecated. must return NO_ERROR.
744 break;
745 case NATIVE_WINDOW_SET_USAGE:
746 res = dispatchSetUsage(args);
747 break;
748 case NATIVE_WINDOW_SET_CROP:
749 res = dispatchSetCrop(args);
750 break;
751 case NATIVE_WINDOW_SET_BUFFER_COUNT:
752 res = dispatchSetBufferCount(args);
753 break;
754 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
755 res = dispatchSetBuffersGeometry(args);
756 break;
757 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
758 res = dispatchSetBuffersTransform(args);
759 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700760 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
761 res = dispatchSetBuffersStickyTransform(args);
762 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800763 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
764 res = dispatchSetBuffersTimestamp(args);
765 break;
766 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
767 res = dispatchSetBuffersDimensions(args);
768 break;
769 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
770 res = dispatchSetBuffersUserDimensions(args);
771 break;
772 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
773 res = dispatchSetBuffersFormat(args);
774 break;
775 case NATIVE_WINDOW_LOCK:
776 res = dispatchLock(args);
777 break;
778 case NATIVE_WINDOW_UNLOCK_AND_POST:
779 res = dispatchUnlockAndPost(args);
780 break;
781 case NATIVE_WINDOW_SET_SCALING_MODE:
782 res = dispatchSetScalingMode(args);
783 break;
784 case NATIVE_WINDOW_API_CONNECT:
785 res = dispatchConnect(args);
786 break;
787 case NATIVE_WINDOW_API_DISCONNECT:
788 res = dispatchDisconnect(args);
789 break;
Rachad7cb0d392014-07-29 17:53:53 -0700790 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
791 res = dispatchSetSidebandStream(args);
792 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800793 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
794 res = dispatchSetBuffersDataSpace(args);
795 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700796 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
797 res = dispatchSetSurfaceDamage(args);
798 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700799 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
800 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700801 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800802 case NATIVE_WINDOW_SET_AUTO_REFRESH:
803 res = dispatchSetAutoRefresh(args);
804 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700805 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
806 res = dispatchEnableFrameTimestamps(args);
807 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800808 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
809 res = dispatchGetFrameTimestamps(args);
810 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800811 default:
812 res = NAME_NOT_FOUND;
813 break;
814 }
815 return res;
816}
Mathias Agopiana138f892010-05-21 17:24:35 -0700817
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800818int Surface::dispatchConnect(va_list args) {
819 int api = va_arg(args, int);
820 return connect(api);
821}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800822
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800823int Surface::dispatchDisconnect(va_list args) {
824 int api = va_arg(args, int);
825 return disconnect(api);
826}
827
828int Surface::dispatchSetUsage(va_list args) {
829 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800830 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800831}
832
833int Surface::dispatchSetCrop(va_list args) {
834 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
835 return setCrop(reinterpret_cast<Rect const*>(rect));
836}
837
838int Surface::dispatchSetBufferCount(va_list args) {
839 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800840 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800841}
842
843int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800844 uint32_t width = va_arg(args, uint32_t);
845 uint32_t height = va_arg(args, uint32_t);
846 PixelFormat format = va_arg(args, PixelFormat);
847 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800848 if (err != 0) {
849 return err;
850 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800851 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800852}
853
854int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800855 uint32_t width = va_arg(args, uint32_t);
856 uint32_t height = va_arg(args, uint32_t);
857 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800858}
859
860int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800861 uint32_t width = va_arg(args, uint32_t);
862 uint32_t height = va_arg(args, uint32_t);
863 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800864}
865
866int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800867 PixelFormat format = va_arg(args, PixelFormat);
868 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800869}
870
871int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800872 int mode = va_arg(args, int);
873 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800874}
875
876int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800877 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800878 return setBuffersTransform(transform);
879}
880
Ruben Brunk1681d952014-06-27 15:51:55 -0700881int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800882 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700883 return setBuffersStickyTransform(transform);
884}
885
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800886int Surface::dispatchSetBuffersTimestamp(va_list args) {
887 int64_t timestamp = va_arg(args, int64_t);
888 return setBuffersTimestamp(timestamp);
889}
890
891int Surface::dispatchLock(va_list args) {
892 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
893 ARect* inOutDirtyBounds = va_arg(args, ARect*);
894 return lock(outBuffer, inOutDirtyBounds);
895}
896
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800897int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800898 return unlockAndPost();
899}
900
Rachad7cb0d392014-07-29 17:53:53 -0700901int Surface::dispatchSetSidebandStream(va_list args) {
902 native_handle_t* sH = va_arg(args, native_handle_t*);
903 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
904 setSidebandStream(sidebandHandle);
905 return OK;
906}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800907
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800908int Surface::dispatchSetBuffersDataSpace(va_list args) {
909 android_dataspace dataspace =
910 static_cast<android_dataspace>(va_arg(args, int));
911 return setBuffersDataSpace(dataspace);
912}
913
Dan Stoza5065a552015-03-17 16:23:42 -0700914int Surface::dispatchSetSurfaceDamage(va_list args) {
915 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
916 size_t numRects = va_arg(args, size_t);
917 setSurfaceDamage(rects, numRects);
918 return NO_ERROR;
919}
920
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700921int Surface::dispatchSetSharedBufferMode(va_list args) {
922 bool sharedBufferMode = va_arg(args, int);
923 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800924}
925
926int Surface::dispatchSetAutoRefresh(va_list args) {
927 bool autoRefresh = va_arg(args, int);
928 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700929}
930
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700931int Surface::dispatchEnableFrameTimestamps(va_list args) {
932 bool enable = va_arg(args, int);
933 enableFrameTimestamps(enable);
934 return NO_ERROR;
935}
936
Pablo Ceballosce796e72016-02-04 19:10:51 -0800937int Surface::dispatchGetFrameTimestamps(va_list args) {
938 uint32_t framesAgo = va_arg(args, uint32_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -0700939 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800940 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
941 nsecs_t* outRefreshStartTime = va_arg(args, int64_t*);
942 nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700943 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800944 nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*);
945 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -0700946 return getFrameTimestamps(getNextFrameNumber() - 1 - framesAgo,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700947 outRequestedPresentTime, outAcquireTime, outRefreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -0700948 outGlCompositionDoneTime, outDisplayPresentTime,
949 outDisplayRetireTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -0800950}
951
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800952int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -0800953 static sp<IProducerListener> listener = new DummyProducerListener();
954 return connect(api, listener);
955}
956
957int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800958 ATRACE_CALL();
959 ALOGV("Surface::connect");
960 Mutex::Autolock lock(mMutex);
961 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700962 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800963 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700964 mDefaultWidth = output.width;
965 mDefaultHeight = output.height;
966 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -0700967
968 // Disable transform hint if sticky transform is set.
969 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700970 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700971 }
972
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700973 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800974 }
975 if (!err && api == NATIVE_WINDOW_API_CPU) {
976 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -0700977 // Clear the dirty region in case we're switching from a non-CPU API
978 mDirtyRegion.clear();
979 } else if (!err) {
980 // Initialize the dirty region for tracking surface damage
981 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800982 }
Dan Stoza5065a552015-03-17 16:23:42 -0700983
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800984 return err;
985}
986
Mathias Agopian365857d2013-09-11 19:35:45 -0700987
Robert Carr97b9c862016-09-08 13:54:35 -0700988int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800989 ATRACE_CALL();
990 ALOGV("Surface::disconnect");
991 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800992 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
993 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800994 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -0700995 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800996 if (!err) {
997 mReqFormat = 0;
998 mReqWidth = 0;
999 mReqHeight = 0;
1000 mReqUsage = 0;
1001 mCrop.clear();
1002 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1003 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001004 mStickyTransform = 0;
1005
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001006 if (api == NATIVE_WINDOW_API_CPU) {
1007 mConnectedToCpu = false;
1008 }
1009 }
1010 return err;
1011}
1012
Dan Stozad9c49712015-04-27 11:06:01 -07001013int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001014 sp<Fence>* outFence) {
1015 ATRACE_CALL();
1016 ALOGV("Surface::detachNextBuffer");
1017
1018 if (outBuffer == NULL || outFence == NULL) {
1019 return BAD_VALUE;
1020 }
1021
1022 Mutex::Autolock lock(mMutex);
1023
1024 sp<GraphicBuffer> buffer(NULL);
1025 sp<Fence> fence(NULL);
1026 status_t result = mGraphicBufferProducer->detachNextBuffer(
1027 &buffer, &fence);
1028 if (result != NO_ERROR) {
1029 return result;
1030 }
1031
Dan Stozad9c49712015-04-27 11:06:01 -07001032 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001033 if (fence != NULL && fence->isValid()) {
1034 *outFence = fence;
1035 } else {
1036 *outFence = Fence::NO_FENCE;
1037 }
1038
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001039 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1040 if (mSlots[i].buffer != NULL &&
1041 mSlots[i].buffer->handle == buffer->handle) {
1042 mSlots[i].buffer = NULL;
1043 }
1044 }
1045
Dan Stoza231832e2015-03-11 11:55:01 -07001046 return NO_ERROR;
1047}
1048
1049int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1050{
1051 ATRACE_CALL();
1052 ALOGV("Surface::attachBuffer");
1053
1054 Mutex::Autolock lock(mMutex);
1055
1056 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001057 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1058 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001059 int32_t attachedSlot = -1;
1060 status_t result = mGraphicBufferProducer->attachBuffer(
1061 &attachedSlot, graphicBuffer);
1062 if (result != NO_ERROR) {
1063 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001064 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001065 return result;
1066 }
1067 mSlots[attachedSlot].buffer = graphicBuffer;
1068
1069 return NO_ERROR;
1070}
1071
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001072int Surface::setUsage(uint32_t reqUsage)
1073{
1074 ALOGV("Surface::setUsage");
1075 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001076 if (reqUsage != mReqUsage) {
1077 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1078 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001079 mReqUsage = reqUsage;
1080 return OK;
1081}
1082
1083int Surface::setCrop(Rect const* rect)
1084{
1085 ATRACE_CALL();
1086
Pablo Ceballos60d69222015-08-07 14:47:20 -07001087 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001088 if (rect == NULL || rect->isEmpty()) {
1089 realRect.clear();
1090 } else {
1091 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001092 }
1093
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001094 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1095 realRect.left, realRect.top, realRect.right, realRect.bottom);
1096
1097 Mutex::Autolock lock(mMutex);
1098 mCrop = realRect;
1099 return NO_ERROR;
1100}
1101
1102int Surface::setBufferCount(int bufferCount)
1103{
1104 ATRACE_CALL();
1105 ALOGV("Surface::setBufferCount");
1106 Mutex::Autolock lock(mMutex);
1107
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001108 status_t err = NO_ERROR;
1109 if (bufferCount == 0) {
1110 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1111 } else {
1112 int minUndequeuedBuffers = 0;
1113 err = mGraphicBufferProducer->query(
1114 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1115 if (err == NO_ERROR) {
1116 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1117 bufferCount - minUndequeuedBuffers);
1118 }
1119 }
Mathias Agopian90147262010-01-22 11:47:55 -08001120
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001121 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1122 bufferCount, strerror(-err));
1123
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001124 return err;
1125}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001126
Pablo Ceballosfa455352015-08-12 17:47:47 -07001127int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1128 ATRACE_CALL();
1129 ALOGV("Surface::setMaxDequeuedBufferCount");
1130 Mutex::Autolock lock(mMutex);
1131
1132 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1133 maxDequeuedBuffers);
1134 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1135 "returned %s", maxDequeuedBuffers, strerror(-err));
1136
Pablo Ceballosfa455352015-08-12 17:47:47 -07001137 return err;
1138}
1139
1140int Surface::setAsyncMode(bool async) {
1141 ATRACE_CALL();
1142 ALOGV("Surface::setAsyncMode");
1143 Mutex::Autolock lock(mMutex);
1144
1145 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1146 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1147 async, strerror(-err));
1148
Pablo Ceballosfa455352015-08-12 17:47:47 -07001149 return err;
1150}
1151
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001152int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001153 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001154 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001155 Mutex::Autolock lock(mMutex);
1156
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001157 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1158 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001159 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001160 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001161 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001162 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1163 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001164
1165 return err;
1166}
1167
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001168int Surface::setAutoRefresh(bool autoRefresh) {
1169 ATRACE_CALL();
1170 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1171 Mutex::Autolock lock(mMutex);
1172
1173 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1174 if (err == NO_ERROR) {
1175 mAutoRefresh = autoRefresh;
1176 }
1177 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1178 autoRefresh, strerror(-err));
1179 return err;
1180}
1181
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001182int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001183{
1184 ATRACE_CALL();
1185 ALOGV("Surface::setBuffersDimensions");
1186
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001187 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001188 return BAD_VALUE;
1189
1190 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001191 if (width != mReqWidth || height != mReqHeight) {
1192 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1193 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001194 mReqWidth = width;
1195 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001196 return NO_ERROR;
1197}
1198
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001199int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001200{
1201 ATRACE_CALL();
1202 ALOGV("Surface::setBuffersUserDimensions");
1203
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001204 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001205 return BAD_VALUE;
1206
1207 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001208 if (width != mUserWidth || height != mUserHeight) {
1209 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1210 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001211 mUserWidth = width;
1212 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001213 return NO_ERROR;
1214}
1215
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001216int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001217{
1218 ALOGV("Surface::setBuffersFormat");
1219
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001220 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001221 if (format != mReqFormat) {
1222 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1223 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001224 mReqFormat = format;
1225 return NO_ERROR;
1226}
1227
1228int Surface::setScalingMode(int mode)
1229{
1230 ATRACE_CALL();
1231 ALOGV("Surface::setScalingMode(%d)", mode);
1232
1233 switch (mode) {
1234 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1235 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1236 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001237 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001238 break;
1239 default:
1240 ALOGE("unknown scaling mode: %d", mode);
1241 return BAD_VALUE;
1242 }
1243
1244 Mutex::Autolock lock(mMutex);
1245 mScalingMode = mode;
1246 return NO_ERROR;
1247}
1248
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001249int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001250{
1251 ATRACE_CALL();
1252 ALOGV("Surface::setBuffersTransform");
1253 Mutex::Autolock lock(mMutex);
1254 mTransform = transform;
1255 return NO_ERROR;
1256}
1257
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001258int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001259{
1260 ATRACE_CALL();
1261 ALOGV("Surface::setBuffersStickyTransform");
1262 Mutex::Autolock lock(mMutex);
1263 mStickyTransform = transform;
1264 return NO_ERROR;
1265}
1266
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001267int Surface::setBuffersTimestamp(int64_t timestamp)
1268{
1269 ALOGV("Surface::setBuffersTimestamp");
1270 Mutex::Autolock lock(mMutex);
1271 mTimestamp = timestamp;
1272 return NO_ERROR;
1273}
1274
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001275int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1276{
1277 ALOGV("Surface::setBuffersDataSpace");
1278 Mutex::Autolock lock(mMutex);
1279 mDataSpace = dataSpace;
1280 return NO_ERROR;
1281}
1282
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001283void Surface::freeAllBuffers() {
1284 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1285 mSlots[i].buffer = 0;
1286 }
1287}
1288
Dan Stoza5065a552015-03-17 16:23:42 -07001289void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1290 ATRACE_CALL();
1291 ALOGV("Surface::setSurfaceDamage");
1292 Mutex::Autolock lock(mMutex);
1293
Dan Stozac62acbd2015-04-21 16:42:49 -07001294 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001295 mDirtyRegion = Region::INVALID_REGION;
1296 return;
1297 }
1298
1299 mDirtyRegion.clear();
1300 for (size_t r = 0; r < numRects; ++r) {
1301 // We intentionally flip top and bottom here, since because they're
1302 // specified with a bottom-left origin, top > bottom, which fails
1303 // validation in the Region class. We will fix this up when we flip to a
1304 // top-left origin in queueBuffer.
1305 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1306 mDirtyRegion.orSelf(rect);
1307 }
1308}
1309
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001310// ----------------------------------------------------------------------
1311// the lock/unlock APIs must be used from the same thread
1312
1313static status_t copyBlt(
1314 const sp<GraphicBuffer>& dst,
1315 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001316 const Region& reg,
1317 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001318{
1319 // src and dst with, height and format must be identical. no verification
1320 // is done here.
1321 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001322 uint8_t* src_bits = NULL;
1323 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1324 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001325 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1326
1327 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001328 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1329 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001330 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001331 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001332
1333 Region::const_iterator head(reg.begin());
1334 Region::const_iterator tail(reg.end());
1335 if (head != tail && src_bits && dst_bits) {
1336 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001337 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1338 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001339
1340 while (head != tail) {
1341 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001342 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001343 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001344 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1345 uint8_t const * s = src_bits +
1346 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1347 uint8_t * d = dst_bits +
1348 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001349 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001350 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001351 h = 1;
1352 }
1353 do {
1354 memcpy(d, s, size);
1355 d += dbpr;
1356 s += sbpr;
1357 } while (--h > 0);
1358 }
1359 }
1360
1361 if (src_bits)
1362 src->unlock();
1363
1364 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001365 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001366
1367 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368}
1369
Mathias Agopiana138f892010-05-21 17:24:35 -07001370// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001371
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001372status_t Surface::lock(
1373 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1374{
1375 if (mLockedBuffer != 0) {
1376 ALOGE("Surface::lock failed, already locked");
1377 return INVALID_OPERATION;
1378 }
1379
1380 if (!mConnectedToCpu) {
1381 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1382 if (err) {
1383 return err;
1384 }
1385 // we're intending to do software rendering from this point
1386 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1387 }
1388
1389 ANativeWindowBuffer* out;
1390 int fenceFd = -1;
1391 status_t err = dequeueBuffer(&out, &fenceFd);
1392 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1393 if (err == NO_ERROR) {
1394 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001395 const Rect bounds(backBuffer->width, backBuffer->height);
1396
1397 Region newDirtyRegion;
1398 if (inOutDirtyBounds) {
1399 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1400 newDirtyRegion.andSelf(bounds);
1401 } else {
1402 newDirtyRegion.set(bounds);
1403 }
1404
1405 // figure out if we can copy the frontbuffer back
1406 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1407 const bool canCopyBack = (frontBuffer != 0 &&
1408 backBuffer->width == frontBuffer->width &&
1409 backBuffer->height == frontBuffer->height &&
1410 backBuffer->format == frontBuffer->format);
1411
1412 if (canCopyBack) {
1413 // copy the area that is invalid and not repainted this round
1414 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001415 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001416 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001417 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001418 } else {
1419 // if we can't copy-back anything, modify the user's dirty
1420 // region to make sure they redraw the whole buffer
1421 newDirtyRegion.set(bounds);
1422 mDirtyRegion.clear();
1423 Mutex::Autolock lock(mMutex);
1424 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1425 mSlots[i].dirtyRegion.clear();
1426 }
1427 }
1428
1429
1430 { // scope for the lock
1431 Mutex::Autolock lock(mMutex);
1432 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1433 if (backBufferSlot >= 0) {
1434 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1435 mDirtyRegion.subtract(dirtyRegion);
1436 dirtyRegion = newDirtyRegion;
1437 }
1438 }
1439
1440 mDirtyRegion.orSelf(newDirtyRegion);
1441 if (inOutDirtyBounds) {
1442 *inOutDirtyBounds = newDirtyRegion.getBounds();
1443 }
1444
1445 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001446 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001447 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001448 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001449
1450 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1451 backBuffer->handle);
1452
1453 if (res != 0) {
1454 err = INVALID_OPERATION;
1455 } else {
1456 mLockedBuffer = backBuffer;
1457 outBuffer->width = backBuffer->width;
1458 outBuffer->height = backBuffer->height;
1459 outBuffer->stride = backBuffer->stride;
1460 outBuffer->format = backBuffer->format;
1461 outBuffer->bits = vaddr;
1462 }
1463 }
1464 return err;
1465}
1466
1467status_t Surface::unlockAndPost()
1468{
1469 if (mLockedBuffer == 0) {
1470 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1471 return INVALID_OPERATION;
1472 }
1473
Francis Hart8f396012014-04-01 15:30:53 +03001474 int fd = -1;
1475 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001476 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1477
Francis Hart8f396012014-04-01 15:30:53 +03001478 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001479 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1480 mLockedBuffer->handle, strerror(-err));
1481
1482 mPostedBuffer = mLockedBuffer;
1483 mLockedBuffer = 0;
1484 return err;
1485}
1486
Robert Carr9f31e292016-04-11 11:15:32 -07001487bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1488 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001489 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001490 return true;
1491 }
1492 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1493}
1494
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001495status_t Surface::getUniqueId(uint64_t* outId) const {
1496 Mutex::Autolock lock(mMutex);
1497 return mGraphicBufferProducer->getUniqueId(outId);
1498}
1499
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001500namespace view {
1501
1502status_t Surface::writeToParcel(Parcel* parcel) const {
1503 return writeToParcel(parcel, false);
1504}
1505
1506status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const {
1507 if (parcel == nullptr) return BAD_VALUE;
1508
1509 status_t res = OK;
1510
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001511 if (!nameAlreadyWritten) {
1512 res = parcel->writeString16(name);
1513 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001514
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001515 /* isSingleBuffered defaults to no */
1516 res = parcel->writeInt32(0);
1517 if (res != OK) return res;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001518 }
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001519
1520 res = parcel->writeStrongBinder(
1521 IGraphicBufferProducer::asBinder(graphicBufferProducer));
1522
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001523 return res;
1524}
1525
1526status_t Surface::readFromParcel(const Parcel* parcel) {
1527 return readFromParcel(parcel, false);
1528}
1529
1530status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) {
1531 if (parcel == nullptr) return BAD_VALUE;
1532
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001533 status_t res = OK;
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001534 if (!nameAlreadyRead) {
1535 name = readMaybeEmptyString16(parcel);
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001536 // Discard this for now
1537 int isSingleBuffered;
1538 res = parcel->readInt32(&isSingleBuffered);
1539 if (res != OK) {
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001540 ALOGE("Can't read isSingleBuffered");
Eino-Ville Talvala529a1032016-08-29 17:33:08 -07001541 return res;
1542 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001543 }
1544
1545 sp<IBinder> binder;
1546
Eino-Ville Talvala74079632016-09-22 15:04:04 -07001547 res = parcel->readNullableStrongBinder(&binder);
1548 if (res != OK) {
1549 ALOGE("%s: Can't read strong binder", __FUNCTION__);
1550 return res;
1551 }
Eino-Ville Talvala88612912016-01-06 12:09:11 -08001552
1553 graphicBufferProducer = interface_cast<IGraphicBufferProducer>(binder);
1554
1555 return OK;
1556}
1557
1558String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
1559 size_t len;
1560 const char16_t* str = parcel->readString16Inplace(&len);
1561 if (str != nullptr) {
1562 return String16(str, len);
1563 } else {
1564 return String16();
1565 }
1566}
1567
1568} // namespace view
1569
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001570}; // namespace android