| 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 |  | 
|  | 59 | bool BufferStateLayer::shouldPresentNow(const DispSync& /*dispSync*/) const { | 
|  | 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 |  | 
|  | 326 | status_t BufferStateLayer::bindTextureImage() const { | 
|  | 327 | const State& s(getDrawingState()); | 
|  | 328 | auto& engine(mFlinger->getRenderEngine()); | 
|  | 329 |  | 
|  | 330 | if (!engine.isCurrent()) { | 
|  | 331 | ALOGE("RenderEngine is not current"); | 
|  | 332 | return INVALID_OPERATION; | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | engine.checkErrors(); | 
|  | 336 |  | 
|  | 337 | if (!mTextureImage) { | 
|  | 338 | ALOGE("no currently-bound texture"); | 
|  | 339 | engine.bindExternalTextureImage(mTextureName, *engine.createImage()); | 
|  | 340 | return NO_INIT; | 
|  | 341 | } | 
|  | 342 |  | 
|  | 343 | bool created = | 
|  | 344 | mTextureImage->setNativeWindowBuffer(s.buffer->getNativeBuffer(), | 
|  | 345 | s.buffer->getUsage() & GRALLOC_USAGE_PROTECTED); | 
|  | 346 | if (!created) { | 
|  | 347 | ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d", | 
|  | 348 | s.buffer->getWidth(), s.buffer->getHeight(), s.buffer->getStride(), | 
|  | 349 | s.buffer->getUsage(), s.buffer->getPixelFormat()); | 
|  | 350 | engine.bindExternalTextureImage(mTextureName, *engine.createImage()); | 
|  | 351 | return NO_INIT; | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | engine.bindExternalTextureImage(mTextureName, *mTextureImage); | 
|  | 355 |  | 
|  | 356 | // Wait for the new buffer to be ready. | 
|  | 357 | if (s.acquireFence->isValid()) { | 
|  | 358 | if (SyncFeatures::getInstance().useWaitSync()) { | 
|  | 359 | base::unique_fd fenceFd(s.acquireFence->dup()); | 
|  | 360 | if (fenceFd == -1) { | 
|  | 361 | ALOGE("error dup'ing fence fd: %d", errno); | 
|  | 362 | return -errno; | 
|  | 363 | } | 
|  | 364 | if (!engine.waitFence(std::move(fenceFd))) { | 
|  | 365 | ALOGE("failed to wait on fence fd"); | 
|  | 366 | return UNKNOWN_ERROR; | 
|  | 367 | } | 
|  | 368 | } else { | 
|  | 369 | status_t err = s.acquireFence->waitForever("BufferStateLayer::bindTextureImage"); | 
|  | 370 | if (err != NO_ERROR) { | 
|  | 371 | ALOGE("error waiting for fence: %d", err); | 
|  | 372 | return err; | 
|  | 373 | } | 
|  | 374 | } | 
|  | 375 | } | 
|  | 376 |  | 
|  | 377 | return NO_ERROR; | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime) { | 
|  | 381 | const State& s(getDrawingState()); | 
|  | 382 |  | 
|  | 383 | if (!s.buffer) { | 
|  | 384 | return NO_ERROR; | 
|  | 385 | } | 
|  | 386 |  | 
|  | 387 | auto& engine(mFlinger->getRenderEngine()); | 
|  | 388 | if (!engine.isCurrent()) { | 
|  | 389 | ALOGE("RenderEngine is not current"); | 
|  | 390 | return INVALID_OPERATION; | 
|  | 391 | } | 
|  | 392 | engine.checkErrors(); | 
|  | 393 |  | 
|  | 394 | // TODO(marissaw): once buffers are cached, don't create a new image everytime | 
|  | 395 | mTextureImage = engine.createImage(); | 
|  | 396 |  | 
|  | 397 | // Reject if the layer is invalid | 
|  | 398 | uint32_t bufferWidth = s.buffer->width; | 
|  | 399 | uint32_t bufferHeight = s.buffer->height; | 
|  | 400 |  | 
| Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 401 | if (s.transform & ui::Transform::ROT_90) { | 
| Peiyong Lin | 3db4234 | 2018-08-16 09:15:59 -0700 | [diff] [blame] | 402 | std::swap(bufferWidth, bufferHeight); | 
| Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 403 | } | 
|  | 404 |  | 
|  | 405 | if (s.transformToDisplayInverse) { | 
|  | 406 | uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform(); | 
| Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 407 | if (invTransform & ui::Transform::ROT_90) { | 
| Peiyong Lin | 3db4234 | 2018-08-16 09:15:59 -0700 | [diff] [blame] | 408 | std::swap(bufferWidth, bufferHeight); | 
| Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 409 | } | 
|  | 410 | } | 
|  | 411 |  | 
|  | 412 | if (mOverrideScalingMode == NATIVE_WINDOW_SCALING_MODE_FREEZE && | 
|  | 413 | (s.active.w != bufferWidth || s.active.h != bufferHeight)) { | 
|  | 414 | ALOGE("[%s] rejecting buffer: " | 
|  | 415 | "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}", | 
|  | 416 | mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h); | 
|  | 417 | mTimeStats.removeTimeRecord(getName().c_str(), getFrameNumber()); | 
|  | 418 | return BAD_VALUE; | 
|  | 419 | } | 
|  | 420 |  | 
|  | 421 | // Handle sync fences | 
|  | 422 | if (SyncFeatures::getInstance().useNativeFenceSync()) { | 
|  | 423 | base::unique_fd fenceFd = engine.flush(); | 
|  | 424 | if (fenceFd == -1) { | 
|  | 425 | ALOGE("failed to flush RenderEngine"); | 
|  | 426 | mTimeStats.clearLayerRecord(getName().c_str()); | 
|  | 427 | return UNKNOWN_ERROR; | 
|  | 428 | } | 
|  | 429 |  | 
|  | 430 | sp<Fence> fence(new Fence(std::move(fenceFd))); | 
|  | 431 |  | 
|  | 432 | // Check status of fences first because merging is expensive. | 
|  | 433 | // Merging an invalid fence with any other fence results in an | 
|  | 434 | // invalid fence. | 
|  | 435 | auto currentStatus = s.acquireFence->getStatus(); | 
|  | 436 | if (currentStatus == Fence::Status::Invalid) { | 
|  | 437 | ALOGE("Existing fence has invalid state"); | 
|  | 438 | mTimeStats.clearLayerRecord(getName().c_str()); | 
|  | 439 | return BAD_VALUE; | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | auto incomingStatus = fence->getStatus(); | 
|  | 443 | if (incomingStatus == Fence::Status::Invalid) { | 
|  | 444 | ALOGE("New fence has invalid state"); | 
|  | 445 | mDrawingState.acquireFence = fence; | 
|  | 446 | mTimeStats.clearLayerRecord(getName().c_str()); | 
|  | 447 | return BAD_VALUE; | 
|  | 448 | } | 
|  | 449 |  | 
|  | 450 | // If both fences are signaled or both are unsignaled, we need to merge | 
|  | 451 | // them to get an accurate timestamp. | 
|  | 452 | if (currentStatus == incomingStatus) { | 
|  | 453 | char fenceName[32] = {}; | 
|  | 454 | snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber); | 
|  | 455 | sp<Fence> mergedFence = Fence::merge(fenceName, mDrawingState.acquireFence, fence); | 
|  | 456 | if (!mergedFence.get()) { | 
|  | 457 | ALOGE("failed to merge release fences"); | 
|  | 458 | // synchronization is broken, the best we can do is hope fences | 
|  | 459 | // signal in order so the new fence will act like a union | 
|  | 460 | mDrawingState.acquireFence = fence; | 
|  | 461 | mTimeStats.clearLayerRecord(getName().c_str()); | 
|  | 462 | return BAD_VALUE; | 
|  | 463 | } | 
|  | 464 | mDrawingState.acquireFence = mergedFence; | 
|  | 465 | } else if (incomingStatus == Fence::Status::Unsignaled) { | 
|  | 466 | // If one fence has signaled and the other hasn't, the unsignaled | 
|  | 467 | // fence will approximately correspond with the correct timestamp. | 
|  | 468 | // There's a small race if both fences signal at about the same time | 
|  | 469 | // and their statuses are retrieved with unfortunate timing. However, | 
|  | 470 | // by this point, they will have both signaled and only the timestamp | 
|  | 471 | // will be slightly off; any dependencies after this point will | 
|  | 472 | // already have been met. | 
|  | 473 | mDrawingState.acquireFence = fence; | 
|  | 474 | } | 
|  | 475 | } else { | 
|  | 476 | // Bind the new buffer to the GL texture. | 
|  | 477 | // | 
|  | 478 | // Older devices require the "implicit" synchronization provided | 
|  | 479 | // by glEGLImageTargetTexture2DOES, which this method calls.  Newer | 
|  | 480 | // devices will either call this in Layer::onDraw, or (if it's not | 
|  | 481 | // a GL-composited layer) not at all. | 
|  | 482 | status_t err = bindTextureImage(); | 
|  | 483 | if (err != NO_ERROR) { | 
|  | 484 | mTimeStats.clearLayerRecord(getName().c_str()); | 
|  | 485 | return BAD_VALUE; | 
|  | 486 | } | 
|  | 487 | } | 
|  | 488 |  | 
|  | 489 | // TODO(marissaw): properly support mTimeStats | 
|  | 490 | const std::string layerName(getName().c_str()); | 
|  | 491 | mTimeStats.setPostTime(getName().c_str(), getFrameNumber(), latchTime); | 
|  | 492 | mTimeStats.setAcquireFence(layerName, getFrameNumber(), getCurrentFenceTime()); | 
|  | 493 | mTimeStats.setLatchTime(layerName, getFrameNumber(), latchTime); | 
|  | 494 |  | 
|  | 495 | return NO_ERROR; | 
|  | 496 | } | 
|  | 497 |  | 
|  | 498 | status_t BufferStateLayer::updateActiveBuffer() { | 
|  | 499 | const State& s(getDrawingState()); | 
|  | 500 |  | 
|  | 501 | if (s.buffer == nullptr) { | 
|  | 502 | return BAD_VALUE; | 
|  | 503 | } | 
|  | 504 |  | 
|  | 505 | mActiveBuffer = s.buffer; | 
|  | 506 | getBE().compositionInfo.mBuffer = mActiveBuffer; | 
|  | 507 | getBE().compositionInfo.mBufferSlot = 0; | 
|  | 508 |  | 
|  | 509 | return NO_ERROR; | 
|  | 510 | } | 
|  | 511 |  | 
|  | 512 | status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) { | 
|  | 513 | // TODO(marissaw): support frame history events | 
|  | 514 | mCurrentFrameNumber = mFrameNumber; | 
|  | 515 | return NO_ERROR; | 
|  | 516 | } | 
|  | 517 |  | 
|  | 518 | void BufferStateLayer::setHwcLayerBuffer(const sp<const DisplayDevice>& display) { | 
|  | 519 | const auto displayId = display->getId(); | 
|  | 520 | auto& hwcInfo = getBE().mHwcLayers[displayId]; | 
|  | 521 | auto& hwcLayer = hwcInfo.layer; | 
|  | 522 |  | 
|  | 523 | const State& s(getDrawingState()); | 
|  | 524 |  | 
|  | 525 | // TODO(marissaw): support more than one slot | 
|  | 526 | uint32_t hwcSlot = 0; | 
|  | 527 |  | 
|  | 528 | auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence); | 
|  | 529 | if (error != HWC2::Error::None) { | 
|  | 530 | ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(), | 
|  | 531 | s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error)); | 
|  | 532 | } | 
|  | 533 |  | 
|  | 534 | mFrameNumber++; | 
|  | 535 | } | 
|  | 536 |  | 
|  | 537 | void BufferStateLayer::onFirstRef() { | 
| Dan Stoza | 7b1b5a8 | 2018-07-31 16:00:21 -0700 | [diff] [blame] | 538 | BufferLayer::onFirstRef(); | 
|  | 539 |  | 
| Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 540 | if (const auto display = mFlinger->getDefaultDisplayDevice()) { | 
|  | 541 | updateTransformHint(display); | 
|  | 542 | } | 
|  | 543 | } | 
|  | 544 |  | 
|  | 545 | } // namespace android |