blob: 20f765c4a8b54215ae5fbb46e60a1b1985759997 [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Lloyd Pique32cbe282018-10-19 13:09:22 -070021#include <android-base/stringprintf.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070022#include <compositionengine/CompositionEngine.h>
Lloyd Piqued3d69882019-02-28 16:03:46 -080023#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070024#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070025#include <compositionengine/DisplaySurface.h>
Lloyd Piquedf336d92019-03-07 21:38:42 -080026#include <compositionengine/LayerFE.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070027#include <compositionengine/impl/Display.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070028#include <compositionengine/impl/DisplayColorProfile.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070029#include <compositionengine/impl/DumpHelpers.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080030#include <compositionengine/impl/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070031#include <compositionengine/impl/RenderSurface.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080032#include <utils/Trace.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070033
34#include "DisplayHardware/HWComposer.h"
Lloyd Pique688abd42019-02-15 15:42:24 -080035#include "DisplayHardware/PowerAdvisor.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070036
37namespace android::compositionengine::impl {
38
Lloyd Pique35fca9d2019-02-13 14:24:11 -080039std::shared_ptr<Display> createDisplay(
Lloyd Pique45a165a2018-10-19 11:54:47 -070040 const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070041 const compositionengine::DisplayCreationArgs& args) {
42 return createDisplayTemplated<Display>(compositionEngine, args);
Lloyd Pique45a165a2018-10-19 11:54:47 -070043}
44
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070045Display::Display(const DisplayCreationArgs& args)
46 : mIsVirtual(args.isVirtual), mId(args.displayId), mPowerAdvisor(args.powerAdvisor) {}
Lloyd Pique45a165a2018-10-19 11:54:47 -070047
48Display::~Display() = default;
49
50const std::optional<DisplayId>& Display::getId() const {
51 return mId;
52}
53
54bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070055 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070056}
57
58bool Display::isVirtual() const {
59 return mIsVirtual;
60}
61
Lloyd Pique6c564cf2019-05-17 17:31:36 -070062std::optional<DisplayId> Display::getDisplayId() const {
63 return mId;
64}
65
Lloyd Pique45a165a2018-10-19 11:54:47 -070066void Display::disconnect() {
67 if (!mId) {
68 return;
69 }
70
Lloyd Pique32cbe282018-10-19 13:09:22 -070071 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique45a165a2018-10-19 11:54:47 -070072 hwc.disconnectDisplay(*mId);
73 mId.reset();
74}
75
Lloyd Pique3eb1b212019-03-07 21:15:40 -080076void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
77 Output::setColorTransform(args);
78
79 if (!mId || CC_LIKELY(!args.colorTransformMatrix)) {
80 return;
81 }
Lloyd Pique32cbe282018-10-19 13:09:22 -070082
83 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique3eb1b212019-03-07 21:15:40 -080084 status_t result = hwc.setColorTransform(*mId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -070085 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
86 mId ? to_string(*mId).c_str() : "", result);
87}
88
Lloyd Pique6a3b4462019-03-07 20:58:12 -080089void Display::setColorProfile(const ColorProfile& colorProfile) {
90 const ui::Dataspace targetDataspace =
91 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
92 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -080093
Lloyd Pique6a3b4462019-03-07 20:58:12 -080094 if (colorProfile.mode == getState().colorMode &&
95 colorProfile.dataspace == getState().dataspace &&
96 colorProfile.renderIntent == getState().renderIntent &&
97 targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070098 return;
99 }
100
101 if (mIsVirtual) {
102 ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
103 return;
104 }
105
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800106 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700107
108 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800109 hwc.setActiveColorMode(*mId, colorProfile.mode, colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700110}
111
112void Display::dump(std::string& out) const {
113 using android::base::StringAppendF;
114
115 StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str());
116
117 out.append("\n ");
118
119 dumpVal(out, "isVirtual", mIsVirtual);
120 if (mId) {
121 dumpVal(out, "hwcId", to_string(*mId));
122 } else {
123 StringAppendF(&out, "no hwcId, ");
124 }
125
126 out.append("\n");
127
128 Output::dumpBase(out);
129}
130
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700131void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
132 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700133}
134
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700135void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
136 setRenderSurface(
137 compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700138}
139
Lloyd Piquedf336d92019-03-07 21:38:42 -0800140std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
141 const std::shared_ptr<compositionengine::Layer>& layer,
142 const sp<compositionengine::LayerFE>& layerFE) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700143 auto result = impl::createOutputLayer(*this, layer, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800144
145 if (result && mId) {
146 auto& hwc = getCompositionEngine().getHwComposer();
147 auto displayId = *mId;
148 // Note: For the moment we ensure it is safe to take a reference to the
149 // HWComposer implementation by destroying all the OutputLayers (and
150 // hence the HWC2::Layers they own) before setting a new HWComposer. See
151 // for example SurfaceFlinger::updateVrFlinger().
152 // TODO(b/121291683): Make this safer.
153 auto hwcLayer = std::shared_ptr<HWC2::Layer>(hwc.createLayer(displayId),
154 [&hwc, displayId](HWC2::Layer* layer) {
155 hwc.destroyLayer(displayId, layer);
156 });
157 ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
158 getName().c_str());
159 result->setHwcLayer(std::move(hwcLayer));
160 }
161 return result;
162}
163
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800164void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
165 Output::setReleasedLayers(refreshArgs);
166
167 if (!mId || refreshArgs.layersWithQueuedFrames.empty()) {
168 return;
169 }
170
171 // For layers that are being removed from a HWC display, and that have
172 // queued frames, add them to a a list of released layers so we can properly
173 // set a fence.
174 compositionengine::Output::ReleasedLayers releasedLayers;
175
176 // Any non-null entries in the current list of layers are layers that are no
177 // longer going to be visible
Lloyd Pique01c77c12019-04-17 12:48:32 -0700178 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800179 if (!layer) {
180 continue;
181 }
182
183 sp<compositionengine::LayerFE> layerFE(&layer->getLayerFE());
184 const bool hasQueuedFrames =
185 std::find(refreshArgs.layersWithQueuedFrames.cbegin(),
186 refreshArgs.layersWithQueuedFrames.cend(),
187 &layer->getLayer()) != refreshArgs.layersWithQueuedFrames.cend();
188
189 if (hasQueuedFrames) {
190 releasedLayers.emplace_back(layerFE);
191 }
192 }
193
194 setReleasedLayers(std::move(releasedLayers));
195}
196
Lloyd Pique66d68602019-02-13 14:23:31 -0800197void Display::chooseCompositionStrategy() {
198 ATRACE_CALL();
199 ALOGV(__FUNCTION__);
200
201 // Default to the base settings -- client composition only.
202 Output::chooseCompositionStrategy();
203
204 // If we don't have a HWC display, then we are done
205 if (!mId) {
206 return;
207 }
208
209 // Get any composition changes requested by the HWC device, and apply them.
210 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
211 auto& hwc = getCompositionEngine().getHwComposer();
212 if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
213 &changes);
214 result != NO_ERROR) {
215 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
216 strerror(-result));
217 return;
218 }
219 if (changes) {
220 applyChangedTypesToLayers(changes->changedTypes);
221 applyDisplayRequests(changes->displayRequests);
222 applyLayerRequestsToLayers(changes->layerRequests);
223 }
224
225 // Determine what type of composition we are doing from the final state
226 auto& state = editState();
227 state.usesClientComposition = anyLayersRequireClientComposition();
228 state.usesDeviceComposition = !allLayersRequireClientComposition();
229}
230
Lloyd Pique688abd42019-02-15 15:42:24 -0800231bool Display::getSkipColorTransform() const {
232 if (!mId) {
233 return false;
234 }
235
236 auto& hwc = getCompositionEngine().getHwComposer();
237 return hwc.hasDisplayCapability(*mId, HWC2::DisplayCapability::SkipClientColorTransform);
238}
239
Lloyd Pique66d68602019-02-13 14:23:31 -0800240bool Display::anyLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700241 const auto layers = getOutputLayersOrderedByZ();
242 return std::any_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800243 [](const auto& layer) { return layer->requiresClientComposition(); });
244}
245
246bool Display::allLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700247 const auto layers = getOutputLayersOrderedByZ();
248 return std::all_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800249 [](const auto& layer) { return layer->requiresClientComposition(); });
250}
251
252void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
253 if (changedTypes.empty()) {
254 return;
255 }
256
Lloyd Pique01c77c12019-04-17 12:48:32 -0700257 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800258 auto hwcLayer = layer->getHwcLayer();
259 if (!hwcLayer) {
260 continue;
261 }
262
263 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
264 layer->applyDeviceCompositionTypeChange(
265 static_cast<Hwc2::IComposerClient::Composition>(it->second));
266 }
267 }
268}
269
270void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
271 auto& state = editState();
272 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
273 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0;
274 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
275}
276
277void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700278 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800279 layer->prepareForDeviceLayerRequests();
280
281 auto hwcLayer = layer->getHwcLayer();
282 if (!hwcLayer) {
283 continue;
284 }
285
286 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
287 layer->applyDeviceLayerRequest(
288 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
289 }
290 }
291}
292
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800293compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
294 auto result = impl::Output::presentAndGetFrameFences();
295
296 if (!mId) {
297 return result;
298 }
299
300 auto& hwc = getCompositionEngine().getHwComposer();
301 hwc.presentAndGetReleaseFences(*mId);
302
303 result.presentFence = hwc.getPresentFence(*mId);
304
305 // TODO(b/121291683): Change HWComposer call to return entire map
Lloyd Pique01c77c12019-04-17 12:48:32 -0700306 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800307 auto hwcLayer = layer->getHwcLayer();
308 if (!hwcLayer) {
309 continue;
310 }
311
312 result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
313 }
314
315 hwc.clearReleaseFences(*mId);
316
317 return result;
318}
319
Lloyd Pique688abd42019-02-15 15:42:24 -0800320void Display::setExpensiveRenderingExpected(bool enabled) {
321 Output::setExpensiveRenderingExpected(enabled);
322
323 if (mPowerAdvisor && mId) {
324 mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled);
325 }
326}
327
Lloyd Piqued3d69882019-02-28 16:03:46 -0800328void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
329 // We only need to actually compose the display if:
330 // 1) It is being handled by hardware composer, which may need this to
331 // keep its virtual display state machine in sync, or
332 // 2) There is work to be done (the dirty region isn't empty)
333 if (!mId) {
334 if (getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) {
335 ALOGV("Skipping display composition");
336 return;
337 }
338 }
339
340 impl::Output::finishFrame(refreshArgs);
341}
342
Lloyd Pique45a165a2018-10-19 11:54:47 -0700343} // namespace android::compositionengine::impl
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800344
345// TODO(b/129481165): remove the #pragma below and fix conversion issues
346#pragma clang diagnostic pop // ignored "-Wconversion"