blob: c809e1afad6d33e1451a35dfea8f0d219962eb57 [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
Dan Stoza269dc4d2021-01-15 15:07:43 -080058Output::Output() {
59 const bool enableLayerCaching = [] {
60 const bool enable =
61 android::sysprop::SurfaceFlingerProperties::enable_layer_caching().value_or(false);
62 return base::GetBoolProperty(std::string("debug.sf.enable_layer_caching"), enable);
63 }();
64
65 if (enableLayerCaching) {
66 mPlanner = std::make_unique<planner::Planner>();
67 }
68}
69
Lloyd Piquec29e4c62019-03-07 21:48:19 -080070namespace {
71
72template <typename T>
73class Reversed {
74public:
75 explicit Reversed(const T& container) : mContainer(container) {}
76 auto begin() { return mContainer.rbegin(); }
77 auto end() { return mContainer.rend(); }
78
79private:
80 const T& mContainer;
81};
82
83// Helper for enumerating over a container in reverse order
84template <typename T>
85Reversed<T> reversed(const T& c) {
86 return Reversed<T>(c);
87}
88
Marin Shalamanovb15d2272020-09-17 21:41:52 +020089struct ScaleVector {
90 float x;
91 float y;
92};
93
94// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
95// where to' will have the same size as "to". In the case where "from" and "to"
96// start at the origin to'=to.
97ScaleVector getScale(const Rect& from, const Rect& to) {
98 return {.x = static_cast<float>(to.width()) / from.width(),
99 .y = static_cast<float>(to.height()) / from.height()};
100}
101
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800102} // namespace
103
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700104std::shared_ptr<Output> createOutput(
105 const compositionengine::CompositionEngine& compositionEngine) {
106 return createOutputTemplated<Output>(compositionEngine);
107}
Lloyd Pique32cbe282018-10-19 13:09:22 -0700108
109Output::~Output() = default;
110
Lloyd Pique32cbe282018-10-19 13:09:22 -0700111bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700112 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
113 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700114}
115
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700116std::optional<DisplayId> Output::getDisplayId() const {
117 return {};
118}
119
Lloyd Pique32cbe282018-10-19 13:09:22 -0700120const std::string& Output::getName() const {
121 return mName;
122}
123
124void Output::setName(const std::string& name) {
125 mName = name;
126}
127
128void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700129 auto& outputState = editState();
130 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700131 return;
132 }
133
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700134 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700135 dirtyEntireOutput();
136}
137
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200138void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
139 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700140 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200141
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200142 outputState.displaySpace.orientation = orientation;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200143 LOG_FATAL_IF(outputState.displaySpace.bounds == Rect::INVALID_RECT,
144 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200145
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200146 // Compute orientedDisplaySpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200147 ui::Size orientedSize = outputState.displaySpace.bounds.getSize();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200148 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200149 std::swap(orientedSize.width, orientedSize.height);
150 }
151 outputState.orientedDisplaySpace.bounds = Rect(orientedSize);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200152 outputState.orientedDisplaySpace.content = orientedDisplaySpaceRect;
153
154 // Compute displaySpace.content
155 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
156 ui::Transform rotation;
157 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
158 const auto displaySize = outputState.displaySpace.bounds;
159 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
160 }
161 outputState.displaySpace.content = rotation.transform(orientedDisplaySpaceRect);
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200162
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200163 // Compute framebufferSpace
164 outputState.framebufferSpace.orientation = orientation;
165 LOG_FATAL_IF(outputState.framebufferSpace.bounds == Rect::INVALID_RECT,
166 "The framebuffer bounds are unknown.");
167 const auto scale =
Marin Shalamanov209ae612020-10-01 00:17:39 +0200168 getScale(outputState.displaySpace.bounds, outputState.framebufferSpace.bounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200169 outputState.framebufferSpace.content = outputState.displaySpace.content.scale(scale.x, scale.y);
170
171 // Compute layerStackSpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200172 outputState.layerStackSpace.content = layerStackSpaceRect;
173 outputState.layerStackSpace.bounds = layerStackSpaceRect;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200174
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200175 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
176 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700177 dirtyEntireOutput();
178}
179
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200180void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700181 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200182
183 auto& state = editState();
184
185 // Update framebuffer space
186 const Rect newBounds(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200187 state.framebufferSpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200188
189 // Update display space
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200190 state.displaySpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200191 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
192
193 // Update oriented display space
194 const auto orientation = state.displaySpace.orientation;
195 ui::Size orientedSize = size;
196 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
197 std::swap(orientedSize.width, orientedSize.height);
198 }
199 const Rect newOrientedBounds(orientedSize);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200200 state.orientedDisplaySpace.bounds = newOrientedBounds;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700201
Dan Stoza6166c312021-01-15 16:34:05 -0800202 if (mPlanner) {
203 mPlanner->setDisplaySize(size);
204 }
205
Lloyd Pique32cbe282018-10-19 13:09:22 -0700206 dirtyEntireOutput();
207}
208
Garfield Tan54edd912020-10-21 16:31:41 -0700209ui::Transform::RotationFlags Output::getTransformHint() const {
210 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
211}
212
Lloyd Piqueef36b002019-01-23 17:52:04 -0800213void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700214 auto& outputState = editState();
215 outputState.layerStackId = layerStackId;
216 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700217
218 dirtyEntireOutput();
219}
220
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800221void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700222 auto& colorTransformMatrix = editState().colorTransformMatrix;
223 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700224 return;
225 }
226
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700227 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800228
229 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700230}
231
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800232void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700233 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800234 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
235 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800236
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700237 auto& outputState = editState();
238 if (outputState.colorMode == colorProfile.mode &&
239 outputState.dataspace == colorProfile.dataspace &&
240 outputState.renderIntent == colorProfile.renderIntent &&
241 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800242 return;
243 }
244
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700245 outputState.colorMode = colorProfile.mode;
246 outputState.dataspace = colorProfile.dataspace;
247 outputState.renderIntent = colorProfile.renderIntent;
248 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700249
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800250 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700251
Lloyd Pique32cbe282018-10-19 13:09:22 -0700252 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800253 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
254 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800255
256 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700257}
258
259void Output::dump(std::string& out) const {
260 using android::base::StringAppendF;
261
262 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
263
264 out.append("\n ");
265
266 dumpBase(out);
267}
268
269void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700270 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700271
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700272 if (mDisplayColorProfile) {
273 mDisplayColorProfile->dump(out);
274 } else {
275 out.append(" No display color profile!\n");
276 }
277
Lloyd Pique31cb2942018-10-19 17:23:03 -0700278 if (mRenderSurface) {
279 mRenderSurface->dump(out);
280 } else {
281 out.append(" No render surface!\n");
282 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800283
Lloyd Pique01c77c12019-04-17 12:48:32 -0700284 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
285 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800286 if (!outputLayer) {
287 continue;
288 }
289 outputLayer->dump(out);
290 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700291}
292
Dan Stoza269dc4d2021-01-15 15:07:43 -0800293void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
294 if (!mPlanner) {
295 base::StringAppendF(&out, "Planner is disabled\n");
296 return;
297 }
298 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
299 mPlanner->dump(args, out);
300}
301
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700302compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
303 return mDisplayColorProfile.get();
304}
305
306void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
307 mDisplayColorProfile = std::move(mode);
308}
309
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800310const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
311 return mReleasedLayers;
312}
313
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700314void Output::setDisplayColorProfileForTest(
315 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
316 mDisplayColorProfile = std::move(mode);
317}
318
Lloyd Pique31cb2942018-10-19 17:23:03 -0700319compositionengine::RenderSurface* Output::getRenderSurface() const {
320 return mRenderSurface.get();
321}
322
323void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
324 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800325 const auto size = mRenderSurface->getSize();
326 editState().framebufferSpace.bounds = Rect(size);
327 if (mPlanner) {
328 mPlanner->setDisplaySize(size);
329 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700330 dirtyEntireOutput();
331}
332
Vishnu Nair9b079a22020-01-21 14:36:08 -0800333void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
334 if (cacheSize == 0) {
335 mClientCompositionRequestCache.reset();
336 } else {
337 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
338 }
339};
340
Lloyd Pique31cb2942018-10-19 17:23:03 -0700341void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
342 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700343}
344
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000345Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700346 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200347 Region dirty(outputState.layerStackSpace.content);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000348 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700349 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700350 }
351 return dirty;
352}
353
Lloyd Piquec6687342019-03-07 21:34:57 -0800354bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800355 // The layerStackId's must match, and also the layer must not be internal
356 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700357 const auto& outputState = getState();
358 return layerStackId && (*layerStackId == outputState.layerStackId) &&
359 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700360}
361
Lloyd Piquede196652020-01-22 17:29:58 -0800362bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const {
363 const auto* layerFEState = layerFE->getCompositionState();
364 return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800365}
366
Lloyd Piquedf336d92019-03-07 21:38:42 -0800367std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800368 const sp<LayerFE>& layerFE) const {
369 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800370}
371
Lloyd Piquede196652020-01-22 17:29:58 -0800372compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
373 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700374 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800375}
376
Lloyd Pique01c77c12019-04-17 12:48:32 -0700377std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800378 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700379 for (size_t i = 0; i < getOutputLayerCount(); i++) {
380 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800381 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700382 return i;
383 }
384 }
385 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800386}
387
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800388void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
389 mReleasedLayers = std::move(layers);
390}
391
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800392void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
393 LayerFESet& geomSnapshots) {
394 ATRACE_CALL();
395 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800396
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800397 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800398}
399
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800400void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800401 ATRACE_CALL();
402 ALOGV(__FUNCTION__);
403
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800404 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800405 updateCompositionState(refreshArgs);
406 planComposition();
407 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800408 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800409 beginFrame();
410 prepareFrame();
411 devOptRepaintFlash(refreshArgs);
412 finishFrame(refreshArgs);
413 postFramebuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800414 renderCachedSets();
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800415}
416
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800417void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
418 LayerFESet& layerFESet) {
419 ATRACE_CALL();
420 ALOGV(__FUNCTION__);
421
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700422 auto& outputState = editState();
423
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800424 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700425 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800426 return;
427 }
428
429 // Process the layers to determine visibility and coverage
430 compositionengine::Output::CoverageState coverage{layerFESet};
431 collectVisibleLayers(refreshArgs, coverage);
432
433 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700434 const ui::Transform& tr = outputState.transform;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200435 Region undefinedRegion{outputState.displaySpace.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800436 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
437
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700438 outputState.undefinedRegion = undefinedRegion;
439 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800440}
441
442void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
443 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800444 // Evaluate the layers from front to back to determine what is visible. This
445 // also incrementally calculates the coverage information for each layer as
446 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800447 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700448 // Incrementally process the coverage for each layer
449 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800450
451 // TODO(b/121291683): Stop early if the output is completely covered and
452 // no more layers could even be visible underneath the ones on top.
453 }
454
Lloyd Pique01c77c12019-04-17 12:48:32 -0700455 setReleasedLayers(refreshArgs);
456
457 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800458}
459
Lloyd Piquede196652020-01-22 17:29:58 -0800460void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700461 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800462 // Ensure we have a snapshot of the basic geometry layer state. Limit the
463 // snapshots to once per frame for each candidate layer, as layers may
464 // appear on multiple outputs.
465 if (!coverage.latchedLayers.count(layerFE)) {
466 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800467 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800468 }
469
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800470 // Only consider the layers on the given layer stack
Lloyd Piquede196652020-01-22 17:29:58 -0800471 if (!belongsInOutput(layerFE)) {
472 return;
473 }
474
475 // Obtain a read-only pointer to the front-end layer state
476 const auto* layerFEState = layerFE->getCompositionState();
477 if (CC_UNLIKELY(!layerFEState)) {
478 return;
479 }
480
481 // handle hidden surfaces by setting the visible region to empty
482 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700483 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800484 }
485
486 /*
487 * opaqueRegion: area of a surface that is fully opaque.
488 */
489 Region opaqueRegion;
490
491 /*
492 * visibleRegion: area of a surface that is visible on screen and not fully
493 * transparent. This is essentially the layer's footprint minus the opaque
494 * regions above it. Areas covered by a translucent surface are considered
495 * visible.
496 */
497 Region visibleRegion;
498
499 /*
500 * coveredRegion: area of a surface that is covered by all visible regions
501 * above it (which includes the translucent areas).
502 */
503 Region coveredRegion;
504
505 /*
506 * transparentRegion: area of a surface that is hinted to be completely
507 * transparent. This is only used to tell when the layer has no visible non-
508 * transparent regions and can be removed from the layer list. It does not
509 * affect the visibleRegion of this layer or any layers beneath it. The hint
510 * may not be correct if apps don't respect the SurfaceView restrictions
511 * (which, sadly, some don't).
512 */
513 Region transparentRegion;
514
Vishnu Naira483b4a2019-12-12 15:07:52 -0800515 /*
516 * shadowRegion: Region cast by the layer's shadow.
517 */
518 Region shadowRegion;
519
Lloyd Piquede196652020-01-22 17:29:58 -0800520 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800521
522 // Get the visible region
523 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
524 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800525 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800526 visibleRegion.set(visibleRect);
527
Lloyd Piquede196652020-01-22 17:29:58 -0800528 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800529 // if the layer casts a shadow, offset the layers visible region and
530 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800531 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800532 Rect visibleRectWithShadows(visibleRect);
533 visibleRectWithShadows.inset(inset, inset, inset, inset);
534 visibleRegion.set(visibleRectWithShadows);
535 shadowRegion = visibleRegion.subtract(visibleRect);
536 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800537
538 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700539 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800540 }
541
542 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800543 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800544 if (tr.preserveRects()) {
545 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800546 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800547 } else {
548 // transformation too complex, can't do the
549 // transparent region optimization.
550 transparentRegion.clear();
551 }
552 }
553
554 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800555 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800556 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800557 // If we one of the simple category of transforms (0/90/180/270 rotation
558 // + any flip), then the opaque region is the layer's footprint.
559 // Otherwise we don't try and compute the opaque region since there may
560 // be errors at the edges, and we treat the entire layer as
561 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800562 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800563 }
564
565 // Clip the covered region to the visible region
566 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
567
568 // Update accumAboveCoveredLayers for next (lower) layer
569 coverage.aboveCoveredLayers.orSelf(visibleRegion);
570
571 // subtract the opaque region covered by the layers above us
572 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
573
574 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700575 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800576 }
577
578 // Get coverage information for the layer as previously displayed,
579 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800580 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700581 auto prevOutputLayer =
582 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800583
584 // Get coverage information for the layer as previously displayed
585 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
586 const Region kEmptyRegion;
587 const Region& oldVisibleRegion =
588 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
589 const Region& oldCoveredRegion =
590 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
591
592 // compute this layer's dirty region
593 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800594 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800595 // we need to invalidate the whole region
596 dirty = visibleRegion;
597 // as well, as the old visible region
598 dirty.orSelf(oldVisibleRegion);
599 } else {
600 /* compute the exposed region:
601 * the exposed region consists of two components:
602 * 1) what's VISIBLE now and was COVERED before
603 * 2) what's EXPOSED now less what was EXPOSED before
604 *
605 * note that (1) is conservative, we start with the whole visible region
606 * but only keep what used to be covered by something -- which mean it
607 * may have been exposed.
608 *
609 * (2) handles areas that were not covered by anything but got exposed
610 * because of a resize.
611 *
612 */
613 const Region newExposed = visibleRegion - coveredRegion;
614 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
615 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
616 }
617 dirty.subtractSelf(coverage.aboveOpaqueLayers);
618
619 // accumulate to the screen dirty region
620 coverage.dirtyRegion.orSelf(dirty);
621
622 // Update accumAboveOpaqueLayers for next (lower) layer
623 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
624
625 // Compute the visible non-transparent region
626 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
627
Vishnu Naira483b4a2019-12-12 15:07:52 -0800628 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800629 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700630 const auto& outputState = getState();
631 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200632 drawRegion.andSelf(outputState.displaySpace.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800633 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700634 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800635 }
636
Vishnu Naira483b4a2019-12-12 15:07:52 -0800637 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
638
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800639 // The layer is visible. Either reuse the existing outputLayer if we have
640 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800641 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800642
643 // Store the layer coverage information into the layer state as some of it
644 // is useful later.
645 auto& outputLayerState = result->editState();
646 outputLayerState.visibleRegion = visibleRegion;
647 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
648 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200649 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
650 visibleNonShadowRegion.intersect(outputState.layerStackSpace.content));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800651 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800652}
653
654void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
655 // The base class does nothing with this call.
656}
657
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800658void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700659 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800660 layer->getLayerFE().prepareCompositionState(
661 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
662 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800663 }
664}
665
Dan Stoza269dc4d2021-01-15 15:07:43 -0800666void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800667 ATRACE_CALL();
668 ALOGV(__FUNCTION__);
669
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800670 if (!getState().isEnabled) {
671 return;
672 }
673
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800674 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
675 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
676
Lloyd Pique01c77c12019-04-17 12:48:32 -0700677 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700678 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800679 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200680 forceClientComposition,
681 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800682
683 if (mLayerRequestingBackgroundBlur == layer) {
684 forceClientComposition = false;
685 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800686 }
687}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800688
Dan Stoza269dc4d2021-01-15 15:07:43 -0800689void Output::planComposition() {
690 if (!mPlanner || !getState().isEnabled) {
691 return;
692 }
693
694 ATRACE_CALL();
695 ALOGV(__FUNCTION__);
696
697 mPlanner->plan(getOutputLayersOrderedByZ());
698}
699
700void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
701 ATRACE_CALL();
702 ALOGV(__FUNCTION__);
703
704 if (!getState().isEnabled) {
705 return;
706 }
707
Ady Abraham3645e642021-04-20 18:39:00 -0700708 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
709
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400710 OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800711 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400712 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400713 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400714 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800715 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400716 if (layer == peekThroughLayer) {
717 // No longer needed, although it should not show up again, so
718 // resetting it is not truly needed either.
719 peekThroughLayer = nullptr;
720
721 // peekThroughLayer was already drawn ahead of its z order.
722 continue;
723 }
Dan Stoza6166c312021-01-15 16:34:05 -0800724 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400725 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400726 if (overrideInfo.buffer != nullptr) {
727 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800728 ALOGV("Skipping redundant buffer");
729 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400730 } else {
731 // First layer with the override buffer.
732 if (overrideInfo.peekThroughLayer) {
733 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400734
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400735 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400736 overrideZ = true;
737 includeGeometry = true;
738 constexpr bool isPeekingThrough = true;
739 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
740 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400741 }
742
743 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800744 }
Dan Stoza6166c312021-01-15 16:34:05 -0800745 }
746
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400747 constexpr bool isPeekingThrough = false;
748 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800749 }
750}
751
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800752compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
753 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
754 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100755 auto* compState = layer->getLayerFE().getCompositionState();
756
757 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
758 // want to force client composition because of the blur.
759 if (compState->sidebandStream != nullptr) {
760 return nullptr;
761 }
762 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800763 layerRequestingBgComposition = layer;
764 }
765 }
766 return layerRequestingBgComposition;
767}
768
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800769void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
770 setColorProfile(pickColorProfile(refreshArgs));
771}
772
773// Returns a data space that fits all visible layers. The returned data space
774// can only be one of
775// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
776// - Dataspace::DISPLAY_P3
777// - Dataspace::DISPLAY_BT2020
778// The returned HDR data space is one of
779// - Dataspace::UNKNOWN
780// - Dataspace::BT2020_HLG
781// - Dataspace::BT2020_PQ
782ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
783 bool* outIsHdrClientComposition) const {
784 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
785 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
786
Lloyd Pique01c77c12019-04-17 12:48:32 -0700787 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800788 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800789 case ui::Dataspace::V0_SCRGB:
790 case ui::Dataspace::V0_SCRGB_LINEAR:
791 case ui::Dataspace::BT2020:
792 case ui::Dataspace::BT2020_ITU:
793 case ui::Dataspace::BT2020_LINEAR:
794 case ui::Dataspace::DISPLAY_BT2020:
795 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
796 break;
797 case ui::Dataspace::DISPLAY_P3:
798 bestDataSpace = ui::Dataspace::DISPLAY_P3;
799 break;
800 case ui::Dataspace::BT2020_PQ:
801 case ui::Dataspace::BT2020_ITU_PQ:
802 bestDataSpace = ui::Dataspace::DISPLAY_P3;
803 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800804 *outIsHdrClientComposition =
805 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800806 break;
807 case ui::Dataspace::BT2020_HLG:
808 case ui::Dataspace::BT2020_ITU_HLG:
809 bestDataSpace = ui::Dataspace::DISPLAY_P3;
810 // When there's mixed PQ content and HLG content, we set the HDR
811 // data space to be BT2020_PQ and convert HLG to PQ.
812 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
813 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
814 }
815 break;
816 default:
817 break;
818 }
819 }
820
821 return bestDataSpace;
822}
823
824compositionengine::Output::ColorProfile Output::pickColorProfile(
825 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
826 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
827 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
828 ui::RenderIntent::COLORIMETRIC,
829 refreshArgs.colorSpaceAgnosticDataspace};
830 }
831
832 ui::Dataspace hdrDataSpace;
833 bool isHdrClientComposition = false;
834 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
835
836 switch (refreshArgs.forceOutputColorMode) {
837 case ui::ColorMode::SRGB:
838 bestDataSpace = ui::Dataspace::V0_SRGB;
839 break;
840 case ui::ColorMode::DISPLAY_P3:
841 bestDataSpace = ui::Dataspace::DISPLAY_P3;
842 break;
843 default:
844 break;
845 }
846
847 // respect hdrDataSpace only when there is no legacy HDR support
848 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
849 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
850 if (isHdr) {
851 bestDataSpace = hdrDataSpace;
852 }
853
854 ui::RenderIntent intent;
855 switch (refreshArgs.outputColorSetting) {
856 case OutputColorSetting::kManaged:
857 case OutputColorSetting::kUnmanaged:
858 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
859 : ui::RenderIntent::COLORIMETRIC;
860 break;
861 case OutputColorSetting::kEnhanced:
862 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
863 break;
864 default: // vendor display color setting
865 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
866 break;
867 }
868
869 ui::ColorMode outMode;
870 ui::Dataspace outDataSpace;
871 ui::RenderIntent outRenderIntent;
872 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
873 &outRenderIntent);
874
875 return ColorProfile{outMode, outDataSpace, outRenderIntent,
876 refreshArgs.colorSpaceAgnosticDataspace};
877}
878
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800879void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700880 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800881 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700882 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700883 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800884
885 // If nothing has changed (!dirty), don't recompose.
886 // If something changed, but we don't currently have any visible layers,
887 // and didn't when we last did a composition, then skip it this time.
888 // The second rule does two things:
889 // - When all layers are removed from a display, we'll emit one black
890 // frame, then nothing more until we get new layers.
891 // - When a display is created with a private layer stack, we won't
892 // emit any black frames until a layer is added to the layer stack.
893 const bool mustRecompose = dirty && !(empty && wasEmpty);
894
895 const char flagPrefix[] = {'-', '+'};
896 static_cast<void>(flagPrefix);
897 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
898 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
899 flagPrefix[empty], flagPrefix[wasEmpty]);
900
901 mRenderSurface->beginFrame(mustRecompose);
902
903 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700904 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800905 }
906}
907
Lloyd Pique66d68602019-02-13 14:23:31 -0800908void Output::prepareFrame() {
909 ATRACE_CALL();
910 ALOGV(__FUNCTION__);
911
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700912 const auto& outputState = getState();
913 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800914 return;
915 }
916
917 chooseCompositionStrategy();
918
Dan Stoza47437bb2021-01-15 16:21:07 -0800919 if (mPlanner) {
920 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
921 }
922
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700923 mRenderSurface->prepareFrame(outputState.usesClientComposition,
924 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800925}
926
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800927void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
928 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
929 return;
930 }
931
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700932 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800933 // transform the dirty region into this screen's coordinate space
934 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
935 if (!dirtyRegion.isEmpty()) {
936 base::unique_fd readyFence;
937 // redraw the whole screen
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800938 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800939
940 mRenderSurface->queueBuffer(std::move(readyFence));
941 }
942 }
943
944 postFramebuffer();
945
946 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
947
948 prepareFrame();
949}
950
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800951void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800952 ATRACE_CALL();
953 ALOGV(__FUNCTION__);
954
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700955 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800956 return;
957 }
958
959 // Repaint the framebuffer (if needed), getting the optional fence for when
960 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800961 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800962 if (!optReadyFence) {
963 return;
964 }
965
966 // swap buffers (presentation)
967 mRenderSurface->queueBuffer(std::move(*optReadyFence));
968}
969
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800970std::optional<base::unique_fd> Output::composeSurfaces(
971 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800972 ATRACE_CALL();
973 ALOGV(__FUNCTION__);
974
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700975 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800976 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800977 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700978 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -0700979
980 auto& renderEngine = getCompositionEngine().getRenderEngine();
981 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
982
983 // If we the display is secure, protected content support is enabled, and at
984 // least one layer has protected content, we need to use a secure back
985 // buffer.
986 if (outputState.isSecure && supportsProtectedContent) {
987 auto layers = getOutputLayersOrderedByZ();
988 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
989 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
990 });
991 if (needsProtected != renderEngine.isProtected()) {
992 renderEngine.useProtectedContext(needsProtected);
993 }
994 if (needsProtected != mRenderSurface->isProtected() &&
995 needsProtected == renderEngine.isProtected()) {
996 mRenderSurface->setProtected(needsProtected);
997 }
Peiyong Lin09f910f2020-09-25 10:54:13 -0700998 } else if (!outputState.isSecure && renderEngine.isProtected()) {
999 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001000 }
1001
1002 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +00001003
1004 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001005
1006 // If we aren't doing client composition on this output, but do have a
1007 // flipClientTarget request for this frame on this output, we still need to
1008 // dequeue a buffer.
1009 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +00001010 tex = mRenderSurface->dequeueBuffer(&fd);
1011 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001012 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1013 "client composition for this frame",
1014 mName.c_str());
1015 return {};
1016 }
1017 }
1018
Lloyd Piqued3d69882019-02-28 16:03:46 -08001019 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001020 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001021 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -08001022 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001023 }
1024
1025 ALOGV("hasClientComposition");
1026
Lloyd Pique688abd42019-02-15 15:42:24 -08001027 renderengine::DisplaySettings clientCompositionDisplay;
Marin Shalamanovb15d2272020-09-17 21:41:52 +02001028 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.content;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001029 clientCompositionDisplay.clip = outputState.layerStackSpace.content;
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001030 clientCompositionDisplay.orientation =
1031 ui::Transform::toRotationFlags(outputState.displaySpace.orientation);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001032 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1033 ? outputState.dataspace
1034 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -08001035 clientCompositionDisplay.maxLuminance =
1036 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1037
1038 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001039 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1040 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001041 }
1042
1043 // Note: Updated by generateClientCompositionRequests
1044 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
1045
1046 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -08001047 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001048 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001049 clientCompositionDisplay.clearRegion,
1050 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -08001051 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1052
Vishnu Nair9b079a22020-01-21 14:36:08 -08001053 // Check if the client composition requests were rendered into the provided graphic buffer. If
1054 // so, we can reuse the buffer and avoid client composition.
1055 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001056 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1057 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001058 clientCompositionLayers)) {
1059 outputCompositionState.reusedClientComposition = true;
1060 setExpensiveRenderingExpected(false);
1061 return readyFence;
1062 }
Alec Mouria90a5702021-04-16 16:36:21 +00001063 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001064 clientCompositionLayers);
1065 }
1066
Lloyd Pique688abd42019-02-15 15:42:24 -08001067 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001068 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1069 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1070 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001071 const bool expensiveBlurs =
1072 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001073 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001074 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001075 if (expensiveRenderingExpected) {
1076 setExpensiveRenderingExpected(true);
1077 }
1078
Vishnu Nair9b079a22020-01-21 14:36:08 -08001079 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
1080 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
1081 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1082 std::back_inserter(clientCompositionLayerPointers),
1083 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
1084 return &settings;
1085 });
1086
Alec Mourie4034bb2019-11-19 12:45:54 -08001087 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001088 // Only use the framebuffer cache when rendering to an internal display
1089 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1090 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1091 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1092 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1093 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
1094 const bool useFramebufferCache = outputState.layerStackInternal;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001095 status_t status =
Alec Mouria90a5702021-04-16 16:36:21 +00001096 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, tex,
Alec Mouri1684c702021-02-04 12:27:26 -08001097 useFramebufferCache, std::move(fd), &readyFence);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001098
1099 if (status != NO_ERROR && mClientCompositionRequestCache) {
1100 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001101 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001102 }
1103
Alec Mourie4034bb2019-11-19 12:45:54 -08001104 auto& timeStats = getCompositionEngine().getTimeStats();
1105 if (readyFence.get() < 0) {
1106 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1107 } else {
1108 timeStats.recordRenderEngineDuration(renderEngineStart,
1109 std::make_shared<FenceTime>(
1110 new Fence(dup(readyFence.get()))));
1111 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001112
Lloyd Piqued3d69882019-02-28 16:03:46 -08001113 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001114}
1115
Vishnu Nair9b079a22020-01-21 14:36:08 -08001116std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001117 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001118 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001119 ALOGV("Rendering client layers");
1120
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001121 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001122 const Region viewportRegion(outputState.layerStackSpace.content);
Lloyd Pique688abd42019-02-15 15:42:24 -08001123 bool firstLayer = true;
1124 // Used when a layer clears part of the buffer.
Peiyong Lind8460c82020-07-28 16:04:22 -07001125 Region stubRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -08001126
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001127 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001128 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001129
Lloyd Pique01c77c12019-04-17 12:48:32 -07001130 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001131 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001132 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001133 auto& layerFE = layer->getLayerFE();
1134
Lloyd Piquea2468662019-03-07 21:31:06 -08001135 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001136 ALOGV("Layer: %s", layerFE.getDebugName());
1137 if (clip.isEmpty()) {
1138 ALOGV(" Skipping for empty clip");
1139 firstLayer = false;
1140 continue;
1141 }
1142
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001143 disableBlurs |= layerFEState->sidebandStream != nullptr;
1144
Vishnu Naira483b4a2019-12-12 15:07:52 -08001145 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001146
1147 // We clear the client target for non-client composed layers if
1148 // requested by the HWC. We skip this if the layer is not an opaque
1149 // rectangle, as by definition the layer must blend with whatever is
1150 // underneath. We also skip the first layer as the buffer target is
1151 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001152 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001153 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001154
1155 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1156
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001157 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1158 // composing the non-shadow content and only draw the shadows.
1159 const bool realContentIsVisible = clientComposition &&
1160 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1161
Lloyd Pique688abd42019-02-15 15:42:24 -08001162 if (clientComposition || clearClientComposition) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001163 compositionengine::LayerFE::ClientCompositionTargetSettings
1164 targetSettings{.clip = clip,
1165 .needsFiltering =
1166 layer->needsFiltering() || outputState.needsFiltering,
1167 .isSecure = outputState.isSecure,
1168 .supportsProtectedContent = supportsProtectedContent,
1169 .clearRegion = clientComposition ? clearRegion : stubRegion,
1170 .viewport = outputState.layerStackSpace.content,
1171 .dataspace = outputDataspace,
1172 .realContentIsVisible = realContentIsVisible,
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001173 .clearContent = !clientComposition,
1174 .disableBlurs = disableBlurs};
Dan Stoza6166c312021-01-15 16:34:05 -08001175
1176 std::vector<LayerFE::LayerSettings> results;
1177 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001178 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001179 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001180 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001181 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1182 } else {
1183 ALOGV("Skipping redundant override buffer for [%s] in RE",
1184 layer->getLayerFE().getDebugName());
1185 }
Dan Stoza6166c312021-01-15 16:34:05 -08001186 } else {
1187 results = layerFE.prepareClientCompositionList(targetSettings);
1188 if (realContentIsVisible && !results.empty()) {
1189 layer->editState().clientCompositionTimestamp = systemTime();
1190 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001191 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001192
1193 clientCompositionLayers.insert(clientCompositionLayers.end(),
1194 std::make_move_iterator(results.begin()),
1195 std::make_move_iterator(results.end()));
1196 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001197 }
1198
1199 firstLayer = false;
1200 }
1201
1202 return clientCompositionLayers;
1203}
1204
1205void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001206 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001207 if (flashRegion.isEmpty()) {
1208 return;
1209 }
1210
Vishnu Nair9b079a22020-01-21 14:36:08 -08001211 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001212 layerSettings.source.buffer.buffer = nullptr;
1213 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1214 layerSettings.alpha = half(1.0);
1215
1216 for (const auto& rect : flashRegion) {
1217 layerSettings.geometry.boundaries = rect.toFloatRect();
1218 clientCompositionLayers.push_back(layerSettings);
1219 }
1220}
1221
1222void Output::setExpensiveRenderingExpected(bool) {
1223 // The base class does nothing with this call.
1224}
1225
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001226void Output::postFramebuffer() {
1227 ATRACE_CALL();
1228 ALOGV(__FUNCTION__);
1229
1230 if (!getState().isEnabled) {
1231 return;
1232 }
1233
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001234 auto& outputState = editState();
1235 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001236 mRenderSurface->flip();
1237
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001238 auto frame = presentAndGetFrameFences();
1239
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001240 mRenderSurface->onPresentDisplayCompleted();
1241
Lloyd Pique01c77c12019-04-17 12:48:32 -07001242 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001243 // The layer buffer from the previous frame (if any) is released
1244 // by HWC only when the release fence from this frame (if any) is
1245 // signaled. Always get the release fence from HWC first.
1246 sp<Fence> releaseFence = Fence::NO_FENCE;
1247
1248 if (auto hwcLayer = layer->getHwcLayer()) {
1249 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1250 releaseFence = f->second;
1251 }
1252 }
1253
1254 // If the layer was client composited in the previous frame, we
1255 // need to merge with the previous client target acquire fence.
1256 // Since we do not track that, always merge with the current
1257 // client target acquire fence when it is available, even though
1258 // this is suboptimal.
1259 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001260 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001261 releaseFence =
1262 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1263 }
1264
1265 layer->getLayerFE().onLayerDisplayed(releaseFence);
1266 }
1267
1268 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001269 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001270 // supply them with the present fence.
1271 for (auto& weakLayer : mReleasedLayers) {
1272 if (auto layer = weakLayer.promote(); layer != nullptr) {
1273 layer->onLayerDisplayed(frame.presentFence);
1274 }
1275 }
1276
1277 // Clear out the released layers now that we're done with them.
1278 mReleasedLayers.clear();
1279}
1280
Dan Stoza6166c312021-01-15 16:34:05 -08001281void Output::renderCachedSets() {
1282 if (mPlanner) {
Huihong Luoa5825112021-03-24 12:28:29 -07001283 mPlanner->renderCachedSets(getCompositionEngine().getRenderEngine(), getState());
Dan Stoza6166c312021-01-15 16:34:05 -08001284 }
1285}
1286
Lloyd Pique32cbe282018-10-19 13:09:22 -07001287void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001288 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001289 outputState.dirtyRegion.set(outputState.displaySpace.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001290}
1291
Lloyd Pique66d68602019-02-13 14:23:31 -08001292void Output::chooseCompositionStrategy() {
1293 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001294 auto& outputState = editState();
1295 outputState.usesClientComposition = true;
1296 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001297 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001298}
1299
Lloyd Pique688abd42019-02-15 15:42:24 -08001300bool Output::getSkipColorTransform() const {
1301 return true;
1302}
1303
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001304compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1305 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001306 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001307 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1308 }
1309 return result;
1310}
1311
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001312} // namespace impl
1313} // namespace android::compositionengine