Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 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_NDEBUG 0 |
| 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "BufferStateLayer" |
| 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 22 | #include <limits> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 23 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 24 | #include <compositionengine/Display.h> |
Lloyd Pique | 0b785d8 | 2018-12-04 17:25:27 -0800 | [diff] [blame] | 25 | #include <compositionengine/Layer.h> |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 26 | #include <compositionengine/OutputLayer.h> |
Lloyd Pique | 0b785d8 | 2018-12-04 17:25:27 -0800 | [diff] [blame] | 27 | #include <compositionengine/impl/LayerCompositionState.h> |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 28 | #include <compositionengine/impl/OutputLayerCompositionState.h> |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame^] | 29 | #include <gui/BufferQueue.h> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 30 | #include <private/gui/SyncFeatures.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 31 | #include <renderengine/Image.h> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 32 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 33 | #include "BufferStateLayer.h" |
| 34 | #include "ColorLayer.h" |
| 35 | #include "TimeStats/TimeStats.h" |
Valerie Hau | 0bc0915 | 2018-12-20 07:42:47 -0800 | [diff] [blame] | 36 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 37 | namespace android { |
| 38 | |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 39 | // clang-format off |
| 40 | const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{ |
| 41 | 1, 0, 0, 0, |
| 42 | 0, 1, 0, 0, |
| 43 | 0, 0, 1, 0, |
| 44 | 0, 0, 0, 1 |
| 45 | }; |
| 46 | // clang-format on |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 47 | |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame^] | 48 | BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) |
| 49 | : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) { |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 50 | mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
Marissa Wall | 3ff826c | 2019-02-07 11:58:25 -0800 | [diff] [blame] | 51 | mCurrentState.dataspace = ui::Dataspace::V0_SRGB; |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 52 | } |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 53 | BufferStateLayer::~BufferStateLayer() { |
| 54 | if (mActiveBuffer != nullptr) { |
| 55 | auto& engine(mFlinger->getRenderEngine()); |
| 56 | engine.unbindExternalTextureBuffer(mActiveBuffer->getId()); |
| 57 | } |
| 58 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 59 | |
| 60 | // ----------------------------------------------------------------------- |
| 61 | // Interface implementation for Layer |
| 62 | // ----------------------------------------------------------------------- |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 63 | void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) { |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 64 | // The previous release fence notifies the client that SurfaceFlinger is done with the previous |
| 65 | // buffer that was presented on this layer. The first transaction that came in this frame that |
| 66 | // replaced the previous buffer on this layer needs this release fence, because the fence will |
| 67 | // let the client know when that previous buffer is removed from the screen. |
| 68 | // |
| 69 | // Every other transaction on this layer does not need a release fence because no other |
| 70 | // Transactions that were set on this layer this frame are going to have their preceeding buffer |
| 71 | // removed from the display this frame. |
| 72 | // |
| 73 | // For example, if we have 3 transactions this frame. The first transaction doesn't contain a |
| 74 | // buffer so it doesn't need a previous release fence because the layer still needs the previous |
| 75 | // buffer. The second transaction contains a buffer so it needs a previous release fence because |
| 76 | // the previous buffer will be released this frame. The third transaction also contains a |
| 77 | // buffer. It replaces the buffer in the second transaction. The buffer in the second |
| 78 | // transaction will now no longer be presented so it is released immediately and the third |
| 79 | // transaction doesn't need a previous release fence. |
| 80 | for (auto& handle : mDrawingState.callbackHandles) { |
| 81 | if (handle->releasePreviousBuffer) { |
| 82 | handle->previousReleaseFence = releaseFence; |
| 83 | break; |
| 84 | } |
| 85 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const { |
| 89 | // TODO(marissaw): send the transform hint to buffer owner |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) { |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 94 | mFlinger->getTransactionCompletedThread().addPresentedCallbackHandles( |
| 95 | mDrawingState.callbackHandles); |
| 96 | |
| 97 | mDrawingState.callbackHandles = {}; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 100 | bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 101 | if (getSidebandStreamChanged() || getAutoRefresh()) { |
| 102 | return true; |
| 103 | } |
| 104 | |
Marissa Wall | 024a191 | 2018-08-13 13:55:35 -0700 | [diff] [blame] | 105 | return hasFrameUpdate(); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 108 | bool BufferStateLayer::willPresentCurrentTransaction() const { |
| 109 | // Returns true if the most recent Transaction applied to CurrentState will be presented. |
| 110 | return getSidebandStreamChanged() || getAutoRefresh() || |
Valerie Hau | aa19456 | 2019-02-05 16:21:38 -0800 | [diff] [blame] | 111 | (mCurrentState.modified && |
| 112 | (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr)); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 115 | bool BufferStateLayer::getTransformToDisplayInverse() const { |
| 116 | return mCurrentState.transformToDisplayInverse; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 119 | void BufferStateLayer::pushPendingState() { |
| 120 | if (!mCurrentState.modified) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 121 | return; |
| 122 | } |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 123 | mPendingStates.push_back(mCurrentState); |
| 124 | ATRACE_INT(mTransactionName.string(), mPendingStates.size()); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 128 | const bool stateUpdateAvailable = !mPendingStates.empty(); |
| 129 | while (!mPendingStates.empty()) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 130 | popPendingState(stateToCommit); |
| 131 | } |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 132 | mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified; |
| 133 | mCurrentState.modified = false; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 134 | return stateUpdateAvailable; |
| 135 | } |
| 136 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 137 | // Crop that applies to the window |
| 138 | Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const { |
| 139 | return Rect::INVALID_RECT; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | bool BufferStateLayer::setTransform(uint32_t transform) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 143 | if (mCurrentState.transform == transform) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 144 | mCurrentState.transform = transform; |
| 145 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 146 | setTransactionFlags(eTransactionNeeded); |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 151 | if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false; |
| 152 | mCurrentState.sequence++; |
| 153 | mCurrentState.transformToDisplayInverse = transformToDisplayInverse; |
| 154 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 155 | setTransactionFlags(eTransactionNeeded); |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | bool BufferStateLayer::setCrop(const Rect& crop) { |
Marissa Wall | 290ad08 | 2019-03-06 13:23:47 -0800 | [diff] [blame] | 160 | Rect c = crop; |
| 161 | if (c.left < 0) { |
| 162 | c.left = 0; |
| 163 | } |
| 164 | if (c.top < 0) { |
| 165 | c.top = 0; |
| 166 | } |
| 167 | // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below |
| 168 | // treats all invalid rectangles the same. |
| 169 | if (!c.isValid()) { |
| 170 | c.makeInvalid(); |
| 171 | } |
| 172 | |
| 173 | if (mCurrentState.crop == c) return false; |
Marissa Wall | 290ad08 | 2019-03-06 13:23:47 -0800 | [diff] [blame] | 174 | mCurrentState.crop = c; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 175 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 176 | setTransactionFlags(eTransactionNeeded); |
| 177 | return true; |
| 178 | } |
| 179 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 180 | bool BufferStateLayer::setFrame(const Rect& frame) { |
| 181 | int x = frame.left; |
| 182 | int y = frame.top; |
| 183 | int w = frame.getWidth(); |
| 184 | int h = frame.getHeight(); |
| 185 | |
Marissa Wall | 0f3242d | 2018-12-20 15:10:22 -0800 | [diff] [blame] | 186 | if (x < 0) { |
| 187 | x = 0; |
| 188 | w = frame.right; |
| 189 | } |
| 190 | |
| 191 | if (y < 0) { |
| 192 | y = 0; |
| 193 | h = frame.bottom; |
| 194 | } |
| 195 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 196 | if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y && |
| 197 | mCurrentState.active.w == w && mCurrentState.active.h == h) { |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 198 | return false; |
| 199 | } |
| 200 | |
| 201 | if (!frame.isValid()) { |
| 202 | x = y = w = h = 0; |
| 203 | } |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 204 | mCurrentState.active.transform.set(x, y); |
| 205 | mCurrentState.active.w = w; |
| 206 | mCurrentState.active.h = h; |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 207 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 208 | mCurrentState.sequence++; |
| 209 | mCurrentState.modified = true; |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 210 | setTransactionFlags(eTransactionNeeded); |
| 211 | return true; |
| 212 | } |
| 213 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 214 | bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime, |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame^] | 215 | nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 216 | if (mCurrentState.buffer) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 217 | mReleasePreviousBuffer = true; |
| 218 | } |
| 219 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 220 | mCurrentState.buffer = buffer; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame^] | 221 | mCurrentState.clientCacheId = clientCacheId; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 222 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 223 | setTransactionFlags(eTransactionNeeded); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 224 | |
| 225 | mFlinger->mTimeStats->setPostTime(getSequence(), getFrameNumber(), getName().c_str(), postTime); |
| 226 | mDesiredPresentTime = desiredPresentTime; |
| 227 | |
| 228 | if (mFlinger->mUseSmart90ForVideo) { |
| 229 | const nsecs_t presentTime = (mDesiredPresentTime == -1) ? 0 : mDesiredPresentTime; |
| 230 | mFlinger->mScheduler->addLayerPresentTime(mSchedulerLayerHandle, presentTime); |
| 231 | } |
| 232 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | |
| 236 | bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 237 | // The acquire fences of BufferStateLayers have already signaled before they are set |
| 238 | mCallbackHandleAcquireTime = fence->getSignalTime(); |
| 239 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 240 | mCurrentState.acquireFence = fence; |
| 241 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 242 | setTransactionFlags(eTransactionNeeded); |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 247 | if (mCurrentState.dataspace == dataspace) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 248 | mCurrentState.dataspace = dataspace; |
| 249 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 250 | setTransactionFlags(eTransactionNeeded); |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 255 | if (mCurrentState.hdrMetadata == hdrMetadata) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 256 | mCurrentState.hdrMetadata = hdrMetadata; |
| 257 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 258 | setTransactionFlags(eTransactionNeeded); |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 263 | mCurrentState.surfaceDamageRegion = surfaceDamage; |
| 264 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 265 | setTransactionFlags(eTransactionNeeded); |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | bool BufferStateLayer::setApi(int32_t api) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 270 | if (mCurrentState.api == api) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 271 | mCurrentState.api = api; |
| 272 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 273 | setTransactionFlags(eTransactionNeeded); |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 278 | if (mCurrentState.sidebandStream == sidebandStream) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 279 | mCurrentState.sidebandStream = sidebandStream; |
| 280 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 281 | setTransactionFlags(eTransactionNeeded); |
| 282 | |
| 283 | if (!mSidebandStreamChanged.exchange(true)) { |
| 284 | // mSidebandStreamChanged was false |
| 285 | mFlinger->signalLayerUpdate(); |
| 286 | } |
| 287 | return true; |
| 288 | } |
| 289 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 290 | bool BufferStateLayer::setTransactionCompletedListeners( |
| 291 | const std::vector<sp<CallbackHandle>>& handles) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 292 | // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 293 | if (handles.empty()) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 294 | mReleasePreviousBuffer = false; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 295 | return false; |
| 296 | } |
| 297 | |
| 298 | const bool willPresent = willPresentCurrentTransaction(); |
| 299 | |
| 300 | for (const auto& handle : handles) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 301 | // If this transaction set a buffer on this layer, release its previous buffer |
| 302 | handle->releasePreviousBuffer = mReleasePreviousBuffer; |
| 303 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 304 | // If this layer will be presented in this frame |
| 305 | if (willPresent) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 306 | // If this transaction set an acquire fence on this layer, set its acquire time |
| 307 | handle->acquireTime = mCallbackHandleAcquireTime; |
| 308 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 309 | // Notify the transaction completed thread that there is a pending latched callback |
| 310 | // handle |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 311 | mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 312 | |
| 313 | // Store so latched time and release fence can be set |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 314 | mCurrentState.callbackHandles.push_back(handle); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 315 | |
| 316 | } else { // If this layer will NOT need to be relatched and presented this frame |
| 317 | // Notify the transaction completed thread this handle is done |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 318 | mFlinger->getTransactionCompletedThread().addUnpresentedCallbackHandle(handle); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 322 | mReleasePreviousBuffer = false; |
| 323 | mCallbackHandleAcquireTime = -1; |
| 324 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 325 | return willPresent; |
| 326 | } |
| 327 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 328 | bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 329 | mCurrentState.transparentRegionHint = transparent; |
| 330 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 331 | setTransactionFlags(eTransactionNeeded); |
| 332 | return true; |
| 333 | } |
| 334 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 335 | Rect BufferStateLayer::getBufferSize(const State& s) const { |
| 336 | // for buffer state layers we use the display frame size as the buffer size. |
| 337 | if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) { |
| 338 | return Rect(getActiveWidth(s), getActiveHeight(s)); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 341 | // if the display frame is not defined, use the parent bounds as the buffer size. |
| 342 | const auto& p = mDrawingParent.promote(); |
| 343 | if (p != nullptr) { |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 344 | Rect parentBounds = Rect(p->getBounds(Region())); |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 345 | if (!parentBounds.isEmpty()) { |
| 346 | return parentBounds; |
| 347 | } |
| 348 | } |
| 349 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 350 | return Rect::INVALID_RECT; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 351 | } |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 352 | |
| 353 | FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const { |
| 354 | const State& s(getDrawingState()); |
| 355 | // for buffer state layers we use the display frame size as the buffer size. |
| 356 | if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) { |
| 357 | return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s)); |
| 358 | } |
| 359 | |
| 360 | // if the display frame is not defined, use the parent bounds as the buffer size. |
| 361 | return parentBounds; |
| 362 | } |
| 363 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 364 | // ----------------------------------------------------------------------- |
| 365 | |
| 366 | // ----------------------------------------------------------------------- |
| 367 | // Interface implementation for BufferLayer |
| 368 | // ----------------------------------------------------------------------- |
| 369 | bool BufferStateLayer::fenceHasSignaled() const { |
| 370 | if (latchUnsignaledBuffers()) { |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled; |
| 375 | } |
| 376 | |
| 377 | nsecs_t BufferStateLayer::getDesiredPresentTime() { |
Valerie Hau | bc6ddb1 | 2019-03-08 11:10:15 -0800 | [diff] [blame] | 378 | return mDesiredPresentTime; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 381 | std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 382 | return std::make_shared<FenceTime>(getDrawingState().acquireFence); |
| 383 | } |
| 384 | |
| 385 | void BufferStateLayer::getDrawingTransformMatrix(float *matrix) { |
| 386 | std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix); |
| 387 | } |
| 388 | |
| 389 | uint32_t BufferStateLayer::getDrawingTransform() const { |
| 390 | return getDrawingState().transform; |
| 391 | } |
| 392 | |
| 393 | ui::Dataspace BufferStateLayer::getDrawingDataSpace() const { |
| 394 | return getDrawingState().dataspace; |
| 395 | } |
| 396 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 397 | // Crop that applies to the buffer |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 398 | Rect BufferStateLayer::getDrawingCrop() const { |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 399 | const State& s(getDrawingState()); |
| 400 | |
| 401 | if (s.crop.isEmpty() && s.buffer) { |
| 402 | return s.buffer->getBounds(); |
Valerie Hau | 0bc0915 | 2018-12-20 07:42:47 -0800 | [diff] [blame] | 403 | } else if (s.buffer) { |
| 404 | Rect crop = s.crop; |
| 405 | crop.left = std::max(crop.left, 0); |
| 406 | crop.top = std::max(crop.top, 0); |
| 407 | uint32_t bufferWidth = s.buffer->getWidth(); |
| 408 | uint32_t bufferHeight = s.buffer->getHeight(); |
| 409 | if (bufferHeight <= std::numeric_limits<int32_t>::max() && |
| 410 | bufferWidth <= std::numeric_limits<int32_t>::max()) { |
| 411 | crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth)); |
| 412 | crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight)); |
| 413 | } |
| 414 | if (!crop.isValid()) { |
| 415 | // Crop rect is out of bounds, return whole buffer |
| 416 | return s.buffer->getBounds(); |
| 417 | } |
| 418 | return crop; |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 419 | } |
| 420 | return s.crop; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | uint32_t BufferStateLayer::getDrawingScalingMode() const { |
Marissa Wall | ec463ac | 2018-10-08 12:35:04 -0700 | [diff] [blame] | 424 | return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | Region BufferStateLayer::getDrawingSurfaceDamage() const { |
| 428 | return getDrawingState().surfaceDamageRegion; |
| 429 | } |
| 430 | |
| 431 | const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const { |
| 432 | return getDrawingState().hdrMetadata; |
| 433 | } |
| 434 | |
| 435 | int BufferStateLayer::getDrawingApi() const { |
| 436 | return getDrawingState().api; |
| 437 | } |
| 438 | |
| 439 | PixelFormat BufferStateLayer::getPixelFormat() const { |
Marissa Wall | 5aec641 | 2018-11-14 11:49:18 -0800 | [diff] [blame] | 440 | if (!mActiveBuffer) { |
| 441 | return PIXEL_FORMAT_NONE; |
| 442 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 443 | return mActiveBuffer->format; |
| 444 | } |
| 445 | |
| 446 | uint64_t BufferStateLayer::getFrameNumber() const { |
| 447 | return mFrameNumber; |
| 448 | } |
| 449 | |
| 450 | bool BufferStateLayer::getAutoRefresh() const { |
| 451 | // TODO(marissaw): support shared buffer mode |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | bool BufferStateLayer::getSidebandStreamChanged() const { |
| 456 | return mSidebandStreamChanged.load(); |
| 457 | } |
| 458 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 459 | bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 460 | if (mSidebandStreamChanged.exchange(false)) { |
| 461 | const State& s(getDrawingState()); |
| 462 | // mSidebandStreamChanged was true |
Lloyd Pique | 0b785d8 | 2018-12-04 17:25:27 -0800 | [diff] [blame] | 463 | LOG_ALWAYS_FATAL_IF(!getCompositionLayer()); |
| 464 | mSidebandStream = s.sidebandStream; |
| 465 | getCompositionLayer()->editState().frontEnd.sidebandStream = mSidebandStream; |
| 466 | if (mSidebandStream != nullptr) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 467 | setTransactionFlags(eTransactionNeeded); |
| 468 | mFlinger->setTransactionFlags(eTraversalNeeded); |
| 469 | } |
| 470 | recomputeVisibleRegions = true; |
| 471 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 472 | return true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 473 | } |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 474 | return false; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 477 | bool BufferStateLayer::hasFrameUpdate() const { |
Valerie Hau | aa19456 | 2019-02-05 16:21:38 -0800 | [diff] [blame] | 478 | const State& c(getCurrentState()); |
| 479 | return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | void BufferStateLayer::setFilteringEnabled(bool enabled) { |
| 483 | GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop, |
| 484 | mCurrentTransform, enabled); |
| 485 | } |
| 486 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 487 | status_t BufferStateLayer::bindTextureImage() { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 488 | const State& s(getDrawingState()); |
| 489 | auto& engine(mFlinger->getRenderEngine()); |
| 490 | |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 491 | return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 494 | status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 495 | const State& s(getDrawingState()); |
| 496 | |
| 497 | if (!s.buffer) { |
Valerie Hau | aa19456 | 2019-02-05 16:21:38 -0800 | [diff] [blame] | 498 | if (s.bgColorLayer) { |
| 499 | for (auto& handle : mDrawingState.callbackHandles) { |
| 500 | handle->latchTime = latchTime; |
| 501 | } |
| 502 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 503 | return NO_ERROR; |
| 504 | } |
| 505 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 506 | const int32_t layerID = getSequence(); |
| 507 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 508 | // Reject if the layer is invalid |
| 509 | uint32_t bufferWidth = s.buffer->width; |
| 510 | uint32_t bufferHeight = s.buffer->height; |
| 511 | |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 512 | if (s.transform & ui::Transform::ROT_90) { |
Peiyong Lin | 3db4234 | 2018-08-16 09:15:59 -0700 | [diff] [blame] | 513 | std::swap(bufferWidth, bufferHeight); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | if (s.transformToDisplayInverse) { |
| 517 | uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform(); |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 518 | if (invTransform & ui::Transform::ROT_90) { |
Peiyong Lin | 3db4234 | 2018-08-16 09:15:59 -0700 | [diff] [blame] | 519 | std::swap(bufferWidth, bufferHeight); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 523 | if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE && |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 524 | (s.active.w != bufferWidth || s.active.h != bufferHeight)) { |
| 525 | ALOGE("[%s] rejecting buffer: " |
| 526 | "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}", |
| 527 | mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h); |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 528 | mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber()); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 529 | return BAD_VALUE; |
| 530 | } |
| 531 | |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 532 | for (auto& handle : mDrawingState.callbackHandles) { |
| 533 | handle->latchTime = latchTime; |
| 534 | } |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 535 | |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 536 | if (!SyncFeatures::getInstance().useNativeFenceSync()) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 537 | // Bind the new buffer to the GL texture. |
| 538 | // |
| 539 | // Older devices require the "implicit" synchronization provided |
| 540 | // by glEGLImageTargetTexture2DOES, which this method calls. Newer |
| 541 | // devices will either call this in Layer::onDraw, or (if it's not |
| 542 | // a GL-composited layer) not at all. |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 543 | status_t err = bindTextureImage(); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 544 | if (err != NO_ERROR) { |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 545 | mFlinger->mTimeStats->onDestroy(layerID); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 546 | return BAD_VALUE; |
| 547 | } |
| 548 | } |
| 549 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 550 | mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime()); |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 551 | mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 552 | |
Marissa Wall | 16c112d | 2019-03-20 13:21:13 -0700 | [diff] [blame] | 553 | mCurrentStateModified = false; |
| 554 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 555 | return NO_ERROR; |
| 556 | } |
| 557 | |
| 558 | status_t BufferStateLayer::updateActiveBuffer() { |
| 559 | const State& s(getDrawingState()); |
| 560 | |
| 561 | if (s.buffer == nullptr) { |
| 562 | return BAD_VALUE; |
| 563 | } |
| 564 | |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 565 | if (mActiveBuffer != nullptr) { |
| 566 | // todo: get this to work with BufferStateLayerCache |
| 567 | auto& engine(mFlinger->getRenderEngine()); |
| 568 | engine.unbindExternalTextureBuffer(mActiveBuffer->getId()); |
| 569 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 570 | mActiveBuffer = s.buffer; |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 571 | mActiveBufferFence = s.acquireFence; |
Lloyd Pique | 0b785d8 | 2018-12-04 17:25:27 -0800 | [diff] [blame] | 572 | auto& layerCompositionState = getCompositionLayer()->editState().frontEnd; |
| 573 | layerCompositionState.buffer = mActiveBuffer; |
| 574 | layerCompositionState.bufferSlot = 0; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 575 | |
| 576 | return NO_ERROR; |
| 577 | } |
| 578 | |
| 579 | status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) { |
| 580 | // TODO(marissaw): support frame history events |
| 581 | mCurrentFrameNumber = mFrameNumber; |
| 582 | return NO_ERROR; |
| 583 | } |
| 584 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 585 | void BufferStateLayer::setHwcLayerBuffer(const sp<const DisplayDevice>& display) { |
| 586 | const auto outputLayer = findOutputLayerForDisplay(display); |
| 587 | LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc); |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 588 | auto& hwcInfo = *outputLayer->editState().hwc; |
| 589 | auto& hwcLayer = hwcInfo.hwcLayer; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 590 | |
| 591 | const State& s(getDrawingState()); |
| 592 | |
Valerie Hau | 13f0d1a | 2019-03-22 10:35:42 -0700 | [diff] [blame] | 593 | uint32_t hwcSlot; |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 594 | sp<GraphicBuffer> buffer; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame^] | 595 | hwcInfo.hwcBufferCache.getHwcBuffer(mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId), |
| 596 | s.buffer, &hwcSlot, &buffer); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 597 | |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 598 | auto error = hwcLayer->setBuffer(hwcSlot, buffer, s.acquireFence); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 599 | if (error != HWC2::Error::None) { |
| 600 | ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(), |
| 601 | s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error)); |
| 602 | } |
| 603 | |
| 604 | mFrameNumber++; |
| 605 | } |
| 606 | |
| 607 | void BufferStateLayer::onFirstRef() { |
Dan Stoza | 7b1b5a8 | 2018-07-31 16:00:21 -0700 | [diff] [blame] | 608 | BufferLayer::onFirstRef(); |
| 609 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 610 | if (const auto display = mFlinger->getDefaultDisplayDevice()) { |
| 611 | updateTransformHint(display); |
| 612 | } |
| 613 | } |
| 614 | |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame^] | 615 | void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) { |
| 616 | std::lock_guard lock(mMutex); |
| 617 | if (!clientCacheId.isValid()) { |
| 618 | ALOGE("invalid process, failed to erase buffer"); |
| 619 | return; |
| 620 | } |
| 621 | eraseBufferLocked(clientCacheId); |
| 622 | } |
| 623 | |
| 624 | uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) { |
| 625 | std::lock_guard<std::mutex> lock(mMutex); |
| 626 | auto itr = mCachedBuffers.find(clientCacheId); |
| 627 | if (itr == mCachedBuffers.end()) { |
| 628 | return addCachedBuffer(clientCacheId); |
| 629 | } |
| 630 | auto& [hwcCacheSlot, counter] = itr->second; |
| 631 | counter = mCounter++; |
| 632 | return hwcCacheSlot; |
| 633 | } |
| 634 | |
| 635 | uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId) |
| 636 | REQUIRES(mMutex) { |
| 637 | if (!clientCacheId.isValid()) { |
| 638 | ALOGE("invalid process, returning invalid slot"); |
| 639 | return BufferQueue::INVALID_BUFFER_SLOT; |
| 640 | } |
| 641 | |
| 642 | ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this)); |
| 643 | |
| 644 | uint32_t hwcCacheSlot = getFreeHwcCacheSlot(); |
| 645 | mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++}; |
| 646 | return hwcCacheSlot; |
| 647 | } |
| 648 | |
| 649 | uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) { |
| 650 | if (mFreeHwcCacheSlots.empty()) { |
| 651 | evictLeastRecentlyUsed(); |
| 652 | } |
| 653 | |
| 654 | uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top(); |
| 655 | mFreeHwcCacheSlots.pop(); |
| 656 | return hwcCacheSlot; |
| 657 | } |
| 658 | |
| 659 | void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) { |
| 660 | uint64_t minCounter = UINT_MAX; |
| 661 | client_cache_t minClientCacheId = {}; |
| 662 | for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) { |
| 663 | const auto& [hwcCacheSlot, counter] = slotCounter; |
| 664 | if (counter < minCounter) { |
| 665 | minCounter = counter; |
| 666 | minClientCacheId = clientCacheId; |
| 667 | } |
| 668 | } |
| 669 | eraseBufferLocked(minClientCacheId); |
| 670 | |
| 671 | ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this); |
| 672 | } |
| 673 | |
| 674 | void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId) |
| 675 | REQUIRES(mMutex) { |
| 676 | auto itr = mCachedBuffers.find(clientCacheId); |
| 677 | if (itr == mCachedBuffers.end()) { |
| 678 | return; |
| 679 | } |
| 680 | auto& [hwcCacheSlot, counter] = itr->second; |
| 681 | |
| 682 | // TODO send to hwc cache and resources |
| 683 | |
| 684 | mFreeHwcCacheSlots.push(hwcCacheSlot); |
| 685 | mCachedBuffers.erase(clientCacheId); |
| 686 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 687 | } // namespace android |