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