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 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "BufferStateLayer" |
| 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 22 | #include <limits> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 23 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 24 | #include <compositionengine/Display.h> |
Lloyd Pique | 0b785d8 | 2018-12-04 17:25:27 -0800 | [diff] [blame] | 25 | #include <compositionengine/Layer.h> |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 26 | #include <compositionengine/OutputLayer.h> |
Lloyd Pique | 0b785d8 | 2018-12-04 17:25:27 -0800 | [diff] [blame] | 27 | #include <compositionengine/impl/LayerCompositionState.h> |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 28 | #include <compositionengine/impl/OutputLayerCompositionState.h> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 29 | #include <private/gui/SyncFeatures.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 30 | #include <renderengine/Image.h> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 31 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 32 | #include "BufferStateLayer.h" |
| 33 | #include "ColorLayer.h" |
| 34 | #include "TimeStats/TimeStats.h" |
Valerie Hau | 0bc0915 | 2018-12-20 07:42:47 -0800 | [diff] [blame] | 35 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 36 | namespace android { |
| 37 | |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 38 | // clang-format off |
| 39 | const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{ |
| 40 | 1, 0, 0, 0, |
| 41 | 0, 1, 0, 0, |
| 42 | 0, 0, 1, 0, |
| 43 | 0, 0, 0, 1 |
| 44 | }; |
| 45 | // clang-format on |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 46 | |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 47 | BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) : BufferLayer(args) { |
| 48 | mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
Marissa Wall | 3ff826c | 2019-02-07 11:58:25 -0800 | [diff] [blame] | 49 | mCurrentState.dataspace = ui::Dataspace::V0_SRGB; |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 50 | } |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 51 | BufferStateLayer::~BufferStateLayer() { |
| 52 | if (mActiveBuffer != nullptr) { |
| 53 | auto& engine(mFlinger->getRenderEngine()); |
| 54 | engine.unbindExternalTextureBuffer(mActiveBuffer->getId()); |
| 55 | } |
| 56 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 57 | |
| 58 | // ----------------------------------------------------------------------- |
| 59 | // Interface implementation for Layer |
| 60 | // ----------------------------------------------------------------------- |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 61 | void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) { |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 62 | // The previous release fence notifies the client that SurfaceFlinger is done with the previous |
| 63 | // buffer that was presented on this layer. The first transaction that came in this frame that |
| 64 | // replaced the previous buffer on this layer needs this release fence, because the fence will |
| 65 | // let the client know when that previous buffer is removed from the screen. |
| 66 | // |
| 67 | // Every other transaction on this layer does not need a release fence because no other |
| 68 | // Transactions that were set on this layer this frame are going to have their preceeding buffer |
| 69 | // removed from the display this frame. |
| 70 | // |
| 71 | // For example, if we have 3 transactions this frame. The first transaction doesn't contain a |
| 72 | // buffer so it doesn't need a previous release fence because the layer still needs the previous |
| 73 | // buffer. The second transaction contains a buffer so it needs a previous release fence because |
| 74 | // the previous buffer will be released this frame. The third transaction also contains a |
| 75 | // buffer. It replaces the buffer in the second transaction. The buffer in the second |
| 76 | // transaction will now no longer be presented so it is released immediately and the third |
| 77 | // transaction doesn't need a previous release fence. |
| 78 | for (auto& handle : mDrawingState.callbackHandles) { |
| 79 | if (handle->releasePreviousBuffer) { |
| 80 | handle->previousReleaseFence = releaseFence; |
| 81 | break; |
| 82 | } |
| 83 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const { |
| 87 | // TODO(marissaw): send the transform hint to buffer owner |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) { |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 92 | mFlinger->getTransactionCompletedThread().addPresentedCallbackHandles( |
| 93 | mDrawingState.callbackHandles); |
| 94 | |
| 95 | mDrawingState.callbackHandles = {}; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 98 | bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 99 | if (getSidebandStreamChanged() || getAutoRefresh()) { |
| 100 | return true; |
| 101 | } |
| 102 | |
Marissa Wall | 024a191 | 2018-08-13 13:55:35 -0700 | [diff] [blame] | 103 | return hasFrameUpdate(); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 106 | bool BufferStateLayer::willPresentCurrentTransaction() const { |
| 107 | // Returns true if the most recent Transaction applied to CurrentState will be presented. |
| 108 | return getSidebandStreamChanged() || getAutoRefresh() || |
Valerie Hau | aa19456 | 2019-02-05 16:21:38 -0800 | [diff] [blame] | 109 | (mCurrentState.modified && |
| 110 | (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr)); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 113 | bool BufferStateLayer::getTransformToDisplayInverse() const { |
| 114 | return mCurrentState.transformToDisplayInverse; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 117 | void BufferStateLayer::pushPendingState() { |
| 118 | if (!mCurrentState.modified) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 119 | return; |
| 120 | } |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 121 | mPendingStates.push_back(mCurrentState); |
| 122 | ATRACE_INT(mTransactionName.string(), mPendingStates.size()); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 126 | const bool stateUpdateAvailable = !mPendingStates.empty(); |
| 127 | while (!mPendingStates.empty()) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 128 | popPendingState(stateToCommit); |
| 129 | } |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 130 | mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified; |
| 131 | mCurrentState.modified = false; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 132 | return stateUpdateAvailable; |
| 133 | } |
| 134 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 135 | // Crop that applies to the window |
| 136 | Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const { |
| 137 | return Rect::INVALID_RECT; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | bool BufferStateLayer::setTransform(uint32_t transform) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 141 | if (mCurrentState.transform == transform) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 142 | mCurrentState.transform = transform; |
| 143 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 144 | setTransactionFlags(eTransactionNeeded); |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 149 | if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false; |
| 150 | mCurrentState.sequence++; |
| 151 | mCurrentState.transformToDisplayInverse = transformToDisplayInverse; |
| 152 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 153 | setTransactionFlags(eTransactionNeeded); |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | bool BufferStateLayer::setCrop(const Rect& crop) { |
Marissa Wall | 290ad08 | 2019-03-06 13:23:47 -0800 | [diff] [blame] | 158 | Rect c = crop; |
| 159 | if (c.left < 0) { |
| 160 | c.left = 0; |
| 161 | } |
| 162 | if (c.top < 0) { |
| 163 | c.top = 0; |
| 164 | } |
| 165 | // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below |
| 166 | // treats all invalid rectangles the same. |
| 167 | if (!c.isValid()) { |
| 168 | c.makeInvalid(); |
| 169 | } |
| 170 | |
| 171 | if (mCurrentState.crop == c) return false; |
Marissa Wall | 290ad08 | 2019-03-06 13:23:47 -0800 | [diff] [blame] | 172 | mCurrentState.crop = c; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 173 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 174 | setTransactionFlags(eTransactionNeeded); |
| 175 | return true; |
| 176 | } |
| 177 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 178 | bool BufferStateLayer::setFrame(const Rect& frame) { |
| 179 | int x = frame.left; |
| 180 | int y = frame.top; |
| 181 | int w = frame.getWidth(); |
| 182 | int h = frame.getHeight(); |
| 183 | |
Marissa Wall | 0f3242d | 2018-12-20 15:10:22 -0800 | [diff] [blame] | 184 | if (x < 0) { |
| 185 | x = 0; |
| 186 | w = frame.right; |
| 187 | } |
| 188 | |
| 189 | if (y < 0) { |
| 190 | y = 0; |
| 191 | h = frame.bottom; |
| 192 | } |
| 193 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 194 | if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y && |
| 195 | mCurrentState.active.w == w && mCurrentState.active.h == h) { |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 196 | return false; |
| 197 | } |
| 198 | |
| 199 | if (!frame.isValid()) { |
| 200 | x = y = w = h = 0; |
| 201 | } |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 202 | mCurrentState.active.transform.set(x, y); |
| 203 | mCurrentState.active.w = w; |
| 204 | mCurrentState.active.h = h; |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 205 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 206 | mCurrentState.sequence++; |
| 207 | mCurrentState.modified = true; |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 208 | setTransactionFlags(eTransactionNeeded); |
| 209 | return true; |
| 210 | } |
| 211 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 212 | bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime, |
| 213 | nsecs_t desiredPresentTime) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 214 | if (mCurrentState.buffer) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 215 | mReleasePreviousBuffer = true; |
| 216 | } |
| 217 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 218 | mCurrentState.buffer = buffer; |
| 219 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 220 | setTransactionFlags(eTransactionNeeded); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 221 | |
| 222 | mFlinger->mTimeStats->setPostTime(getSequence(), getFrameNumber(), getName().c_str(), postTime); |
| 223 | mDesiredPresentTime = desiredPresentTime; |
| 224 | |
| 225 | if (mFlinger->mUseSmart90ForVideo) { |
| 226 | const nsecs_t presentTime = (mDesiredPresentTime == -1) ? 0 : mDesiredPresentTime; |
| 227 | mFlinger->mScheduler->addLayerPresentTime(mSchedulerLayerHandle, presentTime); |
| 228 | } |
| 229 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | |
| 233 | bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 234 | // The acquire fences of BufferStateLayers have already signaled before they are set |
| 235 | mCallbackHandleAcquireTime = fence->getSignalTime(); |
| 236 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 237 | mCurrentState.acquireFence = fence; |
| 238 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 239 | setTransactionFlags(eTransactionNeeded); |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 244 | if (mCurrentState.dataspace == dataspace) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 245 | mCurrentState.dataspace = dataspace; |
| 246 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 247 | setTransactionFlags(eTransactionNeeded); |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 252 | if (mCurrentState.hdrMetadata == hdrMetadata) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 253 | mCurrentState.hdrMetadata = hdrMetadata; |
| 254 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 255 | setTransactionFlags(eTransactionNeeded); |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 260 | mCurrentState.surfaceDamageRegion = surfaceDamage; |
| 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::setApi(int32_t api) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 267 | if (mCurrentState.api == api) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 268 | mCurrentState.api = api; |
| 269 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 270 | setTransactionFlags(eTransactionNeeded); |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 275 | if (mCurrentState.sidebandStream == sidebandStream) return false; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 276 | mCurrentState.sidebandStream = sidebandStream; |
| 277 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 278 | setTransactionFlags(eTransactionNeeded); |
| 279 | |
| 280 | if (!mSidebandStreamChanged.exchange(true)) { |
| 281 | // mSidebandStreamChanged was false |
| 282 | mFlinger->signalLayerUpdate(); |
| 283 | } |
| 284 | return true; |
| 285 | } |
| 286 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 287 | bool BufferStateLayer::setTransactionCompletedListeners( |
| 288 | const std::vector<sp<CallbackHandle>>& handles) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 289 | // 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] | 290 | if (handles.empty()) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 291 | mReleasePreviousBuffer = false; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 292 | return false; |
| 293 | } |
| 294 | |
| 295 | const bool willPresent = willPresentCurrentTransaction(); |
| 296 | |
| 297 | for (const auto& handle : handles) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 298 | // If this transaction set a buffer on this layer, release its previous buffer |
| 299 | handle->releasePreviousBuffer = mReleasePreviousBuffer; |
| 300 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 301 | // If this layer will be presented in this frame |
| 302 | if (willPresent) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 303 | // If this transaction set an acquire fence on this layer, set its acquire time |
| 304 | handle->acquireTime = mCallbackHandleAcquireTime; |
| 305 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 306 | // Notify the transaction completed thread that there is a pending latched callback |
| 307 | // handle |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 308 | mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 309 | |
| 310 | // Store so latched time and release fence can be set |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 311 | mCurrentState.callbackHandles.push_back(handle); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 312 | |
| 313 | } else { // If this layer will NOT need to be relatched and presented this frame |
| 314 | // Notify the transaction completed thread this handle is done |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 315 | mFlinger->getTransactionCompletedThread().addUnpresentedCallbackHandle(handle); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 319 | mReleasePreviousBuffer = false; |
| 320 | mCallbackHandleAcquireTime = -1; |
| 321 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 322 | return willPresent; |
| 323 | } |
| 324 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 325 | bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 326 | mCurrentState.transparentRegionHint = transparent; |
| 327 | mCurrentState.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 328 | setTransactionFlags(eTransactionNeeded); |
| 329 | return true; |
| 330 | } |
| 331 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 332 | Rect BufferStateLayer::getBufferSize(const State& s) const { |
| 333 | // for buffer state layers we use the display frame size as the buffer size. |
| 334 | if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) { |
| 335 | return Rect(getActiveWidth(s), getActiveHeight(s)); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 338 | // if the display frame is not defined, use the parent bounds as the buffer size. |
| 339 | const auto& p = mDrawingParent.promote(); |
| 340 | if (p != nullptr) { |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 341 | Rect parentBounds = Rect(p->getBounds(Region())); |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 342 | if (!parentBounds.isEmpty()) { |
| 343 | return parentBounds; |
| 344 | } |
| 345 | } |
| 346 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 347 | return Rect::INVALID_RECT; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 348 | } |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 349 | |
| 350 | FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const { |
| 351 | const State& s(getDrawingState()); |
| 352 | // for buffer state layers we use the display frame size as the buffer size. |
| 353 | if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) { |
| 354 | return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s)); |
| 355 | } |
| 356 | |
| 357 | // if the display frame is not defined, use the parent bounds as the buffer size. |
| 358 | return parentBounds; |
| 359 | } |
| 360 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 361 | // ----------------------------------------------------------------------- |
| 362 | |
| 363 | // ----------------------------------------------------------------------- |
| 364 | // Interface implementation for BufferLayer |
| 365 | // ----------------------------------------------------------------------- |
| 366 | bool BufferStateLayer::fenceHasSignaled() const { |
| 367 | if (latchUnsignaledBuffers()) { |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled; |
| 372 | } |
| 373 | |
| 374 | nsecs_t BufferStateLayer::getDesiredPresentTime() { |
Valerie Hau | bc6ddb1 | 2019-03-08 11:10:15 -0800 | [diff] [blame] | 375 | return mDesiredPresentTime; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 378 | std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 379 | return std::make_shared<FenceTime>(getDrawingState().acquireFence); |
| 380 | } |
| 381 | |
| 382 | void BufferStateLayer::getDrawingTransformMatrix(float *matrix) { |
| 383 | std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix); |
| 384 | } |
| 385 | |
| 386 | uint32_t BufferStateLayer::getDrawingTransform() const { |
| 387 | return getDrawingState().transform; |
| 388 | } |
| 389 | |
| 390 | ui::Dataspace BufferStateLayer::getDrawingDataSpace() const { |
| 391 | return getDrawingState().dataspace; |
| 392 | } |
| 393 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 394 | // Crop that applies to the buffer |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 395 | Rect BufferStateLayer::getDrawingCrop() const { |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 396 | const State& s(getDrawingState()); |
| 397 | |
| 398 | if (s.crop.isEmpty() && s.buffer) { |
| 399 | return s.buffer->getBounds(); |
Valerie Hau | 0bc0915 | 2018-12-20 07:42:47 -0800 | [diff] [blame] | 400 | } else if (s.buffer) { |
| 401 | Rect crop = s.crop; |
| 402 | crop.left = std::max(crop.left, 0); |
| 403 | crop.top = std::max(crop.top, 0); |
| 404 | uint32_t bufferWidth = s.buffer->getWidth(); |
| 405 | uint32_t bufferHeight = s.buffer->getHeight(); |
| 406 | if (bufferHeight <= std::numeric_limits<int32_t>::max() && |
| 407 | bufferWidth <= std::numeric_limits<int32_t>::max()) { |
| 408 | crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth)); |
| 409 | crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight)); |
| 410 | } |
| 411 | if (!crop.isValid()) { |
| 412 | // Crop rect is out of bounds, return whole buffer |
| 413 | return s.buffer->getBounds(); |
| 414 | } |
| 415 | return crop; |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 416 | } |
| 417 | return s.crop; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | uint32_t BufferStateLayer::getDrawingScalingMode() const { |
Marissa Wall | ec463ac | 2018-10-08 12:35:04 -0700 | [diff] [blame] | 421 | return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | Region BufferStateLayer::getDrawingSurfaceDamage() const { |
| 425 | return getDrawingState().surfaceDamageRegion; |
| 426 | } |
| 427 | |
| 428 | const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const { |
| 429 | return getDrawingState().hdrMetadata; |
| 430 | } |
| 431 | |
| 432 | int BufferStateLayer::getDrawingApi() const { |
| 433 | return getDrawingState().api; |
| 434 | } |
| 435 | |
| 436 | PixelFormat BufferStateLayer::getPixelFormat() const { |
Marissa Wall | 5aec641 | 2018-11-14 11:49:18 -0800 | [diff] [blame] | 437 | if (!mActiveBuffer) { |
| 438 | return PIXEL_FORMAT_NONE; |
| 439 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 440 | return mActiveBuffer->format; |
| 441 | } |
| 442 | |
| 443 | uint64_t BufferStateLayer::getFrameNumber() const { |
| 444 | return mFrameNumber; |
| 445 | } |
| 446 | |
| 447 | bool BufferStateLayer::getAutoRefresh() const { |
| 448 | // TODO(marissaw): support shared buffer mode |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | bool BufferStateLayer::getSidebandStreamChanged() const { |
| 453 | return mSidebandStreamChanged.load(); |
| 454 | } |
| 455 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 456 | bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 457 | if (mSidebandStreamChanged.exchange(false)) { |
| 458 | const State& s(getDrawingState()); |
| 459 | // mSidebandStreamChanged was true |
Lloyd Pique | 0b785d8 | 2018-12-04 17:25:27 -0800 | [diff] [blame] | 460 | LOG_ALWAYS_FATAL_IF(!getCompositionLayer()); |
| 461 | mSidebandStream = s.sidebandStream; |
| 462 | getCompositionLayer()->editState().frontEnd.sidebandStream = mSidebandStream; |
| 463 | if (mSidebandStream != nullptr) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 464 | setTransactionFlags(eTransactionNeeded); |
| 465 | mFlinger->setTransactionFlags(eTraversalNeeded); |
| 466 | } |
| 467 | recomputeVisibleRegions = true; |
| 468 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 469 | return true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 470 | } |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 471 | return false; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 474 | bool BufferStateLayer::hasFrameUpdate() const { |
Valerie Hau | aa19456 | 2019-02-05 16:21:38 -0800 | [diff] [blame] | 475 | const State& c(getCurrentState()); |
| 476 | return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void BufferStateLayer::setFilteringEnabled(bool enabled) { |
| 480 | GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop, |
| 481 | mCurrentTransform, enabled); |
| 482 | } |
| 483 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 484 | status_t BufferStateLayer::bindTextureImage() { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 485 | const State& s(getDrawingState()); |
| 486 | auto& engine(mFlinger->getRenderEngine()); |
| 487 | |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 488 | return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 491 | status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 492 | const State& s(getDrawingState()); |
| 493 | |
| 494 | if (!s.buffer) { |
Valerie Hau | aa19456 | 2019-02-05 16:21:38 -0800 | [diff] [blame] | 495 | if (s.bgColorLayer) { |
| 496 | for (auto& handle : mDrawingState.callbackHandles) { |
| 497 | handle->latchTime = latchTime; |
| 498 | } |
| 499 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 500 | return NO_ERROR; |
| 501 | } |
| 502 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 503 | const int32_t layerID = getSequence(); |
| 504 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 505 | // Reject if the layer is invalid |
| 506 | uint32_t bufferWidth = s.buffer->width; |
| 507 | uint32_t bufferHeight = s.buffer->height; |
| 508 | |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 509 | if (s.transform & ui::Transform::ROT_90) { |
Peiyong Lin | 3db4234 | 2018-08-16 09:15:59 -0700 | [diff] [blame] | 510 | std::swap(bufferWidth, bufferHeight); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if (s.transformToDisplayInverse) { |
| 514 | uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform(); |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 515 | if (invTransform & ui::Transform::ROT_90) { |
Peiyong Lin | 3db4234 | 2018-08-16 09:15:59 -0700 | [diff] [blame] | 516 | std::swap(bufferWidth, bufferHeight); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 520 | if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE && |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 521 | (s.active.w != bufferWidth || s.active.h != bufferHeight)) { |
| 522 | ALOGE("[%s] rejecting buffer: " |
| 523 | "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}", |
| 524 | mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h); |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 525 | mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber()); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 526 | return BAD_VALUE; |
| 527 | } |
| 528 | |
Marissa Wall | 5a68a77 | 2018-12-22 17:43:42 -0800 | [diff] [blame] | 529 | for (auto& handle : mDrawingState.callbackHandles) { |
| 530 | handle->latchTime = latchTime; |
| 531 | } |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 532 | |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 533 | if (!SyncFeatures::getInstance().useNativeFenceSync()) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 534 | // Bind the new buffer to the GL texture. |
| 535 | // |
| 536 | // Older devices require the "implicit" synchronization provided |
| 537 | // by glEGLImageTargetTexture2DOES, which this method calls. Newer |
| 538 | // devices will either call this in Layer::onDraw, or (if it's not |
| 539 | // a GL-composited layer) not at all. |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 540 | status_t err = bindTextureImage(); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 541 | if (err != NO_ERROR) { |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 542 | mFlinger->mTimeStats->onDestroy(layerID); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 543 | return BAD_VALUE; |
| 544 | } |
| 545 | } |
| 546 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 547 | mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime()); |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 548 | mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 549 | |
Marissa Wall | 16c112d | 2019-03-20 13:21:13 -0700 | [diff] [blame] | 550 | mCurrentStateModified = false; |
| 551 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 552 | return NO_ERROR; |
| 553 | } |
| 554 | |
| 555 | status_t BufferStateLayer::updateActiveBuffer() { |
| 556 | const State& s(getDrawingState()); |
| 557 | |
| 558 | if (s.buffer == nullptr) { |
| 559 | return BAD_VALUE; |
| 560 | } |
| 561 | |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 562 | if (mActiveBuffer != nullptr) { |
| 563 | // todo: get this to work with BufferStateLayerCache |
| 564 | auto& engine(mFlinger->getRenderEngine()); |
| 565 | engine.unbindExternalTextureBuffer(mActiveBuffer->getId()); |
| 566 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 567 | mActiveBuffer = s.buffer; |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 568 | mActiveBufferFence = s.acquireFence; |
Lloyd Pique | 0b785d8 | 2018-12-04 17:25:27 -0800 | [diff] [blame] | 569 | auto& layerCompositionState = getCompositionLayer()->editState().frontEnd; |
| 570 | layerCompositionState.buffer = mActiveBuffer; |
| 571 | layerCompositionState.bufferSlot = 0; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 572 | |
| 573 | return NO_ERROR; |
| 574 | } |
| 575 | |
| 576 | status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) { |
| 577 | // TODO(marissaw): support frame history events |
| 578 | mCurrentFrameNumber = mFrameNumber; |
| 579 | return NO_ERROR; |
| 580 | } |
| 581 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 582 | void BufferStateLayer::setHwcLayerBuffer(const sp<const DisplayDevice>& display) { |
| 583 | const auto outputLayer = findOutputLayerForDisplay(display); |
| 584 | LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc); |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 585 | auto& hwcInfo = *outputLayer->editState().hwc; |
| 586 | auto& hwcLayer = hwcInfo.hwcLayer; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 587 | |
| 588 | const State& s(getDrawingState()); |
| 589 | |
Valerie Hau | 13f0d1a | 2019-03-22 10:35:42 -0700 | [diff] [blame] | 590 | uint32_t hwcSlot; |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 591 | sp<GraphicBuffer> buffer; |
Valerie Hau | 13f0d1a | 2019-03-22 10:35:42 -0700 | [diff] [blame] | 592 | hwcInfo.hwcBufferCache.getHwcBuffer(BufferQueue::INVALID_BUFFER_SLOT, s.buffer, &hwcSlot, |
| 593 | &buffer); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 594 | |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 595 | auto error = hwcLayer->setBuffer(hwcSlot, buffer, s.acquireFence); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 596 | if (error != HWC2::Error::None) { |
| 597 | ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(), |
| 598 | s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error)); |
| 599 | } |
| 600 | |
| 601 | mFrameNumber++; |
| 602 | } |
| 603 | |
| 604 | void BufferStateLayer::onFirstRef() { |
Dan Stoza | 7b1b5a8 | 2018-07-31 16:00:21 -0700 | [diff] [blame] | 605 | BufferLayer::onFirstRef(); |
| 606 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 607 | if (const auto display = mFlinger->getDefaultDisplayDevice()) { |
| 608 | updateTransformHint(display); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | } // namespace android |