David Sodman | 0c69cad | 2017-08-21 12:12:51 -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 "BufferLayer" |
| 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
| 22 | #include "BufferLayer.h" |
| 23 | #include "Colorizer.h" |
| 24 | #include "DisplayDevice.h" |
| 25 | #include "LayerRejecter.h" |
| 26 | #include "clz.h" |
| 27 | |
| 28 | #include "RenderEngine/RenderEngine.h" |
| 29 | |
| 30 | #include <gui/BufferItem.h> |
| 31 | #include <gui/BufferQueue.h> |
| 32 | #include <gui/LayerDebugInfo.h> |
| 33 | #include <gui/Surface.h> |
| 34 | |
| 35 | #include <ui/DebugUtils.h> |
| 36 | |
| 37 | #include <utils/Errors.h> |
| 38 | #include <utils/Log.h> |
| 39 | #include <utils/NativeHandle.h> |
| 40 | #include <utils/StopWatch.h> |
| 41 | #include <utils/Trace.h> |
| 42 | |
| 43 | #include <cutils/compiler.h> |
| 44 | #include <cutils/native_handle.h> |
| 45 | #include <cutils/properties.h> |
| 46 | |
| 47 | #include <math.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <mutex> |
| 50 | |
| 51 | namespace android { |
| 52 | |
| 53 | BufferLayer::BufferLayer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, |
| 54 | uint32_t w, uint32_t h, uint32_t flags) |
| 55 | : Layer(flinger, client, name, w, h, flags), |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 56 | mSurfaceFlingerConsumer(nullptr), |
Ivan Lozano | eb13f9e | 2017-11-09 12:39:31 -0800 | [diff] [blame] | 57 | mTextureName(UINT32_MAX), |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 58 | mFormat(PIXEL_FORMAT_NONE), |
| 59 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 60 | mBufferLatched(false), |
| 61 | mPreviousFrameNumber(0), |
| 62 | mUpdateTexImageFailed(false), |
| 63 | mRefreshPending(false) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 64 | ALOGV("Creating Layer %s", name.string()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 65 | |
| 66 | mFlinger->getRenderEngine().genTextures(1, &mTextureName); |
| 67 | mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName); |
| 68 | |
| 69 | if (flags & ISurfaceComposerClient::eNonPremultiplied) mPremultipliedAlpha = false; |
| 70 | |
| 71 | mCurrentState.requested = mCurrentState.active; |
| 72 | |
| 73 | // drawing state & current state are identical |
| 74 | mDrawingState = mCurrentState; |
| 75 | } |
| 76 | |
| 77 | BufferLayer::~BufferLayer() { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 78 | mFlinger->deleteTextureAsync(mTextureName); |
| 79 | |
David Sodman | 6f65f3e | 2017-11-03 14:28:09 -0700 | [diff] [blame] | 80 | if (!getBE().mHwcLayers.empty()) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 81 | ALOGE("Found stale hardware composer layers when destroying " |
| 82 | "surface flinger layer %s", |
| 83 | mName.string()); |
| 84 | destroyAllHwcLayers(); |
| 85 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 86 | } |
| 87 | |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 88 | void BufferLayer::useSurfaceDamage() { |
| 89 | if (mFlinger->mForceFullDamage) { |
| 90 | surfaceDamageRegion = Region::INVALID_REGION; |
| 91 | } else { |
| 92 | surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void BufferLayer::useEmptyDamage() { |
| 97 | surfaceDamageRegion.clear(); |
| 98 | } |
| 99 | |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 100 | bool BufferLayer::isProtected() const { |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 101 | const sp<GraphicBuffer>& buffer(getBE().compositionInfo.mBuffer); |
David Sodman | 5b4cffc | 2017-11-23 13:20:29 -0800 | [diff] [blame] | 102 | return (buffer != 0) && |
| 103 | (buffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | bool BufferLayer::isVisible() const { |
| 107 | return !(isHiddenByPolicy()) && getAlpha() > 0.0f && |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 108 | (getBE().compositionInfo.mBuffer != NULL || getBE().compositionInfo.hwc.sidebandStream != NULL); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool BufferLayer::isFixedSize() const { |
| 112 | return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 113 | } |
| 114 | |
| 115 | status_t BufferLayer::setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags) { |
| 116 | uint32_t const maxSurfaceDims = |
| 117 | min(mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims()); |
| 118 | |
| 119 | // never allow a surface larger than what our underlying GL implementation |
| 120 | // can handle. |
| 121 | if ((uint32_t(w) > maxSurfaceDims) || (uint32_t(h) > maxSurfaceDims)) { |
| 122 | ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h)); |
| 123 | return BAD_VALUE; |
| 124 | } |
| 125 | |
| 126 | mFormat = format; |
| 127 | |
| 128 | mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false; |
| 129 | mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false; |
| 130 | mCurrentOpacity = getOpacityForFormat(format); |
| 131 | |
| 132 | mSurfaceFlingerConsumer->setDefaultBufferSize(w, h); |
| 133 | mSurfaceFlingerConsumer->setDefaultBufferFormat(format); |
| 134 | mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); |
| 135 | |
| 136 | return NO_ERROR; |
| 137 | } |
| 138 | |
| 139 | static constexpr mat4 inverseOrientation(uint32_t transform) { |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 140 | const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1); |
| 141 | const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1); |
| 142 | const mat4 rot90(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 143 | mat4 tr; |
| 144 | |
| 145 | if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 146 | tr = tr * rot90; |
| 147 | } |
| 148 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 149 | tr = tr * flipH; |
| 150 | } |
| 151 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 152 | tr = tr * flipV; |
| 153 | } |
| 154 | return inverse(tr); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * onDraw will draw the current layer onto the presentable buffer |
| 159 | */ |
| 160 | void BufferLayer::onDraw(const RenderArea& renderArea, const Region& clip, |
| 161 | bool useIdentityTransform) const { |
| 162 | ATRACE_CALL(); |
| 163 | |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 164 | if (CC_UNLIKELY(getBE().compositionInfo.mBuffer == 0)) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 165 | // the texture has not been created yet, this Layer has |
| 166 | // in fact never been drawn into. This happens frequently with |
| 167 | // SurfaceView because the WindowManager can't know when the client |
| 168 | // has drawn the first time. |
| 169 | |
| 170 | // If there is nothing under us, we paint the screen in black, otherwise |
| 171 | // we just skip this update. |
| 172 | |
| 173 | // figure out if there is something below us |
| 174 | Region under; |
| 175 | bool finished = false; |
| 176 | mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) { |
| 177 | if (finished || layer == static_cast<BufferLayer const*>(this)) { |
| 178 | finished = true; |
| 179 | return; |
| 180 | } |
| 181 | under.orSelf(renderArea.getTransform().transform(layer->visibleRegion)); |
| 182 | }); |
| 183 | // if not everything below us is covered, we plug the holes! |
| 184 | Region holes(clip.subtract(under)); |
| 185 | if (!holes.isEmpty()) { |
| 186 | clearWithOpenGL(renderArea, 0, 0, 0, 1); |
| 187 | } |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | // Bind the current buffer to the GL texture, and wait for it to be |
| 192 | // ready for us to draw into. |
| 193 | status_t err = mSurfaceFlingerConsumer->bindTextureImage(); |
| 194 | if (err != NO_ERROR) { |
| 195 | ALOGW("onDraw: bindTextureImage failed (err=%d)", err); |
| 196 | // Go ahead and draw the buffer anyway; no matter what we do the screen |
| 197 | // is probably going to have something visibly wrong. |
| 198 | } |
| 199 | |
| 200 | bool blackOutLayer = isProtected() || (isSecure() && !renderArea.isSecure()); |
| 201 | |
| 202 | RenderEngine& engine(mFlinger->getRenderEngine()); |
| 203 | |
| 204 | if (!blackOutLayer) { |
| 205 | // TODO: we could be more subtle with isFixedSize() |
| 206 | const bool useFiltering = getFiltering() || needsFiltering(renderArea) || isFixedSize(); |
| 207 | |
| 208 | // Query the texture matrix given our current filtering mode. |
| 209 | float textureMatrix[16]; |
| 210 | mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering); |
| 211 | mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix); |
| 212 | |
| 213 | if (getTransformToDisplayInverse()) { |
| 214 | /* |
| 215 | * the code below applies the primary display's inverse transform to |
| 216 | * the texture transform |
| 217 | */ |
| 218 | uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform(); |
| 219 | mat4 tr = inverseOrientation(transform); |
| 220 | |
| 221 | /** |
| 222 | * TODO(b/36727915): This is basically a hack. |
| 223 | * |
| 224 | * Ensure that regardless of the parent transformation, |
| 225 | * this buffer is always transformed from native display |
| 226 | * orientation to display orientation. For example, in the case |
| 227 | * of a camera where the buffer remains in native orientation, |
| 228 | * we want the pixels to always be upright. |
| 229 | */ |
| 230 | sp<Layer> p = mDrawingParent.promote(); |
| 231 | if (p != nullptr) { |
| 232 | const auto parentTransform = p->getTransform(); |
| 233 | tr = tr * inverseOrientation(parentTransform.getOrientation()); |
| 234 | } |
| 235 | |
| 236 | // and finally apply it to the original texture matrix |
| 237 | const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr); |
| 238 | memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix)); |
| 239 | } |
| 240 | |
| 241 | // Set things up for texturing. |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 242 | mTexture.setDimensions(getBE().compositionInfo.mBuffer->getWidth(), |
| 243 | getBE().compositionInfo.mBuffer->getHeight()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 244 | mTexture.setFiltering(useFiltering); |
| 245 | mTexture.setMatrix(textureMatrix); |
| 246 | |
| 247 | engine.setupLayerTexturing(mTexture); |
| 248 | } else { |
| 249 | engine.setupLayerBlackedOut(); |
| 250 | } |
| 251 | drawWithOpenGL(renderArea, useIdentityTransform); |
| 252 | engine.disableTexturing(); |
| 253 | } |
| 254 | |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 255 | void BufferLayer::onLayerDisplayed(const sp<Fence>& releaseFence) { |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 256 | mSurfaceFlingerConsumer->setReleaseFence(releaseFence); |
| 257 | } |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 258 | |
| 259 | void BufferLayer::abandon() { |
| 260 | mSurfaceFlingerConsumer->abandon(); |
| 261 | } |
| 262 | |
| 263 | bool BufferLayer::shouldPresentNow(const DispSync& dispSync) const { |
| 264 | if (mSidebandStreamChanged || mAutoRefresh) { |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | Mutex::Autolock lock(mQueueItemLock); |
| 269 | if (mQueueItems.empty()) { |
| 270 | return false; |
| 271 | } |
| 272 | auto timestamp = mQueueItems[0].mTimestamp; |
| 273 | nsecs_t expectedPresent = mSurfaceFlingerConsumer->computeExpectedPresent(dispSync); |
| 274 | |
| 275 | // Ignore timestamps more than a second in the future |
| 276 | bool isPlausible = timestamp < (expectedPresent + s2ns(1)); |
| 277 | ALOGW_IF(!isPlausible, |
| 278 | "[%s] Timestamp %" PRId64 " seems implausible " |
| 279 | "relative to expectedPresent %" PRId64, |
| 280 | mName.string(), timestamp, expectedPresent); |
| 281 | |
| 282 | bool isDue = timestamp < expectedPresent; |
| 283 | return isDue || !isPlausible; |
| 284 | } |
| 285 | |
| 286 | void BufferLayer::setTransformHint(uint32_t orientation) const { |
| 287 | mSurfaceFlingerConsumer->setTransformHint(orientation); |
| 288 | } |
| 289 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 290 | bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) { |
| 291 | if (mBufferLatched) { |
| 292 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 293 | mFrameEventHistory.addPreComposition(mCurrentFrameNumber, |
| 294 | refreshStartTime); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 295 | } |
| 296 | mRefreshPending = false; |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 297 | return mQueuedFrames > 0 || mSidebandStreamChanged || |
| 298 | mAutoRefresh; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 299 | } |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 300 | bool BufferLayer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence, |
| 301 | const std::shared_ptr<FenceTime>& presentFence, |
| 302 | const CompositorTiming& compositorTiming) { |
| 303 | // mFrameLatencyNeeded is true when a new frame was latched for the |
| 304 | // composition. |
| 305 | if (!mFrameLatencyNeeded) return false; |
| 306 | |
| 307 | // Update mFrameEventHistory. |
| 308 | { |
| 309 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 310 | mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, |
| 311 | presentFence, compositorTiming); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | // Update mFrameTracker. |
| 315 | nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp(); |
| 316 | mFrameTracker.setDesiredPresentTime(desiredPresentTime); |
| 317 | |
| 318 | std::shared_ptr<FenceTime> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFenceTime(); |
| 319 | if (frameReadyFence->isValid()) { |
| 320 | mFrameTracker.setFrameReadyFence(std::move(frameReadyFence)); |
| 321 | } else { |
| 322 | // There was no fence for this frame, so assume that it was ready |
| 323 | // to be presented at the desired present time. |
| 324 | mFrameTracker.setFrameReadyTime(desiredPresentTime); |
| 325 | } |
| 326 | |
| 327 | if (presentFence->isValid()) { |
| 328 | mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence)); |
| 329 | } else { |
| 330 | // The HWC doesn't support present fences, so use the refresh |
| 331 | // timestamp instead. |
| 332 | mFrameTracker.setActualPresentTime( |
| 333 | mFlinger->getHwComposer().getRefreshTimestamp(HWC_DISPLAY_PRIMARY)); |
| 334 | } |
| 335 | |
| 336 | mFrameTracker.advanceFrame(); |
| 337 | mFrameLatencyNeeded = false; |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | std::vector<OccupancyTracker::Segment> BufferLayer::getOccupancyHistory(bool forceFlush) { |
| 342 | std::vector<OccupancyTracker::Segment> history; |
| 343 | status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush, &history); |
| 344 | if (result != NO_ERROR) { |
| 345 | ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(), result); |
| 346 | return {}; |
| 347 | } |
| 348 | return history; |
| 349 | } |
| 350 | |
| 351 | bool BufferLayer::getTransformToDisplayInverse() const { |
| 352 | return mSurfaceFlingerConsumer->getTransformToDisplayInverse(); |
| 353 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 354 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 355 | void BufferLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) { |
| 356 | if (!mSurfaceFlingerConsumer->releasePendingBuffer()) { |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | auto releaseFenceTime = |
| 361 | std::make_shared<FenceTime>(mSurfaceFlingerConsumer->getPrevFinalReleaseFence()); |
| 362 | mReleaseTimeline.updateSignalTimes(); |
| 363 | mReleaseTimeline.push(releaseFenceTime); |
| 364 | |
| 365 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 366 | if (mPreviousFrameNumber != 0) { |
| 367 | mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime, |
| 368 | std::move(releaseFenceTime)); |
| 369 | } |
| 370 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 371 | |
| 372 | Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) { |
| 373 | ATRACE_CALL(); |
| 374 | |
| 375 | if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) { |
| 376 | // mSidebandStreamChanged was true |
| 377 | mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream(); |
David Sodman | 386c22e | 2017-11-09 16:34:46 -0800 | [diff] [blame] | 378 | // replicated in LayerBE until FE/BE is ready to be synchronized |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 379 | getBE().compositionInfo.hwc.sidebandStream = mSidebandStream; |
| 380 | if (getBE().compositionInfo.hwc.sidebandStream != NULL) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 381 | setTransactionFlags(eTransactionNeeded); |
| 382 | mFlinger->setTransactionFlags(eTraversalNeeded); |
| 383 | } |
| 384 | recomputeVisibleRegions = true; |
| 385 | |
| 386 | const State& s(getDrawingState()); |
| 387 | return getTransform().transform(Region(Rect(s.active.w, s.active.h))); |
| 388 | } |
| 389 | |
| 390 | Region outDirtyRegion; |
| 391 | if (mQueuedFrames <= 0 && !mAutoRefresh) { |
| 392 | return outDirtyRegion; |
| 393 | } |
| 394 | |
| 395 | // if we've already called updateTexImage() without going through |
| 396 | // a composition step, we have to skip this layer at this point |
| 397 | // because we cannot call updateTeximage() without a corresponding |
| 398 | // compositionComplete() call. |
| 399 | // we'll trigger an update in onPreComposition(). |
| 400 | if (mRefreshPending) { |
| 401 | return outDirtyRegion; |
| 402 | } |
| 403 | |
| 404 | // If the head buffer's acquire fence hasn't signaled yet, return and |
| 405 | // try again later |
| 406 | if (!headFenceHasSignaled()) { |
| 407 | mFlinger->signalLayerUpdate(); |
| 408 | return outDirtyRegion; |
| 409 | } |
| 410 | |
| 411 | // Capture the old state of the layer for comparisons later |
| 412 | const State& s(getDrawingState()); |
| 413 | const bool oldOpacity = isOpaque(s); |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 414 | sp<GraphicBuffer> oldBuffer = getBE().compositionInfo.mBuffer; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 415 | |
| 416 | if (!allTransactionsSignaled()) { |
| 417 | mFlinger->signalLayerUpdate(); |
| 418 | return outDirtyRegion; |
| 419 | } |
| 420 | |
| 421 | // This boolean is used to make sure that SurfaceFlinger's shadow copy |
| 422 | // of the buffer queue isn't modified when the buffer queue is returning |
| 423 | // BufferItem's that weren't actually queued. This can happen in shared |
| 424 | // buffer mode. |
| 425 | bool queuedBuffer = false; |
| 426 | LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions, |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 427 | getProducerStickyTransform() != 0, mName.string(), |
| 428 | mOverrideScalingMode, mFreezeGeometryUpdates); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 429 | status_t updateResult = |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 430 | mSurfaceFlingerConsumer->updateTexImage(&r, mFlinger->mPrimaryDispSync, |
| 431 | &mAutoRefresh, &queuedBuffer, |
| 432 | mLastFrameNumberReceived); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 433 | if (updateResult == BufferQueue::PRESENT_LATER) { |
| 434 | // Producer doesn't want buffer to be displayed yet. Signal a |
| 435 | // layer update so we check again at the next opportunity. |
| 436 | mFlinger->signalLayerUpdate(); |
| 437 | return outDirtyRegion; |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame] | 438 | } else if (updateResult == BufferLayerConsumer::BUFFER_REJECTED) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 439 | // If the buffer has been rejected, remove it from the shadow queue |
| 440 | // and return early |
| 441 | if (queuedBuffer) { |
| 442 | Mutex::Autolock lock(mQueueItemLock); |
| 443 | mQueueItems.removeAt(0); |
| 444 | android_atomic_dec(&mQueuedFrames); |
| 445 | } |
| 446 | return outDirtyRegion; |
| 447 | } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) { |
| 448 | // This can occur if something goes wrong when trying to create the |
| 449 | // EGLImage for this buffer. If this happens, the buffer has already |
| 450 | // been released, so we need to clean up the queue and bug out |
| 451 | // early. |
| 452 | if (queuedBuffer) { |
| 453 | Mutex::Autolock lock(mQueueItemLock); |
| 454 | mQueueItems.clear(); |
| 455 | android_atomic_and(0, &mQueuedFrames); |
| 456 | } |
| 457 | |
| 458 | // Once we have hit this state, the shadow queue may no longer |
| 459 | // correctly reflect the incoming BufferQueue's contents, so even if |
| 460 | // updateTexImage starts working, the only safe course of action is |
| 461 | // to continue to ignore updates. |
| 462 | mUpdateTexImageFailed = true; |
| 463 | |
| 464 | return outDirtyRegion; |
| 465 | } |
| 466 | |
| 467 | if (queuedBuffer) { |
| 468 | // Autolock scope |
| 469 | auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber(); |
| 470 | |
| 471 | Mutex::Autolock lock(mQueueItemLock); |
| 472 | |
| 473 | // Remove any stale buffers that have been dropped during |
| 474 | // updateTexImage |
| 475 | while (mQueueItems[0].mFrameNumber != currentFrameNumber) { |
| 476 | mQueueItems.removeAt(0); |
| 477 | android_atomic_dec(&mQueuedFrames); |
| 478 | } |
| 479 | |
| 480 | mQueueItems.removeAt(0); |
| 481 | } |
| 482 | |
| 483 | // Decrement the queued-frames count. Signal another event if we |
| 484 | // have more frames pending. |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 485 | if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1) || |
| 486 | mAutoRefresh) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 487 | mFlinger->signalLayerUpdate(); |
| 488 | } |
| 489 | |
| 490 | // update the active buffer |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 491 | getBE().compositionInfo.mBuffer = |
| 492 | mSurfaceFlingerConsumer->getCurrentBuffer(&getBE().compositionInfo.mBufferSlot); |
David Sodman | 5b4cffc | 2017-11-23 13:20:29 -0800 | [diff] [blame] | 493 | // replicated in LayerBE until FE/BE is ready to be synchronized |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 494 | mActiveBuffer = getBE().compositionInfo.mBuffer; |
| 495 | if (getBE().compositionInfo.mBuffer == NULL) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 496 | // this can only happen if the very first buffer was rejected. |
| 497 | return outDirtyRegion; |
| 498 | } |
| 499 | |
| 500 | mBufferLatched = true; |
| 501 | mPreviousFrameNumber = mCurrentFrameNumber; |
| 502 | mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber(); |
| 503 | |
| 504 | { |
| 505 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 506 | mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | mRefreshPending = true; |
| 510 | mFrameLatencyNeeded = true; |
David Sodman | 5b4cffc | 2017-11-23 13:20:29 -0800 | [diff] [blame] | 511 | if (oldBuffer == NULL) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 512 | // the first time we receive a buffer, we need to trigger a |
| 513 | // geometry invalidation. |
| 514 | recomputeVisibleRegions = true; |
| 515 | } |
| 516 | |
| 517 | setDataSpace(mSurfaceFlingerConsumer->getCurrentDataSpace()); |
| 518 | |
| 519 | Rect crop(mSurfaceFlingerConsumer->getCurrentCrop()); |
| 520 | const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform()); |
| 521 | const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode()); |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 522 | if ((crop != mCurrentCrop) || |
| 523 | (transform != mCurrentTransform) || |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 524 | (scalingMode != mCurrentScalingMode)) { |
| 525 | mCurrentCrop = crop; |
| 526 | mCurrentTransform = transform; |
| 527 | mCurrentScalingMode = scalingMode; |
| 528 | recomputeVisibleRegions = true; |
| 529 | } |
| 530 | |
David Sodman | 5b4cffc | 2017-11-23 13:20:29 -0800 | [diff] [blame] | 531 | if (oldBuffer != NULL) { |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 532 | uint32_t bufWidth = getBE().compositionInfo.mBuffer->getWidth(); |
| 533 | uint32_t bufHeight = getBE().compositionInfo.mBuffer->getHeight(); |
David Sodman | 5b4cffc | 2017-11-23 13:20:29 -0800 | [diff] [blame] | 534 | if (bufWidth != uint32_t(oldBuffer->width) || |
| 535 | bufHeight != uint32_t(oldBuffer->height)) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 536 | recomputeVisibleRegions = true; |
| 537 | } |
| 538 | } |
| 539 | |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 540 | mCurrentOpacity = getOpacityForFormat(getBE().compositionInfo.mBuffer->format); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 541 | if (oldOpacity != isOpaque(s)) { |
| 542 | recomputeVisibleRegions = true; |
| 543 | } |
| 544 | |
| 545 | // Remove any sync points corresponding to the buffer which was just |
| 546 | // latched |
| 547 | { |
| 548 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 549 | auto point = mLocalSyncPoints.begin(); |
| 550 | while (point != mLocalSyncPoints.end()) { |
| 551 | if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) { |
| 552 | // This sync point must have been added since we started |
| 553 | // latching. Don't drop it yet. |
| 554 | ++point; |
| 555 | continue; |
| 556 | } |
| 557 | |
| 558 | if ((*point)->getFrameNumber() <= mCurrentFrameNumber) { |
| 559 | point = mLocalSyncPoints.erase(point); |
| 560 | } else { |
| 561 | ++point; |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | // FIXME: postedRegion should be dirty & bounds |
| 567 | Region dirtyRegion(Rect(s.active.w, s.active.h)); |
| 568 | |
| 569 | // transform the dirty region to window-manager space |
| 570 | outDirtyRegion = (getTransform().transform(dirtyRegion)); |
| 571 | |
| 572 | return outDirtyRegion; |
| 573 | } |
| 574 | |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 575 | void BufferLayer::setDefaultBufferSize(uint32_t w, uint32_t h) { |
| 576 | mSurfaceFlingerConsumer->setDefaultBufferSize(w, h); |
| 577 | } |
| 578 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 579 | void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) { |
| 580 | // Apply this display's projection's viewport to the visible region |
| 581 | // before giving it to the HWC HAL. |
| 582 | const Transform& tr = displayDevice->getTransform(); |
| 583 | const auto& viewport = displayDevice->getViewport(); |
| 584 | Region visible = tr.transform(visibleRegion.intersect(viewport)); |
| 585 | auto hwcId = displayDevice->getHwcDisplayId(); |
David Sodman | 6f65f3e | 2017-11-03 14:28:09 -0700 | [diff] [blame] | 586 | auto& hwcInfo = getBE().mHwcLayers[hwcId]; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 587 | auto& hwcLayer = hwcInfo.layer; |
| 588 | auto error = hwcLayer->setVisibleRegion(visible); |
| 589 | if (error != HWC2::Error::None) { |
| 590 | ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(), |
| 591 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 592 | visible.dump(LOG_TAG); |
| 593 | } |
| 594 | |
| 595 | error = hwcLayer->setSurfaceDamage(surfaceDamageRegion); |
| 596 | if (error != HWC2::Error::None) { |
| 597 | ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(), |
| 598 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 599 | surfaceDamageRegion.dump(LOG_TAG); |
| 600 | } |
| 601 | |
| 602 | // Sideband layers |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 603 | if (getBE().compositionInfo.hwc.sidebandStream.get()) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 604 | setCompositionType(hwcId, HWC2::Composition::Sideband); |
| 605 | ALOGV("[%s] Requesting Sideband composition", mName.string()); |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 606 | error = hwcLayer->setSidebandStream(getBE().compositionInfo.hwc.sidebandStream->handle()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 607 | if (error != HWC2::Error::None) { |
| 608 | ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(), |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 609 | getBE().compositionInfo.hwc.sidebandStream->handle(), to_string(error).c_str(), |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 610 | static_cast<int32_t>(error)); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 611 | } |
| 612 | return; |
| 613 | } |
| 614 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 615 | // Device or Cursor layers |
| 616 | if (mPotentialCursor) { |
| 617 | ALOGV("[%s] Requesting Cursor composition", mName.string()); |
| 618 | setCompositionType(hwcId, HWC2::Composition::Cursor); |
| 619 | } else { |
| 620 | ALOGV("[%s] Requesting Device composition", mName.string()); |
| 621 | setCompositionType(hwcId, HWC2::Composition::Device); |
| 622 | } |
| 623 | |
| 624 | ALOGV("setPerFrameData: dataspace = %d", mCurrentState.dataSpace); |
| 625 | error = hwcLayer->setDataspace(mCurrentState.dataSpace); |
| 626 | if (error != HWC2::Error::None) { |
| 627 | ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mCurrentState.dataSpace, |
| 628 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 629 | } |
| 630 | |
| 631 | uint32_t hwcSlot = 0; |
| 632 | sp<GraphicBuffer> hwcBuffer; |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 633 | hwcInfo.bufferCache.getHwcBuffer(getBE().compositionInfo.mBufferSlot, |
| 634 | getBE().compositionInfo.mBuffer, &hwcSlot, &hwcBuffer); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 635 | |
| 636 | auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence(); |
| 637 | error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence); |
| 638 | if (error != HWC2::Error::None) { |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 639 | ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(), |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 640 | getBE().compositionInfo.mBuffer->handle, to_string(error).c_str(), |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 641 | static_cast<int32_t>(error)); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 645 | bool BufferLayer::isOpaque(const Layer::State& s) const { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 646 | // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the |
| 647 | // layer's opaque flag. |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 648 | if ((getBE().compositionInfo.hwc.sidebandStream == nullptr) && (getBE().compositionInfo.mBuffer == nullptr)) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 649 | return false; |
| 650 | } |
| 651 | |
| 652 | // if the layer has the opaque flag, then we're always opaque, |
| 653 | // otherwise we use the current buffer's format. |
| 654 | return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity; |
| 655 | } |
| 656 | |
| 657 | void BufferLayer::onFirstRef() { |
| 658 | // Creates a custom BufferQueue for SurfaceFlingerConsumer to use |
| 659 | sp<IGraphicBufferProducer> producer; |
| 660 | sp<IGraphicBufferConsumer> consumer; |
| 661 | BufferQueue::createBufferQueue(&producer, &consumer, true); |
| 662 | mProducer = new MonitoredProducer(producer, mFlinger, this); |
Chia-I Wu | 9f2db77 | 2017-11-30 21:06:50 -0800 | [diff] [blame^] | 663 | mSurfaceFlingerConsumer = new BufferLayerConsumer(consumer, |
| 664 | mFlinger->getRenderEngine(), mTextureName, this); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 665 | mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); |
| 666 | mSurfaceFlingerConsumer->setContentsChangedListener(this); |
| 667 | mSurfaceFlingerConsumer->setName(mName); |
| 668 | |
| 669 | if (mFlinger->isLayerTripleBufferingDisabled()) { |
| 670 | mProducer->setMaxDequeuedBufferCount(2); |
| 671 | } |
| 672 | |
| 673 | const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice()); |
| 674 | updateTransformHint(hw); |
| 675 | } |
| 676 | |
| 677 | // --------------------------------------------------------------------------- |
| 678 | // Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener |
| 679 | // --------------------------------------------------------------------------- |
| 680 | |
| 681 | void BufferLayer::onFrameAvailable(const BufferItem& item) { |
| 682 | // Add this buffer from our internal queue tracker |
| 683 | { // Autolock scope |
| 684 | Mutex::Autolock lock(mQueueItemLock); |
| 685 | mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(), |
| 686 | item.mGraphicBuffer->getHeight(), |
| 687 | item.mFrameNumber); |
| 688 | // Reset the frame number tracker when we receive the first buffer after |
| 689 | // a frame number reset |
| 690 | if (item.mFrameNumber == 1) { |
| 691 | mLastFrameNumberReceived = 0; |
| 692 | } |
| 693 | |
| 694 | // Ensure that callbacks are handled in order |
| 695 | while (item.mFrameNumber != mLastFrameNumberReceived + 1) { |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 696 | status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, |
| 697 | ms2ns(500)); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 698 | if (result != NO_ERROR) { |
| 699 | ALOGE("[%s] Timed out waiting on callback", mName.string()); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | mQueueItems.push_back(item); |
| 704 | android_atomic_inc(&mQueuedFrames); |
| 705 | |
| 706 | // Wake up any pending callbacks |
| 707 | mLastFrameNumberReceived = item.mFrameNumber; |
| 708 | mQueueItemCondition.broadcast(); |
| 709 | } |
| 710 | |
| 711 | mFlinger->signalLayerUpdate(); |
| 712 | } |
| 713 | |
| 714 | void BufferLayer::onFrameReplaced(const BufferItem& item) { |
| 715 | { // Autolock scope |
| 716 | Mutex::Autolock lock(mQueueItemLock); |
| 717 | |
| 718 | // Ensure that callbacks are handled in order |
| 719 | while (item.mFrameNumber != mLastFrameNumberReceived + 1) { |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 720 | status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, |
| 721 | ms2ns(500)); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 722 | if (result != NO_ERROR) { |
| 723 | ALOGE("[%s] Timed out waiting on callback", mName.string()); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | if (mQueueItems.empty()) { |
| 728 | ALOGE("Can't replace a frame on an empty queue"); |
| 729 | return; |
| 730 | } |
| 731 | mQueueItems.editItemAt(mQueueItems.size() - 1) = item; |
| 732 | |
| 733 | // Wake up any pending callbacks |
| 734 | mLastFrameNumberReceived = item.mFrameNumber; |
| 735 | mQueueItemCondition.broadcast(); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | void BufferLayer::onSidebandStreamChanged() { |
| 740 | if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) { |
| 741 | // mSidebandStreamChanged was false |
| 742 | mFlinger->signalLayerUpdate(); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | bool BufferLayer::needsFiltering(const RenderArea& renderArea) const { |
| 747 | return mNeedsFiltering || renderArea.needsFiltering(); |
| 748 | } |
| 749 | |
| 750 | // As documented in libhardware header, formats in the range |
| 751 | // 0x100 - 0x1FF are specific to the HAL implementation, and |
| 752 | // are known to have no alpha channel |
| 753 | // TODO: move definition for device-specific range into |
| 754 | // hardware.h, instead of using hard-coded values here. |
| 755 | #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) |
| 756 | |
| 757 | bool BufferLayer::getOpacityForFormat(uint32_t format) { |
| 758 | if (HARDWARE_IS_DEVICE_FORMAT(format)) { |
| 759 | return true; |
| 760 | } |
| 761 | switch (format) { |
| 762 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 763 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 764 | case HAL_PIXEL_FORMAT_RGBA_FP16: |
| 765 | case HAL_PIXEL_FORMAT_RGBA_1010102: |
| 766 | return false; |
| 767 | } |
| 768 | // in all other case, we have no blending (also for unknown formats) |
| 769 | return true; |
| 770 | } |
| 771 | |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 772 | void BufferLayer::drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 773 | const State& s(getDrawingState()); |
| 774 | |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 775 | computeGeometry(renderArea, getBE().mMesh, useIdentityTransform); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 776 | |
| 777 | /* |
| 778 | * NOTE: the way we compute the texture coordinates here produces |
| 779 | * different results than when we take the HWC path -- in the later case |
| 780 | * the "source crop" is rounded to texel boundaries. |
| 781 | * This can produce significantly different results when the texture |
| 782 | * is scaled by a large amount. |
| 783 | * |
| 784 | * The GL code below is more logical (imho), and the difference with |
| 785 | * HWC is due to a limitation of the HWC API to integers -- a question |
| 786 | * is suspend is whether we should ignore this problem or revert to |
| 787 | * GL composition when a buffer scaling is applied (maybe with some |
| 788 | * minimal value)? Or, we could make GL behave like HWC -- but this feel |
| 789 | * like more of a hack. |
| 790 | */ |
| 791 | Rect win(computeBounds()); |
| 792 | |
| 793 | Transform t = getTransform(); |
| 794 | if (!s.finalCrop.isEmpty()) { |
| 795 | win = t.transform(win); |
| 796 | if (!win.intersect(s.finalCrop, &win)) { |
| 797 | win.clear(); |
| 798 | } |
| 799 | win = t.inverse().transform(win); |
| 800 | if (!win.intersect(computeBounds(), &win)) { |
| 801 | win.clear(); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | float left = float(win.left) / float(s.active.w); |
| 806 | float top = float(win.top) / float(s.active.h); |
| 807 | float right = float(win.right) / float(s.active.w); |
| 808 | float bottom = float(win.bottom) / float(s.active.h); |
| 809 | |
| 810 | // TODO: we probably want to generate the texture coords with the mesh |
| 811 | // here we assume that we only have 4 vertices |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 812 | Mesh::VertexArray<vec2> texCoords(getBE().mMesh.getTexCoordArray<vec2>()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 813 | texCoords[0] = vec2(left, 1.0f - top); |
| 814 | texCoords[1] = vec2(left, 1.0f - bottom); |
| 815 | texCoords[2] = vec2(right, 1.0f - bottom); |
| 816 | texCoords[3] = vec2(right, 1.0f - top); |
| 817 | |
| 818 | RenderEngine& engine(mFlinger->getRenderEngine()); |
| 819 | engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), false /* disableTexture */, |
| 820 | getColor()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 821 | engine.setSourceDataSpace(mCurrentState.dataSpace); |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 822 | engine.drawMesh(getBE().mMesh); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 823 | engine.disableBlending(); |
| 824 | } |
| 825 | |
| 826 | uint32_t BufferLayer::getProducerStickyTransform() const { |
| 827 | int producerStickyTransform = 0; |
| 828 | int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform); |
| 829 | if (ret != OK) { |
| 830 | ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__, |
| 831 | strerror(-ret), ret); |
| 832 | return 0; |
| 833 | } |
| 834 | return static_cast<uint32_t>(producerStickyTransform); |
| 835 | } |
| 836 | |
| 837 | bool BufferLayer::latchUnsignaledBuffers() { |
| 838 | static bool propertyLoaded = false; |
| 839 | static bool latch = false; |
| 840 | static std::mutex mutex; |
| 841 | std::lock_guard<std::mutex> lock(mutex); |
| 842 | if (!propertyLoaded) { |
| 843 | char value[PROPERTY_VALUE_MAX] = {}; |
| 844 | property_get("debug.sf.latch_unsignaled", value, "0"); |
| 845 | latch = atoi(value); |
| 846 | propertyLoaded = true; |
| 847 | } |
| 848 | return latch; |
| 849 | } |
| 850 | |
| 851 | uint64_t BufferLayer::getHeadFrameNumber() const { |
| 852 | Mutex::Autolock lock(mQueueItemLock); |
| 853 | if (!mQueueItems.empty()) { |
| 854 | return mQueueItems[0].mFrameNumber; |
| 855 | } else { |
| 856 | return mCurrentFrameNumber; |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | bool BufferLayer::headFenceHasSignaled() const { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 861 | if (latchUnsignaledBuffers()) { |
| 862 | return true; |
| 863 | } |
| 864 | |
| 865 | Mutex::Autolock lock(mQueueItemLock); |
| 866 | if (mQueueItems.empty()) { |
| 867 | return true; |
| 868 | } |
| 869 | if (mQueueItems[0].mIsDroppable) { |
| 870 | // Even though this buffer's fence may not have signaled yet, it could |
| 871 | // be replaced by another buffer before it has a chance to, which means |
| 872 | // that it's possible to get into a situation where a buffer is never |
| 873 | // able to be latched. To avoid this, grab this buffer anyway. |
| 874 | return true; |
| 875 | } |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 876 | return mQueueItems[0].mFenceTime->getSignalTime() != |
| 877 | Fence::SIGNAL_TIME_PENDING; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | uint32_t BufferLayer::getEffectiveScalingMode() const { |
| 881 | if (mOverrideScalingMode >= 0) { |
| 882 | return mOverrideScalingMode; |
| 883 | } |
| 884 | return mCurrentScalingMode; |
| 885 | } |
| 886 | |
| 887 | // ---------------------------------------------------------------------------- |
| 888 | // transaction |
| 889 | // ---------------------------------------------------------------------------- |
| 890 | |
| 891 | void BufferLayer::notifyAvailableFrames() { |
| 892 | auto headFrameNumber = getHeadFrameNumber(); |
| 893 | bool headFenceSignaled = headFenceHasSignaled(); |
| 894 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 895 | for (auto& point : mLocalSyncPoints) { |
| 896 | if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) { |
| 897 | point->setFrameAvailable(); |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | sp<IGraphicBufferProducer> BufferLayer::getProducer() const { |
| 903 | return mProducer; |
| 904 | } |
| 905 | |
| 906 | // --------------------------------------------------------------------------- |
| 907 | // h/w composer set-up |
| 908 | // --------------------------------------------------------------------------- |
| 909 | |
| 910 | bool BufferLayer::allTransactionsSignaled() { |
| 911 | auto headFrameNumber = getHeadFrameNumber(); |
| 912 | bool matchingFramesFound = false; |
| 913 | bool allTransactionsApplied = true; |
| 914 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 915 | |
| 916 | for (auto& point : mLocalSyncPoints) { |
| 917 | if (point->getFrameNumber() > headFrameNumber) { |
| 918 | break; |
| 919 | } |
| 920 | matchingFramesFound = true; |
| 921 | |
| 922 | if (!point->frameIsAvailable()) { |
| 923 | // We haven't notified the remote layer that the frame for |
| 924 | // this point is available yet. Notify it now, and then |
| 925 | // abort this attempt to latch. |
| 926 | point->setFrameAvailable(); |
| 927 | allTransactionsApplied = false; |
| 928 | break; |
| 929 | } |
| 930 | |
| 931 | allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied(); |
| 932 | } |
| 933 | return !matchingFramesFound || allTransactionsApplied; |
| 934 | } |
| 935 | |
| 936 | } // namespace android |
| 937 | |
| 938 | #if defined(__gl_h_) |
| 939 | #error "don't include gl/gl.h in this file" |
| 940 | #endif |
| 941 | |
| 942 | #if defined(__gl2_h_) |
| 943 | #error "don't include gl2/gl2.h in this file" |
| 944 | #endif |