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