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