blob: 68319013d743ddb959f7586bd70b6a37906918ad [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"
29
30namespace android::compositionengine::impl {
31
Lloyd Pique35fca9d2019-02-13 14:24:11 -080032std::shared_ptr<Display> createDisplay(
Lloyd Pique45a165a2018-10-19 11:54:47 -070033 const compositionengine::CompositionEngine& compositionEngine,
34 compositionengine::DisplayCreationArgs&& args) {
35 return std::make_shared<Display>(compositionEngine, std::move(args));
36}
37
38Display::Display(const CompositionEngine& compositionEngine, DisplayCreationArgs&& args)
Lloyd Pique32cbe282018-10-19 13:09:22 -070039 : compositionengine::impl::Output(compositionEngine),
Lloyd Pique45a165a2018-10-19 11:54:47 -070040 mIsVirtual(args.isVirtual),
Lloyd Pique32cbe282018-10-19 13:09:22 -070041 mId(args.displayId) {
42 editState().isSecure = args.isSecure;
43}
Lloyd Pique45a165a2018-10-19 11:54:47 -070044
45Display::~Display() = default;
46
47const std::optional<DisplayId>& Display::getId() const {
48 return mId;
49}
50
51bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070052 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070053}
54
55bool Display::isVirtual() const {
56 return mIsVirtual;
57}
58
59void Display::disconnect() {
60 if (!mId) {
61 return;
62 }
63
Lloyd Pique32cbe282018-10-19 13:09:22 -070064 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique45a165a2018-10-19 11:54:47 -070065 hwc.disconnectDisplay(*mId);
66 mId.reset();
67}
68
Lloyd Pique32cbe282018-10-19 13:09:22 -070069void Display::setColorTransform(const mat4& transform) {
70 Output::setColorTransform(transform);
71
72 auto& hwc = getCompositionEngine().getHwComposer();
73 status_t result = hwc.setColorTransform(*mId, transform);
74 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
75 mId ? to_string(*mId).c_str() : "", result);
76}
77
78void Display::setColorMode(ui::ColorMode mode, ui::Dataspace dataspace,
Lloyd Piquef5275482019-01-29 18:42:42 -080079 ui::RenderIntent renderIntent,
80 ui::Dataspace colorSpaceAgnosticDataspace) {
81 ui::Dataspace targetDataspace =
82 getDisplayColorProfile()->getTargetDataspace(mode, dataspace,
83 colorSpaceAgnosticDataspace);
84
Lloyd Pique32cbe282018-10-19 13:09:22 -070085 if (mode == getState().colorMode && dataspace == getState().dataspace &&
Lloyd Piquef5275482019-01-29 18:42:42 -080086 renderIntent == getState().renderIntent && targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070087 return;
88 }
89
90 if (mIsVirtual) {
91 ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
92 return;
93 }
94
Lloyd Piquef5275482019-01-29 18:42:42 -080095 Output::setColorMode(mode, dataspace, renderIntent, colorSpaceAgnosticDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -070096
97 auto& hwc = getCompositionEngine().getHwComposer();
98 hwc.setActiveColorMode(*mId, mode, renderIntent);
99}
100
101void Display::dump(std::string& out) const {
102 using android::base::StringAppendF;
103
104 StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str());
105
106 out.append("\n ");
107
108 dumpVal(out, "isVirtual", mIsVirtual);
109 if (mId) {
110 dumpVal(out, "hwcId", to_string(*mId));
111 } else {
112 StringAppendF(&out, "no hwcId, ");
113 }
114
115 out.append("\n");
116
117 Output::dumpBase(out);
118}
119
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700120void Display::createDisplayColorProfile(DisplayColorProfileCreationArgs&& args) {
121 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(std::move(args)));
122}
123
Lloyd Pique31cb2942018-10-19 17:23:03 -0700124void Display::createRenderSurface(RenderSurfaceCreationArgs&& args) {
125 setRenderSurface(compositionengine::impl::createRenderSurface(getCompositionEngine(), *this,
126 std::move(args)));
127}
128
Lloyd Pique66d68602019-02-13 14:23:31 -0800129void Display::chooseCompositionStrategy() {
130 ATRACE_CALL();
131 ALOGV(__FUNCTION__);
132
133 // Default to the base settings -- client composition only.
134 Output::chooseCompositionStrategy();
135
136 // If we don't have a HWC display, then we are done
137 if (!mId) {
138 return;
139 }
140
141 // Get any composition changes requested by the HWC device, and apply them.
142 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
143 auto& hwc = getCompositionEngine().getHwComposer();
144 if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
145 &changes);
146 result != NO_ERROR) {
147 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
148 strerror(-result));
149 return;
150 }
151 if (changes) {
152 applyChangedTypesToLayers(changes->changedTypes);
153 applyDisplayRequests(changes->displayRequests);
154 applyLayerRequestsToLayers(changes->layerRequests);
155 }
156
157 // Determine what type of composition we are doing from the final state
158 auto& state = editState();
159 state.usesClientComposition = anyLayersRequireClientComposition();
160 state.usesDeviceComposition = !allLayersRequireClientComposition();
161}
162
163bool Display::anyLayersRequireClientComposition() const {
164 const auto& layers = getOutputLayersOrderedByZ();
165 return std::any_of(layers.cbegin(), layers.cend(),
166 [](const auto& layer) { return layer->requiresClientComposition(); });
167}
168
169bool Display::allLayersRequireClientComposition() const {
170 const auto& layers = getOutputLayersOrderedByZ();
171 return std::all_of(layers.cbegin(), layers.cend(),
172 [](const auto& layer) { return layer->requiresClientComposition(); });
173}
174
175void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
176 if (changedTypes.empty()) {
177 return;
178 }
179
180 for (auto& layer : getOutputLayersOrderedByZ()) {
181 auto hwcLayer = layer->getHwcLayer();
182 if (!hwcLayer) {
183 continue;
184 }
185
186 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
187 layer->applyDeviceCompositionTypeChange(
188 static_cast<Hwc2::IComposerClient::Composition>(it->second));
189 }
190 }
191}
192
193void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
194 auto& state = editState();
195 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
196 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0;
197 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
198}
199
200void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
201 for (auto& layer : getOutputLayersOrderedByZ()) {
202 layer->prepareForDeviceLayerRequests();
203
204 auto hwcLayer = layer->getHwcLayer();
205 if (!hwcLayer) {
206 continue;
207 }
208
209 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
210 layer->applyDeviceLayerRequest(
211 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
212 }
213 }
214}
215
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800216compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
217 auto result = impl::Output::presentAndGetFrameFences();
218
219 if (!mId) {
220 return result;
221 }
222
223 auto& hwc = getCompositionEngine().getHwComposer();
224 hwc.presentAndGetReleaseFences(*mId);
225
226 result.presentFence = hwc.getPresentFence(*mId);
227
228 // TODO(b/121291683): Change HWComposer call to return entire map
229 for (const auto& layer : getOutputLayersOrderedByZ()) {
230 auto hwcLayer = layer->getHwcLayer();
231 if (!hwcLayer) {
232 continue;
233 }
234
235 result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
236 }
237
238 hwc.clearReleaseFences(*mId);
239
240 return result;
241}
242
Lloyd Pique45a165a2018-10-19 11:54:47 -0700243} // namespace android::compositionengine::impl