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