blob: 993b8ef2a947dc9722a518900d8be878d18edbff [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>
Dan Stoza269dc4d2021-01-15 15:07:43 -080032
Alec Mouria90a5702021-04-16 16:36:21 +000033#include <thread>
34
35#include "renderengine/ExternalTexture.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080036
37// TODO(b/129481165): remove the #pragma below and fix conversion issues
38#pragma clang diagnostic push
39#pragma clang diagnostic ignored "-Wconversion"
40
Lloyd Pique688abd42019-02-15 15:42:24 -080041#include <renderengine/DisplaySettings.h>
42#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080043
44// TODO(b/129481165): remove the #pragma below and fix conversion issues
45#pragma clang diagnostic pop // ignored "-Wconversion"
46
Dan Stoza269dc4d2021-01-15 15:07:43 -080047#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070048#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080049#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080050#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070051
Lloyd Pique688abd42019-02-15 15:42:24 -080052#include "TracedOrdinal.h"
53
Leon Scroggins III9a0afda2022-01-11 16:53:09 -050054using aidl::android::hardware::graphics::composer3::Composition;
55
Lloyd Piquefeb73d72018-12-04 17:23:44 -080056namespace android::compositionengine {
57
58Output::~Output() = default;
59
60namespace impl {
Vishnu Nair9cf89262022-02-26 09:17:49 -080061using CompositionStrategyPredictionState =
62 OutputCompositionState::CompositionStrategyPredictionState;
Lloyd Piquec29e4c62019-03-07 21:48:19 -080063namespace {
64
65template <typename T>
66class Reversed {
67public:
68 explicit Reversed(const T& container) : mContainer(container) {}
69 auto begin() { return mContainer.rbegin(); }
70 auto end() { return mContainer.rend(); }
71
72private:
73 const T& mContainer;
74};
75
76// Helper for enumerating over a container in reverse order
77template <typename T>
78Reversed<T> reversed(const T& c) {
79 return Reversed<T>(c);
80}
81
Marin Shalamanovb15d2272020-09-17 21:41:52 +020082struct ScaleVector {
83 float x;
84 float y;
85};
86
87// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
88// where to' will have the same size as "to". In the case where "from" and "to"
89// start at the origin to'=to.
90ScaleVector getScale(const Rect& from, const Rect& to) {
91 return {.x = static_cast<float>(to.width()) / from.width(),
92 .y = static_cast<float>(to.height()) / from.height()};
93}
94
Lloyd Piquec29e4c62019-03-07 21:48:19 -080095} // namespace
96
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070097std::shared_ptr<Output> createOutput(
98 const compositionengine::CompositionEngine& compositionEngine) {
99 return createOutputTemplated<Output>(compositionEngine);
100}
Lloyd Pique32cbe282018-10-19 13:09:22 -0700101
102Output::~Output() = default;
103
Lloyd Pique32cbe282018-10-19 13:09:22 -0700104bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700105 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
106 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700107}
108
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700109std::optional<DisplayId> Output::getDisplayId() const {
110 return {};
111}
112
Lloyd Pique32cbe282018-10-19 13:09:22 -0700113const std::string& Output::getName() const {
114 return mName;
115}
116
117void Output::setName(const std::string& name) {
118 mName = name;
119}
120
121void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700122 auto& outputState = editState();
123 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700124 return;
125 }
126
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700127 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700128 dirtyEntireOutput();
129}
130
Alec Mouri023c1882021-05-08 16:36:33 -0700131void Output::setLayerCachingEnabled(bool enabled) {
132 if (enabled == (mPlanner != nullptr)) {
133 return;
134 }
135
136 if (enabled) {
Alec Mouridf6201b2021-06-01 16:20:42 -0700137 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
Alec Mouri023c1882021-05-08 16:36:33 -0700138 if (mRenderSurface) {
139 mPlanner->setDisplaySize(mRenderSurface->getSize());
140 }
141 } else {
142 mPlanner.reset();
143 }
Alec Mouric773472b2021-05-19 14:29:05 -0700144
145 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
146 if (!outputLayer) {
147 continue;
148 }
149
150 outputLayer->editState().overrideInfo = {};
151 }
Alec Mouri023c1882021-05-08 16:36:33 -0700152}
153
Ady Abrahamdb036a82021-07-16 14:18:34 -0700154void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
155 if (mPlanner) {
156 mPlanner->setTexturePoolEnabled(enabled);
157 }
158}
159
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200160void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
161 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700162 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200163
Angel Aguayob084e0c2021-08-04 23:27:28 +0000164 outputState.displaySpace.setOrientation(orientation);
165 LOG_FATAL_IF(outputState.displaySpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200166 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200167
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200168 // Compute orientedDisplaySpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000169 ui::Size orientedSize = outputState.displaySpace.getBounds();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200170 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200171 std::swap(orientedSize.width, orientedSize.height);
172 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000173 outputState.orientedDisplaySpace.setBounds(orientedSize);
174 outputState.orientedDisplaySpace.setContent(orientedDisplaySpaceRect);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200175
176 // Compute displaySpace.content
177 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
178 ui::Transform rotation;
179 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000180 const auto displaySize = outputState.displaySpace.getBoundsAsRect();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200181 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
182 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000183 outputState.displaySpace.setContent(rotation.transform(orientedDisplaySpaceRect));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200184
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200185 // Compute framebufferSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000186 outputState.framebufferSpace.setOrientation(orientation);
187 LOG_FATAL_IF(outputState.framebufferSpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200188 "The framebuffer bounds are unknown.");
Angel Aguayob084e0c2021-08-04 23:27:28 +0000189 const auto scale = getScale(outputState.displaySpace.getBoundsAsRect(),
190 outputState.framebufferSpace.getBoundsAsRect());
191 outputState.framebufferSpace.setContent(
192 outputState.displaySpace.getContent().scale(scale.x, scale.y));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200193
194 // Compute layerStackSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000195 outputState.layerStackSpace.setContent(layerStackSpaceRect);
196 outputState.layerStackSpace.setBounds(
197 ui::Size(layerStackSpaceRect.getWidth(), layerStackSpaceRect.getHeight()));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200198
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200199 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
200 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700201 dirtyEntireOutput();
202}
203
Alec Mouricdf16792021-12-10 13:16:06 -0800204void Output::setNextBrightness(float brightness) {
205 editState().displayBrightness = brightness;
206}
207
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200208void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700209 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200210
211 auto& state = editState();
212
213 // Update framebuffer space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000214 const ui::Size newBounds(size);
215 state.framebufferSpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200216
217 // Update display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000218 state.displaySpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200219 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
220
221 // Update oriented display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000222 const auto orientation = state.displaySpace.getOrientation();
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200223 ui::Size orientedSize = size;
224 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
225 std::swap(orientedSize.width, orientedSize.height);
226 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000227 const ui::Size newOrientedBounds(orientedSize);
228 state.orientedDisplaySpace.setBounds(newOrientedBounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700229
Dan Stoza6166c312021-01-15 16:34:05 -0800230 if (mPlanner) {
231 mPlanner->setDisplaySize(size);
232 }
233
Lloyd Pique32cbe282018-10-19 13:09:22 -0700234 dirtyEntireOutput();
235}
236
Garfield Tan54edd912020-10-21 16:31:41 -0700237ui::Transform::RotationFlags Output::getTransformHint() const {
238 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
239}
240
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700241void Output::setLayerFilter(ui::LayerFilter filter) {
242 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700243 dirtyEntireOutput();
244}
245
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800246void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700247 auto& colorTransformMatrix = editState().colorTransformMatrix;
248 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700249 return;
250 }
251
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700252 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800253
254 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700255}
256
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800257void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700258 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800259 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
260 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800261
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700262 auto& outputState = editState();
263 if (outputState.colorMode == colorProfile.mode &&
264 outputState.dataspace == colorProfile.dataspace &&
265 outputState.renderIntent == colorProfile.renderIntent &&
266 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800267 return;
268 }
269
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700270 outputState.colorMode = colorProfile.mode;
271 outputState.dataspace = colorProfile.dataspace;
272 outputState.renderIntent = colorProfile.renderIntent;
273 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700274
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800275 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700276
Lloyd Pique32cbe282018-10-19 13:09:22 -0700277 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800278 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
279 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800280
281 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700282}
283
John Reckac09e452021-04-07 16:35:37 -0400284void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
285 auto& outputState = editState();
286 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
287 outputState.displayBrightnessNits == displayBrightnessNits) {
288 // Nothing changed
289 return;
290 }
291 outputState.sdrWhitePointNits = sdrWhitePointNits;
292 outputState.displayBrightnessNits = displayBrightnessNits;
293 dirtyEntireOutput();
294}
295
Lloyd Pique32cbe282018-10-19 13:09:22 -0700296void Output::dump(std::string& out) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700297 base::StringAppendF(&out, "Output \"%s\"", mName.c_str());
298 out.append("\n Composition Output State:\n");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700299
300 dumpBase(out);
301}
302
303void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700304 dumpState(out);
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700305 out += '\n';
Lloyd Pique31cb2942018-10-19 17:23:03 -0700306
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700307 if (mDisplayColorProfile) {
308 mDisplayColorProfile->dump(out);
309 } else {
310 out.append(" No display color profile!\n");
311 }
312
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700313 out += '\n';
314
Lloyd Pique31cb2942018-10-19 17:23:03 -0700315 if (mRenderSurface) {
316 mRenderSurface->dump(out);
317 } else {
318 out.append(" No render surface!\n");
319 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800320
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700321 base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
Lloyd Pique01c77c12019-04-17 12:48:32 -0700322 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800323 if (!outputLayer) {
324 continue;
325 }
326 outputLayer->dump(out);
327 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700328}
329
Dan Stoza269dc4d2021-01-15 15:07:43 -0800330void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
331 if (!mPlanner) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700332 out.append("Planner is disabled\n");
Dan Stoza269dc4d2021-01-15 15:07:43 -0800333 return;
334 }
335 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
336 mPlanner->dump(args, out);
337}
338
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700339compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
340 return mDisplayColorProfile.get();
341}
342
343void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
344 mDisplayColorProfile = std::move(mode);
345}
346
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800347const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
348 return mReleasedLayers;
349}
350
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700351void Output::setDisplayColorProfileForTest(
352 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
353 mDisplayColorProfile = std::move(mode);
354}
355
Lloyd Pique31cb2942018-10-19 17:23:03 -0700356compositionengine::RenderSurface* Output::getRenderSurface() const {
357 return mRenderSurface.get();
358}
359
360void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
361 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800362 const auto size = mRenderSurface->getSize();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000363 editState().framebufferSpace.setBounds(size);
Dan Stoza6166c312021-01-15 16:34:05 -0800364 if (mPlanner) {
365 mPlanner->setDisplaySize(size);
366 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700367 dirtyEntireOutput();
368}
369
Vishnu Nair9b079a22020-01-21 14:36:08 -0800370void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
371 if (cacheSize == 0) {
372 mClientCompositionRequestCache.reset();
373 } else {
374 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
375 }
376};
377
Lloyd Pique31cb2942018-10-19 17:23:03 -0700378void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
379 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700380}
381
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700382Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700383 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000384 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700385}
386
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700387bool Output::includesLayer(ui::LayerFilter filter) const {
388 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700389}
390
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700391bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800392 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700393 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800394}
395
Lloyd Piquedf336d92019-03-07 21:38:42 -0800396std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800397 const sp<LayerFE>& layerFE) const {
398 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800399}
400
Lloyd Piquede196652020-01-22 17:29:58 -0800401compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
402 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700403 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800404}
405
Lloyd Pique01c77c12019-04-17 12:48:32 -0700406std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800407 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700408 for (size_t i = 0; i < getOutputLayerCount(); i++) {
409 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800410 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700411 return i;
412 }
413 }
414 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800415}
416
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800417void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
418 mReleasedLayers = std::move(layers);
419}
420
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800421void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
422 LayerFESet& geomSnapshots) {
423 ATRACE_CALL();
424 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800425
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800426 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800427}
428
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800429void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800430 ATRACE_CALL();
431 ALOGV(__FUNCTION__);
432
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800433 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800434 updateCompositionState(refreshArgs);
435 planComposition();
436 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800437 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800438 beginFrame();
Vishnu Naira3140382022-02-24 14:07:11 -0800439
440 GpuCompositionResult result;
441 const bool predictCompositionStrategy = canPredictCompositionStrategy(refreshArgs);
442 if (predictCompositionStrategy) {
443 result = prepareFrameAsync(refreshArgs);
444 } else {
445 prepareFrame();
446 }
447
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800448 devOptRepaintFlash(refreshArgs);
Vishnu Naira3140382022-02-24 14:07:11 -0800449 finishFrame(refreshArgs, std::move(result));
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800450 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700451 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800452}
453
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800454void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
455 LayerFESet& layerFESet) {
456 ATRACE_CALL();
457 ALOGV(__FUNCTION__);
458
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700459 auto& outputState = editState();
460
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800461 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700462 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800463 return;
464 }
465
466 // Process the layers to determine visibility and coverage
467 compositionengine::Output::CoverageState coverage{layerFESet};
468 collectVisibleLayers(refreshArgs, coverage);
469
470 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700471 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000472 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800473 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
474
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700475 outputState.undefinedRegion = undefinedRegion;
476 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800477}
478
479void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
480 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800481 // Evaluate the layers from front to back to determine what is visible. This
482 // also incrementally calculates the coverage information for each layer as
483 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800484 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700485 // Incrementally process the coverage for each layer
486 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800487
488 // TODO(b/121291683): Stop early if the output is completely covered and
489 // no more layers could even be visible underneath the ones on top.
490 }
491
Lloyd Pique01c77c12019-04-17 12:48:32 -0700492 setReleasedLayers(refreshArgs);
493
494 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800495}
496
Lloyd Piquede196652020-01-22 17:29:58 -0800497void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700498 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800499 // Ensure we have a snapshot of the basic geometry layer state. Limit the
500 // snapshots to once per frame for each candidate layer, as layers may
501 // appear on multiple outputs.
502 if (!coverage.latchedLayers.count(layerFE)) {
503 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800504 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800505 }
506
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700507 // Only consider the layers on this output
508 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800509 return;
510 }
511
512 // Obtain a read-only pointer to the front-end layer state
513 const auto* layerFEState = layerFE->getCompositionState();
514 if (CC_UNLIKELY(!layerFEState)) {
515 return;
516 }
517
518 // handle hidden surfaces by setting the visible region to empty
519 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700520 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800521 }
522
523 /*
524 * opaqueRegion: area of a surface that is fully opaque.
525 */
526 Region opaqueRegion;
527
528 /*
529 * visibleRegion: area of a surface that is visible on screen and not fully
530 * transparent. This is essentially the layer's footprint minus the opaque
531 * regions above it. Areas covered by a translucent surface are considered
532 * visible.
533 */
534 Region visibleRegion;
535
536 /*
537 * coveredRegion: area of a surface that is covered by all visible regions
538 * above it (which includes the translucent areas).
539 */
540 Region coveredRegion;
541
542 /*
543 * transparentRegion: area of a surface that is hinted to be completely
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500544 * transparent.
545 * This is used to tell when the layer has no visible non-transparent
546 * regions and can be removed from the layer list. It does not affect the
547 * visibleRegion of this layer or any layers beneath it. The hint may not
548 * be correct if apps don't respect the SurfaceView restrictions (which,
549 * sadly, some don't).
550 *
551 * In addition, it is used on DISPLAY_DECORATION layers to specify the
552 * blockingRegion, allowing the DPU to skip it to save power. Once we have
553 * hardware that supports a blockingRegion on frames with AFBC, it may be
554 * useful to use this for other layers, too, so long as we can prevent
555 * regressions on b/7179570.
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800556 */
557 Region transparentRegion;
558
Vishnu Naira483b4a2019-12-12 15:07:52 -0800559 /*
560 * shadowRegion: Region cast by the layer's shadow.
561 */
562 Region shadowRegion;
563
Lloyd Piquede196652020-01-22 17:29:58 -0800564 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800565
566 // Get the visible region
567 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
568 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800569 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800570 visibleRegion.set(visibleRect);
571
Lloyd Piquede196652020-01-22 17:29:58 -0800572 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800573 // if the layer casts a shadow, offset the layers visible region and
574 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800575 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800576 Rect visibleRectWithShadows(visibleRect);
577 visibleRectWithShadows.inset(inset, inset, inset, inset);
578 visibleRegion.set(visibleRectWithShadows);
579 shadowRegion = visibleRegion.subtract(visibleRect);
580 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800581
582 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700583 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800584 }
585
586 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800587 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800588 if (tr.preserveRects()) {
Alec Mourie60f0b92022-06-10 19:15:20 +0000589 // Clip the transparent region to geomLayerBounds first
590 // The transparent region may be influenced by applications, for
591 // instance, by overriding ViewGroup#gatherTransparentRegion with a
592 // custom view. Once the layer stack -> display mapping is known, we
593 // must guard against very wrong inputs to prevent underflow or
594 // overflow errors. We do this here by constraining the transparent
595 // region to be within the pre-transform layer bounds, since the
596 // layer bounds are expected to play nicely with the full
597 // transform.
598 const Region clippedTransparentRegionHint =
599 layerFEState->transparentRegionHint.intersect(
600 Rect(layerFEState->geomLayerBounds));
601
602 if (clippedTransparentRegionHint.isEmpty()) {
603 if (!layerFEState->transparentRegionHint.isEmpty()) {
604 ALOGD("Layer: %s had an out of bounds transparent region",
605 layerFE->getDebugName());
606 layerFEState->transparentRegionHint.dump("transparentRegionHint");
607 }
608 transparentRegion.clear();
609 } else {
610 transparentRegion = tr.transform(clippedTransparentRegionHint);
611 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800612 } else {
613 // transformation too complex, can't do the
614 // transparent region optimization.
615 transparentRegion.clear();
616 }
617 }
618
619 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800620 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800621 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800622 // If we one of the simple category of transforms (0/90/180/270 rotation
623 // + any flip), then the opaque region is the layer's footprint.
624 // Otherwise we don't try and compute the opaque region since there may
625 // be errors at the edges, and we treat the entire layer as
626 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800627 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800628 }
629
630 // Clip the covered region to the visible region
631 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
632
633 // Update accumAboveCoveredLayers for next (lower) layer
634 coverage.aboveCoveredLayers.orSelf(visibleRegion);
635
636 // subtract the opaque region covered by the layers above us
637 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
638
639 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700640 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800641 }
642
643 // Get coverage information for the layer as previously displayed,
644 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800645 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700646 auto prevOutputLayer =
647 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800648
649 // Get coverage information for the layer as previously displayed
650 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
651 const Region kEmptyRegion;
652 const Region& oldVisibleRegion =
653 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
654 const Region& oldCoveredRegion =
655 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
656
657 // compute this layer's dirty region
658 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800659 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800660 // we need to invalidate the whole region
661 dirty = visibleRegion;
662 // as well, as the old visible region
663 dirty.orSelf(oldVisibleRegion);
664 } else {
665 /* compute the exposed region:
666 * the exposed region consists of two components:
667 * 1) what's VISIBLE now and was COVERED before
668 * 2) what's EXPOSED now less what was EXPOSED before
669 *
670 * note that (1) is conservative, we start with the whole visible region
671 * but only keep what used to be covered by something -- which mean it
672 * may have been exposed.
673 *
674 * (2) handles areas that were not covered by anything but got exposed
675 * because of a resize.
676 *
677 */
678 const Region newExposed = visibleRegion - coveredRegion;
679 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
680 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
681 }
682 dirty.subtractSelf(coverage.aboveOpaqueLayers);
683
684 // accumulate to the screen dirty region
685 coverage.dirtyRegion.orSelf(dirty);
686
687 // Update accumAboveOpaqueLayers for next (lower) layer
688 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
689
690 // Compute the visible non-transparent region
691 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
692
Vishnu Naira483b4a2019-12-12 15:07:52 -0800693 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800694 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700695 const auto& outputState = getState();
696 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000697 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800698 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700699 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800700 }
701
Vishnu Naira483b4a2019-12-12 15:07:52 -0800702 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
703
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800704 // The layer is visible. Either reuse the existing outputLayer if we have
705 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800706 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800707
708 // Store the layer coverage information into the layer state as some of it
709 // is useful later.
710 auto& outputLayerState = result->editState();
711 outputLayerState.visibleRegion = visibleRegion;
712 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
713 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200714 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000715 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800716 outputLayerState.shadowRegion = shadowRegion;
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500717 outputLayerState.outputSpaceBlockingRegionHint =
Leon Scroggins III7f7ad2c2022-03-17 17:06:20 -0400718 layerFEState->compositionType == Composition::DISPLAY_DECORATION
719 ? outputState.transform.transform(
720 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
721 : Region();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800722}
723
724void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
725 // The base class does nothing with this call.
726}
727
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800728void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700729 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800730 layer->getLayerFE().prepareCompositionState(
731 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
732 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800733 }
734}
735
Dan Stoza269dc4d2021-01-15 15:07:43 -0800736void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800737 ATRACE_CALL();
738 ALOGV(__FUNCTION__);
739
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800740 if (!getState().isEnabled) {
741 return;
742 }
743
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800744 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
745 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
746
Lloyd Pique01c77c12019-04-17 12:48:32 -0700747 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700748 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800749 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200750 forceClientComposition,
751 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800752
753 if (mLayerRequestingBackgroundBlur == layer) {
754 forceClientComposition = false;
755 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800756 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000757
758 updateCompositionStateForBorder(refreshArgs);
759}
760
761void Output::updateCompositionStateForBorder(
762 const compositionengine::CompositionRefreshArgs& refreshArgs) {
763 std::unordered_map<int32_t, const Region*> layerVisibleRegionMap;
764 // Store a map of layerId to their computed visible region.
765 for (auto* layer : getOutputLayersOrderedByZ()) {
766 int layerId = (layer->getLayerFE()).getSequence();
767 layerVisibleRegionMap[layerId] = &((layer->getState()).visibleRegion);
768 }
769 OutputCompositionState& outputCompositionState = editState();
770 outputCompositionState.borderInfoList.clear();
771 bool clientComposeTopLayer = false;
772 for (const auto& borderInfo : refreshArgs.borderInfoList) {
773 renderengine::BorderRenderInfo info;
774 for (const auto& id : borderInfo.layerIds) {
775 info.combinedRegion.orSelf(*(layerVisibleRegionMap[id]));
776 }
Tianhao Yao10cea3c2022-03-30 01:37:22 +0000777
778 if (!info.combinedRegion.isEmpty()) {
779 info.width = borderInfo.width;
780 info.color = borderInfo.color;
781 outputCompositionState.borderInfoList.emplace_back(std::move(info));
782 clientComposeTopLayer = true;
783 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000784 }
785
786 // In this situation we must client compose the top layer instead of using hwc
787 // because we want to draw the border above all else.
788 // This could potentially cause a bit of a performance regression if the top
789 // layer would have been rendered using hwc originally.
790 // TODO(b/227656283): Measure system's performance before enabling the border feature
791 if (clientComposeTopLayer) {
792 auto topLayer = getOutputLayerOrderedByZByIndex(getOutputLayerCount() - 1);
793 (topLayer->editState()).forceClientComposition = true;
794 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800795}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800796
Dan Stoza269dc4d2021-01-15 15:07:43 -0800797void Output::planComposition() {
798 if (!mPlanner || !getState().isEnabled) {
799 return;
800 }
801
802 ATRACE_CALL();
803 ALOGV(__FUNCTION__);
804
805 mPlanner->plan(getOutputLayersOrderedByZ());
806}
807
808void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
809 ATRACE_CALL();
810 ALOGV(__FUNCTION__);
811
812 if (!getState().isEnabled) {
813 return;
814 }
815
Ady Abraham3645e642021-04-20 18:39:00 -0700816 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700817 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham43065bd2021-12-10 17:22:15 -0800818 editState().expectedPresentTime = refreshArgs.expectedPresentTime;
Ady Abraham3645e642021-04-20 18:39:00 -0700819
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400820 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800821 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400822 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400823 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400824 bool overrideZ = false;
Robert Carrec8ccca2022-05-04 09:36:14 -0700825 uint64_t outputLayerHash = 0;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800826 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400827 if (layer == peekThroughLayer) {
828 // No longer needed, although it should not show up again, so
829 // resetting it is not truly needed either.
830 peekThroughLayer = nullptr;
831
832 // peekThroughLayer was already drawn ahead of its z order.
833 continue;
834 }
Dan Stoza6166c312021-01-15 16:34:05 -0800835 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400836 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400837 if (overrideInfo.buffer != nullptr) {
838 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800839 ALOGV("Skipping redundant buffer");
840 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400841 } else {
842 // First layer with the override buffer.
843 if (overrideInfo.peekThroughLayer) {
844 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400845
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400846 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400847 overrideZ = true;
848 includeGeometry = true;
849 constexpr bool isPeekingThrough = true;
850 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
851 isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700852 outputLayerHash ^= android::hashCombine(
853 reinterpret_cast<uint64_t>(&peekThroughLayer->getLayerFE()),
854 z, includeGeometry, overrideZ, isPeekingThrough,
855 peekThroughLayer->requiresClientComposition());
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400856 }
857
858 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800859 }
Dan Stoza6166c312021-01-15 16:34:05 -0800860 }
861
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400862 constexpr bool isPeekingThrough = false;
863 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700864 if (!skipLayer) {
865 outputLayerHash ^= android::hashCombine(
866 reinterpret_cast<uint64_t>(&layer->getLayerFE()),
867 z, includeGeometry, overrideZ, isPeekingThrough,
868 layer->requiresClientComposition());
869 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800870 }
Robert Carrec8ccca2022-05-04 09:36:14 -0700871 editState().outputLayerHash = outputLayerHash;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800872}
873
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800874compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
875 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
876 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100877 auto* compState = layer->getLayerFE().getCompositionState();
878
879 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
880 // want to force client composition because of the blur.
881 if (compState->sidebandStream != nullptr) {
882 return nullptr;
883 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000884 if (compState->isOpaque) {
885 continue;
886 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100887 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800888 layerRequestingBgComposition = layer;
889 }
890 }
891 return layerRequestingBgComposition;
892}
893
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800894void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
895 setColorProfile(pickColorProfile(refreshArgs));
896}
897
898// Returns a data space that fits all visible layers. The returned data space
899// can only be one of
900// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
901// - Dataspace::DISPLAY_P3
902// - Dataspace::DISPLAY_BT2020
903// The returned HDR data space is one of
904// - Dataspace::UNKNOWN
905// - Dataspace::BT2020_HLG
906// - Dataspace::BT2020_PQ
907ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
908 bool* outIsHdrClientComposition) const {
909 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
910 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
911
Lloyd Pique01c77c12019-04-17 12:48:32 -0700912 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800913 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800914 case ui::Dataspace::V0_SCRGB:
915 case ui::Dataspace::V0_SCRGB_LINEAR:
916 case ui::Dataspace::BT2020:
917 case ui::Dataspace::BT2020_ITU:
918 case ui::Dataspace::BT2020_LINEAR:
919 case ui::Dataspace::DISPLAY_BT2020:
920 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
921 break;
922 case ui::Dataspace::DISPLAY_P3:
923 bestDataSpace = ui::Dataspace::DISPLAY_P3;
924 break;
925 case ui::Dataspace::BT2020_PQ:
926 case ui::Dataspace::BT2020_ITU_PQ:
927 bestDataSpace = ui::Dataspace::DISPLAY_P3;
928 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800929 *outIsHdrClientComposition =
930 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800931 break;
932 case ui::Dataspace::BT2020_HLG:
933 case ui::Dataspace::BT2020_ITU_HLG:
934 bestDataSpace = ui::Dataspace::DISPLAY_P3;
935 // When there's mixed PQ content and HLG content, we set the HDR
936 // data space to be BT2020_PQ and convert HLG to PQ.
937 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
938 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
939 }
940 break;
941 default:
942 break;
943 }
944 }
945
946 return bestDataSpace;
947}
948
949compositionengine::Output::ColorProfile Output::pickColorProfile(
950 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
951 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
952 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
953 ui::RenderIntent::COLORIMETRIC,
954 refreshArgs.colorSpaceAgnosticDataspace};
955 }
956
957 ui::Dataspace hdrDataSpace;
958 bool isHdrClientComposition = false;
959 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
960
961 switch (refreshArgs.forceOutputColorMode) {
962 case ui::ColorMode::SRGB:
963 bestDataSpace = ui::Dataspace::V0_SRGB;
964 break;
965 case ui::ColorMode::DISPLAY_P3:
966 bestDataSpace = ui::Dataspace::DISPLAY_P3;
967 break;
968 default:
969 break;
970 }
971
972 // respect hdrDataSpace only when there is no legacy HDR support
973 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
974 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
975 if (isHdr) {
976 bestDataSpace = hdrDataSpace;
977 }
978
979 ui::RenderIntent intent;
980 switch (refreshArgs.outputColorSetting) {
981 case OutputColorSetting::kManaged:
982 case OutputColorSetting::kUnmanaged:
983 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
984 : ui::RenderIntent::COLORIMETRIC;
985 break;
986 case OutputColorSetting::kEnhanced:
987 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
988 break;
989 default: // vendor display color setting
990 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
991 break;
992 }
993
994 ui::ColorMode outMode;
995 ui::Dataspace outDataSpace;
996 ui::RenderIntent outRenderIntent;
997 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
998 &outRenderIntent);
999
1000 return ColorProfile{outMode, outDataSpace, outRenderIntent,
1001 refreshArgs.colorSpaceAgnosticDataspace};
1002}
1003
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001004void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001005 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001006 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -07001007 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001008 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001009
1010 // If nothing has changed (!dirty), don't recompose.
1011 // If something changed, but we don't currently have any visible layers,
1012 // and didn't when we last did a composition, then skip it this time.
1013 // The second rule does two things:
1014 // - When all layers are removed from a display, we'll emit one black
1015 // frame, then nothing more until we get new layers.
1016 // - When a display is created with a private layer stack, we won't
1017 // emit any black frames until a layer is added to the layer stack.
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001018 mMustRecompose = dirty && !(empty && wasEmpty);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001019
1020 const char flagPrefix[] = {'-', '+'};
1021 static_cast<void>(flagPrefix);
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001022 ALOGV("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __func__,
1023 mMustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
1024 flagPrefix[empty], flagPrefix[wasEmpty]);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001025
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001026 mRenderSurface->beginFrame(mMustRecompose);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001027
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001028 if (mMustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001029 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001030 }
1031}
1032
Lloyd Pique66d68602019-02-13 14:23:31 -08001033void Output::prepareFrame() {
1034 ATRACE_CALL();
1035 ALOGV(__FUNCTION__);
1036
Vishnu Naira3140382022-02-24 14:07:11 -08001037 auto& outputState = editState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001038 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -08001039 return;
1040 }
1041
Vishnu Naira3140382022-02-24 14:07:11 -08001042 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1043 bool success = chooseCompositionStrategy(&changes);
1044 resetCompositionStrategy();
Vishnu Nair9cf89262022-02-26 09:17:49 -08001045 outputState.strategyPrediction = CompositionStrategyPredictionState::DISABLED;
Vishnu Naira3140382022-02-24 14:07:11 -08001046 outputState.previousDeviceRequestedChanges = changes;
1047 outputState.previousDeviceRequestedSuccess = success;
1048 if (success) {
1049 applyCompositionStrategy(changes);
1050 }
1051 finishPrepareFrame();
1052}
Lloyd Pique66d68602019-02-13 14:23:31 -08001053
Vishnu Naira3140382022-02-24 14:07:11 -08001054std::future<bool> Output::chooseCompositionStrategyAsync(
1055 std::optional<android::HWComposer::DeviceRequestedChanges>* changes) {
1056 return mHwComposerAsyncWorker->send(
1057 [&, changes]() { return chooseCompositionStrategy(changes); });
1058}
1059
1060GpuCompositionResult Output::prepareFrameAsync(const CompositionRefreshArgs& refreshArgs) {
1061 ATRACE_CALL();
1062 ALOGV(__FUNCTION__);
1063 auto& state = editState();
1064 const auto& previousChanges = state.previousDeviceRequestedChanges;
1065 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1066 resetCompositionStrategy();
1067 auto hwcResult = chooseCompositionStrategyAsync(&changes);
1068 if (state.previousDeviceRequestedSuccess) {
1069 applyCompositionStrategy(previousChanges);
1070 }
1071 finishPrepareFrame();
1072
1073 base::unique_fd bufferFence;
1074 std::shared_ptr<renderengine::ExternalTexture> buffer;
1075 updateProtectedContentState();
1076 const bool dequeueSucceeded = dequeueRenderBuffer(&bufferFence, &buffer);
1077 GpuCompositionResult compositionResult;
1078 if (dequeueSucceeded) {
1079 std::optional<base::unique_fd> optFd =
1080 composeSurfaces(Region::INVALID_REGION, refreshArgs, buffer, bufferFence);
1081 if (optFd) {
1082 compositionResult.fence = std::move(*optFd);
1083 }
Dan Stoza47437bb2021-01-15 16:21:07 -08001084 }
1085
Vishnu Naira3140382022-02-24 14:07:11 -08001086 auto chooseCompositionSuccess = hwcResult.get();
1087 const bool predictionSucceeded = dequeueSucceeded && changes == previousChanges;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001088 state.strategyPrediction = predictionSucceeded ? CompositionStrategyPredictionState::SUCCESS
1089 : CompositionStrategyPredictionState::FAIL;
Vishnu Naira3140382022-02-24 14:07:11 -08001090 if (!predictionSucceeded) {
1091 ATRACE_NAME("CompositionStrategyPredictionMiss");
1092 resetCompositionStrategy();
1093 if (chooseCompositionSuccess) {
1094 applyCompositionStrategy(changes);
1095 }
1096 finishPrepareFrame();
1097 // Track the dequeued buffer to reuse so we don't need to dequeue another one.
1098 compositionResult.buffer = buffer;
1099 } else {
1100 ATRACE_NAME("CompositionStrategyPredictionHit");
1101 }
1102 state.previousDeviceRequestedChanges = std::move(changes);
1103 state.previousDeviceRequestedSuccess = chooseCompositionSuccess;
1104 return compositionResult;
Lloyd Pique66d68602019-02-13 14:23:31 -08001105}
1106
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001107void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1108 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
1109 return;
1110 }
1111
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001112 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001113 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Vishnu Naira3140382022-02-24 14:07:11 -08001114 base::unique_fd bufferFence;
1115 std::shared_ptr<renderengine::ExternalTexture> buffer;
1116 updateProtectedContentState();
1117 dequeueRenderBuffer(&bufferFence, &buffer);
1118 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs, buffer, bufferFence));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001119 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001120 }
1121 }
1122
1123 postFramebuffer();
1124
1125 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
1126
1127 prepareFrame();
1128}
1129
Vishnu Naira3140382022-02-24 14:07:11 -08001130void Output::finishFrame(const CompositionRefreshArgs& refreshArgs, GpuCompositionResult&& result) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001131 ATRACE_CALL();
1132 ALOGV(__FUNCTION__);
Vishnu Nair9cf89262022-02-26 09:17:49 -08001133 const auto& outputState = getState();
1134 if (!outputState.isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001135 return;
1136 }
1137
Vishnu Naira3140382022-02-24 14:07:11 -08001138 std::optional<base::unique_fd> optReadyFence;
1139 std::shared_ptr<renderengine::ExternalTexture> buffer;
1140 base::unique_fd bufferFence;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001141 if (outputState.strategyPrediction == CompositionStrategyPredictionState::SUCCESS) {
Vishnu Naira3140382022-02-24 14:07:11 -08001142 optReadyFence = std::move(result.fence);
1143 } else {
1144 if (result.bufferAvailable()) {
1145 buffer = std::move(result.buffer);
1146 bufferFence = std::move(result.fence);
1147 } else {
1148 updateProtectedContentState();
1149 if (!dequeueRenderBuffer(&bufferFence, &buffer)) {
1150 return;
1151 }
1152 }
1153 // Repaint the framebuffer (if needed), getting the optional fence for when
1154 // the composition completes.
1155 optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs, buffer, bufferFence);
1156 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001157 if (!optReadyFence) {
1158 return;
1159 }
1160
Matt Buckley50c44062022-01-17 20:48:10 +00001161 if (isPowerHintSessionEnabled()) {
1162 // get fence end time to know when gpu is complete in display
Ady Abrahamd11bade2022-08-01 16:18:03 -07001163 setHintSessionGpuFence(
1164 std::make_unique<FenceTime>(sp<Fence>::make(dup(optReadyFence->get()))));
Matt Buckley50c44062022-01-17 20:48:10 +00001165 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001166 // swap buffers (presentation)
1167 mRenderSurface->queueBuffer(std::move(*optReadyFence));
1168}
1169
Vishnu Naira3140382022-02-24 14:07:11 -08001170void Output::updateProtectedContentState() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001171 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001172 auto& renderEngine = getCompositionEngine().getRenderEngine();
1173 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1174
1175 // If we the display is secure, protected content support is enabled, and at
1176 // least one layer has protected content, we need to use a secure back
1177 // buffer.
1178 if (outputState.isSecure && supportsProtectedContent) {
1179 auto layers = getOutputLayersOrderedByZ();
1180 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1181 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1182 });
1183 if (needsProtected != renderEngine.isProtected()) {
1184 renderEngine.useProtectedContext(needsProtected);
1185 }
1186 if (needsProtected != mRenderSurface->isProtected() &&
1187 needsProtected == renderEngine.isProtected()) {
1188 mRenderSurface->setProtected(needsProtected);
1189 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001190 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1191 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001192 }
Vishnu Naira3140382022-02-24 14:07:11 -08001193}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001194
Vishnu Naira3140382022-02-24 14:07:11 -08001195bool Output::dequeueRenderBuffer(base::unique_fd* bufferFence,
1196 std::shared_ptr<renderengine::ExternalTexture>* tex) {
1197 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001198
1199 // If we aren't doing client composition on this output, but do have a
1200 // flipClientTarget request for this frame on this output, we still need to
1201 // dequeue a buffer.
Vishnu Naira3140382022-02-24 14:07:11 -08001202 if (outputState.usesClientComposition || outputState.flipClientTarget) {
1203 *tex = mRenderSurface->dequeueBuffer(bufferFence);
1204 if (*tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001205 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1206 "client composition for this frame",
1207 mName.c_str());
Vishnu Naira3140382022-02-24 14:07:11 -08001208 return false;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001209 }
1210 }
Vishnu Naira3140382022-02-24 14:07:11 -08001211 return true;
1212}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001213
Vishnu Naira3140382022-02-24 14:07:11 -08001214std::optional<base::unique_fd> Output::composeSurfaces(
1215 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs,
1216 std::shared_ptr<renderengine::ExternalTexture> tex, base::unique_fd& fd) {
1217 ATRACE_CALL();
1218 ALOGV(__FUNCTION__);
1219
1220 const auto& outputState = getState();
1221 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
1222 outputState.usesClientComposition};
Lloyd Pique688abd42019-02-15 15:42:24 -08001223 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001224 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001225 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001226 }
1227
Vishnu Naira3140382022-02-24 14:07:11 -08001228 if (tex == nullptr) {
1229 ALOGW("Buffer not valid for display [%s], bailing out of "
1230 "client composition for this frame",
1231 mName.c_str());
1232 return {};
1233 }
1234
Lloyd Pique688abd42019-02-15 15:42:24 -08001235 ALOGV("hasClientComposition");
1236
Lloyd Pique688abd42019-02-15 15:42:24 -08001237 renderengine::DisplaySettings clientCompositionDisplay;
Angel Aguayob084e0c2021-08-04 23:27:28 +00001238 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1239 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001240 clientCompositionDisplay.orientation =
Angel Aguayob084e0c2021-08-04 23:27:28 +00001241 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001242 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1243 ? outputState.dataspace
1244 : ui::Dataspace::UNKNOWN;
John Reckac09e452021-04-07 16:35:37 -04001245
1246 // If we have a valid current display brightness use that, otherwise fall back to the
1247 // display's max desired
Alec Mourib21d94e2022-01-13 17:44:10 -08001248 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
John Reckac09e452021-04-07 16:35:37 -04001249 ? outputState.displayBrightnessNits
1250 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mourib21d94e2022-01-13 17:44:10 -08001251 clientCompositionDisplay.maxLuminance =
1252 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mourif8d093d2022-02-10 15:16:59 -08001253 clientCompositionDisplay.targetLuminanceNits =
1254 outputState.clientTargetBrightness * outputState.displayBrightnessNits;
Alec Mouri85065692022-03-18 00:58:26 +00001255 clientCompositionDisplay.dimmingStage = outputState.clientTargetDimmingStage;
Alec Mourifcedb9c2022-04-11 20:02:17 +00001256 clientCompositionDisplay.renderIntent =
1257 static_cast<aidl::android::hardware::graphics::composer3::RenderIntent>(
1258 outputState.renderIntent);
Lloyd Pique688abd42019-02-15 15:42:24 -08001259
1260 // Compute the global color transform matrix.
Leon Scroggins III745dcaa2022-01-26 11:55:58 -05001261 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Tianhao Yao67dd7122022-02-22 17:48:33 +00001262 for (auto& info : outputState.borderInfoList) {
1263 renderengine::BorderRenderInfo borderInfo;
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001264 borderInfo.width = info.width;
1265 borderInfo.color = info.color;
Tianhao Yao67dd7122022-02-22 17:48:33 +00001266 borderInfo.combinedRegion = info.combinedRegion;
1267 clientCompositionDisplay.borderInfoList.emplace_back(std::move(borderInfo));
1268 }
Leon Scroggins III745dcaa2022-01-26 11:55:58 -05001269 clientCompositionDisplay.deviceHandlesColorTransform =
1270 outputState.usesDeviceComposition || getSkipColorTransform();
Lloyd Pique688abd42019-02-15 15:42:24 -08001271
Lloyd Pique688abd42019-02-15 15:42:24 -08001272 // Generate the client composition requests for the layers on this output.
Vishnu Naira3140382022-02-24 14:07:11 -08001273 auto& renderEngine = getCompositionEngine().getRenderEngine();
1274 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
Robert Carrccab4242021-09-28 16:53:03 -07001275 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001276 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001277 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001278 clientCompositionDisplay.outputDataspace,
1279 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001280 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1281
Vishnu Naira3140382022-02-24 14:07:11 -08001282 OutputCompositionState& outputCompositionState = editState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001283 // Check if the client composition requests were rendered into the provided graphic buffer. If
1284 // so, we can reuse the buffer and avoid client composition.
1285 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001286 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1287 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001288 clientCompositionLayers)) {
Vishnu Naira3140382022-02-24 14:07:11 -08001289 ATRACE_NAME("ClientCompositionCacheHit");
Vishnu Nair9b079a22020-01-21 14:36:08 -08001290 outputCompositionState.reusedClientComposition = true;
1291 setExpensiveRenderingExpected(false);
Vishnu Nair3a49f0a2022-07-29 21:52:53 +00001292 // b/239944175 pass the fence associated with the buffer.
1293 return base::unique_fd(std::move(fd));
Vishnu Nair9b079a22020-01-21 14:36:08 -08001294 }
Vishnu Naira3140382022-02-24 14:07:11 -08001295 ATRACE_NAME("ClientCompositionCacheMiss");
Alec Mouria90a5702021-04-16 16:36:21 +00001296 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001297 clientCompositionLayers);
1298 }
1299
Lloyd Pique688abd42019-02-15 15:42:24 -08001300 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001301 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1302 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1303 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001304 const bool expensiveBlurs =
1305 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Leon Scroggins IIIcf17ebc2022-03-03 14:54:00 -05001306 const bool expensiveRenderingExpected = expensiveBlurs ||
1307 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1308 [outputDataspace =
1309 clientCompositionDisplay.outputDataspace](const auto& layer) {
1310 return layer.sourceDataspace != outputDataspace;
1311 });
Lloyd Pique688abd42019-02-15 15:42:24 -08001312 if (expensiveRenderingExpected) {
1313 setExpensiveRenderingExpected(true);
1314 }
1315
Sally Qi59a9f502021-10-12 18:53:23 +00001316 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1317 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001318 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001319 std::back_inserter(clientRenderEngineLayers),
1320 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1321 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001322 });
1323
Alec Mourie4034bb2019-11-19 12:45:54 -08001324 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001325 // Only use the framebuffer cache when rendering to an internal display
1326 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1327 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1328 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1329 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1330 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001331 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001332
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001333 auto fenceResult =
1334 toFenceResult(renderEngine
1335 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers,
1336 tex, useFramebufferCache, std::move(fd))
1337 .get());
1338
1339 if (mClientCompositionRequestCache && fenceStatus(fenceResult) != NO_ERROR) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001340 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001341 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001342 }
1343
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001344 const auto fence = std::move(fenceResult).value_or(Fence::NO_FENCE);
1345
1346 if (auto& timeStats = getCompositionEngine().getTimeStats(); fence->isValid()) {
1347 timeStats.recordRenderEngineDuration(renderEngineStart, std::make_shared<FenceTime>(fence));
Alec Mourie4034bb2019-11-19 12:45:54 -08001348 } else {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001349 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
Alec Mourie4034bb2019-11-19 12:45:54 -08001350 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001351
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001352 for (auto* clientComposedLayer : clientCompositionLayersFE) {
1353 clientComposedLayer->setWasClientComposed(fence);
Robert Carrccab4242021-09-28 16:53:03 -07001354 }
1355
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001356 return base::unique_fd(fence->dup());
Lloyd Pique688abd42019-02-15 15:42:24 -08001357}
1358
Vishnu Nair9b079a22020-01-21 14:36:08 -08001359std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001360 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001361 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001362 ALOGV("Rendering client layers");
1363
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001364 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001365 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001366 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001367
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001368 bool disableBlurs = false;
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001369 uint64_t previousOverrideBufferId = 0;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001370
Lloyd Pique01c77c12019-04-17 12:48:32 -07001371 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001372 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001373 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001374 auto& layerFE = layer->getLayerFE();
Robert Carr05da0082022-05-25 23:29:34 -07001375 layerFE.setWasClientComposed(nullptr);
Lloyd Pique688abd42019-02-15 15:42:24 -08001376
Lloyd Piquea2468662019-03-07 21:31:06 -08001377 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001378 ALOGV("Layer: %s", layerFE.getDebugName());
1379 if (clip.isEmpty()) {
1380 ALOGV(" Skipping for empty clip");
1381 firstLayer = false;
1382 continue;
1383 }
1384
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001385 disableBlurs |= layerFEState->sidebandStream != nullptr;
1386
Vishnu Naira483b4a2019-12-12 15:07:52 -08001387 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001388
1389 // We clear the client target for non-client composed layers if
1390 // requested by the HWC. We skip this if the layer is not an opaque
1391 // rectangle, as by definition the layer must blend with whatever is
1392 // underneath. We also skip the first layer as the buffer target is
1393 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001394 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001395 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001396
1397 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1398
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001399 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1400 // composing the non-shadow content and only draw the shadows.
1401 const bool realContentIsVisible = clientComposition &&
1402 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1403
Lloyd Pique688abd42019-02-15 15:42:24 -08001404 if (clientComposition || clearClientComposition) {
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001405 if (auto overrideSettings = layer->getOverrideCompositionSettings()) {
1406 if (overrideSettings->bufferId != previousOverrideBufferId) {
1407 previousOverrideBufferId = overrideSettings->bufferId;
1408 clientCompositionLayers.push_back(std::move(*overrideSettings));
Huihong Luo91ac3b52021-04-08 11:07:41 -07001409 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1410 } else {
1411 ALOGV("Skipping redundant override buffer for [%s] in RE",
1412 layer->getLayerFE().getDebugName());
1413 }
Dan Stoza6166c312021-01-15 16:34:05 -08001414 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001415 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1416 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1417 : (layer->getState().overrideInfo.disableBackgroundBlur
1418 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1419 BlurRegionsOnly
1420 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1421 Enabled);
1422 compositionengine::LayerFE::ClientCompositionTargetSettings
1423 targetSettings{.clip = clip,
1424 .needsFiltering = layer->needsFiltering() ||
1425 outputState.needsFiltering,
1426 .isSecure = outputState.isSecure,
1427 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001428 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001429 .dataspace = outputDataspace,
1430 .realContentIsVisible = realContentIsVisible,
1431 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001432 .blurSetting = blurSetting,
1433 .whitePointNits = layerState.whitePointNits};
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001434 if (auto clientCompositionSettings =
1435 layerFE.prepareClientComposition(targetSettings)) {
1436 clientCompositionLayers.push_back(std::move(*clientCompositionSettings));
1437 if (realContentIsVisible) {
1438 layer->editState().clientCompositionTimestamp = systemTime();
1439 }
Dan Stoza6166c312021-01-15 16:34:05 -08001440 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001441 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001442
Tianhua Sunf91f1402022-05-09 05:45:46 +00001443 if (clientComposition) {
1444 outLayerFEs.push_back(&layerFE);
1445 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001446 }
1447
1448 firstLayer = false;
1449 }
1450
1451 return clientCompositionLayers;
1452}
1453
1454void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001455 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001456 if (flashRegion.isEmpty()) {
1457 return;
1458 }
1459
Vishnu Nair9b079a22020-01-21 14:36:08 -08001460 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001461 layerSettings.source.buffer.buffer = nullptr;
1462 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1463 layerSettings.alpha = half(1.0);
1464
1465 for (const auto& rect : flashRegion) {
1466 layerSettings.geometry.boundaries = rect.toFloatRect();
1467 clientCompositionLayers.push_back(layerSettings);
1468 }
1469}
1470
1471void Output::setExpensiveRenderingExpected(bool) {
1472 // The base class does nothing with this call.
1473}
1474
Matt Buckley50c44062022-01-17 20:48:10 +00001475void Output::setHintSessionGpuFence(std::unique_ptr<FenceTime>&&) {
1476 // The base class does nothing with this call.
1477}
1478
1479bool Output::isPowerHintSessionEnabled() {
1480 return false;
1481}
1482
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001483void Output::postFramebuffer() {
1484 ATRACE_CALL();
1485 ALOGV(__FUNCTION__);
1486
1487 if (!getState().isEnabled) {
1488 return;
1489 }
1490
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001491 auto& outputState = editState();
1492 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001493
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001494 auto frame = presentAndGetFrameFences();
1495
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001496 mRenderSurface->onPresentDisplayCompleted();
1497
Lloyd Pique01c77c12019-04-17 12:48:32 -07001498 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001499 // The layer buffer from the previous frame (if any) is released
1500 // by HWC only when the release fence from this frame (if any) is
1501 // signaled. Always get the release fence from HWC first.
1502 sp<Fence> releaseFence = Fence::NO_FENCE;
1503
1504 if (auto hwcLayer = layer->getHwcLayer()) {
1505 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1506 releaseFence = f->second;
1507 }
1508 }
1509
1510 // If the layer was client composited in the previous frame, we
1511 // need to merge with the previous client target acquire fence.
1512 // Since we do not track that, always merge with the current
1513 // client target acquire fence when it is available, even though
1514 // this is suboptimal.
1515 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001516 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001517 releaseFence =
1518 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1519 }
Sally Qi59a9f502021-10-12 18:53:23 +00001520 layer->getLayerFE().onLayerDisplayed(
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001521 ftl::yield<FenceResult>(std::move(releaseFence)).share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001522 }
1523
1524 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001525 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001526 // supply them with the present fence.
1527 for (auto& weakLayer : mReleasedLayers) {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001528 if (const auto layer = weakLayer.promote()) {
1529 layer->onLayerDisplayed(ftl::yield<FenceResult>(frame.presentFence).share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001530 }
1531 }
1532
1533 // Clear out the released layers now that we're done with them.
1534 mReleasedLayers.clear();
1535}
1536
Alec Mouriaa831582021-06-07 16:23:01 -07001537void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001538 if (mPlanner) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -07001539 mPlanner->renderCachedSets(getState(), refreshArgs.scheduledFrameTime);
Dan Stoza6166c312021-01-15 16:34:05 -08001540 }
1541}
1542
Lloyd Pique32cbe282018-10-19 13:09:22 -07001543void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001544 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001545 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001546}
1547
Vishnu Naira3140382022-02-24 14:07:11 -08001548void Output::resetCompositionStrategy() {
Lloyd Pique66d68602019-02-13 14:23:31 -08001549 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001550 auto& outputState = editState();
1551 outputState.usesClientComposition = true;
1552 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001553 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001554}
1555
Lloyd Pique688abd42019-02-15 15:42:24 -08001556bool Output::getSkipColorTransform() const {
1557 return true;
1558}
1559
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001560compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1561 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001562 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001563 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1564 }
1565 return result;
1566}
1567
Vishnu Naira3140382022-02-24 14:07:11 -08001568void Output::setPredictCompositionStrategy(bool predict) {
1569 if (predict) {
1570 mHwComposerAsyncWorker = std::make_unique<HwcAsyncWorker>();
1571 } else {
1572 mHwComposerAsyncWorker.reset(nullptr);
1573 }
1574}
1575
Alec Mouridda07d92022-04-25 22:39:25 +00001576void Output::setTreat170mAsSrgb(bool enable) {
1577 editState().treat170mAsSrgb = enable;
1578}
1579
Vishnu Naira3140382022-02-24 14:07:11 -08001580bool Output::canPredictCompositionStrategy(const CompositionRefreshArgs& refreshArgs) {
Robert Carrec8ccca2022-05-04 09:36:14 -07001581 uint64_t lastOutputLayerHash = getState().lastOutputLayerHash;
1582 uint64_t outputLayerHash = getState().outputLayerHash;
1583 editState().lastOutputLayerHash = outputLayerHash;
1584
Vishnu Naira3140382022-02-24 14:07:11 -08001585 if (!getState().isEnabled || !mHwComposerAsyncWorker) {
1586 ALOGV("canPredictCompositionStrategy disabled");
1587 return false;
1588 }
1589
1590 if (!getState().previousDeviceRequestedChanges) {
1591 ALOGV("canPredictCompositionStrategy previous changes not available");
1592 return false;
1593 }
1594
1595 if (!mRenderSurface->supportsCompositionStrategyPrediction()) {
1596 ALOGV("canPredictCompositionStrategy surface does not support");
1597 return false;
1598 }
1599
1600 if (refreshArgs.devOptFlashDirtyRegionsDelay) {
1601 ALOGV("canPredictCompositionStrategy devOptFlashDirtyRegionsDelay");
1602 return false;
1603 }
1604
Robert Carrec8ccca2022-05-04 09:36:14 -07001605 if (lastOutputLayerHash != outputLayerHash) {
1606 ALOGV("canPredictCompositionStrategy output layers changed");
1607 return false;
1608 }
1609
Vishnu Naira3140382022-02-24 14:07:11 -08001610 // If no layer uses clientComposition, then don't predict composition strategy
1611 // because we have less work to do in parallel.
1612 if (!anyLayersRequireClientComposition()) {
1613 ALOGV("canPredictCompositionStrategy no layer uses clientComposition");
1614 return false;
1615 }
1616
Robert Carrec8ccca2022-05-04 09:36:14 -07001617 return true;
Vishnu Naira3140382022-02-24 14:07:11 -08001618}
1619
1620bool Output::anyLayersRequireClientComposition() const {
1621 const auto layers = getOutputLayersOrderedByZ();
1622 return std::any_of(layers.begin(), layers.end(),
1623 [](const auto& layer) { return layer->requiresClientComposition(); });
1624}
1625
1626void Output::finishPrepareFrame() {
1627 const auto& state = getState();
1628 if (mPlanner) {
1629 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
1630 }
1631 mRenderSurface->prepareFrame(state.usesClientComposition, state.usesDeviceComposition);
1632}
1633
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001634bool Output::mustRecompose() const {
1635 return mMustRecompose;
1636}
1637
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001638} // namespace impl
1639} // namespace android::compositionengine