blob: e885629c4b70b41835b81961f9593980fc060812 [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
Lloyd Pique32cbe282018-10-19 13:09:22 -070017#include <android-base/stringprintf.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070018#include <compositionengine/CompositionEngine.h>
Lloyd Piqued3d69882019-02-28 16:03:46 -080019#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070020#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070021#include <compositionengine/DisplaySurface.h>
Lloyd Piquedf336d92019-03-07 21:38:42 -080022#include <compositionengine/LayerFE.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070023#include <compositionengine/impl/Display.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070024#include <compositionengine/impl/DisplayColorProfile.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070025#include <compositionengine/impl/DumpHelpers.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080026#include <compositionengine/impl/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070027#include <compositionengine/impl/RenderSurface.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080028#include <utils/Trace.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070029
30#include "DisplayHardware/HWComposer.h"
Lloyd Pique688abd42019-02-15 15:42:24 -080031#include "DisplayHardware/PowerAdvisor.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070032
33namespace android::compositionengine::impl {
34
Lloyd Pique35fca9d2019-02-13 14:24:11 -080035std::shared_ptr<Display> createDisplay(
Lloyd Pique45a165a2018-10-19 11:54:47 -070036 const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070037 const compositionengine::DisplayCreationArgs& args) {
38 return createDisplayTemplated<Display>(compositionEngine, args);
Lloyd Pique45a165a2018-10-19 11:54:47 -070039}
40
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070041Display::Display(const DisplayCreationArgs& args)
42 : mIsVirtual(args.isVirtual), mId(args.displayId), mPowerAdvisor(args.powerAdvisor) {}
Lloyd Pique45a165a2018-10-19 11:54:47 -070043
44Display::~Display() = default;
45
46const std::optional<DisplayId>& Display::getId() const {
47 return mId;
48}
49
50bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070051 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070052}
53
54bool Display::isVirtual() const {
55 return mIsVirtual;
56}
57
Lloyd Pique6c564cf2019-05-17 17:31:36 -070058std::optional<DisplayId> Display::getDisplayId() const {
59 return mId;
60}
61
Lloyd Pique45a165a2018-10-19 11:54:47 -070062void Display::disconnect() {
63 if (!mId) {
64 return;
65 }
66
Lloyd Pique32cbe282018-10-19 13:09:22 -070067 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique45a165a2018-10-19 11:54:47 -070068 hwc.disconnectDisplay(*mId);
69 mId.reset();
70}
71
Lloyd Pique3eb1b212019-03-07 21:15:40 -080072void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
73 Output::setColorTransform(args);
74
75 if (!mId || CC_LIKELY(!args.colorTransformMatrix)) {
76 return;
77 }
Lloyd Pique32cbe282018-10-19 13:09:22 -070078
79 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique3eb1b212019-03-07 21:15:40 -080080 status_t result = hwc.setColorTransform(*mId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -070081 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
82 mId ? to_string(*mId).c_str() : "", result);
83}
84
Lloyd Pique6a3b4462019-03-07 20:58:12 -080085void Display::setColorProfile(const ColorProfile& colorProfile) {
86 const ui::Dataspace targetDataspace =
87 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
88 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -080089
Lloyd Pique6a3b4462019-03-07 20:58:12 -080090 if (colorProfile.mode == getState().colorMode &&
91 colorProfile.dataspace == getState().dataspace &&
92 colorProfile.renderIntent == getState().renderIntent &&
93 targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070094 return;
95 }
96
97 if (mIsVirtual) {
98 ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
99 return;
100 }
101
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800102 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700103
104 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800105 hwc.setActiveColorMode(*mId, colorProfile.mode, colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700106}
107
108void Display::dump(std::string& out) const {
109 using android::base::StringAppendF;
110
111 StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str());
112
113 out.append("\n ");
114
115 dumpVal(out, "isVirtual", mIsVirtual);
116 if (mId) {
117 dumpVal(out, "hwcId", to_string(*mId));
118 } else {
119 StringAppendF(&out, "no hwcId, ");
120 }
121
122 out.append("\n");
123
124 Output::dumpBase(out);
125}
126
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700127void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
128 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700129}
130
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700131void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
132 setRenderSurface(
133 compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700134}
135
Lloyd Piquedf336d92019-03-07 21:38:42 -0800136std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
137 const std::shared_ptr<compositionengine::Layer>& layer,
138 const sp<compositionengine::LayerFE>& layerFE) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700139 auto result = impl::createOutputLayer(*this, layer, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800140
141 if (result && mId) {
142 auto& hwc = getCompositionEngine().getHwComposer();
143 auto displayId = *mId;
144 // Note: For the moment we ensure it is safe to take a reference to the
145 // HWComposer implementation by destroying all the OutputLayers (and
146 // hence the HWC2::Layers they own) before setting a new HWComposer. See
147 // for example SurfaceFlinger::updateVrFlinger().
148 // TODO(b/121291683): Make this safer.
149 auto hwcLayer = std::shared_ptr<HWC2::Layer>(hwc.createLayer(displayId),
150 [&hwc, displayId](HWC2::Layer* layer) {
151 hwc.destroyLayer(displayId, layer);
152 });
153 ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
154 getName().c_str());
155 result->setHwcLayer(std::move(hwcLayer));
156 }
157 return result;
158}
159
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800160void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
161 Output::setReleasedLayers(refreshArgs);
162
163 if (!mId || refreshArgs.layersWithQueuedFrames.empty()) {
164 return;
165 }
166
167 // For layers that are being removed from a HWC display, and that have
168 // queued frames, add them to a a list of released layers so we can properly
169 // set a fence.
170 compositionengine::Output::ReleasedLayers releasedLayers;
171
172 // Any non-null entries in the current list of layers are layers that are no
173 // longer going to be visible
Lloyd Pique01c77c12019-04-17 12:48:32 -0700174 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800175 if (!layer) {
176 continue;
177 }
178
179 sp<compositionengine::LayerFE> layerFE(&layer->getLayerFE());
180 const bool hasQueuedFrames =
181 std::find(refreshArgs.layersWithQueuedFrames.cbegin(),
182 refreshArgs.layersWithQueuedFrames.cend(),
183 &layer->getLayer()) != refreshArgs.layersWithQueuedFrames.cend();
184
185 if (hasQueuedFrames) {
186 releasedLayers.emplace_back(layerFE);
187 }
188 }
189
190 setReleasedLayers(std::move(releasedLayers));
191}
192
Lloyd Pique66d68602019-02-13 14:23:31 -0800193void Display::chooseCompositionStrategy() {
194 ATRACE_CALL();
195 ALOGV(__FUNCTION__);
196
197 // Default to the base settings -- client composition only.
198 Output::chooseCompositionStrategy();
199
200 // If we don't have a HWC display, then we are done
201 if (!mId) {
202 return;
203 }
204
205 // Get any composition changes requested by the HWC device, and apply them.
206 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
207 auto& hwc = getCompositionEngine().getHwComposer();
208 if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
209 &changes);
210 result != NO_ERROR) {
211 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
212 strerror(-result));
213 return;
214 }
215 if (changes) {
216 applyChangedTypesToLayers(changes->changedTypes);
217 applyDisplayRequests(changes->displayRequests);
218 applyLayerRequestsToLayers(changes->layerRequests);
219 }
220
221 // Determine what type of composition we are doing from the final state
222 auto& state = editState();
223 state.usesClientComposition = anyLayersRequireClientComposition();
224 state.usesDeviceComposition = !allLayersRequireClientComposition();
225}
226
Lloyd Pique688abd42019-02-15 15:42:24 -0800227bool Display::getSkipColorTransform() const {
228 if (!mId) {
229 return false;
230 }
231
232 auto& hwc = getCompositionEngine().getHwComposer();
233 return hwc.hasDisplayCapability(*mId, HWC2::DisplayCapability::SkipClientColorTransform);
234}
235
Lloyd Pique66d68602019-02-13 14:23:31 -0800236bool Display::anyLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700237 const auto layers = getOutputLayersOrderedByZ();
238 return std::any_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800239 [](const auto& layer) { return layer->requiresClientComposition(); });
240}
241
242bool Display::allLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700243 const auto layers = getOutputLayersOrderedByZ();
244 return std::all_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800245 [](const auto& layer) { return layer->requiresClientComposition(); });
246}
247
248void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
249 if (changedTypes.empty()) {
250 return;
251 }
252
Lloyd Pique01c77c12019-04-17 12:48:32 -0700253 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800254 auto hwcLayer = layer->getHwcLayer();
255 if (!hwcLayer) {
256 continue;
257 }
258
259 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
260 layer->applyDeviceCompositionTypeChange(
261 static_cast<Hwc2::IComposerClient::Composition>(it->second));
262 }
263 }
264}
265
266void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
267 auto& state = editState();
268 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
269 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0;
270 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
271}
272
273void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700274 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800275 layer->prepareForDeviceLayerRequests();
276
277 auto hwcLayer = layer->getHwcLayer();
278 if (!hwcLayer) {
279 continue;
280 }
281
282 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
283 layer->applyDeviceLayerRequest(
284 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
285 }
286 }
287}
288
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800289compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
290 auto result = impl::Output::presentAndGetFrameFences();
291
292 if (!mId) {
293 return result;
294 }
295
296 auto& hwc = getCompositionEngine().getHwComposer();
297 hwc.presentAndGetReleaseFences(*mId);
298
299 result.presentFence = hwc.getPresentFence(*mId);
300
301 // TODO(b/121291683): Change HWComposer call to return entire map
Lloyd Pique01c77c12019-04-17 12:48:32 -0700302 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800303 auto hwcLayer = layer->getHwcLayer();
304 if (!hwcLayer) {
305 continue;
306 }
307
308 result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
309 }
310
311 hwc.clearReleaseFences(*mId);
312
313 return result;
314}
315
Lloyd Pique688abd42019-02-15 15:42:24 -0800316void Display::setExpensiveRenderingExpected(bool enabled) {
317 Output::setExpensiveRenderingExpected(enabled);
318
319 if (mPowerAdvisor && mId) {
320 mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled);
321 }
322}
323
Lloyd Piqued3d69882019-02-28 16:03:46 -0800324void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
325 // We only need to actually compose the display if:
326 // 1) It is being handled by hardware composer, which may need this to
327 // keep its virtual display state machine in sync, or
328 // 2) There is work to be done (the dirty region isn't empty)
329 if (!mId) {
330 if (getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) {
331 ALOGV("Skipping display composition");
332 return;
333 }
334 }
335
336 impl::Output::finishFrame(refreshArgs);
337}
338
Lloyd Pique45a165a2018-10-19 11:54:47 -0700339} // namespace android::compositionengine::impl