blob: 690d35f068dd8f5aeab2cb573a03e4c757313dd3 [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>
Leon Scroggins III5a655b82022-09-07 13:17:09 -040028#include <gui/TraceUtils.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080029
Lloyd Pique66d68602019-02-13 14:23:31 -080030#include <utils/Trace.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070031
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080032// TODO(b/129481165): remove the #pragma below and fix conversion issues
33#pragma clang diagnostic push
34#pragma clang diagnostic ignored "-Wconversion"
35
Lloyd Pique45a165a2018-10-19 11:54:47 -070036#include "DisplayHardware/HWComposer.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080037
38// TODO(b/129481165): remove the #pragma below and fix conversion issues
39#pragma clang diagnostic pop // ignored "-Wconversion"
40
Lloyd Pique688abd42019-02-15 15:42:24 -080041#include "DisplayHardware/PowerAdvisor.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070042
Ady Abrahamde549d42022-01-26 19:19:17 -080043using aidl::android::hardware::graphics::composer3::Capability;
Leon Scroggins III5967aec2021-12-29 11:14:22 -050044using aidl::android::hardware::graphics::composer3::DisplayCapability;
45
Lloyd Pique45a165a2018-10-19 11:54:47 -070046namespace android::compositionengine::impl {
47
Lloyd Pique35fca9d2019-02-13 14:24:11 -080048std::shared_ptr<Display> createDisplay(
Lloyd Pique45a165a2018-10-19 11:54:47 -070049 const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070050 const compositionengine::DisplayCreationArgs& args) {
51 return createDisplayTemplated<Display>(compositionEngine, args);
Lloyd Pique45a165a2018-10-19 11:54:47 -070052}
53
Lloyd Pique45a165a2018-10-19 11:54:47 -070054Display::~Display() = default;
55
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070056void Display::setConfiguration(const compositionengine::DisplayCreationArgs& args) {
Dominik Laskowski13948602021-03-08 20:48:28 -080057 mId = args.id;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070058 mPowerAdvisor = args.powerAdvisor;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070059 editState().isSecure = args.isSecure;
Chavi Weingarten18fa7c62023-11-28 21:16:03 +000060 editState().isProtected = args.isProtected;
Angel Aguayob084e0c2021-08-04 23:27:28 +000061 editState().displaySpace.setBounds(args.pixels);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070062 setName(args.name);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070063}
64
65bool Display::isValid() const {
66 return Output::isValid() && mPowerAdvisor;
67}
68
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020069DisplayId Display::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -070070 return mId;
71}
72
73bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070074 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070075}
76
77bool Display::isVirtual() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -070078 return VirtualDisplayId::tryCast(mId).has_value();
Lloyd Pique45a165a2018-10-19 11:54:47 -070079}
80
Lloyd Pique6c564cf2019-05-17 17:31:36 -070081std::optional<DisplayId> Display::getDisplayId() const {
82 return mId;
83}
84
Lloyd Pique45a165a2018-10-19 11:54:47 -070085void Display::disconnect() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020086 if (mIsDisconnected) {
Lloyd Pique45a165a2018-10-19 11:54:47 -070087 return;
88 }
89
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020090 mIsDisconnected = true;
Dominik Laskowski13948602021-03-08 20:48:28 -080091
92 if (const auto id = HalDisplayId::tryCast(mId)) {
93 getCompositionEngine().getHwComposer().disconnectDisplay(*id);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020094 }
Lloyd Pique45a165a2018-10-19 11:54:47 -070095}
96
Lloyd Pique3eb1b212019-03-07 21:15:40 -080097void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
98 Output::setColorTransform(args);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020099 const auto halDisplayId = HalDisplayId::tryCast(mId);
100 if (mIsDisconnected || !halDisplayId || CC_LIKELY(!args.colorTransformMatrix)) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800101 return;
102 }
Lloyd Pique32cbe282018-10-19 13:09:22 -0700103
104 auto& hwc = getCompositionEngine().getHwComposer();
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200105 status_t result = hwc.setColorTransform(*halDisplayId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700106 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200107 to_string(mId).c_str(), result);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700108}
109
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800110void Display::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800111 if (colorProfile.mode == getState().colorMode &&
112 colorProfile.dataspace == getState().dataspace &&
Alec Mouri88790f32023-07-21 01:25:14 +0000113 colorProfile.renderIntent == getState().renderIntent) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700114 return;
115 }
116
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700117 if (isVirtual()) {
118 ALOGW("%s: Invalid operation on virtual display", __func__);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700119 return;
120 }
121
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800122 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700123
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200124 const auto physicalId = PhysicalDisplayId::tryCast(mId);
125 LOG_FATAL_IF(!physicalId);
126 getCompositionEngine().getHwComposer().setActiveColorMode(*physicalId, colorProfile.mode,
127 colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700128}
129
130void Display::dump(std::string& out) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700131 const char* const type = isVirtual() ? "virtual" : "physical";
132 base::StringAppendF(&out, "Display %s (%s, \"%s\")", to_string(mId).c_str(), type,
133 getName().c_str());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700134
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700135 out.append("\n Composition Display State:\n");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700136 Output::dumpBase(out);
137}
138
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700139void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
140 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700141}
142
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700143void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
144 setRenderSurface(
145 compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700146}
147
Vishnu Nair9b079a22020-01-21 14:36:08 -0800148void Display::createClientCompositionCache(uint32_t cacheSize) {
149 cacheClientCompositionRequests(cacheSize);
150}
151
Lloyd Piquedf336d92019-03-07 21:38:42 -0800152std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
Lloyd Piquedf336d92019-03-07 21:38:42 -0800153 const sp<compositionengine::LayerFE>& layerFE) const {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200154 auto outputLayer = impl::createOutputLayer(*this, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800155
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200156 if (const auto halDisplayId = HalDisplayId::tryCast(mId);
157 outputLayer && !mIsDisconnected && halDisplayId) {
Lloyd Piquedf336d92019-03-07 21:38:42 -0800158 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700159 auto hwcLayer = hwc.createLayer(*halDisplayId);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800160 ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
161 getName().c_str());
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200162 outputLayer->setHwcLayer(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -0800163 }
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200164 return outputLayer;
Lloyd Piquedf336d92019-03-07 21:38:42 -0800165}
166
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800167void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
168 Output::setReleasedLayers(refreshArgs);
169
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200170 if (mIsDisconnected || GpuVirtualDisplayId::tryCast(mId) ||
171 refreshArgs.layersWithQueuedFrames.empty()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800172 return;
173 }
174
175 // For layers that are being removed from a HWC display, and that have
176 // queued frames, add them to a a list of released layers so we can properly
177 // set a fence.
178 compositionengine::Output::ReleasedLayers releasedLayers;
179
180 // Any non-null entries in the current list of layers are layers that are no
181 // longer going to be visible
Lloyd Piquede196652020-01-22 17:29:58 -0800182 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
183 if (!outputLayer) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800184 continue;
185 }
186
Lloyd Piquede196652020-01-22 17:29:58 -0800187 compositionengine::LayerFE* layerFE = &outputLayer->getLayerFE();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800188 const bool hasQueuedFrames =
Lloyd Piquede196652020-01-22 17:29:58 -0800189 std::any_of(refreshArgs.layersWithQueuedFrames.cbegin(),
190 refreshArgs.layersWithQueuedFrames.cend(),
191 [layerFE](sp<compositionengine::LayerFE> layerWithQueuedFrames) {
192 return layerFE == layerWithQueuedFrames.get();
193 });
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800194
195 if (hasQueuedFrames) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700196 releasedLayers.emplace_back(wp<LayerFE>::fromExisting(layerFE));
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800197 }
198 }
199
200 setReleasedLayers(std::move(releasedLayers));
201}
202
joenchene72ba5e2022-08-24 13:08:58 +0000203void Display::applyDisplayBrightness(const bool applyImmediately) {
204 auto& hwc = getCompositionEngine().getHwComposer();
205 const auto halDisplayId = HalDisplayId::tryCast(*getDisplayId());
206 if (const auto physicalDisplayId = PhysicalDisplayId::tryCast(*halDisplayId);
207 physicalDisplayId && getState().displayBrightness) {
208 const status_t result =
209 hwc.setDisplayBrightness(*physicalDisplayId, *getState().displayBrightness,
210 getState().displayBrightnessNits,
211 Hwc2::Composer::DisplayBrightnessOptions{
212 .applyImmediately = applyImmediately})
213 .get();
214 ALOGE_IF(result != NO_ERROR, "setDisplayBrightness failed for %s: %d, (%s)",
215 getName().c_str(), result, strerror(-result));
216 }
217 // Clear out the display brightness now that it's been communicated to composer.
218 editState().displayBrightness.reset();
219}
220
Vishnu Naira3140382022-02-24 14:07:11 -0800221void Display::beginFrame() {
222 Output::beginFrame();
Lloyd Pique66d68602019-02-13 14:23:31 -0800223
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200224 // If we don't have a HWC display, then we are done.
225 const auto halDisplayId = HalDisplayId::tryCast(mId);
226 if (!halDisplayId) {
Robert Carr6fe2bef2022-03-09 13:49:41 -0800227 return;
Lloyd Pique66d68602019-02-13 14:23:31 -0800228 }
229
joenchene72ba5e2022-08-24 13:08:58 +0000230 applyDisplayBrightness(false);
Vishnu Naira3140382022-02-24 14:07:11 -0800231}
Alec Mouricdf16792021-12-10 13:16:06 -0800232
Vishnu Naira3140382022-02-24 14:07:11 -0800233bool Display::chooseCompositionStrategy(
234 std::optional<android::HWComposer::DeviceRequestedChanges>* outChanges) {
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400235 ATRACE_FORMAT("%s for %s", __func__, getNamePlusId().c_str());
Vishnu Naira3140382022-02-24 14:07:11 -0800236 ALOGV(__FUNCTION__);
237
238 if (mIsDisconnected) {
239 return false;
240 }
241
242 // If we don't have a HWC display, then we are done.
243 const auto halDisplayId = HalDisplayId::tryCast(mId);
244 if (!halDisplayId) {
245 return false;
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();
Matt Buckley50c44062022-01-17 20:48:10 +0000251 const bool requiresClientComposition = anyLayersRequireClientComposition();
Matt Buckley3d398a02022-10-28 09:03:37 +0000252
253 if (isPowerHintSessionEnabled()) {
254 mPowerAdvisor->setRequiresClientComposition(mId, requiresClientComposition);
255 }
256
257 const TimePoint hwcValidateStartTime = TimePoint::now();
258
ramindani4aac32c2023-10-30 14:13:30 -0700259 if (status_t result = hwc.getDeviceCompositionChanges(*halDisplayId, requiresClientComposition,
260 getState().earliestPresentTime,
261 getState().expectedPresentTime,
262 getState().frameInterval, outChanges);
Lloyd Pique66d68602019-02-13 14:23:31 -0800263 result != NO_ERROR) {
264 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
265 strerror(-result));
Vishnu Naira3140382022-02-24 14:07:11 -0800266 return false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800267 }
Vishnu Naira3140382022-02-24 14:07:11 -0800268
Matt Buckley50c44062022-01-17 20:48:10 +0000269 if (isPowerHintSessionEnabled()) {
Matt Buckley3d398a02022-10-28 09:03:37 +0000270 mPowerAdvisor->setHwcValidateTiming(mId, hwcValidateStartTime, TimePoint::now());
271 if (auto halDisplayId = HalDisplayId::tryCast(mId)) {
272 mPowerAdvisor->setSkippedValidate(mId, hwc.getValidateSkipped(*halDisplayId));
273 }
Matt Buckley50c44062022-01-17 20:48:10 +0000274 }
275
Vishnu Naira3140382022-02-24 14:07:11 -0800276 return true;
277}
278
279void Display::applyCompositionStrategy(const std::optional<DeviceRequestedChanges>& changes) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800280 if (changes) {
281 applyChangedTypesToLayers(changes->changedTypes);
282 applyDisplayRequests(changes->displayRequests);
283 applyLayerRequestsToLayers(changes->layerRequests);
Alec Mouri85065692022-03-18 00:58:26 +0000284 applyClientTargetRequests(changes->clientTargetProperty);
Lloyd Pique66d68602019-02-13 14:23:31 -0800285 }
286
287 // Determine what type of composition we are doing from the final state
288 auto& state = editState();
289 state.usesClientComposition = anyLayersRequireClientComposition();
290 state.usesDeviceComposition = !allLayersRequireClientComposition();
291}
292
Lloyd Pique688abd42019-02-15 15:42:24 -0800293bool Display::getSkipColorTransform() const {
Dominik Laskowski1162e472020-04-02 19:02:47 -0700294 const auto& hwc = getCompositionEngine().getHwComposer();
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200295 if (const auto halDisplayId = HalDisplayId::tryCast(mId)) {
296 return hwc.hasDisplayCapability(*halDisplayId,
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500297 DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200298 }
299
Ady Abrahamde549d42022-01-26 19:19:17 -0800300 return hwc.hasCapability(Capability::SKIP_CLIENT_COLOR_TRANSFORM);
Lloyd Pique688abd42019-02-15 15:42:24 -0800301}
302
Lloyd Pique66d68602019-02-13 14:23:31 -0800303bool Display::allLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700304 const auto layers = getOutputLayersOrderedByZ();
305 return std::all_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800306 [](const auto& layer) { return layer->requiresClientComposition(); });
307}
308
309void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
310 if (changedTypes.empty()) {
311 return;
312 }
313
Lloyd Pique01c77c12019-04-17 12:48:32 -0700314 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800315 auto hwcLayer = layer->getHwcLayer();
316 if (!hwcLayer) {
317 continue;
318 }
319
320 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
321 layer->applyDeviceCompositionTypeChange(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500322 static_cast<aidl::android::hardware::graphics::composer3::Composition>(
323 it->second));
Lloyd Pique66d68602019-02-13 14:23:31 -0800324 }
325 }
326}
327
328void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
329 auto& state = editState();
330 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
Peiyong Line9d809e2020-04-14 13:10:48 -0700331 static_cast<uint32_t>(hal::DisplayRequest::FLIP_CLIENT_TARGET)) != 0;
Lloyd Pique66d68602019-02-13 14:23:31 -0800332 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
333}
334
335void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700336 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800337 layer->prepareForDeviceLayerRequests();
338
339 auto hwcLayer = layer->getHwcLayer();
340 if (!hwcLayer) {
341 continue;
342 }
343
344 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
345 layer->applyDeviceLayerRequest(
346 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
347 }
348 }
349}
350
Alec Mouri85065692022-03-18 00:58:26 +0000351void Display::applyClientTargetRequests(const ClientTargetProperty& clientTargetProperty) {
352 if (static_cast<ui::Dataspace>(clientTargetProperty.clientTargetProperty.dataspace) ==
353 ui::Dataspace::UNKNOWN) {
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700354 return;
355 }
Ady Abraham0094dc62021-06-03 10:08:33 -0700356
Alec Mouri85065692022-03-18 00:58:26 +0000357 editState().dataspace =
358 static_cast<ui::Dataspace>(clientTargetProperty.clientTargetProperty.dataspace);
359 editState().clientTargetBrightness = clientTargetProperty.brightness;
360 editState().clientTargetDimmingStage = clientTargetProperty.dimmingStage;
361 getRenderSurface()->setBufferDataspace(editState().dataspace);
362 getRenderSurface()->setBufferPixelFormat(
363 static_cast<ui::PixelFormat>(clientTargetProperty.clientTargetProperty.pixelFormat));
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700364}
365
Leon Scroggins IIIc1623d12023-11-06 15:31:05 -0500366compositionengine::Output::FrameFences Display::presentFrame() {
367 auto fences = impl::Output::presentFrame();
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800368
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200369 const auto halDisplayIdOpt = HalDisplayId::tryCast(mId);
370 if (mIsDisconnected || !halDisplayIdOpt) {
371 return fences;
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800372 }
373
374 auto& hwc = getCompositionEngine().getHwComposer();
Matt Buckley50c44062022-01-17 20:48:10 +0000375
Matt Buckley2fa85012022-08-30 22:38:45 +0000376 const TimePoint startTime = TimePoint::now();
Matt Buckley50c44062022-01-17 20:48:10 +0000377
Ady Abrahamf1db8032023-03-24 17:52:34 -0700378 if (isPowerHintSessionEnabled() && getState().earliestPresentTime) {
379 mPowerAdvisor->setHwcPresentDelayedTime(mId, *getState().earliestPresentTime);
Matt Buckley50c44062022-01-17 20:48:10 +0000380 }
381
Ady Abrahamf1db8032023-03-24 17:52:34 -0700382 hwc.presentAndGetReleaseFences(*halDisplayIdOpt, getState().earliestPresentTime);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800383
Matt Buckley50c44062022-01-17 20:48:10 +0000384 if (isPowerHintSessionEnabled()) {
Matt Buckley2fa85012022-08-30 22:38:45 +0000385 mPowerAdvisor->setHwcPresentTiming(mId, startTime, TimePoint::now());
Matt Buckley50c44062022-01-17 20:48:10 +0000386 }
387
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200388 fences.presentFence = hwc.getPresentFence(*halDisplayIdOpt);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800389
390 // TODO(b/121291683): Change HWComposer call to return entire map
Lloyd Pique01c77c12019-04-17 12:48:32 -0700391 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800392 auto hwcLayer = layer->getHwcLayer();
393 if (!hwcLayer) {
394 continue;
395 }
396
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200397 fences.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*halDisplayIdOpt, hwcLayer));
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800398 }
399
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200400 hwc.clearReleaseFences(*halDisplayIdOpt);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800401
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200402 return fences;
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800403}
404
Lloyd Pique688abd42019-02-15 15:42:24 -0800405void Display::setExpensiveRenderingExpected(bool enabled) {
406 Output::setExpensiveRenderingExpected(enabled);
407
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200408 if (mPowerAdvisor && !GpuVirtualDisplayId::tryCast(mId)) {
409 mPowerAdvisor->setExpensiveRenderingExpected(mId, enabled);
Lloyd Pique688abd42019-02-15 15:42:24 -0800410 }
411}
412
Matt Buckley50c44062022-01-17 20:48:10 +0000413bool Display::isPowerHintSessionEnabled() {
414 return mPowerAdvisor != nullptr && mPowerAdvisor->usePowerHintSession();
415}
416
417void Display::setHintSessionGpuFence(std::unique_ptr<FenceTime>&& gpuFence) {
418 mPowerAdvisor->setGpuFenceTime(mId, std::move(gpuFence));
419}
420
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000421void Display::finishFrame(GpuCompositionResult&& result) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800422 // We only need to actually compose the display if:
423 // 1) It is being handled by hardware composer, which may need this to
424 // keep its virtual display state machine in sync, or
425 // 2) There is work to be done (the dirty region isn't empty)
Chavi Weingarten09fa1d62022-08-17 21:57:04 +0000426 if (GpuVirtualDisplayId::tryCast(mId) && !mustRecompose()) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200427 ALOGV("Skipping display composition");
428 return;
Lloyd Piqued3d69882019-02-28 16:03:46 -0800429 }
430
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000431 impl::Output::finishFrame(std::move(result));
Lloyd Piqued3d69882019-02-28 16:03:46 -0800432}
433
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400434bool Display::supportsOffloadPresent() const {
435 if (const auto halDisplayId = HalDisplayId::tryCast(mId)) {
436 const auto& hwc = getCompositionEngine().getHwComposer();
437 return hwc.hasDisplayCapability(*halDisplayId, DisplayCapability::MULTI_THREADED_PRESENT);
438 }
439
440 return false;
441}
442
Lloyd Pique45a165a2018-10-19 11:54:47 -0700443} // namespace android::compositionengine::impl