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