The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | * |
| 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 Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | //#define LOG_NDEBUG 0 |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 21 | #include <gui/Surface.h> |
Mathias Agopian | b0e76f4 | 2012-03-23 14:15:44 -0700 | [diff] [blame] | 22 | |
Yiwei Zhang | 9e919eb | 2019-03-07 23:21:40 -0800 | [diff] [blame] | 23 | #include <condition_variable> |
| 24 | #include <deque> |
| 25 | #include <mutex> |
| 26 | #include <thread> |
| 27 | |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 28 | #include <inttypes.h> |
| 29 | |
Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 30 | #include <android/native_window.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 31 | |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 32 | #include <utils/Log.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 34 | #include <utils/NativeHandle.h> |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 35 | |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 36 | #include <ui/DisplayStatInfo.h> |
Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 37 | #include <ui/Fence.h> |
Shuzhen Wang | 067fcd3 | 2019-08-14 10:41:12 -0700 | [diff] [blame] | 38 | #include <ui/GraphicBuffer.h> |
Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 39 | #include <ui/HdrCapabilities.h> |
| 40 | #include <ui/Region.h> |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 41 | |
Mathias Agopian | 2b5dd40 | 2017-02-07 17:36:19 -0800 | [diff] [blame] | 42 | #include <gui/BufferItem.h> |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 43 | #include <gui/IProducerListener.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 44 | |
Mathias Agopian | 2b5dd40 | 2017-02-07 17:36:19 -0800 | [diff] [blame] | 45 | #include <gui/ISurfaceComposer.h> |
Steven Thomas | 62a4cf8 | 2020-01-31 12:04:03 -0800 | [diff] [blame] | 46 | #include <gui/LayerState.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 47 | #include <private/gui/ComposerService.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 48 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | namespace android { |
| 50 | |
Peiyong Lin | fd997e0 | 2018-03-28 15:29:00 -0700 | [diff] [blame] | 51 | using ui::ColorMode; |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 52 | using ui::Dataspace; |
Peiyong Lin | fd997e0 | 2018-03-28 15:29:00 -0700 | [diff] [blame] | 53 | |
Alec Mouri | 09d122a | 2019-11-25 10:00:53 -0800 | [diff] [blame] | 54 | namespace { |
| 55 | |
| 56 | bool isInterceptorRegistrationOp(int op) { |
| 57 | return op == NATIVE_WINDOW_SET_CANCEL_INTERCEPTOR || |
| 58 | op == NATIVE_WINDOW_SET_DEQUEUE_INTERCEPTOR || |
| 59 | op == NATIVE_WINDOW_SET_PERFORM_INTERCEPTOR || |
John Reck | 401cda6 | 2020-05-07 16:04:41 -0700 | [diff] [blame] | 60 | op == NATIVE_WINDOW_SET_QUEUE_INTERCEPTOR || |
| 61 | op == NATIVE_WINDOW_SET_QUERY_INTERCEPTOR; |
Alec Mouri | 09d122a | 2019-11-25 10:00:53 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | } // namespace |
| 65 | |
Vishnu Nair | 992496b | 2020-10-22 17:27:21 -0700 | [diff] [blame] | 66 | Surface::Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp, |
| 67 | const sp<IBinder>& surfaceControlHandle) |
Ian Elliott | a2eb34c | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 68 | : mGraphicBufferProducer(bufferProducer), |
| 69 | mCrop(Rect::EMPTY_RECT), |
| 70 | mBufferAge(0), |
| 71 | mGenerationNumber(0), |
| 72 | mSharedBufferMode(false), |
| 73 | mAutoRefresh(false), |
| 74 | mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT), |
| 75 | mSharedBufferHasBeenQueued(false), |
| 76 | mQueriedSupportedTimestamps(false), |
| 77 | mFrameTimestampsSupportsPresent(false), |
| 78 | mEnableFrameTimestamps(false), |
| 79 | mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>()) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 80 | // Initialize the ANativeWindow function pointers. |
| 81 | ANativeWindow::setSwapInterval = hook_setSwapInterval; |
| 82 | ANativeWindow::dequeueBuffer = hook_dequeueBuffer; |
| 83 | ANativeWindow::cancelBuffer = hook_cancelBuffer; |
| 84 | ANativeWindow::queueBuffer = hook_queueBuffer; |
| 85 | ANativeWindow::query = hook_query; |
| 86 | ANativeWindow::perform = hook_perform; |
| 87 | |
| 88 | ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED; |
| 89 | ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED; |
| 90 | ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED; |
| 91 | ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED; |
| 92 | |
| 93 | const_cast<int&>(ANativeWindow::minSwapInterval) = 0; |
| 94 | const_cast<int&>(ANativeWindow::maxSwapInterval) = 1; |
| 95 | |
| 96 | mReqWidth = 0; |
| 97 | mReqHeight = 0; |
| 98 | mReqFormat = 0; |
| 99 | mReqUsage = 0; |
| 100 | mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO; |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 101 | mDataSpace = Dataspace::UNKNOWN; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 102 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 103 | mTransform = 0; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 104 | mStickyTransform = 0; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 105 | mDefaultWidth = 0; |
| 106 | mDefaultHeight = 0; |
| 107 | mUserWidth = 0; |
| 108 | mUserHeight = 0; |
| 109 | mTransformHint = 0; |
| 110 | mConsumerRunningBehind = false; |
| 111 | mConnectedToCpu = false; |
Eino-Ville Talvala | 7895e90 | 2013-08-21 11:53:37 -0700 | [diff] [blame] | 112 | mProducerControlledByApp = controlledByApp; |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 113 | mSwapIntervalZero = false; |
Yiwei Zhang | 74c9cc3 | 2020-06-20 03:35:17 -0700 | [diff] [blame] | 114 | mMaxBufferCount = NUM_BUFFER_SLOTS; |
Vishnu Nair | 992496b | 2020-10-22 17:27:21 -0700 | [diff] [blame] | 115 | mSurfaceControlHandle = surfaceControlHandle; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Mathias Agopian | 35ffa6a | 2013-03-12 18:45:09 -0700 | [diff] [blame] | 118 | Surface::~Surface() { |
| 119 | if (mConnectedToCpu) { |
| 120 | Surface::disconnect(NATIVE_WINDOW_API_CPU); |
| 121 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 124 | sp<ISurfaceComposer> Surface::composerService() const { |
| 125 | return ComposerService::getComposerService(); |
| 126 | } |
| 127 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 128 | nsecs_t Surface::now() const { |
| 129 | return systemTime(); |
| 130 | } |
| 131 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 132 | sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const { |
| 133 | return mGraphicBufferProducer; |
| 134 | } |
| 135 | |
Wonsik Kim | 0ee14ca | 2014-03-17 17:46:53 +0900 | [diff] [blame] | 136 | void Surface::setSidebandStream(const sp<NativeHandle>& stream) { |
| 137 | mGraphicBufferProducer->setSidebandStream(stream); |
| 138 | } |
| 139 | |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 140 | void Surface::allocateBuffers() { |
| 141 | uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth; |
| 142 | uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight; |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 143 | mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight, |
| 144 | mReqFormat, mReqUsage); |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 147 | status_t Surface::setGenerationNumber(uint32_t generation) { |
| 148 | status_t result = mGraphicBufferProducer->setGenerationNumber(generation); |
| 149 | if (result == NO_ERROR) { |
| 150 | mGenerationNumber = generation; |
| 151 | } |
| 152 | return result; |
| 153 | } |
| 154 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 155 | uint64_t Surface::getNextFrameNumber() const { |
Pablo Ceballos | bc8c192 | 2016-07-01 14:15:41 -0700 | [diff] [blame] | 156 | Mutex::Autolock lock(mMutex); |
| 157 | return mNextFrameNumber; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Dan Stoza | c6f30bd | 2015-06-08 09:32:50 -0700 | [diff] [blame] | 160 | String8 Surface::getConsumerName() const { |
| 161 | return mGraphicBufferProducer->getConsumerName(); |
| 162 | } |
| 163 | |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 164 | status_t Surface::setDequeueTimeout(nsecs_t timeout) { |
| 165 | return mGraphicBufferProducer->setDequeueTimeout(timeout); |
| 166 | } |
| 167 | |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 168 | status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, |
John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 169 | sp<Fence>* outFence, float outTransformMatrix[16]) { |
| 170 | return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence, |
| 171 | outTransformMatrix); |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 174 | status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) { |
| 175 | ATRACE_CALL(); |
| 176 | |
| 177 | DisplayStatInfo stats; |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 178 | status_t result = composerService()->getDisplayStats(nullptr, &stats); |
Chih-Hung Hsieh | d66be0a | 2017-10-05 13:51:32 -0700 | [diff] [blame] | 179 | if (result != NO_ERROR) { |
| 180 | return result; |
| 181 | } |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 182 | |
| 183 | *outRefreshDuration = stats.vsyncPeriod; |
| 184 | |
| 185 | return NO_ERROR; |
| 186 | } |
| 187 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 188 | void Surface::enableFrameTimestamps(bool enable) { |
| 189 | Mutex::Autolock lock(mMutex); |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 190 | // If going from disabled to enabled, get the initial values for |
| 191 | // compositor and display timing. |
| 192 | if (!mEnableFrameTimestamps && enable) { |
| 193 | FrameEventHistoryDelta delta; |
| 194 | mGraphicBufferProducer->getFrameTimestamps(&delta); |
| 195 | mFrameEventHistory->applyDelta(delta); |
| 196 | } |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 197 | mEnableFrameTimestamps = enable; |
| 198 | } |
| 199 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 200 | status_t Surface::getCompositorTiming( |
| 201 | nsecs_t* compositeDeadline, nsecs_t* compositeInterval, |
| 202 | nsecs_t* compositeToPresentLatency) { |
| 203 | Mutex::Autolock lock(mMutex); |
| 204 | if (!mEnableFrameTimestamps) { |
| 205 | return INVALID_OPERATION; |
| 206 | } |
| 207 | |
| 208 | if (compositeDeadline != nullptr) { |
| 209 | *compositeDeadline = |
| 210 | mFrameEventHistory->getNextCompositeDeadline(now()); |
| 211 | } |
| 212 | if (compositeInterval != nullptr) { |
| 213 | *compositeInterval = mFrameEventHistory->getCompositeInterval(); |
| 214 | } |
| 215 | if (compositeToPresentLatency != nullptr) { |
| 216 | *compositeToPresentLatency = |
| 217 | mFrameEventHistory->getCompositeToPresentLatency(); |
| 218 | } |
| 219 | return NO_ERROR; |
| 220 | } |
| 221 | |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 222 | static bool checkConsumerForUpdates( |
| 223 | const FrameEvents* e, const uint64_t lastFrameNumber, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 224 | const nsecs_t* outLatchTime, |
| 225 | const nsecs_t* outFirstRefreshStartTime, |
| 226 | const nsecs_t* outLastRefreshStartTime, |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 227 | const nsecs_t* outGpuCompositionDoneTime, |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 228 | const nsecs_t* outDisplayPresentTime, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 229 | const nsecs_t* outDequeueReadyTime, |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 230 | const nsecs_t* outReleaseTime) { |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 231 | bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo(); |
| 232 | bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) && |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 233 | !e->hasFirstRefreshStartInfo(); |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 234 | bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) && |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 235 | !e->hasGpuCompositionDoneInfo(); |
| 236 | bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) && |
| 237 | !e->hasDisplayPresentInfo(); |
| 238 | |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 239 | // LastRefreshStart, DequeueReady, and Release are never available for the |
| 240 | // last frame. |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 241 | bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) && |
| 242 | !e->hasLastRefreshStartInfo() && |
| 243 | (e->frameNumber != lastFrameNumber); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 244 | bool checkForDequeueReady = (outDequeueReadyTime != nullptr) && |
| 245 | !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber); |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 246 | bool checkForRelease = (outReleaseTime != nullptr) && |
| 247 | !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber); |
| 248 | |
| 249 | // RequestedPresent and Acquire info are always available producer-side. |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 250 | return checkForLatch || checkForFirstRefreshStart || |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 251 | checkForLastRefreshStart || checkForGpuCompositionDone || |
Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 252 | checkForDisplayPresent || checkForDequeueReady || checkForRelease; |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 255 | static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) { |
| 256 | if (dst != nullptr) { |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 257 | // We always get valid timestamps for these eventually. |
| 258 | *dst = (src == FrameEvents::TIMESTAMP_PENDING) ? |
| 259 | NATIVE_WINDOW_TIMESTAMP_PENDING : src; |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 263 | static void getFrameTimestampFence(nsecs_t *dst, |
| 264 | const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) { |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 265 | if (dst != nullptr) { |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 266 | if (!fenceShouldBeKnown) { |
| 267 | *dst = NATIVE_WINDOW_TIMESTAMP_PENDING; |
| 268 | return; |
| 269 | } |
| 270 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 271 | nsecs_t signalTime = src->getSignalTime(); |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 272 | *dst = (signalTime == Fence::SIGNAL_TIME_PENDING) ? |
| 273 | NATIVE_WINDOW_TIMESTAMP_PENDING : |
| 274 | (signalTime == Fence::SIGNAL_TIME_INVALID) ? |
| 275 | NATIVE_WINDOW_TIMESTAMP_INVALID : |
| 276 | signalTime; |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 280 | status_t Surface::getFrameTimestamps(uint64_t frameNumber, |
Brian Anderson | dbd0ea8 | 2016-07-22 09:38:59 -0700 | [diff] [blame] | 281 | nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 282 | nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime, |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 283 | nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime, |
Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 284 | nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime, |
| 285 | nsecs_t* outReleaseTime) { |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 286 | ATRACE_CALL(); |
| 287 | |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 288 | Mutex::Autolock lock(mMutex); |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 289 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 290 | if (!mEnableFrameTimestamps) { |
| 291 | return INVALID_OPERATION; |
| 292 | } |
| 293 | |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 294 | // Verify the requested timestamps are supported. |
| 295 | querySupportedTimestampsLocked(); |
| 296 | if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) { |
| 297 | return BAD_VALUE; |
| 298 | } |
| 299 | |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 300 | FrameEvents* events = mFrameEventHistory->getFrame(frameNumber); |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 301 | if (events == nullptr) { |
| 302 | // If the entry isn't available in the producer, it's definitely not |
| 303 | // available in the consumer. |
| 304 | return NAME_NOT_FOUND; |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 307 | // Update our cache of events if the requested events are not available. |
| 308 | if (checkConsumerForUpdates(events, mLastFrameNumber, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 309 | outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime, |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 310 | outGpuCompositionDoneTime, outDisplayPresentTime, |
Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 311 | outDequeueReadyTime, outReleaseTime)) { |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 312 | FrameEventHistoryDelta delta; |
| 313 | mGraphicBufferProducer->getFrameTimestamps(&delta); |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 314 | mFrameEventHistory->applyDelta(delta); |
| 315 | events = mFrameEventHistory->getFrame(frameNumber); |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 318 | if (events == nullptr) { |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 319 | // The entry was available before the update, but was overwritten |
| 320 | // after the update. Make sure not to send the wrong frame's data. |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 321 | return NAME_NOT_FOUND; |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 322 | } |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 323 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 324 | getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 325 | getFrameTimestamp(outLatchTime, events->latchTime); |
| 326 | getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime); |
| 327 | getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime); |
| 328 | getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime); |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 329 | |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 330 | getFrameTimestampFence(outAcquireTime, events->acquireFence, |
| 331 | events->hasAcquireInfo()); |
| 332 | getFrameTimestampFence(outGpuCompositionDoneTime, |
| 333 | events->gpuCompositionDoneFence, |
| 334 | events->hasGpuCompositionDoneInfo()); |
| 335 | getFrameTimestampFence(outDisplayPresentTime, events->displayPresentFence, |
| 336 | events->hasDisplayPresentInfo()); |
| 337 | getFrameTimestampFence(outReleaseTime, events->releaseFence, |
| 338 | events->hasReleaseInfo()); |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 339 | |
| 340 | return NO_ERROR; |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 343 | status_t Surface::getWideColorSupport(bool* supported) { |
| 344 | ATRACE_CALL(); |
| 345 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 346 | const sp<IBinder> display = composerService()->getInternalDisplayToken(); |
| 347 | if (display == nullptr) { |
| 348 | return NAME_NOT_FOUND; |
| 349 | } |
| 350 | |
Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 351 | *supported = false; |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 352 | status_t error = composerService()->isWideColorDisplay(display, supported); |
| 353 | return error; |
Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 356 | status_t Surface::getHdrSupport(bool* supported) { |
| 357 | ATRACE_CALL(); |
| 358 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 359 | const sp<IBinder> display = composerService()->getInternalDisplayToken(); |
| 360 | if (display == nullptr) { |
| 361 | return NAME_NOT_FOUND; |
| 362 | } |
| 363 | |
Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 364 | HdrCapabilities hdrCapabilities; |
| 365 | status_t err = |
| 366 | composerService()->getHdrCapabilities(display, &hdrCapabilities); |
| 367 | |
| 368 | if (err) |
| 369 | return err; |
| 370 | |
| 371 | *supported = !hdrCapabilities.getSupportedHdrTypes().empty(); |
| 372 | |
| 373 | return NO_ERROR; |
| 374 | } |
| 375 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 376 | int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) { |
| 377 | Surface* c = getSelf(window); |
| 378 | return c->setSwapInterval(interval); |
| 379 | } |
| 380 | |
| 381 | int Surface::hook_dequeueBuffer(ANativeWindow* window, |
| 382 | ANativeWindowBuffer** buffer, int* fenceFd) { |
| 383 | Surface* c = getSelf(window); |
Alec Mouri | 09d122a | 2019-11-25 10:00:53 -0800 | [diff] [blame] | 384 | { |
| 385 | std::shared_lock<std::shared_mutex> lock(c->mInterceptorMutex); |
| 386 | if (c->mDequeueInterceptor != nullptr) { |
| 387 | auto interceptor = c->mDequeueInterceptor; |
| 388 | auto data = c->mDequeueInterceptorData; |
| 389 | return interceptor(window, Surface::dequeueBufferInternal, data, buffer, fenceFd); |
| 390 | } |
| 391 | } |
| 392 | return c->dequeueBuffer(buffer, fenceFd); |
| 393 | } |
| 394 | |
| 395 | int Surface::dequeueBufferInternal(ANativeWindow* window, ANativeWindowBuffer** buffer, |
| 396 | int* fenceFd) { |
| 397 | Surface* c = getSelf(window); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 398 | return c->dequeueBuffer(buffer, fenceFd); |
| 399 | } |
| 400 | |
| 401 | int Surface::hook_cancelBuffer(ANativeWindow* window, |
| 402 | ANativeWindowBuffer* buffer, int fenceFd) { |
| 403 | Surface* c = getSelf(window); |
Alec Mouri | 09d122a | 2019-11-25 10:00:53 -0800 | [diff] [blame] | 404 | { |
| 405 | std::shared_lock<std::shared_mutex> lock(c->mInterceptorMutex); |
| 406 | if (c->mCancelInterceptor != nullptr) { |
| 407 | auto interceptor = c->mCancelInterceptor; |
| 408 | auto data = c->mCancelInterceptorData; |
| 409 | return interceptor(window, Surface::cancelBufferInternal, data, buffer, fenceFd); |
| 410 | } |
| 411 | } |
| 412 | return c->cancelBuffer(buffer, fenceFd); |
| 413 | } |
| 414 | |
| 415 | int Surface::cancelBufferInternal(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) { |
| 416 | Surface* c = getSelf(window); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 417 | return c->cancelBuffer(buffer, fenceFd); |
| 418 | } |
| 419 | |
| 420 | int Surface::hook_queueBuffer(ANativeWindow* window, |
| 421 | ANativeWindowBuffer* buffer, int fenceFd) { |
| 422 | Surface* c = getSelf(window); |
Alec Mouri | 09d122a | 2019-11-25 10:00:53 -0800 | [diff] [blame] | 423 | { |
| 424 | std::shared_lock<std::shared_mutex> lock(c->mInterceptorMutex); |
| 425 | if (c->mQueueInterceptor != nullptr) { |
| 426 | auto interceptor = c->mQueueInterceptor; |
| 427 | auto data = c->mQueueInterceptorData; |
| 428 | return interceptor(window, Surface::queueBufferInternal, data, buffer, fenceFd); |
| 429 | } |
| 430 | } |
| 431 | return c->queueBuffer(buffer, fenceFd); |
| 432 | } |
| 433 | |
| 434 | int Surface::queueBufferInternal(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) { |
| 435 | Surface* c = getSelf(window); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 436 | return c->queueBuffer(buffer, fenceFd); |
| 437 | } |
| 438 | |
| 439 | int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, |
| 440 | ANativeWindowBuffer** buffer) { |
| 441 | Surface* c = getSelf(window); |
| 442 | ANativeWindowBuffer* buf; |
| 443 | int fenceFd = -1; |
| 444 | int result = c->dequeueBuffer(&buf, &fenceFd); |
Mike Stroyan | 87709c9 | 2016-06-03 12:43:26 -0600 | [diff] [blame] | 445 | if (result != OK) { |
| 446 | return result; |
| 447 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 448 | sp<Fence> fence(new Fence(fenceFd)); |
Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 449 | int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED"); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 450 | if (waitResult != OK) { |
| 451 | ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d", |
| 452 | waitResult); |
| 453 | c->cancelBuffer(buf, -1); |
| 454 | return waitResult; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 455 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 456 | *buffer = buf; |
| 457 | return result; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 460 | int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window, |
| 461 | ANativeWindowBuffer* buffer) { |
| 462 | Surface* c = getSelf(window); |
| 463 | return c->cancelBuffer(buffer, -1); |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 466 | int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window, |
| 467 | ANativeWindowBuffer* buffer) { |
| 468 | Surface* c = getSelf(window); |
| 469 | return c->lockBuffer_DEPRECATED(buffer); |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 472 | int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window, |
| 473 | ANativeWindowBuffer* buffer) { |
| 474 | Surface* c = getSelf(window); |
| 475 | return c->queueBuffer(buffer, -1); |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 476 | } |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 477 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 478 | int Surface::hook_perform(ANativeWindow* window, int operation, ...) { |
| 479 | va_list args; |
| 480 | va_start(args, operation); |
| 481 | Surface* c = getSelf(window); |
Alec Mouri | 09d122a | 2019-11-25 10:00:53 -0800 | [diff] [blame] | 482 | int result; |
| 483 | // Don't acquire shared ownership of the interceptor mutex if we're going to |
| 484 | // do interceptor registration, as otherwise we'll deadlock on acquiring |
| 485 | // exclusive ownership. |
| 486 | if (!isInterceptorRegistrationOp(operation)) { |
| 487 | std::shared_lock<std::shared_mutex> lock(c->mInterceptorMutex); |
| 488 | if (c->mPerformInterceptor != nullptr) { |
| 489 | result = c->mPerformInterceptor(window, Surface::performInternal, |
| 490 | c->mPerformInterceptorData, operation, args); |
| 491 | va_end(args); |
| 492 | return result; |
| 493 | } |
| 494 | } |
| 495 | result = c->perform(operation, args); |
Haixia Shi | d89c2bb | 2015-09-14 11:02:18 -0700 | [diff] [blame] | 496 | va_end(args); |
| 497 | return result; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Alec Mouri | 09d122a | 2019-11-25 10:00:53 -0800 | [diff] [blame] | 500 | int Surface::performInternal(ANativeWindow* window, int operation, va_list args) { |
| 501 | Surface* c = getSelf(window); |
| 502 | return c->perform(operation, args); |
| 503 | } |
| 504 | |
| 505 | int Surface::hook_query(const ANativeWindow* window, int what, int* value) { |
| 506 | const Surface* c = getSelf(window); |
John Reck | 401cda6 | 2020-05-07 16:04:41 -0700 | [diff] [blame] | 507 | { |
| 508 | std::shared_lock<std::shared_mutex> lock(c->mInterceptorMutex); |
| 509 | if (c->mQueryInterceptor != nullptr) { |
| 510 | auto interceptor = c->mQueryInterceptor; |
| 511 | auto data = c->mQueryInterceptorData; |
| 512 | return interceptor(window, Surface::queryInternal, data, what, value); |
| 513 | } |
| 514 | } |
| 515 | return c->query(what, value); |
| 516 | } |
| 517 | |
| 518 | int Surface::queryInternal(const ANativeWindow* window, int what, int* value) { |
| 519 | const Surface* c = getSelf(window); |
Alec Mouri | 09d122a | 2019-11-25 10:00:53 -0800 | [diff] [blame] | 520 | return c->query(what, value); |
| 521 | } |
| 522 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 523 | int Surface::setSwapInterval(int interval) { |
| 524 | ATRACE_CALL(); |
| 525 | // EGL specification states: |
| 526 | // interval is silently clamped to minimum and maximum implementation |
| 527 | // dependent values before being stored. |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 528 | |
| 529 | if (interval < minSwapInterval) |
| 530 | interval = minSwapInterval; |
| 531 | |
| 532 | if (interval > maxSwapInterval) |
| 533 | interval = maxSwapInterval; |
| 534 | |
Jorim Jaggi | 0a3e784 | 2018-07-17 13:48:33 +0200 | [diff] [blame] | 535 | const bool wasSwapIntervalZero = mSwapIntervalZero; |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 536 | mSwapIntervalZero = (interval == 0); |
Jorim Jaggi | 0a3e784 | 2018-07-17 13:48:33 +0200 | [diff] [blame] | 537 | |
| 538 | if (mSwapIntervalZero != wasSwapIntervalZero) { |
| 539 | mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero); |
| 540 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 541 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 542 | return NO_ERROR; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Yiwei Zhang | 9e919eb | 2019-03-07 23:21:40 -0800 | [diff] [blame] | 545 | class FenceMonitor { |
| 546 | public: |
| 547 | explicit FenceMonitor(const char* name) : mName(name), mFencesQueued(0), mFencesSignaled(0) { |
| 548 | std::thread thread(&FenceMonitor::loop, this); |
| 549 | pthread_setname_np(thread.native_handle(), mName); |
| 550 | thread.detach(); |
| 551 | } |
| 552 | |
| 553 | void queueFence(const sp<Fence>& fence) { |
| 554 | char message[64]; |
| 555 | |
| 556 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 2817813 | 2019-03-08 17:14:53 -0800 | [diff] [blame] | 557 | if (fence->getSignalTime() != Fence::SIGNAL_TIME_PENDING) { |
| 558 | snprintf(message, sizeof(message), "%s fence %u has signaled", mName, mFencesQueued); |
| 559 | ATRACE_NAME(message); |
| 560 | // Need an increment on both to make the trace number correct. |
| 561 | mFencesQueued++; |
| 562 | mFencesSignaled++; |
| 563 | return; |
| 564 | } |
| 565 | snprintf(message, sizeof(message), "Trace %s fence %u", mName, mFencesQueued); |
Yiwei Zhang | 9e919eb | 2019-03-07 23:21:40 -0800 | [diff] [blame] | 566 | ATRACE_NAME(message); |
| 567 | |
| 568 | mQueue.push_back(fence); |
| 569 | mCondition.notify_one(); |
| 570 | mFencesQueued++; |
| 571 | ATRACE_INT(mName, int32_t(mQueue.size())); |
| 572 | } |
| 573 | |
| 574 | private: |
| 575 | #pragma clang diagnostic push |
| 576 | #pragma clang diagnostic ignored "-Wmissing-noreturn" |
| 577 | void loop() { |
| 578 | while (true) { |
| 579 | threadLoop(); |
| 580 | } |
| 581 | } |
| 582 | #pragma clang diagnostic pop |
| 583 | |
| 584 | void threadLoop() { |
| 585 | sp<Fence> fence; |
| 586 | uint32_t fenceNum; |
| 587 | { |
| 588 | std::unique_lock<std::mutex> lock(mMutex); |
| 589 | while (mQueue.empty()) { |
| 590 | mCondition.wait(lock); |
| 591 | } |
| 592 | fence = mQueue[0]; |
| 593 | fenceNum = mFencesSignaled; |
| 594 | } |
| 595 | { |
| 596 | char message[64]; |
| 597 | snprintf(message, sizeof(message), "waiting for %s %u", mName, fenceNum); |
| 598 | ATRACE_NAME(message); |
| 599 | |
| 600 | status_t result = fence->waitForever(message); |
| 601 | if (result != OK) { |
| 602 | ALOGE("Error waiting for fence: %d", result); |
| 603 | } |
| 604 | } |
| 605 | { |
| 606 | std::lock_guard<std::mutex> lock(mMutex); |
| 607 | mQueue.pop_front(); |
| 608 | mFencesSignaled++; |
| 609 | ATRACE_INT(mName, int32_t(mQueue.size())); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | const char* mName; |
| 614 | uint32_t mFencesQueued; |
| 615 | uint32_t mFencesSignaled; |
| 616 | std::deque<sp<Fence>> mQueue; |
| 617 | std::condition_variable mCondition; |
| 618 | std::mutex mMutex; |
| 619 | }; |
| 620 | |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 621 | void Surface::getDequeueBufferInputLocked( |
| 622 | IGraphicBufferProducer::DequeueBufferInput* dequeueInput) { |
| 623 | LOG_ALWAYS_FATAL_IF(dequeueInput == nullptr, "input is null"); |
| 624 | |
| 625 | dequeueInput->width = mReqWidth ? mReqWidth : mUserWidth; |
| 626 | dequeueInput->height = mReqHeight ? mReqHeight : mUserHeight; |
| 627 | |
| 628 | dequeueInput->format = mReqFormat; |
| 629 | dequeueInput->usage = mReqUsage; |
| 630 | |
| 631 | dequeueInput->getTimestamps = mEnableFrameTimestamps; |
| 632 | } |
| 633 | |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 634 | int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 635 | ATRACE_CALL(); |
| 636 | ALOGV("Surface::dequeueBuffer"); |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 637 | |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 638 | IGraphicBufferProducer::DequeueBufferInput dqInput; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 639 | { |
| 640 | Mutex::Autolock lock(mMutex); |
Yin-Chia Yeh | 1f2af5c | 2017-05-11 16:54:04 -0700 | [diff] [blame] | 641 | if (mReportRemovedBuffers) { |
| 642 | mRemovedBuffers.clear(); |
| 643 | } |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 644 | |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 645 | getDequeueBufferInputLocked(&dqInput); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 646 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 647 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot != |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 648 | BufferItem::INVALID_BUFFER_SLOT) { |
| 649 | sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer); |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 650 | if (gbuf != nullptr) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 651 | *buffer = gbuf.get(); |
| 652 | *fenceFd = -1; |
| 653 | return OK; |
| 654 | } |
| 655 | } |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 656 | } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer |
| 657 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 658 | int buf = -1; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 659 | sp<Fence> fence; |
Dan Stoza | 932f008 | 2017-05-31 13:50:16 -0700 | [diff] [blame] | 660 | nsecs_t startTime = systemTime(); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 661 | |
| 662 | FrameEventHistoryDelta frameTimestamps; |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 663 | status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, dqInput.width, |
| 664 | dqInput.height, dqInput.format, |
| 665 | dqInput.usage, &mBufferAge, |
| 666 | dqInput.getTimestamps ? |
| 667 | &frameTimestamps : nullptr); |
Dan Stoza | 932f008 | 2017-05-31 13:50:16 -0700 | [diff] [blame] | 668 | mLastDequeueDuration = systemTime() - startTime; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 669 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 670 | if (result < 0) { |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 671 | ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer" |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 672 | "(%d, %d, %d, %#" PRIx64 ") failed: %d", |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 673 | dqInput.width, dqInput.height, dqInput.format, dqInput.usage, result); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 674 | return result; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 675 | } |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 676 | |
Dan Stoza | 90ce2a9 | 2017-05-01 16:31:53 -0700 | [diff] [blame] | 677 | if (buf < 0 || buf >= NUM_BUFFER_SLOTS) { |
| 678 | ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf); |
| 679 | android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging |
| 680 | return FAILED_TRANSACTION; |
| 681 | } |
| 682 | |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 683 | Mutex::Autolock lock(mMutex); |
| 684 | |
Dan Stoza | 932f008 | 2017-05-31 13:50:16 -0700 | [diff] [blame] | 685 | // Write this while holding the mutex |
| 686 | mLastDequeueStartTime = startTime; |
| 687 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 688 | sp<GraphicBuffer>& gbuf(mSlots[buf].buffer); |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 689 | |
| 690 | // this should never happen |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 691 | ALOGE_IF(fence == nullptr, "Surface::dequeueBuffer: received null Fence! buf=%d", buf); |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 692 | |
Yiwei Zhang | 9e919eb | 2019-03-07 23:21:40 -0800 | [diff] [blame] | 693 | if (CC_UNLIKELY(atrace_is_tag_enabled(ATRACE_TAG_GRAPHICS))) { |
| 694 | static FenceMonitor hwcReleaseThread("HWC release"); |
| 695 | hwcReleaseThread.queueFence(fence); |
| 696 | } |
| 697 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 698 | if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) { |
| 699 | freeAllBuffers(); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 700 | } |
Ted Bonkenburg | bd050ab | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 701 | |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 702 | if (dqInput.getTimestamps) { |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 703 | mFrameEventHistory->applyDelta(frameTimestamps); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 706 | if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) { |
| 707 | if (mReportRemovedBuffers && (gbuf != nullptr)) { |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 708 | mRemovedBuffers.push_back(gbuf); |
| 709 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 710 | result = mGraphicBufferProducer->requestBuffer(buf, &gbuf); |
| 711 | if (result != NO_ERROR) { |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 712 | ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result); |
Jesse Hall | 9f5a1b6 | 2014-10-02 11:09:03 -0700 | [diff] [blame] | 713 | mGraphicBufferProducer->cancelBuffer(buf, fence); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 714 | return result; |
| 715 | } |
| 716 | } |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 717 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 718 | if (fence->isValid()) { |
| 719 | *fenceFd = fence->dup(); |
| 720 | if (*fenceFd == -1) { |
| 721 | ALOGE("dequeueBuffer: error duping fence: %d", errno); |
| 722 | // dup() should never fail; something is badly wrong. Soldier on |
| 723 | // and hope for the best; the worst that should happen is some |
| 724 | // visible corruption that lasts until the next frame. |
| 725 | } |
Ted Bonkenburg | e5d6eb8 | 2011-08-09 22:38:41 -0700 | [diff] [blame] | 726 | } else { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 727 | *fenceFd = -1; |
Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 728 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 729 | |
| 730 | *buffer = gbuf.get(); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 731 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 732 | if (mSharedBufferMode && mAutoRefresh) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 733 | mSharedBufferSlot = buf; |
| 734 | mSharedBufferHasBeenQueued = false; |
| 735 | } else if (mSharedBufferSlot == buf) { |
| 736 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 737 | mSharedBufferHasBeenQueued = false; |
| 738 | } |
| 739 | |
Yin-Chia Yeh | da2e609 | 2020-07-29 15:00:07 -0700 | [diff] [blame] | 740 | mDequeuedSlots.insert(buf); |
| 741 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 742 | return OK; |
Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 745 | int Surface::dequeueBuffers(std::vector<BatchBuffer>* buffers) { |
| 746 | using DequeueBufferInput = IGraphicBufferProducer::DequeueBufferInput; |
| 747 | using DequeueBufferOutput = IGraphicBufferProducer::DequeueBufferOutput; |
| 748 | using CancelBufferInput = IGraphicBufferProducer::CancelBufferInput; |
| 749 | using RequestBufferOutput = IGraphicBufferProducer::RequestBufferOutput; |
| 750 | |
| 751 | ATRACE_CALL(); |
| 752 | ALOGV("Surface::dequeueBuffers"); |
| 753 | |
| 754 | if (buffers->size() == 0) { |
| 755 | ALOGE("%s: must dequeue at least 1 buffer!", __FUNCTION__); |
| 756 | return BAD_VALUE; |
| 757 | } |
| 758 | |
| 759 | if (mSharedBufferMode) { |
| 760 | ALOGE("%s: batch operation is not supported in shared buffer mode!", |
| 761 | __FUNCTION__); |
| 762 | return INVALID_OPERATION; |
| 763 | } |
| 764 | |
| 765 | size_t numBufferRequested = buffers->size(); |
| 766 | DequeueBufferInput input; |
| 767 | |
| 768 | { |
| 769 | Mutex::Autolock lock(mMutex); |
| 770 | if (mReportRemovedBuffers) { |
| 771 | mRemovedBuffers.clear(); |
| 772 | } |
| 773 | |
| 774 | getDequeueBufferInputLocked(&input); |
| 775 | } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffers |
| 776 | |
| 777 | std::vector<DequeueBufferInput> dequeueInput(numBufferRequested, input); |
| 778 | std::vector<DequeueBufferOutput> dequeueOutput; |
| 779 | |
| 780 | nsecs_t startTime = systemTime(); |
| 781 | |
| 782 | status_t result = mGraphicBufferProducer->dequeueBuffers(dequeueInput, &dequeueOutput); |
| 783 | |
| 784 | mLastDequeueDuration = systemTime() - startTime; |
| 785 | |
| 786 | if (result < 0) { |
| 787 | ALOGV("%s: IGraphicBufferProducer::dequeueBuffers" |
| 788 | "(%d, %d, %d, %#" PRIx64 ") failed: %d", |
| 789 | __FUNCTION__, input.width, input.height, input.format, input.usage, result); |
| 790 | return result; |
| 791 | } |
| 792 | |
| 793 | std::vector<CancelBufferInput> cancelBufferInputs(numBufferRequested); |
| 794 | std::vector<status_t> cancelBufferOutputs; |
| 795 | for (size_t i = 0; i < numBufferRequested; i++) { |
| 796 | cancelBufferInputs[i].slot = dequeueOutput[i].slot; |
| 797 | cancelBufferInputs[i].fence = dequeueOutput[i].fence; |
| 798 | } |
| 799 | |
| 800 | for (const auto& output : dequeueOutput) { |
| 801 | if (output.result < 0) { |
| 802 | mGraphicBufferProducer->cancelBuffers(cancelBufferInputs, &cancelBufferOutputs); |
| 803 | ALOGV("%s: IGraphicBufferProducer::dequeueBuffers" |
| 804 | "(%d, %d, %d, %#" PRIx64 ") failed: %d", |
| 805 | __FUNCTION__, input.width, input.height, input.format, input.usage, |
| 806 | output.result); |
| 807 | return output.result; |
| 808 | } |
| 809 | |
| 810 | if (output.slot < 0 || output.slot >= NUM_BUFFER_SLOTS) { |
| 811 | mGraphicBufferProducer->cancelBuffers(cancelBufferInputs, &cancelBufferOutputs); |
| 812 | ALOGE("%s: IGraphicBufferProducer returned invalid slot number %d", |
| 813 | __FUNCTION__, output.slot); |
| 814 | android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging |
| 815 | return FAILED_TRANSACTION; |
| 816 | } |
| 817 | |
| 818 | if (input.getTimestamps && !output.timestamps.has_value()) { |
| 819 | mGraphicBufferProducer->cancelBuffers(cancelBufferInputs, &cancelBufferOutputs); |
| 820 | ALOGE("%s: no frame timestamp returns!", __FUNCTION__); |
| 821 | return FAILED_TRANSACTION; |
| 822 | } |
| 823 | |
| 824 | // this should never happen |
| 825 | ALOGE_IF(output.fence == nullptr, |
| 826 | "%s: received null Fence! slot=%d", __FUNCTION__, output.slot); |
| 827 | } |
| 828 | |
| 829 | Mutex::Autolock lock(mMutex); |
| 830 | |
| 831 | // Write this while holding the mutex |
| 832 | mLastDequeueStartTime = startTime; |
| 833 | |
| 834 | std::vector<int32_t> requestBufferSlots; |
| 835 | requestBufferSlots.reserve(numBufferRequested); |
| 836 | // handle release all buffers and request buffers |
| 837 | for (const auto& output : dequeueOutput) { |
| 838 | if (output.result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) { |
| 839 | ALOGV("%s: RELEASE_ALL_BUFFERS during batch operation", __FUNCTION__); |
| 840 | freeAllBuffers(); |
| 841 | break; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | for (const auto& output : dequeueOutput) { |
| 846 | // Collect slots that needs requesting buffer |
| 847 | sp<GraphicBuffer>& gbuf(mSlots[output.slot].buffer); |
| 848 | if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) { |
| 849 | if (mReportRemovedBuffers && (gbuf != nullptr)) { |
| 850 | mRemovedBuffers.push_back(gbuf); |
| 851 | } |
| 852 | requestBufferSlots.push_back(output.slot); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | // Batch request Buffer |
| 857 | std::vector<RequestBufferOutput> reqBufferOutput; |
| 858 | if (requestBufferSlots.size() > 0) { |
| 859 | result = mGraphicBufferProducer->requestBuffers(requestBufferSlots, &reqBufferOutput); |
| 860 | if (result != NO_ERROR) { |
| 861 | ALOGE("%s: IGraphicBufferProducer::requestBuffers failed: %d", |
| 862 | __FUNCTION__, result); |
| 863 | mGraphicBufferProducer->cancelBuffers(cancelBufferInputs, &cancelBufferOutputs); |
| 864 | return result; |
| 865 | } |
| 866 | |
| 867 | // Check if we have any single failure |
| 868 | for (size_t i = 0; i < requestBufferSlots.size(); i++) { |
| 869 | if (reqBufferOutput[i].result != OK) { |
| 870 | ALOGE("%s: IGraphicBufferProducer::requestBuffers failed at %zu-th buffer, slot %d", |
| 871 | __FUNCTION__, i, requestBufferSlots[i]); |
| 872 | mGraphicBufferProducer->cancelBuffers(cancelBufferInputs, &cancelBufferOutputs); |
| 873 | return reqBufferOutput[i].result; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | // Fill request buffer results to mSlots |
| 878 | for (size_t i = 0; i < requestBufferSlots.size(); i++) { |
| 879 | mSlots[requestBufferSlots[i]].buffer = reqBufferOutput[i].buffer; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | for (size_t batchIdx = 0; batchIdx < numBufferRequested; batchIdx++) { |
| 884 | const auto& output = dequeueOutput[batchIdx]; |
| 885 | int slot = output.slot; |
| 886 | sp<GraphicBuffer>& gbuf(mSlots[slot].buffer); |
| 887 | |
| 888 | if (CC_UNLIKELY(atrace_is_tag_enabled(ATRACE_TAG_GRAPHICS))) { |
| 889 | static FenceMonitor hwcReleaseThread("HWC release"); |
| 890 | hwcReleaseThread.queueFence(output.fence); |
| 891 | } |
| 892 | |
| 893 | if (input.getTimestamps) { |
| 894 | mFrameEventHistory->applyDelta(output.timestamps.value()); |
| 895 | } |
| 896 | |
| 897 | if (output.fence->isValid()) { |
| 898 | buffers->at(batchIdx).fenceFd = output.fence->dup(); |
| 899 | if (buffers->at(batchIdx).fenceFd == -1) { |
| 900 | ALOGE("%s: error duping fence: %d", __FUNCTION__, errno); |
| 901 | // dup() should never fail; something is badly wrong. Soldier on |
| 902 | // and hope for the best; the worst that should happen is some |
| 903 | // visible corruption that lasts until the next frame. |
| 904 | } |
| 905 | } else { |
| 906 | buffers->at(batchIdx).fenceFd = -1; |
| 907 | } |
| 908 | |
| 909 | buffers->at(batchIdx).buffer = gbuf.get(); |
| 910 | mDequeuedSlots.insert(slot); |
| 911 | } |
| 912 | return OK; |
| 913 | } |
| 914 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 915 | int Surface::cancelBuffer(android_native_buffer_t* buffer, |
| 916 | int fenceFd) { |
| 917 | ATRACE_CALL(); |
| 918 | ALOGV("Surface::cancelBuffer"); |
| 919 | Mutex::Autolock lock(mMutex); |
| 920 | int i = getSlotFromBufferLocked(buffer); |
| 921 | if (i < 0) { |
Taiju Tsuiki | 4d0cd3f | 2015-04-30 22:15:33 +0900 | [diff] [blame] | 922 | if (fenceFd >= 0) { |
| 923 | close(fenceFd); |
| 924 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 925 | return i; |
| 926 | } |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 927 | if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) { |
| 928 | if (fenceFd >= 0) { |
| 929 | close(fenceFd); |
| 930 | } |
| 931 | return OK; |
| 932 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 933 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); |
| 934 | mGraphicBufferProducer->cancelBuffer(i, fence); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 935 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 936 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 937 | mSharedBufferHasBeenQueued = true; |
| 938 | } |
| 939 | |
Yin-Chia Yeh | da2e609 | 2020-07-29 15:00:07 -0700 | [diff] [blame] | 940 | mDequeuedSlots.erase(i); |
| 941 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 942 | return OK; |
| 943 | } |
| 944 | |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 945 | int Surface::cancelBuffers(const std::vector<BatchBuffer>& buffers) { |
| 946 | using CancelBufferInput = IGraphicBufferProducer::CancelBufferInput; |
| 947 | ATRACE_CALL(); |
| 948 | ALOGV("Surface::cancelBuffers"); |
| 949 | |
| 950 | if (mSharedBufferMode) { |
| 951 | ALOGE("%s: batch operation is not supported in shared buffer mode!", |
| 952 | __FUNCTION__); |
| 953 | return INVALID_OPERATION; |
| 954 | } |
| 955 | |
| 956 | size_t numBuffers = buffers.size(); |
| 957 | std::vector<CancelBufferInput> cancelBufferInputs(numBuffers); |
| 958 | std::vector<status_t> cancelBufferOutputs; |
| 959 | size_t numBuffersCancelled = 0; |
| 960 | int badSlotResult = 0; |
| 961 | for (size_t i = 0; i < numBuffers; i++) { |
| 962 | int slot = getSlotFromBufferLocked(buffers[i].buffer); |
| 963 | int fenceFd = buffers[i].fenceFd; |
| 964 | if (slot < 0) { |
| 965 | if (fenceFd >= 0) { |
| 966 | close(fenceFd); |
| 967 | } |
| 968 | ALOGE("%s: cannot find slot number for cancelled buffer", __FUNCTION__); |
| 969 | badSlotResult = slot; |
| 970 | } else { |
| 971 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); |
| 972 | cancelBufferInputs[numBuffersCancelled].slot = slot; |
| 973 | cancelBufferInputs[numBuffersCancelled++].fence = fence; |
| 974 | } |
| 975 | } |
| 976 | cancelBufferInputs.resize(numBuffersCancelled); |
| 977 | mGraphicBufferProducer->cancelBuffers(cancelBufferInputs, &cancelBufferOutputs); |
| 978 | |
| 979 | |
| 980 | for (size_t i = 0; i < numBuffersCancelled; i++) { |
| 981 | mDequeuedSlots.erase(cancelBufferInputs[i].slot); |
| 982 | } |
| 983 | |
| 984 | if (badSlotResult != 0) { |
| 985 | return badSlotResult; |
| 986 | } |
| 987 | return OK; |
| 988 | } |
| 989 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 990 | int Surface::getSlotFromBufferLocked( |
| 991 | android_native_buffer_t* buffer) const { |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 992 | if (buffer == nullptr) { |
| 993 | ALOGE("%s: input buffer is null!", __FUNCTION__); |
| 994 | return BAD_VALUE; |
| 995 | } |
| 996 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 997 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 998 | if (mSlots[i].buffer != nullptr && |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 999 | mSlots[i].buffer->handle == buffer->handle) { |
| 1000 | return i; |
Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 1001 | } |
| 1002 | } |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 1003 | ALOGE("%s: unknown buffer: %p", __FUNCTION__, buffer->handle); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1004 | return BAD_VALUE; |
Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 1007 | int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1008 | ALOGV("Surface::lockBuffer"); |
| 1009 | Mutex::Autolock lock(mMutex); |
| 1010 | return OK; |
| 1011 | } |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 1012 | |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 1013 | void Surface::getQueueBufferInputLocked(android_native_buffer_t* buffer, int fenceFd, |
| 1014 | nsecs_t timestamp, IGraphicBufferProducer::QueueBufferInput* out) { |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 1015 | bool isAutoTimestamp = false; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1016 | |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 1017 | if (timestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) { |
Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 1018 | timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 1019 | isAutoTimestamp = true; |
Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 1020 | ALOGV("Surface::queueBuffer making up timestamp: %.2f ms", |
Colin Cross | 9a80d50 | 2016-09-27 14:12:48 -0700 | [diff] [blame] | 1021 | timestamp / 1000000.0); |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 1022 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1024 | // Make sure the crop rectangle is entirely inside the buffer. |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 1025 | Rect crop(Rect::EMPTY_RECT); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1026 | mCrop.intersect(Rect(buffer->width, buffer->height), &crop); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1027 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1028 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 1029 | IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp, |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 1030 | static_cast<android_dataspace>(mDataSpace), crop, mScalingMode, |
| 1031 | mTransform ^ mStickyTransform, fence, mStickyTransform, |
| 1032 | mEnableFrameTimestamps); |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1033 | |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 1034 | // we should send HDR metadata as needed if this becomes a bottleneck |
| 1035 | input.setHdrMetadata(mHdrMetadata); |
| 1036 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 1037 | if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) { |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1038 | input.setSurfaceDamage(Region::INVALID_REGION); |
| 1039 | } else { |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 1040 | // Here we do two things: |
| 1041 | // 1) The surface damage was specified using the OpenGL ES convention of |
| 1042 | // the origin being in the bottom-left corner. Here we flip to the |
| 1043 | // convention that the rest of the system uses (top-left corner) by |
| 1044 | // subtracting all top/bottom coordinates from the buffer height. |
| 1045 | // 2) If the buffer is coming in rotated (for example, because the EGL |
| 1046 | // implementation is reacting to the transform hint coming back from |
| 1047 | // SurfaceFlinger), the surface damage needs to be rotated the |
| 1048 | // opposite direction, since it was generated assuming an unrotated |
| 1049 | // buffer (the app doesn't know that the EGL implementation is |
| 1050 | // reacting to the transform hint behind its back). The |
| 1051 | // transformations in the switch statement below apply those |
| 1052 | // complementary rotations (e.g., if 90 degrees, rotate 270 degrees). |
| 1053 | |
| 1054 | int width = buffer->width; |
Dan Stoza | 0e65e6c | 2015-05-26 13:22:27 -0700 | [diff] [blame] | 1055 | int height = buffer->height; |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 1056 | bool rotated90 = (mTransform ^ mStickyTransform) & |
| 1057 | NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 1058 | if (rotated90) { |
| 1059 | std::swap(width, height); |
Dan Stoza | 0e65e6c | 2015-05-26 13:22:27 -0700 | [diff] [blame] | 1060 | } |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 1061 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1062 | Region flippedRegion; |
| 1063 | for (auto rect : mDirtyRegion) { |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 1064 | int left = rect.left; |
| 1065 | int right = rect.right; |
| 1066 | int top = height - rect.bottom; // Flip from OpenGL convention |
| 1067 | int bottom = height - rect.top; // Flip from OpenGL convention |
| 1068 | switch (mTransform ^ mStickyTransform) { |
| 1069 | case NATIVE_WINDOW_TRANSFORM_ROT_90: { |
| 1070 | // Rotate 270 degrees |
| 1071 | Rect flippedRect{top, width - right, bottom, width - left}; |
| 1072 | flippedRegion.orSelf(flippedRect); |
| 1073 | break; |
| 1074 | } |
| 1075 | case NATIVE_WINDOW_TRANSFORM_ROT_180: { |
| 1076 | // Rotate 180 degrees |
| 1077 | Rect flippedRect{width - right, height - bottom, |
| 1078 | width - left, height - top}; |
| 1079 | flippedRegion.orSelf(flippedRect); |
| 1080 | break; |
| 1081 | } |
| 1082 | case NATIVE_WINDOW_TRANSFORM_ROT_270: { |
| 1083 | // Rotate 90 degrees |
| 1084 | Rect flippedRect{height - bottom, left, |
| 1085 | height - top, right}; |
| 1086 | flippedRegion.orSelf(flippedRect); |
| 1087 | break; |
| 1088 | } |
| 1089 | default: { |
| 1090 | Rect flippedRect{left, top, right, bottom}; |
| 1091 | flippedRegion.orSelf(flippedRect); |
| 1092 | break; |
| 1093 | } |
| 1094 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | input.setSurfaceDamage(flippedRegion); |
| 1098 | } |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 1099 | *out = input; |
| 1100 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1101 | |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 1102 | void Surface::onBufferQueuedLocked(int slot, sp<Fence> fence, |
| 1103 | const IGraphicBufferProducer::QueueBufferOutput& output) { |
| 1104 | mDequeuedSlots.erase(slot); |
Yin-Chia Yeh | da2e609 | 2020-07-29 15:00:07 -0700 | [diff] [blame] | 1105 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1106 | if (mEnableFrameTimestamps) { |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 1107 | mFrameEventHistory->applyDelta(output.frameTimestamps); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1108 | // Update timestamps with the local acquire fence. |
| 1109 | // The consumer doesn't send it back to prevent us from having two |
| 1110 | // file descriptors of the same fence. |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 1111 | mFrameEventHistory->updateAcquireFence(mNextFrameNumber, |
Yiwei Zhang | f0c1250 | 2019-03-13 16:09:05 -0700 | [diff] [blame] | 1112 | std::make_shared<FenceTime>(fence)); |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 1113 | |
| 1114 | // Cache timestamps of signaled fences so we can close their file |
| 1115 | // descriptors. |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 1116 | mFrameEventHistory->updateSignalTimes(); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 1119 | mLastFrameNumber = mNextFrameNumber; |
| 1120 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1121 | mDefaultWidth = output.width; |
| 1122 | mDefaultHeight = output.height; |
| 1123 | mNextFrameNumber = output.nextFrameNumber; |
tedbo | 1e7fa9e | 2011-06-22 15:52:53 -0700 | [diff] [blame] | 1124 | |
Vishnu Nair | 5eb3f06 | 2019-04-08 08:21:03 -0700 | [diff] [blame] | 1125 | // Ignore transform hint if sticky transform is set or transform to display inverse flag is |
| 1126 | // set. |
| 1127 | if (mStickyTransform == 0 && !transformToDisplayInverse()) { |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1128 | mTransformHint = output.transformHint; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1131 | mConsumerRunningBehind = (output.numPendingBuffers >= 2); |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 1132 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 1133 | if (!mConnectedToCpu) { |
| 1134 | // Clear surface damage back to full-buffer |
| 1135 | mDirtyRegion = Region::INVALID_REGION; |
| 1136 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1137 | |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 1138 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == slot) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1139 | mSharedBufferHasBeenQueued = true; |
| 1140 | } |
| 1141 | |
Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 1142 | mQueueBufferCondition.broadcast(); |
| 1143 | |
Yiwei Zhang | 9e919eb | 2019-03-07 23:21:40 -0800 | [diff] [blame] | 1144 | if (CC_UNLIKELY(atrace_is_tag_enabled(ATRACE_TAG_GRAPHICS))) { |
| 1145 | static FenceMonitor gpuCompletionThread("GPU completion"); |
| 1146 | gpuCompletionThread.queueFence(fence); |
| 1147 | } |
Yin-Chia Yeh | 78c0ad7 | 2020-02-07 14:05:35 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) { |
| 1151 | ATRACE_CALL(); |
| 1152 | ALOGV("Surface::queueBuffer"); |
| 1153 | Mutex::Autolock lock(mMutex); |
| 1154 | |
| 1155 | int i = getSlotFromBufferLocked(buffer); |
| 1156 | if (i < 0) { |
| 1157 | if (fenceFd >= 0) { |
| 1158 | close(fenceFd); |
| 1159 | } |
| 1160 | return i; |
| 1161 | } |
| 1162 | if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) { |
| 1163 | if (fenceFd >= 0) { |
| 1164 | close(fenceFd); |
| 1165 | } |
| 1166 | return OK; |
| 1167 | } |
| 1168 | |
| 1169 | IGraphicBufferProducer::QueueBufferOutput output; |
| 1170 | IGraphicBufferProducer::QueueBufferInput input; |
| 1171 | getQueueBufferInputLocked(buffer, fenceFd, mTimestamp, &input); |
| 1172 | sp<Fence> fence = input.fence; |
| 1173 | |
| 1174 | nsecs_t now = systemTime(); |
| 1175 | status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output); |
| 1176 | mLastQueueDuration = systemTime() - now; |
| 1177 | if (err != OK) { |
| 1178 | ALOGE("queueBuffer: error queuing buffer, %d", err); |
| 1179 | } |
| 1180 | |
| 1181 | onBufferQueuedLocked(i, fence, output); |
| 1182 | return err; |
| 1183 | } |
| 1184 | |
| 1185 | int Surface::queueBuffers(const std::vector<BatchQueuedBuffer>& buffers) { |
| 1186 | ATRACE_CALL(); |
| 1187 | ALOGV("Surface::queueBuffers"); |
| 1188 | Mutex::Autolock lock(mMutex); |
| 1189 | |
| 1190 | if (mSharedBufferMode) { |
| 1191 | ALOGE("%s: batched operation is not supported in shared buffer mode", __FUNCTION__); |
| 1192 | return INVALID_OPERATION; |
| 1193 | } |
| 1194 | |
| 1195 | size_t numBuffers = buffers.size(); |
| 1196 | std::vector<IGraphicBufferProducer::QueueBufferInput> queueBufferInputs(numBuffers); |
| 1197 | std::vector<IGraphicBufferProducer::QueueBufferOutput> queueBufferOutputs; |
| 1198 | std::vector<int> bufferSlots(numBuffers, -1); |
| 1199 | std::vector<sp<Fence>> bufferFences(numBuffers); |
| 1200 | |
| 1201 | for (size_t batchIdx = 0; batchIdx < numBuffers; batchIdx++) { |
| 1202 | int i = getSlotFromBufferLocked(buffers[batchIdx].buffer); |
| 1203 | if (i < 0) { |
| 1204 | if (buffers[batchIdx].fenceFd >= 0) { |
| 1205 | close(buffers[batchIdx].fenceFd); |
| 1206 | } |
| 1207 | return i; |
| 1208 | } |
| 1209 | bufferSlots[batchIdx] = i; |
| 1210 | |
| 1211 | IGraphicBufferProducer::QueueBufferInput input; |
| 1212 | getQueueBufferInputLocked( |
| 1213 | buffers[batchIdx].buffer, buffers[batchIdx].fenceFd, buffers[batchIdx].timestamp, |
| 1214 | &input); |
| 1215 | bufferFences[batchIdx] = input.fence; |
| 1216 | queueBufferInputs[batchIdx] = input; |
| 1217 | } |
| 1218 | |
| 1219 | nsecs_t now = systemTime(); |
| 1220 | status_t err = mGraphicBufferProducer->queueBuffers(queueBufferInputs, &queueBufferOutputs); |
| 1221 | mLastQueueDuration = systemTime() - now; |
| 1222 | if (err != OK) { |
| 1223 | ALOGE("%s: error queuing buffer, %d", __FUNCTION__, err); |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | for (size_t batchIdx = 0; batchIdx < numBuffers; batchIdx++) { |
| 1228 | onBufferQueuedLocked(bufferSlots[batchIdx], bufferFences[batchIdx], |
| 1229 | queueBufferOutputs[batchIdx]); |
| 1230 | } |
Yiwei Zhang | 9e919eb | 2019-03-07 23:21:40 -0800 | [diff] [blame] | 1231 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1232 | return err; |
| 1233 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1234 | |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 1235 | void Surface::querySupportedTimestampsLocked() const { |
| 1236 | // mMutex must be locked when calling this method. |
| 1237 | |
| 1238 | if (mQueriedSupportedTimestamps) { |
| 1239 | return; |
| 1240 | } |
| 1241 | mQueriedSupportedTimestamps = true; |
| 1242 | |
| 1243 | std::vector<FrameEvent> supportedFrameTimestamps; |
| 1244 | status_t err = composerService()->getSupportedFrameTimestamps( |
| 1245 | &supportedFrameTimestamps); |
| 1246 | |
| 1247 | if (err != NO_ERROR) { |
| 1248 | return; |
| 1249 | } |
| 1250 | |
| 1251 | for (auto sft : supportedFrameTimestamps) { |
| 1252 | if (sft == FrameEvent::DISPLAY_PRESENT) { |
| 1253 | mFrameTimestampsSupportsPresent = true; |
| 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1258 | int Surface::query(int what, int* value) const { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1259 | ATRACE_CALL(); |
| 1260 | ALOGV("Surface::query"); |
| 1261 | { // scope for the lock |
| 1262 | Mutex::Autolock lock(mMutex); |
| 1263 | switch (what) { |
| 1264 | case NATIVE_WINDOW_FORMAT: |
| 1265 | if (mReqFormat) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1266 | *value = static_cast<int>(mReqFormat); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1267 | return NO_ERROR; |
| 1268 | } |
| 1269 | break; |
| 1270 | case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: { |
Vishnu Nair | 17dde61 | 2020-12-28 11:39:59 -0800 | [diff] [blame] | 1271 | status_t err = mGraphicBufferProducer->query(what, value); |
| 1272 | if (err == NO_ERROR) { |
| 1273 | return NO_ERROR; |
| 1274 | } |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 1275 | if (composerService()->authenticateSurfaceTexture( |
| 1276 | mGraphicBufferProducer)) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1277 | *value = 1; |
| 1278 | } else { |
| 1279 | *value = 0; |
| 1280 | } |
| 1281 | return NO_ERROR; |
| 1282 | } |
| 1283 | case NATIVE_WINDOW_CONCRETE_TYPE: |
| 1284 | *value = NATIVE_WINDOW_SURFACE; |
| 1285 | return NO_ERROR; |
| 1286 | case NATIVE_WINDOW_DEFAULT_WIDTH: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1287 | *value = static_cast<int>( |
| 1288 | mUserWidth ? mUserWidth : mDefaultWidth); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1289 | return NO_ERROR; |
| 1290 | case NATIVE_WINDOW_DEFAULT_HEIGHT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1291 | *value = static_cast<int>( |
| 1292 | mUserHeight ? mUserHeight : mDefaultHeight); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1293 | return NO_ERROR; |
| 1294 | case NATIVE_WINDOW_TRANSFORM_HINT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1295 | *value = static_cast<int>(mTransformHint); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1296 | return NO_ERROR; |
| 1297 | case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: { |
| 1298 | status_t err = NO_ERROR; |
| 1299 | if (!mConsumerRunningBehind) { |
| 1300 | *value = 0; |
| 1301 | } else { |
| 1302 | err = mGraphicBufferProducer->query(what, value); |
| 1303 | if (err == NO_ERROR) { |
| 1304 | mConsumerRunningBehind = *value; |
| 1305 | } |
| 1306 | } |
| 1307 | return err; |
| 1308 | } |
Ian Elliott | a2eb34c | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 1309 | case NATIVE_WINDOW_BUFFER_AGE: { |
| 1310 | if (mBufferAge > INT32_MAX) { |
| 1311 | *value = 0; |
| 1312 | } else { |
| 1313 | *value = static_cast<int32_t>(mBufferAge); |
| 1314 | } |
| 1315 | return NO_ERROR; |
| 1316 | } |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 1317 | case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: { |
| 1318 | int64_t durationUs = mLastDequeueDuration / 1000; |
| 1319 | *value = durationUs > std::numeric_limits<int>::max() ? |
| 1320 | std::numeric_limits<int>::max() : |
| 1321 | static_cast<int>(durationUs); |
| 1322 | return NO_ERROR; |
| 1323 | } |
| 1324 | case NATIVE_WINDOW_LAST_QUEUE_DURATION: { |
| 1325 | int64_t durationUs = mLastQueueDuration / 1000; |
| 1326 | *value = durationUs > std::numeric_limits<int>::max() ? |
| 1327 | std::numeric_limits<int>::max() : |
| 1328 | static_cast<int>(durationUs); |
| 1329 | return NO_ERROR; |
| 1330 | } |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 1331 | case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: { |
| 1332 | querySupportedTimestampsLocked(); |
| 1333 | *value = mFrameTimestampsSupportsPresent ? 1 : 0; |
| 1334 | return NO_ERROR; |
| 1335 | } |
Mathias Agopian | 10e9ab5 | 2017-03-08 15:02:55 -0800 | [diff] [blame] | 1336 | case NATIVE_WINDOW_IS_VALID: { |
| 1337 | *value = mGraphicBufferProducer != nullptr ? 1 : 0; |
| 1338 | return NO_ERROR; |
| 1339 | } |
Peiyong Lin | 654f87b | 2018-01-30 14:21:33 -0800 | [diff] [blame] | 1340 | case NATIVE_WINDOW_DATASPACE: { |
| 1341 | *value = static_cast<int>(mDataSpace); |
| 1342 | return NO_ERROR; |
| 1343 | } |
silence_dogood | e9d092a | 2019-06-19 16:14:53 -0700 | [diff] [blame] | 1344 | case NATIVE_WINDOW_MAX_BUFFER_COUNT: { |
| 1345 | *value = mMaxBufferCount; |
| 1346 | return NO_ERROR; |
| 1347 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1348 | } |
Jamie Gennis | 391bbe2 | 2011-03-14 15:00:06 -0700 | [diff] [blame] | 1349 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1350 | return mGraphicBufferProducer->query(what, value); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 1351 | } |
| 1352 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1353 | int Surface::perform(int operation, va_list args) |
| 1354 | { |
| 1355 | int res = NO_ERROR; |
| 1356 | switch (operation) { |
| 1357 | case NATIVE_WINDOW_CONNECT: |
| 1358 | // deprecated. must return NO_ERROR. |
| 1359 | break; |
| 1360 | case NATIVE_WINDOW_DISCONNECT: |
| 1361 | // deprecated. must return NO_ERROR. |
| 1362 | break; |
| 1363 | case NATIVE_WINDOW_SET_USAGE: |
| 1364 | res = dispatchSetUsage(args); |
| 1365 | break; |
| 1366 | case NATIVE_WINDOW_SET_CROP: |
| 1367 | res = dispatchSetCrop(args); |
| 1368 | break; |
| 1369 | case NATIVE_WINDOW_SET_BUFFER_COUNT: |
| 1370 | res = dispatchSetBufferCount(args); |
| 1371 | break; |
| 1372 | case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: |
| 1373 | res = dispatchSetBuffersGeometry(args); |
| 1374 | break; |
| 1375 | case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: |
| 1376 | res = dispatchSetBuffersTransform(args); |
| 1377 | break; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1378 | case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM: |
| 1379 | res = dispatchSetBuffersStickyTransform(args); |
| 1380 | break; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1381 | case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP: |
| 1382 | res = dispatchSetBuffersTimestamp(args); |
| 1383 | break; |
| 1384 | case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS: |
| 1385 | res = dispatchSetBuffersDimensions(args); |
| 1386 | break; |
| 1387 | case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS: |
| 1388 | res = dispatchSetBuffersUserDimensions(args); |
| 1389 | break; |
| 1390 | case NATIVE_WINDOW_SET_BUFFERS_FORMAT: |
| 1391 | res = dispatchSetBuffersFormat(args); |
| 1392 | break; |
| 1393 | case NATIVE_WINDOW_LOCK: |
| 1394 | res = dispatchLock(args); |
| 1395 | break; |
| 1396 | case NATIVE_WINDOW_UNLOCK_AND_POST: |
| 1397 | res = dispatchUnlockAndPost(args); |
| 1398 | break; |
| 1399 | case NATIVE_WINDOW_SET_SCALING_MODE: |
| 1400 | res = dispatchSetScalingMode(args); |
| 1401 | break; |
| 1402 | case NATIVE_WINDOW_API_CONNECT: |
| 1403 | res = dispatchConnect(args); |
| 1404 | break; |
| 1405 | case NATIVE_WINDOW_API_DISCONNECT: |
| 1406 | res = dispatchDisconnect(args); |
| 1407 | break; |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 1408 | case NATIVE_WINDOW_SET_SIDEBAND_STREAM: |
| 1409 | res = dispatchSetSidebandStream(args); |
| 1410 | break; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1411 | case NATIVE_WINDOW_SET_BUFFERS_DATASPACE: |
| 1412 | res = dispatchSetBuffersDataSpace(args); |
| 1413 | break; |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 1414 | case NATIVE_WINDOW_SET_BUFFERS_SMPTE2086_METADATA: |
| 1415 | res = dispatchSetBuffersSmpte2086Metadata(args); |
| 1416 | break; |
| 1417 | case NATIVE_WINDOW_SET_BUFFERS_CTA861_3_METADATA: |
| 1418 | res = dispatchSetBuffersCta8613Metadata(args); |
| 1419 | break; |
Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 1420 | case NATIVE_WINDOW_SET_BUFFERS_HDR10_PLUS_METADATA: |
| 1421 | res = dispatchSetBuffersHdr10PlusMetadata(args); |
| 1422 | break; |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1423 | case NATIVE_WINDOW_SET_SURFACE_DAMAGE: |
| 1424 | res = dispatchSetSurfaceDamage(args); |
| 1425 | break; |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1426 | case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE: |
| 1427 | res = dispatchSetSharedBufferMode(args); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1428 | break; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1429 | case NATIVE_WINDOW_SET_AUTO_REFRESH: |
| 1430 | res = dispatchSetAutoRefresh(args); |
| 1431 | break; |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1432 | case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION: |
| 1433 | res = dispatchGetDisplayRefreshCycleDuration(args); |
| 1434 | break; |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1435 | case NATIVE_WINDOW_GET_NEXT_FRAME_ID: |
| 1436 | res = dispatchGetNextFrameId(args); |
| 1437 | break; |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1438 | case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS: |
| 1439 | res = dispatchEnableFrameTimestamps(args); |
| 1440 | break; |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1441 | case NATIVE_WINDOW_GET_COMPOSITOR_TIMING: |
| 1442 | res = dispatchGetCompositorTiming(args); |
| 1443 | break; |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1444 | case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS: |
| 1445 | res = dispatchGetFrameTimestamps(args); |
| 1446 | break; |
Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 1447 | case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT: |
| 1448 | res = dispatchGetWideColorSupport(args); |
| 1449 | break; |
Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 1450 | case NATIVE_WINDOW_GET_HDR_SUPPORT: |
| 1451 | res = dispatchGetHdrSupport(args); |
| 1452 | break; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1453 | case NATIVE_WINDOW_SET_USAGE64: |
| 1454 | res = dispatchSetUsage64(args); |
| 1455 | break; |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 1456 | case NATIVE_WINDOW_GET_CONSUMER_USAGE64: |
| 1457 | res = dispatchGetConsumerUsage64(args); |
| 1458 | break; |
Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1459 | case NATIVE_WINDOW_SET_AUTO_PREROTATION: |
| 1460 | res = dispatchSetAutoPrerotation(args); |
| 1461 | break; |
Alec Mouri | a161966 | 2019-08-21 19:30:48 -0700 | [diff] [blame] | 1462 | case NATIVE_WINDOW_GET_LAST_DEQUEUE_START: |
| 1463 | res = dispatchGetLastDequeueStartTime(args); |
| 1464 | break; |
Alec Mouri | 04fdb60 | 2019-08-23 19:41:43 -0700 | [diff] [blame] | 1465 | case NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT: |
| 1466 | res = dispatchSetDequeueTimeout(args); |
| 1467 | break; |
Alec Mouri | 72670c5 | 2019-08-31 01:54:33 -0700 | [diff] [blame] | 1468 | case NATIVE_WINDOW_GET_LAST_DEQUEUE_DURATION: |
| 1469 | res = dispatchGetLastDequeueDuration(args); |
| 1470 | break; |
| 1471 | case NATIVE_WINDOW_GET_LAST_QUEUE_DURATION: |
| 1472 | res = dispatchGetLastQueueDuration(args); |
| 1473 | break; |
Steven Thomas | 3172e20 | 2020-01-06 19:25:30 -0800 | [diff] [blame] | 1474 | case NATIVE_WINDOW_SET_FRAME_RATE: |
| 1475 | res = dispatchSetFrameRate(args); |
| 1476 | break; |
Alec Mouri | 09d122a | 2019-11-25 10:00:53 -0800 | [diff] [blame] | 1477 | case NATIVE_WINDOW_SET_CANCEL_INTERCEPTOR: |
| 1478 | res = dispatchAddCancelInterceptor(args); |
| 1479 | break; |
| 1480 | case NATIVE_WINDOW_SET_DEQUEUE_INTERCEPTOR: |
| 1481 | res = dispatchAddDequeueInterceptor(args); |
| 1482 | break; |
| 1483 | case NATIVE_WINDOW_SET_PERFORM_INTERCEPTOR: |
| 1484 | res = dispatchAddPerformInterceptor(args); |
| 1485 | break; |
| 1486 | case NATIVE_WINDOW_SET_QUEUE_INTERCEPTOR: |
| 1487 | res = dispatchAddQueueInterceptor(args); |
| 1488 | break; |
John Reck | 401cda6 | 2020-05-07 16:04:41 -0700 | [diff] [blame] | 1489 | case NATIVE_WINDOW_SET_QUERY_INTERCEPTOR: |
| 1490 | res = dispatchAddQueryInterceptor(args); |
| 1491 | break; |
Alec Mouri | 74aef6d | 2019-12-09 17:10:24 -0800 | [diff] [blame] | 1492 | case NATIVE_WINDOW_ALLOCATE_BUFFERS: |
| 1493 | allocateBuffers(); |
| 1494 | res = NO_ERROR; |
| 1495 | break; |
Alec Mouri | ef0b153 | 2019-12-17 09:39:07 -0800 | [diff] [blame] | 1496 | case NATIVE_WINDOW_GET_LAST_QUEUED_BUFFER: |
| 1497 | res = dispatchGetLastQueuedBuffer(args); |
| 1498 | break; |
Ady Abraham | 74e1756 | 2020-08-24 18:18:19 -0700 | [diff] [blame] | 1499 | case NATIVE_WINDOW_SET_FRAME_TIMELINE_VSYNC: |
| 1500 | res = dispatchSetFrameTimelineVsync(args); |
| 1501 | break; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1502 | default: |
| 1503 | res = NAME_NOT_FOUND; |
| 1504 | break; |
| 1505 | } |
| 1506 | return res; |
| 1507 | } |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1508 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1509 | int Surface::dispatchConnect(va_list args) { |
| 1510 | int api = va_arg(args, int); |
| 1511 | return connect(api); |
| 1512 | } |
Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 1513 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1514 | int Surface::dispatchDisconnect(va_list args) { |
| 1515 | int api = va_arg(args, int); |
| 1516 | return disconnect(api); |
| 1517 | } |
| 1518 | |
| 1519 | int Surface::dispatchSetUsage(va_list args) { |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1520 | uint64_t usage = va_arg(args, uint32_t); |
| 1521 | return setUsage(usage); |
| 1522 | } |
| 1523 | |
| 1524 | int Surface::dispatchSetUsage64(va_list args) { |
| 1525 | uint64_t usage = va_arg(args, uint64_t); |
| 1526 | return setUsage(usage); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | int Surface::dispatchSetCrop(va_list args) { |
| 1530 | android_native_rect_t const* rect = va_arg(args, android_native_rect_t*); |
| 1531 | return setCrop(reinterpret_cast<Rect const*>(rect)); |
| 1532 | } |
| 1533 | |
| 1534 | int Surface::dispatchSetBufferCount(va_list args) { |
| 1535 | size_t bufferCount = va_arg(args, size_t); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1536 | return setBufferCount(static_cast<int32_t>(bufferCount)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | int Surface::dispatchSetBuffersGeometry(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1540 | uint32_t width = va_arg(args, uint32_t); |
| 1541 | uint32_t height = va_arg(args, uint32_t); |
| 1542 | PixelFormat format = va_arg(args, PixelFormat); |
| 1543 | int err = setBuffersDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1544 | if (err != 0) { |
| 1545 | return err; |
| 1546 | } |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1547 | return setBuffersFormat(format); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | int Surface::dispatchSetBuffersDimensions(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1551 | uint32_t width = va_arg(args, uint32_t); |
| 1552 | uint32_t height = va_arg(args, uint32_t); |
| 1553 | return setBuffersDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | int Surface::dispatchSetBuffersUserDimensions(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1557 | uint32_t width = va_arg(args, uint32_t); |
| 1558 | uint32_t height = va_arg(args, uint32_t); |
| 1559 | return setBuffersUserDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | int Surface::dispatchSetBuffersFormat(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1563 | PixelFormat format = va_arg(args, PixelFormat); |
| 1564 | return setBuffersFormat(format); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | int Surface::dispatchSetScalingMode(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1568 | int mode = va_arg(args, int); |
| 1569 | return setScalingMode(mode); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | int Surface::dispatchSetBuffersTransform(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1573 | uint32_t transform = va_arg(args, uint32_t); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1574 | return setBuffersTransform(transform); |
| 1575 | } |
| 1576 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1577 | int Surface::dispatchSetBuffersStickyTransform(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1578 | uint32_t transform = va_arg(args, uint32_t); |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1579 | return setBuffersStickyTransform(transform); |
| 1580 | } |
| 1581 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1582 | int Surface::dispatchSetBuffersTimestamp(va_list args) { |
| 1583 | int64_t timestamp = va_arg(args, int64_t); |
| 1584 | return setBuffersTimestamp(timestamp); |
| 1585 | } |
| 1586 | |
| 1587 | int Surface::dispatchLock(va_list args) { |
| 1588 | ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*); |
| 1589 | ARect* inOutDirtyBounds = va_arg(args, ARect*); |
| 1590 | return lock(outBuffer, inOutDirtyBounds); |
| 1591 | } |
| 1592 | |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 1593 | int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1594 | return unlockAndPost(); |
| 1595 | } |
| 1596 | |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 1597 | int Surface::dispatchSetSidebandStream(va_list args) { |
| 1598 | native_handle_t* sH = va_arg(args, native_handle_t*); |
| 1599 | sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false); |
| 1600 | setSidebandStream(sidebandHandle); |
| 1601 | return OK; |
| 1602 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1603 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1604 | int Surface::dispatchSetBuffersDataSpace(va_list args) { |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 1605 | Dataspace dataspace = static_cast<Dataspace>(va_arg(args, int)); |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1606 | return setBuffersDataSpace(dataspace); |
| 1607 | } |
| 1608 | |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 1609 | int Surface::dispatchSetBuffersSmpte2086Metadata(va_list args) { |
| 1610 | const android_smpte2086_metadata* metadata = |
| 1611 | va_arg(args, const android_smpte2086_metadata*); |
| 1612 | return setBuffersSmpte2086Metadata(metadata); |
| 1613 | } |
| 1614 | |
| 1615 | int Surface::dispatchSetBuffersCta8613Metadata(va_list args) { |
| 1616 | const android_cta861_3_metadata* metadata = |
| 1617 | va_arg(args, const android_cta861_3_metadata*); |
| 1618 | return setBuffersCta8613Metadata(metadata); |
| 1619 | } |
| 1620 | |
Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 1621 | int Surface::dispatchSetBuffersHdr10PlusMetadata(va_list args) { |
| 1622 | const size_t size = va_arg(args, size_t); |
| 1623 | const uint8_t* metadata = va_arg(args, const uint8_t*); |
| 1624 | return setBuffersHdr10PlusMetadata(size, metadata); |
| 1625 | } |
| 1626 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1627 | int Surface::dispatchSetSurfaceDamage(va_list args) { |
| 1628 | android_native_rect_t* rects = va_arg(args, android_native_rect_t*); |
| 1629 | size_t numRects = va_arg(args, size_t); |
| 1630 | setSurfaceDamage(rects, numRects); |
| 1631 | return NO_ERROR; |
| 1632 | } |
| 1633 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1634 | int Surface::dispatchSetSharedBufferMode(va_list args) { |
| 1635 | bool sharedBufferMode = va_arg(args, int); |
| 1636 | return setSharedBufferMode(sharedBufferMode); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | int Surface::dispatchSetAutoRefresh(va_list args) { |
| 1640 | bool autoRefresh = va_arg(args, int); |
| 1641 | return setAutoRefresh(autoRefresh); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1644 | int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) { |
| 1645 | nsecs_t* outRefreshDuration = va_arg(args, int64_t*); |
| 1646 | return getDisplayRefreshCycleDuration(outRefreshDuration); |
| 1647 | } |
| 1648 | |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1649 | int Surface::dispatchGetNextFrameId(va_list args) { |
| 1650 | uint64_t* nextFrameId = va_arg(args, uint64_t*); |
| 1651 | *nextFrameId = getNextFrameNumber(); |
| 1652 | return NO_ERROR; |
| 1653 | } |
| 1654 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1655 | int Surface::dispatchEnableFrameTimestamps(va_list args) { |
| 1656 | bool enable = va_arg(args, int); |
| 1657 | enableFrameTimestamps(enable); |
| 1658 | return NO_ERROR; |
| 1659 | } |
| 1660 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1661 | int Surface::dispatchGetCompositorTiming(va_list args) { |
| 1662 | nsecs_t* compositeDeadline = va_arg(args, int64_t*); |
| 1663 | nsecs_t* compositeInterval = va_arg(args, int64_t*); |
| 1664 | nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*); |
| 1665 | return getCompositorTiming(compositeDeadline, compositeInterval, |
| 1666 | compositeToPresentLatency); |
| 1667 | } |
| 1668 | |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1669 | int Surface::dispatchGetFrameTimestamps(va_list args) { |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1670 | uint64_t frameId = va_arg(args, uint64_t); |
Brian Anderson | dbd0ea8 | 2016-07-22 09:38:59 -0700 | [diff] [blame] | 1671 | nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1672 | nsecs_t* outAcquireTime = va_arg(args, int64_t*); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1673 | nsecs_t* outLatchTime = va_arg(args, int64_t*); |
| 1674 | nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*); |
| 1675 | nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*); |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 1676 | nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*); |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 1677 | nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1678 | nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1679 | nsecs_t* outReleaseTime = va_arg(args, int64_t*); |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1680 | return getFrameTimestamps(frameId, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1681 | outRequestedPresentTime, outAcquireTime, outLatchTime, |
| 1682 | outFirstRefreshStartTime, outLastRefreshStartTime, |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 1683 | outGpuCompositionDoneTime, outDisplayPresentTime, |
Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 1684 | outDequeueReadyTime, outReleaseTime); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1685 | } |
| 1686 | |
Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 1687 | int Surface::dispatchGetWideColorSupport(va_list args) { |
| 1688 | bool* outSupport = va_arg(args, bool*); |
| 1689 | return getWideColorSupport(outSupport); |
| 1690 | } |
| 1691 | |
Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 1692 | int Surface::dispatchGetHdrSupport(va_list args) { |
| 1693 | bool* outSupport = va_arg(args, bool*); |
| 1694 | return getHdrSupport(outSupport); |
| 1695 | } |
| 1696 | |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 1697 | int Surface::dispatchGetConsumerUsage64(va_list args) { |
| 1698 | uint64_t* usage = va_arg(args, uint64_t*); |
| 1699 | return getConsumerUsage(usage); |
| 1700 | } |
| 1701 | |
Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1702 | int Surface::dispatchSetAutoPrerotation(va_list args) { |
| 1703 | bool autoPrerotation = va_arg(args, int); |
| 1704 | return setAutoPrerotation(autoPrerotation); |
| 1705 | } |
| 1706 | |
Alec Mouri | a161966 | 2019-08-21 19:30:48 -0700 | [diff] [blame] | 1707 | int Surface::dispatchGetLastDequeueStartTime(va_list args) { |
| 1708 | int64_t* lastDequeueStartTime = va_arg(args, int64_t*); |
| 1709 | *lastDequeueStartTime = mLastDequeueStartTime; |
| 1710 | return NO_ERROR; |
| 1711 | } |
| 1712 | |
Alec Mouri | 04fdb60 | 2019-08-23 19:41:43 -0700 | [diff] [blame] | 1713 | int Surface::dispatchSetDequeueTimeout(va_list args) { |
| 1714 | nsecs_t timeout = va_arg(args, int64_t); |
| 1715 | return setDequeueTimeout(timeout); |
| 1716 | } |
| 1717 | |
Alec Mouri | 72670c5 | 2019-08-31 01:54:33 -0700 | [diff] [blame] | 1718 | int Surface::dispatchGetLastDequeueDuration(va_list args) { |
| 1719 | int64_t* lastDequeueDuration = va_arg(args, int64_t*); |
| 1720 | *lastDequeueDuration = mLastDequeueDuration; |
| 1721 | return NO_ERROR; |
| 1722 | } |
| 1723 | |
| 1724 | int Surface::dispatchGetLastQueueDuration(va_list args) { |
| 1725 | int64_t* lastQueueDuration = va_arg(args, int64_t*); |
| 1726 | *lastQueueDuration = mLastQueueDuration; |
| 1727 | return NO_ERROR; |
| 1728 | } |
| 1729 | |
Steven Thomas | 3172e20 | 2020-01-06 19:25:30 -0800 | [diff] [blame] | 1730 | int Surface::dispatchSetFrameRate(va_list args) { |
| 1731 | float frameRate = static_cast<float>(va_arg(args, double)); |
Steven Thomas | 62a4cf8 | 2020-01-31 12:04:03 -0800 | [diff] [blame] | 1732 | int8_t compatibility = static_cast<int8_t>(va_arg(args, int)); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 1733 | bool shouldBeSeamless = static_cast<bool>(va_arg(args, int)); |
| 1734 | return setFrameRate(frameRate, compatibility, shouldBeSeamless); |
Steven Thomas | 3172e20 | 2020-01-06 19:25:30 -0800 | [diff] [blame] | 1735 | } |
| 1736 | |
Alec Mouri | 09d122a | 2019-11-25 10:00:53 -0800 | [diff] [blame] | 1737 | int Surface::dispatchAddCancelInterceptor(va_list args) { |
| 1738 | ANativeWindow_cancelBufferInterceptor interceptor = |
| 1739 | va_arg(args, ANativeWindow_cancelBufferInterceptor); |
| 1740 | void* data = va_arg(args, void*); |
| 1741 | std::lock_guard<std::shared_mutex> lock(mInterceptorMutex); |
| 1742 | mCancelInterceptor = interceptor; |
| 1743 | mCancelInterceptorData = data; |
| 1744 | return NO_ERROR; |
| 1745 | } |
| 1746 | |
| 1747 | int Surface::dispatchAddDequeueInterceptor(va_list args) { |
| 1748 | ANativeWindow_dequeueBufferInterceptor interceptor = |
| 1749 | va_arg(args, ANativeWindow_dequeueBufferInterceptor); |
| 1750 | void* data = va_arg(args, void*); |
| 1751 | std::lock_guard<std::shared_mutex> lock(mInterceptorMutex); |
| 1752 | mDequeueInterceptor = interceptor; |
| 1753 | mDequeueInterceptorData = data; |
| 1754 | return NO_ERROR; |
| 1755 | } |
| 1756 | |
| 1757 | int Surface::dispatchAddPerformInterceptor(va_list args) { |
| 1758 | ANativeWindow_performInterceptor interceptor = va_arg(args, ANativeWindow_performInterceptor); |
| 1759 | void* data = va_arg(args, void*); |
| 1760 | std::lock_guard<std::shared_mutex> lock(mInterceptorMutex); |
| 1761 | mPerformInterceptor = interceptor; |
| 1762 | mPerformInterceptorData = data; |
| 1763 | return NO_ERROR; |
| 1764 | } |
| 1765 | |
| 1766 | int Surface::dispatchAddQueueInterceptor(va_list args) { |
| 1767 | ANativeWindow_queueBufferInterceptor interceptor = |
| 1768 | va_arg(args, ANativeWindow_queueBufferInterceptor); |
| 1769 | void* data = va_arg(args, void*); |
| 1770 | std::lock_guard<std::shared_mutex> lock(mInterceptorMutex); |
| 1771 | mQueueInterceptor = interceptor; |
| 1772 | mQueueInterceptorData = data; |
| 1773 | return NO_ERROR; |
| 1774 | } |
| 1775 | |
John Reck | 401cda6 | 2020-05-07 16:04:41 -0700 | [diff] [blame] | 1776 | int Surface::dispatchAddQueryInterceptor(va_list args) { |
| 1777 | ANativeWindow_queryInterceptor interceptor = va_arg(args, ANativeWindow_queryInterceptor); |
| 1778 | void* data = va_arg(args, void*); |
| 1779 | std::lock_guard<std::shared_mutex> lock(mInterceptorMutex); |
| 1780 | mQueryInterceptor = interceptor; |
| 1781 | mQueryInterceptorData = data; |
| 1782 | return NO_ERROR; |
| 1783 | } |
| 1784 | |
Alec Mouri | ef0b153 | 2019-12-17 09:39:07 -0800 | [diff] [blame] | 1785 | int Surface::dispatchGetLastQueuedBuffer(va_list args) { |
| 1786 | AHardwareBuffer** buffer = va_arg(args, AHardwareBuffer**); |
| 1787 | int* fence = va_arg(args, int*); |
| 1788 | float* matrix = va_arg(args, float*); |
| 1789 | sp<GraphicBuffer> graphicBuffer; |
| 1790 | sp<Fence> spFence; |
| 1791 | |
| 1792 | int result = mGraphicBufferProducer->getLastQueuedBuffer(&graphicBuffer, &spFence, matrix); |
| 1793 | |
| 1794 | if (graphicBuffer != nullptr) { |
Yiwei Zhang | 03fbf9c | 2020-06-03 10:01:34 -0700 | [diff] [blame] | 1795 | *buffer = graphicBuffer->toAHardwareBuffer(); |
Alec Mouri | ef0b153 | 2019-12-17 09:39:07 -0800 | [diff] [blame] | 1796 | AHardwareBuffer_acquire(*buffer); |
| 1797 | } else { |
| 1798 | *buffer = nullptr; |
| 1799 | } |
| 1800 | |
| 1801 | if (spFence != nullptr) { |
| 1802 | *fence = spFence->dup(); |
| 1803 | } else { |
| 1804 | *fence = -1; |
| 1805 | } |
| 1806 | return result; |
| 1807 | } |
| 1808 | |
Ady Abraham | 74e1756 | 2020-08-24 18:18:19 -0700 | [diff] [blame] | 1809 | int Surface::dispatchSetFrameTimelineVsync(va_list args) { |
| 1810 | ATRACE_CALL(); |
| 1811 | auto frameTimelineVsyncId = static_cast<int64_t>(va_arg(args, int64_t)); |
| 1812 | |
| 1813 | ALOGV("Surface::dispatchSetFrameTimelineVsync"); |
Robert Carr | 9b611b7 | 2020-10-19 12:00:23 -0700 | [diff] [blame] | 1814 | return setFrameTimelineVsync(frameTimelineVsyncId); |
Ady Abraham | 74e1756 | 2020-08-24 18:18:19 -0700 | [diff] [blame] | 1815 | } |
| 1816 | |
Vishnu Nair | 5eb3f06 | 2019-04-08 08:21:03 -0700 | [diff] [blame] | 1817 | bool Surface::transformToDisplayInverse() { |
| 1818 | return (mTransform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) == |
| 1819 | NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY; |
| 1820 | } |
| 1821 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1822 | int Surface::connect(int api) { |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 1823 | static sp<IProducerListener> listener = new StubProducerListener(); |
Dan Stoza | 966b98b | 2015-03-02 22:12:37 -0800 | [diff] [blame] | 1824 | return connect(api, listener); |
| 1825 | } |
| 1826 | |
| 1827 | int Surface::connect(int api, const sp<IProducerListener>& listener) { |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1828 | return connect(api, listener, false); |
| 1829 | } |
| 1830 | |
| 1831 | int Surface::connect( |
Shuzhen Wang | 067fcd3 | 2019-08-14 10:41:12 -0700 | [diff] [blame] | 1832 | int api, bool reportBufferRemoval, const sp<SurfaceListener>& sListener) { |
| 1833 | if (sListener != nullptr) { |
| 1834 | mListenerProxy = new ProducerListenerProxy(this, sListener); |
| 1835 | } |
| 1836 | return connect(api, mListenerProxy, reportBufferRemoval); |
| 1837 | } |
| 1838 | |
| 1839 | int Surface::connect( |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1840 | int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1841 | ATRACE_CALL(); |
| 1842 | ALOGV("Surface::connect"); |
| 1843 | Mutex::Autolock lock(mMutex); |
| 1844 | IGraphicBufferProducer::QueueBufferOutput output; |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1845 | mReportRemovedBuffers = reportBufferRemoval; |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1846 | int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1847 | if (err == NO_ERROR) { |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1848 | mDefaultWidth = output.width; |
| 1849 | mDefaultHeight = output.height; |
| 1850 | mNextFrameNumber = output.nextFrameNumber; |
silence_dogood | e9d092a | 2019-06-19 16:14:53 -0700 | [diff] [blame] | 1851 | mMaxBufferCount = output.maxBufferCount; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1852 | |
Vishnu Nair | 5eb3f06 | 2019-04-08 08:21:03 -0700 | [diff] [blame] | 1853 | // Ignore transform hint if sticky transform is set or transform to display inverse flag is |
| 1854 | // set. Transform hint should be ignored if the client is expected to always submit buffers |
| 1855 | // in the same orientation. |
| 1856 | if (mStickyTransform == 0 && !transformToDisplayInverse()) { |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1857 | mTransformHint = output.transformHint; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1860 | mConsumerRunningBehind = (output.numPendingBuffers >= 2); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1861 | } |
| 1862 | if (!err && api == NATIVE_WINDOW_API_CPU) { |
| 1863 | mConnectedToCpu = true; |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1864 | // Clear the dirty region in case we're switching from a non-CPU API |
| 1865 | mDirtyRegion.clear(); |
| 1866 | } else if (!err) { |
| 1867 | // Initialize the dirty region for tracking surface damage |
| 1868 | mDirtyRegion = Region::INVALID_REGION; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1869 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1870 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1871 | return err; |
| 1872 | } |
| 1873 | |
Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 1874 | |
Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1875 | int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1876 | ATRACE_CALL(); |
| 1877 | ALOGV("Surface::disconnect"); |
| 1878 | Mutex::Autolock lock(mMutex); |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1879 | mRemovedBuffers.clear(); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1880 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1881 | mSharedBufferHasBeenQueued = false; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1882 | freeAllBuffers(); |
Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1883 | int err = mGraphicBufferProducer->disconnect(api, mode); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1884 | if (!err) { |
| 1885 | mReqFormat = 0; |
| 1886 | mReqWidth = 0; |
| 1887 | mReqHeight = 0; |
| 1888 | mReqUsage = 0; |
| 1889 | mCrop.clear(); |
| 1890 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 1891 | mTransform = 0; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1892 | mStickyTransform = 0; |
Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1893 | mAutoPrerotation = false; |
Yiwei Zhang | c185192 | 2019-08-21 14:02:45 -0700 | [diff] [blame] | 1894 | mEnableFrameTimestamps = false; |
Yiwei Zhang | 74c9cc3 | 2020-06-20 03:35:17 -0700 | [diff] [blame] | 1895 | mMaxBufferCount = NUM_BUFFER_SLOTS; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1896 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1897 | if (api == NATIVE_WINDOW_API_CPU) { |
| 1898 | mConnectedToCpu = false; |
| 1899 | } |
| 1900 | } |
| 1901 | return err; |
| 1902 | } |
| 1903 | |
Dan Stoza | d9c4971 | 2015-04-27 11:06:01 -0700 | [diff] [blame] | 1904 | int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer, |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1905 | sp<Fence>* outFence) { |
| 1906 | ATRACE_CALL(); |
| 1907 | ALOGV("Surface::detachNextBuffer"); |
| 1908 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1909 | if (outBuffer == nullptr || outFence == nullptr) { |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1910 | return BAD_VALUE; |
| 1911 | } |
| 1912 | |
| 1913 | Mutex::Autolock lock(mMutex); |
Yin-Chia Yeh | 1f2af5c | 2017-05-11 16:54:04 -0700 | [diff] [blame] | 1914 | if (mReportRemovedBuffers) { |
| 1915 | mRemovedBuffers.clear(); |
| 1916 | } |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1917 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1918 | sp<GraphicBuffer> buffer(nullptr); |
| 1919 | sp<Fence> fence(nullptr); |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1920 | status_t result = mGraphicBufferProducer->detachNextBuffer( |
| 1921 | &buffer, &fence); |
| 1922 | if (result != NO_ERROR) { |
| 1923 | return result; |
| 1924 | } |
| 1925 | |
Dan Stoza | d9c4971 | 2015-04-27 11:06:01 -0700 | [diff] [blame] | 1926 | *outBuffer = buffer; |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1927 | if (fence != nullptr && fence->isValid()) { |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1928 | *outFence = fence; |
| 1929 | } else { |
| 1930 | *outFence = Fence::NO_FENCE; |
| 1931 | } |
| 1932 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1933 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1934 | if (mSlots[i].buffer != nullptr && |
Shuzhen Wang | 55be505 | 2017-05-23 09:41:54 -0700 | [diff] [blame] | 1935 | mSlots[i].buffer->getId() == buffer->getId()) { |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1936 | if (mReportRemovedBuffers) { |
| 1937 | mRemovedBuffers.push_back(mSlots[i].buffer); |
| 1938 | } |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1939 | mSlots[i].buffer = nullptr; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1940 | } |
| 1941 | } |
| 1942 | |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1943 | return NO_ERROR; |
| 1944 | } |
| 1945 | |
| 1946 | int Surface::attachBuffer(ANativeWindowBuffer* buffer) |
| 1947 | { |
| 1948 | ATRACE_CALL(); |
| 1949 | ALOGV("Surface::attachBuffer"); |
| 1950 | |
| 1951 | Mutex::Autolock lock(mMutex); |
Yin-Chia Yeh | 1f2af5c | 2017-05-11 16:54:04 -0700 | [diff] [blame] | 1952 | if (mReportRemovedBuffers) { |
| 1953 | mRemovedBuffers.clear(); |
| 1954 | } |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1955 | |
| 1956 | sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer)); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1957 | uint32_t priorGeneration = graphicBuffer->mGenerationNumber; |
| 1958 | graphicBuffer->mGenerationNumber = mGenerationNumber; |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1959 | int32_t attachedSlot = -1; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1960 | status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer); |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1961 | if (result != NO_ERROR) { |
| 1962 | ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1963 | graphicBuffer->mGenerationNumber = priorGeneration; |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1964 | return result; |
| 1965 | } |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1966 | if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) { |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1967 | mRemovedBuffers.push_back(mSlots[attachedSlot].buffer); |
| 1968 | } |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1969 | mSlots[attachedSlot].buffer = graphicBuffer; |
Yin-Chia Yeh | da2e609 | 2020-07-29 15:00:07 -0700 | [diff] [blame] | 1970 | mDequeuedSlots.insert(attachedSlot); |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1971 | |
| 1972 | return NO_ERROR; |
| 1973 | } |
| 1974 | |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1975 | int Surface::setUsage(uint64_t reqUsage) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1976 | { |
| 1977 | ALOGV("Surface::setUsage"); |
| 1978 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1979 | if (reqUsage != mReqUsage) { |
| 1980 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1981 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1982 | mReqUsage = reqUsage; |
| 1983 | return OK; |
| 1984 | } |
| 1985 | |
| 1986 | int Surface::setCrop(Rect const* rect) |
| 1987 | { |
| 1988 | ATRACE_CALL(); |
| 1989 | |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 1990 | Rect realRect(Rect::EMPTY_RECT); |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1991 | if (rect == nullptr || rect->isEmpty()) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1992 | realRect.clear(); |
| 1993 | } else { |
| 1994 | realRect = *rect; |
Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 1995 | } |
| 1996 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1997 | ALOGV("Surface::setCrop rect=[%d %d %d %d]", |
| 1998 | realRect.left, realRect.top, realRect.right, realRect.bottom); |
| 1999 | |
| 2000 | Mutex::Autolock lock(mMutex); |
| 2001 | mCrop = realRect; |
| 2002 | return NO_ERROR; |
| 2003 | } |
| 2004 | |
| 2005 | int Surface::setBufferCount(int bufferCount) |
| 2006 | { |
| 2007 | ATRACE_CALL(); |
| 2008 | ALOGV("Surface::setBufferCount"); |
| 2009 | Mutex::Autolock lock(mMutex); |
| 2010 | |
Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 2011 | status_t err = NO_ERROR; |
| 2012 | if (bufferCount == 0) { |
| 2013 | err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1); |
| 2014 | } else { |
| 2015 | int minUndequeuedBuffers = 0; |
| 2016 | err = mGraphicBufferProducer->query( |
| 2017 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers); |
| 2018 | if (err == NO_ERROR) { |
| 2019 | err = mGraphicBufferProducer->setMaxDequeuedBufferCount( |
| 2020 | bufferCount - minUndequeuedBuffers); |
| 2021 | } |
| 2022 | } |
Mathias Agopian | 9014726 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 2023 | |
Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 2024 | ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s", |
| 2025 | bufferCount, strerror(-err)); |
| 2026 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 2027 | return err; |
| 2028 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 2029 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 2030 | int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) { |
| 2031 | ATRACE_CALL(); |
| 2032 | ALOGV("Surface::setMaxDequeuedBufferCount"); |
| 2033 | Mutex::Autolock lock(mMutex); |
| 2034 | |
| 2035 | status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount( |
| 2036 | maxDequeuedBuffers); |
| 2037 | ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) " |
| 2038 | "returned %s", maxDequeuedBuffers, strerror(-err)); |
| 2039 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 2040 | return err; |
| 2041 | } |
| 2042 | |
| 2043 | int Surface::setAsyncMode(bool async) { |
| 2044 | ATRACE_CALL(); |
| 2045 | ALOGV("Surface::setAsyncMode"); |
| 2046 | Mutex::Autolock lock(mMutex); |
| 2047 | |
| 2048 | status_t err = mGraphicBufferProducer->setAsyncMode(async); |
| 2049 | ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s", |
| 2050 | async, strerror(-err)); |
| 2051 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 2052 | return err; |
| 2053 | } |
| 2054 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 2055 | int Surface::setSharedBufferMode(bool sharedBufferMode) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 2056 | ATRACE_CALL(); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 2057 | ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 2058 | Mutex::Autolock lock(mMutex); |
| 2059 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 2060 | status_t err = mGraphicBufferProducer->setSharedBufferMode( |
| 2061 | sharedBufferMode); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 2062 | if (err == NO_ERROR) { |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 2063 | mSharedBufferMode = sharedBufferMode; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 2064 | } |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 2065 | ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned" |
| 2066 | "%s", sharedBufferMode, strerror(-err)); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 2067 | |
| 2068 | return err; |
| 2069 | } |
| 2070 | |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 2071 | int Surface::setAutoRefresh(bool autoRefresh) { |
| 2072 | ATRACE_CALL(); |
| 2073 | ALOGV("Surface::setAutoRefresh (%d)", autoRefresh); |
| 2074 | Mutex::Autolock lock(mMutex); |
| 2075 | |
| 2076 | status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh); |
| 2077 | if (err == NO_ERROR) { |
| 2078 | mAutoRefresh = autoRefresh; |
| 2079 | } |
| 2080 | ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s", |
| 2081 | autoRefresh, strerror(-err)); |
| 2082 | return err; |
| 2083 | } |
| 2084 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2085 | int Surface::setBuffersDimensions(uint32_t width, uint32_t height) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2086 | { |
| 2087 | ATRACE_CALL(); |
| 2088 | ALOGV("Surface::setBuffersDimensions"); |
| 2089 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2090 | if ((width && !height) || (!width && height)) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2091 | return BAD_VALUE; |
| 2092 | |
| 2093 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 2094 | if (width != mReqWidth || height != mReqHeight) { |
| 2095 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 2096 | } |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2097 | mReqWidth = width; |
| 2098 | mReqHeight = height; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2099 | return NO_ERROR; |
| 2100 | } |
| 2101 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2102 | int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2103 | { |
| 2104 | ATRACE_CALL(); |
| 2105 | ALOGV("Surface::setBuffersUserDimensions"); |
| 2106 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2107 | if ((width && !height) || (!width && height)) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2108 | return BAD_VALUE; |
| 2109 | |
| 2110 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 2111 | if (width != mUserWidth || height != mUserHeight) { |
| 2112 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 2113 | } |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2114 | mUserWidth = width; |
| 2115 | mUserHeight = height; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2116 | return NO_ERROR; |
| 2117 | } |
| 2118 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2119 | int Surface::setBuffersFormat(PixelFormat format) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2120 | { |
| 2121 | ALOGV("Surface::setBuffersFormat"); |
| 2122 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2123 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 2124 | if (format != mReqFormat) { |
| 2125 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 2126 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2127 | mReqFormat = format; |
| 2128 | return NO_ERROR; |
| 2129 | } |
| 2130 | |
| 2131 | int Surface::setScalingMode(int mode) |
| 2132 | { |
| 2133 | ATRACE_CALL(); |
| 2134 | ALOGV("Surface::setScalingMode(%d)", mode); |
| 2135 | |
| 2136 | switch (mode) { |
| 2137 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: |
| 2138 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: |
| 2139 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: |
Robert Carr | c2e7788 | 2015-12-16 18:14:03 -0800 | [diff] [blame] | 2140 | case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP: |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2141 | break; |
| 2142 | default: |
| 2143 | ALOGE("unknown scaling mode: %d", mode); |
| 2144 | return BAD_VALUE; |
| 2145 | } |
| 2146 | |
| 2147 | Mutex::Autolock lock(mMutex); |
| 2148 | mScalingMode = mode; |
| 2149 | return NO_ERROR; |
| 2150 | } |
| 2151 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2152 | int Surface::setBuffersTransform(uint32_t transform) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2153 | { |
| 2154 | ATRACE_CALL(); |
| 2155 | ALOGV("Surface::setBuffersTransform"); |
| 2156 | Mutex::Autolock lock(mMutex); |
Vishnu Nair | 5eb3f06 | 2019-04-08 08:21:03 -0700 | [diff] [blame] | 2157 | // Ensure NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY is sticky. If the client sets the flag, do not |
| 2158 | // override it until the surface is disconnected. This is a temporary workaround for camera |
| 2159 | // until they switch to using Buffer State Layers. Currently if client sets the buffer transform |
| 2160 | // it may be overriden by the buffer producer when the producer sets the buffer transform. |
| 2161 | if (transformToDisplayInverse()) { |
| 2162 | transform |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY; |
| 2163 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2164 | mTransform = transform; |
| 2165 | return NO_ERROR; |
| 2166 | } |
| 2167 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2168 | int Surface::setBuffersStickyTransform(uint32_t transform) |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 2169 | { |
| 2170 | ATRACE_CALL(); |
| 2171 | ALOGV("Surface::setBuffersStickyTransform"); |
| 2172 | Mutex::Autolock lock(mMutex); |
| 2173 | mStickyTransform = transform; |
| 2174 | return NO_ERROR; |
| 2175 | } |
| 2176 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2177 | int Surface::setBuffersTimestamp(int64_t timestamp) |
| 2178 | { |
| 2179 | ALOGV("Surface::setBuffersTimestamp"); |
| 2180 | Mutex::Autolock lock(mMutex); |
| 2181 | mTimestamp = timestamp; |
| 2182 | return NO_ERROR; |
| 2183 | } |
| 2184 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 2185 | int Surface::setBuffersDataSpace(Dataspace dataSpace) |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 2186 | { |
| 2187 | ALOGV("Surface::setBuffersDataSpace"); |
| 2188 | Mutex::Autolock lock(mMutex); |
| 2189 | mDataSpace = dataSpace; |
| 2190 | return NO_ERROR; |
| 2191 | } |
| 2192 | |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 2193 | int Surface::setBuffersSmpte2086Metadata(const android_smpte2086_metadata* metadata) { |
| 2194 | ALOGV("Surface::setBuffersSmpte2086Metadata"); |
| 2195 | Mutex::Autolock lock(mMutex); |
| 2196 | if (metadata) { |
| 2197 | mHdrMetadata.smpte2086 = *metadata; |
| 2198 | mHdrMetadata.validTypes |= HdrMetadata::SMPTE2086; |
| 2199 | } else { |
| 2200 | mHdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086; |
| 2201 | } |
| 2202 | return NO_ERROR; |
| 2203 | } |
| 2204 | |
| 2205 | int Surface::setBuffersCta8613Metadata(const android_cta861_3_metadata* metadata) { |
| 2206 | ALOGV("Surface::setBuffersCta8613Metadata"); |
| 2207 | Mutex::Autolock lock(mMutex); |
| 2208 | if (metadata) { |
| 2209 | mHdrMetadata.cta8613 = *metadata; |
| 2210 | mHdrMetadata.validTypes |= HdrMetadata::CTA861_3; |
| 2211 | } else { |
| 2212 | mHdrMetadata.validTypes &= ~HdrMetadata::CTA861_3; |
| 2213 | } |
| 2214 | return NO_ERROR; |
| 2215 | } |
| 2216 | |
Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 2217 | int Surface::setBuffersHdr10PlusMetadata(const size_t size, const uint8_t* metadata) { |
| 2218 | ALOGV("Surface::setBuffersBlobMetadata"); |
| 2219 | Mutex::Autolock lock(mMutex); |
| 2220 | if (size > 0) { |
| 2221 | mHdrMetadata.hdr10plus.assign(metadata, metadata + size); |
| 2222 | mHdrMetadata.validTypes |= HdrMetadata::HDR10PLUS; |
| 2223 | } else { |
| 2224 | mHdrMetadata.validTypes &= ~HdrMetadata::HDR10PLUS; |
| 2225 | mHdrMetadata.hdr10plus.clear(); |
| 2226 | } |
| 2227 | return NO_ERROR; |
| 2228 | } |
| 2229 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 2230 | Dataspace Surface::getBuffersDataSpace() { |
Courtney Goeltzenleuchter | 152279d | 2017-08-14 18:18:30 -0600 | [diff] [blame] | 2231 | ALOGV("Surface::getBuffersDataSpace"); |
| 2232 | Mutex::Autolock lock(mMutex); |
| 2233 | return mDataSpace; |
| 2234 | } |
| 2235 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2236 | void Surface::freeAllBuffers() { |
Yin-Chia Yeh | da2e609 | 2020-07-29 15:00:07 -0700 | [diff] [blame] | 2237 | if (!mDequeuedSlots.empty()) { |
| 2238 | ALOGE("%s: %zu buffers were freed while being dequeued!", |
| 2239 | __FUNCTION__, mDequeuedSlots.size()); |
| 2240 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2241 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 2242 | mSlots[i].buffer = nullptr; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | |
Shuzhen Wang | 067fcd3 | 2019-08-14 10:41:12 -0700 | [diff] [blame] | 2246 | status_t Surface::getAndFlushBuffersFromSlots(const std::vector<int32_t>& slots, |
| 2247 | std::vector<sp<GraphicBuffer>>* outBuffers) { |
| 2248 | ALOGV("Surface::getAndFlushBuffersFromSlots"); |
| 2249 | for (int32_t i : slots) { |
| 2250 | if (i < 0 || i >= NUM_BUFFER_SLOTS) { |
| 2251 | ALOGE("%s: Invalid slotIndex: %d", __FUNCTION__, i); |
| 2252 | return BAD_VALUE; |
| 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | Mutex::Autolock lock(mMutex); |
| 2257 | for (int32_t i : slots) { |
| 2258 | if (mSlots[i].buffer == nullptr) { |
| 2259 | ALOGW("%s: Discarded slot %d doesn't contain buffer!", __FUNCTION__, i); |
| 2260 | continue; |
| 2261 | } |
Yin-Chia Yeh | da2e609 | 2020-07-29 15:00:07 -0700 | [diff] [blame] | 2262 | // Don't flush currently dequeued buffers |
| 2263 | if (mDequeuedSlots.count(i) > 0) { |
| 2264 | continue; |
| 2265 | } |
Shuzhen Wang | 067fcd3 | 2019-08-14 10:41:12 -0700 | [diff] [blame] | 2266 | outBuffers->push_back(mSlots[i].buffer); |
| 2267 | mSlots[i].buffer = nullptr; |
| 2268 | } |
| 2269 | return OK; |
| 2270 | } |
| 2271 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 2272 | void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) { |
| 2273 | ATRACE_CALL(); |
| 2274 | ALOGV("Surface::setSurfaceDamage"); |
| 2275 | Mutex::Autolock lock(mMutex); |
| 2276 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 2277 | if (mConnectedToCpu || numRects == 0) { |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 2278 | mDirtyRegion = Region::INVALID_REGION; |
| 2279 | return; |
| 2280 | } |
| 2281 | |
| 2282 | mDirtyRegion.clear(); |
| 2283 | for (size_t r = 0; r < numRects; ++r) { |
| 2284 | // We intentionally flip top and bottom here, since because they're |
| 2285 | // specified with a bottom-left origin, top > bottom, which fails |
| 2286 | // validation in the Region class. We will fix this up when we flip to a |
| 2287 | // top-left origin in queueBuffer. |
| 2288 | Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top); |
| 2289 | mDirtyRegion.orSelf(rect); |
| 2290 | } |
| 2291 | } |
| 2292 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2293 | // ---------------------------------------------------------------------- |
| 2294 | // the lock/unlock APIs must be used from the same thread |
| 2295 | |
| 2296 | static status_t copyBlt( |
| 2297 | const sp<GraphicBuffer>& dst, |
| 2298 | const sp<GraphicBuffer>& src, |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 2299 | const Region& reg, |
| 2300 | int *dstFenceFd) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2301 | { |
Dennis Kempin | add458f | 2017-05-17 19:16:31 -0700 | [diff] [blame] | 2302 | if (dst->getId() == src->getId()) |
| 2303 | return OK; |
| 2304 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2305 | // src and dst with, height and format must be identical. no verification |
| 2306 | // is done here. |
| 2307 | status_t err; |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 2308 | uint8_t* src_bits = nullptr; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2309 | err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), |
| 2310 | reinterpret_cast<void**>(&src_bits)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2311 | ALOGE_IF(err, "error locking src buffer %s", strerror(-err)); |
| 2312 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 2313 | uint8_t* dst_bits = nullptr; |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 2314 | err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), |
| 2315 | reinterpret_cast<void**>(&dst_bits), *dstFenceFd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2316 | ALOGE_IF(err, "error locking dst buffer %s", strerror(-err)); |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 2317 | *dstFenceFd = -1; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2318 | |
| 2319 | Region::const_iterator head(reg.begin()); |
| 2320 | Region::const_iterator tail(reg.end()); |
| 2321 | if (head != tail && src_bits && dst_bits) { |
| 2322 | const size_t bpp = bytesPerPixel(src->format); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2323 | const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp; |
| 2324 | const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2325 | |
| 2326 | while (head != tail) { |
| 2327 | const Rect& r(*head++); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2328 | int32_t h = r.height(); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2329 | if (h <= 0) continue; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2330 | size_t size = static_cast<uint32_t>(r.width()) * bpp; |
| 2331 | uint8_t const * s = src_bits + |
| 2332 | static_cast<uint32_t>(r.left + src->stride * r.top) * bpp; |
| 2333 | uint8_t * d = dst_bits + |
| 2334 | static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2335 | if (dbpr==sbpr && size==sbpr) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 2336 | size *= static_cast<size_t>(h); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2337 | h = 1; |
| 2338 | } |
| 2339 | do { |
| 2340 | memcpy(d, s, size); |
| 2341 | d += dbpr; |
| 2342 | s += sbpr; |
| 2343 | } while (--h > 0); |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | if (src_bits) |
| 2348 | src->unlock(); |
| 2349 | |
| 2350 | if (dst_bits) |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 2351 | dst->unlockAsync(dstFenceFd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2352 | |
| 2353 | return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2354 | } |
| 2355 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 2356 | // ---------------------------------------------------------------------------- |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2357 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2358 | status_t Surface::lock( |
| 2359 | ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) |
| 2360 | { |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 2361 | if (mLockedBuffer != nullptr) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2362 | ALOGE("Surface::lock failed, already locked"); |
| 2363 | return INVALID_OPERATION; |
| 2364 | } |
| 2365 | |
| 2366 | if (!mConnectedToCpu) { |
| 2367 | int err = Surface::connect(NATIVE_WINDOW_API_CPU); |
| 2368 | if (err) { |
| 2369 | return err; |
| 2370 | } |
| 2371 | // we're intending to do software rendering from this point |
| 2372 | setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 2373 | } |
| 2374 | |
| 2375 | ANativeWindowBuffer* out; |
| 2376 | int fenceFd = -1; |
| 2377 | status_t err = dequeueBuffer(&out, &fenceFd); |
| 2378 | ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err)); |
| 2379 | if (err == NO_ERROR) { |
| 2380 | sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2381 | const Rect bounds(backBuffer->width, backBuffer->height); |
| 2382 | |
| 2383 | Region newDirtyRegion; |
| 2384 | if (inOutDirtyBounds) { |
| 2385 | newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds)); |
| 2386 | newDirtyRegion.andSelf(bounds); |
| 2387 | } else { |
| 2388 | newDirtyRegion.set(bounds); |
| 2389 | } |
| 2390 | |
| 2391 | // figure out if we can copy the frontbuffer back |
| 2392 | const sp<GraphicBuffer>& frontBuffer(mPostedBuffer); |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 2393 | const bool canCopyBack = (frontBuffer != nullptr && |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2394 | backBuffer->width == frontBuffer->width && |
| 2395 | backBuffer->height == frontBuffer->height && |
| 2396 | backBuffer->format == frontBuffer->format); |
| 2397 | |
| 2398 | if (canCopyBack) { |
| 2399 | // copy the area that is invalid and not repainted this round |
| 2400 | const Region copyback(mDirtyRegion.subtract(newDirtyRegion)); |
Francis Hart | dc10f84 | 2014-12-01 16:04:49 +0200 | [diff] [blame] | 2401 | if (!copyback.isEmpty()) { |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 2402 | copyBlt(backBuffer, frontBuffer, copyback, &fenceFd); |
Francis Hart | dc10f84 | 2014-12-01 16:04:49 +0200 | [diff] [blame] | 2403 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2404 | } else { |
| 2405 | // if we can't copy-back anything, modify the user's dirty |
| 2406 | // region to make sure they redraw the whole buffer |
| 2407 | newDirtyRegion.set(bounds); |
| 2408 | mDirtyRegion.clear(); |
| 2409 | Mutex::Autolock lock(mMutex); |
| 2410 | for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) { |
| 2411 | mSlots[i].dirtyRegion.clear(); |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | |
| 2416 | { // scope for the lock |
| 2417 | Mutex::Autolock lock(mMutex); |
| 2418 | int backBufferSlot(getSlotFromBufferLocked(backBuffer.get())); |
| 2419 | if (backBufferSlot >= 0) { |
| 2420 | Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion); |
| 2421 | mDirtyRegion.subtract(dirtyRegion); |
| 2422 | dirtyRegion = newDirtyRegion; |
| 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | mDirtyRegion.orSelf(newDirtyRegion); |
| 2427 | if (inOutDirtyBounds) { |
| 2428 | *inOutDirtyBounds = newDirtyRegion.getBounds(); |
| 2429 | } |
| 2430 | |
| 2431 | void* vaddr; |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 2432 | status_t res = backBuffer->lockAsync( |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2433 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 2434 | newDirtyRegion.bounds(), &vaddr, fenceFd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2435 | |
| 2436 | ALOGW_IF(res, "failed locking buffer (handle = %p)", |
| 2437 | backBuffer->handle); |
| 2438 | |
| 2439 | if (res != 0) { |
| 2440 | err = INVALID_OPERATION; |
| 2441 | } else { |
| 2442 | mLockedBuffer = backBuffer; |
| 2443 | outBuffer->width = backBuffer->width; |
| 2444 | outBuffer->height = backBuffer->height; |
| 2445 | outBuffer->stride = backBuffer->stride; |
| 2446 | outBuffer->format = backBuffer->format; |
| 2447 | outBuffer->bits = vaddr; |
| 2448 | } |
| 2449 | } |
| 2450 | return err; |
| 2451 | } |
| 2452 | |
| 2453 | status_t Surface::unlockAndPost() |
| 2454 | { |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 2455 | if (mLockedBuffer == nullptr) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2456 | ALOGE("Surface::unlockAndPost failed, no locked buffer"); |
| 2457 | return INVALID_OPERATION; |
| 2458 | } |
| 2459 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 2460 | int fd = -1; |
| 2461 | status_t err = mLockedBuffer->unlockAsync(&fd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2462 | ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle); |
| 2463 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 2464 | err = queueBuffer(mLockedBuffer.get(), fd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2465 | ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)", |
| 2466 | mLockedBuffer->handle, strerror(-err)); |
| 2467 | |
| 2468 | mPostedBuffer = mLockedBuffer; |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 2469 | mLockedBuffer = nullptr; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2470 | return err; |
| 2471 | } |
| 2472 | |
Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 2473 | bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) { |
| 2474 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | bc8c192 | 2016-07-01 14:15:41 -0700 | [diff] [blame] | 2475 | if (mNextFrameNumber > lastFrame) { |
Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 2476 | return true; |
| 2477 | } |
| 2478 | return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK; |
| 2479 | } |
| 2480 | |
Pablo Ceballos | 8e3e92b | 2016-06-27 17:56:53 -0700 | [diff] [blame] | 2481 | status_t Surface::getUniqueId(uint64_t* outId) const { |
| 2482 | Mutex::Autolock lock(mMutex); |
| 2483 | return mGraphicBufferProducer->getUniqueId(outId); |
| 2484 | } |
| 2485 | |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 2486 | int Surface::getConsumerUsage(uint64_t* outUsage) const { |
| 2487 | Mutex::Autolock lock(mMutex); |
| 2488 | return mGraphicBufferProducer->getConsumerUsage(outUsage); |
| 2489 | } |
| 2490 | |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 2491 | status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) { |
| 2492 | if (out == nullptr) { |
| 2493 | ALOGE("%s: out must not be null!", __FUNCTION__); |
| 2494 | return BAD_VALUE; |
| 2495 | } |
| 2496 | |
| 2497 | Mutex::Autolock lock(mMutex); |
| 2498 | *out = mRemovedBuffers; |
| 2499 | mRemovedBuffers.clear(); |
| 2500 | return OK; |
| 2501 | } |
| 2502 | |
Peiyong Lin | 0f6a412 | 2019-06-25 17:25:02 -0700 | [diff] [blame] | 2503 | status_t Surface::attachAndQueueBufferWithDataspace(Surface* surface, sp<GraphicBuffer> buffer, |
| 2504 | Dataspace dataspace) { |
Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2505 | if (buffer == nullptr) { |
| 2506 | return BAD_VALUE; |
| 2507 | } |
| 2508 | int err = static_cast<ANativeWindow*>(surface)->perform(surface, NATIVE_WINDOW_API_CONNECT, |
| 2509 | NATIVE_WINDOW_API_CPU); |
| 2510 | if (err != OK) { |
| 2511 | return err; |
| 2512 | } |
Peiyong Lin | 0f6a412 | 2019-06-25 17:25:02 -0700 | [diff] [blame] | 2513 | ui::Dataspace tmpDataspace = surface->getBuffersDataSpace(); |
| 2514 | err = surface->setBuffersDataSpace(dataspace); |
| 2515 | if (err != OK) { |
| 2516 | return err; |
| 2517 | } |
Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2518 | err = surface->attachBuffer(buffer->getNativeBuffer()); |
| 2519 | if (err != OK) { |
| 2520 | return err; |
| 2521 | } |
| 2522 | err = static_cast<ANativeWindow*>(surface)->queueBuffer(surface, buffer->getNativeBuffer(), -1); |
| 2523 | if (err != OK) { |
| 2524 | return err; |
| 2525 | } |
Peiyong Lin | 0f6a412 | 2019-06-25 17:25:02 -0700 | [diff] [blame] | 2526 | err = surface->setBuffersDataSpace(tmpDataspace); |
| 2527 | if (err != OK) { |
| 2528 | return err; |
| 2529 | } |
Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 2530 | err = surface->disconnect(NATIVE_WINDOW_API_CPU); |
| 2531 | return err; |
| 2532 | } |
| 2533 | |
Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 2534 | int Surface::setAutoPrerotation(bool autoPrerotation) { |
| 2535 | ATRACE_CALL(); |
| 2536 | ALOGV("Surface::setAutoPrerotation (%d)", autoPrerotation); |
| 2537 | Mutex::Autolock lock(mMutex); |
| 2538 | |
| 2539 | if (mAutoPrerotation == autoPrerotation) { |
| 2540 | return OK; |
| 2541 | } |
| 2542 | |
| 2543 | status_t err = mGraphicBufferProducer->setAutoPrerotation(autoPrerotation); |
| 2544 | if (err == NO_ERROR) { |
| 2545 | mAutoPrerotation = autoPrerotation; |
| 2546 | } |
| 2547 | ALOGE_IF(err, "IGraphicBufferProducer::setAutoPrerotation(%d) returned %s", autoPrerotation, |
| 2548 | strerror(-err)); |
| 2549 | return err; |
| 2550 | } |
| 2551 | |
Shuzhen Wang | 067fcd3 | 2019-08-14 10:41:12 -0700 | [diff] [blame] | 2552 | void Surface::ProducerListenerProxy::onBuffersDiscarded(const std::vector<int32_t>& slots) { |
| 2553 | ATRACE_CALL(); |
| 2554 | sp<Surface> parent = mParent.promote(); |
| 2555 | if (parent == nullptr) { |
| 2556 | return; |
| 2557 | } |
| 2558 | |
| 2559 | std::vector<sp<GraphicBuffer>> discardedBufs; |
| 2560 | status_t res = parent->getAndFlushBuffersFromSlots(slots, &discardedBufs); |
| 2561 | if (res != OK) { |
| 2562 | ALOGE("%s: Failed to get buffers from slots: %s(%d)", __FUNCTION__, |
| 2563 | strerror(-res), res); |
| 2564 | return; |
| 2565 | } |
| 2566 | |
| 2567 | mSurfaceListener->onBuffersDiscarded(discardedBufs); |
| 2568 | } |
| 2569 | |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 2570 | status_t Surface::setFrameRate(float frameRate, int8_t compatibility, bool shouldBeSeamless) { |
Steven Thomas | 3172e20 | 2020-01-06 19:25:30 -0800 | [diff] [blame] | 2571 | ATRACE_CALL(); |
Steven Thomas | 62a4cf8 | 2020-01-31 12:04:03 -0800 | [diff] [blame] | 2572 | ALOGV("Surface::setFrameRate"); |
| 2573 | |
| 2574 | if (!ValidateFrameRate(frameRate, compatibility, "Surface::setFrameRate")) { |
| 2575 | return BAD_VALUE; |
| 2576 | } |
| 2577 | |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 2578 | return composerService()->setFrameRate(mGraphicBufferProducer, frameRate, compatibility, |
| 2579 | shouldBeSeamless); |
Steven Thomas | 3172e20 | 2020-01-06 19:25:30 -0800 | [diff] [blame] | 2580 | } |
| 2581 | |
Robert Carr | 9b611b7 | 2020-10-19 12:00:23 -0700 | [diff] [blame] | 2582 | status_t Surface::setFrameTimelineVsync(int64_t frameTimelineVsyncId) { |
| 2583 | return composerService()->setFrameTimelineVsync(mGraphicBufferProducer, |
| 2584 | frameTimelineVsyncId); |
| 2585 | } |
| 2586 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2587 | }; // namespace android |