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 | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 23 | #include <android/native_window.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 24 | |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 25 | #include <utils/Log.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 26 | #include <utils/Trace.h> |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 27 | #include <utils/NativeHandle.h> |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 28 | |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 29 | #include <ui/DisplayStatInfo.h> |
Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 30 | #include <ui/Fence.h> |
| 31 | #include <ui/HdrCapabilities.h> |
| 32 | #include <ui/Region.h> |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 33 | |
Mathias Agopian | 2b5dd40 | 2017-02-07 17:36:19 -0800 | [diff] [blame] | 34 | #include <gui/BufferItem.h> |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 35 | #include <gui/IProducerListener.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 36 | |
Mathias Agopian | 2b5dd40 | 2017-02-07 17:36:19 -0800 | [diff] [blame] | 37 | #include <gui/ISurfaceComposer.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 38 | #include <private/gui/ComposerService.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 39 | |
Courtney Goeltzenleuchter | a0c93e1 | 2017-03-17 16:16:48 -0600 | [diff] [blame] | 40 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> |
| 41 | #include <configstore/Utils.h> |
| 42 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | namespace android { |
| 44 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 45 | Surface::Surface( |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 46 | const sp<IGraphicBufferProducer>& bufferProducer, |
| 47 | bool controlledByApp) |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 48 | : mGraphicBufferProducer(bufferProducer), |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 49 | mCrop(Rect::EMPTY_RECT), |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 50 | mGenerationNumber(0), |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 51 | mSharedBufferMode(false), |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 52 | mAutoRefresh(false), |
| 53 | mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT), |
Pablo Ceballos | bc8c192 | 2016-07-01 14:15:41 -0700 | [diff] [blame] | 54 | mSharedBufferHasBeenQueued(false), |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 55 | mQueriedSupportedTimestamps(false), |
| 56 | mFrameTimestampsSupportsPresent(false), |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 57 | mEnableFrameTimestamps(false), |
| 58 | mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>()) |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 59 | { |
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; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 474 | uint32_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); |
| 479 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 480 | reqWidth = mReqWidth ? mReqWidth : mUserWidth; |
| 481 | reqHeight = mReqHeight ? mReqHeight : mUserHeight; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 482 | |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 483 | reqFormat = mReqFormat; |
| 484 | reqUsage = mReqUsage; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 485 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 486 | enableFrameTimestamps = mEnableFrameTimestamps; |
| 487 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 488 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot != |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 489 | BufferItem::INVALID_BUFFER_SLOT) { |
| 490 | sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer); |
| 491 | if (gbuf != NULL) { |
| 492 | *buffer = gbuf.get(); |
| 493 | *fenceFd = -1; |
| 494 | return OK; |
| 495 | } |
| 496 | } |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 497 | } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer |
| 498 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 499 | int buf = -1; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 500 | sp<Fence> fence; |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 501 | nsecs_t now = systemTime(); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 502 | |
| 503 | FrameEventHistoryDelta frameTimestamps; |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 504 | status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 505 | reqWidth, reqHeight, reqFormat, reqUsage, |
| 506 | enableFrameTimestamps ? &frameTimestamps : nullptr); |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 507 | mLastDequeueDuration = systemTime() - now; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 508 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 509 | if (result < 0) { |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 510 | ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer" |
| 511 | "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat, |
| 512 | reqUsage, result); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 513 | return result; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 514 | } |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 515 | |
| 516 | Mutex::Autolock lock(mMutex); |
| 517 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 518 | sp<GraphicBuffer>& gbuf(mSlots[buf].buffer); |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 519 | |
| 520 | // this should never happen |
| 521 | ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf); |
| 522 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 523 | if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) { |
| 524 | freeAllBuffers(); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 525 | } |
Ted Bonkenburg | bd050ab | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 526 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 527 | if (enableFrameTimestamps) { |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 528 | mFrameEventHistory->applyDelta(frameTimestamps); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 531 | if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) { |
| 532 | if (mReportRemovedBuffers && (gbuf != nullptr)) { |
| 533 | mRemovedBuffers.clear(); |
| 534 | mRemovedBuffers.push_back(gbuf); |
| 535 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 536 | result = mGraphicBufferProducer->requestBuffer(buf, &gbuf); |
| 537 | if (result != NO_ERROR) { |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 538 | ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result); |
Jesse Hall | 9f5a1b6 | 2014-10-02 11:09:03 -0700 | [diff] [blame] | 539 | mGraphicBufferProducer->cancelBuffer(buf, fence); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 540 | return result; |
| 541 | } |
| 542 | } |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 543 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 544 | if (fence->isValid()) { |
| 545 | *fenceFd = fence->dup(); |
| 546 | if (*fenceFd == -1) { |
| 547 | ALOGE("dequeueBuffer: error duping fence: %d", errno); |
| 548 | // dup() should never fail; something is badly wrong. Soldier on |
| 549 | // and hope for the best; the worst that should happen is some |
| 550 | // visible corruption that lasts until the next frame. |
| 551 | } |
Ted Bonkenburg | e5d6eb8 | 2011-08-09 22:38:41 -0700 | [diff] [blame] | 552 | } else { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 553 | *fenceFd = -1; |
Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 554 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 555 | |
| 556 | *buffer = gbuf.get(); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 557 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 558 | if (mSharedBufferMode && mAutoRefresh) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 559 | mSharedBufferSlot = buf; |
| 560 | mSharedBufferHasBeenQueued = false; |
| 561 | } else if (mSharedBufferSlot == buf) { |
| 562 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 563 | mSharedBufferHasBeenQueued = false; |
| 564 | } |
| 565 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 566 | return OK; |
Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 569 | int Surface::cancelBuffer(android_native_buffer_t* buffer, |
| 570 | int fenceFd) { |
| 571 | ATRACE_CALL(); |
| 572 | ALOGV("Surface::cancelBuffer"); |
| 573 | Mutex::Autolock lock(mMutex); |
| 574 | int i = getSlotFromBufferLocked(buffer); |
| 575 | if (i < 0) { |
Taiju Tsuiki | 4d0cd3f | 2015-04-30 22:15:33 +0900 | [diff] [blame] | 576 | if (fenceFd >= 0) { |
| 577 | close(fenceFd); |
| 578 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 579 | return i; |
| 580 | } |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 581 | if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) { |
| 582 | if (fenceFd >= 0) { |
| 583 | close(fenceFd); |
| 584 | } |
| 585 | return OK; |
| 586 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 587 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); |
| 588 | mGraphicBufferProducer->cancelBuffer(i, fence); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 589 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 590 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 591 | mSharedBufferHasBeenQueued = true; |
| 592 | } |
| 593 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 594 | return OK; |
| 595 | } |
| 596 | |
| 597 | int Surface::getSlotFromBufferLocked( |
| 598 | android_native_buffer_t* buffer) const { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 599 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 600 | if (mSlots[i].buffer != NULL && |
| 601 | mSlots[i].buffer->handle == buffer->handle) { |
| 602 | return i; |
Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 605 | ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle); |
| 606 | return BAD_VALUE; |
Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 609 | int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 610 | ALOGV("Surface::lockBuffer"); |
| 611 | Mutex::Autolock lock(mMutex); |
| 612 | return OK; |
| 613 | } |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 614 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 615 | int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) { |
| 616 | ATRACE_CALL(); |
| 617 | ALOGV("Surface::queueBuffer"); |
| 618 | Mutex::Autolock lock(mMutex); |
| 619 | int64_t timestamp; |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 620 | bool isAutoTimestamp = false; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 621 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 622 | if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) { |
Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 623 | timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 624 | isAutoTimestamp = true; |
Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 625 | ALOGV("Surface::queueBuffer making up timestamp: %.2f ms", |
Colin Cross | 9a80d50 | 2016-09-27 14:12:48 -0700 | [diff] [blame] | 626 | timestamp / 1000000.0); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 627 | } else { |
| 628 | timestamp = mTimestamp; |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 629 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 630 | int i = getSlotFromBufferLocked(buffer); |
| 631 | if (i < 0) { |
Taiju Tsuiki | 4d0cd3f | 2015-04-30 22:15:33 +0900 | [diff] [blame] | 632 | if (fenceFd >= 0) { |
| 633 | close(fenceFd); |
| 634 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 635 | return i; |
| 636 | } |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 637 | if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) { |
| 638 | if (fenceFd >= 0) { |
| 639 | close(fenceFd); |
| 640 | } |
| 641 | return OK; |
| 642 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 645 | // Make sure the crop rectangle is entirely inside the buffer. |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 646 | Rect crop(Rect::EMPTY_RECT); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 647 | mCrop.intersect(Rect(buffer->width, buffer->height), &crop); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 649 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); |
| 650 | IGraphicBufferProducer::QueueBufferOutput output; |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 651 | IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp, |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 652 | mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform, |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 653 | fence, mStickyTransform, mEnableFrameTimestamps); |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 654 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 655 | if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) { |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 656 | input.setSurfaceDamage(Region::INVALID_REGION); |
| 657 | } else { |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 658 | // Here we do two things: |
| 659 | // 1) The surface damage was specified using the OpenGL ES convention of |
| 660 | // the origin being in the bottom-left corner. Here we flip to the |
| 661 | // convention that the rest of the system uses (top-left corner) by |
| 662 | // subtracting all top/bottom coordinates from the buffer height. |
| 663 | // 2) If the buffer is coming in rotated (for example, because the EGL |
| 664 | // implementation is reacting to the transform hint coming back from |
| 665 | // SurfaceFlinger), the surface damage needs to be rotated the |
| 666 | // opposite direction, since it was generated assuming an unrotated |
| 667 | // buffer (the app doesn't know that the EGL implementation is |
| 668 | // reacting to the transform hint behind its back). The |
| 669 | // transformations in the switch statement below apply those |
| 670 | // complementary rotations (e.g., if 90 degrees, rotate 270 degrees). |
| 671 | |
| 672 | int width = buffer->width; |
Dan Stoza | 0e65e6c | 2015-05-26 13:22:27 -0700 | [diff] [blame] | 673 | int height = buffer->height; |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 674 | bool rotated90 = (mTransform ^ mStickyTransform) & |
| 675 | NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 676 | if (rotated90) { |
| 677 | std::swap(width, height); |
Dan Stoza | 0e65e6c | 2015-05-26 13:22:27 -0700 | [diff] [blame] | 678 | } |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 679 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 680 | Region flippedRegion; |
| 681 | for (auto rect : mDirtyRegion) { |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 682 | int left = rect.left; |
| 683 | int right = rect.right; |
| 684 | int top = height - rect.bottom; // Flip from OpenGL convention |
| 685 | int bottom = height - rect.top; // Flip from OpenGL convention |
| 686 | switch (mTransform ^ mStickyTransform) { |
| 687 | case NATIVE_WINDOW_TRANSFORM_ROT_90: { |
| 688 | // Rotate 270 degrees |
| 689 | Rect flippedRect{top, width - right, bottom, width - left}; |
| 690 | flippedRegion.orSelf(flippedRect); |
| 691 | break; |
| 692 | } |
| 693 | case NATIVE_WINDOW_TRANSFORM_ROT_180: { |
| 694 | // Rotate 180 degrees |
| 695 | Rect flippedRect{width - right, height - bottom, |
| 696 | width - left, height - top}; |
| 697 | flippedRegion.orSelf(flippedRect); |
| 698 | break; |
| 699 | } |
| 700 | case NATIVE_WINDOW_TRANSFORM_ROT_270: { |
| 701 | // Rotate 90 degrees |
| 702 | Rect flippedRect{height - bottom, left, |
| 703 | height - top, right}; |
| 704 | flippedRegion.orSelf(flippedRect); |
| 705 | break; |
| 706 | } |
| 707 | default: { |
| 708 | Rect flippedRect{left, top, right, bottom}; |
| 709 | flippedRegion.orSelf(flippedRect); |
| 710 | break; |
| 711 | } |
| 712 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | input.setSurfaceDamage(flippedRegion); |
| 716 | } |
| 717 | |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 718 | nsecs_t now = systemTime(); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 719 | status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output); |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 720 | mLastQueueDuration = systemTime() - now; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 721 | if (err != OK) { |
| 722 | ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err); |
| 723 | } |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 724 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 725 | if (mEnableFrameTimestamps) { |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 726 | mFrameEventHistory->applyDelta(output.frameTimestamps); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 727 | // Update timestamps with the local acquire fence. |
| 728 | // The consumer doesn't send it back to prevent us from having two |
| 729 | // file descriptors of the same fence. |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 730 | mFrameEventHistory->updateAcquireFence(mNextFrameNumber, |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 731 | std::make_shared<FenceTime>(std::move(fence))); |
| 732 | |
| 733 | // Cache timestamps of signaled fences so we can close their file |
| 734 | // descriptors. |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 735 | mFrameEventHistory->updateSignalTimes(); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 738 | mLastFrameNumber = mNextFrameNumber; |
| 739 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 740 | mDefaultWidth = output.width; |
| 741 | mDefaultHeight = output.height; |
| 742 | mNextFrameNumber = output.nextFrameNumber; |
tedbo | 1e7fa9e | 2011-06-22 15:52:53 -0700 | [diff] [blame] | 743 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 744 | // Disable transform hint if sticky transform is set. |
| 745 | if (mStickyTransform == 0) { |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 746 | mTransformHint = output.transformHint; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 749 | mConsumerRunningBehind = (output.numPendingBuffers >= 2); |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 750 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 751 | if (!mConnectedToCpu) { |
| 752 | // Clear surface damage back to full-buffer |
| 753 | mDirtyRegion = Region::INVALID_REGION; |
| 754 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 755 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 756 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 757 | mSharedBufferHasBeenQueued = true; |
| 758 | } |
| 759 | |
Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 760 | mQueueBufferCondition.broadcast(); |
| 761 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 762 | return err; |
| 763 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 764 | |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 765 | void Surface::querySupportedTimestampsLocked() const { |
| 766 | // mMutex must be locked when calling this method. |
| 767 | |
| 768 | if (mQueriedSupportedTimestamps) { |
| 769 | return; |
| 770 | } |
| 771 | mQueriedSupportedTimestamps = true; |
| 772 | |
| 773 | std::vector<FrameEvent> supportedFrameTimestamps; |
| 774 | status_t err = composerService()->getSupportedFrameTimestamps( |
| 775 | &supportedFrameTimestamps); |
| 776 | |
| 777 | if (err != NO_ERROR) { |
| 778 | return; |
| 779 | } |
| 780 | |
| 781 | for (auto sft : supportedFrameTimestamps) { |
| 782 | if (sft == FrameEvent::DISPLAY_PRESENT) { |
| 783 | mFrameTimestampsSupportsPresent = true; |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 788 | int Surface::query(int what, int* value) const { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 789 | ATRACE_CALL(); |
| 790 | ALOGV("Surface::query"); |
| 791 | { // scope for the lock |
| 792 | Mutex::Autolock lock(mMutex); |
| 793 | switch (what) { |
| 794 | case NATIVE_WINDOW_FORMAT: |
| 795 | if (mReqFormat) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 796 | *value = static_cast<int>(mReqFormat); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 797 | return NO_ERROR; |
| 798 | } |
| 799 | break; |
| 800 | case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: { |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 801 | if (composerService()->authenticateSurfaceTexture( |
| 802 | mGraphicBufferProducer)) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 803 | *value = 1; |
| 804 | } else { |
| 805 | *value = 0; |
| 806 | } |
| 807 | return NO_ERROR; |
| 808 | } |
| 809 | case NATIVE_WINDOW_CONCRETE_TYPE: |
| 810 | *value = NATIVE_WINDOW_SURFACE; |
| 811 | return NO_ERROR; |
| 812 | case NATIVE_WINDOW_DEFAULT_WIDTH: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 813 | *value = static_cast<int>( |
| 814 | mUserWidth ? mUserWidth : mDefaultWidth); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 815 | return NO_ERROR; |
| 816 | case NATIVE_WINDOW_DEFAULT_HEIGHT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 817 | *value = static_cast<int>( |
| 818 | mUserHeight ? mUserHeight : mDefaultHeight); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 819 | return NO_ERROR; |
| 820 | case NATIVE_WINDOW_TRANSFORM_HINT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 821 | *value = static_cast<int>(mTransformHint); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 822 | return NO_ERROR; |
| 823 | case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: { |
| 824 | status_t err = NO_ERROR; |
| 825 | if (!mConsumerRunningBehind) { |
| 826 | *value = 0; |
| 827 | } else { |
| 828 | err = mGraphicBufferProducer->query(what, value); |
| 829 | if (err == NO_ERROR) { |
| 830 | mConsumerRunningBehind = *value; |
| 831 | } |
| 832 | } |
| 833 | return err; |
| 834 | } |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 835 | case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: { |
| 836 | int64_t durationUs = mLastDequeueDuration / 1000; |
| 837 | *value = durationUs > std::numeric_limits<int>::max() ? |
| 838 | std::numeric_limits<int>::max() : |
| 839 | static_cast<int>(durationUs); |
| 840 | return NO_ERROR; |
| 841 | } |
| 842 | case NATIVE_WINDOW_LAST_QUEUE_DURATION: { |
| 843 | int64_t durationUs = mLastQueueDuration / 1000; |
| 844 | *value = durationUs > std::numeric_limits<int>::max() ? |
| 845 | std::numeric_limits<int>::max() : |
| 846 | static_cast<int>(durationUs); |
| 847 | return NO_ERROR; |
| 848 | } |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 849 | case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: { |
| 850 | querySupportedTimestampsLocked(); |
| 851 | *value = mFrameTimestampsSupportsPresent ? 1 : 0; |
| 852 | return NO_ERROR; |
| 853 | } |
Mathias Agopian | 10e9ab5 | 2017-03-08 15:02:55 -0800 | [diff] [blame] | 854 | case NATIVE_WINDOW_IS_VALID: { |
| 855 | *value = mGraphicBufferProducer != nullptr ? 1 : 0; |
| 856 | return NO_ERROR; |
| 857 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 858 | } |
Jamie Gennis | 391bbe2 | 2011-03-14 15:00:06 -0700 | [diff] [blame] | 859 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 860 | return mGraphicBufferProducer->query(what, value); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 861 | } |
| 862 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 863 | int Surface::perform(int operation, va_list args) |
| 864 | { |
| 865 | int res = NO_ERROR; |
| 866 | switch (operation) { |
| 867 | case NATIVE_WINDOW_CONNECT: |
| 868 | // deprecated. must return NO_ERROR. |
| 869 | break; |
| 870 | case NATIVE_WINDOW_DISCONNECT: |
| 871 | // deprecated. must return NO_ERROR. |
| 872 | break; |
| 873 | case NATIVE_WINDOW_SET_USAGE: |
| 874 | res = dispatchSetUsage(args); |
| 875 | break; |
| 876 | case NATIVE_WINDOW_SET_CROP: |
| 877 | res = dispatchSetCrop(args); |
| 878 | break; |
| 879 | case NATIVE_WINDOW_SET_BUFFER_COUNT: |
| 880 | res = dispatchSetBufferCount(args); |
| 881 | break; |
| 882 | case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: |
| 883 | res = dispatchSetBuffersGeometry(args); |
| 884 | break; |
| 885 | case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: |
| 886 | res = dispatchSetBuffersTransform(args); |
| 887 | break; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 888 | case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM: |
| 889 | res = dispatchSetBuffersStickyTransform(args); |
| 890 | break; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 891 | case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP: |
| 892 | res = dispatchSetBuffersTimestamp(args); |
| 893 | break; |
| 894 | case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS: |
| 895 | res = dispatchSetBuffersDimensions(args); |
| 896 | break; |
| 897 | case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS: |
| 898 | res = dispatchSetBuffersUserDimensions(args); |
| 899 | break; |
| 900 | case NATIVE_WINDOW_SET_BUFFERS_FORMAT: |
| 901 | res = dispatchSetBuffersFormat(args); |
| 902 | break; |
| 903 | case NATIVE_WINDOW_LOCK: |
| 904 | res = dispatchLock(args); |
| 905 | break; |
| 906 | case NATIVE_WINDOW_UNLOCK_AND_POST: |
| 907 | res = dispatchUnlockAndPost(args); |
| 908 | break; |
| 909 | case NATIVE_WINDOW_SET_SCALING_MODE: |
| 910 | res = dispatchSetScalingMode(args); |
| 911 | break; |
| 912 | case NATIVE_WINDOW_API_CONNECT: |
| 913 | res = dispatchConnect(args); |
| 914 | break; |
| 915 | case NATIVE_WINDOW_API_DISCONNECT: |
| 916 | res = dispatchDisconnect(args); |
| 917 | break; |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 918 | case NATIVE_WINDOW_SET_SIDEBAND_STREAM: |
| 919 | res = dispatchSetSidebandStream(args); |
| 920 | break; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 921 | case NATIVE_WINDOW_SET_BUFFERS_DATASPACE: |
| 922 | res = dispatchSetBuffersDataSpace(args); |
| 923 | break; |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 924 | case NATIVE_WINDOW_SET_SURFACE_DAMAGE: |
| 925 | res = dispatchSetSurfaceDamage(args); |
| 926 | break; |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 927 | case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE: |
| 928 | res = dispatchSetSharedBufferMode(args); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 929 | break; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 930 | case NATIVE_WINDOW_SET_AUTO_REFRESH: |
| 931 | res = dispatchSetAutoRefresh(args); |
| 932 | break; |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 933 | case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION: |
| 934 | res = dispatchGetDisplayRefreshCycleDuration(args); |
| 935 | break; |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 936 | case NATIVE_WINDOW_GET_NEXT_FRAME_ID: |
| 937 | res = dispatchGetNextFrameId(args); |
| 938 | break; |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 939 | case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS: |
| 940 | res = dispatchEnableFrameTimestamps(args); |
| 941 | break; |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 942 | case NATIVE_WINDOW_GET_COMPOSITOR_TIMING: |
| 943 | res = dispatchGetCompositorTiming(args); |
| 944 | break; |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 945 | case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS: |
| 946 | res = dispatchGetFrameTimestamps(args); |
| 947 | break; |
Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 948 | case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT: |
| 949 | res = dispatchGetWideColorSupport(args); |
| 950 | break; |
Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 951 | case NATIVE_WINDOW_GET_HDR_SUPPORT: |
| 952 | res = dispatchGetHdrSupport(args); |
| 953 | break; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 954 | default: |
| 955 | res = NAME_NOT_FOUND; |
| 956 | break; |
| 957 | } |
| 958 | return res; |
| 959 | } |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 960 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 961 | int Surface::dispatchConnect(va_list args) { |
| 962 | int api = va_arg(args, int); |
| 963 | return connect(api); |
| 964 | } |
Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 965 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 966 | int Surface::dispatchDisconnect(va_list args) { |
| 967 | int api = va_arg(args, int); |
| 968 | return disconnect(api); |
| 969 | } |
| 970 | |
| 971 | int Surface::dispatchSetUsage(va_list args) { |
| 972 | int usage = va_arg(args, int); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 973 | return setUsage(static_cast<uint32_t>(usage)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | int Surface::dispatchSetCrop(va_list args) { |
| 977 | android_native_rect_t const* rect = va_arg(args, android_native_rect_t*); |
| 978 | return setCrop(reinterpret_cast<Rect const*>(rect)); |
| 979 | } |
| 980 | |
| 981 | int Surface::dispatchSetBufferCount(va_list args) { |
| 982 | size_t bufferCount = va_arg(args, size_t); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 983 | return setBufferCount(static_cast<int32_t>(bufferCount)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | int Surface::dispatchSetBuffersGeometry(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 987 | uint32_t width = va_arg(args, uint32_t); |
| 988 | uint32_t height = va_arg(args, uint32_t); |
| 989 | PixelFormat format = va_arg(args, PixelFormat); |
| 990 | int err = setBuffersDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 991 | if (err != 0) { |
| 992 | return err; |
| 993 | } |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 994 | return setBuffersFormat(format); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | int Surface::dispatchSetBuffersDimensions(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 998 | uint32_t width = va_arg(args, uint32_t); |
| 999 | uint32_t height = va_arg(args, uint32_t); |
| 1000 | return setBuffersDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | int Surface::dispatchSetBuffersUserDimensions(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1004 | uint32_t width = va_arg(args, uint32_t); |
| 1005 | uint32_t height = va_arg(args, uint32_t); |
| 1006 | return setBuffersUserDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | int Surface::dispatchSetBuffersFormat(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1010 | PixelFormat format = va_arg(args, PixelFormat); |
| 1011 | return setBuffersFormat(format); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | int Surface::dispatchSetScalingMode(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1015 | int mode = va_arg(args, int); |
| 1016 | return setScalingMode(mode); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | int Surface::dispatchSetBuffersTransform(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1020 | uint32_t transform = va_arg(args, uint32_t); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1021 | return setBuffersTransform(transform); |
| 1022 | } |
| 1023 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1024 | int Surface::dispatchSetBuffersStickyTransform(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1025 | uint32_t transform = va_arg(args, uint32_t); |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1026 | return setBuffersStickyTransform(transform); |
| 1027 | } |
| 1028 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1029 | int Surface::dispatchSetBuffersTimestamp(va_list args) { |
| 1030 | int64_t timestamp = va_arg(args, int64_t); |
| 1031 | return setBuffersTimestamp(timestamp); |
| 1032 | } |
| 1033 | |
| 1034 | int Surface::dispatchLock(va_list args) { |
| 1035 | ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*); |
| 1036 | ARect* inOutDirtyBounds = va_arg(args, ARect*); |
| 1037 | return lock(outBuffer, inOutDirtyBounds); |
| 1038 | } |
| 1039 | |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 1040 | int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1041 | return unlockAndPost(); |
| 1042 | } |
| 1043 | |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 1044 | int Surface::dispatchSetSidebandStream(va_list args) { |
| 1045 | native_handle_t* sH = va_arg(args, native_handle_t*); |
| 1046 | sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false); |
| 1047 | setSidebandStream(sidebandHandle); |
| 1048 | return OK; |
| 1049 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1050 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1051 | int Surface::dispatchSetBuffersDataSpace(va_list args) { |
| 1052 | android_dataspace dataspace = |
| 1053 | static_cast<android_dataspace>(va_arg(args, int)); |
| 1054 | return setBuffersDataSpace(dataspace); |
| 1055 | } |
| 1056 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1057 | int Surface::dispatchSetSurfaceDamage(va_list args) { |
| 1058 | android_native_rect_t* rects = va_arg(args, android_native_rect_t*); |
| 1059 | size_t numRects = va_arg(args, size_t); |
| 1060 | setSurfaceDamage(rects, numRects); |
| 1061 | return NO_ERROR; |
| 1062 | } |
| 1063 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1064 | int Surface::dispatchSetSharedBufferMode(va_list args) { |
| 1065 | bool sharedBufferMode = va_arg(args, int); |
| 1066 | return setSharedBufferMode(sharedBufferMode); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | int Surface::dispatchSetAutoRefresh(va_list args) { |
| 1070 | bool autoRefresh = va_arg(args, int); |
| 1071 | return setAutoRefresh(autoRefresh); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1074 | int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) { |
| 1075 | nsecs_t* outRefreshDuration = va_arg(args, int64_t*); |
| 1076 | return getDisplayRefreshCycleDuration(outRefreshDuration); |
| 1077 | } |
| 1078 | |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1079 | int Surface::dispatchGetNextFrameId(va_list args) { |
| 1080 | uint64_t* nextFrameId = va_arg(args, uint64_t*); |
| 1081 | *nextFrameId = getNextFrameNumber(); |
| 1082 | return NO_ERROR; |
| 1083 | } |
| 1084 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1085 | int Surface::dispatchEnableFrameTimestamps(va_list args) { |
| 1086 | bool enable = va_arg(args, int); |
| 1087 | enableFrameTimestamps(enable); |
| 1088 | return NO_ERROR; |
| 1089 | } |
| 1090 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1091 | int Surface::dispatchGetCompositorTiming(va_list args) { |
| 1092 | nsecs_t* compositeDeadline = va_arg(args, int64_t*); |
| 1093 | nsecs_t* compositeInterval = va_arg(args, int64_t*); |
| 1094 | nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*); |
| 1095 | return getCompositorTiming(compositeDeadline, compositeInterval, |
| 1096 | compositeToPresentLatency); |
| 1097 | } |
| 1098 | |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1099 | int Surface::dispatchGetFrameTimestamps(va_list args) { |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1100 | uint64_t frameId = va_arg(args, uint64_t); |
Brian Anderson | dbd0ea8 | 2016-07-22 09:38:59 -0700 | [diff] [blame] | 1101 | nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1102 | nsecs_t* outAcquireTime = va_arg(args, int64_t*); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1103 | nsecs_t* outLatchTime = va_arg(args, int64_t*); |
| 1104 | nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*); |
| 1105 | nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*); |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 1106 | nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*); |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 1107 | nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1108 | nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1109 | nsecs_t* outReleaseTime = va_arg(args, int64_t*); |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1110 | return getFrameTimestamps(frameId, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1111 | outRequestedPresentTime, outAcquireTime, outLatchTime, |
| 1112 | outFirstRefreshStartTime, outLastRefreshStartTime, |
Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 1113 | outGpuCompositionDoneTime, outDisplayPresentTime, |
Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 1114 | outDequeueReadyTime, outReleaseTime); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1115 | } |
| 1116 | |
Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 1117 | int Surface::dispatchGetWideColorSupport(va_list args) { |
| 1118 | bool* outSupport = va_arg(args, bool*); |
| 1119 | return getWideColorSupport(outSupport); |
| 1120 | } |
| 1121 | |
Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 1122 | int Surface::dispatchGetHdrSupport(va_list args) { |
| 1123 | bool* outSupport = va_arg(args, bool*); |
| 1124 | return getHdrSupport(outSupport); |
| 1125 | } |
| 1126 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1127 | int Surface::connect(int api) { |
Dan Stoza | 966b98b | 2015-03-02 22:12:37 -0800 | [diff] [blame] | 1128 | static sp<IProducerListener> listener = new DummyProducerListener(); |
| 1129 | return connect(api, listener); |
| 1130 | } |
| 1131 | |
| 1132 | int Surface::connect(int api, const sp<IProducerListener>& listener) { |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1133 | return connect(api, listener, false); |
| 1134 | } |
| 1135 | |
| 1136 | int Surface::connect( |
| 1137 | int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1138 | ATRACE_CALL(); |
| 1139 | ALOGV("Surface::connect"); |
| 1140 | Mutex::Autolock lock(mMutex); |
| 1141 | IGraphicBufferProducer::QueueBufferOutput output; |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1142 | mReportRemovedBuffers = reportBufferRemoval; |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1143 | int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1144 | if (err == NO_ERROR) { |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1145 | mDefaultWidth = output.width; |
| 1146 | mDefaultHeight = output.height; |
| 1147 | mNextFrameNumber = output.nextFrameNumber; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1148 | |
| 1149 | // Disable transform hint if sticky transform is set. |
| 1150 | if (mStickyTransform == 0) { |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1151 | mTransformHint = output.transformHint; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1154 | mConsumerRunningBehind = (output.numPendingBuffers >= 2); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1155 | } |
| 1156 | if (!err && api == NATIVE_WINDOW_API_CPU) { |
| 1157 | mConnectedToCpu = true; |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1158 | // Clear the dirty region in case we're switching from a non-CPU API |
| 1159 | mDirtyRegion.clear(); |
| 1160 | } else if (!err) { |
| 1161 | // Initialize the dirty region for tracking surface damage |
| 1162 | mDirtyRegion = Region::INVALID_REGION; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1163 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1164 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1165 | return err; |
| 1166 | } |
| 1167 | |
Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 1168 | |
Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1169 | int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1170 | ATRACE_CALL(); |
| 1171 | ALOGV("Surface::disconnect"); |
| 1172 | Mutex::Autolock lock(mMutex); |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1173 | mRemovedBuffers.clear(); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1174 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1175 | mSharedBufferHasBeenQueued = false; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1176 | freeAllBuffers(); |
Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1177 | int err = mGraphicBufferProducer->disconnect(api, mode); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1178 | if (!err) { |
| 1179 | mReqFormat = 0; |
| 1180 | mReqWidth = 0; |
| 1181 | mReqHeight = 0; |
| 1182 | mReqUsage = 0; |
| 1183 | mCrop.clear(); |
| 1184 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 1185 | mTransform = 0; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1186 | mStickyTransform = 0; |
| 1187 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1188 | if (api == NATIVE_WINDOW_API_CPU) { |
| 1189 | mConnectedToCpu = false; |
| 1190 | } |
| 1191 | } |
| 1192 | return err; |
| 1193 | } |
| 1194 | |
Dan Stoza | d9c4971 | 2015-04-27 11:06:01 -0700 | [diff] [blame] | 1195 | int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer, |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1196 | sp<Fence>* outFence) { |
| 1197 | ATRACE_CALL(); |
| 1198 | ALOGV("Surface::detachNextBuffer"); |
| 1199 | |
| 1200 | if (outBuffer == NULL || outFence == NULL) { |
| 1201 | return BAD_VALUE; |
| 1202 | } |
| 1203 | |
| 1204 | Mutex::Autolock lock(mMutex); |
| 1205 | |
| 1206 | sp<GraphicBuffer> buffer(NULL); |
| 1207 | sp<Fence> fence(NULL); |
| 1208 | status_t result = mGraphicBufferProducer->detachNextBuffer( |
| 1209 | &buffer, &fence); |
| 1210 | if (result != NO_ERROR) { |
| 1211 | return result; |
| 1212 | } |
| 1213 | |
Dan Stoza | d9c4971 | 2015-04-27 11:06:01 -0700 | [diff] [blame] | 1214 | *outBuffer = buffer; |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1215 | if (fence != NULL && fence->isValid()) { |
| 1216 | *outFence = fence; |
| 1217 | } else { |
| 1218 | *outFence = Fence::NO_FENCE; |
| 1219 | } |
| 1220 | |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1221 | if (mReportRemovedBuffers) { |
| 1222 | mRemovedBuffers.clear(); |
| 1223 | } |
| 1224 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1225 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 1226 | if (mSlots[i].buffer != NULL && |
| 1227 | mSlots[i].buffer->handle == buffer->handle) { |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1228 | if (mReportRemovedBuffers) { |
| 1229 | mRemovedBuffers.push_back(mSlots[i].buffer); |
| 1230 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1231 | mSlots[i].buffer = NULL; |
| 1232 | } |
| 1233 | } |
| 1234 | |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1235 | return NO_ERROR; |
| 1236 | } |
| 1237 | |
| 1238 | int Surface::attachBuffer(ANativeWindowBuffer* buffer) |
| 1239 | { |
| 1240 | ATRACE_CALL(); |
| 1241 | ALOGV("Surface::attachBuffer"); |
| 1242 | |
| 1243 | Mutex::Autolock lock(mMutex); |
| 1244 | |
| 1245 | sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer)); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1246 | uint32_t priorGeneration = graphicBuffer->mGenerationNumber; |
| 1247 | graphicBuffer->mGenerationNumber = mGenerationNumber; |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1248 | int32_t attachedSlot = -1; |
| 1249 | status_t result = mGraphicBufferProducer->attachBuffer( |
| 1250 | &attachedSlot, graphicBuffer); |
| 1251 | if (result != NO_ERROR) { |
| 1252 | ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1253 | graphicBuffer->mGenerationNumber = priorGeneration; |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1254 | return result; |
| 1255 | } |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1256 | if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) { |
| 1257 | mRemovedBuffers.clear(); |
| 1258 | mRemovedBuffers.push_back(mSlots[attachedSlot].buffer); |
| 1259 | } |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1260 | mSlots[attachedSlot].buffer = graphicBuffer; |
| 1261 | |
| 1262 | return NO_ERROR; |
| 1263 | } |
| 1264 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1265 | int Surface::setUsage(uint32_t reqUsage) |
| 1266 | { |
| 1267 | ALOGV("Surface::setUsage"); |
| 1268 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1269 | if (reqUsage != mReqUsage) { |
| 1270 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1271 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1272 | mReqUsage = reqUsage; |
| 1273 | return OK; |
| 1274 | } |
| 1275 | |
| 1276 | int Surface::setCrop(Rect const* rect) |
| 1277 | { |
| 1278 | ATRACE_CALL(); |
| 1279 | |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 1280 | Rect realRect(Rect::EMPTY_RECT); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1281 | if (rect == NULL || rect->isEmpty()) { |
| 1282 | realRect.clear(); |
| 1283 | } else { |
| 1284 | realRect = *rect; |
Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1287 | ALOGV("Surface::setCrop rect=[%d %d %d %d]", |
| 1288 | realRect.left, realRect.top, realRect.right, realRect.bottom); |
| 1289 | |
| 1290 | Mutex::Autolock lock(mMutex); |
| 1291 | mCrop = realRect; |
| 1292 | return NO_ERROR; |
| 1293 | } |
| 1294 | |
| 1295 | int Surface::setBufferCount(int bufferCount) |
| 1296 | { |
| 1297 | ATRACE_CALL(); |
| 1298 | ALOGV("Surface::setBufferCount"); |
| 1299 | Mutex::Autolock lock(mMutex); |
| 1300 | |
Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 1301 | status_t err = NO_ERROR; |
| 1302 | if (bufferCount == 0) { |
| 1303 | err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1); |
| 1304 | } else { |
| 1305 | int minUndequeuedBuffers = 0; |
| 1306 | err = mGraphicBufferProducer->query( |
| 1307 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers); |
| 1308 | if (err == NO_ERROR) { |
| 1309 | err = mGraphicBufferProducer->setMaxDequeuedBufferCount( |
| 1310 | bufferCount - minUndequeuedBuffers); |
| 1311 | } |
| 1312 | } |
Mathias Agopian | 9014726 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 1313 | |
Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 1314 | ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s", |
| 1315 | bufferCount, strerror(-err)); |
| 1316 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1317 | return err; |
| 1318 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1319 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 1320 | int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) { |
| 1321 | ATRACE_CALL(); |
| 1322 | ALOGV("Surface::setMaxDequeuedBufferCount"); |
| 1323 | Mutex::Autolock lock(mMutex); |
| 1324 | |
| 1325 | status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount( |
| 1326 | maxDequeuedBuffers); |
| 1327 | ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) " |
| 1328 | "returned %s", maxDequeuedBuffers, strerror(-err)); |
| 1329 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 1330 | return err; |
| 1331 | } |
| 1332 | |
| 1333 | int Surface::setAsyncMode(bool async) { |
| 1334 | ATRACE_CALL(); |
| 1335 | ALOGV("Surface::setAsyncMode"); |
| 1336 | Mutex::Autolock lock(mMutex); |
| 1337 | |
| 1338 | status_t err = mGraphicBufferProducer->setAsyncMode(async); |
| 1339 | ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s", |
| 1340 | async, strerror(-err)); |
| 1341 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 1342 | return err; |
| 1343 | } |
| 1344 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1345 | int Surface::setSharedBufferMode(bool sharedBufferMode) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1346 | ATRACE_CALL(); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1347 | ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1348 | Mutex::Autolock lock(mMutex); |
| 1349 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1350 | status_t err = mGraphicBufferProducer->setSharedBufferMode( |
| 1351 | sharedBufferMode); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1352 | if (err == NO_ERROR) { |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1353 | mSharedBufferMode = sharedBufferMode; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1354 | } |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1355 | ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned" |
| 1356 | "%s", sharedBufferMode, strerror(-err)); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1357 | |
| 1358 | return err; |
| 1359 | } |
| 1360 | |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1361 | int Surface::setAutoRefresh(bool autoRefresh) { |
| 1362 | ATRACE_CALL(); |
| 1363 | ALOGV("Surface::setAutoRefresh (%d)", autoRefresh); |
| 1364 | Mutex::Autolock lock(mMutex); |
| 1365 | |
| 1366 | status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh); |
| 1367 | if (err == NO_ERROR) { |
| 1368 | mAutoRefresh = autoRefresh; |
| 1369 | } |
| 1370 | ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s", |
| 1371 | autoRefresh, strerror(-err)); |
| 1372 | return err; |
| 1373 | } |
| 1374 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1375 | int Surface::setBuffersDimensions(uint32_t width, uint32_t height) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1376 | { |
| 1377 | ATRACE_CALL(); |
| 1378 | ALOGV("Surface::setBuffersDimensions"); |
| 1379 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1380 | if ((width && !height) || (!width && height)) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1381 | return BAD_VALUE; |
| 1382 | |
| 1383 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1384 | if (width != mReqWidth || height != mReqHeight) { |
| 1385 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1386 | } |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1387 | mReqWidth = width; |
| 1388 | mReqHeight = height; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1389 | return NO_ERROR; |
| 1390 | } |
| 1391 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1392 | int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1393 | { |
| 1394 | ATRACE_CALL(); |
| 1395 | ALOGV("Surface::setBuffersUserDimensions"); |
| 1396 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1397 | if ((width && !height) || (!width && height)) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1398 | return BAD_VALUE; |
| 1399 | |
| 1400 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1401 | if (width != mUserWidth || height != mUserHeight) { |
| 1402 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1403 | } |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1404 | mUserWidth = width; |
| 1405 | mUserHeight = height; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1406 | return NO_ERROR; |
| 1407 | } |
| 1408 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1409 | int Surface::setBuffersFormat(PixelFormat format) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1410 | { |
| 1411 | ALOGV("Surface::setBuffersFormat"); |
| 1412 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1413 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1414 | if (format != mReqFormat) { |
| 1415 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1416 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1417 | mReqFormat = format; |
| 1418 | return NO_ERROR; |
| 1419 | } |
| 1420 | |
| 1421 | int Surface::setScalingMode(int mode) |
| 1422 | { |
| 1423 | ATRACE_CALL(); |
| 1424 | ALOGV("Surface::setScalingMode(%d)", mode); |
| 1425 | |
| 1426 | switch (mode) { |
| 1427 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: |
| 1428 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: |
| 1429 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: |
Robert Carr | c2e7788 | 2015-12-16 18:14:03 -0800 | [diff] [blame] | 1430 | case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP: |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1431 | break; |
| 1432 | default: |
| 1433 | ALOGE("unknown scaling mode: %d", mode); |
| 1434 | return BAD_VALUE; |
| 1435 | } |
| 1436 | |
| 1437 | Mutex::Autolock lock(mMutex); |
| 1438 | mScalingMode = mode; |
| 1439 | return NO_ERROR; |
| 1440 | } |
| 1441 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1442 | int Surface::setBuffersTransform(uint32_t transform) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1443 | { |
| 1444 | ATRACE_CALL(); |
| 1445 | ALOGV("Surface::setBuffersTransform"); |
| 1446 | Mutex::Autolock lock(mMutex); |
| 1447 | mTransform = transform; |
| 1448 | return NO_ERROR; |
| 1449 | } |
| 1450 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1451 | int Surface::setBuffersStickyTransform(uint32_t transform) |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1452 | { |
| 1453 | ATRACE_CALL(); |
| 1454 | ALOGV("Surface::setBuffersStickyTransform"); |
| 1455 | Mutex::Autolock lock(mMutex); |
| 1456 | mStickyTransform = transform; |
| 1457 | return NO_ERROR; |
| 1458 | } |
| 1459 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1460 | int Surface::setBuffersTimestamp(int64_t timestamp) |
| 1461 | { |
| 1462 | ALOGV("Surface::setBuffersTimestamp"); |
| 1463 | Mutex::Autolock lock(mMutex); |
| 1464 | mTimestamp = timestamp; |
| 1465 | return NO_ERROR; |
| 1466 | } |
| 1467 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1468 | int Surface::setBuffersDataSpace(android_dataspace dataSpace) |
| 1469 | { |
| 1470 | ALOGV("Surface::setBuffersDataSpace"); |
| 1471 | Mutex::Autolock lock(mMutex); |
| 1472 | mDataSpace = dataSpace; |
| 1473 | return NO_ERROR; |
| 1474 | } |
| 1475 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1476 | void Surface::freeAllBuffers() { |
| 1477 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 1478 | mSlots[i].buffer = 0; |
| 1479 | } |
| 1480 | } |
| 1481 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1482 | void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) { |
| 1483 | ATRACE_CALL(); |
| 1484 | ALOGV("Surface::setSurfaceDamage"); |
| 1485 | Mutex::Autolock lock(mMutex); |
| 1486 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 1487 | if (mConnectedToCpu || numRects == 0) { |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1488 | mDirtyRegion = Region::INVALID_REGION; |
| 1489 | return; |
| 1490 | } |
| 1491 | |
| 1492 | mDirtyRegion.clear(); |
| 1493 | for (size_t r = 0; r < numRects; ++r) { |
| 1494 | // We intentionally flip top and bottom here, since because they're |
| 1495 | // specified with a bottom-left origin, top > bottom, which fails |
| 1496 | // validation in the Region class. We will fix this up when we flip to a |
| 1497 | // top-left origin in queueBuffer. |
| 1498 | Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top); |
| 1499 | mDirtyRegion.orSelf(rect); |
| 1500 | } |
| 1501 | } |
| 1502 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1503 | // ---------------------------------------------------------------------- |
| 1504 | // the lock/unlock APIs must be used from the same thread |
| 1505 | |
| 1506 | static status_t copyBlt( |
| 1507 | const sp<GraphicBuffer>& dst, |
| 1508 | const sp<GraphicBuffer>& src, |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1509 | const Region& reg, |
| 1510 | int *dstFenceFd) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1511 | { |
| 1512 | // src and dst with, height and format must be identical. no verification |
| 1513 | // is done here. |
| 1514 | status_t err; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1515 | uint8_t* src_bits = NULL; |
| 1516 | err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), |
| 1517 | reinterpret_cast<void**>(&src_bits)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1518 | ALOGE_IF(err, "error locking src buffer %s", strerror(-err)); |
| 1519 | |
| 1520 | uint8_t* dst_bits = NULL; |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1521 | err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), |
| 1522 | reinterpret_cast<void**>(&dst_bits), *dstFenceFd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1523 | ALOGE_IF(err, "error locking dst buffer %s", strerror(-err)); |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1524 | *dstFenceFd = -1; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1525 | |
| 1526 | Region::const_iterator head(reg.begin()); |
| 1527 | Region::const_iterator tail(reg.end()); |
| 1528 | if (head != tail && src_bits && dst_bits) { |
| 1529 | const size_t bpp = bytesPerPixel(src->format); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1530 | const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp; |
| 1531 | const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1532 | |
| 1533 | while (head != tail) { |
| 1534 | const Rect& r(*head++); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1535 | int32_t h = r.height(); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1536 | if (h <= 0) continue; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1537 | size_t size = static_cast<uint32_t>(r.width()) * bpp; |
| 1538 | uint8_t const * s = src_bits + |
| 1539 | static_cast<uint32_t>(r.left + src->stride * r.top) * bpp; |
| 1540 | uint8_t * d = dst_bits + |
| 1541 | static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1542 | if (dbpr==sbpr && size==sbpr) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1543 | size *= static_cast<size_t>(h); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1544 | h = 1; |
| 1545 | } |
| 1546 | do { |
| 1547 | memcpy(d, s, size); |
| 1548 | d += dbpr; |
| 1549 | s += sbpr; |
| 1550 | } while (--h > 0); |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | if (src_bits) |
| 1555 | src->unlock(); |
| 1556 | |
| 1557 | if (dst_bits) |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1558 | dst->unlockAsync(dstFenceFd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1559 | |
| 1560 | return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1561 | } |
| 1562 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1563 | // ---------------------------------------------------------------------------- |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1564 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1565 | status_t Surface::lock( |
| 1566 | ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) |
| 1567 | { |
| 1568 | if (mLockedBuffer != 0) { |
| 1569 | ALOGE("Surface::lock failed, already locked"); |
| 1570 | return INVALID_OPERATION; |
| 1571 | } |
| 1572 | |
| 1573 | if (!mConnectedToCpu) { |
| 1574 | int err = Surface::connect(NATIVE_WINDOW_API_CPU); |
| 1575 | if (err) { |
| 1576 | return err; |
| 1577 | } |
| 1578 | // we're intending to do software rendering from this point |
| 1579 | setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 1580 | } |
| 1581 | |
| 1582 | ANativeWindowBuffer* out; |
| 1583 | int fenceFd = -1; |
| 1584 | status_t err = dequeueBuffer(&out, &fenceFd); |
| 1585 | ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err)); |
| 1586 | if (err == NO_ERROR) { |
| 1587 | sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1588 | const Rect bounds(backBuffer->width, backBuffer->height); |
| 1589 | |
| 1590 | Region newDirtyRegion; |
| 1591 | if (inOutDirtyBounds) { |
| 1592 | newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds)); |
| 1593 | newDirtyRegion.andSelf(bounds); |
| 1594 | } else { |
| 1595 | newDirtyRegion.set(bounds); |
| 1596 | } |
| 1597 | |
| 1598 | // figure out if we can copy the frontbuffer back |
| 1599 | const sp<GraphicBuffer>& frontBuffer(mPostedBuffer); |
| 1600 | const bool canCopyBack = (frontBuffer != 0 && |
| 1601 | backBuffer->width == frontBuffer->width && |
| 1602 | backBuffer->height == frontBuffer->height && |
| 1603 | backBuffer->format == frontBuffer->format); |
| 1604 | |
| 1605 | if (canCopyBack) { |
| 1606 | // copy the area that is invalid and not repainted this round |
| 1607 | const Region copyback(mDirtyRegion.subtract(newDirtyRegion)); |
Francis Hart | dc10f84 | 2014-12-01 16:04:49 +0200 | [diff] [blame] | 1608 | if (!copyback.isEmpty()) { |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1609 | copyBlt(backBuffer, frontBuffer, copyback, &fenceFd); |
Francis Hart | dc10f84 | 2014-12-01 16:04:49 +0200 | [diff] [blame] | 1610 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1611 | } else { |
| 1612 | // if we can't copy-back anything, modify the user's dirty |
| 1613 | // region to make sure they redraw the whole buffer |
| 1614 | newDirtyRegion.set(bounds); |
| 1615 | mDirtyRegion.clear(); |
| 1616 | Mutex::Autolock lock(mMutex); |
| 1617 | for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) { |
| 1618 | mSlots[i].dirtyRegion.clear(); |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | |
| 1623 | { // scope for the lock |
| 1624 | Mutex::Autolock lock(mMutex); |
| 1625 | int backBufferSlot(getSlotFromBufferLocked(backBuffer.get())); |
| 1626 | if (backBufferSlot >= 0) { |
| 1627 | Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion); |
| 1628 | mDirtyRegion.subtract(dirtyRegion); |
| 1629 | dirtyRegion = newDirtyRegion; |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | mDirtyRegion.orSelf(newDirtyRegion); |
| 1634 | if (inOutDirtyBounds) { |
| 1635 | *inOutDirtyBounds = newDirtyRegion.getBounds(); |
| 1636 | } |
| 1637 | |
| 1638 | void* vaddr; |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1639 | status_t res = backBuffer->lockAsync( |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1640 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1641 | newDirtyRegion.bounds(), &vaddr, fenceFd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1642 | |
| 1643 | ALOGW_IF(res, "failed locking buffer (handle = %p)", |
| 1644 | backBuffer->handle); |
| 1645 | |
| 1646 | if (res != 0) { |
| 1647 | err = INVALID_OPERATION; |
| 1648 | } else { |
| 1649 | mLockedBuffer = backBuffer; |
| 1650 | outBuffer->width = backBuffer->width; |
| 1651 | outBuffer->height = backBuffer->height; |
| 1652 | outBuffer->stride = backBuffer->stride; |
| 1653 | outBuffer->format = backBuffer->format; |
| 1654 | outBuffer->bits = vaddr; |
| 1655 | } |
| 1656 | } |
| 1657 | return err; |
| 1658 | } |
| 1659 | |
| 1660 | status_t Surface::unlockAndPost() |
| 1661 | { |
| 1662 | if (mLockedBuffer == 0) { |
| 1663 | ALOGE("Surface::unlockAndPost failed, no locked buffer"); |
| 1664 | return INVALID_OPERATION; |
| 1665 | } |
| 1666 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1667 | int fd = -1; |
| 1668 | status_t err = mLockedBuffer->unlockAsync(&fd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1669 | ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle); |
| 1670 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1671 | err = queueBuffer(mLockedBuffer.get(), fd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1672 | ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)", |
| 1673 | mLockedBuffer->handle, strerror(-err)); |
| 1674 | |
| 1675 | mPostedBuffer = mLockedBuffer; |
| 1676 | mLockedBuffer = 0; |
| 1677 | return err; |
| 1678 | } |
| 1679 | |
Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 1680 | bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) { |
| 1681 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | bc8c192 | 2016-07-01 14:15:41 -0700 | [diff] [blame] | 1682 | if (mNextFrameNumber > lastFrame) { |
Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 1683 | return true; |
| 1684 | } |
| 1685 | return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK; |
| 1686 | } |
| 1687 | |
Pablo Ceballos | 8e3e92b | 2016-06-27 17:56:53 -0700 | [diff] [blame] | 1688 | status_t Surface::getUniqueId(uint64_t* outId) const { |
| 1689 | Mutex::Autolock lock(mMutex); |
| 1690 | return mGraphicBufferProducer->getUniqueId(outId); |
| 1691 | } |
| 1692 | |
Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1693 | status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) { |
| 1694 | if (out == nullptr) { |
| 1695 | ALOGE("%s: out must not be null!", __FUNCTION__); |
| 1696 | return BAD_VALUE; |
| 1697 | } |
| 1698 | |
| 1699 | Mutex::Autolock lock(mMutex); |
| 1700 | *out = mRemovedBuffers; |
| 1701 | mRemovedBuffers.clear(); |
| 1702 | return OK; |
| 1703 | } |
| 1704 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1705 | }; // namespace android |