| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2019 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 |  | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 17 | #include <SurfaceFlingerProperties.sysprop.h> | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 18 | #include <android-base/stringprintf.h> | 
|  | 19 | #include <compositionengine/CompositionEngine.h> | 
| Lloyd Pique | f8cf14d | 2019-02-28 16:03:12 -0800 | [diff] [blame] | 20 | #include <compositionengine/CompositionRefreshArgs.h> | 
| Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 21 | #include <compositionengine/DisplayColorProfile.h> | 
| Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 22 | #include <compositionengine/LayerFE.h> | 
| Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 23 | #include <compositionengine/LayerFECompositionState.h> | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 24 | #include <compositionengine/RenderSurface.h> | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 25 | #include <compositionengine/impl/Output.h> | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 26 | #include <compositionengine/impl/OutputCompositionState.h> | 
| Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 27 | #include <compositionengine/impl/OutputLayer.h> | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 28 | #include <compositionengine/impl/OutputLayerCompositionState.h> | 
| Dan Stoza | 269dc4d | 2021-01-15 15:07:43 -0800 | [diff] [blame] | 29 | #include <compositionengine/impl/planner/Planner.h> | 
|  | 30 |  | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 31 | #include <thread> | 
|  | 32 |  | 
|  | 33 | #include "renderengine/ExternalTexture.h" | 
| Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 34 |  | 
|  | 35 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 36 | #pragma clang diagnostic push | 
|  | 37 | #pragma clang diagnostic ignored "-Wconversion" | 
|  | 38 |  | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 39 | #include <renderengine/DisplaySettings.h> | 
|  | 40 | #include <renderengine/RenderEngine.h> | 
| Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 41 |  | 
|  | 42 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 43 | #pragma clang diagnostic pop // ignored "-Wconversion" | 
|  | 44 |  | 
| Dan Stoza | 269dc4d | 2021-01-15 15:07:43 -0800 | [diff] [blame] | 45 | #include <android-base/properties.h> | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 46 | #include <ui/DebugUtils.h> | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 47 | #include <ui/HdrCapabilities.h> | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 48 | #include <utils/Trace.h> | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 49 |  | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 50 | #include "TracedOrdinal.h" | 
|  | 51 |  | 
| Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 52 | namespace android::compositionengine { | 
|  | 53 |  | 
|  | 54 | Output::~Output() = default; | 
|  | 55 |  | 
|  | 56 | namespace impl { | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 57 |  | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 58 | namespace { | 
|  | 59 |  | 
|  | 60 | template <typename T> | 
|  | 61 | class Reversed { | 
|  | 62 | public: | 
|  | 63 | explicit Reversed(const T& container) : mContainer(container) {} | 
|  | 64 | auto begin() { return mContainer.rbegin(); } | 
|  | 65 | auto end() { return mContainer.rend(); } | 
|  | 66 |  | 
|  | 67 | private: | 
|  | 68 | const T& mContainer; | 
|  | 69 | }; | 
|  | 70 |  | 
|  | 71 | // Helper for enumerating over a container in reverse order | 
|  | 72 | template <typename T> | 
|  | 73 | Reversed<T> reversed(const T& c) { | 
|  | 74 | return Reversed<T>(c); | 
|  | 75 | } | 
|  | 76 |  | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 77 | struct ScaleVector { | 
|  | 78 | float x; | 
|  | 79 | float y; | 
|  | 80 | }; | 
|  | 81 |  | 
|  | 82 | // Returns a ScaleVector (x, y) such that from.scale(x, y) = to', | 
|  | 83 | // where to' will have the same size as "to". In the case where "from" and "to" | 
|  | 84 | // start at the origin to'=to. | 
|  | 85 | ScaleVector getScale(const Rect& from, const Rect& to) { | 
|  | 86 | return {.x = static_cast<float>(to.width()) / from.width(), | 
|  | 87 | .y = static_cast<float>(to.height()) / from.height()}; | 
|  | 88 | } | 
|  | 89 |  | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 90 | } // namespace | 
|  | 91 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 92 | std::shared_ptr<Output> createOutput( | 
|  | 93 | const compositionengine::CompositionEngine& compositionEngine) { | 
|  | 94 | return createOutputTemplated<Output>(compositionEngine); | 
|  | 95 | } | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 96 |  | 
|  | 97 | Output::~Output() = default; | 
|  | 98 |  | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 99 | bool Output::isValid() const { | 
| Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 100 | return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface && | 
|  | 101 | mRenderSurface->isValid(); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
| Lloyd Pique | 6c564cf | 2019-05-17 17:31:36 -0700 | [diff] [blame] | 104 | std::optional<DisplayId> Output::getDisplayId() const { | 
|  | 105 | return {}; | 
|  | 106 | } | 
|  | 107 |  | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 108 | const std::string& Output::getName() const { | 
|  | 109 | return mName; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | void Output::setName(const std::string& name) { | 
|  | 113 | mName = name; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | void Output::setCompositionEnabled(bool enabled) { | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 117 | auto& outputState = editState(); | 
|  | 118 | if (outputState.isEnabled == enabled) { | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 119 | return; | 
|  | 120 | } | 
|  | 121 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 122 | outputState.isEnabled = enabled; | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 123 | dirtyEntireOutput(); | 
|  | 124 | } | 
|  | 125 |  | 
| Alec Mouri | 023c188 | 2021-05-08 16:36:33 -0700 | [diff] [blame] | 126 | void Output::setLayerCachingEnabled(bool enabled) { | 
|  | 127 | if (enabled == (mPlanner != nullptr)) { | 
|  | 128 | return; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | if (enabled) { | 
| Alec Mouri | df6201b | 2021-06-01 16:20:42 -0700 | [diff] [blame] | 132 | mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine()); | 
| Alec Mouri | 023c188 | 2021-05-08 16:36:33 -0700 | [diff] [blame] | 133 | if (mRenderSurface) { | 
|  | 134 | mPlanner->setDisplaySize(mRenderSurface->getSize()); | 
|  | 135 | } | 
|  | 136 | } else { | 
|  | 137 | mPlanner.reset(); | 
|  | 138 | } | 
| Alec Mouri | c773472b | 2021-05-19 14:29:05 -0700 | [diff] [blame] | 139 |  | 
|  | 140 | for (auto* outputLayer : getOutputLayersOrderedByZ()) { | 
|  | 141 | if (!outputLayer) { | 
|  | 142 | continue; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | outputLayer->editState().overrideInfo = {}; | 
|  | 146 | } | 
| Alec Mouri | 023c188 | 2021-05-08 16:36:33 -0700 | [diff] [blame] | 147 | } | 
|  | 148 |  | 
| Ady Abraham | db036a8 | 2021-07-16 14:18:34 -0700 | [diff] [blame] | 149 | void Output::setLayerCachingTexturePoolEnabled(bool enabled) { | 
|  | 150 | if (mPlanner) { | 
|  | 151 | mPlanner->setTexturePoolEnabled(enabled); | 
|  | 152 | } | 
|  | 153 | } | 
|  | 154 |  | 
| Marin Shalamanov | 68933fb | 2020-09-10 17:58:12 +0200 | [diff] [blame] | 155 | void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect, | 
|  | 156 | const Rect& orientedDisplaySpaceRect) { | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 157 | auto& outputState = editState(); | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 158 |  | 
| Marin Shalamanov | 68933fb | 2020-09-10 17:58:12 +0200 | [diff] [blame] | 159 | outputState.displaySpace.orientation = orientation; | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 160 | LOG_FATAL_IF(outputState.displaySpace.bounds == Rect::INVALID_RECT, | 
|  | 161 | "The display bounds are unknown."); | 
| Marin Shalamanov | 68933fb | 2020-09-10 17:58:12 +0200 | [diff] [blame] | 162 |  | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 163 | // Compute orientedDisplaySpace | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 164 | ui::Size orientedSize = outputState.displaySpace.bounds.getSize(); | 
| Marin Shalamanov | 68933fb | 2020-09-10 17:58:12 +0200 | [diff] [blame] | 165 | if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) { | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 166 | std::swap(orientedSize.width, orientedSize.height); | 
|  | 167 | } | 
|  | 168 | outputState.orientedDisplaySpace.bounds = Rect(orientedSize); | 
| Marin Shalamanov | 68933fb | 2020-09-10 17:58:12 +0200 | [diff] [blame] | 169 | outputState.orientedDisplaySpace.content = orientedDisplaySpaceRect; | 
|  | 170 |  | 
|  | 171 | // Compute displaySpace.content | 
|  | 172 | const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation); | 
|  | 173 | ui::Transform rotation; | 
|  | 174 | if (transformOrientationFlags != ui::Transform::ROT_INVALID) { | 
|  | 175 | const auto displaySize = outputState.displaySpace.bounds; | 
|  | 176 | rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height()); | 
|  | 177 | } | 
|  | 178 | outputState.displaySpace.content = rotation.transform(orientedDisplaySpaceRect); | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 179 |  | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 180 | // Compute framebufferSpace | 
|  | 181 | outputState.framebufferSpace.orientation = orientation; | 
|  | 182 | LOG_FATAL_IF(outputState.framebufferSpace.bounds == Rect::INVALID_RECT, | 
|  | 183 | "The framebuffer bounds are unknown."); | 
|  | 184 | const auto scale = | 
| Marin Shalamanov | 209ae61 | 2020-10-01 00:17:39 +0200 | [diff] [blame] | 185 | getScale(outputState.displaySpace.bounds, outputState.framebufferSpace.bounds); | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 186 | outputState.framebufferSpace.content = outputState.displaySpace.content.scale(scale.x, scale.y); | 
|  | 187 |  | 
|  | 188 | // Compute layerStackSpace | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 189 | outputState.layerStackSpace.content = layerStackSpaceRect; | 
|  | 190 | outputState.layerStackSpace.bounds = layerStackSpaceRect; | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 191 |  | 
| Marin Shalamanov | 68933fb | 2020-09-10 17:58:12 +0200 | [diff] [blame] | 192 | outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace); | 
|  | 193 | outputState.needsFiltering = outputState.transform.needsBilinearFiltering(); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 194 | dirtyEntireOutput(); | 
|  | 195 | } | 
|  | 196 |  | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 197 | void Output::setDisplaySize(const ui::Size& size) { | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 198 | mRenderSurface->setDisplaySize(size); | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 199 |  | 
|  | 200 | auto& state = editState(); | 
|  | 201 |  | 
|  | 202 | // Update framebuffer space | 
|  | 203 | const Rect newBounds(size); | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 204 | state.framebufferSpace.bounds = newBounds; | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 205 |  | 
|  | 206 | // Update display space | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 207 | state.displaySpace.bounds = newBounds; | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 208 | state.transform = state.layerStackSpace.getTransform(state.displaySpace); | 
|  | 209 |  | 
|  | 210 | // Update oriented display space | 
|  | 211 | const auto orientation = state.displaySpace.orientation; | 
|  | 212 | ui::Size orientedSize = size; | 
|  | 213 | if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) { | 
|  | 214 | std::swap(orientedSize.width, orientedSize.height); | 
|  | 215 | } | 
|  | 216 | const Rect newOrientedBounds(orientedSize); | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 217 | state.orientedDisplaySpace.bounds = newOrientedBounds; | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 218 |  | 
| Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 219 | if (mPlanner) { | 
|  | 220 | mPlanner->setDisplaySize(size); | 
|  | 221 | } | 
|  | 222 |  | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 223 | dirtyEntireOutput(); | 
|  | 224 | } | 
|  | 225 |  | 
| Garfield Tan | 54edd91 | 2020-10-21 16:31:41 -0700 | [diff] [blame] | 226 | ui::Transform::RotationFlags Output::getTransformHint() const { | 
|  | 227 | return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation()); | 
|  | 228 | } | 
|  | 229 |  | 
| Lloyd Pique | ef36b00 | 2019-01-23 17:52:04 -0800 | [diff] [blame] | 230 | void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) { | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 231 | auto& outputState = editState(); | 
|  | 232 | outputState.layerStackId = layerStackId; | 
|  | 233 | outputState.layerStackInternal = isInternal; | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 234 |  | 
|  | 235 | dirtyEntireOutput(); | 
|  | 236 | } | 
|  | 237 |  | 
| Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 238 | void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) { | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 239 | auto& colorTransformMatrix = editState().colorTransformMatrix; | 
|  | 240 | if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) { | 
| Lloyd Pique | 77f79a2 | 2019-04-29 15:55:40 -0700 | [diff] [blame] | 241 | return; | 
|  | 242 | } | 
|  | 243 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 244 | colorTransformMatrix = *args.colorTransformMatrix; | 
| Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 245 |  | 
|  | 246 | dirtyEntireOutput(); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
| Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 249 | void Output::setColorProfile(const ColorProfile& colorProfile) { | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 250 | ui::Dataspace targetDataspace = | 
| Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 251 | getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace, | 
|  | 252 | colorProfile.colorSpaceAgnosticDataspace); | 
| Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 253 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 254 | auto& outputState = editState(); | 
|  | 255 | if (outputState.colorMode == colorProfile.mode && | 
|  | 256 | outputState.dataspace == colorProfile.dataspace && | 
|  | 257 | outputState.renderIntent == colorProfile.renderIntent && | 
|  | 258 | outputState.targetDataspace == targetDataspace) { | 
| Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 259 | return; | 
|  | 260 | } | 
|  | 261 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 262 | outputState.colorMode = colorProfile.mode; | 
|  | 263 | outputState.dataspace = colorProfile.dataspace; | 
|  | 264 | outputState.renderIntent = colorProfile.renderIntent; | 
|  | 265 | outputState.targetDataspace = targetDataspace; | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 266 |  | 
| Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 267 | mRenderSurface->setBufferDataspace(colorProfile.dataspace); | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 268 |  | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 269 | ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)", | 
| Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 270 | decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode, | 
|  | 271 | decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent); | 
| Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 272 |  | 
|  | 273 | dirtyEntireOutput(); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 274 | } | 
|  | 275 |  | 
| John Reck | ac09e45 | 2021-04-07 16:35:37 -0400 | [diff] [blame] | 276 | void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) { | 
|  | 277 | auto& outputState = editState(); | 
|  | 278 | if (outputState.sdrWhitePointNits == sdrWhitePointNits && | 
|  | 279 | outputState.displayBrightnessNits == displayBrightnessNits) { | 
|  | 280 | // Nothing changed | 
|  | 281 | return; | 
|  | 282 | } | 
|  | 283 | outputState.sdrWhitePointNits = sdrWhitePointNits; | 
|  | 284 | outputState.displayBrightnessNits = displayBrightnessNits; | 
|  | 285 | dirtyEntireOutput(); | 
|  | 286 | } | 
|  | 287 |  | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 288 | void Output::dump(std::string& out) const { | 
|  | 289 | using android::base::StringAppendF; | 
|  | 290 |  | 
|  | 291 | StringAppendF(&out, "   Composition Output State: [\"%s\"]", mName.c_str()); | 
|  | 292 |  | 
|  | 293 | out.append("\n   "); | 
|  | 294 |  | 
|  | 295 | dumpBase(out); | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | void Output::dumpBase(std::string& out) const { | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 299 | dumpState(out); | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 300 |  | 
| Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 301 | if (mDisplayColorProfile) { | 
|  | 302 | mDisplayColorProfile->dump(out); | 
|  | 303 | } else { | 
|  | 304 | out.append("    No display color profile!\n"); | 
|  | 305 | } | 
|  | 306 |  | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 307 | if (mRenderSurface) { | 
|  | 308 | mRenderSurface->dump(out); | 
|  | 309 | } else { | 
|  | 310 | out.append("    No render surface!\n"); | 
|  | 311 | } | 
| Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 312 |  | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 313 | android::base::StringAppendF(&out, "\n   %zu Layers\n", getOutputLayerCount()); | 
|  | 314 | for (const auto* outputLayer : getOutputLayersOrderedByZ()) { | 
| Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 315 | if (!outputLayer) { | 
|  | 316 | continue; | 
|  | 317 | } | 
|  | 318 | outputLayer->dump(out); | 
|  | 319 | } | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 320 | } | 
|  | 321 |  | 
| Dan Stoza | 269dc4d | 2021-01-15 15:07:43 -0800 | [diff] [blame] | 322 | void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const { | 
|  | 323 | if (!mPlanner) { | 
|  | 324 | base::StringAppendF(&out, "Planner is disabled\n"); | 
|  | 325 | return; | 
|  | 326 | } | 
|  | 327 | base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str()); | 
|  | 328 | mPlanner->dump(args, out); | 
|  | 329 | } | 
|  | 330 |  | 
| Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 331 | compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const { | 
|  | 332 | return mDisplayColorProfile.get(); | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) { | 
|  | 336 | mDisplayColorProfile = std::move(mode); | 
|  | 337 | } | 
|  | 338 |  | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 339 | const Output::ReleasedLayers& Output::getReleasedLayersForTest() const { | 
|  | 340 | return mReleasedLayers; | 
|  | 341 | } | 
|  | 342 |  | 
| Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 343 | void Output::setDisplayColorProfileForTest( | 
|  | 344 | std::unique_ptr<compositionengine::DisplayColorProfile> mode) { | 
|  | 345 | mDisplayColorProfile = std::move(mode); | 
|  | 346 | } | 
|  | 347 |  | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 348 | compositionengine::RenderSurface* Output::getRenderSurface() const { | 
|  | 349 | return mRenderSurface.get(); | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) { | 
|  | 353 | mRenderSurface = std::move(surface); | 
| Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 354 | const auto size = mRenderSurface->getSize(); | 
|  | 355 | editState().framebufferSpace.bounds = Rect(size); | 
|  | 356 | if (mPlanner) { | 
|  | 357 | mPlanner->setDisplaySize(size); | 
|  | 358 | } | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 359 | dirtyEntireOutput(); | 
|  | 360 | } | 
|  | 361 |  | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 362 | void Output::cacheClientCompositionRequests(uint32_t cacheSize) { | 
|  | 363 | if (cacheSize == 0) { | 
|  | 364 | mClientCompositionRequestCache.reset(); | 
|  | 365 | } else { | 
|  | 366 | mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize); | 
|  | 367 | } | 
|  | 368 | }; | 
|  | 369 |  | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 370 | void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) { | 
|  | 371 | mRenderSurface = std::move(surface); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 372 | } | 
|  | 373 |  | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 374 | Region Output::getDirtyRegion(bool repaintEverything) const { | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 375 | const auto& outputState = getState(); | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 376 | Region dirty(outputState.layerStackSpace.content); | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 377 | if (!repaintEverything) { | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 378 | dirty.andSelf(outputState.dirtyRegion); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 379 | } | 
|  | 380 | return dirty; | 
|  | 381 | } | 
|  | 382 |  | 
| Lloyd Pique | c668734 | 2019-03-07 21:34:57 -0800 | [diff] [blame] | 383 | bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const { | 
| Lloyd Pique | ef36b00 | 2019-01-23 17:52:04 -0800 | [diff] [blame] | 384 | // The layerStackId's must match, and also the layer must not be internal | 
|  | 385 | // only when not on an internal output. | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 386 | const auto& outputState = getState(); | 
|  | 387 | return layerStackId && (*layerStackId == outputState.layerStackId) && | 
|  | 388 | (!internalOnly || outputState.layerStackInternal); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 389 | } | 
|  | 390 |  | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 391 | bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const { | 
|  | 392 | const auto* layerFEState = layerFE->getCompositionState(); | 
|  | 393 | return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly); | 
| Lloyd Pique | 66c20c4 | 2019-03-07 21:44:02 -0800 | [diff] [blame] | 394 | } | 
|  | 395 |  | 
| Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 396 | std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer( | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 397 | const sp<LayerFE>& layerFE) const { | 
|  | 398 | return impl::createOutputLayer(*this, layerFE); | 
| Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 399 | } | 
|  | 400 |  | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 401 | compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const { | 
|  | 402 | auto index = findCurrentOutputLayerForLayer(layerFE); | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 403 | return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr; | 
| Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 404 | } | 
|  | 405 |  | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 406 | std::optional<size_t> Output::findCurrentOutputLayerForLayer( | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 407 | const sp<compositionengine::LayerFE>& layer) const { | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 408 | for (size_t i = 0; i < getOutputLayerCount(); i++) { | 
|  | 409 | auto outputLayer = getOutputLayerOrderedByZByIndex(i); | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 410 | if (outputLayer && &outputLayer->getLayerFE() == layer.get()) { | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 411 | return i; | 
|  | 412 | } | 
|  | 413 | } | 
|  | 414 | return std::nullopt; | 
| Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 415 | } | 
|  | 416 |  | 
| Lloyd Pique | c7ef21b | 2019-01-29 18:43:00 -0800 | [diff] [blame] | 417 | void Output::setReleasedLayers(Output::ReleasedLayers&& layers) { | 
|  | 418 | mReleasedLayers = std::move(layers); | 
|  | 419 | } | 
|  | 420 |  | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 421 | void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs, | 
|  | 422 | LayerFESet& geomSnapshots) { | 
|  | 423 | ATRACE_CALL(); | 
|  | 424 | ALOGV(__FUNCTION__); | 
| Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 425 |  | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 426 | rebuildLayerStacks(refreshArgs, geomSnapshots); | 
| Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 427 | } | 
|  | 428 |  | 
| Lloyd Pique | d7b429f | 2019-03-07 21:11:02 -0800 | [diff] [blame] | 429 | void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) { | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 430 | ATRACE_CALL(); | 
|  | 431 | ALOGV(__FUNCTION__); | 
|  | 432 |  | 
| Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 433 | updateColorProfile(refreshArgs); | 
| Dan Stoza | 269dc4d | 2021-01-15 15:07:43 -0800 | [diff] [blame] | 434 | updateCompositionState(refreshArgs); | 
|  | 435 | planComposition(); | 
|  | 436 | writeCompositionState(refreshArgs); | 
| Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 437 | setColorTransform(refreshArgs); | 
| Lloyd Pique | d7b429f | 2019-03-07 21:11:02 -0800 | [diff] [blame] | 438 | beginFrame(); | 
|  | 439 | prepareFrame(); | 
|  | 440 | devOptRepaintFlash(refreshArgs); | 
|  | 441 | finishFrame(refreshArgs); | 
|  | 442 | postFramebuffer(); | 
| Alec Mouri | aa83158 | 2021-06-07 16:23:01 -0700 | [diff] [blame] | 443 | renderCachedSets(refreshArgs); | 
| Lloyd Pique | d7b429f | 2019-03-07 21:11:02 -0800 | [diff] [blame] | 444 | } | 
|  | 445 |  | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 446 | void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs, | 
|  | 447 | LayerFESet& layerFESet) { | 
|  | 448 | ATRACE_CALL(); | 
|  | 449 | ALOGV(__FUNCTION__); | 
|  | 450 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 451 | auto& outputState = editState(); | 
|  | 452 |  | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 453 | // Do nothing if this output is not enabled or there is no need to perform this update | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 454 | if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) { | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 455 | return; | 
|  | 456 | } | 
|  | 457 |  | 
|  | 458 | // Process the layers to determine visibility and coverage | 
|  | 459 | compositionengine::Output::CoverageState coverage{layerFESet}; | 
|  | 460 | collectVisibleLayers(refreshArgs, coverage); | 
|  | 461 |  | 
|  | 462 | // Compute the resulting coverage for this output, and store it for later | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 463 | const ui::Transform& tr = outputState.transform; | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 464 | Region undefinedRegion{outputState.displaySpace.bounds}; | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 465 | undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers)); | 
|  | 466 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 467 | outputState.undefinedRegion = undefinedRegion; | 
|  | 468 | outputState.dirtyRegion.orSelf(coverage.dirtyRegion); | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 469 | } | 
|  | 470 |  | 
|  | 471 | void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs, | 
|  | 472 | compositionengine::Output::CoverageState& coverage) { | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 473 | // Evaluate the layers from front to back to determine what is visible. This | 
|  | 474 | // also incrementally calculates the coverage information for each layer as | 
|  | 475 | // well as the entire output. | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 476 | for (auto layer : reversed(refreshArgs.layers)) { | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 477 | // Incrementally process the coverage for each layer | 
|  | 478 | ensureOutputLayerIfVisible(layer, coverage); | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 479 |  | 
|  | 480 | // TODO(b/121291683): Stop early if the output is completely covered and | 
|  | 481 | // no more layers could even be visible underneath the ones on top. | 
|  | 482 | } | 
|  | 483 |  | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 484 | setReleasedLayers(refreshArgs); | 
|  | 485 |  | 
|  | 486 | finalizePendingOutputLayers(); | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 487 | } | 
|  | 488 |  | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 489 | void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE, | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 490 | compositionengine::Output::CoverageState& coverage) { | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 491 | // Ensure we have a snapshot of the basic geometry layer state. Limit the | 
|  | 492 | // snapshots to once per frame for each candidate layer, as layers may | 
|  | 493 | // appear on multiple outputs. | 
|  | 494 | if (!coverage.latchedLayers.count(layerFE)) { | 
|  | 495 | coverage.latchedLayers.insert(layerFE); | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 496 | layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry); | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 497 | } | 
|  | 498 |  | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 499 | // Only consider the layers on the given layer stack | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 500 | if (!belongsInOutput(layerFE)) { | 
|  | 501 | return; | 
|  | 502 | } | 
|  | 503 |  | 
|  | 504 | // Obtain a read-only pointer to the front-end layer state | 
|  | 505 | const auto* layerFEState = layerFE->getCompositionState(); | 
|  | 506 | if (CC_UNLIKELY(!layerFEState)) { | 
|  | 507 | return; | 
|  | 508 | } | 
|  | 509 |  | 
|  | 510 | // handle hidden surfaces by setting the visible region to empty | 
|  | 511 | if (CC_UNLIKELY(!layerFEState->isVisible)) { | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 512 | return; | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 513 | } | 
|  | 514 |  | 
|  | 515 | /* | 
|  | 516 | * opaqueRegion: area of a surface that is fully opaque. | 
|  | 517 | */ | 
|  | 518 | Region opaqueRegion; | 
|  | 519 |  | 
|  | 520 | /* | 
|  | 521 | * visibleRegion: area of a surface that is visible on screen and not fully | 
|  | 522 | * transparent. This is essentially the layer's footprint minus the opaque | 
|  | 523 | * regions above it. Areas covered by a translucent surface are considered | 
|  | 524 | * visible. | 
|  | 525 | */ | 
|  | 526 | Region visibleRegion; | 
|  | 527 |  | 
|  | 528 | /* | 
|  | 529 | * coveredRegion: area of a surface that is covered by all visible regions | 
|  | 530 | * above it (which includes the translucent areas). | 
|  | 531 | */ | 
|  | 532 | Region coveredRegion; | 
|  | 533 |  | 
|  | 534 | /* | 
|  | 535 | * transparentRegion: area of a surface that is hinted to be completely | 
|  | 536 | * transparent. This is only used to tell when the layer has no visible non- | 
|  | 537 | * transparent regions and can be removed from the layer list. It does not | 
|  | 538 | * affect the visibleRegion of this layer or any layers beneath it. The hint | 
|  | 539 | * may not be correct if apps don't respect the SurfaceView restrictions | 
|  | 540 | * (which, sadly, some don't). | 
|  | 541 | */ | 
|  | 542 | Region transparentRegion; | 
|  | 543 |  | 
| Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 544 | /* | 
|  | 545 | * shadowRegion: Region cast by the layer's shadow. | 
|  | 546 | */ | 
|  | 547 | Region shadowRegion; | 
|  | 548 |  | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 549 | const ui::Transform& tr = layerFEState->geomLayerTransform; | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 550 |  | 
|  | 551 | // Get the visible region | 
|  | 552 | // TODO(b/121291683): Is it worth creating helper methods on LayerFEState | 
|  | 553 | // for computations like this? | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 554 | const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds)); | 
| Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 555 | visibleRegion.set(visibleRect); | 
|  | 556 |  | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 557 | if (layerFEState->shadowRadius > 0.0f) { | 
| Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 558 | // if the layer casts a shadow, offset the layers visible region and | 
|  | 559 | // calculate the shadow region. | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 560 | const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f); | 
| Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 561 | Rect visibleRectWithShadows(visibleRect); | 
|  | 562 | visibleRectWithShadows.inset(inset, inset, inset, inset); | 
|  | 563 | visibleRegion.set(visibleRectWithShadows); | 
|  | 564 | shadowRegion = visibleRegion.subtract(visibleRect); | 
|  | 565 | } | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 566 |  | 
|  | 567 | if (visibleRegion.isEmpty()) { | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 568 | return; | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 569 | } | 
|  | 570 |  | 
|  | 571 | // Remove the transparent area from the visible region | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 572 | if (!layerFEState->isOpaque) { | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 573 | if (tr.preserveRects()) { | 
|  | 574 | // transform the transparent region | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 575 | transparentRegion = tr.transform(layerFEState->transparentRegionHint); | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 576 | } else { | 
|  | 577 | // transformation too complex, can't do the | 
|  | 578 | // transparent region optimization. | 
|  | 579 | transparentRegion.clear(); | 
|  | 580 | } | 
|  | 581 | } | 
|  | 582 |  | 
|  | 583 | // compute the opaque region | 
| Lloyd Pique | 0a45623 | 2020-01-16 17:51:13 -0800 | [diff] [blame] | 584 | const auto layerOrientation = tr.getOrientation(); | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 585 | if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) { | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 586 | // If we one of the simple category of transforms (0/90/180/270 rotation | 
|  | 587 | // + any flip), then the opaque region is the layer's footprint. | 
|  | 588 | // Otherwise we don't try and compute the opaque region since there may | 
|  | 589 | // be errors at the edges, and we treat the entire layer as | 
|  | 590 | // translucent. | 
| Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 591 | opaqueRegion.set(visibleRect); | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 592 | } | 
|  | 593 |  | 
|  | 594 | // Clip the covered region to the visible region | 
|  | 595 | coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion); | 
|  | 596 |  | 
|  | 597 | // Update accumAboveCoveredLayers for next (lower) layer | 
|  | 598 | coverage.aboveCoveredLayers.orSelf(visibleRegion); | 
|  | 599 |  | 
|  | 600 | // subtract the opaque region covered by the layers above us | 
|  | 601 | visibleRegion.subtractSelf(coverage.aboveOpaqueLayers); | 
|  | 602 |  | 
|  | 603 | if (visibleRegion.isEmpty()) { | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 604 | return; | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 605 | } | 
|  | 606 |  | 
|  | 607 | // Get coverage information for the layer as previously displayed, | 
|  | 608 | // also taking over ownership from mOutputLayersorderedByZ. | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 609 | auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE); | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 610 | auto prevOutputLayer = | 
|  | 611 | prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr; | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 612 |  | 
|  | 613 | //  Get coverage information for the layer as previously displayed | 
|  | 614 | // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h | 
|  | 615 | const Region kEmptyRegion; | 
|  | 616 | const Region& oldVisibleRegion = | 
|  | 617 | prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion; | 
|  | 618 | const Region& oldCoveredRegion = | 
|  | 619 | prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion; | 
|  | 620 |  | 
|  | 621 | // compute this layer's dirty region | 
|  | 622 | Region dirty; | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 623 | if (layerFEState->contentDirty) { | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 624 | // we need to invalidate the whole region | 
|  | 625 | dirty = visibleRegion; | 
|  | 626 | // as well, as the old visible region | 
|  | 627 | dirty.orSelf(oldVisibleRegion); | 
|  | 628 | } else { | 
|  | 629 | /* compute the exposed region: | 
|  | 630 | *   the exposed region consists of two components: | 
|  | 631 | *   1) what's VISIBLE now and was COVERED before | 
|  | 632 | *   2) what's EXPOSED now less what was EXPOSED before | 
|  | 633 | * | 
|  | 634 | * note that (1) is conservative, we start with the whole visible region | 
|  | 635 | * but only keep what used to be covered by something -- which mean it | 
|  | 636 | * may have been exposed. | 
|  | 637 | * | 
|  | 638 | * (2) handles areas that were not covered by anything but got exposed | 
|  | 639 | * because of a resize. | 
|  | 640 | * | 
|  | 641 | */ | 
|  | 642 | const Region newExposed = visibleRegion - coveredRegion; | 
|  | 643 | const Region oldExposed = oldVisibleRegion - oldCoveredRegion; | 
|  | 644 | dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed); | 
|  | 645 | } | 
|  | 646 | dirty.subtractSelf(coverage.aboveOpaqueLayers); | 
|  | 647 |  | 
|  | 648 | // accumulate to the screen dirty region | 
|  | 649 | coverage.dirtyRegion.orSelf(dirty); | 
|  | 650 |  | 
|  | 651 | // Update accumAboveOpaqueLayers for next (lower) layer | 
|  | 652 | coverage.aboveOpaqueLayers.orSelf(opaqueRegion); | 
|  | 653 |  | 
|  | 654 | // Compute the visible non-transparent region | 
|  | 655 | Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion); | 
|  | 656 |  | 
| Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 657 | // Perform the final check to see if this layer is visible on this output | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 658 | // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below) | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 659 | const auto& outputState = getState(); | 
|  | 660 | Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion)); | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 661 | drawRegion.andSelf(outputState.displaySpace.bounds); | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 662 | if (drawRegion.isEmpty()) { | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 663 | return; | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 664 | } | 
|  | 665 |  | 
| Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 666 | Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion); | 
|  | 667 |  | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 668 | // The layer is visible. Either reuse the existing outputLayer if we have | 
|  | 669 | // one, or create a new one if we do not. | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 670 | auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE); | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 671 |  | 
|  | 672 | // Store the layer coverage information into the layer state as some of it | 
|  | 673 | // is useful later. | 
|  | 674 | auto& outputLayerState = result->editState(); | 
|  | 675 | outputLayerState.visibleRegion = visibleRegion; | 
|  | 676 | outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion; | 
|  | 677 | outputLayerState.coveredRegion = coveredRegion; | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 678 | outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform( | 
|  | 679 | visibleNonShadowRegion.intersect(outputState.layerStackSpace.content)); | 
| Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 680 | outputLayerState.shadowRegion = shadowRegion; | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 681 | } | 
|  | 682 |  | 
|  | 683 | void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) { | 
|  | 684 | // The base class does nothing with this call. | 
|  | 685 | } | 
|  | 686 |  | 
| Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 687 | void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const { | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 688 | for (auto* layer : getOutputLayersOrderedByZ()) { | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 689 | layer->getLayerFE().prepareCompositionState( | 
|  | 690 | args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent | 
|  | 691 | : LayerFE::StateSubset::Content); | 
| Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 692 | } | 
|  | 693 | } | 
|  | 694 |  | 
| Dan Stoza | 269dc4d | 2021-01-15 15:07:43 -0800 | [diff] [blame] | 695 | void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) { | 
| Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 696 | ATRACE_CALL(); | 
|  | 697 | ALOGV(__FUNCTION__); | 
|  | 698 |  | 
| Alec Mouri | f9a2a2c | 2019-11-12 12:46:02 -0800 | [diff] [blame] | 699 | if (!getState().isEnabled) { | 
|  | 700 | return; | 
|  | 701 | } | 
|  | 702 |  | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 703 | mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition(); | 
|  | 704 | bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr; | 
|  | 705 |  | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 706 | for (auto* layer : getOutputLayersOrderedByZ()) { | 
| Lloyd Pique | 7a23491 | 2019-10-03 11:54:27 -0700 | [diff] [blame] | 707 | layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame, | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 708 | refreshArgs.devOptForceClientComposition || | 
| Snild Dolkow | 9e217d6 | 2020-04-22 15:53:42 +0200 | [diff] [blame] | 709 | forceClientComposition, | 
|  | 710 | refreshArgs.internalDisplayRotationFlags); | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 711 |  | 
|  | 712 | if (mLayerRequestingBackgroundBlur == layer) { | 
|  | 713 | forceClientComposition = false; | 
|  | 714 | } | 
| Dan Stoza | 269dc4d | 2021-01-15 15:07:43 -0800 | [diff] [blame] | 715 | } | 
|  | 716 | } | 
| Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 717 |  | 
| Dan Stoza | 269dc4d | 2021-01-15 15:07:43 -0800 | [diff] [blame] | 718 | void Output::planComposition() { | 
|  | 719 | if (!mPlanner || !getState().isEnabled) { | 
|  | 720 | return; | 
|  | 721 | } | 
|  | 722 |  | 
|  | 723 | ATRACE_CALL(); | 
|  | 724 | ALOGV(__FUNCTION__); | 
|  | 725 |  | 
|  | 726 | mPlanner->plan(getOutputLayersOrderedByZ()); | 
|  | 727 | } | 
|  | 728 |  | 
|  | 729 | void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) { | 
|  | 730 | ATRACE_CALL(); | 
|  | 731 | ALOGV(__FUNCTION__); | 
|  | 732 |  | 
|  | 733 | if (!getState().isEnabled) { | 
|  | 734 | return; | 
|  | 735 | } | 
|  | 736 |  | 
| Ady Abraham | 3645e64 | 2021-04-20 18:39:00 -0700 | [diff] [blame] | 737 | editState().earliestPresentTime = refreshArgs.earliestPresentTime; | 
| Ady Abraham | ec7aa8a | 2021-06-28 12:37:09 -0700 | [diff] [blame] | 738 | editState().previousPresentFence = refreshArgs.previousPresentFence; | 
| Ady Abraham | 3645e64 | 2021-04-20 18:39:00 -0700 | [diff] [blame] | 739 |  | 
| Leon Scroggins III | 2e74a4c | 2021-04-09 13:41:14 -0400 | [diff] [blame] | 740 | compositionengine::OutputLayer* peekThroughLayer = nullptr; | 
| Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 741 | sp<GraphicBuffer> previousOverride = nullptr; | 
| Leon Scroggins III | 9aa25c2 | 2021-04-15 15:30:19 -0400 | [diff] [blame] | 742 | bool includeGeometry = refreshArgs.updatingGeometryThisFrame; | 
| Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 743 | uint32_t z = 0; | 
| Leon Scroggins III | 9aa25c2 | 2021-04-15 15:30:19 -0400 | [diff] [blame] | 744 | bool overrideZ = false; | 
| Dan Stoza | 269dc4d | 2021-01-15 15:07:43 -0800 | [diff] [blame] | 745 | for (auto* layer : getOutputLayersOrderedByZ()) { | 
| Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 746 | if (layer == peekThroughLayer) { | 
|  | 747 | // No longer needed, although it should not show up again, so | 
|  | 748 | // resetting it is not truly needed either. | 
|  | 749 | peekThroughLayer = nullptr; | 
|  | 750 |  | 
|  | 751 | // peekThroughLayer was already drawn ahead of its z order. | 
|  | 752 | continue; | 
|  | 753 | } | 
| Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 754 | bool skipLayer = false; | 
| Leon Scroggins III | d305ef2 | 2021-04-06 09:53:26 -0400 | [diff] [blame] | 755 | const auto& overrideInfo = layer->getState().overrideInfo; | 
| Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 756 | if (overrideInfo.buffer != nullptr) { | 
|  | 757 | if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) { | 
| Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 758 | ALOGV("Skipping redundant buffer"); | 
|  | 759 | skipLayer = true; | 
| Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 760 | } else { | 
|  | 761 | // First layer with the override buffer. | 
|  | 762 | if (overrideInfo.peekThroughLayer) { | 
|  | 763 | peekThroughLayer = overrideInfo.peekThroughLayer; | 
| Leon Scroggins III | d305ef2 | 2021-04-06 09:53:26 -0400 | [diff] [blame] | 764 |  | 
| Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 765 | // Draw peekThroughLayer first. | 
| Leon Scroggins III | 9aa25c2 | 2021-04-15 15:30:19 -0400 | [diff] [blame] | 766 | overrideZ = true; | 
|  | 767 | includeGeometry = true; | 
|  | 768 | constexpr bool isPeekingThrough = true; | 
|  | 769 | peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ, | 
|  | 770 | isPeekingThrough); | 
| Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 771 | } | 
|  | 772 |  | 
|  | 773 | previousOverride = overrideInfo.buffer->getBuffer(); | 
| Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 774 | } | 
| Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 775 | } | 
|  | 776 |  | 
| Leon Scroggins III | 9aa25c2 | 2021-04-15 15:30:19 -0400 | [diff] [blame] | 777 | constexpr bool isPeekingThrough = false; | 
|  | 778 | layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough); | 
| Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 779 | } | 
|  | 780 | } | 
|  | 781 |  | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 782 | compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const { | 
|  | 783 | compositionengine::OutputLayer* layerRequestingBgComposition = nullptr; | 
|  | 784 | for (auto* layer : getOutputLayersOrderedByZ()) { | 
| Galia Peycheva | 66eaf4a | 2020-11-09 13:17:57 +0100 | [diff] [blame] | 785 | auto* compState = layer->getLayerFE().getCompositionState(); | 
|  | 786 |  | 
|  | 787 | // If any layer has a sideband stream, we will disable blurs. In that case, we don't | 
|  | 788 | // want to force client composition because of the blur. | 
|  | 789 | if (compState->sidebandStream != nullptr) { | 
|  | 790 | return nullptr; | 
|  | 791 | } | 
|  | 792 | if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) { | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 793 | layerRequestingBgComposition = layer; | 
|  | 794 | } | 
|  | 795 | } | 
|  | 796 | return layerRequestingBgComposition; | 
|  | 797 | } | 
|  | 798 |  | 
| Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 799 | void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) { | 
|  | 800 | setColorProfile(pickColorProfile(refreshArgs)); | 
|  | 801 | } | 
|  | 802 |  | 
|  | 803 | // Returns a data space that fits all visible layers.  The returned data space | 
|  | 804 | // can only be one of | 
|  | 805 | //  - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced) | 
|  | 806 | //  - Dataspace::DISPLAY_P3 | 
|  | 807 | //  - Dataspace::DISPLAY_BT2020 | 
|  | 808 | // The returned HDR data space is one of | 
|  | 809 | //  - Dataspace::UNKNOWN | 
|  | 810 | //  - Dataspace::BT2020_HLG | 
|  | 811 | //  - Dataspace::BT2020_PQ | 
|  | 812 | ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace, | 
|  | 813 | bool* outIsHdrClientComposition) const { | 
|  | 814 | ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB; | 
|  | 815 | *outHdrDataSpace = ui::Dataspace::UNKNOWN; | 
|  | 816 |  | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 817 | for (const auto* layer : getOutputLayersOrderedByZ()) { | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 818 | switch (layer->getLayerFE().getCompositionState()->dataspace) { | 
| Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 819 | case ui::Dataspace::V0_SCRGB: | 
|  | 820 | case ui::Dataspace::V0_SCRGB_LINEAR: | 
|  | 821 | case ui::Dataspace::BT2020: | 
|  | 822 | case ui::Dataspace::BT2020_ITU: | 
|  | 823 | case ui::Dataspace::BT2020_LINEAR: | 
|  | 824 | case ui::Dataspace::DISPLAY_BT2020: | 
|  | 825 | bestDataSpace = ui::Dataspace::DISPLAY_BT2020; | 
|  | 826 | break; | 
|  | 827 | case ui::Dataspace::DISPLAY_P3: | 
|  | 828 | bestDataSpace = ui::Dataspace::DISPLAY_P3; | 
|  | 829 | break; | 
|  | 830 | case ui::Dataspace::BT2020_PQ: | 
|  | 831 | case ui::Dataspace::BT2020_ITU_PQ: | 
|  | 832 | bestDataSpace = ui::Dataspace::DISPLAY_P3; | 
|  | 833 | *outHdrDataSpace = ui::Dataspace::BT2020_PQ; | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 834 | *outIsHdrClientComposition = | 
|  | 835 | layer->getLayerFE().getCompositionState()->forceClientComposition; | 
| Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 836 | break; | 
|  | 837 | case ui::Dataspace::BT2020_HLG: | 
|  | 838 | case ui::Dataspace::BT2020_ITU_HLG: | 
|  | 839 | bestDataSpace = ui::Dataspace::DISPLAY_P3; | 
|  | 840 | // When there's mixed PQ content and HLG content, we set the HDR | 
|  | 841 | // data space to be BT2020_PQ and convert HLG to PQ. | 
|  | 842 | if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) { | 
|  | 843 | *outHdrDataSpace = ui::Dataspace::BT2020_HLG; | 
|  | 844 | } | 
|  | 845 | break; | 
|  | 846 | default: | 
|  | 847 | break; | 
|  | 848 | } | 
|  | 849 | } | 
|  | 850 |  | 
|  | 851 | return bestDataSpace; | 
|  | 852 | } | 
|  | 853 |  | 
|  | 854 | compositionengine::Output::ColorProfile Output::pickColorProfile( | 
|  | 855 | const compositionengine::CompositionRefreshArgs& refreshArgs) const { | 
|  | 856 | if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) { | 
|  | 857 | return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN, | 
|  | 858 | ui::RenderIntent::COLORIMETRIC, | 
|  | 859 | refreshArgs.colorSpaceAgnosticDataspace}; | 
|  | 860 | } | 
|  | 861 |  | 
|  | 862 | ui::Dataspace hdrDataSpace; | 
|  | 863 | bool isHdrClientComposition = false; | 
|  | 864 | ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition); | 
|  | 865 |  | 
|  | 866 | switch (refreshArgs.forceOutputColorMode) { | 
|  | 867 | case ui::ColorMode::SRGB: | 
|  | 868 | bestDataSpace = ui::Dataspace::V0_SRGB; | 
|  | 869 | break; | 
|  | 870 | case ui::ColorMode::DISPLAY_P3: | 
|  | 871 | bestDataSpace = ui::Dataspace::DISPLAY_P3; | 
|  | 872 | break; | 
|  | 873 | default: | 
|  | 874 | break; | 
|  | 875 | } | 
|  | 876 |  | 
|  | 877 | // respect hdrDataSpace only when there is no legacy HDR support | 
|  | 878 | const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN && | 
|  | 879 | !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition; | 
|  | 880 | if (isHdr) { | 
|  | 881 | bestDataSpace = hdrDataSpace; | 
|  | 882 | } | 
|  | 883 |  | 
|  | 884 | ui::RenderIntent intent; | 
|  | 885 | switch (refreshArgs.outputColorSetting) { | 
|  | 886 | case OutputColorSetting::kManaged: | 
|  | 887 | case OutputColorSetting::kUnmanaged: | 
|  | 888 | intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC | 
|  | 889 | : ui::RenderIntent::COLORIMETRIC; | 
|  | 890 | break; | 
|  | 891 | case OutputColorSetting::kEnhanced: | 
|  | 892 | intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE; | 
|  | 893 | break; | 
|  | 894 | default: // vendor display color setting | 
|  | 895 | intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting); | 
|  | 896 | break; | 
|  | 897 | } | 
|  | 898 |  | 
|  | 899 | ui::ColorMode outMode; | 
|  | 900 | ui::Dataspace outDataSpace; | 
|  | 901 | ui::RenderIntent outRenderIntent; | 
|  | 902 | mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode, | 
|  | 903 | &outRenderIntent); | 
|  | 904 |  | 
|  | 905 | return ColorProfile{outMode, outDataSpace, outRenderIntent, | 
|  | 906 | refreshArgs.colorSpaceAgnosticDataspace}; | 
|  | 907 | } | 
|  | 908 |  | 
| Lloyd Pique | d0a92a0 | 2019-02-19 17:47:26 -0800 | [diff] [blame] | 909 | void Output::beginFrame() { | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 910 | auto& outputState = editState(); | 
| Lloyd Pique | d0a92a0 | 2019-02-19 17:47:26 -0800 | [diff] [blame] | 911 | const bool dirty = !getDirtyRegion(false).isEmpty(); | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 912 | const bool empty = getOutputLayerCount() == 0; | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 913 | const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers; | 
| Lloyd Pique | d0a92a0 | 2019-02-19 17:47:26 -0800 | [diff] [blame] | 914 |  | 
|  | 915 | // If nothing has changed (!dirty), don't recompose. | 
|  | 916 | // If something changed, but we don't currently have any visible layers, | 
|  | 917 | //   and didn't when we last did a composition, then skip it this time. | 
|  | 918 | // The second rule does two things: | 
|  | 919 | // - When all layers are removed from a display, we'll emit one black | 
|  | 920 | //   frame, then nothing more until we get new layers. | 
|  | 921 | // - When a display is created with a private layer stack, we won't | 
|  | 922 | //   emit any black frames until a layer is added to the layer stack. | 
|  | 923 | const bool mustRecompose = dirty && !(empty && wasEmpty); | 
|  | 924 |  | 
|  | 925 | const char flagPrefix[] = {'-', '+'}; | 
|  | 926 | static_cast<void>(flagPrefix); | 
|  | 927 | ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__, | 
|  | 928 | mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty], | 
|  | 929 | flagPrefix[empty], flagPrefix[wasEmpty]); | 
|  | 930 |  | 
|  | 931 | mRenderSurface->beginFrame(mustRecompose); | 
|  | 932 |  | 
|  | 933 | if (mustRecompose) { | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 934 | outputState.lastCompositionHadVisibleLayers = !empty; | 
| Lloyd Pique | d0a92a0 | 2019-02-19 17:47:26 -0800 | [diff] [blame] | 935 | } | 
|  | 936 | } | 
|  | 937 |  | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 938 | void Output::prepareFrame() { | 
|  | 939 | ATRACE_CALL(); | 
|  | 940 | ALOGV(__FUNCTION__); | 
|  | 941 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 942 | const auto& outputState = getState(); | 
|  | 943 | if (!outputState.isEnabled) { | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 944 | return; | 
|  | 945 | } | 
|  | 946 |  | 
|  | 947 | chooseCompositionStrategy(); | 
|  | 948 |  | 
| Dan Stoza | 47437bb | 2021-01-15 16:21:07 -0800 | [diff] [blame] | 949 | if (mPlanner) { | 
|  | 950 | mPlanner->reportFinalPlan(getOutputLayersOrderedByZ()); | 
|  | 951 | } | 
|  | 952 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 953 | mRenderSurface->prepareFrame(outputState.usesClientComposition, | 
|  | 954 | outputState.usesDeviceComposition); | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 955 | } | 
|  | 956 |  | 
| Lloyd Pique | f8cf14d | 2019-02-28 16:03:12 -0800 | [diff] [blame] | 957 | void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) { | 
|  | 958 | if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) { | 
|  | 959 | return; | 
|  | 960 | } | 
|  | 961 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 962 | if (getState().isEnabled) { | 
| Lloyd Pique | f8cf14d | 2019-02-28 16:03:12 -0800 | [diff] [blame] | 963 | // transform the dirty region into this screen's coordinate space | 
|  | 964 | const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything); | 
|  | 965 | if (!dirtyRegion.isEmpty()) { | 
|  | 966 | base::unique_fd readyFence; | 
|  | 967 | // redraw the whole screen | 
| Lucas Dupin | 2dd6f39 | 2020-02-18 17:43:36 -0800 | [diff] [blame] | 968 | static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs)); | 
| Lloyd Pique | f8cf14d | 2019-02-28 16:03:12 -0800 | [diff] [blame] | 969 |  | 
|  | 970 | mRenderSurface->queueBuffer(std::move(readyFence)); | 
|  | 971 | } | 
|  | 972 | } | 
|  | 973 |  | 
|  | 974 | postFramebuffer(); | 
|  | 975 |  | 
|  | 976 | std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay); | 
|  | 977 |  | 
|  | 978 | prepareFrame(); | 
|  | 979 | } | 
|  | 980 |  | 
| Lucas Dupin | 2dd6f39 | 2020-02-18 17:43:36 -0800 | [diff] [blame] | 981 | void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) { | 
| Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 982 | ATRACE_CALL(); | 
|  | 983 | ALOGV(__FUNCTION__); | 
|  | 984 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 985 | if (!getState().isEnabled) { | 
| Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 986 | return; | 
|  | 987 | } | 
|  | 988 |  | 
|  | 989 | // Repaint the framebuffer (if needed), getting the optional fence for when | 
|  | 990 | // the composition completes. | 
| Lucas Dupin | 2dd6f39 | 2020-02-18 17:43:36 -0800 | [diff] [blame] | 991 | auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs); | 
| Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 992 | if (!optReadyFence) { | 
|  | 993 | return; | 
|  | 994 | } | 
|  | 995 |  | 
|  | 996 | // swap buffers (presentation) | 
|  | 997 | mRenderSurface->queueBuffer(std::move(*optReadyFence)); | 
|  | 998 | } | 
|  | 999 |  | 
| Lucas Dupin | 2dd6f39 | 2020-02-18 17:43:36 -0800 | [diff] [blame] | 1000 | std::optional<base::unique_fd> Output::composeSurfaces( | 
|  | 1001 | const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) { | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1002 | ATRACE_CALL(); | 
|  | 1003 | ALOGV(__FUNCTION__); | 
|  | 1004 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1005 | const auto& outputState = getState(); | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1006 | OutputCompositionState& outputCompositionState = editState(); | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1007 | const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition", | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1008 | outputState.usesClientComposition}; | 
| Lloyd Pique | e9eff97 | 2020-05-05 12:36:44 -0700 | [diff] [blame] | 1009 |  | 
|  | 1010 | auto& renderEngine = getCompositionEngine().getRenderEngine(); | 
|  | 1011 | const bool supportsProtectedContent = renderEngine.supportsProtectedContent(); | 
|  | 1012 |  | 
|  | 1013 | // If we the display is secure, protected content support is enabled, and at | 
|  | 1014 | // least one layer has protected content, we need to use a secure back | 
|  | 1015 | // buffer. | 
|  | 1016 | if (outputState.isSecure && supportsProtectedContent) { | 
|  | 1017 | auto layers = getOutputLayersOrderedByZ(); | 
|  | 1018 | bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) { | 
|  | 1019 | return layer->getLayerFE().getCompositionState()->hasProtectedContent; | 
|  | 1020 | }); | 
|  | 1021 | if (needsProtected != renderEngine.isProtected()) { | 
|  | 1022 | renderEngine.useProtectedContext(needsProtected); | 
|  | 1023 | } | 
|  | 1024 | if (needsProtected != mRenderSurface->isProtected() && | 
|  | 1025 | needsProtected == renderEngine.isProtected()) { | 
|  | 1026 | mRenderSurface->setProtected(needsProtected); | 
|  | 1027 | } | 
| Peiyong Lin | 09f910f | 2020-09-25 10:54:13 -0700 | [diff] [blame] | 1028 | } else if (!outputState.isSecure && renderEngine.isProtected()) { | 
|  | 1029 | renderEngine.useProtectedContext(false); | 
| Lloyd Pique | e9eff97 | 2020-05-05 12:36:44 -0700 | [diff] [blame] | 1030 | } | 
|  | 1031 |  | 
|  | 1032 | base::unique_fd fd; | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 1033 |  | 
|  | 1034 | std::shared_ptr<renderengine::ExternalTexture> tex; | 
| Lloyd Pique | e9eff97 | 2020-05-05 12:36:44 -0700 | [diff] [blame] | 1035 |  | 
|  | 1036 | // If we aren't doing client composition on this output, but do have a | 
|  | 1037 | // flipClientTarget request for this frame on this output, we still need to | 
|  | 1038 | // dequeue a buffer. | 
|  | 1039 | if (hasClientComposition || outputState.flipClientTarget) { | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 1040 | tex = mRenderSurface->dequeueBuffer(&fd); | 
|  | 1041 | if (tex == nullptr) { | 
| Lloyd Pique | e9eff97 | 2020-05-05 12:36:44 -0700 | [diff] [blame] | 1042 | ALOGW("Dequeuing buffer for display [%s] failed, bailing out of " | 
|  | 1043 | "client composition for this frame", | 
|  | 1044 | mName.c_str()); | 
|  | 1045 | return {}; | 
|  | 1046 | } | 
|  | 1047 | } | 
|  | 1048 |  | 
| Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 1049 | base::unique_fd readyFence; | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1050 | if (!hasClientComposition) { | 
| Lloyd Pique | a76ce46 | 2020-01-14 13:06:37 -0800 | [diff] [blame] | 1051 | setExpensiveRenderingExpected(false); | 
| Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 1052 | return readyFence; | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1053 | } | 
|  | 1054 |  | 
|  | 1055 | ALOGV("hasClientComposition"); | 
|  | 1056 |  | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1057 | renderengine::DisplaySettings clientCompositionDisplay; | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 1058 | clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.content; | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 1059 | clientCompositionDisplay.clip = outputState.layerStackSpace.content; | 
| Marin Shalamanov | 68933fb | 2020-09-10 17:58:12 +0200 | [diff] [blame] | 1060 | clientCompositionDisplay.orientation = | 
|  | 1061 | ui::Transform::toRotationFlags(outputState.displaySpace.orientation); | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1062 | clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut() | 
|  | 1063 | ? outputState.dataspace | 
|  | 1064 | : ui::Dataspace::UNKNOWN; | 
| John Reck | ac09e45 | 2021-04-07 16:35:37 -0400 | [diff] [blame] | 1065 |  | 
|  | 1066 | // If we have a valid current display brightness use that, otherwise fall back to the | 
|  | 1067 | // display's max desired | 
|  | 1068 | clientCompositionDisplay.maxLuminance = outputState.displayBrightnessNits > 0.f | 
|  | 1069 | ? outputState.displayBrightnessNits | 
|  | 1070 | : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance(); | 
|  | 1071 | clientCompositionDisplay.sdrWhitePointNits = outputState.sdrWhitePointNits; | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1072 |  | 
|  | 1073 | // Compute the global color transform matrix. | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1074 | if (!outputState.usesDeviceComposition && !getSkipColorTransform()) { | 
|  | 1075 | clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix; | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1076 | } | 
|  | 1077 |  | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1078 | // Generate the client composition requests for the layers on this output. | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1079 | std::vector<LayerFE::LayerSettings> clientCompositionLayers = | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1080 | generateClientCompositionRequests(supportsProtectedContent, | 
| Vishnu Nair | 3a7346c | 2019-12-04 08:09:09 -0800 | [diff] [blame] | 1081 | clientCompositionDisplay.outputDataspace); | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1082 | appendRegionFlashRequests(debugRegion, clientCompositionLayers); | 
|  | 1083 |  | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1084 | // Check if the client composition requests were rendered into the provided graphic buffer. If | 
|  | 1085 | // so, we can reuse the buffer and avoid client composition. | 
|  | 1086 | if (mClientCompositionRequestCache) { | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 1087 | if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(), | 
|  | 1088 | clientCompositionDisplay, | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1089 | clientCompositionLayers)) { | 
|  | 1090 | outputCompositionState.reusedClientComposition = true; | 
|  | 1091 | setExpensiveRenderingExpected(false); | 
|  | 1092 | return readyFence; | 
|  | 1093 | } | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 1094 | mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay, | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1095 | clientCompositionLayers); | 
|  | 1096 | } | 
|  | 1097 |  | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1098 | // We boost GPU frequency here because there will be color spaces conversion | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 1099 | // or complex GPU shaders and it's expensive. We boost the GPU frequency so that | 
|  | 1100 | // GPU composition can finish in time. We must reset GPU frequency afterwards, | 
|  | 1101 | // because high frequency consumes extra battery. | 
| Lucas Dupin | 2dd6f39 | 2020-02-18 17:43:36 -0800 | [diff] [blame] | 1102 | const bool expensiveBlurs = | 
|  | 1103 | refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr; | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1104 | const bool expensiveRenderingExpected = | 
| Lucas Dupin | 2dd6f39 | 2020-02-18 17:43:36 -0800 | [diff] [blame] | 1105 | clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs; | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1106 | if (expensiveRenderingExpected) { | 
|  | 1107 | setExpensiveRenderingExpected(true); | 
|  | 1108 | } | 
|  | 1109 |  | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1110 | std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers; | 
|  | 1111 | clientCompositionLayerPointers.reserve(clientCompositionLayers.size()); | 
|  | 1112 | std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(), | 
|  | 1113 | std::back_inserter(clientCompositionLayerPointers), | 
|  | 1114 | [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* { | 
|  | 1115 | return &settings; | 
|  | 1116 | }); | 
|  | 1117 |  | 
| Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 1118 | const nsecs_t renderEngineStart = systemTime(); | 
| Alec Mouri | 1684c70 | 2021-02-04 12:27:26 -0800 | [diff] [blame] | 1119 | // Only use the framebuffer cache when rendering to an internal display | 
|  | 1120 | // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because | 
|  | 1121 | // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine | 
|  | 1122 | // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is | 
|  | 1123 | // probably to encapsulate the output buffer into a structure that dispatches resource cleanup | 
|  | 1124 | // over to RenderEngine, in which case this flag can be removed from the drawLayers interface. | 
|  | 1125 | const bool useFramebufferCache = outputState.layerStackInternal; | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1126 | status_t status = | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 1127 | renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, tex, | 
| Alec Mouri | 1684c70 | 2021-02-04 12:27:26 -0800 | [diff] [blame] | 1128 | useFramebufferCache, std::move(fd), &readyFence); | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1129 |  | 
|  | 1130 | if (status != NO_ERROR && mClientCompositionRequestCache) { | 
|  | 1131 | // If rendering was not successful, remove the request from the cache. | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 1132 | mClientCompositionRequestCache->remove(tex->getBuffer()->getId()); | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1133 | } | 
|  | 1134 |  | 
| Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 1135 | auto& timeStats = getCompositionEngine().getTimeStats(); | 
|  | 1136 | if (readyFence.get() < 0) { | 
|  | 1137 | timeStats.recordRenderEngineDuration(renderEngineStart, systemTime()); | 
|  | 1138 | } else { | 
|  | 1139 | timeStats.recordRenderEngineDuration(renderEngineStart, | 
|  | 1140 | std::make_shared<FenceTime>( | 
|  | 1141 | new Fence(dup(readyFence.get())))); | 
|  | 1142 | } | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1143 |  | 
| Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 1144 | return readyFence; | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1145 | } | 
|  | 1146 |  | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1147 | std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests( | 
| Sally Qi | df3da51 | 2021-07-08 17:27:02 +0000 | [diff] [blame] | 1148 | bool supportsProtectedContent, ui::Dataspace outputDataspace) { | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1149 | std::vector<LayerFE::LayerSettings> clientCompositionLayers; | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1150 | ALOGV("Rendering client layers"); | 
|  | 1151 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1152 | const auto& outputState = getState(); | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 1153 | const Region viewportRegion(outputState.layerStackSpace.content); | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1154 | bool firstLayer = true; | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1155 |  | 
| Galia Peycheva | 66eaf4a | 2020-11-09 13:17:57 +0100 | [diff] [blame] | 1156 | bool disableBlurs = false; | 
| Huihong Luo | 91ac3b5 | 2021-04-08 11:07:41 -0700 | [diff] [blame] | 1157 | sp<GraphicBuffer> previousOverrideBuffer = nullptr; | 
| Galia Peycheva | 66eaf4a | 2020-11-09 13:17:57 +0100 | [diff] [blame] | 1158 |  | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 1159 | for (auto* layer : getOutputLayersOrderedByZ()) { | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1160 | const auto& layerState = layer->getState(); | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 1161 | const auto* layerFEState = layer->getLayerFE().getCompositionState(); | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1162 | auto& layerFE = layer->getLayerFE(); | 
|  | 1163 |  | 
| Lloyd Pique | a246866 | 2019-03-07 21:31:06 -0800 | [diff] [blame] | 1164 | const Region clip(viewportRegion.intersect(layerState.visibleRegion)); | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1165 | ALOGV("Layer: %s", layerFE.getDebugName()); | 
|  | 1166 | if (clip.isEmpty()) { | 
|  | 1167 | ALOGV("  Skipping for empty clip"); | 
|  | 1168 | firstLayer = false; | 
|  | 1169 | continue; | 
|  | 1170 | } | 
|  | 1171 |  | 
| Galia Peycheva | 66eaf4a | 2020-11-09 13:17:57 +0100 | [diff] [blame] | 1172 | disableBlurs |= layerFEState->sidebandStream != nullptr; | 
|  | 1173 |  | 
| Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 1174 | const bool clientComposition = layer->requiresClientComposition(); | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1175 |  | 
|  | 1176 | // We clear the client target for non-client composed layers if | 
|  | 1177 | // requested by the HWC. We skip this if the layer is not an opaque | 
|  | 1178 | // rectangle, as by definition the layer must blend with whatever is | 
|  | 1179 | // underneath. We also skip the first layer as the buffer target is | 
|  | 1180 | // guaranteed to start out cleared. | 
| Vishnu Nair | b87d94f | 2020-02-13 09:17:36 -0800 | [diff] [blame] | 1181 | const bool clearClientComposition = | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 1182 | layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer; | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1183 |  | 
|  | 1184 | ALOGV("  Composition type: client %d clear %d", clientComposition, clearClientComposition); | 
|  | 1185 |  | 
| Vishnu Nair | b87d94f | 2020-02-13 09:17:36 -0800 | [diff] [blame] | 1186 | // If the layer casts a shadow but the content casting the shadow is occluded, skip | 
|  | 1187 | // composing the non-shadow content and only draw the shadows. | 
|  | 1188 | const bool realContentIsVisible = clientComposition && | 
|  | 1189 | !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty(); | 
|  | 1190 |  | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1191 | if (clientComposition || clearClientComposition) { | 
| Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 1192 | std::vector<LayerFE::LayerSettings> results; | 
|  | 1193 | if (layer->getState().overrideInfo.buffer != nullptr) { | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 1194 | if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) { | 
| Huihong Luo | 91ac3b5 | 2021-04-08 11:07:41 -0700 | [diff] [blame] | 1195 | results = layer->getOverrideCompositionList(); | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 1196 | previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer(); | 
| Huihong Luo | 91ac3b5 | 2021-04-08 11:07:41 -0700 | [diff] [blame] | 1197 | ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName()); | 
|  | 1198 | } else { | 
|  | 1199 | ALOGV("Skipping redundant override buffer for [%s] in RE", | 
|  | 1200 | layer->getLayerFE().getDebugName()); | 
|  | 1201 | } | 
| Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 1202 | } else { | 
| Alec Mouri | f54453c | 2021-05-13 16:28:28 -0700 | [diff] [blame] | 1203 | LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs | 
|  | 1204 | ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled | 
|  | 1205 | : (layer->getState().overrideInfo.disableBackgroundBlur | 
|  | 1206 | ? LayerFE::ClientCompositionTargetSettings::BlurSetting:: | 
|  | 1207 | BlurRegionsOnly | 
|  | 1208 | : LayerFE::ClientCompositionTargetSettings::BlurSetting:: | 
|  | 1209 | Enabled); | 
|  | 1210 | compositionengine::LayerFE::ClientCompositionTargetSettings | 
|  | 1211 | targetSettings{.clip = clip, | 
|  | 1212 | .needsFiltering = layer->needsFiltering() || | 
|  | 1213 | outputState.needsFiltering, | 
|  | 1214 | .isSecure = outputState.isSecure, | 
|  | 1215 | .supportsProtectedContent = supportsProtectedContent, | 
| Alec Mouri | f54453c | 2021-05-13 16:28:28 -0700 | [diff] [blame] | 1216 | .viewport = outputState.layerStackSpace.content, | 
|  | 1217 | .dataspace = outputDataspace, | 
|  | 1218 | .realContentIsVisible = realContentIsVisible, | 
|  | 1219 | .clearContent = !clientComposition, | 
|  | 1220 | .blurSetting = blurSetting}; | 
| Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 1221 | results = layerFE.prepareClientCompositionList(targetSettings); | 
|  | 1222 | if (realContentIsVisible && !results.empty()) { | 
|  | 1223 | layer->editState().clientCompositionTimestamp = systemTime(); | 
|  | 1224 | } | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1225 | } | 
| Vishnu Nair | b87d94f | 2020-02-13 09:17:36 -0800 | [diff] [blame] | 1226 |  | 
|  | 1227 | clientCompositionLayers.insert(clientCompositionLayers.end(), | 
|  | 1228 | std::make_move_iterator(results.begin()), | 
|  | 1229 | std::make_move_iterator(results.end())); | 
|  | 1230 | results.clear(); | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1231 | } | 
|  | 1232 |  | 
|  | 1233 | firstLayer = false; | 
|  | 1234 | } | 
|  | 1235 |  | 
|  | 1236 | return clientCompositionLayers; | 
|  | 1237 | } | 
|  | 1238 |  | 
|  | 1239 | void Output::appendRegionFlashRequests( | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1240 | const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) { | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1241 | if (flashRegion.isEmpty()) { | 
|  | 1242 | return; | 
|  | 1243 | } | 
|  | 1244 |  | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1245 | LayerFE::LayerSettings layerSettings; | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1246 | layerSettings.source.buffer.buffer = nullptr; | 
|  | 1247 | layerSettings.source.solidColor = half3(1.0, 0.0, 1.0); | 
|  | 1248 | layerSettings.alpha = half(1.0); | 
|  | 1249 |  | 
|  | 1250 | for (const auto& rect : flashRegion) { | 
|  | 1251 | layerSettings.geometry.boundaries = rect.toFloatRect(); | 
|  | 1252 | clientCompositionLayers.push_back(layerSettings); | 
|  | 1253 | } | 
|  | 1254 | } | 
|  | 1255 |  | 
|  | 1256 | void Output::setExpensiveRenderingExpected(bool) { | 
|  | 1257 | // The base class does nothing with this call. | 
|  | 1258 | } | 
|  | 1259 |  | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1260 | void Output::postFramebuffer() { | 
|  | 1261 | ATRACE_CALL(); | 
|  | 1262 | ALOGV(__FUNCTION__); | 
|  | 1263 |  | 
|  | 1264 | if (!getState().isEnabled) { | 
|  | 1265 | return; | 
|  | 1266 | } | 
|  | 1267 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1268 | auto& outputState = editState(); | 
|  | 1269 | outputState.dirtyRegion.clear(); | 
| Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 1270 | mRenderSurface->flip(); | 
|  | 1271 |  | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1272 | auto frame = presentAndGetFrameFences(); | 
|  | 1273 |  | 
| Lloyd Pique | 7d90ba5 | 2019-08-08 11:57:53 -0700 | [diff] [blame] | 1274 | mRenderSurface->onPresentDisplayCompleted(); | 
|  | 1275 |  | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 1276 | for (auto* layer : getOutputLayersOrderedByZ()) { | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1277 | // The layer buffer from the previous frame (if any) is released | 
|  | 1278 | // by HWC only when the release fence from this frame (if any) is | 
|  | 1279 | // signaled.  Always get the release fence from HWC first. | 
|  | 1280 | sp<Fence> releaseFence = Fence::NO_FENCE; | 
|  | 1281 |  | 
|  | 1282 | if (auto hwcLayer = layer->getHwcLayer()) { | 
|  | 1283 | if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) { | 
|  | 1284 | releaseFence = f->second; | 
|  | 1285 | } | 
|  | 1286 | } | 
|  | 1287 |  | 
|  | 1288 | // If the layer was client composited in the previous frame, we | 
|  | 1289 | // need to merge with the previous client target acquire fence. | 
|  | 1290 | // Since we do not track that, always merge with the current | 
|  | 1291 | // client target acquire fence when it is available, even though | 
|  | 1292 | // this is suboptimal. | 
|  | 1293 | // TODO(b/121291683): Track previous frame client target acquire fence. | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1294 | if (outputState.usesClientComposition) { | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1295 | releaseFence = | 
|  | 1296 | Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence); | 
|  | 1297 | } | 
|  | 1298 |  | 
|  | 1299 | layer->getLayerFE().onLayerDisplayed(releaseFence); | 
|  | 1300 | } | 
|  | 1301 |  | 
|  | 1302 | // We've got a list of layers needing fences, that are disjoint with | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 1303 | // OutputLayersOrderedByZ.  The best we can do is to | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1304 | // supply them with the present fence. | 
|  | 1305 | for (auto& weakLayer : mReleasedLayers) { | 
|  | 1306 | if (auto layer = weakLayer.promote(); layer != nullptr) { | 
|  | 1307 | layer->onLayerDisplayed(frame.presentFence); | 
|  | 1308 | } | 
|  | 1309 | } | 
|  | 1310 |  | 
|  | 1311 | // Clear out the released layers now that we're done with them. | 
|  | 1312 | mReleasedLayers.clear(); | 
|  | 1313 | } | 
|  | 1314 |  | 
| Alec Mouri | aa83158 | 2021-06-07 16:23:01 -0700 | [diff] [blame] | 1315 | void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) { | 
| Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 1316 | if (mPlanner) { | 
| Alec Mouri | aa83158 | 2021-06-07 16:23:01 -0700 | [diff] [blame] | 1317 | mPlanner->renderCachedSets(getState(), refreshArgs.nextInvalidateTime); | 
| Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 1318 | } | 
|  | 1319 | } | 
|  | 1320 |  | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 1321 | void Output::dirtyEntireOutput() { | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1322 | auto& outputState = editState(); | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 1323 | outputState.dirtyRegion.set(outputState.displaySpace.bounds); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 1324 | } | 
|  | 1325 |  | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 1326 | void Output::chooseCompositionStrategy() { | 
|  | 1327 | // The base output implementation can only do client composition | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1328 | auto& outputState = editState(); | 
|  | 1329 | outputState.usesClientComposition = true; | 
|  | 1330 | outputState.usesDeviceComposition = false; | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1331 | outputState.reusedClientComposition = false; | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 1332 | } | 
|  | 1333 |  | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1334 | bool Output::getSkipColorTransform() const { | 
|  | 1335 | return true; | 
|  | 1336 | } | 
|  | 1337 |  | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1338 | compositionengine::Output::FrameFences Output::presentAndGetFrameFences() { | 
|  | 1339 | compositionengine::Output::FrameFences result; | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1340 | if (getState().usesClientComposition) { | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1341 | result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence(); | 
|  | 1342 | } | 
|  | 1343 | return result; | 
|  | 1344 | } | 
|  | 1345 |  | 
| Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 1346 | } // namespace impl | 
|  | 1347 | } // namespace android::compositionengine |