blob: 16ef812d04b3a934fd507b0ef338858431f4357b [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);
Brian Lindahl439afad2022-11-14 11:16:55 -0700431 uncacheBuffers(refreshArgs.bufferIdsToUncache);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800432}
433
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800434void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400435 ATRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800436 ALOGV(__FUNCTION__);
437
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800438 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800439 updateCompositionState(refreshArgs);
440 planComposition();
441 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800442 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800443 beginFrame();
Vishnu Naira3140382022-02-24 14:07:11 -0800444
445 GpuCompositionResult result;
446 const bool predictCompositionStrategy = canPredictCompositionStrategy(refreshArgs);
447 if (predictCompositionStrategy) {
448 result = prepareFrameAsync(refreshArgs);
449 } else {
450 prepareFrame();
451 }
452
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800453 devOptRepaintFlash(refreshArgs);
Vishnu Naira3140382022-02-24 14:07:11 -0800454 finishFrame(refreshArgs, std::move(result));
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800455 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700456 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800457}
458
Brian Lindahl439afad2022-11-14 11:16:55 -0700459void Output::uncacheBuffers(std::vector<uint64_t> const& bufferIdsToUncache) {
460 if (bufferIdsToUncache.empty()) {
461 return;
462 }
463 for (auto outputLayer : getOutputLayersOrderedByZ()) {
464 outputLayer->uncacheBuffers(bufferIdsToUncache);
465 }
466}
467
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800468void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
469 LayerFESet& layerFESet) {
470 ATRACE_CALL();
471 ALOGV(__FUNCTION__);
472
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700473 auto& outputState = editState();
474
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800475 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700476 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800477 return;
478 }
479
480 // Process the layers to determine visibility and coverage
481 compositionengine::Output::CoverageState coverage{layerFESet};
482 collectVisibleLayers(refreshArgs, coverage);
483
484 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700485 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000486 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800487 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
488
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700489 outputState.undefinedRegion = undefinedRegion;
490 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800491}
492
493void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
494 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800495 // Evaluate the layers from front to back to determine what is visible. This
496 // also incrementally calculates the coverage information for each layer as
497 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800498 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700499 // Incrementally process the coverage for each layer
500 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800501
502 // TODO(b/121291683): Stop early if the output is completely covered and
503 // no more layers could even be visible underneath the ones on top.
504 }
505
Lloyd Pique01c77c12019-04-17 12:48:32 -0700506 setReleasedLayers(refreshArgs);
507
508 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800509}
510
Lloyd Piquede196652020-01-22 17:29:58 -0800511void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700512 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800513 // Ensure we have a snapshot of the basic geometry layer state. Limit the
514 // snapshots to once per frame for each candidate layer, as layers may
515 // appear on multiple outputs.
516 if (!coverage.latchedLayers.count(layerFE)) {
517 coverage.latchedLayers.insert(layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800518 }
519
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700520 // Only consider the layers on this output
521 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800522 return;
523 }
524
525 // Obtain a read-only pointer to the front-end layer state
526 const auto* layerFEState = layerFE->getCompositionState();
527 if (CC_UNLIKELY(!layerFEState)) {
528 return;
529 }
530
531 // handle hidden surfaces by setting the visible region to empty
532 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700533 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800534 }
535
536 /*
537 * opaqueRegion: area of a surface that is fully opaque.
538 */
539 Region opaqueRegion;
540
541 /*
542 * visibleRegion: area of a surface that is visible on screen and not fully
543 * transparent. This is essentially the layer's footprint minus the opaque
544 * regions above it. Areas covered by a translucent surface are considered
545 * visible.
546 */
547 Region visibleRegion;
548
549 /*
550 * coveredRegion: area of a surface that is covered by all visible regions
551 * above it (which includes the translucent areas).
552 */
553 Region coveredRegion;
554
555 /*
556 * transparentRegion: area of a surface that is hinted to be completely
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500557 * transparent.
558 * This is used to tell when the layer has no visible non-transparent
559 * regions and can be removed from the layer list. It does not affect the
560 * visibleRegion of this layer or any layers beneath it. The hint may not
561 * be correct if apps don't respect the SurfaceView restrictions (which,
562 * sadly, some don't).
563 *
564 * In addition, it is used on DISPLAY_DECORATION layers to specify the
565 * blockingRegion, allowing the DPU to skip it to save power. Once we have
566 * hardware that supports a blockingRegion on frames with AFBC, it may be
567 * useful to use this for other layers, too, so long as we can prevent
568 * regressions on b/7179570.
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800569 */
570 Region transparentRegion;
571
Vishnu Naira483b4a2019-12-12 15:07:52 -0800572 /*
573 * shadowRegion: Region cast by the layer's shadow.
574 */
575 Region shadowRegion;
576
Lloyd Piquede196652020-01-22 17:29:58 -0800577 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800578
579 // Get the visible region
580 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
581 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800582 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800583 visibleRegion.set(visibleRect);
584
Lloyd Piquede196652020-01-22 17:29:58 -0800585 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800586 // if the layer casts a shadow, offset the layers visible region and
587 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800588 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800589 Rect visibleRectWithShadows(visibleRect);
590 visibleRectWithShadows.inset(inset, inset, inset, inset);
591 visibleRegion.set(visibleRectWithShadows);
592 shadowRegion = visibleRegion.subtract(visibleRect);
593 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800594
595 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700596 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800597 }
598
599 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800600 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800601 if (tr.preserveRects()) {
Alec Mourie60f0b92022-06-10 19:15:20 +0000602 // Clip the transparent region to geomLayerBounds first
603 // The transparent region may be influenced by applications, for
604 // instance, by overriding ViewGroup#gatherTransparentRegion with a
605 // custom view. Once the layer stack -> display mapping is known, we
606 // must guard against very wrong inputs to prevent underflow or
607 // overflow errors. We do this here by constraining the transparent
608 // region to be within the pre-transform layer bounds, since the
609 // layer bounds are expected to play nicely with the full
610 // transform.
611 const Region clippedTransparentRegionHint =
612 layerFEState->transparentRegionHint.intersect(
613 Rect(layerFEState->geomLayerBounds));
614
615 if (clippedTransparentRegionHint.isEmpty()) {
616 if (!layerFEState->transparentRegionHint.isEmpty()) {
617 ALOGD("Layer: %s had an out of bounds transparent region",
618 layerFE->getDebugName());
619 layerFEState->transparentRegionHint.dump("transparentRegionHint");
620 }
621 transparentRegion.clear();
622 } else {
623 transparentRegion = tr.transform(clippedTransparentRegionHint);
624 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800625 } else {
626 // transformation too complex, can't do the
627 // transparent region optimization.
628 transparentRegion.clear();
629 }
630 }
631
632 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800633 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800634 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800635 // If we one of the simple category of transforms (0/90/180/270 rotation
636 // + any flip), then the opaque region is the layer's footprint.
637 // Otherwise we don't try and compute the opaque region since there may
638 // be errors at the edges, and we treat the entire layer as
639 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800640 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800641 }
642
643 // Clip the covered region to the visible region
644 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
645
646 // Update accumAboveCoveredLayers for next (lower) layer
647 coverage.aboveCoveredLayers.orSelf(visibleRegion);
648
649 // subtract the opaque region covered by the layers above us
650 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
651
652 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700653 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800654 }
655
656 // Get coverage information for the layer as previously displayed,
657 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800658 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700659 auto prevOutputLayer =
660 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800661
662 // Get coverage information for the layer as previously displayed
663 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
664 const Region kEmptyRegion;
665 const Region& oldVisibleRegion =
666 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
667 const Region& oldCoveredRegion =
668 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
669
670 // compute this layer's dirty region
671 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800672 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800673 // we need to invalidate the whole region
674 dirty = visibleRegion;
675 // as well, as the old visible region
676 dirty.orSelf(oldVisibleRegion);
677 } else {
678 /* compute the exposed region:
679 * the exposed region consists of two components:
680 * 1) what's VISIBLE now and was COVERED before
681 * 2) what's EXPOSED now less what was EXPOSED before
682 *
683 * note that (1) is conservative, we start with the whole visible region
684 * but only keep what used to be covered by something -- which mean it
685 * may have been exposed.
686 *
687 * (2) handles areas that were not covered by anything but got exposed
688 * because of a resize.
689 *
690 */
691 const Region newExposed = visibleRegion - coveredRegion;
692 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
693 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
694 }
695 dirty.subtractSelf(coverage.aboveOpaqueLayers);
696
697 // accumulate to the screen dirty region
698 coverage.dirtyRegion.orSelf(dirty);
699
700 // Update accumAboveOpaqueLayers for next (lower) layer
701 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
702
703 // Compute the visible non-transparent region
704 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
705
Vishnu Naira483b4a2019-12-12 15:07:52 -0800706 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800707 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700708 const auto& outputState = getState();
709 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000710 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800711 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700712 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800713 }
714
Vishnu Naira483b4a2019-12-12 15:07:52 -0800715 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
716
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800717 // The layer is visible. Either reuse the existing outputLayer if we have
718 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800719 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800720
721 // Store the layer coverage information into the layer state as some of it
722 // is useful later.
723 auto& outputLayerState = result->editState();
724 outputLayerState.visibleRegion = visibleRegion;
725 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
726 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200727 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000728 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800729 outputLayerState.shadowRegion = shadowRegion;
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500730 outputLayerState.outputSpaceBlockingRegionHint =
Leon Scroggins III7f7ad2c2022-03-17 17:06:20 -0400731 layerFEState->compositionType == Composition::DISPLAY_DECORATION
732 ? outputState.transform.transform(
733 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
734 : Region();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800735}
736
737void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
738 // The base class does nothing with this call.
739}
740
Dan Stoza269dc4d2021-01-15 15:07:43 -0800741void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800742 ATRACE_CALL();
743 ALOGV(__FUNCTION__);
744
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800745 if (!getState().isEnabled) {
746 return;
747 }
748
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800749 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
750 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
751
Lloyd Pique01c77c12019-04-17 12:48:32 -0700752 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700753 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800754 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200755 forceClientComposition,
756 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800757
758 if (mLayerRequestingBackgroundBlur == layer) {
759 forceClientComposition = false;
760 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800761 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000762
763 updateCompositionStateForBorder(refreshArgs);
764}
765
766void Output::updateCompositionStateForBorder(
767 const compositionengine::CompositionRefreshArgs& refreshArgs) {
768 std::unordered_map<int32_t, const Region*> layerVisibleRegionMap;
769 // Store a map of layerId to their computed visible region.
770 for (auto* layer : getOutputLayersOrderedByZ()) {
771 int layerId = (layer->getLayerFE()).getSequence();
772 layerVisibleRegionMap[layerId] = &((layer->getState()).visibleRegion);
773 }
774 OutputCompositionState& outputCompositionState = editState();
775 outputCompositionState.borderInfoList.clear();
776 bool clientComposeTopLayer = false;
777 for (const auto& borderInfo : refreshArgs.borderInfoList) {
778 renderengine::BorderRenderInfo info;
779 for (const auto& id : borderInfo.layerIds) {
780 info.combinedRegion.orSelf(*(layerVisibleRegionMap[id]));
781 }
Tianhao Yao10cea3c2022-03-30 01:37:22 +0000782
783 if (!info.combinedRegion.isEmpty()) {
784 info.width = borderInfo.width;
785 info.color = borderInfo.color;
786 outputCompositionState.borderInfoList.emplace_back(std::move(info));
787 clientComposeTopLayer = true;
788 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000789 }
790
791 // In this situation we must client compose the top layer instead of using hwc
792 // because we want to draw the border above all else.
793 // This could potentially cause a bit of a performance regression if the top
794 // layer would have been rendered using hwc originally.
795 // TODO(b/227656283): Measure system's performance before enabling the border feature
796 if (clientComposeTopLayer) {
797 auto topLayer = getOutputLayerOrderedByZByIndex(getOutputLayerCount() - 1);
798 (topLayer->editState()).forceClientComposition = true;
799 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800800}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800801
Dan Stoza269dc4d2021-01-15 15:07:43 -0800802void Output::planComposition() {
803 if (!mPlanner || !getState().isEnabled) {
804 return;
805 }
806
807 ATRACE_CALL();
808 ALOGV(__FUNCTION__);
809
810 mPlanner->plan(getOutputLayersOrderedByZ());
811}
812
813void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
814 ATRACE_CALL();
815 ALOGV(__FUNCTION__);
816
817 if (!getState().isEnabled) {
818 return;
819 }
820
Ady Abraham3645e642021-04-20 18:39:00 -0700821 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700822 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham43065bd2021-12-10 17:22:15 -0800823 editState().expectedPresentTime = refreshArgs.expectedPresentTime;
Ady Abraham3645e642021-04-20 18:39:00 -0700824
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400825 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800826 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400827 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400828 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400829 bool overrideZ = false;
Robert Carrec8ccca2022-05-04 09:36:14 -0700830 uint64_t outputLayerHash = 0;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800831 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400832 if (layer == peekThroughLayer) {
833 // No longer needed, although it should not show up again, so
834 // resetting it is not truly needed either.
835 peekThroughLayer = nullptr;
836
837 // peekThroughLayer was already drawn ahead of its z order.
838 continue;
839 }
Dan Stoza6166c312021-01-15 16:34:05 -0800840 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400841 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400842 if (overrideInfo.buffer != nullptr) {
843 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800844 ALOGV("Skipping redundant buffer");
845 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400846 } else {
847 // First layer with the override buffer.
848 if (overrideInfo.peekThroughLayer) {
849 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400850
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400851 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400852 overrideZ = true;
853 includeGeometry = true;
854 constexpr bool isPeekingThrough = true;
855 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
856 isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700857 outputLayerHash ^= android::hashCombine(
858 reinterpret_cast<uint64_t>(&peekThroughLayer->getLayerFE()),
859 z, includeGeometry, overrideZ, isPeekingThrough,
860 peekThroughLayer->requiresClientComposition());
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400861 }
862
863 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800864 }
Dan Stoza6166c312021-01-15 16:34:05 -0800865 }
866
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400867 constexpr bool isPeekingThrough = false;
868 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700869 if (!skipLayer) {
870 outputLayerHash ^= android::hashCombine(
871 reinterpret_cast<uint64_t>(&layer->getLayerFE()),
872 z, includeGeometry, overrideZ, isPeekingThrough,
873 layer->requiresClientComposition());
874 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800875 }
Robert Carrec8ccca2022-05-04 09:36:14 -0700876 editState().outputLayerHash = outputLayerHash;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800877}
878
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800879compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
880 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
881 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100882 auto* compState = layer->getLayerFE().getCompositionState();
883
884 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
885 // want to force client composition because of the blur.
886 if (compState->sidebandStream != nullptr) {
887 return nullptr;
888 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000889 if (compState->isOpaque) {
890 continue;
891 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100892 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800893 layerRequestingBgComposition = layer;
894 }
895 }
896 return layerRequestingBgComposition;
897}
898
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800899void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
900 setColorProfile(pickColorProfile(refreshArgs));
901}
902
903// Returns a data space that fits all visible layers. The returned data space
904// can only be one of
905// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
906// - Dataspace::DISPLAY_P3
907// - Dataspace::DISPLAY_BT2020
908// The returned HDR data space is one of
909// - Dataspace::UNKNOWN
910// - Dataspace::BT2020_HLG
911// - Dataspace::BT2020_PQ
912ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
913 bool* outIsHdrClientComposition) const {
914 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
915 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
916
Vishnu Naire14c6b32022-08-06 04:20:15 +0000917 // An Output's layers may be stale when it is disabled. As a consequence, the layers returned by
918 // getOutputLayersOrderedByZ may not be in a valid state and it is not safe to access their
919 // properties. Return a default dataspace value in this case.
920 if (!getState().isEnabled) {
921 return ui::Dataspace::V0_SRGB;
922 }
923
Lloyd Pique01c77c12019-04-17 12:48:32 -0700924 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800925 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800926 case ui::Dataspace::V0_SCRGB:
927 case ui::Dataspace::V0_SCRGB_LINEAR:
928 case ui::Dataspace::BT2020:
929 case ui::Dataspace::BT2020_ITU:
930 case ui::Dataspace::BT2020_LINEAR:
931 case ui::Dataspace::DISPLAY_BT2020:
932 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
933 break;
934 case ui::Dataspace::DISPLAY_P3:
935 bestDataSpace = ui::Dataspace::DISPLAY_P3;
936 break;
937 case ui::Dataspace::BT2020_PQ:
938 case ui::Dataspace::BT2020_ITU_PQ:
939 bestDataSpace = ui::Dataspace::DISPLAY_P3;
940 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800941 *outIsHdrClientComposition =
942 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800943 break;
944 case ui::Dataspace::BT2020_HLG:
945 case ui::Dataspace::BT2020_ITU_HLG:
946 bestDataSpace = ui::Dataspace::DISPLAY_P3;
947 // When there's mixed PQ content and HLG content, we set the HDR
948 // data space to be BT2020_PQ and convert HLG to PQ.
949 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
950 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
951 }
952 break;
953 default:
954 break;
955 }
956 }
957
958 return bestDataSpace;
959}
960
961compositionengine::Output::ColorProfile Output::pickColorProfile(
962 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
963 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
964 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
965 ui::RenderIntent::COLORIMETRIC,
966 refreshArgs.colorSpaceAgnosticDataspace};
967 }
968
969 ui::Dataspace hdrDataSpace;
970 bool isHdrClientComposition = false;
971 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
972
973 switch (refreshArgs.forceOutputColorMode) {
974 case ui::ColorMode::SRGB:
975 bestDataSpace = ui::Dataspace::V0_SRGB;
976 break;
977 case ui::ColorMode::DISPLAY_P3:
978 bestDataSpace = ui::Dataspace::DISPLAY_P3;
979 break;
980 default:
981 break;
982 }
983
984 // respect hdrDataSpace only when there is no legacy HDR support
985 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
986 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
987 if (isHdr) {
988 bestDataSpace = hdrDataSpace;
989 }
990
991 ui::RenderIntent intent;
992 switch (refreshArgs.outputColorSetting) {
993 case OutputColorSetting::kManaged:
994 case OutputColorSetting::kUnmanaged:
995 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
996 : ui::RenderIntent::COLORIMETRIC;
997 break;
998 case OutputColorSetting::kEnhanced:
999 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
1000 break;
1001 default: // vendor display color setting
1002 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
1003 break;
1004 }
1005
1006 ui::ColorMode outMode;
1007 ui::Dataspace outDataSpace;
1008 ui::RenderIntent outRenderIntent;
1009 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
1010 &outRenderIntent);
1011
1012 return ColorProfile{outMode, outDataSpace, outRenderIntent,
1013 refreshArgs.colorSpaceAgnosticDataspace};
1014}
1015
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001016void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001017 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001018 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -07001019 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001020 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001021
1022 // If nothing has changed (!dirty), don't recompose.
1023 // If something changed, but we don't currently have any visible layers,
1024 // and didn't when we last did a composition, then skip it this time.
1025 // The second rule does two things:
1026 // - When all layers are removed from a display, we'll emit one black
1027 // frame, then nothing more until we get new layers.
1028 // - When a display is created with a private layer stack, we won't
1029 // emit any black frames until a layer is added to the layer stack.
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001030 mMustRecompose = dirty && !(empty && wasEmpty);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001031
1032 const char flagPrefix[] = {'-', '+'};
1033 static_cast<void>(flagPrefix);
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001034 ALOGV("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __func__,
1035 mMustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
1036 flagPrefix[empty], flagPrefix[wasEmpty]);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001037
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001038 mRenderSurface->beginFrame(mMustRecompose);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001039
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001040 if (mMustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001041 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001042 }
1043}
1044
Lloyd Pique66d68602019-02-13 14:23:31 -08001045void Output::prepareFrame() {
1046 ATRACE_CALL();
1047 ALOGV(__FUNCTION__);
1048
Vishnu Naira3140382022-02-24 14:07:11 -08001049 auto& outputState = editState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001050 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -08001051 return;
1052 }
1053
Vishnu Naira3140382022-02-24 14:07:11 -08001054 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1055 bool success = chooseCompositionStrategy(&changes);
1056 resetCompositionStrategy();
Vishnu Nair9cf89262022-02-26 09:17:49 -08001057 outputState.strategyPrediction = CompositionStrategyPredictionState::DISABLED;
Vishnu Naira3140382022-02-24 14:07:11 -08001058 outputState.previousDeviceRequestedChanges = changes;
1059 outputState.previousDeviceRequestedSuccess = success;
1060 if (success) {
1061 applyCompositionStrategy(changes);
1062 }
1063 finishPrepareFrame();
1064}
Lloyd Pique66d68602019-02-13 14:23:31 -08001065
Vishnu Naira3140382022-02-24 14:07:11 -08001066std::future<bool> Output::chooseCompositionStrategyAsync(
1067 std::optional<android::HWComposer::DeviceRequestedChanges>* changes) {
1068 return mHwComposerAsyncWorker->send(
1069 [&, changes]() { return chooseCompositionStrategy(changes); });
1070}
1071
1072GpuCompositionResult Output::prepareFrameAsync(const CompositionRefreshArgs& refreshArgs) {
1073 ATRACE_CALL();
1074 ALOGV(__FUNCTION__);
1075 auto& state = editState();
1076 const auto& previousChanges = state.previousDeviceRequestedChanges;
1077 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1078 resetCompositionStrategy();
1079 auto hwcResult = chooseCompositionStrategyAsync(&changes);
1080 if (state.previousDeviceRequestedSuccess) {
1081 applyCompositionStrategy(previousChanges);
1082 }
1083 finishPrepareFrame();
1084
1085 base::unique_fd bufferFence;
1086 std::shared_ptr<renderengine::ExternalTexture> buffer;
1087 updateProtectedContentState();
1088 const bool dequeueSucceeded = dequeueRenderBuffer(&bufferFence, &buffer);
1089 GpuCompositionResult compositionResult;
1090 if (dequeueSucceeded) {
1091 std::optional<base::unique_fd> optFd =
1092 composeSurfaces(Region::INVALID_REGION, refreshArgs, buffer, bufferFence);
1093 if (optFd) {
1094 compositionResult.fence = std::move(*optFd);
1095 }
Dan Stoza47437bb2021-01-15 16:21:07 -08001096 }
1097
Vishnu Naira3140382022-02-24 14:07:11 -08001098 auto chooseCompositionSuccess = hwcResult.get();
1099 const bool predictionSucceeded = dequeueSucceeded && changes == previousChanges;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001100 state.strategyPrediction = predictionSucceeded ? CompositionStrategyPredictionState::SUCCESS
1101 : CompositionStrategyPredictionState::FAIL;
Vishnu Naira3140382022-02-24 14:07:11 -08001102 if (!predictionSucceeded) {
1103 ATRACE_NAME("CompositionStrategyPredictionMiss");
1104 resetCompositionStrategy();
1105 if (chooseCompositionSuccess) {
1106 applyCompositionStrategy(changes);
1107 }
1108 finishPrepareFrame();
1109 // Track the dequeued buffer to reuse so we don't need to dequeue another one.
1110 compositionResult.buffer = buffer;
1111 } else {
1112 ATRACE_NAME("CompositionStrategyPredictionHit");
1113 }
1114 state.previousDeviceRequestedChanges = std::move(changes);
1115 state.previousDeviceRequestedSuccess = chooseCompositionSuccess;
1116 return compositionResult;
Lloyd Pique66d68602019-02-13 14:23:31 -08001117}
1118
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001119void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1120 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
1121 return;
1122 }
1123
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001124 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001125 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Vishnu Naira3140382022-02-24 14:07:11 -08001126 base::unique_fd bufferFence;
1127 std::shared_ptr<renderengine::ExternalTexture> buffer;
1128 updateProtectedContentState();
1129 dequeueRenderBuffer(&bufferFence, &buffer);
1130 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs, buffer, bufferFence));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001131 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001132 }
1133 }
1134
1135 postFramebuffer();
1136
1137 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
1138
1139 prepareFrame();
1140}
1141
Vishnu Naira3140382022-02-24 14:07:11 -08001142void Output::finishFrame(const CompositionRefreshArgs& refreshArgs, GpuCompositionResult&& result) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001143 ATRACE_CALL();
1144 ALOGV(__FUNCTION__);
Vishnu Nair9cf89262022-02-26 09:17:49 -08001145 const auto& outputState = getState();
1146 if (!outputState.isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001147 return;
1148 }
1149
Vishnu Naira3140382022-02-24 14:07:11 -08001150 std::optional<base::unique_fd> optReadyFence;
1151 std::shared_ptr<renderengine::ExternalTexture> buffer;
1152 base::unique_fd bufferFence;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001153 if (outputState.strategyPrediction == CompositionStrategyPredictionState::SUCCESS) {
Vishnu Naira3140382022-02-24 14:07:11 -08001154 optReadyFence = std::move(result.fence);
1155 } else {
1156 if (result.bufferAvailable()) {
1157 buffer = std::move(result.buffer);
1158 bufferFence = std::move(result.fence);
1159 } else {
1160 updateProtectedContentState();
1161 if (!dequeueRenderBuffer(&bufferFence, &buffer)) {
1162 return;
1163 }
1164 }
1165 // Repaint the framebuffer (if needed), getting the optional fence for when
1166 // the composition completes.
1167 optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs, buffer, bufferFence);
1168 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001169 if (!optReadyFence) {
1170 return;
1171 }
1172
Matt Buckley50c44062022-01-17 20:48:10 +00001173 if (isPowerHintSessionEnabled()) {
1174 // get fence end time to know when gpu is complete in display
Ady Abrahamd11bade2022-08-01 16:18:03 -07001175 setHintSessionGpuFence(
1176 std::make_unique<FenceTime>(sp<Fence>::make(dup(optReadyFence->get()))));
Matt Buckley50c44062022-01-17 20:48:10 +00001177 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001178 // swap buffers (presentation)
1179 mRenderSurface->queueBuffer(std::move(*optReadyFence));
1180}
1181
Vishnu Naira3140382022-02-24 14:07:11 -08001182void Output::updateProtectedContentState() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001183 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001184 auto& renderEngine = getCompositionEngine().getRenderEngine();
1185 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1186
1187 // If we the display is secure, protected content support is enabled, and at
1188 // least one layer has protected content, we need to use a secure back
1189 // buffer.
1190 if (outputState.isSecure && supportsProtectedContent) {
1191 auto layers = getOutputLayersOrderedByZ();
1192 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1193 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1194 });
Patrick Williams8aed5d22022-10-31 22:18:10 +00001195 if (needsProtected != mRenderSurface->isProtected()) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001196 mRenderSurface->setProtected(needsProtected);
1197 }
1198 }
Vishnu Naira3140382022-02-24 14:07:11 -08001199}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001200
Vishnu Naira3140382022-02-24 14:07:11 -08001201bool Output::dequeueRenderBuffer(base::unique_fd* bufferFence,
1202 std::shared_ptr<renderengine::ExternalTexture>* tex) {
1203 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001204
1205 // If we aren't doing client composition on this output, but do have a
1206 // flipClientTarget request for this frame on this output, we still need to
1207 // dequeue a buffer.
Vishnu Naira3140382022-02-24 14:07:11 -08001208 if (outputState.usesClientComposition || outputState.flipClientTarget) {
1209 *tex = mRenderSurface->dequeueBuffer(bufferFence);
1210 if (*tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001211 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1212 "client composition for this frame",
1213 mName.c_str());
Vishnu Naira3140382022-02-24 14:07:11 -08001214 return false;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001215 }
1216 }
Vishnu Naira3140382022-02-24 14:07:11 -08001217 return true;
1218}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001219
Vishnu Naira3140382022-02-24 14:07:11 -08001220std::optional<base::unique_fd> Output::composeSurfaces(
1221 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs,
1222 std::shared_ptr<renderengine::ExternalTexture> tex, base::unique_fd& fd) {
1223 ATRACE_CALL();
1224 ALOGV(__FUNCTION__);
1225
1226 const auto& outputState = getState();
1227 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
1228 outputState.usesClientComposition};
Lloyd Pique688abd42019-02-15 15:42:24 -08001229 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001230 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001231 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001232 }
1233
Vishnu Naira3140382022-02-24 14:07:11 -08001234 if (tex == nullptr) {
1235 ALOGW("Buffer not valid for display [%s], bailing out of "
1236 "client composition for this frame",
1237 mName.c_str());
1238 return {};
1239 }
1240
Lloyd Pique688abd42019-02-15 15:42:24 -08001241 ALOGV("hasClientComposition");
1242
Patrick Williams7584c6a2022-10-29 02:10:58 +00001243 renderengine::DisplaySettings clientCompositionDisplay =
1244 generateClientCompositionDisplaySettings();
Lloyd Pique688abd42019-02-15 15:42:24 -08001245
Lloyd Pique688abd42019-02-15 15:42:24 -08001246 // Generate the client composition requests for the layers on this output.
Vishnu Naira3140382022-02-24 14:07:11 -08001247 auto& renderEngine = getCompositionEngine().getRenderEngine();
1248 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
Robert Carrccab4242021-09-28 16:53:03 -07001249 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001250 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001251 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001252 clientCompositionDisplay.outputDataspace,
1253 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001254 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1255
Vishnu Naira3140382022-02-24 14:07:11 -08001256 OutputCompositionState& outputCompositionState = editState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001257 // Check if the client composition requests were rendered into the provided graphic buffer. If
1258 // so, we can reuse the buffer and avoid client composition.
1259 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001260 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1261 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001262 clientCompositionLayers)) {
Vishnu Naira3140382022-02-24 14:07:11 -08001263 ATRACE_NAME("ClientCompositionCacheHit");
Vishnu Nair9b079a22020-01-21 14:36:08 -08001264 outputCompositionState.reusedClientComposition = true;
1265 setExpensiveRenderingExpected(false);
Vishnu Nair3a49f0a2022-07-29 21:52:53 +00001266 // b/239944175 pass the fence associated with the buffer.
1267 return base::unique_fd(std::move(fd));
Vishnu Nair9b079a22020-01-21 14:36:08 -08001268 }
Vishnu Naira3140382022-02-24 14:07:11 -08001269 ATRACE_NAME("ClientCompositionCacheMiss");
Alec Mouria90a5702021-04-16 16:36:21 +00001270 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001271 clientCompositionLayers);
1272 }
1273
Lloyd Pique688abd42019-02-15 15:42:24 -08001274 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001275 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1276 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1277 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001278 const bool expensiveBlurs =
1279 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Leon Scroggins IIIcf17ebc2022-03-03 14:54:00 -05001280 const bool expensiveRenderingExpected = expensiveBlurs ||
1281 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1282 [outputDataspace =
1283 clientCompositionDisplay.outputDataspace](const auto& layer) {
1284 return layer.sourceDataspace != outputDataspace;
1285 });
Lloyd Pique688abd42019-02-15 15:42:24 -08001286 if (expensiveRenderingExpected) {
1287 setExpensiveRenderingExpected(true);
1288 }
1289
Sally Qi59a9f502021-10-12 18:53:23 +00001290 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1291 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001292 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001293 std::back_inserter(clientRenderEngineLayers),
1294 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1295 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001296 });
1297
Alec Mourie4034bb2019-11-19 12:45:54 -08001298 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001299 // Only use the framebuffer cache when rendering to an internal display
1300 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1301 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1302 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1303 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1304 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001305 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001306
Patrick Williams2e9748f2022-08-09 22:48:18 +00001307 auto fenceResult = renderEngine
1308 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
1309 useFramebufferCache, std::move(fd))
1310 .get();
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001311
1312 if (mClientCompositionRequestCache && fenceStatus(fenceResult) != NO_ERROR) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001313 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001314 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001315 }
1316
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001317 const auto fence = std::move(fenceResult).value_or(Fence::NO_FENCE);
1318
Patrick Williams74c0bf62022-11-02 23:59:26 +00001319 if (auto timeStats = getCompositionEngine().getTimeStats()) {
1320 if (fence->isValid()) {
1321 timeStats->recordRenderEngineDuration(renderEngineStart,
1322 std::make_shared<FenceTime>(fence));
1323 } else {
1324 timeStats->recordRenderEngineDuration(renderEngineStart, systemTime());
1325 }
Alec Mourie4034bb2019-11-19 12:45:54 -08001326 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001327
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001328 for (auto* clientComposedLayer : clientCompositionLayersFE) {
1329 clientComposedLayer->setWasClientComposed(fence);
Robert Carrccab4242021-09-28 16:53:03 -07001330 }
1331
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001332 return base::unique_fd(fence->dup());
Lloyd Pique688abd42019-02-15 15:42:24 -08001333}
1334
Patrick Williams7584c6a2022-10-29 02:10:58 +00001335renderengine::DisplaySettings Output::generateClientCompositionDisplaySettings() const {
1336 const auto& outputState = getState();
1337
1338 renderengine::DisplaySettings clientCompositionDisplay;
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001339 clientCompositionDisplay.namePlusId = mNamePlusId;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001340 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1341 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
1342 clientCompositionDisplay.orientation =
1343 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
1344 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1345 ? outputState.dataspace
1346 : ui::Dataspace::UNKNOWN;
1347
1348 // If we have a valid current display brightness use that, otherwise fall back to the
1349 // display's max desired
1350 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
1351 ? outputState.displayBrightnessNits
1352 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1353 clientCompositionDisplay.maxLuminance =
1354 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1355 clientCompositionDisplay.targetLuminanceNits =
1356 outputState.clientTargetBrightness * outputState.displayBrightnessNits;
1357 clientCompositionDisplay.dimmingStage = outputState.clientTargetDimmingStage;
1358 clientCompositionDisplay.renderIntent =
1359 static_cast<aidl::android::hardware::graphics::composer3::RenderIntent>(
1360 outputState.renderIntent);
1361
1362 // Compute the global color transform matrix.
1363 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
1364 for (auto& info : outputState.borderInfoList) {
1365 renderengine::BorderRenderInfo borderInfo;
1366 borderInfo.width = info.width;
1367 borderInfo.color = info.color;
1368 borderInfo.combinedRegion = info.combinedRegion;
1369 clientCompositionDisplay.borderInfoList.emplace_back(std::move(borderInfo));
1370 }
1371 clientCompositionDisplay.deviceHandlesColorTransform =
1372 outputState.usesDeviceComposition || getSkipColorTransform();
1373 return clientCompositionDisplay;
1374}
1375
Vishnu Nair9b079a22020-01-21 14:36:08 -08001376std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001377 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001378 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001379 ALOGV("Rendering client layers");
1380
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001381 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001382 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001383 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001384
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001385 bool disableBlurs = false;
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001386 uint64_t previousOverrideBufferId = 0;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001387
Lloyd Pique01c77c12019-04-17 12:48:32 -07001388 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001389 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001390 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001391 auto& layerFE = layer->getLayerFE();
Robert Carr05da0082022-05-25 23:29:34 -07001392 layerFE.setWasClientComposed(nullptr);
Lloyd Pique688abd42019-02-15 15:42:24 -08001393
Lloyd Piquea2468662019-03-07 21:31:06 -08001394 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001395 ALOGV("Layer: %s", layerFE.getDebugName());
1396 if (clip.isEmpty()) {
1397 ALOGV(" Skipping for empty clip");
1398 firstLayer = false;
1399 continue;
1400 }
1401
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001402 disableBlurs |= layerFEState->sidebandStream != nullptr;
1403
Vishnu Naira483b4a2019-12-12 15:07:52 -08001404 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001405
1406 // We clear the client target for non-client composed layers if
1407 // requested by the HWC. We skip this if the layer is not an opaque
1408 // rectangle, as by definition the layer must blend with whatever is
1409 // underneath. We also skip the first layer as the buffer target is
1410 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001411 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001412 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001413
1414 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1415
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001416 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1417 // composing the non-shadow content and only draw the shadows.
1418 const bool realContentIsVisible = clientComposition &&
1419 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1420
Lloyd Pique688abd42019-02-15 15:42:24 -08001421 if (clientComposition || clearClientComposition) {
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001422 if (auto overrideSettings = layer->getOverrideCompositionSettings()) {
1423 if (overrideSettings->bufferId != previousOverrideBufferId) {
1424 previousOverrideBufferId = overrideSettings->bufferId;
1425 clientCompositionLayers.push_back(std::move(*overrideSettings));
Huihong Luo91ac3b52021-04-08 11:07:41 -07001426 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1427 } else {
1428 ALOGV("Skipping redundant override buffer for [%s] in RE",
1429 layer->getLayerFE().getDebugName());
1430 }
Dan Stoza6166c312021-01-15 16:34:05 -08001431 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001432 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1433 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1434 : (layer->getState().overrideInfo.disableBackgroundBlur
1435 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1436 BlurRegionsOnly
1437 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1438 Enabled);
1439 compositionengine::LayerFE::ClientCompositionTargetSettings
1440 targetSettings{.clip = clip,
Patrick Williams7584c6a2022-10-29 02:10:58 +00001441 .needsFiltering = layerNeedsFiltering(layer) ||
Alec Mourif54453c2021-05-13 16:28:28 -07001442 outputState.needsFiltering,
1443 .isSecure = outputState.isSecure,
1444 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001445 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001446 .dataspace = outputDataspace,
1447 .realContentIsVisible = realContentIsVisible,
1448 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001449 .blurSetting = blurSetting,
Vishnu Naire14c6b32022-08-06 04:20:15 +00001450 .whitePointNits = layerState.whitePointNits,
1451 .treat170mAsSrgb = outputState.treat170mAsSrgb};
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001452 if (auto clientCompositionSettings =
1453 layerFE.prepareClientComposition(targetSettings)) {
1454 clientCompositionLayers.push_back(std::move(*clientCompositionSettings));
1455 if (realContentIsVisible) {
1456 layer->editState().clientCompositionTimestamp = systemTime();
1457 }
Dan Stoza6166c312021-01-15 16:34:05 -08001458 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001459 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001460
Tianhua Sunf91f1402022-05-09 05:45:46 +00001461 if (clientComposition) {
1462 outLayerFEs.push_back(&layerFE);
1463 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001464 }
1465
1466 firstLayer = false;
1467 }
1468
1469 return clientCompositionLayers;
1470}
1471
Patrick Williams7584c6a2022-10-29 02:10:58 +00001472bool Output::layerNeedsFiltering(const compositionengine::OutputLayer* layer) const {
1473 return layer->needsFiltering();
1474}
1475
Lloyd Pique688abd42019-02-15 15:42:24 -08001476void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001477 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001478 if (flashRegion.isEmpty()) {
1479 return;
1480 }
1481
Vishnu Nair9b079a22020-01-21 14:36:08 -08001482 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001483 layerSettings.source.buffer.buffer = nullptr;
1484 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1485 layerSettings.alpha = half(1.0);
1486
1487 for (const auto& rect : flashRegion) {
1488 layerSettings.geometry.boundaries = rect.toFloatRect();
1489 clientCompositionLayers.push_back(layerSettings);
1490 }
1491}
1492
1493void Output::setExpensiveRenderingExpected(bool) {
1494 // The base class does nothing with this call.
1495}
1496
Matt Buckley50c44062022-01-17 20:48:10 +00001497void Output::setHintSessionGpuFence(std::unique_ptr<FenceTime>&&) {
1498 // The base class does nothing with this call.
1499}
1500
1501bool Output::isPowerHintSessionEnabled() {
1502 return false;
1503}
1504
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001505void Output::postFramebuffer() {
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001506 ATRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001507 ALOGV(__FUNCTION__);
1508
1509 if (!getState().isEnabled) {
1510 return;
1511 }
1512
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001513 auto& outputState = editState();
1514 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001515
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001516 auto frame = presentAndGetFrameFences();
1517
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001518 mRenderSurface->onPresentDisplayCompleted();
1519
Lloyd Pique01c77c12019-04-17 12:48:32 -07001520 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001521 // The layer buffer from the previous frame (if any) is released
1522 // by HWC only when the release fence from this frame (if any) is
1523 // signaled. Always get the release fence from HWC first.
1524 sp<Fence> releaseFence = Fence::NO_FENCE;
1525
1526 if (auto hwcLayer = layer->getHwcLayer()) {
1527 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1528 releaseFence = f->second;
1529 }
1530 }
1531
1532 // If the layer was client composited in the previous frame, we
1533 // need to merge with the previous client target acquire fence.
1534 // Since we do not track that, always merge with the current
1535 // client target acquire fence when it is available, even though
1536 // this is suboptimal.
1537 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001538 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001539 releaseFence =
1540 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1541 }
Sally Qi59a9f502021-10-12 18:53:23 +00001542 layer->getLayerFE().onLayerDisplayed(
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001543 ftl::yield<FenceResult>(std::move(releaseFence)).share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001544 }
1545
1546 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001547 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001548 // supply them with the present fence.
1549 for (auto& weakLayer : mReleasedLayers) {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001550 if (const auto layer = weakLayer.promote()) {
1551 layer->onLayerDisplayed(ftl::yield<FenceResult>(frame.presentFence).share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001552 }
1553 }
1554
1555 // Clear out the released layers now that we're done with them.
1556 mReleasedLayers.clear();
1557}
1558
Alec Mouriaa831582021-06-07 16:23:01 -07001559void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001560 if (mPlanner) {
Brian Johnson869e28f2022-08-12 22:20:19 +00001561 mPlanner->renderCachedSets(getState(), refreshArgs.scheduledFrameTime,
1562 getState().usesDeviceComposition || getSkipColorTransform());
Dan Stoza6166c312021-01-15 16:34:05 -08001563 }
1564}
1565
Lloyd Pique32cbe282018-10-19 13:09:22 -07001566void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001567 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001568 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001569}
1570
Vishnu Naira3140382022-02-24 14:07:11 -08001571void Output::resetCompositionStrategy() {
Lloyd Pique66d68602019-02-13 14:23:31 -08001572 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001573 auto& outputState = editState();
1574 outputState.usesClientComposition = true;
1575 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001576 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001577}
1578
Lloyd Pique688abd42019-02-15 15:42:24 -08001579bool Output::getSkipColorTransform() const {
1580 return true;
1581}
1582
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001583compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1584 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001585 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001586 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1587 }
1588 return result;
1589}
1590
Vishnu Naira3140382022-02-24 14:07:11 -08001591void Output::setPredictCompositionStrategy(bool predict) {
1592 if (predict) {
1593 mHwComposerAsyncWorker = std::make_unique<HwcAsyncWorker>();
1594 } else {
1595 mHwComposerAsyncWorker.reset(nullptr);
1596 }
1597}
1598
Alec Mouridda07d92022-04-25 22:39:25 +00001599void Output::setTreat170mAsSrgb(bool enable) {
1600 editState().treat170mAsSrgb = enable;
1601}
1602
Vishnu Naira3140382022-02-24 14:07:11 -08001603bool Output::canPredictCompositionStrategy(const CompositionRefreshArgs& refreshArgs) {
Robert Carrec8ccca2022-05-04 09:36:14 -07001604 uint64_t lastOutputLayerHash = getState().lastOutputLayerHash;
1605 uint64_t outputLayerHash = getState().outputLayerHash;
1606 editState().lastOutputLayerHash = outputLayerHash;
1607
Vishnu Naira3140382022-02-24 14:07:11 -08001608 if (!getState().isEnabled || !mHwComposerAsyncWorker) {
1609 ALOGV("canPredictCompositionStrategy disabled");
1610 return false;
1611 }
1612
1613 if (!getState().previousDeviceRequestedChanges) {
1614 ALOGV("canPredictCompositionStrategy previous changes not available");
1615 return false;
1616 }
1617
1618 if (!mRenderSurface->supportsCompositionStrategyPrediction()) {
1619 ALOGV("canPredictCompositionStrategy surface does not support");
1620 return false;
1621 }
1622
1623 if (refreshArgs.devOptFlashDirtyRegionsDelay) {
1624 ALOGV("canPredictCompositionStrategy devOptFlashDirtyRegionsDelay");
1625 return false;
1626 }
1627
Robert Carrec8ccca2022-05-04 09:36:14 -07001628 if (lastOutputLayerHash != outputLayerHash) {
1629 ALOGV("canPredictCompositionStrategy output layers changed");
1630 return false;
1631 }
1632
Vishnu Naira3140382022-02-24 14:07:11 -08001633 // If no layer uses clientComposition, then don't predict composition strategy
1634 // because we have less work to do in parallel.
1635 if (!anyLayersRequireClientComposition()) {
1636 ALOGV("canPredictCompositionStrategy no layer uses clientComposition");
1637 return false;
1638 }
1639
Robert Carrec8ccca2022-05-04 09:36:14 -07001640 return true;
Vishnu Naira3140382022-02-24 14:07:11 -08001641}
1642
1643bool Output::anyLayersRequireClientComposition() const {
1644 const auto layers = getOutputLayersOrderedByZ();
1645 return std::any_of(layers.begin(), layers.end(),
1646 [](const auto& layer) { return layer->requiresClientComposition(); });
1647}
1648
1649void Output::finishPrepareFrame() {
1650 const auto& state = getState();
1651 if (mPlanner) {
1652 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
1653 }
1654 mRenderSurface->prepareFrame(state.usesClientComposition, state.usesDeviceComposition);
1655}
1656
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001657bool Output::mustRecompose() const {
1658 return mMustRecompose;
1659}
1660
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001661} // namespace impl
1662} // namespace android::compositionengine