blob: 02fa49f3c0856cd0d2e813edc76f42185595f0ce [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;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070054 mPowerAdvisor = args.powerAdvisor;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070055 editState().isSecure = args.isSecure;
Angel Aguayob084e0c2021-08-04 23:27:28 +000056 editState().displaySpace.setBounds(args.pixels);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070057 setName(args.name);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070058}
59
60bool Display::isValid() const {
61 return Output::isValid() && mPowerAdvisor;
62}
63
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020064DisplayId Display::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -070065 return mId;
66}
67
68bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070069 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070070}
71
72bool Display::isVirtual() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -070073 return VirtualDisplayId::tryCast(mId).has_value();
Lloyd Pique45a165a2018-10-19 11:54:47 -070074}
75
Lloyd Pique6c564cf2019-05-17 17:31:36 -070076std::optional<DisplayId> Display::getDisplayId() const {
77 return mId;
78}
79
Lloyd Pique45a165a2018-10-19 11:54:47 -070080void Display::disconnect() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020081 if (mIsDisconnected) {
Lloyd Pique45a165a2018-10-19 11:54:47 -070082 return;
83 }
84
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020085 mIsDisconnected = true;
Dominik Laskowski13948602021-03-08 20:48:28 -080086
87 if (const auto id = HalDisplayId::tryCast(mId)) {
88 getCompositionEngine().getHwComposer().disconnectDisplay(*id);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020089 }
Lloyd Pique45a165a2018-10-19 11:54:47 -070090}
91
Lloyd Pique3eb1b212019-03-07 21:15:40 -080092void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
93 Output::setColorTransform(args);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020094 const auto halDisplayId = HalDisplayId::tryCast(mId);
95 if (mIsDisconnected || !halDisplayId || CC_LIKELY(!args.colorTransformMatrix)) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -080096 return;
97 }
Lloyd Pique32cbe282018-10-19 13:09:22 -070098
99 auto& hwc = getCompositionEngine().getHwComposer();
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200100 status_t result = hwc.setColorTransform(*halDisplayId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700101 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200102 to_string(mId).c_str(), result);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700103}
104
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800105void Display::setColorProfile(const ColorProfile& colorProfile) {
106 const ui::Dataspace targetDataspace =
107 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
108 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800109
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800110 if (colorProfile.mode == getState().colorMode &&
111 colorProfile.dataspace == getState().dataspace &&
112 colorProfile.renderIntent == getState().renderIntent &&
113 targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700114 return;
115 }
116
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700117 if (isVirtual()) {
118 ALOGW("%s: Invalid operation on virtual display", __func__);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700119 return;
120 }
121
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800122 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700123
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200124 const auto physicalId = PhysicalDisplayId::tryCast(mId);
125 LOG_FATAL_IF(!physicalId);
126 getCompositionEngine().getHwComposer().setActiveColorMode(*physicalId, colorProfile.mode,
127 colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700128}
129
130void Display::dump(std::string& out) const {
131 using android::base::StringAppendF;
132
133 StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str());
134
135 out.append("\n ");
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700136 dumpVal(out, "isVirtual", isVirtual());
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200137 dumpVal(out, "DisplayId", to_string(mId));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700138 out.append("\n");
139
140 Output::dumpBase(out);
141}
142
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700143void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
144 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700145}
146
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700147void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
148 setRenderSurface(
149 compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700150}
151
Vishnu Nair9b079a22020-01-21 14:36:08 -0800152void Display::createClientCompositionCache(uint32_t cacheSize) {
153 cacheClientCompositionRequests(cacheSize);
154}
155
Lloyd Piquedf336d92019-03-07 21:38:42 -0800156std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
Lloyd Piquedf336d92019-03-07 21:38:42 -0800157 const sp<compositionengine::LayerFE>& layerFE) const {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200158 auto outputLayer = impl::createOutputLayer(*this, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800159
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200160 if (const auto halDisplayId = HalDisplayId::tryCast(mId);
161 outputLayer && !mIsDisconnected && halDisplayId) {
Lloyd Piquedf336d92019-03-07 21:38:42 -0800162 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700163 auto hwcLayer = hwc.createLayer(*halDisplayId);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800164 ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
165 getName().c_str());
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200166 outputLayer->setHwcLayer(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -0800167 }
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200168 return outputLayer;
Lloyd Piquedf336d92019-03-07 21:38:42 -0800169}
170
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800171void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
172 Output::setReleasedLayers(refreshArgs);
173
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200174 if (mIsDisconnected || GpuVirtualDisplayId::tryCast(mId) ||
175 refreshArgs.layersWithQueuedFrames.empty()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800176 return;
177 }
178
179 // For layers that are being removed from a HWC display, and that have
180 // queued frames, add them to a a list of released layers so we can properly
181 // set a fence.
182 compositionengine::Output::ReleasedLayers releasedLayers;
183
184 // Any non-null entries in the current list of layers are layers that are no
185 // longer going to be visible
Lloyd Piquede196652020-01-22 17:29:58 -0800186 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
187 if (!outputLayer) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800188 continue;
189 }
190
Lloyd Piquede196652020-01-22 17:29:58 -0800191 compositionengine::LayerFE* layerFE = &outputLayer->getLayerFE();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800192 const bool hasQueuedFrames =
Lloyd Piquede196652020-01-22 17:29:58 -0800193 std::any_of(refreshArgs.layersWithQueuedFrames.cbegin(),
194 refreshArgs.layersWithQueuedFrames.cend(),
195 [layerFE](sp<compositionengine::LayerFE> layerWithQueuedFrames) {
196 return layerFE == layerWithQueuedFrames.get();
197 });
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800198
199 if (hasQueuedFrames) {
200 releasedLayers.emplace_back(layerFE);
201 }
202 }
203
204 setReleasedLayers(std::move(releasedLayers));
205}
206
Lloyd Pique66d68602019-02-13 14:23:31 -0800207void Display::chooseCompositionStrategy() {
208 ATRACE_CALL();
209 ALOGV(__FUNCTION__);
210
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200211 if (mIsDisconnected) {
212 return;
213 }
214
Lloyd Pique66d68602019-02-13 14:23:31 -0800215 // Default to the base settings -- client composition only.
216 Output::chooseCompositionStrategy();
217
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200218 // If we don't have a HWC display, then we are done.
219 const auto halDisplayId = HalDisplayId::tryCast(mId);
220 if (!halDisplayId) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800221 return;
222 }
223
224 // Get any composition changes requested by the HWC device, and apply them.
225 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
226 auto& hwc = getCompositionEngine().getHwComposer();
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200227 if (status_t result =
228 hwc.getDeviceCompositionChanges(*halDisplayId, anyLayersRequireClientComposition(),
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700229 getState().earliestPresentTime,
230 getState().previousPresentFence, &changes);
Lloyd Pique66d68602019-02-13 14:23:31 -0800231 result != NO_ERROR) {
232 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
233 strerror(-result));
234 return;
235 }
236 if (changes) {
237 applyChangedTypesToLayers(changes->changedTypes);
238 applyDisplayRequests(changes->displayRequests);
239 applyLayerRequestsToLayers(changes->layerRequests);
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700240 applyClientTargetRequests(changes->clientTargetProperty);
Lloyd Pique66d68602019-02-13 14:23:31 -0800241 }
242
243 // Determine what type of composition we are doing from the final state
244 auto& state = editState();
245 state.usesClientComposition = anyLayersRequireClientComposition();
246 state.usesDeviceComposition = !allLayersRequireClientComposition();
247}
248
Lloyd Pique688abd42019-02-15 15:42:24 -0800249bool Display::getSkipColorTransform() const {
Dominik Laskowski1162e472020-04-02 19:02:47 -0700250 const auto& hwc = getCompositionEngine().getHwComposer();
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200251 if (const auto halDisplayId = HalDisplayId::tryCast(mId)) {
252 return hwc.hasDisplayCapability(*halDisplayId,
253 hal::DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM);
254 }
255
256 return hwc.hasCapability(hal::Capability::SKIP_CLIENT_COLOR_TRANSFORM);
Lloyd Pique688abd42019-02-15 15:42:24 -0800257}
258
Lloyd Pique66d68602019-02-13 14:23:31 -0800259bool Display::anyLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700260 const auto layers = getOutputLayersOrderedByZ();
261 return std::any_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800262 [](const auto& layer) { return layer->requiresClientComposition(); });
263}
264
265bool Display::allLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700266 const auto layers = getOutputLayersOrderedByZ();
267 return std::all_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800268 [](const auto& layer) { return layer->requiresClientComposition(); });
269}
270
271void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
272 if (changedTypes.empty()) {
273 return;
274 }
275
Lloyd Pique01c77c12019-04-17 12:48:32 -0700276 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800277 auto hwcLayer = layer->getHwcLayer();
278 if (!hwcLayer) {
279 continue;
280 }
281
282 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
283 layer->applyDeviceCompositionTypeChange(
284 static_cast<Hwc2::IComposerClient::Composition>(it->second));
285 }
286 }
287}
288
289void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
290 auto& state = editState();
291 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
Peiyong Line9d809e2020-04-14 13:10:48 -0700292 static_cast<uint32_t>(hal::DisplayRequest::FLIP_CLIENT_TARGET)) != 0;
Lloyd Pique66d68602019-02-13 14:23:31 -0800293 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
294}
295
296void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700297 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800298 layer->prepareForDeviceLayerRequests();
299
300 auto hwcLayer = layer->getHwcLayer();
301 if (!hwcLayer) {
302 continue;
303 }
304
305 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
306 layer->applyDeviceLayerRequest(
307 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
308 }
309 }
310}
311
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700312void Display::applyClientTargetRequests(const ClientTargetProperty& clientTargetProperty) {
313 if (clientTargetProperty.dataspace == ui::Dataspace::UNKNOWN) {
314 return;
315 }
Ady Abraham0094dc62021-06-03 10:08:33 -0700316
317 editState().dataspace = clientTargetProperty.dataspace;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700318 getRenderSurface()->setBufferDataspace(clientTargetProperty.dataspace);
319 getRenderSurface()->setBufferPixelFormat(clientTargetProperty.pixelFormat);
320}
321
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800322compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200323 auto fences = impl::Output::presentAndGetFrameFences();
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800324
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200325 const auto halDisplayIdOpt = HalDisplayId::tryCast(mId);
326 if (mIsDisconnected || !halDisplayIdOpt) {
327 return fences;
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800328 }
329
330 auto& hwc = getCompositionEngine().getHwComposer();
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700331 hwc.presentAndGetReleaseFences(*halDisplayIdOpt, getState().earliestPresentTime,
332 getState().previousPresentFence);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800333
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200334 fences.presentFence = hwc.getPresentFence(*halDisplayIdOpt);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800335
336 // TODO(b/121291683): Change HWComposer call to return entire map
Lloyd Pique01c77c12019-04-17 12:48:32 -0700337 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800338 auto hwcLayer = layer->getHwcLayer();
339 if (!hwcLayer) {
340 continue;
341 }
342
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200343 fences.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*halDisplayIdOpt, hwcLayer));
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800344 }
345
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200346 hwc.clearReleaseFences(*halDisplayIdOpt);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800347
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200348 return fences;
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800349}
350
Lloyd Pique688abd42019-02-15 15:42:24 -0800351void Display::setExpensiveRenderingExpected(bool enabled) {
352 Output::setExpensiveRenderingExpected(enabled);
353
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200354 if (mPowerAdvisor && !GpuVirtualDisplayId::tryCast(mId)) {
355 mPowerAdvisor->setExpensiveRenderingExpected(mId, enabled);
Lloyd Pique688abd42019-02-15 15:42:24 -0800356 }
357}
358
Lloyd Piqued3d69882019-02-28 16:03:46 -0800359void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
360 // We only need to actually compose the display if:
361 // 1) It is being handled by hardware composer, which may need this to
362 // keep its virtual display state machine in sync, or
363 // 2) There is work to be done (the dirty region isn't empty)
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700364 if (GpuVirtualDisplayId::tryCast(mId) && getDirtyRegion().isEmpty()) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200365 ALOGV("Skipping display composition");
366 return;
Lloyd Piqued3d69882019-02-28 16:03:46 -0800367 }
368
369 impl::Output::finishFrame(refreshArgs);
370}
371
Lloyd Pique45a165a2018-10-19 11:54:47 -0700372} // namespace android::compositionengine::impl