blob: db3f9b673dbfaf153cabf485cb98f7e5ea379e97 [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) {
63 ALOGE("Could not get EDID property\n");
64 return ret;
65 }
Alexandru Gheorghe364f9572018-03-21 11:40:01 +000066 if (writeback()) {
67 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_PIXEL_FORMATS",
68 &writeback_pixel_formats_);
69 if (ret) {
70 ALOGE("Could not get WRITEBACK_PIXEL_FORMATS connector_id = %d\n", id_);
71 return ret;
72 }
73 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_FB_ID",
74 &writeback_fb_id_);
75 if (ret) {
76 ALOGE("Could not get WRITEBACK_FB_ID connector_id = %d\n", id_);
77 return ret;
78 }
79 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_OUT_FENCE_PTR",
80 &writeback_out_fence_);
81 if (ret) {
82 ALOGE("Could not get WRITEBACK_OUT_FENCE_PTR connector_id = %d\n", id_);
83 return ret;
84 }
85 }
Sean Paul6a55e9f2015-04-30 15:31:06 -040086 return 0;
87}
88
89uint32_t DrmConnector::id() const {
90 return id_;
91}
92
93int DrmConnector::display() const {
94 return display_;
95}
96
97void DrmConnector::set_display(int display) {
98 display_ = display;
99}
100
Robert Foss610d9892017-11-01 12:50:04 -0500101bool DrmConnector::internal() const {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400102 return type_ == DRM_MODE_CONNECTOR_LVDS || type_ == DRM_MODE_CONNECTOR_eDP ||
Mykhailo Sopiha87769e62019-07-11 14:31:09 +0300103 type_ == DRM_MODE_CONNECTOR_DSI ||
104 type_ == DRM_MODE_CONNECTOR_VIRTUAL || type_ == DRM_MODE_CONNECTOR_DPI;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400105}
106
Robert Foss610d9892017-11-01 12:50:04 -0500107bool DrmConnector::external() const {
Sean Paulf72cccd2018-08-27 13:59:08 -0400108 return type_ == DRM_MODE_CONNECTOR_HDMIA ||
109 type_ == DRM_MODE_CONNECTOR_DisplayPort ||
Mauro Rossi556bedf2018-01-06 00:59:59 +0100110 type_ == DRM_MODE_CONNECTOR_DVID || type_ == DRM_MODE_CONNECTOR_DVII ||
111 type_ == DRM_MODE_CONNECTOR_VGA;
Robert Foss610d9892017-11-01 12:50:04 -0500112}
113
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000114bool DrmConnector::writeback() const {
115#ifdef DRM_MODE_CONNECTOR_WRITEBACK
116 return type_ == DRM_MODE_CONNECTOR_WRITEBACK;
117#else
118 return false;
119#endif
120}
121
Robert Foss610d9892017-11-01 12:50:04 -0500122bool DrmConnector::valid_type() const {
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000123 return internal() || external() || writeback();
Robert Foss610d9892017-11-01 12:50:04 -0500124}
125
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200126std::string DrmConnector::name() const {
Roman Kovalivskyie606a622019-11-25 19:13:05 +0200127 constexpr std::array<const char *, TYPES_COUNT> names =
128 {"None", "VGA", "DVI-I", "DVI-D", "DVI-A", "Composite",
129 "SVIDEO", "LVDS", "Component", "DIN", "DP", "HDMI-A",
130 "HDMI-B", "TV", "eDP", "Virtual", "DSI"};
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200131
Roman Kovalivskyid6a6e6e2019-11-25 19:17:53 +0200132 if (type_ < TYPES_COUNT) {
133 std::ostringstream name_buf;
134 name_buf << names[type_] << "-" << type_id_;
135 return name_buf.str();
136 } else {
137 ALOGE("Unknown type in connector %d, could not make his name", id_);
138 return "None";
139 }
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200140}
141
Sean Paul6a55e9f2015-04-30 15:31:06 -0400142int DrmConnector::UpdateModes() {
143 int fd = drm_->fd();
144
145 drmModeConnectorPtr c = drmModeGetConnector(fd, id_);
146 if (!c) {
147 ALOGE("Failed to get connector %d", id_);
148 return -ENODEV;
149 }
150
Sean Paul09216492015-10-26 15:36:37 -0400151 state_ = c->connection;
152
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200153 bool preferred_mode_found = false;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400154 std::vector<DrmMode> new_modes;
155 for (int i = 0; i < c->count_modes; ++i) {
156 bool exists = false;
Zach Reiznerff30b522015-10-28 19:08:45 -0700157 for (const DrmMode &mode : modes_) {
158 if (mode == c->modes[i]) {
159 new_modes.push_back(mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400160 exists = true;
161 break;
162 }
163 }
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200164 if (!exists) {
165 DrmMode m(&c->modes[i]);
166 m.set_id(drm_->next_mode_id());
167 new_modes.push_back(m);
168 }
John Stultza3429c72019-03-26 21:11:31 -0700169 // Use only the first DRM_MODE_TYPE_PREFERRED mode found
170 if (!preferred_mode_found &&
171 (new_modes.back().type() & DRM_MODE_TYPE_PREFERRED)) {
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200172 preferred_mode_id_ = new_modes.back().id();
173 preferred_mode_found = true;
174 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400175 }
176 modes_.swap(new_modes);
John Stultz9bd25f92019-03-28 15:18:53 -0700177 if (!preferred_mode_found && modes_.size() != 0) {
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200178 preferred_mode_id_ = modes_[0].id();
179 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400180 return 0;
181}
182
183const DrmMode &DrmConnector::active_mode() const {
184 return active_mode_;
185}
186
Sean Paul877be972015-06-03 14:08:27 -0400187void DrmConnector::set_active_mode(const DrmMode &mode) {
188 active_mode_ = mode;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400189}
190
191const DrmProperty &DrmConnector::dpms_property() const {
192 return dpms_property_;
193}
194
Sean Paul877be972015-06-03 14:08:27 -0400195const DrmProperty &DrmConnector::crtc_id_property() const {
196 return crtc_id_property_;
197}
198
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800199const DrmProperty &DrmConnector::edid_property() const {
200 return edid_property_;
201}
202
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000203const DrmProperty &DrmConnector::writeback_pixel_formats() const {
204 return writeback_pixel_formats_;
205}
206
207const DrmProperty &DrmConnector::writeback_fb_id() const {
208 return writeback_fb_id_;
209}
210
211const DrmProperty &DrmConnector::writeback_out_fence() const {
212 return writeback_out_fence_;
213}
214
Sean Paul6a55e9f2015-04-30 15:31:06 -0400215DrmEncoder *DrmConnector::encoder() const {
216 return encoder_;
217}
218
219void DrmConnector::set_encoder(DrmEncoder *encoder) {
220 encoder_ = encoder;
221}
222
Sean Paul09216492015-10-26 15:36:37 -0400223drmModeConnection DrmConnector::state() const {
224 return state_;
225}
226
Sean Paul6a55e9f2015-04-30 15:31:06 -0400227uint32_t DrmConnector::mm_width() const {
228 return mm_width_;
229}
230
231uint32_t DrmConnector::mm_height() const {
232 return mm_height_;
233}
Sean Paulf72cccd2018-08-27 13:59:08 -0400234} // namespace android