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