blob: 6fde6fb1f2763b41ee170b8bab6486b97ed22ea8 [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
17#define LOG_TAG "hwc-drm-connector"
18
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
Roman Stratiienko650299a2022-01-30 23:46:10 +020045static bool GetOptionalConnectorProperty(const DrmDevice &dev,
46 const DrmConnector &connector,
47 const char *prop_name,
48 DrmProperty *property) {
49 return dev.GetProperty(connector.GetId(), DRM_MODE_OBJECT_CONNECTOR,
50 prop_name, property) == 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040051}
52
Roman Stratiienko650299a2022-01-30 23:46:10 +020053static bool GetConnectorProperty(const DrmDevice &dev,
54 const DrmConnector &connector,
55 const char *prop_name, DrmProperty *property) {
56 if (!GetOptionalConnectorProperty(dev, connector, prop_name, property)) {
57 ALOGE("Could not get %s property\n", prop_name);
58 return false;
Sean Paul6a55e9f2015-04-30 15:31:06 -040059 }
Roman Stratiienko650299a2022-01-30 23:46:10 +020060 return true;
61}
62
63auto DrmConnector::CreateInstance(DrmDevice &dev, uint32_t connector_id,
64 uint32_t index)
65 -> std::unique_ptr<DrmConnector> {
Roman Stratiienko7d899112022-01-31 11:30:27 +020066 auto conn = MakeDrmModeConnectorUnique(dev.GetFd(), connector_id);
Roman Stratiienko650299a2022-01-30 23:46:10 +020067 if (!conn) {
68 ALOGE("Failed to get connector %d", connector_id);
69 return {};
Sean Paul877be972015-06-03 14:08:27 -040070 }
Roman Stratiienko650299a2022-01-30 23:46:10 +020071
72 auto c = std::unique_ptr<DrmConnector>(
73 new DrmConnector(std::move(conn), &dev, index));
74
75 if (!GetConnectorProperty(dev, *c, "DPMS", &c->dpms_property_) ||
76 !GetConnectorProperty(dev, *c, "CRTC_ID", &c->crtc_id_property_)) {
77 return {};
Alexandru Gheorghe364f9572018-03-21 11:40:01 +000078 }
Roman Stratiienko650299a2022-01-30 23:46:10 +020079
80 c->UpdateEdidProperty();
81
82 if (c->IsWriteback() &&
83 (!GetConnectorProperty(dev, *c, "WRITEBACK_PIXEL_FORMATS",
84 &c->writeback_pixel_formats_) ||
85 !GetConnectorProperty(dev, *c, "WRITEBACK_FB_ID",
86 &c->writeback_fb_id_) ||
87 !GetConnectorProperty(dev, *c, "WRITEBACK_OUT_FENCE_PTR",
88 &c->writeback_out_fence_))) {
89 return {};
90 }
91
92 return c;
Sean Paul6a55e9f2015-04-30 15:31:06 -040093}
94
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +030095int DrmConnector::UpdateEdidProperty() {
Roman Stratiienko650299a2022-01-30 23:46:10 +020096 return GetOptionalConnectorProperty(*drm_, *this, "EDID", &edid_property_)
97 ? 0
98 : -EINVAL;
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +030099}
100
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300101auto DrmConnector::GetEdidBlob() -> DrmModePropertyBlobUnique {
Roman Stratiienkoabd8e532022-12-06 23:31:33 +0200102 auto ret = UpdateEdidProperty();
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300103 if (ret != 0) {
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200104 return {};
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300105 }
106
Roman Stratiienkoabd8e532022-12-06 23:31:33 +0200107 auto blob_id = GetEdidProperty().GetValue();
108 if (!blob_id) {
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200109 return {};
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300110 }
111
Roman Stratiienkoabd8e532022-12-06 23:31:33 +0200112 return MakeDrmModePropertyBlobUnique(drm_->GetFd(), *blob_id);
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300113}
114
Roman Stratiienko650299a2022-01-30 23:46:10 +0200115bool DrmConnector::IsInternal() const {
116 auto type = connector_->connector_type;
117 return type == DRM_MODE_CONNECTOR_LVDS || type == DRM_MODE_CONNECTOR_eDP ||
118 type == DRM_MODE_CONNECTOR_DSI || type == DRM_MODE_CONNECTOR_VIRTUAL ||
119 type == DRM_MODE_CONNECTOR_DPI || type == DRM_MODE_CONNECTOR_SPI;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400120}
121
Roman Stratiienko650299a2022-01-30 23:46:10 +0200122bool DrmConnector::IsExternal() const {
123 auto type = connector_->connector_type;
124 return type == DRM_MODE_CONNECTOR_HDMIA ||
125 type == DRM_MODE_CONNECTOR_DisplayPort ||
126 type == DRM_MODE_CONNECTOR_DVID || type == DRM_MODE_CONNECTOR_DVII ||
127 type == DRM_MODE_CONNECTOR_VGA || type == DRM_MODE_CONNECTOR_USB;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400128}
129
Roman Stratiienko650299a2022-01-30 23:46:10 +0200130bool DrmConnector::IsWriteback() const {
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000131#ifdef DRM_MODE_CONNECTOR_WRITEBACK
Roman Stratiienko650299a2022-01-30 23:46:10 +0200132 return connector_->connector_type == DRM_MODE_CONNECTOR_WRITEBACK;
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000133#else
134 return false;
135#endif
136}
137
Roman Stratiienko650299a2022-01-30 23:46:10 +0200138bool DrmConnector::IsValid() const {
139 return IsInternal() || IsExternal() || IsWriteback();
Robert Foss610d9892017-11-01 12:50:04 -0500140}
141
Roman Stratiienko650299a2022-01-30 23:46:10 +0200142std::string DrmConnector::GetName() const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200143 constexpr std::array<const char *, kTypesCount> kNames =
Yongqin Liucdee4f22021-11-28 08:46:30 +0800144 {"None", "VGA", "DVI-I", "DVI-D", "DVI-A", "Composite",
145 "SVIDEO", "LVDS", "Component", "DIN", "DP", "HDMI-A",
146 "HDMI-B", "TV", "eDP", "Virtual", "DSI", "DPI",
147 "Writeback", "SPI", "USB"};
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200148
Roman Stratiienko650299a2022-01-30 23:46:10 +0200149 if (connector_->connector_type < kTypesCount) {
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200150 std::ostringstream name_buf;
Roman Stratiienko650299a2022-01-30 23:46:10 +0200151 name_buf << kNames[connector_->connector_type] << "-"
152 << connector_->connector_type_id;
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200153 return name_buf.str();
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200154 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200155
Roman Stratiienko650299a2022-01-30 23:46:10 +0200156 ALOGE("Unknown type in connector %d, could not make his name", GetId());
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200157 return "None";
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200158}
159
Sean Paul6a55e9f2015-04-30 15:31:06 -0400160int DrmConnector::UpdateModes() {
Roman Stratiienko7d899112022-01-31 11:30:27 +0200161 auto conn = MakeDrmModeConnectorUnique(drm_->GetFd(), GetId());
Roman Stratiienko650299a2022-01-30 23:46:10 +0200162 if (!conn) {
163 ALOGE("Failed to get connector %d", GetId());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400164 return -ENODEV;
165 }
Roman Stratiienko650299a2022-01-30 23:46:10 +0200166 connector_ = std::move(conn);
Roman Stratiienkoad29ee92022-01-17 18:44:22 +0200167
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200168 modes_.clear();
Roman Stratiienko650299a2022-01-30 23:46:10 +0200169 for (int i = 0; i < connector_->count_modes; ++i) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400170 bool exists = false;
Zach Reiznerff30b522015-10-28 19:08:45 -0700171 for (const DrmMode &mode : modes_) {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200172 if (mode == connector_->modes[i]) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400173 exists = true;
174 break;
175 }
176 }
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200177
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200178 if (!exists) {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300179 modes_.emplace_back(&connector_->modes[i]);
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200180 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400181 }
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200182
Sean Paul6a55e9f2015-04-30 15:31:06 -0400183 return 0;
184}
185
Roman Stratiienko650299a2022-01-30 23:46:10 +0200186void DrmConnector::SetActiveMode(DrmMode &mode) {
Sean Paul877be972015-06-03 14:08:27 -0400187 active_mode_ = mode;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400188}
189
Sean Paulf72cccd2018-08-27 13:59:08 -0400190} // namespace android