blob: 81c2b980f4bd13608ea3415a3f5543f12ac4dbae [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
19#include "drmconnector.h"
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010020#include "drmdevice.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040021
22#include <errno.h>
23#include <stdint.h>
24
Roman Kovalivskyidf882992019-11-04 17:43:40 +020025#include <array>
26#include <sstream>
27
John Stultz9057a6f2018-04-26 12:05:55 -070028#include <log/log.h>
Sean Paul6a55e9f2015-04-30 15:31:06 -040029#include <xf86drmMode.h>
30
31namespace android {
32
Roman Kovalivskyie606a622019-11-25 19:13:05 +020033constexpr size_t TYPES_COUNT = 17;
34
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010035DrmConnector::DrmConnector(DrmDevice *drm, drmModeConnectorPtr c,
Sean Paul6a55e9f2015-04-30 15:31:06 -040036 DrmEncoder *current_encoder,
37 std::vector<DrmEncoder *> &possible_encoders)
38 : drm_(drm),
39 id_(c->connector_id),
40 encoder_(current_encoder),
41 display_(-1),
42 type_(c->connector_type),
Roman Kovalivskyidf882992019-11-04 17:43:40 +020043 type_id_(c->connector_type_id),
Sean Paul6a55e9f2015-04-30 15:31:06 -040044 state_(c->connection),
45 mm_width_(c->mmWidth),
46 mm_height_(c->mmHeight),
47 possible_encoders_(possible_encoders) {
48}
49
Sean Paul6a55e9f2015-04-30 15:31:06 -040050int DrmConnector::Init() {
51 int ret = drm_->GetConnectorProperty(*this, "DPMS", &dpms_property_);
52 if (ret) {
53 ALOGE("Could not get DPMS property\n");
54 return ret;
55 }
Sean Paul877be972015-06-03 14:08:27 -040056 ret = drm_->GetConnectorProperty(*this, "CRTC_ID", &crtc_id_property_);
57 if (ret) {
58 ALOGE("Could not get CRTC_ID property\n");
59 return ret;
60 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +080061 ret = drm_->GetConnectorProperty(*this, "EDID", &edid_property_);
62 if (ret) {
John Stultzff730f92020-02-21 05:56:34 +000063 ALOGW("Could not get EDID property\n");
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +080064 }
Alexandru Gheorghe364f9572018-03-21 11:40:01 +000065 if (writeback()) {
66 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_PIXEL_FORMATS",
67 &writeback_pixel_formats_);
68 if (ret) {
69 ALOGE("Could not get WRITEBACK_PIXEL_FORMATS connector_id = %d\n", id_);
70 return ret;
71 }
72 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_FB_ID",
73 &writeback_fb_id_);
74 if (ret) {
75 ALOGE("Could not get WRITEBACK_FB_ID connector_id = %d\n", id_);
76 return ret;
77 }
78 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_OUT_FENCE_PTR",
79 &writeback_out_fence_);
80 if (ret) {
81 ALOGE("Could not get WRITEBACK_OUT_FENCE_PTR connector_id = %d\n", id_);
82 return ret;
83 }
84 }
Sean Paul6a55e9f2015-04-30 15:31:06 -040085 return 0;
86}
87
88uint32_t DrmConnector::id() const {
89 return id_;
90}
91
92int DrmConnector::display() const {
93 return display_;
94}
95
96void DrmConnector::set_display(int display) {
97 display_ = display;
98}
99
Robert Foss610d9892017-11-01 12:50:04 -0500100bool DrmConnector::internal() const {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400101 return type_ == DRM_MODE_CONNECTOR_LVDS || type_ == DRM_MODE_CONNECTOR_eDP ||
Mykhailo Sopiha87769e62019-07-11 14:31:09 +0300102 type_ == DRM_MODE_CONNECTOR_DSI ||
103 type_ == DRM_MODE_CONNECTOR_VIRTUAL || type_ == DRM_MODE_CONNECTOR_DPI;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400104}
105
Robert Foss610d9892017-11-01 12:50:04 -0500106bool DrmConnector::external() const {
Sean Paulf72cccd2018-08-27 13:59:08 -0400107 return type_ == DRM_MODE_CONNECTOR_HDMIA ||
108 type_ == DRM_MODE_CONNECTOR_DisplayPort ||
Mauro Rossi556bedf2018-01-06 00:59:59 +0100109 type_ == DRM_MODE_CONNECTOR_DVID || type_ == DRM_MODE_CONNECTOR_DVII ||
110 type_ == DRM_MODE_CONNECTOR_VGA;
Robert Foss610d9892017-11-01 12:50:04 -0500111}
112
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000113bool DrmConnector::writeback() const {
114#ifdef DRM_MODE_CONNECTOR_WRITEBACK
115 return type_ == DRM_MODE_CONNECTOR_WRITEBACK;
116#else
117 return false;
118#endif
119}
120
Robert Foss610d9892017-11-01 12:50:04 -0500121bool DrmConnector::valid_type() const {
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000122 return internal() || external() || writeback();
Robert Foss610d9892017-11-01 12:50:04 -0500123}
124
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200125std::string DrmConnector::name() const {
Roman Kovalivskyie606a622019-11-25 19:13:05 +0200126 constexpr std::array<const char *, TYPES_COUNT> names =
127 {"None", "VGA", "DVI-I", "DVI-D", "DVI-A", "Composite",
128 "SVIDEO", "LVDS", "Component", "DIN", "DP", "HDMI-A",
129 "HDMI-B", "TV", "eDP", "Virtual", "DSI"};
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200130
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200131 if (type_ < TYPES_COUNT) {
132 std::ostringstream name_buf;
133 name_buf << names[type_] << "-" << type_id_;
134 return name_buf.str();
135 } else {
136 ALOGE("Unknown type in connector %d, could not make his name", id_);
137 return "None";
138 }
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200139}
140
Sean Paul6a55e9f2015-04-30 15:31:06 -0400141int DrmConnector::UpdateModes() {
142 int fd = drm_->fd();
143
144 drmModeConnectorPtr c = drmModeGetConnector(fd, id_);
145 if (!c) {
146 ALOGE("Failed to get connector %d", id_);
147 return -ENODEV;
148 }
149
Sean Paul09216492015-10-26 15:36:37 -0400150 state_ = c->connection;
151
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200152 bool preferred_mode_found = false;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400153 std::vector<DrmMode> new_modes;
154 for (int i = 0; i < c->count_modes; ++i) {
155 bool exists = false;
Zach Reiznerff30b522015-10-28 19:08:45 -0700156 for (const DrmMode &mode : modes_) {
157 if (mode == c->modes[i]) {
158 new_modes.push_back(mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400159 exists = true;
160 break;
161 }
162 }
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200163 if (!exists) {
164 DrmMode m(&c->modes[i]);
165 m.set_id(drm_->next_mode_id());
166 new_modes.push_back(m);
167 }
John Stultza3429c72019-03-26 21:11:31 -0700168 // Use only the first DRM_MODE_TYPE_PREFERRED mode found
169 if (!preferred_mode_found &&
170 (new_modes.back().type() & DRM_MODE_TYPE_PREFERRED)) {
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200171 preferred_mode_id_ = new_modes.back().id();
172 preferred_mode_found = true;
173 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400174 }
175 modes_.swap(new_modes);
John Stultz9bd25f92019-03-28 15:18:53 -0700176 if (!preferred_mode_found && modes_.size() != 0) {
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200177 preferred_mode_id_ = modes_[0].id();
178 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400179 return 0;
180}
181
182const DrmMode &DrmConnector::active_mode() const {
183 return active_mode_;
184}
185
Sean Paul877be972015-06-03 14:08:27 -0400186void DrmConnector::set_active_mode(const DrmMode &mode) {
187 active_mode_ = mode;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400188}
189
190const DrmProperty &DrmConnector::dpms_property() const {
191 return dpms_property_;
192}
193
Sean Paul877be972015-06-03 14:08:27 -0400194const DrmProperty &DrmConnector::crtc_id_property() const {
195 return crtc_id_property_;
196}
197
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800198const DrmProperty &DrmConnector::edid_property() const {
199 return edid_property_;
200}
201
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000202const DrmProperty &DrmConnector::writeback_pixel_formats() const {
203 return writeback_pixel_formats_;
204}
205
206const DrmProperty &DrmConnector::writeback_fb_id() const {
207 return writeback_fb_id_;
208}
209
210const DrmProperty &DrmConnector::writeback_out_fence() const {
211 return writeback_out_fence_;
212}
213
Sean Paul6a55e9f2015-04-30 15:31:06 -0400214DrmEncoder *DrmConnector::encoder() const {
215 return encoder_;
216}
217
218void DrmConnector::set_encoder(DrmEncoder *encoder) {
219 encoder_ = encoder;
220}
221
Sean Paul09216492015-10-26 15:36:37 -0400222drmModeConnection DrmConnector::state() const {
223 return state_;
224}
225
Sean Paul6a55e9f2015-04-30 15:31:06 -0400226uint32_t DrmConnector::mm_width() const {
227 return mm_width_;
228}
229
230uint32_t DrmConnector::mm_height() const {
231 return mm_height_;
232}
Sean Paulf72cccd2018-08-27 13:59:08 -0400233} // namespace android