blob: d2011048105ff55e9fd146ac498105d262b135ae [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 Pique3b5a69e2020-01-16 17:51:01 -080028
Lloyd Pique66d68602019-02-13 14:23:31 -080029#include <utils/Trace.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070030
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080031// TODO(b/129481165): remove the #pragma below and fix conversion issues
32#pragma clang diagnostic push
33#pragma clang diagnostic ignored "-Wconversion"
34
Lloyd Pique45a165a2018-10-19 11:54:47 -070035#include "DisplayHardware/HWComposer.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080036
37// TODO(b/129481165): remove the #pragma below and fix conversion issues
38#pragma clang diagnostic pop // ignored "-Wconversion"
39
Lloyd Pique688abd42019-02-15 15:42:24 -080040#include "DisplayHardware/PowerAdvisor.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070041
42namespace android::compositionengine::impl {
43
Lloyd Pique35fca9d2019-02-13 14:24:11 -080044std::shared_ptr<Display> createDisplay(
Lloyd Pique45a165a2018-10-19 11:54:47 -070045 const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070046 const compositionengine::DisplayCreationArgs& args) {
47 return createDisplayTemplated<Display>(compositionEngine, args);
Lloyd Pique45a165a2018-10-19 11:54:47 -070048}
49
Lloyd Pique45a165a2018-10-19 11:54:47 -070050Display::~Display() = default;
51
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070052void Display::setConfiguration(const compositionengine::DisplayCreationArgs& args) {
53 mIsVirtual = !args.physical;
54 mId = args.physical ? std::make_optional(args.physical->id) : std::nullopt;
55 mPowerAdvisor = args.powerAdvisor;
56
57 editState().isSecure = args.isSecure;
58
59 setLayerStackFilter(args.layerStackId,
60 args.physical ? args.physical->type == DisplayConnectionType::Internal
61 : false);
62 setName(args.name);
63
64 if (!args.physical && args.useHwcVirtualDisplays) {
65 mId = maybeAllocateDisplayIdForVirtualDisplay(args.pixels, args.pixelFormat);
66 }
67}
68
69std::optional<DisplayId> Display::maybeAllocateDisplayIdForVirtualDisplay(
70 ui::Size pixels, ui::PixelFormat pixelFormat) const {
71 auto& hwc = getCompositionEngine().getHwComposer();
72 return hwc.allocateVirtualDisplay(static_cast<uint32_t>(pixels.width),
73 static_cast<uint32_t>(pixels.height), &pixelFormat);
74}
75
76bool Display::isValid() const {
77 return Output::isValid() && mPowerAdvisor;
78}
79
Lloyd Pique45a165a2018-10-19 11:54:47 -070080const std::optional<DisplayId>& Display::getId() const {
81 return mId;
82}
83
84bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070085 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070086}
87
88bool Display::isVirtual() const {
89 return mIsVirtual;
90}
91
Lloyd Pique6c564cf2019-05-17 17:31:36 -070092std::optional<DisplayId> Display::getDisplayId() const {
93 return mId;
94}
95
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070096void Display::setDisplayIdForTesting(std::optional<DisplayId> displayId) {
97 mId = displayId;
98}
99
Lloyd Pique45a165a2018-10-19 11:54:47 -0700100void Display::disconnect() {
101 if (!mId) {
102 return;
103 }
104
Lloyd Pique32cbe282018-10-19 13:09:22 -0700105 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique45a165a2018-10-19 11:54:47 -0700106 hwc.disconnectDisplay(*mId);
107 mId.reset();
108}
109
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800110void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
111 Output::setColorTransform(args);
112
113 if (!mId || CC_LIKELY(!args.colorTransformMatrix)) {
114 return;
115 }
Lloyd Pique32cbe282018-10-19 13:09:22 -0700116
117 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800118 status_t result = hwc.setColorTransform(*mId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700119 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
120 mId ? to_string(*mId).c_str() : "", result);
121}
122
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800123void Display::setColorProfile(const ColorProfile& colorProfile) {
124 const ui::Dataspace targetDataspace =
125 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
126 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800127
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800128 if (colorProfile.mode == getState().colorMode &&
129 colorProfile.dataspace == getState().dataspace &&
130 colorProfile.renderIntent == getState().renderIntent &&
131 targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700132 return;
133 }
134
135 if (mIsVirtual) {
136 ALOGW("%s: Invalid operation on virtual display", __FUNCTION__);
137 return;
138 }
139
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800140 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700141
142 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800143 hwc.setActiveColorMode(*mId, colorProfile.mode, colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700144}
145
146void Display::dump(std::string& out) const {
147 using android::base::StringAppendF;
148
149 StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str());
150
151 out.append("\n ");
152
153 dumpVal(out, "isVirtual", mIsVirtual);
154 if (mId) {
155 dumpVal(out, "hwcId", to_string(*mId));
156 } else {
157 StringAppendF(&out, "no hwcId, ");
158 }
159
160 out.append("\n");
161
162 Output::dumpBase(out);
163}
164
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700165void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
166 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700167}
168
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700169void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
170 setRenderSurface(
171 compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700172}
173
Vishnu Nair9b079a22020-01-21 14:36:08 -0800174void Display::createClientCompositionCache(uint32_t cacheSize) {
175 cacheClientCompositionRequests(cacheSize);
176}
177
Lloyd Piquedf336d92019-03-07 21:38:42 -0800178std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
Lloyd Piquedf336d92019-03-07 21:38:42 -0800179 const sp<compositionengine::LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800180 auto result = impl::createOutputLayer(*this, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800181
182 if (result && mId) {
183 auto& hwc = getCompositionEngine().getHwComposer();
184 auto displayId = *mId;
185 // Note: For the moment we ensure it is safe to take a reference to the
186 // HWComposer implementation by destroying all the OutputLayers (and
187 // hence the HWC2::Layers they own) before setting a new HWComposer. See
188 // for example SurfaceFlinger::updateVrFlinger().
189 // TODO(b/121291683): Make this safer.
190 auto hwcLayer = std::shared_ptr<HWC2::Layer>(hwc.createLayer(displayId),
191 [&hwc, displayId](HWC2::Layer* layer) {
192 hwc.destroyLayer(displayId, layer);
193 });
194 ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
195 getName().c_str());
196 result->setHwcLayer(std::move(hwcLayer));
197 }
198 return result;
199}
200
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800201void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
202 Output::setReleasedLayers(refreshArgs);
203
204 if (!mId || refreshArgs.layersWithQueuedFrames.empty()) {
205 return;
206 }
207
208 // For layers that are being removed from a HWC display, and that have
209 // queued frames, add them to a a list of released layers so we can properly
210 // set a fence.
211 compositionengine::Output::ReleasedLayers releasedLayers;
212
213 // Any non-null entries in the current list of layers are layers that are no
214 // longer going to be visible
Lloyd Piquede196652020-01-22 17:29:58 -0800215 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
216 if (!outputLayer) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800217 continue;
218 }
219
Lloyd Piquede196652020-01-22 17:29:58 -0800220 compositionengine::LayerFE* layerFE = &outputLayer->getLayerFE();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800221 const bool hasQueuedFrames =
Lloyd Piquede196652020-01-22 17:29:58 -0800222 std::any_of(refreshArgs.layersWithQueuedFrames.cbegin(),
223 refreshArgs.layersWithQueuedFrames.cend(),
224 [layerFE](sp<compositionengine::LayerFE> layerWithQueuedFrames) {
225 return layerFE == layerWithQueuedFrames.get();
226 });
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800227
228 if (hasQueuedFrames) {
229 releasedLayers.emplace_back(layerFE);
230 }
231 }
232
233 setReleasedLayers(std::move(releasedLayers));
234}
235
Lloyd Pique66d68602019-02-13 14:23:31 -0800236void Display::chooseCompositionStrategy() {
237 ATRACE_CALL();
238 ALOGV(__FUNCTION__);
239
240 // Default to the base settings -- client composition only.
241 Output::chooseCompositionStrategy();
242
243 // If we don't have a HWC display, then we are done
244 if (!mId) {
245 return;
246 }
247
248 // Get any composition changes requested by the HWC device, and apply them.
249 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
250 auto& hwc = getCompositionEngine().getHwComposer();
251 if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
252 &changes);
253 result != NO_ERROR) {
254 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
255 strerror(-result));
256 return;
257 }
258 if (changes) {
259 applyChangedTypesToLayers(changes->changedTypes);
260 applyDisplayRequests(changes->displayRequests);
261 applyLayerRequestsToLayers(changes->layerRequests);
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700262 applyClientTargetRequests(changes->clientTargetProperty);
Lloyd Pique66d68602019-02-13 14:23:31 -0800263 }
264
265 // Determine what type of composition we are doing from the final state
266 auto& state = editState();
267 state.usesClientComposition = anyLayersRequireClientComposition();
268 state.usesDeviceComposition = !allLayersRequireClientComposition();
269}
270
Lloyd Pique688abd42019-02-15 15:42:24 -0800271bool Display::getSkipColorTransform() const {
Dominik Laskowski1162e472020-04-02 19:02:47 -0700272 const auto& hwc = getCompositionEngine().getHwComposer();
Peiyong Line9d809e2020-04-14 13:10:48 -0700273 return mId ? hwc.hasDisplayCapability(*mId, hal::DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM)
274 : hwc.hasCapability(hal::Capability::SKIP_CLIENT_COLOR_TRANSFORM);
Lloyd Pique688abd42019-02-15 15:42:24 -0800275}
276
Lloyd Pique66d68602019-02-13 14:23:31 -0800277bool Display::anyLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700278 const auto layers = getOutputLayersOrderedByZ();
279 return std::any_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800280 [](const auto& layer) { return layer->requiresClientComposition(); });
281}
282
283bool Display::allLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700284 const auto layers = getOutputLayersOrderedByZ();
285 return std::all_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800286 [](const auto& layer) { return layer->requiresClientComposition(); });
287}
288
289void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
290 if (changedTypes.empty()) {
291 return;
292 }
293
Lloyd Pique01c77c12019-04-17 12:48:32 -0700294 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800295 auto hwcLayer = layer->getHwcLayer();
296 if (!hwcLayer) {
297 continue;
298 }
299
300 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
301 layer->applyDeviceCompositionTypeChange(
302 static_cast<Hwc2::IComposerClient::Composition>(it->second));
303 }
304 }
305}
306
307void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
308 auto& state = editState();
309 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
Peiyong Line9d809e2020-04-14 13:10:48 -0700310 static_cast<uint32_t>(hal::DisplayRequest::FLIP_CLIENT_TARGET)) != 0;
Lloyd Pique66d68602019-02-13 14:23:31 -0800311 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
312}
313
314void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700315 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800316 layer->prepareForDeviceLayerRequests();
317
318 auto hwcLayer = layer->getHwcLayer();
319 if (!hwcLayer) {
320 continue;
321 }
322
323 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
324 layer->applyDeviceLayerRequest(
325 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
326 }
327 }
328}
329
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700330void Display::applyClientTargetRequests(const ClientTargetProperty& clientTargetProperty) {
331 if (clientTargetProperty.dataspace == ui::Dataspace::UNKNOWN) {
332 return;
333 }
334 auto outputState = editState();
335 outputState.dataspace = clientTargetProperty.dataspace;
336 getRenderSurface()->setBufferDataspace(clientTargetProperty.dataspace);
337 getRenderSurface()->setBufferPixelFormat(clientTargetProperty.pixelFormat);
338}
339
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800340compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
341 auto result = impl::Output::presentAndGetFrameFences();
342
343 if (!mId) {
344 return result;
345 }
346
347 auto& hwc = getCompositionEngine().getHwComposer();
348 hwc.presentAndGetReleaseFences(*mId);
349
350 result.presentFence = hwc.getPresentFence(*mId);
351
352 // TODO(b/121291683): Change HWComposer call to return entire map
Lloyd Pique01c77c12019-04-17 12:48:32 -0700353 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800354 auto hwcLayer = layer->getHwcLayer();
355 if (!hwcLayer) {
356 continue;
357 }
358
359 result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer));
360 }
361
362 hwc.clearReleaseFences(*mId);
363
364 return result;
365}
366
Lloyd Pique688abd42019-02-15 15:42:24 -0800367void Display::setExpensiveRenderingExpected(bool enabled) {
368 Output::setExpensiveRenderingExpected(enabled);
369
370 if (mPowerAdvisor && mId) {
371 mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled);
372 }
373}
374
Lloyd Piqued3d69882019-02-28 16:03:46 -0800375void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
376 // We only need to actually compose the display if:
377 // 1) It is being handled by hardware composer, which may need this to
378 // keep its virtual display state machine in sync, or
379 // 2) There is work to be done (the dirty region isn't empty)
380 if (!mId) {
381 if (getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) {
382 ALOGV("Skipping display composition");
383 return;
384 }
385 }
386
387 impl::Output::finishFrame(refreshArgs);
388}
389
Lloyd Pique45a165a2018-10-19 11:54:47 -0700390} // namespace android::compositionengine::impl