blob: fe8fa21a8cc3899b3143b8d7aee1596828ba27b9 [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 Pique66d68602019-02-13 14:23:31 -080028#include <utils/Trace.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070029
30#include "DisplayHardware/HWComposer.h"
Lloyd Pique688abd42019-02-15 15:42:24 -080031#include "DisplayHardware/PowerAdvisor.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070032
33namespace android::compositionengine::impl {
34
Lloyd Pique35fca9d2019-02-13 14:24:11 -080035std::shared_ptr<Display> createDisplay(
Lloyd Pique45a165a2018-10-19 11:54:47 -070036 const compositionengine::CompositionEngine& compositionEngine,
37 compositionengine::DisplayCreationArgs&& args) {
38 return std::make_shared<Display>(compositionEngine, std::move(args));
39}
40
41Display::Display(const CompositionEngine& compositionEngine, DisplayCreationArgs&& args)
Lloyd Pique32cbe282018-10-19 13:09:22 -070042 : compositionengine::impl::Output(compositionEngine),
Lloyd Pique45a165a2018-10-19 11:54:47 -070043 mIsVirtual(args.isVirtual),
Lloyd Pique688abd42019-02-15 15:42:24 -080044 mId(args.displayId),
45 mPowerAdvisor(args.powerAdvisor) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070046 editState().isSecure = args.isSecure;
47}
Lloyd Pique45a165a2018-10-19 11:54:47 -070048
49Display::~Display() = default;
50
51const std::optional<DisplayId>& Display::getId() const {
52 return mId;
53}
54
55bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070056 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070057}
58
59bool Display::isVirtual() const {
60 return mIsVirtual;
61}
62
63void Display::disconnect() {
64 if (!mId) {
65 return;
66 }
67
Lloyd Pique32cbe282018-10-19 13:09:22 -070068 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique45a165a2018-10-19 11:54:47 -070069 hwc.disconnectDisplay(*mId);
70 mId.reset();
71}
72
Lloyd Pique3eb1b212019-03-07 21:15:40 -080073void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
74 Output::setColorTransform(args);
75
76 if (!mId || CC_LIKELY(!args.colorTransformMatrix)) {
77 return;
78 }
Lloyd Pique32cbe282018-10-19 13:09:22 -070079
80 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique3eb1b212019-03-07 21:15:40 -080081 status_t result = hwc.setColorTransform(*mId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -070082 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
83 mId ? to_string(*mId).c_str() : "", result);
84}
85
Lloyd Pique6a3b4462019-03-07 20:58:12 -080086void Display::setColorProfile(const ColorProfile& colorProfile) {
87 const ui::Dataspace targetDataspace =
88 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
89 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -080090
Lloyd Pique6a3b4462019-03-07 20:58:12 -080091 if (colorProfile.mode == getState().colorMode &&
92 colorProfile.dataspace == getState().dataspace &&
93 colorProfile.renderIntent == getState().renderIntent &&
94 targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070095 return;
96 }
97
98 if (mIsVirtual) {
99 ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
100 return;
101 }
102
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800103 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700104
105 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800106 hwc.setActiveColorMode(*mId, colorProfile.mode, colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700107}
108
109void Display::dump(std::string& out) const {
110 using android::base::StringAppendF;
111
112 StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str());
113
114 out.append("\n ");
115
116 dumpVal(out, "isVirtual", mIsVirtual);
117 if (mId) {
118 dumpVal(out, "hwcId", to_string(*mId));
119 } else {
120 StringAppendF(&out, "no hwcId, ");
121 }
122
123 out.append("\n");
124
125 Output::dumpBase(out);
126}
127
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700128void Display::createDisplayColorProfile(DisplayColorProfileCreationArgs&& args) {
129 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(std::move(args)));
130}
131
Lloyd Pique31cb2942018-10-19 17:23:03 -0700132void Display::createRenderSurface(RenderSurfaceCreationArgs&& args) {
133 setRenderSurface(compositionengine::impl::createRenderSurface(getCompositionEngine(), *this,
134 std::move(args)));
135}
136
Lloyd Piquedf336d92019-03-07 21:38:42 -0800137std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
138 const std::shared_ptr<compositionengine::Layer>& layer,
139 const sp<compositionengine::LayerFE>& layerFE) const {
140 auto result = Output::createOutputLayer(layer, layerFE);
141
142 if (result && mId) {
143 auto& hwc = getCompositionEngine().getHwComposer();
144 auto displayId = *mId;
145 // Note: For the moment we ensure it is safe to take a reference to the
146 // HWComposer implementation by destroying all the OutputLayers (and
147 // hence the HWC2::Layers they own) before setting a new HWComposer. See
148 // for example SurfaceFlinger::updateVrFlinger().
149 // TODO(b/121291683): Make this safer.
150 auto hwcLayer = std::shared_ptr<HWC2::Layer>(hwc.createLayer(displayId),
151 [&hwc, displayId](HWC2::Layer* layer) {
152 hwc.destroyLayer(displayId, layer);
153 });
154 ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
155 getName().c_str());
156 result->setHwcLayer(std::move(hwcLayer));
157 }
158 return result;
159}
160
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800161void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
162 Output::setReleasedLayers(refreshArgs);
163
164 if (!mId || refreshArgs.layersWithQueuedFrames.empty()) {
165 return;
166 }
167
168 // For layers that are being removed from a HWC display, and that have
169 // queued frames, add them to a a list of released layers so we can properly
170 // set a fence.
171 compositionengine::Output::ReleasedLayers releasedLayers;
172
173 // Any non-null entries in the current list of layers are layers that are no
174 // longer going to be visible
175 for (auto& layer : getOutputLayersOrderedByZ()) {
176 if (!layer) {
177 continue;
178 }
179
180 sp<compositionengine::LayerFE> layerFE(&layer->getLayerFE());
181 const bool hasQueuedFrames =
182 std::find(refreshArgs.layersWithQueuedFrames.cbegin(),
183 refreshArgs.layersWithQueuedFrames.cend(),
184 &layer->getLayer()) != refreshArgs.layersWithQueuedFrames.cend();
185
186 if (hasQueuedFrames) {
187 releasedLayers.emplace_back(layerFE);
188 }
189 }
190
191 setReleasedLayers(std::move(releasedLayers));
192}
193
Lloyd Pique66d68602019-02-13 14:23:31 -0800194void Display::chooseCompositionStrategy() {
195 ATRACE_CALL();
196 ALOGV(__FUNCTION__);
197
198 // Default to the base settings -- client composition only.
199 Output::chooseCompositionStrategy();
200
201 // If we don't have a HWC display, then we are done
202 if (!mId) {
203 return;
204 }
205
206 // Get any composition changes requested by the HWC device, and apply them.
207 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
208 auto& hwc = getCompositionEngine().getHwComposer();
209 if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
210 &changes);
211 result != NO_ERROR) {
212 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
213 strerror(-result));
214 return;
215 }
216 if (changes) {
217 applyChangedTypesToLayers(changes->changedTypes);
218 applyDisplayRequests(changes->displayRequests);
219 applyLayerRequestsToLayers(changes->layerRequests);
220 }
221
222 // Determine what type of composition we are doing from the final state
223 auto& state = editState();
224 state.usesClientComposition = anyLayersRequireClientComposition();
225 state.usesDeviceComposition = !allLayersRequireClientComposition();
226}
227
Lloyd Pique688abd42019-02-15 15:42:24 -0800228bool Display::getSkipColorTransform() const {
229 if (!mId) {
230 return false;
231 }
232
233 auto& hwc = getCompositionEngine().getHwComposer();
234 return hwc.hasDisplayCapability(*mId, HWC2::DisplayCapability::SkipClientColorTransform);
235}
236
Lloyd Pique66d68602019-02-13 14:23:31 -0800237bool Display::anyLayersRequireClientComposition() const {
238 const auto& layers = getOutputLayersOrderedByZ();
239 return std::any_of(layers.cbegin(), layers.cend(),
240 [](const auto& layer) { return layer->requiresClientComposition(); });
241}
242
243bool Display::allLayersRequireClientComposition() const {
244 const auto& layers = getOutputLayersOrderedByZ();
245 return std::all_of(layers.cbegin(), layers.cend(),
246 [](const auto& layer) { return layer->requiresClientComposition(); });
247}
248
249void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
250 if (changedTypes.empty()) {
251 return;
252 }
253
254 for (auto& layer : getOutputLayersOrderedByZ()) {
255 auto hwcLayer = layer->getHwcLayer();
256 if (!hwcLayer) {
257 continue;
258 }
259
260 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
261 layer->applyDeviceCompositionTypeChange(
262 static_cast<Hwc2::IComposerClient::Composition>(it->second));
263 }
264 }
265}
266
267void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
268 auto& state = editState();
269 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
270 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0;
271 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
272}
273
274void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
275 for (auto& layer : getOutputLayersOrderedByZ()) {
276 layer->prepareForDeviceLayerRequests();
277
278 auto hwcLayer = layer->getHwcLayer();
279 if (!hwcLayer) {
280 continue;
281 }
282
283 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
284 layer->applyDeviceLayerRequest(
285 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
286 }
287 }
288}
289
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800290compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
291 auto result = impl::Output::presentAndGetFrameFences();
292
293 if (!mId) {
294 return result;
295 }
296
297 auto& hwc = getCompositionEngine().getHwComposer();
298 hwc.presentAndGetReleaseFences(*mId);
299
300 result.presentFence = hwc.getPresentFence(*mId);
301
302 // TODO(b/121291683): Change HWComposer call to return entire map
303 for (const auto& layer : getOutputLayersOrderedByZ()) {
304 auto hwcLayer = layer->getHwcLayer();
305 if (!hwcLayer) {
306 continue;
307 }
308
309 result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
310 }
311
312 hwc.clearReleaseFences(*mId);
313
314 return result;
315}
316
Lloyd Pique688abd42019-02-15 15:42:24 -0800317void Display::setExpensiveRenderingExpected(bool enabled) {
318 Output::setExpensiveRenderingExpected(enabled);
319
320 if (mPowerAdvisor && mId) {
321 mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled);
322 }
323}
324
Lloyd Piqued3d69882019-02-28 16:03:46 -0800325void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
326 // We only need to actually compose the display if:
327 // 1) It is being handled by hardware composer, which may need this to
328 // keep its virtual display state machine in sync, or
329 // 2) There is work to be done (the dirty region isn't empty)
330 if (!mId) {
331 if (getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) {
332 ALOGV("Skipping display composition");
333 return;
334 }
335 }
336
337 impl::Output::finishFrame(refreshArgs);
338}
339
Lloyd Pique45a165a2018-10-19 11:54:47 -0700340} // namespace android::compositionengine::impl