blob: 108720a52ba182093c5548a0d3e9e970642e375f [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 Pique32cbe282018-10-19 13:09:22 -070072void Display::setColorTransform(const mat4& transform) {
73 Output::setColorTransform(transform);
74
75 auto& hwc = getCompositionEngine().getHwComposer();
76 status_t result = hwc.setColorTransform(*mId, transform);
77 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
78 mId ? to_string(*mId).c_str() : "", result);
79}
80
Lloyd Pique6a3b4462019-03-07 20:58:12 -080081void Display::setColorProfile(const ColorProfile& colorProfile) {
82 const ui::Dataspace targetDataspace =
83 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
84 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -080085
Lloyd Pique6a3b4462019-03-07 20:58:12 -080086 if (colorProfile.mode == getState().colorMode &&
87 colorProfile.dataspace == getState().dataspace &&
88 colorProfile.renderIntent == getState().renderIntent &&
89 targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070090 return;
91 }
92
93 if (mIsVirtual) {
94 ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
95 return;
96 }
97
Lloyd Pique6a3b4462019-03-07 20:58:12 -080098 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -070099
100 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800101 hwc.setActiveColorMode(*mId, colorProfile.mode, colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102}
103
104void Display::dump(std::string& out) const {
105 using android::base::StringAppendF;
106
107 StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str());
108
109 out.append("\n ");
110
111 dumpVal(out, "isVirtual", mIsVirtual);
112 if (mId) {
113 dumpVal(out, "hwcId", to_string(*mId));
114 } else {
115 StringAppendF(&out, "no hwcId, ");
116 }
117
118 out.append("\n");
119
120 Output::dumpBase(out);
121}
122
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700123void Display::createDisplayColorProfile(DisplayColorProfileCreationArgs&& args) {
124 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(std::move(args)));
125}
126
Lloyd Pique31cb2942018-10-19 17:23:03 -0700127void Display::createRenderSurface(RenderSurfaceCreationArgs&& args) {
128 setRenderSurface(compositionengine::impl::createRenderSurface(getCompositionEngine(), *this,
129 std::move(args)));
130}
131
Lloyd Pique66d68602019-02-13 14:23:31 -0800132void Display::chooseCompositionStrategy() {
133 ATRACE_CALL();
134 ALOGV(__FUNCTION__);
135
136 // Default to the base settings -- client composition only.
137 Output::chooseCompositionStrategy();
138
139 // If we don't have a HWC display, then we are done
140 if (!mId) {
141 return;
142 }
143
144 // Get any composition changes requested by the HWC device, and apply them.
145 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
146 auto& hwc = getCompositionEngine().getHwComposer();
147 if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
148 &changes);
149 result != NO_ERROR) {
150 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
151 strerror(-result));
152 return;
153 }
154 if (changes) {
155 applyChangedTypesToLayers(changes->changedTypes);
156 applyDisplayRequests(changes->displayRequests);
157 applyLayerRequestsToLayers(changes->layerRequests);
158 }
159
160 // Determine what type of composition we are doing from the final state
161 auto& state = editState();
162 state.usesClientComposition = anyLayersRequireClientComposition();
163 state.usesDeviceComposition = !allLayersRequireClientComposition();
164}
165
Lloyd Pique688abd42019-02-15 15:42:24 -0800166bool Display::getSkipColorTransform() const {
167 if (!mId) {
168 return false;
169 }
170
171 auto& hwc = getCompositionEngine().getHwComposer();
172 return hwc.hasDisplayCapability(*mId, HWC2::DisplayCapability::SkipClientColorTransform);
173}
174
Lloyd Pique66d68602019-02-13 14:23:31 -0800175bool Display::anyLayersRequireClientComposition() const {
176 const auto& layers = getOutputLayersOrderedByZ();
177 return std::any_of(layers.cbegin(), layers.cend(),
178 [](const auto& layer) { return layer->requiresClientComposition(); });
179}
180
181bool Display::allLayersRequireClientComposition() const {
182 const auto& layers = getOutputLayersOrderedByZ();
183 return std::all_of(layers.cbegin(), layers.cend(),
184 [](const auto& layer) { return layer->requiresClientComposition(); });
185}
186
187void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
188 if (changedTypes.empty()) {
189 return;
190 }
191
192 for (auto& layer : getOutputLayersOrderedByZ()) {
193 auto hwcLayer = layer->getHwcLayer();
194 if (!hwcLayer) {
195 continue;
196 }
197
198 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
199 layer->applyDeviceCompositionTypeChange(
200 static_cast<Hwc2::IComposerClient::Composition>(it->second));
201 }
202 }
203}
204
205void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
206 auto& state = editState();
207 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
208 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0;
209 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
210}
211
212void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
213 for (auto& layer : getOutputLayersOrderedByZ()) {
214 layer->prepareForDeviceLayerRequests();
215
216 auto hwcLayer = layer->getHwcLayer();
217 if (!hwcLayer) {
218 continue;
219 }
220
221 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
222 layer->applyDeviceLayerRequest(
223 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
224 }
225 }
226}
227
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800228compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
229 auto result = impl::Output::presentAndGetFrameFences();
230
231 if (!mId) {
232 return result;
233 }
234
235 auto& hwc = getCompositionEngine().getHwComposer();
236 hwc.presentAndGetReleaseFences(*mId);
237
238 result.presentFence = hwc.getPresentFence(*mId);
239
240 // TODO(b/121291683): Change HWComposer call to return entire map
241 for (const auto& layer : getOutputLayersOrderedByZ()) {
242 auto hwcLayer = layer->getHwcLayer();
243 if (!hwcLayer) {
244 continue;
245 }
246
247 result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
248 }
249
250 hwc.clearReleaseFences(*mId);
251
252 return result;
253}
254
Lloyd Pique688abd42019-02-15 15:42:24 -0800255void Display::setExpensiveRenderingExpected(bool enabled) {
256 Output::setExpensiveRenderingExpected(enabled);
257
258 if (mPowerAdvisor && mId) {
259 mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled);
260 }
261}
262
Lloyd Piqued3d69882019-02-28 16:03:46 -0800263void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
264 // We only need to actually compose the display if:
265 // 1) It is being handled by hardware composer, which may need this to
266 // keep its virtual display state machine in sync, or
267 // 2) There is work to be done (the dirty region isn't empty)
268 if (!mId) {
269 if (getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) {
270 ALOGV("Skipping display composition");
271 return;
272 }
273 }
274
275 impl::Output::finishFrame(refreshArgs);
276}
277
Lloyd Pique45a165a2018-10-19 11:54:47 -0700278} // namespace android::compositionengine::impl