blob: d08e261e6c4f4661d235d5370c1b4f0fcbd74797 [file] [log] [blame]
Mathias Agopiana350ff92010-08-10 17:14:02 -07001/*
2 * Copyright (C) 2010 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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
ramindanic67d22c2023-11-28 14:58:47 -080018#include <chrono>
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080019#pragma clang diagnostic push
20#pragma clang diagnostic ignored "-Wconversion"
21
Dan Stoza9e56aa02015-11-02 13:00:03 -080022// #define LOG_NDEBUG 0
23
24#undef LOG_TAG
25#define LOG_TAG "HWComposer"
Mathias Agopian2965b262012-04-08 15:13:32 -070026#define ATRACE_TAG ATRACE_TAG_GRAPHICS
27
Lloyd Pique66d68602019-02-13 14:23:31 -080028#include "HWComposer.h"
29
Marin Shalamanovf8c63722020-10-06 13:11:21 +020030#include <android-base/properties.h>
Vishnu Nairbe0ad902024-06-27 23:38:43 +000031#include <common/trace.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include <compositionengine/Output.h>
33#include <compositionengine/OutputLayer.h>
34#include <compositionengine/impl/OutputLayerCompositionState.h>
Leon Scroggins III959a7ff2023-02-07 11:24:25 -050035#include <ftl/concat.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080036#include <log/log.h>
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070037#include <ui/DebugUtils.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <ui/GraphicBuffer.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080039#include <utils/Errors.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070040
Lloyd Pique66d68602019-02-13 14:23:31 -080041#include "../Layer.h" // needed only for debugging
Marin Shalamanovf8c63722020-10-06 13:11:21 +020042#include "../SurfaceFlingerProperties.h"
Lloyd Pique66d68602019-02-13 14:23:31 -080043#include "ComposerHal.h"
44#include "HWC2.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070045
Dominik Laskowskic1f18f62018-06-13 15:17:55 -070046#define LOG_HWC_DISPLAY_ERROR(hwcDisplayId, msg) \
47 ALOGE("%s failed for HWC display %" PRIu64 ": %s", __FUNCTION__, hwcDisplayId, msg)
48
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070049#define LOG_DISPLAY_ERROR(displayId, msg) \
Dominik Laskowski34157762018-10-31 13:07:19 -070050 ALOGE("%s failed for display %s: %s", __FUNCTION__, to_string(displayId).c_str(), msg)
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070051
Dominik Laskowski34157762018-10-31 13:07:19 -070052#define LOG_HWC_ERROR(what, error, displayId) \
53 ALOGE("%s: %s failed for display %s: %s (%d)", __FUNCTION__, what, \
54 to_string(displayId).c_str(), to_string(error).c_str(), static_cast<int32_t>(error))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070055
56#define RETURN_IF_INVALID_DISPLAY(displayId, ...) \
57 do { \
Dominik Laskowski075d3172018-05-24 15:50:06 -070058 if (mDisplayData.count(displayId) == 0) { \
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070059 LOG_DISPLAY_ERROR(displayId, "Invalid display"); \
60 return __VA_ARGS__; \
61 } \
62 } while (false)
63
64#define RETURN_IF_HWC_ERROR_FOR(what, error, displayId, ...) \
65 do { \
Peiyong Line9d809e2020-04-14 13:10:48 -070066 if (error != hal::Error::NONE) { \
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070067 LOG_HWC_ERROR(what, error, displayId); \
68 return __VA_ARGS__; \
69 } \
70 } while (false)
71
72#define RETURN_IF_HWC_ERROR(error, displayId, ...) \
73 RETURN_IF_HWC_ERROR_FOR(__FUNCTION__, error, displayId, __VA_ARGS__)
74
Kriti Dang674b9372022-11-18 10:58:44 +010075using aidl::android::hardware::graphics::common::HdrConversionCapability;
76using aidl::android::hardware::graphics::common::HdrConversionStrategy;
Ady Abrahamde549d42022-01-26 19:19:17 -080077using aidl::android::hardware::graphics::composer3::Capability;
78using aidl::android::hardware::graphics::composer3::DisplayCapability;
Lucas Berthou8d0a0c42024-08-27 14:32:31 +000079using aidl::android::hardware::graphics::composer3::DisplayConfiguration;
ramindani355fca82023-12-18 12:07:34 -080080using namespace std::string_literals;
Peiyong Line9d809e2020-04-14 13:10:48 -070081
Marin Shalamanov6e840172020-12-14 22:13:28 +010082namespace android {
Peiyong Linbdd08cc2019-12-17 21:35:14 -080083
Lloyd Pique441d5042018-10-18 16:49:51 -070084HWComposer::~HWComposer() = default;
85
86namespace impl {
87
Marin Shalamanovf8c63722020-10-06 13:11:21 +020088HWComposer::HWComposer(std::unique_ptr<Hwc2::Composer> composer)
89 : mComposer(std::move(composer)),
Dominik Laskowski13948602021-03-08 20:48:28 -080090 mMaxVirtualDisplayDimension(static_cast<size_t>(sysprop::max_virtual_display_dimension(0))),
Marin Shalamanovf8c63722020-10-06 13:11:21 +020091 mUpdateDeviceProductInfoOnHotplugReconnect(
ramindani355fca82023-12-18 12:07:34 -080092 sysprop::update_device_product_info_on_hotplug_reconnect(false)),
ramindanid2dc07a2024-02-16 16:34:03 -080093 mEnableVrrTimeout(base::GetBoolProperty("debug.sf.vrr_timeout_hint_enabled"s, true)) {}
Peiyong Linbdd08cc2019-12-17 21:35:14 -080094
95HWComposer::HWComposer(const std::string& composerServiceName)
Ady Abraham9fc28052021-10-14 17:21:38 -070096 : HWComposer(Hwc2::Composer::create(composerServiceName)) {}
Mathias Agopianbef42c52013-08-21 17:45:46 -070097
Dominik Laskowskib04f98a2018-11-07 21:07:16 -080098HWComposer::~HWComposer() {
99 mDisplayData.clear();
100}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800101
Yichi Chen3401b562022-01-17 15:42:35 +0800102void HWComposer::setCallback(HWC2::ComposerCallback& callback) {
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800103 loadCapabilities();
104 loadLayerMetadataSupport();
Sally Qibb866c12022-10-17 11:31:20 -0700105 loadOverlayProperties();
Kriti Dang674b9372022-11-18 10:58:44 +0100106 loadHdrConversionCapabilities();
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800107
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800108 if (mRegisteredCallback) {
109 ALOGW("Callback already registered. Ignored extra registration attempt.");
110 return;
111 }
112 mRegisteredCallback = true;
Dominik Laskowskiebc8d3a2021-04-16 23:18:31 -0700113
Yichi Chen3401b562022-01-17 15:42:35 +0800114 mComposer->registerCallback(callback);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800115}
116
Peiyong Line9d809e2020-04-14 13:10:48 -0700117bool HWComposer::getDisplayIdentificationData(hal::HWDisplayId hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700118 DisplayIdentificationData* outData) const {
Peiyong Line9d809e2020-04-14 13:10:48 -0700119 const auto error = static_cast<hal::Error>(
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800120 mComposer->getDisplayIdentificationData(hwcDisplayId, outPort, outData));
Peiyong Line9d809e2020-04-14 13:10:48 -0700121 if (error != hal::Error::NONE) {
122 if (error != hal::Error::UNSUPPORTED) {
Chia-I Wud0aff9d2018-06-21 13:39:09 +0800123 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, to_string(error).c_str());
124 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700125 return false;
126 }
127 return true;
128}
129
Ady Abrahamde549d42022-01-26 19:19:17 -0800130bool HWComposer::hasCapability(Capability capability) const {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800131 return mCapabilities.count(capability) > 0;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700132}
133
Ady Abrahamde549d42022-01-26 19:19:17 -0800134bool HWComposer::hasDisplayCapability(HalDisplayId displayId, DisplayCapability capability) const {
Dominik Laskowski1162e472020-04-02 19:02:47 -0700135 RETURN_IF_INVALID_DISPLAY(displayId, false);
Ady Abraham27fbcc72021-09-20 14:54:57 -0700136 return mDisplayData.at(displayId).hwcDisplay->hasCapability(capability);
Peiyong Lined531a32018-10-26 18:27:56 -0700137}
138
Peiyong Line9d809e2020-04-14 13:10:48 -0700139std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hal::HWDisplayId hwcDisplayId,
140 hal::Connection connection) {
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100141 switch (connection) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700142 case hal::Connection::CONNECTED:
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100143 return onHotplugConnect(hwcDisplayId);
Peiyong Line9d809e2020-04-14 13:10:48 -0700144 case hal::Connection::DISCONNECTED:
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100145 return onHotplugDisconnect(hwcDisplayId);
Peiyong Line9d809e2020-04-14 13:10:48 -0700146 case hal::Connection::INVALID:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700147 return {};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700148 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700149}
150
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200151bool HWComposer::updatesDeviceProductInfoOnHotplugReconnect() const {
152 return mUpdateDeviceProductInfoOnHotplugReconnect;
153}
154
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500155std::optional<PhysicalDisplayId> HWComposer::onVsync(hal::HWDisplayId hwcDisplayId,
156 nsecs_t timestamp) {
157 const auto displayIdOpt = toPhysicalDisplayId(hwcDisplayId);
158 if (!displayIdOpt) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700159 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid HWC display");
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500160 return {};
Jesse Hall1bd20e02012-08-29 10:47:52 -0700161 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700162
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500163 RETURN_IF_INVALID_DISPLAY(*displayIdOpt, {});
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700164
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500165 auto& displayData = mDisplayData[*displayIdOpt];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700166
Dan Stoza9e56aa02015-11-02 13:00:03 -0800167 {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800168 // There have been reports of HWCs that signal several vsync events
169 // with the same timestamp when turning the display off and on. This
170 // is a bug in the HWC implementation, but filter the extra events
171 // out here so they don't cause havoc downstream.
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400172 if (timestamp == displayData.lastPresentTimestamp) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700173 ALOGW("Ignoring duplicate VSYNC event from HWC for display %s (t=%" PRId64 ")",
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500174 to_string(*displayIdOpt).c_str(), timestamp);
175 return {};
Dan Stoza9e56aa02015-11-02 13:00:03 -0800176 }
177
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400178 displayData.lastPresentTimestamp = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700179 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800180
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000181 SFTRACE_INT(ftl::Concat("HW_VSYNC_", displayIdOpt->value).c_str(),
182 displayData.vsyncTraceToggle);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800183 displayData.vsyncTraceToggle = !displayData.vsyncTraceToggle;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800184
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500185 return displayIdOpt;
Jesse Hall1c569c42013-04-05 13:44:52 -0700186}
187
Dominik Laskowski13948602021-03-08 20:48:28 -0800188size_t HWComposer::getMaxVirtualDisplayCount() const {
189 return mComposer->getMaxVirtualDisplayCount();
190}
191
192size_t HWComposer::getMaxVirtualDisplayDimension() const {
193 return mMaxVirtualDisplayDimension;
194}
195
196bool HWComposer::allocateVirtualDisplay(HalVirtualDisplayId displayId, ui::Size resolution,
Dominik Laskowski263eec42021-07-21 23:13:24 -0700197 ui::PixelFormat* format) {
Dominik Laskowski13948602021-03-08 20:48:28 -0800198 if (!resolution.isValid()) {
199 ALOGE("%s: Invalid resolution %dx%d", __func__, resolution.width, resolution.height);
200 return false;
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800201 }
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200202
Dominik Laskowski13948602021-03-08 20:48:28 -0800203 const uint32_t width = static_cast<uint32_t>(resolution.width);
204 const uint32_t height = static_cast<uint32_t>(resolution.height);
205
206 if (mMaxVirtualDisplayDimension > 0 &&
207 (width > mMaxVirtualDisplayDimension || height > mMaxVirtualDisplayDimension)) {
208 ALOGE("%s: Resolution %ux%u exceeds maximum dimension %zu", __func__, width, height,
209 mMaxVirtualDisplayDimension);
210 return false;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200211 }
212
Dominik Laskowski13948602021-03-08 20:48:28 -0800213 hal::HWDisplayId hwcDisplayId;
Peiyong Line9d809e2020-04-14 13:10:48 -0700214 const auto error = static_cast<hal::Error>(
Dominik Laskowski263eec42021-07-21 23:13:24 -0700215 mComposer->createVirtualDisplay(width, height, format, &hwcDisplayId));
Dominik Laskowski13948602021-03-08 20:48:28 -0800216 RETURN_IF_HWC_ERROR_FOR("createVirtualDisplay", error, displayId, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800217
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800218 auto display = std::make_unique<HWC2::impl::Display>(*mComposer.get(), mCapabilities,
Peiyong Line9d809e2020-04-14 13:10:48 -0700219 hwcDisplayId, hal::DisplayType::VIRTUAL);
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800220 display->setConnected(true);
Dominik Laskowski13948602021-03-08 20:48:28 -0800221 auto& displayData = mDisplayData[displayId];
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800222 displayData.hwcDisplay = std::move(display);
Dominik Laskowski13948602021-03-08 20:48:28 -0800223 return true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700224}
225
Lucas Berthou8d0a0c42024-08-27 14:32:31 +0000226void HWComposer::allocatePhysicalDisplay(hal::HWDisplayId hwcDisplayId, PhysicalDisplayId displayId,
227 std::optional<ui::Size> physicalSize) {
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100228 mPhysicalDisplayIdMap[hwcDisplayId] = displayId;
229
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700230 if (!mPrimaryHwcDisplayId) {
231 mPrimaryHwcDisplayId = hwcDisplayId;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100232 }
233
234 auto& displayData = mDisplayData[displayId];
235 auto newDisplay =
236 std::make_unique<HWC2::impl::Display>(*mComposer.get(), mCapabilities, hwcDisplayId,
Peiyong Line9d809e2020-04-14 13:10:48 -0700237 hal::DisplayType::PHYSICAL);
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100238 newDisplay->setConnected(true);
Lucas Berthou8d0a0c42024-08-27 14:32:31 +0000239 newDisplay->setPhysicalSizeInMm(physicalSize);
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100240 displayData.hwcDisplay = std::move(newDisplay);
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100241}
242
243int32_t HWComposer::getAttribute(hal::HWDisplayId hwcDisplayId, hal::HWConfigId configId,
Marin Shalamanov045b7002021-01-07 16:56:24 +0100244 hal::Attribute attribute) const {
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100245 int32_t value = 0;
246 auto error = static_cast<hal::Error>(
247 mComposer->getDisplayAttribute(hwcDisplayId, configId, attribute, &value));
248
249 RETURN_IF_HWC_ERROR_FOR("getDisplayAttribute", error, *toPhysicalDisplayId(hwcDisplayId), -1);
250 return value;
251}
252
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700253std::shared_ptr<HWC2::Layer> HWComposer::createLayer(HalDisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700254 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
255
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700256 auto expected = mDisplayData[displayId].hwcDisplay->createLayer();
257 if (!expected.has_value()) {
258 auto error = std::move(expected).error();
259 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
260 }
261 return std::move(expected).value();
Steven Thomas94e35b92017-07-26 18:48:28 -0700262}
263
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200264bool HWComposer::isConnected(PhysicalDisplayId displayId) const {
Dominik Laskowskif2ddca62022-08-17 12:08:07 -0700265 return mDisplayData.count(displayId) && mDisplayData.at(displayId).hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700266}
267
ramindani263a3f12023-07-18 20:44:49 -0700268std::vector<HWComposer::HWCDisplayMode> HWComposer::getModes(PhysicalDisplayId displayId,
269 int32_t maxFrameIntervalNs) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700270 RETURN_IF_INVALID_DISPLAY(displayId, {});
271
Marin Shalamanov045b7002021-01-07 16:56:24 +0100272 const auto hwcDisplayId = mDisplayData.at(displayId).hwcDisplay->getId();
ramindani0cd1d8d2023-06-13 13:43:23 -0700273
ramindani19919ff2023-12-07 11:27:06 -0800274 if (mComposer->isVrrSupported()) {
ramindani263a3f12023-07-18 20:44:49 -0700275 return getModesFromDisplayConfigurations(hwcDisplayId, maxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700276 }
277
278 return getModesFromLegacyDisplayConfigs(hwcDisplayId);
279}
280
Lucas Berthou8d0a0c42024-08-27 14:32:31 +0000281DisplayConfiguration::Dpi HWComposer::getEstimatedDotsPerInchFromSize(
282 uint64_t hwcDisplayId, const HWCDisplayMode& hwcMode) const {
283 if (!FlagManager::getInstance().correct_dpi_with_display_size()) {
284 return {-1, -1};
285 }
286 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
287 if (const auto it = mDisplayData.find(displayId.value());
288 it != mDisplayData.end() && it->second.hwcDisplay->getPhysicalSizeInMm()) {
289 ui::Size size = it->second.hwcDisplay->getPhysicalSizeInMm().value();
290 if (hwcMode.width > 0 && hwcMode.height > 0 && size.width > 0 && size.height > 0) {
291 static constexpr float kMmPerInch = 25.4f;
292 return {hwcMode.width * kMmPerInch / size.width,
293 hwcMode.height * kMmPerInch / size.height};
294 }
295 }
296 }
297 return {-1, -1};
298}
299
300DisplayConfiguration::Dpi HWComposer::correctedDpiIfneeded(
301 DisplayConfiguration::Dpi dpi, DisplayConfiguration::Dpi estimatedDpi) const {
302 // hwc can be unreliable when it comes to dpi. A rough estimated dpi may yield better
303 // results. For instance, libdrm and bad edid may result in a dpi of {350, 290} for a
304 // 16:9 3840x2160 display, which would match a 4:3 aspect ratio.
305 // The logic here checks if hwc was able to provide some dpi, and if so if the dpi
306 // disparity between the axes is more reasonable than a rough estimate, otherwise use
307 // the estimated dpi as a corrected value.
Lucas Berthou685c3522024-09-16 14:39:05 +0000308 if (estimatedDpi.x == -1 || estimatedDpi.y == -1) {
Lucas Berthou8d0a0c42024-08-27 14:32:31 +0000309 return dpi;
310 }
311 if (dpi.x == -1 || dpi.y == -1) {
312 return estimatedDpi;
313 }
314 if (std::min(dpi.x, dpi.y) != 0 && std::min(estimatedDpi.x, estimatedDpi.y) != 0 &&
315 abs(dpi.x - dpi.y) / std::min(dpi.x, dpi.y) >
316 abs(estimatedDpi.x - estimatedDpi.y) / std::min(estimatedDpi.x, estimatedDpi.y)) {
317 return estimatedDpi;
318 }
319 return dpi;
320}
321
ramindani0cd1d8d2023-06-13 13:43:23 -0700322std::vector<HWComposer::HWCDisplayMode> HWComposer::getModesFromDisplayConfigurations(
ramindani263a3f12023-07-18 20:44:49 -0700323 uint64_t hwcDisplayId, int32_t maxFrameIntervalNs) const {
ramindani0cd1d8d2023-06-13 13:43:23 -0700324 std::vector<hal::DisplayConfiguration> configs;
ramindani263a3f12023-07-18 20:44:49 -0700325 auto error = static_cast<hal::Error>(
326 mComposer->getDisplayConfigurations(hwcDisplayId, maxFrameIntervalNs, &configs));
ramindani0cd1d8d2023-06-13 13:43:23 -0700327 RETURN_IF_HWC_ERROR_FOR("getDisplayConfigurations", error, *toPhysicalDisplayId(hwcDisplayId),
328 {});
329
330 std::vector<HWCDisplayMode> modes;
331 modes.reserve(configs.size());
332 for (auto config : configs) {
ramindania04b8a52023-08-07 18:49:47 -0700333 auto hwcMode = HWCDisplayMode{.hwcId = static_cast<hal::HWConfigId>(config.configId),
334 .width = config.width,
335 .height = config.height,
336 .vsyncPeriod = config.vsyncPeriod,
337 .configGroup = config.configGroup,
338 .vrrConfig = config.vrrConfig};
ramindani0cd1d8d2023-06-13 13:43:23 -0700339
Lucas Berthou8d0a0c42024-08-27 14:32:31 +0000340 const DisplayConfiguration::Dpi estimatedDPI =
341 getEstimatedDotsPerInchFromSize(hwcDisplayId, hwcMode);
ramindani0cd1d8d2023-06-13 13:43:23 -0700342 if (config.dpi) {
Lucas Berthou8d0a0c42024-08-27 14:32:31 +0000343 const DisplayConfiguration::Dpi dpi =
344 correctedDpiIfneeded(config.dpi.value(), estimatedDPI);
345 hwcMode.dpiX = dpi.x;
346 hwcMode.dpiY = dpi.y;
347 } else if (estimatedDPI.x != -1 && estimatedDPI.y != -1) {
348 hwcMode.dpiX = estimatedDPI.x;
349 hwcMode.dpiY = estimatedDPI.y;
ramindani0cd1d8d2023-06-13 13:43:23 -0700350 }
351
ramindani355fca82023-12-18 12:07:34 -0800352 if (!mEnableVrrTimeout) {
353 hwcMode.vrrConfig->notifyExpectedPresentConfig = {};
354 }
355
ramindani0cd1d8d2023-06-13 13:43:23 -0700356 modes.push_back(hwcMode);
357 }
358
359 return modes;
360}
361
362std::vector<HWComposer::HWCDisplayMode> HWComposer::getModesFromLegacyDisplayConfigs(
363 uint64_t hwcDisplayId) const {
Marin Shalamanov045b7002021-01-07 16:56:24 +0100364 std::vector<hal::HWConfigId> configIds;
365 auto error = static_cast<hal::Error>(mComposer->getDisplayConfigs(hwcDisplayId, &configIds));
366 RETURN_IF_HWC_ERROR_FOR("getDisplayConfigs", error, *toPhysicalDisplayId(hwcDisplayId), {});
367
368 std::vector<HWCDisplayMode> modes;
369 modes.reserve(configIds.size());
370 for (auto configId : configIds) {
ramindani0cd1d8d2023-06-13 13:43:23 -0700371 auto hwcMode = HWCDisplayMode{
Marin Shalamanov045b7002021-01-07 16:56:24 +0100372 .hwcId = configId,
373 .width = getAttribute(hwcDisplayId, configId, hal::Attribute::WIDTH),
374 .height = getAttribute(hwcDisplayId, configId, hal::Attribute::HEIGHT),
375 .vsyncPeriod = getAttribute(hwcDisplayId, configId, hal::Attribute::VSYNC_PERIOD),
Marin Shalamanov045b7002021-01-07 16:56:24 +0100376 .configGroup = getAttribute(hwcDisplayId, configId, hal::Attribute::CONFIG_GROUP),
ramindani0cd1d8d2023-06-13 13:43:23 -0700377 };
Marin Shalamanov045b7002021-01-07 16:56:24 +0100378
ramindani0cd1d8d2023-06-13 13:43:23 -0700379 const int32_t dpiX = getAttribute(hwcDisplayId, configId, hal::Attribute::DPI_X);
380 const int32_t dpiY = getAttribute(hwcDisplayId, configId, hal::Attribute::DPI_Y);
Lucas Berthou8d0a0c42024-08-27 14:32:31 +0000381 const DisplayConfiguration::Dpi hwcDpi =
382 DisplayConfiguration::Dpi{dpiX == -1 ? dpiY : dpiX / 1000.f,
383 dpiY == -1 ? dpiY : dpiY / 1000.f};
384 const DisplayConfiguration::Dpi estimatedDPI =
385 getEstimatedDotsPerInchFromSize(hwcDisplayId, hwcMode);
386 const DisplayConfiguration::Dpi dpi = correctedDpiIfneeded(hwcDpi, estimatedDPI);
387 hwcMode.dpiX = dpi.x;
388 hwcMode.dpiY = dpi.y;
ramindani0cd1d8d2023-06-13 13:43:23 -0700389
390 modes.push_back(hwcMode);
391 }
Marin Shalamanov045b7002021-01-07 16:56:24 +0100392 return modes;
Mathias Agopianda27af92012-09-13 18:17:13 -0700393}
394
Dominik Laskowskid940a012024-01-28 13:25:44 -0500395ftl::Expected<hal::HWConfigId, status_t> HWComposer::getActiveMode(
396 PhysicalDisplayId displayId) const {
397 RETURN_IF_INVALID_DISPLAY(displayId, ftl::Unexpected(BAD_INDEX));
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100398 const auto hwcId = *fromPhysicalDisplayId(displayId);
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700399
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100400 hal::HWConfigId configId;
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700401 const auto error = static_cast<hal::Error>(mComposer->getActiveConfig(hwcId, &configId));
Dominik Laskowskid940a012024-01-28 13:25:44 -0500402 if (error == hal::Error::BAD_CONFIG) {
403 return ftl::Unexpected(NO_INIT);
404 }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100405
Dominik Laskowskid940a012024-01-28 13:25:44 -0500406 RETURN_IF_HWC_ERROR_FOR("getActiveConfig", error, displayId, ftl::Unexpected(UNKNOWN_ERROR));
Marin Shalamanov045b7002021-01-07 16:56:24 +0100407 return configId;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700408}
409
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800410// Composer 2.4
411
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100412ui::DisplayConnectionType HWComposer::getDisplayConnectionType(PhysicalDisplayId displayId) const {
413 RETURN_IF_INVALID_DISPLAY(displayId, ui::DisplayConnectionType::Internal);
Dominik Laskowski55c85402020-01-21 16:25:47 -0800414 const auto& hwcDisplay = mDisplayData.at(displayId).hwcDisplay;
415
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500416 if (const auto connectionType = hwcDisplay->getConnectionType()) {
417 return connectionType.value();
418 } else {
419 LOG_HWC_ERROR(__func__, connectionType.error(), displayId);
420 return hwcDisplay->getId() == mPrimaryHwcDisplayId ? ui::DisplayConnectionType::Internal
421 : ui::DisplayConnectionType::External;
422 }
Dominik Laskowski55c85402020-01-21 16:25:47 -0800423}
424
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200425bool HWComposer::isVsyncPeriodSwitchSupported(PhysicalDisplayId displayId) const {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800426 RETURN_IF_INVALID_DISPLAY(displayId, false);
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800427 return mDisplayData.at(displayId).hwcDisplay->isVsyncPeriodSwitchSupported();
428}
429
Dominik Laskowskia109bb22024-01-28 13:33:51 -0500430ftl::Expected<nsecs_t, status_t> HWComposer::getDisplayVsyncPeriod(
431 PhysicalDisplayId displayId) const {
432 RETURN_IF_INVALID_DISPLAY(displayId, ftl::Unexpected(BAD_INDEX));
Dominik Laskowski55c85402020-01-21 16:25:47 -0800433
Marin Shalamanov045b7002021-01-07 16:56:24 +0100434 if (!isVsyncPeriodSwitchSupported(displayId)) {
Dominik Laskowskia109bb22024-01-28 13:33:51 -0500435 return ftl::Unexpected(INVALID_OPERATION);
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700436 }
Dominik Laskowskia109bb22024-01-28 13:33:51 -0500437
Marin Shalamanov045b7002021-01-07 16:56:24 +0100438 const auto hwcId = *fromPhysicalDisplayId(displayId);
439 Hwc2::VsyncPeriodNanos vsyncPeriodNanos = 0;
Dominik Laskowskia109bb22024-01-28 13:33:51 -0500440 const auto error =
Marin Shalamanov045b7002021-01-07 16:56:24 +0100441 static_cast<hal::Error>(mComposer->getDisplayVsyncPeriod(hwcId, &vsyncPeriodNanos));
Dominik Laskowskia109bb22024-01-28 13:33:51 -0500442 RETURN_IF_HWC_ERROR(error, displayId, ftl::Unexpected(UNKNOWN_ERROR));
443 return static_cast<nsecs_t>(vsyncPeriodNanos);
Lloyd Pique3c085a02018-05-09 19:38:32 -0700444}
445
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200446std::vector<ui::ColorMode> HWComposer::getColorModes(PhysicalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700447 RETURN_IF_INVALID_DISPLAY(displayId, {});
448
Peiyong Linfd997e02018-03-28 15:29:00 -0700449 std::vector<ui::ColorMode> modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700450 auto error = mDisplayData.at(displayId).hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700451 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600452 return modes;
453}
454
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200455status_t HWComposer::setActiveColorMode(PhysicalDisplayId displayId, ui::ColorMode mode,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700456 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700457 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700458
459 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700460 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700461 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
462 decodeRenderIntent(renderIntent) + ")")
463 .c_str(),
464 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700465
466 return NO_ERROR;
467}
468
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200469void HWComposer::setVsyncEnabled(PhysicalDisplayId displayId, hal::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700470 RETURN_IF_INVALID_DISPLAY(displayId);
471 auto& displayData = mDisplayData[displayId];
472
Dan Stoza9e56aa02015-11-02 13:00:03 -0800473 // NOTE: we use our own internal lock here because we have to call
474 // into the HWC with the lock held, and we want to make sure
475 // that even if HWC blocks (which it shouldn't), it won't
476 // affect other threads.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800477 std::lock_guard lock(displayData.vsyncEnabledLock);
478 if (enabled == displayData.vsyncEnabled) {
479 return;
Colin Cross10fbdb62012-07-12 17:56:34 -0700480 }
Dominik Laskowski1af47932018-11-12 10:20:46 -0800481
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000482 SFTRACE_CALL();
Dominik Laskowski1af47932018-11-12 10:20:46 -0800483 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
484 RETURN_IF_HWC_ERROR(error, displayId);
485
486 displayData.vsyncEnabled = enabled;
487
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000488 SFTRACE_INT(ftl::Concat("HW_VSYNC_ON_", displayId.value).c_str(),
489 enabled == hal::Vsync::ENABLE ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700490}
491
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200492status_t HWComposer::setClientTarget(HalDisplayId displayId, uint32_t slot,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700493 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
Alec Mourif97df4d2023-09-06 02:10:05 +0000494 ui::Dataspace dataspace, float hdrSdrRatio) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700495 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700496
Dominik Laskowski34157762018-10-31 13:07:19 -0700497 ALOGV("%s for display %s", __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800498 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Alec Mourif97df4d2023-09-06 02:10:05 +0000499 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace, hdrSdrRatio);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700500 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700501 return NO_ERROR;
502}
503
Lloyd Pique66d68602019-02-13 14:23:31 -0800504status_t HWComposer::getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200505 HalDisplayId displayId, bool frameUsesClientComposition,
Ady Abrahamf1db8032023-03-24 17:52:34 -0700506 std::optional<std::chrono::steady_clock::time_point> earliestPresentTime,
ramindani4aac32c2023-10-30 14:13:30 -0700507 nsecs_t expectedPresentTime, Fps frameInterval,
Lloyd Pique66d68602019-02-13 14:23:31 -0800508 std::optional<android::HWComposer::DeviceRequestedChanges>* outChanges) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000509 SFTRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800510
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700511 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800512
513 auto& displayData = mDisplayData[displayId];
514 auto& hwcDisplay = displayData.hwcDisplay;
515 if (!hwcDisplay->isConnected()) {
516 return NO_ERROR;
517 }
518
519 uint32_t numTypes = 0;
520 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700521
Peiyong Line9d809e2020-04-14 13:10:48 -0700522 hal::Error error = hal::Error::NONE;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700523
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700524 // First try to skip validate altogether. We can do that when
525 // 1. The previous frame has not been presented yet or already passed the
526 // earliest time to present. Otherwise, we may present a frame too early.
527 // 2. There is no client composition. Otherwise, we first need to render the
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700528 // client target buffer.
Ady Abraham43065bd2021-12-10 17:22:15 -0800529 const bool canSkipValidate = [&] {
530 // We must call validate if we have client composition
531 if (frameUsesClientComposition) {
532 return false;
533 }
534
535 // If composer supports getting the expected present time, we can skip
536 // as composer will make sure to prevent early presentation
Ady Abrahamf1db8032023-03-24 17:52:34 -0700537 if (!earliestPresentTime) {
Ady Abraham43065bd2021-12-10 17:22:15 -0800538 return true;
539 }
540
541 // composer doesn't support getting the expected present time. We can only
542 // skip validate if we know that we are not going to present early.
Ady Abrahamf1db8032023-03-24 17:52:34 -0700543 return std::chrono::steady_clock::now() >= *earliestPresentTime;
Ady Abraham43065bd2021-12-10 17:22:15 -0800544 }();
545
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700546 displayData.validateWasSkipped = false;
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000547 SFTRACE_FORMAT("NextFrameInterval %d_Hz", frameInterval.getIntValue());
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700548 if (canSkipValidate) {
Leon Scroggins IIIabc2c452023-11-08 17:04:51 -0500549 sp<Fence> outPresentFence = Fence::NO_FENCE;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700550 uint32_t state = UINT32_MAX;
ramindani4aac32c2023-10-30 14:13:30 -0700551 error = hwcDisplay->presentOrValidate(expectedPresentTime, frameInterval.getPeriodNsecs(),
552 &numTypes, &numRequests, &outPresentFence, &state);
Peiyong Line9d809e2020-04-14 13:10:48 -0700553 if (!hasChangesError(error)) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700554 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700555 }
556 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700557 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700558 error = hwcDisplay->getReleaseFences(&releaseFences);
559 displayData.releaseFences = std::move(releaseFences);
560 displayData.lastPresentFence = outPresentFence;
561 displayData.validateWasSkipped = true;
562 displayData.presentError = error;
563 return NO_ERROR;
564 }
565 // Present failed but Validate ran.
566 } else {
ramindani09acbb82023-11-03 09:02:38 -0700567 error = hwcDisplay->validate(expectedPresentTime, frameInterval.getPeriodNsecs(), &numTypes,
568 &numRequests);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700569 }
570 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Peiyong Line9d809e2020-04-14 13:10:48 -0700571 if (!hasChangesError(error)) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700572 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573 }
574
Lloyd Pique66d68602019-02-13 14:23:31 -0800575 android::HWComposer::DeviceRequestedChanges::ChangedTypes changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800576 changedTypes.reserve(numTypes);
577 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700578 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800579
Peiyong Line9d809e2020-04-14 13:10:48 -0700580 auto displayRequests = static_cast<hal::DisplayRequest>(0);
Lloyd Pique66d68602019-02-13 14:23:31 -0800581 android::HWComposer::DeviceRequestedChanges::LayerRequests layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582 layerRequests.reserve(numRequests);
Lloyd Pique66d68602019-02-13 14:23:31 -0800583 error = hwcDisplay->getRequests(&displayRequests, &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700584 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800585
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700586 DeviceRequestedChanges::ClientTargetProperty clientTargetProperty;
Alec Mouri85065692022-03-18 00:58:26 +0000587 error = hwcDisplay->getClientTargetProperty(&clientTargetProperty);
Sally Qi11dcd582024-08-16 18:11:27 -0700588 RETURN_IF_HWC_ERROR_FOR("getClientTargetProperty", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800589
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700590 outChanges->emplace(DeviceRequestedChanges{std::move(changedTypes), std::move(displayRequests),
591 std::move(layerRequests),
Alec Mouri85065692022-03-18 00:58:26 +0000592 std::move(clientTargetProperty)});
Dan Stoza9e56aa02015-11-02 13:00:03 -0800593 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700594 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800595
596 return NO_ERROR;
597}
598
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200599sp<Fence> HWComposer::getPresentFence(HalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700600 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700601 return mDisplayData.at(displayId).lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700602}
603
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400604nsecs_t HWComposer::getPresentTimestamp(PhysicalDisplayId displayId) const {
605 RETURN_IF_INVALID_DISPLAY(displayId, 0);
606 return mDisplayData.at(displayId).lastPresentTimestamp;
607}
608
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200609sp<Fence> HWComposer::getLayerReleaseFence(HalDisplayId displayId, HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700610 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Tim Murray2d3f8b82019-12-04 16:24:17 -0800611 const auto& displayFences = mDisplayData.at(displayId).releaseFences;
612 auto fence = displayFences.find(layer);
613 if (fence == displayFences.end()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800614 ALOGV("getLayerReleaseFence: Release fence not found");
615 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700616 }
Tim Murray2d3f8b82019-12-04 16:24:17 -0800617 return fence->second;
Riley Andrews03414a12014-07-01 14:22:59 -0700618}
619
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700620status_t HWComposer::presentAndGetReleaseFences(
Ady Abrahamf1db8032023-03-24 17:52:34 -0700621 HalDisplayId displayId,
622 std::optional<std::chrono::steady_clock::time_point> earliestPresentTime) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000623 SFTRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700624
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700625 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700626
Dan Stoza9e56aa02015-11-02 13:00:03 -0800627 auto& displayData = mDisplayData[displayId];
628 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700629
630 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700631 // explicitly flush all pending commands
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400632 auto error = static_cast<hal::Error>(mComposer->executeCommands(hwcDisplay->getId()));
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800633 RETURN_IF_HWC_ERROR_FOR("executeCommands", error, displayId, UNKNOWN_ERROR);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700634 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700635 return NO_ERROR;
636 }
637
Ady Abrahamf1db8032023-03-24 17:52:34 -0700638 if (earliestPresentTime) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000639 SFTRACE_NAME("wait for earliest present time");
Ady Abrahamf1db8032023-03-24 17:52:34 -0700640 std::this_thread::sleep_until(*earliestPresentTime);
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700641 }
642
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800643 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700644 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700645
Steven Thomas94e35b92017-07-26 18:48:28 -0700646 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800647 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700648 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800649
650 displayData.releaseFences = std::move(releaseFences);
651
652 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700653}
654
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400655status_t HWComposer::executeCommands(HalDisplayId displayId) {
656 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
657 auto error = static_cast<hal::Error>(mComposer->executeCommands(hwcDisplay->getId()));
658 RETURN_IF_HWC_ERROR_FOR("executeCommands", error, displayId, UNKNOWN_ERROR);
659 return NO_ERROR;
660}
661
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200662status_t HWComposer::setPowerMode(PhysicalDisplayId displayId, hal::PowerMode mode) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700663 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
664
Peiyong Line9d809e2020-04-14 13:10:48 -0700665 if (mode == hal::PowerMode::OFF) {
666 setVsyncEnabled(displayId, hal::Vsync::DISABLE);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800667 }
668
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700669 const auto& displayData = mDisplayData[displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700670 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800671 switch (mode) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700672 case hal::PowerMode::OFF:
673 case hal::PowerMode::ON:
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
675 {
676 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Line9d809e2020-04-14 13:10:48 -0700677 if (error != hal::Error::NONE) {
678 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(), error,
679 displayId);
Peiyong Lin306e4992018-05-07 16:18:22 -0700680 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700681 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800682 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700683 case hal::PowerMode::DOZE:
684 case hal::PowerMode::DOZE_SUSPEND:
Dan Stoza9e56aa02015-11-02 13:00:03 -0800685 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
686 {
687 bool supportsDoze = false;
Leon Scroggins III689c80f2023-06-05 17:49:32 -0400688 const auto queryDozeError = hwcDisplay->supportsDoze(&supportsDoze);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700689
Leon Scroggins III689c80f2023-06-05 17:49:32 -0400690 // queryDozeError might be NO_RESOURCES, in the case of a display that has never
691 // been turned on. In that case, attempt to set to DOZE anyway.
692 if (!supportsDoze && queryDozeError == hal::Error::NONE) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700693 mode = hal::PowerMode::ON;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800694 }
695
Leon Scroggins III689c80f2023-06-05 17:49:32 -0400696 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Line9d809e2020-04-14 13:10:48 -0700697 if (error != hal::Error::NONE) {
698 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(), error,
699 displayId);
Leon Scroggins III689c80f2023-06-05 17:49:32 -0400700 // If the display had never been turned on, so its doze
701 // support was unknown, it may truly not support doze. Try
702 // switching it to ON instead.
703 if (queryDozeError == hal::Error::NO_RESOURCES) {
704 ALOGD("%s: failed to set %s to %s. Trying again with ON", __func__,
705 to_string(displayId).c_str(), to_string(mode).c_str());
706 error = hwcDisplay->setPowerMode(hal::PowerMode::ON);
707 if (error != hal::Error::NONE) {
708 LOG_HWC_ERROR("setPowerMode(ON)", error, displayId);
709 }
710 }
Peiyong Lin306e4992018-05-07 16:18:22 -0700711 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800712 }
713 break;
714 default:
715 ALOGV("setPowerMode: Not calling HWC");
716 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700717 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800718
719 return NO_ERROR;
720}
721
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100722status_t HWComposer::setActiveModeWithConstraints(
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100723 PhysicalDisplayId displayId, hal::HWConfigId hwcModeId,
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200724 const hal::VsyncPeriodChangeConstraints& constraints,
Peiyong Line9d809e2020-04-14 13:10:48 -0700725 hal::VsyncPeriodChangeTimeline* outTimeline) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700726 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800727
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100728 auto error = mDisplayData[displayId].hwcDisplay->setActiveConfigWithConstraints(hwcModeId,
729 constraints,
730 outTimeline);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700731 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800732 return NO_ERROR;
733}
734
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200735status_t HWComposer::setColorTransform(HalDisplayId displayId, const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700736 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700737
738 auto& displayData = mDisplayData[displayId];
Ady Abrahamdc011a92021-12-21 14:06:44 -0800739 auto error = displayData.hwcDisplay->setColorTransform(transform);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700740 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700741 return NO_ERROR;
742}
743
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200744void HWComposer::disconnectDisplay(HalDisplayId displayId) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700745 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800746 auto& displayData = mDisplayData[displayId];
Dominik Laskowski7e045462018-05-30 13:02:02 -0700747 const auto hwcDisplayId = displayData.hwcDisplay->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700748
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800749 mPhysicalDisplayIdMap.erase(hwcDisplayId);
750 mDisplayData.erase(displayId);
Marin Shalamanov8b196592021-08-09 16:24:42 +0200751
752 // Reset the primary display ID if we're disconnecting it.
753 // This way isHeadless() will return false, which is necessary
754 // because getPrimaryDisplayId() will crash.
755 if (mPrimaryHwcDisplayId == hwcDisplayId) {
756 mPrimaryHwcDisplayId.reset();
757 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800758}
759
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200760status_t HWComposer::setOutputBuffer(HalVirtualDisplayId displayId, const sp<Fence>& acquireFence,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700761 const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700762 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700763 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700765 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700766 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800767 return NO_ERROR;
768}
769
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200770void HWComposer::clearReleaseFences(HalDisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700771 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800772 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700773}
774
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200775status_t HWComposer::getHdrCapabilities(HalDisplayId displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700776 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700777
778 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700779 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700780 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700781 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700782}
783
Sally Qibb866c12022-10-17 11:31:20 -0700784const aidl::android::hardware::graphics::composer3::OverlayProperties&
785HWComposer::getOverlaySupport() const {
786 return mOverlayProperties;
Sally Qi0cbd08b2022-08-17 12:12:28 -0700787}
788
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200789int32_t HWComposer::getSupportedPerFrameMetadata(HalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700790 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700791 return mDisplayData.at(displayId).hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700792}
793
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200794std::vector<ui::RenderIntent> HWComposer::getRenderIntents(HalDisplayId displayId,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700795 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700796 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700797
798 std::vector<ui::RenderIntent> renderIntents;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700799 auto error = mDisplayData.at(displayId).hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700800 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700801 return renderIntents;
802}
803
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200804mat4 HWComposer::getDataspaceSaturationMatrix(HalDisplayId displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700805 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700806
807 mat4 matrix;
808 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
809 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700810 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700811 return matrix;
812}
813
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200814status_t HWComposer::getDisplayedContentSamplingAttributes(HalDisplayId displayId,
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700815 ui::PixelFormat* outFormat,
816 ui::Dataspace* outDataspace,
817 uint8_t* outComponentMask) {
818 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
819 const auto error =
820 mDisplayData[displayId]
821 .hwcDisplay->getDisplayedContentSamplingAttributes(outFormat, outDataspace,
822 outComponentMask);
Peiyong Line9d809e2020-04-14 13:10:48 -0700823 if (error == hal::Error::UNSUPPORTED) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700824 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
825 return NO_ERROR;
826}
827
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200828status_t HWComposer::setDisplayContentSamplingEnabled(HalDisplayId displayId, bool enabled,
Kevin DuBois74e53772018-11-19 10:52:38 -0800829 uint8_t componentMask, uint64_t maxFrames) {
830 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
831 const auto error =
832 mDisplayData[displayId].hwcDisplay->setDisplayContentSamplingEnabled(enabled,
833 componentMask,
834 maxFrames);
835
Peiyong Line9d809e2020-04-14 13:10:48 -0700836 if (error == hal::Error::UNSUPPORTED) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
837 if (error == hal::Error::BAD_PARAMETER) RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Kevin DuBois74e53772018-11-19 10:52:38 -0800838 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
839 return NO_ERROR;
840}
841
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200842status_t HWComposer::getDisplayedContentSample(HalDisplayId displayId, uint64_t maxFrames,
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700843 uint64_t timestamp, DisplayedFrameStats* outStats) {
844 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
845 const auto error =
846 mDisplayData[displayId].hwcDisplay->getDisplayedContentSample(maxFrames, timestamp,
847 outStats);
848 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
849 return NO_ERROR;
850}
851
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700852ftl::Future<status_t> HWComposer::setDisplayBrightness(
Alec Mouri4d8a05d2022-03-23 18:14:26 +0000853 PhysicalDisplayId displayId, float brightness, float brightnessNits,
Alec Mouricdf16792021-12-10 13:16:06 -0800854 const Hwc2::Composer::DisplayBrightnessOptions& options) {
Dominik Laskowski4e2b71f2020-11-10 15:05:32 -0800855 RETURN_IF_INVALID_DISPLAY(displayId, ftl::yield<status_t>(BAD_INDEX));
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700856 auto& display = mDisplayData[displayId].hwcDisplay;
857
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700858 return display->setDisplayBrightness(brightness, brightnessNits, options)
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700859 .then([displayId](hal::Error error) -> status_t {
860 if (error == hal::Error::UNSUPPORTED) {
861 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
862 }
863 if (error == hal::Error::BAD_PARAMETER) {
864 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
865 }
866 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
867 return NO_ERROR;
868 });
Dan Gittik57e63c52019-01-18 16:37:54 +0000869}
870
Matt Buckley50c44062022-01-17 20:48:10 +0000871bool HWComposer::getValidateSkipped(HalDisplayId displayId) const {
872 if (mDisplayData.count(displayId) == 0) {
873 return false;
874 }
875 return mDisplayData.at(displayId).validateWasSkipped;
876}
877
Kriti Dang7defaf32021-11-15 11:55:43 +0100878status_t HWComposer::setBootDisplayMode(PhysicalDisplayId displayId,
879 hal::HWConfigId displayModeId) {
880 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
881 const auto error = mDisplayData[displayId].hwcDisplay->setBootDisplayConfig(displayModeId);
882 if (error == hal::Error::UNSUPPORTED) {
883 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
884 }
885 if (error == hal::Error::BAD_PARAMETER) {
886 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
887 }
888 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
889 return NO_ERROR;
890}
891
892status_t HWComposer::clearBootDisplayMode(PhysicalDisplayId displayId) {
893 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
894 const auto error = mDisplayData[displayId].hwcDisplay->clearBootDisplayConfig();
895 if (error == hal::Error::UNSUPPORTED) {
896 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
897 }
898 if (error == hal::Error::BAD_PARAMETER) {
899 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
900 }
901 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
902 return NO_ERROR;
903}
904
Kriti Dang16ca2972022-02-01 20:07:03 +0100905std::optional<hal::HWConfigId> HWComposer::getPreferredBootDisplayMode(
906 PhysicalDisplayId displayId) {
907 RETURN_IF_INVALID_DISPLAY(displayId, std::nullopt);
908 hal::HWConfigId displayModeId;
Kriti Dang7defaf32021-11-15 11:55:43 +0100909 const auto error =
910 mDisplayData[displayId].hwcDisplay->getPreferredBootDisplayConfig(&displayModeId);
Kriti Dang16ca2972022-02-01 20:07:03 +0100911 if (error != hal::Error::NONE) {
912 LOG_DISPLAY_ERROR(displayId, to_string(error).c_str());
913 return std::nullopt;
Kriti Dang7defaf32021-11-15 11:55:43 +0100914 }
Kriti Dang7defaf32021-11-15 11:55:43 +0100915 return displayModeId;
916}
917
Kriti Dang674b9372022-11-18 10:58:44 +0100918std::vector<HdrConversionCapability> HWComposer::getHdrConversionCapabilities() const {
919 return mHdrConversionCapabilities;
920}
921
Kriti Dangd432bb52023-02-09 18:21:04 +0100922status_t HWComposer::setHdrConversionStrategy(
923 HdrConversionStrategy hdrConversionStrategy,
924 aidl::android::hardware::graphics::common::Hdr* outPreferredHdrOutputType) {
925 const auto error =
926 mComposer->setHdrConversionStrategy(hdrConversionStrategy, outPreferredHdrOutputType);
Kriti Dang674b9372022-11-18 10:58:44 +0100927 if (error != hal::Error::NONE) {
928 ALOGE("Error in setting HDR conversion strategy %s", to_string(error).c_str());
Kriti Dangd432bb52023-02-09 18:21:04 +0100929 return INVALID_OPERATION;
Kriti Dang674b9372022-11-18 10:58:44 +0100930 }
931 return NO_ERROR;
932}
933
ramindanib2158ee2023-02-13 20:29:59 -0800934status_t HWComposer::setRefreshRateChangedCallbackDebugEnabled(PhysicalDisplayId displayId,
935 bool enabled) {
936 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
937 const auto error =
938 mComposer->setRefreshRateChangedCallbackDebugEnabled(mDisplayData[displayId]
939 .hwcDisplay->getId(),
940 enabled);
941 if (error != hal::Error::NONE) {
942 ALOGE("Error in setting refresh refresh rate change callback debug enabled %s",
943 to_string(error).c_str());
944 return INVALID_OPERATION;
945 }
946 return NO_ERROR;
947}
948
ramindanic67d22c2023-11-28 14:58:47 -0800949status_t HWComposer::notifyExpectedPresent(PhysicalDisplayId displayId,
950 TimePoint expectedPresentTime, Fps frameInterval) {
ramindani3acaaf52023-09-25 10:31:27 -0700951 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000952 SFTRACE_FORMAT("%s ExpectedPresentTime in %.2fms frameInterval %.2fms", __func__,
953 ticks<std::milli, float>(expectedPresentTime - TimePoint::now()),
954 ticks<std::milli, float>(Duration::fromNs(frameInterval.getPeriodNsecs())));
ramindanic67d22c2023-11-28 14:58:47 -0800955 const auto error = mComposer->notifyExpectedPresent(mDisplayData[displayId].hwcDisplay->getId(),
ramindanif8c0f102023-10-09 12:42:57 -0700956 expectedPresentTime.ns(),
957 frameInterval.getPeriodNsecs());
ramindani3acaaf52023-09-25 10:31:27 -0700958 if (error != hal::Error::NONE) {
959 ALOGE("Error in notifyExpectedPresent call %s", to_string(error).c_str());
960 return INVALID_OPERATION;
961 }
962 return NO_ERROR;
963}
964
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500965status_t HWComposer::getDisplayDecorationSupport(
966 PhysicalDisplayId displayId,
967 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
968 support) {
969 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
970 const auto error = mDisplayData[displayId].hwcDisplay->getDisplayDecorationSupport(support);
971 if (error == hal::Error::UNSUPPORTED) {
972 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
973 }
974 if (error == hal::Error::BAD_PARAMETER) {
975 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
976 }
977 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
978 return NO_ERROR;
979}
980
Sally Qi11dcd582024-08-16 18:11:27 -0700981status_t HWComposer::getRequestedLuts(
982 PhysicalDisplayId displayId,
983 std::vector<aidl::android::hardware::graphics::composer3::DisplayLuts::LayerLut>* outLuts) {
984 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
985 const auto error = mDisplayData[displayId].hwcDisplay->getRequestedLuts(outLuts);
986 if (error == hal::Error::UNSUPPORTED) {
987 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
988 }
989 if (error == hal::Error::BAD_PARAMETER) {
990 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
991 }
992 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
993 return NO_ERROR;
994}
995
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200996status_t HWComposer::setAutoLowLatencyMode(PhysicalDisplayId displayId, bool on) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100997 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
998 const auto error = mDisplayData[displayId].hwcDisplay->setAutoLowLatencyMode(on);
Peiyong Line9d809e2020-04-14 13:10:48 -0700999 if (error == hal::Error::UNSUPPORTED) {
Galia Peycheva5492cb52019-10-30 14:13:16 +01001000 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
1001 }
Peiyong Line9d809e2020-04-14 13:10:48 -07001002 if (error == hal::Error::BAD_PARAMETER) {
Galia Peycheva5492cb52019-10-30 14:13:16 +01001003 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
1004 }
1005 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
1006 return NO_ERROR;
1007}
1008
1009status_t HWComposer::getSupportedContentTypes(
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -08001010 PhysicalDisplayId displayId,
1011 std::vector<hal::ContentType>* outSupportedContentTypes) const {
Galia Peycheva5492cb52019-10-30 14:13:16 +01001012 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -08001013 const auto error = mDisplayData.at(displayId).hwcDisplay->getSupportedContentTypes(
1014 outSupportedContentTypes);
Galia Peycheva5492cb52019-10-30 14:13:16 +01001015
1016 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
1017
1018 return NO_ERROR;
1019}
1020
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +02001021status_t HWComposer::setContentType(PhysicalDisplayId displayId, hal::ContentType contentType) {
Galia Peycheva5492cb52019-10-30 14:13:16 +01001022 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
1023 const auto error = mDisplayData[displayId].hwcDisplay->setContentType(contentType);
Peiyong Line9d809e2020-04-14 13:10:48 -07001024 if (error == hal::Error::UNSUPPORTED) {
Galia Peycheva5492cb52019-10-30 14:13:16 +01001025 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
1026 }
Peiyong Line9d809e2020-04-14 13:10:48 -07001027 if (error == hal::Error::BAD_PARAMETER) {
Galia Peycheva5492cb52019-10-30 14:13:16 +01001028 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
1029 }
1030 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
1031
1032 return NO_ERROR;
1033}
1034
Lloyd Pique4603f3c2020-02-11 12:06:56 -08001035const std::unordered_map<std::string, bool>& HWComposer::getSupportedLayerGenericMetadata() const {
1036 return mSupportedLayerGenericMetadata;
1037}
1038
Sally Qi254ef492024-04-08 15:01:28 -07001039void HWComposer::dumpOverlayProperties(std::string& result) const {
1040 // dump overlay properties
1041 result.append("OverlayProperties:\n");
1042 base::StringAppendF(&result, "supportMixedColorSpaces: %d\n",
1043 mOverlayProperties.supportMixedColorSpaces);
1044 base::StringAppendF(&result, "SupportedBufferCombinations(%zu entries)\n",
1045 mOverlayProperties.combinations.size());
1046 for (const auto& combination : mOverlayProperties.combinations) {
1047 result.append(" pixelFormats=\n");
1048 for (const auto& pixelFormat : combination.pixelFormats) {
1049 base::StringAppendF(&result, " %s (%d)\n",
1050 decodePixelFormat(static_cast<PixelFormat>(pixelFormat)).c_str(),
1051 static_cast<uint32_t>(pixelFormat));
1052 }
1053 result.append(" standards=\n");
1054 for (const auto& standard : combination.standards) {
1055 base::StringAppendF(&result, " %s (%d)\n",
Sally Qi5dfdf142024-04-15 16:50:24 +00001056 decodeStandardOnly(static_cast<uint32_t>(standard)).c_str(),
Sally Qi254ef492024-04-08 15:01:28 -07001057 static_cast<uint32_t>(standard));
1058 }
1059 result.append(" transfers=\n");
1060 for (const auto& transfer : combination.transfers) {
1061 base::StringAppendF(&result, " %s (%d)\n",
1062 decodeTransferOnly(static_cast<uint32_t>(transfer)).c_str(),
1063 static_cast<uint32_t>(transfer));
1064 }
1065 result.append(" ranges=\n");
1066 for (const auto& range : combination.ranges) {
1067 base::StringAppendF(&result, " %s (%d)\n",
1068 decodeRangeOnly(static_cast<uint32_t>(range)).c_str(),
1069 static_cast<uint32_t>(range));
1070 }
1071 result.append("\n");
1072 }
1073}
1074
Yiwei Zhang5434a782018-12-05 18:06:32 -08001075void HWComposer::dump(std::string& result) const {
Peiyong Linbdd08cc2019-12-17 21:35:14 -08001076 result.append(mComposer->dumpDebugInfo());
Sally Qi254ef492024-04-08 15:01:28 -07001077 dumpOverlayProperties(result);
Mathias Agopian83727852010-09-23 18:13:21 -07001078}
1079
Marin Shalamanova524a092020-07-27 21:39:55 +02001080std::optional<PhysicalDisplayId> HWComposer::toPhysicalDisplayId(
1081 hal::HWDisplayId hwcDisplayId) const {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001082 if (const auto it = mPhysicalDisplayIdMap.find(hwcDisplayId);
1083 it != mPhysicalDisplayIdMap.end()) {
1084 return it->second;
1085 }
1086 return {};
1087}
1088
Marin Shalamanova524a092020-07-27 21:39:55 +02001089std::optional<hal::HWDisplayId> HWComposer::fromPhysicalDisplayId(
1090 PhysicalDisplayId displayId) const {
Dominik Laskowskif8db0f02021-04-19 11:05:25 -07001091 if (const auto it = mDisplayData.find(displayId); it != mDisplayData.end()) {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001092 return it->second.hwcDisplay->getId();
1093 }
1094 return {};
1095}
1096
Peiyong Line9d809e2020-04-14 13:10:48 -07001097bool HWComposer::shouldIgnoreHotplugConnect(hal::HWDisplayId hwcDisplayId,
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001098 bool hasDisplayIdentificationData) const {
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001099 if (mHasMultiDisplaySupport && !hasDisplayIdentificationData) {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001100 ALOGE("Ignoring connection of display %" PRIu64 " without identification data",
1101 hwcDisplayId);
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001102 return true;
Dominik Laskowski075d3172018-05-24 15:50:06 -07001103 }
1104
Dominik Laskowskif8db0f02021-04-19 11:05:25 -07001105 // Legacy mode only supports IDs LEGACY_DISPLAY_TYPE_PRIMARY and LEGACY_DISPLAY_TYPE_EXTERNAL.
1106 if (!mHasMultiDisplaySupport && mPhysicalDisplayIdMap.size() == 2) {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001107 ALOGE("Ignoring connection of tertiary display %" PRIu64, hwcDisplayId);
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001108 return true;
1109 }
1110
1111 return false;
1112}
1113
Peiyong Line9d809e2020-04-14 13:10:48 -07001114std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(
1115 hal::HWDisplayId hwcDisplayId) {
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001116 std::optional<DisplayIdentificationInfo> info;
1117 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
1118 info = DisplayIdentificationInfo{.id = *displayId,
1119 .name = std::string(),
1120 .deviceProductInfo = std::nullopt};
Marin Shalamanovf8c63722020-10-06 13:11:21 +02001121 if (mUpdateDeviceProductInfoOnHotplugReconnect) {
1122 uint8_t port;
1123 DisplayIdentificationData data;
1124 getDisplayIdentificationData(hwcDisplayId, &port, &data);
1125 if (auto newInfo = parseDisplayIdentificationData(port, data)) {
1126 info->deviceProductInfo = std::move(newInfo->deviceProductInfo);
Lucas Berthou8d0a0c42024-08-27 14:32:31 +00001127 info->preferredDetailedTimingDescriptor =
1128 std::move(newInfo->preferredDetailedTimingDescriptor);
Marin Shalamanovf8c63722020-10-06 13:11:21 +02001129 } else {
1130 ALOGE("Failed to parse identification data for display %" PRIu64, hwcDisplayId);
1131 }
1132 }
Dominik Laskowski075d3172018-05-24 15:50:06 -07001133 } else {
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001134 uint8_t port;
1135 DisplayIdentificationData data;
1136 const bool hasDisplayIdentificationData =
1137 getDisplayIdentificationData(hwcDisplayId, &port, &data);
1138 if (mPhysicalDisplayIdMap.empty()) {
1139 mHasMultiDisplaySupport = hasDisplayIdentificationData;
1140 ALOGI("Switching to %s multi-display mode",
1141 mHasMultiDisplaySupport ? "generalized" : "legacy");
1142 }
1143
1144 if (shouldIgnoreHotplugConnect(hwcDisplayId, hasDisplayIdentificationData)) {
1145 return {};
1146 }
1147
1148 info = [this, hwcDisplayId, &port, &data, hasDisplayIdentificationData] {
Dominik Laskowskif8db0f02021-04-19 11:05:25 -07001149 const bool isPrimary = !mPrimaryHwcDisplayId;
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001150 if (mHasMultiDisplaySupport) {
1151 if (const auto info = parseDisplayIdentificationData(port, data)) {
1152 return *info;
1153 }
1154 ALOGE("Failed to parse identification data for display %" PRIu64, hwcDisplayId);
1155 } else {
1156 ALOGW_IF(hasDisplayIdentificationData,
1157 "Ignoring identification data for display %" PRIu64, hwcDisplayId);
Peiyong Lin65248e02020-04-18 21:15:07 -07001158 port = isPrimary ? LEGACY_DISPLAY_TYPE_PRIMARY : LEGACY_DISPLAY_TYPE_EXTERNAL;
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001159 }
1160
Marin Shalamanova524a092020-07-27 21:39:55 +02001161 return DisplayIdentificationInfo{.id = PhysicalDisplayId::fromPort(port),
Dominik Laskowskif8db0f02021-04-19 11:05:25 -07001162 .name = isPrimary ? "Primary display"
1163 : "Secondary display",
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001164 .deviceProductInfo = std::nullopt};
1165 }();
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001166
1167 mComposer->onHotplugConnect(hwcDisplayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001168 }
1169
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001170 if (!isConnected(info->id)) {
Lucas Berthou8d0a0c42024-08-27 14:32:31 +00001171 std::optional<ui::Size> size = std::nullopt;
1172 if (info->preferredDetailedTimingDescriptor) {
1173 size = info->preferredDetailedTimingDescriptor->physicalSizeInMm;
1174 }
1175 allocatePhysicalDisplay(hwcDisplayId, info->id, size);
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001176 }
1177 return info;
1178}
1179
1180std::optional<DisplayIdentificationInfo> HWComposer::onHotplugDisconnect(
Peiyong Line9d809e2020-04-14 13:10:48 -07001181 hal::HWDisplayId hwcDisplayId) {
Dominik Laskowskif8db0f02021-04-19 11:05:25 -07001182 LOG_ALWAYS_FATAL_IF(hwcDisplayId == mPrimaryHwcDisplayId,
1183 "Primary display cannot be disconnected.");
1184
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001185 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
1186 if (!displayId) {
Dominik Laskowskif8db0f02021-04-19 11:05:25 -07001187 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid HWC display");
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001188 return {};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001189 }
1190
Dominik Laskowskif2ddca62022-08-17 12:08:07 -07001191 if (!isConnected(*displayId)) {
Dominik Laskowskif8db0f02021-04-19 11:05:25 -07001192 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Already disconnected");
Dominik Laskowskif2ddca62022-08-17 12:08:07 -07001193 return {};
Marin Shalamanovbdd59152020-02-14 15:30:06 +01001194 }
Dominik Laskowskif2ddca62022-08-17 12:08:07 -07001195
1196 // The display will later be destroyed by a call to HWComposer::disconnectDisplay. For now, mark
1197 // it as disconnected.
1198 mDisplayData.at(*displayId).hwcDisplay->setConnected(false);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001199 mComposer->onHotplugDisconnect(hwcDisplayId);
Dominik Laskowskif2ddca62022-08-17 12:08:07 -07001200 return DisplayIdentificationInfo{.id = *displayId};
Steven Thomas6e8f7062017-11-22 14:15:29 -08001201}
1202
Peiyong Linbdd08cc2019-12-17 21:35:14 -08001203void HWComposer::loadCapabilities() {
Peiyong Line9d809e2020-04-14 13:10:48 -07001204 static_assert(sizeof(hal::Capability) == sizeof(int32_t), "Capability size has changed");
Peiyong Linbdd08cc2019-12-17 21:35:14 -08001205 auto capabilities = mComposer->getCapabilities();
1206 for (auto capability : capabilities) {
Ady Abrahamde549d42022-01-26 19:19:17 -08001207 mCapabilities.emplace(capability);
Peiyong Linbdd08cc2019-12-17 21:35:14 -08001208 }
1209}
1210
Sally Qibb866c12022-10-17 11:31:20 -07001211void HWComposer::loadOverlayProperties() {
1212 mComposer->getOverlaySupport(&mOverlayProperties);
1213}
1214
Kriti Dang674b9372022-11-18 10:58:44 +01001215void HWComposer::loadHdrConversionCapabilities() {
1216 const auto error = mComposer->getHdrConversionCapabilities(&mHdrConversionCapabilities);
1217 if (error != hal::Error::NONE) {
1218 ALOGE("Error in fetching HDR conversion capabilities %s", to_string(error).c_str());
1219 mHdrConversionCapabilities = {};
1220 }
1221}
1222
ramindani32cf0602022-03-02 02:30:29 +00001223status_t HWComposer::setIdleTimerEnabled(PhysicalDisplayId displayId,
1224 std::chrono::milliseconds timeout) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001225 SFTRACE_CALL();
ramindani32cf0602022-03-02 02:30:29 +00001226 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
1227 const auto error = mDisplayData[displayId].hwcDisplay->setIdleTimerEnabled(timeout);
1228 if (error == hal::Error::UNSUPPORTED) {
1229 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
1230 }
1231 if (error == hal::Error::BAD_PARAMETER) {
1232 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
1233 }
1234 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
1235 return NO_ERROR;
1236}
1237
ramindani06e518e2022-03-14 18:47:53 +00001238bool HWComposer::hasDisplayIdleTimerCapability(PhysicalDisplayId displayId) const {
ramindani32cf0602022-03-02 02:30:29 +00001239 RETURN_IF_INVALID_DISPLAY(displayId, false);
ramindani06e518e2022-03-14 18:47:53 +00001240 return mDisplayData.at(displayId).hwcDisplay->hasDisplayIdleTimerCapability();
1241}
1242
1243Hwc2::AidlTransform HWComposer::getPhysicalDisplayOrientation(PhysicalDisplayId displayId) const {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001244 SFTRACE_CALL();
ramindani06e518e2022-03-14 18:47:53 +00001245 RETURN_IF_INVALID_DISPLAY(displayId, Hwc2::AidlTransform::NONE);
1246 Hwc2::AidlTransform outTransform;
1247 const auto& hwcDisplay = mDisplayData.at(displayId).hwcDisplay;
1248 const auto error = hwcDisplay->getPhysicalDisplayOrientation(&outTransform);
1249 RETURN_IF_HWC_ERROR(error, displayId, Hwc2::AidlTransform::NONE);
1250 return outTransform;
ramindani32cf0602022-03-02 02:30:29 +00001251}
1252
Lloyd Pique4603f3c2020-02-11 12:06:56 -08001253void HWComposer::loadLayerMetadataSupport() {
1254 mSupportedLayerGenericMetadata.clear();
1255
1256 std::vector<Hwc2::IComposerClient::LayerGenericMetadataKey> supportedMetadataKeyInfo;
1257 const auto error = mComposer->getLayerGenericMetadataKeys(&supportedMetadataKeyInfo);
1258 if (error != hardware::graphics::composer::V2_4::Error::NONE) {
Ady Abraham3891cbe2021-03-29 11:29:31 -07001259 if (error != hardware::graphics::composer::V2_4::Error::UNSUPPORTED) {
1260 ALOGE("%s: %s failed: %s (%d)", __FUNCTION__, "getLayerGenericMetadataKeys",
1261 toString(error).c_str(), static_cast<int32_t>(error));
1262 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -08001263 return;
1264 }
1265
1266 for (const auto& [name, mandatory] : supportedMetadataKeyInfo) {
1267 mSupportedLayerGenericMetadata.emplace(name, mandatory);
1268 }
1269}
1270
Lloyd Pique441d5042018-10-18 16:49:51 -07001271} // namespace impl
Dominik Laskowskif9750f22018-06-06 12:24:53 -07001272} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001273
1274// TODO(b/129481165): remove the #pragma below and fix conversion issues
1275#pragma clang diagnostic pop // ignored "-Wconversion"