blob: 308ec5ae1323ea8660865f5f5d5eb8e635018061 [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 Pique3b5a69e2020-01-16 17:51:01 -080028
Lloyd Pique66d68602019-02-13 14:23:31 -080029#include <utils/Trace.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070030
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080031// TODO(b/129481165): remove the #pragma below and fix conversion issues
32#pragma clang diagnostic push
33#pragma clang diagnostic ignored "-Wconversion"
34
Lloyd Pique45a165a2018-10-19 11:54:47 -070035#include "DisplayHardware/HWComposer.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080036
37// TODO(b/129481165): remove the #pragma below and fix conversion issues
38#pragma clang diagnostic pop // ignored "-Wconversion"
39
Lloyd Pique688abd42019-02-15 15:42:24 -080040#include "DisplayHardware/PowerAdvisor.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070041
42namespace android::compositionengine::impl {
43
Lloyd Pique35fca9d2019-02-13 14:24:11 -080044std::shared_ptr<Display> createDisplay(
Lloyd Pique45a165a2018-10-19 11:54:47 -070045 const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070046 const compositionengine::DisplayCreationArgs& args) {
47 return createDisplayTemplated<Display>(compositionEngine, args);
Lloyd Pique45a165a2018-10-19 11:54:47 -070048}
49
Lloyd Pique45a165a2018-10-19 11:54:47 -070050Display::~Display() = default;
51
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070052void 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
69std::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
76bool Display::isValid() const {
77 return Output::isValid() && mPowerAdvisor;
78}
79
Lloyd Pique45a165a2018-10-19 11:54:47 -070080const std::optional<DisplayId>& Display::getId() const {
81 return mId;
82}
83
84bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070085 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070086}
87
88bool Display::isVirtual() const {
89 return mIsVirtual;
90}
91
Lloyd Pique6c564cf2019-05-17 17:31:36 -070092std::optional<DisplayId> Display::getDisplayId() const {
93 return mId;
94}
95
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070096void Display::setDisplayIdForTesting(std::optional<DisplayId> displayId) {
97 mId = displayId;
98}
99
Lloyd Pique45a165a2018-10-19 11:54:47 -0700100void Display::disconnect() {
101 if (!mId) {
102 return;
103 }
104
Lloyd Pique32cbe282018-10-19 13:09:22 -0700105 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique45a165a2018-10-19 11:54:47 -0700106 hwc.disconnectDisplay(*mId);
107 mId.reset();
108}
109
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800110void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
111 Output::setColorTransform(args);
112
113 if (!mId || CC_LIKELY(!args.colorTransformMatrix)) {
114 return;
115 }
Lloyd Pique32cbe282018-10-19 13:09:22 -0700116
117 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800118 status_t result = hwc.setColorTransform(*mId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700119 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
120 mId ? to_string(*mId).c_str() : "", result);
121}
122
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800123void Display::setColorProfile(const ColorProfile& colorProfile) {
124 const ui::Dataspace targetDataspace =
125 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
126 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800127
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800128 if (colorProfile.mode == getState().colorMode &&
129 colorProfile.dataspace == getState().dataspace &&
130 colorProfile.renderIntent == getState().renderIntent &&
131 targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700132 return;
133 }
134
135 if (mIsVirtual) {
136 ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
137 return;
138 }
139
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800140 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700141
142 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800143 hwc.setActiveColorMode(*mId, colorProfile.mode, colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700144}
145
146void 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
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700165void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
166 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700167}
168
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700169void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
170 setRenderSurface(
171 compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700172}
173
Vishnu Nair9b079a22020-01-21 14:36:08 -0800174void Display::createClientCompositionCache(uint32_t cacheSize) {
175 cacheClientCompositionRequests(cacheSize);
176}
177
Lloyd Piquedf336d92019-03-07 21:38:42 -0800178std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
Lloyd Piquedf336d92019-03-07 21:38:42 -0800179 const sp<compositionengine::LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800180 auto result = impl::createOutputLayer(*this, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800181
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
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800201void 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
Lloyd Piquede196652020-01-22 17:29:58 -0800215 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
216 if (!outputLayer) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800217 continue;
218 }
219
Lloyd Piquede196652020-01-22 17:29:58 -0800220 compositionengine::LayerFE* layerFE = &outputLayer->getLayerFE();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800221 const bool hasQueuedFrames =
Lloyd Piquede196652020-01-22 17:29:58 -0800222 std::any_of(refreshArgs.layersWithQueuedFrames.cbegin(),
223 refreshArgs.layersWithQueuedFrames.cend(),
224 [layerFE](sp<compositionengine::LayerFE> layerWithQueuedFrames) {
225 return layerFE == layerWithQueuedFrames.get();
226 });
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800227
228 if (hasQueuedFrames) {
229 releasedLayers.emplace_back(layerFE);
230 }
231 }
232
233 setReleasedLayers(std::move(releasedLayers));
234}
235
Lloyd Pique66d68602019-02-13 14:23:31 -0800236void 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 }
263
264 // Determine what type of composition we are doing from the final state
265 auto& state = editState();
266 state.usesClientComposition = anyLayersRequireClientComposition();
267 state.usesDeviceComposition = !allLayersRequireClientComposition();
268}
269
Lloyd Pique688abd42019-02-15 15:42:24 -0800270bool Display::getSkipColorTransform() const {
Dominik Laskowski1162e472020-04-02 19:02:47 -0700271 const auto& hwc = getCompositionEngine().getHwComposer();
272 return mId ? hwc.hasDisplayCapability(*mId, HWC2::DisplayCapability::SkipClientColorTransform)
273 : hwc.hasCapability(HWC2::Capability::SkipClientColorTransform);
Lloyd Pique688abd42019-02-15 15:42:24 -0800274}
275
Lloyd Pique66d68602019-02-13 14:23:31 -0800276bool Display::anyLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700277 const auto layers = getOutputLayersOrderedByZ();
278 return std::any_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800279 [](const auto& layer) { return layer->requiresClientComposition(); });
280}
281
282bool Display::allLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700283 const auto layers = getOutputLayersOrderedByZ();
284 return std::all_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800285 [](const auto& layer) { return layer->requiresClientComposition(); });
286}
287
288void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
289 if (changedTypes.empty()) {
290 return;
291 }
292
Lloyd Pique01c77c12019-04-17 12:48:32 -0700293 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800294 auto hwcLayer = layer->getHwcLayer();
295 if (!hwcLayer) {
296 continue;
297 }
298
299 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
300 layer->applyDeviceCompositionTypeChange(
301 static_cast<Hwc2::IComposerClient::Composition>(it->second));
302 }
303 }
304}
305
306void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
307 auto& state = editState();
308 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
309 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0;
310 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
311}
312
313void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700314 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800315 layer->prepareForDeviceLayerRequests();
316
317 auto hwcLayer = layer->getHwcLayer();
318 if (!hwcLayer) {
319 continue;
320 }
321
322 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
323 layer->applyDeviceLayerRequest(
324 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
325 }
326 }
327}
328
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800329compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
330 auto result = impl::Output::presentAndGetFrameFences();
331
332 if (!mId) {
333 return result;
334 }
335
336 auto& hwc = getCompositionEngine().getHwComposer();
337 hwc.presentAndGetReleaseFences(*mId);
338
339 result.presentFence = hwc.getPresentFence(*mId);
340
341 // TODO(b/121291683): Change HWComposer call to return entire map
Lloyd Pique01c77c12019-04-17 12:48:32 -0700342 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800343 auto hwcLayer = layer->getHwcLayer();
344 if (!hwcLayer) {
345 continue;
346 }
347
348 result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
349 }
350
351 hwc.clearReleaseFences(*mId);
352
353 return result;
354}
355
Lloyd Pique688abd42019-02-15 15:42:24 -0800356void Display::setExpensiveRenderingExpected(bool enabled) {
357 Output::setExpensiveRenderingExpected(enabled);
358
359 if (mPowerAdvisor && mId) {
360 mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled);
361 }
362}
363
Lloyd Piqued3d69882019-02-28 16:03:46 -0800364void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
365 // We only need to actually compose the display if:
366 // 1) It is being handled by hardware composer, which may need this to
367 // keep its virtual display state machine in sync, or
368 // 2) There is work to be done (the dirty region isn't empty)
369 if (!mId) {
370 if (getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) {
371 ALOGV("Skipping display composition");
372 return;
373 }
374 }
375
376 impl::Output::finishFrame(refreshArgs);
377}
378
Lloyd Pique45a165a2018-10-19 11:54:47 -0700379} // namespace android::compositionengine::impl