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