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