blob: 174729415986bc044a3cc2f9d1dcc460aec2099f [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
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700371Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700372 const auto& outputState = getState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700373 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.content);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700374}
375
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700376bool Output::includesLayer(ui::LayerFilter filter) const {
377 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700378}
379
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700380bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800381 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700382 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800383}
384
Lloyd Piquedf336d92019-03-07 21:38:42 -0800385std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800386 const sp<LayerFE>& layerFE) const {
387 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800388}
389
Lloyd Piquede196652020-01-22 17:29:58 -0800390compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
391 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700392 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800393}
394
Lloyd Pique01c77c12019-04-17 12:48:32 -0700395std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800396 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700397 for (size_t i = 0; i < getOutputLayerCount(); i++) {
398 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800399 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700400 return i;
401 }
402 }
403 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800404}
405
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800406void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
407 mReleasedLayers = std::move(layers);
408}
409
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800410void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
411 LayerFESet& geomSnapshots) {
412 ATRACE_CALL();
413 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800414
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800415 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800416}
417
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800418void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800419 ATRACE_CALL();
420 ALOGV(__FUNCTION__);
421
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800422 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800423 updateCompositionState(refreshArgs);
424 planComposition();
425 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800426 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800427 beginFrame();
428 prepareFrame();
429 devOptRepaintFlash(refreshArgs);
430 finishFrame(refreshArgs);
431 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700432 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800433}
434
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800435void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
436 LayerFESet& layerFESet) {
437 ATRACE_CALL();
438 ALOGV(__FUNCTION__);
439
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700440 auto& outputState = editState();
441
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800442 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700443 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800444 return;
445 }
446
447 // Process the layers to determine visibility and coverage
448 compositionengine::Output::CoverageState coverage{layerFESet};
449 collectVisibleLayers(refreshArgs, coverage);
450
451 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700452 const ui::Transform& tr = outputState.transform;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200453 Region undefinedRegion{outputState.displaySpace.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800454 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
455
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700456 outputState.undefinedRegion = undefinedRegion;
457 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800458}
459
460void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
461 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800462 // Evaluate the layers from front to back to determine what is visible. This
463 // also incrementally calculates the coverage information for each layer as
464 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800465 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700466 // Incrementally process the coverage for each layer
467 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800468
469 // TODO(b/121291683): Stop early if the output is completely covered and
470 // no more layers could even be visible underneath the ones on top.
471 }
472
Lloyd Pique01c77c12019-04-17 12:48:32 -0700473 setReleasedLayers(refreshArgs);
474
475 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800476}
477
Lloyd Piquede196652020-01-22 17:29:58 -0800478void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700479 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800480 // Ensure we have a snapshot of the basic geometry layer state. Limit the
481 // snapshots to once per frame for each candidate layer, as layers may
482 // appear on multiple outputs.
483 if (!coverage.latchedLayers.count(layerFE)) {
484 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800485 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800486 }
487
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700488 // Only consider the layers on this output
489 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800490 return;
491 }
492
493 // Obtain a read-only pointer to the front-end layer state
494 const auto* layerFEState = layerFE->getCompositionState();
495 if (CC_UNLIKELY(!layerFEState)) {
496 return;
497 }
498
499 // handle hidden surfaces by setting the visible region to empty
500 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700501 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800502 }
503
504 /*
505 * opaqueRegion: area of a surface that is fully opaque.
506 */
507 Region opaqueRegion;
508
509 /*
510 * visibleRegion: area of a surface that is visible on screen and not fully
511 * transparent. This is essentially the layer's footprint minus the opaque
512 * regions above it. Areas covered by a translucent surface are considered
513 * visible.
514 */
515 Region visibleRegion;
516
517 /*
518 * coveredRegion: area of a surface that is covered by all visible regions
519 * above it (which includes the translucent areas).
520 */
521 Region coveredRegion;
522
523 /*
524 * transparentRegion: area of a surface that is hinted to be completely
525 * transparent. This is only used to tell when the layer has no visible non-
526 * transparent regions and can be removed from the layer list. It does not
527 * affect the visibleRegion of this layer or any layers beneath it. The hint
528 * may not be correct if apps don't respect the SurfaceView restrictions
529 * (which, sadly, some don't).
530 */
531 Region transparentRegion;
532
Vishnu Naira483b4a2019-12-12 15:07:52 -0800533 /*
534 * shadowRegion: Region cast by the layer's shadow.
535 */
536 Region shadowRegion;
537
Lloyd Piquede196652020-01-22 17:29:58 -0800538 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800539
540 // Get the visible region
541 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
542 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800543 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800544 visibleRegion.set(visibleRect);
545
Lloyd Piquede196652020-01-22 17:29:58 -0800546 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800547 // if the layer casts a shadow, offset the layers visible region and
548 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800549 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800550 Rect visibleRectWithShadows(visibleRect);
551 visibleRectWithShadows.inset(inset, inset, inset, inset);
552 visibleRegion.set(visibleRectWithShadows);
553 shadowRegion = visibleRegion.subtract(visibleRect);
554 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800555
556 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700557 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800558 }
559
560 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800561 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800562 if (tr.preserveRects()) {
563 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800564 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800565 } else {
566 // transformation too complex, can't do the
567 // transparent region optimization.
568 transparentRegion.clear();
569 }
570 }
571
572 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800573 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800574 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800575 // If we one of the simple category of transforms (0/90/180/270 rotation
576 // + any flip), then the opaque region is the layer's footprint.
577 // Otherwise we don't try and compute the opaque region since there may
578 // be errors at the edges, and we treat the entire layer as
579 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800580 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800581 }
582
583 // Clip the covered region to the visible region
584 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
585
586 // Update accumAboveCoveredLayers for next (lower) layer
587 coverage.aboveCoveredLayers.orSelf(visibleRegion);
588
589 // subtract the opaque region covered by the layers above us
590 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
591
592 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700593 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800594 }
595
596 // Get coverage information for the layer as previously displayed,
597 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800598 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700599 auto prevOutputLayer =
600 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800601
602 // Get coverage information for the layer as previously displayed
603 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
604 const Region kEmptyRegion;
605 const Region& oldVisibleRegion =
606 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
607 const Region& oldCoveredRegion =
608 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
609
610 // compute this layer's dirty region
611 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800612 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800613 // we need to invalidate the whole region
614 dirty = visibleRegion;
615 // as well, as the old visible region
616 dirty.orSelf(oldVisibleRegion);
617 } else {
618 /* compute the exposed region:
619 * the exposed region consists of two components:
620 * 1) what's VISIBLE now and was COVERED before
621 * 2) what's EXPOSED now less what was EXPOSED before
622 *
623 * note that (1) is conservative, we start with the whole visible region
624 * but only keep what used to be covered by something -- which mean it
625 * may have been exposed.
626 *
627 * (2) handles areas that were not covered by anything but got exposed
628 * because of a resize.
629 *
630 */
631 const Region newExposed = visibleRegion - coveredRegion;
632 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
633 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
634 }
635 dirty.subtractSelf(coverage.aboveOpaqueLayers);
636
637 // accumulate to the screen dirty region
638 coverage.dirtyRegion.orSelf(dirty);
639
640 // Update accumAboveOpaqueLayers for next (lower) layer
641 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
642
643 // Compute the visible non-transparent region
644 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
645
Vishnu Naira483b4a2019-12-12 15:07:52 -0800646 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800647 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700648 const auto& outputState = getState();
649 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200650 drawRegion.andSelf(outputState.displaySpace.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800651 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700652 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800653 }
654
Vishnu Naira483b4a2019-12-12 15:07:52 -0800655 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
656
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800657 // The layer is visible. Either reuse the existing outputLayer if we have
658 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800659 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800660
661 // Store the layer coverage information into the layer state as some of it
662 // is useful later.
663 auto& outputLayerState = result->editState();
664 outputLayerState.visibleRegion = visibleRegion;
665 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
666 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200667 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
668 visibleNonShadowRegion.intersect(outputState.layerStackSpace.content));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800669 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800670}
671
672void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
673 // The base class does nothing with this call.
674}
675
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800676void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700677 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800678 layer->getLayerFE().prepareCompositionState(
679 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
680 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800681 }
682}
683
Dan Stoza269dc4d2021-01-15 15:07:43 -0800684void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800685 ATRACE_CALL();
686 ALOGV(__FUNCTION__);
687
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800688 if (!getState().isEnabled) {
689 return;
690 }
691
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800692 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
693 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
694
Lloyd Pique01c77c12019-04-17 12:48:32 -0700695 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700696 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800697 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200698 forceClientComposition,
699 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800700
701 if (mLayerRequestingBackgroundBlur == layer) {
702 forceClientComposition = false;
703 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800704 }
705}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800706
Dan Stoza269dc4d2021-01-15 15:07:43 -0800707void Output::planComposition() {
708 if (!mPlanner || !getState().isEnabled) {
709 return;
710 }
711
712 ATRACE_CALL();
713 ALOGV(__FUNCTION__);
714
715 mPlanner->plan(getOutputLayersOrderedByZ());
716}
717
718void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
719 ATRACE_CALL();
720 ALOGV(__FUNCTION__);
721
722 if (!getState().isEnabled) {
723 return;
724 }
725
Ady Abraham3645e642021-04-20 18:39:00 -0700726 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700727 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham3645e642021-04-20 18:39:00 -0700728
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400729 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800730 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400731 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400732 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400733 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800734 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400735 if (layer == peekThroughLayer) {
736 // No longer needed, although it should not show up again, so
737 // resetting it is not truly needed either.
738 peekThroughLayer = nullptr;
739
740 // peekThroughLayer was already drawn ahead of its z order.
741 continue;
742 }
Dan Stoza6166c312021-01-15 16:34:05 -0800743 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400744 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400745 if (overrideInfo.buffer != nullptr) {
746 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800747 ALOGV("Skipping redundant buffer");
748 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400749 } else {
750 // First layer with the override buffer.
751 if (overrideInfo.peekThroughLayer) {
752 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400753
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400754 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400755 overrideZ = true;
756 includeGeometry = true;
757 constexpr bool isPeekingThrough = true;
758 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
759 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400760 }
761
762 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800763 }
Dan Stoza6166c312021-01-15 16:34:05 -0800764 }
765
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400766 constexpr bool isPeekingThrough = false;
767 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800768 }
769}
770
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800771compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
772 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
773 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100774 auto* compState = layer->getLayerFE().getCompositionState();
775
776 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
777 // want to force client composition because of the blur.
778 if (compState->sidebandStream != nullptr) {
779 return nullptr;
780 }
781 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800782 layerRequestingBgComposition = layer;
783 }
784 }
785 return layerRequestingBgComposition;
786}
787
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800788void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
789 setColorProfile(pickColorProfile(refreshArgs));
790}
791
792// Returns a data space that fits all visible layers. The returned data space
793// can only be one of
794// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
795// - Dataspace::DISPLAY_P3
796// - Dataspace::DISPLAY_BT2020
797// The returned HDR data space is one of
798// - Dataspace::UNKNOWN
799// - Dataspace::BT2020_HLG
800// - Dataspace::BT2020_PQ
801ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
802 bool* outIsHdrClientComposition) const {
803 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
804 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
805
Lloyd Pique01c77c12019-04-17 12:48:32 -0700806 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800807 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800808 case ui::Dataspace::V0_SCRGB:
809 case ui::Dataspace::V0_SCRGB_LINEAR:
810 case ui::Dataspace::BT2020:
811 case ui::Dataspace::BT2020_ITU:
812 case ui::Dataspace::BT2020_LINEAR:
813 case ui::Dataspace::DISPLAY_BT2020:
814 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
815 break;
816 case ui::Dataspace::DISPLAY_P3:
817 bestDataSpace = ui::Dataspace::DISPLAY_P3;
818 break;
819 case ui::Dataspace::BT2020_PQ:
820 case ui::Dataspace::BT2020_ITU_PQ:
821 bestDataSpace = ui::Dataspace::DISPLAY_P3;
822 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800823 *outIsHdrClientComposition =
824 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800825 break;
826 case ui::Dataspace::BT2020_HLG:
827 case ui::Dataspace::BT2020_ITU_HLG:
828 bestDataSpace = ui::Dataspace::DISPLAY_P3;
829 // When there's mixed PQ content and HLG content, we set the HDR
830 // data space to be BT2020_PQ and convert HLG to PQ.
831 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
832 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
833 }
834 break;
835 default:
836 break;
837 }
838 }
839
840 return bestDataSpace;
841}
842
843compositionengine::Output::ColorProfile Output::pickColorProfile(
844 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
845 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
846 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
847 ui::RenderIntent::COLORIMETRIC,
848 refreshArgs.colorSpaceAgnosticDataspace};
849 }
850
851 ui::Dataspace hdrDataSpace;
852 bool isHdrClientComposition = false;
853 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
854
855 switch (refreshArgs.forceOutputColorMode) {
856 case ui::ColorMode::SRGB:
857 bestDataSpace = ui::Dataspace::V0_SRGB;
858 break;
859 case ui::ColorMode::DISPLAY_P3:
860 bestDataSpace = ui::Dataspace::DISPLAY_P3;
861 break;
862 default:
863 break;
864 }
865
866 // respect hdrDataSpace only when there is no legacy HDR support
867 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
868 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
869 if (isHdr) {
870 bestDataSpace = hdrDataSpace;
871 }
872
873 ui::RenderIntent intent;
874 switch (refreshArgs.outputColorSetting) {
875 case OutputColorSetting::kManaged:
876 case OutputColorSetting::kUnmanaged:
877 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
878 : ui::RenderIntent::COLORIMETRIC;
879 break;
880 case OutputColorSetting::kEnhanced:
881 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
882 break;
883 default: // vendor display color setting
884 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
885 break;
886 }
887
888 ui::ColorMode outMode;
889 ui::Dataspace outDataSpace;
890 ui::RenderIntent outRenderIntent;
891 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
892 &outRenderIntent);
893
894 return ColorProfile{outMode, outDataSpace, outRenderIntent,
895 refreshArgs.colorSpaceAgnosticDataspace};
896}
897
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800898void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700899 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700900 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700901 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700902 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800903
904 // If nothing has changed (!dirty), don't recompose.
905 // If something changed, but we don't currently have any visible layers,
906 // and didn't when we last did a composition, then skip it this time.
907 // The second rule does two things:
908 // - When all layers are removed from a display, we'll emit one black
909 // frame, then nothing more until we get new layers.
910 // - When a display is created with a private layer stack, we won't
911 // emit any black frames until a layer is added to the layer stack.
912 const bool mustRecompose = dirty && !(empty && wasEmpty);
913
914 const char flagPrefix[] = {'-', '+'};
915 static_cast<void>(flagPrefix);
916 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
917 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
918 flagPrefix[empty], flagPrefix[wasEmpty]);
919
920 mRenderSurface->beginFrame(mustRecompose);
921
922 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700923 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800924 }
925}
926
Lloyd Pique66d68602019-02-13 14:23:31 -0800927void Output::prepareFrame() {
928 ATRACE_CALL();
929 ALOGV(__FUNCTION__);
930
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700931 const auto& outputState = getState();
932 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800933 return;
934 }
935
936 chooseCompositionStrategy();
937
Dan Stoza47437bb2021-01-15 16:21:07 -0800938 if (mPlanner) {
939 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
940 }
941
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700942 mRenderSurface->prepareFrame(outputState.usesClientComposition,
943 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800944}
945
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800946void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
947 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
948 return;
949 }
950
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700951 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700952 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800953 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700954 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800955 }
956 }
957
958 postFramebuffer();
959
960 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
961
962 prepareFrame();
963}
964
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800965void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800966 ATRACE_CALL();
967 ALOGV(__FUNCTION__);
968
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700969 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800970 return;
971 }
972
973 // Repaint the framebuffer (if needed), getting the optional fence for when
974 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800975 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800976 if (!optReadyFence) {
977 return;
978 }
979
980 // swap buffers (presentation)
981 mRenderSurface->queueBuffer(std::move(*optReadyFence));
982}
983
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800984std::optional<base::unique_fd> Output::composeSurfaces(
985 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800986 ATRACE_CALL();
987 ALOGV(__FUNCTION__);
988
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700989 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800990 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800991 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700992 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -0700993
994 auto& renderEngine = getCompositionEngine().getRenderEngine();
995 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
996
997 // If we the display is secure, protected content support is enabled, and at
998 // least one layer has protected content, we need to use a secure back
999 // buffer.
1000 if (outputState.isSecure && supportsProtectedContent) {
1001 auto layers = getOutputLayersOrderedByZ();
1002 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1003 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1004 });
1005 if (needsProtected != renderEngine.isProtected()) {
1006 renderEngine.useProtectedContext(needsProtected);
1007 }
1008 if (needsProtected != mRenderSurface->isProtected() &&
1009 needsProtected == renderEngine.isProtected()) {
1010 mRenderSurface->setProtected(needsProtected);
1011 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001012 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1013 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001014 }
1015
1016 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +00001017
1018 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001019
1020 // If we aren't doing client composition on this output, but do have a
1021 // flipClientTarget request for this frame on this output, we still need to
1022 // dequeue a buffer.
1023 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +00001024 tex = mRenderSurface->dequeueBuffer(&fd);
1025 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001026 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1027 "client composition for this frame",
1028 mName.c_str());
1029 return {};
1030 }
1031 }
1032
Lloyd Pique688abd42019-02-15 15:42:24 -08001033 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001034 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001035 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001036 }
1037
1038 ALOGV("hasClientComposition");
1039
Lloyd Pique688abd42019-02-15 15:42:24 -08001040 renderengine::DisplaySettings clientCompositionDisplay;
Marin Shalamanovb15d2272020-09-17 21:41:52 +02001041 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.content;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001042 clientCompositionDisplay.clip = outputState.layerStackSpace.content;
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001043 clientCompositionDisplay.orientation =
1044 ui::Transform::toRotationFlags(outputState.displaySpace.orientation);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001045 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1046 ? outputState.dataspace
1047 : ui::Dataspace::UNKNOWN;
John Reckac09e452021-04-07 16:35:37 -04001048
1049 // If we have a valid current display brightness use that, otherwise fall back to the
1050 // display's max desired
1051 clientCompositionDisplay.maxLuminance = outputState.displayBrightnessNits > 0.f
1052 ? outputState.displayBrightnessNits
1053 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1054 clientCompositionDisplay.sdrWhitePointNits = outputState.sdrWhitePointNits;
Lloyd Pique688abd42019-02-15 15:42:24 -08001055
1056 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001057 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1058 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001059 }
1060
Lloyd Pique688abd42019-02-15 15:42:24 -08001061 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -08001062 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001063 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001064 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -08001065 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1066
Vishnu Nair9b079a22020-01-21 14:36:08 -08001067 // Check if the client composition requests were rendered into the provided graphic buffer. If
1068 // so, we can reuse the buffer and avoid client composition.
1069 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001070 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1071 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001072 clientCompositionLayers)) {
1073 outputCompositionState.reusedClientComposition = true;
1074 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001075 return base::unique_fd();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001076 }
Alec Mouria90a5702021-04-16 16:36:21 +00001077 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001078 clientCompositionLayers);
1079 }
1080
Lloyd Pique688abd42019-02-15 15:42:24 -08001081 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001082 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1083 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1084 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001085 const bool expensiveBlurs =
1086 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001087 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001088 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001089 if (expensiveRenderingExpected) {
1090 setExpensiveRenderingExpected(true);
1091 }
1092
Vishnu Nair9b079a22020-01-21 14:36:08 -08001093 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
1094 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
1095 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1096 std::back_inserter(clientCompositionLayerPointers),
1097 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
1098 return &settings;
1099 });
1100
Alec Mourie4034bb2019-11-19 12:45:54 -08001101 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001102 // Only use the framebuffer cache when rendering to an internal display
1103 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1104 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1105 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1106 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1107 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001108 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Sally Qi4cabdd02021-08-05 16:45:57 -07001109 auto [status, drawFence] =
1110 renderEngine
1111 .drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, tex,
1112 useFramebufferCache, std::move(fd))
1113 .get();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001114
1115 if (status != NO_ERROR && mClientCompositionRequestCache) {
1116 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001117 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001118 }
1119
Alec Mourie4034bb2019-11-19 12:45:54 -08001120 auto& timeStats = getCompositionEngine().getTimeStats();
Sally Qi4cabdd02021-08-05 16:45:57 -07001121 if (drawFence.get() < 0) {
Alec Mourie4034bb2019-11-19 12:45:54 -08001122 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1123 } else {
1124 timeStats.recordRenderEngineDuration(renderEngineStart,
1125 std::make_shared<FenceTime>(
Sally Qi4cabdd02021-08-05 16:45:57 -07001126 new Fence(dup(drawFence.get()))));
Alec Mourie4034bb2019-11-19 12:45:54 -08001127 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001128
Sally Qi4cabdd02021-08-05 16:45:57 -07001129 return std::move(drawFence);
Lloyd Pique688abd42019-02-15 15:42:24 -08001130}
1131
Vishnu Nair9b079a22020-01-21 14:36:08 -08001132std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Sally Qidf3da512021-07-08 17:27:02 +00001133 bool supportsProtectedContent, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001134 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001135 ALOGV("Rendering client layers");
1136
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001137 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001138 const Region viewportRegion(outputState.layerStackSpace.content);
Lloyd Pique688abd42019-02-15 15:42:24 -08001139 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001140
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001141 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001142 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001143
Lloyd Pique01c77c12019-04-17 12:48:32 -07001144 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001145 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001146 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001147 auto& layerFE = layer->getLayerFE();
1148
Lloyd Piquea2468662019-03-07 21:31:06 -08001149 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001150 ALOGV("Layer: %s", layerFE.getDebugName());
1151 if (clip.isEmpty()) {
1152 ALOGV(" Skipping for empty clip");
1153 firstLayer = false;
1154 continue;
1155 }
1156
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001157 disableBlurs |= layerFEState->sidebandStream != nullptr;
1158
Vishnu Naira483b4a2019-12-12 15:07:52 -08001159 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001160
1161 // We clear the client target for non-client composed layers if
1162 // requested by the HWC. We skip this if the layer is not an opaque
1163 // rectangle, as by definition the layer must blend with whatever is
1164 // underneath. We also skip the first layer as the buffer target is
1165 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001166 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001167 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001168
1169 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1170
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001171 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1172 // composing the non-shadow content and only draw the shadows.
1173 const bool realContentIsVisible = clientComposition &&
1174 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1175
Lloyd Pique688abd42019-02-15 15:42:24 -08001176 if (clientComposition || clearClientComposition) {
Dan Stoza6166c312021-01-15 16:34:05 -08001177 std::vector<LayerFE::LayerSettings> results;
1178 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001179 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001180 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001181 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001182 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1183 } else {
1184 ALOGV("Skipping redundant override buffer for [%s] in RE",
1185 layer->getLayerFE().getDebugName());
1186 }
Dan Stoza6166c312021-01-15 16:34:05 -08001187 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001188 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1189 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1190 : (layer->getState().overrideInfo.disableBackgroundBlur
1191 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1192 BlurRegionsOnly
1193 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1194 Enabled);
1195 compositionengine::LayerFE::ClientCompositionTargetSettings
1196 targetSettings{.clip = clip,
1197 .needsFiltering = layer->needsFiltering() ||
1198 outputState.needsFiltering,
1199 .isSecure = outputState.isSecure,
1200 .supportsProtectedContent = supportsProtectedContent,
Alec Mourif54453c2021-05-13 16:28:28 -07001201 .viewport = outputState.layerStackSpace.content,
1202 .dataspace = outputDataspace,
1203 .realContentIsVisible = realContentIsVisible,
1204 .clearContent = !clientComposition,
1205 .blurSetting = blurSetting};
Dan Stoza6166c312021-01-15 16:34:05 -08001206 results = layerFE.prepareClientCompositionList(targetSettings);
1207 if (realContentIsVisible && !results.empty()) {
1208 layer->editState().clientCompositionTimestamp = systemTime();
1209 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001210 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001211
1212 clientCompositionLayers.insert(clientCompositionLayers.end(),
1213 std::make_move_iterator(results.begin()),
1214 std::make_move_iterator(results.end()));
1215 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001216 }
1217
1218 firstLayer = false;
1219 }
1220
1221 return clientCompositionLayers;
1222}
1223
1224void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001225 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001226 if (flashRegion.isEmpty()) {
1227 return;
1228 }
1229
Vishnu Nair9b079a22020-01-21 14:36:08 -08001230 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001231 layerSettings.source.buffer.buffer = nullptr;
1232 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1233 layerSettings.alpha = half(1.0);
1234
1235 for (const auto& rect : flashRegion) {
1236 layerSettings.geometry.boundaries = rect.toFloatRect();
1237 clientCompositionLayers.push_back(layerSettings);
1238 }
1239}
1240
1241void Output::setExpensiveRenderingExpected(bool) {
1242 // The base class does nothing with this call.
1243}
1244
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001245void Output::postFramebuffer() {
1246 ATRACE_CALL();
1247 ALOGV(__FUNCTION__);
1248
1249 if (!getState().isEnabled) {
1250 return;
1251 }
1252
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001253 auto& outputState = editState();
1254 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001255 mRenderSurface->flip();
1256
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001257 auto frame = presentAndGetFrameFences();
1258
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001259 mRenderSurface->onPresentDisplayCompleted();
1260
Lloyd Pique01c77c12019-04-17 12:48:32 -07001261 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001262 // The layer buffer from the previous frame (if any) is released
1263 // by HWC only when the release fence from this frame (if any) is
1264 // signaled. Always get the release fence from HWC first.
1265 sp<Fence> releaseFence = Fence::NO_FENCE;
1266
1267 if (auto hwcLayer = layer->getHwcLayer()) {
1268 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1269 releaseFence = f->second;
1270 }
1271 }
1272
1273 // If the layer was client composited in the previous frame, we
1274 // need to merge with the previous client target acquire fence.
1275 // Since we do not track that, always merge with the current
1276 // client target acquire fence when it is available, even though
1277 // this is suboptimal.
1278 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001279 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001280 releaseFence =
1281 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1282 }
1283
1284 layer->getLayerFE().onLayerDisplayed(releaseFence);
1285 }
1286
1287 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001288 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001289 // supply them with the present fence.
1290 for (auto& weakLayer : mReleasedLayers) {
1291 if (auto layer = weakLayer.promote(); layer != nullptr) {
1292 layer->onLayerDisplayed(frame.presentFence);
1293 }
1294 }
1295
1296 // Clear out the released layers now that we're done with them.
1297 mReleasedLayers.clear();
1298}
1299
Alec Mouriaa831582021-06-07 16:23:01 -07001300void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001301 if (mPlanner) {
Alec Mouriaa831582021-06-07 16:23:01 -07001302 mPlanner->renderCachedSets(getState(), refreshArgs.nextInvalidateTime);
Dan Stoza6166c312021-01-15 16:34:05 -08001303 }
1304}
1305
Lloyd Pique32cbe282018-10-19 13:09:22 -07001306void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001307 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001308 outputState.dirtyRegion.set(outputState.displaySpace.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001309}
1310
Lloyd Pique66d68602019-02-13 14:23:31 -08001311void Output::chooseCompositionStrategy() {
1312 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001313 auto& outputState = editState();
1314 outputState.usesClientComposition = true;
1315 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001316 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001317}
1318
Lloyd Pique688abd42019-02-15 15:42:24 -08001319bool Output::getSkipColorTransform() const {
1320 return true;
1321}
1322
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001323compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1324 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001325 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001326 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1327 }
1328 return result;
1329}
1330
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001331} // namespace impl
1332} // namespace android::compositionengine