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