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