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" |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 20 | #pragma clang diagnostic ignored "-Wextra" |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 21 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 22 | //#define LOG_NDEBUG 0 |
| 23 | #undef LOG_TAG |
| 24 | #define LOG_TAG "BufferStateLayer" |
| 25 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 26 | |
Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 27 | #include "BufferStateLayer.h" |
| 28 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 29 | #include <limits> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 30 | |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 31 | #include <FrameTimeline/FrameTimeline.h> |
Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 32 | #include <compositionengine/LayerFECompositionState.h> |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 33 | #include <gui/BufferQueue.h> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 34 | #include <private/gui/SyncFeatures.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 35 | #include <renderengine/Image.h> |
Robert Carr | 38d2500 | 2021-06-11 14:30:09 -0700 | [diff] [blame^] | 36 | #include "TunnelModeEnabledReporter.h" |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 37 | |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 38 | #include "EffectLayer.h" |
Adithya Srinivasan | b238cd5 | 2021-02-04 17:54:05 +0000 | [diff] [blame] | 39 | #include "FrameTracer/FrameTracer.h" |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 40 | #include "TimeStats/TimeStats.h" |
Valerie Hau | 0bc0915 | 2018-12-20 07:42:47 -0800 | [diff] [blame] | 41 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 42 | namespace android { |
| 43 | |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 44 | using PresentState = frametimeline::SurfaceFrame::PresentState; |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 45 | namespace { |
| 46 | void callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener, |
Robert Carr | 97e7cc0 | 2021-06-07 10:45:40 -0700 | [diff] [blame] | 47 | const sp<GraphicBuffer>& buffer, const sp<Fence>& releaseFence, |
| 48 | uint32_t transformHint) { |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 49 | if (!listener) { |
| 50 | return; |
| 51 | } |
Robert Carr | 97e7cc0 | 2021-06-07 10:45:40 -0700 | [diff] [blame] | 52 | listener->onReleaseBuffer(buffer->getId(), releaseFence ? releaseFence : Fence::NO_FENCE, |
| 53 | transformHint); |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 54 | } |
| 55 | } // namespace |
| 56 | |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 57 | // clang-format off |
| 58 | const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{ |
| 59 | 1, 0, 0, 0, |
| 60 | 0, 1, 0, 0, |
| 61 | 0, 0, 1, 0, |
| 62 | 0, 0, 0, 1 |
| 63 | }; |
| 64 | // clang-format on |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 65 | |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 66 | BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) |
| 67 | : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) { |
Marissa Wall | 3ff826c | 2019-02-07 11:58:25 -0800 | [diff] [blame] | 68 | mCurrentState.dataspace = ui::Dataspace::V0_SRGB; |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 69 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 70 | |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 71 | BufferStateLayer::~BufferStateLayer() { |
chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 72 | // The original layer and the clone layer share the same texture and buffer. Therefore, only |
| 73 | // one of the layers, in this case the original layer, needs to handle the deletion. The |
| 74 | // original layer and the clone should be removed at the same time so there shouldn't be any |
| 75 | // issue with the clone layer trying to use the texture. |
| 76 | if (mBufferInfo.mBuffer != nullptr && !isClone()) { |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 77 | callReleaseBufferCallback(mDrawingState.releaseBufferListener, |
Robert Carr | 97e7cc0 | 2021-06-07 10:45:40 -0700 | [diff] [blame] | 78 | mBufferInfo.mBuffer->getBuffer(), mBufferInfo.mFence, |
| 79 | mTransformHint); |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Robert Carr | 8d95853 | 2020-11-10 14:09:16 -0800 | [diff] [blame] | 83 | status_t BufferStateLayer::addReleaseFence(const sp<CallbackHandle>& ch, |
| 84 | const sp<Fence>& fence) { |
| 85 | if (ch == nullptr) { |
| 86 | return OK; |
| 87 | } |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 88 | ch->previousBufferId = mPreviousBufferId; |
Robert Carr | 8d95853 | 2020-11-10 14:09:16 -0800 | [diff] [blame] | 89 | if (!ch->previousReleaseFence.get()) { |
| 90 | ch->previousReleaseFence = fence; |
| 91 | return OK; |
| 92 | } |
| 93 | |
| 94 | // Below logic is lifted from ConsumerBase.cpp: |
| 95 | // Check status of fences first because merging is expensive. |
| 96 | // Merging an invalid fence with any other fence results in an |
| 97 | // invalid fence. |
| 98 | auto currentStatus = ch->previousReleaseFence->getStatus(); |
| 99 | if (currentStatus == Fence::Status::Invalid) { |
| 100 | ALOGE("Existing fence has invalid state, layer: %s", mName.c_str()); |
| 101 | return BAD_VALUE; |
| 102 | } |
| 103 | |
| 104 | auto incomingStatus = fence->getStatus(); |
| 105 | if (incomingStatus == Fence::Status::Invalid) { |
| 106 | ALOGE("New fence has invalid state, layer: %s", mName.c_str()); |
| 107 | ch->previousReleaseFence = fence; |
| 108 | return BAD_VALUE; |
| 109 | } |
| 110 | |
| 111 | // If both fences are signaled or both are unsignaled, we need to merge |
| 112 | // them to get an accurate timestamp. |
| 113 | if (currentStatus == incomingStatus) { |
| 114 | char fenceName[32] = {}; |
| 115 | snprintf(fenceName, 32, "%.28s", mName.c_str()); |
| 116 | sp<Fence> mergedFence = Fence::merge( |
| 117 | fenceName, ch->previousReleaseFence, fence); |
| 118 | if (!mergedFence.get()) { |
| 119 | ALOGE("failed to merge release fences, layer: %s", mName.c_str()); |
| 120 | // synchronization is broken, the best we can do is hope fences |
| 121 | // signal in order so the new fence will act like a union |
| 122 | ch->previousReleaseFence = fence; |
| 123 | return BAD_VALUE; |
| 124 | } |
| 125 | ch->previousReleaseFence = mergedFence; |
| 126 | } else if (incomingStatus == Fence::Status::Unsignaled) { |
| 127 | // If one fence has signaled and the other hasn't, the unsignaled |
| 128 | // fence will approximately correspond with the correct timestamp. |
| 129 | // There's a small race if both fences signal at about the same time |
| 130 | // and their statuses are retrieved with unfortunate timing. However, |
| 131 | // by this point, they will have both signaled and only the timestamp |
| 132 | // will be slightly off; any dependencies after this point will |
| 133 | // already have been met. |
| 134 | ch->previousReleaseFence = fence; |
| 135 | } |
| 136 | // else if (currentStatus == Fence::Status::Unsignaled) is a no-op. |
| 137 | |
| 138 | return OK; |
| 139 | } |
| 140 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 141 | // ----------------------------------------------------------------------- |
| 142 | // Interface implementation for Layer |
| 143 | // ----------------------------------------------------------------------- |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 144 | void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) { |
Robert Carr | 8d95853 | 2020-11-10 14:09:16 -0800 | [diff] [blame] | 145 | if (!releaseFence->isValid()) { |
| 146 | return; |
| 147 | } |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 148 | // The previous release fence notifies the client that SurfaceFlinger is done with the previous |
| 149 | // buffer that was presented on this layer. The first transaction that came in this frame that |
| 150 | // replaced the previous buffer on this layer needs this release fence, because the fence will |
| 151 | // let the client know when that previous buffer is removed from the screen. |
| 152 | // |
| 153 | // Every other transaction on this layer does not need a release fence because no other |
| 154 | // Transactions that were set on this layer this frame are going to have their preceeding buffer |
| 155 | // removed from the display this frame. |
| 156 | // |
| 157 | // For example, if we have 3 transactions this frame. The first transaction doesn't contain a |
| 158 | // buffer so it doesn't need a previous release fence because the layer still needs the previous |
| 159 | // buffer. The second transaction contains a buffer so it needs a previous release fence because |
| 160 | // the previous buffer will be released this frame. The third transaction also contains a |
| 161 | // buffer. It replaces the buffer in the second transaction. The buffer in the second |
| 162 | // transaction will now no longer be presented so it is released immediately and the third |
| 163 | // transaction doesn't need a previous release fence. |
Robert Carr | 8d95853 | 2020-11-10 14:09:16 -0800 | [diff] [blame] | 164 | sp<CallbackHandle> ch; |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 165 | for (auto& handle : mDrawingState.callbackHandles) { |
| 166 | if (handle->releasePreviousBuffer) { |
Robert Carr | 8d95853 | 2020-11-10 14:09:16 -0800 | [diff] [blame] | 167 | ch = handle; |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 168 | break; |
| 169 | } |
| 170 | } |
Robert Carr | 8d95853 | 2020-11-10 14:09:16 -0800 | [diff] [blame] | 171 | auto status = addReleaseFence(ch, releaseFence); |
| 172 | if (status != OK) { |
| 173 | ALOGE("Failed to add release fence for layer %s", getName().c_str()); |
| 174 | } |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 175 | |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 176 | mPreviousReleaseFence = releaseFence; |
| 177 | |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 178 | // Prevent tracing the same release multiple times. |
| 179 | if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) { |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 180 | mPreviousReleasedFrameNumber = mPreviousFrameNumber; |
| 181 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Jorim Jaggi | 9c03b50 | 2020-11-24 23:51:31 +0100 | [diff] [blame] | 184 | void BufferStateLayer::onSurfaceFrameCreated( |
| 185 | const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) { |
Adithya Srinivasan | d17c7da | 2021-03-05 20:43:32 +0000 | [diff] [blame] | 186 | while (mPendingJankClassifications.size() >= kPendingClassificationMaxSurfaceFrames) { |
| 187 | // Too many SurfaceFrames pending classification. The front of the deque is probably not |
| 188 | // tracked by FrameTimeline and will never be presented. This will only result in a memory |
| 189 | // leak. |
| 190 | ALOGW("Removing the front of pending jank deque from layer - %s to prevent memory leak", |
| 191 | mName.c_str()); |
Adithya Srinivasan | 785addd | 2021-03-09 00:38:00 +0000 | [diff] [blame] | 192 | std::string miniDump = mPendingJankClassifications.front()->miniDump(); |
| 193 | ALOGD("Head SurfaceFrame mini dump\n%s", miniDump.c_str()); |
Adithya Srinivasan | d17c7da | 2021-03-05 20:43:32 +0000 | [diff] [blame] | 194 | mPendingJankClassifications.pop_front(); |
| 195 | } |
Jorim Jaggi | 9c03b50 | 2020-11-24 23:51:31 +0100 | [diff] [blame] | 196 | mPendingJankClassifications.emplace_back(surfaceFrame); |
| 197 | } |
| 198 | |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 199 | void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) { |
Valerie Hau | 32cdc1f | 2019-10-21 14:45:54 -0700 | [diff] [blame] | 200 | for (const auto& handle : mDrawingState.callbackHandles) { |
| 201 | handle->transformHint = mTransformHint; |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 202 | handle->dequeueReadyTime = dequeueReadyTime; |
Valerie Hau | 32cdc1f | 2019-10-21 14:45:54 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 205 | // If there are multiple transactions in this frame, set the previous id on the earliest |
| 206 | // transacton. We don't need to pass in the released buffer id to multiple transactions. |
| 207 | // The buffer id does not have to correspond to any particular transaction as long as the |
| 208 | // listening end point is the same but the client expects the first transaction callback that |
| 209 | // replaces the presented buffer to contain the release fence. This follows the same logic. |
| 210 | // see BufferStateLayer::onLayerDisplayed. |
| 211 | for (auto& handle : mDrawingState.callbackHandles) { |
| 212 | if (handle->releasePreviousBuffer) { |
| 213 | handle->previousBufferId = mPreviousBufferId; |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
Jorim Jaggi | 9c03b50 | 2020-11-24 23:51:31 +0100 | [diff] [blame] | 218 | std::vector<JankData> jankData; |
| 219 | jankData.reserve(mPendingJankClassifications.size()); |
| 220 | while (!mPendingJankClassifications.empty() |
| 221 | && mPendingJankClassifications.front()->getJankType()) { |
| 222 | std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame = |
| 223 | mPendingJankClassifications.front(); |
| 224 | mPendingJankClassifications.pop_front(); |
| 225 | jankData.emplace_back( |
| 226 | JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value())); |
| 227 | } |
| 228 | |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 229 | mFlinger->getTransactionCallbackInvoker().finalizePendingCallbackHandles( |
Jorim Jaggi | 9c03b50 | 2020-11-24 23:51:31 +0100 | [diff] [blame] | 230 | mDrawingState.callbackHandles, jankData); |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 231 | |
| 232 | mDrawingState.callbackHandles = {}; |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 233 | |
| 234 | const sp<Fence>& releaseFence(mPreviousReleaseFence); |
| 235 | std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(releaseFence); |
| 236 | { |
| 237 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 238 | if (mPreviousFrameNumber != 0) { |
| 239 | mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime, |
| 240 | std::move(releaseFenceTime)); |
| 241 | } |
| 242 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 245 | void BufferStateLayer::finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence, |
| 246 | const CompositorTiming& compositorTiming) { |
| 247 | for (const auto& handle : mDrawingState.callbackHandles) { |
| 248 | handle->gpuCompositionDoneFence = glDoneFence; |
| 249 | handle->compositorTiming = compositorTiming; |
| 250 | } |
| 251 | } |
| 252 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 253 | bool BufferStateLayer::willPresentCurrentTransaction() const { |
| 254 | // 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] | 255 | return (getSidebandStreamChanged() || getAutoRefresh() || |
Valerie Hau | aa19456 | 2019-02-05 16:21:38 -0800 | [diff] [blame] | 256 | (mCurrentState.modified && |
chaviw | 8ba8b07 | 2021-01-25 14:55:46 -0800 | [diff] [blame] | 257 | (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr))); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 260 | Rect BufferStateLayer::getCrop(const Layer::State& s) const { |
| 261 | return s.crop; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | bool BufferStateLayer::setTransform(uint32_t transform) { |
chaviw | 766c9c5 | 2021-02-10 17:36:47 -0800 | [diff] [blame] | 265 | if (mCurrentState.bufferTransform == transform) return false; |
| 266 | mCurrentState.bufferTransform = transform; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 267 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 268 | setTransactionFlags(eTransactionNeeded); |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 273 | if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false; |
| 274 | mCurrentState.sequence++; |
| 275 | mCurrentState.transformToDisplayInverse = transformToDisplayInverse; |
| 276 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 277 | setTransactionFlags(eTransactionNeeded); |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | bool BufferStateLayer::setCrop(const Rect& crop) { |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 282 | if (mCurrentState.crop == crop) return false; |
| 283 | mCurrentState.sequence++; |
| 284 | mCurrentState.crop = crop; |
Marissa Wall | 290ad08 | 2019-03-06 13:23:47 -0800 | [diff] [blame] | 285 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 286 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 287 | setTransactionFlags(eTransactionNeeded); |
| 288 | return true; |
| 289 | } |
| 290 | |
chaviw | f3f40fe | 2021-04-27 15:54:02 -0500 | [diff] [blame] | 291 | bool BufferStateLayer::setBufferCrop(const Rect& bufferCrop) { |
| 292 | if (mCurrentState.bufferCrop == bufferCrop) return false; |
| 293 | |
| 294 | mCurrentState.sequence++; |
| 295 | mCurrentState.bufferCrop = bufferCrop; |
| 296 | |
| 297 | mCurrentState.modified = true; |
| 298 | setTransactionFlags(eTransactionNeeded); |
| 299 | return true; |
| 300 | } |
| 301 | |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 302 | bool BufferStateLayer::setDestinationFrame(const Rect& destinationFrame) { |
| 303 | if (mCurrentState.destinationFrame == destinationFrame) return false; |
| 304 | |
| 305 | mCurrentState.sequence++; |
| 306 | mCurrentState.destinationFrame = destinationFrame; |
| 307 | |
| 308 | mCurrentState.modified = true; |
| 309 | setTransactionFlags(eTransactionNeeded); |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | // Translate destination frame into scale and position. If a destination frame is not set, use the |
| 314 | // provided scale and position |
| 315 | void BufferStateLayer::updateGeometry() { |
| 316 | if (mCurrentState.destinationFrame.isEmpty()) { |
| 317 | // If destination frame is not set, use the requested transform set via |
| 318 | // BufferStateLayer::setPosition and BufferStateLayer::setMatrix. |
| 319 | mCurrentState.transform = mRequestedTransform; |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | Rect destRect = mCurrentState.destinationFrame; |
| 324 | int32_t destW = destRect.width(); |
| 325 | int32_t destH = destRect.height(); |
| 326 | if (destRect.left < 0) { |
| 327 | destRect.left = 0; |
| 328 | destRect.right = destW; |
| 329 | } |
| 330 | if (destRect.top < 0) { |
| 331 | destRect.top = 0; |
| 332 | destRect.bottom = destH; |
| 333 | } |
| 334 | |
| 335 | if (!mCurrentState.buffer) { |
| 336 | ui::Transform t; |
| 337 | t.set(destRect.left, destRect.top); |
| 338 | mCurrentState.transform = t; |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | uint32_t bufferWidth = mCurrentState.buffer->getBuffer()->getWidth(); |
| 343 | uint32_t bufferHeight = mCurrentState.buffer->getBuffer()->getHeight(); |
| 344 | // Undo any transformations on the buffer. |
| 345 | if (mCurrentState.bufferTransform & ui::Transform::ROT_90) { |
| 346 | std::swap(bufferWidth, bufferHeight); |
| 347 | } |
| 348 | uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags(); |
| 349 | if (mCurrentState.transformToDisplayInverse) { |
| 350 | if (invTransform & ui::Transform::ROT_90) { |
| 351 | std::swap(bufferWidth, bufferHeight); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | float sx = destW / static_cast<float>(bufferWidth); |
| 356 | float sy = destH / static_cast<float>(bufferHeight); |
| 357 | ui::Transform t; |
| 358 | t.set(sx, 0, 0, sy); |
| 359 | t.set(destRect.left, destRect.top); |
| 360 | mCurrentState.transform = t; |
| 361 | return; |
| 362 | } |
| 363 | |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 364 | bool BufferStateLayer::setMatrix(const layer_state_t::matrix22_t& matrix, |
| 365 | bool allowNonRectPreservingTransforms) { |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 366 | if (mRequestedTransform.dsdx() == matrix.dsdx && mRequestedTransform.dtdy() == matrix.dtdy && |
| 367 | mRequestedTransform.dtdx() == matrix.dtdx && mRequestedTransform.dsdy() == matrix.dsdy) { |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 368 | return false; |
| 369 | } |
| 370 | |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 371 | ui::Transform t; |
| 372 | t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy); |
| 373 | |
| 374 | if (!allowNonRectPreservingTransforms && !t.preserveRects()) { |
| 375 | ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor " |
| 376 | "ROTATE_SURFACE_FLINGER ignored"); |
| 377 | return false; |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 378 | } |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 379 | |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 380 | mRequestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy); |
chaviw | 9a93ea6 | 2021-03-11 16:44:42 -0600 | [diff] [blame] | 381 | |
| 382 | mCurrentState.sequence++; |
| 383 | mCurrentState.modified = true; |
| 384 | setTransactionFlags(eTransactionNeeded); |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 385 | |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | bool BufferStateLayer::setPosition(float x, float y) { |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 390 | if (mRequestedTransform.tx() == x && mRequestedTransform.ty() == y) { |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 391 | return false; |
| 392 | } |
| 393 | |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 394 | mRequestedTransform.set(x, y); |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 395 | |
| 396 | mCurrentState.sequence++; |
| 397 | mCurrentState.modified = true; |
| 398 | setTransactionFlags(eTransactionNeeded); |
| 399 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 400 | return true; |
| 401 | } |
| 402 | |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 403 | bool BufferStateLayer::addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime, |
| 404 | nsecs_t desiredPresentTime) { |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 405 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 406 | mAcquireTimeline.updateSignalTimes(); |
| 407 | std::shared_ptr<FenceTime> acquireFenceTime = |
| 408 | std::make_shared<FenceTime>((acquireFence ? acquireFence : Fence::NO_FENCE)); |
| 409 | NewFrameEventsEntry newTimestamps = {mCurrentState.frameNumber, postedTime, desiredPresentTime, |
| 410 | acquireFenceTime}; |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 411 | mFrameEventHistory.setProducerWantsEvents(); |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 412 | mFrameEventHistory.addQueue(newTimestamps); |
| 413 | return true; |
| 414 | } |
| 415 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 416 | bool BufferStateLayer::setBuffer(const std::shared_ptr<renderengine::ExternalTexture>& buffer, |
| 417 | const sp<Fence>& acquireFence, nsecs_t postTime, |
| 418 | nsecs_t desiredPresentTime, bool isAutoTimestamp, |
Vishnu Nair | adf632b | 2021-01-07 14:05:08 -0800 | [diff] [blame] | 419 | const client_cache_t& clientCacheId, uint64_t frameNumber, |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 420 | std::optional<nsecs_t> dequeueTime, const FrameTimelineInfo& info, |
| 421 | const sp<ITransactionCompletedListener>& releaseBufferListener) { |
Robert Carr | 0c1966e | 2020-10-19 12:12:08 -0700 | [diff] [blame] | 422 | ATRACE_CALL(); |
| 423 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 424 | if (mCurrentState.buffer) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 425 | mReleasePreviousBuffer = true; |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 426 | if (!mDrawingState.buffer || |
| 427 | mCurrentState.buffer->getBuffer() != mDrawingState.buffer->getBuffer()) { |
Robert Carr | 7121caf | 2020-12-15 13:07:32 -0800 | [diff] [blame] | 428 | // If mCurrentState has a buffer, and we are about to update again |
| 429 | // before swapping to drawing state, then the first buffer will be |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 430 | // dropped and we should decrement the pending buffer count and |
| 431 | // call any release buffer callbacks if set. |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 432 | callReleaseBufferCallback(mCurrentState.releaseBufferListener, |
| 433 | mCurrentState.buffer->getBuffer(), |
Robert Carr | 97e7cc0 | 2021-06-07 10:45:40 -0700 | [diff] [blame] | 434 | mCurrentState.acquireFence, |
| 435 | mTransformHint); |
Robert Carr | 7121caf | 2020-12-15 13:07:32 -0800 | [diff] [blame] | 436 | decrementPendingBufferCount(); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 437 | if (mCurrentState.bufferSurfaceFrameTX != nullptr) { |
| 438 | addSurfaceFrameDroppedForBuffer(mCurrentState.bufferSurfaceFrameTX); |
| 439 | mCurrentState.bufferSurfaceFrameTX.reset(); |
| 440 | } |
Robert Carr | 7121caf | 2020-12-15 13:07:32 -0800 | [diff] [blame] | 441 | } |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 442 | } |
Vishnu Nair | 6b7c5c9 | 2020-09-29 17:27:05 -0700 | [diff] [blame] | 443 | mCurrentState.frameNumber = frameNumber; |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 444 | mCurrentState.releaseBufferListener = releaseBufferListener; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 445 | mCurrentState.buffer = buffer; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 446 | mCurrentState.clientCacheId = clientCacheId; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 447 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 448 | setTransactionFlags(eTransactionNeeded); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 449 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 450 | const int32_t layerId = getSequence(); |
Valerie Hau | 134651a | 2020-01-28 16:21:22 -0800 | [diff] [blame] | 451 | mFlinger->mTimeStats->setPostTime(layerId, mCurrentState.frameNumber, getName().c_str(), |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 452 | mOwnerUid, postTime, getGameMode()); |
chaviw | fa67b55 | 2019-08-12 16:51:55 -0700 | [diff] [blame] | 453 | mCurrentState.desiredPresentTime = desiredPresentTime; |
Ady Abraham | f0c5649 | 2020-12-17 18:04:15 -0800 | [diff] [blame] | 454 | mCurrentState.isAutoTimestamp = isAutoTimestamp; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 455 | |
Ady Abraham | b7f1556 | 2021-03-15 18:34:08 -0700 | [diff] [blame] | 456 | const nsecs_t presentTime = [&] { |
| 457 | if (!isAutoTimestamp) return desiredPresentTime; |
| 458 | |
| 459 | const auto prediction = |
| 460 | mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId); |
| 461 | if (prediction.has_value()) return prediction->presentTime; |
| 462 | |
| 463 | return static_cast<nsecs_t>(0); |
| 464 | }(); |
| 465 | mFlinger->mScheduler->recordLayerHistory(this, presentTime, |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 466 | LayerHistory::LayerUpdateType::Buffer); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 467 | |
Ady Abraham | f0c5649 | 2020-12-17 18:04:15 -0800 | [diff] [blame] | 468 | addFrameEvent(acquireFence, postTime, isAutoTimestamp ? 0 : desiredPresentTime); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 469 | |
Adithya Srinivasan | 891004e | 2021-02-12 20:20:47 +0000 | [diff] [blame] | 470 | setFrameTimelineVsyncForBufferTransaction(info, postTime); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 471 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 472 | if (buffer && dequeueTime && *dequeueTime != 0) { |
| 473 | const uint64_t bufferId = buffer->getBuffer()->getId(); |
Adithya Srinivasan | b238cd5 | 2021-02-04 17:54:05 +0000 | [diff] [blame] | 474 | mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str()); |
| 475 | mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime, |
| 476 | FrameTracer::FrameEvent::DEQUEUE); |
| 477 | mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime, |
| 478 | FrameTracer::FrameEvent::QUEUE); |
| 479 | } |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 480 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 481 | mCurrentState.width = mCurrentState.buffer->getBuffer()->getWidth(); |
| 482 | mCurrentState.height = mCurrentState.buffer->getBuffer()->getHeight(); |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 483 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 484 | return true; |
| 485 | } |
| 486 | |
| 487 | bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 488 | mCurrentState.acquireFence = fence; |
Ady Abraham | 6c1b7ac | 2021-03-31 16:56:03 -0700 | [diff] [blame] | 489 | mCurrentState.acquireFenceTime = std::make_unique<FenceTime>(fence); |
| 490 | |
| 491 | // The acquire fences of BufferStateLayers have already signaled before they are set |
| 492 | mCallbackHandleAcquireTime = mCurrentState.acquireFenceTime->getSignalTime(); |
| 493 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 494 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 495 | setTransactionFlags(eTransactionNeeded); |
| 496 | return true; |
| 497 | } |
| 498 | |
| 499 | bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 500 | if (mCurrentState.dataspace == dataspace) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 501 | mCurrentState.dataspace = dataspace; |
| 502 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 503 | setTransactionFlags(eTransactionNeeded); |
| 504 | return true; |
| 505 | } |
| 506 | |
| 507 | bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 508 | if (mCurrentState.hdrMetadata == hdrMetadata) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 509 | mCurrentState.hdrMetadata = hdrMetadata; |
| 510 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 511 | setTransactionFlags(eTransactionNeeded); |
| 512 | return true; |
| 513 | } |
| 514 | |
| 515 | bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 516 | mCurrentState.surfaceDamageRegion = surfaceDamage; |
| 517 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 518 | setTransactionFlags(eTransactionNeeded); |
| 519 | return true; |
| 520 | } |
| 521 | |
| 522 | bool BufferStateLayer::setApi(int32_t api) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 523 | if (mCurrentState.api == api) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 524 | mCurrentState.api = api; |
| 525 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 526 | setTransactionFlags(eTransactionNeeded); |
| 527 | return true; |
| 528 | } |
| 529 | |
| 530 | bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 531 | if (mCurrentState.sidebandStream == sidebandStream) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 532 | mCurrentState.sidebandStream = sidebandStream; |
| 533 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 534 | setTransactionFlags(eTransactionNeeded); |
| 535 | |
| 536 | if (!mSidebandStreamChanged.exchange(true)) { |
| 537 | // mSidebandStreamChanged was false |
| 538 | mFlinger->signalLayerUpdate(); |
| 539 | } |
| 540 | return true; |
| 541 | } |
| 542 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 543 | bool BufferStateLayer::setTransactionCompletedListeners( |
| 544 | const std::vector<sp<CallbackHandle>>& handles) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 545 | // 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] | 546 | if (handles.empty()) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 547 | mReleasePreviousBuffer = false; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 548 | return false; |
| 549 | } |
| 550 | |
| 551 | const bool willPresent = willPresentCurrentTransaction(); |
| 552 | |
| 553 | for (const auto& handle : handles) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 554 | // If this transaction set a buffer on this layer, release its previous buffer |
| 555 | handle->releasePreviousBuffer = mReleasePreviousBuffer; |
| 556 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 557 | // If this layer will be presented in this frame |
| 558 | if (willPresent) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 559 | // If this transaction set an acquire fence on this layer, set its acquire time |
| 560 | handle->acquireTime = mCallbackHandleAcquireTime; |
Vishnu Nair | 935590e | 2021-02-10 13:05:52 -0800 | [diff] [blame] | 561 | handle->frameNumber = mCurrentState.frameNumber; |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 562 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 563 | // Notify the transaction completed thread that there is a pending latched callback |
| 564 | // handle |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 565 | mFlinger->getTransactionCallbackInvoker().registerPendingCallbackHandle(handle); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 566 | |
| 567 | // Store so latched time and release fence can be set |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 568 | mCurrentState.callbackHandles.push_back(handle); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 569 | |
| 570 | } else { // If this layer will NOT need to be relatched and presented this frame |
| 571 | // Notify the transaction completed thread this handle is done |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 572 | mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 576 | mReleasePreviousBuffer = false; |
| 577 | mCallbackHandleAcquireTime = -1; |
| 578 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 579 | return willPresent; |
| 580 | } |
| 581 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 582 | bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 583 | mCurrentState.transparentRegionHint = transparent; |
| 584 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 585 | setTransactionFlags(eTransactionNeeded); |
| 586 | return true; |
| 587 | } |
| 588 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 589 | Rect BufferStateLayer::getBufferSize(const State& s) const { |
| 590 | // for buffer state layers we use the display frame size as the buffer size. |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 591 | |
chaviw | 7e72caf | 2020-12-02 16:50:43 -0800 | [diff] [blame] | 592 | if (mBufferInfo.mBuffer == nullptr) { |
| 593 | return Rect::INVALID_RECT; |
| 594 | } |
| 595 | |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 596 | uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth(); |
| 597 | uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight(); |
| 598 | |
| 599 | // Undo any transformations on the buffer and return the result. |
| 600 | if (mBufferInfo.mTransform & ui::Transform::ROT_90) { |
| 601 | std::swap(bufWidth, bufHeight); |
| 602 | } |
| 603 | |
| 604 | if (getTransformToDisplayInverse()) { |
| 605 | uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags(); |
| 606 | if (invTransform & ui::Transform::ROT_90) { |
| 607 | std::swap(bufWidth, bufHeight); |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 611 | return Rect(0, 0, bufWidth, bufHeight); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 612 | } |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 613 | |
| 614 | FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const { |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 615 | if (mBufferInfo.mBuffer == nullptr) { |
| 616 | return parentBounds; |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 619 | return getBufferSize(getDrawingState()).toFloatRect(); |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 620 | } |
| 621 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 622 | // ----------------------------------------------------------------------- |
| 623 | |
| 624 | // ----------------------------------------------------------------------- |
| 625 | // Interface implementation for BufferLayer |
| 626 | // ----------------------------------------------------------------------- |
| 627 | bool BufferStateLayer::fenceHasSignaled() const { |
Alec Mouri | 91f6df3 | 2020-01-30 08:48:58 -0800 | [diff] [blame] | 628 | const bool fenceSignaled = |
| 629 | getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled; |
| 630 | if (!fenceSignaled) { |
| 631 | mFlinger->mTimeStats->incrementLatchSkipped(getSequence(), |
| 632 | TimeStats::LatchSkipReason::LateAcquire); |
| 633 | } |
| 634 | |
| 635 | return fenceSignaled; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 638 | bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const { |
Ady Abraham | cd1580c | 2019-04-29 15:40:03 -0700 | [diff] [blame] | 639 | if (!hasFrameUpdate() || isRemovedFromCurrentState()) { |
| 640 | return true; |
| 641 | } |
| 642 | |
Ady Abraham | f0c5649 | 2020-12-17 18:04:15 -0800 | [diff] [blame] | 643 | return mCurrentState.isAutoTimestamp || mCurrentState.desiredPresentTime <= expectedPresentTime; |
Ady Abraham | cd1580c | 2019-04-29 15:40:03 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 646 | bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) { |
| 647 | for (const auto& handle : mDrawingState.callbackHandles) { |
| 648 | handle->refreshStartTime = refreshStartTime; |
| 649 | } |
| 650 | return BufferLayer::onPreComposition(refreshStartTime); |
| 651 | } |
| 652 | |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 653 | uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const { |
Valerie Hau | 134651a | 2020-01-28 16:21:22 -0800 | [diff] [blame] | 654 | return mDrawingState.frameNumber; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Robert Carr | fe1209c | 2020-02-11 12:25:35 -0800 | [diff] [blame] | 657 | /** |
| 658 | * This is the frameNumber used for deferred transaction signalling. We need to use this because |
| 659 | * of cases where we defer a transaction for a surface to itself. In the BLAST world this |
| 660 | * may not make a huge amount of sense (Why not just merge the Buffer transaction with the |
| 661 | * deferred transaction?) but this is an important legacy use case, for example moving |
| 662 | * a window at the same time it draws makes use of this kind of technique. So anyway |
| 663 | * imagine we have something like this: |
| 664 | * |
| 665 | * Transaction { // containing |
| 666 | * Buffer -> frameNumber = 2 |
| 667 | * DeferTransactionUntil -> frameNumber = 2 |
| 668 | * Random other stuff |
| 669 | * } |
| 670 | * Now imagine getHeadFrameNumber returned mDrawingState.mFrameNumber (or mCurrentFrameNumber). |
| 671 | * Prior to doTransaction SurfaceFlinger will call notifyAvailableFrames, but because we |
| 672 | * haven't swapped mCurrentState to mDrawingState yet we will think the sync point |
| 673 | * is not ready. So we will return false from applyPendingState and not swap |
| 674 | * current state to drawing state. But because we don't swap current state |
| 675 | * to drawing state the number will never update and we will be stuck. This way |
| 676 | * we can see we need to return the frame number for the buffer we are about |
| 677 | * to apply. |
| 678 | */ |
| 679 | uint64_t BufferStateLayer::getHeadFrameNumber(nsecs_t /* expectedPresentTime */) const { |
| 680 | return mCurrentState.frameNumber; |
| 681 | } |
| 682 | |
Vishnu Nair | cf26a0a | 2020-11-13 12:56:20 -0800 | [diff] [blame] | 683 | void BufferStateLayer::setAutoRefresh(bool autoRefresh) { |
| 684 | if (!mAutoRefresh.exchange(autoRefresh)) { |
| 685 | mFlinger->signalLayerUpdate(); |
| 686 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 689 | bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) { |
baocheng sun | a663c2b | 2021-05-13 18:51:28 +0800 | [diff] [blame] | 690 | // We need to update the sideband stream if the layer has both a buffer and a sideband stream. |
| 691 | const bool updateSidebandStream = hasFrameUpdate() && mSidebandStream.get(); |
| 692 | |
| 693 | if (mSidebandStreamChanged.exchange(false) || updateSidebandStream) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 694 | const State& s(getDrawingState()); |
| 695 | // mSidebandStreamChanged was true |
Lloyd Pique | 0b785d8 | 2018-12-04 17:25:27 -0800 | [diff] [blame] | 696 | mSidebandStream = s.sidebandStream; |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 697 | editCompositionState()->sidebandStream = mSidebandStream; |
Lloyd Pique | 0b785d8 | 2018-12-04 17:25:27 -0800 | [diff] [blame] | 698 | if (mSidebandStream != nullptr) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 699 | setTransactionFlags(eTransactionNeeded); |
| 700 | mFlinger->setTransactionFlags(eTraversalNeeded); |
| 701 | } |
| 702 | recomputeVisibleRegions = true; |
| 703 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 704 | return true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 705 | } |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 706 | return false; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 709 | bool BufferStateLayer::hasFrameUpdate() const { |
Valerie Hau | aa19456 | 2019-02-05 16:21:38 -0800 | [diff] [blame] | 710 | const State& c(getCurrentState()); |
| 711 | return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 714 | status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime, |
| 715 | nsecs_t /*expectedPresentTime*/) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 716 | const State& s(getDrawingState()); |
| 717 | |
| 718 | if (!s.buffer) { |
Valerie Hau | aa19456 | 2019-02-05 16:21:38 -0800 | [diff] [blame] | 719 | if (s.bgColorLayer) { |
| 720 | for (auto& handle : mDrawingState.callbackHandles) { |
| 721 | handle->latchTime = latchTime; |
| 722 | } |
| 723 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 724 | return NO_ERROR; |
| 725 | } |
| 726 | |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 727 | for (auto& handle : mDrawingState.callbackHandles) { |
Vishnu Nair | 935590e | 2021-02-10 13:05:52 -0800 | [diff] [blame] | 728 | if (handle->frameNumber == mDrawingState.frameNumber) { |
| 729 | handle->latchTime = latchTime; |
| 730 | } |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 731 | } |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 732 | |
Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 733 | const int32_t layerId = getSequence(); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 734 | const uint64_t bufferId = mDrawingState.buffer->getBuffer()->getId(); |
Adithya Srinivasan | b238cd5 | 2021-02-04 17:54:05 +0000 | [diff] [blame] | 735 | const uint64_t frameNumber = mDrawingState.frameNumber; |
| 736 | const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence); |
| 737 | mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence); |
| 738 | mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime); |
| 739 | |
| 740 | mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence, |
| 741 | FrameTracer::FrameEvent::ACQUIRE_FENCE); |
| 742 | mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime, |
| 743 | FrameTracer::FrameEvent::LATCH); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 744 | |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 745 | auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX; |
| 746 | if (bufferSurfaceFrame != nullptr && |
| 747 | bufferSurfaceFrame->getPresentState() != PresentState::Presented) { |
| 748 | // Update only if the bufferSurfaceFrame wasn't already presented. A Presented |
| 749 | // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we |
| 750 | // are processing the next state. |
| 751 | addSurfaceFramePresentedForBuffer(bufferSurfaceFrame, |
Ady Abraham | 6c1b7ac | 2021-03-31 16:56:03 -0700 | [diff] [blame] | 752 | mDrawingState.acquireFenceTime->getSignalTime(), |
| 753 | latchTime); |
Adithya Srinivasan | b9a7dab | 2021-01-14 23:49:46 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 756 | std::deque<sp<CallbackHandle>> remainingHandles; |
| 757 | mFlinger->getTransactionCallbackInvoker() |
| 758 | .finalizeOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles); |
| 759 | mDrawingState.callbackHandles = remainingHandles; |
| 760 | |
Marissa Wall | 16c112d | 2019-03-20 13:21:13 -0700 | [diff] [blame] | 761 | mCurrentStateModified = false; |
| 762 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 763 | return NO_ERROR; |
| 764 | } |
| 765 | |
| 766 | status_t BufferStateLayer::updateActiveBuffer() { |
| 767 | const State& s(getDrawingState()); |
| 768 | |
| 769 | if (s.buffer == nullptr) { |
| 770 | return BAD_VALUE; |
| 771 | } |
chaviw | df3c5e8 | 2021-01-07 13:00:37 -0800 | [diff] [blame] | 772 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 773 | if (!mBufferInfo.mBuffer || s.buffer->getBuffer() != mBufferInfo.mBuffer->getBuffer()) { |
chaviw | df3c5e8 | 2021-01-07 13:00:37 -0800 | [diff] [blame] | 774 | decrementPendingBufferCount(); |
| 775 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 776 | |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 777 | mPreviousBufferId = getCurrentBufferId(); |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 778 | mBufferInfo.mBuffer = s.buffer; |
| 779 | mBufferInfo.mFence = s.acquireFence; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 780 | |
| 781 | return NO_ERROR; |
| 782 | } |
| 783 | |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 784 | status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 785 | // TODO(marissaw): support frame history events |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 786 | mPreviousFrameNumber = mCurrentFrameNumber; |
Valerie Hau | 134651a | 2020-01-28 16:21:22 -0800 | [diff] [blame] | 787 | mCurrentFrameNumber = mDrawingState.frameNumber; |
Valerie Hau | bf78464 | 2020-01-29 07:25:23 -0800 | [diff] [blame] | 788 | { |
| 789 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 790 | mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime); |
| 791 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 792 | return NO_ERROR; |
| 793 | } |
| 794 | |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 795 | void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) { |
| 796 | std::lock_guard lock(mMutex); |
| 797 | if (!clientCacheId.isValid()) { |
| 798 | ALOGE("invalid process, failed to erase buffer"); |
| 799 | return; |
| 800 | } |
| 801 | eraseBufferLocked(clientCacheId); |
| 802 | } |
| 803 | |
| 804 | uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) { |
| 805 | std::lock_guard<std::mutex> lock(mMutex); |
| 806 | auto itr = mCachedBuffers.find(clientCacheId); |
| 807 | if (itr == mCachedBuffers.end()) { |
| 808 | return addCachedBuffer(clientCacheId); |
| 809 | } |
| 810 | auto& [hwcCacheSlot, counter] = itr->second; |
| 811 | counter = mCounter++; |
| 812 | return hwcCacheSlot; |
| 813 | } |
| 814 | |
| 815 | uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId) |
| 816 | REQUIRES(mMutex) { |
| 817 | if (!clientCacheId.isValid()) { |
| 818 | ALOGE("invalid process, returning invalid slot"); |
| 819 | return BufferQueue::INVALID_BUFFER_SLOT; |
| 820 | } |
| 821 | |
| 822 | ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this)); |
| 823 | |
| 824 | uint32_t hwcCacheSlot = getFreeHwcCacheSlot(); |
| 825 | mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++}; |
| 826 | return hwcCacheSlot; |
| 827 | } |
| 828 | |
| 829 | uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) { |
| 830 | if (mFreeHwcCacheSlots.empty()) { |
| 831 | evictLeastRecentlyUsed(); |
| 832 | } |
| 833 | |
| 834 | uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top(); |
| 835 | mFreeHwcCacheSlots.pop(); |
| 836 | return hwcCacheSlot; |
| 837 | } |
| 838 | |
| 839 | void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) { |
| 840 | uint64_t minCounter = UINT_MAX; |
| 841 | client_cache_t minClientCacheId = {}; |
| 842 | for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) { |
| 843 | const auto& [hwcCacheSlot, counter] = slotCounter; |
| 844 | if (counter < minCounter) { |
| 845 | minCounter = counter; |
| 846 | minClientCacheId = clientCacheId; |
| 847 | } |
| 848 | } |
| 849 | eraseBufferLocked(minClientCacheId); |
| 850 | |
| 851 | ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this); |
| 852 | } |
| 853 | |
| 854 | void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId) |
| 855 | REQUIRES(mMutex) { |
| 856 | auto itr = mCachedBuffers.find(clientCacheId); |
| 857 | if (itr == mCachedBuffers.end()) { |
| 858 | return; |
| 859 | } |
| 860 | auto& [hwcCacheSlot, counter] = itr->second; |
| 861 | |
| 862 | // TODO send to hwc cache and resources |
| 863 | |
| 864 | mFreeHwcCacheSlots.push(hwcCacheSlot); |
| 865 | mCachedBuffers.erase(clientCacheId); |
| 866 | } |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 867 | |
| 868 | void BufferStateLayer::gatherBufferInfo() { |
chaviw | debadb8 | 2020-03-26 14:57:24 -0700 | [diff] [blame] | 869 | BufferLayer::gatherBufferInfo(); |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 870 | |
chaviw | debadb8 | 2020-03-26 14:57:24 -0700 | [diff] [blame] | 871 | const State& s(getDrawingState()); |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 872 | mBufferInfo.mDesiredPresentTime = s.desiredPresentTime; |
| 873 | mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence); |
| 874 | mBufferInfo.mFence = s.acquireFence; |
chaviw | 766c9c5 | 2021-02-10 17:36:47 -0800 | [diff] [blame] | 875 | mBufferInfo.mTransform = s.bufferTransform; |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 876 | mBufferInfo.mDataspace = translateDataspace(s.dataspace); |
Vishnu Nair | 5cc9ac0 | 2021-04-19 13:23:38 -0700 | [diff] [blame] | 877 | mBufferInfo.mCrop = computeBufferCrop(s); |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 878 | mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
| 879 | mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion; |
| 880 | mBufferInfo.mHdrMetadata = s.hdrMetadata; |
| 881 | mBufferInfo.mApi = s.api; |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 882 | mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse; |
chaviw | f83ce18 | 2019-09-12 14:43:08 -0700 | [diff] [blame] | 883 | mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId); |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Robert Carr | 916b036 | 2020-10-06 13:53:03 -0700 | [diff] [blame] | 886 | uint32_t BufferStateLayer::getEffectiveScalingMode() const { |
| 887 | return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
| 888 | } |
| 889 | |
Vishnu Nair | 5cc9ac0 | 2021-04-19 13:23:38 -0700 | [diff] [blame] | 890 | Rect BufferStateLayer::computeBufferCrop(const State& s) { |
chaviw | f3f40fe | 2021-04-27 15:54:02 -0500 | [diff] [blame] | 891 | if (s.buffer && !s.bufferCrop.isEmpty()) { |
| 892 | Rect bufferCrop; |
| 893 | s.buffer->getBuffer()->getBounds().intersect(s.bufferCrop, &bufferCrop); |
| 894 | return bufferCrop; |
| 895 | } else if (s.buffer) { |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 896 | return s.buffer->getBuffer()->getBounds(); |
chaviw | f3f40fe | 2021-04-27 15:54:02 -0500 | [diff] [blame] | 897 | } else { |
| 898 | return s.bufferCrop; |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 899 | } |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 900 | } |
| 901 | |
chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 902 | sp<Layer> BufferStateLayer::createClone() { |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 903 | LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata()); |
chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 904 | args.textureName = mTextureName; |
Lloyd Pique | 1c3a5eb | 2019-10-03 13:07:08 -0700 | [diff] [blame] | 905 | sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args); |
chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 906 | layer->mHwcSlotGenerator = mHwcSlotGenerator; |
| 907 | layer->setInitialValuesForClone(this); |
| 908 | return layer; |
| 909 | } |
Valerie Hau | 92bf548 | 2020-02-10 09:49:08 -0800 | [diff] [blame] | 910 | |
Vishnu Nair | e7f79c5 | 2020-10-29 14:45:03 -0700 | [diff] [blame] | 911 | bool BufferStateLayer::bufferNeedsFiltering() const { |
| 912 | const State& s(getDrawingState()); |
| 913 | if (!s.buffer) { |
| 914 | return false; |
| 915 | } |
| 916 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 917 | uint32_t bufferWidth = s.buffer->getBuffer()->width; |
| 918 | uint32_t bufferHeight = s.buffer->getBuffer()->height; |
Vishnu Nair | e7f79c5 | 2020-10-29 14:45:03 -0700 | [diff] [blame] | 919 | |
| 920 | // Undo any transformations on the buffer and return the result. |
chaviw | 766c9c5 | 2021-02-10 17:36:47 -0800 | [diff] [blame] | 921 | if (s.bufferTransform & ui::Transform::ROT_90) { |
Vishnu Nair | e7f79c5 | 2020-10-29 14:45:03 -0700 | [diff] [blame] | 922 | std::swap(bufferWidth, bufferHeight); |
| 923 | } |
| 924 | |
| 925 | if (s.transformToDisplayInverse) { |
| 926 | uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags(); |
| 927 | if (invTransform & ui::Transform::ROT_90) { |
| 928 | std::swap(bufferWidth, bufferHeight); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | const Rect layerSize{getBounds()}; |
| 933 | return layerSize.width() != bufferWidth || layerSize.height() != bufferHeight; |
| 934 | } |
Robert Carr | 7121caf | 2020-12-15 13:07:32 -0800 | [diff] [blame] | 935 | |
Robert Carr | 7121caf | 2020-12-15 13:07:32 -0800 | [diff] [blame] | 936 | void BufferStateLayer::decrementPendingBufferCount() { |
Vishnu Nair | 8eda69e | 2021-02-26 10:42:10 -0800 | [diff] [blame] | 937 | int32_t pendingBuffers = --mPendingBufferTransactions; |
| 938 | tracePendingBufferCount(pendingBuffers); |
Robert Carr | 7121caf | 2020-12-15 13:07:32 -0800 | [diff] [blame] | 939 | } |
| 940 | |
Vishnu Nair | 8eda69e | 2021-02-26 10:42:10 -0800 | [diff] [blame] | 941 | void BufferStateLayer::tracePendingBufferCount(int32_t pendingBuffers) { |
| 942 | ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers); |
Robert Carr | 7121caf | 2020-12-15 13:07:32 -0800 | [diff] [blame] | 943 | } |
| 944 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 945 | void BufferStateLayer::bufferMayChange(const sp<GraphicBuffer>& newBuffer) { |
| 946 | if (mDrawingState.buffer != nullptr && |
| 947 | (!mBufferInfo.mBuffer || |
| 948 | mDrawingState.buffer->getBuffer() != mBufferInfo.mBuffer->getBuffer()) && |
| 949 | newBuffer != mDrawingState.buffer->getBuffer()) { |
Robert Carr | 7121caf | 2020-12-15 13:07:32 -0800 | [diff] [blame] | 950 | // If we are about to update mDrawingState.buffer but it has not yet latched |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 951 | // then we will drop a buffer and should decrement the pending buffer count and |
| 952 | // call any release buffer callbacks if set. |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 953 | callReleaseBufferCallback(mDrawingState.releaseBufferListener, |
Robert Carr | 97e7cc0 | 2021-06-07 10:45:40 -0700 | [diff] [blame] | 954 | mDrawingState.buffer->getBuffer(), mDrawingState.acquireFence, |
| 955 | mTransformHint); |
Robert Carr | 7121caf | 2020-12-15 13:07:32 -0800 | [diff] [blame] | 956 | decrementPendingBufferCount(); |
| 957 | } |
Robert Carr | 7121caf | 2020-12-15 13:07:32 -0800 | [diff] [blame] | 958 | } |
| 959 | |
chaviw | 39d0147 | 2021-04-08 14:26:24 -0500 | [diff] [blame] | 960 | /* |
| 961 | * We don't want to send the layer's transform to input, but rather the |
| 962 | * parent's transform. This is because BufferStateLayer's transform is |
| 963 | * information about how the buffer is placed on screen. The parent's |
| 964 | * transform makes more sense to send since it's information about how the |
| 965 | * layer is placed on screen. This transform is used by input to determine |
| 966 | * how to go from screen space back to window space. |
| 967 | */ |
| 968 | ui::Transform BufferStateLayer::getInputTransform() const { |
| 969 | sp<Layer> parent = mDrawingParent.promote(); |
| 970 | if (parent == nullptr) { |
| 971 | return ui::Transform(); |
| 972 | } |
| 973 | |
| 974 | return parent->getTransform(); |
| 975 | } |
| 976 | |
| 977 | /** |
| 978 | * Similar to getInputTransform, we need to update the bounds to include the transform. |
| 979 | * This is because bounds for BSL doesn't include buffer transform, where the input assumes |
| 980 | * that's already included. |
| 981 | */ |
| 982 | Rect BufferStateLayer::getInputBounds() const { |
| 983 | Rect bufferBounds = getCroppedBufferSize(getDrawingState()); |
| 984 | if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) { |
| 985 | return bufferBounds; |
| 986 | } |
| 987 | return mDrawingState.transform.transform(bufferBounds); |
| 988 | } |
| 989 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 990 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 991 | |
| 992 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 993 | #pragma clang diagnostic pop // ignored "-Wconversion -Wextra" |