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