blob: 5e00dd6ca801028f2a069f0939e6c44728e3f2b3 [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> {
66 auto conn = MakeDrmModeConnectorUnique(dev.fd(), connector_id);
67 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 Stratiienkob3b5c1e2021-02-15 13:44:19 +0200102 uint64_t blob_id = 0;
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300103 int ret = UpdateEdidProperty();
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300104 if (ret != 0) {
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200105 return {};
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300106 }
107
Roman Stratiienko650299a2022-01-30 23:46:10 +0200108 std::tie(ret, blob_id) = GetEdidProperty().value();
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300109 if (ret != 0) {
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200110 return {};
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300111 }
112
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300113 return MakeDrmModePropertyBlobUnique(drm_->fd(), blob_id);
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300114}
115
Roman Stratiienko650299a2022-01-30 23:46:10 +0200116bool DrmConnector::IsInternal() const {
117 auto type = connector_->connector_type;
118 return type == DRM_MODE_CONNECTOR_LVDS || type == DRM_MODE_CONNECTOR_eDP ||
119 type == DRM_MODE_CONNECTOR_DSI || type == DRM_MODE_CONNECTOR_VIRTUAL ||
120 type == DRM_MODE_CONNECTOR_DPI || type == DRM_MODE_CONNECTOR_SPI;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400121}
122
Roman Stratiienko650299a2022-01-30 23:46:10 +0200123bool DrmConnector::IsExternal() const {
124 auto type = connector_->connector_type;
125 return type == DRM_MODE_CONNECTOR_HDMIA ||
126 type == DRM_MODE_CONNECTOR_DisplayPort ||
127 type == DRM_MODE_CONNECTOR_DVID || type == DRM_MODE_CONNECTOR_DVII ||
128 type == DRM_MODE_CONNECTOR_VGA || type == DRM_MODE_CONNECTOR_USB;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400129}
130
Roman Stratiienko650299a2022-01-30 23:46:10 +0200131bool DrmConnector::IsWriteback() const {
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000132#ifdef DRM_MODE_CONNECTOR_WRITEBACK
Roman Stratiienko650299a2022-01-30 23:46:10 +0200133 return connector_->connector_type == DRM_MODE_CONNECTOR_WRITEBACK;
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000134#else
135 return false;
136#endif
137}
138
Roman Stratiienko650299a2022-01-30 23:46:10 +0200139bool DrmConnector::IsValid() const {
140 return IsInternal() || IsExternal() || IsWriteback();
Robert Foss610d9892017-11-01 12:50:04 -0500141}
142
Roman Stratiienko650299a2022-01-30 23:46:10 +0200143std::string DrmConnector::GetName() const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200144 constexpr std::array<const char *, kTypesCount> kNames =
Yongqin Liucdee4f22021-11-28 08:46:30 +0800145 {"None", "VGA", "DVI-I", "DVI-D", "DVI-A", "Composite",
146 "SVIDEO", "LVDS", "Component", "DIN", "DP", "HDMI-A",
147 "HDMI-B", "TV", "eDP", "Virtual", "DSI", "DPI",
148 "Writeback", "SPI", "USB"};
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200149
Roman Stratiienko650299a2022-01-30 23:46:10 +0200150 if (connector_->connector_type < kTypesCount) {
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200151 std::ostringstream name_buf;
Roman Stratiienko650299a2022-01-30 23:46:10 +0200152 name_buf << kNames[connector_->connector_type] << "-"
153 << connector_->connector_type_id;
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200154 return name_buf.str();
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200155 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200156
Roman Stratiienko650299a2022-01-30 23:46:10 +0200157 ALOGE("Unknown type in connector %d, could not make his name", GetId());
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200158 return "None";
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200159}
160
Sean Paul6a55e9f2015-04-30 15:31:06 -0400161int DrmConnector::UpdateModes() {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200162 auto conn = MakeDrmModeConnectorUnique(drm_->fd(), GetId());
163 if (!conn) {
164 ALOGE("Failed to get connector %d", GetId());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400165 return -ENODEV;
166 }
Roman Stratiienko650299a2022-01-30 23:46:10 +0200167 connector_ = std::move(conn);
Roman Stratiienkoad29ee92022-01-17 18:44:22 +0200168
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200169 modes_.clear();
Roman Stratiienko650299a2022-01-30 23:46:10 +0200170 for (int i = 0; i < connector_->count_modes; ++i) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400171 bool exists = false;
Zach Reiznerff30b522015-10-28 19:08:45 -0700172 for (const DrmMode &mode : modes_) {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200173 if (mode == connector_->modes[i]) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400174 exists = true;
175 break;
176 }
177 }
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200178
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200179 if (!exists) {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200180 modes_.emplace_back(DrmMode(&connector_->modes[i]));
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200181 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400182 }
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200183
Sean Paul6a55e9f2015-04-30 15:31:06 -0400184 return 0;
185}
186
Roman Stratiienko650299a2022-01-30 23:46:10 +0200187void DrmConnector::SetActiveMode(DrmMode &mode) {
Sean Paul877be972015-06-03 14:08:27 -0400188 active_mode_ = mode;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400189}
190
Sean Paulf72cccd2018-08-27 13:59:08 -0400191} // namespace android