blob: 403385ea8c7f9f6738f64cefa4c96fe3a38bc783 [file] [log] [blame]
Lloyd Pique32cbe282018-10-19 13:09:22 -07001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Alec Mouria90a5702021-04-16 16:36:21 +000017#include <SurfaceFlingerProperties.sysprop.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070018#include <android-base/stringprintf.h>
19#include <compositionengine/CompositionEngine.h>
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080020#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070021#include <compositionengine/DisplayColorProfile.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080022#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070023#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070024#include <compositionengine/RenderSurface.h>
Vishnu Naira3140382022-02-24 14:07:11 -080025#include <compositionengine/impl/HwcAsyncWorker.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070026#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070027#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080028#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070029#include <compositionengine/impl/OutputLayerCompositionState.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080030#include <compositionengine/impl/planner/Planner.h>
Sally Qi59a9f502021-10-12 18:53:23 +000031#include <ftl/future.h>
Leon Scroggins III5a655b82022-09-07 13:17:09 -040032#include <gui/TraceUtils.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080033
Alec Mouria90a5702021-04-16 16:36:21 +000034#include <thread>
35
36#include "renderengine/ExternalTexture.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080037
38// TODO(b/129481165): remove the #pragma below and fix conversion issues
39#pragma clang diagnostic push
40#pragma clang diagnostic ignored "-Wconversion"
41
Lloyd Pique688abd42019-02-15 15:42:24 -080042#include <renderengine/DisplaySettings.h>
43#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080044
45// TODO(b/129481165): remove the #pragma below and fix conversion issues
46#pragma clang diagnostic pop // ignored "-Wconversion"
47
Dan Stoza269dc4d2021-01-15 15:07:43 -080048#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070049#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080050#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080051#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070052
Lloyd Pique688abd42019-02-15 15:42:24 -080053#include "TracedOrdinal.h"
54
Leon Scroggins III9a0afda2022-01-11 16:53:09 -050055using aidl::android::hardware::graphics::composer3::Composition;
56
Lloyd Piquefeb73d72018-12-04 17:23:44 -080057namespace android::compositionengine {
58
59Output::~Output() = default;
60
61namespace impl {
Vishnu Nair9cf89262022-02-26 09:17:49 -080062using CompositionStrategyPredictionState =
63 OutputCompositionState::CompositionStrategyPredictionState;
Lloyd Piquec29e4c62019-03-07 21:48:19 -080064namespace {
65
66template <typename T>
67class Reversed {
68public:
69 explicit Reversed(const T& container) : mContainer(container) {}
70 auto begin() { return mContainer.rbegin(); }
71 auto end() { return mContainer.rend(); }
72
73private:
74 const T& mContainer;
75};
76
77// Helper for enumerating over a container in reverse order
78template <typename T>
79Reversed<T> reversed(const T& c) {
80 return Reversed<T>(c);
81}
82
Marin Shalamanovb15d2272020-09-17 21:41:52 +020083struct ScaleVector {
84 float x;
85 float y;
86};
87
88// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
89// where to' will have the same size as "to". In the case where "from" and "to"
90// start at the origin to'=to.
91ScaleVector getScale(const Rect& from, const Rect& to) {
92 return {.x = static_cast<float>(to.width()) / from.width(),
93 .y = static_cast<float>(to.height()) / from.height()};
94}
95
Lloyd Piquec29e4c62019-03-07 21:48:19 -080096} // namespace
97
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070098std::shared_ptr<Output> createOutput(
99 const compositionengine::CompositionEngine& compositionEngine) {
100 return createOutputTemplated<Output>(compositionEngine);
101}
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102
103Output::~Output() = default;
104
Lloyd Pique32cbe282018-10-19 13:09:22 -0700105bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700106 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
107 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700108}
109
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700110std::optional<DisplayId> Output::getDisplayId() const {
111 return {};
112}
113
Lloyd Pique32cbe282018-10-19 13:09:22 -0700114const std::string& Output::getName() const {
115 return mName;
116}
117
118void Output::setName(const std::string& name) {
119 mName = name;
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400120 auto displayIdOpt = getDisplayId();
Leon Scroggins IIIc03d4652023-01-05 13:03:53 -0500121 mNamePlusId = displayIdOpt ? base::StringPrintf("%s (%s)", mName.c_str(),
122 to_string(*displayIdOpt).c_str())
123 : mName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700124}
125
126void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700127 auto& outputState = editState();
128 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700129 return;
130 }
131
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700132 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700133 dirtyEntireOutput();
134}
135
Alec Mouri023c1882021-05-08 16:36:33 -0700136void Output::setLayerCachingEnabled(bool enabled) {
137 if (enabled == (mPlanner != nullptr)) {
138 return;
139 }
140
141 if (enabled) {
Alec Mouridf6201b2021-06-01 16:20:42 -0700142 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
Alec Mouri023c1882021-05-08 16:36:33 -0700143 if (mRenderSurface) {
144 mPlanner->setDisplaySize(mRenderSurface->getSize());
145 }
146 } else {
147 mPlanner.reset();
148 }
Alec Mouric773472b2021-05-19 14:29:05 -0700149
150 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
151 if (!outputLayer) {
152 continue;
153 }
154
155 outputLayer->editState().overrideInfo = {};
156 }
Alec Mouri023c1882021-05-08 16:36:33 -0700157}
158
Ady Abrahamdb036a82021-07-16 14:18:34 -0700159void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
160 if (mPlanner) {
161 mPlanner->setTexturePoolEnabled(enabled);
162 }
163}
164
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200165void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
166 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700167 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200168
Angel Aguayob084e0c2021-08-04 23:27:28 +0000169 outputState.displaySpace.setOrientation(orientation);
170 LOG_FATAL_IF(outputState.displaySpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200171 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200172
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200173 // Compute orientedDisplaySpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000174 ui::Size orientedSize = outputState.displaySpace.getBounds();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200175 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200176 std::swap(orientedSize.width, orientedSize.height);
177 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000178 outputState.orientedDisplaySpace.setBounds(orientedSize);
179 outputState.orientedDisplaySpace.setContent(orientedDisplaySpaceRect);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200180
181 // Compute displaySpace.content
182 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
183 ui::Transform rotation;
184 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000185 const auto displaySize = outputState.displaySpace.getBoundsAsRect();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200186 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
187 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000188 outputState.displaySpace.setContent(rotation.transform(orientedDisplaySpaceRect));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200189
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200190 // Compute framebufferSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000191 outputState.framebufferSpace.setOrientation(orientation);
192 LOG_FATAL_IF(outputState.framebufferSpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200193 "The framebuffer bounds are unknown.");
Angel Aguayob084e0c2021-08-04 23:27:28 +0000194 const auto scale = getScale(outputState.displaySpace.getBoundsAsRect(),
195 outputState.framebufferSpace.getBoundsAsRect());
196 outputState.framebufferSpace.setContent(
197 outputState.displaySpace.getContent().scale(scale.x, scale.y));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200198
199 // Compute layerStackSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000200 outputState.layerStackSpace.setContent(layerStackSpaceRect);
201 outputState.layerStackSpace.setBounds(
202 ui::Size(layerStackSpaceRect.getWidth(), layerStackSpaceRect.getHeight()));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200203
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200204 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
205 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700206 dirtyEntireOutput();
207}
208
Alec Mouricdf16792021-12-10 13:16:06 -0800209void Output::setNextBrightness(float brightness) {
210 editState().displayBrightness = brightness;
211}
212
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200213void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700214 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200215
216 auto& state = editState();
217
218 // Update framebuffer space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000219 const ui::Size newBounds(size);
220 state.framebufferSpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200221
222 // Update display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000223 state.displaySpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200224 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
225
226 // Update oriented display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000227 const auto orientation = state.displaySpace.getOrientation();
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200228 ui::Size orientedSize = size;
229 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
230 std::swap(orientedSize.width, orientedSize.height);
231 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000232 const ui::Size newOrientedBounds(orientedSize);
233 state.orientedDisplaySpace.setBounds(newOrientedBounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700234
Dan Stoza6166c312021-01-15 16:34:05 -0800235 if (mPlanner) {
236 mPlanner->setDisplaySize(size);
237 }
238
Lloyd Pique32cbe282018-10-19 13:09:22 -0700239 dirtyEntireOutput();
240}
241
Garfield Tan54edd912020-10-21 16:31:41 -0700242ui::Transform::RotationFlags Output::getTransformHint() const {
243 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
244}
245
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700246void Output::setLayerFilter(ui::LayerFilter filter) {
247 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700248 dirtyEntireOutput();
249}
250
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800251void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700252 auto& colorTransformMatrix = editState().colorTransformMatrix;
253 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700254 return;
255 }
256
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700257 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800258
259 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700260}
261
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800262void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700263 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800264 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
265 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800266
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700267 auto& outputState = editState();
268 if (outputState.colorMode == colorProfile.mode &&
269 outputState.dataspace == colorProfile.dataspace &&
270 outputState.renderIntent == colorProfile.renderIntent &&
271 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800272 return;
273 }
274
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700275 outputState.colorMode = colorProfile.mode;
276 outputState.dataspace = colorProfile.dataspace;
277 outputState.renderIntent = colorProfile.renderIntent;
278 outputState.targetDataspace = targetDataspace;
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
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800435void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400436 ATRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800437 ALOGV(__FUNCTION__);
438
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800439 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800440 updateCompositionState(refreshArgs);
441 planComposition();
442 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800443 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800444 beginFrame();
Vishnu Naira3140382022-02-24 14:07:11 -0800445
446 GpuCompositionResult result;
447 const bool predictCompositionStrategy = canPredictCompositionStrategy(refreshArgs);
448 if (predictCompositionStrategy) {
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000449 result = prepareFrameAsync();
Vishnu Naira3140382022-02-24 14:07:11 -0800450 } else {
451 prepareFrame();
452 }
453
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800454 devOptRepaintFlash(refreshArgs);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000455 finishFrame(std::move(result));
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800456 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700457 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800458}
459
Brian Lindahl439afad2022-11-14 11:16:55 -0700460void Output::uncacheBuffers(std::vector<uint64_t> const& bufferIdsToUncache) {
461 if (bufferIdsToUncache.empty()) {
462 return;
463 }
464 for (auto outputLayer : getOutputLayersOrderedByZ()) {
465 outputLayer->uncacheBuffers(bufferIdsToUncache);
466 }
467}
468
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800469void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
470 LayerFESet& layerFESet) {
471 ATRACE_CALL();
472 ALOGV(__FUNCTION__);
473
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700474 auto& outputState = editState();
475
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800476 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700477 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800478 return;
479 }
480
481 // Process the layers to determine visibility and coverage
482 compositionengine::Output::CoverageState coverage{layerFESet};
483 collectVisibleLayers(refreshArgs, coverage);
484
485 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700486 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000487 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800488 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
489
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700490 outputState.undefinedRegion = undefinedRegion;
491 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800492}
493
494void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
495 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800496 // Evaluate the layers from front to back to determine what is visible. This
497 // also incrementally calculates the coverage information for each layer as
498 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800499 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700500 // Incrementally process the coverage for each layer
501 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800502
503 // TODO(b/121291683): Stop early if the output is completely covered and
504 // no more layers could even be visible underneath the ones on top.
505 }
506
Lloyd Pique01c77c12019-04-17 12:48:32 -0700507 setReleasedLayers(refreshArgs);
508
509 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800510}
511
Lloyd Piquede196652020-01-22 17:29:58 -0800512void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700513 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800514 // Ensure we have a snapshot of the basic geometry layer state. Limit the
515 // snapshots to once per frame for each candidate layer, as layers may
516 // appear on multiple outputs.
517 if (!coverage.latchedLayers.count(layerFE)) {
518 coverage.latchedLayers.insert(layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800519 }
520
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700521 // Only consider the layers on this output
522 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800523 return;
524 }
525
526 // Obtain a read-only pointer to the front-end layer state
527 const auto* layerFEState = layerFE->getCompositionState();
528 if (CC_UNLIKELY(!layerFEState)) {
529 return;
530 }
531
532 // handle hidden surfaces by setting the visible region to empty
533 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700534 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800535 }
536
537 /*
538 * opaqueRegion: area of a surface that is fully opaque.
539 */
540 Region opaqueRegion;
541
542 /*
543 * visibleRegion: area of a surface that is visible on screen and not fully
544 * transparent. This is essentially the layer's footprint minus the opaque
545 * regions above it. Areas covered by a translucent surface are considered
546 * visible.
547 */
548 Region visibleRegion;
549
550 /*
551 * coveredRegion: area of a surface that is covered by all visible regions
552 * above it (which includes the translucent areas).
553 */
554 Region coveredRegion;
555
556 /*
557 * transparentRegion: area of a surface that is hinted to be completely
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500558 * transparent.
559 * This is used to tell when the layer has no visible non-transparent
560 * regions and can be removed from the layer list. It does not affect the
561 * visibleRegion of this layer or any layers beneath it. The hint may not
562 * be correct if apps don't respect the SurfaceView restrictions (which,
563 * sadly, some don't).
564 *
565 * In addition, it is used on DISPLAY_DECORATION layers to specify the
566 * blockingRegion, allowing the DPU to skip it to save power. Once we have
567 * hardware that supports a blockingRegion on frames with AFBC, it may be
568 * useful to use this for other layers, too, so long as we can prevent
569 * regressions on b/7179570.
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800570 */
571 Region transparentRegion;
572
Vishnu Naira483b4a2019-12-12 15:07:52 -0800573 /*
574 * shadowRegion: Region cast by the layer's shadow.
575 */
576 Region shadowRegion;
577
Lloyd Piquede196652020-01-22 17:29:58 -0800578 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800579
580 // Get the visible region
581 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
582 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800583 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800584 visibleRegion.set(visibleRect);
585
Lloyd Piquede196652020-01-22 17:29:58 -0800586 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800587 // if the layer casts a shadow, offset the layers visible region and
588 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800589 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800590 Rect visibleRectWithShadows(visibleRect);
591 visibleRectWithShadows.inset(inset, inset, inset, inset);
592 visibleRegion.set(visibleRectWithShadows);
593 shadowRegion = visibleRegion.subtract(visibleRect);
594 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800595
596 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700597 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800598 }
599
600 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800601 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800602 if (tr.preserveRects()) {
Alec Mourie60f0b92022-06-10 19:15:20 +0000603 // Clip the transparent region to geomLayerBounds first
604 // The transparent region may be influenced by applications, for
605 // instance, by overriding ViewGroup#gatherTransparentRegion with a
606 // custom view. Once the layer stack -> display mapping is known, we
607 // must guard against very wrong inputs to prevent underflow or
608 // overflow errors. We do this here by constraining the transparent
609 // region to be within the pre-transform layer bounds, since the
610 // layer bounds are expected to play nicely with the full
611 // transform.
612 const Region clippedTransparentRegionHint =
613 layerFEState->transparentRegionHint.intersect(
614 Rect(layerFEState->geomLayerBounds));
615
616 if (clippedTransparentRegionHint.isEmpty()) {
617 if (!layerFEState->transparentRegionHint.isEmpty()) {
618 ALOGD("Layer: %s had an out of bounds transparent region",
619 layerFE->getDebugName());
620 layerFEState->transparentRegionHint.dump("transparentRegionHint");
621 }
622 transparentRegion.clear();
623 } else {
624 transparentRegion = tr.transform(clippedTransparentRegionHint);
625 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800626 } else {
627 // transformation too complex, can't do the
628 // transparent region optimization.
629 transparentRegion.clear();
630 }
631 }
632
633 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800634 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800635 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800636 // If we one of the simple category of transforms (0/90/180/270 rotation
637 // + any flip), then the opaque region is the layer's footprint.
638 // Otherwise we don't try and compute the opaque region since there may
639 // be errors at the edges, and we treat the entire layer as
640 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800641 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800642 }
643
644 // Clip the covered region to the visible region
645 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
646
647 // Update accumAboveCoveredLayers for next (lower) layer
648 coverage.aboveCoveredLayers.orSelf(visibleRegion);
649
650 // subtract the opaque region covered by the layers above us
651 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
652
653 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700654 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800655 }
656
657 // Get coverage information for the layer as previously displayed,
658 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800659 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700660 auto prevOutputLayer =
661 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800662
663 // Get coverage information for the layer as previously displayed
664 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
665 const Region kEmptyRegion;
666 const Region& oldVisibleRegion =
667 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
668 const Region& oldCoveredRegion =
669 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
670
671 // compute this layer's dirty region
672 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800673 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800674 // we need to invalidate the whole region
675 dirty = visibleRegion;
676 // as well, as the old visible region
677 dirty.orSelf(oldVisibleRegion);
678 } else {
679 /* compute the exposed region:
680 * the exposed region consists of two components:
681 * 1) what's VISIBLE now and was COVERED before
682 * 2) what's EXPOSED now less what was EXPOSED before
683 *
684 * note that (1) is conservative, we start with the whole visible region
685 * but only keep what used to be covered by something -- which mean it
686 * may have been exposed.
687 *
688 * (2) handles areas that were not covered by anything but got exposed
689 * because of a resize.
690 *
691 */
692 const Region newExposed = visibleRegion - coveredRegion;
693 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
694 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
695 }
696 dirty.subtractSelf(coverage.aboveOpaqueLayers);
697
698 // accumulate to the screen dirty region
699 coverage.dirtyRegion.orSelf(dirty);
700
701 // Update accumAboveOpaqueLayers for next (lower) layer
702 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
703
704 // Compute the visible non-transparent region
705 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
706
Vishnu Naira483b4a2019-12-12 15:07:52 -0800707 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800708 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700709 const auto& outputState = getState();
710 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000711 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800712 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700713 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800714 }
715
Vishnu Naira483b4a2019-12-12 15:07:52 -0800716 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
717
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800718 // The layer is visible. Either reuse the existing outputLayer if we have
719 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800720 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800721
722 // Store the layer coverage information into the layer state as some of it
723 // is useful later.
724 auto& outputLayerState = result->editState();
725 outputLayerState.visibleRegion = visibleRegion;
726 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
727 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200728 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000729 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800730 outputLayerState.shadowRegion = shadowRegion;
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500731 outputLayerState.outputSpaceBlockingRegionHint =
Leon Scroggins III7f7ad2c2022-03-17 17:06:20 -0400732 layerFEState->compositionType == Composition::DISPLAY_DECORATION
733 ? outputState.transform.transform(
734 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
735 : Region();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800736}
737
738void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
739 // The base class does nothing with this call.
740}
741
Dan Stoza269dc4d2021-01-15 15:07:43 -0800742void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800743 ATRACE_CALL();
744 ALOGV(__FUNCTION__);
745
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800746 if (!getState().isEnabled) {
747 return;
748 }
749
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800750 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
751 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
752
Lloyd Pique01c77c12019-04-17 12:48:32 -0700753 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700754 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800755 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200756 forceClientComposition,
757 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800758
759 if (mLayerRequestingBackgroundBlur == layer) {
760 forceClientComposition = false;
761 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800762 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000763
764 updateCompositionStateForBorder(refreshArgs);
765}
766
767void Output::updateCompositionStateForBorder(
768 const compositionengine::CompositionRefreshArgs& refreshArgs) {
769 std::unordered_map<int32_t, const Region*> layerVisibleRegionMap;
770 // Store a map of layerId to their computed visible region.
771 for (auto* layer : getOutputLayersOrderedByZ()) {
772 int layerId = (layer->getLayerFE()).getSequence();
773 layerVisibleRegionMap[layerId] = &((layer->getState()).visibleRegion);
774 }
775 OutputCompositionState& outputCompositionState = editState();
776 outputCompositionState.borderInfoList.clear();
777 bool clientComposeTopLayer = false;
778 for (const auto& borderInfo : refreshArgs.borderInfoList) {
779 renderengine::BorderRenderInfo info;
780 for (const auto& id : borderInfo.layerIds) {
781 info.combinedRegion.orSelf(*(layerVisibleRegionMap[id]));
782 }
Tianhao Yao10cea3c2022-03-30 01:37:22 +0000783
784 if (!info.combinedRegion.isEmpty()) {
785 info.width = borderInfo.width;
786 info.color = borderInfo.color;
787 outputCompositionState.borderInfoList.emplace_back(std::move(info));
788 clientComposeTopLayer = true;
789 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000790 }
791
792 // In this situation we must client compose the top layer instead of using hwc
793 // because we want to draw the border above all else.
794 // This could potentially cause a bit of a performance regression if the top
795 // layer would have been rendered using hwc originally.
796 // TODO(b/227656283): Measure system's performance before enabling the border feature
797 if (clientComposeTopLayer) {
798 auto topLayer = getOutputLayerOrderedByZByIndex(getOutputLayerCount() - 1);
799 (topLayer->editState()).forceClientComposition = true;
800 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800801}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800802
Dan Stoza269dc4d2021-01-15 15:07:43 -0800803void Output::planComposition() {
804 if (!mPlanner || !getState().isEnabled) {
805 return;
806 }
807
808 ATRACE_CALL();
809 ALOGV(__FUNCTION__);
810
811 mPlanner->plan(getOutputLayersOrderedByZ());
812}
813
814void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
815 ATRACE_CALL();
816 ALOGV(__FUNCTION__);
817
818 if (!getState().isEnabled) {
819 return;
820 }
821
Ady Abraham3645e642021-04-20 18:39:00 -0700822 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700823 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham43065bd2021-12-10 17:22:15 -0800824 editState().expectedPresentTime = refreshArgs.expectedPresentTime;
Ady Abraham3645e642021-04-20 18:39:00 -0700825
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400826 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800827 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400828 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400829 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400830 bool overrideZ = false;
Robert Carrec8ccca2022-05-04 09:36:14 -0700831 uint64_t outputLayerHash = 0;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800832 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400833 if (layer == peekThroughLayer) {
834 // No longer needed, although it should not show up again, so
835 // resetting it is not truly needed either.
836 peekThroughLayer = nullptr;
837
838 // peekThroughLayer was already drawn ahead of its z order.
839 continue;
840 }
Dan Stoza6166c312021-01-15 16:34:05 -0800841 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400842 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400843 if (overrideInfo.buffer != nullptr) {
844 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800845 ALOGV("Skipping redundant buffer");
846 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400847 } else {
848 // First layer with the override buffer.
849 if (overrideInfo.peekThroughLayer) {
850 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400851
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400852 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400853 overrideZ = true;
854 includeGeometry = true;
855 constexpr bool isPeekingThrough = true;
856 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
857 isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700858 outputLayerHash ^= android::hashCombine(
859 reinterpret_cast<uint64_t>(&peekThroughLayer->getLayerFE()),
860 z, includeGeometry, overrideZ, isPeekingThrough,
861 peekThroughLayer->requiresClientComposition());
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400862 }
863
864 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800865 }
Dan Stoza6166c312021-01-15 16:34:05 -0800866 }
867
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400868 constexpr bool isPeekingThrough = false;
869 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700870 if (!skipLayer) {
871 outputLayerHash ^= android::hashCombine(
872 reinterpret_cast<uint64_t>(&layer->getLayerFE()),
873 z, includeGeometry, overrideZ, isPeekingThrough,
874 layer->requiresClientComposition());
875 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800876 }
Robert Carrec8ccca2022-05-04 09:36:14 -0700877 editState().outputLayerHash = outputLayerHash;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800878}
879
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800880compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
881 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
882 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100883 auto* compState = layer->getLayerFE().getCompositionState();
884
885 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
886 // want to force client composition because of the blur.
887 if (compState->sidebandStream != nullptr) {
888 return nullptr;
889 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000890 if (compState->isOpaque) {
891 continue;
892 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100893 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800894 layerRequestingBgComposition = layer;
895 }
896 }
897 return layerRequestingBgComposition;
898}
899
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800900void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
901 setColorProfile(pickColorProfile(refreshArgs));
902}
903
904// Returns a data space that fits all visible layers. The returned data space
905// can only be one of
906// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
907// - Dataspace::DISPLAY_P3
908// - Dataspace::DISPLAY_BT2020
909// The returned HDR data space is one of
910// - Dataspace::UNKNOWN
911// - Dataspace::BT2020_HLG
912// - Dataspace::BT2020_PQ
913ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
914 bool* outIsHdrClientComposition) const {
915 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
916 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
917
Vishnu Naire14c6b32022-08-06 04:20:15 +0000918 // An Output's layers may be stale when it is disabled. As a consequence, the layers returned by
919 // getOutputLayersOrderedByZ may not be in a valid state and it is not safe to access their
920 // properties. Return a default dataspace value in this case.
921 if (!getState().isEnabled) {
922 return ui::Dataspace::V0_SRGB;
923 }
924
Lloyd Pique01c77c12019-04-17 12:48:32 -0700925 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800926 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800927 case ui::Dataspace::V0_SCRGB:
928 case ui::Dataspace::V0_SCRGB_LINEAR:
929 case ui::Dataspace::BT2020:
930 case ui::Dataspace::BT2020_ITU:
931 case ui::Dataspace::BT2020_LINEAR:
932 case ui::Dataspace::DISPLAY_BT2020:
933 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
934 break;
935 case ui::Dataspace::DISPLAY_P3:
936 bestDataSpace = ui::Dataspace::DISPLAY_P3;
937 break;
938 case ui::Dataspace::BT2020_PQ:
939 case ui::Dataspace::BT2020_ITU_PQ:
940 bestDataSpace = ui::Dataspace::DISPLAY_P3;
941 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800942 *outIsHdrClientComposition =
943 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800944 break;
945 case ui::Dataspace::BT2020_HLG:
946 case ui::Dataspace::BT2020_ITU_HLG:
947 bestDataSpace = ui::Dataspace::DISPLAY_P3;
948 // When there's mixed PQ content and HLG content, we set the HDR
949 // data space to be BT2020_PQ and convert HLG to PQ.
950 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
951 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
952 }
953 break;
954 default:
955 break;
956 }
957 }
958
959 return bestDataSpace;
960}
961
962compositionengine::Output::ColorProfile Output::pickColorProfile(
963 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
964 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
965 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
966 ui::RenderIntent::COLORIMETRIC,
967 refreshArgs.colorSpaceAgnosticDataspace};
968 }
969
970 ui::Dataspace hdrDataSpace;
971 bool isHdrClientComposition = false;
972 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
973
974 switch (refreshArgs.forceOutputColorMode) {
975 case ui::ColorMode::SRGB:
976 bestDataSpace = ui::Dataspace::V0_SRGB;
977 break;
978 case ui::ColorMode::DISPLAY_P3:
979 bestDataSpace = ui::Dataspace::DISPLAY_P3;
980 break;
981 default:
982 break;
983 }
984
985 // respect hdrDataSpace only when there is no legacy HDR support
986 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
987 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
988 if (isHdr) {
989 bestDataSpace = hdrDataSpace;
990 }
991
992 ui::RenderIntent intent;
993 switch (refreshArgs.outputColorSetting) {
994 case OutputColorSetting::kManaged:
995 case OutputColorSetting::kUnmanaged:
996 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
997 : ui::RenderIntent::COLORIMETRIC;
998 break;
999 case OutputColorSetting::kEnhanced:
1000 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
1001 break;
1002 default: // vendor display color setting
1003 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
1004 break;
1005 }
1006
1007 ui::ColorMode outMode;
1008 ui::Dataspace outDataSpace;
1009 ui::RenderIntent outRenderIntent;
1010 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
1011 &outRenderIntent);
1012
1013 return ColorProfile{outMode, outDataSpace, outRenderIntent,
1014 refreshArgs.colorSpaceAgnosticDataspace};
1015}
1016
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001017void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001018 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001019 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -07001020 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001021 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001022
1023 // If nothing has changed (!dirty), don't recompose.
1024 // If something changed, but we don't currently have any visible layers,
1025 // and didn't when we last did a composition, then skip it this time.
1026 // The second rule does two things:
1027 // - When all layers are removed from a display, we'll emit one black
1028 // frame, then nothing more until we get new layers.
1029 // - When a display is created with a private layer stack, we won't
1030 // emit any black frames until a layer is added to the layer stack.
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001031 mMustRecompose = dirty && !(empty && wasEmpty);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001032
1033 const char flagPrefix[] = {'-', '+'};
1034 static_cast<void>(flagPrefix);
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001035 ALOGV("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __func__,
1036 mMustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
1037 flagPrefix[empty], flagPrefix[wasEmpty]);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001038
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001039 mRenderSurface->beginFrame(mMustRecompose);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001040
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001041 if (mMustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001042 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001043 }
1044}
1045
Lloyd Pique66d68602019-02-13 14:23:31 -08001046void Output::prepareFrame() {
1047 ATRACE_CALL();
1048 ALOGV(__FUNCTION__);
1049
Vishnu Naira3140382022-02-24 14:07:11 -08001050 auto& outputState = editState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001051 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -08001052 return;
1053 }
1054
Vishnu Naira3140382022-02-24 14:07:11 -08001055 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1056 bool success = chooseCompositionStrategy(&changes);
1057 resetCompositionStrategy();
Vishnu Nair9cf89262022-02-26 09:17:49 -08001058 outputState.strategyPrediction = CompositionStrategyPredictionState::DISABLED;
Vishnu Naira3140382022-02-24 14:07:11 -08001059 outputState.previousDeviceRequestedChanges = changes;
1060 outputState.previousDeviceRequestedSuccess = success;
1061 if (success) {
1062 applyCompositionStrategy(changes);
1063 }
1064 finishPrepareFrame();
1065}
Lloyd Pique66d68602019-02-13 14:23:31 -08001066
Vishnu Naira3140382022-02-24 14:07:11 -08001067std::future<bool> Output::chooseCompositionStrategyAsync(
1068 std::optional<android::HWComposer::DeviceRequestedChanges>* changes) {
1069 return mHwComposerAsyncWorker->send(
1070 [&, changes]() { return chooseCompositionStrategy(changes); });
1071}
1072
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001073GpuCompositionResult Output::prepareFrameAsync() {
Vishnu Naira3140382022-02-24 14:07:11 -08001074 ATRACE_CALL();
1075 ALOGV(__FUNCTION__);
1076 auto& state = editState();
1077 const auto& previousChanges = state.previousDeviceRequestedChanges;
1078 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1079 resetCompositionStrategy();
1080 auto hwcResult = chooseCompositionStrategyAsync(&changes);
1081 if (state.previousDeviceRequestedSuccess) {
1082 applyCompositionStrategy(previousChanges);
1083 }
1084 finishPrepareFrame();
1085
1086 base::unique_fd bufferFence;
1087 std::shared_ptr<renderengine::ExternalTexture> buffer;
1088 updateProtectedContentState();
1089 const bool dequeueSucceeded = dequeueRenderBuffer(&bufferFence, &buffer);
1090 GpuCompositionResult compositionResult;
1091 if (dequeueSucceeded) {
1092 std::optional<base::unique_fd> optFd =
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001093 composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001094 if (optFd) {
1095 compositionResult.fence = std::move(*optFd);
1096 }
Dan Stoza47437bb2021-01-15 16:21:07 -08001097 }
1098
Vishnu Naira3140382022-02-24 14:07:11 -08001099 auto chooseCompositionSuccess = hwcResult.get();
1100 const bool predictionSucceeded = dequeueSucceeded && changes == previousChanges;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001101 state.strategyPrediction = predictionSucceeded ? CompositionStrategyPredictionState::SUCCESS
1102 : CompositionStrategyPredictionState::FAIL;
Vishnu Naira3140382022-02-24 14:07:11 -08001103 if (!predictionSucceeded) {
1104 ATRACE_NAME("CompositionStrategyPredictionMiss");
1105 resetCompositionStrategy();
1106 if (chooseCompositionSuccess) {
1107 applyCompositionStrategy(changes);
1108 }
1109 finishPrepareFrame();
1110 // Track the dequeued buffer to reuse so we don't need to dequeue another one.
1111 compositionResult.buffer = buffer;
1112 } else {
1113 ATRACE_NAME("CompositionStrategyPredictionHit");
1114 }
1115 state.previousDeviceRequestedChanges = std::move(changes);
1116 state.previousDeviceRequestedSuccess = chooseCompositionSuccess;
1117 return compositionResult;
Lloyd Pique66d68602019-02-13 14:23:31 -08001118}
1119
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001120void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1121 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
1122 return;
1123 }
1124
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001125 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001126 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Vishnu Naira3140382022-02-24 14:07:11 -08001127 base::unique_fd bufferFence;
1128 std::shared_ptr<renderengine::ExternalTexture> buffer;
1129 updateProtectedContentState();
1130 dequeueRenderBuffer(&bufferFence, &buffer);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001131 static_cast<void>(composeSurfaces(dirtyRegion, buffer, bufferFence));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001132 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001133 }
1134 }
1135
1136 postFramebuffer();
1137
1138 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
1139
1140 prepareFrame();
1141}
1142
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001143void Output::finishFrame(GpuCompositionResult&& result) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001144 ATRACE_CALL();
1145 ALOGV(__FUNCTION__);
Vishnu Nair9cf89262022-02-26 09:17:49 -08001146 const auto& outputState = getState();
1147 if (!outputState.isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001148 return;
1149 }
1150
Vishnu Naira3140382022-02-24 14:07:11 -08001151 std::optional<base::unique_fd> optReadyFence;
1152 std::shared_ptr<renderengine::ExternalTexture> buffer;
1153 base::unique_fd bufferFence;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001154 if (outputState.strategyPrediction == CompositionStrategyPredictionState::SUCCESS) {
Vishnu Naira3140382022-02-24 14:07:11 -08001155 optReadyFence = std::move(result.fence);
1156 } else {
1157 if (result.bufferAvailable()) {
1158 buffer = std::move(result.buffer);
1159 bufferFence = std::move(result.fence);
1160 } else {
1161 updateProtectedContentState();
1162 if (!dequeueRenderBuffer(&bufferFence, &buffer)) {
1163 return;
1164 }
1165 }
1166 // Repaint the framebuffer (if needed), getting the optional fence for when
1167 // the composition completes.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001168 optReadyFence = composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001169 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001170 if (!optReadyFence) {
1171 return;
1172 }
1173
Matt Buckley50c44062022-01-17 20:48:10 +00001174 if (isPowerHintSessionEnabled()) {
1175 // get fence end time to know when gpu is complete in display
Ady Abrahamd11bade2022-08-01 16:18:03 -07001176 setHintSessionGpuFence(
1177 std::make_unique<FenceTime>(sp<Fence>::make(dup(optReadyFence->get()))));
Matt Buckley50c44062022-01-17 20:48:10 +00001178 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001179 // swap buffers (presentation)
1180 mRenderSurface->queueBuffer(std::move(*optReadyFence));
1181}
1182
Vishnu Naira3140382022-02-24 14:07:11 -08001183void Output::updateProtectedContentState() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001184 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001185 auto& renderEngine = getCompositionEngine().getRenderEngine();
1186 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1187
1188 // If we the display is secure, protected content support is enabled, and at
1189 // least one layer has protected content, we need to use a secure back
1190 // buffer.
1191 if (outputState.isSecure && supportsProtectedContent) {
1192 auto layers = getOutputLayersOrderedByZ();
1193 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1194 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1195 });
Patrick Williams8aed5d22022-10-31 22:18:10 +00001196 if (needsProtected != mRenderSurface->isProtected()) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001197 mRenderSurface->setProtected(needsProtected);
1198 }
1199 }
Vishnu Naira3140382022-02-24 14:07:11 -08001200}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001201
Vishnu Naira3140382022-02-24 14:07:11 -08001202bool Output::dequeueRenderBuffer(base::unique_fd* bufferFence,
1203 std::shared_ptr<renderengine::ExternalTexture>* tex) {
1204 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001205
1206 // If we aren't doing client composition on this output, but do have a
1207 // flipClientTarget request for this frame on this output, we still need to
1208 // dequeue a buffer.
Vishnu Naira3140382022-02-24 14:07:11 -08001209 if (outputState.usesClientComposition || outputState.flipClientTarget) {
1210 *tex = mRenderSurface->dequeueBuffer(bufferFence);
1211 if (*tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001212 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1213 "client composition for this frame",
1214 mName.c_str());
Vishnu Naira3140382022-02-24 14:07:11 -08001215 return false;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001216 }
1217 }
Vishnu Naira3140382022-02-24 14:07:11 -08001218 return true;
1219}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001220
Vishnu Naira3140382022-02-24 14:07:11 -08001221std::optional<base::unique_fd> Output::composeSurfaces(
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001222 const Region& debugRegion, std::shared_ptr<renderengine::ExternalTexture> tex,
1223 base::unique_fd& fd) {
Vishnu Naira3140382022-02-24 14:07:11 -08001224 ATRACE_CALL();
1225 ALOGV(__FUNCTION__);
1226
1227 const auto& outputState = getState();
Leon Scroggins III042fdba2023-01-04 10:53:07 -05001228 const TracedOrdinal<bool> hasClientComposition = {
1229 base::StringPrintf("hasClientComposition %s", mNamePlusId.c_str()),
1230 outputState.usesClientComposition};
Lloyd Pique688abd42019-02-15 15:42:24 -08001231 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001232 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001233 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001234 }
1235
Vishnu Naira3140382022-02-24 14:07:11 -08001236 if (tex == nullptr) {
1237 ALOGW("Buffer not valid for display [%s], bailing out of "
1238 "client composition for this frame",
1239 mName.c_str());
1240 return {};
1241 }
1242
Lloyd Pique688abd42019-02-15 15:42:24 -08001243 ALOGV("hasClientComposition");
1244
Patrick Williams7584c6a2022-10-29 02:10:58 +00001245 renderengine::DisplaySettings clientCompositionDisplay =
1246 generateClientCompositionDisplaySettings();
Lloyd Pique688abd42019-02-15 15:42:24 -08001247
Lloyd Pique688abd42019-02-15 15:42:24 -08001248 // Generate the client composition requests for the layers on this output.
Vishnu Naira3140382022-02-24 14:07:11 -08001249 auto& renderEngine = getCompositionEngine().getRenderEngine();
1250 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
Robert Carrccab4242021-09-28 16:53:03 -07001251 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001252 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001253 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001254 clientCompositionDisplay.outputDataspace,
1255 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001256 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1257
Vishnu Naira3140382022-02-24 14:07:11 -08001258 OutputCompositionState& outputCompositionState = editState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001259 // Check if the client composition requests were rendered into the provided graphic buffer. If
1260 // so, we can reuse the buffer and avoid client composition.
1261 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001262 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1263 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001264 clientCompositionLayers)) {
Vishnu Naira3140382022-02-24 14:07:11 -08001265 ATRACE_NAME("ClientCompositionCacheHit");
Vishnu Nair9b079a22020-01-21 14:36:08 -08001266 outputCompositionState.reusedClientComposition = true;
1267 setExpensiveRenderingExpected(false);
Vishnu Nair3a49f0a2022-07-29 21:52:53 +00001268 // b/239944175 pass the fence associated with the buffer.
1269 return base::unique_fd(std::move(fd));
Vishnu Nair9b079a22020-01-21 14:36:08 -08001270 }
Vishnu Naira3140382022-02-24 14:07:11 -08001271 ATRACE_NAME("ClientCompositionCacheMiss");
Alec Mouria90a5702021-04-16 16:36:21 +00001272 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001273 clientCompositionLayers);
1274 }
1275
Lloyd Pique688abd42019-02-15 15:42:24 -08001276 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001277 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1278 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1279 // because high frequency consumes extra battery.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001280 const bool expensiveRenderingExpected =
Leon Scroggins IIIcf17ebc2022-03-03 14:54:00 -05001281 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1282 [outputDataspace =
1283 clientCompositionDisplay.outputDataspace](const auto& layer) {
1284 return layer.sourceDataspace != outputDataspace;
1285 });
Lloyd Pique688abd42019-02-15 15:42:24 -08001286 if (expensiveRenderingExpected) {
1287 setExpensiveRenderingExpected(true);
1288 }
1289
Sally Qi59a9f502021-10-12 18:53:23 +00001290 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1291 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001292 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001293 std::back_inserter(clientRenderEngineLayers),
1294 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1295 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001296 });
1297
Alec Mourie4034bb2019-11-19 12:45:54 -08001298 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001299 // Only use the framebuffer cache when rendering to an internal display
1300 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1301 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1302 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1303 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1304 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001305 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001306
Patrick Williams2e9748f2022-08-09 22:48:18 +00001307 auto fenceResult = renderEngine
1308 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
1309 useFramebufferCache, std::move(fd))
1310 .get();
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001311
1312 if (mClientCompositionRequestCache && fenceStatus(fenceResult) != NO_ERROR) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001313 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001314 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001315 }
1316
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001317 const auto fence = std::move(fenceResult).value_or(Fence::NO_FENCE);
1318
Patrick Williams74c0bf62022-11-02 23:59:26 +00001319 if (auto timeStats = getCompositionEngine().getTimeStats()) {
1320 if (fence->isValid()) {
1321 timeStats->recordRenderEngineDuration(renderEngineStart,
1322 std::make_shared<FenceTime>(fence));
1323 } else {
1324 timeStats->recordRenderEngineDuration(renderEngineStart, systemTime());
1325 }
Alec Mourie4034bb2019-11-19 12:45:54 -08001326 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001327
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001328 for (auto* clientComposedLayer : clientCompositionLayersFE) {
1329 clientComposedLayer->setWasClientComposed(fence);
Robert Carrccab4242021-09-28 16:53:03 -07001330 }
1331
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001332 return base::unique_fd(fence->dup());
Lloyd Pique688abd42019-02-15 15:42:24 -08001333}
1334
Patrick Williams7584c6a2022-10-29 02:10:58 +00001335renderengine::DisplaySettings Output::generateClientCompositionDisplaySettings() const {
1336 const auto& outputState = getState();
1337
1338 renderengine::DisplaySettings clientCompositionDisplay;
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001339 clientCompositionDisplay.namePlusId = mNamePlusId;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001340 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1341 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
1342 clientCompositionDisplay.orientation =
1343 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
1344 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1345 ? outputState.dataspace
1346 : ui::Dataspace::UNKNOWN;
1347
1348 // If we have a valid current display brightness use that, otherwise fall back to the
1349 // display's max desired
1350 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
1351 ? outputState.displayBrightnessNits
1352 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1353 clientCompositionDisplay.maxLuminance =
1354 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1355 clientCompositionDisplay.targetLuminanceNits =
1356 outputState.clientTargetBrightness * outputState.displayBrightnessNits;
1357 clientCompositionDisplay.dimmingStage = outputState.clientTargetDimmingStage;
1358 clientCompositionDisplay.renderIntent =
1359 static_cast<aidl::android::hardware::graphics::composer3::RenderIntent>(
1360 outputState.renderIntent);
1361
1362 // Compute the global color transform matrix.
1363 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
1364 for (auto& info : outputState.borderInfoList) {
1365 renderengine::BorderRenderInfo borderInfo;
1366 borderInfo.width = info.width;
1367 borderInfo.color = info.color;
1368 borderInfo.combinedRegion = info.combinedRegion;
1369 clientCompositionDisplay.borderInfoList.emplace_back(std::move(borderInfo));
1370 }
1371 clientCompositionDisplay.deviceHandlesColorTransform =
1372 outputState.usesDeviceComposition || getSkipColorTransform();
1373 return clientCompositionDisplay;
1374}
1375
Vishnu Nair9b079a22020-01-21 14:36:08 -08001376std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001377 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001378 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001379 ALOGV("Rendering client layers");
1380
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001381 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001382 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001383 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001384
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001385 bool disableBlurs = false;
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001386 uint64_t previousOverrideBufferId = 0;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001387
Lloyd Pique01c77c12019-04-17 12:48:32 -07001388 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001389 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001390 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001391 auto& layerFE = layer->getLayerFE();
Robert Carr05da0082022-05-25 23:29:34 -07001392 layerFE.setWasClientComposed(nullptr);
Lloyd Pique688abd42019-02-15 15:42:24 -08001393
Lloyd Piquea2468662019-03-07 21:31:06 -08001394 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001395 ALOGV("Layer: %s", layerFE.getDebugName());
1396 if (clip.isEmpty()) {
1397 ALOGV(" Skipping for empty clip");
1398 firstLayer = false;
1399 continue;
1400 }
1401
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001402 disableBlurs |= layerFEState->sidebandStream != nullptr;
1403
Vishnu Naira483b4a2019-12-12 15:07:52 -08001404 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001405
1406 // We clear the client target for non-client composed layers if
1407 // requested by the HWC. We skip this if the layer is not an opaque
1408 // rectangle, as by definition the layer must blend with whatever is
1409 // underneath. We also skip the first layer as the buffer target is
1410 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001411 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001412 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001413
1414 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1415
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001416 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1417 // composing the non-shadow content and only draw the shadows.
1418 const bool realContentIsVisible = clientComposition &&
1419 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1420
Lloyd Pique688abd42019-02-15 15:42:24 -08001421 if (clientComposition || clearClientComposition) {
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001422 if (auto overrideSettings = layer->getOverrideCompositionSettings()) {
1423 if (overrideSettings->bufferId != previousOverrideBufferId) {
1424 previousOverrideBufferId = overrideSettings->bufferId;
1425 clientCompositionLayers.push_back(std::move(*overrideSettings));
Huihong Luo91ac3b52021-04-08 11:07:41 -07001426 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1427 } else {
1428 ALOGV("Skipping redundant override buffer for [%s] in RE",
1429 layer->getLayerFE().getDebugName());
1430 }
Dan Stoza6166c312021-01-15 16:34:05 -08001431 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001432 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1433 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1434 : (layer->getState().overrideInfo.disableBackgroundBlur
1435 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1436 BlurRegionsOnly
1437 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1438 Enabled);
1439 compositionengine::LayerFE::ClientCompositionTargetSettings
1440 targetSettings{.clip = clip,
Patrick Williams278a88f2023-01-27 16:52:40 -06001441 .needsFiltering = layer->needsFiltering() ||
Alec Mourif54453c2021-05-13 16:28:28 -07001442 outputState.needsFiltering,
1443 .isSecure = outputState.isSecure,
1444 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001445 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001446 .dataspace = outputDataspace,
1447 .realContentIsVisible = realContentIsVisible,
1448 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001449 .blurSetting = blurSetting,
Vishnu Naire14c6b32022-08-06 04:20:15 +00001450 .whitePointNits = layerState.whitePointNits,
1451 .treat170mAsSrgb = outputState.treat170mAsSrgb};
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001452 if (auto clientCompositionSettings =
1453 layerFE.prepareClientComposition(targetSettings)) {
1454 clientCompositionLayers.push_back(std::move(*clientCompositionSettings));
1455 if (realContentIsVisible) {
1456 layer->editState().clientCompositionTimestamp = systemTime();
1457 }
Dan Stoza6166c312021-01-15 16:34:05 -08001458 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001459 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001460
Tianhua Sunf91f1402022-05-09 05:45:46 +00001461 if (clientComposition) {
1462 outLayerFEs.push_back(&layerFE);
1463 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001464 }
1465
1466 firstLayer = false;
1467 }
1468
1469 return clientCompositionLayers;
1470}
1471
1472void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001473 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001474 if (flashRegion.isEmpty()) {
1475 return;
1476 }
1477
Vishnu Nair9b079a22020-01-21 14:36:08 -08001478 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001479 layerSettings.source.buffer.buffer = nullptr;
1480 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1481 layerSettings.alpha = half(1.0);
1482
1483 for (const auto& rect : flashRegion) {
1484 layerSettings.geometry.boundaries = rect.toFloatRect();
1485 clientCompositionLayers.push_back(layerSettings);
1486 }
1487}
1488
1489void Output::setExpensiveRenderingExpected(bool) {
1490 // The base class does nothing with this call.
1491}
1492
Matt Buckley50c44062022-01-17 20:48:10 +00001493void Output::setHintSessionGpuFence(std::unique_ptr<FenceTime>&&) {
1494 // The base class does nothing with this call.
1495}
1496
1497bool Output::isPowerHintSessionEnabled() {
1498 return false;
1499}
1500
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001501void Output::postFramebuffer() {
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001502 ATRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001503 ALOGV(__FUNCTION__);
1504
1505 if (!getState().isEnabled) {
1506 return;
1507 }
1508
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001509 auto& outputState = editState();
1510 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001511
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001512 auto frame = presentAndGetFrameFences();
1513
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001514 mRenderSurface->onPresentDisplayCompleted();
1515
Lloyd Pique01c77c12019-04-17 12:48:32 -07001516 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001517 // The layer buffer from the previous frame (if any) is released
1518 // by HWC only when the release fence from this frame (if any) is
1519 // signaled. Always get the release fence from HWC first.
1520 sp<Fence> releaseFence = Fence::NO_FENCE;
1521
1522 if (auto hwcLayer = layer->getHwcLayer()) {
1523 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1524 releaseFence = f->second;
1525 }
1526 }
1527
1528 // If the layer was client composited in the previous frame, we
1529 // need to merge with the previous client target acquire fence.
1530 // Since we do not track that, always merge with the current
1531 // client target acquire fence when it is available, even though
1532 // this is suboptimal.
1533 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001534 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001535 releaseFence =
1536 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1537 }
Sally Qi59a9f502021-10-12 18:53:23 +00001538 layer->getLayerFE().onLayerDisplayed(
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001539 ftl::yield<FenceResult>(std::move(releaseFence)).share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001540 }
1541
1542 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001543 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001544 // supply them with the present fence.
1545 for (auto& weakLayer : mReleasedLayers) {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001546 if (const auto layer = weakLayer.promote()) {
1547 layer->onLayerDisplayed(ftl::yield<FenceResult>(frame.presentFence).share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001548 }
1549 }
1550
1551 // Clear out the released layers now that we're done with them.
1552 mReleasedLayers.clear();
1553}
1554
Alec Mouriaa831582021-06-07 16:23:01 -07001555void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001556 if (mPlanner) {
Brian Johnson869e28f2022-08-12 22:20:19 +00001557 mPlanner->renderCachedSets(getState(), refreshArgs.scheduledFrameTime,
1558 getState().usesDeviceComposition || getSkipColorTransform());
Dan Stoza6166c312021-01-15 16:34:05 -08001559 }
1560}
1561
Lloyd Pique32cbe282018-10-19 13:09:22 -07001562void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001563 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001564 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001565}
1566
Vishnu Naira3140382022-02-24 14:07:11 -08001567void Output::resetCompositionStrategy() {
Lloyd Pique66d68602019-02-13 14:23:31 -08001568 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001569 auto& outputState = editState();
1570 outputState.usesClientComposition = true;
1571 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001572 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001573}
1574
Lloyd Pique688abd42019-02-15 15:42:24 -08001575bool Output::getSkipColorTransform() const {
1576 return true;
1577}
1578
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001579compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1580 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001581 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001582 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1583 }
1584 return result;
1585}
1586
Vishnu Naira3140382022-02-24 14:07:11 -08001587void Output::setPredictCompositionStrategy(bool predict) {
1588 if (predict) {
1589 mHwComposerAsyncWorker = std::make_unique<HwcAsyncWorker>();
1590 } else {
1591 mHwComposerAsyncWorker.reset(nullptr);
1592 }
1593}
1594
Alec Mouridda07d92022-04-25 22:39:25 +00001595void Output::setTreat170mAsSrgb(bool enable) {
1596 editState().treat170mAsSrgb = enable;
1597}
1598
Vishnu Naira3140382022-02-24 14:07:11 -08001599bool Output::canPredictCompositionStrategy(const CompositionRefreshArgs& refreshArgs) {
Robert Carrec8ccca2022-05-04 09:36:14 -07001600 uint64_t lastOutputLayerHash = getState().lastOutputLayerHash;
1601 uint64_t outputLayerHash = getState().outputLayerHash;
1602 editState().lastOutputLayerHash = outputLayerHash;
1603
Vishnu Naira3140382022-02-24 14:07:11 -08001604 if (!getState().isEnabled || !mHwComposerAsyncWorker) {
1605 ALOGV("canPredictCompositionStrategy disabled");
1606 return false;
1607 }
1608
1609 if (!getState().previousDeviceRequestedChanges) {
1610 ALOGV("canPredictCompositionStrategy previous changes not available");
1611 return false;
1612 }
1613
1614 if (!mRenderSurface->supportsCompositionStrategyPrediction()) {
1615 ALOGV("canPredictCompositionStrategy surface does not support");
1616 return false;
1617 }
1618
1619 if (refreshArgs.devOptFlashDirtyRegionsDelay) {
1620 ALOGV("canPredictCompositionStrategy devOptFlashDirtyRegionsDelay");
1621 return false;
1622 }
1623
Robert Carrec8ccca2022-05-04 09:36:14 -07001624 if (lastOutputLayerHash != outputLayerHash) {
1625 ALOGV("canPredictCompositionStrategy output layers changed");
1626 return false;
1627 }
1628
Vishnu Naira3140382022-02-24 14:07:11 -08001629 // If no layer uses clientComposition, then don't predict composition strategy
1630 // because we have less work to do in parallel.
1631 if (!anyLayersRequireClientComposition()) {
1632 ALOGV("canPredictCompositionStrategy no layer uses clientComposition");
1633 return false;
1634 }
1635
Robert Carrec8ccca2022-05-04 09:36:14 -07001636 return true;
Vishnu Naira3140382022-02-24 14:07:11 -08001637}
1638
1639bool Output::anyLayersRequireClientComposition() const {
1640 const auto layers = getOutputLayersOrderedByZ();
1641 return std::any_of(layers.begin(), layers.end(),
1642 [](const auto& layer) { return layer->requiresClientComposition(); });
1643}
1644
1645void Output::finishPrepareFrame() {
1646 const auto& state = getState();
1647 if (mPlanner) {
1648 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
1649 }
1650 mRenderSurface->prepareFrame(state.usesClientComposition, state.usesDeviceComposition);
1651}
1652
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001653bool Output::mustRecompose() const {
1654 return mMustRecompose;
1655}
1656
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001657} // namespace impl
1658} // namespace android::compositionengine