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