blob: 49727dfed51845c6d45d95cec4f9f5ee4c981b65 [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 Pique66d68602019-02-13 14:23:31 -0800161void Display::chooseCompositionStrategy() {
162 ATRACE_CALL();
163 ALOGV(__FUNCTION__);
164
165 // Default to the base settings -- client composition only.
166 Output::chooseCompositionStrategy();
167
168 // If we don't have a HWC display, then we are done
169 if (!mId) {
170 return;
171 }
172
173 // Get any composition changes requested by the HWC device, and apply them.
174 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
175 auto& hwc = getCompositionEngine().getHwComposer();
176 if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
177 &changes);
178 result != NO_ERROR) {
179 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
180 strerror(-result));
181 return;
182 }
183 if (changes) {
184 applyChangedTypesToLayers(changes->changedTypes);
185 applyDisplayRequests(changes->displayRequests);
186 applyLayerRequestsToLayers(changes->layerRequests);
187 }
188
189 // Determine what type of composition we are doing from the final state
190 auto& state = editState();
191 state.usesClientComposition = anyLayersRequireClientComposition();
192 state.usesDeviceComposition = !allLayersRequireClientComposition();
193}
194
Lloyd Pique688abd42019-02-15 15:42:24 -0800195bool Display::getSkipColorTransform() const {
196 if (!mId) {
197 return false;
198 }
199
200 auto& hwc = getCompositionEngine().getHwComposer();
201 return hwc.hasDisplayCapability(*mId, HWC2::DisplayCapability::SkipClientColorTransform);
202}
203
Lloyd Pique66d68602019-02-13 14:23:31 -0800204bool Display::anyLayersRequireClientComposition() const {
205 const auto& layers = getOutputLayersOrderedByZ();
206 return std::any_of(layers.cbegin(), layers.cend(),
207 [](const auto& layer) { return layer->requiresClientComposition(); });
208}
209
210bool Display::allLayersRequireClientComposition() const {
211 const auto& layers = getOutputLayersOrderedByZ();
212 return std::all_of(layers.cbegin(), layers.cend(),
213 [](const auto& layer) { return layer->requiresClientComposition(); });
214}
215
216void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
217 if (changedTypes.empty()) {
218 return;
219 }
220
221 for (auto& layer : getOutputLayersOrderedByZ()) {
222 auto hwcLayer = layer->getHwcLayer();
223 if (!hwcLayer) {
224 continue;
225 }
226
227 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
228 layer->applyDeviceCompositionTypeChange(
229 static_cast<Hwc2::IComposerClient::Composition>(it->second));
230 }
231 }
232}
233
234void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
235 auto& state = editState();
236 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
237 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0;
238 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
239}
240
241void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
242 for (auto& layer : getOutputLayersOrderedByZ()) {
243 layer->prepareForDeviceLayerRequests();
244
245 auto hwcLayer = layer->getHwcLayer();
246 if (!hwcLayer) {
247 continue;
248 }
249
250 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
251 layer->applyDeviceLayerRequest(
252 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
253 }
254 }
255}
256
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800257compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
258 auto result = impl::Output::presentAndGetFrameFences();
259
260 if (!mId) {
261 return result;
262 }
263
264 auto& hwc = getCompositionEngine().getHwComposer();
265 hwc.presentAndGetReleaseFences(*mId);
266
267 result.presentFence = hwc.getPresentFence(*mId);
268
269 // TODO(b/121291683): Change HWComposer call to return entire map
270 for (const auto& layer : getOutputLayersOrderedByZ()) {
271 auto hwcLayer = layer->getHwcLayer();
272 if (!hwcLayer) {
273 continue;
274 }
275
276 result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
277 }
278
279 hwc.clearReleaseFences(*mId);
280
281 return result;
282}
283
Lloyd Pique688abd42019-02-15 15:42:24 -0800284void Display::setExpensiveRenderingExpected(bool enabled) {
285 Output::setExpensiveRenderingExpected(enabled);
286
287 if (mPowerAdvisor && mId) {
288 mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled);
289 }
290}
291
Lloyd Piqued3d69882019-02-28 16:03:46 -0800292void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
293 // We only need to actually compose the display if:
294 // 1) It is being handled by hardware composer, which may need this to
295 // keep its virtual display state machine in sync, or
296 // 2) There is work to be done (the dirty region isn't empty)
297 if (!mId) {
298 if (getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) {
299 ALOGV("Skipping display composition");
300 return;
301 }
302 }
303
304 impl::Output::finishFrame(refreshArgs);
305}
306
Lloyd Pique45a165a2018-10-19 11:54:47 -0700307} // namespace android::compositionengine::impl