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