blob: 3468b204fc29e6d505684768ddec98375ddd4b3c [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 // Generate a simple Z-order values to each visible output layer
460 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700461 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800462 outputLayer->editState().z = zOrder++;
463 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800464}
465
Lloyd Piquede196652020-01-22 17:29:58 -0800466void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700467 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800468 // Ensure we have a snapshot of the basic geometry layer state. Limit the
469 // snapshots to once per frame for each candidate layer, as layers may
470 // appear on multiple outputs.
471 if (!coverage.latchedLayers.count(layerFE)) {
472 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800473 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800474 }
475
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800476 // Only consider the layers on the given layer stack
Lloyd Piquede196652020-01-22 17:29:58 -0800477 if (!belongsInOutput(layerFE)) {
478 return;
479 }
480
481 // Obtain a read-only pointer to the front-end layer state
482 const auto* layerFEState = layerFE->getCompositionState();
483 if (CC_UNLIKELY(!layerFEState)) {
484 return;
485 }
486
487 // handle hidden surfaces by setting the visible region to empty
488 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700489 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800490 }
491
492 /*
493 * opaqueRegion: area of a surface that is fully opaque.
494 */
495 Region opaqueRegion;
496
497 /*
498 * visibleRegion: area of a surface that is visible on screen and not fully
499 * transparent. This is essentially the layer's footprint minus the opaque
500 * regions above it. Areas covered by a translucent surface are considered
501 * visible.
502 */
503 Region visibleRegion;
504
505 /*
506 * coveredRegion: area of a surface that is covered by all visible regions
507 * above it (which includes the translucent areas).
508 */
509 Region coveredRegion;
510
511 /*
512 * transparentRegion: area of a surface that is hinted to be completely
513 * transparent. This is only used to tell when the layer has no visible non-
514 * transparent regions and can be removed from the layer list. It does not
515 * affect the visibleRegion of this layer or any layers beneath it. The hint
516 * may not be correct if apps don't respect the SurfaceView restrictions
517 * (which, sadly, some don't).
518 */
519 Region transparentRegion;
520
Vishnu Naira483b4a2019-12-12 15:07:52 -0800521 /*
522 * shadowRegion: Region cast by the layer's shadow.
523 */
524 Region shadowRegion;
525
Lloyd Piquede196652020-01-22 17:29:58 -0800526 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800527
528 // Get the visible region
529 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
530 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800531 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800532 visibleRegion.set(visibleRect);
533
Lloyd Piquede196652020-01-22 17:29:58 -0800534 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800535 // if the layer casts a shadow, offset the layers visible region and
536 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800537 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800538 Rect visibleRectWithShadows(visibleRect);
539 visibleRectWithShadows.inset(inset, inset, inset, inset);
540 visibleRegion.set(visibleRectWithShadows);
541 shadowRegion = visibleRegion.subtract(visibleRect);
542 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800543
544 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700545 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800546 }
547
548 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800549 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800550 if (tr.preserveRects()) {
551 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800552 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800553 } else {
554 // transformation too complex, can't do the
555 // transparent region optimization.
556 transparentRegion.clear();
557 }
558 }
559
560 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800561 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800562 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800563 // If we one of the simple category of transforms (0/90/180/270 rotation
564 // + any flip), then the opaque region is the layer's footprint.
565 // Otherwise we don't try and compute the opaque region since there may
566 // be errors at the edges, and we treat the entire layer as
567 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800568 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800569 }
570
571 // Clip the covered region to the visible region
572 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
573
574 // Update accumAboveCoveredLayers for next (lower) layer
575 coverage.aboveCoveredLayers.orSelf(visibleRegion);
576
577 // subtract the opaque region covered by the layers above us
578 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
579
580 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700581 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800582 }
583
584 // Get coverage information for the layer as previously displayed,
585 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800586 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700587 auto prevOutputLayer =
588 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800589
590 // Get coverage information for the layer as previously displayed
591 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
592 const Region kEmptyRegion;
593 const Region& oldVisibleRegion =
594 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
595 const Region& oldCoveredRegion =
596 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
597
598 // compute this layer's dirty region
599 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800600 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800601 // we need to invalidate the whole region
602 dirty = visibleRegion;
603 // as well, as the old visible region
604 dirty.orSelf(oldVisibleRegion);
605 } else {
606 /* compute the exposed region:
607 * the exposed region consists of two components:
608 * 1) what's VISIBLE now and was COVERED before
609 * 2) what's EXPOSED now less what was EXPOSED before
610 *
611 * note that (1) is conservative, we start with the whole visible region
612 * but only keep what used to be covered by something -- which mean it
613 * may have been exposed.
614 *
615 * (2) handles areas that were not covered by anything but got exposed
616 * because of a resize.
617 *
618 */
619 const Region newExposed = visibleRegion - coveredRegion;
620 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
621 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
622 }
623 dirty.subtractSelf(coverage.aboveOpaqueLayers);
624
625 // accumulate to the screen dirty region
626 coverage.dirtyRegion.orSelf(dirty);
627
628 // Update accumAboveOpaqueLayers for next (lower) layer
629 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
630
631 // Compute the visible non-transparent region
632 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
633
Vishnu Naira483b4a2019-12-12 15:07:52 -0800634 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800635 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700636 const auto& outputState = getState();
637 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200638 drawRegion.andSelf(outputState.displaySpace.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800639 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700640 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800641 }
642
Vishnu Naira483b4a2019-12-12 15:07:52 -0800643 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
644
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800645 // The layer is visible. Either reuse the existing outputLayer if we have
646 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800647 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800648
649 // Store the layer coverage information into the layer state as some of it
650 // is useful later.
651 auto& outputLayerState = result->editState();
652 outputLayerState.visibleRegion = visibleRegion;
653 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
654 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200655 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
656 visibleNonShadowRegion.intersect(outputState.layerStackSpace.content));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800657 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800658}
659
660void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
661 // The base class does nothing with this call.
662}
663
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800664void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700665 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800666 layer->getLayerFE().prepareCompositionState(
667 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
668 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800669 }
670}
671
Dan Stoza269dc4d2021-01-15 15:07:43 -0800672void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800673 ATRACE_CALL();
674 ALOGV(__FUNCTION__);
675
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800676 if (!getState().isEnabled) {
677 return;
678 }
679
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800680 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
681 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
682
Lloyd Pique01c77c12019-04-17 12:48:32 -0700683 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700684 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800685 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200686 forceClientComposition,
687 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800688
689 if (mLayerRequestingBackgroundBlur == layer) {
690 forceClientComposition = false;
691 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800692 }
693}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800694
Dan Stoza269dc4d2021-01-15 15:07:43 -0800695void Output::planComposition() {
696 if (!mPlanner || !getState().isEnabled) {
697 return;
698 }
699
700 ATRACE_CALL();
701 ALOGV(__FUNCTION__);
702
703 mPlanner->plan(getOutputLayersOrderedByZ());
704}
705
706void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
707 ATRACE_CALL();
708 ALOGV(__FUNCTION__);
709
710 if (!getState().isEnabled) {
711 return;
712 }
713
Dan Stoza6166c312021-01-15 16:34:05 -0800714 sp<GraphicBuffer> previousOverride = nullptr;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800715 for (auto* layer : getOutputLayersOrderedByZ()) {
Dan Stoza6166c312021-01-15 16:34:05 -0800716 bool skipLayer = false;
717 if (layer->getState().overrideInfo.buffer != nullptr) {
718 if (previousOverride != nullptr &&
Alec Mouria90a5702021-04-16 16:36:21 +0000719 layer->getState().overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800720 ALOGV("Skipping redundant buffer");
721 skipLayer = true;
722 }
Alec Mouria90a5702021-04-16 16:36:21 +0000723 previousOverride = layer->getState().overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800724 }
725
Yichi Chen413d46a2021-04-07 21:42:09 +0800726 const bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Dan Stoza6166c312021-01-15 16:34:05 -0800727 layer->writeStateToHWC(includeGeometry, skipLayer);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800728 }
729}
730
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800731compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
732 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
733 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100734 auto* compState = layer->getLayerFE().getCompositionState();
735
736 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
737 // want to force client composition because of the blur.
738 if (compState->sidebandStream != nullptr) {
739 return nullptr;
740 }
741 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800742 layerRequestingBgComposition = layer;
743 }
744 }
745 return layerRequestingBgComposition;
746}
747
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800748void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
749 setColorProfile(pickColorProfile(refreshArgs));
750}
751
752// Returns a data space that fits all visible layers. The returned data space
753// can only be one of
754// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
755// - Dataspace::DISPLAY_P3
756// - Dataspace::DISPLAY_BT2020
757// The returned HDR data space is one of
758// - Dataspace::UNKNOWN
759// - Dataspace::BT2020_HLG
760// - Dataspace::BT2020_PQ
761ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
762 bool* outIsHdrClientComposition) const {
763 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
764 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
765
Lloyd Pique01c77c12019-04-17 12:48:32 -0700766 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800767 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800768 case ui::Dataspace::V0_SCRGB:
769 case ui::Dataspace::V0_SCRGB_LINEAR:
770 case ui::Dataspace::BT2020:
771 case ui::Dataspace::BT2020_ITU:
772 case ui::Dataspace::BT2020_LINEAR:
773 case ui::Dataspace::DISPLAY_BT2020:
774 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
775 break;
776 case ui::Dataspace::DISPLAY_P3:
777 bestDataSpace = ui::Dataspace::DISPLAY_P3;
778 break;
779 case ui::Dataspace::BT2020_PQ:
780 case ui::Dataspace::BT2020_ITU_PQ:
781 bestDataSpace = ui::Dataspace::DISPLAY_P3;
782 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800783 *outIsHdrClientComposition =
784 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800785 break;
786 case ui::Dataspace::BT2020_HLG:
787 case ui::Dataspace::BT2020_ITU_HLG:
788 bestDataSpace = ui::Dataspace::DISPLAY_P3;
789 // When there's mixed PQ content and HLG content, we set the HDR
790 // data space to be BT2020_PQ and convert HLG to PQ.
791 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
792 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
793 }
794 break;
795 default:
796 break;
797 }
798 }
799
800 return bestDataSpace;
801}
802
803compositionengine::Output::ColorProfile Output::pickColorProfile(
804 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
805 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
806 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
807 ui::RenderIntent::COLORIMETRIC,
808 refreshArgs.colorSpaceAgnosticDataspace};
809 }
810
811 ui::Dataspace hdrDataSpace;
812 bool isHdrClientComposition = false;
813 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
814
815 switch (refreshArgs.forceOutputColorMode) {
816 case ui::ColorMode::SRGB:
817 bestDataSpace = ui::Dataspace::V0_SRGB;
818 break;
819 case ui::ColorMode::DISPLAY_P3:
820 bestDataSpace = ui::Dataspace::DISPLAY_P3;
821 break;
822 default:
823 break;
824 }
825
826 // respect hdrDataSpace only when there is no legacy HDR support
827 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
828 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
829 if (isHdr) {
830 bestDataSpace = hdrDataSpace;
831 }
832
833 ui::RenderIntent intent;
834 switch (refreshArgs.outputColorSetting) {
835 case OutputColorSetting::kManaged:
836 case OutputColorSetting::kUnmanaged:
837 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
838 : ui::RenderIntent::COLORIMETRIC;
839 break;
840 case OutputColorSetting::kEnhanced:
841 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
842 break;
843 default: // vendor display color setting
844 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
845 break;
846 }
847
848 ui::ColorMode outMode;
849 ui::Dataspace outDataSpace;
850 ui::RenderIntent outRenderIntent;
851 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
852 &outRenderIntent);
853
854 return ColorProfile{outMode, outDataSpace, outRenderIntent,
855 refreshArgs.colorSpaceAgnosticDataspace};
856}
857
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800858void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700859 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800860 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700861 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700862 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800863
864 // If nothing has changed (!dirty), don't recompose.
865 // If something changed, but we don't currently have any visible layers,
866 // and didn't when we last did a composition, then skip it this time.
867 // The second rule does two things:
868 // - When all layers are removed from a display, we'll emit one black
869 // frame, then nothing more until we get new layers.
870 // - When a display is created with a private layer stack, we won't
871 // emit any black frames until a layer is added to the layer stack.
872 const bool mustRecompose = dirty && !(empty && wasEmpty);
873
874 const char flagPrefix[] = {'-', '+'};
875 static_cast<void>(flagPrefix);
876 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
877 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
878 flagPrefix[empty], flagPrefix[wasEmpty]);
879
880 mRenderSurface->beginFrame(mustRecompose);
881
882 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700883 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800884 }
885}
886
Lloyd Pique66d68602019-02-13 14:23:31 -0800887void Output::prepareFrame() {
888 ATRACE_CALL();
889 ALOGV(__FUNCTION__);
890
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700891 const auto& outputState = getState();
892 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800893 return;
894 }
895
896 chooseCompositionStrategy();
897
Dan Stoza47437bb2021-01-15 16:21:07 -0800898 if (mPlanner) {
899 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
900 }
901
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700902 mRenderSurface->prepareFrame(outputState.usesClientComposition,
903 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800904}
905
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800906void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
907 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
908 return;
909 }
910
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700911 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800912 // transform the dirty region into this screen's coordinate space
913 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
914 if (!dirtyRegion.isEmpty()) {
915 base::unique_fd readyFence;
916 // redraw the whole screen
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800917 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800918
919 mRenderSurface->queueBuffer(std::move(readyFence));
920 }
921 }
922
923 postFramebuffer();
924
925 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
926
927 prepareFrame();
928}
929
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800930void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800931 ATRACE_CALL();
932 ALOGV(__FUNCTION__);
933
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700934 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800935 return;
936 }
937
938 // Repaint the framebuffer (if needed), getting the optional fence for when
939 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800940 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800941 if (!optReadyFence) {
942 return;
943 }
944
945 // swap buffers (presentation)
946 mRenderSurface->queueBuffer(std::move(*optReadyFence));
947}
948
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800949std::optional<base::unique_fd> Output::composeSurfaces(
950 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800951 ATRACE_CALL();
952 ALOGV(__FUNCTION__);
953
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700954 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800955 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800956 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700957 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -0700958
959 auto& renderEngine = getCompositionEngine().getRenderEngine();
960 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
961
962 // If we the display is secure, protected content support is enabled, and at
963 // least one layer has protected content, we need to use a secure back
964 // buffer.
965 if (outputState.isSecure && supportsProtectedContent) {
966 auto layers = getOutputLayersOrderedByZ();
967 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
968 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
969 });
970 if (needsProtected != renderEngine.isProtected()) {
971 renderEngine.useProtectedContext(needsProtected);
972 }
973 if (needsProtected != mRenderSurface->isProtected() &&
974 needsProtected == renderEngine.isProtected()) {
975 mRenderSurface->setProtected(needsProtected);
976 }
Peiyong Lin09f910f2020-09-25 10:54:13 -0700977 } else if (!outputState.isSecure && renderEngine.isProtected()) {
978 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -0700979 }
980
981 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +0000982
983 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -0700984
985 // If we aren't doing client composition on this output, but do have a
986 // flipClientTarget request for this frame on this output, we still need to
987 // dequeue a buffer.
988 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +0000989 tex = mRenderSurface->dequeueBuffer(&fd);
990 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -0700991 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
992 "client composition for this frame",
993 mName.c_str());
994 return {};
995 }
996 }
997
Lloyd Piqued3d69882019-02-28 16:03:46 -0800998 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800999 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001000 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -08001001 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001002 }
1003
1004 ALOGV("hasClientComposition");
1005
Lloyd Pique688abd42019-02-15 15:42:24 -08001006 renderengine::DisplaySettings clientCompositionDisplay;
Marin Shalamanovb15d2272020-09-17 21:41:52 +02001007 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.content;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001008 clientCompositionDisplay.clip = outputState.layerStackSpace.content;
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001009 clientCompositionDisplay.orientation =
1010 ui::Transform::toRotationFlags(outputState.displaySpace.orientation);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001011 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1012 ? outputState.dataspace
1013 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -08001014 clientCompositionDisplay.maxLuminance =
1015 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1016
1017 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001018 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1019 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001020 }
1021
1022 // Note: Updated by generateClientCompositionRequests
1023 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
1024
1025 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -08001026 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001027 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001028 clientCompositionDisplay.clearRegion,
1029 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -08001030 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1031
Vishnu Nair9b079a22020-01-21 14:36:08 -08001032 // Check if the client composition requests were rendered into the provided graphic buffer. If
1033 // so, we can reuse the buffer and avoid client composition.
1034 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001035 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1036 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001037 clientCompositionLayers)) {
1038 outputCompositionState.reusedClientComposition = true;
1039 setExpensiveRenderingExpected(false);
1040 return readyFence;
1041 }
Alec Mouria90a5702021-04-16 16:36:21 +00001042 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001043 clientCompositionLayers);
1044 }
1045
Lloyd Pique688abd42019-02-15 15:42:24 -08001046 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001047 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1048 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1049 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001050 const bool expensiveBlurs =
1051 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001052 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001053 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001054 if (expensiveRenderingExpected) {
1055 setExpensiveRenderingExpected(true);
1056 }
1057
Vishnu Nair9b079a22020-01-21 14:36:08 -08001058 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
1059 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
1060 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1061 std::back_inserter(clientCompositionLayerPointers),
1062 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
1063 return &settings;
1064 });
1065
Alec Mourie4034bb2019-11-19 12:45:54 -08001066 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001067 // Only use the framebuffer cache when rendering to an internal display
1068 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1069 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1070 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1071 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1072 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
1073 const bool useFramebufferCache = outputState.layerStackInternal;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001074 status_t status =
Alec Mouria90a5702021-04-16 16:36:21 +00001075 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, tex,
Alec Mouri1684c702021-02-04 12:27:26 -08001076 useFramebufferCache, std::move(fd), &readyFence);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001077
1078 if (status != NO_ERROR && mClientCompositionRequestCache) {
1079 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001080 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001081 }
1082
Alec Mourie4034bb2019-11-19 12:45:54 -08001083 auto& timeStats = getCompositionEngine().getTimeStats();
1084 if (readyFence.get() < 0) {
1085 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1086 } else {
1087 timeStats.recordRenderEngineDuration(renderEngineStart,
1088 std::make_shared<FenceTime>(
1089 new Fence(dup(readyFence.get()))));
1090 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001091
Lloyd Piqued3d69882019-02-28 16:03:46 -08001092 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001093}
1094
Vishnu Nair9b079a22020-01-21 14:36:08 -08001095std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001096 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001097 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001098 ALOGV("Rendering client layers");
1099
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001100 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001101 const Region viewportRegion(outputState.layerStackSpace.content);
Lloyd Pique688abd42019-02-15 15:42:24 -08001102 bool firstLayer = true;
1103 // Used when a layer clears part of the buffer.
Peiyong Lind8460c82020-07-28 16:04:22 -07001104 Region stubRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -08001105
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001106 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001107 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001108
Lloyd Pique01c77c12019-04-17 12:48:32 -07001109 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001110 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001111 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001112 auto& layerFE = layer->getLayerFE();
1113
Lloyd Piquea2468662019-03-07 21:31:06 -08001114 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001115 ALOGV("Layer: %s", layerFE.getDebugName());
1116 if (clip.isEmpty()) {
1117 ALOGV(" Skipping for empty clip");
1118 firstLayer = false;
1119 continue;
1120 }
1121
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001122 disableBlurs |= layerFEState->sidebandStream != nullptr;
1123
Vishnu Naira483b4a2019-12-12 15:07:52 -08001124 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001125
1126 // We clear the client target for non-client composed layers if
1127 // requested by the HWC. We skip this if the layer is not an opaque
1128 // rectangle, as by definition the layer must blend with whatever is
1129 // underneath. We also skip the first layer as the buffer target is
1130 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001131 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001132 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001133
1134 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1135
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001136 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1137 // composing the non-shadow content and only draw the shadows.
1138 const bool realContentIsVisible = clientComposition &&
1139 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1140
Lloyd Pique688abd42019-02-15 15:42:24 -08001141 if (clientComposition || clearClientComposition) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001142 compositionengine::LayerFE::ClientCompositionTargetSettings
1143 targetSettings{.clip = clip,
1144 .needsFiltering =
1145 layer->needsFiltering() || outputState.needsFiltering,
1146 .isSecure = outputState.isSecure,
1147 .supportsProtectedContent = supportsProtectedContent,
1148 .clearRegion = clientComposition ? clearRegion : stubRegion,
1149 .viewport = outputState.layerStackSpace.content,
1150 .dataspace = outputDataspace,
1151 .realContentIsVisible = realContentIsVisible,
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001152 .clearContent = !clientComposition,
1153 .disableBlurs = disableBlurs};
Dan Stoza6166c312021-01-15 16:34:05 -08001154
1155 std::vector<LayerFE::LayerSettings> results;
1156 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001157 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001158 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001159 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001160 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1161 } else {
1162 ALOGV("Skipping redundant override buffer for [%s] in RE",
1163 layer->getLayerFE().getDebugName());
1164 }
Dan Stoza6166c312021-01-15 16:34:05 -08001165 } else {
1166 results = layerFE.prepareClientCompositionList(targetSettings);
1167 if (realContentIsVisible && !results.empty()) {
1168 layer->editState().clientCompositionTimestamp = systemTime();
1169 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001170 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001171
1172 clientCompositionLayers.insert(clientCompositionLayers.end(),
1173 std::make_move_iterator(results.begin()),
1174 std::make_move_iterator(results.end()));
1175 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001176 }
1177
1178 firstLayer = false;
1179 }
1180
1181 return clientCompositionLayers;
1182}
1183
1184void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001185 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001186 if (flashRegion.isEmpty()) {
1187 return;
1188 }
1189
Vishnu Nair9b079a22020-01-21 14:36:08 -08001190 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001191 layerSettings.source.buffer.buffer = nullptr;
1192 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1193 layerSettings.alpha = half(1.0);
1194
1195 for (const auto& rect : flashRegion) {
1196 layerSettings.geometry.boundaries = rect.toFloatRect();
1197 clientCompositionLayers.push_back(layerSettings);
1198 }
1199}
1200
1201void Output::setExpensiveRenderingExpected(bool) {
1202 // The base class does nothing with this call.
1203}
1204
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001205void Output::postFramebuffer() {
1206 ATRACE_CALL();
1207 ALOGV(__FUNCTION__);
1208
1209 if (!getState().isEnabled) {
1210 return;
1211 }
1212
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001213 auto& outputState = editState();
1214 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001215 mRenderSurface->flip();
1216
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001217 auto frame = presentAndGetFrameFences();
1218
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001219 mRenderSurface->onPresentDisplayCompleted();
1220
Lloyd Pique01c77c12019-04-17 12:48:32 -07001221 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001222 // The layer buffer from the previous frame (if any) is released
1223 // by HWC only when the release fence from this frame (if any) is
1224 // signaled. Always get the release fence from HWC first.
1225 sp<Fence> releaseFence = Fence::NO_FENCE;
1226
1227 if (auto hwcLayer = layer->getHwcLayer()) {
1228 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1229 releaseFence = f->second;
1230 }
1231 }
1232
1233 // If the layer was client composited in the previous frame, we
1234 // need to merge with the previous client target acquire fence.
1235 // Since we do not track that, always merge with the current
1236 // client target acquire fence when it is available, even though
1237 // this is suboptimal.
1238 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001239 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001240 releaseFence =
1241 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1242 }
1243
1244 layer->getLayerFE().onLayerDisplayed(releaseFence);
1245 }
1246
1247 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001248 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001249 // supply them with the present fence.
1250 for (auto& weakLayer : mReleasedLayers) {
1251 if (auto layer = weakLayer.promote(); layer != nullptr) {
1252 layer->onLayerDisplayed(frame.presentFence);
1253 }
1254 }
1255
1256 // Clear out the released layers now that we're done with them.
1257 mReleasedLayers.clear();
1258}
1259
Dan Stoza6166c312021-01-15 16:34:05 -08001260void Output::renderCachedSets() {
1261 if (mPlanner) {
Huihong Luoa5825112021-03-24 12:28:29 -07001262 mPlanner->renderCachedSets(getCompositionEngine().getRenderEngine(), getState());
Dan Stoza6166c312021-01-15 16:34:05 -08001263 }
1264}
1265
Lloyd Pique32cbe282018-10-19 13:09:22 -07001266void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001267 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001268 outputState.dirtyRegion.set(outputState.displaySpace.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001269}
1270
Lloyd Pique66d68602019-02-13 14:23:31 -08001271void Output::chooseCompositionStrategy() {
1272 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001273 auto& outputState = editState();
1274 outputState.usesClientComposition = true;
1275 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001276 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001277}
1278
Lloyd Pique688abd42019-02-15 15:42:24 -08001279bool Output::getSkipColorTransform() const {
1280 return true;
1281}
1282
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001283compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1284 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001285 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001286 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1287 }
1288 return result;
1289}
1290
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001291} // namespace impl
1292} // namespace android::compositionengine