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 | |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 23 | #include <inttypes.h> |
| 24 | |
Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 25 | #include <android/native_window.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 26 | |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 27 | #include <utils/Log.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 28 | #include <utils/Trace.h> |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 29 | #include <utils/NativeHandle.h> |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 30 | |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 31 | #include <ui/DisplayStatInfo.h> |
Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 32 | #include <ui/Fence.h> |
| 33 | #include <ui/HdrCapabilities.h> |
| 34 | #include <ui/Region.h> |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 35 | |
Mathias Agopian | 2b5dd40 | 2017-02-07 17:36:19 -0800 | [diff] [blame] | 36 | #include <gui/BufferItem.h> |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 37 | #include <gui/IProducerListener.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 38 | |
Mathias Agopian | 2b5dd40 | 2017-02-07 17:36:19 -0800 | [diff] [blame] | 39 | #include <gui/ISurfaceComposer.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 40 | #include <private/gui/ComposerService.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 41 | |
Courtney Goeltzenleuchter | a0c93e1 | 2017-03-17 16:16:48 -0600 | [diff] [blame] | 42 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> |
| 43 | #include <configstore/Utils.h> |
| 44 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | |
Ian Elliott | a2eb34c | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 47 | Surface::Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp) |
| 48 | : mGraphicBufferProducer(bufferProducer), |
| 49 | mCrop(Rect::EMPTY_RECT), |
| 50 | mBufferAge(0), |
| 51 | mGenerationNumber(0), |
| 52 | mSharedBufferMode(false), |
| 53 | mAutoRefresh(false), |
| 54 | mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT), |
| 55 | mSharedBufferHasBeenQueued(false), |
| 56 | mQueriedSupportedTimestamps(false), |
| 57 | mFrameTimestampsSupportsPresent(false), |
| 58 | mEnableFrameTimestamps(false), |
| 59 | mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>()) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 60 | // Initialize the ANativeWindow function pointers. |
| 61 | ANativeWindow::setSwapInterval = hook_setSwapInterval; |
| 62 | ANativeWindow::dequeueBuffer = hook_dequeueBuffer; |
| 63 | ANativeWindow::cancelBuffer = hook_cancelBuffer; |
| 64 | ANativeWindow::queueBuffer = hook_queueBuffer; |
| 65 | ANativeWindow::query = hook_query; |
| 66 | ANativeWindow::perform = hook_perform; |
| 67 | |
| 68 | ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED; |
| 69 | ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED; |
| 70 | ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED; |
| 71 | ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED; |
| 72 | |
| 73 | const_cast<int&>(ANativeWindow::minSwapInterval) = 0; |
| 74 | const_cast<int&>(ANativeWindow::maxSwapInterval) = 1; |
| 75 | |
| 76 | mReqWidth = 0; |
| 77 | mReqHeight = 0; |
| 78 | mReqFormat = 0; |
| 79 | mReqUsage = 0; |
| 80 | mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 81 | mDataSpace = HAL_DATASPACE_UNKNOWN; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 82 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 83 | mTransform = 0; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 84 | mStickyTransform = 0; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 85 | mDefaultWidth = 0; |
| 86 | mDefaultHeight = 0; |
| 87 | mUserWidth = 0; |
| 88 | mUserHeight = 0; |
| 89 | mTransformHint = 0; |
| 90 | mConsumerRunningBehind = false; |
| 91 | mConnectedToCpu = false; |
Eino-Ville Talvala | 7895e90 | 2013-08-21 11:53:37 -0700 | [diff] [blame] | 92 | mProducerControlledByApp = controlledByApp; |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 93 | mSwapIntervalZero = false; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Mathias Agopian | 35ffa6a | 2013-03-12 18:45:09 -0700 | [diff] [blame] | 96 | Surface::~Surface() { |
| 97 | if (mConnectedToCpu) { |
| 98 | Surface::disconnect(NATIVE_WINDOW_API_CPU); |
| 99 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 102 | sp<ISurfaceComposer> Surface::composerService() const { |
| 103 | return ComposerService::getComposerService(); |
| 104 | } |
| 105 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 106 | nsecs_t Surface::now() const { |
| 107 | return systemTime(); |
| 108 | } |
| 109 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 110 | sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const { |
| 111 | return mGraphicBufferProducer; |
| 112 | } |
| 113 | |
Wonsik Kim | 0ee14ca | 2014-03-17 17:46:53 +0900 | [diff] [blame] | 114 | void Surface::setSidebandStream(const sp<NativeHandle>& stream) { |
| 115 | mGraphicBufferProducer->setSidebandStream(stream); |
| 116 | } |
| 117 | |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 118 | void Surface::allocateBuffers() { |
| 119 | uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth; |
| 120 | uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight; |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 121 | mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight, |
| 122 | mReqFormat, mReqUsage); |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 125 | status_t Surface::setGenerationNumber(uint32_t generation) { |
| 126 | status_t result = mGraphicBufferProducer->setGenerationNumber(generation); |
| 127 | if (result == NO_ERROR) { |
| 128 | mGenerationNumber = generation; |
| 129 | } |
| 130 | return result; |
| 131 | } |
| 132 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 133 | uint64_t Surface::getNextFrameNumber() const { |
Pablo Ceballos | bc8c192 | 2016-07-01 14:15:41 -0700 | [diff] [blame] | 134 | Mutex::Autolock lock(mMutex); |
| 135 | return mNextFrameNumber; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Dan Stoza | c6f30bd | 2015-06-08 09:32:50 -0700 | [diff] [blame] | 138 | String8 Surface::getConsumerName() const { |
| 139 | return mGraphicBufferProducer->getConsumerName(); |
| 140 | } |
| 141 | |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 142 | status_t Surface::setDequeueTimeout(nsecs_t timeout) { |
| 143 | return mGraphicBufferProducer->setDequeueTimeout(timeout); |
| 144 | } |
| 145 | |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 146 | status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, |
John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 147 | sp<Fence>* outFence, float outTransformMatrix[16]) { |
| 148 | return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence, |
| 149 | outTransformMatrix); |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 152 | status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) { |
| 153 | ATRACE_CALL(); |
| 154 | |
| 155 | DisplayStatInfo stats; |
| 156 | status_t err = composerService()->getDisplayStats(NULL, &stats); |
| 157 | |
| 158 | *outRefreshDuration = stats.vsyncPeriod; |
| 159 | |
| 160 | return NO_ERROR; |
| 161 | } |
| 162 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 163 | void Surface::enableFrameTimestamps(bool enable) { |
| 164 | Mutex::Autolock lock(mMutex); |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 165 | // If going from disabled to enabled, get the initial values for |
| 166 | // compositor and display timing. |
| 167 | if (!mEnableFrameTimestamps && enable) { |
| 168 | FrameEventHistoryDelta delta; |
| 169 | mGraphicBufferProducer->getFrameTimestamps(&delta); |
| 170 | mFrameEventHistory->applyDelta(delta); |
| 171 | } |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 172 | mEnableFrameTimestamps = enable; |
| 173 | } |
| 174 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 175 | status_t Surface::getCompositorTiming( |
| 176 | nsecs_t* compositeDeadline, nsecs_t* compositeInterval, |
| 177 | nsecs_t* compositeToPresentLatency) { |
| 178 | Mutex::Autolock lock(mMutex); |
| 179 | if (!mEnableFrameTimestamps) { |
| 180 | return INVALID_OPERATION; |
| 181 | } |
| 182 | |
| 183 | if (compositeDeadline != nullptr) { |
| 184 | *compositeDeadline = |
| 185 | mFrameEventHistory->getNextCompositeDeadline(now()); |
| 186 | } |
| 187 | if (compositeInterval != nullptr) { |
| 188 | *compositeInterval = mFrameEventHistory->getCompositeInterval(); |
| 189 | } |
| 190 | if (compositeToPresentLatency != nullptr) { |
| 191 | *compositeToPresentLatency = |
| 192 | mFrameEventHistory->getCompositeToPresentLatency(); |
| 193 | } |
| 194 | return NO_ERROR; |
| 195 | } |
| 196 | |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 197 | static bool checkConsumerForUpdates( |
| 198 | const FrameEvents* e, const uint64_t lastFrameNumber, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 199 | const nsecs_t* outLatchTime, |
| 200 | const nsecs_t* outFirstRefreshStartTime, |
| 201 | const nsecs_t* outLastRefreshStartTime, |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 202 | const nsecs_t* outGpuCompositionDoneTime, |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 203 | const nsecs_t* outDisplayPresentTime, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 204 | const nsecs_t* outDequeueReadyTime, |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 205 | const nsecs_t* outReleaseTime) { |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 206 | bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo(); |
| 207 | bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) && |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 208 | !e->hasFirstRefreshStartInfo(); |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 209 | bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) && |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 210 | !e->hasGpuCompositionDoneInfo(); |
| 211 | bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) && |
| 212 | !e->hasDisplayPresentInfo(); |
| 213 | |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 214 | // LastRefreshStart, DequeueReady, and Release are never available for the |
| 215 | // last frame. |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 216 | bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) && |
| 217 | !e->hasLastRefreshStartInfo() && |
| 218 | (e->frameNumber != lastFrameNumber); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 219 | bool checkForDequeueReady = (outDequeueReadyTime != nullptr) && |
| 220 | !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber); |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 221 | bool checkForRelease = (outReleaseTime != nullptr) && |
| 222 | !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber); |
| 223 | |
| 224 | // RequestedPresent and Acquire info are always available producer-side. |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 225 | return checkForLatch || checkForFirstRefreshStart || |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 226 | checkForLastRefreshStart || checkForGpuCompositionDone || |
Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 227 | checkForDisplayPresent || checkForDequeueReady || checkForRelease; |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 230 | static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) { |
| 231 | if (dst != nullptr) { |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 232 | // We always get valid timestamps for these eventually. |
| 233 | *dst = (src == FrameEvents::TIMESTAMP_PENDING) ? |
| 234 | NATIVE_WINDOW_TIMESTAMP_PENDING : src; |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 238 | static void getFrameTimestampFence(nsecs_t *dst, |
| 239 | const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) { |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 240 | if (dst != nullptr) { |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 241 | if (!fenceShouldBeKnown) { |
| 242 | *dst = NATIVE_WINDOW_TIMESTAMP_PENDING; |
| 243 | return; |
| 244 | } |
| 245 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 246 | nsecs_t signalTime = src->getSignalTime(); |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 247 | *dst = (signalTime == Fence::SIGNAL_TIME_PENDING) ? |
| 248 | NATIVE_WINDOW_TIMESTAMP_PENDING : |
| 249 | (signalTime == Fence::SIGNAL_TIME_INVALID) ? |
| 250 | NATIVE_WINDOW_TIMESTAMP_INVALID : |
| 251 | signalTime; |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 255 | status_t Surface::getFrameTimestamps(uint64_t frameNumber, |
Brian Anderson | dbd0ea8 | 2016-07-22 09:38:59 -0700 | [diff] [blame] | 256 | nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 257 | nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime, |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 258 | nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime, |
Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 259 | nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime, |
| 260 | nsecs_t* outReleaseTime) { |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 261 | ATRACE_CALL(); |
| 262 | |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 263 | Mutex::Autolock lock(mMutex); |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 264 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 265 | if (!mEnableFrameTimestamps) { |
| 266 | return INVALID_OPERATION; |
| 267 | } |
| 268 | |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 269 | // Verify the requested timestamps are supported. |
| 270 | querySupportedTimestampsLocked(); |
| 271 | if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) { |
| 272 | return BAD_VALUE; |
| 273 | } |
| 274 | |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 275 | FrameEvents* events = mFrameEventHistory->getFrame(frameNumber); |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 276 | if (events == nullptr) { |
| 277 | // If the entry isn't available in the producer, it's definitely not |
| 278 | // available in the consumer. |
| 279 | return NAME_NOT_FOUND; |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 282 | // Update our cache of events if the requested events are not available. |
| 283 | if (checkConsumerForUpdates(events, mLastFrameNumber, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 284 | outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime, |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 285 | outGpuCompositionDoneTime, outDisplayPresentTime, |
Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 286 | outDequeueReadyTime, outReleaseTime)) { |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 287 | FrameEventHistoryDelta delta; |
| 288 | mGraphicBufferProducer->getFrameTimestamps(&delta); |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 289 | mFrameEventHistory->applyDelta(delta); |
| 290 | events = mFrameEventHistory->getFrame(frameNumber); |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 293 | if (events == nullptr) { |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 294 | // The entry was available before the update, but was overwritten |
| 295 | // 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] | 296 | return NAME_NOT_FOUND; |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 297 | } |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 298 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 299 | getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 300 | getFrameTimestamp(outLatchTime, events->latchTime); |
| 301 | getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime); |
| 302 | getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime); |
| 303 | getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime); |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 304 | |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 305 | getFrameTimestampFence(outAcquireTime, events->acquireFence, |
| 306 | events->hasAcquireInfo()); |
| 307 | getFrameTimestampFence(outGpuCompositionDoneTime, |
| 308 | events->gpuCompositionDoneFence, |
| 309 | events->hasGpuCompositionDoneInfo()); |
| 310 | getFrameTimestampFence(outDisplayPresentTime, events->displayPresentFence, |
| 311 | events->hasDisplayPresentInfo()); |
| 312 | getFrameTimestampFence(outReleaseTime, events->releaseFence, |
| 313 | events->hasReleaseInfo()); |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 314 | |
| 315 | return NO_ERROR; |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Courtney Goeltzenleuchter | a0c93e1 | 2017-03-17 16:16:48 -0600 | [diff] [blame] | 318 | using namespace android::hardware::configstore; |
| 319 | using namespace android::hardware::configstore::V1_0; |
| 320 | |
Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 321 | status_t Surface::getWideColorSupport(bool* supported) { |
| 322 | ATRACE_CALL(); |
| 323 | |
| 324 | sp<IBinder> display( |
| 325 | composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 326 | Vector<android_color_mode_t> colorModes; |
| 327 | status_t err = |
| 328 | composerService()->getDisplayColorModes(display, &colorModes); |
| 329 | |
| 330 | if (err) |
| 331 | return err; |
| 332 | |
Courtney Goeltzenleuchter | a0c93e1 | 2017-03-17 16:16:48 -0600 | [diff] [blame] | 333 | bool wideColorBoardConfig = |
| 334 | getBool<ISurfaceFlingerConfigs, |
| 335 | &ISurfaceFlingerConfigs::hasWideColorDisplay>(false); |
| 336 | |
Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 337 | *supported = false; |
| 338 | for (android_color_mode_t colorMode : colorModes) { |
| 339 | switch (colorMode) { |
| 340 | case HAL_COLOR_MODE_DISPLAY_P3: |
| 341 | case HAL_COLOR_MODE_ADOBE_RGB: |
| 342 | case HAL_COLOR_MODE_DCI_P3: |
Courtney Goeltzenleuchter | a0c93e1 | 2017-03-17 16:16:48 -0600 | [diff] [blame] | 343 | if (wideColorBoardConfig) { |
| 344 | *supported = true; |
| 345 | } |
Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 346 | break; |
| 347 | default: |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | return NO_ERROR; |
| 353 | } |
| 354 | |
Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 355 | status_t Surface::getHdrSupport(bool* supported) { |
| 356 | ATRACE_CALL(); |
| 357 | |
| 358 | sp<IBinder> display( |
| 359 | composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 360 | HdrCapabilities hdrCapabilities; |
| 361 | status_t err = |
| 362 | composerService()->getHdrCapabilities(display, &hdrCapabilities); |
| 363 | |
| 364 | if (err) |
| 365 | return err; |
| 366 | |
| 367 | *supported = !hdrCapabilities.getSupportedHdrTypes().empty(); |
| 368 | |
| 369 | return NO_ERROR; |
| 370 | } |
| 371 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 372 | int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) { |
| 373 | Surface* c = getSelf(window); |
| 374 | return c->setSwapInterval(interval); |
| 375 | } |
| 376 | |
| 377 | int Surface::hook_dequeueBuffer(ANativeWindow* window, |
| 378 | ANativeWindowBuffer** buffer, int* fenceFd) { |
| 379 | Surface* c = getSelf(window); |
| 380 | return c->dequeueBuffer(buffer, fenceFd); |
| 381 | } |
| 382 | |
| 383 | int Surface::hook_cancelBuffer(ANativeWindow* window, |
| 384 | ANativeWindowBuffer* buffer, int fenceFd) { |
| 385 | Surface* c = getSelf(window); |
| 386 | return c->cancelBuffer(buffer, fenceFd); |
| 387 | } |
| 388 | |
| 389 | int Surface::hook_queueBuffer(ANativeWindow* window, |
| 390 | ANativeWindowBuffer* buffer, int fenceFd) { |
| 391 | Surface* c = getSelf(window); |
| 392 | return c->queueBuffer(buffer, fenceFd); |
| 393 | } |
| 394 | |
| 395 | int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, |
| 396 | ANativeWindowBuffer** buffer) { |
| 397 | Surface* c = getSelf(window); |
| 398 | ANativeWindowBuffer* buf; |
| 399 | int fenceFd = -1; |
| 400 | int result = c->dequeueBuffer(&buf, &fenceFd); |
Mike Stroyan | 87709c9 | 2016-06-03 12:43:26 -0600 | [diff] [blame] | 401 | if (result != OK) { |
| 402 | return result; |
| 403 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 404 | sp<Fence> fence(new Fence(fenceFd)); |
Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 405 | int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED"); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 406 | if (waitResult != OK) { |
| 407 | ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d", |
| 408 | waitResult); |
| 409 | c->cancelBuffer(buf, -1); |
| 410 | return waitResult; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 411 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 412 | *buffer = buf; |
| 413 | return result; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 416 | int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window, |
| 417 | ANativeWindowBuffer* buffer) { |
| 418 | Surface* c = getSelf(window); |
| 419 | return c->cancelBuffer(buffer, -1); |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 422 | int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window, |
| 423 | ANativeWindowBuffer* buffer) { |
| 424 | Surface* c = getSelf(window); |
| 425 | return c->lockBuffer_DEPRECATED(buffer); |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 428 | int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window, |
| 429 | ANativeWindowBuffer* buffer) { |
| 430 | Surface* c = getSelf(window); |
| 431 | return c->queueBuffer(buffer, -1); |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 432 | } |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 433 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 434 | int Surface::hook_query(const ANativeWindow* window, |
| 435 | int what, int* value) { |
| 436 | const Surface* c = getSelf(window); |
| 437 | return c->query(what, value); |
| 438 | } |
| 439 | |
| 440 | int Surface::hook_perform(ANativeWindow* window, int operation, ...) { |
| 441 | va_list args; |
| 442 | va_start(args, operation); |
| 443 | Surface* c = getSelf(window); |
Haixia Shi | d89c2bb | 2015-09-14 11:02:18 -0700 | [diff] [blame] | 444 | int result = c->perform(operation, args); |
| 445 | va_end(args); |
| 446 | return result; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | int Surface::setSwapInterval(int interval) { |
| 450 | ATRACE_CALL(); |
| 451 | // EGL specification states: |
| 452 | // interval is silently clamped to minimum and maximum implementation |
| 453 | // dependent values before being stored. |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 454 | |
| 455 | if (interval < minSwapInterval) |
| 456 | interval = minSwapInterval; |
| 457 | |
| 458 | if (interval > maxSwapInterval) |
| 459 | interval = maxSwapInterval; |
| 460 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 461 | mSwapIntervalZero = (interval == 0); |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 462 | mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 463 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 464 | return NO_ERROR; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 467 | int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 468 | ATRACE_CALL(); |
| 469 | ALOGV("Surface::dequeueBuffer"); |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 470 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 471 | uint32_t reqWidth; |
| 472 | uint32_t reqHeight; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 473 | PixelFormat reqFormat; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 474 | uint64_t reqUsage; |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 475 | bool enableFrameTimestamps; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 476 | |
| 477 | { |
| 478 | Mutex::Autolock lock(mMutex); |
Yin-Chia Yeh | 1f2af5c | 2017-05-11 16:54:04 -0700 | [diff] [blame] | 479 | if (mReportRemovedBuffers) { |
| 480 | mRemovedBuffers.clear(); |
| 481 | } |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 482 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 483 | reqWidth = mReqWidth ? mReqWidth : mUserWidth; |
| 484 | reqHeight = mReqHeight ? mReqHeight : mUserHeight; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 485 | |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 486 | reqFormat = mReqFormat; |
| 487 | reqUsage = mReqUsage; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 488 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 489 | enableFrameTimestamps = mEnableFrameTimestamps; |
| 490 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 491 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot != |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 492 | BufferItem::INVALID_BUFFER_SLOT) { |
| 493 | sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer); |
| 494 | if (gbuf != NULL) { |
| 495 | *buffer = gbuf.get(); |
| 496 | *fenceFd = -1; |
| 497 | return OK; |
| 498 | } |
| 499 | } |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 500 | } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer |
| 501 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 502 | int buf = -1; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 503 | sp<Fence> fence; |
Dan Stoza | 932f008 | 2017-05-31 13:50:16 -0700 | [diff] [blame] | 504 | nsecs_t startTime = systemTime(); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 505 | |
| 506 | FrameEventHistoryDelta frameTimestamps; |
Ian Elliott | a2eb34c | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 507 | status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, reqWidth, reqHeight, |
| 508 | reqFormat, reqUsage, &mBufferAge, |
| 509 | enableFrameTimestamps ? &frameTimestamps |
| 510 | : nullptr); |
Dan Stoza | 932f008 | 2017-05-31 13:50:16 -0700 | [diff] [blame] | 511 | mLastDequeueDuration = systemTime() - startTime; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 512 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 513 | if (result < 0) { |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 514 | ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer" |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 515 | "(%d, %d, %d, %#" PRIx64 ") failed: %d", |
| 516 | reqWidth, reqHeight, reqFormat, reqUsage, result); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 517 | return result; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 518 | } |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 519 | |
Dan Stoza | 90ce2a9 | 2017-05-01 16:31:53 -0700 | [diff] [blame] | 520 | if (buf < 0 || buf >= NUM_BUFFER_SLOTS) { |
| 521 | ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf); |
| 522 | android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging |
| 523 | return FAILED_TRANSACTION; |
| 524 | } |
| 525 | |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 526 | Mutex::Autolock lock(mMutex); |
| 527 | |
Dan Stoza | 932f008 | 2017-05-31 13:50:16 -0700 | [diff] [blame] | 528 | // Write this while holding the mutex |
| 529 | mLastDequeueStartTime = startTime; |
| 530 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 531 | sp<GraphicBuffer>& gbuf(mSlots[buf].buffer); |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 532 | |
| 533 | // this should never happen |
| 534 | ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf); |
| 535 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 536 | if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) { |
| 537 | freeAllBuffers(); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 538 | } |
Ted Bonkenburg | bd050ab | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 539 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 540 | if (enableFrameTimestamps) { |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 541 | mFrameEventHistory->applyDelta(frameTimestamps); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 544 | if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) { |
| 545 | if (mReportRemovedBuffers && (gbuf != nullptr)) { |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 546 | mRemovedBuffers.push_back(gbuf); |
| 547 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 548 | result = mGraphicBufferProducer->requestBuffer(buf, &gbuf); |
| 549 | if (result != NO_ERROR) { |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 550 | ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result); |
Jesse Hall | 9f5a1b6 | 2014-10-02 11:09:03 -0700 | [diff] [blame] | 551 | mGraphicBufferProducer->cancelBuffer(buf, fence); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 552 | return result; |
| 553 | } |
| 554 | } |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 555 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 556 | if (fence->isValid()) { |
| 557 | *fenceFd = fence->dup(); |
| 558 | if (*fenceFd == -1) { |
| 559 | ALOGE("dequeueBuffer: error duping fence: %d", errno); |
| 560 | // dup() should never fail; something is badly wrong. Soldier on |
| 561 | // and hope for the best; the worst that should happen is some |
| 562 | // visible corruption that lasts until the next frame. |
| 563 | } |
Ted Bonkenburg | e5d6eb8 | 2011-08-09 22:38:41 -0700 | [diff] [blame] | 564 | } else { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 565 | *fenceFd = -1; |
Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 566 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 567 | |
| 568 | *buffer = gbuf.get(); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 569 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 570 | if (mSharedBufferMode && mAutoRefresh) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 571 | mSharedBufferSlot = buf; |
| 572 | mSharedBufferHasBeenQueued = false; |
| 573 | } else if (mSharedBufferSlot == buf) { |
| 574 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 575 | mSharedBufferHasBeenQueued = false; |
| 576 | } |
| 577 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 578 | return OK; |
Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 581 | int Surface::cancelBuffer(android_native_buffer_t* buffer, |
| 582 | int fenceFd) { |
| 583 | ATRACE_CALL(); |
| 584 | ALOGV("Surface::cancelBuffer"); |
| 585 | Mutex::Autolock lock(mMutex); |
| 586 | int i = getSlotFromBufferLocked(buffer); |
| 587 | if (i < 0) { |
Taiju Tsuiki | 4d0cd3f | 2015-04-30 22:15:33 +0900 | [diff] [blame] | 588 | if (fenceFd >= 0) { |
| 589 | close(fenceFd); |
| 590 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 591 | return i; |
| 592 | } |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 593 | if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) { |
| 594 | if (fenceFd >= 0) { |
| 595 | close(fenceFd); |
| 596 | } |
| 597 | return OK; |
| 598 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 599 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); |
| 600 | mGraphicBufferProducer->cancelBuffer(i, fence); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 601 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 602 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 603 | mSharedBufferHasBeenQueued = true; |
| 604 | } |
| 605 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 606 | return OK; |
| 607 | } |
| 608 | |
| 609 | int Surface::getSlotFromBufferLocked( |
| 610 | android_native_buffer_t* buffer) const { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 611 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 612 | if (mSlots[i].buffer != NULL && |
| 613 | mSlots[i].buffer->handle == buffer->handle) { |
| 614 | return i; |
Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 615 | } |
| 616 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 617 | ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle); |
| 618 | return BAD_VALUE; |
Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 621 | int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 622 | ALOGV("Surface::lockBuffer"); |
| 623 | Mutex::Autolock lock(mMutex); |
| 624 | return OK; |
| 625 | } |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 626 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 627 | int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) { |
| 628 | ATRACE_CALL(); |
| 629 | ALOGV("Surface::queueBuffer"); |
| 630 | Mutex::Autolock lock(mMutex); |
| 631 | int64_t timestamp; |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 632 | bool isAutoTimestamp = false; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 633 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 634 | if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) { |
Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 635 | timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 636 | isAutoTimestamp = true; |
Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 637 | ALOGV("Surface::queueBuffer making up timestamp: %.2f ms", |
Colin Cross | 9a80d50 | 2016-09-27 14:12:48 -0700 | [diff] [blame] | 638 | timestamp / 1000000.0); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 639 | } else { |
| 640 | timestamp = mTimestamp; |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 641 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 642 | int i = getSlotFromBufferLocked(buffer); |
| 643 | if (i < 0) { |
Taiju Tsuiki | 4d0cd3f | 2015-04-30 22:15:33 +0900 | [diff] [blame] | 644 | if (fenceFd >= 0) { |
| 645 | close(fenceFd); |
| 646 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 647 | return i; |
| 648 | } |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 649 | if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) { |
| 650 | if (fenceFd >= 0) { |
| 651 | close(fenceFd); |
| 652 | } |
| 653 | return OK; |
| 654 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 657 | // Make sure the crop rectangle is entirely inside the buffer. |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 658 | Rect crop(Rect::EMPTY_RECT); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 659 | mCrop.intersect(Rect(buffer->width, buffer->height), &crop); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 661 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); |
| 662 | IGraphicBufferProducer::QueueBufferOutput output; |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 663 | IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp, |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 664 | mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform, |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 665 | fence, mStickyTransform, mEnableFrameTimestamps); |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 666 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 667 | if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) { |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 668 | input.setSurfaceDamage(Region::INVALID_REGION); |
| 669 | } else { |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 670 | // Here we do two things: |
| 671 | // 1) The surface damage was specified using the OpenGL ES convention of |
| 672 | // the origin being in the bottom-left corner. Here we flip to the |
| 673 | // convention that the rest of the system uses (top-left corner) by |
| 674 | // subtracting all top/bottom coordinates from the buffer height. |
| 675 | // 2) If the buffer is coming in rotated (for example, because the EGL |
| 676 | // implementation is reacting to the transform hint coming back from |
| 677 | // SurfaceFlinger), the surface damage needs to be rotated the |
| 678 | // opposite direction, since it was generated assuming an unrotated |
| 679 | // buffer (the app doesn't know that the EGL implementation is |
| 680 | // reacting to the transform hint behind its back). The |
| 681 | // transformations in the switch statement below apply those |
| 682 | // complementary rotations (e.g., if 90 degrees, rotate 270 degrees). |
| 683 | |
| 684 | int width = buffer->width; |
Dan Stoza | 0e65e6c | 2015-05-26 13:22:27 -0700 | [diff] [blame] | 685 | int height = buffer->height; |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 686 | bool rotated90 = (mTransform ^ mStickyTransform) & |
| 687 | NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 688 | if (rotated90) { |
| 689 | std::swap(width, height); |
Dan Stoza | 0e65e6c | 2015-05-26 13:22:27 -0700 | [diff] [blame] | 690 | } |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 691 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 692 | Region flippedRegion; |
| 693 | for (auto rect : mDirtyRegion) { |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 694 | int left = rect.left; |
| 695 | int right = rect.right; |
| 696 | int top = height - rect.bottom; // Flip from OpenGL convention |
| 697 | int bottom = height - rect.top; // Flip from OpenGL convention |
| 698 | switch (mTransform ^ mStickyTransform) { |
| 699 | case NATIVE_WINDOW_TRANSFORM_ROT_90: { |
| 700 | // Rotate 270 degrees |
| 701 | Rect flippedRect{top, width - right, bottom, width - left}; |
| 702 | flippedRegion.orSelf(flippedRect); |
| 703 | break; |
| 704 | } |
| 705 | case NATIVE_WINDOW_TRANSFORM_ROT_180: { |
| 706 | // Rotate 180 degrees |
| 707 | Rect flippedRect{width - right, height - bottom, |
| 708 | width - left, height - top}; |
| 709 | flippedRegion.orSelf(flippedRect); |
| 710 | break; |
| 711 | } |
| 712 | case NATIVE_WINDOW_TRANSFORM_ROT_270: { |
| 713 | // Rotate 90 degrees |
| 714 | Rect flippedRect{height - bottom, left, |
| 715 | height - top, right}; |
| 716 | flippedRegion.orSelf(flippedRect); |
| 717 | break; |
| 718 | } |
| 719 | default: { |
| 720 | Rect flippedRect{left, top, right, bottom}; |
| 721 | flippedRegion.orSelf(flippedRect); |
| 722 | break; |
| 723 | } |
| 724 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | input.setSurfaceDamage(flippedRegion); |
| 728 | } |
| 729 | |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 730 | nsecs_t now = systemTime(); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 731 | status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output); |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 732 | mLastQueueDuration = systemTime() - now; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 733 | if (err != OK) { |
| 734 | ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err); |
| 735 | } |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 736 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 737 | if (mEnableFrameTimestamps) { |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 738 | mFrameEventHistory->applyDelta(output.frameTimestamps); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 739 | // Update timestamps with the local acquire fence. |
| 740 | // The consumer doesn't send it back to prevent us from having two |
| 741 | // file descriptors of the same fence. |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 742 | mFrameEventHistory->updateAcquireFence(mNextFrameNumber, |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 743 | std::make_shared<FenceTime>(std::move(fence))); |
| 744 | |
| 745 | // Cache timestamps of signaled fences so we can close their file |
| 746 | // descriptors. |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 747 | mFrameEventHistory->updateSignalTimes(); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 750 | mLastFrameNumber = mNextFrameNumber; |
| 751 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 752 | mDefaultWidth = output.width; |
| 753 | mDefaultHeight = output.height; |
| 754 | mNextFrameNumber = output.nextFrameNumber; |
tedbo | 1e7fa9e | 2011-06-22 15:52:53 -0700 | [diff] [blame] | 755 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 756 | // Disable transform hint if sticky transform is set. |
| 757 | if (mStickyTransform == 0) { |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 758 | mTransformHint = output.transformHint; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 761 | mConsumerRunningBehind = (output.numPendingBuffers >= 2); |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 762 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 763 | if (!mConnectedToCpu) { |
| 764 | // Clear surface damage back to full-buffer |
| 765 | mDirtyRegion = Region::INVALID_REGION; |
| 766 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 767 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 768 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 769 | mSharedBufferHasBeenQueued = true; |
| 770 | } |
| 771 | |
Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 772 | mQueueBufferCondition.broadcast(); |
| 773 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 774 | return err; |
| 775 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 776 | |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 777 | void Surface::querySupportedTimestampsLocked() const { |
| 778 | // mMutex must be locked when calling this method. |
| 779 | |
| 780 | if (mQueriedSupportedTimestamps) { |
| 781 | return; |
| 782 | } |
| 783 | mQueriedSupportedTimestamps = true; |
| 784 | |
| 785 | std::vector<FrameEvent> supportedFrameTimestamps; |
| 786 | status_t err = composerService()->getSupportedFrameTimestamps( |
| 787 | &supportedFrameTimestamps); |
| 788 | |
| 789 | if (err != NO_ERROR) { |
| 790 | return; |
| 791 | } |
| 792 | |
| 793 | for (auto sft : supportedFrameTimestamps) { |
| 794 | if (sft == FrameEvent::DISPLAY_PRESENT) { |
| 795 | mFrameTimestampsSupportsPresent = true; |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 800 | int Surface::query(int what, int* value) const { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 801 | ATRACE_CALL(); |
| 802 | ALOGV("Surface::query"); |
| 803 | { // scope for the lock |
| 804 | Mutex::Autolock lock(mMutex); |
| 805 | switch (what) { |
| 806 | case NATIVE_WINDOW_FORMAT: |
| 807 | if (mReqFormat) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 808 | *value = static_cast<int>(mReqFormat); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 809 | return NO_ERROR; |
| 810 | } |
| 811 | break; |
| 812 | case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: { |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 813 | if (composerService()->authenticateSurfaceTexture( |
| 814 | mGraphicBufferProducer)) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 815 | *value = 1; |
| 816 | } else { |
| 817 | *value = 0; |
| 818 | } |
| 819 | return NO_ERROR; |
| 820 | } |
| 821 | case NATIVE_WINDOW_CONCRETE_TYPE: |
| 822 | *value = NATIVE_WINDOW_SURFACE; |
| 823 | return NO_ERROR; |
| 824 | case NATIVE_WINDOW_DEFAULT_WIDTH: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 825 | *value = static_cast<int>( |
| 826 | mUserWidth ? mUserWidth : mDefaultWidth); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 827 | return NO_ERROR; |
| 828 | case NATIVE_WINDOW_DEFAULT_HEIGHT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 829 | *value = static_cast<int>( |
| 830 | mUserHeight ? mUserHeight : mDefaultHeight); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 831 | return NO_ERROR; |
| 832 | case NATIVE_WINDOW_TRANSFORM_HINT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 833 | *value = static_cast<int>(mTransformHint); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 834 | return NO_ERROR; |
| 835 | case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: { |
| 836 | status_t err = NO_ERROR; |
| 837 | if (!mConsumerRunningBehind) { |
| 838 | *value = 0; |
| 839 | } else { |
| 840 | err = mGraphicBufferProducer->query(what, value); |
| 841 | if (err == NO_ERROR) { |
| 842 | mConsumerRunningBehind = *value; |
| 843 | } |
| 844 | } |
| 845 | return err; |
| 846 | } |
Ian Elliott | a2eb34c | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 847 | case NATIVE_WINDOW_BUFFER_AGE: { |
| 848 | if (mBufferAge > INT32_MAX) { |
| 849 | *value = 0; |
| 850 | } else { |
| 851 | *value = static_cast<int32_t>(mBufferAge); |
| 852 | } |
| 853 | return NO_ERROR; |
| 854 | } |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 855 | case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: { |
| 856 | int64_t durationUs = mLastDequeueDuration / 1000; |
| 857 | *value = durationUs > std::numeric_limits<int>::max() ? |
| 858 | std::numeric_limits<int>::max() : |
| 859 | static_cast<int>(durationUs); |
| 860 | return NO_ERROR; |
| 861 | } |
| 862 | case NATIVE_WINDOW_LAST_QUEUE_DURATION: { |
| 863 | int64_t durationUs = mLastQueueDuration / 1000; |
| 864 | *value = durationUs > std::numeric_limits<int>::max() ? |
| 865 | std::numeric_limits<int>::max() : |
| 866 | static_cast<int>(durationUs); |
| 867 | return NO_ERROR; |
| 868 | } |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 869 | case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: { |
| 870 | querySupportedTimestampsLocked(); |
| 871 | *value = mFrameTimestampsSupportsPresent ? 1 : 0; |
| 872 | return NO_ERROR; |
| 873 | } |
Mathias Agopian | 10e9ab5 | 2017-03-08 15:02:55 -0800 | [diff] [blame] | 874 | case NATIVE_WINDOW_IS_VALID: { |
| 875 | *value = mGraphicBufferProducer != nullptr ? 1 : 0; |
| 876 | return NO_ERROR; |
| 877 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 878 | } |
Jamie Gennis | 391bbe2 | 2011-03-14 15:00:06 -0700 | [diff] [blame] | 879 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 880 | return mGraphicBufferProducer->query(what, value); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 881 | } |
| 882 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 883 | int Surface::perform(int operation, va_list args) |
| 884 | { |
| 885 | int res = NO_ERROR; |
| 886 | switch (operation) { |
| 887 | case NATIVE_WINDOW_CONNECT: |
| 888 | // deprecated. must return NO_ERROR. |
| 889 | break; |
| 890 | case NATIVE_WINDOW_DISCONNECT: |
| 891 | // deprecated. must return NO_ERROR. |
| 892 | break; |
| 893 | case NATIVE_WINDOW_SET_USAGE: |
| 894 | res = dispatchSetUsage(args); |
| 895 | break; |
| 896 | case NATIVE_WINDOW_SET_CROP: |
| 897 | res = dispatchSetCrop(args); |
| 898 | break; |
| 899 | case NATIVE_WINDOW_SET_BUFFER_COUNT: |
| 900 | res = dispatchSetBufferCount(args); |
| 901 | break; |
| 902 | case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: |
| 903 | res = dispatchSetBuffersGeometry(args); |
| 904 | break; |
| 905 | case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: |
| 906 | res = dispatchSetBuffersTransform(args); |
| 907 | break; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 908 | case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM: |
| 909 | res = dispatchSetBuffersStickyTransform(args); |
| 910 | break; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 911 | case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP: |
| 912 | res = dispatchSetBuffersTimestamp(args); |
| 913 | break; |
| 914 | case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS: |
| 915 | res = dispatchSetBuffersDimensions(args); |
| 916 | break; |
| 917 | case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS: |
| 918 | res = dispatchSetBuffersUserDimensions(args); |
| 919 | break; |
| 920 | case NATIVE_WINDOW_SET_BUFFERS_FORMAT: |
| 921 | res = dispatchSetBuffersFormat(args); |
| 922 | break; |
| 923 | case NATIVE_WINDOW_LOCK: |
| 924 | res = dispatchLock(args); |
| 925 | break; |
| 926 | case NATIVE_WINDOW_UNLOCK_AND_POST: |
| 927 | res = dispatchUnlockAndPost(args); |
| 928 | break; |
| 929 | case NATIVE_WINDOW_SET_SCALING_MODE: |
| 930 | res = dispatchSetScalingMode(args); |
| 931 | break; |
| 932 | case NATIVE_WINDOW_API_CONNECT: |
| 933 | res = dispatchConnect(args); |
| 934 | break; |
| 935 | case NATIVE_WINDOW_API_DISCONNECT: |
| 936 | res = dispatchDisconnect(args); |
| 937 | break; |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 938 | case NATIVE_WINDOW_SET_SIDEBAND_STREAM: |
| 939 | res = dispatchSetSidebandStream(args); |
| 940 | break; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 941 | case NATIVE_WINDOW_SET_BUFFERS_DATASPACE: |
| 942 | res = dispatchSetBuffersDataSpace(args); |
| 943 | break; |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 944 | case NATIVE_WINDOW_SET_SURFACE_DAMAGE: |
| 945 | res = dispatchSetSurfaceDamage(args); |
| 946 | break; |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 947 | case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE: |
| 948 | res = dispatchSetSharedBufferMode(args); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 949 | break; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 950 | case NATIVE_WINDOW_SET_AUTO_REFRESH: |
| 951 | res = dispatchSetAutoRefresh(args); |
| 952 | break; |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 953 | case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION: |
| 954 | res = dispatchGetDisplayRefreshCycleDuration(args); |
| 955 | break; |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 956 | case NATIVE_WINDOW_GET_NEXT_FRAME_ID: |
| 957 | res = dispatchGetNextFrameId(args); |
| 958 | break; |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 959 | case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS: |
| 960 | res = dispatchEnableFrameTimestamps(args); |
| 961 | break; |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 962 | case NATIVE_WINDOW_GET_COMPOSITOR_TIMING: |
| 963 | res = dispatchGetCompositorTiming(args); |
| 964 | break; |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 965 | case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS: |
| 966 | res = dispatchGetFrameTimestamps(args); |
| 967 | break; |
Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 968 | case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT: |
| 969 | res = dispatchGetWideColorSupport(args); |
| 970 | break; |
Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 971 | case NATIVE_WINDOW_GET_HDR_SUPPORT: |
| 972 | res = dispatchGetHdrSupport(args); |
| 973 | break; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 974 | case NATIVE_WINDOW_SET_USAGE64: |
| 975 | res = dispatchSetUsage64(args); |
| 976 | break; |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame^] | 977 | case NATIVE_WINDOW_GET_CONSUMER_USAGE64: |
| 978 | res = dispatchGetConsumerUsage64(args); |
| 979 | break; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 980 | default: |
| 981 | res = NAME_NOT_FOUND; |
| 982 | break; |
| 983 | } |
| 984 | return res; |
| 985 | } |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 986 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 987 | int Surface::dispatchConnect(va_list args) { |
| 988 | int api = va_arg(args, int); |
| 989 | return connect(api); |
| 990 | } |
Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 991 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 992 | int Surface::dispatchDisconnect(va_list args) { |
| 993 | int api = va_arg(args, int); |
| 994 | return disconnect(api); |
| 995 | } |
| 996 | |
| 997 | int Surface::dispatchSetUsage(va_list args) { |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 998 | uint64_t usage = va_arg(args, uint32_t); |
| 999 | return setUsage(usage); |
| 1000 | } |
| 1001 | |
| 1002 | int Surface::dispatchSetUsage64(va_list args) { |
| 1003 | uint64_t usage = va_arg(args, uint64_t); |
| 1004 | return setUsage(usage); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | int Surface::dispatchSetCrop(va_list args) { |
| 1008 | android_native_rect_t const* rect = va_arg(args, android_native_rect_t*); |
| 1009 | return setCrop(reinterpret_cast<Rect const*>(rect)); |
| 1010 | } |
| 1011 | |
| 1012 | int Surface::dispatchSetBufferCount(va_list args) { |
| 1013 | size_t bufferCount = va_arg(args, size_t); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1014 | return setBufferCount(static_cast<int32_t>(bufferCount)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | int Surface::dispatchSetBuffersGeometry(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1018 | uint32_t width = va_arg(args, uint32_t); |
| 1019 | uint32_t height = va_arg(args, uint32_t); |
| 1020 | PixelFormat format = va_arg(args, PixelFormat); |
| 1021 | int err = setBuffersDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1022 | if (err != 0) { |
| 1023 | return err; |
| 1024 | } |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1025 | return setBuffersFormat(format); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | int Surface::dispatchSetBuffersDimensions(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1029 | uint32_t width = va_arg(args, uint32_t); |
| 1030 | uint32_t height = va_arg(args, uint32_t); |
| 1031 | return setBuffersDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | int Surface::dispatchSetBuffersUserDimensions(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1035 | uint32_t width = va_arg(args, uint32_t); |
| 1036 | uint32_t height = va_arg(args, uint32_t); |
| 1037 | return setBuffersUserDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | int Surface::dispatchSetBuffersFormat(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1041 | PixelFormat format = va_arg(args, PixelFormat); |
| 1042 | return setBuffersFormat(format); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | int Surface::dispatchSetScalingMode(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1046 | int mode = va_arg(args, int); |
| 1047 | return setScalingMode(mode); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | int Surface::dispatchSetBuffersTransform(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1051 | uint32_t transform = va_arg(args, uint32_t); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1052 | return setBuffersTransform(transform); |
| 1053 | } |
| 1054 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1055 | int Surface::dispatchSetBuffersStickyTransform(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1056 | uint32_t transform = va_arg(args, uint32_t); |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1057 | return setBuffersStickyTransform(transform); |
| 1058 | } |
| 1059 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1060 | int Surface::dispatchSetBuffersTimestamp(va_list args) { |
| 1061 | int64_t timestamp = va_arg(args, int64_t); |
| 1062 | return setBuffersTimestamp(timestamp); |
| 1063 | } |
| 1064 | |
| 1065 | int Surface::dispatchLock(va_list args) { |
| 1066 | ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*); |
| 1067 | ARect* inOutDirtyBounds = va_arg(args, ARect*); |
| 1068 | return lock(outBuffer, inOutDirtyBounds); |
| 1069 | } |
| 1070 | |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 1071 | int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1072 | return unlockAndPost(); |
| 1073 | } |
| 1074 | |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 1075 | int Surface::dispatchSetSidebandStream(va_list args) { |
| 1076 | native_handle_t* sH = va_arg(args, native_handle_t*); |
| 1077 | sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false); |
| 1078 | setSidebandStream(sidebandHandle); |
| 1079 | return OK; |
| 1080 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1081 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1082 | int Surface::dispatchSetBuffersDataSpace(va_list args) { |
| 1083 | android_dataspace dataspace = |
| 1084 | static_cast<android_dataspace>(va_arg(args, int)); |
| 1085 | return setBuffersDataSpace(dataspace); |
| 1086 | } |
| 1087 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1088 | int Surface::dispatchSetSurfaceDamage(va_list args) { |
| 1089 | android_native_rect_t* rects = va_arg(args, android_native_rect_t*); |
| 1090 | size_t numRects = va_arg(args, size_t); |
| 1091 | setSurfaceDamage(rects, numRects); |
| 1092 | return NO_ERROR; |
| 1093 | } |
| 1094 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1095 | int Surface::dispatchSetSharedBufferMode(va_list args) { |
| 1096 | bool sharedBufferMode = va_arg(args, int); |
| 1097 | return setSharedBufferMode(sharedBufferMode); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | int Surface::dispatchSetAutoRefresh(va_list args) { |
| 1101 | bool autoRefresh = va_arg(args, int); |
| 1102 | return setAutoRefresh(autoRefresh); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1105 | int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) { |
| 1106 | nsecs_t* outRefreshDuration = va_arg(args, int64_t*); |
| 1107 | return getDisplayRefreshCycleDuration(outRefreshDuration); |
| 1108 | } |
| 1109 | |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1110 | int Surface::dispatchGetNextFrameId(va_list args) { |
| 1111 | uint64_t* nextFrameId = va_arg(args, uint64_t*); |
| 1112 | *nextFrameId = getNextFrameNumber(); |
| 1113 | return NO_ERROR; |
| 1114 | } |
| 1115 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1116 | int Surface::dispatchEnableFrameTimestamps(va_list args) { |
| 1117 | bool enable = va_arg(args, int); |
| 1118 | enableFrameTimestamps(enable); |
| 1119 | return NO_ERROR; |
| 1120 | } |
| 1121 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1122 | int Surface::dispatchGetCompositorTiming(va_list args) { |
| 1123 | nsecs_t* compositeDeadline = va_arg(args, int64_t*); |
| 1124 | nsecs_t* compositeInterval = va_arg(args, int64_t*); |
| 1125 | nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*); |
| 1126 | return getCompositorTiming(compositeDeadline, compositeInterval, |
| 1127 | compositeToPresentLatency); |
| 1128 | } |
| 1129 | |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1130 | int Surface::dispatchGetFrameTimestamps(va_list args) { |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1131 | uint64_t frameId = va_arg(args, uint64_t); |
Brian Anderson | dbd0ea8 | 2016-07-22 09:38:59 -0700 | [diff] [blame] | 1132 | nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1133 | nsecs_t* outAcquireTime = va_arg(args, int64_t*); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1134 | nsecs_t* outLatchTime = va_arg(args, int64_t*); |
| 1135 | nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*); |
| 1136 | nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*); |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 1137 | nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*); |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 1138 | nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1139 | nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1140 | nsecs_t* outReleaseTime = va_arg(args, int64_t*); |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1141 | return getFrameTimestamps(frameId, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1142 | outRequestedPresentTime, outAcquireTime, outLatchTime, |
| 1143 | outFirstRefreshStartTime, outLastRefreshStartTime, |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 1144 | outGpuCompositionDoneTime, outDisplayPresentTime, |
Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 1145 | outDequeueReadyTime, outReleaseTime); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 1148 | int Surface::dispatchGetWideColorSupport(va_list args) { |
| 1149 | bool* outSupport = va_arg(args, bool*); |
| 1150 | return getWideColorSupport(outSupport); |
| 1151 | } |
| 1152 | |
Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 1153 | int Surface::dispatchGetHdrSupport(va_list args) { |
| 1154 | bool* outSupport = va_arg(args, bool*); |
| 1155 | return getHdrSupport(outSupport); |
| 1156 | } |
| 1157 | |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame^] | 1158 | int Surface::dispatchGetConsumerUsage64(va_list args) { |
| 1159 | uint64_t* usage = va_arg(args, uint64_t*); |
| 1160 | return getConsumerUsage(usage); |
| 1161 | } |
| 1162 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1163 | int Surface::connect(int api) { |
Dan Stoza | 966b98b | 2015-03-02 22:12:37 -0800 | [diff] [blame] | 1164 | static sp<IProducerListener> listener = new DummyProducerListener(); |
| 1165 | return connect(api, listener); |
| 1166 | } |
| 1167 | |
| 1168 | int Surface::connect(int api, const sp<IProducerListener>& listener) { |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1169 | return connect(api, listener, false); |
| 1170 | } |
| 1171 | |
| 1172 | int Surface::connect( |
| 1173 | int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1174 | ATRACE_CALL(); |
| 1175 | ALOGV("Surface::connect"); |
| 1176 | Mutex::Autolock lock(mMutex); |
| 1177 | IGraphicBufferProducer::QueueBufferOutput output; |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1178 | mReportRemovedBuffers = reportBufferRemoval; |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1179 | int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1180 | if (err == NO_ERROR) { |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1181 | mDefaultWidth = output.width; |
| 1182 | mDefaultHeight = output.height; |
| 1183 | mNextFrameNumber = output.nextFrameNumber; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1184 | |
| 1185 | // Disable transform hint if sticky transform is set. |
| 1186 | if (mStickyTransform == 0) { |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1187 | mTransformHint = output.transformHint; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1190 | mConsumerRunningBehind = (output.numPendingBuffers >= 2); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1191 | } |
| 1192 | if (!err && api == NATIVE_WINDOW_API_CPU) { |
| 1193 | mConnectedToCpu = true; |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1194 | // Clear the dirty region in case we're switching from a non-CPU API |
| 1195 | mDirtyRegion.clear(); |
| 1196 | } else if (!err) { |
| 1197 | // Initialize the dirty region for tracking surface damage |
| 1198 | mDirtyRegion = Region::INVALID_REGION; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1199 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1200 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1201 | return err; |
| 1202 | } |
| 1203 | |
Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 1204 | |
Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1205 | int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1206 | ATRACE_CALL(); |
| 1207 | ALOGV("Surface::disconnect"); |
| 1208 | Mutex::Autolock lock(mMutex); |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1209 | mRemovedBuffers.clear(); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1210 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1211 | mSharedBufferHasBeenQueued = false; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1212 | freeAllBuffers(); |
Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1213 | int err = mGraphicBufferProducer->disconnect(api, mode); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1214 | if (!err) { |
| 1215 | mReqFormat = 0; |
| 1216 | mReqWidth = 0; |
| 1217 | mReqHeight = 0; |
| 1218 | mReqUsage = 0; |
| 1219 | mCrop.clear(); |
| 1220 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 1221 | mTransform = 0; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1222 | mStickyTransform = 0; |
| 1223 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1224 | if (api == NATIVE_WINDOW_API_CPU) { |
| 1225 | mConnectedToCpu = false; |
| 1226 | } |
| 1227 | } |
| 1228 | return err; |
| 1229 | } |
| 1230 | |
Dan Stoza | d9c4971 | 2015-04-27 11:06:01 -0700 | [diff] [blame] | 1231 | int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer, |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1232 | sp<Fence>* outFence) { |
| 1233 | ATRACE_CALL(); |
| 1234 | ALOGV("Surface::detachNextBuffer"); |
| 1235 | |
| 1236 | if (outBuffer == NULL || outFence == NULL) { |
| 1237 | return BAD_VALUE; |
| 1238 | } |
| 1239 | |
| 1240 | Mutex::Autolock lock(mMutex); |
Yin-Chia Yeh | 1f2af5c | 2017-05-11 16:54:04 -0700 | [diff] [blame] | 1241 | if (mReportRemovedBuffers) { |
| 1242 | mRemovedBuffers.clear(); |
| 1243 | } |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1244 | |
| 1245 | sp<GraphicBuffer> buffer(NULL); |
| 1246 | sp<Fence> fence(NULL); |
| 1247 | status_t result = mGraphicBufferProducer->detachNextBuffer( |
| 1248 | &buffer, &fence); |
| 1249 | if (result != NO_ERROR) { |
| 1250 | return result; |
| 1251 | } |
| 1252 | |
Dan Stoza | d9c4971 | 2015-04-27 11:06:01 -0700 | [diff] [blame] | 1253 | *outBuffer = buffer; |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1254 | if (fence != NULL && fence->isValid()) { |
| 1255 | *outFence = fence; |
| 1256 | } else { |
| 1257 | *outFence = Fence::NO_FENCE; |
| 1258 | } |
| 1259 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1260 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 1261 | if (mSlots[i].buffer != NULL && |
Shuzhen Wang | 55be505 | 2017-05-23 09:41:54 -0700 | [diff] [blame] | 1262 | mSlots[i].buffer->getId() == buffer->getId()) { |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1263 | if (mReportRemovedBuffers) { |
| 1264 | mRemovedBuffers.push_back(mSlots[i].buffer); |
| 1265 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1266 | mSlots[i].buffer = NULL; |
| 1267 | } |
| 1268 | } |
| 1269 | |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1270 | return NO_ERROR; |
| 1271 | } |
| 1272 | |
| 1273 | int Surface::attachBuffer(ANativeWindowBuffer* buffer) |
| 1274 | { |
| 1275 | ATRACE_CALL(); |
| 1276 | ALOGV("Surface::attachBuffer"); |
| 1277 | |
| 1278 | Mutex::Autolock lock(mMutex); |
Yin-Chia Yeh | 1f2af5c | 2017-05-11 16:54:04 -0700 | [diff] [blame] | 1279 | if (mReportRemovedBuffers) { |
| 1280 | mRemovedBuffers.clear(); |
| 1281 | } |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1282 | |
| 1283 | sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer)); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1284 | uint32_t priorGeneration = graphicBuffer->mGenerationNumber; |
| 1285 | graphicBuffer->mGenerationNumber = mGenerationNumber; |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1286 | int32_t attachedSlot = -1; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1287 | status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer); |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1288 | if (result != NO_ERROR) { |
| 1289 | ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1290 | graphicBuffer->mGenerationNumber = priorGeneration; |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1291 | return result; |
| 1292 | } |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1293 | if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) { |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1294 | mRemovedBuffers.push_back(mSlots[attachedSlot].buffer); |
| 1295 | } |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1296 | mSlots[attachedSlot].buffer = graphicBuffer; |
| 1297 | |
| 1298 | return NO_ERROR; |
| 1299 | } |
| 1300 | |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1301 | int Surface::setUsage(uint64_t reqUsage) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1302 | { |
| 1303 | ALOGV("Surface::setUsage"); |
| 1304 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1305 | if (reqUsage != mReqUsage) { |
| 1306 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1307 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1308 | mReqUsage = reqUsage; |
| 1309 | return OK; |
| 1310 | } |
| 1311 | |
| 1312 | int Surface::setCrop(Rect const* rect) |
| 1313 | { |
| 1314 | ATRACE_CALL(); |
| 1315 | |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 1316 | Rect realRect(Rect::EMPTY_RECT); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1317 | if (rect == NULL || rect->isEmpty()) { |
| 1318 | realRect.clear(); |
| 1319 | } else { |
| 1320 | realRect = *rect; |
Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1323 | ALOGV("Surface::setCrop rect=[%d %d %d %d]", |
| 1324 | realRect.left, realRect.top, realRect.right, realRect.bottom); |
| 1325 | |
| 1326 | Mutex::Autolock lock(mMutex); |
| 1327 | mCrop = realRect; |
| 1328 | return NO_ERROR; |
| 1329 | } |
| 1330 | |
| 1331 | int Surface::setBufferCount(int bufferCount) |
| 1332 | { |
| 1333 | ATRACE_CALL(); |
| 1334 | ALOGV("Surface::setBufferCount"); |
| 1335 | Mutex::Autolock lock(mMutex); |
| 1336 | |
Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 1337 | status_t err = NO_ERROR; |
| 1338 | if (bufferCount == 0) { |
| 1339 | err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1); |
| 1340 | } else { |
| 1341 | int minUndequeuedBuffers = 0; |
| 1342 | err = mGraphicBufferProducer->query( |
| 1343 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers); |
| 1344 | if (err == NO_ERROR) { |
| 1345 | err = mGraphicBufferProducer->setMaxDequeuedBufferCount( |
| 1346 | bufferCount - minUndequeuedBuffers); |
| 1347 | } |
| 1348 | } |
Mathias Agopian | 9014726 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 1349 | |
Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 1350 | ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s", |
| 1351 | bufferCount, strerror(-err)); |
| 1352 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1353 | return err; |
| 1354 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1355 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 1356 | int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) { |
| 1357 | ATRACE_CALL(); |
| 1358 | ALOGV("Surface::setMaxDequeuedBufferCount"); |
| 1359 | Mutex::Autolock lock(mMutex); |
| 1360 | |
| 1361 | status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount( |
| 1362 | maxDequeuedBuffers); |
| 1363 | ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) " |
| 1364 | "returned %s", maxDequeuedBuffers, strerror(-err)); |
| 1365 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 1366 | return err; |
| 1367 | } |
| 1368 | |
| 1369 | int Surface::setAsyncMode(bool async) { |
| 1370 | ATRACE_CALL(); |
| 1371 | ALOGV("Surface::setAsyncMode"); |
| 1372 | Mutex::Autolock lock(mMutex); |
| 1373 | |
| 1374 | status_t err = mGraphicBufferProducer->setAsyncMode(async); |
| 1375 | ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s", |
| 1376 | async, strerror(-err)); |
| 1377 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 1378 | return err; |
| 1379 | } |
| 1380 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1381 | int Surface::setSharedBufferMode(bool sharedBufferMode) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1382 | ATRACE_CALL(); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1383 | ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1384 | Mutex::Autolock lock(mMutex); |
| 1385 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1386 | status_t err = mGraphicBufferProducer->setSharedBufferMode( |
| 1387 | sharedBufferMode); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1388 | if (err == NO_ERROR) { |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1389 | mSharedBufferMode = sharedBufferMode; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1390 | } |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1391 | ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned" |
| 1392 | "%s", sharedBufferMode, strerror(-err)); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1393 | |
| 1394 | return err; |
| 1395 | } |
| 1396 | |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1397 | int Surface::setAutoRefresh(bool autoRefresh) { |
| 1398 | ATRACE_CALL(); |
| 1399 | ALOGV("Surface::setAutoRefresh (%d)", autoRefresh); |
| 1400 | Mutex::Autolock lock(mMutex); |
| 1401 | |
| 1402 | status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh); |
| 1403 | if (err == NO_ERROR) { |
| 1404 | mAutoRefresh = autoRefresh; |
| 1405 | } |
| 1406 | ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s", |
| 1407 | autoRefresh, strerror(-err)); |
| 1408 | return err; |
| 1409 | } |
| 1410 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1411 | int Surface::setBuffersDimensions(uint32_t width, uint32_t height) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1412 | { |
| 1413 | ATRACE_CALL(); |
| 1414 | ALOGV("Surface::setBuffersDimensions"); |
| 1415 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1416 | if ((width && !height) || (!width && height)) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1417 | return BAD_VALUE; |
| 1418 | |
| 1419 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1420 | if (width != mReqWidth || height != mReqHeight) { |
| 1421 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1422 | } |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1423 | mReqWidth = width; |
| 1424 | mReqHeight = height; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1425 | return NO_ERROR; |
| 1426 | } |
| 1427 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1428 | int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1429 | { |
| 1430 | ATRACE_CALL(); |
| 1431 | ALOGV("Surface::setBuffersUserDimensions"); |
| 1432 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1433 | if ((width && !height) || (!width && height)) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1434 | return BAD_VALUE; |
| 1435 | |
| 1436 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1437 | if (width != mUserWidth || height != mUserHeight) { |
| 1438 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1439 | } |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1440 | mUserWidth = width; |
| 1441 | mUserHeight = height; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1442 | return NO_ERROR; |
| 1443 | } |
| 1444 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1445 | int Surface::setBuffersFormat(PixelFormat format) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1446 | { |
| 1447 | ALOGV("Surface::setBuffersFormat"); |
| 1448 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1449 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1450 | if (format != mReqFormat) { |
| 1451 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1452 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1453 | mReqFormat = format; |
| 1454 | return NO_ERROR; |
| 1455 | } |
| 1456 | |
| 1457 | int Surface::setScalingMode(int mode) |
| 1458 | { |
| 1459 | ATRACE_CALL(); |
| 1460 | ALOGV("Surface::setScalingMode(%d)", mode); |
| 1461 | |
| 1462 | switch (mode) { |
| 1463 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: |
| 1464 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: |
| 1465 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: |
Robert Carr | c2e7788 | 2015-12-16 18:14:03 -0800 | [diff] [blame] | 1466 | case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP: |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1467 | break; |
| 1468 | default: |
| 1469 | ALOGE("unknown scaling mode: %d", mode); |
| 1470 | return BAD_VALUE; |
| 1471 | } |
| 1472 | |
| 1473 | Mutex::Autolock lock(mMutex); |
| 1474 | mScalingMode = mode; |
| 1475 | return NO_ERROR; |
| 1476 | } |
| 1477 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1478 | int Surface::setBuffersTransform(uint32_t transform) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1479 | { |
| 1480 | ATRACE_CALL(); |
| 1481 | ALOGV("Surface::setBuffersTransform"); |
| 1482 | Mutex::Autolock lock(mMutex); |
| 1483 | mTransform = transform; |
| 1484 | return NO_ERROR; |
| 1485 | } |
| 1486 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1487 | int Surface::setBuffersStickyTransform(uint32_t transform) |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1488 | { |
| 1489 | ATRACE_CALL(); |
| 1490 | ALOGV("Surface::setBuffersStickyTransform"); |
| 1491 | Mutex::Autolock lock(mMutex); |
| 1492 | mStickyTransform = transform; |
| 1493 | return NO_ERROR; |
| 1494 | } |
| 1495 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1496 | int Surface::setBuffersTimestamp(int64_t timestamp) |
| 1497 | { |
| 1498 | ALOGV("Surface::setBuffersTimestamp"); |
| 1499 | Mutex::Autolock lock(mMutex); |
| 1500 | mTimestamp = timestamp; |
| 1501 | return NO_ERROR; |
| 1502 | } |
| 1503 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1504 | int Surface::setBuffersDataSpace(android_dataspace dataSpace) |
| 1505 | { |
| 1506 | ALOGV("Surface::setBuffersDataSpace"); |
| 1507 | Mutex::Autolock lock(mMutex); |
| 1508 | mDataSpace = dataSpace; |
| 1509 | return NO_ERROR; |
| 1510 | } |
| 1511 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1512 | void Surface::freeAllBuffers() { |
| 1513 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 1514 | mSlots[i].buffer = 0; |
| 1515 | } |
| 1516 | } |
| 1517 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1518 | void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) { |
| 1519 | ATRACE_CALL(); |
| 1520 | ALOGV("Surface::setSurfaceDamage"); |
| 1521 | Mutex::Autolock lock(mMutex); |
| 1522 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 1523 | if (mConnectedToCpu || numRects == 0) { |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1524 | mDirtyRegion = Region::INVALID_REGION; |
| 1525 | return; |
| 1526 | } |
| 1527 | |
| 1528 | mDirtyRegion.clear(); |
| 1529 | for (size_t r = 0; r < numRects; ++r) { |
| 1530 | // We intentionally flip top and bottom here, since because they're |
| 1531 | // specified with a bottom-left origin, top > bottom, which fails |
| 1532 | // validation in the Region class. We will fix this up when we flip to a |
| 1533 | // top-left origin in queueBuffer. |
| 1534 | Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top); |
| 1535 | mDirtyRegion.orSelf(rect); |
| 1536 | } |
| 1537 | } |
| 1538 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1539 | // ---------------------------------------------------------------------- |
| 1540 | // the lock/unlock APIs must be used from the same thread |
| 1541 | |
| 1542 | static status_t copyBlt( |
| 1543 | const sp<GraphicBuffer>& dst, |
| 1544 | const sp<GraphicBuffer>& src, |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1545 | const Region& reg, |
| 1546 | int *dstFenceFd) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1547 | { |
Dennis Kempin | add458f | 2017-05-17 19:16:31 -0700 | [diff] [blame] | 1548 | if (dst->getId() == src->getId()) |
| 1549 | return OK; |
| 1550 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1551 | // src and dst with, height and format must be identical. no verification |
| 1552 | // is done here. |
| 1553 | status_t err; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1554 | uint8_t* src_bits = NULL; |
| 1555 | err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), |
| 1556 | reinterpret_cast<void**>(&src_bits)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1557 | ALOGE_IF(err, "error locking src buffer %s", strerror(-err)); |
| 1558 | |
| 1559 | uint8_t* dst_bits = NULL; |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1560 | err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), |
| 1561 | reinterpret_cast<void**>(&dst_bits), *dstFenceFd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1562 | ALOGE_IF(err, "error locking dst buffer %s", strerror(-err)); |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1563 | *dstFenceFd = -1; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1564 | |
| 1565 | Region::const_iterator head(reg.begin()); |
| 1566 | Region::const_iterator tail(reg.end()); |
| 1567 | if (head != tail && src_bits && dst_bits) { |
| 1568 | const size_t bpp = bytesPerPixel(src->format); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1569 | const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp; |
| 1570 | const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1571 | |
| 1572 | while (head != tail) { |
| 1573 | const Rect& r(*head++); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1574 | int32_t h = r.height(); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1575 | if (h <= 0) continue; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1576 | size_t size = static_cast<uint32_t>(r.width()) * bpp; |
| 1577 | uint8_t const * s = src_bits + |
| 1578 | static_cast<uint32_t>(r.left + src->stride * r.top) * bpp; |
| 1579 | uint8_t * d = dst_bits + |
| 1580 | static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1581 | if (dbpr==sbpr && size==sbpr) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1582 | size *= static_cast<size_t>(h); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1583 | h = 1; |
| 1584 | } |
| 1585 | do { |
| 1586 | memcpy(d, s, size); |
| 1587 | d += dbpr; |
| 1588 | s += sbpr; |
| 1589 | } while (--h > 0); |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | if (src_bits) |
| 1594 | src->unlock(); |
| 1595 | |
| 1596 | if (dst_bits) |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1597 | dst->unlockAsync(dstFenceFd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1598 | |
| 1599 | return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1600 | } |
| 1601 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1602 | // ---------------------------------------------------------------------------- |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1603 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1604 | status_t Surface::lock( |
| 1605 | ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) |
| 1606 | { |
| 1607 | if (mLockedBuffer != 0) { |
| 1608 | ALOGE("Surface::lock failed, already locked"); |
| 1609 | return INVALID_OPERATION; |
| 1610 | } |
| 1611 | |
| 1612 | if (!mConnectedToCpu) { |
| 1613 | int err = Surface::connect(NATIVE_WINDOW_API_CPU); |
| 1614 | if (err) { |
| 1615 | return err; |
| 1616 | } |
| 1617 | // we're intending to do software rendering from this point |
| 1618 | setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 1619 | } |
| 1620 | |
| 1621 | ANativeWindowBuffer* out; |
| 1622 | int fenceFd = -1; |
| 1623 | status_t err = dequeueBuffer(&out, &fenceFd); |
| 1624 | ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err)); |
| 1625 | if (err == NO_ERROR) { |
| 1626 | sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1627 | const Rect bounds(backBuffer->width, backBuffer->height); |
| 1628 | |
| 1629 | Region newDirtyRegion; |
| 1630 | if (inOutDirtyBounds) { |
| 1631 | newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds)); |
| 1632 | newDirtyRegion.andSelf(bounds); |
| 1633 | } else { |
| 1634 | newDirtyRegion.set(bounds); |
| 1635 | } |
| 1636 | |
| 1637 | // figure out if we can copy the frontbuffer back |
| 1638 | const sp<GraphicBuffer>& frontBuffer(mPostedBuffer); |
| 1639 | const bool canCopyBack = (frontBuffer != 0 && |
| 1640 | backBuffer->width == frontBuffer->width && |
| 1641 | backBuffer->height == frontBuffer->height && |
| 1642 | backBuffer->format == frontBuffer->format); |
| 1643 | |
| 1644 | if (canCopyBack) { |
| 1645 | // copy the area that is invalid and not repainted this round |
| 1646 | const Region copyback(mDirtyRegion.subtract(newDirtyRegion)); |
Francis Hart | dc10f84 | 2014-12-01 16:04:49 +0200 | [diff] [blame] | 1647 | if (!copyback.isEmpty()) { |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1648 | copyBlt(backBuffer, frontBuffer, copyback, &fenceFd); |
Francis Hart | dc10f84 | 2014-12-01 16:04:49 +0200 | [diff] [blame] | 1649 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1650 | } else { |
| 1651 | // if we can't copy-back anything, modify the user's dirty |
| 1652 | // region to make sure they redraw the whole buffer |
| 1653 | newDirtyRegion.set(bounds); |
| 1654 | mDirtyRegion.clear(); |
| 1655 | Mutex::Autolock lock(mMutex); |
| 1656 | for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) { |
| 1657 | mSlots[i].dirtyRegion.clear(); |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | |
| 1662 | { // scope for the lock |
| 1663 | Mutex::Autolock lock(mMutex); |
| 1664 | int backBufferSlot(getSlotFromBufferLocked(backBuffer.get())); |
| 1665 | if (backBufferSlot >= 0) { |
| 1666 | Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion); |
| 1667 | mDirtyRegion.subtract(dirtyRegion); |
| 1668 | dirtyRegion = newDirtyRegion; |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | mDirtyRegion.orSelf(newDirtyRegion); |
| 1673 | if (inOutDirtyBounds) { |
| 1674 | *inOutDirtyBounds = newDirtyRegion.getBounds(); |
| 1675 | } |
| 1676 | |
| 1677 | void* vaddr; |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1678 | status_t res = backBuffer->lockAsync( |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1679 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1680 | newDirtyRegion.bounds(), &vaddr, fenceFd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1681 | |
| 1682 | ALOGW_IF(res, "failed locking buffer (handle = %p)", |
| 1683 | backBuffer->handle); |
| 1684 | |
| 1685 | if (res != 0) { |
| 1686 | err = INVALID_OPERATION; |
| 1687 | } else { |
| 1688 | mLockedBuffer = backBuffer; |
| 1689 | outBuffer->width = backBuffer->width; |
| 1690 | outBuffer->height = backBuffer->height; |
| 1691 | outBuffer->stride = backBuffer->stride; |
| 1692 | outBuffer->format = backBuffer->format; |
| 1693 | outBuffer->bits = vaddr; |
| 1694 | } |
| 1695 | } |
| 1696 | return err; |
| 1697 | } |
| 1698 | |
| 1699 | status_t Surface::unlockAndPost() |
| 1700 | { |
| 1701 | if (mLockedBuffer == 0) { |
| 1702 | ALOGE("Surface::unlockAndPost failed, no locked buffer"); |
| 1703 | return INVALID_OPERATION; |
| 1704 | } |
| 1705 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1706 | int fd = -1; |
| 1707 | status_t err = mLockedBuffer->unlockAsync(&fd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1708 | ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle); |
| 1709 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1710 | err = queueBuffer(mLockedBuffer.get(), fd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1711 | ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)", |
| 1712 | mLockedBuffer->handle, strerror(-err)); |
| 1713 | |
| 1714 | mPostedBuffer = mLockedBuffer; |
| 1715 | mLockedBuffer = 0; |
| 1716 | return err; |
| 1717 | } |
| 1718 | |
Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 1719 | bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) { |
| 1720 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | bc8c192 | 2016-07-01 14:15:41 -0700 | [diff] [blame] | 1721 | if (mNextFrameNumber > lastFrame) { |
Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 1722 | return true; |
| 1723 | } |
| 1724 | return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK; |
| 1725 | } |
| 1726 | |
Pablo Ceballos | 8e3e92b | 2016-06-27 17:56:53 -0700 | [diff] [blame] | 1727 | status_t Surface::getUniqueId(uint64_t* outId) const { |
| 1728 | Mutex::Autolock lock(mMutex); |
| 1729 | return mGraphicBufferProducer->getUniqueId(outId); |
| 1730 | } |
| 1731 | |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame^] | 1732 | int Surface::getConsumerUsage(uint64_t* outUsage) const { |
| 1733 | Mutex::Autolock lock(mMutex); |
| 1734 | return mGraphicBufferProducer->getConsumerUsage(outUsage); |
| 1735 | } |
| 1736 | |
Dan Stoza | 932f008 | 2017-05-31 13:50:16 -0700 | [diff] [blame] | 1737 | nsecs_t Surface::getLastDequeueStartTime() const { |
| 1738 | Mutex::Autolock lock(mMutex); |
| 1739 | return mLastDequeueStartTime; |
| 1740 | } |
| 1741 | |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1742 | status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) { |
| 1743 | if (out == nullptr) { |
| 1744 | ALOGE("%s: out must not be null!", __FUNCTION__); |
| 1745 | return BAD_VALUE; |
| 1746 | } |
| 1747 | |
| 1748 | Mutex::Autolock lock(mMutex); |
| 1749 | *out = mRemovedBuffers; |
| 1750 | mRemovedBuffers.clear(); |
| 1751 | return OK; |
| 1752 | } |
| 1753 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1754 | }; // namespace android |