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