blob: 77b1940e23181bf87a1d6323526a4dd4dab021e5 [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>
Vishnu Nairbe0ad902024-06-27 23:38:43 +000018#include <common/trace.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070019#include <compositionengine/CompositionEngine.h>
Lloyd Piqued3d69882019-02-28 16:03:46 -080020#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070021#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070022#include <compositionengine/DisplaySurface.h>
Lloyd Piquedf336d92019-03-07 21:38:42 -080023#include <compositionengine/LayerFE.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070024#include <compositionengine/impl/Display.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070025#include <compositionengine/impl/DisplayColorProfile.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070026#include <compositionengine/impl/DumpHelpers.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080027#include <compositionengine/impl/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070028#include <compositionengine/impl/RenderSurface.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070029
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080030// TODO(b/129481165): remove the #pragma below and fix conversion issues
31#pragma clang diagnostic push
32#pragma clang diagnostic ignored "-Wconversion"
33
Lloyd Pique45a165a2018-10-19 11:54:47 -070034#include "DisplayHardware/HWComposer.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080035
36// TODO(b/129481165): remove the #pragma below and fix conversion issues
37#pragma clang diagnostic pop // ignored "-Wconversion"
38
Lloyd Pique688abd42019-02-15 15:42:24 -080039#include "DisplayHardware/PowerAdvisor.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070040
Ady Abrahamde549d42022-01-26 19:19:17 -080041using aidl::android::hardware::graphics::composer3::Capability;
Leon Scroggins III5967aec2021-12-29 11:14:22 -050042using aidl::android::hardware::graphics::composer3::DisplayCapability;
43
Lloyd Pique45a165a2018-10-19 11:54:47 -070044namespace android::compositionengine::impl {
45
Lloyd Pique35fca9d2019-02-13 14:24:11 -080046std::shared_ptr<Display> createDisplay(
Lloyd Pique45a165a2018-10-19 11:54:47 -070047 const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070048 const compositionengine::DisplayCreationArgs& args) {
49 return createDisplayTemplated<Display>(compositionEngine, args);
Lloyd Pique45a165a2018-10-19 11:54:47 -070050}
51
Lloyd Pique45a165a2018-10-19 11:54:47 -070052Display::~Display() = default;
53
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070054void Display::setConfiguration(const compositionengine::DisplayCreationArgs& args) {
Dominik Laskowski13948602021-03-08 20:48:28 -080055 mId = args.id;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070056 mPowerAdvisor = args.powerAdvisor;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070057 editState().isSecure = args.isSecure;
Chavi Weingarten18fa7c62023-11-28 21:16:03 +000058 editState().isProtected = args.isProtected;
Angel Aguayob084e0c2021-08-04 23:27:28 +000059 editState().displaySpace.setBounds(args.pixels);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070060 setName(args.name);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070061}
62
63bool Display::isValid() const {
64 return Output::isValid() && mPowerAdvisor;
65}
66
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020067DisplayId Display::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -070068 return mId;
69}
70
71bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070072 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070073}
74
Huihong Luo9ebb7a72023-06-27 17:01:50 -070075void Display::setSecure(bool secure) {
76 editState().isSecure = secure;
77}
78
Lloyd Pique45a165a2018-10-19 11:54:47 -070079bool Display::isVirtual() const {
Alan Dingc9de92a2024-05-24 00:27:11 -070080 return mId.isVirtual();
Lloyd Pique45a165a2018-10-19 11:54:47 -070081}
82
Lloyd Pique6c564cf2019-05-17 17:31:36 -070083std::optional<DisplayId> Display::getDisplayId() const {
84 return mId;
85}
86
Lloyd Pique45a165a2018-10-19 11:54:47 -070087void Display::disconnect() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020088 if (mIsDisconnected) {
Lloyd Pique45a165a2018-10-19 11:54:47 -070089 return;
90 }
91
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020092 mIsDisconnected = true;
Dominik Laskowski13948602021-03-08 20:48:28 -080093
94 if (const auto id = HalDisplayId::tryCast(mId)) {
95 getCompositionEngine().getHwComposer().disconnectDisplay(*id);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020096 }
Lloyd Pique45a165a2018-10-19 11:54:47 -070097}
98
Lloyd Pique3eb1b212019-03-07 21:15:40 -080099void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
100 Output::setColorTransform(args);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200101 const auto halDisplayId = HalDisplayId::tryCast(mId);
102 if (mIsDisconnected || !halDisplayId || CC_LIKELY(!args.colorTransformMatrix)) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800103 return;
104 }
Lloyd Pique32cbe282018-10-19 13:09:22 -0700105
106 auto& hwc = getCompositionEngine().getHwComposer();
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200107 status_t result = hwc.setColorTransform(*halDisplayId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700108 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200109 to_string(mId).c_str(), result);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700110}
111
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800112void Display::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800113 if (colorProfile.mode == getState().colorMode &&
114 colorProfile.dataspace == getState().dataspace &&
Alec Mouri88790f32023-07-21 01:25:14 +0000115 colorProfile.renderIntent == getState().renderIntent) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700116 return;
117 }
118
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700119 if (isVirtual()) {
120 ALOGW("%s: Invalid operation on virtual display", __func__);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700121 return;
122 }
123
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800124 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700125
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200126 const auto physicalId = PhysicalDisplayId::tryCast(mId);
127 LOG_FATAL_IF(!physicalId);
128 getCompositionEngine().getHwComposer().setActiveColorMode(*physicalId, colorProfile.mode,
129 colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700130}
131
132void Display::dump(std::string& out) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700133 const char* const type = isVirtual() ? "virtual" : "physical";
134 base::StringAppendF(&out, "Display %s (%s, \"%s\")", to_string(mId).c_str(), type,
135 getName().c_str());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700136
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700137 out.append("\n Composition Display State:\n");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700138 Output::dumpBase(out);
139}
140
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700141void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
142 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700143}
144
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700145void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
146 setRenderSurface(
147 compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700148}
149
Vishnu Nair9b079a22020-01-21 14:36:08 -0800150void Display::createClientCompositionCache(uint32_t cacheSize) {
151 cacheClientCompositionRequests(cacheSize);
152}
153
Lloyd Piquedf336d92019-03-07 21:38:42 -0800154std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
Lloyd Piquedf336d92019-03-07 21:38:42 -0800155 const sp<compositionengine::LayerFE>& layerFE) const {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200156 auto outputLayer = impl::createOutputLayer(*this, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800157
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200158 if (const auto halDisplayId = HalDisplayId::tryCast(mId);
159 outputLayer && !mIsDisconnected && halDisplayId) {
Lloyd Piquedf336d92019-03-07 21:38:42 -0800160 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700161 auto hwcLayer = hwc.createLayer(*halDisplayId);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800162 ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
163 getName().c_str());
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200164 outputLayer->setHwcLayer(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -0800165 }
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200166 return outputLayer;
Lloyd Piquedf336d92019-03-07 21:38:42 -0800167}
168
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800169void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
170 Output::setReleasedLayers(refreshArgs);
171
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200172 if (mIsDisconnected || GpuVirtualDisplayId::tryCast(mId) ||
173 refreshArgs.layersWithQueuedFrames.empty()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800174 return;
175 }
176
177 // For layers that are being removed from a HWC display, and that have
178 // queued frames, add them to a a list of released layers so we can properly
179 // set a fence.
180 compositionengine::Output::ReleasedLayers releasedLayers;
181
182 // Any non-null entries in the current list of layers are layers that are no
183 // longer going to be visible
Lloyd Piquede196652020-01-22 17:29:58 -0800184 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
185 if (!outputLayer) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800186 continue;
187 }
188
Lloyd Piquede196652020-01-22 17:29:58 -0800189 compositionengine::LayerFE* layerFE = &outputLayer->getLayerFE();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800190 const bool hasQueuedFrames =
Lloyd Piquede196652020-01-22 17:29:58 -0800191 std::any_of(refreshArgs.layersWithQueuedFrames.cbegin(),
192 refreshArgs.layersWithQueuedFrames.cend(),
193 [layerFE](sp<compositionengine::LayerFE> layerWithQueuedFrames) {
194 return layerFE == layerWithQueuedFrames.get();
195 });
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800196
197 if (hasQueuedFrames) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700198 releasedLayers.emplace_back(wp<LayerFE>::fromExisting(layerFE));
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800199 }
200 }
201
202 setReleasedLayers(std::move(releasedLayers));
203}
204
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400205void Display::applyDisplayBrightness(bool applyImmediately) {
206 if (const auto displayId = ftl::Optional(getDisplayId()).and_then(PhysicalDisplayId::tryCast);
207 displayId && getState().displayBrightness) {
208 auto& hwc = getCompositionEngine().getHwComposer();
joenchene72ba5e2022-08-24 13:08:58 +0000209 const status_t result =
Dominik Laskowskia42d5392022-09-29 14:26:53 -0400210 hwc.setDisplayBrightness(*displayId, *getState().displayBrightness,
joenchene72ba5e2022-08-24 13:08:58 +0000211 getState().displayBrightnessNits,
212 Hwc2::Composer::DisplayBrightnessOptions{
213 .applyImmediately = applyImmediately})
214 .get();
215 ALOGE_IF(result != NO_ERROR, "setDisplayBrightness failed for %s: %d, (%s)",
216 getName().c_str(), result, strerror(-result));
217 }
218 // Clear out the display brightness now that it's been communicated to composer.
219 editState().displayBrightness.reset();
220}
221
Vishnu Naira3140382022-02-24 14:07:11 -0800222void Display::beginFrame() {
223 Output::beginFrame();
Lloyd Pique66d68602019-02-13 14:23:31 -0800224
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200225 // If we don't have a HWC display, then we are done.
226 const auto halDisplayId = HalDisplayId::tryCast(mId);
227 if (!halDisplayId) {
Robert Carr6fe2bef2022-03-09 13:49:41 -0800228 return;
Lloyd Pique66d68602019-02-13 14:23:31 -0800229 }
230
joenchene72ba5e2022-08-24 13:08:58 +0000231 applyDisplayBrightness(false);
Vishnu Naira3140382022-02-24 14:07:11 -0800232}
Alec Mouricdf16792021-12-10 13:16:06 -0800233
Vishnu Naira3140382022-02-24 14:07:11 -0800234bool Display::chooseCompositionStrategy(
235 std::optional<android::HWComposer::DeviceRequestedChanges>* outChanges) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000236 SFTRACE_FORMAT("%s for %s", __func__, getNamePlusId().c_str());
Vishnu Naira3140382022-02-24 14:07:11 -0800237 ALOGV(__FUNCTION__);
238
239 if (mIsDisconnected) {
240 return false;
241 }
242
243 // If we don't have a HWC display, then we are done.
244 const auto halDisplayId = HalDisplayId::tryCast(mId);
245 if (!halDisplayId) {
246 return false;
247 }
248
249 // Get any composition changes requested by the HWC device, and apply them.
Vishnu Naira3140382022-02-24 14:07:11 -0800250 auto& hwc = getCompositionEngine().getHwComposer();
Matt Buckley50c44062022-01-17 20:48:10 +0000251 const bool requiresClientComposition = anyLayersRequireClientComposition();
Matt Buckley3d398a02022-10-28 09:03:37 +0000252
Matt Buckley3d398a02022-10-28 09:03:37 +0000253 const TimePoint hwcValidateStartTime = TimePoint::now();
254
ramindani4aac32c2023-10-30 14:13:30 -0700255 if (status_t result = hwc.getDeviceCompositionChanges(*halDisplayId, requiresClientComposition,
256 getState().earliestPresentTime,
257 getState().expectedPresentTime,
258 getState().frameInterval, outChanges);
Lloyd Pique66d68602019-02-13 14:23:31 -0800259 result != NO_ERROR) {
260 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
261 strerror(-result));
Vishnu Naira3140382022-02-24 14:07:11 -0800262 return false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800263 }
Vishnu Naira3140382022-02-24 14:07:11 -0800264
Matt Buckley50c44062022-01-17 20:48:10 +0000265 if (isPowerHintSessionEnabled()) {
Matt Buckley3d398a02022-10-28 09:03:37 +0000266 mPowerAdvisor->setHwcValidateTiming(mId, hwcValidateStartTime, TimePoint::now());
267 if (auto halDisplayId = HalDisplayId::tryCast(mId)) {
268 mPowerAdvisor->setSkippedValidate(mId, hwc.getValidateSkipped(*halDisplayId));
269 }
Matt Buckley50c44062022-01-17 20:48:10 +0000270 }
271
Vishnu Naira3140382022-02-24 14:07:11 -0800272 return true;
273}
274
275void Display::applyCompositionStrategy(const std::optional<DeviceRequestedChanges>& changes) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800276 if (changes) {
277 applyChangedTypesToLayers(changes->changedTypes);
278 applyDisplayRequests(changes->displayRequests);
279 applyLayerRequestsToLayers(changes->layerRequests);
Alec Mouri85065692022-03-18 00:58:26 +0000280 applyClientTargetRequests(changes->clientTargetProperty);
Lloyd Pique66d68602019-02-13 14:23:31 -0800281 }
282
283 // Determine what type of composition we are doing from the final state
284 auto& state = editState();
285 state.usesClientComposition = anyLayersRequireClientComposition();
286 state.usesDeviceComposition = !allLayersRequireClientComposition();
287}
288
Lloyd Pique688abd42019-02-15 15:42:24 -0800289bool Display::getSkipColorTransform() const {
Dominik Laskowski1162e472020-04-02 19:02:47 -0700290 const auto& hwc = getCompositionEngine().getHwComposer();
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200291 if (const auto halDisplayId = HalDisplayId::tryCast(mId)) {
292 return hwc.hasDisplayCapability(*halDisplayId,
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500293 DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200294 }
295
Ady Abrahamde549d42022-01-26 19:19:17 -0800296 return hwc.hasCapability(Capability::SKIP_CLIENT_COLOR_TRANSFORM);
Lloyd Pique688abd42019-02-15 15:42:24 -0800297}
298
Lloyd Pique66d68602019-02-13 14:23:31 -0800299bool Display::allLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700300 const auto layers = getOutputLayersOrderedByZ();
301 return std::all_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800302 [](const auto& layer) { return layer->requiresClientComposition(); });
303}
304
305void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
306 if (changedTypes.empty()) {
307 return;
308 }
309
Lloyd Pique01c77c12019-04-17 12:48:32 -0700310 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800311 auto hwcLayer = layer->getHwcLayer();
312 if (!hwcLayer) {
313 continue;
314 }
315
316 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
317 layer->applyDeviceCompositionTypeChange(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500318 static_cast<aidl::android::hardware::graphics::composer3::Composition>(
319 it->second));
Lloyd Pique66d68602019-02-13 14:23:31 -0800320 }
321 }
322}
323
324void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
325 auto& state = editState();
326 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
Peiyong Line9d809e2020-04-14 13:10:48 -0700327 static_cast<uint32_t>(hal::DisplayRequest::FLIP_CLIENT_TARGET)) != 0;
Lloyd Pique66d68602019-02-13 14:23:31 -0800328 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
329}
330
331void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700332 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800333 layer->prepareForDeviceLayerRequests();
334
335 auto hwcLayer = layer->getHwcLayer();
336 if (!hwcLayer) {
337 continue;
338 }
339
340 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
341 layer->applyDeviceLayerRequest(
342 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
343 }
344 }
345}
346
Alec Mouri85065692022-03-18 00:58:26 +0000347void Display::applyClientTargetRequests(const ClientTargetProperty& clientTargetProperty) {
348 if (static_cast<ui::Dataspace>(clientTargetProperty.clientTargetProperty.dataspace) ==
349 ui::Dataspace::UNKNOWN) {
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700350 return;
351 }
Ady Abraham0094dc62021-06-03 10:08:33 -0700352
Alec Mouri85065692022-03-18 00:58:26 +0000353 editState().dataspace =
354 static_cast<ui::Dataspace>(clientTargetProperty.clientTargetProperty.dataspace);
355 editState().clientTargetBrightness = clientTargetProperty.brightness;
356 editState().clientTargetDimmingStage = clientTargetProperty.dimmingStage;
357 getRenderSurface()->setBufferDataspace(editState().dataspace);
358 getRenderSurface()->setBufferPixelFormat(
359 static_cast<ui::PixelFormat>(clientTargetProperty.clientTargetProperty.pixelFormat));
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700360}
361
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400362void Display::executeCommands() {
363 const auto halDisplayIdOpt = HalDisplayId::tryCast(mId);
364 if (mIsDisconnected || !halDisplayIdOpt) {
365 return;
366 }
367
368 getCompositionEngine().getHwComposer().executeCommands(*halDisplayIdOpt);
369}
370
Leon Scroggins IIIc1623d12023-11-06 15:31:05 -0500371compositionengine::Output::FrameFences Display::presentFrame() {
372 auto fences = impl::Output::presentFrame();
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800373
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200374 const auto halDisplayIdOpt = HalDisplayId::tryCast(mId);
375 if (mIsDisconnected || !halDisplayIdOpt) {
376 return fences;
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800377 }
378
379 auto& hwc = getCompositionEngine().getHwComposer();
Matt Buckley50c44062022-01-17 20:48:10 +0000380
Matt Buckley2fa85012022-08-30 22:38:45 +0000381 const TimePoint startTime = TimePoint::now();
Matt Buckley50c44062022-01-17 20:48:10 +0000382
Ady Abrahamf1db8032023-03-24 17:52:34 -0700383 if (isPowerHintSessionEnabled() && getState().earliestPresentTime) {
384 mPowerAdvisor->setHwcPresentDelayedTime(mId, *getState().earliestPresentTime);
Matt Buckley50c44062022-01-17 20:48:10 +0000385 }
386
Ady Abrahamf1db8032023-03-24 17:52:34 -0700387 hwc.presentAndGetReleaseFences(*halDisplayIdOpt, getState().earliestPresentTime);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800388
Matt Buckley50c44062022-01-17 20:48:10 +0000389 if (isPowerHintSessionEnabled()) {
Matt Buckley2fa85012022-08-30 22:38:45 +0000390 mPowerAdvisor->setHwcPresentTiming(mId, startTime, TimePoint::now());
Matt Buckley50c44062022-01-17 20:48:10 +0000391 }
392
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200393 fences.presentFence = hwc.getPresentFence(*halDisplayIdOpt);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800394
395 // TODO(b/121291683): Change HWComposer call to return entire map
Lloyd Pique01c77c12019-04-17 12:48:32 -0700396 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800397 auto hwcLayer = layer->getHwcLayer();
398 if (!hwcLayer) {
399 continue;
400 }
401
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200402 fences.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*halDisplayIdOpt, hwcLayer));
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800403 }
404
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200405 hwc.clearReleaseFences(*halDisplayIdOpt);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800406
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200407 return fences;
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800408}
409
Lloyd Pique688abd42019-02-15 15:42:24 -0800410void Display::setExpensiveRenderingExpected(bool enabled) {
411 Output::setExpensiveRenderingExpected(enabled);
412
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200413 if (mPowerAdvisor && !GpuVirtualDisplayId::tryCast(mId)) {
414 mPowerAdvisor->setExpensiveRenderingExpected(mId, enabled);
Lloyd Pique688abd42019-02-15 15:42:24 -0800415 }
416}
417
Matt Buckley50c44062022-01-17 20:48:10 +0000418bool Display::isPowerHintSessionEnabled() {
419 return mPowerAdvisor != nullptr && mPowerAdvisor->usePowerHintSession();
420}
421
Xiang Wangcb50bbd2024-04-18 16:57:54 -0700422bool Display::isPowerHintSessionGpuReportingEnabled() {
423 return mPowerAdvisor != nullptr && mPowerAdvisor->supportsGpuReporting();
424}
425
Xiang Wangaab31162024-03-12 19:48:08 -0700426// For ADPF GPU v0 this is expected to set start time to when the GPU commands are submitted with
427// fence returned, i.e. when RenderEngine flushes the commands and returns the draw fence.
428void Display::setHintSessionGpuStart(TimePoint startTime) {
429 mPowerAdvisor->setGpuStartTime(mId, startTime);
430}
431
Matt Buckley50c44062022-01-17 20:48:10 +0000432void Display::setHintSessionGpuFence(std::unique_ptr<FenceTime>&& gpuFence) {
433 mPowerAdvisor->setGpuFenceTime(mId, std::move(gpuFence));
434}
435
Xiang Wangaab31162024-03-12 19:48:08 -0700436void Display::setHintSessionRequiresRenderEngine(bool requiresRenderEngine) {
437 mPowerAdvisor->setRequiresRenderEngine(mId, requiresRenderEngine);
438}
439
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000440void Display::finishFrame(GpuCompositionResult&& result) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800441 // We only need to actually compose the display if:
442 // 1) It is being handled by hardware composer, which may need this to
443 // keep its virtual display state machine in sync, or
444 // 2) There is work to be done (the dirty region isn't empty)
Chavi Weingarten09fa1d62022-08-17 21:57:04 +0000445 if (GpuVirtualDisplayId::tryCast(mId) && !mustRecompose()) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200446 ALOGV("Skipping display composition");
447 return;
Lloyd Piqued3d69882019-02-28 16:03:46 -0800448 }
449
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000450 impl::Output::finishFrame(std::move(result));
Lloyd Piqued3d69882019-02-28 16:03:46 -0800451}
452
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400453bool Display::supportsOffloadPresent() const {
454 if (const auto halDisplayId = HalDisplayId::tryCast(mId)) {
455 const auto& hwc = getCompositionEngine().getHwComposer();
456 return hwc.hasDisplayCapability(*halDisplayId, DisplayCapability::MULTI_THREADED_PRESENT);
457 }
458
459 return false;
460}
461
Lloyd Pique45a165a2018-10-19 11:54:47 -0700462} // namespace android::compositionengine::impl