blob: 5e40802e935b667e2b49fc8d81ea2a0948cdf4d6 [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>
Lloyd Pique32cbe282018-10-19 13:09:22 -070025#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070026#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080027#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070028#include <compositionengine/impl/OutputLayerCompositionState.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080029#include <compositionengine/impl/planner/Planner.h>
30
Alec Mouria90a5702021-04-16 16:36:21 +000031#include <thread>
32
33#include "renderengine/ExternalTexture.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080034
35// TODO(b/129481165): remove the #pragma below and fix conversion issues
36#pragma clang diagnostic push
37#pragma clang diagnostic ignored "-Wconversion"
38
Lloyd Pique688abd42019-02-15 15:42:24 -080039#include <renderengine/DisplaySettings.h>
40#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080041
42// TODO(b/129481165): remove the #pragma below and fix conversion issues
43#pragma clang diagnostic pop // ignored "-Wconversion"
44
Dan Stoza269dc4d2021-01-15 15:07:43 -080045#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070046#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080047#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080048#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070049
Lloyd Pique688abd42019-02-15 15:42:24 -080050#include "TracedOrdinal.h"
51
Lloyd Piquefeb73d72018-12-04 17:23:44 -080052namespace android::compositionengine {
53
54Output::~Output() = default;
55
56namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070057
Lloyd Piquec29e4c62019-03-07 21:48:19 -080058namespace {
59
60template <typename T>
61class Reversed {
62public:
63 explicit Reversed(const T& container) : mContainer(container) {}
64 auto begin() { return mContainer.rbegin(); }
65 auto end() { return mContainer.rend(); }
66
67private:
68 const T& mContainer;
69};
70
71// Helper for enumerating over a container in reverse order
72template <typename T>
73Reversed<T> reversed(const T& c) {
74 return Reversed<T>(c);
75}
76
Marin Shalamanovb15d2272020-09-17 21:41:52 +020077struct ScaleVector {
78 float x;
79 float y;
80};
81
82// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
83// where to' will have the same size as "to". In the case where "from" and "to"
84// start at the origin to'=to.
85ScaleVector getScale(const Rect& from, const Rect& to) {
86 return {.x = static_cast<float>(to.width()) / from.width(),
87 .y = static_cast<float>(to.height()) / from.height()};
88}
89
Lloyd Piquec29e4c62019-03-07 21:48:19 -080090} // namespace
91
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070092std::shared_ptr<Output> createOutput(
93 const compositionengine::CompositionEngine& compositionEngine) {
94 return createOutputTemplated<Output>(compositionEngine);
95}
Lloyd Pique32cbe282018-10-19 13:09:22 -070096
97Output::~Output() = default;
98
Lloyd Pique32cbe282018-10-19 13:09:22 -070099bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700100 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
101 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102}
103
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700104std::optional<DisplayId> Output::getDisplayId() const {
105 return {};
106}
107
Lloyd Pique32cbe282018-10-19 13:09:22 -0700108const std::string& Output::getName() const {
109 return mName;
110}
111
112void Output::setName(const std::string& name) {
113 mName = name;
114}
115
116void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700117 auto& outputState = editState();
118 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700119 return;
120 }
121
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700122 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700123 dirtyEntireOutput();
124}
125
Alec Mouri023c1882021-05-08 16:36:33 -0700126void Output::setLayerCachingEnabled(bool enabled) {
127 if (enabled == (mPlanner != nullptr)) {
128 return;
129 }
130
131 if (enabled) {
Alec Mouridf6201b2021-06-01 16:20:42 -0700132 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
Alec Mouri023c1882021-05-08 16:36:33 -0700133 if (mRenderSurface) {
134 mPlanner->setDisplaySize(mRenderSurface->getSize());
135 }
136 } else {
137 mPlanner.reset();
138 }
Alec Mouric773472b2021-05-19 14:29:05 -0700139
140 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
141 if (!outputLayer) {
142 continue;
143 }
144
145 outputLayer->editState().overrideInfo = {};
146 }
Alec Mouri023c1882021-05-08 16:36:33 -0700147}
148
Ady Abrahamdb036a82021-07-16 14:18:34 -0700149void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
150 if (mPlanner) {
151 mPlanner->setTexturePoolEnabled(enabled);
152 }
153}
154
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200155void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
156 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700157 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200158
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200159 outputState.displaySpace.orientation = orientation;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200160 LOG_FATAL_IF(outputState.displaySpace.bounds == Rect::INVALID_RECT,
161 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200162
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200163 // Compute orientedDisplaySpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200164 ui::Size orientedSize = outputState.displaySpace.bounds.getSize();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200165 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200166 std::swap(orientedSize.width, orientedSize.height);
167 }
168 outputState.orientedDisplaySpace.bounds = Rect(orientedSize);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200169 outputState.orientedDisplaySpace.content = orientedDisplaySpaceRect;
170
171 // Compute displaySpace.content
172 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
173 ui::Transform rotation;
174 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
175 const auto displaySize = outputState.displaySpace.bounds;
176 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
177 }
178 outputState.displaySpace.content = rotation.transform(orientedDisplaySpaceRect);
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200179
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200180 // Compute framebufferSpace
181 outputState.framebufferSpace.orientation = orientation;
182 LOG_FATAL_IF(outputState.framebufferSpace.bounds == Rect::INVALID_RECT,
183 "The framebuffer bounds are unknown.");
184 const auto scale =
Marin Shalamanov209ae612020-10-01 00:17:39 +0200185 getScale(outputState.displaySpace.bounds, outputState.framebufferSpace.bounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200186 outputState.framebufferSpace.content = outputState.displaySpace.content.scale(scale.x, scale.y);
187
188 // Compute layerStackSpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200189 outputState.layerStackSpace.content = layerStackSpaceRect;
190 outputState.layerStackSpace.bounds = layerStackSpaceRect;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200191
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200192 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
193 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700194 dirtyEntireOutput();
195}
196
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200197void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700198 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200199
200 auto& state = editState();
201
202 // Update framebuffer space
203 const Rect newBounds(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200204 state.framebufferSpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200205
206 // Update display space
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200207 state.displaySpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200208 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
209
210 // Update oriented display space
211 const auto orientation = state.displaySpace.orientation;
212 ui::Size orientedSize = size;
213 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
214 std::swap(orientedSize.width, orientedSize.height);
215 }
216 const Rect newOrientedBounds(orientedSize);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200217 state.orientedDisplaySpace.bounds = newOrientedBounds;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700218
Dan Stoza6166c312021-01-15 16:34:05 -0800219 if (mPlanner) {
220 mPlanner->setDisplaySize(size);
221 }
222
Lloyd Pique32cbe282018-10-19 13:09:22 -0700223 dirtyEntireOutput();
224}
225
Garfield Tan54edd912020-10-21 16:31:41 -0700226ui::Transform::RotationFlags Output::getTransformHint() const {
227 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
228}
229
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700230void Output::setLayerFilter(ui::LayerFilter filter) {
231 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700232 dirtyEntireOutput();
233}
234
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800235void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700236 auto& colorTransformMatrix = editState().colorTransformMatrix;
237 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700238 return;
239 }
240
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700241 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800242
243 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700244}
245
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800246void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700247 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800248 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
249 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800250
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700251 auto& outputState = editState();
252 if (outputState.colorMode == colorProfile.mode &&
253 outputState.dataspace == colorProfile.dataspace &&
254 outputState.renderIntent == colorProfile.renderIntent &&
255 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800256 return;
257 }
258
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700259 outputState.colorMode = colorProfile.mode;
260 outputState.dataspace = colorProfile.dataspace;
261 outputState.renderIntent = colorProfile.renderIntent;
262 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700263
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800264 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700265
Lloyd Pique32cbe282018-10-19 13:09:22 -0700266 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800267 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
268 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800269
270 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700271}
272
John Reckac09e452021-04-07 16:35:37 -0400273void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
274 auto& outputState = editState();
275 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
276 outputState.displayBrightnessNits == displayBrightnessNits) {
277 // Nothing changed
278 return;
279 }
280 outputState.sdrWhitePointNits = sdrWhitePointNits;
281 outputState.displayBrightnessNits = displayBrightnessNits;
282 dirtyEntireOutput();
283}
284
Lloyd Pique32cbe282018-10-19 13:09:22 -0700285void Output::dump(std::string& out) const {
286 using android::base::StringAppendF;
287
288 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
289
290 out.append("\n ");
291
292 dumpBase(out);
293}
294
295void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700296 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700297
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700298 if (mDisplayColorProfile) {
299 mDisplayColorProfile->dump(out);
300 } else {
301 out.append(" No display color profile!\n");
302 }
303
Lloyd Pique31cb2942018-10-19 17:23:03 -0700304 if (mRenderSurface) {
305 mRenderSurface->dump(out);
306 } else {
307 out.append(" No render surface!\n");
308 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800309
Lloyd Pique01c77c12019-04-17 12:48:32 -0700310 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
311 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800312 if (!outputLayer) {
313 continue;
314 }
315 outputLayer->dump(out);
316 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700317}
318
Dan Stoza269dc4d2021-01-15 15:07:43 -0800319void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
320 if (!mPlanner) {
321 base::StringAppendF(&out, "Planner is disabled\n");
322 return;
323 }
324 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
325 mPlanner->dump(args, out);
326}
327
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700328compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
329 return mDisplayColorProfile.get();
330}
331
332void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
333 mDisplayColorProfile = std::move(mode);
334}
335
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800336const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
337 return mReleasedLayers;
338}
339
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700340void Output::setDisplayColorProfileForTest(
341 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
342 mDisplayColorProfile = std::move(mode);
343}
344
Lloyd Pique31cb2942018-10-19 17:23:03 -0700345compositionengine::RenderSurface* Output::getRenderSurface() const {
346 return mRenderSurface.get();
347}
348
349void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
350 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800351 const auto size = mRenderSurface->getSize();
352 editState().framebufferSpace.bounds = Rect(size);
353 if (mPlanner) {
354 mPlanner->setDisplaySize(size);
355 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700356 dirtyEntireOutput();
357}
358
Vishnu Nair9b079a22020-01-21 14:36:08 -0800359void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
360 if (cacheSize == 0) {
361 mClientCompositionRequestCache.reset();
362 } else {
363 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
364 }
365};
366
Lloyd Pique31cb2942018-10-19 17:23:03 -0700367void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
368 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700369}
370
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000371Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700372 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200373 Region dirty(outputState.layerStackSpace.content);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000374 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700375 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700376 }
377 return dirty;
378}
379
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700380bool Output::includesLayer(ui::LayerFilter filter) const {
381 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700382}
383
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700384bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800385 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700386 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800387}
388
Lloyd Piquedf336d92019-03-07 21:38:42 -0800389std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800390 const sp<LayerFE>& layerFE) const {
391 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800392}
393
Lloyd Piquede196652020-01-22 17:29:58 -0800394compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
395 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700396 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800397}
398
Lloyd Pique01c77c12019-04-17 12:48:32 -0700399std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800400 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700401 for (size_t i = 0; i < getOutputLayerCount(); i++) {
402 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800403 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700404 return i;
405 }
406 }
407 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800408}
409
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800410void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
411 mReleasedLayers = std::move(layers);
412}
413
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800414void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
415 LayerFESet& geomSnapshots) {
416 ATRACE_CALL();
417 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800418
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800419 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800420}
421
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800422void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800423 ATRACE_CALL();
424 ALOGV(__FUNCTION__);
425
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800426 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800427 updateCompositionState(refreshArgs);
428 planComposition();
429 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800430 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800431 beginFrame();
432 prepareFrame();
433 devOptRepaintFlash(refreshArgs);
434 finishFrame(refreshArgs);
435 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700436 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800437}
438
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800439void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
440 LayerFESet& layerFESet) {
441 ATRACE_CALL();
442 ALOGV(__FUNCTION__);
443
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700444 auto& outputState = editState();
445
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800446 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700447 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800448 return;
449 }
450
451 // Process the layers to determine visibility and coverage
452 compositionengine::Output::CoverageState coverage{layerFESet};
453 collectVisibleLayers(refreshArgs, coverage);
454
455 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700456 const ui::Transform& tr = outputState.transform;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200457 Region undefinedRegion{outputState.displaySpace.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800458 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
459
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700460 outputState.undefinedRegion = undefinedRegion;
461 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800462}
463
464void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
465 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800466 // Evaluate the layers from front to back to determine what is visible. This
467 // also incrementally calculates the coverage information for each layer as
468 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800469 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700470 // Incrementally process the coverage for each layer
471 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800472
473 // TODO(b/121291683): Stop early if the output is completely covered and
474 // no more layers could even be visible underneath the ones on top.
475 }
476
Lloyd Pique01c77c12019-04-17 12:48:32 -0700477 setReleasedLayers(refreshArgs);
478
479 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800480}
481
Lloyd Piquede196652020-01-22 17:29:58 -0800482void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700483 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800484 // Ensure we have a snapshot of the basic geometry layer state. Limit the
485 // snapshots to once per frame for each candidate layer, as layers may
486 // appear on multiple outputs.
487 if (!coverage.latchedLayers.count(layerFE)) {
488 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800489 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800490 }
491
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700492 // Only consider the layers on this output
493 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800494 return;
495 }
496
497 // Obtain a read-only pointer to the front-end layer state
498 const auto* layerFEState = layerFE->getCompositionState();
499 if (CC_UNLIKELY(!layerFEState)) {
500 return;
501 }
502
503 // handle hidden surfaces by setting the visible region to empty
504 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700505 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800506 }
507
508 /*
509 * opaqueRegion: area of a surface that is fully opaque.
510 */
511 Region opaqueRegion;
512
513 /*
514 * visibleRegion: area of a surface that is visible on screen and not fully
515 * transparent. This is essentially the layer's footprint minus the opaque
516 * regions above it. Areas covered by a translucent surface are considered
517 * visible.
518 */
519 Region visibleRegion;
520
521 /*
522 * coveredRegion: area of a surface that is covered by all visible regions
523 * above it (which includes the translucent areas).
524 */
525 Region coveredRegion;
526
527 /*
528 * transparentRegion: area of a surface that is hinted to be completely
529 * transparent. This is only used to tell when the layer has no visible non-
530 * transparent regions and can be removed from the layer list. It does not
531 * affect the visibleRegion of this layer or any layers beneath it. The hint
532 * may not be correct if apps don't respect the SurfaceView restrictions
533 * (which, sadly, some don't).
534 */
535 Region transparentRegion;
536
Vishnu Naira483b4a2019-12-12 15:07:52 -0800537 /*
538 * shadowRegion: Region cast by the layer's shadow.
539 */
540 Region shadowRegion;
541
Lloyd Piquede196652020-01-22 17:29:58 -0800542 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800543
544 // Get the visible region
545 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
546 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800547 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800548 visibleRegion.set(visibleRect);
549
Lloyd Piquede196652020-01-22 17:29:58 -0800550 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800551 // if the layer casts a shadow, offset the layers visible region and
552 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800553 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800554 Rect visibleRectWithShadows(visibleRect);
555 visibleRectWithShadows.inset(inset, inset, inset, inset);
556 visibleRegion.set(visibleRectWithShadows);
557 shadowRegion = visibleRegion.subtract(visibleRect);
558 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800559
560 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700561 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800562 }
563
564 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800565 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800566 if (tr.preserveRects()) {
567 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800568 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800569 } else {
570 // transformation too complex, can't do the
571 // transparent region optimization.
572 transparentRegion.clear();
573 }
574 }
575
576 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800577 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800578 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800579 // If we one of the simple category of transforms (0/90/180/270 rotation
580 // + any flip), then the opaque region is the layer's footprint.
581 // Otherwise we don't try and compute the opaque region since there may
582 // be errors at the edges, and we treat the entire layer as
583 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800584 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800585 }
586
587 // Clip the covered region to the visible region
588 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
589
590 // Update accumAboveCoveredLayers for next (lower) layer
591 coverage.aboveCoveredLayers.orSelf(visibleRegion);
592
593 // subtract the opaque region covered by the layers above us
594 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
595
596 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700597 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800598 }
599
600 // Get coverage information for the layer as previously displayed,
601 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800602 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700603 auto prevOutputLayer =
604 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800605
606 // Get coverage information for the layer as previously displayed
607 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
608 const Region kEmptyRegion;
609 const Region& oldVisibleRegion =
610 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
611 const Region& oldCoveredRegion =
612 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
613
614 // compute this layer's dirty region
615 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800616 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800617 // we need to invalidate the whole region
618 dirty = visibleRegion;
619 // as well, as the old visible region
620 dirty.orSelf(oldVisibleRegion);
621 } else {
622 /* compute the exposed region:
623 * the exposed region consists of two components:
624 * 1) what's VISIBLE now and was COVERED before
625 * 2) what's EXPOSED now less what was EXPOSED before
626 *
627 * note that (1) is conservative, we start with the whole visible region
628 * but only keep what used to be covered by something -- which mean it
629 * may have been exposed.
630 *
631 * (2) handles areas that were not covered by anything but got exposed
632 * because of a resize.
633 *
634 */
635 const Region newExposed = visibleRegion - coveredRegion;
636 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
637 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
638 }
639 dirty.subtractSelf(coverage.aboveOpaqueLayers);
640
641 // accumulate to the screen dirty region
642 coverage.dirtyRegion.orSelf(dirty);
643
644 // Update accumAboveOpaqueLayers for next (lower) layer
645 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
646
647 // Compute the visible non-transparent region
648 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
649
Vishnu Naira483b4a2019-12-12 15:07:52 -0800650 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800651 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700652 const auto& outputState = getState();
653 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200654 drawRegion.andSelf(outputState.displaySpace.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800655 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700656 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800657 }
658
Vishnu Naira483b4a2019-12-12 15:07:52 -0800659 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
660
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800661 // The layer is visible. Either reuse the existing outputLayer if we have
662 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800663 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800664
665 // Store the layer coverage information into the layer state as some of it
666 // is useful later.
667 auto& outputLayerState = result->editState();
668 outputLayerState.visibleRegion = visibleRegion;
669 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
670 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200671 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
672 visibleNonShadowRegion.intersect(outputState.layerStackSpace.content));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800673 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800674}
675
676void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
677 // The base class does nothing with this call.
678}
679
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800680void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700681 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800682 layer->getLayerFE().prepareCompositionState(
683 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
684 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800685 }
686}
687
Dan Stoza269dc4d2021-01-15 15:07:43 -0800688void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800689 ATRACE_CALL();
690 ALOGV(__FUNCTION__);
691
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800692 if (!getState().isEnabled) {
693 return;
694 }
695
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800696 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
697 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
698
Lloyd Pique01c77c12019-04-17 12:48:32 -0700699 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700700 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800701 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200702 forceClientComposition,
703 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800704
705 if (mLayerRequestingBackgroundBlur == layer) {
706 forceClientComposition = false;
707 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800708 }
709}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800710
Dan Stoza269dc4d2021-01-15 15:07:43 -0800711void Output::planComposition() {
712 if (!mPlanner || !getState().isEnabled) {
713 return;
714 }
715
716 ATRACE_CALL();
717 ALOGV(__FUNCTION__);
718
719 mPlanner->plan(getOutputLayersOrderedByZ());
720}
721
722void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
723 ATRACE_CALL();
724 ALOGV(__FUNCTION__);
725
726 if (!getState().isEnabled) {
727 return;
728 }
729
Ady Abraham3645e642021-04-20 18:39:00 -0700730 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700731 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham3645e642021-04-20 18:39:00 -0700732
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400733 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800734 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400735 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400736 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400737 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800738 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400739 if (layer == peekThroughLayer) {
740 // No longer needed, although it should not show up again, so
741 // resetting it is not truly needed either.
742 peekThroughLayer = nullptr;
743
744 // peekThroughLayer was already drawn ahead of its z order.
745 continue;
746 }
Dan Stoza6166c312021-01-15 16:34:05 -0800747 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400748 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400749 if (overrideInfo.buffer != nullptr) {
750 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800751 ALOGV("Skipping redundant buffer");
752 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400753 } else {
754 // First layer with the override buffer.
755 if (overrideInfo.peekThroughLayer) {
756 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400757
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400758 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400759 overrideZ = true;
760 includeGeometry = true;
761 constexpr bool isPeekingThrough = true;
762 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
763 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400764 }
765
766 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800767 }
Dan Stoza6166c312021-01-15 16:34:05 -0800768 }
769
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400770 constexpr bool isPeekingThrough = false;
771 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800772 }
773}
774
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800775compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
776 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
777 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100778 auto* compState = layer->getLayerFE().getCompositionState();
779
780 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
781 // want to force client composition because of the blur.
782 if (compState->sidebandStream != nullptr) {
783 return nullptr;
784 }
785 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800786 layerRequestingBgComposition = layer;
787 }
788 }
789 return layerRequestingBgComposition;
790}
791
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800792void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
793 setColorProfile(pickColorProfile(refreshArgs));
794}
795
796// Returns a data space that fits all visible layers. The returned data space
797// can only be one of
798// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
799// - Dataspace::DISPLAY_P3
800// - Dataspace::DISPLAY_BT2020
801// The returned HDR data space is one of
802// - Dataspace::UNKNOWN
803// - Dataspace::BT2020_HLG
804// - Dataspace::BT2020_PQ
805ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
806 bool* outIsHdrClientComposition) const {
807 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
808 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
809
Lloyd Pique01c77c12019-04-17 12:48:32 -0700810 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800811 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800812 case ui::Dataspace::V0_SCRGB:
813 case ui::Dataspace::V0_SCRGB_LINEAR:
814 case ui::Dataspace::BT2020:
815 case ui::Dataspace::BT2020_ITU:
816 case ui::Dataspace::BT2020_LINEAR:
817 case ui::Dataspace::DISPLAY_BT2020:
818 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
819 break;
820 case ui::Dataspace::DISPLAY_P3:
821 bestDataSpace = ui::Dataspace::DISPLAY_P3;
822 break;
823 case ui::Dataspace::BT2020_PQ:
824 case ui::Dataspace::BT2020_ITU_PQ:
825 bestDataSpace = ui::Dataspace::DISPLAY_P3;
826 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800827 *outIsHdrClientComposition =
828 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800829 break;
830 case ui::Dataspace::BT2020_HLG:
831 case ui::Dataspace::BT2020_ITU_HLG:
832 bestDataSpace = ui::Dataspace::DISPLAY_P3;
833 // When there's mixed PQ content and HLG content, we set the HDR
834 // data space to be BT2020_PQ and convert HLG to PQ.
835 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
836 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
837 }
838 break;
839 default:
840 break;
841 }
842 }
843
844 return bestDataSpace;
845}
846
847compositionengine::Output::ColorProfile Output::pickColorProfile(
848 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
849 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
850 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
851 ui::RenderIntent::COLORIMETRIC,
852 refreshArgs.colorSpaceAgnosticDataspace};
853 }
854
855 ui::Dataspace hdrDataSpace;
856 bool isHdrClientComposition = false;
857 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
858
859 switch (refreshArgs.forceOutputColorMode) {
860 case ui::ColorMode::SRGB:
861 bestDataSpace = ui::Dataspace::V0_SRGB;
862 break;
863 case ui::ColorMode::DISPLAY_P3:
864 bestDataSpace = ui::Dataspace::DISPLAY_P3;
865 break;
866 default:
867 break;
868 }
869
870 // respect hdrDataSpace only when there is no legacy HDR support
871 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
872 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
873 if (isHdr) {
874 bestDataSpace = hdrDataSpace;
875 }
876
877 ui::RenderIntent intent;
878 switch (refreshArgs.outputColorSetting) {
879 case OutputColorSetting::kManaged:
880 case OutputColorSetting::kUnmanaged:
881 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
882 : ui::RenderIntent::COLORIMETRIC;
883 break;
884 case OutputColorSetting::kEnhanced:
885 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
886 break;
887 default: // vendor display color setting
888 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
889 break;
890 }
891
892 ui::ColorMode outMode;
893 ui::Dataspace outDataSpace;
894 ui::RenderIntent outRenderIntent;
895 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
896 &outRenderIntent);
897
898 return ColorProfile{outMode, outDataSpace, outRenderIntent,
899 refreshArgs.colorSpaceAgnosticDataspace};
900}
901
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800902void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700903 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800904 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700905 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700906 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800907
908 // If nothing has changed (!dirty), don't recompose.
909 // If something changed, but we don't currently have any visible layers,
910 // and didn't when we last did a composition, then skip it this time.
911 // The second rule does two things:
912 // - When all layers are removed from a display, we'll emit one black
913 // frame, then nothing more until we get new layers.
914 // - When a display is created with a private layer stack, we won't
915 // emit any black frames until a layer is added to the layer stack.
916 const bool mustRecompose = dirty && !(empty && wasEmpty);
917
918 const char flagPrefix[] = {'-', '+'};
919 static_cast<void>(flagPrefix);
920 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
921 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
922 flagPrefix[empty], flagPrefix[wasEmpty]);
923
924 mRenderSurface->beginFrame(mustRecompose);
925
926 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700927 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800928 }
929}
930
Lloyd Pique66d68602019-02-13 14:23:31 -0800931void Output::prepareFrame() {
932 ATRACE_CALL();
933 ALOGV(__FUNCTION__);
934
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700935 const auto& outputState = getState();
936 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800937 return;
938 }
939
940 chooseCompositionStrategy();
941
Dan Stoza47437bb2021-01-15 16:21:07 -0800942 if (mPlanner) {
943 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
944 }
945
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700946 mRenderSurface->prepareFrame(outputState.usesClientComposition,
947 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800948}
949
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800950void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
951 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
952 return;
953 }
954
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700955 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800956 // transform the dirty region into this screen's coordinate space
957 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
958 if (!dirtyRegion.isEmpty()) {
959 base::unique_fd readyFence;
960 // redraw the whole screen
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800961 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800962
963 mRenderSurface->queueBuffer(std::move(readyFence));
964 }
965 }
966
967 postFramebuffer();
968
969 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
970
971 prepareFrame();
972}
973
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800974void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800975 ATRACE_CALL();
976 ALOGV(__FUNCTION__);
977
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700978 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800979 return;
980 }
981
982 // Repaint the framebuffer (if needed), getting the optional fence for when
983 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800984 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800985 if (!optReadyFence) {
986 return;
987 }
988
989 // swap buffers (presentation)
990 mRenderSurface->queueBuffer(std::move(*optReadyFence));
991}
992
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800993std::optional<base::unique_fd> Output::composeSurfaces(
994 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800995 ATRACE_CALL();
996 ALOGV(__FUNCTION__);
997
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700998 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800999 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001000 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001001 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -07001002
1003 auto& renderEngine = getCompositionEngine().getRenderEngine();
1004 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1005
1006 // If we the display is secure, protected content support is enabled, and at
1007 // least one layer has protected content, we need to use a secure back
1008 // buffer.
1009 if (outputState.isSecure && supportsProtectedContent) {
1010 auto layers = getOutputLayersOrderedByZ();
1011 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1012 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1013 });
1014 if (needsProtected != renderEngine.isProtected()) {
1015 renderEngine.useProtectedContext(needsProtected);
1016 }
1017 if (needsProtected != mRenderSurface->isProtected() &&
1018 needsProtected == renderEngine.isProtected()) {
1019 mRenderSurface->setProtected(needsProtected);
1020 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001021 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1022 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001023 }
1024
1025 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +00001026
1027 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001028
1029 // If we aren't doing client composition on this output, but do have a
1030 // flipClientTarget request for this frame on this output, we still need to
1031 // dequeue a buffer.
1032 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +00001033 tex = mRenderSurface->dequeueBuffer(&fd);
1034 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001035 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1036 "client composition for this frame",
1037 mName.c_str());
1038 return {};
1039 }
1040 }
1041
Lloyd Piqued3d69882019-02-28 16:03:46 -08001042 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001043 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001044 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -08001045 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001046 }
1047
1048 ALOGV("hasClientComposition");
1049
Lloyd Pique688abd42019-02-15 15:42:24 -08001050 renderengine::DisplaySettings clientCompositionDisplay;
Marin Shalamanovb15d2272020-09-17 21:41:52 +02001051 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.content;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001052 clientCompositionDisplay.clip = outputState.layerStackSpace.content;
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001053 clientCompositionDisplay.orientation =
1054 ui::Transform::toRotationFlags(outputState.displaySpace.orientation);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001055 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1056 ? outputState.dataspace
1057 : ui::Dataspace::UNKNOWN;
John Reckac09e452021-04-07 16:35:37 -04001058
1059 // If we have a valid current display brightness use that, otherwise fall back to the
1060 // display's max desired
1061 clientCompositionDisplay.maxLuminance = outputState.displayBrightnessNits > 0.f
1062 ? outputState.displayBrightnessNits
1063 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1064 clientCompositionDisplay.sdrWhitePointNits = outputState.sdrWhitePointNits;
Lloyd Pique688abd42019-02-15 15:42:24 -08001065
1066 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001067 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1068 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001069 }
1070
Lloyd Pique688abd42019-02-15 15:42:24 -08001071 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -08001072 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001073 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001074 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -08001075 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1076
Vishnu Nair9b079a22020-01-21 14:36:08 -08001077 // Check if the client composition requests were rendered into the provided graphic buffer. If
1078 // so, we can reuse the buffer and avoid client composition.
1079 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001080 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1081 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001082 clientCompositionLayers)) {
1083 outputCompositionState.reusedClientComposition = true;
1084 setExpensiveRenderingExpected(false);
1085 return readyFence;
1086 }
Alec Mouria90a5702021-04-16 16:36:21 +00001087 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001088 clientCompositionLayers);
1089 }
1090
Lloyd Pique688abd42019-02-15 15:42:24 -08001091 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001092 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1093 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1094 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001095 const bool expensiveBlurs =
1096 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001097 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001098 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001099 if (expensiveRenderingExpected) {
1100 setExpensiveRenderingExpected(true);
1101 }
1102
Vishnu Nair9b079a22020-01-21 14:36:08 -08001103 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
1104 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
1105 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1106 std::back_inserter(clientCompositionLayerPointers),
1107 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
1108 return &settings;
1109 });
1110
Alec Mourie4034bb2019-11-19 12:45:54 -08001111 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001112 // Only use the framebuffer cache when rendering to an internal display
1113 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1114 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1115 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1116 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1117 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001118 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001119 status_t status =
Alec Mouria90a5702021-04-16 16:36:21 +00001120 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, tex,
Alec Mouri1684c702021-02-04 12:27:26 -08001121 useFramebufferCache, std::move(fd), &readyFence);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001122
1123 if (status != NO_ERROR && mClientCompositionRequestCache) {
1124 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001125 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001126 }
1127
Alec Mourie4034bb2019-11-19 12:45:54 -08001128 auto& timeStats = getCompositionEngine().getTimeStats();
1129 if (readyFence.get() < 0) {
1130 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1131 } else {
1132 timeStats.recordRenderEngineDuration(renderEngineStart,
1133 std::make_shared<FenceTime>(
1134 new Fence(dup(readyFence.get()))));
1135 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001136
Lloyd Piqued3d69882019-02-28 16:03:46 -08001137 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001138}
1139
Vishnu Nair9b079a22020-01-21 14:36:08 -08001140std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Sally Qidf3da512021-07-08 17:27:02 +00001141 bool supportsProtectedContent, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001142 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001143 ALOGV("Rendering client layers");
1144
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001145 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001146 const Region viewportRegion(outputState.layerStackSpace.content);
Lloyd Pique688abd42019-02-15 15:42:24 -08001147 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001148
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001149 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001150 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001151
Lloyd Pique01c77c12019-04-17 12:48:32 -07001152 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001153 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001154 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001155 auto& layerFE = layer->getLayerFE();
1156
Lloyd Piquea2468662019-03-07 21:31:06 -08001157 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001158 ALOGV("Layer: %s", layerFE.getDebugName());
1159 if (clip.isEmpty()) {
1160 ALOGV(" Skipping for empty clip");
1161 firstLayer = false;
1162 continue;
1163 }
1164
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001165 disableBlurs |= layerFEState->sidebandStream != nullptr;
1166
Vishnu Naira483b4a2019-12-12 15:07:52 -08001167 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001168
1169 // We clear the client target for non-client composed layers if
1170 // requested by the HWC. We skip this if the layer is not an opaque
1171 // rectangle, as by definition the layer must blend with whatever is
1172 // underneath. We also skip the first layer as the buffer target is
1173 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001174 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001175 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001176
1177 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1178
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001179 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1180 // composing the non-shadow content and only draw the shadows.
1181 const bool realContentIsVisible = clientComposition &&
1182 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1183
Lloyd Pique688abd42019-02-15 15:42:24 -08001184 if (clientComposition || clearClientComposition) {
Dan Stoza6166c312021-01-15 16:34:05 -08001185 std::vector<LayerFE::LayerSettings> results;
1186 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001187 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001188 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001189 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001190 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1191 } else {
1192 ALOGV("Skipping redundant override buffer for [%s] in RE",
1193 layer->getLayerFE().getDebugName());
1194 }
Dan Stoza6166c312021-01-15 16:34:05 -08001195 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001196 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1197 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1198 : (layer->getState().overrideInfo.disableBackgroundBlur
1199 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1200 BlurRegionsOnly
1201 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1202 Enabled);
1203 compositionengine::LayerFE::ClientCompositionTargetSettings
1204 targetSettings{.clip = clip,
1205 .needsFiltering = layer->needsFiltering() ||
1206 outputState.needsFiltering,
1207 .isSecure = outputState.isSecure,
1208 .supportsProtectedContent = supportsProtectedContent,
Alec Mourif54453c2021-05-13 16:28:28 -07001209 .viewport = outputState.layerStackSpace.content,
1210 .dataspace = outputDataspace,
1211 .realContentIsVisible = realContentIsVisible,
1212 .clearContent = !clientComposition,
1213 .blurSetting = blurSetting};
Dan Stoza6166c312021-01-15 16:34:05 -08001214 results = layerFE.prepareClientCompositionList(targetSettings);
1215 if (realContentIsVisible && !results.empty()) {
1216 layer->editState().clientCompositionTimestamp = systemTime();
1217 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001218 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001219
1220 clientCompositionLayers.insert(clientCompositionLayers.end(),
1221 std::make_move_iterator(results.begin()),
1222 std::make_move_iterator(results.end()));
1223 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001224 }
1225
1226 firstLayer = false;
1227 }
1228
1229 return clientCompositionLayers;
1230}
1231
1232void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001233 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001234 if (flashRegion.isEmpty()) {
1235 return;
1236 }
1237
Vishnu Nair9b079a22020-01-21 14:36:08 -08001238 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001239 layerSettings.source.buffer.buffer = nullptr;
1240 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1241 layerSettings.alpha = half(1.0);
1242
1243 for (const auto& rect : flashRegion) {
1244 layerSettings.geometry.boundaries = rect.toFloatRect();
1245 clientCompositionLayers.push_back(layerSettings);
1246 }
1247}
1248
1249void Output::setExpensiveRenderingExpected(bool) {
1250 // The base class does nothing with this call.
1251}
1252
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001253void Output::postFramebuffer() {
1254 ATRACE_CALL();
1255 ALOGV(__FUNCTION__);
1256
1257 if (!getState().isEnabled) {
1258 return;
1259 }
1260
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001261 auto& outputState = editState();
1262 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001263 mRenderSurface->flip();
1264
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001265 auto frame = presentAndGetFrameFences();
1266
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001267 mRenderSurface->onPresentDisplayCompleted();
1268
Lloyd Pique01c77c12019-04-17 12:48:32 -07001269 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001270 // The layer buffer from the previous frame (if any) is released
1271 // by HWC only when the release fence from this frame (if any) is
1272 // signaled. Always get the release fence from HWC first.
1273 sp<Fence> releaseFence = Fence::NO_FENCE;
1274
1275 if (auto hwcLayer = layer->getHwcLayer()) {
1276 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1277 releaseFence = f->second;
1278 }
1279 }
1280
1281 // If the layer was client composited in the previous frame, we
1282 // need to merge with the previous client target acquire fence.
1283 // Since we do not track that, always merge with the current
1284 // client target acquire fence when it is available, even though
1285 // this is suboptimal.
1286 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001287 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001288 releaseFence =
1289 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1290 }
1291
1292 layer->getLayerFE().onLayerDisplayed(releaseFence);
1293 }
1294
1295 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001296 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001297 // supply them with the present fence.
1298 for (auto& weakLayer : mReleasedLayers) {
1299 if (auto layer = weakLayer.promote(); layer != nullptr) {
1300 layer->onLayerDisplayed(frame.presentFence);
1301 }
1302 }
1303
1304 // Clear out the released layers now that we're done with them.
1305 mReleasedLayers.clear();
1306}
1307
Alec Mouriaa831582021-06-07 16:23:01 -07001308void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001309 if (mPlanner) {
Alec Mouriaa831582021-06-07 16:23:01 -07001310 mPlanner->renderCachedSets(getState(), refreshArgs.nextInvalidateTime);
Dan Stoza6166c312021-01-15 16:34:05 -08001311 }
1312}
1313
Lloyd Pique32cbe282018-10-19 13:09:22 -07001314void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001315 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001316 outputState.dirtyRegion.set(outputState.displaySpace.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001317}
1318
Lloyd Pique66d68602019-02-13 14:23:31 -08001319void Output::chooseCompositionStrategy() {
1320 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001321 auto& outputState = editState();
1322 outputState.usesClientComposition = true;
1323 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001324 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001325}
1326
Lloyd Pique688abd42019-02-15 15:42:24 -08001327bool Output::getSkipColorTransform() const {
1328 return true;
1329}
1330
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001331compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1332 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001333 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001334 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1335 }
1336 return result;
1337}
1338
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001339} // namespace impl
1340} // namespace android::compositionengine