• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 #include <utils/Trace.h>
30 
31 // TODO(b/129481165): remove the #pragma below and fix conversion issues
32 #pragma clang diagnostic push
33 #pragma clang diagnostic ignored "-Wconversion"
34 
35 #include "DisplayHardware/HWComposer.h"
36 
37 // TODO(b/129481165): remove the #pragma below and fix conversion issues
38 #pragma clang diagnostic pop // ignored "-Wconversion"
39 
40 #include "DisplayHardware/PowerAdvisor.h"
41 
42 namespace android::compositionengine::impl {
43 
createDisplay(const compositionengine::CompositionEngine & compositionEngine,const compositionengine::DisplayCreationArgs & args)44 std::shared_ptr<Display> createDisplay(
45         const compositionengine::CompositionEngine& compositionEngine,
46         const compositionengine::DisplayCreationArgs& args) {
47     return createDisplayTemplated<Display>(compositionEngine, args);
48 }
49 
50 Display::~Display() = default;
51 
setConfiguration(const compositionengine::DisplayCreationArgs & args)52 void Display::setConfiguration(const compositionengine::DisplayCreationArgs& args) {
53     mIsVirtual = !args.physical;
54     mId = args.physical ? std::make_optional(args.physical->id) : std::nullopt;
55     mPowerAdvisor = args.powerAdvisor;
56 
57     editState().isSecure = args.isSecure;
58 
59     setLayerStackFilter(args.layerStackId,
60                         args.physical ? args.physical->type == DisplayConnectionType::Internal
61                                       : false);
62     setName(args.name);
63 
64     if (!args.physical && args.useHwcVirtualDisplays) {
65         mId = maybeAllocateDisplayIdForVirtualDisplay(args.pixels, args.pixelFormat);
66     }
67 }
68 
maybeAllocateDisplayIdForVirtualDisplay(ui::Size pixels,ui::PixelFormat pixelFormat) const69 std::optional<DisplayId> Display::maybeAllocateDisplayIdForVirtualDisplay(
70         ui::Size pixels, ui::PixelFormat pixelFormat) const {
71     auto& hwc = getCompositionEngine().getHwComposer();
72     return hwc.allocateVirtualDisplay(static_cast<uint32_t>(pixels.width),
73                                       static_cast<uint32_t>(pixels.height), &pixelFormat);
74 }
75 
isValid() const76 bool Display::isValid() const {
77     return Output::isValid() && mPowerAdvisor;
78 }
79 
getId() const80 const std::optional<DisplayId>& Display::getId() const {
81     return mId;
82 }
83 
isSecure() const84 bool Display::isSecure() const {
85     return getState().isSecure;
86 }
87 
isVirtual() const88 bool Display::isVirtual() const {
89     return mIsVirtual;
90 }
91 
getDisplayId() const92 std::optional<DisplayId> Display::getDisplayId() const {
93     return mId;
94 }
95 
setDisplayIdForTesting(std::optional<DisplayId> displayId)96 void Display::setDisplayIdForTesting(std::optional<DisplayId> displayId) {
97     mId = displayId;
98 }
99 
disconnect()100 void Display::disconnect() {
101     if (!mId) {
102         return;
103     }
104 
105     auto& hwc = getCompositionEngine().getHwComposer();
106     hwc.disconnectDisplay(*mId);
107     mId.reset();
108 }
109 
setColorTransform(const compositionengine::CompositionRefreshArgs & args)110 void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
111     Output::setColorTransform(args);
112 
113     if (!mId || CC_LIKELY(!args.colorTransformMatrix)) {
114         return;
115     }
116 
117     auto& hwc = getCompositionEngine().getHwComposer();
118     status_t result = hwc.setColorTransform(*mId, *args.colorTransformMatrix);
119     ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
120              mId ? to_string(*mId).c_str() : "", result);
121 }
122 
setColorProfile(const ColorProfile & colorProfile)123 void Display::setColorProfile(const ColorProfile& colorProfile) {
124     const ui::Dataspace targetDataspace =
125             getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
126                                                          colorProfile.colorSpaceAgnosticDataspace);
127 
128     if (colorProfile.mode == getState().colorMode &&
129         colorProfile.dataspace == getState().dataspace &&
130         colorProfile.renderIntent == getState().renderIntent &&
131         targetDataspace == getState().targetDataspace) {
132         return;
133     }
134 
135     if (mIsVirtual) {
136         ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
137         return;
138     }
139 
140     Output::setColorProfile(colorProfile);
141 
142     auto& hwc = getCompositionEngine().getHwComposer();
143     hwc.setActiveColorMode(*mId, colorProfile.mode, colorProfile.renderIntent);
144 }
145 
dump(std::string & out) const146 void Display::dump(std::string& out) const {
147     using android::base::StringAppendF;
148 
149     StringAppendF(&out, "   Composition Display State: [\"%s\"]", getName().c_str());
150 
151     out.append("\n   ");
152 
153     dumpVal(out, "isVirtual", mIsVirtual);
154     if (mId) {
155         dumpVal(out, "hwcId", to_string(*mId));
156     } else {
157         StringAppendF(&out, "no hwcId, ");
158     }
159 
160     out.append("\n");
161 
162     Output::dumpBase(out);
163 }
164 
createDisplayColorProfile(const DisplayColorProfileCreationArgs & args)165 void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
166     setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
167 }
168 
createRenderSurface(const RenderSurfaceCreationArgs & args)169 void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
170     setRenderSurface(
171             compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
172 }
173 
createClientCompositionCache(uint32_t cacheSize)174 void Display::createClientCompositionCache(uint32_t cacheSize) {
175     cacheClientCompositionRequests(cacheSize);
176 }
177 
createOutputLayer(const sp<compositionengine::LayerFE> & layerFE) const178 std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
179         const sp<compositionengine::LayerFE>& layerFE) const {
180     auto result = impl::createOutputLayer(*this, layerFE);
181 
182     if (result && mId) {
183         auto& hwc = getCompositionEngine().getHwComposer();
184         auto displayId = *mId;
185         // Note: For the moment we ensure it is safe to take a reference to the
186         // HWComposer implementation by destroying all the OutputLayers (and
187         // hence the HWC2::Layers they own) before setting a new HWComposer. See
188         // for example SurfaceFlinger::updateVrFlinger().
189         // TODO(b/121291683): Make this safer.
190         auto hwcLayer = std::shared_ptr<HWC2::Layer>(hwc.createLayer(displayId),
191                                                      [&hwc, displayId](HWC2::Layer* layer) {
192                                                          hwc.destroyLayer(displayId, layer);
193                                                      });
194         ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
195                  getName().c_str());
196         result->setHwcLayer(std::move(hwcLayer));
197     }
198     return result;
199 }
200 
setReleasedLayers(const compositionengine::CompositionRefreshArgs & refreshArgs)201 void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
202     Output::setReleasedLayers(refreshArgs);
203 
204     if (!mId || refreshArgs.layersWithQueuedFrames.empty()) {
205         return;
206     }
207 
208     // For layers that are being removed from a HWC display, and that have
209     // queued frames, add them to a a list of released layers so we can properly
210     // set a fence.
211     compositionengine::Output::ReleasedLayers releasedLayers;
212 
213     // Any non-null entries in the current list of layers are layers that are no
214     // longer going to be visible
215     for (auto* outputLayer : getOutputLayersOrderedByZ()) {
216         if (!outputLayer) {
217             continue;
218         }
219 
220         compositionengine::LayerFE* layerFE = &outputLayer->getLayerFE();
221         const bool hasQueuedFrames =
222                 std::any_of(refreshArgs.layersWithQueuedFrames.cbegin(),
223                             refreshArgs.layersWithQueuedFrames.cend(),
224                             [layerFE](sp<compositionengine::LayerFE> layerWithQueuedFrames) {
225                                 return layerFE == layerWithQueuedFrames.get();
226                             });
227 
228         if (hasQueuedFrames) {
229             releasedLayers.emplace_back(layerFE);
230         }
231     }
232 
233     setReleasedLayers(std::move(releasedLayers));
234 }
235 
chooseCompositionStrategy()236 void Display::chooseCompositionStrategy() {
237     ATRACE_CALL();
238     ALOGV(__FUNCTION__);
239 
240     // Default to the base settings -- client composition only.
241     Output::chooseCompositionStrategy();
242 
243     // If we don't have a HWC display, then we are done
244     if (!mId) {
245         return;
246     }
247 
248     // Get any composition changes requested by the HWC device, and apply them.
249     std::optional<android::HWComposer::DeviceRequestedChanges> changes;
250     auto& hwc = getCompositionEngine().getHwComposer();
251     if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
252                                                           &changes);
253         result != NO_ERROR) {
254         ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
255               strerror(-result));
256         return;
257     }
258     if (changes) {
259         applyChangedTypesToLayers(changes->changedTypes);
260         applyDisplayRequests(changes->displayRequests);
261         applyLayerRequestsToLayers(changes->layerRequests);
262         applyClientTargetRequests(changes->clientTargetProperty);
263     }
264 
265     // Determine what type of composition we are doing from the final state
266     auto& state = editState();
267     state.usesClientComposition = anyLayersRequireClientComposition();
268     state.usesDeviceComposition = !allLayersRequireClientComposition();
269 }
270 
getSkipColorTransform() const271 bool Display::getSkipColorTransform() const {
272     const auto& hwc = getCompositionEngine().getHwComposer();
273     return mId ? hwc.hasDisplayCapability(*mId, hal::DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM)
274                : hwc.hasCapability(hal::Capability::SKIP_CLIENT_COLOR_TRANSFORM);
275 }
276 
anyLayersRequireClientComposition() const277 bool Display::anyLayersRequireClientComposition() const {
278     const auto layers = getOutputLayersOrderedByZ();
279     return std::any_of(layers.begin(), layers.end(),
280                        [](const auto& layer) { return layer->requiresClientComposition(); });
281 }
282 
allLayersRequireClientComposition() const283 bool Display::allLayersRequireClientComposition() const {
284     const auto layers = getOutputLayersOrderedByZ();
285     return std::all_of(layers.begin(), layers.end(),
286                        [](const auto& layer) { return layer->requiresClientComposition(); });
287 }
288 
applyChangedTypesToLayers(const ChangedTypes & changedTypes)289 void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
290     if (changedTypes.empty()) {
291         return;
292     }
293 
294     for (auto* layer : getOutputLayersOrderedByZ()) {
295         auto hwcLayer = layer->getHwcLayer();
296         if (!hwcLayer) {
297             continue;
298         }
299 
300         if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
301             layer->applyDeviceCompositionTypeChange(
302                     static_cast<Hwc2::IComposerClient::Composition>(it->second));
303         }
304     }
305 }
306 
applyDisplayRequests(const DisplayRequests & displayRequests)307 void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
308     auto& state = editState();
309     state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
310                               static_cast<uint32_t>(hal::DisplayRequest::FLIP_CLIENT_TARGET)) != 0;
311     // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
312 }
313 
applyLayerRequestsToLayers(const LayerRequests & layerRequests)314 void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
315     for (auto* layer : getOutputLayersOrderedByZ()) {
316         layer->prepareForDeviceLayerRequests();
317 
318         auto hwcLayer = layer->getHwcLayer();
319         if (!hwcLayer) {
320             continue;
321         }
322 
323         if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
324             layer->applyDeviceLayerRequest(
325                     static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
326         }
327     }
328 }
329 
applyClientTargetRequests(const ClientTargetProperty & clientTargetProperty)330 void Display::applyClientTargetRequests(const ClientTargetProperty& clientTargetProperty) {
331     if (clientTargetProperty.dataspace == ui::Dataspace::UNKNOWN) {
332         return;
333     }
334     auto outputState = editState();
335     outputState.dataspace = clientTargetProperty.dataspace;
336     getRenderSurface()->setBufferDataspace(clientTargetProperty.dataspace);
337     getRenderSurface()->setBufferPixelFormat(clientTargetProperty.pixelFormat);
338 }
339 
presentAndGetFrameFences()340 compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
341     auto result = impl::Output::presentAndGetFrameFences();
342 
343     if (!mId) {
344         return result;
345     }
346 
347     auto& hwc = getCompositionEngine().getHwComposer();
348     hwc.presentAndGetReleaseFences(*mId);
349 
350     result.presentFence = hwc.getPresentFence(*mId);
351 
352     // TODO(b/121291683): Change HWComposer call to return entire map
353     for (const auto* layer : getOutputLayersOrderedByZ()) {
354         auto hwcLayer = layer->getHwcLayer();
355         if (!hwcLayer) {
356             continue;
357         }
358 
359         result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
360     }
361 
362     hwc.clearReleaseFences(*mId);
363 
364     return result;
365 }
366 
setExpensiveRenderingExpected(bool enabled)367 void Display::setExpensiveRenderingExpected(bool enabled) {
368     Output::setExpensiveRenderingExpected(enabled);
369 
370     if (mPowerAdvisor && mId) {
371         mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled);
372     }
373 }
374 
finishFrame(const compositionengine::CompositionRefreshArgs & refreshArgs)375 void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
376     // We only need to actually compose the display if:
377     // 1) It is being handled by hardware composer, which may need this to
378     //    keep its virtual display state machine in sync, or
379     // 2) There is work to be done (the dirty region isn't empty)
380     if (!mId) {
381         if (getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) {
382             ALOGV("Skipping display composition");
383             return;
384         }
385     }
386 
387     impl::Output::finishFrame(refreshArgs);
388 }
389 
390 } // namespace android::compositionengine::impl
391