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