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