blob: 06fc31d16c4fa42bd856db0266db76aa3ace18b6 [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
Ian Elliott62c48c92017-01-20 13:13:20 -070029#include <ui/DisplayStatInfo.h>
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -070030#include <ui/Fence.h>
31#include <ui/HdrCapabilities.h>
32#include <ui/Region.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070033
Mathias Agopian2b5dd402017-02-07 17:36:19 -080034#include <gui/BufferItem.h>
Dan Stozaf0eaf252014-03-21 13:05:51 -070035#include <gui/IProducerListener.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080036
Mathias Agopian2b5dd402017-02-07 17:36:19 -080037#include <gui/ISurfaceComposer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080038#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -060040#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
41#include <configstore/Utils.h>
42
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043namespace android {
44
Mathias Agopiane3c697f2013-02-14 17:11:02 -080045Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070046 const sp<IGraphicBufferProducer>& bufferProducer,
47 bool controlledByApp)
Dan Stoza812ed062015-06-02 15:45:22 -070048 : mGraphicBufferProducer(bufferProducer),
Pablo Ceballos60d69222015-08-07 14:47:20 -070049 mCrop(Rect::EMPTY_RECT),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080050 mGenerationNumber(0),
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070051 mSharedBufferMode(false),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080052 mAutoRefresh(false),
53 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
Pablo Ceballosbc8c1922016-07-01 14:15:41 -070054 mSharedBufferHasBeenQueued(false),
Brian Anderson3da8d272016-07-28 16:20:47 -070055 mEnableFrameTimestamps(false),
56 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>())
Mathias Agopian62185b72009-04-16 16:19:50 -070057{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080058 // Initialize the ANativeWindow function pointers.
59 ANativeWindow::setSwapInterval = hook_setSwapInterval;
60 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
61 ANativeWindow::cancelBuffer = hook_cancelBuffer;
62 ANativeWindow::queueBuffer = hook_queueBuffer;
63 ANativeWindow::query = hook_query;
64 ANativeWindow::perform = hook_perform;
65
66 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
67 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
68 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
69 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
70
71 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
72 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
73
74 mReqWidth = 0;
75 mReqHeight = 0;
76 mReqFormat = 0;
77 mReqUsage = 0;
78 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080079 mDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080080 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
81 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -070082 mStickyTransform = 0;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080083 mDefaultWidth = 0;
84 mDefaultHeight = 0;
85 mUserWidth = 0;
86 mUserHeight = 0;
87 mTransformHint = 0;
88 mConsumerRunningBehind = false;
89 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070090 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070091 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070092}
93
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070094Surface::~Surface() {
95 if (mConnectedToCpu) {
96 Surface::disconnect(NATIVE_WINDOW_API_CPU);
97 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -080098}
99
Brian Anderson3da8d272016-07-28 16:20:47 -0700100sp<ISurfaceComposer> Surface::composerService() const {
101 return ComposerService::getComposerService();
102}
103
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800104nsecs_t Surface::now() const {
105 return systemTime();
106}
107
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800108sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
109 return mGraphicBufferProducer;
110}
111
Wonsik Kim0ee14ca2014-03-17 17:46:53 +0900112void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
113 mGraphicBufferProducer->setSidebandStream(stream);
114}
115
Dan Stoza29a3e902014-06-20 13:13:57 -0700116void Surface::allocateBuffers() {
117 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
118 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700119 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
120 mReqFormat, mReqUsage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700121}
122
Dan Stoza812ed062015-06-02 15:45:22 -0700123status_t Surface::setGenerationNumber(uint32_t generation) {
124 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
125 if (result == NO_ERROR) {
126 mGenerationNumber = generation;
127 }
128 return result;
129}
130
Dan Stoza7dde5992015-05-22 09:51:44 -0700131uint64_t Surface::getNextFrameNumber() const {
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700132 Mutex::Autolock lock(mMutex);
133 return mNextFrameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -0700134}
135
Dan Stozac6f30bd2015-06-08 09:32:50 -0700136String8 Surface::getConsumerName() const {
137 return mGraphicBufferProducer->getConsumerName();
138}
139
Dan Stoza127fc632015-06-30 13:43:32 -0700140status_t Surface::setDequeueTimeout(nsecs_t timeout) {
141 return mGraphicBufferProducer->setDequeueTimeout(timeout);
142}
143
Dan Stoza50101d02016-04-07 16:53:23 -0700144status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700145 sp<Fence>* outFence, float outTransformMatrix[16]) {
146 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
147 outTransformMatrix);
Dan Stoza50101d02016-04-07 16:53:23 -0700148}
149
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800150status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
151 ATRACE_CALL();
152
153 DisplayStatInfo stats;
154 status_t err = composerService()->getDisplayStats(NULL, &stats);
155
156 *outRefreshDuration = stats.vsyncPeriod;
157
158 return NO_ERROR;
159}
160
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700161void Surface::enableFrameTimestamps(bool enable) {
162 Mutex::Autolock lock(mMutex);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800163 // If going from disabled to enabled, get the initial values for
164 // compositor and display timing.
165 if (!mEnableFrameTimestamps && enable) {
166 FrameEventHistoryDelta delta;
167 mGraphicBufferProducer->getFrameTimestamps(&delta);
168 mFrameEventHistory->applyDelta(delta);
169 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700170 mEnableFrameTimestamps = enable;
171}
172
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800173status_t Surface::getCompositorTiming(
174 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
175 nsecs_t* compositeToPresentLatency) {
176 Mutex::Autolock lock(mMutex);
177 if (!mEnableFrameTimestamps) {
178 return INVALID_OPERATION;
179 }
180
181 if (compositeDeadline != nullptr) {
182 *compositeDeadline =
183 mFrameEventHistory->getNextCompositeDeadline(now());
184 }
185 if (compositeInterval != nullptr) {
186 *compositeInterval = mFrameEventHistory->getCompositeInterval();
187 }
188 if (compositeToPresentLatency != nullptr) {
189 *compositeToPresentLatency =
190 mFrameEventHistory->getCompositeToPresentLatency();
191 }
192 return NO_ERROR;
193}
194
Brian Anderson50143b32016-09-30 14:01:24 -0700195static bool checkConsumerForUpdates(
196 const FrameEvents* e, const uint64_t lastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700197 const nsecs_t* outLatchTime,
198 const nsecs_t* outFirstRefreshStartTime,
199 const nsecs_t* outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700200 const nsecs_t* outGpuCompositionDoneTime,
Brian Anderson50143b32016-09-30 14:01:24 -0700201 const nsecs_t* outDisplayPresentTime,
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();
Brian Andersonb04c6f02016-10-21 12:57:46 -0700207 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
Brian Anderson50143b32016-09-30 14:01:24 -0700208 !e->hasGpuCompositionDoneInfo();
209 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
210 !e->hasDisplayPresentInfo();
211
Brian Anderson4e606e32017-03-16 15:34:57 -0700212 // LastRefreshStart, DequeueReady, and Release are never
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700213 // available for the last frame.
214 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
215 !e->hasLastRefreshStartInfo() &&
216 (e->frameNumber != lastFrameNumber);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700217 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
218 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700219 bool checkForRelease = (outReleaseTime != nullptr) &&
220 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
221
222 // RequestedPresent and Acquire info are always available producer-side.
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700223 return checkForLatch || checkForFirstRefreshStart ||
Brian Andersonb04c6f02016-10-21 12:57:46 -0700224 checkForLastRefreshStart || checkForGpuCompositionDone ||
Brian Anderson4e606e32017-03-16 15:34:57 -0700225 checkForDisplayPresent || checkForDequeueReady || checkForRelease;
Brian Anderson50143b32016-09-30 14:01:24 -0700226}
227
Brian Anderson3d4039d2016-09-23 16:31:30 -0700228static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
229 if (dst != nullptr) {
Brian Andersoned816e62016-10-26 16:12:21 -0700230 *dst = FrameEvents::isValidTimestamp(src) ? src : 0;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700231 }
232}
233
234static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) {
235 if (dst != nullptr) {
236 nsecs_t signalTime = src->getSignalTime();
237 *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0;
238 }
239}
240
Brian Anderson069b3652016-07-22 10:32:47 -0700241status_t Surface::getFrameTimestamps(uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -0700242 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700243 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700244 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700245 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
246 nsecs_t* outReleaseTime) {
Pablo Ceballosce796e72016-02-04 19:10:51 -0800247 ATRACE_CALL();
248
Brian Anderson3890c392016-07-25 12:48:08 -0700249 Mutex::Autolock lock(mMutex);
Brian Anderson069b3652016-07-22 10:32:47 -0700250
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700251 if (!mEnableFrameTimestamps) {
252 return INVALID_OPERATION;
253 }
254
Brian Anderson3da8d272016-07-28 16:20:47 -0700255 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700256 if (events == nullptr) {
257 // If the entry isn't available in the producer, it's definitely not
258 // available in the consumer.
259 return NAME_NOT_FOUND;
Brian Anderson3890c392016-07-25 12:48:08 -0700260 }
261
Brian Anderson50143b32016-09-30 14:01:24 -0700262 // Update our cache of events if the requested events are not available.
263 if (checkConsumerForUpdates(events, mLastFrameNumber,
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700264 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -0700265 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -0700266 outDequeueReadyTime, outReleaseTime)) {
Brian Anderson50143b32016-09-30 14:01:24 -0700267 FrameEventHistoryDelta delta;
268 mGraphicBufferProducer->getFrameTimestamps(&delta);
Brian Anderson3da8d272016-07-28 16:20:47 -0700269 mFrameEventHistory->applyDelta(delta);
270 events = mFrameEventHistory->getFrame(frameNumber);
Brian Anderson50143b32016-09-30 14:01:24 -0700271 }
272
Brian Anderson3890c392016-07-25 12:48:08 -0700273 if (events == nullptr) {
Brian Anderson50143b32016-09-30 14:01:24 -0700274 // The entry was available before the update, but was overwritten
275 // after the update. Make sure not to send the wrong frame's data.
Brian Anderson069b3652016-07-22 10:32:47 -0700276 return NAME_NOT_FOUND;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800277 }
Brian Anderson069b3652016-07-22 10:32:47 -0700278
Brian Anderson3d4039d2016-09-23 16:31:30 -0700279 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
Brian Andersonf7fd56a2016-09-02 10:10:04 -0700280 getFrameTimestamp(outLatchTime, events->latchTime);
281 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
282 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
283 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
Brian Anderson3890c392016-07-25 12:48:08 -0700284
Brian Anderson3d4039d2016-09-23 16:31:30 -0700285 getFrameTimestampFence(outAcquireTime, events->acquireFence);
286 getFrameTimestampFence(
Brian Andersonb04c6f02016-10-21 12:57:46 -0700287 outGpuCompositionDoneTime, events->gpuCompositionDoneFence);
Brian Anderson3d4039d2016-09-23 16:31:30 -0700288 getFrameTimestampFence(
289 outDisplayPresentTime, events->displayPresentFence);
Brian Anderson3d4039d2016-09-23 16:31:30 -0700290 getFrameTimestampFence(outReleaseTime, events->releaseFence);
Brian Anderson069b3652016-07-22 10:32:47 -0700291
292 return NO_ERROR;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800293}
294
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600295using namespace android::hardware::configstore;
296using namespace android::hardware::configstore::V1_0;
297
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700298status_t Surface::getWideColorSupport(bool* supported) {
299 ATRACE_CALL();
300
301 sp<IBinder> display(
302 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
303 Vector<android_color_mode_t> colorModes;
304 status_t err =
305 composerService()->getDisplayColorModes(display, &colorModes);
306
307 if (err)
308 return err;
309
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600310 bool wideColorBoardConfig =
311 getBool<ISurfaceFlingerConfigs,
312 &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
313
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700314 *supported = false;
315 for (android_color_mode_t colorMode : colorModes) {
316 switch (colorMode) {
317 case HAL_COLOR_MODE_DISPLAY_P3:
318 case HAL_COLOR_MODE_ADOBE_RGB:
319 case HAL_COLOR_MODE_DCI_P3:
Courtney Goeltzenleuchtera0c93e12017-03-17 16:16:48 -0600320 if (wideColorBoardConfig) {
321 *supported = true;
322 }
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700323 break;
324 default:
325 break;
326 }
327 }
328
329 return NO_ERROR;
330}
331
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700332status_t Surface::getHdrSupport(bool* supported) {
333 ATRACE_CALL();
334
335 sp<IBinder> display(
336 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
337 HdrCapabilities hdrCapabilities;
338 status_t err =
339 composerService()->getHdrCapabilities(display, &hdrCapabilities);
340
341 if (err)
342 return err;
343
344 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
345
346 return NO_ERROR;
347}
348
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800349int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
350 Surface* c = getSelf(window);
351 return c->setSwapInterval(interval);
352}
353
354int Surface::hook_dequeueBuffer(ANativeWindow* window,
355 ANativeWindowBuffer** buffer, int* fenceFd) {
356 Surface* c = getSelf(window);
357 return c->dequeueBuffer(buffer, fenceFd);
358}
359
360int Surface::hook_cancelBuffer(ANativeWindow* window,
361 ANativeWindowBuffer* buffer, int fenceFd) {
362 Surface* c = getSelf(window);
363 return c->cancelBuffer(buffer, fenceFd);
364}
365
366int Surface::hook_queueBuffer(ANativeWindow* window,
367 ANativeWindowBuffer* buffer, int fenceFd) {
368 Surface* c = getSelf(window);
369 return c->queueBuffer(buffer, fenceFd);
370}
371
372int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
373 ANativeWindowBuffer** buffer) {
374 Surface* c = getSelf(window);
375 ANativeWindowBuffer* buf;
376 int fenceFd = -1;
377 int result = c->dequeueBuffer(&buf, &fenceFd);
Mike Stroyan87709c92016-06-03 12:43:26 -0600378 if (result != OK) {
379 return result;
380 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800381 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700382 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800383 if (waitResult != OK) {
384 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
385 waitResult);
386 c->cancelBuffer(buf, -1);
387 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700388 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800389 *buffer = buf;
390 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700391}
392
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800393int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
394 ANativeWindowBuffer* buffer) {
395 Surface* c = getSelf(window);
396 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700397}
398
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800399int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
400 ANativeWindowBuffer* buffer) {
401 Surface* c = getSelf(window);
402 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700403}
404
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800405int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
406 ANativeWindowBuffer* buffer) {
407 Surface* c = getSelf(window);
408 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700409}
Mathias Agopian62185b72009-04-16 16:19:50 -0700410
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800411int Surface::hook_query(const ANativeWindow* window,
412 int what, int* value) {
413 const Surface* c = getSelf(window);
414 return c->query(what, value);
415}
416
417int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
418 va_list args;
419 va_start(args, operation);
420 Surface* c = getSelf(window);
Haixia Shid89c2bb2015-09-14 11:02:18 -0700421 int result = c->perform(operation, args);
422 va_end(args);
423 return result;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800424}
425
426int Surface::setSwapInterval(int interval) {
427 ATRACE_CALL();
428 // EGL specification states:
429 // interval is silently clamped to minimum and maximum implementation
430 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800431
432 if (interval < minSwapInterval)
433 interval = minSwapInterval;
434
435 if (interval > maxSwapInterval)
436 interval = maxSwapInterval;
437
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700438 mSwapIntervalZero = (interval == 0);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700439 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800440
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700441 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800442}
443
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700444int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800445 ATRACE_CALL();
446 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800447
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800448 uint32_t reqWidth;
449 uint32_t reqHeight;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800450 PixelFormat reqFormat;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800451 uint32_t reqUsage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700452 bool enableFrameTimestamps;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800453
454 {
455 Mutex::Autolock lock(mMutex);
456
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800457 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
458 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800459
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800460 reqFormat = mReqFormat;
461 reqUsage = mReqUsage;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800462
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700463 enableFrameTimestamps = mEnableFrameTimestamps;
464
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700465 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800466 BufferItem::INVALID_BUFFER_SLOT) {
467 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
468 if (gbuf != NULL) {
469 *buffer = gbuf.get();
470 *fenceFd = -1;
471 return OK;
472 }
473 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800474 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
475
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800476 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800477 sp<Fence> fence;
Dan Stoza70ccba52016-07-01 14:00:40 -0700478 nsecs_t now = systemTime();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700479
480 FrameEventHistoryDelta frameTimestamps;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700481 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
Brian Anderson50143b32016-09-30 14:01:24 -0700482 reqWidth, reqHeight, reqFormat, reqUsage,
483 enableFrameTimestamps ? &frameTimestamps : nullptr);
Dan Stoza70ccba52016-07-01 14:00:40 -0700484 mLastDequeueDuration = systemTime() - now;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800485
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800486 if (result < 0) {
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700487 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
488 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
489 reqUsage, result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800490 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700491 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800492
493 Mutex::Autolock lock(mMutex);
494
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800495 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700496
497 // this should never happen
498 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
499
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800500 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
501 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700502 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700503
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700504 if (enableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700505 mFrameEventHistory->applyDelta(frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700506 }
507
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800508 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
509 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
510 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700511 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Jesse Hall9f5a1b62014-10-02 11:09:03 -0700512 mGraphicBufferProducer->cancelBuffer(buf, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800513 return result;
514 }
515 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700516
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800517 if (fence->isValid()) {
518 *fenceFd = fence->dup();
519 if (*fenceFd == -1) {
520 ALOGE("dequeueBuffer: error duping fence: %d", errno);
521 // dup() should never fail; something is badly wrong. Soldier on
522 // and hope for the best; the worst that should happen is some
523 // visible corruption that lasts until the next frame.
524 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700525 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800526 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700527 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800528
529 *buffer = gbuf.get();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800530
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700531 if (mSharedBufferMode && mAutoRefresh) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800532 mSharedBufferSlot = buf;
533 mSharedBufferHasBeenQueued = false;
534 } else if (mSharedBufferSlot == buf) {
535 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
536 mSharedBufferHasBeenQueued = false;
537 }
538
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800539 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700540}
541
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800542int Surface::cancelBuffer(android_native_buffer_t* buffer,
543 int fenceFd) {
544 ATRACE_CALL();
545 ALOGV("Surface::cancelBuffer");
546 Mutex::Autolock lock(mMutex);
547 int i = getSlotFromBufferLocked(buffer);
548 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900549 if (fenceFd >= 0) {
550 close(fenceFd);
551 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800552 return i;
553 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800554 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
555 if (fenceFd >= 0) {
556 close(fenceFd);
557 }
558 return OK;
559 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800560 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
561 mGraphicBufferProducer->cancelBuffer(i, fence);
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800562
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700563 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800564 mSharedBufferHasBeenQueued = true;
565 }
566
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800567 return OK;
568}
569
570int Surface::getSlotFromBufferLocked(
571 android_native_buffer_t* buffer) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800572 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
573 if (mSlots[i].buffer != NULL &&
574 mSlots[i].buffer->handle == buffer->handle) {
575 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700576 }
577 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800578 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
579 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700580}
581
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800582int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800583 ALOGV("Surface::lockBuffer");
584 Mutex::Autolock lock(mMutex);
585 return OK;
586}
Mathias Agopian631f3582010-05-25 17:51:34 -0700587
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800588int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
589 ATRACE_CALL();
590 ALOGV("Surface::queueBuffer");
591 Mutex::Autolock lock(mMutex);
592 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700593 bool isAutoTimestamp = false;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800594
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800595 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700596 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700597 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700598 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
Colin Cross9a80d502016-09-27 14:12:48 -0700599 timestamp / 1000000.0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800600 } else {
601 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700602 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800603 int i = getSlotFromBufferLocked(buffer);
604 if (i < 0) {
Taiju Tsuiki4d0cd3f2015-04-30 22:15:33 +0900605 if (fenceFd >= 0) {
606 close(fenceFd);
607 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800608 return i;
609 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800610 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
611 if (fenceFd >= 0) {
612 close(fenceFd);
613 }
614 return OK;
615 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800618 // Make sure the crop rectangle is entirely inside the buffer.
Pablo Ceballos60d69222015-08-07 14:47:20 -0700619 Rect crop(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800620 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800622 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
623 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700624 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800625 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700626 fence, mStickyTransform, mEnableFrameTimestamps);
Dan Stoza5065a552015-03-17 16:23:42 -0700627
Dan Stozac62acbd2015-04-21 16:42:49 -0700628 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
Dan Stoza5065a552015-03-17 16:23:42 -0700629 input.setSurfaceDamage(Region::INVALID_REGION);
630 } else {
Dan Stozadb4850c2015-06-25 16:10:18 -0700631 // Here we do two things:
632 // 1) The surface damage was specified using the OpenGL ES convention of
633 // the origin being in the bottom-left corner. Here we flip to the
634 // convention that the rest of the system uses (top-left corner) by
635 // subtracting all top/bottom coordinates from the buffer height.
636 // 2) If the buffer is coming in rotated (for example, because the EGL
637 // implementation is reacting to the transform hint coming back from
638 // SurfaceFlinger), the surface damage needs to be rotated the
639 // opposite direction, since it was generated assuming an unrotated
640 // buffer (the app doesn't know that the EGL implementation is
641 // reacting to the transform hint behind its back). The
642 // transformations in the switch statement below apply those
643 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
644
645 int width = buffer->width;
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700646 int height = buffer->height;
Dan Stozadb4850c2015-06-25 16:10:18 -0700647 bool rotated90 = (mTransform ^ mStickyTransform) &
648 NATIVE_WINDOW_TRANSFORM_ROT_90;
649 if (rotated90) {
650 std::swap(width, height);
Dan Stoza0e65e6c2015-05-26 13:22:27 -0700651 }
Dan Stozadb4850c2015-06-25 16:10:18 -0700652
Dan Stoza5065a552015-03-17 16:23:42 -0700653 Region flippedRegion;
654 for (auto rect : mDirtyRegion) {
Dan Stozadb4850c2015-06-25 16:10:18 -0700655 int left = rect.left;
656 int right = rect.right;
657 int top = height - rect.bottom; // Flip from OpenGL convention
658 int bottom = height - rect.top; // Flip from OpenGL convention
659 switch (mTransform ^ mStickyTransform) {
660 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
661 // Rotate 270 degrees
662 Rect flippedRect{top, width - right, bottom, width - left};
663 flippedRegion.orSelf(flippedRect);
664 break;
665 }
666 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
667 // Rotate 180 degrees
668 Rect flippedRect{width - right, height - bottom,
669 width - left, height - top};
670 flippedRegion.orSelf(flippedRect);
671 break;
672 }
673 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
674 // Rotate 90 degrees
675 Rect flippedRect{height - bottom, left,
676 height - top, right};
677 flippedRegion.orSelf(flippedRect);
678 break;
679 }
680 default: {
681 Rect flippedRect{left, top, right, bottom};
682 flippedRegion.orSelf(flippedRect);
683 break;
684 }
685 }
Dan Stoza5065a552015-03-17 16:23:42 -0700686 }
687
688 input.setSurfaceDamage(flippedRegion);
689 }
690
Dan Stoza70ccba52016-07-01 14:00:40 -0700691 nsecs_t now = systemTime();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800692 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
Dan Stoza70ccba52016-07-01 14:00:40 -0700693 mLastQueueDuration = systemTime() - now;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800694 if (err != OK) {
695 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
696 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800697
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700698 if (mEnableFrameTimestamps) {
Brian Anderson3da8d272016-07-28 16:20:47 -0700699 mFrameEventHistory->applyDelta(output.frameTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700700 // Update timestamps with the local acquire fence.
701 // The consumer doesn't send it back to prevent us from having two
702 // file descriptors of the same fence.
Brian Anderson3da8d272016-07-28 16:20:47 -0700703 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700704 std::make_shared<FenceTime>(std::move(fence)));
705
706 // Cache timestamps of signaled fences so we can close their file
707 // descriptors.
Brian Anderson3da8d272016-07-28 16:20:47 -0700708 mFrameEventHistory->updateSignalTimes();
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700709 }
710
Brian Anderson50143b32016-09-30 14:01:24 -0700711 mLastFrameNumber = mNextFrameNumber;
712
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700713 mDefaultWidth = output.width;
714 mDefaultHeight = output.height;
715 mNextFrameNumber = output.nextFrameNumber;
tedbo1e7fa9e2011-06-22 15:52:53 -0700716
Ruben Brunk1681d952014-06-27 15:51:55 -0700717 // Disable transform hint if sticky transform is set.
718 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700719 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -0700720 }
721
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700722 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700723
Dan Stozac62acbd2015-04-21 16:42:49 -0700724 if (!mConnectedToCpu) {
725 // Clear surface damage back to full-buffer
726 mDirtyRegion = Region::INVALID_REGION;
727 }
Dan Stoza5065a552015-03-17 16:23:42 -0700728
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700729 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800730 mSharedBufferHasBeenQueued = true;
731 }
732
Robert Carr9f31e292016-04-11 11:15:32 -0700733 mQueueBufferCondition.broadcast();
734
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800735 return err;
736}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700737
Mathias Agopiana67932f2011-04-20 14:20:59 -0700738int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800739 ATRACE_CALL();
740 ALOGV("Surface::query");
741 { // scope for the lock
742 Mutex::Autolock lock(mMutex);
743 switch (what) {
744 case NATIVE_WINDOW_FORMAT:
745 if (mReqFormat) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800746 *value = static_cast<int>(mReqFormat);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800747 return NO_ERROR;
748 }
749 break;
750 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
Brian Anderson3da8d272016-07-28 16:20:47 -0700751 if (composerService()->authenticateSurfaceTexture(
752 mGraphicBufferProducer)) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800753 *value = 1;
754 } else {
755 *value = 0;
756 }
757 return NO_ERROR;
758 }
759 case NATIVE_WINDOW_CONCRETE_TYPE:
760 *value = NATIVE_WINDOW_SURFACE;
761 return NO_ERROR;
762 case NATIVE_WINDOW_DEFAULT_WIDTH:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800763 *value = static_cast<int>(
764 mUserWidth ? mUserWidth : mDefaultWidth);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800765 return NO_ERROR;
766 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800767 *value = static_cast<int>(
768 mUserHeight ? mUserHeight : mDefaultHeight);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800769 return NO_ERROR;
770 case NATIVE_WINDOW_TRANSFORM_HINT:
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800771 *value = static_cast<int>(mTransformHint);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800772 return NO_ERROR;
773 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
774 status_t err = NO_ERROR;
775 if (!mConsumerRunningBehind) {
776 *value = 0;
777 } else {
778 err = mGraphicBufferProducer->query(what, value);
779 if (err == NO_ERROR) {
780 mConsumerRunningBehind = *value;
781 }
782 }
783 return err;
784 }
Dan Stoza70ccba52016-07-01 14:00:40 -0700785 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
786 int64_t durationUs = mLastDequeueDuration / 1000;
787 *value = durationUs > std::numeric_limits<int>::max() ?
788 std::numeric_limits<int>::max() :
789 static_cast<int>(durationUs);
790 return NO_ERROR;
791 }
792 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
793 int64_t durationUs = mLastQueueDuration / 1000;
794 *value = durationUs > std::numeric_limits<int>::max() ?
795 std::numeric_limits<int>::max() :
796 static_cast<int>(durationUs);
797 return NO_ERROR;
798 }
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800799 case NATIVE_WINDOW_IS_VALID: {
800 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
801 return NO_ERROR;
802 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800803 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700804 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800805 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800806}
807
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800808int Surface::perform(int operation, va_list args)
809{
810 int res = NO_ERROR;
811 switch (operation) {
812 case NATIVE_WINDOW_CONNECT:
813 // deprecated. must return NO_ERROR.
814 break;
815 case NATIVE_WINDOW_DISCONNECT:
816 // deprecated. must return NO_ERROR.
817 break;
818 case NATIVE_WINDOW_SET_USAGE:
819 res = dispatchSetUsage(args);
820 break;
821 case NATIVE_WINDOW_SET_CROP:
822 res = dispatchSetCrop(args);
823 break;
824 case NATIVE_WINDOW_SET_BUFFER_COUNT:
825 res = dispatchSetBufferCount(args);
826 break;
827 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
828 res = dispatchSetBuffersGeometry(args);
829 break;
830 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
831 res = dispatchSetBuffersTransform(args);
832 break;
Ruben Brunk1681d952014-06-27 15:51:55 -0700833 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
834 res = dispatchSetBuffersStickyTransform(args);
835 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800836 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
837 res = dispatchSetBuffersTimestamp(args);
838 break;
839 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
840 res = dispatchSetBuffersDimensions(args);
841 break;
842 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
843 res = dispatchSetBuffersUserDimensions(args);
844 break;
845 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
846 res = dispatchSetBuffersFormat(args);
847 break;
848 case NATIVE_WINDOW_LOCK:
849 res = dispatchLock(args);
850 break;
851 case NATIVE_WINDOW_UNLOCK_AND_POST:
852 res = dispatchUnlockAndPost(args);
853 break;
854 case NATIVE_WINDOW_SET_SCALING_MODE:
855 res = dispatchSetScalingMode(args);
856 break;
857 case NATIVE_WINDOW_API_CONNECT:
858 res = dispatchConnect(args);
859 break;
860 case NATIVE_WINDOW_API_DISCONNECT:
861 res = dispatchDisconnect(args);
862 break;
Rachad7cb0d392014-07-29 17:53:53 -0700863 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
864 res = dispatchSetSidebandStream(args);
865 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800866 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
867 res = dispatchSetBuffersDataSpace(args);
868 break;
Dan Stoza5065a552015-03-17 16:23:42 -0700869 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
870 res = dispatchSetSurfaceDamage(args);
871 break;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700872 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
873 res = dispatchSetSharedBufferMode(args);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700874 break;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800875 case NATIVE_WINDOW_SET_AUTO_REFRESH:
876 res = dispatchSetAutoRefresh(args);
877 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800878 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
879 res = dispatchGetDisplayRefreshCycleDuration(args);
880 break;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800881 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
882 res = dispatchGetNextFrameId(args);
883 break;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700884 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
885 res = dispatchEnableFrameTimestamps(args);
886 break;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800887 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
888 res = dispatchGetCompositorTiming(args);
889 break;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800890 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
891 res = dispatchGetFrameTimestamps(args);
892 break;
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -0700893 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
894 res = dispatchGetWideColorSupport(args);
895 break;
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -0700896 case NATIVE_WINDOW_GET_HDR_SUPPORT:
897 res = dispatchGetHdrSupport(args);
898 break;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800899 default:
900 res = NAME_NOT_FOUND;
901 break;
902 }
903 return res;
904}
Mathias Agopiana138f892010-05-21 17:24:35 -0700905
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800906int Surface::dispatchConnect(va_list args) {
907 int api = va_arg(args, int);
908 return connect(api);
909}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800910
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800911int Surface::dispatchDisconnect(va_list args) {
912 int api = va_arg(args, int);
913 return disconnect(api);
914}
915
916int Surface::dispatchSetUsage(va_list args) {
917 int usage = va_arg(args, int);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800918 return setUsage(static_cast<uint32_t>(usage));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800919}
920
921int Surface::dispatchSetCrop(va_list args) {
922 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
923 return setCrop(reinterpret_cast<Rect const*>(rect));
924}
925
926int Surface::dispatchSetBufferCount(va_list args) {
927 size_t bufferCount = va_arg(args, size_t);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800928 return setBufferCount(static_cast<int32_t>(bufferCount));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800929}
930
931int Surface::dispatchSetBuffersGeometry(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800932 uint32_t width = va_arg(args, uint32_t);
933 uint32_t height = va_arg(args, uint32_t);
934 PixelFormat format = va_arg(args, PixelFormat);
935 int err = setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800936 if (err != 0) {
937 return err;
938 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800939 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800940}
941
942int Surface::dispatchSetBuffersDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800943 uint32_t width = va_arg(args, uint32_t);
944 uint32_t height = va_arg(args, uint32_t);
945 return setBuffersDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800946}
947
948int Surface::dispatchSetBuffersUserDimensions(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800949 uint32_t width = va_arg(args, uint32_t);
950 uint32_t height = va_arg(args, uint32_t);
951 return setBuffersUserDimensions(width, height);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800952}
953
954int Surface::dispatchSetBuffersFormat(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800955 PixelFormat format = va_arg(args, PixelFormat);
956 return setBuffersFormat(format);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800957}
958
959int Surface::dispatchSetScalingMode(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800960 int mode = va_arg(args, int);
961 return setScalingMode(mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800962}
963
964int Surface::dispatchSetBuffersTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800965 uint32_t transform = va_arg(args, uint32_t);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800966 return setBuffersTransform(transform);
967}
968
Ruben Brunk1681d952014-06-27 15:51:55 -0700969int Surface::dispatchSetBuffersStickyTransform(va_list args) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800970 uint32_t transform = va_arg(args, uint32_t);
Ruben Brunk1681d952014-06-27 15:51:55 -0700971 return setBuffersStickyTransform(transform);
972}
973
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800974int Surface::dispatchSetBuffersTimestamp(va_list args) {
975 int64_t timestamp = va_arg(args, int64_t);
976 return setBuffersTimestamp(timestamp);
977}
978
979int Surface::dispatchLock(va_list args) {
980 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
981 ARect* inOutDirtyBounds = va_arg(args, ARect*);
982 return lock(outBuffer, inOutDirtyBounds);
983}
984
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800985int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800986 return unlockAndPost();
987}
988
Rachad7cb0d392014-07-29 17:53:53 -0700989int Surface::dispatchSetSidebandStream(va_list args) {
990 native_handle_t* sH = va_arg(args, native_handle_t*);
991 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
992 setSidebandStream(sidebandHandle);
993 return OK;
994}
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800995
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800996int Surface::dispatchSetBuffersDataSpace(va_list args) {
997 android_dataspace dataspace =
998 static_cast<android_dataspace>(va_arg(args, int));
999 return setBuffersDataSpace(dataspace);
1000}
1001
Dan Stoza5065a552015-03-17 16:23:42 -07001002int Surface::dispatchSetSurfaceDamage(va_list args) {
1003 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1004 size_t numRects = va_arg(args, size_t);
1005 setSurfaceDamage(rects, numRects);
1006 return NO_ERROR;
1007}
1008
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001009int Surface::dispatchSetSharedBufferMode(va_list args) {
1010 bool sharedBufferMode = va_arg(args, int);
1011 return setSharedBufferMode(sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001012}
1013
1014int Surface::dispatchSetAutoRefresh(va_list args) {
1015 bool autoRefresh = va_arg(args, int);
1016 return setAutoRefresh(autoRefresh);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001017}
1018
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001019int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1020 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1021 return getDisplayRefreshCycleDuration(outRefreshDuration);
1022}
1023
Brian Anderson1049d1d2016-12-16 17:25:57 -08001024int Surface::dispatchGetNextFrameId(va_list args) {
1025 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1026 *nextFrameId = getNextFrameNumber();
1027 return NO_ERROR;
1028}
1029
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001030int Surface::dispatchEnableFrameTimestamps(va_list args) {
1031 bool enable = va_arg(args, int);
1032 enableFrameTimestamps(enable);
1033 return NO_ERROR;
1034}
1035
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001036int Surface::dispatchGetCompositorTiming(va_list args) {
1037 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1038 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1039 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1040 return getCompositorTiming(compositeDeadline, compositeInterval,
1041 compositeToPresentLatency);
1042}
1043
Pablo Ceballosce796e72016-02-04 19:10:51 -08001044int Surface::dispatchGetFrameTimestamps(va_list args) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001045 uint64_t frameId = va_arg(args, uint64_t);
Brian Andersondbd0ea82016-07-22 09:38:59 -07001046 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001047 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001048 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1049 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1050 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
Brian Andersonb04c6f02016-10-21 12:57:46 -07001051 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
Brian Anderson069b3652016-07-22 10:32:47 -07001052 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001053 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001054 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001055 return getFrameTimestamps(frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07001056 outRequestedPresentTime, outAcquireTime, outLatchTime,
1057 outFirstRefreshStartTime, outLastRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07001058 outGpuCompositionDoneTime, outDisplayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07001059 outDequeueReadyTime, outReleaseTime);
Pablo Ceballosce796e72016-02-04 19:10:51 -08001060}
1061
Courtney Goeltzenleuchter1eb1b272017-02-02 16:51:06 -07001062int Surface::dispatchGetWideColorSupport(va_list args) {
1063 bool* outSupport = va_arg(args, bool*);
1064 return getWideColorSupport(outSupport);
1065}
1066
Courtney Goeltzenleuchterc5b97c52017-02-26 14:47:13 -07001067int Surface::dispatchGetHdrSupport(va_list args) {
1068 bool* outSupport = va_arg(args, bool*);
1069 return getHdrSupport(outSupport);
1070}
1071
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001072int Surface::connect(int api) {
Dan Stoza966b98b2015-03-02 22:12:37 -08001073 static sp<IProducerListener> listener = new DummyProducerListener();
1074 return connect(api, listener);
1075}
1076
1077int Surface::connect(int api, const sp<IProducerListener>& listener) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001078 ATRACE_CALL();
1079 ALOGV("Surface::connect");
1080 Mutex::Autolock lock(mMutex);
1081 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -07001082 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001083 if (err == NO_ERROR) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001084 mDefaultWidth = output.width;
1085 mDefaultHeight = output.height;
1086 mNextFrameNumber = output.nextFrameNumber;
Ruben Brunk1681d952014-06-27 15:51:55 -07001087
1088 // Disable transform hint if sticky transform is set.
1089 if (mStickyTransform == 0) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001090 mTransformHint = output.transformHint;
Ruben Brunk1681d952014-06-27 15:51:55 -07001091 }
1092
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07001093 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001094 }
1095 if (!err && api == NATIVE_WINDOW_API_CPU) {
1096 mConnectedToCpu = true;
Dan Stoza5065a552015-03-17 16:23:42 -07001097 // Clear the dirty region in case we're switching from a non-CPU API
1098 mDirtyRegion.clear();
1099 } else if (!err) {
1100 // Initialize the dirty region for tracking surface damage
1101 mDirtyRegion = Region::INVALID_REGION;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001102 }
Dan Stoza5065a552015-03-17 16:23:42 -07001103
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001104 return err;
1105}
1106
Mathias Agopian365857d2013-09-11 19:35:45 -07001107
Robert Carr97b9c862016-09-08 13:54:35 -07001108int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001109 ATRACE_CALL();
1110 ALOGV("Surface::disconnect");
1111 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001112 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1113 mSharedBufferHasBeenQueued = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001114 freeAllBuffers();
Robert Carr97b9c862016-09-08 13:54:35 -07001115 int err = mGraphicBufferProducer->disconnect(api, mode);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001116 if (!err) {
1117 mReqFormat = 0;
1118 mReqWidth = 0;
1119 mReqHeight = 0;
1120 mReqUsage = 0;
1121 mCrop.clear();
1122 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1123 mTransform = 0;
Ruben Brunk1681d952014-06-27 15:51:55 -07001124 mStickyTransform = 0;
1125
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001126 if (api == NATIVE_WINDOW_API_CPU) {
1127 mConnectedToCpu = false;
1128 }
1129 }
1130 return err;
1131}
1132
Dan Stozad9c49712015-04-27 11:06:01 -07001133int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
Dan Stoza231832e2015-03-11 11:55:01 -07001134 sp<Fence>* outFence) {
1135 ATRACE_CALL();
1136 ALOGV("Surface::detachNextBuffer");
1137
1138 if (outBuffer == NULL || outFence == NULL) {
1139 return BAD_VALUE;
1140 }
1141
1142 Mutex::Autolock lock(mMutex);
1143
1144 sp<GraphicBuffer> buffer(NULL);
1145 sp<Fence> fence(NULL);
1146 status_t result = mGraphicBufferProducer->detachNextBuffer(
1147 &buffer, &fence);
1148 if (result != NO_ERROR) {
1149 return result;
1150 }
1151
Dan Stozad9c49712015-04-27 11:06:01 -07001152 *outBuffer = buffer;
Dan Stoza231832e2015-03-11 11:55:01 -07001153 if (fence != NULL && fence->isValid()) {
1154 *outFence = fence;
1155 } else {
1156 *outFence = Fence::NO_FENCE;
1157 }
1158
Pablo Ceballos23b4abe2016-01-08 12:15:22 -08001159 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1160 if (mSlots[i].buffer != NULL &&
1161 mSlots[i].buffer->handle == buffer->handle) {
1162 mSlots[i].buffer = NULL;
1163 }
1164 }
1165
Dan Stoza231832e2015-03-11 11:55:01 -07001166 return NO_ERROR;
1167}
1168
1169int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1170{
1171 ATRACE_CALL();
1172 ALOGV("Surface::attachBuffer");
1173
1174 Mutex::Autolock lock(mMutex);
1175
1176 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
Dan Stoza812ed062015-06-02 15:45:22 -07001177 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1178 graphicBuffer->mGenerationNumber = mGenerationNumber;
Dan Stoza231832e2015-03-11 11:55:01 -07001179 int32_t attachedSlot = -1;
1180 status_t result = mGraphicBufferProducer->attachBuffer(
1181 &attachedSlot, graphicBuffer);
1182 if (result != NO_ERROR) {
1183 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
Dan Stoza812ed062015-06-02 15:45:22 -07001184 graphicBuffer->mGenerationNumber = priorGeneration;
Dan Stoza231832e2015-03-11 11:55:01 -07001185 return result;
1186 }
1187 mSlots[attachedSlot].buffer = graphicBuffer;
1188
1189 return NO_ERROR;
1190}
1191
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001192int Surface::setUsage(uint32_t reqUsage)
1193{
1194 ALOGV("Surface::setUsage");
1195 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001196 if (reqUsage != mReqUsage) {
1197 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1198 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001199 mReqUsage = reqUsage;
1200 return OK;
1201}
1202
1203int Surface::setCrop(Rect const* rect)
1204{
1205 ATRACE_CALL();
1206
Pablo Ceballos60d69222015-08-07 14:47:20 -07001207 Rect realRect(Rect::EMPTY_RECT);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001208 if (rect == NULL || rect->isEmpty()) {
1209 realRect.clear();
1210 } else {
1211 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -08001212 }
1213
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001214 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1215 realRect.left, realRect.top, realRect.right, realRect.bottom);
1216
1217 Mutex::Autolock lock(mMutex);
1218 mCrop = realRect;
1219 return NO_ERROR;
1220}
1221
1222int Surface::setBufferCount(int bufferCount)
1223{
1224 ATRACE_CALL();
1225 ALOGV("Surface::setBufferCount");
1226 Mutex::Autolock lock(mMutex);
1227
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001228 status_t err = NO_ERROR;
1229 if (bufferCount == 0) {
1230 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1231 } else {
1232 int minUndequeuedBuffers = 0;
1233 err = mGraphicBufferProducer->query(
1234 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1235 if (err == NO_ERROR) {
1236 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1237 bufferCount - minUndequeuedBuffers);
1238 }
1239 }
Mathias Agopian90147262010-01-22 11:47:55 -08001240
Pablo Ceballose5b755a2015-08-13 16:18:19 -07001241 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1242 bufferCount, strerror(-err));
1243
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001244 return err;
1245}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001246
Pablo Ceballosfa455352015-08-12 17:47:47 -07001247int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1248 ATRACE_CALL();
1249 ALOGV("Surface::setMaxDequeuedBufferCount");
1250 Mutex::Autolock lock(mMutex);
1251
1252 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1253 maxDequeuedBuffers);
1254 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1255 "returned %s", maxDequeuedBuffers, strerror(-err));
1256
Pablo Ceballosfa455352015-08-12 17:47:47 -07001257 return err;
1258}
1259
1260int Surface::setAsyncMode(bool async) {
1261 ATRACE_CALL();
1262 ALOGV("Surface::setAsyncMode");
1263 Mutex::Autolock lock(mMutex);
1264
1265 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1266 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1267 async, strerror(-err));
1268
Pablo Ceballosfa455352015-08-12 17:47:47 -07001269 return err;
1270}
1271
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001272int Surface::setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001273 ATRACE_CALL();
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001274 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001275 Mutex::Autolock lock(mMutex);
1276
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001277 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1278 sharedBufferMode);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001279 if (err == NO_ERROR) {
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001280 mSharedBufferMode = sharedBufferMode;
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001281 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -07001282 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1283 "%s", sharedBufferMode, strerror(-err));
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001284
1285 return err;
1286}
1287
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001288int Surface::setAutoRefresh(bool autoRefresh) {
1289 ATRACE_CALL();
1290 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1291 Mutex::Autolock lock(mMutex);
1292
1293 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1294 if (err == NO_ERROR) {
1295 mAutoRefresh = autoRefresh;
1296 }
1297 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1298 autoRefresh, strerror(-err));
1299 return err;
1300}
1301
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001302int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001303{
1304 ATRACE_CALL();
1305 ALOGV("Surface::setBuffersDimensions");
1306
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001307 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001308 return BAD_VALUE;
1309
1310 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001311 if (width != mReqWidth || height != mReqHeight) {
1312 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1313 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001314 mReqWidth = width;
1315 mReqHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001316 return NO_ERROR;
1317}
1318
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001319int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001320{
1321 ATRACE_CALL();
1322 ALOGV("Surface::setBuffersUserDimensions");
1323
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001324 if ((width && !height) || (!width && height))
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001325 return BAD_VALUE;
1326
1327 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001328 if (width != mUserWidth || height != mUserHeight) {
1329 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1330 }
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001331 mUserWidth = width;
1332 mUserHeight = height;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001333 return NO_ERROR;
1334}
1335
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001336int Surface::setBuffersFormat(PixelFormat format)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001337{
1338 ALOGV("Surface::setBuffersFormat");
1339
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001340 Mutex::Autolock lock(mMutex);
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001341 if (format != mReqFormat) {
1342 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1343 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001344 mReqFormat = format;
1345 return NO_ERROR;
1346}
1347
1348int Surface::setScalingMode(int mode)
1349{
1350 ATRACE_CALL();
1351 ALOGV("Surface::setScalingMode(%d)", mode);
1352
1353 switch (mode) {
1354 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1355 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1356 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Robert Carrc2e77882015-12-16 18:14:03 -08001357 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001358 break;
1359 default:
1360 ALOGE("unknown scaling mode: %d", mode);
1361 return BAD_VALUE;
1362 }
1363
1364 Mutex::Autolock lock(mMutex);
1365 mScalingMode = mode;
1366 return NO_ERROR;
1367}
1368
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001369int Surface::setBuffersTransform(uint32_t transform)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001370{
1371 ATRACE_CALL();
1372 ALOGV("Surface::setBuffersTransform");
1373 Mutex::Autolock lock(mMutex);
1374 mTransform = transform;
1375 return NO_ERROR;
1376}
1377
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001378int Surface::setBuffersStickyTransform(uint32_t transform)
Ruben Brunk1681d952014-06-27 15:51:55 -07001379{
1380 ATRACE_CALL();
1381 ALOGV("Surface::setBuffersStickyTransform");
1382 Mutex::Autolock lock(mMutex);
1383 mStickyTransform = transform;
1384 return NO_ERROR;
1385}
1386
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001387int Surface::setBuffersTimestamp(int64_t timestamp)
1388{
1389 ALOGV("Surface::setBuffersTimestamp");
1390 Mutex::Autolock lock(mMutex);
1391 mTimestamp = timestamp;
1392 return NO_ERROR;
1393}
1394
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -08001395int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1396{
1397 ALOGV("Surface::setBuffersDataSpace");
1398 Mutex::Autolock lock(mMutex);
1399 mDataSpace = dataSpace;
1400 return NO_ERROR;
1401}
1402
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001403void Surface::freeAllBuffers() {
1404 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1405 mSlots[i].buffer = 0;
1406 }
1407}
1408
Dan Stoza5065a552015-03-17 16:23:42 -07001409void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1410 ATRACE_CALL();
1411 ALOGV("Surface::setSurfaceDamage");
1412 Mutex::Autolock lock(mMutex);
1413
Dan Stozac62acbd2015-04-21 16:42:49 -07001414 if (mConnectedToCpu || numRects == 0) {
Dan Stoza5065a552015-03-17 16:23:42 -07001415 mDirtyRegion = Region::INVALID_REGION;
1416 return;
1417 }
1418
1419 mDirtyRegion.clear();
1420 for (size_t r = 0; r < numRects; ++r) {
1421 // We intentionally flip top and bottom here, since because they're
1422 // specified with a bottom-left origin, top > bottom, which fails
1423 // validation in the Region class. We will fix this up when we flip to a
1424 // top-left origin in queueBuffer.
1425 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1426 mDirtyRegion.orSelf(rect);
1427 }
1428}
1429
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001430// ----------------------------------------------------------------------
1431// the lock/unlock APIs must be used from the same thread
1432
1433static status_t copyBlt(
1434 const sp<GraphicBuffer>& dst,
1435 const sp<GraphicBuffer>& src,
Francis Hart7b09e792015-01-09 11:10:54 +02001436 const Region& reg,
1437 int *dstFenceFd)
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001438{
1439 // src and dst with, height and format must be identical. no verification
1440 // is done here.
1441 status_t err;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001442 uint8_t* src_bits = NULL;
1443 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1444 reinterpret_cast<void**>(&src_bits));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001445 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1446
1447 uint8_t* dst_bits = NULL;
Francis Hart7b09e792015-01-09 11:10:54 +02001448 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1449 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001450 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Francis Hart7b09e792015-01-09 11:10:54 +02001451 *dstFenceFd = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001452
1453 Region::const_iterator head(reg.begin());
1454 Region::const_iterator tail(reg.end());
1455 if (head != tail && src_bits && dst_bits) {
1456 const size_t bpp = bytesPerPixel(src->format);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001457 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1458 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001459
1460 while (head != tail) {
1461 const Rect& r(*head++);
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001462 int32_t h = r.height();
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001463 if (h <= 0) continue;
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001464 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1465 uint8_t const * s = src_bits +
1466 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1467 uint8_t * d = dst_bits +
1468 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001469 if (dbpr==sbpr && size==sbpr) {
Dan Stoza3be1c6b2014-11-18 10:24:03 -08001470 size *= static_cast<size_t>(h);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001471 h = 1;
1472 }
1473 do {
1474 memcpy(d, s, size);
1475 d += dbpr;
1476 s += sbpr;
1477 } while (--h > 0);
1478 }
1479 }
1480
1481 if (src_bits)
1482 src->unlock();
1483
1484 if (dst_bits)
Francis Hart7b09e792015-01-09 11:10:54 +02001485 dst->unlockAsync(dstFenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001486
1487 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488}
1489
Mathias Agopiana138f892010-05-21 17:24:35 -07001490// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001491
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001492status_t Surface::lock(
1493 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1494{
1495 if (mLockedBuffer != 0) {
1496 ALOGE("Surface::lock failed, already locked");
1497 return INVALID_OPERATION;
1498 }
1499
1500 if (!mConnectedToCpu) {
1501 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1502 if (err) {
1503 return err;
1504 }
1505 // we're intending to do software rendering from this point
1506 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1507 }
1508
1509 ANativeWindowBuffer* out;
1510 int fenceFd = -1;
1511 status_t err = dequeueBuffer(&out, &fenceFd);
1512 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1513 if (err == NO_ERROR) {
1514 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001515 const Rect bounds(backBuffer->width, backBuffer->height);
1516
1517 Region newDirtyRegion;
1518 if (inOutDirtyBounds) {
1519 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1520 newDirtyRegion.andSelf(bounds);
1521 } else {
1522 newDirtyRegion.set(bounds);
1523 }
1524
1525 // figure out if we can copy the frontbuffer back
1526 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1527 const bool canCopyBack = (frontBuffer != 0 &&
1528 backBuffer->width == frontBuffer->width &&
1529 backBuffer->height == frontBuffer->height &&
1530 backBuffer->format == frontBuffer->format);
1531
1532 if (canCopyBack) {
1533 // copy the area that is invalid and not repainted this round
1534 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Francis Hartdc10f842014-12-01 16:04:49 +02001535 if (!copyback.isEmpty()) {
Francis Hart7b09e792015-01-09 11:10:54 +02001536 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
Francis Hartdc10f842014-12-01 16:04:49 +02001537 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001538 } else {
1539 // if we can't copy-back anything, modify the user's dirty
1540 // region to make sure they redraw the whole buffer
1541 newDirtyRegion.set(bounds);
1542 mDirtyRegion.clear();
1543 Mutex::Autolock lock(mMutex);
1544 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1545 mSlots[i].dirtyRegion.clear();
1546 }
1547 }
1548
1549
1550 { // scope for the lock
1551 Mutex::Autolock lock(mMutex);
1552 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1553 if (backBufferSlot >= 0) {
1554 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1555 mDirtyRegion.subtract(dirtyRegion);
1556 dirtyRegion = newDirtyRegion;
1557 }
1558 }
1559
1560 mDirtyRegion.orSelf(newDirtyRegion);
1561 if (inOutDirtyBounds) {
1562 *inOutDirtyBounds = newDirtyRegion.getBounds();
1563 }
1564
1565 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +03001566 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001567 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +03001568 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001569
1570 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1571 backBuffer->handle);
1572
1573 if (res != 0) {
1574 err = INVALID_OPERATION;
1575 } else {
1576 mLockedBuffer = backBuffer;
1577 outBuffer->width = backBuffer->width;
1578 outBuffer->height = backBuffer->height;
1579 outBuffer->stride = backBuffer->stride;
1580 outBuffer->format = backBuffer->format;
1581 outBuffer->bits = vaddr;
1582 }
1583 }
1584 return err;
1585}
1586
1587status_t Surface::unlockAndPost()
1588{
1589 if (mLockedBuffer == 0) {
1590 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1591 return INVALID_OPERATION;
1592 }
1593
Francis Hart8f396012014-04-01 15:30:53 +03001594 int fd = -1;
1595 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001596 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1597
Francis Hart8f396012014-04-01 15:30:53 +03001598 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001599 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1600 mLockedBuffer->handle, strerror(-err));
1601
1602 mPostedBuffer = mLockedBuffer;
1603 mLockedBuffer = 0;
1604 return err;
1605}
1606
Robert Carr9f31e292016-04-11 11:15:32 -07001607bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1608 Mutex::Autolock lock(mMutex);
Pablo Ceballosbc8c1922016-07-01 14:15:41 -07001609 if (mNextFrameNumber > lastFrame) {
Robert Carr9f31e292016-04-11 11:15:32 -07001610 return true;
1611 }
1612 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1613}
1614
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -07001615status_t Surface::getUniqueId(uint64_t* outId) const {
1616 Mutex::Autolock lock(mMutex);
1617 return mGraphicBufferProducer->getUniqueId(outId);
1618}
1619
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620}; // namespace android