blob: bf36355fd8c51d1eef7ef7d256e3fe1d659cf4bc [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 }
139}
140
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200141void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
142 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700143 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200144
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200145 outputState.displaySpace.orientation = orientation;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200146 LOG_FATAL_IF(outputState.displaySpace.bounds == Rect::INVALID_RECT,
147 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200148
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200149 // Compute orientedDisplaySpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200150 ui::Size orientedSize = outputState.displaySpace.bounds.getSize();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200151 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200152 std::swap(orientedSize.width, orientedSize.height);
153 }
154 outputState.orientedDisplaySpace.bounds = Rect(orientedSize);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200155 outputState.orientedDisplaySpace.content = orientedDisplaySpaceRect;
156
157 // Compute displaySpace.content
158 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
159 ui::Transform rotation;
160 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
161 const auto displaySize = outputState.displaySpace.bounds;
162 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
163 }
164 outputState.displaySpace.content = rotation.transform(orientedDisplaySpaceRect);
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200165
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200166 // Compute framebufferSpace
167 outputState.framebufferSpace.orientation = orientation;
168 LOG_FATAL_IF(outputState.framebufferSpace.bounds == Rect::INVALID_RECT,
169 "The framebuffer bounds are unknown.");
170 const auto scale =
Marin Shalamanov209ae612020-10-01 00:17:39 +0200171 getScale(outputState.displaySpace.bounds, outputState.framebufferSpace.bounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200172 outputState.framebufferSpace.content = outputState.displaySpace.content.scale(scale.x, scale.y);
173
174 // Compute layerStackSpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200175 outputState.layerStackSpace.content = layerStackSpaceRect;
176 outputState.layerStackSpace.bounds = layerStackSpaceRect;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200177
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200178 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
179 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700180 dirtyEntireOutput();
181}
182
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200183void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700184 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200185
186 auto& state = editState();
187
188 // Update framebuffer space
189 const Rect newBounds(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200190 state.framebufferSpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200191
192 // Update display space
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200193 state.displaySpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200194 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
195
196 // Update oriented display space
197 const auto orientation = state.displaySpace.orientation;
198 ui::Size orientedSize = size;
199 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
200 std::swap(orientedSize.width, orientedSize.height);
201 }
202 const Rect newOrientedBounds(orientedSize);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200203 state.orientedDisplaySpace.bounds = newOrientedBounds;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700204
Dan Stoza6166c312021-01-15 16:34:05 -0800205 if (mPlanner) {
206 mPlanner->setDisplaySize(size);
207 }
208
Lloyd Pique32cbe282018-10-19 13:09:22 -0700209 dirtyEntireOutput();
210}
211
Garfield Tan54edd912020-10-21 16:31:41 -0700212ui::Transform::RotationFlags Output::getTransformHint() const {
213 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
214}
215
Lloyd Piqueef36b002019-01-23 17:52:04 -0800216void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700217 auto& outputState = editState();
218 outputState.layerStackId = layerStackId;
219 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700220
221 dirtyEntireOutput();
222}
223
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800224void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700225 auto& colorTransformMatrix = editState().colorTransformMatrix;
226 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700227 return;
228 }
229
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700230 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800231
232 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700233}
234
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800235void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700236 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800237 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
238 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800239
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700240 auto& outputState = editState();
241 if (outputState.colorMode == colorProfile.mode &&
242 outputState.dataspace == colorProfile.dataspace &&
243 outputState.renderIntent == colorProfile.renderIntent &&
244 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800245 return;
246 }
247
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700248 outputState.colorMode = colorProfile.mode;
249 outputState.dataspace = colorProfile.dataspace;
250 outputState.renderIntent = colorProfile.renderIntent;
251 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700252
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800253 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700254
Lloyd Pique32cbe282018-10-19 13:09:22 -0700255 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800256 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
257 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800258
259 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700260}
261
John Reckac09e452021-04-07 16:35:37 -0400262void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
263 auto& outputState = editState();
264 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
265 outputState.displayBrightnessNits == displayBrightnessNits) {
266 // Nothing changed
267 return;
268 }
269 outputState.sdrWhitePointNits = sdrWhitePointNits;
270 outputState.displayBrightnessNits = displayBrightnessNits;
271 dirtyEntireOutput();
272}
273
Lloyd Pique32cbe282018-10-19 13:09:22 -0700274void Output::dump(std::string& out) const {
275 using android::base::StringAppendF;
276
277 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
278
279 out.append("\n ");
280
281 dumpBase(out);
282}
283
284void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700285 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700286
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700287 if (mDisplayColorProfile) {
288 mDisplayColorProfile->dump(out);
289 } else {
290 out.append(" No display color profile!\n");
291 }
292
Lloyd Pique31cb2942018-10-19 17:23:03 -0700293 if (mRenderSurface) {
294 mRenderSurface->dump(out);
295 } else {
296 out.append(" No render surface!\n");
297 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800298
Lloyd Pique01c77c12019-04-17 12:48:32 -0700299 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
300 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800301 if (!outputLayer) {
302 continue;
303 }
304 outputLayer->dump(out);
305 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700306}
307
Dan Stoza269dc4d2021-01-15 15:07:43 -0800308void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
309 if (!mPlanner) {
310 base::StringAppendF(&out, "Planner is disabled\n");
311 return;
312 }
313 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
314 mPlanner->dump(args, out);
315}
316
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700317compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
318 return mDisplayColorProfile.get();
319}
320
321void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
322 mDisplayColorProfile = std::move(mode);
323}
324
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800325const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
326 return mReleasedLayers;
327}
328
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700329void Output::setDisplayColorProfileForTest(
330 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
331 mDisplayColorProfile = std::move(mode);
332}
333
Lloyd Pique31cb2942018-10-19 17:23:03 -0700334compositionengine::RenderSurface* Output::getRenderSurface() const {
335 return mRenderSurface.get();
336}
337
338void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
339 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800340 const auto size = mRenderSurface->getSize();
341 editState().framebufferSpace.bounds = Rect(size);
342 if (mPlanner) {
343 mPlanner->setDisplaySize(size);
344 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700345 dirtyEntireOutput();
346}
347
Vishnu Nair9b079a22020-01-21 14:36:08 -0800348void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
349 if (cacheSize == 0) {
350 mClientCompositionRequestCache.reset();
351 } else {
352 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
353 }
354};
355
Lloyd Pique31cb2942018-10-19 17:23:03 -0700356void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
357 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700358}
359
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000360Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700361 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200362 Region dirty(outputState.layerStackSpace.content);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000363 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700364 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700365 }
366 return dirty;
367}
368
Lloyd Piquec6687342019-03-07 21:34:57 -0800369bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800370 // The layerStackId's must match, and also the layer must not be internal
371 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700372 const auto& outputState = getState();
373 return layerStackId && (*layerStackId == outputState.layerStackId) &&
374 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700375}
376
Lloyd Piquede196652020-01-22 17:29:58 -0800377bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const {
378 const auto* layerFEState = layerFE->getCompositionState();
379 return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800380}
381
Lloyd Piquedf336d92019-03-07 21:38:42 -0800382std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800383 const sp<LayerFE>& layerFE) const {
384 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800385}
386
Lloyd Piquede196652020-01-22 17:29:58 -0800387compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
388 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700389 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800390}
391
Lloyd Pique01c77c12019-04-17 12:48:32 -0700392std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800393 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700394 for (size_t i = 0; i < getOutputLayerCount(); i++) {
395 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800396 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700397 return i;
398 }
399 }
400 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800401}
402
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800403void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
404 mReleasedLayers = std::move(layers);
405}
406
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800407void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
408 LayerFESet& geomSnapshots) {
409 ATRACE_CALL();
410 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800411
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800412 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800413}
414
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800415void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800416 ATRACE_CALL();
417 ALOGV(__FUNCTION__);
418
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800419 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800420 updateCompositionState(refreshArgs);
421 planComposition();
422 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800423 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800424 beginFrame();
425 prepareFrame();
426 devOptRepaintFlash(refreshArgs);
427 finishFrame(refreshArgs);
428 postFramebuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800429 renderCachedSets();
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800430}
431
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800432void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
433 LayerFESet& layerFESet) {
434 ATRACE_CALL();
435 ALOGV(__FUNCTION__);
436
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700437 auto& outputState = editState();
438
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800439 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700440 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800441 return;
442 }
443
444 // Process the layers to determine visibility and coverage
445 compositionengine::Output::CoverageState coverage{layerFESet};
446 collectVisibleLayers(refreshArgs, coverage);
447
448 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700449 const ui::Transform& tr = outputState.transform;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200450 Region undefinedRegion{outputState.displaySpace.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800451 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
452
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700453 outputState.undefinedRegion = undefinedRegion;
454 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800455}
456
457void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
458 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800459 // Evaluate the layers from front to back to determine what is visible. This
460 // also incrementally calculates the coverage information for each layer as
461 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800462 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700463 // Incrementally process the coverage for each layer
464 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800465
466 // TODO(b/121291683): Stop early if the output is completely covered and
467 // no more layers could even be visible underneath the ones on top.
468 }
469
Lloyd Pique01c77c12019-04-17 12:48:32 -0700470 setReleasedLayers(refreshArgs);
471
472 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800473}
474
Lloyd Piquede196652020-01-22 17:29:58 -0800475void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700476 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800477 // Ensure we have a snapshot of the basic geometry layer state. Limit the
478 // snapshots to once per frame for each candidate layer, as layers may
479 // appear on multiple outputs.
480 if (!coverage.latchedLayers.count(layerFE)) {
481 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800482 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800483 }
484
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800485 // Only consider the layers on the given layer stack
Lloyd Piquede196652020-01-22 17:29:58 -0800486 if (!belongsInOutput(layerFE)) {
487 return;
488 }
489
490 // Obtain a read-only pointer to the front-end layer state
491 const auto* layerFEState = layerFE->getCompositionState();
492 if (CC_UNLIKELY(!layerFEState)) {
493 return;
494 }
495
496 // handle hidden surfaces by setting the visible region to empty
497 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700498 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800499 }
500
501 /*
502 * opaqueRegion: area of a surface that is fully opaque.
503 */
504 Region opaqueRegion;
505
506 /*
507 * visibleRegion: area of a surface that is visible on screen and not fully
508 * transparent. This is essentially the layer's footprint minus the opaque
509 * regions above it. Areas covered by a translucent surface are considered
510 * visible.
511 */
512 Region visibleRegion;
513
514 /*
515 * coveredRegion: area of a surface that is covered by all visible regions
516 * above it (which includes the translucent areas).
517 */
518 Region coveredRegion;
519
520 /*
521 * transparentRegion: area of a surface that is hinted to be completely
522 * transparent. This is only used to tell when the layer has no visible non-
523 * transparent regions and can be removed from the layer list. It does not
524 * affect the visibleRegion of this layer or any layers beneath it. The hint
525 * may not be correct if apps don't respect the SurfaceView restrictions
526 * (which, sadly, some don't).
527 */
528 Region transparentRegion;
529
Vishnu Naira483b4a2019-12-12 15:07:52 -0800530 /*
531 * shadowRegion: Region cast by the layer's shadow.
532 */
533 Region shadowRegion;
534
Lloyd Piquede196652020-01-22 17:29:58 -0800535 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800536
537 // Get the visible region
538 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
539 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800540 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800541 visibleRegion.set(visibleRect);
542
Lloyd Piquede196652020-01-22 17:29:58 -0800543 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800544 // if the layer casts a shadow, offset the layers visible region and
545 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800546 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800547 Rect visibleRectWithShadows(visibleRect);
548 visibleRectWithShadows.inset(inset, inset, inset, inset);
549 visibleRegion.set(visibleRectWithShadows);
550 shadowRegion = visibleRegion.subtract(visibleRect);
551 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800552
553 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700554 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800555 }
556
557 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800558 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800559 if (tr.preserveRects()) {
560 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800561 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800562 } else {
563 // transformation too complex, can't do the
564 // transparent region optimization.
565 transparentRegion.clear();
566 }
567 }
568
569 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800570 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800571 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800572 // If we one of the simple category of transforms (0/90/180/270 rotation
573 // + any flip), then the opaque region is the layer's footprint.
574 // Otherwise we don't try and compute the opaque region since there may
575 // be errors at the edges, and we treat the entire layer as
576 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800577 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800578 }
579
580 // Clip the covered region to the visible region
581 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
582
583 // Update accumAboveCoveredLayers for next (lower) layer
584 coverage.aboveCoveredLayers.orSelf(visibleRegion);
585
586 // subtract the opaque region covered by the layers above us
587 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
588
589 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700590 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800591 }
592
593 // Get coverage information for the layer as previously displayed,
594 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800595 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700596 auto prevOutputLayer =
597 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800598
599 // Get coverage information for the layer as previously displayed
600 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
601 const Region kEmptyRegion;
602 const Region& oldVisibleRegion =
603 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
604 const Region& oldCoveredRegion =
605 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
606
607 // compute this layer's dirty region
608 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800609 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800610 // we need to invalidate the whole region
611 dirty = visibleRegion;
612 // as well, as the old visible region
613 dirty.orSelf(oldVisibleRegion);
614 } else {
615 /* compute the exposed region:
616 * the exposed region consists of two components:
617 * 1) what's VISIBLE now and was COVERED before
618 * 2) what's EXPOSED now less what was EXPOSED before
619 *
620 * note that (1) is conservative, we start with the whole visible region
621 * but only keep what used to be covered by something -- which mean it
622 * may have been exposed.
623 *
624 * (2) handles areas that were not covered by anything but got exposed
625 * because of a resize.
626 *
627 */
628 const Region newExposed = visibleRegion - coveredRegion;
629 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
630 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
631 }
632 dirty.subtractSelf(coverage.aboveOpaqueLayers);
633
634 // accumulate to the screen dirty region
635 coverage.dirtyRegion.orSelf(dirty);
636
637 // Update accumAboveOpaqueLayers for next (lower) layer
638 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
639
640 // Compute the visible non-transparent region
641 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
642
Vishnu Naira483b4a2019-12-12 15:07:52 -0800643 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800644 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700645 const auto& outputState = getState();
646 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200647 drawRegion.andSelf(outputState.displaySpace.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800648 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700649 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800650 }
651
Vishnu Naira483b4a2019-12-12 15:07:52 -0800652 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
653
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800654 // The layer is visible. Either reuse the existing outputLayer if we have
655 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800656 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800657
658 // Store the layer coverage information into the layer state as some of it
659 // is useful later.
660 auto& outputLayerState = result->editState();
661 outputLayerState.visibleRegion = visibleRegion;
662 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
663 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200664 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
665 visibleNonShadowRegion.intersect(outputState.layerStackSpace.content));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800666 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800667}
668
669void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
670 // The base class does nothing with this call.
671}
672
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800673void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700674 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800675 layer->getLayerFE().prepareCompositionState(
676 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
677 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800678 }
679}
680
Dan Stoza269dc4d2021-01-15 15:07:43 -0800681void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800682 ATRACE_CALL();
683 ALOGV(__FUNCTION__);
684
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800685 if (!getState().isEnabled) {
686 return;
687 }
688
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800689 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
690 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
691
Lloyd Pique01c77c12019-04-17 12:48:32 -0700692 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700693 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800694 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200695 forceClientComposition,
696 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800697
698 if (mLayerRequestingBackgroundBlur == layer) {
699 forceClientComposition = false;
700 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800701 }
702}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800703
Dan Stoza269dc4d2021-01-15 15:07:43 -0800704void Output::planComposition() {
705 if (!mPlanner || !getState().isEnabled) {
706 return;
707 }
708
709 ATRACE_CALL();
710 ALOGV(__FUNCTION__);
711
712 mPlanner->plan(getOutputLayersOrderedByZ());
713}
714
715void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
716 ATRACE_CALL();
717 ALOGV(__FUNCTION__);
718
719 if (!getState().isEnabled) {
720 return;
721 }
722
Ady Abraham3645e642021-04-20 18:39:00 -0700723 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
724
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400725 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800726 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400727 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400728 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400729 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800730 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400731 if (layer == peekThroughLayer) {
732 // No longer needed, although it should not show up again, so
733 // resetting it is not truly needed either.
734 peekThroughLayer = nullptr;
735
736 // peekThroughLayer was already drawn ahead of its z order.
737 continue;
738 }
Dan Stoza6166c312021-01-15 16:34:05 -0800739 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400740 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400741 if (overrideInfo.buffer != nullptr) {
742 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800743 ALOGV("Skipping redundant buffer");
744 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400745 } else {
746 // First layer with the override buffer.
747 if (overrideInfo.peekThroughLayer) {
748 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400749
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400750 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400751 overrideZ = true;
752 includeGeometry = true;
753 constexpr bool isPeekingThrough = true;
754 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
755 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400756 }
757
758 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800759 }
Dan Stoza6166c312021-01-15 16:34:05 -0800760 }
761
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400762 constexpr bool isPeekingThrough = false;
763 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800764 }
765}
766
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800767compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
768 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
769 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100770 auto* compState = layer->getLayerFE().getCompositionState();
771
772 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
773 // want to force client composition because of the blur.
774 if (compState->sidebandStream != nullptr) {
775 return nullptr;
776 }
777 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800778 layerRequestingBgComposition = layer;
779 }
780 }
781 return layerRequestingBgComposition;
782}
783
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800784void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
785 setColorProfile(pickColorProfile(refreshArgs));
786}
787
788// Returns a data space that fits all visible layers. The returned data space
789// can only be one of
790// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
791// - Dataspace::DISPLAY_P3
792// - Dataspace::DISPLAY_BT2020
793// The returned HDR data space is one of
794// - Dataspace::UNKNOWN
795// - Dataspace::BT2020_HLG
796// - Dataspace::BT2020_PQ
797ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
798 bool* outIsHdrClientComposition) const {
799 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
800 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
801
Lloyd Pique01c77c12019-04-17 12:48:32 -0700802 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800803 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800804 case ui::Dataspace::V0_SCRGB:
805 case ui::Dataspace::V0_SCRGB_LINEAR:
806 case ui::Dataspace::BT2020:
807 case ui::Dataspace::BT2020_ITU:
808 case ui::Dataspace::BT2020_LINEAR:
809 case ui::Dataspace::DISPLAY_BT2020:
810 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
811 break;
812 case ui::Dataspace::DISPLAY_P3:
813 bestDataSpace = ui::Dataspace::DISPLAY_P3;
814 break;
815 case ui::Dataspace::BT2020_PQ:
816 case ui::Dataspace::BT2020_ITU_PQ:
817 bestDataSpace = ui::Dataspace::DISPLAY_P3;
818 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800819 *outIsHdrClientComposition =
820 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800821 break;
822 case ui::Dataspace::BT2020_HLG:
823 case ui::Dataspace::BT2020_ITU_HLG:
824 bestDataSpace = ui::Dataspace::DISPLAY_P3;
825 // When there's mixed PQ content and HLG content, we set the HDR
826 // data space to be BT2020_PQ and convert HLG to PQ.
827 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
828 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
829 }
830 break;
831 default:
832 break;
833 }
834 }
835
836 return bestDataSpace;
837}
838
839compositionengine::Output::ColorProfile Output::pickColorProfile(
840 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
841 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
842 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
843 ui::RenderIntent::COLORIMETRIC,
844 refreshArgs.colorSpaceAgnosticDataspace};
845 }
846
847 ui::Dataspace hdrDataSpace;
848 bool isHdrClientComposition = false;
849 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
850
851 switch (refreshArgs.forceOutputColorMode) {
852 case ui::ColorMode::SRGB:
853 bestDataSpace = ui::Dataspace::V0_SRGB;
854 break;
855 case ui::ColorMode::DISPLAY_P3:
856 bestDataSpace = ui::Dataspace::DISPLAY_P3;
857 break;
858 default:
859 break;
860 }
861
862 // respect hdrDataSpace only when there is no legacy HDR support
863 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
864 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
865 if (isHdr) {
866 bestDataSpace = hdrDataSpace;
867 }
868
869 ui::RenderIntent intent;
870 switch (refreshArgs.outputColorSetting) {
871 case OutputColorSetting::kManaged:
872 case OutputColorSetting::kUnmanaged:
873 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
874 : ui::RenderIntent::COLORIMETRIC;
875 break;
876 case OutputColorSetting::kEnhanced:
877 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
878 break;
879 default: // vendor display color setting
880 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
881 break;
882 }
883
884 ui::ColorMode outMode;
885 ui::Dataspace outDataSpace;
886 ui::RenderIntent outRenderIntent;
887 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
888 &outRenderIntent);
889
890 return ColorProfile{outMode, outDataSpace, outRenderIntent,
891 refreshArgs.colorSpaceAgnosticDataspace};
892}
893
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800894void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700895 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800896 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700897 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700898 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800899
900 // If nothing has changed (!dirty), don't recompose.
901 // If something changed, but we don't currently have any visible layers,
902 // and didn't when we last did a composition, then skip it this time.
903 // The second rule does two things:
904 // - When all layers are removed from a display, we'll emit one black
905 // frame, then nothing more until we get new layers.
906 // - When a display is created with a private layer stack, we won't
907 // emit any black frames until a layer is added to the layer stack.
908 const bool mustRecompose = dirty && !(empty && wasEmpty);
909
910 const char flagPrefix[] = {'-', '+'};
911 static_cast<void>(flagPrefix);
912 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
913 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
914 flagPrefix[empty], flagPrefix[wasEmpty]);
915
916 mRenderSurface->beginFrame(mustRecompose);
917
918 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700919 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800920 }
921}
922
Lloyd Pique66d68602019-02-13 14:23:31 -0800923void Output::prepareFrame() {
924 ATRACE_CALL();
925 ALOGV(__FUNCTION__);
926
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700927 const auto& outputState = getState();
928 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800929 return;
930 }
931
932 chooseCompositionStrategy();
933
Dan Stoza47437bb2021-01-15 16:21:07 -0800934 if (mPlanner) {
935 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
936 }
937
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700938 mRenderSurface->prepareFrame(outputState.usesClientComposition,
939 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800940}
941
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800942void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
943 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
944 return;
945 }
946
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700947 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800948 // transform the dirty region into this screen's coordinate space
949 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
950 if (!dirtyRegion.isEmpty()) {
951 base::unique_fd readyFence;
952 // redraw the whole screen
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800953 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800954
955 mRenderSurface->queueBuffer(std::move(readyFence));
956 }
957 }
958
959 postFramebuffer();
960
961 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
962
963 prepareFrame();
964}
965
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800966void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800967 ATRACE_CALL();
968 ALOGV(__FUNCTION__);
969
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700970 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800971 return;
972 }
973
974 // Repaint the framebuffer (if needed), getting the optional fence for when
975 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800976 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800977 if (!optReadyFence) {
978 return;
979 }
980
981 // swap buffers (presentation)
982 mRenderSurface->queueBuffer(std::move(*optReadyFence));
983}
984
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800985std::optional<base::unique_fd> Output::composeSurfaces(
986 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800987 ATRACE_CALL();
988 ALOGV(__FUNCTION__);
989
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700990 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800991 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800992 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700993 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -0700994
995 auto& renderEngine = getCompositionEngine().getRenderEngine();
996 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
997
998 // If we the display is secure, protected content support is enabled, and at
999 // least one layer has protected content, we need to use a secure back
1000 // buffer.
1001 if (outputState.isSecure && supportsProtectedContent) {
1002 auto layers = getOutputLayersOrderedByZ();
1003 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1004 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1005 });
1006 if (needsProtected != renderEngine.isProtected()) {
1007 renderEngine.useProtectedContext(needsProtected);
1008 }
1009 if (needsProtected != mRenderSurface->isProtected() &&
1010 needsProtected == renderEngine.isProtected()) {
1011 mRenderSurface->setProtected(needsProtected);
1012 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001013 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1014 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001015 }
1016
1017 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +00001018
1019 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001020
1021 // If we aren't doing client composition on this output, but do have a
1022 // flipClientTarget request for this frame on this output, we still need to
1023 // dequeue a buffer.
1024 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +00001025 tex = mRenderSurface->dequeueBuffer(&fd);
1026 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001027 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1028 "client composition for this frame",
1029 mName.c_str());
1030 return {};
1031 }
1032 }
1033
Lloyd Piqued3d69882019-02-28 16:03:46 -08001034 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001035 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001036 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -08001037 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001038 }
1039
1040 ALOGV("hasClientComposition");
1041
Lloyd Pique688abd42019-02-15 15:42:24 -08001042 renderengine::DisplaySettings clientCompositionDisplay;
Marin Shalamanovb15d2272020-09-17 21:41:52 +02001043 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.content;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001044 clientCompositionDisplay.clip = outputState.layerStackSpace.content;
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001045 clientCompositionDisplay.orientation =
1046 ui::Transform::toRotationFlags(outputState.displaySpace.orientation);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001047 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1048 ? outputState.dataspace
1049 : ui::Dataspace::UNKNOWN;
John Reckac09e452021-04-07 16:35:37 -04001050
1051 // If we have a valid current display brightness use that, otherwise fall back to the
1052 // display's max desired
1053 clientCompositionDisplay.maxLuminance = outputState.displayBrightnessNits > 0.f
1054 ? outputState.displayBrightnessNits
1055 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1056 clientCompositionDisplay.sdrWhitePointNits = outputState.sdrWhitePointNits;
Lloyd Pique688abd42019-02-15 15:42:24 -08001057
1058 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001059 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1060 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001061 }
1062
1063 // Note: Updated by generateClientCompositionRequests
1064 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
1065
1066 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -08001067 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001068 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001069 clientCompositionDisplay.clearRegion,
1070 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -08001071 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1072
Vishnu Nair9b079a22020-01-21 14:36:08 -08001073 // Check if the client composition requests were rendered into the provided graphic buffer. If
1074 // so, we can reuse the buffer and avoid client composition.
1075 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001076 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1077 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001078 clientCompositionLayers)) {
1079 outputCompositionState.reusedClientComposition = true;
1080 setExpensiveRenderingExpected(false);
1081 return readyFence;
1082 }
Alec Mouria90a5702021-04-16 16:36:21 +00001083 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001084 clientCompositionLayers);
1085 }
1086
Lloyd Pique688abd42019-02-15 15:42:24 -08001087 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001088 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1089 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1090 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001091 const bool expensiveBlurs =
1092 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001093 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001094 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001095 if (expensiveRenderingExpected) {
1096 setExpensiveRenderingExpected(true);
1097 }
1098
Vishnu Nair9b079a22020-01-21 14:36:08 -08001099 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
1100 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
1101 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1102 std::back_inserter(clientCompositionLayerPointers),
1103 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
1104 return &settings;
1105 });
1106
Alec Mourie4034bb2019-11-19 12:45:54 -08001107 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001108 // Only use the framebuffer cache when rendering to an internal display
1109 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1110 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1111 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1112 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1113 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
1114 const bool useFramebufferCache = outputState.layerStackInternal;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001115 status_t status =
Alec Mouria90a5702021-04-16 16:36:21 +00001116 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, tex,
Alec Mouri1684c702021-02-04 12:27:26 -08001117 useFramebufferCache, std::move(fd), &readyFence);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001118
1119 if (status != NO_ERROR && mClientCompositionRequestCache) {
1120 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001121 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001122 }
1123
Alec Mourie4034bb2019-11-19 12:45:54 -08001124 auto& timeStats = getCompositionEngine().getTimeStats();
1125 if (readyFence.get() < 0) {
1126 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1127 } else {
1128 timeStats.recordRenderEngineDuration(renderEngineStart,
1129 std::make_shared<FenceTime>(
1130 new Fence(dup(readyFence.get()))));
1131 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001132
Lloyd Piqued3d69882019-02-28 16:03:46 -08001133 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001134}
1135
Vishnu Nair9b079a22020-01-21 14:36:08 -08001136std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001137 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001138 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001139 ALOGV("Rendering client layers");
1140
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001141 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001142 const Region viewportRegion(outputState.layerStackSpace.content);
Lloyd Pique688abd42019-02-15 15:42:24 -08001143 bool firstLayer = true;
1144 // Used when a layer clears part of the buffer.
Peiyong Lind8460c82020-07-28 16:04:22 -07001145 Region stubRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -08001146
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001147 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001148 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001149
Lloyd Pique01c77c12019-04-17 12:48:32 -07001150 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001151 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001152 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001153 auto& layerFE = layer->getLayerFE();
1154
Lloyd Piquea2468662019-03-07 21:31:06 -08001155 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001156 ALOGV("Layer: %s", layerFE.getDebugName());
1157 if (clip.isEmpty()) {
1158 ALOGV(" Skipping for empty clip");
1159 firstLayer = false;
1160 continue;
1161 }
1162
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001163 disableBlurs |= layerFEState->sidebandStream != nullptr;
1164
Vishnu Naira483b4a2019-12-12 15:07:52 -08001165 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001166
1167 // We clear the client target for non-client composed layers if
1168 // requested by the HWC. We skip this if the layer is not an opaque
1169 // rectangle, as by definition the layer must blend with whatever is
1170 // underneath. We also skip the first layer as the buffer target is
1171 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001172 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001173 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001174
1175 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1176
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001177 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1178 // composing the non-shadow content and only draw the shadows.
1179 const bool realContentIsVisible = clientComposition &&
1180 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1181
Lloyd Pique688abd42019-02-15 15:42:24 -08001182 if (clientComposition || clearClientComposition) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001183 compositionengine::LayerFE::ClientCompositionTargetSettings
1184 targetSettings{.clip = clip,
1185 .needsFiltering =
1186 layer->needsFiltering() || outputState.needsFiltering,
1187 .isSecure = outputState.isSecure,
1188 .supportsProtectedContent = supportsProtectedContent,
1189 .clearRegion = clientComposition ? clearRegion : stubRegion,
1190 .viewport = outputState.layerStackSpace.content,
1191 .dataspace = outputDataspace,
1192 .realContentIsVisible = realContentIsVisible,
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001193 .clearContent = !clientComposition,
1194 .disableBlurs = disableBlurs};
Dan Stoza6166c312021-01-15 16:34:05 -08001195
1196 std::vector<LayerFE::LayerSettings> results;
1197 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001198 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001199 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001200 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001201 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1202 } else {
1203 ALOGV("Skipping redundant override buffer for [%s] in RE",
1204 layer->getLayerFE().getDebugName());
1205 }
Dan Stoza6166c312021-01-15 16:34:05 -08001206 } else {
1207 results = layerFE.prepareClientCompositionList(targetSettings);
1208 if (realContentIsVisible && !results.empty()) {
1209 layer->editState().clientCompositionTimestamp = systemTime();
1210 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001211 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001212
1213 clientCompositionLayers.insert(clientCompositionLayers.end(),
1214 std::make_move_iterator(results.begin()),
1215 std::make_move_iterator(results.end()));
1216 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001217 }
1218
1219 firstLayer = false;
1220 }
1221
1222 return clientCompositionLayers;
1223}
1224
1225void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001226 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001227 if (flashRegion.isEmpty()) {
1228 return;
1229 }
1230
Vishnu Nair9b079a22020-01-21 14:36:08 -08001231 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001232 layerSettings.source.buffer.buffer = nullptr;
1233 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1234 layerSettings.alpha = half(1.0);
1235
1236 for (const auto& rect : flashRegion) {
1237 layerSettings.geometry.boundaries = rect.toFloatRect();
1238 clientCompositionLayers.push_back(layerSettings);
1239 }
1240}
1241
1242void Output::setExpensiveRenderingExpected(bool) {
1243 // The base class does nothing with this call.
1244}
1245
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001246void Output::postFramebuffer() {
1247 ATRACE_CALL();
1248 ALOGV(__FUNCTION__);
1249
1250 if (!getState().isEnabled) {
1251 return;
1252 }
1253
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001254 auto& outputState = editState();
1255 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001256 mRenderSurface->flip();
1257
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001258 auto frame = presentAndGetFrameFences();
1259
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001260 mRenderSurface->onPresentDisplayCompleted();
1261
Lloyd Pique01c77c12019-04-17 12:48:32 -07001262 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001263 // The layer buffer from the previous frame (if any) is released
1264 // by HWC only when the release fence from this frame (if any) is
1265 // signaled. Always get the release fence from HWC first.
1266 sp<Fence> releaseFence = Fence::NO_FENCE;
1267
1268 if (auto hwcLayer = layer->getHwcLayer()) {
1269 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1270 releaseFence = f->second;
1271 }
1272 }
1273
1274 // If the layer was client composited in the previous frame, we
1275 // need to merge with the previous client target acquire fence.
1276 // Since we do not track that, always merge with the current
1277 // client target acquire fence when it is available, even though
1278 // this is suboptimal.
1279 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001280 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001281 releaseFence =
1282 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1283 }
1284
1285 layer->getLayerFE().onLayerDisplayed(releaseFence);
1286 }
1287
1288 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001289 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001290 // supply them with the present fence.
1291 for (auto& weakLayer : mReleasedLayers) {
1292 if (auto layer = weakLayer.promote(); layer != nullptr) {
1293 layer->onLayerDisplayed(frame.presentFence);
1294 }
1295 }
1296
1297 // Clear out the released layers now that we're done with them.
1298 mReleasedLayers.clear();
1299}
1300
Dan Stoza6166c312021-01-15 16:34:05 -08001301void Output::renderCachedSets() {
1302 if (mPlanner) {
Huihong Luoa5825112021-03-24 12:28:29 -07001303 mPlanner->renderCachedSets(getCompositionEngine().getRenderEngine(), getState());
Dan Stoza6166c312021-01-15 16:34:05 -08001304 }
1305}
1306
Lloyd Pique32cbe282018-10-19 13:09:22 -07001307void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001308 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001309 outputState.dirtyRegion.set(outputState.displaySpace.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001310}
1311
Lloyd Pique66d68602019-02-13 14:23:31 -08001312void Output::chooseCompositionStrategy() {
1313 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001314 auto& outputState = editState();
1315 outputState.usesClientComposition = true;
1316 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001317 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001318}
1319
Lloyd Pique688abd42019-02-15 15:42:24 -08001320bool Output::getSkipColorTransform() const {
1321 return true;
1322}
1323
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001324compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1325 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001326 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001327 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1328 }
1329 return result;
1330}
1331
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001332} // namespace impl
1333} // namespace android::compositionengine