blob: ae1336ec9d3f7c631490c3edd3584e89428ec424 [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) {
Dominik Laskowski13948602021-03-08 20:48:28 -080053 mId = args.id;
54 mIsVirtual = !args.connectionType;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070055 mPowerAdvisor = args.powerAdvisor;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070056 editState().isSecure = args.isSecure;
Marin Shalamanovb15d2272020-09-17 21:41:52 +020057 editState().displaySpace.bounds = Rect(args.pixels);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070058 setLayerStackFilter(args.layerStackId,
Dominik Laskowski13948602021-03-08 20:48:28 -080059 args.connectionType == ui::DisplayConnectionType::Internal);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070060 setName(args.name);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070061}
62
63bool Display::isValid() const {
64 return Output::isValid() && mPowerAdvisor;
65}
66
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020067DisplayId Display::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -070068 return mId;
69}
70
71bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070072 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070073}
74
75bool Display::isVirtual() const {
76 return mIsVirtual;
77}
78
Lloyd Pique6c564cf2019-05-17 17:31:36 -070079std::optional<DisplayId> Display::getDisplayId() const {
80 return mId;
81}
82
Lloyd Pique45a165a2018-10-19 11:54:47 -070083void Display::disconnect() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020084 if (mIsDisconnected) {
Lloyd Pique45a165a2018-10-19 11:54:47 -070085 return;
86 }
87
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020088 mIsDisconnected = true;
Dominik Laskowski13948602021-03-08 20:48:28 -080089
90 if (const auto id = HalDisplayId::tryCast(mId)) {
91 getCompositionEngine().getHwComposer().disconnectDisplay(*id);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020092 }
Lloyd Pique45a165a2018-10-19 11:54:47 -070093}
94
Lloyd Pique3eb1b212019-03-07 21:15:40 -080095void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
96 Output::setColorTransform(args);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020097 const auto halDisplayId = HalDisplayId::tryCast(mId);
98 if (mIsDisconnected || !halDisplayId || CC_LIKELY(!args.colorTransformMatrix)) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -080099 return;
100 }
Lloyd Pique32cbe282018-10-19 13:09:22 -0700101
102 auto& hwc = getCompositionEngine().getHwComposer();
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200103 status_t result = hwc.setColorTransform(*halDisplayId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700104 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200105 to_string(mId).c_str(), result);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700106}
107
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800108void Display::setColorProfile(const ColorProfile& colorProfile) {
109 const ui::Dataspace targetDataspace =
110 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
111 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800112
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800113 if (colorProfile.mode == getState().colorMode &&
114 colorProfile.dataspace == getState().dataspace &&
115 colorProfile.renderIntent == getState().renderIntent &&
116 targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700117 return;
118 }
119
120 if (mIsVirtual) {
121 ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
122 return;
123 }
124
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800125 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700126
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200127 const auto physicalId = PhysicalDisplayId::tryCast(mId);
128 LOG_FATAL_IF(!physicalId);
129 getCompositionEngine().getHwComposer().setActiveColorMode(*physicalId, colorProfile.mode,
130 colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700131}
132
133void Display::dump(std::string& out) const {
134 using android::base::StringAppendF;
135
136 StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str());
137
138 out.append("\n ");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700139 dumpVal(out, "isVirtual", mIsVirtual);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200140 dumpVal(out, "DisplayId", to_string(mId));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700141 out.append("\n");
142
143 Output::dumpBase(out);
144}
145
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700146void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
147 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700148}
149
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700150void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
151 setRenderSurface(
152 compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700153}
154
Vishnu Nair9b079a22020-01-21 14:36:08 -0800155void Display::createClientCompositionCache(uint32_t cacheSize) {
156 cacheClientCompositionRequests(cacheSize);
157}
158
Lloyd Piquedf336d92019-03-07 21:38:42 -0800159std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
Lloyd Piquedf336d92019-03-07 21:38:42 -0800160 const sp<compositionengine::LayerFE>& layerFE) const {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200161 auto outputLayer = impl::createOutputLayer(*this, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800162
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200163 if (const auto halDisplayId = HalDisplayId::tryCast(mId);
164 outputLayer && !mIsDisconnected && halDisplayId) {
Lloyd Piquedf336d92019-03-07 21:38:42 -0800165 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700166 auto hwcLayer = hwc.createLayer(*halDisplayId);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800167 ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
168 getName().c_str());
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200169 outputLayer->setHwcLayer(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -0800170 }
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200171 return outputLayer;
Lloyd Piquedf336d92019-03-07 21:38:42 -0800172}
173
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800174void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
175 Output::setReleasedLayers(refreshArgs);
176
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200177 if (mIsDisconnected || GpuVirtualDisplayId::tryCast(mId) ||
178 refreshArgs.layersWithQueuedFrames.empty()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800179 return;
180 }
181
182 // For layers that are being removed from a HWC display, and that have
183 // queued frames, add them to a a list of released layers so we can properly
184 // set a fence.
185 compositionengine::Output::ReleasedLayers releasedLayers;
186
187 // Any non-null entries in the current list of layers are layers that are no
188 // longer going to be visible
Lloyd Piquede196652020-01-22 17:29:58 -0800189 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
190 if (!outputLayer) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800191 continue;
192 }
193
Lloyd Piquede196652020-01-22 17:29:58 -0800194 compositionengine::LayerFE* layerFE = &outputLayer->getLayerFE();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800195 const bool hasQueuedFrames =
Lloyd Piquede196652020-01-22 17:29:58 -0800196 std::any_of(refreshArgs.layersWithQueuedFrames.cbegin(),
197 refreshArgs.layersWithQueuedFrames.cend(),
198 [layerFE](sp<compositionengine::LayerFE> layerWithQueuedFrames) {
199 return layerFE == layerWithQueuedFrames.get();
200 });
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800201
202 if (hasQueuedFrames) {
203 releasedLayers.emplace_back(layerFE);
204 }
205 }
206
207 setReleasedLayers(std::move(releasedLayers));
208}
209
Lloyd Pique66d68602019-02-13 14:23:31 -0800210void Display::chooseCompositionStrategy() {
211 ATRACE_CALL();
212 ALOGV(__FUNCTION__);
213
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200214 if (mIsDisconnected) {
215 return;
216 }
217
Lloyd Pique66d68602019-02-13 14:23:31 -0800218 // Default to the base settings -- client composition only.
219 Output::chooseCompositionStrategy();
220
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200221 // If we don't have a HWC display, then we are done.
222 const auto halDisplayId = HalDisplayId::tryCast(mId);
223 if (!halDisplayId) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800224 return;
225 }
226
227 // Get any composition changes requested by the HWC device, and apply them.
228 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
229 auto& hwc = getCompositionEngine().getHwComposer();
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200230 if (status_t result =
231 hwc.getDeviceCompositionChanges(*halDisplayId, anyLayersRequireClientComposition(),
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700232 getState().earliestPresentTime, &changes);
Lloyd Pique66d68602019-02-13 14:23:31 -0800233 result != NO_ERROR) {
234 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
235 strerror(-result));
236 return;
237 }
238 if (changes) {
239 applyChangedTypesToLayers(changes->changedTypes);
240 applyDisplayRequests(changes->displayRequests);
241 applyLayerRequestsToLayers(changes->layerRequests);
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700242 applyClientTargetRequests(changes->clientTargetProperty);
Lloyd Pique66d68602019-02-13 14:23:31 -0800243 }
244
245 // Determine what type of composition we are doing from the final state
246 auto& state = editState();
247 state.usesClientComposition = anyLayersRequireClientComposition();
248 state.usesDeviceComposition = !allLayersRequireClientComposition();
249}
250
Lloyd Pique688abd42019-02-15 15:42:24 -0800251bool Display::getSkipColorTransform() const {
Dominik Laskowski1162e472020-04-02 19:02:47 -0700252 const auto& hwc = getCompositionEngine().getHwComposer();
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200253 if (const auto halDisplayId = HalDisplayId::tryCast(mId)) {
254 return hwc.hasDisplayCapability(*halDisplayId,
255 hal::DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM);
256 }
257
258 return hwc.hasCapability(hal::Capability::SKIP_CLIENT_COLOR_TRANSFORM);
Lloyd Pique688abd42019-02-15 15:42:24 -0800259}
260
Lloyd Pique66d68602019-02-13 14:23:31 -0800261bool Display::anyLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700262 const auto layers = getOutputLayersOrderedByZ();
263 return std::any_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800264 [](const auto& layer) { return layer->requiresClientComposition(); });
265}
266
267bool Display::allLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700268 const auto layers = getOutputLayersOrderedByZ();
269 return std::all_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800270 [](const auto& layer) { return layer->requiresClientComposition(); });
271}
272
273void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
274 if (changedTypes.empty()) {
275 return;
276 }
277
Lloyd Pique01c77c12019-04-17 12:48:32 -0700278 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800279 auto hwcLayer = layer->getHwcLayer();
280 if (!hwcLayer) {
281 continue;
282 }
283
284 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
285 layer->applyDeviceCompositionTypeChange(
286 static_cast<Hwc2::IComposerClient::Composition>(it->second));
287 }
288 }
289}
290
291void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
292 auto& state = editState();
293 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
Peiyong Line9d809e2020-04-14 13:10:48 -0700294 static_cast<uint32_t>(hal::DisplayRequest::FLIP_CLIENT_TARGET)) != 0;
Lloyd Pique66d68602019-02-13 14:23:31 -0800295 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
296}
297
298void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700299 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800300 layer->prepareForDeviceLayerRequests();
301
302 auto hwcLayer = layer->getHwcLayer();
303 if (!hwcLayer) {
304 continue;
305 }
306
307 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
308 layer->applyDeviceLayerRequest(
309 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
310 }
311 }
312}
313
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700314void Display::applyClientTargetRequests(const ClientTargetProperty& clientTargetProperty) {
315 if (clientTargetProperty.dataspace == ui::Dataspace::UNKNOWN) {
316 return;
317 }
Ady Abraham0094dc62021-06-03 10:08:33 -0700318
319 editState().dataspace = clientTargetProperty.dataspace;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700320 getRenderSurface()->setBufferDataspace(clientTargetProperty.dataspace);
321 getRenderSurface()->setBufferPixelFormat(clientTargetProperty.pixelFormat);
322}
323
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800324compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200325 auto fences = impl::Output::presentAndGetFrameFences();
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800326
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200327 const auto halDisplayIdOpt = HalDisplayId::tryCast(mId);
328 if (mIsDisconnected || !halDisplayIdOpt) {
329 return fences;
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800330 }
331
332 auto& hwc = getCompositionEngine().getHwComposer();
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700333 hwc.presentAndGetReleaseFences(*halDisplayIdOpt, getState().earliestPresentTime);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800334
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200335 fences.presentFence = hwc.getPresentFence(*halDisplayIdOpt);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800336
337 // TODO(b/121291683): Change HWComposer call to return entire map
Lloyd Pique01c77c12019-04-17 12:48:32 -0700338 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800339 auto hwcLayer = layer->getHwcLayer();
340 if (!hwcLayer) {
341 continue;
342 }
343
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200344 fences.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*halDisplayIdOpt, hwcLayer));
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800345 }
346
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200347 hwc.clearReleaseFences(*halDisplayIdOpt);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800348
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200349 return fences;
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800350}
351
Lloyd Pique688abd42019-02-15 15:42:24 -0800352void Display::setExpensiveRenderingExpected(bool enabled) {
353 Output::setExpensiveRenderingExpected(enabled);
354
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200355 if (mPowerAdvisor && !GpuVirtualDisplayId::tryCast(mId)) {
356 mPowerAdvisor->setExpensiveRenderingExpected(mId, enabled);
Lloyd Pique688abd42019-02-15 15:42:24 -0800357 }
358}
359
Lloyd Piqued3d69882019-02-28 16:03:46 -0800360void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
361 // We only need to actually compose the display if:
362 // 1) It is being handled by hardware composer, which may need this to
363 // keep its virtual display state machine in sync, or
364 // 2) There is work to be done (the dirty region isn't empty)
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200365 if (GpuVirtualDisplayId::tryCast(mId) &&
366 getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) {
367 ALOGV("Skipping display composition");
368 return;
Lloyd Piqued3d69882019-02-28 16:03:46 -0800369 }
370
371 impl::Output::finishFrame(refreshArgs);
372}
373
Lloyd Pique45a165a2018-10-19 11:54:47 -0700374} // namespace android::compositionengine::impl