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