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