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