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