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 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 21 | //#define LOG_NDEBUG 0 |
| 22 | #undef LOG_TAG |
| 23 | #define LOG_TAG "BufferLayer" |
| 24 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 25 | |
Alec Mouri | e60041e | 2019-06-14 18:59:51 -0700 | [diff] [blame] | 26 | #include "BufferLayer.h" |
Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 27 | |
| 28 | #include <compositionengine/CompositionEngine.h> |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 29 | #include <compositionengine/LayerFECompositionState.h> |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 30 | #include <compositionengine/OutputLayer.h> |
| 31 | #include <compositionengine/impl/OutputLayerCompositionState.h> |
Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 32 | #include <cutils/compiler.h> |
| 33 | #include <cutils/native_handle.h> |
| 34 | #include <cutils/properties.h> |
| 35 | #include <gui/BufferItem.h> |
| 36 | #include <gui/BufferQueue.h> |
chaviw | f83ce18 | 2019-09-12 14:43:08 -0700 | [diff] [blame] | 37 | #include <gui/GLConsumer.h> |
Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 38 | #include <gui/LayerDebugInfo.h> |
| 39 | #include <gui/Surface.h> |
| 40 | #include <renderengine/RenderEngine.h> |
| 41 | #include <ui/DebugUtils.h> |
| 42 | #include <utils/Errors.h> |
| 43 | #include <utils/Log.h> |
| 44 | #include <utils/NativeHandle.h> |
| 45 | #include <utils/StopWatch.h> |
| 46 | #include <utils/Trace.h> |
| 47 | |
Alec Mouri | e60041e | 2019-06-14 18:59:51 -0700 | [diff] [blame] | 48 | #include <cmath> |
| 49 | #include <cstdlib> |
| 50 | #include <mutex> |
| 51 | #include <sstream> |
| 52 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 53 | #include "Colorizer.h" |
| 54 | #include "DisplayDevice.h" |
Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 55 | #include "FrameTracer/FrameTracer.h" |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 56 | #include "LayerRejecter.h" |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 57 | #include "TimeStats/TimeStats.h" |
| 58 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 59 | namespace android { |
| 60 | |
Peiyong Lin | 1a70eca | 2019-11-15 09:33:33 -0800 | [diff] [blame] | 61 | static constexpr float defaultMaxMasteringLuminance = 1000.0; |
| 62 | static constexpr float defaultMaxContentLuminance = 1000.0; |
| 63 | |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 64 | BufferLayer::BufferLayer(const LayerCreationArgs& args) |
Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 65 | : Layer(args), |
chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 66 | mTextureName(args.textureName), |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 67 | mCompositionState{mFlinger->getCompositionEngine().createLayerFECompositionState()} { |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 68 | ALOGV("Creating Layer %s", getDebugName()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 69 | |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 70 | mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 71 | |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 72 | mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow; |
| 73 | mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | BufferLayer::~BufferLayer() { |
chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 77 | if (!isClone()) { |
| 78 | // The original layer and the clone layer share the same texture. Therefore, only one of |
| 79 | // the layers, in this case the original layer, needs to handle the deletion. The original |
| 80 | // layer and the clone should be removed at the same time so there shouldn't be any issue |
| 81 | // with the clone layer trying to use the deleted texture. |
| 82 | mFlinger->deleteTextureAsync(mTextureName); |
| 83 | } |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 84 | const int32_t layerId = getSequence(); |
| 85 | mFlinger->mTimeStats->onDestroy(layerId); |
| 86 | mFlinger->mFrameTracer->onDestroy(layerId); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 87 | } |
| 88 | |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 89 | void BufferLayer::useSurfaceDamage() { |
| 90 | if (mFlinger->mForceFullDamage) { |
| 91 | surfaceDamageRegion = Region::INVALID_REGION; |
| 92 | } else { |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 93 | surfaceDamageRegion = mBufferInfo.mSurfaceDamage; |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | void BufferLayer::useEmptyDamage() { |
| 98 | surfaceDamageRegion.clear(); |
| 99 | } |
| 100 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 101 | bool BufferLayer::isOpaque(const Layer::State& s) const { |
| 102 | // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the |
| 103 | // layer's opaque flag. |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 104 | if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
| 108 | // if the layer has the opaque flag, then we're always opaque, |
| 109 | // otherwise we use the current buffer's format. |
| 110 | return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | bool BufferLayer::isVisible() const { |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 114 | return !isHiddenByPolicy() && getAlpha() > 0.0f && |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 115 | (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | bool BufferLayer::isFixedSize() const { |
| 119 | return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 120 | } |
| 121 | |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 122 | bool BufferLayer::usesSourceCrop() const { |
| 123 | return true; |
| 124 | } |
| 125 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 126 | static constexpr mat4 inverseOrientation(uint32_t transform) { |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 127 | const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1); |
| 128 | const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1); |
| 129 | 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] | 130 | mat4 tr; |
| 131 | |
| 132 | if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 133 | tr = tr * rot90; |
| 134 | } |
| 135 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 136 | tr = tr * flipH; |
| 137 | } |
| 138 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 139 | tr = tr * flipV; |
| 140 | } |
| 141 | return inverse(tr); |
| 142 | } |
| 143 | |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 144 | std::optional<compositionengine::LayerFE::LayerSettings> BufferLayer::prepareClientComposition( |
Lloyd Pique | f16688f | 2019-02-19 17:47:57 -0800 | [diff] [blame] | 145 | compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 146 | ATRACE_CALL(); |
Lloyd Pique | f16688f | 2019-02-19 17:47:57 -0800 | [diff] [blame] | 147 | |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 148 | std::optional<compositionengine::LayerFE::LayerSettings> result = |
| 149 | Layer::prepareClientComposition(targetSettings); |
Lloyd Pique | f16688f | 2019-02-19 17:47:57 -0800 | [diff] [blame] | 150 | if (!result) { |
| 151 | return result; |
| 152 | } |
| 153 | |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 154 | if (CC_UNLIKELY(mBufferInfo.mBuffer == 0)) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 155 | // the texture has not been created yet, this Layer has |
| 156 | // in fact never been drawn into. This happens frequently with |
| 157 | // SurfaceView because the WindowManager can't know when the client |
| 158 | // has drawn the first time. |
| 159 | |
| 160 | // If there is nothing under us, we paint the screen in black, otherwise |
| 161 | // we just skip this update. |
| 162 | |
| 163 | // figure out if there is something below us |
| 164 | Region under; |
| 165 | bool finished = false; |
| 166 | mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) { |
| 167 | if (finished || layer == static_cast<BufferLayer const*>(this)) { |
| 168 | finished = true; |
| 169 | return; |
| 170 | } |
Lloyd Pique | a246866 | 2019-03-07 21:31:06 -0800 | [diff] [blame] | 171 | |
| 172 | under.orSelf(layer->getScreenBounds()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 173 | }); |
| 174 | // if not everything below us is covered, we plug the holes! |
Lloyd Pique | f16688f | 2019-02-19 17:47:57 -0800 | [diff] [blame] | 175 | Region holes(targetSettings.clip.subtract(under)); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 176 | if (!holes.isEmpty()) { |
Lloyd Pique | f16688f | 2019-02-19 17:47:57 -0800 | [diff] [blame] | 177 | targetSettings.clearRegion.orSelf(holes); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 178 | } |
Tianhua Sun | dcff00c | 2020-12-29 07:20:55 -0500 | [diff] [blame] | 179 | |
| 180 | if (mSidebandStream != nullptr) { |
| 181 | // For surfaceview of tv sideband, there is no activeBuffer |
| 182 | // in bufferqueue, we need return LayerSettings. |
| 183 | return result; |
| 184 | } else { |
| 185 | return std::nullopt; |
| 186 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 187 | } |
Ady Abraham | 7a60afb | 2021-03-29 13:20:55 -0700 | [diff] [blame] | 188 | const bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) || |
yuhui.zhang | a102ff9 | 2021-04-09 14:45:51 +0800 | [diff] [blame] | 189 | ((isSecure() || isProtected()) && !targetSettings.isSecure); |
Ady Abraham | 7a60afb | 2021-03-29 13:20:55 -0700 | [diff] [blame] | 190 | const bool bufferCanBeUsedAsHwTexture = |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 191 | mBufferInfo.mBuffer->getBuffer()->getUsage() & GraphicBuffer::USAGE_HW_TEXTURE; |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 192 | compositionengine::LayerFE::LayerSettings& layer = *result; |
Ady Abraham | 7a60afb | 2021-03-29 13:20:55 -0700 | [diff] [blame] | 193 | if (blackOutLayer || !bufferCanBeUsedAsHwTexture) { |
| 194 | ALOGE_IF(!bufferCanBeUsedAsHwTexture, "%s is blacked out as buffer is not gpu readable", |
| 195 | mName.c_str()); |
Vishnu Nair | b87d94f | 2020-02-13 09:17:36 -0800 | [diff] [blame] | 196 | prepareClearClientComposition(layer, true /* blackout */); |
| 197 | return layer; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 198 | } |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 199 | |
Vishnu Nair | b87d94f | 2020-02-13 09:17:36 -0800 | [diff] [blame] | 200 | const State& s(getDrawingState()); |
| 201 | layer.source.buffer.buffer = mBufferInfo.mBuffer; |
| 202 | layer.source.buffer.isOpaque = isOpaque(s); |
| 203 | layer.source.buffer.fence = mBufferInfo.mFence; |
| 204 | layer.source.buffer.textureName = mTextureName; |
| 205 | layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha(); |
| 206 | layer.source.buffer.isY410BT2020 = isHdrY410(); |
| 207 | bool hasSmpte2086 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::SMPTE2086; |
| 208 | bool hasCta861_3 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::CTA861_3; |
| 209 | layer.source.buffer.maxMasteringLuminance = hasSmpte2086 |
| 210 | ? mBufferInfo.mHdrMetadata.smpte2086.maxLuminance |
| 211 | : defaultMaxMasteringLuminance; |
| 212 | layer.source.buffer.maxContentLuminance = hasCta861_3 |
| 213 | ? mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel |
| 214 | : defaultMaxContentLuminance; |
| 215 | layer.frameNumber = mCurrentFrameNumber; |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 216 | layer.bufferId = mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer()->getId() : 0; |
Vishnu Nair | b87d94f | 2020-02-13 09:17:36 -0800 | [diff] [blame] | 217 | |
Vishnu Nair | e7f79c5 | 2020-10-29 14:45:03 -0700 | [diff] [blame] | 218 | const bool useFiltering = |
| 219 | targetSettings.needsFiltering || mNeedsFiltering || bufferNeedsFiltering(); |
Vishnu Nair | b87d94f | 2020-02-13 09:17:36 -0800 | [diff] [blame] | 220 | |
| 221 | // Query the texture matrix given our current filtering mode. |
| 222 | float textureMatrix[16]; |
| 223 | getDrawingTransformMatrix(useFiltering, textureMatrix); |
| 224 | |
| 225 | if (getTransformToDisplayInverse()) { |
| 226 | /* |
| 227 | * the code below applies the primary display's inverse transform to |
| 228 | * the texture transform |
| 229 | */ |
| 230 | uint32_t transform = DisplayDevice::getPrimaryDisplayRotationFlags(); |
| 231 | mat4 tr = inverseOrientation(transform); |
| 232 | |
| 233 | /** |
| 234 | * TODO(b/36727915): This is basically a hack. |
| 235 | * |
| 236 | * Ensure that regardless of the parent transformation, |
| 237 | * this buffer is always transformed from native display |
| 238 | * orientation to display orientation. For example, in the case |
| 239 | * of a camera where the buffer remains in native orientation, |
| 240 | * we want the pixels to always be upright. |
| 241 | */ |
| 242 | sp<Layer> p = mDrawingParent.promote(); |
| 243 | if (p != nullptr) { |
| 244 | const auto parentTransform = p->getTransform(); |
| 245 | tr = tr * inverseOrientation(parentTransform.getOrientation()); |
| 246 | } |
| 247 | |
| 248 | // and finally apply it to the original texture matrix |
| 249 | const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr); |
| 250 | memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix)); |
| 251 | } |
| 252 | |
| 253 | const Rect win{getBounds()}; |
| 254 | float bufferWidth = getBufferSize(s).getWidth(); |
| 255 | float bufferHeight = getBufferSize(s).getHeight(); |
| 256 | |
| 257 | // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has |
| 258 | // been set and there is no parent layer bounds. In that case, the scale is meaningless so |
| 259 | // ignore them. |
| 260 | if (!getBufferSize(s).isValid()) { |
| 261 | bufferWidth = float(win.right) - float(win.left); |
| 262 | bufferHeight = float(win.bottom) - float(win.top); |
| 263 | } |
| 264 | |
| 265 | const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight; |
| 266 | const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth; |
| 267 | const float translateY = float(win.top) / bufferHeight; |
| 268 | const float translateX = float(win.left) / bufferWidth; |
| 269 | |
| 270 | // Flip y-coordinates because GLConsumer expects OpenGL convention. |
| 271 | mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) * |
| 272 | mat4::translate(vec4(-.5, -.5, 0, 1)) * |
| 273 | mat4::translate(vec4(translateX, translateY, 0, 1)) * |
| 274 | mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0)); |
| 275 | |
| 276 | layer.source.buffer.useTextureFiltering = useFiltering; |
| 277 | layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr; |
| 278 | |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 279 | return layer; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 282 | bool BufferLayer::isHdrY410() const { |
| 283 | // pixel format is HDR Y410 masquerading as RGBA_1010102 |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 284 | return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ && |
| 285 | mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA && |
chaviw | debadb8 | 2020-03-26 14:57:24 -0700 | [diff] [blame] | 286 | mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 289 | sp<compositionengine::LayerFE> BufferLayer::getCompositionEngineLayerFE() const { |
| 290 | return asLayerFE(); |
| 291 | } |
| 292 | |
| 293 | compositionengine::LayerFECompositionState* BufferLayer::editCompositionState() { |
| 294 | return mCompositionState.get(); |
| 295 | } |
| 296 | |
| 297 | const compositionengine::LayerFECompositionState* BufferLayer::getCompositionState() const { |
| 298 | return mCompositionState.get(); |
| 299 | } |
| 300 | |
| 301 | void BufferLayer::preparePerFrameCompositionState() { |
| 302 | Layer::preparePerFrameCompositionState(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 303 | |
| 304 | // Sideband layers |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 305 | auto* compositionState = editCompositionState(); |
| 306 | if (compositionState->sidebandStream.get()) { |
| 307 | compositionState->compositionType = Hwc2::IComposerClient::Composition::SIDEBAND; |
Robert Carr | a6bb2bc | 2020-04-08 11:03:23 -0700 | [diff] [blame] | 308 | return; |
David Sodman | 1509411 | 2018-10-11 09:39:37 -0700 | [diff] [blame] | 309 | } else { |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 310 | // Normal buffer layers |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 311 | compositionState->hdrMetadata = mBufferInfo.mHdrMetadata; |
| 312 | compositionState->compositionType = mPotentialCursor |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 313 | ? Hwc2::IComposerClient::Composition::CURSOR |
| 314 | : Hwc2::IComposerClient::Composition::DEVICE; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 315 | } |
Robert Carr | a6bb2bc | 2020-04-08 11:03:23 -0700 | [diff] [blame] | 316 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 317 | compositionState->buffer = mBufferInfo.mBuffer->getBuffer(); |
Robert Carr | a6bb2bc | 2020-04-08 11:03:23 -0700 | [diff] [blame] | 318 | compositionState->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT) |
| 319 | ? 0 |
| 320 | : mBufferInfo.mBufferSlot; |
| 321 | compositionState->acquireFence = mBufferInfo.mFence; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 324 | bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) { |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 325 | if (mBufferInfo.mBuffer != nullptr) { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 326 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 327 | mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 328 | } |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 329 | mRefreshPending = false; |
| 330 | return hasReadyFrame(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 331 | } |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 332 | namespace { |
| 333 | TimeStats::SetFrameRateVote frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate) { |
| 334 | using FrameRateCompatibility = TimeStats::SetFrameRateVote::FrameRateCompatibility; |
| 335 | using Seamlessness = TimeStats::SetFrameRateVote::Seamlessness; |
| 336 | const auto frameRateCompatibility = [frameRate] { |
| 337 | switch (frameRate.type) { |
| 338 | case Layer::FrameRateCompatibility::Default: |
| 339 | return FrameRateCompatibility::Default; |
| 340 | case Layer::FrameRateCompatibility::ExactOrMultiple: |
| 341 | return FrameRateCompatibility::ExactOrMultiple; |
| 342 | default: |
| 343 | return FrameRateCompatibility::Undefined; |
| 344 | } |
| 345 | }(); |
| 346 | |
| 347 | const auto seamlessness = [frameRate] { |
| 348 | switch (frameRate.seamlessness) { |
| 349 | case scheduler::Seamlessness::OnlySeamless: |
| 350 | return Seamlessness::ShouldBeSeamless; |
| 351 | case scheduler::Seamlessness::SeamedAndSeamless: |
| 352 | return Seamlessness::NotRequired; |
| 353 | default: |
| 354 | return Seamlessness::Undefined; |
| 355 | } |
| 356 | }(); |
| 357 | |
| 358 | return TimeStats::SetFrameRateVote{.frameRate = frameRate.rate.getValue(), |
| 359 | .frameRateCompatibility = frameRateCompatibility, |
| 360 | .seamlessness = seamlessness}; |
| 361 | } |
| 362 | } // namespace |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 363 | |
Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 364 | bool BufferLayer::onPostComposition(const DisplayDevice* display, |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 365 | const std::shared_ptr<FenceTime>& glDoneFence, |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 366 | const std::shared_ptr<FenceTime>& presentFence, |
| 367 | const CompositorTiming& compositorTiming) { |
| 368 | // mFrameLatencyNeeded is true when a new frame was latched for the |
| 369 | // composition. |
chaviw | 74b0317 | 2019-08-19 11:09:03 -0700 | [diff] [blame] | 370 | if (!mBufferInfo.mFrameLatencyNeeded) return false; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 371 | |
| 372 | // Update mFrameEventHistory. |
Dan Stoza | 436ccf3 | 2018-06-21 12:10:12 -0700 | [diff] [blame] | 373 | { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 374 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 375 | mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence, |
| 376 | compositorTiming); |
Valerie Hau | 93d5a5e | 2020-02-13 14:50:01 -0800 | [diff] [blame] | 377 | finalizeFrameEventHistory(glDoneFence, compositorTiming); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 380 | // Update mFrameTracker. |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 381 | nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 382 | mFrameTracker.setDesiredPresentTime(desiredPresentTime); |
| 383 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 384 | const int32_t layerId = getSequence(); |
| 385 | mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 386 | |
Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 387 | const auto outputLayer = findOutputLayerForDisplay(display); |
Adithya Srinivasan | b69e076 | 2019-11-11 18:39:53 -0800 | [diff] [blame] | 388 | if (outputLayer && outputLayer->requiresClientComposition()) { |
| 389 | nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp; |
| 390 | mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber, |
| 391 | clientCompositionTimestamp, |
| 392 | FrameTracer::FrameEvent::FALLBACK_COMPOSITION); |
Adithya Srinivasan | b6a2fa1 | 2021-03-13 00:23:09 +0000 | [diff] [blame] | 393 | // Update the SurfaceFrames in the drawing state |
| 394 | if (mDrawingState.bufferSurfaceFrameTX) { |
| 395 | mDrawingState.bufferSurfaceFrameTX->setGpuComposition(); |
| 396 | } |
| 397 | for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) { |
| 398 | surfaceFrame->setGpuComposition(); |
| 399 | } |
Adithya Srinivasan | b69e076 | 2019-11-11 18:39:53 -0800 | [diff] [blame] | 400 | } |
| 401 | |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 402 | std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 403 | if (frameReadyFence->isValid()) { |
| 404 | mFrameTracker.setFrameReadyFence(std::move(frameReadyFence)); |
| 405 | } else { |
| 406 | // There was no fence for this frame, so assume that it was ready |
| 407 | // to be presented at the desired present time. |
| 408 | mFrameTracker.setFrameReadyTime(desiredPresentTime); |
Dominik Laskowski | 45de9bd | 2018-06-11 17:44:10 -0700 | [diff] [blame] | 409 | } |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 410 | |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 411 | const Fps refreshRate = mFlinger->mRefreshRateConfigs->getCurrentRefreshRate().getFps(); |
| 412 | const std::optional<Fps> renderRate = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid()); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 413 | if (presentFence->isValid()) { |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 414 | mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence, |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 415 | refreshRate, renderRate, |
| 416 | frameRateToSetFrameRateVotePayload( |
| 417 | mDrawingState.frameRate)); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 418 | mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber, |
Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 419 | presentFence, FrameTracer::FrameEvent::PRESENT_FENCE); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 420 | mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence)); |
Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 421 | } else if (!display) { |
| 422 | // Do nothing. |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 423 | } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId()); |
Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 424 | displayId && mFlinger->getHwComposer().isConnected(*displayId)) { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 425 | // The HWC doesn't support present fences, so use the refresh |
| 426 | // timestamp instead. |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 427 | const nsecs_t actualPresentTime = display->getRefreshTimestamp(); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 428 | mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime, |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 429 | refreshRate, renderRate, |
| 430 | frameRateToSetFrameRateVotePayload( |
| 431 | mDrawingState.frameRate)); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 432 | mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber, |
Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 433 | actualPresentTime, |
| 434 | FrameTracer::FrameEvent::PRESENT_FENCE); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 435 | mFrameTracker.setActualPresentTime(actualPresentTime); |
| 436 | } |
| 437 | |
| 438 | mFrameTracker.advanceFrame(); |
chaviw | 74b0317 | 2019-08-19 11:09:03 -0700 | [diff] [blame] | 439 | mBufferInfo.mFrameLatencyNeeded = false; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 440 | return true; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 441 | } |
| 442 | |
chaviw | debadb8 | 2020-03-26 14:57:24 -0700 | [diff] [blame] | 443 | void BufferLayer::gatherBufferInfo() { |
| 444 | mBufferInfo.mPixelFormat = |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 445 | !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getBuffer()->format; |
chaviw | debadb8 | 2020-03-26 14:57:24 -0700 | [diff] [blame] | 446 | mBufferInfo.mFrameLatencyNeeded = true; |
| 447 | } |
| 448 | |
Ady Abraham | 63a3e59 | 2021-01-06 10:47:15 -0800 | [diff] [blame] | 449 | bool BufferLayer::shouldPresentNow(nsecs_t expectedPresentTime) const { |
| 450 | // If this is not a valid vsync for the layer's uid, return and try again later |
| 451 | const bool isVsyncValidForUid = |
| 452 | mFlinger->mScheduler->isVsyncValid(expectedPresentTime, mOwnerUid); |
| 453 | if (!isVsyncValidForUid) { |
| 454 | ATRACE_NAME("!isVsyncValidForUid"); |
| 455 | return false; |
| 456 | } |
| 457 | |
| 458 | // AutoRefresh layers and sideband streams should always be presented |
| 459 | if (getSidebandStreamChanged() || getAutoRefresh()) { |
| 460 | return true; |
| 461 | } |
| 462 | |
Ady Abraham | 63a3e59 | 2021-01-06 10:47:15 -0800 | [diff] [blame] | 463 | // If this layer doesn't have a frame is shouldn't be presented |
| 464 | if (!hasFrameUpdate()) { |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | // Defer to the derived class to decide whether the next buffer is due for |
| 469 | // presentation. |
| 470 | return isBufferDue(expectedPresentTime); |
| 471 | } |
| 472 | |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 473 | bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime, |
| 474 | nsecs_t expectedPresentTime) { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 475 | ATRACE_CALL(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 476 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 477 | bool refreshRequired = latchSidebandStream(recomputeVisibleRegions); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 478 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 479 | if (refreshRequired) { |
| 480 | return refreshRequired; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 483 | if (!hasReadyFrame()) { |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 484 | return false; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 485 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 486 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 487 | // if we've already called updateTexImage() without going through |
| 488 | // a composition step, we have to skip this layer at this point |
| 489 | // because we cannot call updateTeximage() without a corresponding |
| 490 | // compositionComplete() call. |
| 491 | // we'll trigger an update in onPreComposition(). |
| 492 | if (mRefreshPending) { |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 493 | return false; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // If the head buffer's acquire fence hasn't signaled yet, return and |
| 497 | // try again later |
| 498 | if (!fenceHasSignaled()) { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 499 | ATRACE_NAME("!fenceHasSignaled()"); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 500 | mFlinger->signalLayerUpdate(); |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 501 | return false; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | // Capture the old state of the layer for comparisons later |
| 505 | const State& s(getDrawingState()); |
| 506 | const bool oldOpacity = isOpaque(s); |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 507 | |
| 508 | BufferInfo oldBufferInfo = mBufferInfo; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 509 | |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 510 | status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 511 | if (err != NO_ERROR) { |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 512 | return false; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | err = updateActiveBuffer(); |
| 516 | if (err != NO_ERROR) { |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 517 | return false; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 520 | err = updateFrameNumber(latchTime); |
| 521 | if (err != NO_ERROR) { |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 522 | return false; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 523 | } |
| 524 | |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 525 | gatherBufferInfo(); |
| 526 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 527 | mRefreshPending = true; |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 528 | if (oldBufferInfo.mBuffer == nullptr) { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 529 | // the first time we receive a buffer, we need to trigger a |
| 530 | // geometry invalidation. |
| 531 | recomputeVisibleRegions = true; |
| 532 | } |
| 533 | |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 534 | if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) || |
| 535 | (mBufferInfo.mTransform != oldBufferInfo.mTransform) || |
| 536 | (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) || |
| 537 | (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 538 | recomputeVisibleRegions = true; |
| 539 | } |
| 540 | |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 541 | if (oldBufferInfo.mBuffer != nullptr) { |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 542 | uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth(); |
| 543 | uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight(); |
| 544 | if (bufWidth != uint32_t(oldBufferInfo.mBuffer->getBuffer()->width) || |
| 545 | bufHeight != uint32_t(oldBufferInfo.mBuffer->getBuffer()->height)) { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 546 | recomputeVisibleRegions = true; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if (oldOpacity != isOpaque(s)) { |
| 551 | recomputeVisibleRegions = true; |
| 552 | } |
| 553 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 554 | return true; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 557 | bool BufferLayer::hasReadyFrame() const { |
Marissa Wall | 024a191 | 2018-08-13 13:55:35 -0700 | [diff] [blame] | 558 | return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh(); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | uint32_t BufferLayer::getEffectiveScalingMode() const { |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 562 | return mBufferInfo.mScaleMode; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | bool BufferLayer::isProtected() const { |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 566 | return (mBufferInfo.mBuffer != nullptr) && |
| 567 | (mBufferInfo.mBuffer->getBuffer()->getUsage() & GRALLOC_USAGE_PROTECTED); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 568 | } |
| 569 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 570 | // As documented in libhardware header, formats in the range |
| 571 | // 0x100 - 0x1FF are specific to the HAL implementation, and |
| 572 | // are known to have no alpha channel |
| 573 | // TODO: move definition for device-specific range into |
| 574 | // hardware.h, instead of using hard-coded values here. |
| 575 | #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) |
| 576 | |
| 577 | bool BufferLayer::getOpacityForFormat(uint32_t format) { |
| 578 | if (HARDWARE_IS_DEVICE_FORMAT(format)) { |
| 579 | return true; |
| 580 | } |
| 581 | switch (format) { |
| 582 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 583 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 584 | case HAL_PIXEL_FORMAT_RGBA_FP16: |
| 585 | case HAL_PIXEL_FORMAT_RGBA_1010102: |
| 586 | return false; |
| 587 | } |
| 588 | // in all other case, we have no blending (also for unknown formats) |
| 589 | return true; |
| 590 | } |
| 591 | |
Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 592 | bool BufferLayer::needsFiltering(const DisplayDevice* display) const { |
| 593 | const auto outputLayer = findOutputLayerForDisplay(display); |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 594 | if (outputLayer == nullptr) { |
Lloyd Pique | f16688f | 2019-02-19 17:47:57 -0800 | [diff] [blame] | 595 | return false; |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Lloyd Pique | f16688f | 2019-02-19 17:47:57 -0800 | [diff] [blame] | 598 | // We need filtering if the sourceCrop rectangle size does not match the |
| 599 | // displayframe rectangle size (not a 1:1 render) |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 600 | const auto& compositionState = outputLayer->getState(); |
| 601 | const auto displayFrame = compositionState.displayFrame; |
| 602 | const auto sourceCrop = compositionState.sourceCrop; |
Lloyd Pique | f16688f | 2019-02-19 17:47:57 -0800 | [diff] [blame] | 603 | return sourceCrop.getHeight() != displayFrame.getHeight() || |
Peiyong Lin | c2020ca | 2019-01-10 11:36:12 -0800 | [diff] [blame] | 604 | sourceCrop.getWidth() != displayFrame.getWidth(); |
Chia-I Wu | 692e083 | 2018-06-05 15:46:58 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 607 | bool BufferLayer::needsFilteringForScreenshots(const DisplayDevice* display, |
Alec Mouri | 5a6d857 | 2020-03-23 23:56:15 -0700 | [diff] [blame] | 608 | const ui::Transform& inverseParentTransform) const { |
Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 609 | const auto outputLayer = findOutputLayerForDisplay(display); |
Alec Mouri | 5a6d857 | 2020-03-23 23:56:15 -0700 | [diff] [blame] | 610 | if (outputLayer == nullptr) { |
| 611 | return false; |
| 612 | } |
| 613 | |
| 614 | // We need filtering if the sourceCrop rectangle size does not match the |
| 615 | // viewport rectangle size (not a 1:1 render) |
| 616 | const auto& compositionState = outputLayer->getState(); |
Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 617 | const ui::Transform& displayTransform = display->getTransform(); |
Alec Mouri | 5a6d857 | 2020-03-23 23:56:15 -0700 | [diff] [blame] | 618 | const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse(); |
| 619 | // Undo the transformation of the displayFrame so that we're back into |
| 620 | // layer-stack space. |
| 621 | const Rect frame = inverseTransform.transform(compositionState.displayFrame); |
| 622 | const FloatRect sourceCrop = compositionState.sourceCrop; |
| 623 | |
| 624 | int32_t frameHeight = frame.getHeight(); |
| 625 | int32_t frameWidth = frame.getWidth(); |
| 626 | // If the display transform had a rotational component then undo the |
| 627 | // rotation so that the orientation matches the source crop. |
| 628 | if (displayTransform.getOrientation() & ui::Transform::ROT_90) { |
| 629 | std::swap(frameHeight, frameWidth); |
| 630 | } |
| 631 | return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth; |
| 632 | } |
| 633 | |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 634 | uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 635 | if (hasFrameUpdate()) { |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 636 | return getFrameNumber(expectedPresentTime); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 637 | } else { |
| 638 | return mCurrentFrameNumber; |
| 639 | } |
| 640 | } |
| 641 | |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 642 | Rect BufferLayer::getBufferSize(const State& s) const { |
| 643 | // If we have a sideband stream, or we are scaling the buffer then return the layer size since |
| 644 | // we cannot determine the buffer size. |
| 645 | if ((s.sidebandStream != nullptr) || |
| 646 | (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) { |
| 647 | return Rect(getActiveWidth(s), getActiveHeight(s)); |
| 648 | } |
| 649 | |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 650 | if (mBufferInfo.mBuffer == nullptr) { |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 651 | return Rect::INVALID_RECT; |
| 652 | } |
| 653 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 654 | uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth(); |
| 655 | uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight(); |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 656 | |
| 657 | // Undo any transformations on the buffer and return the result. |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 658 | if (mBufferInfo.mTransform & ui::Transform::ROT_90) { |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 659 | std::swap(bufWidth, bufHeight); |
| 660 | } |
| 661 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 662 | if (getTransformToDisplayInverse()) { |
Dominik Laskowski | 718f960 | 2019-11-09 20:01:35 -0800 | [diff] [blame] | 663 | uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags(); |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 664 | if (invTransform & ui::Transform::ROT_90) { |
| 665 | std::swap(bufWidth, bufHeight); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | return Rect(bufWidth, bufHeight); |
| 670 | } |
| 671 | |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 672 | FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const { |
| 673 | const State& s(getDrawingState()); |
| 674 | |
| 675 | // If we have a sideband stream, or we are scaling the buffer then return the layer size since |
| 676 | // we cannot determine the buffer size. |
| 677 | if ((s.sidebandStream != nullptr) || |
| 678 | (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) { |
| 679 | return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s)); |
| 680 | } |
| 681 | |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 682 | if (mBufferInfo.mBuffer == nullptr) { |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 683 | return parentBounds; |
| 684 | } |
| 685 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 686 | uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth(); |
| 687 | uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight(); |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 688 | |
| 689 | // Undo any transformations on the buffer and return the result. |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 690 | if (mBufferInfo.mTransform & ui::Transform::ROT_90) { |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 691 | std::swap(bufWidth, bufHeight); |
| 692 | } |
| 693 | |
| 694 | if (getTransformToDisplayInverse()) { |
Dominik Laskowski | 718f960 | 2019-11-09 20:01:35 -0800 | [diff] [blame] | 695 | uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags(); |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 696 | if (invTransform & ui::Transform::ROT_90) { |
| 697 | std::swap(bufWidth, bufHeight); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | return FloatRect(0, 0, bufWidth, bufHeight); |
| 702 | } |
| 703 | |
chaviw | 49a108c | 2019-08-12 11:23:06 -0700 | [diff] [blame] | 704 | void BufferLayer::latchAndReleaseBuffer() { |
| 705 | mRefreshPending = false; |
| 706 | if (hasReadyFrame()) { |
| 707 | bool ignored = false; |
| 708 | latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */); |
| 709 | } |
| 710 | releasePendingBuffer(systemTime()); |
| 711 | } |
| 712 | |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 713 | PixelFormat BufferLayer::getPixelFormat() const { |
| 714 | return mBufferInfo.mPixelFormat; |
| 715 | } |
| 716 | |
| 717 | bool BufferLayer::getTransformToDisplayInverse() const { |
| 718 | return mBufferInfo.mTransformToDisplayInverse; |
| 719 | } |
| 720 | |
| 721 | Rect BufferLayer::getBufferCrop() const { |
| 722 | // this is the crop rectangle that applies to the buffer |
| 723 | // itself (as opposed to the window) |
| 724 | if (!mBufferInfo.mCrop.isEmpty()) { |
| 725 | // if the buffer crop is defined, we use that |
| 726 | return mBufferInfo.mCrop; |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 727 | } else if (mBufferInfo.mBuffer != nullptr) { |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 728 | // otherwise we use the whole buffer |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 729 | return mBufferInfo.mBuffer->getBuffer()->getBounds(); |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 730 | } else { |
| 731 | // if we don't have a buffer yet, we use an empty/invalid crop |
| 732 | return Rect(); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | uint32_t BufferLayer::getBufferTransform() const { |
| 737 | return mBufferInfo.mTransform; |
| 738 | } |
| 739 | |
| 740 | ui::Dataspace BufferLayer::getDataSpace() const { |
| 741 | return mBufferInfo.mDataspace; |
| 742 | } |
| 743 | |
| 744 | ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) { |
| 745 | ui::Dataspace updatedDataspace = dataspace; |
| 746 | // translate legacy dataspaces to modern dataspaces |
| 747 | switch (dataspace) { |
| 748 | case ui::Dataspace::SRGB: |
| 749 | updatedDataspace = ui::Dataspace::V0_SRGB; |
| 750 | break; |
| 751 | case ui::Dataspace::SRGB_LINEAR: |
| 752 | updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR; |
| 753 | break; |
| 754 | case ui::Dataspace::JFIF: |
| 755 | updatedDataspace = ui::Dataspace::V0_JFIF; |
| 756 | break; |
| 757 | case ui::Dataspace::BT601_625: |
| 758 | updatedDataspace = ui::Dataspace::V0_BT601_625; |
| 759 | break; |
| 760 | case ui::Dataspace::BT601_525: |
| 761 | updatedDataspace = ui::Dataspace::V0_BT601_525; |
| 762 | break; |
| 763 | case ui::Dataspace::BT709: |
| 764 | updatedDataspace = ui::Dataspace::V0_BT709; |
| 765 | break; |
| 766 | default: |
| 767 | break; |
| 768 | } |
| 769 | |
| 770 | return updatedDataspace; |
| 771 | } |
| 772 | |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 773 | sp<GraphicBuffer> BufferLayer::getBuffer() const { |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 774 | return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr; |
chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 775 | } |
| 776 | |
chaviw | f83ce18 | 2019-09-12 14:43:08 -0700 | [diff] [blame] | 777 | void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) { |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 778 | GLConsumer::computeTransformMatrix(outMatrix, |
| 779 | mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() |
| 780 | : nullptr, |
| 781 | mBufferInfo.mCrop, mBufferInfo.mTransform, filteringEnabled); |
chaviw | f83ce18 | 2019-09-12 14:43:08 -0700 | [diff] [blame] | 782 | } |
| 783 | |
chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 784 | void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) { |
| 785 | Layer::setInitialValuesForClone(clonedFrom); |
| 786 | |
| 787 | sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get()); |
| 788 | mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha; |
| 789 | mPotentialCursor = bufferClonedFrom->mPotentialCursor; |
| 790 | mProtectedByApp = bufferClonedFrom->mProtectedByApp; |
chaviw | 74b0317 | 2019-08-19 11:09:03 -0700 | [diff] [blame] | 791 | |
| 792 | updateCloneBufferInfo(); |
| 793 | } |
| 794 | |
| 795 | void BufferLayer::updateCloneBufferInfo() { |
| 796 | if (!isClone() || !isClonedFromAlive()) { |
| 797 | return; |
| 798 | } |
| 799 | |
| 800 | sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get()); |
| 801 | mBufferInfo = clonedFrom->mBufferInfo; |
| 802 | mSidebandStream = clonedFrom->mSidebandStream; |
| 803 | surfaceDamageRegion = clonedFrom->surfaceDamageRegion; |
| 804 | mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load(); |
| 805 | mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber; |
| 806 | |
| 807 | // After buffer info is updated, the drawingState from the real layer needs to be copied into |
| 808 | // the cloned. This is because some properties of drawingState can change when latchBuffer is |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 809 | // called. However, copying the drawingState would also overwrite the cloned layer's relatives |
| 810 | // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in |
| 811 | // the cloned drawingState again. |
chaviw | 74b0317 | 2019-08-19 11:09:03 -0700 | [diff] [blame] | 812 | wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf; |
| 813 | SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 814 | wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop; |
| 815 | InputWindowInfo tmpInputInfo = mDrawingState.inputInfo; |
| 816 | |
chaviw | 74b0317 | 2019-08-19 11:09:03 -0700 | [diff] [blame] | 817 | mDrawingState = clonedFrom->mDrawingState; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 818 | |
| 819 | mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop; |
chaviw | 74b0317 | 2019-08-19 11:09:03 -0700 | [diff] [blame] | 820 | mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf; |
| 821 | mDrawingState.zOrderRelatives = tmpZOrderRelatives; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 822 | mDrawingState.inputInfo = tmpInputInfo; |
chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 825 | void BufferLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) { |
Vishnu Nair | 6213bd9 | 2020-05-08 17:42:25 -0700 | [diff] [blame] | 826 | mTransformHint = getFixedTransformHint(); |
| 827 | if (mTransformHint == ui::Transform::ROT_INVALID) { |
| 828 | mTransformHint = displayTransformHint; |
| 829 | } |
| 830 | } |
| 831 | |
Vishnu Nair | e7f79c5 | 2020-10-29 14:45:03 -0700 | [diff] [blame] | 832 | bool BufferLayer::bufferNeedsFiltering() const { |
| 833 | return isFixedSize(); |
| 834 | } |
| 835 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 836 | } // namespace android |
| 837 | |
| 838 | #if defined(__gl_h_) |
| 839 | #error "don't include gl/gl.h in this file" |
| 840 | #endif |
| 841 | |
| 842 | #if defined(__gl2_h_) |
| 843 | #error "don't include gl2/gl2.h in this file" |
| 844 | #endif |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 845 | |
| 846 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 847 | #pragma clang diagnostic pop // ignored "-Wconversion" |