blob: 6cd392eabd9545e3e2ce09cef6f0424d060ada61 [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>
19#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070020#include <compositionengine/DisplaySurface.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070021#include <compositionengine/impl/Display.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070022#include <compositionengine/impl/DisplayColorProfile.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070023#include <compositionengine/impl/DumpHelpers.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080024#include <compositionengine/impl/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070025#include <compositionengine/impl/RenderSurface.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080026#include <utils/Trace.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070027
28#include "DisplayHardware/HWComposer.h"
Lloyd Pique688abd42019-02-15 15:42:24 -080029#include "DisplayHardware/PowerAdvisor.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070030
31namespace android::compositionengine::impl {
32
Lloyd Pique35fca9d2019-02-13 14:24:11 -080033std::shared_ptr<Display> createDisplay(
Lloyd Pique45a165a2018-10-19 11:54:47 -070034 const compositionengine::CompositionEngine& compositionEngine,
35 compositionengine::DisplayCreationArgs&& args) {
36 return std::make_shared<Display>(compositionEngine, std::move(args));
37}
38
39Display::Display(const CompositionEngine& compositionEngine, DisplayCreationArgs&& args)
Lloyd Pique32cbe282018-10-19 13:09:22 -070040 : compositionengine::impl::Output(compositionEngine),
Lloyd Pique45a165a2018-10-19 11:54:47 -070041 mIsVirtual(args.isVirtual),
Lloyd Pique688abd42019-02-15 15:42:24 -080042 mId(args.displayId),
43 mPowerAdvisor(args.powerAdvisor) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070044 editState().isSecure = args.isSecure;
45}
Lloyd Pique45a165a2018-10-19 11:54:47 -070046
47Display::~Display() = default;
48
49const std::optional<DisplayId>& Display::getId() const {
50 return mId;
51}
52
53bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070054 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070055}
56
57bool Display::isVirtual() const {
58 return mIsVirtual;
59}
60
61void Display::disconnect() {
62 if (!mId) {
63 return;
64 }
65
Lloyd Pique32cbe282018-10-19 13:09:22 -070066 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique45a165a2018-10-19 11:54:47 -070067 hwc.disconnectDisplay(*mId);
68 mId.reset();
69}
70
Lloyd Pique32cbe282018-10-19 13:09:22 -070071void Display::setColorTransform(const mat4& transform) {
72 Output::setColorTransform(transform);
73
74 auto& hwc = getCompositionEngine().getHwComposer();
75 status_t result = hwc.setColorTransform(*mId, transform);
76 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
77 mId ? to_string(*mId).c_str() : "", result);
78}
79
80void Display::setColorMode(ui::ColorMode mode, ui::Dataspace dataspace,
Lloyd Piquef5275482019-01-29 18:42:42 -080081 ui::RenderIntent renderIntent,
82 ui::Dataspace colorSpaceAgnosticDataspace) {
83 ui::Dataspace targetDataspace =
84 getDisplayColorProfile()->getTargetDataspace(mode, dataspace,
85 colorSpaceAgnosticDataspace);
86
Lloyd Pique32cbe282018-10-19 13:09:22 -070087 if (mode == getState().colorMode && dataspace == getState().dataspace &&
Lloyd Piquef5275482019-01-29 18:42:42 -080088 renderIntent == getState().renderIntent && targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070089 return;
90 }
91
92 if (mIsVirtual) {
93 ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
94 return;
95 }
96
Lloyd Piquef5275482019-01-29 18:42:42 -080097 Output::setColorMode(mode, dataspace, renderIntent, colorSpaceAgnosticDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -070098
99 auto& hwc = getCompositionEngine().getHwComposer();
100 hwc.setActiveColorMode(*mId, mode, renderIntent);
101}
102
103void Display::dump(std::string& out) const {
104 using android::base::StringAppendF;
105
106 StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str());
107
108 out.append("\n ");
109
110 dumpVal(out, "isVirtual", mIsVirtual);
111 if (mId) {
112 dumpVal(out, "hwcId", to_string(*mId));
113 } else {
114 StringAppendF(&out, "no hwcId, ");
115 }
116
117 out.append("\n");
118
119 Output::dumpBase(out);
120}
121
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700122void Display::createDisplayColorProfile(DisplayColorProfileCreationArgs&& args) {
123 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(std::move(args)));
124}
125
Lloyd Pique31cb2942018-10-19 17:23:03 -0700126void Display::createRenderSurface(RenderSurfaceCreationArgs&& args) {
127 setRenderSurface(compositionengine::impl::createRenderSurface(getCompositionEngine(), *this,
128 std::move(args)));
129}
130
Lloyd Pique66d68602019-02-13 14:23:31 -0800131void Display::chooseCompositionStrategy() {
132 ATRACE_CALL();
133 ALOGV(__FUNCTION__);
134
135 // Default to the base settings -- client composition only.
136 Output::chooseCompositionStrategy();
137
138 // If we don't have a HWC display, then we are done
139 if (!mId) {
140 return;
141 }
142
143 // Get any composition changes requested by the HWC device, and apply them.
144 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
145 auto& hwc = getCompositionEngine().getHwComposer();
146 if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
147 &changes);
148 result != NO_ERROR) {
149 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
150 strerror(-result));
151 return;
152 }
153 if (changes) {
154 applyChangedTypesToLayers(changes->changedTypes);
155 applyDisplayRequests(changes->displayRequests);
156 applyLayerRequestsToLayers(changes->layerRequests);
157 }
158
159 // Determine what type of composition we are doing from the final state
160 auto& state = editState();
161 state.usesClientComposition = anyLayersRequireClientComposition();
162 state.usesDeviceComposition = !allLayersRequireClientComposition();
163}
164
Lloyd Pique688abd42019-02-15 15:42:24 -0800165bool Display::getSkipColorTransform() const {
166 if (!mId) {
167 return false;
168 }
169
170 auto& hwc = getCompositionEngine().getHwComposer();
171 return hwc.hasDisplayCapability(*mId, HWC2::DisplayCapability::SkipClientColorTransform);
172}
173
Lloyd Pique66d68602019-02-13 14:23:31 -0800174bool Display::anyLayersRequireClientComposition() const {
175 const auto& layers = getOutputLayersOrderedByZ();
176 return std::any_of(layers.cbegin(), layers.cend(),
177 [](const auto& layer) { return layer->requiresClientComposition(); });
178}
179
180bool Display::allLayersRequireClientComposition() const {
181 const auto& layers = getOutputLayersOrderedByZ();
182 return std::all_of(layers.cbegin(), layers.cend(),
183 [](const auto& layer) { return layer->requiresClientComposition(); });
184}
185
186void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
187 if (changedTypes.empty()) {
188 return;
189 }
190
191 for (auto& layer : getOutputLayersOrderedByZ()) {
192 auto hwcLayer = layer->getHwcLayer();
193 if (!hwcLayer) {
194 continue;
195 }
196
197 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
198 layer->applyDeviceCompositionTypeChange(
199 static_cast<Hwc2::IComposerClient::Composition>(it->second));
200 }
201 }
202}
203
204void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
205 auto& state = editState();
206 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
207 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0;
208 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
209}
210
211void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
212 for (auto& layer : getOutputLayersOrderedByZ()) {
213 layer->prepareForDeviceLayerRequests();
214
215 auto hwcLayer = layer->getHwcLayer();
216 if (!hwcLayer) {
217 continue;
218 }
219
220 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
221 layer->applyDeviceLayerRequest(
222 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
223 }
224 }
225}
226
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800227compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
228 auto result = impl::Output::presentAndGetFrameFences();
229
230 if (!mId) {
231 return result;
232 }
233
234 auto& hwc = getCompositionEngine().getHwComposer();
235 hwc.presentAndGetReleaseFences(*mId);
236
237 result.presentFence = hwc.getPresentFence(*mId);
238
239 // TODO(b/121291683): Change HWComposer call to return entire map
240 for (const auto& layer : getOutputLayersOrderedByZ()) {
241 auto hwcLayer = layer->getHwcLayer();
242 if (!hwcLayer) {
243 continue;
244 }
245
246 result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
247 }
248
249 hwc.clearReleaseFences(*mId);
250
251 return result;
252}
253
Lloyd Pique688abd42019-02-15 15:42:24 -0800254void Display::setExpensiveRenderingExpected(bool enabled) {
255 Output::setExpensiveRenderingExpected(enabled);
256
257 if (mPowerAdvisor && mId) {
258 mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled);
259 }
260}
261
Lloyd Pique45a165a2018-10-19 11:54:47 -0700262} // namespace android::compositionengine::impl