blob: 088a400877b16033e461ec1d405ba7cc50ef68fb [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) {
132 mPlanner = std::make_unique<planner::Planner>();
133 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
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200149void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
150 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700151 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200152
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200153 outputState.displaySpace.orientation = orientation;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200154 LOG_FATAL_IF(outputState.displaySpace.bounds == Rect::INVALID_RECT,
155 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200156
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200157 // Compute orientedDisplaySpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200158 ui::Size orientedSize = outputState.displaySpace.bounds.getSize();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200159 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200160 std::swap(orientedSize.width, orientedSize.height);
161 }
162 outputState.orientedDisplaySpace.bounds = Rect(orientedSize);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200163 outputState.orientedDisplaySpace.content = orientedDisplaySpaceRect;
164
165 // Compute displaySpace.content
166 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
167 ui::Transform rotation;
168 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
169 const auto displaySize = outputState.displaySpace.bounds;
170 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
171 }
172 outputState.displaySpace.content = rotation.transform(orientedDisplaySpaceRect);
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200173
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200174 // Compute framebufferSpace
175 outputState.framebufferSpace.orientation = orientation;
176 LOG_FATAL_IF(outputState.framebufferSpace.bounds == Rect::INVALID_RECT,
177 "The framebuffer bounds are unknown.");
178 const auto scale =
Marin Shalamanov209ae612020-10-01 00:17:39 +0200179 getScale(outputState.displaySpace.bounds, outputState.framebufferSpace.bounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200180 outputState.framebufferSpace.content = outputState.displaySpace.content.scale(scale.x, scale.y);
181
182 // Compute layerStackSpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200183 outputState.layerStackSpace.content = layerStackSpaceRect;
184 outputState.layerStackSpace.bounds = layerStackSpaceRect;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200185
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200186 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
187 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700188 dirtyEntireOutput();
189}
190
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200191void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700192 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200193
194 auto& state = editState();
195
196 // Update framebuffer space
197 const Rect newBounds(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200198 state.framebufferSpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200199
200 // Update display space
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200201 state.displaySpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200202 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
203
204 // Update oriented display space
205 const auto orientation = state.displaySpace.orientation;
206 ui::Size orientedSize = size;
207 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
208 std::swap(orientedSize.width, orientedSize.height);
209 }
210 const Rect newOrientedBounds(orientedSize);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200211 state.orientedDisplaySpace.bounds = newOrientedBounds;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700212
Dan Stoza6166c312021-01-15 16:34:05 -0800213 if (mPlanner) {
214 mPlanner->setDisplaySize(size);
215 }
216
Lloyd Pique32cbe282018-10-19 13:09:22 -0700217 dirtyEntireOutput();
218}
219
Garfield Tan54edd912020-10-21 16:31:41 -0700220ui::Transform::RotationFlags Output::getTransformHint() const {
221 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
222}
223
Lloyd Piqueef36b002019-01-23 17:52:04 -0800224void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700225 auto& outputState = editState();
226 outputState.layerStackId = layerStackId;
227 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700228
229 dirtyEntireOutput();
230}
231
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800232void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700233 auto& colorTransformMatrix = editState().colorTransformMatrix;
234 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700235 return;
236 }
237
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700238 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800239
240 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700241}
242
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800243void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700244 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800245 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
246 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800247
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700248 auto& outputState = editState();
249 if (outputState.colorMode == colorProfile.mode &&
250 outputState.dataspace == colorProfile.dataspace &&
251 outputState.renderIntent == colorProfile.renderIntent &&
252 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800253 return;
254 }
255
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700256 outputState.colorMode = colorProfile.mode;
257 outputState.dataspace = colorProfile.dataspace;
258 outputState.renderIntent = colorProfile.renderIntent;
259 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700260
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800261 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700262
Lloyd Pique32cbe282018-10-19 13:09:22 -0700263 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800264 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
265 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800266
267 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700268}
269
John Reckac09e452021-04-07 16:35:37 -0400270void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
271 auto& outputState = editState();
272 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
273 outputState.displayBrightnessNits == displayBrightnessNits) {
274 // Nothing changed
275 return;
276 }
277 outputState.sdrWhitePointNits = sdrWhitePointNits;
278 outputState.displayBrightnessNits = displayBrightnessNits;
279 dirtyEntireOutput();
280}
281
Lloyd Pique32cbe282018-10-19 13:09:22 -0700282void Output::dump(std::string& out) const {
283 using android::base::StringAppendF;
284
285 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
286
287 out.append("\n ");
288
289 dumpBase(out);
290}
291
292void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700293 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700294
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700295 if (mDisplayColorProfile) {
296 mDisplayColorProfile->dump(out);
297 } else {
298 out.append(" No display color profile!\n");
299 }
300
Lloyd Pique31cb2942018-10-19 17:23:03 -0700301 if (mRenderSurface) {
302 mRenderSurface->dump(out);
303 } else {
304 out.append(" No render surface!\n");
305 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800306
Lloyd Pique01c77c12019-04-17 12:48:32 -0700307 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
308 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800309 if (!outputLayer) {
310 continue;
311 }
312 outputLayer->dump(out);
313 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700314}
315
Dan Stoza269dc4d2021-01-15 15:07:43 -0800316void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
317 if (!mPlanner) {
318 base::StringAppendF(&out, "Planner is disabled\n");
319 return;
320 }
321 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
322 mPlanner->dump(args, out);
323}
324
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700325compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
326 return mDisplayColorProfile.get();
327}
328
329void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
330 mDisplayColorProfile = std::move(mode);
331}
332
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800333const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
334 return mReleasedLayers;
335}
336
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700337void Output::setDisplayColorProfileForTest(
338 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
339 mDisplayColorProfile = std::move(mode);
340}
341
Lloyd Pique31cb2942018-10-19 17:23:03 -0700342compositionengine::RenderSurface* Output::getRenderSurface() const {
343 return mRenderSurface.get();
344}
345
346void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
347 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800348 const auto size = mRenderSurface->getSize();
349 editState().framebufferSpace.bounds = Rect(size);
350 if (mPlanner) {
351 mPlanner->setDisplaySize(size);
352 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700353 dirtyEntireOutput();
354}
355
Vishnu Nair9b079a22020-01-21 14:36:08 -0800356void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
357 if (cacheSize == 0) {
358 mClientCompositionRequestCache.reset();
359 } else {
360 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
361 }
362};
363
Lloyd Pique31cb2942018-10-19 17:23:03 -0700364void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
365 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700366}
367
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000368Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700369 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200370 Region dirty(outputState.layerStackSpace.content);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000371 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700372 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700373 }
374 return dirty;
375}
376
Lloyd Piquec6687342019-03-07 21:34:57 -0800377bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800378 // The layerStackId's must match, and also the layer must not be internal
379 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700380 const auto& outputState = getState();
381 return layerStackId && (*layerStackId == outputState.layerStackId) &&
382 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700383}
384
Lloyd Piquede196652020-01-22 17:29:58 -0800385bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const {
386 const auto* layerFEState = layerFE->getCompositionState();
387 return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800388}
389
Lloyd Piquedf336d92019-03-07 21:38:42 -0800390std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800391 const sp<LayerFE>& layerFE) const {
392 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800393}
394
Lloyd Piquede196652020-01-22 17:29:58 -0800395compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
396 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700397 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800398}
399
Lloyd Pique01c77c12019-04-17 12:48:32 -0700400std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800401 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700402 for (size_t i = 0; i < getOutputLayerCount(); i++) {
403 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800404 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700405 return i;
406 }
407 }
408 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800409}
410
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800411void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
412 mReleasedLayers = std::move(layers);
413}
414
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800415void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
416 LayerFESet& geomSnapshots) {
417 ATRACE_CALL();
418 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800419
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800420 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800421}
422
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800423void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800424 ATRACE_CALL();
425 ALOGV(__FUNCTION__);
426
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800427 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800428 updateCompositionState(refreshArgs);
429 planComposition();
430 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800431 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800432 beginFrame();
433 prepareFrame();
434 devOptRepaintFlash(refreshArgs);
435 finishFrame(refreshArgs);
436 postFramebuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800437 renderCachedSets();
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800438}
439
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800440void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
441 LayerFESet& layerFESet) {
442 ATRACE_CALL();
443 ALOGV(__FUNCTION__);
444
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700445 auto& outputState = editState();
446
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800447 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700448 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800449 return;
450 }
451
452 // Process the layers to determine visibility and coverage
453 compositionengine::Output::CoverageState coverage{layerFESet};
454 collectVisibleLayers(refreshArgs, coverage);
455
456 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700457 const ui::Transform& tr = outputState.transform;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200458 Region undefinedRegion{outputState.displaySpace.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800459 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
460
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700461 outputState.undefinedRegion = undefinedRegion;
462 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800463}
464
465void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
466 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800467 // Evaluate the layers from front to back to determine what is visible. This
468 // also incrementally calculates the coverage information for each layer as
469 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800470 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700471 // Incrementally process the coverage for each layer
472 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800473
474 // TODO(b/121291683): Stop early if the output is completely covered and
475 // no more layers could even be visible underneath the ones on top.
476 }
477
Lloyd Pique01c77c12019-04-17 12:48:32 -0700478 setReleasedLayers(refreshArgs);
479
480 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800481}
482
Lloyd Piquede196652020-01-22 17:29:58 -0800483void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700484 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800485 // Ensure we have a snapshot of the basic geometry layer state. Limit the
486 // snapshots to once per frame for each candidate layer, as layers may
487 // appear on multiple outputs.
488 if (!coverage.latchedLayers.count(layerFE)) {
489 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800490 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800491 }
492
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800493 // Only consider the layers on the given layer stack
Lloyd Piquede196652020-01-22 17:29:58 -0800494 if (!belongsInOutput(layerFE)) {
495 return;
496 }
497
498 // Obtain a read-only pointer to the front-end layer state
499 const auto* layerFEState = layerFE->getCompositionState();
500 if (CC_UNLIKELY(!layerFEState)) {
501 return;
502 }
503
504 // handle hidden surfaces by setting the visible region to empty
505 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700506 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800507 }
508
509 /*
510 * opaqueRegion: area of a surface that is fully opaque.
511 */
512 Region opaqueRegion;
513
514 /*
515 * visibleRegion: area of a surface that is visible on screen and not fully
516 * transparent. This is essentially the layer's footprint minus the opaque
517 * regions above it. Areas covered by a translucent surface are considered
518 * visible.
519 */
520 Region visibleRegion;
521
522 /*
523 * coveredRegion: area of a surface that is covered by all visible regions
524 * above it (which includes the translucent areas).
525 */
526 Region coveredRegion;
527
528 /*
529 * transparentRegion: area of a surface that is hinted to be completely
530 * transparent. This is only used to tell when the layer has no visible non-
531 * transparent regions and can be removed from the layer list. It does not
532 * affect the visibleRegion of this layer or any layers beneath it. The hint
533 * may not be correct if apps don't respect the SurfaceView restrictions
534 * (which, sadly, some don't).
535 */
536 Region transparentRegion;
537
Vishnu Naira483b4a2019-12-12 15:07:52 -0800538 /*
539 * shadowRegion: Region cast by the layer's shadow.
540 */
541 Region shadowRegion;
542
Lloyd Piquede196652020-01-22 17:29:58 -0800543 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800544
545 // Get the visible region
546 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
547 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800548 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800549 visibleRegion.set(visibleRect);
550
Lloyd Piquede196652020-01-22 17:29:58 -0800551 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800552 // if the layer casts a shadow, offset the layers visible region and
553 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800554 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800555 Rect visibleRectWithShadows(visibleRect);
556 visibleRectWithShadows.inset(inset, inset, inset, inset);
557 visibleRegion.set(visibleRectWithShadows);
558 shadowRegion = visibleRegion.subtract(visibleRect);
559 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800560
561 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700562 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800563 }
564
565 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800566 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800567 if (tr.preserveRects()) {
568 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800569 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800570 } else {
571 // transformation too complex, can't do the
572 // transparent region optimization.
573 transparentRegion.clear();
574 }
575 }
576
577 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800578 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800579 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800580 // If we one of the simple category of transforms (0/90/180/270 rotation
581 // + any flip), then the opaque region is the layer's footprint.
582 // Otherwise we don't try and compute the opaque region since there may
583 // be errors at the edges, and we treat the entire layer as
584 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800585 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800586 }
587
588 // Clip the covered region to the visible region
589 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
590
591 // Update accumAboveCoveredLayers for next (lower) layer
592 coverage.aboveCoveredLayers.orSelf(visibleRegion);
593
594 // subtract the opaque region covered by the layers above us
595 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
596
597 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700598 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800599 }
600
601 // Get coverage information for the layer as previously displayed,
602 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800603 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700604 auto prevOutputLayer =
605 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800606
607 // Get coverage information for the layer as previously displayed
608 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
609 const Region kEmptyRegion;
610 const Region& oldVisibleRegion =
611 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
612 const Region& oldCoveredRegion =
613 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
614
615 // compute this layer's dirty region
616 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800617 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800618 // we need to invalidate the whole region
619 dirty = visibleRegion;
620 // as well, as the old visible region
621 dirty.orSelf(oldVisibleRegion);
622 } else {
623 /* compute the exposed region:
624 * the exposed region consists of two components:
625 * 1) what's VISIBLE now and was COVERED before
626 * 2) what's EXPOSED now less what was EXPOSED before
627 *
628 * note that (1) is conservative, we start with the whole visible region
629 * but only keep what used to be covered by something -- which mean it
630 * may have been exposed.
631 *
632 * (2) handles areas that were not covered by anything but got exposed
633 * because of a resize.
634 *
635 */
636 const Region newExposed = visibleRegion - coveredRegion;
637 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
638 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
639 }
640 dirty.subtractSelf(coverage.aboveOpaqueLayers);
641
642 // accumulate to the screen dirty region
643 coverage.dirtyRegion.orSelf(dirty);
644
645 // Update accumAboveOpaqueLayers for next (lower) layer
646 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
647
648 // Compute the visible non-transparent region
649 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
650
Vishnu Naira483b4a2019-12-12 15:07:52 -0800651 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800652 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700653 const auto& outputState = getState();
654 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200655 drawRegion.andSelf(outputState.displaySpace.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800656 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700657 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800658 }
659
Vishnu Naira483b4a2019-12-12 15:07:52 -0800660 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
661
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800662 // The layer is visible. Either reuse the existing outputLayer if we have
663 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800664 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800665
666 // Store the layer coverage information into the layer state as some of it
667 // is useful later.
668 auto& outputLayerState = result->editState();
669 outputLayerState.visibleRegion = visibleRegion;
670 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
671 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200672 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
673 visibleNonShadowRegion.intersect(outputState.layerStackSpace.content));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800674 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800675}
676
677void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
678 // The base class does nothing with this call.
679}
680
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800681void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700682 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800683 layer->getLayerFE().prepareCompositionState(
684 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
685 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800686 }
687}
688
Dan Stoza269dc4d2021-01-15 15:07:43 -0800689void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800690 ATRACE_CALL();
691 ALOGV(__FUNCTION__);
692
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800693 if (!getState().isEnabled) {
694 return;
695 }
696
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800697 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
698 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
699
Lloyd Pique01c77c12019-04-17 12:48:32 -0700700 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700701 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800702 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200703 forceClientComposition,
704 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800705
706 if (mLayerRequestingBackgroundBlur == layer) {
707 forceClientComposition = false;
708 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800709 }
710}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800711
Dan Stoza269dc4d2021-01-15 15:07:43 -0800712void Output::planComposition() {
713 if (!mPlanner || !getState().isEnabled) {
714 return;
715 }
716
717 ATRACE_CALL();
718 ALOGV(__FUNCTION__);
719
720 mPlanner->plan(getOutputLayersOrderedByZ());
721}
722
723void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
724 ATRACE_CALL();
725 ALOGV(__FUNCTION__);
726
727 if (!getState().isEnabled) {
728 return;
729 }
730
Ady Abraham3645e642021-04-20 18:39:00 -0700731 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
732
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
1071 // Note: Updated by generateClientCompositionRequests
1072 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
1073
1074 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -08001075 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001076 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001077 clientCompositionDisplay.clearRegion,
1078 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -08001079 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1080
Vishnu Nair9b079a22020-01-21 14:36:08 -08001081 // Check if the client composition requests were rendered into the provided graphic buffer. If
1082 // so, we can reuse the buffer and avoid client composition.
1083 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001084 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1085 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001086 clientCompositionLayers)) {
1087 outputCompositionState.reusedClientComposition = true;
1088 setExpensiveRenderingExpected(false);
1089 return readyFence;
1090 }
Alec Mouria90a5702021-04-16 16:36:21 +00001091 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001092 clientCompositionLayers);
1093 }
1094
Lloyd Pique688abd42019-02-15 15:42:24 -08001095 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001096 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1097 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1098 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001099 const bool expensiveBlurs =
1100 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001101 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001102 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001103 if (expensiveRenderingExpected) {
1104 setExpensiveRenderingExpected(true);
1105 }
1106
Vishnu Nair9b079a22020-01-21 14:36:08 -08001107 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
1108 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
1109 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1110 std::back_inserter(clientCompositionLayerPointers),
1111 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
1112 return &settings;
1113 });
1114
Alec Mourie4034bb2019-11-19 12:45:54 -08001115 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001116 // Only use the framebuffer cache when rendering to an internal display
1117 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1118 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1119 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1120 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1121 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
1122 const bool useFramebufferCache = outputState.layerStackInternal;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001123 status_t status =
Alec Mouria90a5702021-04-16 16:36:21 +00001124 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, tex,
Alec Mouri1684c702021-02-04 12:27:26 -08001125 useFramebufferCache, std::move(fd), &readyFence);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001126
1127 if (status != NO_ERROR && mClientCompositionRequestCache) {
1128 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001129 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001130 }
1131
Alec Mourie4034bb2019-11-19 12:45:54 -08001132 auto& timeStats = getCompositionEngine().getTimeStats();
1133 if (readyFence.get() < 0) {
1134 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1135 } else {
1136 timeStats.recordRenderEngineDuration(renderEngineStart,
1137 std::make_shared<FenceTime>(
1138 new Fence(dup(readyFence.get()))));
1139 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001140
Lloyd Piqued3d69882019-02-28 16:03:46 -08001141 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001142}
1143
Vishnu Nair9b079a22020-01-21 14:36:08 -08001144std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001145 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001146 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001147 ALOGV("Rendering client layers");
1148
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001149 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001150 const Region viewportRegion(outputState.layerStackSpace.content);
Lloyd Pique688abd42019-02-15 15:42:24 -08001151 bool firstLayer = true;
1152 // Used when a layer clears part of the buffer.
Peiyong Lind8460c82020-07-28 16:04:22 -07001153 Region stubRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -08001154
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001155 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001156 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001157
Lloyd Pique01c77c12019-04-17 12:48:32 -07001158 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001159 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001160 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001161 auto& layerFE = layer->getLayerFE();
1162
Lloyd Piquea2468662019-03-07 21:31:06 -08001163 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001164 ALOGV("Layer: %s", layerFE.getDebugName());
1165 if (clip.isEmpty()) {
1166 ALOGV(" Skipping for empty clip");
1167 firstLayer = false;
1168 continue;
1169 }
1170
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001171 disableBlurs |= layerFEState->sidebandStream != nullptr;
1172
Vishnu Naira483b4a2019-12-12 15:07:52 -08001173 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001174
1175 // We clear the client target for non-client composed layers if
1176 // requested by the HWC. We skip this if the layer is not an opaque
1177 // rectangle, as by definition the layer must blend with whatever is
1178 // underneath. We also skip the first layer as the buffer target is
1179 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001180 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001181 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001182
1183 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1184
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001185 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1186 // composing the non-shadow content and only draw the shadows.
1187 const bool realContentIsVisible = clientComposition &&
1188 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1189
Lloyd Pique688abd42019-02-15 15:42:24 -08001190 if (clientComposition || clearClientComposition) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001191 compositionengine::LayerFE::ClientCompositionTargetSettings
1192 targetSettings{.clip = clip,
1193 .needsFiltering =
1194 layer->needsFiltering() || outputState.needsFiltering,
1195 .isSecure = outputState.isSecure,
1196 .supportsProtectedContent = supportsProtectedContent,
1197 .clearRegion = clientComposition ? clearRegion : stubRegion,
1198 .viewport = outputState.layerStackSpace.content,
1199 .dataspace = outputDataspace,
1200 .realContentIsVisible = realContentIsVisible,
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001201 .clearContent = !clientComposition,
1202 .disableBlurs = disableBlurs};
Dan Stoza6166c312021-01-15 16:34:05 -08001203
1204 std::vector<LayerFE::LayerSettings> results;
1205 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001206 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001207 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001208 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001209 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1210 } else {
1211 ALOGV("Skipping redundant override buffer for [%s] in RE",
1212 layer->getLayerFE().getDebugName());
1213 }
Dan Stoza6166c312021-01-15 16:34:05 -08001214 } else {
1215 results = layerFE.prepareClientCompositionList(targetSettings);
1216 if (realContentIsVisible && !results.empty()) {
1217 layer->editState().clientCompositionTimestamp = systemTime();
1218 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001219 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001220
1221 clientCompositionLayers.insert(clientCompositionLayers.end(),
1222 std::make_move_iterator(results.begin()),
1223 std::make_move_iterator(results.end()));
1224 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001225 }
1226
1227 firstLayer = false;
1228 }
1229
1230 return clientCompositionLayers;
1231}
1232
1233void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001234 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001235 if (flashRegion.isEmpty()) {
1236 return;
1237 }
1238
Vishnu Nair9b079a22020-01-21 14:36:08 -08001239 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001240 layerSettings.source.buffer.buffer = nullptr;
1241 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1242 layerSettings.alpha = half(1.0);
1243
1244 for (const auto& rect : flashRegion) {
1245 layerSettings.geometry.boundaries = rect.toFloatRect();
1246 clientCompositionLayers.push_back(layerSettings);
1247 }
1248}
1249
1250void Output::setExpensiveRenderingExpected(bool) {
1251 // The base class does nothing with this call.
1252}
1253
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001254void Output::postFramebuffer() {
1255 ATRACE_CALL();
1256 ALOGV(__FUNCTION__);
1257
1258 if (!getState().isEnabled) {
1259 return;
1260 }
1261
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001262 auto& outputState = editState();
1263 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001264 mRenderSurface->flip();
1265
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001266 auto frame = presentAndGetFrameFences();
1267
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001268 mRenderSurface->onPresentDisplayCompleted();
1269
Lloyd Pique01c77c12019-04-17 12:48:32 -07001270 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001271 // The layer buffer from the previous frame (if any) is released
1272 // by HWC only when the release fence from this frame (if any) is
1273 // signaled. Always get the release fence from HWC first.
1274 sp<Fence> releaseFence = Fence::NO_FENCE;
1275
1276 if (auto hwcLayer = layer->getHwcLayer()) {
1277 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1278 releaseFence = f->second;
1279 }
1280 }
1281
1282 // If the layer was client composited in the previous frame, we
1283 // need to merge with the previous client target acquire fence.
1284 // Since we do not track that, always merge with the current
1285 // client target acquire fence when it is available, even though
1286 // this is suboptimal.
1287 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001288 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001289 releaseFence =
1290 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1291 }
1292
1293 layer->getLayerFE().onLayerDisplayed(releaseFence);
1294 }
1295
1296 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001297 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001298 // supply them with the present fence.
1299 for (auto& weakLayer : mReleasedLayers) {
1300 if (auto layer = weakLayer.promote(); layer != nullptr) {
1301 layer->onLayerDisplayed(frame.presentFence);
1302 }
1303 }
1304
1305 // Clear out the released layers now that we're done with them.
1306 mReleasedLayers.clear();
1307}
1308
Dan Stoza6166c312021-01-15 16:34:05 -08001309void Output::renderCachedSets() {
1310 if (mPlanner) {
Huihong Luoa5825112021-03-24 12:28:29 -07001311 mPlanner->renderCachedSets(getCompositionEngine().getRenderEngine(), getState());
Dan Stoza6166c312021-01-15 16:34:05 -08001312 }
1313}
1314
Lloyd Pique32cbe282018-10-19 13:09:22 -07001315void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001316 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001317 outputState.dirtyRegion.set(outputState.displaySpace.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001318}
1319
Lloyd Pique66d68602019-02-13 14:23:31 -08001320void Output::chooseCompositionStrategy() {
1321 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001322 auto& outputState = editState();
1323 outputState.usesClientComposition = true;
1324 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001325 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001326}
1327
Lloyd Pique688abd42019-02-15 15:42:24 -08001328bool Output::getSkipColorTransform() const {
1329 return true;
1330}
1331
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001332compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1333 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001334 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001335 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1336 }
1337 return result;
1338}
1339
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001340} // namespace impl
1341} // namespace android::compositionengine