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