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