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