blob: 659fc16b66e3ab1ffb5eac475c367831f40105d8 [file] [log] [blame]
Lloyd Pique32cbe282018-10-19 13:09:22 -07001/*
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 Mouria90a5702021-04-16 16:36:21 +000017#include <SurfaceFlingerProperties.sysprop.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070018#include <android-base/stringprintf.h>
19#include <compositionengine/CompositionEngine.h>
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080020#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070021#include <compositionengine/DisplayColorProfile.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080022#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070023#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070024#include <compositionengine/RenderSurface.h>
Vishnu Naira3140382022-02-24 14:07:11 -080025#include <compositionengine/impl/HwcAsyncWorker.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070026#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070027#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080028#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070029#include <compositionengine/impl/OutputLayerCompositionState.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080030#include <compositionengine/impl/planner/Planner.h>
Sally Qi59a9f502021-10-12 18:53:23 +000031#include <ftl/future.h>
Leon Scroggins III5a655b82022-09-07 13:17:09 -040032#include <gui/TraceUtils.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080033
Alec Mouria90a5702021-04-16 16:36:21 +000034#include <thread>
35
36#include "renderengine/ExternalTexture.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080037
38// TODO(b/129481165): remove the #pragma below and fix conversion issues
39#pragma clang diagnostic push
40#pragma clang diagnostic ignored "-Wconversion"
41
Lloyd Pique688abd42019-02-15 15:42:24 -080042#include <renderengine/DisplaySettings.h>
43#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080044
45// TODO(b/129481165): remove the #pragma below and fix conversion issues
46#pragma clang diagnostic pop // ignored "-Wconversion"
47
Dan Stoza269dc4d2021-01-15 15:07:43 -080048#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070049#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080050#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080051#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070052
Lloyd Pique688abd42019-02-15 15:42:24 -080053#include "TracedOrdinal.h"
54
Leon Scroggins III9a0afda2022-01-11 16:53:09 -050055using aidl::android::hardware::graphics::composer3::Composition;
56
Lloyd Piquefeb73d72018-12-04 17:23:44 -080057namespace android::compositionengine {
58
59Output::~Output() = default;
60
61namespace impl {
Vishnu Nair9cf89262022-02-26 09:17:49 -080062using CompositionStrategyPredictionState =
63 OutputCompositionState::CompositionStrategyPredictionState;
Lloyd Piquec29e4c62019-03-07 21:48:19 -080064namespace {
65
66template <typename T>
67class Reversed {
68public:
69 explicit Reversed(const T& container) : mContainer(container) {}
70 auto begin() { return mContainer.rbegin(); }
71 auto end() { return mContainer.rend(); }
72
73private:
74 const T& mContainer;
75};
76
77// Helper for enumerating over a container in reverse order
78template <typename T>
79Reversed<T> reversed(const T& c) {
80 return Reversed<T>(c);
81}
82
Marin Shalamanovb15d2272020-09-17 21:41:52 +020083struct ScaleVector {
84 float x;
85 float y;
86};
87
88// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
89// where to' will have the same size as "to". In the case where "from" and "to"
90// start at the origin to'=to.
91ScaleVector getScale(const Rect& from, const Rect& to) {
92 return {.x = static_cast<float>(to.width()) / from.width(),
93 .y = static_cast<float>(to.height()) / from.height()};
94}
95
Lloyd Piquec29e4c62019-03-07 21:48:19 -080096} // namespace
97
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070098std::shared_ptr<Output> createOutput(
99 const compositionengine::CompositionEngine& compositionEngine) {
100 return createOutputTemplated<Output>(compositionEngine);
101}
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102
103Output::~Output() = default;
104
Lloyd Pique32cbe282018-10-19 13:09:22 -0700105bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700106 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
107 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700108}
109
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700110std::optional<DisplayId> Output::getDisplayId() const {
111 return {};
112}
113
Lloyd Pique32cbe282018-10-19 13:09:22 -0700114const std::string& Output::getName() const {
115 return mName;
116}
117
118void Output::setName(const std::string& name) {
119 mName = name;
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400120 auto displayIdOpt = getDisplayId();
121 mNamePlusId = base::StringPrintf("%s (%s)", mName.c_str(),
122 displayIdOpt ? to_string(*displayIdOpt).c_str() : "NA");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700123}
124
125void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700126 auto& outputState = editState();
127 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700128 return;
129 }
130
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700131 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700132 dirtyEntireOutput();
133}
134
Alec Mouri023c1882021-05-08 16:36:33 -0700135void Output::setLayerCachingEnabled(bool enabled) {
136 if (enabled == (mPlanner != nullptr)) {
137 return;
138 }
139
140 if (enabled) {
Alec Mouridf6201b2021-06-01 16:20:42 -0700141 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
Alec Mouri023c1882021-05-08 16:36:33 -0700142 if (mRenderSurface) {
143 mPlanner->setDisplaySize(mRenderSurface->getSize());
144 }
145 } else {
146 mPlanner.reset();
147 }
Alec Mouric773472b2021-05-19 14:29:05 -0700148
149 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
150 if (!outputLayer) {
151 continue;
152 }
153
154 outputLayer->editState().overrideInfo = {};
155 }
Alec Mouri023c1882021-05-08 16:36:33 -0700156}
157
Ady Abrahamdb036a82021-07-16 14:18:34 -0700158void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
159 if (mPlanner) {
160 mPlanner->setTexturePoolEnabled(enabled);
161 }
162}
163
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200164void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
165 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700166 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200167
Angel Aguayob084e0c2021-08-04 23:27:28 +0000168 outputState.displaySpace.setOrientation(orientation);
169 LOG_FATAL_IF(outputState.displaySpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200170 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200171
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200172 // Compute orientedDisplaySpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000173 ui::Size orientedSize = outputState.displaySpace.getBounds();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200174 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200175 std::swap(orientedSize.width, orientedSize.height);
176 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000177 outputState.orientedDisplaySpace.setBounds(orientedSize);
178 outputState.orientedDisplaySpace.setContent(orientedDisplaySpaceRect);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200179
180 // Compute displaySpace.content
181 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
182 ui::Transform rotation;
183 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000184 const auto displaySize = outputState.displaySpace.getBoundsAsRect();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200185 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
186 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000187 outputState.displaySpace.setContent(rotation.transform(orientedDisplaySpaceRect));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200188
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200189 // Compute framebufferSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000190 outputState.framebufferSpace.setOrientation(orientation);
191 LOG_FATAL_IF(outputState.framebufferSpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200192 "The framebuffer bounds are unknown.");
Angel Aguayob084e0c2021-08-04 23:27:28 +0000193 const auto scale = getScale(outputState.displaySpace.getBoundsAsRect(),
194 outputState.framebufferSpace.getBoundsAsRect());
195 outputState.framebufferSpace.setContent(
196 outputState.displaySpace.getContent().scale(scale.x, scale.y));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200197
198 // Compute layerStackSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000199 outputState.layerStackSpace.setContent(layerStackSpaceRect);
200 outputState.layerStackSpace.setBounds(
201 ui::Size(layerStackSpaceRect.getWidth(), layerStackSpaceRect.getHeight()));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200202
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200203 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
204 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700205 dirtyEntireOutput();
206}
207
Alec Mouricdf16792021-12-10 13:16:06 -0800208void Output::setNextBrightness(float brightness) {
209 editState().displayBrightness = brightness;
210}
211
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200212void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700213 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200214
215 auto& state = editState();
216
217 // Update framebuffer space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000218 const ui::Size newBounds(size);
219 state.framebufferSpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200220
221 // Update display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000222 state.displaySpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200223 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
224
225 // Update oriented display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000226 const auto orientation = state.displaySpace.getOrientation();
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200227 ui::Size orientedSize = size;
228 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
229 std::swap(orientedSize.width, orientedSize.height);
230 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000231 const ui::Size newOrientedBounds(orientedSize);
232 state.orientedDisplaySpace.setBounds(newOrientedBounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700233
Dan Stoza6166c312021-01-15 16:34:05 -0800234 if (mPlanner) {
235 mPlanner->setDisplaySize(size);
236 }
237
Lloyd Pique32cbe282018-10-19 13:09:22 -0700238 dirtyEntireOutput();
239}
240
Garfield Tan54edd912020-10-21 16:31:41 -0700241ui::Transform::RotationFlags Output::getTransformHint() const {
242 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
243}
244
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700245void Output::setLayerFilter(ui::LayerFilter filter) {
246 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700247 dirtyEntireOutput();
248}
249
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800250void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700251 auto& colorTransformMatrix = editState().colorTransformMatrix;
252 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700253 return;
254 }
255
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700256 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800257
258 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700259}
260
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800261void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700262 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800263 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
264 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800265
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700266 auto& outputState = editState();
267 if (outputState.colorMode == colorProfile.mode &&
268 outputState.dataspace == colorProfile.dataspace &&
269 outputState.renderIntent == colorProfile.renderIntent &&
270 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800271 return;
272 }
273
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700274 outputState.colorMode = colorProfile.mode;
275 outputState.dataspace = colorProfile.dataspace;
276 outputState.renderIntent = colorProfile.renderIntent;
277 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700278
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800279 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700280
Lloyd Pique32cbe282018-10-19 13:09:22 -0700281 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800282 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
283 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800284
285 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700286}
287
John Reckac09e452021-04-07 16:35:37 -0400288void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
289 auto& outputState = editState();
290 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
291 outputState.displayBrightnessNits == displayBrightnessNits) {
292 // Nothing changed
293 return;
294 }
295 outputState.sdrWhitePointNits = sdrWhitePointNits;
296 outputState.displayBrightnessNits = displayBrightnessNits;
297 dirtyEntireOutput();
298}
299
Lloyd Pique32cbe282018-10-19 13:09:22 -0700300void Output::dump(std::string& out) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700301 base::StringAppendF(&out, "Output \"%s\"", mName.c_str());
302 out.append("\n Composition Output State:\n");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700303
304 dumpBase(out);
305}
306
307void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700308 dumpState(out);
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700309 out += '\n';
Lloyd Pique31cb2942018-10-19 17:23:03 -0700310
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700311 if (mDisplayColorProfile) {
312 mDisplayColorProfile->dump(out);
313 } else {
314 out.append(" No display color profile!\n");
315 }
316
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700317 out += '\n';
318
Lloyd Pique31cb2942018-10-19 17:23:03 -0700319 if (mRenderSurface) {
320 mRenderSurface->dump(out);
321 } else {
322 out.append(" No render surface!\n");
323 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800324
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700325 base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
Lloyd Pique01c77c12019-04-17 12:48:32 -0700326 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800327 if (!outputLayer) {
328 continue;
329 }
330 outputLayer->dump(out);
331 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700332}
333
Dan Stoza269dc4d2021-01-15 15:07:43 -0800334void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
335 if (!mPlanner) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700336 out.append("Planner is disabled\n");
Dan Stoza269dc4d2021-01-15 15:07:43 -0800337 return;
338 }
339 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
340 mPlanner->dump(args, out);
341}
342
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700343compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
344 return mDisplayColorProfile.get();
345}
346
347void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
348 mDisplayColorProfile = std::move(mode);
349}
350
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800351const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
352 return mReleasedLayers;
353}
354
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700355void Output::setDisplayColorProfileForTest(
356 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
357 mDisplayColorProfile = std::move(mode);
358}
359
Lloyd Pique31cb2942018-10-19 17:23:03 -0700360compositionengine::RenderSurface* Output::getRenderSurface() const {
361 return mRenderSurface.get();
362}
363
364void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
365 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800366 const auto size = mRenderSurface->getSize();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000367 editState().framebufferSpace.setBounds(size);
Dan Stoza6166c312021-01-15 16:34:05 -0800368 if (mPlanner) {
369 mPlanner->setDisplaySize(size);
370 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700371 dirtyEntireOutput();
372}
373
Vishnu Nair9b079a22020-01-21 14:36:08 -0800374void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
375 if (cacheSize == 0) {
376 mClientCompositionRequestCache.reset();
377 } else {
378 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
379 }
380};
381
Lloyd Pique31cb2942018-10-19 17:23:03 -0700382void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
383 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700384}
385
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700386Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700387 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000388 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700389}
390
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700391bool Output::includesLayer(ui::LayerFilter filter) const {
392 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700393}
394
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700395bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800396 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700397 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800398}
399
Lloyd Piquedf336d92019-03-07 21:38:42 -0800400std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800401 const sp<LayerFE>& layerFE) const {
402 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800403}
404
Lloyd Piquede196652020-01-22 17:29:58 -0800405compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
406 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700407 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800408}
409
Lloyd Pique01c77c12019-04-17 12:48:32 -0700410std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800411 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700412 for (size_t i = 0; i < getOutputLayerCount(); i++) {
413 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800414 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700415 return i;
416 }
417 }
418 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800419}
420
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800421void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
422 mReleasedLayers = std::move(layers);
423}
424
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800425void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
426 LayerFESet& geomSnapshots) {
427 ATRACE_CALL();
428 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800429
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800430 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800431}
432
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800433void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400434 ATRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800435 ALOGV(__FUNCTION__);
436
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800437 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800438 updateCompositionState(refreshArgs);
439 planComposition();
440 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800441 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800442 beginFrame();
Vishnu Naira3140382022-02-24 14:07:11 -0800443
444 GpuCompositionResult result;
445 const bool predictCompositionStrategy = canPredictCompositionStrategy(refreshArgs);
446 if (predictCompositionStrategy) {
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000447 result = prepareFrameAsync();
Vishnu Naira3140382022-02-24 14:07:11 -0800448 } else {
449 prepareFrame();
450 }
451
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800452 devOptRepaintFlash(refreshArgs);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000453 finishFrame(std::move(result));
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800454 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700455 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800456}
457
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800458void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
459 LayerFESet& layerFESet) {
460 ATRACE_CALL();
461 ALOGV(__FUNCTION__);
462
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700463 auto& outputState = editState();
464
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800465 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700466 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800467 return;
468 }
469
470 // Process the layers to determine visibility and coverage
471 compositionengine::Output::CoverageState coverage{layerFESet};
472 collectVisibleLayers(refreshArgs, coverage);
473
474 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700475 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000476 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800477 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
478
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700479 outputState.undefinedRegion = undefinedRegion;
480 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800481}
482
483void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
484 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800485 // Evaluate the layers from front to back to determine what is visible. This
486 // also incrementally calculates the coverage information for each layer as
487 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800488 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700489 // Incrementally process the coverage for each layer
490 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800491
492 // TODO(b/121291683): Stop early if the output is completely covered and
493 // no more layers could even be visible underneath the ones on top.
494 }
495
Lloyd Pique01c77c12019-04-17 12:48:32 -0700496 setReleasedLayers(refreshArgs);
497
498 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800499}
500
Lloyd Piquede196652020-01-22 17:29:58 -0800501void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700502 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800503 // Ensure we have a snapshot of the basic geometry layer state. Limit the
504 // snapshots to once per frame for each candidate layer, as layers may
505 // appear on multiple outputs.
506 if (!coverage.latchedLayers.count(layerFE)) {
507 coverage.latchedLayers.insert(layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800508 }
509
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700510 // Only consider the layers on this output
511 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800512 return;
513 }
514
515 // Obtain a read-only pointer to the front-end layer state
516 const auto* layerFEState = layerFE->getCompositionState();
517 if (CC_UNLIKELY(!layerFEState)) {
518 return;
519 }
520
521 // handle hidden surfaces by setting the visible region to empty
522 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700523 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800524 }
525
526 /*
527 * opaqueRegion: area of a surface that is fully opaque.
528 */
529 Region opaqueRegion;
530
531 /*
532 * visibleRegion: area of a surface that is visible on screen and not fully
533 * transparent. This is essentially the layer's footprint minus the opaque
534 * regions above it. Areas covered by a translucent surface are considered
535 * visible.
536 */
537 Region visibleRegion;
538
539 /*
540 * coveredRegion: area of a surface that is covered by all visible regions
541 * above it (which includes the translucent areas).
542 */
543 Region coveredRegion;
544
545 /*
546 * transparentRegion: area of a surface that is hinted to be completely
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500547 * transparent.
548 * This is used to tell when the layer has no visible non-transparent
549 * regions and can be removed from the layer list. It does not affect the
550 * visibleRegion of this layer or any layers beneath it. The hint may not
551 * be correct if apps don't respect the SurfaceView restrictions (which,
552 * sadly, some don't).
553 *
554 * In addition, it is used on DISPLAY_DECORATION layers to specify the
555 * blockingRegion, allowing the DPU to skip it to save power. Once we have
556 * hardware that supports a blockingRegion on frames with AFBC, it may be
557 * useful to use this for other layers, too, so long as we can prevent
558 * regressions on b/7179570.
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800559 */
560 Region transparentRegion;
561
Vishnu Naira483b4a2019-12-12 15:07:52 -0800562 /*
563 * shadowRegion: Region cast by the layer's shadow.
564 */
565 Region shadowRegion;
566
Lloyd Piquede196652020-01-22 17:29:58 -0800567 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800568
569 // Get the visible region
570 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
571 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800572 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800573 visibleRegion.set(visibleRect);
574
Lloyd Piquede196652020-01-22 17:29:58 -0800575 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800576 // if the layer casts a shadow, offset the layers visible region and
577 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800578 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800579 Rect visibleRectWithShadows(visibleRect);
580 visibleRectWithShadows.inset(inset, inset, inset, inset);
581 visibleRegion.set(visibleRectWithShadows);
582 shadowRegion = visibleRegion.subtract(visibleRect);
583 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800584
585 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700586 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800587 }
588
589 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800590 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800591 if (tr.preserveRects()) {
Alec Mourie60f0b92022-06-10 19:15:20 +0000592 // Clip the transparent region to geomLayerBounds first
593 // The transparent region may be influenced by applications, for
594 // instance, by overriding ViewGroup#gatherTransparentRegion with a
595 // custom view. Once the layer stack -> display mapping is known, we
596 // must guard against very wrong inputs to prevent underflow or
597 // overflow errors. We do this here by constraining the transparent
598 // region to be within the pre-transform layer bounds, since the
599 // layer bounds are expected to play nicely with the full
600 // transform.
601 const Region clippedTransparentRegionHint =
602 layerFEState->transparentRegionHint.intersect(
603 Rect(layerFEState->geomLayerBounds));
604
605 if (clippedTransparentRegionHint.isEmpty()) {
606 if (!layerFEState->transparentRegionHint.isEmpty()) {
607 ALOGD("Layer: %s had an out of bounds transparent region",
608 layerFE->getDebugName());
609 layerFEState->transparentRegionHint.dump("transparentRegionHint");
610 }
611 transparentRegion.clear();
612 } else {
613 transparentRegion = tr.transform(clippedTransparentRegionHint);
614 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800615 } else {
616 // transformation too complex, can't do the
617 // transparent region optimization.
618 transparentRegion.clear();
619 }
620 }
621
622 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800623 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800624 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800625 // If we one of the simple category of transforms (0/90/180/270 rotation
626 // + any flip), then the opaque region is the layer's footprint.
627 // Otherwise we don't try and compute the opaque region since there may
628 // be errors at the edges, and we treat the entire layer as
629 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800630 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800631 }
632
633 // Clip the covered region to the visible region
634 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
635
636 // Update accumAboveCoveredLayers for next (lower) layer
637 coverage.aboveCoveredLayers.orSelf(visibleRegion);
638
639 // subtract the opaque region covered by the layers above us
640 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
641
642 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700643 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800644 }
645
646 // Get coverage information for the layer as previously displayed,
647 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800648 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700649 auto prevOutputLayer =
650 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800651
652 // Get coverage information for the layer as previously displayed
653 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
654 const Region kEmptyRegion;
655 const Region& oldVisibleRegion =
656 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
657 const Region& oldCoveredRegion =
658 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
659
660 // compute this layer's dirty region
661 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800662 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800663 // we need to invalidate the whole region
664 dirty = visibleRegion;
665 // as well, as the old visible region
666 dirty.orSelf(oldVisibleRegion);
667 } else {
668 /* compute the exposed region:
669 * the exposed region consists of two components:
670 * 1) what's VISIBLE now and was COVERED before
671 * 2) what's EXPOSED now less what was EXPOSED before
672 *
673 * note that (1) is conservative, we start with the whole visible region
674 * but only keep what used to be covered by something -- which mean it
675 * may have been exposed.
676 *
677 * (2) handles areas that were not covered by anything but got exposed
678 * because of a resize.
679 *
680 */
681 const Region newExposed = visibleRegion - coveredRegion;
682 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
683 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
684 }
685 dirty.subtractSelf(coverage.aboveOpaqueLayers);
686
687 // accumulate to the screen dirty region
688 coverage.dirtyRegion.orSelf(dirty);
689
690 // Update accumAboveOpaqueLayers for next (lower) layer
691 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
692
693 // Compute the visible non-transparent region
694 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
695
Vishnu Naira483b4a2019-12-12 15:07:52 -0800696 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800697 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700698 const auto& outputState = getState();
699 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000700 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800701 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700702 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800703 }
704
Vishnu Naira483b4a2019-12-12 15:07:52 -0800705 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
706
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800707 // The layer is visible. Either reuse the existing outputLayer if we have
708 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800709 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800710
711 // Store the layer coverage information into the layer state as some of it
712 // is useful later.
713 auto& outputLayerState = result->editState();
714 outputLayerState.visibleRegion = visibleRegion;
715 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
716 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200717 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000718 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800719 outputLayerState.shadowRegion = shadowRegion;
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500720 outputLayerState.outputSpaceBlockingRegionHint =
Leon Scroggins III7f7ad2c2022-03-17 17:06:20 -0400721 layerFEState->compositionType == Composition::DISPLAY_DECORATION
722 ? outputState.transform.transform(
723 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
724 : Region();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800725}
726
727void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
728 // The base class does nothing with this call.
729}
730
Dan Stoza269dc4d2021-01-15 15:07:43 -0800731void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800732 ATRACE_CALL();
733 ALOGV(__FUNCTION__);
734
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800735 if (!getState().isEnabled) {
736 return;
737 }
738
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800739 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
740 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
741
Lloyd Pique01c77c12019-04-17 12:48:32 -0700742 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700743 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800744 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200745 forceClientComposition,
746 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800747
748 if (mLayerRequestingBackgroundBlur == layer) {
749 forceClientComposition = false;
750 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800751 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000752
753 updateCompositionStateForBorder(refreshArgs);
754}
755
756void Output::updateCompositionStateForBorder(
757 const compositionengine::CompositionRefreshArgs& refreshArgs) {
758 std::unordered_map<int32_t, const Region*> layerVisibleRegionMap;
759 // Store a map of layerId to their computed visible region.
760 for (auto* layer : getOutputLayersOrderedByZ()) {
761 int layerId = (layer->getLayerFE()).getSequence();
762 layerVisibleRegionMap[layerId] = &((layer->getState()).visibleRegion);
763 }
764 OutputCompositionState& outputCompositionState = editState();
765 outputCompositionState.borderInfoList.clear();
766 bool clientComposeTopLayer = false;
767 for (const auto& borderInfo : refreshArgs.borderInfoList) {
768 renderengine::BorderRenderInfo info;
769 for (const auto& id : borderInfo.layerIds) {
770 info.combinedRegion.orSelf(*(layerVisibleRegionMap[id]));
771 }
Tianhao Yao10cea3c2022-03-30 01:37:22 +0000772
773 if (!info.combinedRegion.isEmpty()) {
774 info.width = borderInfo.width;
775 info.color = borderInfo.color;
776 outputCompositionState.borderInfoList.emplace_back(std::move(info));
777 clientComposeTopLayer = true;
778 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000779 }
780
781 // In this situation we must client compose the top layer instead of using hwc
782 // because we want to draw the border above all else.
783 // This could potentially cause a bit of a performance regression if the top
784 // layer would have been rendered using hwc originally.
785 // TODO(b/227656283): Measure system's performance before enabling the border feature
786 if (clientComposeTopLayer) {
787 auto topLayer = getOutputLayerOrderedByZByIndex(getOutputLayerCount() - 1);
788 (topLayer->editState()).forceClientComposition = true;
789 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800790}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800791
Dan Stoza269dc4d2021-01-15 15:07:43 -0800792void Output::planComposition() {
793 if (!mPlanner || !getState().isEnabled) {
794 return;
795 }
796
797 ATRACE_CALL();
798 ALOGV(__FUNCTION__);
799
800 mPlanner->plan(getOutputLayersOrderedByZ());
801}
802
803void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
804 ATRACE_CALL();
805 ALOGV(__FUNCTION__);
806
807 if (!getState().isEnabled) {
808 return;
809 }
810
Ady Abraham3645e642021-04-20 18:39:00 -0700811 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700812 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham43065bd2021-12-10 17:22:15 -0800813 editState().expectedPresentTime = refreshArgs.expectedPresentTime;
Ady Abraham3645e642021-04-20 18:39:00 -0700814
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400815 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800816 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400817 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400818 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400819 bool overrideZ = false;
Robert Carrec8ccca2022-05-04 09:36:14 -0700820 uint64_t outputLayerHash = 0;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800821 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400822 if (layer == peekThroughLayer) {
823 // No longer needed, although it should not show up again, so
824 // resetting it is not truly needed either.
825 peekThroughLayer = nullptr;
826
827 // peekThroughLayer was already drawn ahead of its z order.
828 continue;
829 }
Dan Stoza6166c312021-01-15 16:34:05 -0800830 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400831 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400832 if (overrideInfo.buffer != nullptr) {
833 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800834 ALOGV("Skipping redundant buffer");
835 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400836 } else {
837 // First layer with the override buffer.
838 if (overrideInfo.peekThroughLayer) {
839 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400840
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400841 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400842 overrideZ = true;
843 includeGeometry = true;
844 constexpr bool isPeekingThrough = true;
845 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
846 isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700847 outputLayerHash ^= android::hashCombine(
848 reinterpret_cast<uint64_t>(&peekThroughLayer->getLayerFE()),
849 z, includeGeometry, overrideZ, isPeekingThrough,
850 peekThroughLayer->requiresClientComposition());
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400851 }
852
853 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800854 }
Dan Stoza6166c312021-01-15 16:34:05 -0800855 }
856
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400857 constexpr bool isPeekingThrough = false;
858 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700859 if (!skipLayer) {
860 outputLayerHash ^= android::hashCombine(
861 reinterpret_cast<uint64_t>(&layer->getLayerFE()),
862 z, includeGeometry, overrideZ, isPeekingThrough,
863 layer->requiresClientComposition());
864 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800865 }
Robert Carrec8ccca2022-05-04 09:36:14 -0700866 editState().outputLayerHash = outputLayerHash;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800867}
868
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800869compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
870 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
871 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100872 auto* compState = layer->getLayerFE().getCompositionState();
873
874 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
875 // want to force client composition because of the blur.
876 if (compState->sidebandStream != nullptr) {
877 return nullptr;
878 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000879 if (compState->isOpaque) {
880 continue;
881 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100882 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800883 layerRequestingBgComposition = layer;
884 }
885 }
886 return layerRequestingBgComposition;
887}
888
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800889void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
890 setColorProfile(pickColorProfile(refreshArgs));
891}
892
893// Returns a data space that fits all visible layers. The returned data space
894// can only be one of
895// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
896// - Dataspace::DISPLAY_P3
897// - Dataspace::DISPLAY_BT2020
898// The returned HDR data space is one of
899// - Dataspace::UNKNOWN
900// - Dataspace::BT2020_HLG
901// - Dataspace::BT2020_PQ
902ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
903 bool* outIsHdrClientComposition) const {
904 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
905 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
906
Vishnu Naire14c6b32022-08-06 04:20:15 +0000907 // An Output's layers may be stale when it is disabled. As a consequence, the layers returned by
908 // getOutputLayersOrderedByZ may not be in a valid state and it is not safe to access their
909 // properties. Return a default dataspace value in this case.
910 if (!getState().isEnabled) {
911 return ui::Dataspace::V0_SRGB;
912 }
913
Lloyd Pique01c77c12019-04-17 12:48:32 -0700914 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800915 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800916 case ui::Dataspace::V0_SCRGB:
917 case ui::Dataspace::V0_SCRGB_LINEAR:
918 case ui::Dataspace::BT2020:
919 case ui::Dataspace::BT2020_ITU:
920 case ui::Dataspace::BT2020_LINEAR:
921 case ui::Dataspace::DISPLAY_BT2020:
922 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
923 break;
924 case ui::Dataspace::DISPLAY_P3:
925 bestDataSpace = ui::Dataspace::DISPLAY_P3;
926 break;
927 case ui::Dataspace::BT2020_PQ:
928 case ui::Dataspace::BT2020_ITU_PQ:
929 bestDataSpace = ui::Dataspace::DISPLAY_P3;
930 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800931 *outIsHdrClientComposition =
932 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800933 break;
934 case ui::Dataspace::BT2020_HLG:
935 case ui::Dataspace::BT2020_ITU_HLG:
936 bestDataSpace = ui::Dataspace::DISPLAY_P3;
937 // When there's mixed PQ content and HLG content, we set the HDR
938 // data space to be BT2020_PQ and convert HLG to PQ.
939 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
940 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
941 }
942 break;
943 default:
944 break;
945 }
946 }
947
948 return bestDataSpace;
949}
950
951compositionengine::Output::ColorProfile Output::pickColorProfile(
952 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
953 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
954 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
955 ui::RenderIntent::COLORIMETRIC,
956 refreshArgs.colorSpaceAgnosticDataspace};
957 }
958
959 ui::Dataspace hdrDataSpace;
960 bool isHdrClientComposition = false;
961 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
962
963 switch (refreshArgs.forceOutputColorMode) {
964 case ui::ColorMode::SRGB:
965 bestDataSpace = ui::Dataspace::V0_SRGB;
966 break;
967 case ui::ColorMode::DISPLAY_P3:
968 bestDataSpace = ui::Dataspace::DISPLAY_P3;
969 break;
970 default:
971 break;
972 }
973
974 // respect hdrDataSpace only when there is no legacy HDR support
975 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
976 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
977 if (isHdr) {
978 bestDataSpace = hdrDataSpace;
979 }
980
981 ui::RenderIntent intent;
982 switch (refreshArgs.outputColorSetting) {
983 case OutputColorSetting::kManaged:
984 case OutputColorSetting::kUnmanaged:
985 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
986 : ui::RenderIntent::COLORIMETRIC;
987 break;
988 case OutputColorSetting::kEnhanced:
989 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
990 break;
991 default: // vendor display color setting
992 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
993 break;
994 }
995
996 ui::ColorMode outMode;
997 ui::Dataspace outDataSpace;
998 ui::RenderIntent outRenderIntent;
999 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
1000 &outRenderIntent);
1001
1002 return ColorProfile{outMode, outDataSpace, outRenderIntent,
1003 refreshArgs.colorSpaceAgnosticDataspace};
1004}
1005
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001006void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001007 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001008 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -07001009 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001010 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001011
1012 // If nothing has changed (!dirty), don't recompose.
1013 // If something changed, but we don't currently have any visible layers,
1014 // and didn't when we last did a composition, then skip it this time.
1015 // The second rule does two things:
1016 // - When all layers are removed from a display, we'll emit one black
1017 // frame, then nothing more until we get new layers.
1018 // - When a display is created with a private layer stack, we won't
1019 // emit any black frames until a layer is added to the layer stack.
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001020 mMustRecompose = dirty && !(empty && wasEmpty);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001021
1022 const char flagPrefix[] = {'-', '+'};
1023 static_cast<void>(flagPrefix);
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001024 ALOGV("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __func__,
1025 mMustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
1026 flagPrefix[empty], flagPrefix[wasEmpty]);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001027
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001028 mRenderSurface->beginFrame(mMustRecompose);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001029
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001030 if (mMustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001031 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001032 }
1033}
1034
Lloyd Pique66d68602019-02-13 14:23:31 -08001035void Output::prepareFrame() {
1036 ATRACE_CALL();
1037 ALOGV(__FUNCTION__);
1038
Vishnu Naira3140382022-02-24 14:07:11 -08001039 auto& outputState = editState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001040 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -08001041 return;
1042 }
1043
Vishnu Naira3140382022-02-24 14:07:11 -08001044 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1045 bool success = chooseCompositionStrategy(&changes);
1046 resetCompositionStrategy();
Vishnu Nair9cf89262022-02-26 09:17:49 -08001047 outputState.strategyPrediction = CompositionStrategyPredictionState::DISABLED;
Vishnu Naira3140382022-02-24 14:07:11 -08001048 outputState.previousDeviceRequestedChanges = changes;
1049 outputState.previousDeviceRequestedSuccess = success;
1050 if (success) {
1051 applyCompositionStrategy(changes);
1052 }
1053 finishPrepareFrame();
1054}
Lloyd Pique66d68602019-02-13 14:23:31 -08001055
Vishnu Naira3140382022-02-24 14:07:11 -08001056std::future<bool> Output::chooseCompositionStrategyAsync(
1057 std::optional<android::HWComposer::DeviceRequestedChanges>* changes) {
1058 return mHwComposerAsyncWorker->send(
1059 [&, changes]() { return chooseCompositionStrategy(changes); });
1060}
1061
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001062GpuCompositionResult Output::prepareFrameAsync() {
Vishnu Naira3140382022-02-24 14:07:11 -08001063 ATRACE_CALL();
1064 ALOGV(__FUNCTION__);
1065 auto& state = editState();
1066 const auto& previousChanges = state.previousDeviceRequestedChanges;
1067 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1068 resetCompositionStrategy();
1069 auto hwcResult = chooseCompositionStrategyAsync(&changes);
1070 if (state.previousDeviceRequestedSuccess) {
1071 applyCompositionStrategy(previousChanges);
1072 }
1073 finishPrepareFrame();
1074
1075 base::unique_fd bufferFence;
1076 std::shared_ptr<renderengine::ExternalTexture> buffer;
1077 updateProtectedContentState();
1078 const bool dequeueSucceeded = dequeueRenderBuffer(&bufferFence, &buffer);
1079 GpuCompositionResult compositionResult;
1080 if (dequeueSucceeded) {
1081 std::optional<base::unique_fd> optFd =
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001082 composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001083 if (optFd) {
1084 compositionResult.fence = std::move(*optFd);
1085 }
Dan Stoza47437bb2021-01-15 16:21:07 -08001086 }
1087
Vishnu Naira3140382022-02-24 14:07:11 -08001088 auto chooseCompositionSuccess = hwcResult.get();
1089 const bool predictionSucceeded = dequeueSucceeded && changes == previousChanges;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001090 state.strategyPrediction = predictionSucceeded ? CompositionStrategyPredictionState::SUCCESS
1091 : CompositionStrategyPredictionState::FAIL;
Vishnu Naira3140382022-02-24 14:07:11 -08001092 if (!predictionSucceeded) {
1093 ATRACE_NAME("CompositionStrategyPredictionMiss");
1094 resetCompositionStrategy();
1095 if (chooseCompositionSuccess) {
1096 applyCompositionStrategy(changes);
1097 }
1098 finishPrepareFrame();
1099 // Track the dequeued buffer to reuse so we don't need to dequeue another one.
1100 compositionResult.buffer = buffer;
1101 } else {
1102 ATRACE_NAME("CompositionStrategyPredictionHit");
1103 }
1104 state.previousDeviceRequestedChanges = std::move(changes);
1105 state.previousDeviceRequestedSuccess = chooseCompositionSuccess;
1106 return compositionResult;
Lloyd Pique66d68602019-02-13 14:23:31 -08001107}
1108
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001109void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1110 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
1111 return;
1112 }
1113
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001114 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001115 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Vishnu Naira3140382022-02-24 14:07:11 -08001116 base::unique_fd bufferFence;
1117 std::shared_ptr<renderengine::ExternalTexture> buffer;
1118 updateProtectedContentState();
1119 dequeueRenderBuffer(&bufferFence, &buffer);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001120 static_cast<void>(composeSurfaces(dirtyRegion, buffer, bufferFence));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001121 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001122 }
1123 }
1124
1125 postFramebuffer();
1126
1127 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
1128
1129 prepareFrame();
1130}
1131
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001132void Output::finishFrame(GpuCompositionResult&& result) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001133 ATRACE_CALL();
1134 ALOGV(__FUNCTION__);
Vishnu Nair9cf89262022-02-26 09:17:49 -08001135 const auto& outputState = getState();
1136 if (!outputState.isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001137 return;
1138 }
1139
Vishnu Naira3140382022-02-24 14:07:11 -08001140 std::optional<base::unique_fd> optReadyFence;
1141 std::shared_ptr<renderengine::ExternalTexture> buffer;
1142 base::unique_fd bufferFence;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001143 if (outputState.strategyPrediction == CompositionStrategyPredictionState::SUCCESS) {
Vishnu Naira3140382022-02-24 14:07:11 -08001144 optReadyFence = std::move(result.fence);
1145 } else {
1146 if (result.bufferAvailable()) {
1147 buffer = std::move(result.buffer);
1148 bufferFence = std::move(result.fence);
1149 } else {
1150 updateProtectedContentState();
1151 if (!dequeueRenderBuffer(&bufferFence, &buffer)) {
1152 return;
1153 }
1154 }
1155 // Repaint the framebuffer (if needed), getting the optional fence for when
1156 // the composition completes.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001157 optReadyFence = composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001158 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001159 if (!optReadyFence) {
1160 return;
1161 }
1162
Matt Buckley50c44062022-01-17 20:48:10 +00001163 if (isPowerHintSessionEnabled()) {
1164 // get fence end time to know when gpu is complete in display
Ady Abrahamd11bade2022-08-01 16:18:03 -07001165 setHintSessionGpuFence(
1166 std::make_unique<FenceTime>(sp<Fence>::make(dup(optReadyFence->get()))));
Matt Buckley50c44062022-01-17 20:48:10 +00001167 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001168 // swap buffers (presentation)
1169 mRenderSurface->queueBuffer(std::move(*optReadyFence));
1170}
1171
Vishnu Naira3140382022-02-24 14:07:11 -08001172void Output::updateProtectedContentState() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001173 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001174 auto& renderEngine = getCompositionEngine().getRenderEngine();
1175 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1176
1177 // If we the display is secure, protected content support is enabled, and at
1178 // least one layer has protected content, we need to use a secure back
1179 // buffer.
1180 if (outputState.isSecure && supportsProtectedContent) {
1181 auto layers = getOutputLayersOrderedByZ();
1182 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1183 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1184 });
Patrick Williams8aed5d22022-10-31 22:18:10 +00001185 if (needsProtected != mRenderSurface->isProtected()) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001186 mRenderSurface->setProtected(needsProtected);
1187 }
1188 }
Vishnu Naira3140382022-02-24 14:07:11 -08001189}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001190
Vishnu Naira3140382022-02-24 14:07:11 -08001191bool Output::dequeueRenderBuffer(base::unique_fd* bufferFence,
1192 std::shared_ptr<renderengine::ExternalTexture>* tex) {
1193 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001194
1195 // If we aren't doing client composition on this output, but do have a
1196 // flipClientTarget request for this frame on this output, we still need to
1197 // dequeue a buffer.
Vishnu Naira3140382022-02-24 14:07:11 -08001198 if (outputState.usesClientComposition || outputState.flipClientTarget) {
1199 *tex = mRenderSurface->dequeueBuffer(bufferFence);
1200 if (*tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001201 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1202 "client composition for this frame",
1203 mName.c_str());
Vishnu Naira3140382022-02-24 14:07:11 -08001204 return false;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001205 }
1206 }
Vishnu Naira3140382022-02-24 14:07:11 -08001207 return true;
1208}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001209
Vishnu Naira3140382022-02-24 14:07:11 -08001210std::optional<base::unique_fd> Output::composeSurfaces(
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001211 const Region& debugRegion, std::shared_ptr<renderengine::ExternalTexture> tex,
1212 base::unique_fd& fd) {
Vishnu Naira3140382022-02-24 14:07:11 -08001213 ATRACE_CALL();
1214 ALOGV(__FUNCTION__);
1215
1216 const auto& outputState = getState();
1217 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
1218 outputState.usesClientComposition};
Lloyd Pique688abd42019-02-15 15:42:24 -08001219 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001220 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001221 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001222 }
1223
Vishnu Naira3140382022-02-24 14:07:11 -08001224 if (tex == nullptr) {
1225 ALOGW("Buffer not valid for display [%s], bailing out of "
1226 "client composition for this frame",
1227 mName.c_str());
1228 return {};
1229 }
1230
Lloyd Pique688abd42019-02-15 15:42:24 -08001231 ALOGV("hasClientComposition");
1232
Patrick Williams7584c6a2022-10-29 02:10:58 +00001233 renderengine::DisplaySettings clientCompositionDisplay =
1234 generateClientCompositionDisplaySettings();
Lloyd Pique688abd42019-02-15 15:42:24 -08001235
Lloyd Pique688abd42019-02-15 15:42:24 -08001236 // Generate the client composition requests for the layers on this output.
Vishnu Naira3140382022-02-24 14:07:11 -08001237 auto& renderEngine = getCompositionEngine().getRenderEngine();
1238 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
Robert Carrccab4242021-09-28 16:53:03 -07001239 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001240 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001241 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001242 clientCompositionDisplay.outputDataspace,
1243 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001244 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1245
Vishnu Naira3140382022-02-24 14:07:11 -08001246 OutputCompositionState& outputCompositionState = editState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001247 // Check if the client composition requests were rendered into the provided graphic buffer. If
1248 // so, we can reuse the buffer and avoid client composition.
1249 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001250 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1251 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001252 clientCompositionLayers)) {
Vishnu Naira3140382022-02-24 14:07:11 -08001253 ATRACE_NAME("ClientCompositionCacheHit");
Vishnu Nair9b079a22020-01-21 14:36:08 -08001254 outputCompositionState.reusedClientComposition = true;
1255 setExpensiveRenderingExpected(false);
Vishnu Nair3a49f0a2022-07-29 21:52:53 +00001256 // b/239944175 pass the fence associated with the buffer.
1257 return base::unique_fd(std::move(fd));
Vishnu Nair9b079a22020-01-21 14:36:08 -08001258 }
Vishnu Naira3140382022-02-24 14:07:11 -08001259 ATRACE_NAME("ClientCompositionCacheMiss");
Alec Mouria90a5702021-04-16 16:36:21 +00001260 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001261 clientCompositionLayers);
1262 }
1263
Lloyd Pique688abd42019-02-15 15:42:24 -08001264 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001265 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1266 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1267 // because high frequency consumes extra battery.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001268 const bool expensiveRenderingExpected =
Leon Scroggins IIIcf17ebc2022-03-03 14:54:00 -05001269 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1270 [outputDataspace =
1271 clientCompositionDisplay.outputDataspace](const auto& layer) {
1272 return layer.sourceDataspace != outputDataspace;
1273 });
Lloyd Pique688abd42019-02-15 15:42:24 -08001274 if (expensiveRenderingExpected) {
1275 setExpensiveRenderingExpected(true);
1276 }
1277
Sally Qi59a9f502021-10-12 18:53:23 +00001278 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1279 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001280 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001281 std::back_inserter(clientRenderEngineLayers),
1282 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1283 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001284 });
1285
Alec Mourie4034bb2019-11-19 12:45:54 -08001286 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001287 // Only use the framebuffer cache when rendering to an internal display
1288 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1289 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1290 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1291 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1292 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001293 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001294
Patrick Williams2e9748f2022-08-09 22:48:18 +00001295 auto fenceResult = renderEngine
1296 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
1297 useFramebufferCache, std::move(fd))
1298 .get();
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001299
1300 if (mClientCompositionRequestCache && fenceStatus(fenceResult) != NO_ERROR) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001301 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001302 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001303 }
1304
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001305 const auto fence = std::move(fenceResult).value_or(Fence::NO_FENCE);
1306
Patrick Williams74c0bf62022-11-02 23:59:26 +00001307 if (auto timeStats = getCompositionEngine().getTimeStats()) {
1308 if (fence->isValid()) {
1309 timeStats->recordRenderEngineDuration(renderEngineStart,
1310 std::make_shared<FenceTime>(fence));
1311 } else {
1312 timeStats->recordRenderEngineDuration(renderEngineStart, systemTime());
1313 }
Alec Mourie4034bb2019-11-19 12:45:54 -08001314 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001315
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001316 for (auto* clientComposedLayer : clientCompositionLayersFE) {
1317 clientComposedLayer->setWasClientComposed(fence);
Robert Carrccab4242021-09-28 16:53:03 -07001318 }
1319
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001320 return base::unique_fd(fence->dup());
Lloyd Pique688abd42019-02-15 15:42:24 -08001321}
1322
Patrick Williams7584c6a2022-10-29 02:10:58 +00001323renderengine::DisplaySettings Output::generateClientCompositionDisplaySettings() const {
1324 const auto& outputState = getState();
1325
1326 renderengine::DisplaySettings clientCompositionDisplay;
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001327 clientCompositionDisplay.namePlusId = mNamePlusId;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001328 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1329 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
1330 clientCompositionDisplay.orientation =
1331 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
1332 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1333 ? outputState.dataspace
1334 : ui::Dataspace::UNKNOWN;
1335
1336 // If we have a valid current display brightness use that, otherwise fall back to the
1337 // display's max desired
1338 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
1339 ? outputState.displayBrightnessNits
1340 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1341 clientCompositionDisplay.maxLuminance =
1342 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1343 clientCompositionDisplay.targetLuminanceNits =
1344 outputState.clientTargetBrightness * outputState.displayBrightnessNits;
1345 clientCompositionDisplay.dimmingStage = outputState.clientTargetDimmingStage;
1346 clientCompositionDisplay.renderIntent =
1347 static_cast<aidl::android::hardware::graphics::composer3::RenderIntent>(
1348 outputState.renderIntent);
1349
1350 // Compute the global color transform matrix.
1351 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
1352 for (auto& info : outputState.borderInfoList) {
1353 renderengine::BorderRenderInfo borderInfo;
1354 borderInfo.width = info.width;
1355 borderInfo.color = info.color;
1356 borderInfo.combinedRegion = info.combinedRegion;
1357 clientCompositionDisplay.borderInfoList.emplace_back(std::move(borderInfo));
1358 }
1359 clientCompositionDisplay.deviceHandlesColorTransform =
1360 outputState.usesDeviceComposition || getSkipColorTransform();
1361 return clientCompositionDisplay;
1362}
1363
Vishnu Nair9b079a22020-01-21 14:36:08 -08001364std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001365 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001366 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001367 ALOGV("Rendering client layers");
1368
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001369 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001370 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001371 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001372
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001373 bool disableBlurs = false;
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001374 uint64_t previousOverrideBufferId = 0;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001375
Lloyd Pique01c77c12019-04-17 12:48:32 -07001376 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001377 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001378 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001379 auto& layerFE = layer->getLayerFE();
Robert Carr05da0082022-05-25 23:29:34 -07001380 layerFE.setWasClientComposed(nullptr);
Lloyd Pique688abd42019-02-15 15:42:24 -08001381
Lloyd Piquea2468662019-03-07 21:31:06 -08001382 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001383 ALOGV("Layer: %s", layerFE.getDebugName());
1384 if (clip.isEmpty()) {
1385 ALOGV(" Skipping for empty clip");
1386 firstLayer = false;
1387 continue;
1388 }
1389
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001390 disableBlurs |= layerFEState->sidebandStream != nullptr;
1391
Vishnu Naira483b4a2019-12-12 15:07:52 -08001392 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001393
1394 // We clear the client target for non-client composed layers if
1395 // requested by the HWC. We skip this if the layer is not an opaque
1396 // rectangle, as by definition the layer must blend with whatever is
1397 // underneath. We also skip the first layer as the buffer target is
1398 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001399 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001400 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001401
1402 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1403
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001404 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1405 // composing the non-shadow content and only draw the shadows.
1406 const bool realContentIsVisible = clientComposition &&
1407 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1408
Lloyd Pique688abd42019-02-15 15:42:24 -08001409 if (clientComposition || clearClientComposition) {
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001410 if (auto overrideSettings = layer->getOverrideCompositionSettings()) {
1411 if (overrideSettings->bufferId != previousOverrideBufferId) {
1412 previousOverrideBufferId = overrideSettings->bufferId;
1413 clientCompositionLayers.push_back(std::move(*overrideSettings));
Huihong Luo91ac3b52021-04-08 11:07:41 -07001414 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1415 } else {
1416 ALOGV("Skipping redundant override buffer for [%s] in RE",
1417 layer->getLayerFE().getDebugName());
1418 }
Dan Stoza6166c312021-01-15 16:34:05 -08001419 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001420 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1421 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1422 : (layer->getState().overrideInfo.disableBackgroundBlur
1423 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1424 BlurRegionsOnly
1425 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1426 Enabled);
1427 compositionengine::LayerFE::ClientCompositionTargetSettings
1428 targetSettings{.clip = clip,
Patrick Williams7584c6a2022-10-29 02:10:58 +00001429 .needsFiltering = layerNeedsFiltering(layer) ||
Alec Mourif54453c2021-05-13 16:28:28 -07001430 outputState.needsFiltering,
1431 .isSecure = outputState.isSecure,
1432 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001433 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001434 .dataspace = outputDataspace,
1435 .realContentIsVisible = realContentIsVisible,
1436 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001437 .blurSetting = blurSetting,
Vishnu Naire14c6b32022-08-06 04:20:15 +00001438 .whitePointNits = layerState.whitePointNits,
1439 .treat170mAsSrgb = outputState.treat170mAsSrgb};
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001440 if (auto clientCompositionSettings =
1441 layerFE.prepareClientComposition(targetSettings)) {
1442 clientCompositionLayers.push_back(std::move(*clientCompositionSettings));
1443 if (realContentIsVisible) {
1444 layer->editState().clientCompositionTimestamp = systemTime();
1445 }
Dan Stoza6166c312021-01-15 16:34:05 -08001446 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001447 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001448
Tianhua Sunf91f1402022-05-09 05:45:46 +00001449 if (clientComposition) {
1450 outLayerFEs.push_back(&layerFE);
1451 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001452 }
1453
1454 firstLayer = false;
1455 }
1456
1457 return clientCompositionLayers;
1458}
1459
Patrick Williams7584c6a2022-10-29 02:10:58 +00001460bool Output::layerNeedsFiltering(const compositionengine::OutputLayer* layer) const {
1461 return layer->needsFiltering();
1462}
1463
Lloyd Pique688abd42019-02-15 15:42:24 -08001464void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001465 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001466 if (flashRegion.isEmpty()) {
1467 return;
1468 }
1469
Vishnu Nair9b079a22020-01-21 14:36:08 -08001470 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001471 layerSettings.source.buffer.buffer = nullptr;
1472 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1473 layerSettings.alpha = half(1.0);
1474
1475 for (const auto& rect : flashRegion) {
1476 layerSettings.geometry.boundaries = rect.toFloatRect();
1477 clientCompositionLayers.push_back(layerSettings);
1478 }
1479}
1480
1481void Output::setExpensiveRenderingExpected(bool) {
1482 // The base class does nothing with this call.
1483}
1484
Matt Buckley50c44062022-01-17 20:48:10 +00001485void Output::setHintSessionGpuFence(std::unique_ptr<FenceTime>&&) {
1486 // The base class does nothing with this call.
1487}
1488
1489bool Output::isPowerHintSessionEnabled() {
1490 return false;
1491}
1492
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001493void Output::postFramebuffer() {
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001494 ATRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001495 ALOGV(__FUNCTION__);
1496
1497 if (!getState().isEnabled) {
1498 return;
1499 }
1500
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001501 auto& outputState = editState();
1502 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001503
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001504 auto frame = presentAndGetFrameFences();
1505
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001506 mRenderSurface->onPresentDisplayCompleted();
1507
Lloyd Pique01c77c12019-04-17 12:48:32 -07001508 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001509 // The layer buffer from the previous frame (if any) is released
1510 // by HWC only when the release fence from this frame (if any) is
1511 // signaled. Always get the release fence from HWC first.
1512 sp<Fence> releaseFence = Fence::NO_FENCE;
1513
1514 if (auto hwcLayer = layer->getHwcLayer()) {
1515 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1516 releaseFence = f->second;
1517 }
1518 }
1519
1520 // If the layer was client composited in the previous frame, we
1521 // need to merge with the previous client target acquire fence.
1522 // Since we do not track that, always merge with the current
1523 // client target acquire fence when it is available, even though
1524 // this is suboptimal.
1525 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001526 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001527 releaseFence =
1528 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1529 }
Sally Qi59a9f502021-10-12 18:53:23 +00001530 layer->getLayerFE().onLayerDisplayed(
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001531 ftl::yield<FenceResult>(std::move(releaseFence)).share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001532 }
1533
1534 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001535 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001536 // supply them with the present fence.
1537 for (auto& weakLayer : mReleasedLayers) {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001538 if (const auto layer = weakLayer.promote()) {
1539 layer->onLayerDisplayed(ftl::yield<FenceResult>(frame.presentFence).share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001540 }
1541 }
1542
1543 // Clear out the released layers now that we're done with them.
1544 mReleasedLayers.clear();
1545}
1546
Alec Mouriaa831582021-06-07 16:23:01 -07001547void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001548 if (mPlanner) {
Brian Johnson869e28f2022-08-12 22:20:19 +00001549 mPlanner->renderCachedSets(getState(), refreshArgs.scheduledFrameTime,
1550 getState().usesDeviceComposition || getSkipColorTransform());
Dan Stoza6166c312021-01-15 16:34:05 -08001551 }
1552}
1553
Lloyd Pique32cbe282018-10-19 13:09:22 -07001554void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001555 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001556 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001557}
1558
Vishnu Naira3140382022-02-24 14:07:11 -08001559void Output::resetCompositionStrategy() {
Lloyd Pique66d68602019-02-13 14:23:31 -08001560 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001561 auto& outputState = editState();
1562 outputState.usesClientComposition = true;
1563 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001564 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001565}
1566
Lloyd Pique688abd42019-02-15 15:42:24 -08001567bool Output::getSkipColorTransform() const {
1568 return true;
1569}
1570
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001571compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1572 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001573 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001574 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1575 }
1576 return result;
1577}
1578
Vishnu Naira3140382022-02-24 14:07:11 -08001579void Output::setPredictCompositionStrategy(bool predict) {
1580 if (predict) {
1581 mHwComposerAsyncWorker = std::make_unique<HwcAsyncWorker>();
1582 } else {
1583 mHwComposerAsyncWorker.reset(nullptr);
1584 }
1585}
1586
Alec Mouridda07d92022-04-25 22:39:25 +00001587void Output::setTreat170mAsSrgb(bool enable) {
1588 editState().treat170mAsSrgb = enable;
1589}
1590
Vishnu Naira3140382022-02-24 14:07:11 -08001591bool Output::canPredictCompositionStrategy(const CompositionRefreshArgs& refreshArgs) {
Robert Carrec8ccca2022-05-04 09:36:14 -07001592 uint64_t lastOutputLayerHash = getState().lastOutputLayerHash;
1593 uint64_t outputLayerHash = getState().outputLayerHash;
1594 editState().lastOutputLayerHash = outputLayerHash;
1595
Vishnu Naira3140382022-02-24 14:07:11 -08001596 if (!getState().isEnabled || !mHwComposerAsyncWorker) {
1597 ALOGV("canPredictCompositionStrategy disabled");
1598 return false;
1599 }
1600
1601 if (!getState().previousDeviceRequestedChanges) {
1602 ALOGV("canPredictCompositionStrategy previous changes not available");
1603 return false;
1604 }
1605
1606 if (!mRenderSurface->supportsCompositionStrategyPrediction()) {
1607 ALOGV("canPredictCompositionStrategy surface does not support");
1608 return false;
1609 }
1610
1611 if (refreshArgs.devOptFlashDirtyRegionsDelay) {
1612 ALOGV("canPredictCompositionStrategy devOptFlashDirtyRegionsDelay");
1613 return false;
1614 }
1615
Robert Carrec8ccca2022-05-04 09:36:14 -07001616 if (lastOutputLayerHash != outputLayerHash) {
1617 ALOGV("canPredictCompositionStrategy output layers changed");
1618 return false;
1619 }
1620
Vishnu Naira3140382022-02-24 14:07:11 -08001621 // If no layer uses clientComposition, then don't predict composition strategy
1622 // because we have less work to do in parallel.
1623 if (!anyLayersRequireClientComposition()) {
1624 ALOGV("canPredictCompositionStrategy no layer uses clientComposition");
1625 return false;
1626 }
1627
Robert Carrec8ccca2022-05-04 09:36:14 -07001628 return true;
Vishnu Naira3140382022-02-24 14:07:11 -08001629}
1630
1631bool Output::anyLayersRequireClientComposition() const {
1632 const auto layers = getOutputLayersOrderedByZ();
1633 return std::any_of(layers.begin(), layers.end(),
1634 [](const auto& layer) { return layer->requiresClientComposition(); });
1635}
1636
1637void Output::finishPrepareFrame() {
1638 const auto& state = getState();
1639 if (mPlanner) {
1640 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
1641 }
1642 mRenderSurface->prepareFrame(state.usesClientComposition, state.usesDeviceComposition);
1643}
1644
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001645bool Output::mustRecompose() const {
1646 return mMustRecompose;
1647}
1648
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001649} // namespace impl
1650} // namespace android::compositionengine