blob: 54598619e94235c3c7e75044db049ba4cf2539ed [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;
Marin Shalamanovb15d2272020-09-17 21:41:52 +020058 editState().displaySpace.bounds = Rect(args.pixels);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070059
60 setLayerStackFilter(args.layerStackId,
61 args.physical ? args.physical->type == DisplayConnectionType::Internal
62 : false);
63 setName(args.name);
64
65 if (!args.physical && args.useHwcVirtualDisplays) {
66 mId = maybeAllocateDisplayIdForVirtualDisplay(args.pixels, args.pixelFormat);
67 }
68}
69
70std::optional<DisplayId> Display::maybeAllocateDisplayIdForVirtualDisplay(
71 ui::Size pixels, ui::PixelFormat pixelFormat) const {
72 auto& hwc = getCompositionEngine().getHwComposer();
73 return hwc.allocateVirtualDisplay(static_cast<uint32_t>(pixels.width),
74 static_cast<uint32_t>(pixels.height), &pixelFormat);
75}
76
77bool Display::isValid() const {
78 return Output::isValid() && mPowerAdvisor;
79}
80
Lloyd Pique45a165a2018-10-19 11:54:47 -070081const std::optional<DisplayId>& Display::getId() const {
82 return mId;
83}
84
85bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070086 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070087}
88
89bool Display::isVirtual() const {
90 return mIsVirtual;
91}
92
Lloyd Pique6c564cf2019-05-17 17:31:36 -070093std::optional<DisplayId> Display::getDisplayId() const {
94 return mId;
95}
96
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070097void Display::setDisplayIdForTesting(std::optional<DisplayId> displayId) {
98 mId = displayId;
99}
100
Lloyd Pique45a165a2018-10-19 11:54:47 -0700101void Display::disconnect() {
102 if (!mId) {
103 return;
104 }
105
Lloyd Pique32cbe282018-10-19 13:09:22 -0700106 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique45a165a2018-10-19 11:54:47 -0700107 hwc.disconnectDisplay(*mId);
108 mId.reset();
109}
110
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800111void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
112 Output::setColorTransform(args);
113
114 if (!mId || CC_LIKELY(!args.colorTransformMatrix)) {
115 return;
116 }
Lloyd Pique32cbe282018-10-19 13:09:22 -0700117
118 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800119 status_t result = hwc.setColorTransform(*mId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700120 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
121 mId ? to_string(*mId).c_str() : "", result);
122}
123
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800124void Display::setColorProfile(const ColorProfile& colorProfile) {
125 const ui::Dataspace targetDataspace =
126 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
127 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800128
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800129 if (colorProfile.mode == getState().colorMode &&
130 colorProfile.dataspace == getState().dataspace &&
131 colorProfile.renderIntent == getState().renderIntent &&
132 targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700133 return;
134 }
135
136 if (mIsVirtual) {
137 ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
138 return;
139 }
140
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800141 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700142
143 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800144 hwc.setActiveColorMode(*mId, colorProfile.mode, colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700145}
146
147void Display::dump(std::string& out) const {
148 using android::base::StringAppendF;
149
150 StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str());
151
152 out.append("\n ");
153
154 dumpVal(out, "isVirtual", mIsVirtual);
155 if (mId) {
156 dumpVal(out, "hwcId", to_string(*mId));
157 } else {
158 StringAppendF(&out, "no hwcId, ");
159 }
160
161 out.append("\n");
162
163 Output::dumpBase(out);
164}
165
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700166void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
167 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700168}
169
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700170void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
171 setRenderSurface(
172 compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700173}
174
Vishnu Nair9b079a22020-01-21 14:36:08 -0800175void Display::createClientCompositionCache(uint32_t cacheSize) {
176 cacheClientCompositionRequests(cacheSize);
177}
178
Lloyd Piquedf336d92019-03-07 21:38:42 -0800179std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
Lloyd Piquedf336d92019-03-07 21:38:42 -0800180 const sp<compositionengine::LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800181 auto result = impl::createOutputLayer(*this, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800182
183 if (result && mId) {
184 auto& hwc = getCompositionEngine().getHwComposer();
185 auto displayId = *mId;
186 // Note: For the moment we ensure it is safe to take a reference to the
187 // HWComposer implementation by destroying all the OutputLayers (and
188 // hence the HWC2::Layers they own) before setting a new HWComposer. See
189 // for example SurfaceFlinger::updateVrFlinger().
190 // TODO(b/121291683): Make this safer.
191 auto hwcLayer = std::shared_ptr<HWC2::Layer>(hwc.createLayer(displayId),
192 [&hwc, displayId](HWC2::Layer* layer) {
193 hwc.destroyLayer(displayId, layer);
194 });
195 ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
196 getName().c_str());
197 result->setHwcLayer(std::move(hwcLayer));
198 }
199 return result;
200}
201
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800202void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
203 Output::setReleasedLayers(refreshArgs);
204
205 if (!mId || refreshArgs.layersWithQueuedFrames.empty()) {
206 return;
207 }
208
209 // For layers that are being removed from a HWC display, and that have
210 // queued frames, add them to a a list of released layers so we can properly
211 // set a fence.
212 compositionengine::Output::ReleasedLayers releasedLayers;
213
214 // Any non-null entries in the current list of layers are layers that are no
215 // longer going to be visible
Lloyd Piquede196652020-01-22 17:29:58 -0800216 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
217 if (!outputLayer) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800218 continue;
219 }
220
Lloyd Piquede196652020-01-22 17:29:58 -0800221 compositionengine::LayerFE* layerFE = &outputLayer->getLayerFE();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800222 const bool hasQueuedFrames =
Lloyd Piquede196652020-01-22 17:29:58 -0800223 std::any_of(refreshArgs.layersWithQueuedFrames.cbegin(),
224 refreshArgs.layersWithQueuedFrames.cend(),
225 [layerFE](sp<compositionengine::LayerFE> layerWithQueuedFrames) {
226 return layerFE == layerWithQueuedFrames.get();
227 });
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800228
229 if (hasQueuedFrames) {
230 releasedLayers.emplace_back(layerFE);
231 }
232 }
233
234 setReleasedLayers(std::move(releasedLayers));
235}
236
Lloyd Pique66d68602019-02-13 14:23:31 -0800237void Display::chooseCompositionStrategy() {
238 ATRACE_CALL();
239 ALOGV(__FUNCTION__);
240
241 // Default to the base settings -- client composition only.
242 Output::chooseCompositionStrategy();
243
244 // If we don't have a HWC display, then we are done
245 if (!mId) {
246 return;
247 }
248
249 // Get any composition changes requested by the HWC device, and apply them.
250 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
251 auto& hwc = getCompositionEngine().getHwComposer();
252 if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
253 &changes);
254 result != NO_ERROR) {
255 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
256 strerror(-result));
257 return;
258 }
259 if (changes) {
260 applyChangedTypesToLayers(changes->changedTypes);
261 applyDisplayRequests(changes->displayRequests);
262 applyLayerRequestsToLayers(changes->layerRequests);
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700263 applyClientTargetRequests(changes->clientTargetProperty);
Lloyd Pique66d68602019-02-13 14:23:31 -0800264 }
265
266 // Determine what type of composition we are doing from the final state
267 auto& state = editState();
268 state.usesClientComposition = anyLayersRequireClientComposition();
269 state.usesDeviceComposition = !allLayersRequireClientComposition();
270}
271
Lloyd Pique688abd42019-02-15 15:42:24 -0800272bool Display::getSkipColorTransform() const {
Dominik Laskowski1162e472020-04-02 19:02:47 -0700273 const auto& hwc = getCompositionEngine().getHwComposer();
Peiyong Line9d809e2020-04-14 13:10:48 -0700274 return mId ? hwc.hasDisplayCapability(*mId, hal::DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM)
275 : hwc.hasCapability(hal::Capability::SKIP_CLIENT_COLOR_TRANSFORM);
Lloyd Pique688abd42019-02-15 15:42:24 -0800276}
277
Lloyd Pique66d68602019-02-13 14:23:31 -0800278bool Display::anyLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700279 const auto layers = getOutputLayersOrderedByZ();
280 return std::any_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800281 [](const auto& layer) { return layer->requiresClientComposition(); });
282}
283
284bool Display::allLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700285 const auto layers = getOutputLayersOrderedByZ();
286 return std::all_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800287 [](const auto& layer) { return layer->requiresClientComposition(); });
288}
289
290void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
291 if (changedTypes.empty()) {
292 return;
293 }
294
Lloyd Pique01c77c12019-04-17 12:48:32 -0700295 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800296 auto hwcLayer = layer->getHwcLayer();
297 if (!hwcLayer) {
298 continue;
299 }
300
301 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
302 layer->applyDeviceCompositionTypeChange(
303 static_cast<Hwc2::IComposerClient::Composition>(it->second));
304 }
305 }
306}
307
308void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
309 auto& state = editState();
310 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
Peiyong Line9d809e2020-04-14 13:10:48 -0700311 static_cast<uint32_t>(hal::DisplayRequest::FLIP_CLIENT_TARGET)) != 0;
Lloyd Pique66d68602019-02-13 14:23:31 -0800312 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
313}
314
315void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700316 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800317 layer->prepareForDeviceLayerRequests();
318
319 auto hwcLayer = layer->getHwcLayer();
320 if (!hwcLayer) {
321 continue;
322 }
323
324 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
325 layer->applyDeviceLayerRequest(
326 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
327 }
328 }
329}
330
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700331void Display::applyClientTargetRequests(const ClientTargetProperty& clientTargetProperty) {
332 if (clientTargetProperty.dataspace == ui::Dataspace::UNKNOWN) {
333 return;
334 }
335 auto outputState = editState();
336 outputState.dataspace = clientTargetProperty.dataspace;
337 getRenderSurface()->setBufferDataspace(clientTargetProperty.dataspace);
338 getRenderSurface()->setBufferPixelFormat(clientTargetProperty.pixelFormat);
339}
340
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800341compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
342 auto result = impl::Output::presentAndGetFrameFences();
343
344 if (!mId) {
345 return result;
346 }
347
348 auto& hwc = getCompositionEngine().getHwComposer();
349 hwc.presentAndGetReleaseFences(*mId);
350
351 result.presentFence = hwc.getPresentFence(*mId);
352
353 // TODO(b/121291683): Change HWComposer call to return entire map
Lloyd Pique01c77c12019-04-17 12:48:32 -0700354 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800355 auto hwcLayer = layer->getHwcLayer();
356 if (!hwcLayer) {
357 continue;
358 }
359
360 result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
361 }
362
363 hwc.clearReleaseFences(*mId);
364
365 return result;
366}
367
Lloyd Pique688abd42019-02-15 15:42:24 -0800368void Display::setExpensiveRenderingExpected(bool enabled) {
369 Output::setExpensiveRenderingExpected(enabled);
370
371 if (mPowerAdvisor && mId) {
372 mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled);
373 }
374}
375
Lloyd Piqued3d69882019-02-28 16:03:46 -0800376void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
377 // We only need to actually compose the display if:
378 // 1) It is being handled by hardware composer, which may need this to
379 // keep its virtual display state machine in sync, or
380 // 2) There is work to be done (the dirty region isn't empty)
381 if (!mId) {
382 if (getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) {
383 ALOGV("Skipping display composition");
384 return;
385 }
386 }
387
388 impl::Output::finishFrame(refreshArgs);
389}
390
Lloyd Pique45a165a2018-10-19 11:54:47 -0700391} // namespace android::compositionengine::impl