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