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 #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
98
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
108 if (GetOptionalConnectorProperty("Colorspace", &colorspace_property_)) {
109 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_);
137 colorspace_property_.AddEnumToMap("RGB_WIDE_FLOAT",
138 Colorspace::kRgbWideFloat,
139 colorspace_enum_map_);
140 colorspace_property_.AddEnumToMap("BT601_YCC", Colorspace::kBt601Ycc,
141 colorspace_enum_map_);
142 }
143
144 GetOptionalConnectorProperty("content type", &content_type_property_);
145
146 GetOptionalConnectorProperty("HDR_OUTPUT_METADATA",
147 &hdr_output_metadata_property_);
148
149 GetOptionalConnectorProperty("min bpc", &min_bpc_property_);
150
151 if (GetOptionalConnectorProperty("panel orientation", &panel_orientation_)) {
152 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
170 return true;
171 }
172
UpdateEdidProperty()173 int DrmConnector::UpdateEdidProperty() {
174 return GetOptionalConnectorProperty("EDID", &edid_property_) ? 0 : -EINVAL;
175 }
176
GetEdidBlob()177 auto DrmConnector::GetEdidBlob() -> DrmModePropertyBlobUnique {
178 auto ret = UpdateEdidProperty();
179 if (ret != 0) {
180 return {};
181 }
182
183 auto blob_id = GetEdidProperty().GetValue();
184 if (!blob_id) {
185 return {};
186 }
187
188 return MakeDrmModePropertyBlobUnique(*drm_->GetFd(), *blob_id);
189 }
190
IsInternal() const191 bool DrmConnector::IsInternal() const {
192 auto type = connector_->connector_type;
193 return type == DRM_MODE_CONNECTOR_Unknown ||
194 type == DRM_MODE_CONNECTOR_LVDS || type == DRM_MODE_CONNECTOR_eDP ||
195 type == DRM_MODE_CONNECTOR_DSI || type == DRM_MODE_CONNECTOR_VIRTUAL ||
196 type == DRM_MODE_CONNECTOR_DPI || type == DRM_MODE_CONNECTOR_SPI;
197 }
198
IsExternal() const199 bool 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;
205 }
206
IsWriteback() const207 bool DrmConnector::IsWriteback() const {
208 #ifdef DRM_MODE_CONNECTOR_WRITEBACK
209 return connector_->connector_type == DRM_MODE_CONNECTOR_WRITEBACK;
210 #else
211 return false;
212 #endif
213 }
214
IsValid() const215 bool DrmConnector::IsValid() const {
216 return IsInternal() || IsExternal() || IsWriteback();
217 }
218
GetName() const219 std::string DrmConnector::GetName() const {
220 constexpr std::array<const char *, kTypesCount> kNames =
221 {"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"};
225
226 if (connector_->connector_type < kTypesCount) {
227 std::ostringstream name_buf;
228 name_buf << kNames[connector_->connector_type] << "-"
229 << connector_->connector_type_id;
230 return name_buf.str();
231 }
232
233 ALOGE("Unknown type in connector %d, could not make his name", GetId());
234 return "None";
235 }
236
UpdateModes()237 int DrmConnector::UpdateModes() {
238 auto conn = MakeDrmModeConnectorUnique(*drm_->GetFd(), GetId());
239 if (!conn) {
240 ALOGE("Failed to get connector %d", GetId());
241 return -ENODEV;
242 }
243 connector_ = std::move(conn);
244
245 modes_.clear();
246 for (int i = 0; i < connector_->count_modes; ++i) {
247 bool exists = false;
248 for (const DrmMode &mode : modes_) {
249 if (mode == connector_->modes[i]) {
250 exists = true;
251 break;
252 }
253 }
254
255 if (!exists) {
256 modes_.emplace_back(&connector_->modes[i]);
257 }
258 }
259
260 return 0;
261 }
262
IsLinkStatusGood()263 bool 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 }
273
GetPanelOrientation()274 std::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
290 ALOGE("Unknown panel orientation: panel_orientation = %" PRIu64,
291 panel_orientation_value);
292 return {};
293 }
294
295 } // namespace android
296