blob: 13f5137064a41bddd048867808f8d8b1483ac81b [file] [log] [blame]
Lloyd Pique45a165a2018-10-19 11:54:47 -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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Lloyd Pique32cbe282018-10-19 13:09:22 -070021#include <android-base/stringprintf.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070022#include <compositionengine/CompositionEngine.h>
Lloyd Piqued3d69882019-02-28 16:03:46 -080023#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070024#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070025#include <compositionengine/DisplaySurface.h>
Lloyd Piquedf336d92019-03-07 21:38:42 -080026#include <compositionengine/LayerFE.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070027#include <compositionengine/impl/Display.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070028#include <compositionengine/impl/DisplayColorProfile.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070029#include <compositionengine/impl/DumpHelpers.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080030#include <compositionengine/impl/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070031#include <compositionengine/impl/RenderSurface.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080032#include <utils/Trace.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070033
34#include "DisplayHardware/HWComposer.h"
Lloyd Pique688abd42019-02-15 15:42:24 -080035#include "DisplayHardware/PowerAdvisor.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070036
37namespace android::compositionengine::impl {
38
Lloyd Pique35fca9d2019-02-13 14:24:11 -080039std::shared_ptr<Display> createDisplay(
Lloyd Pique45a165a2018-10-19 11:54:47 -070040 const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070041 const compositionengine::DisplayCreationArgs& args) {
42 return createDisplayTemplated<Display>(compositionEngine, args);
Lloyd Pique45a165a2018-10-19 11:54:47 -070043}
44
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070045Display::Display(const DisplayCreationArgs& args)
46 : mIsVirtual(args.isVirtual), mId(args.displayId), mPowerAdvisor(args.powerAdvisor) {}
Lloyd Pique45a165a2018-10-19 11:54:47 -070047
48Display::~Display() = default;
49
50const std::optional<DisplayId>& Display::getId() const {
51 return mId;
52}
53
54bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070055 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070056}
57
58bool Display::isVirtual() const {
59 return mIsVirtual;
60}
61
Lloyd Pique6c564cf2019-05-17 17:31:36 -070062std::optional<DisplayId> Display::getDisplayId() const {
63 return mId;
64}
65
Lloyd Pique45a165a2018-10-19 11:54:47 -070066void Display::disconnect() {
67 if (!mId) {
68 return;
69 }
70
Lloyd Pique32cbe282018-10-19 13:09:22 -070071 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique45a165a2018-10-19 11:54:47 -070072 hwc.disconnectDisplay(*mId);
73 mId.reset();
74}
75
Lloyd Pique3eb1b212019-03-07 21:15:40 -080076void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
77 Output::setColorTransform(args);
78
79 if (!mId || CC_LIKELY(!args.colorTransformMatrix)) {
80 return;
81 }
Lloyd Pique32cbe282018-10-19 13:09:22 -070082
83 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique3eb1b212019-03-07 21:15:40 -080084 status_t result = hwc.setColorTransform(*mId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -070085 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
86 mId ? to_string(*mId).c_str() : "", result);
87}
88
Lloyd Pique6a3b4462019-03-07 20:58:12 -080089void Display::setColorProfile(const ColorProfile& colorProfile) {
90 const ui::Dataspace targetDataspace =
91 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
92 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -080093
Lloyd Pique6a3b4462019-03-07 20:58:12 -080094 if (colorProfile.mode == getState().colorMode &&
95 colorProfile.dataspace == getState().dataspace &&
96 colorProfile.renderIntent == getState().renderIntent &&
97 targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070098 return;
99 }
100
101 if (mIsVirtual) {
102 ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
103 return;
104 }
105
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800106 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700107
108 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800109 hwc.setActiveColorMode(*mId, colorProfile.mode, colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700110}
111
112void Display::dump(std::string& out) const {
113 using android::base::StringAppendF;
114
115 StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str());
116
117 out.append("\n ");
118
119 dumpVal(out, "isVirtual", mIsVirtual);
120 if (mId) {
121 dumpVal(out, "hwcId", to_string(*mId));
122 } else {
123 StringAppendF(&out, "no hwcId, ");
124 }
125
126 out.append("\n");
127
128 Output::dumpBase(out);
129}
130
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700131void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
132 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700133}
134
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700135void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
136 setRenderSurface(
137 compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700138}
139
Vishnu Nair9b079a22020-01-21 14:36:08 -0800140void Display::createClientCompositionCache(uint32_t cacheSize) {
141 cacheClientCompositionRequests(cacheSize);
142}
143
Lloyd Piquedf336d92019-03-07 21:38:42 -0800144std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
145 const std::shared_ptr<compositionengine::Layer>& layer,
146 const sp<compositionengine::LayerFE>& layerFE) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700147 auto result = impl::createOutputLayer(*this, layer, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800148
149 if (result && mId) {
150 auto& hwc = getCompositionEngine().getHwComposer();
151 auto displayId = *mId;
152 // Note: For the moment we ensure it is safe to take a reference to the
153 // HWComposer implementation by destroying all the OutputLayers (and
154 // hence the HWC2::Layers they own) before setting a new HWComposer. See
155 // for example SurfaceFlinger::updateVrFlinger().
156 // TODO(b/121291683): Make this safer.
157 auto hwcLayer = std::shared_ptr<HWC2::Layer>(hwc.createLayer(displayId),
158 [&hwc, displayId](HWC2::Layer* layer) {
159 hwc.destroyLayer(displayId, layer);
160 });
161 ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
162 getName().c_str());
163 result->setHwcLayer(std::move(hwcLayer));
164 }
165 return result;
166}
167
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800168void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
169 Output::setReleasedLayers(refreshArgs);
170
171 if (!mId || refreshArgs.layersWithQueuedFrames.empty()) {
172 return;
173 }
174
175 // For layers that are being removed from a HWC display, and that have
176 // queued frames, add them to a a list of released layers so we can properly
177 // set a fence.
178 compositionengine::Output::ReleasedLayers releasedLayers;
179
180 // Any non-null entries in the current list of layers are layers that are no
181 // longer going to be visible
Lloyd Pique01c77c12019-04-17 12:48:32 -0700182 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800183 if (!layer) {
184 continue;
185 }
186
187 sp<compositionengine::LayerFE> layerFE(&layer->getLayerFE());
188 const bool hasQueuedFrames =
189 std::find(refreshArgs.layersWithQueuedFrames.cbegin(),
190 refreshArgs.layersWithQueuedFrames.cend(),
191 &layer->getLayer()) != refreshArgs.layersWithQueuedFrames.cend();
192
193 if (hasQueuedFrames) {
194 releasedLayers.emplace_back(layerFE);
195 }
196 }
197
198 setReleasedLayers(std::move(releasedLayers));
199}
200
Lloyd Pique66d68602019-02-13 14:23:31 -0800201void Display::chooseCompositionStrategy() {
202 ATRACE_CALL();
203 ALOGV(__FUNCTION__);
204
205 // Default to the base settings -- client composition only.
206 Output::chooseCompositionStrategy();
207
208 // If we don't have a HWC display, then we are done
209 if (!mId) {
210 return;
211 }
212
213 // Get any composition changes requested by the HWC device, and apply them.
214 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
215 auto& hwc = getCompositionEngine().getHwComposer();
216 if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
217 &changes);
218 result != NO_ERROR) {
219 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
220 strerror(-result));
221 return;
222 }
223 if (changes) {
224 applyChangedTypesToLayers(changes->changedTypes);
225 applyDisplayRequests(changes->displayRequests);
226 applyLayerRequestsToLayers(changes->layerRequests);
227 }
228
229 // Determine what type of composition we are doing from the final state
230 auto& state = editState();
231 state.usesClientComposition = anyLayersRequireClientComposition();
232 state.usesDeviceComposition = !allLayersRequireClientComposition();
233}
234
Lloyd Pique688abd42019-02-15 15:42:24 -0800235bool Display::getSkipColorTransform() const {
236 if (!mId) {
237 return false;
238 }
239
240 auto& hwc = getCompositionEngine().getHwComposer();
241 return hwc.hasDisplayCapability(*mId, HWC2::DisplayCapability::SkipClientColorTransform);
242}
243
Lloyd Pique66d68602019-02-13 14:23:31 -0800244bool Display::anyLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700245 const auto layers = getOutputLayersOrderedByZ();
246 return std::any_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800247 [](const auto& layer) { return layer->requiresClientComposition(); });
248}
249
250bool Display::allLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700251 const auto layers = getOutputLayersOrderedByZ();
252 return std::all_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800253 [](const auto& layer) { return layer->requiresClientComposition(); });
254}
255
256void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
257 if (changedTypes.empty()) {
258 return;
259 }
260
Lloyd Pique01c77c12019-04-17 12:48:32 -0700261 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800262 auto hwcLayer = layer->getHwcLayer();
263 if (!hwcLayer) {
264 continue;
265 }
266
267 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
268 layer->applyDeviceCompositionTypeChange(
269 static_cast<Hwc2::IComposerClient::Composition>(it->second));
270 }
271 }
272}
273
274void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
275 auto& state = editState();
276 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
277 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0;
278 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
279}
280
281void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700282 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800283 layer->prepareForDeviceLayerRequests();
284
285 auto hwcLayer = layer->getHwcLayer();
286 if (!hwcLayer) {
287 continue;
288 }
289
290 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
291 layer->applyDeviceLayerRequest(
292 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
293 }
294 }
295}
296
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800297compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
298 auto result = impl::Output::presentAndGetFrameFences();
299
300 if (!mId) {
301 return result;
302 }
303
304 auto& hwc = getCompositionEngine().getHwComposer();
305 hwc.presentAndGetReleaseFences(*mId);
306
307 result.presentFence = hwc.getPresentFence(*mId);
308
309 // TODO(b/121291683): Change HWComposer call to return entire map
Lloyd Pique01c77c12019-04-17 12:48:32 -0700310 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800311 auto hwcLayer = layer->getHwcLayer();
312 if (!hwcLayer) {
313 continue;
314 }
315
316 result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
317 }
318
319 hwc.clearReleaseFences(*mId);
320
321 return result;
322}
323
Lloyd Pique688abd42019-02-15 15:42:24 -0800324void Display::setExpensiveRenderingExpected(bool enabled) {
325 Output::setExpensiveRenderingExpected(enabled);
326
327 if (mPowerAdvisor && mId) {
328 mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled);
329 }
330}
331
Lloyd Piqued3d69882019-02-28 16:03:46 -0800332void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
333 // We only need to actually compose the display if:
334 // 1) It is being handled by hardware composer, which may need this to
335 // keep its virtual display state machine in sync, or
336 // 2) There is work to be done (the dirty region isn't empty)
337 if (!mId) {
338 if (getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) {
339 ALOGV("Skipping display composition");
340 return;
341 }
342 }
343
344 impl::Output::finishFrame(refreshArgs);
345}
346
Lloyd Pique45a165a2018-10-19 11:54:47 -0700347} // namespace android::compositionengine::impl
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800348
349// TODO(b/129481165): remove the #pragma below and fix conversion issues
350#pragma clang diagnostic pop // ignored "-Wconversion"