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