blob: 4ae8befb9feb8b10867ef04d16f8617feace7539 [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
Sean Paul468a7542024-07-16 19:50:58 +000017#define LOG_TAG "drmhwc"
Sean Paul6a55e9f2015-04-30 15:31:06 -040018
Roman Stratiienko13cc3662020-08-29 21:35:39 +030019#include "DrmConnector.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040020
Roman Stratiienko13cc3662020-08-29 21:35:39 +030021#include <xf86drmMode.h>
Sean Paul6a55e9f2015-04-30 15:31:06 -040022
Roman Kovalivskyidf882992019-11-04 17:43:40 +020023#include <array>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020024#include <cerrno>
25#include <cstdint>
Roman Kovalivskyidf882992019-11-04 17:43:40 +020026#include <sstream>
27
Roman Stratiienko13cc3662020-08-29 21:35:39 +030028#include "DrmDevice.h"
Roman Stratiienkod518a052021-02-25 19:15:14 +020029#include "utils/log.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040030
Yongqin Liucdee4f22021-11-28 08:46:30 +080031#ifndef DRM_MODE_CONNECTOR_SPI
Roman Stratiienko650299a2022-01-30 23:46:10 +020032// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
Yongqin Liucdee4f22021-11-28 08:46:30 +080033#define DRM_MODE_CONNECTOR_SPI 19
34#endif
35
36#ifndef DRM_MODE_CONNECTOR_USB
Roman Stratiienko650299a2022-01-30 23:46:10 +020037// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
Yongqin Liucdee4f22021-11-28 08:46:30 +080038#define DRM_MODE_CONNECTOR_USB 20
39#endif
40
Sean Paul6a55e9f2015-04-30 15:31:06 -040041namespace android {
42
Yongqin Liucdee4f22021-11-28 08:46:30 +080043constexpr size_t kTypesCount = 21;
Roman Kovalivskyie606a622019-11-25 19:13:05 +020044
Sasha McIntosh173247b2024-09-18 18:06:52 -040045auto DrmConnector::GetConnectorProperty(const char *prop_name,
46 DrmProperty *property,
47 bool is_optional) -> bool {
48 auto err = drm_->GetProperty(GetId(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
49 property);
50 if (err == 0)
51 return true;
Sean Paul6a55e9f2015-04-30 15:31:06 -040052
Sasha McIntosh173247b2024-09-18 18:06:52 -040053 if (is_optional) {
54 ALOGV("Could not get optional %s property from connector %d", prop_name,
55 GetId());
56 } else {
57 ALOGE("Could not get %s property from connector %d", prop_name, GetId());
Sean Paul6a55e9f2015-04-30 15:31:06 -040058 }
Sasha McIntosh173247b2024-09-18 18:06:52 -040059 return false;
Roman Stratiienko650299a2022-01-30 23:46:10 +020060}
61
62auto DrmConnector::CreateInstance(DrmDevice &dev, uint32_t connector_id,
63 uint32_t index)
64 -> std::unique_ptr<DrmConnector> {
Roman Stratiienko76892782023-01-16 17:15:53 +020065 auto conn = MakeDrmModeConnectorUnique(*dev.GetFd(), connector_id);
Roman Stratiienko650299a2022-01-30 23:46:10 +020066 if (!conn) {
67 ALOGE("Failed to get connector %d", connector_id);
68 return {};
Sean Paul877be972015-06-03 14:08:27 -040069 }
Roman Stratiienko650299a2022-01-30 23:46:10 +020070
71 auto c = std::unique_ptr<DrmConnector>(
72 new DrmConnector(std::move(conn), &dev, index));
73
Sasha McIntosh173247b2024-09-18 18:06:52 -040074 if (!c->Init()) {
75 ALOGE("Failed to initialize connector %d", connector_id);
Roman Stratiienko650299a2022-01-30 23:46:10 +020076 return {};
77 }
78
79 return c;
Sean Paul6a55e9f2015-04-30 15:31:06 -040080}
81
Sasha McIntosh173247b2024-09-18 18:06:52 -040082auto DrmConnector::Init()-> bool {
83 if (!GetConnectorProperty("DPMS", &dpms_property_) ||
84 !GetConnectorProperty("CRTC_ID", &crtc_id_property_)) {
85 return false;
86 }
87
88 UpdateEdidProperty();
89
90 if (IsWriteback() &&
91 (!GetConnectorProperty("WRITEBACK_PIXEL_FORMATS",
92 &writeback_pixel_formats_) ||
93 !GetConnectorProperty("WRITEBACK_FB_ID", &writeback_fb_id_) ||
94 !GetConnectorProperty("WRITEBACK_OUT_FENCE_PTR",
95 &writeback_out_fence_))) {
96 return false;
97 }
98
99 GetConnectorProperty("content type", &content_type_property_,
100 /*is_optional=*/true);
101
102 return true;
103}
104
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300105int DrmConnector::UpdateEdidProperty() {
Sasha McIntosh173247b2024-09-18 18:06:52 -0400106 return GetConnectorProperty("EDID", &edid_property_, /*is_optional=*/true)
Roman Stratiienko650299a2022-01-30 23:46:10 +0200107 ? 0
108 : -EINVAL;
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300109}
110
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300111auto DrmConnector::GetEdidBlob() -> DrmModePropertyBlobUnique {
Roman Stratiienkoabd8e532022-12-06 23:31:33 +0200112 auto ret = UpdateEdidProperty();
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300113 if (ret != 0) {
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200114 return {};
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300115 }
116
Roman Stratiienkoabd8e532022-12-06 23:31:33 +0200117 auto blob_id = GetEdidProperty().GetValue();
118 if (!blob_id) {
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200119 return {};
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300120 }
121
Roman Stratiienko76892782023-01-16 17:15:53 +0200122 return MakeDrmModePropertyBlobUnique(*drm_->GetFd(), *blob_id);
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300123}
124
Roman Stratiienko650299a2022-01-30 23:46:10 +0200125bool DrmConnector::IsInternal() const {
126 auto type = connector_->connector_type;
Lingkai Dong63ed8072023-10-06 15:45:45 +0100127 return type == DRM_MODE_CONNECTOR_Unknown ||
128 type == DRM_MODE_CONNECTOR_LVDS || type == DRM_MODE_CONNECTOR_eDP ||
Roman Stratiienko650299a2022-01-30 23:46:10 +0200129 type == DRM_MODE_CONNECTOR_DSI || type == DRM_MODE_CONNECTOR_VIRTUAL ||
130 type == DRM_MODE_CONNECTOR_DPI || type == DRM_MODE_CONNECTOR_SPI;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400131}
132
Roman Stratiienko650299a2022-01-30 23:46:10 +0200133bool DrmConnector::IsExternal() const {
134 auto type = connector_->connector_type;
135 return type == DRM_MODE_CONNECTOR_HDMIA ||
136 type == DRM_MODE_CONNECTOR_DisplayPort ||
137 type == DRM_MODE_CONNECTOR_DVID || type == DRM_MODE_CONNECTOR_DVII ||
138 type == DRM_MODE_CONNECTOR_VGA || type == DRM_MODE_CONNECTOR_USB;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400139}
140
Roman Stratiienko650299a2022-01-30 23:46:10 +0200141bool DrmConnector::IsWriteback() const {
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000142#ifdef DRM_MODE_CONNECTOR_WRITEBACK
Roman Stratiienko650299a2022-01-30 23:46:10 +0200143 return connector_->connector_type == DRM_MODE_CONNECTOR_WRITEBACK;
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000144#else
145 return false;
146#endif
147}
148
Roman Stratiienko650299a2022-01-30 23:46:10 +0200149bool DrmConnector::IsValid() const {
150 return IsInternal() || IsExternal() || IsWriteback();
Robert Foss610d9892017-11-01 12:50:04 -0500151}
152
Roman Stratiienko650299a2022-01-30 23:46:10 +0200153std::string DrmConnector::GetName() const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200154 constexpr std::array<const char *, kTypesCount> kNames =
Yongqin Liucdee4f22021-11-28 08:46:30 +0800155 {"None", "VGA", "DVI-I", "DVI-D", "DVI-A", "Composite",
156 "SVIDEO", "LVDS", "Component", "DIN", "DP", "HDMI-A",
157 "HDMI-B", "TV", "eDP", "Virtual", "DSI", "DPI",
158 "Writeback", "SPI", "USB"};
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200159
Roman Stratiienko650299a2022-01-30 23:46:10 +0200160 if (connector_->connector_type < kTypesCount) {
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200161 std::ostringstream name_buf;
Roman Stratiienko650299a2022-01-30 23:46:10 +0200162 name_buf << kNames[connector_->connector_type] << "-"
163 << connector_->connector_type_id;
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200164 return name_buf.str();
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200165 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200166
Roman Stratiienko650299a2022-01-30 23:46:10 +0200167 ALOGE("Unknown type in connector %d, could not make his name", GetId());
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200168 return "None";
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200169}
170
Sean Paul6a55e9f2015-04-30 15:31:06 -0400171int DrmConnector::UpdateModes() {
Roman Stratiienko76892782023-01-16 17:15:53 +0200172 auto conn = MakeDrmModeConnectorUnique(*drm_->GetFd(), GetId());
Roman Stratiienko650299a2022-01-30 23:46:10 +0200173 if (!conn) {
174 ALOGE("Failed to get connector %d", GetId());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400175 return -ENODEV;
176 }
Roman Stratiienko650299a2022-01-30 23:46:10 +0200177 connector_ = std::move(conn);
Roman Stratiienkoad29ee92022-01-17 18:44:22 +0200178
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200179 modes_.clear();
Roman Stratiienko650299a2022-01-30 23:46:10 +0200180 for (int i = 0; i < connector_->count_modes; ++i) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400181 bool exists = false;
Zach Reiznerff30b522015-10-28 19:08:45 -0700182 for (const DrmMode &mode : modes_) {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200183 if (mode == connector_->modes[i]) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400184 exists = true;
185 break;
186 }
187 }
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200188
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200189 if (!exists) {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300190 modes_.emplace_back(&connector_->modes[i]);
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200191 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400192 }
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200193
Sean Paul6a55e9f2015-04-30 15:31:06 -0400194 return 0;
195}
196
Sean Paulf72cccd2018-08-27 13:59:08 -0400197} // namespace android