blob: 945c06b34bb283fda6bb5633145ff5064af448a8 [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>
Melody Hsu793f8362024-01-08 20:00:35 +000019#include <common/FlagManager.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070020#include <compositionengine/CompositionEngine.h>
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080021#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070022#include <compositionengine/DisplayColorProfile.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080023#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070024#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070025#include <compositionengine/RenderSurface.h>
daniml39d6a2162021-05-19 15:56:21 +020026#include <compositionengine/UdfpsExtension.h>
Vishnu Naira3140382022-02-24 14:07:11 -080027#include <compositionengine/impl/HwcAsyncWorker.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070028#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070029#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080030#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070031#include <compositionengine/impl/OutputLayerCompositionState.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080032#include <compositionengine/impl/planner/Planner.h>
Leon Scroggins III370b8b52022-12-08 13:20:45 -050033#include <ftl/algorithm.h>
Sally Qi59a9f502021-10-12 18:53:23 +000034#include <ftl/future.h>
Leon Scroggins III5a655b82022-09-07 13:17:09 -040035#include <gui/TraceUtils.h>
Leon Scroggins III370b8b52022-12-08 13:20:45 -050036#include <scheduler/FrameTargeter.h>
37#include <scheduler/Time.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080038
Chavi Weingarten545da0e2023-02-09 14:55:57 +000039#include <optional>
Alec Mouria90a5702021-04-16 16:36:21 +000040#include <thread>
41
42#include "renderengine/ExternalTexture.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 push
46#pragma clang diagnostic ignored "-Wconversion"
47
Lloyd Pique688abd42019-02-15 15:42:24 -080048#include <renderengine/DisplaySettings.h>
49#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080050
51// TODO(b/129481165): remove the #pragma below and fix conversion issues
52#pragma clang diagnostic pop // ignored "-Wconversion"
53
Dan Stoza269dc4d2021-01-15 15:07:43 -080054#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070055#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080056#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080057#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070058
Lloyd Pique688abd42019-02-15 15:42:24 -080059#include "TracedOrdinal.h"
60
Leon Scroggins III9a0afda2022-01-11 16:53:09 -050061using aidl::android::hardware::graphics::composer3::Composition;
62
Lloyd Piquefeb73d72018-12-04 17:23:44 -080063namespace android::compositionengine {
64
65Output::~Output() = default;
66
67namespace impl {
Vishnu Nair9cf89262022-02-26 09:17:49 -080068using CompositionStrategyPredictionState =
69 OutputCompositionState::CompositionStrategyPredictionState;
Lloyd Piquec29e4c62019-03-07 21:48:19 -080070namespace {
71
72template <typename T>
73class Reversed {
74public:
75 explicit Reversed(const T& container) : mContainer(container) {}
76 auto begin() { return mContainer.rbegin(); }
77 auto end() { return mContainer.rend(); }
78
79private:
80 const T& mContainer;
81};
82
83// Helper for enumerating over a container in reverse order
84template <typename T>
85Reversed<T> reversed(const T& c) {
86 return Reversed<T>(c);
87}
88
Marin Shalamanovb15d2272020-09-17 21:41:52 +020089struct ScaleVector {
90 float x;
91 float y;
92};
93
94// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
95// where to' will have the same size as "to". In the case where "from" and "to"
96// start at the origin to'=to.
97ScaleVector getScale(const Rect& from, const Rect& to) {
98 return {.x = static_cast<float>(to.width()) / from.width(),
99 .y = static_cast<float>(to.height()) / from.height()};
100}
101
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800102} // namespace
103
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700104std::shared_ptr<Output> createOutput(
105 const compositionengine::CompositionEngine& compositionEngine) {
106 return createOutputTemplated<Output>(compositionEngine);
107}
Lloyd Pique32cbe282018-10-19 13:09:22 -0700108
109Output::~Output() = default;
110
Lloyd Pique32cbe282018-10-19 13:09:22 -0700111bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700112 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
113 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700114}
115
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700116std::optional<DisplayId> Output::getDisplayId() const {
117 return {};
118}
119
Lloyd Pique32cbe282018-10-19 13:09:22 -0700120const std::string& Output::getName() const {
121 return mName;
122}
123
124void Output::setName(const std::string& name) {
125 mName = name;
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400126 auto displayIdOpt = getDisplayId();
Leon Scroggins IIIc03d4652023-01-05 13:03:53 -0500127 mNamePlusId = displayIdOpt ? base::StringPrintf("%s (%s)", mName.c_str(),
128 to_string(*displayIdOpt).c_str())
129 : mName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700130}
131
132void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700133 auto& outputState = editState();
134 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700135 return;
136 }
137
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700138 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700139 dirtyEntireOutput();
140}
141
Alec Mouri023c1882021-05-08 16:36:33 -0700142void Output::setLayerCachingEnabled(bool enabled) {
143 if (enabled == (mPlanner != nullptr)) {
144 return;
145 }
146
147 if (enabled) {
Alec Mouridf6201b2021-06-01 16:20:42 -0700148 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
Alec Mouri023c1882021-05-08 16:36:33 -0700149 if (mRenderSurface) {
150 mPlanner->setDisplaySize(mRenderSurface->getSize());
151 }
152 } else {
153 mPlanner.reset();
154 }
Alec Mouric773472b2021-05-19 14:29:05 -0700155
156 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
157 if (!outputLayer) {
158 continue;
159 }
160
161 outputLayer->editState().overrideInfo = {};
162 }
Alec Mouri023c1882021-05-08 16:36:33 -0700163}
164
Ady Abrahamdb036a82021-07-16 14:18:34 -0700165void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
166 if (mPlanner) {
167 mPlanner->setTexturePoolEnabled(enabled);
168 }
169}
170
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200171void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
172 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700173 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200174
Angel Aguayob084e0c2021-08-04 23:27:28 +0000175 outputState.displaySpace.setOrientation(orientation);
176 LOG_FATAL_IF(outputState.displaySpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200177 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200178
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200179 // Compute orientedDisplaySpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000180 ui::Size orientedSize = outputState.displaySpace.getBounds();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200181 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200182 std::swap(orientedSize.width, orientedSize.height);
183 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000184 outputState.orientedDisplaySpace.setBounds(orientedSize);
185 outputState.orientedDisplaySpace.setContent(orientedDisplaySpaceRect);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200186
187 // Compute displaySpace.content
188 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
189 ui::Transform rotation;
190 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000191 const auto displaySize = outputState.displaySpace.getBoundsAsRect();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200192 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
193 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000194 outputState.displaySpace.setContent(rotation.transform(orientedDisplaySpaceRect));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200195
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200196 // Compute framebufferSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000197 outputState.framebufferSpace.setOrientation(orientation);
198 LOG_FATAL_IF(outputState.framebufferSpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200199 "The framebuffer bounds are unknown.");
Angel Aguayob084e0c2021-08-04 23:27:28 +0000200 const auto scale = getScale(outputState.displaySpace.getBoundsAsRect(),
201 outputState.framebufferSpace.getBoundsAsRect());
202 outputState.framebufferSpace.setContent(
203 outputState.displaySpace.getContent().scale(scale.x, scale.y));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200204
205 // Compute layerStackSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000206 outputState.layerStackSpace.setContent(layerStackSpaceRect);
207 outputState.layerStackSpace.setBounds(
208 ui::Size(layerStackSpaceRect.getWidth(), layerStackSpaceRect.getHeight()));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200209
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200210 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
211 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700212 dirtyEntireOutput();
213}
214
Alec Mouricdf16792021-12-10 13:16:06 -0800215void Output::setNextBrightness(float brightness) {
216 editState().displayBrightness = brightness;
217}
218
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200219void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700220 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200221
222 auto& state = editState();
223
224 // Update framebuffer space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000225 const ui::Size newBounds(size);
226 state.framebufferSpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200227
228 // Update display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000229 state.displaySpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200230 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
231
232 // Update oriented display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000233 const auto orientation = state.displaySpace.getOrientation();
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200234 ui::Size orientedSize = size;
235 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
236 std::swap(orientedSize.width, orientedSize.height);
237 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000238 const ui::Size newOrientedBounds(orientedSize);
239 state.orientedDisplaySpace.setBounds(newOrientedBounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700240
Dan Stoza6166c312021-01-15 16:34:05 -0800241 if (mPlanner) {
242 mPlanner->setDisplaySize(size);
243 }
244
Lloyd Pique32cbe282018-10-19 13:09:22 -0700245 dirtyEntireOutput();
246}
247
Garfield Tan54edd912020-10-21 16:31:41 -0700248ui::Transform::RotationFlags Output::getTransformHint() const {
249 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
250}
251
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700252void Output::setLayerFilter(ui::LayerFilter filter) {
253 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700254 dirtyEntireOutput();
255}
256
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800257void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700258 auto& colorTransformMatrix = editState().colorTransformMatrix;
259 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700260 return;
261 }
262
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700263 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800264
265 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700266}
267
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800268void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700269 auto& outputState = editState();
270 if (outputState.colorMode == colorProfile.mode &&
271 outputState.dataspace == colorProfile.dataspace &&
Alec Mouri88790f32023-07-21 01:25:14 +0000272 outputState.renderIntent == colorProfile.renderIntent) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800273 return;
274 }
275
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700276 outputState.colorMode = colorProfile.mode;
277 outputState.dataspace = colorProfile.dataspace;
278 outputState.renderIntent = colorProfile.renderIntent;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700279
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800280 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700281
Lloyd Pique32cbe282018-10-19 13:09:22 -0700282 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800283 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
284 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800285
286 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700287}
288
John Reckac09e452021-04-07 16:35:37 -0400289void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
290 auto& outputState = editState();
291 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
292 outputState.displayBrightnessNits == displayBrightnessNits) {
293 // Nothing changed
294 return;
295 }
296 outputState.sdrWhitePointNits = sdrWhitePointNits;
297 outputState.displayBrightnessNits = displayBrightnessNits;
298 dirtyEntireOutput();
299}
300
Lloyd Pique32cbe282018-10-19 13:09:22 -0700301void Output::dump(std::string& out) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700302 base::StringAppendF(&out, "Output \"%s\"", mName.c_str());
303 out.append("\n Composition Output State:\n");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700304
305 dumpBase(out);
306}
307
308void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700309 dumpState(out);
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700310 out += '\n';
Lloyd Pique31cb2942018-10-19 17:23:03 -0700311
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700312 if (mDisplayColorProfile) {
313 mDisplayColorProfile->dump(out);
314 } else {
315 out.append(" No display color profile!\n");
316 }
317
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700318 out += '\n';
319
Lloyd Pique31cb2942018-10-19 17:23:03 -0700320 if (mRenderSurface) {
321 mRenderSurface->dump(out);
322 } else {
323 out.append(" No render surface!\n");
324 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800325
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700326 base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
Lloyd Pique01c77c12019-04-17 12:48:32 -0700327 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800328 if (!outputLayer) {
329 continue;
330 }
331 outputLayer->dump(out);
332 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700333}
334
Dan Stoza269dc4d2021-01-15 15:07:43 -0800335void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
336 if (!mPlanner) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700337 out.append("Planner is disabled\n");
Dan Stoza269dc4d2021-01-15 15:07:43 -0800338 return;
339 }
340 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
341 mPlanner->dump(args, out);
342}
343
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700344compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
345 return mDisplayColorProfile.get();
346}
347
348void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
349 mDisplayColorProfile = std::move(mode);
350}
351
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800352const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
353 return mReleasedLayers;
354}
355
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700356void Output::setDisplayColorProfileForTest(
357 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
358 mDisplayColorProfile = std::move(mode);
359}
360
Lloyd Pique31cb2942018-10-19 17:23:03 -0700361compositionengine::RenderSurface* Output::getRenderSurface() const {
362 return mRenderSurface.get();
363}
364
365void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
366 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800367 const auto size = mRenderSurface->getSize();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000368 editState().framebufferSpace.setBounds(size);
Dan Stoza6166c312021-01-15 16:34:05 -0800369 if (mPlanner) {
370 mPlanner->setDisplaySize(size);
371 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700372 dirtyEntireOutput();
373}
374
Vishnu Nair9b079a22020-01-21 14:36:08 -0800375void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
376 if (cacheSize == 0) {
377 mClientCompositionRequestCache.reset();
378 } else {
379 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
380 }
381};
382
Lloyd Pique31cb2942018-10-19 17:23:03 -0700383void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
384 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700385}
386
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700387Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700388 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000389 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700390}
391
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700392bool Output::includesLayer(ui::LayerFilter filter) const {
393 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700394}
395
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700396bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800397 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700398 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800399}
400
Lloyd Piquedf336d92019-03-07 21:38:42 -0800401std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800402 const sp<LayerFE>& layerFE) const {
403 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800404}
405
Lloyd Piquede196652020-01-22 17:29:58 -0800406compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
407 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700408 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800409}
410
Lloyd Pique01c77c12019-04-17 12:48:32 -0700411std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800412 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700413 for (size_t i = 0; i < getOutputLayerCount(); i++) {
414 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800415 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700416 return i;
417 }
418 }
419 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800420}
421
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800422void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
423 mReleasedLayers = std::move(layers);
424}
425
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800426void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
427 LayerFESet& geomSnapshots) {
428 ATRACE_CALL();
429 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800430
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800431 rebuildLayerStacks(refreshArgs, geomSnapshots);
Brian Lindahl439afad2022-11-14 11:16:55 -0700432 uncacheBuffers(refreshArgs.bufferIdsToUncache);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800433}
434
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400435ftl::Future<std::monostate> Output::present(
436 const compositionengine::CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500437 const auto stringifyExpectedPresentTime = [this, &refreshArgs]() -> std::string {
438 return ftl::Optional(getDisplayId())
439 .and_then(PhysicalDisplayId::tryCast)
440 .and_then([&refreshArgs](PhysicalDisplayId id) {
441 return refreshArgs.frameTargets.get(id);
442 })
443 .transform([](const auto& frameTargetPtr) {
444 return frameTargetPtr.get()->expectedPresentTime();
445 })
446 .transform([](TimePoint expectedPresentTime) {
447 return base::StringPrintf(" vsyncIn %.2fms",
448 ticks<std::milli, float>(expectedPresentTime -
449 TimePoint::now()));
450 })
451 .or_else([] {
452 // There is no vsync for this output.
453 return std::make_optional(std::string());
454 })
455 .value();
456 };
457 ATRACE_FORMAT("%s for %s%s", __func__, mNamePlusId.c_str(),
458 stringifyExpectedPresentTime().c_str());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800459 ALOGV(__FUNCTION__);
460
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800461 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800462 updateCompositionState(refreshArgs);
463 planComposition();
464 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800465 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800466 beginFrame();
Vishnu Naira3140382022-02-24 14:07:11 -0800467
Xiang Wangaab31162024-03-12 19:48:08 -0700468 if (isPowerHintSessionEnabled()) {
469 // always reset the flag before the composition prediction
470 setHintSessionRequiresRenderEngine(false);
471 }
Vishnu Naira3140382022-02-24 14:07:11 -0800472 GpuCompositionResult result;
473 const bool predictCompositionStrategy = canPredictCompositionStrategy(refreshArgs);
474 if (predictCompositionStrategy) {
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000475 result = prepareFrameAsync();
Vishnu Naira3140382022-02-24 14:07:11 -0800476 } else {
477 prepareFrame();
478 }
479
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800480 devOptRepaintFlash(refreshArgs);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000481 finishFrame(std::move(result));
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400482 ftl::Future<std::monostate> future;
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400483 const bool flushEvenWhenDisabled = !refreshArgs.bufferIdsToUncache.empty();
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400484 if (mOffloadPresent) {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400485 future = presentFrameAndReleaseLayersAsync(flushEvenWhenDisabled);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400486
487 // Only offload for this frame. The next frame will determine whether it
488 // needs to be offloaded. Leave the HwcAsyncWorker in place. For one thing,
489 // it is currently presenting. Further, it may be needed next frame, and
490 // we don't want to churn.
491 mOffloadPresent = false;
492 } else {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400493 presentFrameAndReleaseLayers(flushEvenWhenDisabled);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400494 future = ftl::yield<std::monostate>({});
495 }
Alec Mouriaa831582021-06-07 16:23:01 -0700496 renderCachedSets(refreshArgs);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400497 return future;
498}
499
500void Output::offloadPresentNextFrame() {
501 mOffloadPresent = true;
502 updateHwcAsyncWorker();
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800503}
504
Brian Lindahl439afad2022-11-14 11:16:55 -0700505void Output::uncacheBuffers(std::vector<uint64_t> const& bufferIdsToUncache) {
506 if (bufferIdsToUncache.empty()) {
507 return;
508 }
509 for (auto outputLayer : getOutputLayersOrderedByZ()) {
510 outputLayer->uncacheBuffers(bufferIdsToUncache);
511 }
512}
513
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800514void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
515 LayerFESet& layerFESet) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700516 auto& outputState = editState();
517
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800518 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700519 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800520 return;
521 }
Vishnu Naird9a640b2023-07-21 14:20:27 +0000522 ATRACE_CALL();
523 ALOGV(__FUNCTION__);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800524
525 // Process the layers to determine visibility and coverage
526 compositionengine::Output::CoverageState coverage{layerFESet};
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000527 coverage.aboveCoveredLayersExcludingOverlays = refreshArgs.hasTrustedPresentationListener
528 ? std::make_optional<Region>()
529 : std::nullopt;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800530 collectVisibleLayers(refreshArgs, coverage);
531
532 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700533 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000534 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800535 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
536
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700537 outputState.undefinedRegion = undefinedRegion;
538 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800539}
540
541void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
542 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800543 // Evaluate the layers from front to back to determine what is visible. This
544 // also incrementally calculates the coverage information for each layer as
545 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800546 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700547 // Incrementally process the coverage for each layer
548 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800549
550 // TODO(b/121291683): Stop early if the output is completely covered and
551 // no more layers could even be visible underneath the ones on top.
552 }
553
Lloyd Pique01c77c12019-04-17 12:48:32 -0700554 setReleasedLayers(refreshArgs);
555
556 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800557}
558
Lloyd Piquede196652020-01-22 17:29:58 -0800559void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700560 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800561 // Ensure we have a snapshot of the basic geometry layer state. Limit the
562 // snapshots to once per frame for each candidate layer, as layers may
563 // appear on multiple outputs.
564 if (!coverage.latchedLayers.count(layerFE)) {
565 coverage.latchedLayers.insert(layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800566 }
567
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700568 // Only consider the layers on this output
569 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800570 return;
571 }
572
573 // Obtain a read-only pointer to the front-end layer state
574 const auto* layerFEState = layerFE->getCompositionState();
575 if (CC_UNLIKELY(!layerFEState)) {
576 return;
577 }
578
579 // handle hidden surfaces by setting the visible region to empty
580 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700581 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800582 }
583
Vishnu Naird47bcee2023-02-24 18:08:51 +0000584 bool computeAboveCoveredExcludingOverlays = coverage.aboveCoveredLayersExcludingOverlays &&
585 !layerFEState->outputFilter.toInternalDisplay;
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000586
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800587 /*
588 * opaqueRegion: area of a surface that is fully opaque.
589 */
590 Region opaqueRegion;
591
592 /*
593 * visibleRegion: area of a surface that is visible on screen and not fully
594 * transparent. This is essentially the layer's footprint minus the opaque
595 * regions above it. Areas covered by a translucent surface are considered
596 * visible.
597 */
598 Region visibleRegion;
599
600 /*
601 * coveredRegion: area of a surface that is covered by all visible regions
602 * above it (which includes the translucent areas).
603 */
604 Region coveredRegion;
605
606 /*
607 * transparentRegion: area of a surface that is hinted to be completely
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500608 * transparent.
609 * This is used to tell when the layer has no visible non-transparent
610 * regions and can be removed from the layer list. It does not affect the
611 * visibleRegion of this layer or any layers beneath it. The hint may not
612 * be correct if apps don't respect the SurfaceView restrictions (which,
613 * sadly, some don't).
614 *
615 * In addition, it is used on DISPLAY_DECORATION layers to specify the
616 * blockingRegion, allowing the DPU to skip it to save power. Once we have
617 * hardware that supports a blockingRegion on frames with AFBC, it may be
618 * useful to use this for other layers, too, so long as we can prevent
619 * regressions on b/7179570.
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800620 */
621 Region transparentRegion;
622
Vishnu Naira483b4a2019-12-12 15:07:52 -0800623 /*
624 * shadowRegion: Region cast by the layer's shadow.
625 */
626 Region shadowRegion;
627
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000628 /**
629 * covered region above excluding internal display overlay layers
630 */
631 std::optional<Region> coveredRegionExcludingDisplayOverlays = std::nullopt;
632
Lloyd Piquede196652020-01-22 17:29:58 -0800633 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800634
635 // Get the visible region
636 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
637 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800638 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800639 visibleRegion.set(visibleRect);
640
Vishnu Naird9e4f462023-10-06 04:05:45 +0000641 if (layerFEState->shadowSettings.length > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800642 // if the layer casts a shadow, offset the layers visible region and
643 // calculate the shadow region.
Vishnu Naird9e4f462023-10-06 04:05:45 +0000644 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowSettings.length) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800645 Rect visibleRectWithShadows(visibleRect);
646 visibleRectWithShadows.inset(inset, inset, inset, inset);
647 visibleRegion.set(visibleRectWithShadows);
648 shadowRegion = visibleRegion.subtract(visibleRect);
649 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800650
651 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700652 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800653 }
654
655 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800656 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800657 if (tr.preserveRects()) {
Alec Mourie60f0b92022-06-10 19:15:20 +0000658 // Clip the transparent region to geomLayerBounds first
659 // The transparent region may be influenced by applications, for
660 // instance, by overriding ViewGroup#gatherTransparentRegion with a
661 // custom view. Once the layer stack -> display mapping is known, we
662 // must guard against very wrong inputs to prevent underflow or
663 // overflow errors. We do this here by constraining the transparent
664 // region to be within the pre-transform layer bounds, since the
665 // layer bounds are expected to play nicely with the full
666 // transform.
667 const Region clippedTransparentRegionHint =
668 layerFEState->transparentRegionHint.intersect(
669 Rect(layerFEState->geomLayerBounds));
670
671 if (clippedTransparentRegionHint.isEmpty()) {
672 if (!layerFEState->transparentRegionHint.isEmpty()) {
673 ALOGD("Layer: %s had an out of bounds transparent region",
674 layerFE->getDebugName());
675 layerFEState->transparentRegionHint.dump("transparentRegionHint");
676 }
677 transparentRegion.clear();
678 } else {
679 transparentRegion = tr.transform(clippedTransparentRegionHint);
680 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800681 } else {
682 // transformation too complex, can't do the
683 // transparent region optimization.
684 transparentRegion.clear();
685 }
686 }
687
688 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800689 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800690 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800691 // If we one of the simple category of transforms (0/90/180/270 rotation
692 // + any flip), then the opaque region is the layer's footprint.
693 // Otherwise we don't try and compute the opaque region since there may
694 // be errors at the edges, and we treat the entire layer as
695 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800696 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800697 }
698
699 // Clip the covered region to the visible region
700 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
701
702 // Update accumAboveCoveredLayers for next (lower) layer
703 coverage.aboveCoveredLayers.orSelf(visibleRegion);
704
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000705 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
706 coveredRegionExcludingDisplayOverlays =
707 coverage.aboveCoveredLayersExcludingOverlays->intersect(visibleRegion);
708 coverage.aboveCoveredLayersExcludingOverlays->orSelf(visibleRegion);
709 }
710
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800711 // subtract the opaque region covered by the layers above us
712 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
713
714 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700715 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800716 }
717
718 // Get coverage information for the layer as previously displayed,
719 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800720 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700721 auto prevOutputLayer =
722 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800723
724 // Get coverage information for the layer as previously displayed
725 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
726 const Region kEmptyRegion;
727 const Region& oldVisibleRegion =
728 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
729 const Region& oldCoveredRegion =
730 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
731
732 // compute this layer's dirty region
733 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800734 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800735 // we need to invalidate the whole region
736 dirty = visibleRegion;
737 // as well, as the old visible region
738 dirty.orSelf(oldVisibleRegion);
739 } else {
740 /* compute the exposed region:
741 * the exposed region consists of two components:
742 * 1) what's VISIBLE now and was COVERED before
743 * 2) what's EXPOSED now less what was EXPOSED before
744 *
745 * note that (1) is conservative, we start with the whole visible region
746 * but only keep what used to be covered by something -- which mean it
747 * may have been exposed.
748 *
749 * (2) handles areas that were not covered by anything but got exposed
750 * because of a resize.
751 *
752 */
753 const Region newExposed = visibleRegion - coveredRegion;
754 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
755 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
756 }
757 dirty.subtractSelf(coverage.aboveOpaqueLayers);
758
759 // accumulate to the screen dirty region
760 coverage.dirtyRegion.orSelf(dirty);
761
762 // Update accumAboveOpaqueLayers for next (lower) layer
763 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
764
765 // Compute the visible non-transparent region
766 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
767
Vishnu Naira483b4a2019-12-12 15:07:52 -0800768 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800769 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700770 const auto& outputState = getState();
771 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000772 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800773 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700774 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800775 }
776
Vishnu Naira483b4a2019-12-12 15:07:52 -0800777 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
778
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800779 // The layer is visible. Either reuse the existing outputLayer if we have
780 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800781 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800782
783 // Store the layer coverage information into the layer state as some of it
784 // is useful later.
785 auto& outputLayerState = result->editState();
786 outputLayerState.visibleRegion = visibleRegion;
787 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
788 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200789 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000790 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800791 outputLayerState.shadowRegion = shadowRegion;
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500792 outputLayerState.outputSpaceBlockingRegionHint =
Leon Scroggins III7f7ad2c2022-03-17 17:06:20 -0400793 layerFEState->compositionType == Composition::DISPLAY_DECORATION
794 ? outputState.transform.transform(
795 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
796 : Region();
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000797 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
798 outputLayerState.coveredRegionExcludingDisplayOverlays =
799 std::move(coveredRegionExcludingDisplayOverlays);
800 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800801}
802
803void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
804 // The base class does nothing with this call.
805}
806
Dan Stoza269dc4d2021-01-15 15:07:43 -0800807void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800808 ATRACE_CALL();
809 ALOGV(__FUNCTION__);
810
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800811 if (!getState().isEnabled) {
812 return;
813 }
814
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800815 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
816 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
817
Lloyd Pique01c77c12019-04-17 12:48:32 -0700818 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700819 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800820 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200821 forceClientComposition,
822 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800823
824 if (mLayerRequestingBackgroundBlur == layer) {
825 forceClientComposition = false;
826 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800827 }
828}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800829
Dan Stoza269dc4d2021-01-15 15:07:43 -0800830void Output::planComposition() {
831 if (!mPlanner || !getState().isEnabled) {
832 return;
833 }
834
835 ATRACE_CALL();
836 ALOGV(__FUNCTION__);
837
838 mPlanner->plan(getOutputLayersOrderedByZ());
839}
840
841void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
842 ATRACE_CALL();
843 ALOGV(__FUNCTION__);
844
845 if (!getState().isEnabled) {
846 return;
847 }
848
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500849 if (auto frameTargetPtrOpt = ftl::Optional(getDisplayId())
850 .and_then(PhysicalDisplayId::tryCast)
851 .and_then([&refreshArgs](PhysicalDisplayId id) {
852 return refreshArgs.frameTargets.get(id);
853 })) {
854 editState().earliestPresentTime = frameTargetPtrOpt->get()->earliestPresentTime();
855 editState().expectedPresentTime = frameTargetPtrOpt->get()->expectedPresentTime().ns();
856 }
ramindani4aac32c2023-10-30 14:13:30 -0700857 editState().frameInterval = refreshArgs.frameInterval;
jimmyshiu4e211772023-06-15 15:18:38 +0000858 editState().powerCallback = refreshArgs.powerCallback;
Ady Abraham3645e642021-04-20 18:39:00 -0700859
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400860 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800861 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400862 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400863 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400864 bool overrideZ = false;
Robert Carrec8ccca2022-05-04 09:36:14 -0700865 uint64_t outputLayerHash = 0;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800866 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400867 if (layer == peekThroughLayer) {
868 // No longer needed, although it should not show up again, so
869 // resetting it is not truly needed either.
870 peekThroughLayer = nullptr;
871
872 // peekThroughLayer was already drawn ahead of its z order.
873 continue;
874 }
Dan Stoza6166c312021-01-15 16:34:05 -0800875 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400876 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400877 if (overrideInfo.buffer != nullptr) {
878 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800879 ALOGV("Skipping redundant buffer");
880 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400881 } else {
882 // First layer with the override buffer.
883 if (overrideInfo.peekThroughLayer) {
884 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400885
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400886 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400887 overrideZ = true;
888 includeGeometry = true;
889 constexpr bool isPeekingThrough = true;
890 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
891 isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700892 outputLayerHash ^= android::hashCombine(
893 reinterpret_cast<uint64_t>(&peekThroughLayer->getLayerFE()),
894 z, includeGeometry, overrideZ, isPeekingThrough,
895 peekThroughLayer->requiresClientComposition());
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400896 }
897
898 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800899 }
Dan Stoza6166c312021-01-15 16:34:05 -0800900 }
901
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400902 constexpr bool isPeekingThrough = false;
903 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700904 if (!skipLayer) {
905 outputLayerHash ^= android::hashCombine(
906 reinterpret_cast<uint64_t>(&layer->getLayerFE()),
907 z, includeGeometry, overrideZ, isPeekingThrough,
908 layer->requiresClientComposition());
909 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800910 }
Robert Carrec8ccca2022-05-04 09:36:14 -0700911 editState().outputLayerHash = outputLayerHash;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800912}
913
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800914compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
915 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
daniml39d6a2162021-05-19 15:56:21 +0200916 for (size_t i = 0; i < getOutputLayerCount(); i++) {
917 compositionengine::OutputLayer* layer = getOutputLayerOrderedByZByIndex(i);
918 compositionengine::OutputLayer* nextLayer = getOutputLayerOrderedByZByIndex(i + 1);
919
Leon Scroggins IIIc1dbfcb2022-03-21 16:48:10 -0400920 const auto* compState = layer->getLayerFE().getCompositionState();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100921
922 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
923 // want to force client composition because of the blur.
924 if (compState->sidebandStream != nullptr) {
925 return nullptr;
926 }
Leon Scroggins IIIc1dbfcb2022-03-21 16:48:10 -0400927
928 // If RenderEngine cannot render protected content, we cannot blur.
929 if (compState->hasProtectedContent &&
930 !getCompositionEngine().getRenderEngine().supportsProtectedContent()) {
931 return nullptr;
932 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000933 if (compState->isOpaque) {
934 continue;
935 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100936 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800937 layerRequestingBgComposition = layer;
938 }
daniml39d6a2162021-05-19 15:56:21 +0200939
940 // If the next layer is the Udfps touched layer, enable client composition for it
941 // because that somehow leads to the Udfps touched layer getting device composition
942 // consistently.
943 if ((nextLayer != nullptr && layerRequestingBgComposition == nullptr) &&
944 (strncmp(nextLayer->getLayerFE().getDebugName(), UDFPS_TOUCHED_LAYER_NAME,
945 strlen(UDFPS_TOUCHED_LAYER_NAME)) == 0)) {
946 layerRequestingBgComposition = layer;
947 break;
948 }
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800949 }
950 return layerRequestingBgComposition;
951}
952
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800953void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
954 setColorProfile(pickColorProfile(refreshArgs));
955}
956
957// Returns a data space that fits all visible layers. The returned data space
958// can only be one of
959// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
960// - Dataspace::DISPLAY_P3
961// - Dataspace::DISPLAY_BT2020
962// The returned HDR data space is one of
963// - Dataspace::UNKNOWN
964// - Dataspace::BT2020_HLG
965// - Dataspace::BT2020_PQ
966ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
967 bool* outIsHdrClientComposition) const {
968 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
969 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
970
Vishnu Naire14c6b32022-08-06 04:20:15 +0000971 // An Output's layers may be stale when it is disabled. As a consequence, the layers returned by
972 // getOutputLayersOrderedByZ may not be in a valid state and it is not safe to access their
973 // properties. Return a default dataspace value in this case.
974 if (!getState().isEnabled) {
975 return ui::Dataspace::V0_SRGB;
976 }
977
Lloyd Pique01c77c12019-04-17 12:48:32 -0700978 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800979 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800980 case ui::Dataspace::V0_SCRGB:
981 case ui::Dataspace::V0_SCRGB_LINEAR:
982 case ui::Dataspace::BT2020:
983 case ui::Dataspace::BT2020_ITU:
984 case ui::Dataspace::BT2020_LINEAR:
985 case ui::Dataspace::DISPLAY_BT2020:
986 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
987 break;
988 case ui::Dataspace::DISPLAY_P3:
989 bestDataSpace = ui::Dataspace::DISPLAY_P3;
990 break;
991 case ui::Dataspace::BT2020_PQ:
992 case ui::Dataspace::BT2020_ITU_PQ:
993 bestDataSpace = ui::Dataspace::DISPLAY_P3;
994 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800995 *outIsHdrClientComposition =
996 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800997 break;
998 case ui::Dataspace::BT2020_HLG:
999 case ui::Dataspace::BT2020_ITU_HLG:
1000 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1001 // When there's mixed PQ content and HLG content, we set the HDR
Sally Qi37d07c02023-10-05 17:32:32 +00001002 // data space to be BT2020_HLG and convert PQ to HLG.
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001003 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
1004 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
1005 }
1006 break;
1007 default:
1008 break;
1009 }
1010 }
1011
1012 return bestDataSpace;
1013}
1014
1015compositionengine::Output::ColorProfile Output::pickColorProfile(
1016 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
1017 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
1018 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
Alec Mouri88790f32023-07-21 01:25:14 +00001019 ui::RenderIntent::COLORIMETRIC};
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001020 }
1021
1022 ui::Dataspace hdrDataSpace;
1023 bool isHdrClientComposition = false;
1024 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
1025
1026 switch (refreshArgs.forceOutputColorMode) {
1027 case ui::ColorMode::SRGB:
1028 bestDataSpace = ui::Dataspace::V0_SRGB;
1029 break;
1030 case ui::ColorMode::DISPLAY_P3:
1031 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1032 break;
1033 default:
1034 break;
1035 }
1036
1037 // respect hdrDataSpace only when there is no legacy HDR support
1038 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
1039 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
1040 if (isHdr) {
1041 bestDataSpace = hdrDataSpace;
1042 }
1043
1044 ui::RenderIntent intent;
1045 switch (refreshArgs.outputColorSetting) {
1046 case OutputColorSetting::kManaged:
1047 case OutputColorSetting::kUnmanaged:
1048 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
1049 : ui::RenderIntent::COLORIMETRIC;
1050 break;
1051 case OutputColorSetting::kEnhanced:
1052 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
1053 break;
1054 default: // vendor display color setting
1055 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
1056 break;
1057 }
1058
1059 ui::ColorMode outMode;
1060 ui::Dataspace outDataSpace;
1061 ui::RenderIntent outRenderIntent;
1062 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
1063 &outRenderIntent);
1064
Alec Mouri88790f32023-07-21 01:25:14 +00001065 return ColorProfile{outMode, outDataSpace, outRenderIntent};
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001066}
1067
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001068void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001069 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001070 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -07001071 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001072 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001073
1074 // If nothing has changed (!dirty), don't recompose.
1075 // If something changed, but we don't currently have any visible layers,
1076 // and didn't when we last did a composition, then skip it this time.
1077 // The second rule does two things:
1078 // - When all layers are removed from a display, we'll emit one black
1079 // frame, then nothing more until we get new layers.
1080 // - When a display is created with a private layer stack, we won't
1081 // emit any black frames until a layer is added to the layer stack.
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001082 mMustRecompose = dirty && !(empty && wasEmpty);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001083
1084 const char flagPrefix[] = {'-', '+'};
1085 static_cast<void>(flagPrefix);
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001086 ALOGV("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __func__,
1087 mMustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
1088 flagPrefix[empty], flagPrefix[wasEmpty]);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001089
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001090 mRenderSurface->beginFrame(mMustRecompose);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001091
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001092 if (mMustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001093 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001094 }
1095}
1096
Lloyd Pique66d68602019-02-13 14:23:31 -08001097void Output::prepareFrame() {
1098 ATRACE_CALL();
1099 ALOGV(__FUNCTION__);
1100
Vishnu Naira3140382022-02-24 14:07:11 -08001101 auto& outputState = editState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001102 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -08001103 return;
1104 }
1105
Vishnu Naira3140382022-02-24 14:07:11 -08001106 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1107 bool success = chooseCompositionStrategy(&changes);
1108 resetCompositionStrategy();
Vishnu Nair9cf89262022-02-26 09:17:49 -08001109 outputState.strategyPrediction = CompositionStrategyPredictionState::DISABLED;
Vishnu Naira3140382022-02-24 14:07:11 -08001110 outputState.previousDeviceRequestedChanges = changes;
1111 outputState.previousDeviceRequestedSuccess = success;
1112 if (success) {
1113 applyCompositionStrategy(changes);
1114 }
1115 finishPrepareFrame();
1116}
Lloyd Pique66d68602019-02-13 14:23:31 -08001117
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001118ftl::Future<std::monostate> Output::presentFrameAndReleaseLayersAsync(bool flushEvenWhenDisabled) {
Leon Scroggins III277cbaf2024-06-24 10:44:13 -04001119 return ftl::Future<bool>(std::move(mHwComposerAsyncWorker->send([this, flushEvenWhenDisabled]() {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001120 presentFrameAndReleaseLayers(flushEvenWhenDisabled);
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001121 return true;
1122 })))
1123 .then([](bool) { return std::monostate{}; });
1124}
1125
Vishnu Naira3140382022-02-24 14:07:11 -08001126std::future<bool> Output::chooseCompositionStrategyAsync(
1127 std::optional<android::HWComposer::DeviceRequestedChanges>* changes) {
1128 return mHwComposerAsyncWorker->send(
1129 [&, changes]() { return chooseCompositionStrategy(changes); });
1130}
1131
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001132GpuCompositionResult Output::prepareFrameAsync() {
Vishnu Naira3140382022-02-24 14:07:11 -08001133 ATRACE_CALL();
1134 ALOGV(__FUNCTION__);
1135 auto& state = editState();
1136 const auto& previousChanges = state.previousDeviceRequestedChanges;
1137 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1138 resetCompositionStrategy();
1139 auto hwcResult = chooseCompositionStrategyAsync(&changes);
1140 if (state.previousDeviceRequestedSuccess) {
1141 applyCompositionStrategy(previousChanges);
1142 }
1143 finishPrepareFrame();
1144
1145 base::unique_fd bufferFence;
1146 std::shared_ptr<renderengine::ExternalTexture> buffer;
1147 updateProtectedContentState();
1148 const bool dequeueSucceeded = dequeueRenderBuffer(&bufferFence, &buffer);
1149 GpuCompositionResult compositionResult;
1150 if (dequeueSucceeded) {
1151 std::optional<base::unique_fd> optFd =
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001152 composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001153 if (optFd) {
1154 compositionResult.fence = std::move(*optFd);
1155 }
Dan Stoza47437bb2021-01-15 16:21:07 -08001156 }
1157
Vishnu Naira3140382022-02-24 14:07:11 -08001158 auto chooseCompositionSuccess = hwcResult.get();
1159 const bool predictionSucceeded = dequeueSucceeded && changes == previousChanges;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001160 state.strategyPrediction = predictionSucceeded ? CompositionStrategyPredictionState::SUCCESS
1161 : CompositionStrategyPredictionState::FAIL;
Vishnu Naira3140382022-02-24 14:07:11 -08001162 if (!predictionSucceeded) {
1163 ATRACE_NAME("CompositionStrategyPredictionMiss");
1164 resetCompositionStrategy();
1165 if (chooseCompositionSuccess) {
1166 applyCompositionStrategy(changes);
1167 }
1168 finishPrepareFrame();
1169 // Track the dequeued buffer to reuse so we don't need to dequeue another one.
1170 compositionResult.buffer = buffer;
1171 } else {
1172 ATRACE_NAME("CompositionStrategyPredictionHit");
1173 }
1174 state.previousDeviceRequestedChanges = std::move(changes);
1175 state.previousDeviceRequestedSuccess = chooseCompositionSuccess;
1176 return compositionResult;
Lloyd Pique66d68602019-02-13 14:23:31 -08001177}
1178
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001179void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1180 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
1181 return;
1182 }
1183
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001184 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001185 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Vishnu Naira3140382022-02-24 14:07:11 -08001186 base::unique_fd bufferFence;
1187 std::shared_ptr<renderengine::ExternalTexture> buffer;
1188 updateProtectedContentState();
1189 dequeueRenderBuffer(&bufferFence, &buffer);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001190 static_cast<void>(composeSurfaces(dirtyRegion, buffer, bufferFence));
Alec Mourif97df4d2023-09-06 02:10:05 +00001191 mRenderSurface->queueBuffer(base::unique_fd(), getHdrSdrRatio(buffer));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001192 }
1193 }
1194
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001195 constexpr bool kFlushEvenWhenDisabled = false;
1196 presentFrameAndReleaseLayers(kFlushEvenWhenDisabled);
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001197
1198 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
1199
1200 prepareFrame();
1201}
1202
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001203void Output::finishFrame(GpuCompositionResult&& result) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001204 ATRACE_CALL();
1205 ALOGV(__FUNCTION__);
Vishnu Nair9cf89262022-02-26 09:17:49 -08001206 const auto& outputState = getState();
1207 if (!outputState.isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001208 return;
1209 }
1210
Vishnu Naira3140382022-02-24 14:07:11 -08001211 std::optional<base::unique_fd> optReadyFence;
1212 std::shared_ptr<renderengine::ExternalTexture> buffer;
1213 base::unique_fd bufferFence;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001214 if (outputState.strategyPrediction == CompositionStrategyPredictionState::SUCCESS) {
Vishnu Naira3140382022-02-24 14:07:11 -08001215 optReadyFence = std::move(result.fence);
1216 } else {
1217 if (result.bufferAvailable()) {
1218 buffer = std::move(result.buffer);
1219 bufferFence = std::move(result.fence);
1220 } else {
1221 updateProtectedContentState();
1222 if (!dequeueRenderBuffer(&bufferFence, &buffer)) {
1223 return;
1224 }
1225 }
1226 // Repaint the framebuffer (if needed), getting the optional fence for when
1227 // the composition completes.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001228 optReadyFence = composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001229 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001230 if (!optReadyFence) {
1231 return;
1232 }
Xiang Wangcb50bbd2024-04-18 16:57:54 -07001233 if (isPowerHintSessionEnabled() && !isPowerHintSessionGpuReportingEnabled()) {
Matt Buckley50c44062022-01-17 20:48:10 +00001234 // get fence end time to know when gpu is complete in display
Ady Abrahamd11bade2022-08-01 16:18:03 -07001235 setHintSessionGpuFence(
1236 std::make_unique<FenceTime>(sp<Fence>::make(dup(optReadyFence->get()))));
Matt Buckley50c44062022-01-17 20:48:10 +00001237 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001238 // swap buffers (presentation)
Alec Mourif97df4d2023-09-06 02:10:05 +00001239 mRenderSurface->queueBuffer(std::move(*optReadyFence), getHdrSdrRatio(buffer));
Lloyd Piqued3d69882019-02-28 16:03:46 -08001240}
1241
Vishnu Naira3140382022-02-24 14:07:11 -08001242void Output::updateProtectedContentState() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001243 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001244 auto& renderEngine = getCompositionEngine().getRenderEngine();
1245 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1246
Chavi Weingarten18fa7c62023-11-28 21:16:03 +00001247 bool isProtected;
1248 if (FlagManager::getInstance().display_protected()) {
1249 isProtected = outputState.isProtected;
1250 } else {
1251 isProtected = outputState.isSecure;
1252 }
1253
1254 // We need to set the render surface as protected (DRM) if all the following conditions are met:
1255 // 1. The display is protected (in legacy, check if the display is secure)
1256 // 2. Protected content is supported
1257 // 3. At least one layer has protected content.
1258 if (isProtected && supportsProtectedContent) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001259 auto layers = getOutputLayersOrderedByZ();
1260 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
Eason Chiu45099662023-10-23 08:55:48 +08001261 return layer->getLayerFE().getCompositionState()->hasProtectedContent &&
1262 (!FlagManager::getInstance().protected_if_client() ||
1263 layer->requiresClientComposition());
Lloyd Piquee9eff972020-05-05 12:36:44 -07001264 });
Patrick Williams8aed5d22022-10-31 22:18:10 +00001265 if (needsProtected != mRenderSurface->isProtected()) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001266 mRenderSurface->setProtected(needsProtected);
1267 }
1268 }
Vishnu Naira3140382022-02-24 14:07:11 -08001269}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001270
Vishnu Naira3140382022-02-24 14:07:11 -08001271bool Output::dequeueRenderBuffer(base::unique_fd* bufferFence,
1272 std::shared_ptr<renderengine::ExternalTexture>* tex) {
1273 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001274
1275 // If we aren't doing client composition on this output, but do have a
1276 // flipClientTarget request for this frame on this output, we still need to
1277 // dequeue a buffer.
Vishnu Naira3140382022-02-24 14:07:11 -08001278 if (outputState.usesClientComposition || outputState.flipClientTarget) {
1279 *tex = mRenderSurface->dequeueBuffer(bufferFence);
1280 if (*tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001281 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1282 "client composition for this frame",
1283 mName.c_str());
Vishnu Naira3140382022-02-24 14:07:11 -08001284 return false;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001285 }
1286 }
Vishnu Naira3140382022-02-24 14:07:11 -08001287 return true;
1288}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001289
Vishnu Naira3140382022-02-24 14:07:11 -08001290std::optional<base::unique_fd> Output::composeSurfaces(
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001291 const Region& debugRegion, std::shared_ptr<renderengine::ExternalTexture> tex,
1292 base::unique_fd& fd) {
Vishnu Naira3140382022-02-24 14:07:11 -08001293 ATRACE_CALL();
1294 ALOGV(__FUNCTION__);
1295
1296 const auto& outputState = getState();
Leon Scroggins III042fdba2023-01-04 10:53:07 -05001297 const TracedOrdinal<bool> hasClientComposition = {
1298 base::StringPrintf("hasClientComposition %s", mNamePlusId.c_str()),
1299 outputState.usesClientComposition};
Lloyd Pique688abd42019-02-15 15:42:24 -08001300 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001301 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001302 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001303 }
1304
Vishnu Naira3140382022-02-24 14:07:11 -08001305 if (tex == nullptr) {
1306 ALOGW("Buffer not valid for display [%s], bailing out of "
1307 "client composition for this frame",
1308 mName.c_str());
1309 return {};
1310 }
1311
Lloyd Pique688abd42019-02-15 15:42:24 -08001312 ALOGV("hasClientComposition");
1313
Patrick Williams7584c6a2022-10-29 02:10:58 +00001314 renderengine::DisplaySettings clientCompositionDisplay =
Alec Mourif97df4d2023-09-06 02:10:05 +00001315 generateClientCompositionDisplaySettings(tex);
Lloyd Pique688abd42019-02-15 15:42:24 -08001316
Lloyd Pique688abd42019-02-15 15:42:24 -08001317 // Generate the client composition requests for the layers on this output.
Vishnu Naira3140382022-02-24 14:07:11 -08001318 auto& renderEngine = getCompositionEngine().getRenderEngine();
1319 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
Robert Carrccab4242021-09-28 16:53:03 -07001320 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001321 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001322 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001323 clientCompositionDisplay.outputDataspace,
1324 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001325 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1326
Vishnu Naira3140382022-02-24 14:07:11 -08001327 OutputCompositionState& outputCompositionState = editState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001328 // Check if the client composition requests were rendered into the provided graphic buffer. If
1329 // so, we can reuse the buffer and avoid client composition.
1330 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001331 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1332 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001333 clientCompositionLayers)) {
Vishnu Naira3140382022-02-24 14:07:11 -08001334 ATRACE_NAME("ClientCompositionCacheHit");
Vishnu Nair9b079a22020-01-21 14:36:08 -08001335 outputCompositionState.reusedClientComposition = true;
1336 setExpensiveRenderingExpected(false);
Vishnu Nair3a49f0a2022-07-29 21:52:53 +00001337 // b/239944175 pass the fence associated with the buffer.
1338 return base::unique_fd(std::move(fd));
Vishnu Nair9b079a22020-01-21 14:36:08 -08001339 }
Vishnu Naira3140382022-02-24 14:07:11 -08001340 ATRACE_NAME("ClientCompositionCacheMiss");
Alec Mouria90a5702021-04-16 16:36:21 +00001341 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001342 clientCompositionLayers);
1343 }
1344
Lloyd Pique688abd42019-02-15 15:42:24 -08001345 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001346 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1347 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1348 // because high frequency consumes extra battery.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001349 const bool expensiveRenderingExpected =
Leon Scroggins IIIcf17ebc2022-03-03 14:54:00 -05001350 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1351 [outputDataspace =
1352 clientCompositionDisplay.outputDataspace](const auto& layer) {
1353 return layer.sourceDataspace != outputDataspace;
1354 });
Lloyd Pique688abd42019-02-15 15:42:24 -08001355 if (expensiveRenderingExpected) {
1356 setExpensiveRenderingExpected(true);
1357 }
1358
Sally Qi59a9f502021-10-12 18:53:23 +00001359 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1360 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001361 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001362 std::back_inserter(clientRenderEngineLayers),
1363 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1364 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001365 });
1366
Alec Mourie4034bb2019-11-19 12:45:54 -08001367 const nsecs_t renderEngineStart = systemTime();
Patrick Williams2e9748f2022-08-09 22:48:18 +00001368 auto fenceResult = renderEngine
1369 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
Alec Mourif29700f2023-08-17 21:53:31 +00001370 std::move(fd))
Patrick Williams2e9748f2022-08-09 22:48:18 +00001371 .get();
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001372
1373 if (mClientCompositionRequestCache && fenceStatus(fenceResult) != NO_ERROR) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001374 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001375 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001376 }
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001377 const auto fence = std::move(fenceResult).value_or(Fence::NO_FENCE);
Xiang Wangaab31162024-03-12 19:48:08 -07001378 if (isPowerHintSessionEnabled()) {
1379 if (fence != Fence::NO_FENCE && fence->isValid() &&
1380 !outputCompositionState.reusedClientComposition) {
1381 setHintSessionRequiresRenderEngine(true);
Xiang Wangcb50bbd2024-04-18 16:57:54 -07001382 if (isPowerHintSessionGpuReportingEnabled()) {
Xiang Wangaab31162024-03-12 19:48:08 -07001383 // the order of the two calls here matters as we should check if the previously
1384 // tracked fence has signaled first and archive the previous start time
1385 setHintSessionGpuStart(TimePoint::now());
1386 setHintSessionGpuFence(
1387 std::make_unique<FenceTime>(sp<Fence>::make(dup(fence->get()))));
1388 }
1389 }
1390 }
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001391
Patrick Williams74c0bf62022-11-02 23:59:26 +00001392 if (auto timeStats = getCompositionEngine().getTimeStats()) {
1393 if (fence->isValid()) {
1394 timeStats->recordRenderEngineDuration(renderEngineStart,
1395 std::make_shared<FenceTime>(fence));
1396 } else {
1397 timeStats->recordRenderEngineDuration(renderEngineStart, systemTime());
1398 }
Alec Mourie4034bb2019-11-19 12:45:54 -08001399 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001400
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001401 for (auto* clientComposedLayer : clientCompositionLayersFE) {
1402 clientComposedLayer->setWasClientComposed(fence);
Robert Carrccab4242021-09-28 16:53:03 -07001403 }
1404
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001405 return base::unique_fd(fence->dup());
Lloyd Pique688abd42019-02-15 15:42:24 -08001406}
1407
Alec Mourif97df4d2023-09-06 02:10:05 +00001408renderengine::DisplaySettings Output::generateClientCompositionDisplaySettings(
1409 const std::shared_ptr<renderengine::ExternalTexture>& buffer) const {
Patrick Williams7584c6a2022-10-29 02:10:58 +00001410 const auto& outputState = getState();
1411
1412 renderengine::DisplaySettings clientCompositionDisplay;
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001413 clientCompositionDisplay.namePlusId = mNamePlusId;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001414 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1415 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
1416 clientCompositionDisplay.orientation =
1417 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
1418 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1419 ? outputState.dataspace
1420 : ui::Dataspace::UNKNOWN;
1421
1422 // If we have a valid current display brightness use that, otherwise fall back to the
1423 // display's max desired
1424 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
1425 ? outputState.displayBrightnessNits
1426 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1427 clientCompositionDisplay.maxLuminance =
1428 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mourif97df4d2023-09-06 02:10:05 +00001429
1430 float hdrSdrRatioMultiplier = 1.0f / getHdrSdrRatio(buffer);
1431 clientCompositionDisplay.targetLuminanceNits = outputState.clientTargetBrightness *
1432 outputState.displayBrightnessNits * hdrSdrRatioMultiplier;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001433 clientCompositionDisplay.dimmingStage = outputState.clientTargetDimmingStage;
1434 clientCompositionDisplay.renderIntent =
1435 static_cast<aidl::android::hardware::graphics::composer3::RenderIntent>(
1436 outputState.renderIntent);
1437
1438 // Compute the global color transform matrix.
1439 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001440 clientCompositionDisplay.deviceHandlesColorTransform =
1441 outputState.usesDeviceComposition || getSkipColorTransform();
1442 return clientCompositionDisplay;
1443}
1444
Vishnu Nair9b079a22020-01-21 14:36:08 -08001445std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001446 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001447 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001448 ALOGV("Rendering client layers");
1449
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001450 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001451 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001452 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001453
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001454 bool disableBlurs = false;
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001455 uint64_t previousOverrideBufferId = 0;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001456
Lloyd Pique01c77c12019-04-17 12:48:32 -07001457 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001458 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001459 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001460 auto& layerFE = layer->getLayerFE();
Robert Carr05da0082022-05-25 23:29:34 -07001461 layerFE.setWasClientComposed(nullptr);
Lloyd Pique688abd42019-02-15 15:42:24 -08001462
Lloyd Piquea2468662019-03-07 21:31:06 -08001463 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001464 ALOGV("Layer: %s", layerFE.getDebugName());
1465 if (clip.isEmpty()) {
1466 ALOGV(" Skipping for empty clip");
1467 firstLayer = false;
1468 continue;
1469 }
1470
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001471 disableBlurs |= layerFEState->sidebandStream != nullptr;
1472
Vishnu Naira483b4a2019-12-12 15:07:52 -08001473 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001474
1475 // We clear the client target for non-client composed layers if
1476 // requested by the HWC. We skip this if the layer is not an opaque
1477 // rectangle, as by definition the layer must blend with whatever is
1478 // underneath. We also skip the first layer as the buffer target is
1479 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001480 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001481 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001482
1483 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1484
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001485 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1486 // composing the non-shadow content and only draw the shadows.
1487 const bool realContentIsVisible = clientComposition &&
1488 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1489
Lloyd Pique688abd42019-02-15 15:42:24 -08001490 if (clientComposition || clearClientComposition) {
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001491 if (auto overrideSettings = layer->getOverrideCompositionSettings()) {
1492 if (overrideSettings->bufferId != previousOverrideBufferId) {
1493 previousOverrideBufferId = overrideSettings->bufferId;
1494 clientCompositionLayers.push_back(std::move(*overrideSettings));
Huihong Luo91ac3b52021-04-08 11:07:41 -07001495 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1496 } else {
1497 ALOGV("Skipping redundant override buffer for [%s] in RE",
1498 layer->getLayerFE().getDebugName());
1499 }
Dan Stoza6166c312021-01-15 16:34:05 -08001500 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001501 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1502 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1503 : (layer->getState().overrideInfo.disableBackgroundBlur
1504 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1505 BlurRegionsOnly
1506 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1507 Enabled);
Chavi Weingarten18fa7c62023-11-28 21:16:03 +00001508 bool isProtected = supportsProtectedContent;
1509 if (FlagManager::getInstance().display_protected()) {
1510 isProtected = outputState.isProtected && supportsProtectedContent;
1511 }
Alec Mourif54453c2021-05-13 16:28:28 -07001512 compositionengine::LayerFE::ClientCompositionTargetSettings
1513 targetSettings{.clip = clip,
Patrick Williams278a88f2023-01-27 16:52:40 -06001514 .needsFiltering = layer->needsFiltering() ||
Alec Mourif54453c2021-05-13 16:28:28 -07001515 outputState.needsFiltering,
1516 .isSecure = outputState.isSecure,
Chavi Weingarten18fa7c62023-11-28 21:16:03 +00001517 .isProtected = isProtected,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001518 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001519 .dataspace = outputDataspace,
1520 .realContentIsVisible = realContentIsVisible,
1521 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001522 .blurSetting = blurSetting,
Vishnu Naire14c6b32022-08-06 04:20:15 +00001523 .whitePointNits = layerState.whitePointNits,
1524 .treat170mAsSrgb = outputState.treat170mAsSrgb};
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001525 if (auto clientCompositionSettings =
1526 layerFE.prepareClientComposition(targetSettings)) {
1527 clientCompositionLayers.push_back(std::move(*clientCompositionSettings));
1528 if (realContentIsVisible) {
1529 layer->editState().clientCompositionTimestamp = systemTime();
1530 }
Dan Stoza6166c312021-01-15 16:34:05 -08001531 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001532 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001533
Tianhua Sunf91f1402022-05-09 05:45:46 +00001534 if (clientComposition) {
1535 outLayerFEs.push_back(&layerFE);
1536 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001537 }
1538
1539 firstLayer = false;
1540 }
1541
1542 return clientCompositionLayers;
1543}
1544
1545void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001546 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001547 if (flashRegion.isEmpty()) {
1548 return;
1549 }
1550
Vishnu Nair9b079a22020-01-21 14:36:08 -08001551 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001552 layerSettings.source.buffer.buffer = nullptr;
1553 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1554 layerSettings.alpha = half(1.0);
1555
1556 for (const auto& rect : flashRegion) {
1557 layerSettings.geometry.boundaries = rect.toFloatRect();
1558 clientCompositionLayers.push_back(layerSettings);
1559 }
1560}
1561
1562void Output::setExpensiveRenderingExpected(bool) {
1563 // The base class does nothing with this call.
1564}
1565
Xiang Wangaab31162024-03-12 19:48:08 -07001566void Output::setHintSessionGpuStart(TimePoint) {
1567 // The base class does nothing with this call.
1568}
1569
Matt Buckley50c44062022-01-17 20:48:10 +00001570void Output::setHintSessionGpuFence(std::unique_ptr<FenceTime>&&) {
1571 // The base class does nothing with this call.
1572}
1573
Xiang Wangaab31162024-03-12 19:48:08 -07001574void Output::setHintSessionRequiresRenderEngine(bool) {
1575 // The base class does nothing with this call.
1576}
1577
Matt Buckley50c44062022-01-17 20:48:10 +00001578bool Output::isPowerHintSessionEnabled() {
1579 return false;
1580}
1581
Xiang Wangcb50bbd2024-04-18 16:57:54 -07001582bool Output::isPowerHintSessionGpuReportingEnabled() {
1583 return false;
1584}
1585
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001586void Output::presentFrameAndReleaseLayers(bool flushEvenWhenDisabled) {
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001587 ATRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001588 ALOGV(__FUNCTION__);
1589
1590 if (!getState().isEnabled) {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001591 if (flushEvenWhenDisabled && FlagManager::getInstance().flush_buffer_slots_to_uncache()) {
1592 // Some commands, like clearing buffer slots, should still be executed
1593 // even if the display is not enabled.
1594 executeCommands();
1595 }
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001596 return;
1597 }
1598
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001599 auto& outputState = editState();
1600 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001601
Leon Scroggins IIIc1623d12023-11-06 15:31:05 -05001602 auto frame = presentFrame();
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001603
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001604 mRenderSurface->onPresentDisplayCompleted();
1605
Lloyd Pique01c77c12019-04-17 12:48:32 -07001606 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001607 // The layer buffer from the previous frame (if any) is released
1608 // by HWC only when the release fence from this frame (if any) is
1609 // signaled. Always get the release fence from HWC first.
1610 sp<Fence> releaseFence = Fence::NO_FENCE;
1611
1612 if (auto hwcLayer = layer->getHwcLayer()) {
1613 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1614 releaseFence = f->second;
1615 }
1616 }
1617
1618 // If the layer was client composited in the previous frame, we
1619 // need to merge with the previous client target acquire fence.
1620 // Since we do not track that, always merge with the current
1621 // client target acquire fence when it is available, even though
1622 // this is suboptimal.
1623 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001624 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001625 releaseFence =
1626 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1627 }
Melody Hsu793f8362024-01-08 20:00:35 +00001628 if (FlagManager::getInstance().ce_fence_promise()) {
1629 layer->getLayerFE().setReleaseFence(releaseFence);
1630 } else {
1631 layer->getLayerFE()
1632 .onLayerDisplayed(ftl::yield<FenceResult>(std::move(releaseFence)).share(),
1633 outputState.layerFilter.layerStack);
1634 }
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001635 }
1636
1637 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001638 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001639 // supply them with the present fence.
1640 for (auto& weakLayer : mReleasedLayers) {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001641 if (const auto layer = weakLayer.promote()) {
Melody Hsu793f8362024-01-08 20:00:35 +00001642 if (FlagManager::getInstance().ce_fence_promise()) {
1643 layer->setReleaseFence(frame.presentFence);
1644 } else {
1645 layer->onLayerDisplayed(ftl::yield<FenceResult>(frame.presentFence).share(),
1646 outputState.layerFilter.layerStack);
1647 }
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001648 }
1649 }
1650
1651 // Clear out the released layers now that we're done with them.
1652 mReleasedLayers.clear();
1653}
1654
Alec Mouriaa831582021-06-07 16:23:01 -07001655void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III43b5d522023-04-10 15:53:45 -04001656 const auto& outputState = getState();
1657 if (mPlanner && outputState.isEnabled) {
1658 mPlanner->renderCachedSets(outputState, refreshArgs.scheduledFrameTime,
1659 outputState.usesDeviceComposition || getSkipColorTransform());
Dan Stoza6166c312021-01-15 16:34:05 -08001660 }
1661}
1662
Lloyd Pique32cbe282018-10-19 13:09:22 -07001663void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001664 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001665 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001666}
1667
Vishnu Naira3140382022-02-24 14:07:11 -08001668void Output::resetCompositionStrategy() {
Lloyd Pique66d68602019-02-13 14:23:31 -08001669 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001670 auto& outputState = editState();
1671 outputState.usesClientComposition = true;
1672 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001673 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001674}
1675
Lloyd Pique688abd42019-02-15 15:42:24 -08001676bool Output::getSkipColorTransform() const {
1677 return true;
1678}
1679
Leon Scroggins IIIc1623d12023-11-06 15:31:05 -05001680compositionengine::Output::FrameFences Output::presentFrame() {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001681 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001682 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001683 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1684 }
1685 return result;
1686}
1687
Vishnu Naira3140382022-02-24 14:07:11 -08001688void Output::setPredictCompositionStrategy(bool predict) {
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001689 mPredictCompositionStrategy = predict;
1690 updateHwcAsyncWorker();
1691}
1692
1693void Output::updateHwcAsyncWorker() {
1694 if (mPredictCompositionStrategy || mOffloadPresent) {
1695 if (!mHwComposerAsyncWorker) {
1696 mHwComposerAsyncWorker = std::make_unique<HwcAsyncWorker>();
1697 }
Vishnu Naira3140382022-02-24 14:07:11 -08001698 } else {
1699 mHwComposerAsyncWorker.reset(nullptr);
1700 }
1701}
1702
Alec Mouridda07d92022-04-25 22:39:25 +00001703void Output::setTreat170mAsSrgb(bool enable) {
1704 editState().treat170mAsSrgb = enable;
1705}
1706
Vishnu Naira3140382022-02-24 14:07:11 -08001707bool Output::canPredictCompositionStrategy(const CompositionRefreshArgs& refreshArgs) {
Robert Carrec8ccca2022-05-04 09:36:14 -07001708 uint64_t lastOutputLayerHash = getState().lastOutputLayerHash;
1709 uint64_t outputLayerHash = getState().outputLayerHash;
1710 editState().lastOutputLayerHash = outputLayerHash;
1711
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001712 if (!getState().isEnabled || !mPredictCompositionStrategy) {
Vishnu Naira3140382022-02-24 14:07:11 -08001713 ALOGV("canPredictCompositionStrategy disabled");
1714 return false;
1715 }
1716
1717 if (!getState().previousDeviceRequestedChanges) {
1718 ALOGV("canPredictCompositionStrategy previous changes not available");
1719 return false;
1720 }
1721
1722 if (!mRenderSurface->supportsCompositionStrategyPrediction()) {
1723 ALOGV("canPredictCompositionStrategy surface does not support");
1724 return false;
1725 }
1726
1727 if (refreshArgs.devOptFlashDirtyRegionsDelay) {
1728 ALOGV("canPredictCompositionStrategy devOptFlashDirtyRegionsDelay");
1729 return false;
1730 }
1731
Robert Carrec8ccca2022-05-04 09:36:14 -07001732 if (lastOutputLayerHash != outputLayerHash) {
1733 ALOGV("canPredictCompositionStrategy output layers changed");
1734 return false;
1735 }
1736
Vishnu Naira3140382022-02-24 14:07:11 -08001737 // If no layer uses clientComposition, then don't predict composition strategy
1738 // because we have less work to do in parallel.
1739 if (!anyLayersRequireClientComposition()) {
1740 ALOGV("canPredictCompositionStrategy no layer uses clientComposition");
1741 return false;
1742 }
1743
Robert Carrec8ccca2022-05-04 09:36:14 -07001744 return true;
Vishnu Naira3140382022-02-24 14:07:11 -08001745}
1746
1747bool Output::anyLayersRequireClientComposition() const {
1748 const auto layers = getOutputLayersOrderedByZ();
1749 return std::any_of(layers.begin(), layers.end(),
1750 [](const auto& layer) { return layer->requiresClientComposition(); });
1751}
1752
1753void Output::finishPrepareFrame() {
1754 const auto& state = getState();
1755 if (mPlanner) {
1756 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
1757 }
1758 mRenderSurface->prepareFrame(state.usesClientComposition, state.usesDeviceComposition);
1759}
1760
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001761bool Output::mustRecompose() const {
1762 return mMustRecompose;
1763}
1764
Alec Mourif97df4d2023-09-06 02:10:05 +00001765float Output::getHdrSdrRatio(const std::shared_ptr<renderengine::ExternalTexture>& buffer) const {
1766 if (buffer == nullptr) {
1767 return 1.0f;
1768 }
1769
1770 if (!FlagManager::getInstance().fp16_client_target()) {
1771 return 1.0f;
1772 }
1773
1774 if (getState().displayBrightnessNits < 0.0f || getState().sdrWhitePointNits <= 0.0f ||
1775 buffer->getPixelFormat() != PIXEL_FORMAT_RGBA_FP16 ||
1776 (static_cast<int32_t>(getState().dataspace) &
1777 static_cast<int32_t>(ui::Dataspace::RANGE_MASK)) !=
1778 static_cast<int32_t>(ui::Dataspace::RANGE_EXTENDED)) {
1779 return 1.0f;
1780 }
1781
1782 return getState().displayBrightnessNits / getState().sdrWhitePointNits;
1783}
1784
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001785} // namespace impl
1786} // namespace android::compositionengine