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 | |
| 22 | #include "BufferStateLayer.h" |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 23 | |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 24 | #include "TimeStats/TimeStats.h" |
| 25 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 26 | #include <private/gui/SyncFeatures.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 27 | #include <renderengine/Image.h> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 31 | // clang-format off |
| 32 | const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{ |
| 33 | 1, 0, 0, 0, |
| 34 | 0, 1, 0, 0, |
| 35 | 0, 0, 1, 0, |
| 36 | 0, 0, 0, 1 |
| 37 | }; |
| 38 | // clang-format on |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 39 | |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 40 | BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) : BufferLayer(args) { |
| 41 | mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
| 42 | } |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 43 | BufferStateLayer::~BufferStateLayer() = default; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 44 | |
| 45 | // ----------------------------------------------------------------------- |
| 46 | // Interface implementation for Layer |
| 47 | // ----------------------------------------------------------------------- |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 48 | void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) { |
| 49 | // The transaction completed callback can only be sent if the release fence from the PREVIOUS |
| 50 | // frame has fired. In practice, we should never actually wait on the previous release fence |
| 51 | // but we should store it just in case. |
| 52 | mPreviousReleaseFence = releaseFence; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const { |
| 56 | // TODO(marissaw): send the transform hint to buffer owner |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 64 | bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 65 | if (getSidebandStreamChanged() || getAutoRefresh()) { |
| 66 | return true; |
| 67 | } |
| 68 | |
Marissa Wall | 024a191 | 2018-08-13 13:55:35 -0700 | [diff] [blame] | 69 | return hasFrameUpdate(); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 72 | bool BufferStateLayer::willPresentCurrentTransaction() const { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 73 | Mutex::Autolock lock(mStateMutex); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 74 | // Returns true if the most recent Transaction applied to CurrentState will be presented. |
| 75 | return getSidebandStreamChanged() || getAutoRefresh() || |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 76 | (mState.current.modified && mState.current.buffer != nullptr); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 79 | bool BufferStateLayer::getTransformToDisplayInverseLocked() const { |
| 80 | return mState.current.transformToDisplayInverse; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 83 | void BufferStateLayer::pushPendingStateLocked() { |
| 84 | if (!mState.current.modified) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 85 | return; |
| 86 | } |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 87 | mState.pending.push_back(mState.current); |
| 88 | ATRACE_INT(mTransactionName.string(), mState.pending.size()); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 92 | const bool stateUpdateAvailable = !mState.pending.empty(); |
| 93 | while (!mState.pending.empty()) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 94 | popPendingState(stateToCommit); |
| 95 | } |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 96 | mCurrentStateModified = stateUpdateAvailable && mState.current.modified; |
| 97 | mState.current.modified = false; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 98 | return stateUpdateAvailable; |
| 99 | } |
| 100 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 101 | // Crop that applies to the window |
| 102 | Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const { |
| 103 | return Rect::INVALID_RECT; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | bool BufferStateLayer::setTransform(uint32_t transform) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 107 | Mutex::Autolock lock(mStateMutex); |
| 108 | if (mState.current.transform == transform) return false; |
| 109 | mState.current.sequence++; |
| 110 | mState.current.transform = transform; |
| 111 | mState.current.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 112 | setTransactionFlags(eTransactionNeeded); |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 117 | Mutex::Autolock lock(mStateMutex); |
| 118 | if (mState.current.transformToDisplayInverse == transformToDisplayInverse) return false; |
| 119 | mState.current.sequence++; |
| 120 | mState.current.transformToDisplayInverse = transformToDisplayInverse; |
| 121 | mState.current.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 122 | setTransactionFlags(eTransactionNeeded); |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool BufferStateLayer::setCrop(const Rect& crop) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 127 | Mutex::Autolock lock(mStateMutex); |
| 128 | if (mState.current.crop == crop) return false; |
| 129 | mState.current.sequence++; |
| 130 | mState.current.crop = crop; |
| 131 | mState.current.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 132 | setTransactionFlags(eTransactionNeeded); |
| 133 | return true; |
| 134 | } |
| 135 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 136 | bool BufferStateLayer::setFrame(const Rect& frame) { |
| 137 | int x = frame.left; |
| 138 | int y = frame.top; |
| 139 | int w = frame.getWidth(); |
| 140 | int h = frame.getHeight(); |
| 141 | |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 142 | Mutex::Autolock lock(mStateMutex); |
| 143 | if (mState.current.active.transform.tx() == x && mState.current.active.transform.ty() == y && |
| 144 | mState.current.active.w == w && mState.current.active.h == h) { |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 145 | return false; |
| 146 | } |
| 147 | |
| 148 | if (!frame.isValid()) { |
| 149 | x = y = w = h = 0; |
| 150 | } |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 151 | mState.current.active.transform.set(x, y); |
| 152 | mState.current.active.w = w; |
| 153 | mState.current.active.h = h; |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 154 | |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 155 | mState.current.sequence++; |
| 156 | mState.current.modified = true; |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 157 | setTransactionFlags(eTransactionNeeded); |
| 158 | return true; |
| 159 | } |
| 160 | |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 161 | bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 162 | Mutex::Autolock lock(mStateMutex); |
| 163 | if (mState.current.buffer) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 164 | mReleasePreviousBuffer = true; |
| 165 | } |
| 166 | |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 167 | mState.current.sequence++; |
| 168 | mState.current.buffer = buffer; |
| 169 | mState.current.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 170 | setTransactionFlags(eTransactionNeeded); |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 175 | Mutex::Autolock lock(mStateMutex); |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 176 | // The acquire fences of BufferStateLayers have already signaled before they are set |
| 177 | mCallbackHandleAcquireTime = fence->getSignalTime(); |
| 178 | |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 179 | mState.current.acquireFence = fence; |
| 180 | mState.current.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 181 | setTransactionFlags(eTransactionNeeded); |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 186 | Mutex::Autolock lock(mStateMutex); |
| 187 | if (mState.current.dataspace == dataspace) return false; |
| 188 | mState.current.sequence++; |
| 189 | mState.current.dataspace = dataspace; |
| 190 | mState.current.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 191 | setTransactionFlags(eTransactionNeeded); |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 196 | Mutex::Autolock lock(mStateMutex); |
| 197 | if (mState.current.hdrMetadata == hdrMetadata) return false; |
| 198 | mState.current.sequence++; |
| 199 | mState.current.hdrMetadata = hdrMetadata; |
| 200 | mState.current.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 201 | setTransactionFlags(eTransactionNeeded); |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 206 | Mutex::Autolock lock(mStateMutex); |
| 207 | mState.current.sequence++; |
| 208 | mState.current.surfaceDamageRegion = surfaceDamage; |
| 209 | mState.current.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 210 | setTransactionFlags(eTransactionNeeded); |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | bool BufferStateLayer::setApi(int32_t api) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 215 | Mutex::Autolock lock(mStateMutex); |
| 216 | if (mState.current.api == api) return false; |
| 217 | mState.current.sequence++; |
| 218 | mState.current.api = api; |
| 219 | mState.current.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 220 | setTransactionFlags(eTransactionNeeded); |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 225 | Mutex::Autolock lock(mStateMutex); |
| 226 | if (mState.current.sidebandStream == sidebandStream) return false; |
| 227 | mState.current.sequence++; |
| 228 | mState.current.sidebandStream = sidebandStream; |
| 229 | mState.current.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 230 | setTransactionFlags(eTransactionNeeded); |
| 231 | |
| 232 | if (!mSidebandStreamChanged.exchange(true)) { |
| 233 | // mSidebandStreamChanged was false |
| 234 | mFlinger->signalLayerUpdate(); |
| 235 | } |
| 236 | return true; |
| 237 | } |
| 238 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 239 | bool BufferStateLayer::setTransactionCompletedListeners( |
| 240 | const std::vector<sp<CallbackHandle>>& handles) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 241 | // 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] | 242 | if (handles.empty()) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 243 | mReleasePreviousBuffer = false; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | |
| 247 | const bool willPresent = willPresentCurrentTransaction(); |
| 248 | |
| 249 | for (const auto& handle : handles) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 250 | // If this transaction set a buffer on this layer, release its previous buffer |
| 251 | handle->releasePreviousBuffer = mReleasePreviousBuffer; |
| 252 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 253 | // If this layer will be presented in this frame |
| 254 | if (willPresent) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 255 | // If this transaction set an acquire fence on this layer, set its acquire time |
| 256 | handle->acquireTime = mCallbackHandleAcquireTime; |
| 257 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 258 | // Notify the transaction completed thread that there is a pending latched callback |
| 259 | // handle |
| 260 | mFlinger->getTransactionCompletedThread().registerPendingLatchedCallbackHandle(handle); |
| 261 | |
| 262 | // Store so latched time and release fence can be set |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 263 | { |
| 264 | Mutex::Autolock lock(mStateMutex); |
| 265 | mState.current.callbackHandles.push_back(handle); |
| 266 | } |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 267 | |
| 268 | } else { // If this layer will NOT need to be relatched and presented this frame |
| 269 | // Notify the transaction completed thread this handle is done |
| 270 | mFlinger->getTransactionCompletedThread().addUnlatchedCallbackHandle(handle); |
| 271 | } |
| 272 | } |
| 273 | |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 274 | mReleasePreviousBuffer = false; |
| 275 | mCallbackHandleAcquireTime = -1; |
| 276 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 277 | return willPresent; |
| 278 | } |
| 279 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 280 | bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 281 | Mutex::Autolock lock(mStateMutex); |
| 282 | mState.current.transparentRegionHint = transparent; |
| 283 | mState.current.modified = true; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 284 | setTransactionFlags(eTransactionNeeded); |
| 285 | return true; |
| 286 | } |
| 287 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 288 | Rect BufferStateLayer::getBufferSize(const State& s) const { |
| 289 | // for buffer state layers we use the display frame size as the buffer size. |
| 290 | if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) { |
| 291 | return Rect(getActiveWidth(s), getActiveHeight(s)); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 294 | // if the display frame is not defined, use the parent bounds as the buffer size. |
| 295 | const auto& p = mDrawingParent.promote(); |
| 296 | if (p != nullptr) { |
| 297 | Rect parentBounds = Rect(p->computeBounds(Region())); |
| 298 | if (!parentBounds.isEmpty()) { |
| 299 | return parentBounds; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // if there is no parent layer, use the buffer's bounds as the buffer size |
| 304 | if (s.buffer) { |
| 305 | return s.buffer->getBounds(); |
| 306 | } |
| 307 | return Rect::INVALID_RECT; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 308 | } |
| 309 | // ----------------------------------------------------------------------- |
| 310 | |
| 311 | // ----------------------------------------------------------------------- |
| 312 | // Interface implementation for BufferLayer |
| 313 | // ----------------------------------------------------------------------- |
| 314 | bool BufferStateLayer::fenceHasSignaled() const { |
| 315 | if (latchUnsignaledBuffers()) { |
| 316 | return true; |
| 317 | } |
| 318 | |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 319 | Mutex::Autolock lock(mStateMutex); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 320 | return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled; |
| 321 | } |
| 322 | |
| 323 | nsecs_t BufferStateLayer::getDesiredPresentTime() { |
| 324 | // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics |
| 325 | return 0; |
| 326 | } |
| 327 | |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 328 | std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTimeLocked() const { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 329 | return std::make_shared<FenceTime>(getDrawingState().acquireFence); |
| 330 | } |
| 331 | |
| 332 | void BufferStateLayer::getDrawingTransformMatrix(float *matrix) { |
| 333 | std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix); |
| 334 | } |
| 335 | |
| 336 | uint32_t BufferStateLayer::getDrawingTransform() const { |
| 337 | return getDrawingState().transform; |
| 338 | } |
| 339 | |
| 340 | ui::Dataspace BufferStateLayer::getDrawingDataSpace() const { |
| 341 | return getDrawingState().dataspace; |
| 342 | } |
| 343 | |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 344 | // Crop that applies to the buffer |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 345 | Rect BufferStateLayer::getDrawingCrop() const { |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 346 | const State& s(getDrawingState()); |
| 347 | |
| 348 | if (s.crop.isEmpty() && s.buffer) { |
| 349 | return s.buffer->getBounds(); |
| 350 | } |
| 351 | return s.crop; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | uint32_t BufferStateLayer::getDrawingScalingMode() const { |
Marissa Wall | ec463ac | 2018-10-08 12:35:04 -0700 | [diff] [blame] | 355 | return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | Region BufferStateLayer::getDrawingSurfaceDamage() const { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 359 | Mutex::Autolock lock(mStateMutex); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 360 | return getDrawingState().surfaceDamageRegion; |
| 361 | } |
| 362 | |
| 363 | const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 364 | Mutex::Autolock lock(mStateMutex); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 365 | return getDrawingState().hdrMetadata; |
| 366 | } |
| 367 | |
| 368 | int BufferStateLayer::getDrawingApi() const { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 369 | Mutex::Autolock lock(mStateMutex); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 370 | return getDrawingState().api; |
| 371 | } |
| 372 | |
| 373 | PixelFormat BufferStateLayer::getPixelFormat() const { |
Marissa Wall | 5aec641 | 2018-11-14 11:49:18 -0800 | [diff] [blame] | 374 | if (!mActiveBuffer) { |
| 375 | return PIXEL_FORMAT_NONE; |
| 376 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 377 | return mActiveBuffer->format; |
| 378 | } |
| 379 | |
| 380 | uint64_t BufferStateLayer::getFrameNumber() const { |
| 381 | return mFrameNumber; |
| 382 | } |
| 383 | |
| 384 | bool BufferStateLayer::getAutoRefresh() const { |
| 385 | // TODO(marissaw): support shared buffer mode |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | bool BufferStateLayer::getSidebandStreamChanged() const { |
| 390 | return mSidebandStreamChanged.load(); |
| 391 | } |
| 392 | |
| 393 | std::optional<Region> BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 394 | Mutex::Autolock lock(mStateMutex); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 395 | if (mSidebandStreamChanged.exchange(false)) { |
| 396 | const State& s(getDrawingState()); |
| 397 | // mSidebandStreamChanged was true |
| 398 | // replicated in LayerBE until FE/BE is ready to be synchronized |
| 399 | getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream; |
| 400 | if (getBE().compositionInfo.hwc.sidebandStream != nullptr) { |
| 401 | setTransactionFlags(eTransactionNeeded); |
| 402 | mFlinger->setTransactionFlags(eTraversalNeeded); |
| 403 | } |
| 404 | recomputeVisibleRegions = true; |
| 405 | |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 406 | return getTransformLocked().transform(Region(Rect(s.active.w, s.active.h))); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 407 | } |
| 408 | return {}; |
| 409 | } |
| 410 | |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 411 | bool BufferStateLayer::hasFrameUpdateLocked() const { |
Marissa Wall | 024a191 | 2018-08-13 13:55:35 -0700 | [diff] [blame] | 412 | return mCurrentStateModified && getCurrentState().buffer != nullptr; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void BufferStateLayer::setFilteringEnabled(bool enabled) { |
| 416 | GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop, |
| 417 | mCurrentTransform, enabled); |
| 418 | } |
| 419 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 420 | status_t BufferStateLayer::bindTextureImage() { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 421 | Mutex::Autolock lock(mStateMutex); |
| 422 | return bindTextureImageLocked(); |
| 423 | } |
| 424 | status_t BufferStateLayer::bindTextureImageLocked() { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 425 | const State& s(getDrawingState()); |
| 426 | auto& engine(mFlinger->getRenderEngine()); |
| 427 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 428 | engine.checkErrors(); |
| 429 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 430 | // TODO(marissaw): once buffers are cached, don't create a new image everytime |
| 431 | mTextureImage = engine.createImage(); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 432 | |
| 433 | bool created = |
| 434 | mTextureImage->setNativeWindowBuffer(s.buffer->getNativeBuffer(), |
| 435 | s.buffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
| 436 | if (!created) { |
| 437 | ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d", |
| 438 | s.buffer->getWidth(), s.buffer->getHeight(), s.buffer->getStride(), |
| 439 | s.buffer->getUsage(), s.buffer->getPixelFormat()); |
| 440 | engine.bindExternalTextureImage(mTextureName, *engine.createImage()); |
| 441 | return NO_INIT; |
| 442 | } |
| 443 | |
| 444 | engine.bindExternalTextureImage(mTextureName, *mTextureImage); |
| 445 | |
| 446 | // Wait for the new buffer to be ready. |
| 447 | if (s.acquireFence->isValid()) { |
| 448 | if (SyncFeatures::getInstance().useWaitSync()) { |
| 449 | base::unique_fd fenceFd(s.acquireFence->dup()); |
| 450 | if (fenceFd == -1) { |
| 451 | ALOGE("error dup'ing fence fd: %d", errno); |
| 452 | return -errno; |
| 453 | } |
| 454 | if (!engine.waitFence(std::move(fenceFd))) { |
| 455 | ALOGE("failed to wait on fence fd"); |
| 456 | return UNKNOWN_ERROR; |
| 457 | } |
| 458 | } else { |
| 459 | status_t err = s.acquireFence->waitForever("BufferStateLayer::bindTextureImage"); |
| 460 | if (err != NO_ERROR) { |
| 461 | ALOGE("error waiting for fence: %d", err); |
| 462 | return err; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return NO_ERROR; |
| 468 | } |
| 469 | |
Alec Mouri | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 470 | status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime, |
| 471 | const sp<Fence>& releaseFence) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 472 | const State& s(getDrawingState()); |
| 473 | |
| 474 | if (!s.buffer) { |
| 475 | return NO_ERROR; |
| 476 | } |
| 477 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 478 | const int32_t layerID = getSequence(); |
| 479 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 480 | // Reject if the layer is invalid |
| 481 | uint32_t bufferWidth = s.buffer->width; |
| 482 | uint32_t bufferHeight = s.buffer->height; |
| 483 | |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 484 | if (s.transform & ui::Transform::ROT_90) { |
Peiyong Lin | 3db4234 | 2018-08-16 09:15:59 -0700 | [diff] [blame] | 485 | std::swap(bufferWidth, bufferHeight); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | if (s.transformToDisplayInverse) { |
| 489 | uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform(); |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 490 | if (invTransform & ui::Transform::ROT_90) { |
Peiyong Lin | 3db4234 | 2018-08-16 09:15:59 -0700 | [diff] [blame] | 491 | std::swap(bufferWidth, bufferHeight); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 495 | if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE && |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 496 | (s.active.w != bufferWidth || s.active.h != bufferHeight)) { |
| 497 | ALOGE("[%s] rejecting buffer: " |
| 498 | "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}", |
| 499 | mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h); |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 500 | mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber()); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 501 | return BAD_VALUE; |
| 502 | } |
| 503 | |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 504 | mFlinger->getTransactionCompletedThread() |
| 505 | .addLatchedCallbackHandles(getDrawingState().callbackHandles, latchTime, |
| 506 | mPreviousReleaseFence); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 507 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 508 | // Handle sync fences |
Alec Mouri | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 509 | if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) { |
| 510 | // TODO(alecmouri): Fail somewhere upstream if the fence is invalid. |
| 511 | if (!releaseFence->isValid()) { |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 512 | mFlinger->mTimeStats->onDestroy(layerID); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 513 | return UNKNOWN_ERROR; |
| 514 | } |
| 515 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 516 | // Check status of fences first because merging is expensive. |
| 517 | // Merging an invalid fence with any other fence results in an |
| 518 | // invalid fence. |
| 519 | auto currentStatus = s.acquireFence->getStatus(); |
| 520 | if (currentStatus == Fence::Status::Invalid) { |
| 521 | ALOGE("Existing fence has invalid state"); |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 522 | mFlinger->mTimeStats->onDestroy(layerID); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 523 | return BAD_VALUE; |
| 524 | } |
| 525 | |
Alec Mouri | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 526 | auto incomingStatus = releaseFence->getStatus(); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 527 | if (incomingStatus == Fence::Status::Invalid) { |
| 528 | ALOGE("New fence has invalid state"); |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 529 | mState.drawing.acquireFence = releaseFence; |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 530 | mFlinger->mTimeStats->onDestroy(layerID); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 531 | return BAD_VALUE; |
| 532 | } |
| 533 | |
| 534 | // If both fences are signaled or both are unsignaled, we need to merge |
| 535 | // them to get an accurate timestamp. |
| 536 | if (currentStatus == incomingStatus) { |
| 537 | char fenceName[32] = {}; |
| 538 | snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber); |
Alec Mouri | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 539 | sp<Fence> mergedFence = |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 540 | Fence::merge(fenceName, mState.drawing.acquireFence, releaseFence); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 541 | if (!mergedFence.get()) { |
| 542 | ALOGE("failed to merge release fences"); |
| 543 | // synchronization is broken, the best we can do is hope fences |
| 544 | // signal in order so the new fence will act like a union |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 545 | mState.drawing.acquireFence = releaseFence; |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 546 | mFlinger->mTimeStats->onDestroy(layerID); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 547 | return BAD_VALUE; |
| 548 | } |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 549 | mState.drawing.acquireFence = mergedFence; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 550 | } else if (incomingStatus == Fence::Status::Unsignaled) { |
| 551 | // If one fence has signaled and the other hasn't, the unsignaled |
| 552 | // fence will approximately correspond with the correct timestamp. |
| 553 | // There's a small race if both fences signal at about the same time |
| 554 | // and their statuses are retrieved with unfortunate timing. However, |
| 555 | // by this point, they will have both signaled and only the timestamp |
| 556 | // will be slightly off; any dependencies after this point will |
| 557 | // already have been met. |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 558 | mState.drawing.acquireFence = releaseFence; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 559 | } |
| 560 | } else { |
| 561 | // Bind the new buffer to the GL texture. |
| 562 | // |
| 563 | // Older devices require the "implicit" synchronization provided |
| 564 | // by glEGLImageTargetTexture2DOES, which this method calls. Newer |
| 565 | // devices will either call this in Layer::onDraw, or (if it's not |
| 566 | // a GL-composited layer) not at all. |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 567 | status_t err = bindTextureImageLocked(); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 568 | if (err != NO_ERROR) { |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 569 | mFlinger->mTimeStats->onDestroy(layerID); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 570 | return BAD_VALUE; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | // TODO(marissaw): properly support mTimeStats |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 575 | mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime); |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 576 | mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTimeLocked()); |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 577 | mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 578 | |
| 579 | return NO_ERROR; |
| 580 | } |
| 581 | |
| 582 | status_t BufferStateLayer::updateActiveBuffer() { |
| 583 | const State& s(getDrawingState()); |
| 584 | |
| 585 | if (s.buffer == nullptr) { |
| 586 | return BAD_VALUE; |
| 587 | } |
| 588 | |
| 589 | mActiveBuffer = s.buffer; |
| 590 | getBE().compositionInfo.mBuffer = mActiveBuffer; |
| 591 | getBE().compositionInfo.mBufferSlot = 0; |
| 592 | |
| 593 | return NO_ERROR; |
| 594 | } |
| 595 | |
| 596 | status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) { |
| 597 | // TODO(marissaw): support frame history events |
| 598 | mCurrentFrameNumber = mFrameNumber; |
| 599 | return NO_ERROR; |
| 600 | } |
| 601 | |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 602 | void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) { |
Ady Abraham | 8372988 | 2018-12-07 12:26:48 -0800 | [diff] [blame] | 603 | Mutex::Autolock lock(mStateMutex); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 604 | auto& hwcInfo = getBE().mHwcLayers[displayId]; |
| 605 | auto& hwcLayer = hwcInfo.layer; |
| 606 | |
| 607 | const State& s(getDrawingState()); |
| 608 | |
| 609 | // TODO(marissaw): support more than one slot |
| 610 | uint32_t hwcSlot = 0; |
| 611 | |
| 612 | auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence); |
| 613 | if (error != HWC2::Error::None) { |
| 614 | ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(), |
| 615 | s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error)); |
| 616 | } |
| 617 | |
Marissa Wall | 024a191 | 2018-08-13 13:55:35 -0700 | [diff] [blame] | 618 | mCurrentStateModified = false; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 619 | mFrameNumber++; |
| 620 | } |
| 621 | |
| 622 | void BufferStateLayer::onFirstRef() { |
Dan Stoza | 7b1b5a8 | 2018-07-31 16:00:21 -0700 | [diff] [blame] | 623 | BufferLayer::onFirstRef(); |
| 624 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 625 | if (const auto display = mFlinger->getDefaultDisplayDevice()) { |
| 626 | updateTransformHint(display); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | } // namespace android |