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().isProtected = args.isProtected;
61     editState().displaySpace.setBounds(args.pixels);
62     setName(args.name);
63 }
64 
isValid() const65 bool Display::isValid() const {
66     return Output::isValid() && mPowerAdvisor;
67 }
68 
getId() const69 DisplayId Display::getId() const {
70     return mId;
71 }
72 
isSecure() const73 bool Display::isSecure() const {
74     return getState().isSecure;
75 }
76 
setSecure(bool secure)77 void Display::setSecure(bool secure) {
78     editState().isSecure = secure;
79 }
80 
isVirtual() const81 bool Display::isVirtual() const {
82     return mId.isVirtual();
83 }
84 
getDisplayId() const85 std::optional<DisplayId> Display::getDisplayId() const {
86     return mId;
87 }
88 
disconnect()89 void Display::disconnect() {
90     if (mIsDisconnected) {
91         return;
92     }
93 
94     mIsDisconnected = true;
95 
96     if (const auto id = HalDisplayId::tryCast(mId)) {
97         getCompositionEngine().getHwComposer().disconnectDisplay(*id);
98     }
99 }
100 
setColorTransform(const compositionengine::CompositionRefreshArgs & args)101 void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
102     Output::setColorTransform(args);
103     const auto halDisplayId = HalDisplayId::tryCast(mId);
104     if (mIsDisconnected || !halDisplayId || CC_LIKELY(!args.colorTransformMatrix)) {
105         return;
106     }
107 
108     auto& hwc = getCompositionEngine().getHwComposer();
109     status_t result = hwc.setColorTransform(*halDisplayId, *args.colorTransformMatrix);
110     ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
111              to_string(mId).c_str(), result);
112 }
113 
setColorProfile(const ColorProfile & colorProfile)114 void Display::setColorProfile(const ColorProfile& colorProfile) {
115     if (colorProfile.mode == getState().colorMode &&
116         colorProfile.dataspace == getState().dataspace &&
117         colorProfile.renderIntent == getState().renderIntent) {
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(bool applyImmediately)207 void Display::applyDisplayBrightness(bool applyImmediately) {
208     if (const auto displayId = ftl::Optional(getDisplayId()).and_then(PhysicalDisplayId::tryCast);
209         displayId && getState().displayBrightness) {
210         auto& hwc = getCompositionEngine().getHwComposer();
211         const status_t result =
212                 hwc.setDisplayBrightness(*displayId, *getState().displayBrightness,
213                                          getState().displayBrightnessNits,
214                                          Hwc2::Composer::DisplayBrightnessOptions{
215                                                  .applyImmediately = applyImmediately})
216                         .get();
217         ALOGE_IF(result != NO_ERROR, "setDisplayBrightness failed for %s: %d, (%s)",
218                  getName().c_str(), result, strerror(-result));
219     }
220     // Clear out the display brightness now that it's been communicated to composer.
221     editState().displayBrightness.reset();
222 }
223 
beginFrame()224 void Display::beginFrame() {
225     Output::beginFrame();
226 
227     // If we don't have a HWC display, then we are done.
228     const auto halDisplayId = HalDisplayId::tryCast(mId);
229     if (!halDisplayId) {
230         return;
231     }
232 
233     applyDisplayBrightness(false);
234 }
235 
chooseCompositionStrategy(std::optional<android::HWComposer::DeviceRequestedChanges> * outChanges)236 bool Display::chooseCompositionStrategy(
237         std::optional<android::HWComposer::DeviceRequestedChanges>* outChanges) {
238     ATRACE_FORMAT("%s for %s", __func__, getNamePlusId().c_str());
239     ALOGV(__FUNCTION__);
240 
241     if (mIsDisconnected) {
242         return false;
243     }
244 
245     // If we don't have a HWC display, then we are done.
246     const auto halDisplayId = HalDisplayId::tryCast(mId);
247     if (!halDisplayId) {
248         return false;
249     }
250 
251     // Get any composition changes requested by the HWC device, and apply them.
252     auto& hwc = getCompositionEngine().getHwComposer();
253     const bool requiresClientComposition = anyLayersRequireClientComposition();
254 
255     const TimePoint hwcValidateStartTime = TimePoint::now();
256 
257     if (status_t result = hwc.getDeviceCompositionChanges(*halDisplayId, requiresClientComposition,
258                                                           getState().earliestPresentTime,
259                                                           getState().expectedPresentTime,
260                                                           getState().frameInterval, outChanges);
261         result != NO_ERROR) {
262         ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
263               strerror(-result));
264         return false;
265     }
266 
267     if (isPowerHintSessionEnabled()) {
268         mPowerAdvisor->setHwcValidateTiming(mId, hwcValidateStartTime, TimePoint::now());
269         if (auto halDisplayId = HalDisplayId::tryCast(mId)) {
270             mPowerAdvisor->setSkippedValidate(mId, hwc.getValidateSkipped(*halDisplayId));
271         }
272     }
273 
274     return true;
275 }
276 
applyCompositionStrategy(const std::optional<DeviceRequestedChanges> & changes)277 void Display::applyCompositionStrategy(const std::optional<DeviceRequestedChanges>& changes) {
278     if (changes) {
279         applyChangedTypesToLayers(changes->changedTypes);
280         applyDisplayRequests(changes->displayRequests);
281         applyLayerRequestsToLayers(changes->layerRequests);
282         applyClientTargetRequests(changes->clientTargetProperty);
283     }
284 
285     // Determine what type of composition we are doing from the final state
286     auto& state = editState();
287     state.usesClientComposition = anyLayersRequireClientComposition();
288     state.usesDeviceComposition = !allLayersRequireClientComposition();
289 }
290 
getSkipColorTransform() const291 bool Display::getSkipColorTransform() const {
292     const auto& hwc = getCompositionEngine().getHwComposer();
293     if (const auto halDisplayId = HalDisplayId::tryCast(mId)) {
294         return hwc.hasDisplayCapability(*halDisplayId,
295                                         DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM);
296     }
297 
298     return hwc.hasCapability(Capability::SKIP_CLIENT_COLOR_TRANSFORM);
299 }
300 
allLayersRequireClientComposition() const301 bool Display::allLayersRequireClientComposition() const {
302     const auto layers = getOutputLayersOrderedByZ();
303     return std::all_of(layers.begin(), layers.end(),
304                        [](const auto& layer) { return layer->requiresClientComposition(); });
305 }
306 
applyChangedTypesToLayers(const ChangedTypes & changedTypes)307 void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
308     if (changedTypes.empty()) {
309         return;
310     }
311 
312     for (auto* layer : getOutputLayersOrderedByZ()) {
313         auto hwcLayer = layer->getHwcLayer();
314         if (!hwcLayer) {
315             continue;
316         }
317 
318         if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
319             layer->applyDeviceCompositionTypeChange(
320                     static_cast<aidl::android::hardware::graphics::composer3::Composition>(
321                             it->second));
322         }
323     }
324 }
325 
applyDisplayRequests(const DisplayRequests & displayRequests)326 void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
327     auto& state = editState();
328     state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
329                               static_cast<uint32_t>(hal::DisplayRequest::FLIP_CLIENT_TARGET)) != 0;
330     // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
331 }
332 
applyLayerRequestsToLayers(const LayerRequests & layerRequests)333 void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
334     for (auto* layer : getOutputLayersOrderedByZ()) {
335         layer->prepareForDeviceLayerRequests();
336 
337         auto hwcLayer = layer->getHwcLayer();
338         if (!hwcLayer) {
339             continue;
340         }
341 
342         if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
343             layer->applyDeviceLayerRequest(
344                     static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
345         }
346     }
347 }
348 
applyClientTargetRequests(const ClientTargetProperty & clientTargetProperty)349 void Display::applyClientTargetRequests(const ClientTargetProperty& clientTargetProperty) {
350     if (static_cast<ui::Dataspace>(clientTargetProperty.clientTargetProperty.dataspace) ==
351         ui::Dataspace::UNKNOWN) {
352         return;
353     }
354 
355     editState().dataspace =
356             static_cast<ui::Dataspace>(clientTargetProperty.clientTargetProperty.dataspace);
357     editState().clientTargetBrightness = clientTargetProperty.brightness;
358     editState().clientTargetDimmingStage = clientTargetProperty.dimmingStage;
359     getRenderSurface()->setBufferDataspace(editState().dataspace);
360     getRenderSurface()->setBufferPixelFormat(
361             static_cast<ui::PixelFormat>(clientTargetProperty.clientTargetProperty.pixelFormat));
362 }
363 
executeCommands()364 void Display::executeCommands() {
365     const auto halDisplayIdOpt = HalDisplayId::tryCast(mId);
366     if (mIsDisconnected || !halDisplayIdOpt) {
367         return;
368     }
369 
370     getCompositionEngine().getHwComposer().executeCommands(*halDisplayIdOpt);
371 }
372 
presentFrame()373 compositionengine::Output::FrameFences Display::presentFrame() {
374     auto fences = impl::Output::presentFrame();
375 
376     const auto halDisplayIdOpt = HalDisplayId::tryCast(mId);
377     if (mIsDisconnected || !halDisplayIdOpt) {
378         return fences;
379     }
380 
381     auto& hwc = getCompositionEngine().getHwComposer();
382 
383     const TimePoint startTime = TimePoint::now();
384 
385     if (isPowerHintSessionEnabled() && getState().earliestPresentTime) {
386         mPowerAdvisor->setHwcPresentDelayedTime(mId, *getState().earliestPresentTime);
387     }
388 
389     hwc.presentAndGetReleaseFences(*halDisplayIdOpt, getState().earliestPresentTime);
390 
391     if (isPowerHintSessionEnabled()) {
392         mPowerAdvisor->setHwcPresentTiming(mId, startTime, TimePoint::now());
393     }
394 
395     fences.presentFence = hwc.getPresentFence(*halDisplayIdOpt);
396 
397     // TODO(b/121291683): Change HWComposer call to return entire map
398     for (const auto* layer : getOutputLayersOrderedByZ()) {
399         auto hwcLayer = layer->getHwcLayer();
400         if (!hwcLayer) {
401             continue;
402         }
403 
404         fences.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*halDisplayIdOpt, hwcLayer));
405     }
406 
407     hwc.clearReleaseFences(*halDisplayIdOpt);
408 
409     return fences;
410 }
411 
setExpensiveRenderingExpected(bool enabled)412 void Display::setExpensiveRenderingExpected(bool enabled) {
413     Output::setExpensiveRenderingExpected(enabled);
414 
415     if (mPowerAdvisor && !GpuVirtualDisplayId::tryCast(mId)) {
416         mPowerAdvisor->setExpensiveRenderingExpected(mId, enabled);
417     }
418 }
419 
isPowerHintSessionEnabled()420 bool Display::isPowerHintSessionEnabled() {
421     return mPowerAdvisor != nullptr && mPowerAdvisor->usePowerHintSession();
422 }
423 
isPowerHintSessionGpuReportingEnabled()424 bool Display::isPowerHintSessionGpuReportingEnabled() {
425     return mPowerAdvisor != nullptr && mPowerAdvisor->supportsGpuReporting();
426 }
427 
428 // For ADPF GPU v0 this is expected to set start time to when the GPU commands are submitted with
429 // fence returned, i.e. when RenderEngine flushes the commands and returns the draw fence.
setHintSessionGpuStart(TimePoint startTime)430 void Display::setHintSessionGpuStart(TimePoint startTime) {
431     mPowerAdvisor->setGpuStartTime(mId, startTime);
432 }
433 
setHintSessionGpuFence(std::unique_ptr<FenceTime> && gpuFence)434 void Display::setHintSessionGpuFence(std::unique_ptr<FenceTime>&& gpuFence) {
435     mPowerAdvisor->setGpuFenceTime(mId, std::move(gpuFence));
436 }
437 
setHintSessionRequiresRenderEngine(bool requiresRenderEngine)438 void Display::setHintSessionRequiresRenderEngine(bool requiresRenderEngine) {
439     mPowerAdvisor->setRequiresRenderEngine(mId, requiresRenderEngine);
440 }
441 
finishFrame(GpuCompositionResult && result)442 void Display::finishFrame(GpuCompositionResult&& result) {
443     // We only need to actually compose the display if:
444     // 1) It is being handled by hardware composer, which may need this to
445     //    keep its virtual display state machine in sync, or
446     // 2) There is work to be done (the dirty region isn't empty)
447     if (GpuVirtualDisplayId::tryCast(mId) && !mustRecompose()) {
448         ALOGV("Skipping display composition");
449         return;
450     }
451 
452     impl::Output::finishFrame(std::move(result));
453 }
454 
supportsOffloadPresent() const455 bool Display::supportsOffloadPresent() const {
456     if (const auto halDisplayId = HalDisplayId::tryCast(mId)) {
457         const auto& hwc = getCompositionEngine().getHwComposer();
458         return hwc.hasDisplayCapability(*halDisplayId, DisplayCapability::MULTI_THREADED_PRESENT);
459     }
460 
461     return false;
462 }
463 
464 } // namespace android::compositionengine::impl
465