blob: 000a294c75328e03b4ccf60f48f51026ac746927 [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 Pique45a165a2018-10-19 11:54:47 -070022#include <compositionengine/impl/Display.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070023#include <compositionengine/impl/DisplayColorProfile.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070024#include <compositionengine/impl/DumpHelpers.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080025#include <compositionengine/impl/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070026#include <compositionengine/impl/RenderSurface.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080027#include <utils/Trace.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070028
29#include "DisplayHardware/HWComposer.h"
Lloyd Pique688abd42019-02-15 15:42:24 -080030#include "DisplayHardware/PowerAdvisor.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070031
32namespace android::compositionengine::impl {
33
Lloyd Pique35fca9d2019-02-13 14:24:11 -080034std::shared_ptr<Display> createDisplay(
Lloyd Pique45a165a2018-10-19 11:54:47 -070035 const compositionengine::CompositionEngine& compositionEngine,
36 compositionengine::DisplayCreationArgs&& args) {
37 return std::make_shared<Display>(compositionEngine, std::move(args));
38}
39
40Display::Display(const CompositionEngine& compositionEngine, DisplayCreationArgs&& args)
Lloyd Pique32cbe282018-10-19 13:09:22 -070041 : compositionengine::impl::Output(compositionEngine),
Lloyd Pique45a165a2018-10-19 11:54:47 -070042 mIsVirtual(args.isVirtual),
Lloyd Pique688abd42019-02-15 15:42:24 -080043 mId(args.displayId),
44 mPowerAdvisor(args.powerAdvisor) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070045 editState().isSecure = args.isSecure;
46}
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
62void Display::disconnect() {
63 if (!mId) {
64 return;
65 }
66
Lloyd Pique32cbe282018-10-19 13:09:22 -070067 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique45a165a2018-10-19 11:54:47 -070068 hwc.disconnectDisplay(*mId);
69 mId.reset();
70}
71
Lloyd Pique3eb1b212019-03-07 21:15:40 -080072void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
73 Output::setColorTransform(args);
74
75 if (!mId || CC_LIKELY(!args.colorTransformMatrix)) {
76 return;
77 }
Lloyd Pique32cbe282018-10-19 13:09:22 -070078
79 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique3eb1b212019-03-07 21:15:40 -080080 status_t result = hwc.setColorTransform(*mId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -070081 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
82 mId ? to_string(*mId).c_str() : "", result);
83}
84
Lloyd Pique6a3b4462019-03-07 20:58:12 -080085void Display::setColorProfile(const ColorProfile& colorProfile) {
86 const ui::Dataspace targetDataspace =
87 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
88 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -080089
Lloyd Pique6a3b4462019-03-07 20:58:12 -080090 if (colorProfile.mode == getState().colorMode &&
91 colorProfile.dataspace == getState().dataspace &&
92 colorProfile.renderIntent == getState().renderIntent &&
93 targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070094 return;
95 }
96
97 if (mIsVirtual) {
98 ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
99 return;
100 }
101
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800102 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700103
104 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800105 hwc.setActiveColorMode(*mId, colorProfile.mode, colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700106}
107
108void Display::dump(std::string& out) const {
109 using android::base::StringAppendF;
110
111 StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str());
112
113 out.append("\n ");
114
115 dumpVal(out, "isVirtual", mIsVirtual);
116 if (mId) {
117 dumpVal(out, "hwcId", to_string(*mId));
118 } else {
119 StringAppendF(&out, "no hwcId, ");
120 }
121
122 out.append("\n");
123
124 Output::dumpBase(out);
125}
126
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700127void Display::createDisplayColorProfile(DisplayColorProfileCreationArgs&& args) {
128 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(std::move(args)));
129}
130
Lloyd Pique31cb2942018-10-19 17:23:03 -0700131void Display::createRenderSurface(RenderSurfaceCreationArgs&& args) {
132 setRenderSurface(compositionengine::impl::createRenderSurface(getCompositionEngine(), *this,
133 std::move(args)));
134}
135
Lloyd Pique66d68602019-02-13 14:23:31 -0800136void Display::chooseCompositionStrategy() {
137 ATRACE_CALL();
138 ALOGV(__FUNCTION__);
139
140 // Default to the base settings -- client composition only.
141 Output::chooseCompositionStrategy();
142
143 // If we don't have a HWC display, then we are done
144 if (!mId) {
145 return;
146 }
147
148 // Get any composition changes requested by the HWC device, and apply them.
149 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
150 auto& hwc = getCompositionEngine().getHwComposer();
151 if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
152 &changes);
153 result != NO_ERROR) {
154 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
155 strerror(-result));
156 return;
157 }
158 if (changes) {
159 applyChangedTypesToLayers(changes->changedTypes);
160 applyDisplayRequests(changes->displayRequests);
161 applyLayerRequestsToLayers(changes->layerRequests);
162 }
163
164 // Determine what type of composition we are doing from the final state
165 auto& state = editState();
166 state.usesClientComposition = anyLayersRequireClientComposition();
167 state.usesDeviceComposition = !allLayersRequireClientComposition();
168}
169
Lloyd Pique688abd42019-02-15 15:42:24 -0800170bool Display::getSkipColorTransform() const {
171 if (!mId) {
172 return false;
173 }
174
175 auto& hwc = getCompositionEngine().getHwComposer();
176 return hwc.hasDisplayCapability(*mId, HWC2::DisplayCapability::SkipClientColorTransform);
177}
178
Lloyd Pique66d68602019-02-13 14:23:31 -0800179bool Display::anyLayersRequireClientComposition() const {
180 const auto& layers = getOutputLayersOrderedByZ();
181 return std::any_of(layers.cbegin(), layers.cend(),
182 [](const auto& layer) { return layer->requiresClientComposition(); });
183}
184
185bool Display::allLayersRequireClientComposition() const {
186 const auto& layers = getOutputLayersOrderedByZ();
187 return std::all_of(layers.cbegin(), layers.cend(),
188 [](const auto& layer) { return layer->requiresClientComposition(); });
189}
190
191void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
192 if (changedTypes.empty()) {
193 return;
194 }
195
196 for (auto& layer : getOutputLayersOrderedByZ()) {
197 auto hwcLayer = layer->getHwcLayer();
198 if (!hwcLayer) {
199 continue;
200 }
201
202 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
203 layer->applyDeviceCompositionTypeChange(
204 static_cast<Hwc2::IComposerClient::Composition>(it->second));
205 }
206 }
207}
208
209void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
210 auto& state = editState();
211 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
212 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0;
213 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
214}
215
216void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
217 for (auto& layer : getOutputLayersOrderedByZ()) {
218 layer->prepareForDeviceLayerRequests();
219
220 auto hwcLayer = layer->getHwcLayer();
221 if (!hwcLayer) {
222 continue;
223 }
224
225 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
226 layer->applyDeviceLayerRequest(
227 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
228 }
229 }
230}
231
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800232compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
233 auto result = impl::Output::presentAndGetFrameFences();
234
235 if (!mId) {
236 return result;
237 }
238
239 auto& hwc = getCompositionEngine().getHwComposer();
240 hwc.presentAndGetReleaseFences(*mId);
241
242 result.presentFence = hwc.getPresentFence(*mId);
243
244 // TODO(b/121291683): Change HWComposer call to return entire map
245 for (const auto& layer : getOutputLayersOrderedByZ()) {
246 auto hwcLayer = layer->getHwcLayer();
247 if (!hwcLayer) {
248 continue;
249 }
250
251 result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
252 }
253
254 hwc.clearReleaseFences(*mId);
255
256 return result;
257}
258
Lloyd Pique688abd42019-02-15 15:42:24 -0800259void Display::setExpensiveRenderingExpected(bool enabled) {
260 Output::setExpensiveRenderingExpected(enabled);
261
262 if (mPowerAdvisor && mId) {
263 mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled);
264 }
265}
266
Lloyd Piqued3d69882019-02-28 16:03:46 -0800267void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
268 // We only need to actually compose the display if:
269 // 1) It is being handled by hardware composer, which may need this to
270 // keep its virtual display state machine in sync, or
271 // 2) There is work to be done (the dirty region isn't empty)
272 if (!mId) {
273 if (getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) {
274 ALOGV("Skipping display composition");
275 return;
276 }
277 }
278
279 impl::Output::finishFrame(refreshArgs);
280}
281
Lloyd Pique45a165a2018-10-19 11:54:47 -0700282} // namespace android::compositionengine::impl