blob: 29534764d4f182bba40373ec1317c7623a9bf76d [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 IIIe2ee0402021-04-02 16:59:37 -0400712 uint32_t z = 0;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800713 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400714 if (layer == peekThroughLayer) {
715 // No longer needed, although it should not show up again, so
716 // resetting it is not truly needed either.
717 peekThroughLayer = nullptr;
718
719 // peekThroughLayer was already drawn ahead of its z order.
720 continue;
721 }
Dan Stoza6166c312021-01-15 16:34:05 -0800722 bool skipLayer = false;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400723 auto& overrideInfo = layer->getState().overrideInfo;
724 if (overrideInfo.buffer != nullptr) {
725 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800726 ALOGV("Skipping redundant buffer");
727 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400728 } else {
729 // First layer with the override buffer.
730 if (overrideInfo.peekThroughLayer) {
731 peekThroughLayer = overrideInfo.peekThroughLayer;
732 // Draw peekThroughLayer first.
733 const bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
734 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++);
735 }
736
737 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800738 }
Dan Stoza6166c312021-01-15 16:34:05 -0800739 }
740
Yichi Chen413d46a2021-04-07 21:42:09 +0800741 const bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400742 layer->writeStateToHWC(includeGeometry, skipLayer, z++);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800743 }
744}
745
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800746compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
747 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
748 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100749 auto* compState = layer->getLayerFE().getCompositionState();
750
751 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
752 // want to force client composition because of the blur.
753 if (compState->sidebandStream != nullptr) {
754 return nullptr;
755 }
756 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800757 layerRequestingBgComposition = layer;
758 }
759 }
760 return layerRequestingBgComposition;
761}
762
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800763void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
764 setColorProfile(pickColorProfile(refreshArgs));
765}
766
767// Returns a data space that fits all visible layers. The returned data space
768// can only be one of
769// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
770// - Dataspace::DISPLAY_P3
771// - Dataspace::DISPLAY_BT2020
772// The returned HDR data space is one of
773// - Dataspace::UNKNOWN
774// - Dataspace::BT2020_HLG
775// - Dataspace::BT2020_PQ
776ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
777 bool* outIsHdrClientComposition) const {
778 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
779 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
780
Lloyd Pique01c77c12019-04-17 12:48:32 -0700781 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800782 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800783 case ui::Dataspace::V0_SCRGB:
784 case ui::Dataspace::V0_SCRGB_LINEAR:
785 case ui::Dataspace::BT2020:
786 case ui::Dataspace::BT2020_ITU:
787 case ui::Dataspace::BT2020_LINEAR:
788 case ui::Dataspace::DISPLAY_BT2020:
789 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
790 break;
791 case ui::Dataspace::DISPLAY_P3:
792 bestDataSpace = ui::Dataspace::DISPLAY_P3;
793 break;
794 case ui::Dataspace::BT2020_PQ:
795 case ui::Dataspace::BT2020_ITU_PQ:
796 bestDataSpace = ui::Dataspace::DISPLAY_P3;
797 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800798 *outIsHdrClientComposition =
799 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800800 break;
801 case ui::Dataspace::BT2020_HLG:
802 case ui::Dataspace::BT2020_ITU_HLG:
803 bestDataSpace = ui::Dataspace::DISPLAY_P3;
804 // When there's mixed PQ content and HLG content, we set the HDR
805 // data space to be BT2020_PQ and convert HLG to PQ.
806 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
807 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
808 }
809 break;
810 default:
811 break;
812 }
813 }
814
815 return bestDataSpace;
816}
817
818compositionengine::Output::ColorProfile Output::pickColorProfile(
819 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
820 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
821 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
822 ui::RenderIntent::COLORIMETRIC,
823 refreshArgs.colorSpaceAgnosticDataspace};
824 }
825
826 ui::Dataspace hdrDataSpace;
827 bool isHdrClientComposition = false;
828 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
829
830 switch (refreshArgs.forceOutputColorMode) {
831 case ui::ColorMode::SRGB:
832 bestDataSpace = ui::Dataspace::V0_SRGB;
833 break;
834 case ui::ColorMode::DISPLAY_P3:
835 bestDataSpace = ui::Dataspace::DISPLAY_P3;
836 break;
837 default:
838 break;
839 }
840
841 // respect hdrDataSpace only when there is no legacy HDR support
842 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
843 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
844 if (isHdr) {
845 bestDataSpace = hdrDataSpace;
846 }
847
848 ui::RenderIntent intent;
849 switch (refreshArgs.outputColorSetting) {
850 case OutputColorSetting::kManaged:
851 case OutputColorSetting::kUnmanaged:
852 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
853 : ui::RenderIntent::COLORIMETRIC;
854 break;
855 case OutputColorSetting::kEnhanced:
856 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
857 break;
858 default: // vendor display color setting
859 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
860 break;
861 }
862
863 ui::ColorMode outMode;
864 ui::Dataspace outDataSpace;
865 ui::RenderIntent outRenderIntent;
866 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
867 &outRenderIntent);
868
869 return ColorProfile{outMode, outDataSpace, outRenderIntent,
870 refreshArgs.colorSpaceAgnosticDataspace};
871}
872
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800873void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700874 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800875 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700876 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700877 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800878
879 // If nothing has changed (!dirty), don't recompose.
880 // If something changed, but we don't currently have any visible layers,
881 // and didn't when we last did a composition, then skip it this time.
882 // The second rule does two things:
883 // - When all layers are removed from a display, we'll emit one black
884 // frame, then nothing more until we get new layers.
885 // - When a display is created with a private layer stack, we won't
886 // emit any black frames until a layer is added to the layer stack.
887 const bool mustRecompose = dirty && !(empty && wasEmpty);
888
889 const char flagPrefix[] = {'-', '+'};
890 static_cast<void>(flagPrefix);
891 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
892 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
893 flagPrefix[empty], flagPrefix[wasEmpty]);
894
895 mRenderSurface->beginFrame(mustRecompose);
896
897 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700898 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800899 }
900}
901
Lloyd Pique66d68602019-02-13 14:23:31 -0800902void Output::prepareFrame() {
903 ATRACE_CALL();
904 ALOGV(__FUNCTION__);
905
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700906 const auto& outputState = getState();
907 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800908 return;
909 }
910
911 chooseCompositionStrategy();
912
Dan Stoza47437bb2021-01-15 16:21:07 -0800913 if (mPlanner) {
914 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
915 }
916
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700917 mRenderSurface->prepareFrame(outputState.usesClientComposition,
918 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800919}
920
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800921void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
922 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
923 return;
924 }
925
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700926 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800927 // transform the dirty region into this screen's coordinate space
928 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
929 if (!dirtyRegion.isEmpty()) {
930 base::unique_fd readyFence;
931 // redraw the whole screen
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800932 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800933
934 mRenderSurface->queueBuffer(std::move(readyFence));
935 }
936 }
937
938 postFramebuffer();
939
940 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
941
942 prepareFrame();
943}
944
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800945void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800946 ATRACE_CALL();
947 ALOGV(__FUNCTION__);
948
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700949 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800950 return;
951 }
952
953 // Repaint the framebuffer (if needed), getting the optional fence for when
954 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800955 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800956 if (!optReadyFence) {
957 return;
958 }
959
960 // swap buffers (presentation)
961 mRenderSurface->queueBuffer(std::move(*optReadyFence));
962}
963
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800964std::optional<base::unique_fd> Output::composeSurfaces(
965 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800966 ATRACE_CALL();
967 ALOGV(__FUNCTION__);
968
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700969 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800970 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800971 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700972 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -0700973
974 auto& renderEngine = getCompositionEngine().getRenderEngine();
975 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
976
977 // If we the display is secure, protected content support is enabled, and at
978 // least one layer has protected content, we need to use a secure back
979 // buffer.
980 if (outputState.isSecure && supportsProtectedContent) {
981 auto layers = getOutputLayersOrderedByZ();
982 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
983 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
984 });
985 if (needsProtected != renderEngine.isProtected()) {
986 renderEngine.useProtectedContext(needsProtected);
987 }
988 if (needsProtected != mRenderSurface->isProtected() &&
989 needsProtected == renderEngine.isProtected()) {
990 mRenderSurface->setProtected(needsProtected);
991 }
Peiyong Lin09f910f2020-09-25 10:54:13 -0700992 } else if (!outputState.isSecure && renderEngine.isProtected()) {
993 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -0700994 }
995
996 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +0000997
998 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -0700999
1000 // If we aren't doing client composition on this output, but do have a
1001 // flipClientTarget request for this frame on this output, we still need to
1002 // dequeue a buffer.
1003 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +00001004 tex = mRenderSurface->dequeueBuffer(&fd);
1005 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001006 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1007 "client composition for this frame",
1008 mName.c_str());
1009 return {};
1010 }
1011 }
1012
Lloyd Piqued3d69882019-02-28 16:03:46 -08001013 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001014 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001015 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -08001016 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001017 }
1018
1019 ALOGV("hasClientComposition");
1020
Lloyd Pique688abd42019-02-15 15:42:24 -08001021 renderengine::DisplaySettings clientCompositionDisplay;
Marin Shalamanovb15d2272020-09-17 21:41:52 +02001022 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.content;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001023 clientCompositionDisplay.clip = outputState.layerStackSpace.content;
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001024 clientCompositionDisplay.orientation =
1025 ui::Transform::toRotationFlags(outputState.displaySpace.orientation);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001026 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1027 ? outputState.dataspace
1028 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -08001029 clientCompositionDisplay.maxLuminance =
1030 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1031
1032 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001033 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1034 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001035 }
1036
1037 // Note: Updated by generateClientCompositionRequests
1038 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
1039
1040 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -08001041 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001042 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001043 clientCompositionDisplay.clearRegion,
1044 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -08001045 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1046
Vishnu Nair9b079a22020-01-21 14:36:08 -08001047 // Check if the client composition requests were rendered into the provided graphic buffer. If
1048 // so, we can reuse the buffer and avoid client composition.
1049 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001050 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1051 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001052 clientCompositionLayers)) {
1053 outputCompositionState.reusedClientComposition = true;
1054 setExpensiveRenderingExpected(false);
1055 return readyFence;
1056 }
Alec Mouria90a5702021-04-16 16:36:21 +00001057 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001058 clientCompositionLayers);
1059 }
1060
Lloyd Pique688abd42019-02-15 15:42:24 -08001061 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001062 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1063 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1064 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001065 const bool expensiveBlurs =
1066 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001067 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001068 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001069 if (expensiveRenderingExpected) {
1070 setExpensiveRenderingExpected(true);
1071 }
1072
Vishnu Nair9b079a22020-01-21 14:36:08 -08001073 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
1074 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
1075 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1076 std::back_inserter(clientCompositionLayerPointers),
1077 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
1078 return &settings;
1079 });
1080
Alec Mourie4034bb2019-11-19 12:45:54 -08001081 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001082 // Only use the framebuffer cache when rendering to an internal display
1083 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1084 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1085 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1086 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1087 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
1088 const bool useFramebufferCache = outputState.layerStackInternal;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001089 status_t status =
Alec Mouria90a5702021-04-16 16:36:21 +00001090 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, tex,
Alec Mouri1684c702021-02-04 12:27:26 -08001091 useFramebufferCache, std::move(fd), &readyFence);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001092
1093 if (status != NO_ERROR && mClientCompositionRequestCache) {
1094 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001095 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001096 }
1097
Alec Mourie4034bb2019-11-19 12:45:54 -08001098 auto& timeStats = getCompositionEngine().getTimeStats();
1099 if (readyFence.get() < 0) {
1100 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1101 } else {
1102 timeStats.recordRenderEngineDuration(renderEngineStart,
1103 std::make_shared<FenceTime>(
1104 new Fence(dup(readyFence.get()))));
1105 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001106
Lloyd Piqued3d69882019-02-28 16:03:46 -08001107 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001108}
1109
Vishnu Nair9b079a22020-01-21 14:36:08 -08001110std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001111 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001112 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001113 ALOGV("Rendering client layers");
1114
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001115 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001116 const Region viewportRegion(outputState.layerStackSpace.content);
Lloyd Pique688abd42019-02-15 15:42:24 -08001117 bool firstLayer = true;
1118 // Used when a layer clears part of the buffer.
Peiyong Lind8460c82020-07-28 16:04:22 -07001119 Region stubRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -08001120
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001121 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001122 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001123
Lloyd Pique01c77c12019-04-17 12:48:32 -07001124 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001125 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001126 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001127 auto& layerFE = layer->getLayerFE();
1128
Lloyd Piquea2468662019-03-07 21:31:06 -08001129 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001130 ALOGV("Layer: %s", layerFE.getDebugName());
1131 if (clip.isEmpty()) {
1132 ALOGV(" Skipping for empty clip");
1133 firstLayer = false;
1134 continue;
1135 }
1136
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001137 disableBlurs |= layerFEState->sidebandStream != nullptr;
1138
Vishnu Naira483b4a2019-12-12 15:07:52 -08001139 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001140
1141 // We clear the client target for non-client composed layers if
1142 // requested by the HWC. We skip this if the layer is not an opaque
1143 // rectangle, as by definition the layer must blend with whatever is
1144 // underneath. We also skip the first layer as the buffer target is
1145 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001146 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001147 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001148
1149 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1150
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001151 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1152 // composing the non-shadow content and only draw the shadows.
1153 const bool realContentIsVisible = clientComposition &&
1154 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1155
Lloyd Pique688abd42019-02-15 15:42:24 -08001156 if (clientComposition || clearClientComposition) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001157 compositionengine::LayerFE::ClientCompositionTargetSettings
1158 targetSettings{.clip = clip,
1159 .needsFiltering =
1160 layer->needsFiltering() || outputState.needsFiltering,
1161 .isSecure = outputState.isSecure,
1162 .supportsProtectedContent = supportsProtectedContent,
1163 .clearRegion = clientComposition ? clearRegion : stubRegion,
1164 .viewport = outputState.layerStackSpace.content,
1165 .dataspace = outputDataspace,
1166 .realContentIsVisible = realContentIsVisible,
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001167 .clearContent = !clientComposition,
1168 .disableBlurs = disableBlurs};
Dan Stoza6166c312021-01-15 16:34:05 -08001169
1170 std::vector<LayerFE::LayerSettings> results;
1171 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001172 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001173 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001174 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001175 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1176 } else {
1177 ALOGV("Skipping redundant override buffer for [%s] in RE",
1178 layer->getLayerFE().getDebugName());
1179 }
Dan Stoza6166c312021-01-15 16:34:05 -08001180 } else {
1181 results = layerFE.prepareClientCompositionList(targetSettings);
1182 if (realContentIsVisible && !results.empty()) {
1183 layer->editState().clientCompositionTimestamp = systemTime();
1184 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001185 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001186
1187 clientCompositionLayers.insert(clientCompositionLayers.end(),
1188 std::make_move_iterator(results.begin()),
1189 std::make_move_iterator(results.end()));
1190 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001191 }
1192
1193 firstLayer = false;
1194 }
1195
1196 return clientCompositionLayers;
1197}
1198
1199void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001200 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001201 if (flashRegion.isEmpty()) {
1202 return;
1203 }
1204
Vishnu Nair9b079a22020-01-21 14:36:08 -08001205 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001206 layerSettings.source.buffer.buffer = nullptr;
1207 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1208 layerSettings.alpha = half(1.0);
1209
1210 for (const auto& rect : flashRegion) {
1211 layerSettings.geometry.boundaries = rect.toFloatRect();
1212 clientCompositionLayers.push_back(layerSettings);
1213 }
1214}
1215
1216void Output::setExpensiveRenderingExpected(bool) {
1217 // The base class does nothing with this call.
1218}
1219
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001220void Output::postFramebuffer() {
1221 ATRACE_CALL();
1222 ALOGV(__FUNCTION__);
1223
1224 if (!getState().isEnabled) {
1225 return;
1226 }
1227
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001228 auto& outputState = editState();
1229 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001230 mRenderSurface->flip();
1231
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001232 auto frame = presentAndGetFrameFences();
1233
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001234 mRenderSurface->onPresentDisplayCompleted();
1235
Lloyd Pique01c77c12019-04-17 12:48:32 -07001236 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001237 // The layer buffer from the previous frame (if any) is released
1238 // by HWC only when the release fence from this frame (if any) is
1239 // signaled. Always get the release fence from HWC first.
1240 sp<Fence> releaseFence = Fence::NO_FENCE;
1241
1242 if (auto hwcLayer = layer->getHwcLayer()) {
1243 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1244 releaseFence = f->second;
1245 }
1246 }
1247
1248 // If the layer was client composited in the previous frame, we
1249 // need to merge with the previous client target acquire fence.
1250 // Since we do not track that, always merge with the current
1251 // client target acquire fence when it is available, even though
1252 // this is suboptimal.
1253 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001254 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001255 releaseFence =
1256 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1257 }
1258
1259 layer->getLayerFE().onLayerDisplayed(releaseFence);
1260 }
1261
1262 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001263 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001264 // supply them with the present fence.
1265 for (auto& weakLayer : mReleasedLayers) {
1266 if (auto layer = weakLayer.promote(); layer != nullptr) {
1267 layer->onLayerDisplayed(frame.presentFence);
1268 }
1269 }
1270
1271 // Clear out the released layers now that we're done with them.
1272 mReleasedLayers.clear();
1273}
1274
Dan Stoza6166c312021-01-15 16:34:05 -08001275void Output::renderCachedSets() {
1276 if (mPlanner) {
Huihong Luoa5825112021-03-24 12:28:29 -07001277 mPlanner->renderCachedSets(getCompositionEngine().getRenderEngine(), getState());
Dan Stoza6166c312021-01-15 16:34:05 -08001278 }
1279}
1280
Lloyd Pique32cbe282018-10-19 13:09:22 -07001281void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001282 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001283 outputState.dirtyRegion.set(outputState.displaySpace.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001284}
1285
Lloyd Pique66d68602019-02-13 14:23:31 -08001286void Output::chooseCompositionStrategy() {
1287 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001288 auto& outputState = editState();
1289 outputState.usesClientComposition = true;
1290 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001291 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001292}
1293
Lloyd Pique688abd42019-02-15 15:42:24 -08001294bool Output::getSkipColorTransform() const {
1295 return true;
1296}
1297
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001298compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1299 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001300 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001301 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1302 }
1303 return result;
1304}
1305
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001306} // namespace impl
1307} // namespace android::compositionengine