blob: 1d8a23f4600b633203a7ee4657cc3eb007b6e1e8 [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 Piquea38ea7e2019-04-16 18:10:26 -070050Display::Display(const DisplayCreationArgs& args)
51 : mIsVirtual(args.isVirtual), mId(args.displayId), mPowerAdvisor(args.powerAdvisor) {}
Lloyd Pique45a165a2018-10-19 11:54:47 -070052
53Display::~Display() = default;
54
55const std::optional<DisplayId>& Display::getId() const {
56 return mId;
57}
58
59bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070060 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070061}
62
63bool Display::isVirtual() const {
64 return mIsVirtual;
65}
66
Lloyd Pique6c564cf2019-05-17 17:31:36 -070067std::optional<DisplayId> Display::getDisplayId() const {
68 return mId;
69}
70
Lloyd Pique45a165a2018-10-19 11:54:47 -070071void Display::disconnect() {
72 if (!mId) {
73 return;
74 }
75
Lloyd Pique32cbe282018-10-19 13:09:22 -070076 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique45a165a2018-10-19 11:54:47 -070077 hwc.disconnectDisplay(*mId);
78 mId.reset();
79}
80
Lloyd Pique3eb1b212019-03-07 21:15:40 -080081void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
82 Output::setColorTransform(args);
83
84 if (!mId || CC_LIKELY(!args.colorTransformMatrix)) {
85 return;
86 }
Lloyd Pique32cbe282018-10-19 13:09:22 -070087
88 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique3eb1b212019-03-07 21:15:40 -080089 status_t result = hwc.setColorTransform(*mId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -070090 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
91 mId ? to_string(*mId).c_str() : "", result);
92}
93
Lloyd Pique6a3b4462019-03-07 20:58:12 -080094void Display::setColorProfile(const ColorProfile& colorProfile) {
95 const ui::Dataspace targetDataspace =
96 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
97 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -080098
Lloyd Pique6a3b4462019-03-07 20:58:12 -080099 if (colorProfile.mode == getState().colorMode &&
100 colorProfile.dataspace == getState().dataspace &&
101 colorProfile.renderIntent == getState().renderIntent &&
102 targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700103 return;
104 }
105
106 if (mIsVirtual) {
107 ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
108 return;
109 }
110
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800111 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700112
113 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800114 hwc.setActiveColorMode(*mId, colorProfile.mode, colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700115}
116
117void Display::dump(std::string& out) const {
118 using android::base::StringAppendF;
119
120 StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str());
121
122 out.append("\n ");
123
124 dumpVal(out, "isVirtual", mIsVirtual);
125 if (mId) {
126 dumpVal(out, "hwcId", to_string(*mId));
127 } else {
128 StringAppendF(&out, "no hwcId, ");
129 }
130
131 out.append("\n");
132
133 Output::dumpBase(out);
134}
135
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700136void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
137 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700138}
139
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700140void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
141 setRenderSurface(
142 compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700143}
144
Vishnu Nair9b079a22020-01-21 14:36:08 -0800145void Display::createClientCompositionCache(uint32_t cacheSize) {
146 cacheClientCompositionRequests(cacheSize);
147}
148
Lloyd Piquedf336d92019-03-07 21:38:42 -0800149std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
Lloyd Piquedf336d92019-03-07 21:38:42 -0800150 const sp<compositionengine::LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800151 auto result = impl::createOutputLayer(*this, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800152
153 if (result && mId) {
154 auto& hwc = getCompositionEngine().getHwComposer();
155 auto displayId = *mId;
156 // Note: For the moment we ensure it is safe to take a reference to the
157 // HWComposer implementation by destroying all the OutputLayers (and
158 // hence the HWC2::Layers they own) before setting a new HWComposer. See
159 // for example SurfaceFlinger::updateVrFlinger().
160 // TODO(b/121291683): Make this safer.
161 auto hwcLayer = std::shared_ptr<HWC2::Layer>(hwc.createLayer(displayId),
162 [&hwc, displayId](HWC2::Layer* layer) {
163 hwc.destroyLayer(displayId, layer);
164 });
165 ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
166 getName().c_str());
167 result->setHwcLayer(std::move(hwcLayer));
168 }
169 return result;
170}
171
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800172void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
173 Output::setReleasedLayers(refreshArgs);
174
175 if (!mId || refreshArgs.layersWithQueuedFrames.empty()) {
176 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
211 // Default to the base settings -- client composition only.
212 Output::chooseCompositionStrategy();
213
214 // If we don't have a HWC display, then we are done
215 if (!mId) {
216 return;
217 }
218
219 // Get any composition changes requested by the HWC device, and apply them.
220 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
221 auto& hwc = getCompositionEngine().getHwComposer();
222 if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
223 &changes);
224 result != NO_ERROR) {
225 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
226 strerror(-result));
227 return;
228 }
229 if (changes) {
230 applyChangedTypesToLayers(changes->changedTypes);
231 applyDisplayRequests(changes->displayRequests);
232 applyLayerRequestsToLayers(changes->layerRequests);
233 }
234
235 // Determine what type of composition we are doing from the final state
236 auto& state = editState();
237 state.usesClientComposition = anyLayersRequireClientComposition();
238 state.usesDeviceComposition = !allLayersRequireClientComposition();
239}
240
Lloyd Pique688abd42019-02-15 15:42:24 -0800241bool Display::getSkipColorTransform() const {
242 if (!mId) {
243 return false;
244 }
245
246 auto& hwc = getCompositionEngine().getHwComposer();
247 return hwc.hasDisplayCapability(*mId, HWC2::DisplayCapability::SkipClientColorTransform);
248}
249
Lloyd Pique66d68602019-02-13 14:23:31 -0800250bool Display::anyLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700251 const auto layers = getOutputLayersOrderedByZ();
252 return std::any_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800253 [](const auto& layer) { return layer->requiresClientComposition(); });
254}
255
256bool Display::allLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700257 const auto layers = getOutputLayersOrderedByZ();
258 return std::all_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800259 [](const auto& layer) { return layer->requiresClientComposition(); });
260}
261
262void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
263 if (changedTypes.empty()) {
264 return;
265 }
266
Lloyd Pique01c77c12019-04-17 12:48:32 -0700267 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800268 auto hwcLayer = layer->getHwcLayer();
269 if (!hwcLayer) {
270 continue;
271 }
272
273 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
274 layer->applyDeviceCompositionTypeChange(
275 static_cast<Hwc2::IComposerClient::Composition>(it->second));
276 }
277 }
278}
279
280void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
281 auto& state = editState();
282 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
283 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0;
284 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
285}
286
287void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700288 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800289 layer->prepareForDeviceLayerRequests();
290
291 auto hwcLayer = layer->getHwcLayer();
292 if (!hwcLayer) {
293 continue;
294 }
295
296 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
297 layer->applyDeviceLayerRequest(
298 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
299 }
300 }
301}
302
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800303compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
304 auto result = impl::Output::presentAndGetFrameFences();
305
306 if (!mId) {
307 return result;
308 }
309
310 auto& hwc = getCompositionEngine().getHwComposer();
311 hwc.presentAndGetReleaseFences(*mId);
312
313 result.presentFence = hwc.getPresentFence(*mId);
314
315 // TODO(b/121291683): Change HWComposer call to return entire map
Lloyd Pique01c77c12019-04-17 12:48:32 -0700316 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800317 auto hwcLayer = layer->getHwcLayer();
318 if (!hwcLayer) {
319 continue;
320 }
321
322 result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
323 }
324
325 hwc.clearReleaseFences(*mId);
326
327 return result;
328}
329
Lloyd Pique688abd42019-02-15 15:42:24 -0800330void Display::setExpensiveRenderingExpected(bool enabled) {
331 Output::setExpensiveRenderingExpected(enabled);
332
333 if (mPowerAdvisor && mId) {
334 mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled);
335 }
336}
337
Lloyd Piqued3d69882019-02-28 16:03:46 -0800338void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
339 // We only need to actually compose the display if:
340 // 1) It is being handled by hardware composer, which may need this to
341 // keep its virtual display state machine in sync, or
342 // 2) There is work to be done (the dirty region isn't empty)
343 if (!mId) {
344 if (getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) {
345 ALOGV("Skipping display composition");
346 return;
347 }
348 }
349
350 impl::Output::finishFrame(refreshArgs);
351}
352
Lloyd Pique45a165a2018-10-19 11:54:47 -0700353} // namespace android::compositionengine::impl