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