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