blob: 3ac5433457c5a20d9ffbb7f8f57692acf73452a5 [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 Mouri617752f2021-04-15 16:27:01 +000017#include <thread>
18
Lloyd Pique32cbe282018-10-19 13:09:22 -070019#include <android-base/stringprintf.h>
20#include <compositionengine/CompositionEngine.h>
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080021#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070022#include <compositionengine/DisplayColorProfile.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080023#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070024#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070025#include <compositionengine/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070026#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070027#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080028#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070029#include <compositionengine/impl/OutputLayerCompositionState.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080030#include <compositionengine/impl/planner/Planner.h>
31
Alec Mouri617752f2021-04-15 16:27:01 +000032#include <SurfaceFlingerProperties.sysprop.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080033
34// TODO(b/129481165): remove the #pragma below and fix conversion issues
35#pragma clang diagnostic push
36#pragma clang diagnostic ignored "-Wconversion"
37
Lloyd Pique688abd42019-02-15 15:42:24 -080038#include <renderengine/DisplaySettings.h>
39#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080040
41// TODO(b/129481165): remove the #pragma below and fix conversion issues
42#pragma clang diagnostic pop // ignored "-Wconversion"
43
Dan Stoza269dc4d2021-01-15 15:07:43 -080044#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070045#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080046#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080047#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070048
Lloyd Pique688abd42019-02-15 15:42:24 -080049#include "TracedOrdinal.h"
50
Lloyd Piquefeb73d72018-12-04 17:23:44 -080051namespace android::compositionengine {
52
53Output::~Output() = default;
54
55namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070056
Dan Stoza269dc4d2021-01-15 15:07:43 -080057Output::Output() {
58 const bool enableLayerCaching = [] {
59 const bool enable =
60 android::sysprop::SurfaceFlingerProperties::enable_layer_caching().value_or(false);
61 return base::GetBoolProperty(std::string("debug.sf.enable_layer_caching"), enable);
62 }();
63
64 if (enableLayerCaching) {
65 mPlanner = std::make_unique<planner::Planner>();
66 }
67}
68
Lloyd Piquec29e4c62019-03-07 21:48:19 -080069namespace {
70
71template <typename T>
72class Reversed {
73public:
74 explicit Reversed(const T& container) : mContainer(container) {}
75 auto begin() { return mContainer.rbegin(); }
76 auto end() { return mContainer.rend(); }
77
78private:
79 const T& mContainer;
80};
81
82// Helper for enumerating over a container in reverse order
83template <typename T>
84Reversed<T> reversed(const T& c) {
85 return Reversed<T>(c);
86}
87
Marin Shalamanovb15d2272020-09-17 21:41:52 +020088struct ScaleVector {
89 float x;
90 float y;
91};
92
93// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
94// where to' will have the same size as "to". In the case where "from" and "to"
95// start at the origin to'=to.
96ScaleVector getScale(const Rect& from, const Rect& to) {
97 return {.x = static_cast<float>(to.width()) / from.width(),
98 .y = static_cast<float>(to.height()) / from.height()};
99}
100
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800101} // namespace
102
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700103std::shared_ptr<Output> createOutput(
104 const compositionengine::CompositionEngine& compositionEngine) {
105 return createOutputTemplated<Output>(compositionEngine);
106}
Lloyd Pique32cbe282018-10-19 13:09:22 -0700107
108Output::~Output() = default;
109
Lloyd Pique32cbe282018-10-19 13:09:22 -0700110bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700111 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
112 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700113}
114
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700115std::optional<DisplayId> Output::getDisplayId() const {
116 return {};
117}
118
Lloyd Pique32cbe282018-10-19 13:09:22 -0700119const std::string& Output::getName() const {
120 return mName;
121}
122
123void Output::setName(const std::string& name) {
124 mName = name;
125}
126
127void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700128 auto& outputState = editState();
129 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700130 return;
131 }
132
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700133 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700134 dirtyEntireOutput();
135}
136
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200137void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
138 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700139 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200140
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200141 outputState.displaySpace.orientation = orientation;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200142 LOG_FATAL_IF(outputState.displaySpace.bounds == Rect::INVALID_RECT,
143 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200144
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200145 // Compute orientedDisplaySpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200146 ui::Size orientedSize = outputState.displaySpace.bounds.getSize();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200147 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200148 std::swap(orientedSize.width, orientedSize.height);
149 }
150 outputState.orientedDisplaySpace.bounds = Rect(orientedSize);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200151 outputState.orientedDisplaySpace.content = orientedDisplaySpaceRect;
152
153 // Compute displaySpace.content
154 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
155 ui::Transform rotation;
156 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
157 const auto displaySize = outputState.displaySpace.bounds;
158 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
159 }
160 outputState.displaySpace.content = rotation.transform(orientedDisplaySpaceRect);
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200161
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200162 // Compute framebufferSpace
163 outputState.framebufferSpace.orientation = orientation;
164 LOG_FATAL_IF(outputState.framebufferSpace.bounds == Rect::INVALID_RECT,
165 "The framebuffer bounds are unknown.");
166 const auto scale =
Marin Shalamanov209ae612020-10-01 00:17:39 +0200167 getScale(outputState.displaySpace.bounds, outputState.framebufferSpace.bounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200168 outputState.framebufferSpace.content = outputState.displaySpace.content.scale(scale.x, scale.y);
169
170 // Compute layerStackSpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200171 outputState.layerStackSpace.content = layerStackSpaceRect;
172 outputState.layerStackSpace.bounds = layerStackSpaceRect;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200173
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200174 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
175 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700176 dirtyEntireOutput();
177}
178
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200179void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700180 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200181
182 auto& state = editState();
183
184 // Update framebuffer space
185 const Rect newBounds(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200186 state.framebufferSpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200187
188 // Update display space
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200189 state.displaySpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200190 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
191
192 // Update oriented display space
193 const auto orientation = state.displaySpace.orientation;
194 ui::Size orientedSize = size;
195 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
196 std::swap(orientedSize.width, orientedSize.height);
197 }
198 const Rect newOrientedBounds(orientedSize);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200199 state.orientedDisplaySpace.bounds = newOrientedBounds;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700200
Dan Stoza6166c312021-01-15 16:34:05 -0800201 if (mPlanner) {
202 mPlanner->setDisplaySize(size);
203 }
204
Lloyd Pique32cbe282018-10-19 13:09:22 -0700205 dirtyEntireOutput();
206}
207
Garfield Tan54edd912020-10-21 16:31:41 -0700208ui::Transform::RotationFlags Output::getTransformHint() const {
209 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
210}
211
Lloyd Piqueef36b002019-01-23 17:52:04 -0800212void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700213 auto& outputState = editState();
214 outputState.layerStackId = layerStackId;
215 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700216
217 dirtyEntireOutput();
218}
219
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800220void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700221 auto& colorTransformMatrix = editState().colorTransformMatrix;
222 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700223 return;
224 }
225
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700226 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800227
228 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700229}
230
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800231void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700232 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800233 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
234 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800235
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700236 auto& outputState = editState();
237 if (outputState.colorMode == colorProfile.mode &&
238 outputState.dataspace == colorProfile.dataspace &&
239 outputState.renderIntent == colorProfile.renderIntent &&
240 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800241 return;
242 }
243
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700244 outputState.colorMode = colorProfile.mode;
245 outputState.dataspace = colorProfile.dataspace;
246 outputState.renderIntent = colorProfile.renderIntent;
247 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700248
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800249 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700250
Lloyd Pique32cbe282018-10-19 13:09:22 -0700251 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800252 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
253 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800254
255 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700256}
257
258void Output::dump(std::string& out) const {
259 using android::base::StringAppendF;
260
261 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
262
263 out.append("\n ");
264
265 dumpBase(out);
266}
267
268void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700269 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700270
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700271 if (mDisplayColorProfile) {
272 mDisplayColorProfile->dump(out);
273 } else {
274 out.append(" No display color profile!\n");
275 }
276
Lloyd Pique31cb2942018-10-19 17:23:03 -0700277 if (mRenderSurface) {
278 mRenderSurface->dump(out);
279 } else {
280 out.append(" No render surface!\n");
281 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800282
Lloyd Pique01c77c12019-04-17 12:48:32 -0700283 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
284 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800285 if (!outputLayer) {
286 continue;
287 }
288 outputLayer->dump(out);
289 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700290}
291
Dan Stoza269dc4d2021-01-15 15:07:43 -0800292void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
293 if (!mPlanner) {
294 base::StringAppendF(&out, "Planner is disabled\n");
295 return;
296 }
297 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
298 mPlanner->dump(args, out);
299}
300
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700301compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
302 return mDisplayColorProfile.get();
303}
304
305void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
306 mDisplayColorProfile = std::move(mode);
307}
308
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800309const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
310 return mReleasedLayers;
311}
312
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700313void Output::setDisplayColorProfileForTest(
314 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
315 mDisplayColorProfile = std::move(mode);
316}
317
Lloyd Pique31cb2942018-10-19 17:23:03 -0700318compositionengine::RenderSurface* Output::getRenderSurface() const {
319 return mRenderSurface.get();
320}
321
322void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
323 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800324 const auto size = mRenderSurface->getSize();
325 editState().framebufferSpace.bounds = Rect(size);
326 if (mPlanner) {
327 mPlanner->setDisplaySize(size);
328 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700329 dirtyEntireOutput();
330}
331
Vishnu Nair9b079a22020-01-21 14:36:08 -0800332void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
333 if (cacheSize == 0) {
334 mClientCompositionRequestCache.reset();
335 } else {
336 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
337 }
338};
339
Lloyd Pique31cb2942018-10-19 17:23:03 -0700340void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
341 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700342}
343
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000344Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700345 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200346 Region dirty(outputState.layerStackSpace.content);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000347 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700348 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700349 }
350 return dirty;
351}
352
Lloyd Piquec6687342019-03-07 21:34:57 -0800353bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800354 // The layerStackId's must match, and also the layer must not be internal
355 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700356 const auto& outputState = getState();
357 return layerStackId && (*layerStackId == outputState.layerStackId) &&
358 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700359}
360
Lloyd Piquede196652020-01-22 17:29:58 -0800361bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const {
362 const auto* layerFEState = layerFE->getCompositionState();
363 return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800364}
365
Lloyd Piquedf336d92019-03-07 21:38:42 -0800366std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800367 const sp<LayerFE>& layerFE) const {
368 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800369}
370
Lloyd Piquede196652020-01-22 17:29:58 -0800371compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
372 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700373 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800374}
375
Lloyd Pique01c77c12019-04-17 12:48:32 -0700376std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800377 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700378 for (size_t i = 0; i < getOutputLayerCount(); i++) {
379 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800380 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700381 return i;
382 }
383 }
384 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800385}
386
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800387void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
388 mReleasedLayers = std::move(layers);
389}
390
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800391void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
392 LayerFESet& geomSnapshots) {
393 ATRACE_CALL();
394 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800395
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800396 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800397}
398
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800399void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800400 ATRACE_CALL();
401 ALOGV(__FUNCTION__);
402
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800403 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800404 updateCompositionState(refreshArgs);
405 planComposition();
406 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800407 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800408 beginFrame();
409 prepareFrame();
410 devOptRepaintFlash(refreshArgs);
411 finishFrame(refreshArgs);
412 postFramebuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800413 renderCachedSets();
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800414}
415
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800416void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
417 LayerFESet& layerFESet) {
418 ATRACE_CALL();
419 ALOGV(__FUNCTION__);
420
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700421 auto& outputState = editState();
422
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800423 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700424 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800425 return;
426 }
427
428 // Process the layers to determine visibility and coverage
429 compositionengine::Output::CoverageState coverage{layerFESet};
430 collectVisibleLayers(refreshArgs, coverage);
431
432 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700433 const ui::Transform& tr = outputState.transform;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200434 Region undefinedRegion{outputState.displaySpace.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800435 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
436
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700437 outputState.undefinedRegion = undefinedRegion;
438 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800439}
440
441void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
442 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800443 // Evaluate the layers from front to back to determine what is visible. This
444 // also incrementally calculates the coverage information for each layer as
445 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800446 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700447 // Incrementally process the coverage for each layer
448 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800449
450 // TODO(b/121291683): Stop early if the output is completely covered and
451 // no more layers could even be visible underneath the ones on top.
452 }
453
Lloyd Pique01c77c12019-04-17 12:48:32 -0700454 setReleasedLayers(refreshArgs);
455
456 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800457
458 // Generate a simple Z-order values to each visible output layer
459 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700460 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800461 outputLayer->editState().z = zOrder++;
462 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800463}
464
Lloyd Piquede196652020-01-22 17:29:58 -0800465void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700466 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800467 // Ensure we have a snapshot of the basic geometry layer state. Limit the
468 // snapshots to once per frame for each candidate layer, as layers may
469 // appear on multiple outputs.
470 if (!coverage.latchedLayers.count(layerFE)) {
471 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800472 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800473 }
474
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800475 // Only consider the layers on the given layer stack
Lloyd Piquede196652020-01-22 17:29:58 -0800476 if (!belongsInOutput(layerFE)) {
477 return;
478 }
479
480 // Obtain a read-only pointer to the front-end layer state
481 const auto* layerFEState = layerFE->getCompositionState();
482 if (CC_UNLIKELY(!layerFEState)) {
483 return;
484 }
485
486 // handle hidden surfaces by setting the visible region to empty
487 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700488 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800489 }
490
491 /*
492 * opaqueRegion: area of a surface that is fully opaque.
493 */
494 Region opaqueRegion;
495
496 /*
497 * visibleRegion: area of a surface that is visible on screen and not fully
498 * transparent. This is essentially the layer's footprint minus the opaque
499 * regions above it. Areas covered by a translucent surface are considered
500 * visible.
501 */
502 Region visibleRegion;
503
504 /*
505 * coveredRegion: area of a surface that is covered by all visible regions
506 * above it (which includes the translucent areas).
507 */
508 Region coveredRegion;
509
510 /*
511 * transparentRegion: area of a surface that is hinted to be completely
512 * transparent. This is only used to tell when the layer has no visible non-
513 * transparent regions and can be removed from the layer list. It does not
514 * affect the visibleRegion of this layer or any layers beneath it. The hint
515 * may not be correct if apps don't respect the SurfaceView restrictions
516 * (which, sadly, some don't).
517 */
518 Region transparentRegion;
519
Vishnu Naira483b4a2019-12-12 15:07:52 -0800520 /*
521 * shadowRegion: Region cast by the layer's shadow.
522 */
523 Region shadowRegion;
524
Lloyd Piquede196652020-01-22 17:29:58 -0800525 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800526
527 // Get the visible region
528 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
529 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800530 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800531 visibleRegion.set(visibleRect);
532
Lloyd Piquede196652020-01-22 17:29:58 -0800533 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800534 // if the layer casts a shadow, offset the layers visible region and
535 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800536 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800537 Rect visibleRectWithShadows(visibleRect);
538 visibleRectWithShadows.inset(inset, inset, inset, inset);
539 visibleRegion.set(visibleRectWithShadows);
540 shadowRegion = visibleRegion.subtract(visibleRect);
541 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800542
543 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700544 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800545 }
546
547 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800548 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800549 if (tr.preserveRects()) {
550 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800551 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800552 } else {
553 // transformation too complex, can't do the
554 // transparent region optimization.
555 transparentRegion.clear();
556 }
557 }
558
559 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800560 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800561 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800562 // If we one of the simple category of transforms (0/90/180/270 rotation
563 // + any flip), then the opaque region is the layer's footprint.
564 // Otherwise we don't try and compute the opaque region since there may
565 // be errors at the edges, and we treat the entire layer as
566 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800567 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800568 }
569
570 // Clip the covered region to the visible region
571 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
572
573 // Update accumAboveCoveredLayers for next (lower) layer
574 coverage.aboveCoveredLayers.orSelf(visibleRegion);
575
576 // subtract the opaque region covered by the layers above us
577 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
578
579 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700580 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800581 }
582
583 // Get coverage information for the layer as previously displayed,
584 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800585 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700586 auto prevOutputLayer =
587 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800588
589 // Get coverage information for the layer as previously displayed
590 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
591 const Region kEmptyRegion;
592 const Region& oldVisibleRegion =
593 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
594 const Region& oldCoveredRegion =
595 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
596
597 // compute this layer's dirty region
598 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800599 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800600 // we need to invalidate the whole region
601 dirty = visibleRegion;
602 // as well, as the old visible region
603 dirty.orSelf(oldVisibleRegion);
604 } else {
605 /* compute the exposed region:
606 * the exposed region consists of two components:
607 * 1) what's VISIBLE now and was COVERED before
608 * 2) what's EXPOSED now less what was EXPOSED before
609 *
610 * note that (1) is conservative, we start with the whole visible region
611 * but only keep what used to be covered by something -- which mean it
612 * may have been exposed.
613 *
614 * (2) handles areas that were not covered by anything but got exposed
615 * because of a resize.
616 *
617 */
618 const Region newExposed = visibleRegion - coveredRegion;
619 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
620 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
621 }
622 dirty.subtractSelf(coverage.aboveOpaqueLayers);
623
624 // accumulate to the screen dirty region
625 coverage.dirtyRegion.orSelf(dirty);
626
627 // Update accumAboveOpaqueLayers for next (lower) layer
628 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
629
630 // Compute the visible non-transparent region
631 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
632
Vishnu Naira483b4a2019-12-12 15:07:52 -0800633 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800634 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700635 const auto& outputState = getState();
636 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200637 drawRegion.andSelf(outputState.displaySpace.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800638 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700639 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800640 }
641
Vishnu Naira483b4a2019-12-12 15:07:52 -0800642 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
643
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800644 // The layer is visible. Either reuse the existing outputLayer if we have
645 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800646 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800647
648 // Store the layer coverage information into the layer state as some of it
649 // is useful later.
650 auto& outputLayerState = result->editState();
651 outputLayerState.visibleRegion = visibleRegion;
652 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
653 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200654 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
655 visibleNonShadowRegion.intersect(outputState.layerStackSpace.content));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800656 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800657}
658
659void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
660 // The base class does nothing with this call.
661}
662
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800663void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700664 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800665 layer->getLayerFE().prepareCompositionState(
666 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
667 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800668 }
669}
670
Dan Stoza269dc4d2021-01-15 15:07:43 -0800671void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800672 ATRACE_CALL();
673 ALOGV(__FUNCTION__);
674
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800675 if (!getState().isEnabled) {
676 return;
677 }
678
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800679 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
680 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
681
Lloyd Pique01c77c12019-04-17 12:48:32 -0700682 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700683 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800684 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200685 forceClientComposition,
686 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800687
688 if (mLayerRequestingBackgroundBlur == layer) {
689 forceClientComposition = false;
690 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800691 }
692}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800693
Dan Stoza269dc4d2021-01-15 15:07:43 -0800694void Output::planComposition() {
695 if (!mPlanner || !getState().isEnabled) {
696 return;
697 }
698
699 ATRACE_CALL();
700 ALOGV(__FUNCTION__);
701
702 mPlanner->plan(getOutputLayersOrderedByZ());
703}
704
705void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
706 ATRACE_CALL();
707 ALOGV(__FUNCTION__);
708
709 if (!getState().isEnabled) {
710 return;
711 }
712
Dan Stoza6166c312021-01-15 16:34:05 -0800713 sp<GraphicBuffer> previousOverride = nullptr;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800714 for (auto* layer : getOutputLayersOrderedByZ()) {
Dan Stoza6166c312021-01-15 16:34:05 -0800715 bool skipLayer = false;
716 if (layer->getState().overrideInfo.buffer != nullptr) {
717 if (previousOverride != nullptr &&
Alec Mouri617752f2021-04-15 16:27:01 +0000718 layer->getState().overrideInfo.buffer == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800719 ALOGV("Skipping redundant buffer");
720 skipLayer = true;
721 }
Alec Mouri617752f2021-04-15 16:27:01 +0000722 previousOverride = layer->getState().overrideInfo.buffer;
Dan Stoza6166c312021-01-15 16:34:05 -0800723 }
724
Yichi Chen413d46a2021-04-07 21:42:09 +0800725 const bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Dan Stoza6166c312021-01-15 16:34:05 -0800726 layer->writeStateToHWC(includeGeometry, skipLayer);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800727 }
728}
729
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800730compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
731 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
732 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100733 auto* compState = layer->getLayerFE().getCompositionState();
734
735 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
736 // want to force client composition because of the blur.
737 if (compState->sidebandStream != nullptr) {
738 return nullptr;
739 }
740 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800741 layerRequestingBgComposition = layer;
742 }
743 }
744 return layerRequestingBgComposition;
745}
746
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800747void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
748 setColorProfile(pickColorProfile(refreshArgs));
749}
750
751// Returns a data space that fits all visible layers. The returned data space
752// can only be one of
753// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
754// - Dataspace::DISPLAY_P3
755// - Dataspace::DISPLAY_BT2020
756// The returned HDR data space is one of
757// - Dataspace::UNKNOWN
758// - Dataspace::BT2020_HLG
759// - Dataspace::BT2020_PQ
760ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
761 bool* outIsHdrClientComposition) const {
762 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
763 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
764
Lloyd Pique01c77c12019-04-17 12:48:32 -0700765 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800766 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800767 case ui::Dataspace::V0_SCRGB:
768 case ui::Dataspace::V0_SCRGB_LINEAR:
769 case ui::Dataspace::BT2020:
770 case ui::Dataspace::BT2020_ITU:
771 case ui::Dataspace::BT2020_LINEAR:
772 case ui::Dataspace::DISPLAY_BT2020:
773 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
774 break;
775 case ui::Dataspace::DISPLAY_P3:
776 bestDataSpace = ui::Dataspace::DISPLAY_P3;
777 break;
778 case ui::Dataspace::BT2020_PQ:
779 case ui::Dataspace::BT2020_ITU_PQ:
780 bestDataSpace = ui::Dataspace::DISPLAY_P3;
781 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800782 *outIsHdrClientComposition =
783 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800784 break;
785 case ui::Dataspace::BT2020_HLG:
786 case ui::Dataspace::BT2020_ITU_HLG:
787 bestDataSpace = ui::Dataspace::DISPLAY_P3;
788 // When there's mixed PQ content and HLG content, we set the HDR
789 // data space to be BT2020_PQ and convert HLG to PQ.
790 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
791 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
792 }
793 break;
794 default:
795 break;
796 }
797 }
798
799 return bestDataSpace;
800}
801
802compositionengine::Output::ColorProfile Output::pickColorProfile(
803 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
804 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
805 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
806 ui::RenderIntent::COLORIMETRIC,
807 refreshArgs.colorSpaceAgnosticDataspace};
808 }
809
810 ui::Dataspace hdrDataSpace;
811 bool isHdrClientComposition = false;
812 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
813
814 switch (refreshArgs.forceOutputColorMode) {
815 case ui::ColorMode::SRGB:
816 bestDataSpace = ui::Dataspace::V0_SRGB;
817 break;
818 case ui::ColorMode::DISPLAY_P3:
819 bestDataSpace = ui::Dataspace::DISPLAY_P3;
820 break;
821 default:
822 break;
823 }
824
825 // respect hdrDataSpace only when there is no legacy HDR support
826 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
827 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
828 if (isHdr) {
829 bestDataSpace = hdrDataSpace;
830 }
831
832 ui::RenderIntent intent;
833 switch (refreshArgs.outputColorSetting) {
834 case OutputColorSetting::kManaged:
835 case OutputColorSetting::kUnmanaged:
836 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
837 : ui::RenderIntent::COLORIMETRIC;
838 break;
839 case OutputColorSetting::kEnhanced:
840 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
841 break;
842 default: // vendor display color setting
843 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
844 break;
845 }
846
847 ui::ColorMode outMode;
848 ui::Dataspace outDataSpace;
849 ui::RenderIntent outRenderIntent;
850 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
851 &outRenderIntent);
852
853 return ColorProfile{outMode, outDataSpace, outRenderIntent,
854 refreshArgs.colorSpaceAgnosticDataspace};
855}
856
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800857void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700858 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800859 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700860 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700861 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800862
863 // If nothing has changed (!dirty), don't recompose.
864 // If something changed, but we don't currently have any visible layers,
865 // and didn't when we last did a composition, then skip it this time.
866 // The second rule does two things:
867 // - When all layers are removed from a display, we'll emit one black
868 // frame, then nothing more until we get new layers.
869 // - When a display is created with a private layer stack, we won't
870 // emit any black frames until a layer is added to the layer stack.
871 const bool mustRecompose = dirty && !(empty && wasEmpty);
872
873 const char flagPrefix[] = {'-', '+'};
874 static_cast<void>(flagPrefix);
875 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
876 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
877 flagPrefix[empty], flagPrefix[wasEmpty]);
878
879 mRenderSurface->beginFrame(mustRecompose);
880
881 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700882 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800883 }
884}
885
Lloyd Pique66d68602019-02-13 14:23:31 -0800886void Output::prepareFrame() {
887 ATRACE_CALL();
888 ALOGV(__FUNCTION__);
889
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700890 const auto& outputState = getState();
891 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800892 return;
893 }
894
895 chooseCompositionStrategy();
896
Dan Stoza47437bb2021-01-15 16:21:07 -0800897 if (mPlanner) {
898 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
899 }
900
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700901 mRenderSurface->prepareFrame(outputState.usesClientComposition,
902 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800903}
904
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800905void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
906 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
907 return;
908 }
909
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700910 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800911 // transform the dirty region into this screen's coordinate space
912 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
913 if (!dirtyRegion.isEmpty()) {
914 base::unique_fd readyFence;
915 // redraw the whole screen
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800916 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800917
918 mRenderSurface->queueBuffer(std::move(readyFence));
919 }
920 }
921
922 postFramebuffer();
923
924 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
925
926 prepareFrame();
927}
928
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800929void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800930 ATRACE_CALL();
931 ALOGV(__FUNCTION__);
932
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700933 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800934 return;
935 }
936
937 // Repaint the framebuffer (if needed), getting the optional fence for when
938 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800939 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800940 if (!optReadyFence) {
941 return;
942 }
943
944 // swap buffers (presentation)
945 mRenderSurface->queueBuffer(std::move(*optReadyFence));
946}
947
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800948std::optional<base::unique_fd> Output::composeSurfaces(
949 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800950 ATRACE_CALL();
951 ALOGV(__FUNCTION__);
952
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700953 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800954 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800955 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700956 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -0700957
958 auto& renderEngine = getCompositionEngine().getRenderEngine();
959 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
960
961 // If we the display is secure, protected content support is enabled, and at
962 // least one layer has protected content, we need to use a secure back
963 // buffer.
964 if (outputState.isSecure && supportsProtectedContent) {
965 auto layers = getOutputLayersOrderedByZ();
966 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
967 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
968 });
969 if (needsProtected != renderEngine.isProtected()) {
970 renderEngine.useProtectedContext(needsProtected);
971 }
972 if (needsProtected != mRenderSurface->isProtected() &&
973 needsProtected == renderEngine.isProtected()) {
974 mRenderSurface->setProtected(needsProtected);
975 }
Peiyong Lin09f910f2020-09-25 10:54:13 -0700976 } else if (!outputState.isSecure && renderEngine.isProtected()) {
977 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -0700978 }
979
980 base::unique_fd fd;
Alec Mouri617752f2021-04-15 16:27:01 +0000981 sp<GraphicBuffer> buf;
Lloyd Piquee9eff972020-05-05 12:36:44 -0700982
983 // If we aren't doing client composition on this output, but do have a
984 // flipClientTarget request for this frame on this output, we still need to
985 // dequeue a buffer.
986 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouri617752f2021-04-15 16:27:01 +0000987 buf = mRenderSurface->dequeueBuffer(&fd);
988 if (buf == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -0700989 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
990 "client composition for this frame",
991 mName.c_str());
992 return {};
993 }
994 }
995
Lloyd Piqued3d69882019-02-28 16:03:46 -0800996 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800997 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -0800998 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800999 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001000 }
1001
1002 ALOGV("hasClientComposition");
1003
Lloyd Pique688abd42019-02-15 15:42:24 -08001004 renderengine::DisplaySettings clientCompositionDisplay;
Marin Shalamanovb15d2272020-09-17 21:41:52 +02001005 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.content;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001006 clientCompositionDisplay.clip = outputState.layerStackSpace.content;
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001007 clientCompositionDisplay.orientation =
1008 ui::Transform::toRotationFlags(outputState.displaySpace.orientation);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001009 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1010 ? outputState.dataspace
1011 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -08001012 clientCompositionDisplay.maxLuminance =
1013 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1014
1015 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001016 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1017 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001018 }
1019
1020 // Note: Updated by generateClientCompositionRequests
1021 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
1022
1023 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -08001024 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001025 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001026 clientCompositionDisplay.clearRegion,
1027 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -08001028 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1029
Vishnu Nair9b079a22020-01-21 14:36:08 -08001030 // Check if the client composition requests were rendered into the provided graphic buffer. If
1031 // so, we can reuse the buffer and avoid client composition.
1032 if (mClientCompositionRequestCache) {
Alec Mouri617752f2021-04-15 16:27:01 +00001033 if (mClientCompositionRequestCache->exists(buf->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001034 clientCompositionLayers)) {
1035 outputCompositionState.reusedClientComposition = true;
1036 setExpensiveRenderingExpected(false);
1037 return readyFence;
1038 }
Alec Mouri617752f2021-04-15 16:27:01 +00001039 mClientCompositionRequestCache->add(buf->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001040 clientCompositionLayers);
1041 }
1042
Lloyd Pique688abd42019-02-15 15:42:24 -08001043 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001044 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1045 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1046 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001047 const bool expensiveBlurs =
1048 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001049 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001050 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001051 if (expensiveRenderingExpected) {
1052 setExpensiveRenderingExpected(true);
1053 }
1054
Vishnu Nair9b079a22020-01-21 14:36:08 -08001055 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
1056 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
1057 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1058 std::back_inserter(clientCompositionLayerPointers),
1059 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
1060 return &settings;
1061 });
1062
Alec Mourie4034bb2019-11-19 12:45:54 -08001063 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001064 // Only use the framebuffer cache when rendering to an internal display
1065 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1066 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1067 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1068 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1069 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
1070 const bool useFramebufferCache = outputState.layerStackInternal;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001071 status_t status =
Alec Mouri617752f2021-04-15 16:27:01 +00001072 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, buf,
Alec Mouri1684c702021-02-04 12:27:26 -08001073 useFramebufferCache, std::move(fd), &readyFence);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001074
1075 if (status != NO_ERROR && mClientCompositionRequestCache) {
1076 // If rendering was not successful, remove the request from the cache.
Alec Mouri617752f2021-04-15 16:27:01 +00001077 mClientCompositionRequestCache->remove(buf->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001078 }
1079
Alec Mourie4034bb2019-11-19 12:45:54 -08001080 auto& timeStats = getCompositionEngine().getTimeStats();
1081 if (readyFence.get() < 0) {
1082 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1083 } else {
1084 timeStats.recordRenderEngineDuration(renderEngineStart,
1085 std::make_shared<FenceTime>(
1086 new Fence(dup(readyFence.get()))));
1087 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001088
Lloyd Piqued3d69882019-02-28 16:03:46 -08001089 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001090}
1091
Vishnu Nair9b079a22020-01-21 14:36:08 -08001092std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001093 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001094 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001095 ALOGV("Rendering client layers");
1096
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001097 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001098 const Region viewportRegion(outputState.layerStackSpace.content);
Lloyd Pique688abd42019-02-15 15:42:24 -08001099 bool firstLayer = true;
1100 // Used when a layer clears part of the buffer.
Peiyong Lind8460c82020-07-28 16:04:22 -07001101 Region stubRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -08001102
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001103 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001104 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001105
Lloyd Pique01c77c12019-04-17 12:48:32 -07001106 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001107 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001108 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001109 auto& layerFE = layer->getLayerFE();
1110
Lloyd Piquea2468662019-03-07 21:31:06 -08001111 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001112 ALOGV("Layer: %s", layerFE.getDebugName());
1113 if (clip.isEmpty()) {
1114 ALOGV(" Skipping for empty clip");
1115 firstLayer = false;
1116 continue;
1117 }
1118
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001119 disableBlurs |= layerFEState->sidebandStream != nullptr;
1120
Vishnu Naira483b4a2019-12-12 15:07:52 -08001121 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001122
1123 // We clear the client target for non-client composed layers if
1124 // requested by the HWC. We skip this if the layer is not an opaque
1125 // rectangle, as by definition the layer must blend with whatever is
1126 // underneath. We also skip the first layer as the buffer target is
1127 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001128 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001129 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001130
1131 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1132
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001133 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1134 // composing the non-shadow content and only draw the shadows.
1135 const bool realContentIsVisible = clientComposition &&
1136 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1137
Lloyd Pique688abd42019-02-15 15:42:24 -08001138 if (clientComposition || clearClientComposition) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001139 compositionengine::LayerFE::ClientCompositionTargetSettings
1140 targetSettings{.clip = clip,
1141 .needsFiltering =
1142 layer->needsFiltering() || outputState.needsFiltering,
1143 .isSecure = outputState.isSecure,
1144 .supportsProtectedContent = supportsProtectedContent,
1145 .clearRegion = clientComposition ? clearRegion : stubRegion,
1146 .viewport = outputState.layerStackSpace.content,
1147 .dataspace = outputDataspace,
1148 .realContentIsVisible = realContentIsVisible,
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001149 .clearContent = !clientComposition,
1150 .disableBlurs = disableBlurs};
Dan Stoza6166c312021-01-15 16:34:05 -08001151
1152 std::vector<LayerFE::LayerSettings> results;
1153 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouri617752f2021-04-15 16:27:01 +00001154 if (layer->getState().overrideInfo.buffer != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001155 results = layer->getOverrideCompositionList();
Alec Mouri617752f2021-04-15 16:27:01 +00001156 previousOverrideBuffer = layer->getState().overrideInfo.buffer;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001157 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1158 } else {
1159 ALOGV("Skipping redundant override buffer for [%s] in RE",
1160 layer->getLayerFE().getDebugName());
1161 }
Dan Stoza6166c312021-01-15 16:34:05 -08001162 } else {
1163 results = layerFE.prepareClientCompositionList(targetSettings);
1164 if (realContentIsVisible && !results.empty()) {
1165 layer->editState().clientCompositionTimestamp = systemTime();
1166 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001167 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001168
1169 clientCompositionLayers.insert(clientCompositionLayers.end(),
1170 std::make_move_iterator(results.begin()),
1171 std::make_move_iterator(results.end()));
1172 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001173 }
1174
1175 firstLayer = false;
1176 }
1177
1178 return clientCompositionLayers;
1179}
1180
1181void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001182 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001183 if (flashRegion.isEmpty()) {
1184 return;
1185 }
1186
Vishnu Nair9b079a22020-01-21 14:36:08 -08001187 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001188 layerSettings.source.buffer.buffer = nullptr;
1189 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1190 layerSettings.alpha = half(1.0);
1191
1192 for (const auto& rect : flashRegion) {
1193 layerSettings.geometry.boundaries = rect.toFloatRect();
1194 clientCompositionLayers.push_back(layerSettings);
1195 }
1196}
1197
1198void Output::setExpensiveRenderingExpected(bool) {
1199 // The base class does nothing with this call.
1200}
1201
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001202void Output::postFramebuffer() {
1203 ATRACE_CALL();
1204 ALOGV(__FUNCTION__);
1205
1206 if (!getState().isEnabled) {
1207 return;
1208 }
1209
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001210 auto& outputState = editState();
1211 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001212 mRenderSurface->flip();
1213
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001214 auto frame = presentAndGetFrameFences();
1215
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001216 mRenderSurface->onPresentDisplayCompleted();
1217
Lloyd Pique01c77c12019-04-17 12:48:32 -07001218 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001219 // The layer buffer from the previous frame (if any) is released
1220 // by HWC only when the release fence from this frame (if any) is
1221 // signaled. Always get the release fence from HWC first.
1222 sp<Fence> releaseFence = Fence::NO_FENCE;
1223
1224 if (auto hwcLayer = layer->getHwcLayer()) {
1225 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1226 releaseFence = f->second;
1227 }
1228 }
1229
1230 // If the layer was client composited in the previous frame, we
1231 // need to merge with the previous client target acquire fence.
1232 // Since we do not track that, always merge with the current
1233 // client target acquire fence when it is available, even though
1234 // this is suboptimal.
1235 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001236 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001237 releaseFence =
1238 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1239 }
1240
1241 layer->getLayerFE().onLayerDisplayed(releaseFence);
1242 }
1243
1244 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001245 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001246 // supply them with the present fence.
1247 for (auto& weakLayer : mReleasedLayers) {
1248 if (auto layer = weakLayer.promote(); layer != nullptr) {
1249 layer->onLayerDisplayed(frame.presentFence);
1250 }
1251 }
1252
1253 // Clear out the released layers now that we're done with them.
1254 mReleasedLayers.clear();
1255}
1256
Dan Stoza6166c312021-01-15 16:34:05 -08001257void Output::renderCachedSets() {
1258 if (mPlanner) {
Huihong Luoa5825112021-03-24 12:28:29 -07001259 mPlanner->renderCachedSets(getCompositionEngine().getRenderEngine(), getState());
Dan Stoza6166c312021-01-15 16:34:05 -08001260 }
1261}
1262
Lloyd Pique32cbe282018-10-19 13:09:22 -07001263void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001264 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001265 outputState.dirtyRegion.set(outputState.displaySpace.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001266}
1267
Lloyd Pique66d68602019-02-13 14:23:31 -08001268void Output::chooseCompositionStrategy() {
1269 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001270 auto& outputState = editState();
1271 outputState.usesClientComposition = true;
1272 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001273 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001274}
1275
Lloyd Pique688abd42019-02-15 15:42:24 -08001276bool Output::getSkipColorTransform() const {
1277 return true;
1278}
1279
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001280compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1281 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001282 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001283 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1284 }
1285 return result;
1286}
1287
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001288} // namespace impl
1289} // namespace android::compositionengine