1 /*
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
17 #include <android-base/stringprintf.h>
18 #include <compositionengine/CompositionEngine.h>
19 #include <compositionengine/CompositionRefreshArgs.h>
20 #include <compositionengine/DisplayCreationArgs.h>
21 #include <compositionengine/DisplaySurface.h>
22 #include <compositionengine/LayerFE.h>
23 #include <compositionengine/impl/Display.h>
24 #include <compositionengine/impl/DisplayColorProfile.h>
25 #include <compositionengine/impl/DumpHelpers.h>
26 #include <compositionengine/impl/OutputLayer.h>
27 #include <compositionengine/impl/RenderSurface.h>
28 #include <gui/TraceUtils.h>
29
30 #include <utils/Trace.h>
31
32 // TODO(b/129481165): remove the #pragma below and fix conversion issues
33 #pragma clang diagnostic push
34 #pragma clang diagnostic ignored "-Wconversion"
35
36 #include "DisplayHardware/HWComposer.h"
37
38 // TODO(b/129481165): remove the #pragma below and fix conversion issues
39 #pragma clang diagnostic pop // ignored "-Wconversion"
40
41 #include "DisplayHardware/PowerAdvisor.h"
42
43 using aidl::android::hardware::graphics::composer3::Capability;
44 using aidl::android::hardware::graphics::composer3::DisplayCapability;
45
46 namespace android::compositionengine::impl {
47
createDisplay(const compositionengine::CompositionEngine & compositionEngine,const compositionengine::DisplayCreationArgs & args)48 std::shared_ptr<Display> createDisplay(
49 const compositionengine::CompositionEngine& compositionEngine,
50 const compositionengine::DisplayCreationArgs& args) {
51 return createDisplayTemplated<Display>(compositionEngine, args);
52 }
53
54 Display::~Display() = default;
55
setConfiguration(const compositionengine::DisplayCreationArgs & args)56 void Display::setConfiguration(const compositionengine::DisplayCreationArgs& args) {
57 mId = args.id;
58 mPowerAdvisor = args.powerAdvisor;
59 editState().isSecure = args.isSecure;
60 editState().displaySpace.setBounds(args.pixels);
61 setName(args.name);
62 }
63
isValid() const64 bool Display::isValid() const {
65 return Output::isValid() && mPowerAdvisor;
66 }
67
getId() const68 DisplayId Display::getId() const {
69 return mId;
70 }
71
isSecure() const72 bool Display::isSecure() const {
73 return getState().isSecure;
74 }
75
isVirtual() const76 bool Display::isVirtual() const {
77 return VirtualDisplayId::tryCast(mId).has_value();
78 }
79
getDisplayId() const80 std::optional<DisplayId> Display::getDisplayId() const {
81 return mId;
82 }
83
disconnect()84 void Display::disconnect() {
85 if (mIsDisconnected) {
86 return;
87 }
88
89 mIsDisconnected = true;
90
91 if (const auto id = HalDisplayId::tryCast(mId)) {
92 getCompositionEngine().getHwComposer().disconnectDisplay(*id);
93 }
94 }
95
setColorTransform(const compositionengine::CompositionRefreshArgs & args)96 void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
97 Output::setColorTransform(args);
98 const auto halDisplayId = HalDisplayId::tryCast(mId);
99 if (mIsDisconnected || !halDisplayId || CC_LIKELY(!args.colorTransformMatrix)) {
100 return;
101 }
102
103 auto& hwc = getCompositionEngine().getHwComposer();
104 status_t result = hwc.setColorTransform(*halDisplayId, *args.colorTransformMatrix);
105 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
106 to_string(mId).c_str(), result);
107 }
108
setColorProfile(const ColorProfile & colorProfile)109 void Display::setColorProfile(const ColorProfile& colorProfile) {
110 const ui::Dataspace targetDataspace =
111 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
112 colorProfile.colorSpaceAgnosticDataspace);
113
114 if (colorProfile.mode == getState().colorMode &&
115 colorProfile.dataspace == getState().dataspace &&
116 colorProfile.renderIntent == getState().renderIntent &&
117 targetDataspace == getState().targetDataspace) {
118 return;
119 }
120
121 if (isVirtual()) {
122 ALOGW("%s: Invalid operation on virtual display", __func__);
123 return;
124 }
125
126 Output::setColorProfile(colorProfile);
127
128 const auto physicalId = PhysicalDisplayId::tryCast(mId);
129 LOG_FATAL_IF(!physicalId);
130 getCompositionEngine().getHwComposer().setActiveColorMode(*physicalId, colorProfile.mode,
131 colorProfile.renderIntent);
132 }
133
dump(std::string & out) const134 void Display::dump(std::string& out) const {
135 const char* const type = isVirtual() ? "virtual" : "physical";
136 base::StringAppendF(&out, "Display %s (%s, \"%s\")", to_string(mId).c_str(), type,
137 getName().c_str());
138
139 out.append("\n Composition Display State:\n");
140 Output::dumpBase(out);
141 }
142
createDisplayColorProfile(const DisplayColorProfileCreationArgs & args)143 void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
144 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
145 }
146
createRenderSurface(const RenderSurfaceCreationArgs & args)147 void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
148 setRenderSurface(
149 compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
150 }
151
createClientCompositionCache(uint32_t cacheSize)152 void Display::createClientCompositionCache(uint32_t cacheSize) {
153 cacheClientCompositionRequests(cacheSize);
154 }
155
createOutputLayer(const sp<compositionengine::LayerFE> & layerFE) const156 std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
157 const sp<compositionengine::LayerFE>& layerFE) const {
158 auto outputLayer = impl::createOutputLayer(*this, layerFE);
159
160 if (const auto halDisplayId = HalDisplayId::tryCast(mId);
161 outputLayer && !mIsDisconnected && halDisplayId) {
162 auto& hwc = getCompositionEngine().getHwComposer();
163 auto hwcLayer = hwc.createLayer(*halDisplayId);
164 ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
165 getName().c_str());
166 outputLayer->setHwcLayer(std::move(hwcLayer));
167 }
168 return outputLayer;
169 }
170
setReleasedLayers(const compositionengine::CompositionRefreshArgs & refreshArgs)171 void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
172 Output::setReleasedLayers(refreshArgs);
173
174 if (mIsDisconnected || GpuVirtualDisplayId::tryCast(mId) ||
175 refreshArgs.layersWithQueuedFrames.empty()) {
176 return;
177 }
178
179 // For layers that are being removed from a HWC display, and that have
180 // queued frames, add them to a a list of released layers so we can properly
181 // set a fence.
182 compositionengine::Output::ReleasedLayers releasedLayers;
183
184 // Any non-null entries in the current list of layers are layers that are no
185 // longer going to be visible
186 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
187 if (!outputLayer) {
188 continue;
189 }
190
191 compositionengine::LayerFE* layerFE = &outputLayer->getLayerFE();
192 const bool hasQueuedFrames =
193 std::any_of(refreshArgs.layersWithQueuedFrames.cbegin(),
194 refreshArgs.layersWithQueuedFrames.cend(),
195 [layerFE](sp<compositionengine::LayerFE> layerWithQueuedFrames) {
196 return layerFE == layerWithQueuedFrames.get();
197 });
198
199 if (hasQueuedFrames) {
200 releasedLayers.emplace_back(wp<LayerFE>::fromExisting(layerFE));
201 }
202 }
203
204 setReleasedLayers(std::move(releasedLayers));
205 }
206
applyDisplayBrightness(const bool applyImmediately)207 void Display::applyDisplayBrightness(const bool applyImmediately) {
208 auto& hwc = getCompositionEngine().getHwComposer();
209 const auto halDisplayId = HalDisplayId::tryCast(*getDisplayId());
210 if (const auto physicalDisplayId = PhysicalDisplayId::tryCast(*halDisplayId);
211 physicalDisplayId && getState().displayBrightness) {
212 const status_t result =
213 hwc.setDisplayBrightness(*physicalDisplayId, *getState().displayBrightness,
214 getState().displayBrightnessNits,
215 Hwc2::Composer::DisplayBrightnessOptions{
216 .applyImmediately = applyImmediately})
217 .get();
218 ALOGE_IF(result != NO_ERROR, "setDisplayBrightness failed for %s: %d, (%s)",
219 getName().c_str(), result, strerror(-result));
220 }
221 // Clear out the display brightness now that it's been communicated to composer.
222 editState().displayBrightness.reset();
223 }
224
beginFrame()225 void Display::beginFrame() {
226 Output::beginFrame();
227
228 // If we don't have a HWC display, then we are done.
229 const auto halDisplayId = HalDisplayId::tryCast(mId);
230 if (!halDisplayId) {
231 return;
232 }
233
234 applyDisplayBrightness(false);
235 }
236
chooseCompositionStrategy(std::optional<android::HWComposer::DeviceRequestedChanges> * outChanges)237 bool Display::chooseCompositionStrategy(
238 std::optional<android::HWComposer::DeviceRequestedChanges>* outChanges) {
239 ATRACE_FORMAT("%s for %s", __func__, getNamePlusId().c_str());
240 ALOGV(__FUNCTION__);
241
242 if (mIsDisconnected) {
243 return false;
244 }
245
246 // If we don't have a HWC display, then we are done.
247 const auto halDisplayId = HalDisplayId::tryCast(mId);
248 if (!halDisplayId) {
249 return false;
250 }
251
252 // Get any composition changes requested by the HWC device, and apply them.
253 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
254 auto& hwc = getCompositionEngine().getHwComposer();
255 const bool requiresClientComposition = anyLayersRequireClientComposition();
256
257 if (isPowerHintSessionEnabled()) {
258 mPowerAdvisor->setRequiresClientComposition(mId, requiresClientComposition);
259 }
260
261 const TimePoint hwcValidateStartTime = TimePoint::now();
262
263 if (status_t result =
264 hwc.getDeviceCompositionChanges(*halDisplayId, requiresClientComposition,
265 getState().earliestPresentTime,
266 getState().expectedPresentTime, outChanges);
267 result != NO_ERROR) {
268 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
269 strerror(-result));
270 return false;
271 }
272
273 if (isPowerHintSessionEnabled()) {
274 mPowerAdvisor->setHwcValidateTiming(mId, hwcValidateStartTime, TimePoint::now());
275 if (auto halDisplayId = HalDisplayId::tryCast(mId)) {
276 mPowerAdvisor->setSkippedValidate(mId, hwc.getValidateSkipped(*halDisplayId));
277 }
278 }
279
280 return true;
281 }
282
applyCompositionStrategy(const std::optional<DeviceRequestedChanges> & changes)283 void Display::applyCompositionStrategy(const std::optional<DeviceRequestedChanges>& changes) {
284 if (changes) {
285 applyChangedTypesToLayers(changes->changedTypes);
286 applyDisplayRequests(changes->displayRequests);
287 applyLayerRequestsToLayers(changes->layerRequests);
288 applyClientTargetRequests(changes->clientTargetProperty);
289 }
290
291 // Determine what type of composition we are doing from the final state
292 auto& state = editState();
293 state.usesClientComposition = anyLayersRequireClientComposition();
294 state.usesDeviceComposition = !allLayersRequireClientComposition();
295 }
296
getSkipColorTransform() const297 bool Display::getSkipColorTransform() const {
298 const auto& hwc = getCompositionEngine().getHwComposer();
299 if (const auto halDisplayId = HalDisplayId::tryCast(mId)) {
300 return hwc.hasDisplayCapability(*halDisplayId,
301 DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM);
302 }
303
304 return hwc.hasCapability(Capability::SKIP_CLIENT_COLOR_TRANSFORM);
305 }
306
allLayersRequireClientComposition() const307 bool Display::allLayersRequireClientComposition() const {
308 const auto layers = getOutputLayersOrderedByZ();
309 return std::all_of(layers.begin(), layers.end(),
310 [](const auto& layer) { return layer->requiresClientComposition(); });
311 }
312
applyChangedTypesToLayers(const ChangedTypes & changedTypes)313 void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
314 if (changedTypes.empty()) {
315 return;
316 }
317
318 for (auto* layer : getOutputLayersOrderedByZ()) {
319 auto hwcLayer = layer->getHwcLayer();
320 if (!hwcLayer) {
321 continue;
322 }
323
324 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
325 layer->applyDeviceCompositionTypeChange(
326 static_cast<aidl::android::hardware::graphics::composer3::Composition>(
327 it->second));
328 }
329 }
330 }
331
applyDisplayRequests(const DisplayRequests & displayRequests)332 void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
333 auto& state = editState();
334 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
335 static_cast<uint32_t>(hal::DisplayRequest::FLIP_CLIENT_TARGET)) != 0;
336 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
337 }
338
applyLayerRequestsToLayers(const LayerRequests & layerRequests)339 void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
340 for (auto* layer : getOutputLayersOrderedByZ()) {
341 layer->prepareForDeviceLayerRequests();
342
343 auto hwcLayer = layer->getHwcLayer();
344 if (!hwcLayer) {
345 continue;
346 }
347
348 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
349 layer->applyDeviceLayerRequest(
350 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
351 }
352 }
353 }
354
applyClientTargetRequests(const ClientTargetProperty & clientTargetProperty)355 void Display::applyClientTargetRequests(const ClientTargetProperty& clientTargetProperty) {
356 if (static_cast<ui::Dataspace>(clientTargetProperty.clientTargetProperty.dataspace) ==
357 ui::Dataspace::UNKNOWN) {
358 return;
359 }
360
361 editState().dataspace =
362 static_cast<ui::Dataspace>(clientTargetProperty.clientTargetProperty.dataspace);
363 editState().clientTargetBrightness = clientTargetProperty.brightness;
364 editState().clientTargetDimmingStage = clientTargetProperty.dimmingStage;
365 getRenderSurface()->setBufferDataspace(editState().dataspace);
366 getRenderSurface()->setBufferPixelFormat(
367 static_cast<ui::PixelFormat>(clientTargetProperty.clientTargetProperty.pixelFormat));
368 }
369
presentAndGetFrameFences()370 compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
371 auto fences = impl::Output::presentAndGetFrameFences();
372
373 const auto halDisplayIdOpt = HalDisplayId::tryCast(mId);
374 if (mIsDisconnected || !halDisplayIdOpt) {
375 return fences;
376 }
377
378 auto& hwc = getCompositionEngine().getHwComposer();
379
380 const TimePoint startTime = TimePoint::now();
381
382 if (isPowerHintSessionEnabled() && getState().earliestPresentTime) {
383 mPowerAdvisor->setHwcPresentDelayedTime(mId, *getState().earliestPresentTime);
384 }
385
386 hwc.presentAndGetReleaseFences(*halDisplayIdOpt, getState().earliestPresentTime);
387
388 if (isPowerHintSessionEnabled()) {
389 mPowerAdvisor->setHwcPresentTiming(mId, startTime, TimePoint::now());
390 }
391
392 fences.presentFence = hwc.getPresentFence(*halDisplayIdOpt);
393
394 // TODO(b/121291683): Change HWComposer call to return entire map
395 for (const auto* layer : getOutputLayersOrderedByZ()) {
396 auto hwcLayer = layer->getHwcLayer();
397 if (!hwcLayer) {
398 continue;
399 }
400
401 fences.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*halDisplayIdOpt, hwcLayer));
402 }
403
404 hwc.clearReleaseFences(*halDisplayIdOpt);
405
406 return fences;
407 }
408
setExpensiveRenderingExpected(bool enabled)409 void Display::setExpensiveRenderingExpected(bool enabled) {
410 Output::setExpensiveRenderingExpected(enabled);
411
412 if (mPowerAdvisor && !GpuVirtualDisplayId::tryCast(mId)) {
413 mPowerAdvisor->setExpensiveRenderingExpected(mId, enabled);
414 }
415 }
416
isPowerHintSessionEnabled()417 bool Display::isPowerHintSessionEnabled() {
418 return mPowerAdvisor != nullptr && mPowerAdvisor->usePowerHintSession();
419 }
420
setHintSessionGpuFence(std::unique_ptr<FenceTime> && gpuFence)421 void Display::setHintSessionGpuFence(std::unique_ptr<FenceTime>&& gpuFence) {
422 mPowerAdvisor->setGpuFenceTime(mId, std::move(gpuFence));
423 }
424
finishFrame(GpuCompositionResult && result)425 void Display::finishFrame(GpuCompositionResult&& result) {
426 // We only need to actually compose the display if:
427 // 1) It is being handled by hardware composer, which may need this to
428 // keep its virtual display state machine in sync, or
429 // 2) There is work to be done (the dirty region isn't empty)
430 if (GpuVirtualDisplayId::tryCast(mId) && !mustRecompose()) {
431 ALOGV("Skipping display composition");
432 return;
433 }
434
435 impl::Output::finishFrame(std::move(result));
436 }
437
438 } // namespace android::compositionengine::impl
439