blob: 82a109b3fbbc50f16ac3fc4b997d22e810114a0d [file] [log] [blame]
Sean Paul6a55e9f2015-04-30 15:31:06 -04001/*
2 * Copyright (C) 2015 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
Manasi Navare3f0c01a2024-10-04 18:01:55 +000017#include <drm/drm_mode.h>
Sean Paul468a7542024-07-16 19:50:58 +000018#define LOG_TAG "drmhwc"
Sean Paul6a55e9f2015-04-30 15:31:06 -040019
Roman Stratiienko13cc3662020-08-29 21:35:39 +030020#include "DrmConnector.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040021
Roman Stratiienko13cc3662020-08-29 21:35:39 +030022#include <xf86drmMode.h>
Sean Paul6a55e9f2015-04-30 15:31:06 -040023
Roman Kovalivskyidf882992019-11-04 17:43:40 +020024#include <array>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020025#include <cerrno>
Yongqin Liu1f41ac72024-10-29 09:30:13 +080026#include <cinttypes>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020027#include <cstdint>
Roman Kovalivskyidf882992019-11-04 17:43:40 +020028#include <sstream>
29
Roman Stratiienko13cc3662020-08-29 21:35:39 +030030#include "DrmDevice.h"
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060031#include "compositor/DisplayInfo.h"
Roman Stratiienkod518a052021-02-25 19:15:14 +020032#include "utils/log.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040033
Yongqin Liucdee4f22021-11-28 08:46:30 +080034#ifndef DRM_MODE_CONNECTOR_SPI
Roman Stratiienko650299a2022-01-30 23:46:10 +020035// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
Yongqin Liucdee4f22021-11-28 08:46:30 +080036#define DRM_MODE_CONNECTOR_SPI 19
37#endif
38
39#ifndef DRM_MODE_CONNECTOR_USB
Roman Stratiienko650299a2022-01-30 23:46:10 +020040// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
Yongqin Liucdee4f22021-11-28 08:46:30 +080041#define DRM_MODE_CONNECTOR_USB 20
42#endif
43
Sean Paul6a55e9f2015-04-30 15:31:06 -040044namespace android {
45
Yongqin Liucdee4f22021-11-28 08:46:30 +080046constexpr size_t kTypesCount = 21;
Roman Kovalivskyie606a622019-11-25 19:13:05 +020047
Sasha McIntosh173247b2024-09-18 18:06:52 -040048auto DrmConnector::GetConnectorProperty(const char *prop_name,
49 DrmProperty *property,
50 bool is_optional) -> bool {
51 auto err = drm_->GetProperty(GetId(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
52 property);
53 if (err == 0)
54 return true;
Sean Paul6a55e9f2015-04-30 15:31:06 -040055
Sasha McIntosh173247b2024-09-18 18:06:52 -040056 if (is_optional) {
57 ALOGV("Could not get optional %s property from connector %d", prop_name,
58 GetId());
59 } else {
60 ALOGE("Could not get %s property from connector %d", prop_name, GetId());
Sean Paul6a55e9f2015-04-30 15:31:06 -040061 }
Sasha McIntosh173247b2024-09-18 18:06:52 -040062 return false;
Roman Stratiienko650299a2022-01-30 23:46:10 +020063}
64
65auto DrmConnector::CreateInstance(DrmDevice &dev, uint32_t connector_id,
66 uint32_t index)
67 -> std::unique_ptr<DrmConnector> {
Roman Stratiienko76892782023-01-16 17:15:53 +020068 auto conn = MakeDrmModeConnectorUnique(*dev.GetFd(), connector_id);
Roman Stratiienko650299a2022-01-30 23:46:10 +020069 if (!conn) {
70 ALOGE("Failed to get connector %d", connector_id);
71 return {};
Sean Paul877be972015-06-03 14:08:27 -040072 }
Roman Stratiienko650299a2022-01-30 23:46:10 +020073
74 auto c = std::unique_ptr<DrmConnector>(
75 new DrmConnector(std::move(conn), &dev, index));
76
Sasha McIntosh173247b2024-09-18 18:06:52 -040077 if (!c->Init()) {
78 ALOGE("Failed to initialize connector %d", connector_id);
Roman Stratiienko650299a2022-01-30 23:46:10 +020079 return {};
80 }
81
82 return c;
Sean Paul6a55e9f2015-04-30 15:31:06 -040083}
84
Sasha McIntosh173247b2024-09-18 18:06:52 -040085auto DrmConnector::Init()-> bool {
86 if (!GetConnectorProperty("DPMS", &dpms_property_) ||
87 !GetConnectorProperty("CRTC_ID", &crtc_id_property_)) {
88 return false;
89 }
90
91 UpdateEdidProperty();
Sasha McIntoshf8c14112024-12-11 12:03:32 -050092#if HAS_LIBDISPLAY_INFO
93 auto edid = LibdisplayEdidWrapper::Create(GetEdidBlob());
94 edid_wrapper_ = edid ? std::move(edid) : std::make_unique<EdidWrapper>();
95#else
96 edid_wrapper_ = std::make_unique<EdidWrapper>();
97#endif
Sasha McIntosh173247b2024-09-18 18:06:52 -040098
99 if (IsWriteback() &&
100 (!GetConnectorProperty("WRITEBACK_PIXEL_FORMATS",
101 &writeback_pixel_formats_) ||
102 !GetConnectorProperty("WRITEBACK_FB_ID", &writeback_fb_id_) ||
103 !GetConnectorProperty("WRITEBACK_OUT_FENCE_PTR",
104 &writeback_out_fence_))) {
105 return false;
106 }
107
Sasha McIntoshda080fe2024-11-26 12:17:27 -0500108 if (GetOptionalConnectorProperty("Colorspace", &colorspace_property_)) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400109 colorspace_property_.AddEnumToMap("Default", Colorspace::kDefault,
110 colorspace_enum_map_);
111 colorspace_property_.AddEnumToMap("SMPTE_170M_YCC", Colorspace::kSmpte170MYcc,
112 colorspace_enum_map_);
113 colorspace_property_.AddEnumToMap("BT709_YCC", Colorspace::kBt709Ycc,
114 colorspace_enum_map_);
115 colorspace_property_.AddEnumToMap("XVYCC_601", Colorspace::kXvycc601,
116 colorspace_enum_map_);
117 colorspace_property_.AddEnumToMap("XVYCC_709", Colorspace::kXvycc709,
118 colorspace_enum_map_);
119 colorspace_property_.AddEnumToMap("SYCC_601", Colorspace::kSycc601,
120 colorspace_enum_map_);
121 colorspace_property_.AddEnumToMap("opYCC_601", Colorspace::kOpycc601,
122 colorspace_enum_map_);
123 colorspace_property_.AddEnumToMap("opRGB", Colorspace::kOprgb,
124 colorspace_enum_map_);
125 colorspace_property_.AddEnumToMap("BT2020_CYCC", Colorspace::kBt2020Cycc,
126 colorspace_enum_map_);
127 colorspace_property_.AddEnumToMap("BT2020_RGB", Colorspace::kBt2020Rgb,
128 colorspace_enum_map_);
129 colorspace_property_.AddEnumToMap("BT2020_YCC", Colorspace::kBt2020Ycc,
130 colorspace_enum_map_);
131 colorspace_property_.AddEnumToMap("DCI-P3_RGB_D65", Colorspace::kDciP3RgbD65,
132 colorspace_enum_map_);
133 colorspace_property_.AddEnumToMap("DCI-P3_RGB_Theater", Colorspace::kDciP3RgbTheater,
134 colorspace_enum_map_);
135 colorspace_property_.AddEnumToMap("RGB_WIDE_FIXED", Colorspace::kRgbWideFixed,
136 colorspace_enum_map_);
Sasha McIntoshf8c14112024-12-11 12:03:32 -0500137 colorspace_property_.AddEnumToMap("RGB_WIDE_FLOAT",
138 Colorspace::kRgbWideFloat,
Sasha McIntosh5294f092024-09-18 18:14:54 -0400139 colorspace_enum_map_);
140 colorspace_property_.AddEnumToMap("BT601_YCC", Colorspace::kBt601Ycc,
141 colorspace_enum_map_);
142 }
143
Sasha McIntoshda080fe2024-11-26 12:17:27 -0500144 GetOptionalConnectorProperty("content type", &content_type_property_);
Sasha McIntosh173247b2024-09-18 18:06:52 -0400145
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500146 GetOptionalConnectorProperty("HDR_OUTPUT_METADATA",
147 &hdr_output_metadata_property_);
148
Sasha McIntoshdfcc8ed2024-11-07 14:40:45 -0500149 GetOptionalConnectorProperty("min bpc", &min_bpc_property_);
150
Sasha McIntoshda080fe2024-11-26 12:17:27 -0500151 if (GetOptionalConnectorProperty("panel orientation", &panel_orientation_)) {
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600152 panel_orientation_
153 .AddEnumToMapReverse("Normal",
154 PanelOrientation::kModePanelOrientationNormal,
155 panel_orientation_enum_map_);
156 panel_orientation_
157 .AddEnumToMapReverse("Upside Down",
158 PanelOrientation::kModePanelOrientationBottomUp,
159 panel_orientation_enum_map_);
160 panel_orientation_
161 .AddEnumToMapReverse("Left Side Up",
162 PanelOrientation::kModePanelOrientationLeftUp,
163 panel_orientation_enum_map_);
164 panel_orientation_
165 .AddEnumToMapReverse("Right Side Up",
166 PanelOrientation::kModePanelOrientationRightUp,
167 panel_orientation_enum_map_);
168 }
169
Sasha McIntosh173247b2024-09-18 18:06:52 -0400170 return true;
171}
172
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300173int DrmConnector::UpdateEdidProperty() {
Sasha McIntoshda080fe2024-11-26 12:17:27 -0500174 return GetOptionalConnectorProperty("EDID", &edid_property_) ? 0 : -EINVAL;
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300175}
176
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300177auto DrmConnector::GetEdidBlob() -> DrmModePropertyBlobUnique {
Roman Stratiienkoabd8e532022-12-06 23:31:33 +0200178 auto ret = UpdateEdidProperty();
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300179 if (ret != 0) {
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200180 return {};
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300181 }
182
Roman Stratiienkoabd8e532022-12-06 23:31:33 +0200183 auto blob_id = GetEdidProperty().GetValue();
184 if (!blob_id) {
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200185 return {};
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300186 }
187
Roman Stratiienko76892782023-01-16 17:15:53 +0200188 return MakeDrmModePropertyBlobUnique(*drm_->GetFd(), *blob_id);
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300189}
190
Roman Stratiienko650299a2022-01-30 23:46:10 +0200191bool DrmConnector::IsInternal() const {
192 auto type = connector_->connector_type;
Lingkai Dong63ed8072023-10-06 15:45:45 +0100193 return type == DRM_MODE_CONNECTOR_Unknown ||
194 type == DRM_MODE_CONNECTOR_LVDS || type == DRM_MODE_CONNECTOR_eDP ||
Roman Stratiienko650299a2022-01-30 23:46:10 +0200195 type == DRM_MODE_CONNECTOR_DSI || type == DRM_MODE_CONNECTOR_VIRTUAL ||
196 type == DRM_MODE_CONNECTOR_DPI || type == DRM_MODE_CONNECTOR_SPI;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400197}
198
Roman Stratiienko650299a2022-01-30 23:46:10 +0200199bool DrmConnector::IsExternal() const {
200 auto type = connector_->connector_type;
201 return type == DRM_MODE_CONNECTOR_HDMIA ||
202 type == DRM_MODE_CONNECTOR_DisplayPort ||
203 type == DRM_MODE_CONNECTOR_DVID || type == DRM_MODE_CONNECTOR_DVII ||
204 type == DRM_MODE_CONNECTOR_VGA || type == DRM_MODE_CONNECTOR_USB;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400205}
206
Roman Stratiienko650299a2022-01-30 23:46:10 +0200207bool DrmConnector::IsWriteback() const {
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000208#ifdef DRM_MODE_CONNECTOR_WRITEBACK
Roman Stratiienko650299a2022-01-30 23:46:10 +0200209 return connector_->connector_type == DRM_MODE_CONNECTOR_WRITEBACK;
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000210#else
211 return false;
212#endif
213}
214
Roman Stratiienko650299a2022-01-30 23:46:10 +0200215bool DrmConnector::IsValid() const {
216 return IsInternal() || IsExternal() || IsWriteback();
Robert Foss610d9892017-11-01 12:50:04 -0500217}
218
Roman Stratiienko650299a2022-01-30 23:46:10 +0200219std::string DrmConnector::GetName() const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200220 constexpr std::array<const char *, kTypesCount> kNames =
Yongqin Liucdee4f22021-11-28 08:46:30 +0800221 {"None", "VGA", "DVI-I", "DVI-D", "DVI-A", "Composite",
222 "SVIDEO", "LVDS", "Component", "DIN", "DP", "HDMI-A",
223 "HDMI-B", "TV", "eDP", "Virtual", "DSI", "DPI",
224 "Writeback", "SPI", "USB"};
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200225
Roman Stratiienko650299a2022-01-30 23:46:10 +0200226 if (connector_->connector_type < kTypesCount) {
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200227 std::ostringstream name_buf;
Roman Stratiienko650299a2022-01-30 23:46:10 +0200228 name_buf << kNames[connector_->connector_type] << "-"
229 << connector_->connector_type_id;
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200230 return name_buf.str();
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200231 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200232
Roman Stratiienko650299a2022-01-30 23:46:10 +0200233 ALOGE("Unknown type in connector %d, could not make his name", GetId());
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200234 return "None";
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200235}
236
Sean Paul6a55e9f2015-04-30 15:31:06 -0400237int DrmConnector::UpdateModes() {
Roman Stratiienko76892782023-01-16 17:15:53 +0200238 auto conn = MakeDrmModeConnectorUnique(*drm_->GetFd(), GetId());
Roman Stratiienko650299a2022-01-30 23:46:10 +0200239 if (!conn) {
240 ALOGE("Failed to get connector %d", GetId());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400241 return -ENODEV;
242 }
Roman Stratiienko650299a2022-01-30 23:46:10 +0200243 connector_ = std::move(conn);
Roman Stratiienkoad29ee92022-01-17 18:44:22 +0200244
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200245 modes_.clear();
Roman Stratiienko650299a2022-01-30 23:46:10 +0200246 for (int i = 0; i < connector_->count_modes; ++i) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400247 bool exists = false;
Zach Reiznerff30b522015-10-28 19:08:45 -0700248 for (const DrmMode &mode : modes_) {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200249 if (mode == connector_->modes[i]) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400250 exists = true;
251 break;
252 }
253 }
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200254
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200255 if (!exists) {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300256 modes_.emplace_back(&connector_->modes[i]);
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200257 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400258 }
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200259
Sean Paul6a55e9f2015-04-30 15:31:06 -0400260 return 0;
261}
262
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000263bool DrmConnector::IsLinkStatusGood() {
264 if (GetConnectorProperty("link-status", &link_status_property_, false)) {
265 auto link_status_property_value = link_status_property_.GetValue();
266 if (link_status_property_value &&
267 (link_status_property_value == DRM_MODE_LINK_STATUS_BAD))
268 return false;
269 }
270
271 return true;
272}
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600273
274std::optional<PanelOrientation> DrmConnector::GetPanelOrientation() {
275 if (!panel_orientation_.GetValue().has_value()) {
276 ALOGW("No panel orientation property available.");
277 return {};
278 }
279
280 /* The value_or(0) satisfies the compiler warning. However,
281 * panel_orientation_.GetValue() is guaranteed to have a value since we check
282 * has_value() and return early otherwise.
283 */
284 uint64_t panel_orientation_value = panel_orientation_.GetValue().value_or(0);
285
286 if (panel_orientation_enum_map_.count(panel_orientation_value) == 1) {
287 return panel_orientation_enum_map_[panel_orientation_value];
288 }
289
Yongqin Liu1f41ac72024-10-29 09:30:13 +0800290 ALOGE("Unknown panel orientation: panel_orientation = %" PRIu64,
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600291 panel_orientation_value);
292 return {};
293}
294
Sean Paulf72cccd2018-08-27 13:59:08 -0400295} // namespace android