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