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()); |
Peiyong Lin | 04d2587 | 2019-04-18 10:26:19 -0700 | [diff] [blame] | 324 | if (error == HWC2::Error::Unsupported) { |
| 325 | // If per layer color transform is not supported, we use GPU composition. |
| 326 | setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::CLIENT); |
| 327 | } else if (error != HWC2::Error::None) { |
David Sodman | 1509411 | 2018-10-11 09:39:37 -0700 | [diff] [blame] | 328 | ALOGE("[%s] Failed to setColorTransform: %s (%d)", mName.string(), |
| 329 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 330 | } |
Lloyd Pique | 0b785d8 | 2018-12-04 17:25:27 -0800 | [diff] [blame] | 331 | layerCompositionState.dataspace = mCurrentDataSpace; |
| 332 | layerCompositionState.colorTransform = getColorTransform(); |
| 333 | layerCompositionState.hdrMetadata = metadata; |
Lloyd Pique | 074e812 | 2018-07-26 12:57:23 -0700 | [diff] [blame] | 334 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 335 | setHwcLayerBuffer(displayDevice); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 338 | bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) { |
| 339 | if (mBufferLatched) { |
| 340 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 341 | mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 342 | } |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 343 | mRefreshPending = false; |
| 344 | return hasReadyFrame(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 347 | bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId, |
| 348 | const std::shared_ptr<FenceTime>& glDoneFence, |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 349 | const std::shared_ptr<FenceTime>& presentFence, |
| 350 | const CompositorTiming& compositorTiming) { |
| 351 | // mFrameLatencyNeeded is true when a new frame was latched for the |
| 352 | // composition. |
| 353 | if (!mFrameLatencyNeeded) return false; |
| 354 | |
| 355 | // Update mFrameEventHistory. |
Dan Stoza | 436ccf3 | 2018-06-21 12:10:12 -0700 | [diff] [blame] | 356 | { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 357 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 358 | mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence, |
| 359 | compositorTiming); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 362 | // Update mFrameTracker. |
| 363 | nsecs_t desiredPresentTime = getDesiredPresentTime(); |
| 364 | mFrameTracker.setDesiredPresentTime(desiredPresentTime); |
| 365 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 366 | const int32_t layerID = getSequence(); |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 367 | mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 368 | |
| 369 | std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime(); |
| 370 | if (frameReadyFence->isValid()) { |
| 371 | mFrameTracker.setFrameReadyFence(std::move(frameReadyFence)); |
| 372 | } else { |
| 373 | // There was no fence for this frame, so assume that it was ready |
| 374 | // to be presented at the desired present time. |
| 375 | mFrameTracker.setFrameReadyTime(desiredPresentTime); |
Dominik Laskowski | 45de9bd | 2018-06-11 17:44:10 -0700 | [diff] [blame] | 376 | } |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 377 | |
| 378 | if (presentFence->isValid()) { |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 379 | mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 380 | mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence)); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 381 | } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 382 | // The HWC doesn't support present fences, so use the refresh |
| 383 | // timestamp instead. |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 384 | const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId); |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 385 | mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 386 | mFrameTracker.setActualPresentTime(actualPresentTime); |
| 387 | } |
| 388 | |
| 389 | mFrameTracker.advanceFrame(); |
| 390 | mFrameLatencyNeeded = false; |
| 391 | return true; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 394 | bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 395 | ATRACE_CALL(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 396 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 397 | bool refreshRequired = latchSidebandStream(recomputeVisibleRegions); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 398 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 399 | if (refreshRequired) { |
| 400 | return refreshRequired; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 403 | if (!hasReadyFrame()) { |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 404 | return false; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 405 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 406 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 407 | // if we've already called updateTexImage() without going through |
| 408 | // a composition step, we have to skip this layer at this point |
| 409 | // because we cannot call updateTeximage() without a corresponding |
| 410 | // compositionComplete() call. |
| 411 | // we'll trigger an update in onPreComposition(). |
| 412 | if (mRefreshPending) { |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 413 | return false; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // If the head buffer's acquire fence hasn't signaled yet, return and |
| 417 | // try again later |
| 418 | if (!fenceHasSignaled()) { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 419 | ATRACE_NAME("!fenceHasSignaled()"); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 420 | mFlinger->signalLayerUpdate(); |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 421 | return false; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // Capture the old state of the layer for comparisons later |
| 425 | const State& s(getDrawingState()); |
| 426 | const bool oldOpacity = isOpaque(s); |
| 427 | sp<GraphicBuffer> oldBuffer = mActiveBuffer; |
| 428 | |
| 429 | if (!allTransactionsSignaled()) { |
| 430 | mFlinger->signalLayerUpdate(); |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 431 | return false; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 434 | status_t err = updateTexImage(recomputeVisibleRegions, latchTime); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 435 | if (err != NO_ERROR) { |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 436 | return false; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | err = updateActiveBuffer(); |
| 440 | if (err != NO_ERROR) { |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 441 | return false; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | mBufferLatched = true; |
| 445 | |
| 446 | err = updateFrameNumber(latchTime); |
| 447 | if (err != NO_ERROR) { |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 448 | return false; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | mRefreshPending = true; |
| 452 | mFrameLatencyNeeded = true; |
| 453 | if (oldBuffer == nullptr) { |
| 454 | // the first time we receive a buffer, we need to trigger a |
| 455 | // geometry invalidation. |
| 456 | recomputeVisibleRegions = true; |
| 457 | } |
| 458 | |
| 459 | ui::Dataspace dataSpace = getDrawingDataSpace(); |
Peiyong Lin | 14724e6 | 2018-12-05 07:27:30 -0800 | [diff] [blame] | 460 | // translate legacy dataspaces to modern dataspaces |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 461 | switch (dataSpace) { |
Peiyong Lin | 14724e6 | 2018-12-05 07:27:30 -0800 | [diff] [blame] | 462 | case ui::Dataspace::SRGB: |
| 463 | dataSpace = ui::Dataspace::V0_SRGB; |
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::SRGB_LINEAR: |
| 466 | dataSpace = ui::Dataspace::V0_SRGB_LINEAR; |
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::JFIF: |
| 469 | dataSpace = ui::Dataspace::V0_JFIF; |
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_625: |
| 472 | dataSpace = ui::Dataspace::V0_BT601_625; |
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::BT601_525: |
| 475 | dataSpace = ui::Dataspace::V0_BT601_525; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 476 | break; |
Peiyong Lin | 14724e6 | 2018-12-05 07:27:30 -0800 | [diff] [blame] | 477 | case ui::Dataspace::BT709: |
| 478 | dataSpace = ui::Dataspace::V0_BT709; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 479 | break; |
| 480 | default: |
| 481 | break; |
| 482 | } |
| 483 | mCurrentDataSpace = dataSpace; |
| 484 | |
| 485 | Rect crop(getDrawingCrop()); |
| 486 | const uint32_t transform(getDrawingTransform()); |
| 487 | const uint32_t scalingMode(getDrawingScalingMode()); |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 488 | const bool transformToDisplayInverse(getTransformToDisplayInverse()); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 489 | if ((crop != mCurrentCrop) || (transform != mCurrentTransform) || |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 490 | (scalingMode != mCurrentScalingMode) || |
| 491 | (transformToDisplayInverse != mTransformToDisplayInverse)) { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 492 | mCurrentCrop = crop; |
| 493 | mCurrentTransform = transform; |
| 494 | mCurrentScalingMode = scalingMode; |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 495 | mTransformToDisplayInverse = transformToDisplayInverse; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 496 | recomputeVisibleRegions = true; |
| 497 | } |
| 498 | |
| 499 | if (oldBuffer != nullptr) { |
| 500 | uint32_t bufWidth = mActiveBuffer->getWidth(); |
| 501 | uint32_t bufHeight = mActiveBuffer->getHeight(); |
| 502 | if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) { |
| 503 | recomputeVisibleRegions = true; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | if (oldOpacity != isOpaque(s)) { |
| 508 | recomputeVisibleRegions = true; |
| 509 | } |
| 510 | |
| 511 | // Remove any sync points corresponding to the buffer which was just |
| 512 | // latched |
| 513 | { |
| 514 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 515 | auto point = mLocalSyncPoints.begin(); |
| 516 | while (point != mLocalSyncPoints.end()) { |
| 517 | if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) { |
| 518 | // This sync point must have been added since we started |
| 519 | // latching. Don't drop it yet. |
| 520 | ++point; |
| 521 | continue; |
| 522 | } |
| 523 | |
| 524 | if ((*point)->getFrameNumber() <= mCurrentFrameNumber) { |
| 525 | point = mLocalSyncPoints.erase(point); |
| 526 | } else { |
| 527 | ++point; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 532 | return true; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // transaction |
| 536 | void BufferLayer::notifyAvailableFrames() { |
Ady Abraham | cd1580c | 2019-04-29 15:40:03 -0700 | [diff] [blame^] | 537 | const auto headFrameNumber = getHeadFrameNumber(); |
| 538 | const bool headFenceSignaled = fenceHasSignaled(); |
| 539 | const bool presentTimeIsCurrent = framePresentTimeIsCurrent(); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 540 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 541 | for (auto& point : mLocalSyncPoints) { |
Ady Abraham | cd1580c | 2019-04-29 15:40:03 -0700 | [diff] [blame^] | 542 | if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled && |
| 543 | presentTimeIsCurrent) { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 544 | point->setFrameAvailable(); |
| 545 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 549 | bool BufferLayer::hasReadyFrame() const { |
Marissa Wall | 024a191 | 2018-08-13 13:55:35 -0700 | [diff] [blame] | 550 | return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh(); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | uint32_t BufferLayer::getEffectiveScalingMode() const { |
| 554 | if (mOverrideScalingMode >= 0) { |
| 555 | return mOverrideScalingMode; |
| 556 | } |
| 557 | |
| 558 | return mCurrentScalingMode; |
| 559 | } |
| 560 | |
| 561 | bool BufferLayer::isProtected() const { |
| 562 | const sp<GraphicBuffer>& buffer(mActiveBuffer); |
| 563 | return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
| 564 | } |
| 565 | |
| 566 | bool BufferLayer::latchUnsignaledBuffers() { |
| 567 | static bool propertyLoaded = false; |
| 568 | static bool latch = false; |
| 569 | static std::mutex mutex; |
| 570 | std::lock_guard<std::mutex> lock(mutex); |
| 571 | if (!propertyLoaded) { |
| 572 | char value[PROPERTY_VALUE_MAX] = {}; |
| 573 | property_get("debug.sf.latch_unsignaled", value, "0"); |
| 574 | latch = atoi(value); |
| 575 | propertyLoaded = true; |
| 576 | } |
| 577 | return latch; |
| 578 | } |
| 579 | |
| 580 | // h/w composer set-up |
| 581 | bool BufferLayer::allTransactionsSignaled() { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 582 | auto headFrameNumber = getHeadFrameNumber(); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 583 | bool matchingFramesFound = false; |
| 584 | bool allTransactionsApplied = true; |
| 585 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 586 | |
| 587 | for (auto& point : mLocalSyncPoints) { |
| 588 | if (point->getFrameNumber() > headFrameNumber) { |
| 589 | break; |
| 590 | } |
| 591 | matchingFramesFound = true; |
| 592 | |
| 593 | if (!point->frameIsAvailable()) { |
| 594 | // We haven't notified the remote layer that the frame for |
| 595 | // this point is available yet. Notify it now, and then |
| 596 | // abort this attempt to latch. |
| 597 | point->setFrameAvailable(); |
| 598 | allTransactionsApplied = false; |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied(); |
| 603 | } |
| 604 | return !matchingFramesFound || allTransactionsApplied; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | // As documented in libhardware header, formats in the range |
| 608 | // 0x100 - 0x1FF are specific to the HAL implementation, and |
| 609 | // are known to have no alpha channel |
| 610 | // TODO: move definition for device-specific range into |
| 611 | // hardware.h, instead of using hard-coded values here. |
| 612 | #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) |
| 613 | |
| 614 | bool BufferLayer::getOpacityForFormat(uint32_t format) { |
| 615 | if (HARDWARE_IS_DEVICE_FORMAT(format)) { |
| 616 | return true; |
| 617 | } |
| 618 | switch (format) { |
| 619 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 620 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 621 | case HAL_PIXEL_FORMAT_RGBA_FP16: |
| 622 | case HAL_PIXEL_FORMAT_RGBA_1010102: |
| 623 | return false; |
| 624 | } |
| 625 | // in all other case, we have no blending (also for unknown formats) |
| 626 | return true; |
| 627 | } |
| 628 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 629 | bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const { |
| 630 | // If we are not capturing based on the state of a known display device, we |
| 631 | // only return mNeedsFiltering |
| 632 | if (displayDevice == nullptr) { |
| 633 | return mNeedsFiltering; |
| 634 | } |
| 635 | |
| 636 | const auto outputLayer = findOutputLayerForDisplay(displayDevice); |
| 637 | if (outputLayer == nullptr) { |
| 638 | return mNeedsFiltering; |
| 639 | } |
| 640 | |
| 641 | const auto& compositionState = outputLayer->getState(); |
| 642 | const auto displayFrame = compositionState.displayFrame; |
| 643 | const auto sourceCrop = compositionState.sourceCrop; |
Peiyong Lin | c2020ca | 2019-01-10 11:36:12 -0800 | [diff] [blame] | 644 | return mNeedsFiltering || sourceCrop.getHeight() != displayFrame.getHeight() || |
| 645 | sourceCrop.getWidth() != displayFrame.getWidth(); |
Chia-I Wu | 692e083 | 2018-06-05 15:46:58 -0700 | [diff] [blame] | 646 | } |
| 647 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 648 | uint64_t BufferLayer::getHeadFrameNumber() const { |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 649 | if (hasFrameUpdate()) { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 650 | return getFrameNumber(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 651 | } else { |
| 652 | return mCurrentFrameNumber; |
| 653 | } |
| 654 | } |
| 655 | |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 656 | Rect BufferLayer::getBufferSize(const State& s) const { |
| 657 | // If we have a sideband stream, or we are scaling the buffer then return the layer size since |
| 658 | // we cannot determine the buffer size. |
| 659 | if ((s.sidebandStream != nullptr) || |
| 660 | (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) { |
| 661 | return Rect(getActiveWidth(s), getActiveHeight(s)); |
| 662 | } |
| 663 | |
| 664 | if (mActiveBuffer == nullptr) { |
| 665 | return Rect::INVALID_RECT; |
| 666 | } |
| 667 | |
| 668 | uint32_t bufWidth = mActiveBuffer->getWidth(); |
| 669 | uint32_t bufHeight = mActiveBuffer->getHeight(); |
| 670 | |
| 671 | // Undo any transformations on the buffer and return the result. |
| 672 | if (mCurrentTransform & ui::Transform::ROT_90) { |
| 673 | std::swap(bufWidth, bufHeight); |
| 674 | } |
| 675 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 676 | if (getTransformToDisplayInverse()) { |
Vishnu Nair | 6035634 | 2018-11-13 13:00:45 -0800 | [diff] [blame] | 677 | uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform(); |
| 678 | if (invTransform & ui::Transform::ROT_90) { |
| 679 | std::swap(bufWidth, bufHeight); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | return Rect(bufWidth, bufHeight); |
| 684 | } |
| 685 | |
Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 686 | std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const { |
| 687 | return mCompositionLayer; |
| 688 | } |
| 689 | |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 690 | FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const { |
| 691 | const State& s(getDrawingState()); |
| 692 | |
| 693 | // If we have a sideband stream, or we are scaling the buffer then return the layer size since |
| 694 | // we cannot determine the buffer size. |
| 695 | if ((s.sidebandStream != nullptr) || |
| 696 | (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) { |
| 697 | return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s)); |
| 698 | } |
| 699 | |
| 700 | if (mActiveBuffer == nullptr) { |
| 701 | return parentBounds; |
| 702 | } |
| 703 | |
| 704 | uint32_t bufWidth = mActiveBuffer->getWidth(); |
| 705 | uint32_t bufHeight = mActiveBuffer->getHeight(); |
| 706 | |
| 707 | // Undo any transformations on the buffer and return the result. |
| 708 | if (mCurrentTransform & ui::Transform::ROT_90) { |
| 709 | std::swap(bufWidth, bufHeight); |
| 710 | } |
| 711 | |
| 712 | if (getTransformToDisplayInverse()) { |
| 713 | uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform(); |
| 714 | if (invTransform & ui::Transform::ROT_90) { |
| 715 | std::swap(bufWidth, bufHeight); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | return FloatRect(0, 0, bufWidth, bufHeight); |
| 720 | } |
| 721 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 722 | } // namespace android |
| 723 | |
| 724 | #if defined(__gl_h_) |
| 725 | #error "don't include gl/gl.h in this file" |
| 726 | #endif |
| 727 | |
| 728 | #if defined(__gl2_h_) |
| 729 | #error "don't include gl2/gl2.h in this file" |
| 730 | #endif |