blob: 2c0adda358b10889025cd9a442b9fbf7f3b91d23 [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 }
Alexandru Gheorghe364f9572018-03-21 11:40:01 +000061 if (writeback()) {
62 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_PIXEL_FORMATS",
63 &writeback_pixel_formats_);
64 if (ret) {
65 ALOGE("Could not get WRITEBACK_PIXEL_FORMATS connector_id = %d\n", id_);
66 return ret;
67 }
68 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_FB_ID",
69 &writeback_fb_id_);
70 if (ret) {
71 ALOGE("Could not get WRITEBACK_FB_ID connector_id = %d\n", id_);
72 return ret;
73 }
74 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_OUT_FENCE_PTR",
75 &writeback_out_fence_);
76 if (ret) {
77 ALOGE("Could not get WRITEBACK_OUT_FENCE_PTR connector_id = %d\n", id_);
78 return ret;
79 }
80 }
Sean Paul6a55e9f2015-04-30 15:31:06 -040081 return 0;
82}
83
84uint32_t DrmConnector::id() const {
85 return id_;
86}
87
88int DrmConnector::display() const {
89 return display_;
90}
91
92void DrmConnector::set_display(int display) {
93 display_ = display;
94}
95
Robert Foss610d9892017-11-01 12:50:04 -050096bool DrmConnector::internal() const {
Sean Paul6a55e9f2015-04-30 15:31:06 -040097 return type_ == DRM_MODE_CONNECTOR_LVDS || type_ == DRM_MODE_CONNECTOR_eDP ||
Mykhailo Sopiha87769e62019-07-11 14:31:09 +030098 type_ == DRM_MODE_CONNECTOR_DSI ||
99 type_ == DRM_MODE_CONNECTOR_VIRTUAL || type_ == DRM_MODE_CONNECTOR_DPI;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400100}
101
Robert Foss610d9892017-11-01 12:50:04 -0500102bool DrmConnector::external() const {
Sean Paulf72cccd2018-08-27 13:59:08 -0400103 return type_ == DRM_MODE_CONNECTOR_HDMIA ||
104 type_ == DRM_MODE_CONNECTOR_DisplayPort ||
Mauro Rossi556bedf2018-01-06 00:59:59 +0100105 type_ == DRM_MODE_CONNECTOR_DVID || type_ == DRM_MODE_CONNECTOR_DVII ||
106 type_ == DRM_MODE_CONNECTOR_VGA;
Robert Foss610d9892017-11-01 12:50:04 -0500107}
108
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000109bool DrmConnector::writeback() const {
110#ifdef DRM_MODE_CONNECTOR_WRITEBACK
111 return type_ == DRM_MODE_CONNECTOR_WRITEBACK;
112#else
113 return false;
114#endif
115}
116
Robert Foss610d9892017-11-01 12:50:04 -0500117bool DrmConnector::valid_type() const {
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000118 return internal() || external() || writeback();
Robert Foss610d9892017-11-01 12:50:04 -0500119}
120
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200121std::string DrmConnector::name() const {
Roman Kovalivskyie606a622019-11-25 19:13:05 +0200122 constexpr std::array<const char *, TYPES_COUNT> names =
123 {"None", "VGA", "DVI-I", "DVI-D", "DVI-A", "Composite",
124 "SVIDEO", "LVDS", "Component", "DIN", "DP", "HDMI-A",
125 "HDMI-B", "TV", "eDP", "Virtual", "DSI"};
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200126
127 std::ostringstream name_buf;
128 name_buf << names[type_] << "-" << type_id_;
129 return name_buf.str();
130}
131
Sean Paul6a55e9f2015-04-30 15:31:06 -0400132int DrmConnector::UpdateModes() {
133 int fd = drm_->fd();
134
135 drmModeConnectorPtr c = drmModeGetConnector(fd, id_);
136 if (!c) {
137 ALOGE("Failed to get connector %d", id_);
138 return -ENODEV;
139 }
140
Sean Paul09216492015-10-26 15:36:37 -0400141 state_ = c->connection;
142
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200143 bool preferred_mode_found = false;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400144 std::vector<DrmMode> new_modes;
145 for (int i = 0; i < c->count_modes; ++i) {
146 bool exists = false;
Zach Reiznerff30b522015-10-28 19:08:45 -0700147 for (const DrmMode &mode : modes_) {
148 if (mode == c->modes[i]) {
149 new_modes.push_back(mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400150 exists = true;
151 break;
152 }
153 }
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200154 if (!exists) {
155 DrmMode m(&c->modes[i]);
156 m.set_id(drm_->next_mode_id());
157 new_modes.push_back(m);
158 }
John Stultza3429c72019-03-26 21:11:31 -0700159 // Use only the first DRM_MODE_TYPE_PREFERRED mode found
160 if (!preferred_mode_found &&
161 (new_modes.back().type() & DRM_MODE_TYPE_PREFERRED)) {
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200162 preferred_mode_id_ = new_modes.back().id();
163 preferred_mode_found = true;
164 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400165 }
166 modes_.swap(new_modes);
John Stultz9bd25f92019-03-28 15:18:53 -0700167 if (!preferred_mode_found && modes_.size() != 0) {
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200168 preferred_mode_id_ = modes_[0].id();
169 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400170 return 0;
171}
172
173const DrmMode &DrmConnector::active_mode() const {
174 return active_mode_;
175}
176
Sean Paul877be972015-06-03 14:08:27 -0400177void DrmConnector::set_active_mode(const DrmMode &mode) {
178 active_mode_ = mode;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400179}
180
181const DrmProperty &DrmConnector::dpms_property() const {
182 return dpms_property_;
183}
184
Sean Paul877be972015-06-03 14:08:27 -0400185const DrmProperty &DrmConnector::crtc_id_property() const {
186 return crtc_id_property_;
187}
188
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000189const DrmProperty &DrmConnector::writeback_pixel_formats() const {
190 return writeback_pixel_formats_;
191}
192
193const DrmProperty &DrmConnector::writeback_fb_id() const {
194 return writeback_fb_id_;
195}
196
197const DrmProperty &DrmConnector::writeback_out_fence() const {
198 return writeback_out_fence_;
199}
200
Sean Paul6a55e9f2015-04-30 15:31:06 -0400201DrmEncoder *DrmConnector::encoder() const {
202 return encoder_;
203}
204
205void DrmConnector::set_encoder(DrmEncoder *encoder) {
206 encoder_ = encoder;
207}
208
Sean Paul09216492015-10-26 15:36:37 -0400209drmModeConnection DrmConnector::state() const {
210 return state_;
211}
212
Sean Paul6a55e9f2015-04-30 15:31:06 -0400213uint32_t DrmConnector::mm_width() const {
214 return mm_width_;
215}
216
217uint32_t DrmConnector::mm_height() const {
218 return mm_height_;
219}
Sean Paulf72cccd2018-08-27 13:59:08 -0400220} // namespace android