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