1 /*
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
17 #include <drm/drm_mode.h>
18 #define LOG_TAG "drmhwc"
19
20 #include "DrmConnector.h"
21
22 #include <xf86drmMode.h>
23
24 #include <array>
25 #include <cerrno>
26 #include <cinttypes>
27 #include <cstdint>
28 #include <sstream>
29
30 #include "DrmDevice.h"
31 #include "compositor/DisplayInfo.h"
32 #include "utils/log.h"
33
34 #ifndef DRM_MODE_CONNECTOR_SPI
35 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
36 #define DRM_MODE_CONNECTOR_SPI 19
37 #endif
38
39 #ifndef DRM_MODE_CONNECTOR_USB
40 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
41 #define DRM_MODE_CONNECTOR_USB 20
42 #endif
43
44 namespace android {
45
46 constexpr size_t kTypesCount = 21;
47
GetConnectorProperty(const char * prop_name,DrmProperty * property,bool is_optional)48 auto 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;
55
56 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());
61 }
62 return false;
63 }
64
CreateInstance(DrmDevice & dev,uint32_t connector_id,uint32_t index)65 auto DrmConnector::CreateInstance(DrmDevice &dev, uint32_t connector_id,
66 uint32_t index)
67 -> std::unique_ptr<DrmConnector> {
68 auto conn = MakeDrmModeConnectorUnique(*dev.GetFd(), connector_id);
69 if (!conn) {
70 ALOGE("Failed to get connector %d", connector_id);
71 return {};
72 }
73
74 auto c = std::unique_ptr<DrmConnector>(
75 new DrmConnector(std::move(conn), &dev, index));
76
77 if (!c->Init()) {
78 ALOGE("Failed to initialize connector %d", connector_id);
79 return {};
80 }
81
82 return c;
83 }
84
Init()85 auto DrmConnector::Init()-> bool {
86 if (!GetConnectorProperty("DPMS", &dpms_property_) ||
87 !GetConnectorProperty("CRTC_ID", &crtc_id_property_)) {
88 return false;
89 }
90
91 UpdateEdidProperty();
92
93 if (IsWriteback() &&
94 (!GetConnectorProperty("WRITEBACK_PIXEL_FORMATS",
95 &writeback_pixel_formats_) ||
96 !GetConnectorProperty("WRITEBACK_FB_ID", &writeback_fb_id_) ||
97 !GetConnectorProperty("WRITEBACK_OUT_FENCE_PTR",
98 &writeback_out_fence_))) {
99 return false;
100 }
101
102 if (GetConnectorProperty("Colorspace", &colorspace_property_,
103 /*is_optional=*/true)) {
104 colorspace_property_.AddEnumToMap("Default", Colorspace::kDefault,
105 colorspace_enum_map_);
106 colorspace_property_.AddEnumToMap("SMPTE_170M_YCC", Colorspace::kSmpte170MYcc,
107 colorspace_enum_map_);
108 colorspace_property_.AddEnumToMap("BT709_YCC", Colorspace::kBt709Ycc,
109 colorspace_enum_map_);
110 colorspace_property_.AddEnumToMap("XVYCC_601", Colorspace::kXvycc601,
111 colorspace_enum_map_);
112 colorspace_property_.AddEnumToMap("XVYCC_709", Colorspace::kXvycc709,
113 colorspace_enum_map_);
114 colorspace_property_.AddEnumToMap("SYCC_601", Colorspace::kSycc601,
115 colorspace_enum_map_);
116 colorspace_property_.AddEnumToMap("opYCC_601", Colorspace::kOpycc601,
117 colorspace_enum_map_);
118 colorspace_property_.AddEnumToMap("opRGB", Colorspace::kOprgb,
119 colorspace_enum_map_);
120 colorspace_property_.AddEnumToMap("BT2020_CYCC", Colorspace::kBt2020Cycc,
121 colorspace_enum_map_);
122 colorspace_property_.AddEnumToMap("BT2020_RGB", Colorspace::kBt2020Rgb,
123 colorspace_enum_map_);
124 colorspace_property_.AddEnumToMap("BT2020_YCC", Colorspace::kBt2020Ycc,
125 colorspace_enum_map_);
126 colorspace_property_.AddEnumToMap("DCI-P3_RGB_D65", Colorspace::kDciP3RgbD65,
127 colorspace_enum_map_);
128 colorspace_property_.AddEnumToMap("DCI-P3_RGB_Theater", Colorspace::kDciP3RgbTheater,
129 colorspace_enum_map_);
130 colorspace_property_.AddEnumToMap("RGB_WIDE_FIXED", Colorspace::kRgbWideFixed,
131 colorspace_enum_map_);
132 colorspace_property_.AddEnumToMap("RGB_WIDE_FLOAT", Colorspace::kRgbWideFloat,
133 colorspace_enum_map_);
134 colorspace_property_.AddEnumToMap("BT601_YCC", Colorspace::kBt601Ycc,
135 colorspace_enum_map_);
136 }
137
138 GetConnectorProperty("content type", &content_type_property_,
139 /*is_optional=*/true);
140
141 if (GetConnectorProperty("panel orientation", &panel_orientation_,
142 /*is_optional=*/true)) {
143 panel_orientation_
144 .AddEnumToMapReverse("Normal",
145 PanelOrientation::kModePanelOrientationNormal,
146 panel_orientation_enum_map_);
147 panel_orientation_
148 .AddEnumToMapReverse("Upside Down",
149 PanelOrientation::kModePanelOrientationBottomUp,
150 panel_orientation_enum_map_);
151 panel_orientation_
152 .AddEnumToMapReverse("Left Side Up",
153 PanelOrientation::kModePanelOrientationLeftUp,
154 panel_orientation_enum_map_);
155 panel_orientation_
156 .AddEnumToMapReverse("Right Side Up",
157 PanelOrientation::kModePanelOrientationRightUp,
158 panel_orientation_enum_map_);
159 }
160
161 return true;
162 }
163
UpdateEdidProperty()164 int DrmConnector::UpdateEdidProperty() {
165 return GetConnectorProperty("EDID", &edid_property_, /*is_optional=*/true)
166 ? 0
167 : -EINVAL;
168 }
169
GetEdidBlob()170 auto DrmConnector::GetEdidBlob() -> DrmModePropertyBlobUnique {
171 auto ret = UpdateEdidProperty();
172 if (ret != 0) {
173 return {};
174 }
175
176 auto blob_id = GetEdidProperty().GetValue();
177 if (!blob_id) {
178 return {};
179 }
180
181 return MakeDrmModePropertyBlobUnique(*drm_->GetFd(), *blob_id);
182 }
183
IsInternal() const184 bool DrmConnector::IsInternal() const {
185 auto type = connector_->connector_type;
186 return type == DRM_MODE_CONNECTOR_Unknown ||
187 type == DRM_MODE_CONNECTOR_LVDS || type == DRM_MODE_CONNECTOR_eDP ||
188 type == DRM_MODE_CONNECTOR_DSI || type == DRM_MODE_CONNECTOR_VIRTUAL ||
189 type == DRM_MODE_CONNECTOR_DPI || type == DRM_MODE_CONNECTOR_SPI;
190 }
191
IsExternal() const192 bool DrmConnector::IsExternal() const {
193 auto type = connector_->connector_type;
194 return type == DRM_MODE_CONNECTOR_HDMIA ||
195 type == DRM_MODE_CONNECTOR_DisplayPort ||
196 type == DRM_MODE_CONNECTOR_DVID || type == DRM_MODE_CONNECTOR_DVII ||
197 type == DRM_MODE_CONNECTOR_VGA || type == DRM_MODE_CONNECTOR_USB;
198 }
199
IsWriteback() const200 bool DrmConnector::IsWriteback() const {
201 #ifdef DRM_MODE_CONNECTOR_WRITEBACK
202 return connector_->connector_type == DRM_MODE_CONNECTOR_WRITEBACK;
203 #else
204 return false;
205 #endif
206 }
207
IsValid() const208 bool DrmConnector::IsValid() const {
209 return IsInternal() || IsExternal() || IsWriteback();
210 }
211
GetName() const212 std::string DrmConnector::GetName() const {
213 constexpr std::array<const char *, kTypesCount> kNames =
214 {"None", "VGA", "DVI-I", "DVI-D", "DVI-A", "Composite",
215 "SVIDEO", "LVDS", "Component", "DIN", "DP", "HDMI-A",
216 "HDMI-B", "TV", "eDP", "Virtual", "DSI", "DPI",
217 "Writeback", "SPI", "USB"};
218
219 if (connector_->connector_type < kTypesCount) {
220 std::ostringstream name_buf;
221 name_buf << kNames[connector_->connector_type] << "-"
222 << connector_->connector_type_id;
223 return name_buf.str();
224 }
225
226 ALOGE("Unknown type in connector %d, could not make his name", GetId());
227 return "None";
228 }
229
UpdateModes()230 int DrmConnector::UpdateModes() {
231 auto conn = MakeDrmModeConnectorUnique(*drm_->GetFd(), GetId());
232 if (!conn) {
233 ALOGE("Failed to get connector %d", GetId());
234 return -ENODEV;
235 }
236 connector_ = std::move(conn);
237
238 modes_.clear();
239 for (int i = 0; i < connector_->count_modes; ++i) {
240 bool exists = false;
241 for (const DrmMode &mode : modes_) {
242 if (mode == connector_->modes[i]) {
243 exists = true;
244 break;
245 }
246 }
247
248 if (!exists) {
249 modes_.emplace_back(&connector_->modes[i]);
250 }
251 }
252
253 return 0;
254 }
255
IsLinkStatusGood()256 bool DrmConnector::IsLinkStatusGood() {
257 if (GetConnectorProperty("link-status", &link_status_property_, false)) {
258 auto link_status_property_value = link_status_property_.GetValue();
259 if (link_status_property_value &&
260 (link_status_property_value == DRM_MODE_LINK_STATUS_BAD))
261 return false;
262 }
263
264 return true;
265 }
266
GetPanelOrientation()267 std::optional<PanelOrientation> DrmConnector::GetPanelOrientation() {
268 if (!panel_orientation_.GetValue().has_value()) {
269 ALOGW("No panel orientation property available.");
270 return {};
271 }
272
273 /* The value_or(0) satisfies the compiler warning. However,
274 * panel_orientation_.GetValue() is guaranteed to have a value since we check
275 * has_value() and return early otherwise.
276 */
277 uint64_t panel_orientation_value = panel_orientation_.GetValue().value_or(0);
278
279 if (panel_orientation_enum_map_.count(panel_orientation_value) == 1) {
280 return panel_orientation_enum_map_[panel_orientation_value];
281 }
282
283 ALOGE("Unknown panel orientation: panel_orientation = %" PRIu64,
284 panel_orientation_value);
285 return {};
286 }
287
288 } // namespace android
289