blob: bca7575c0d9e70e2de769402cb7ddd8083e3aa18 [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
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010033DrmConnector::DrmConnector(DrmDevice *drm, drmModeConnectorPtr c,
Sean Paul6a55e9f2015-04-30 15:31:06 -040034 DrmEncoder *current_encoder,
35 std::vector<DrmEncoder *> &possible_encoders)
36 : drm_(drm),
37 id_(c->connector_id),
38 encoder_(current_encoder),
39 display_(-1),
40 type_(c->connector_type),
Roman Kovalivskyidf882992019-11-04 17:43:40 +020041 type_id_(c->connector_type_id),
Sean Paul6a55e9f2015-04-30 15:31:06 -040042 state_(c->connection),
43 mm_width_(c->mmWidth),
44 mm_height_(c->mmHeight),
45 possible_encoders_(possible_encoders) {
46}
47
Sean Paul6a55e9f2015-04-30 15:31:06 -040048int DrmConnector::Init() {
49 int ret = drm_->GetConnectorProperty(*this, "DPMS", &dpms_property_);
50 if (ret) {
51 ALOGE("Could not get DPMS property\n");
52 return ret;
53 }
Sean Paul877be972015-06-03 14:08:27 -040054 ret = drm_->GetConnectorProperty(*this, "CRTC_ID", &crtc_id_property_);
55 if (ret) {
56 ALOGE("Could not get CRTC_ID property\n");
57 return ret;
58 }
Alexandru Gheorghe364f9572018-03-21 11:40:01 +000059 if (writeback()) {
60 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_PIXEL_FORMATS",
61 &writeback_pixel_formats_);
62 if (ret) {
63 ALOGE("Could not get WRITEBACK_PIXEL_FORMATS connector_id = %d\n", id_);
64 return ret;
65 }
66 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_FB_ID",
67 &writeback_fb_id_);
68 if (ret) {
69 ALOGE("Could not get WRITEBACK_FB_ID connector_id = %d\n", id_);
70 return ret;
71 }
72 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_OUT_FENCE_PTR",
73 &writeback_out_fence_);
74 if (ret) {
75 ALOGE("Could not get WRITEBACK_OUT_FENCE_PTR connector_id = %d\n", id_);
76 return ret;
77 }
78 }
Sean Paul6a55e9f2015-04-30 15:31:06 -040079 return 0;
80}
81
82uint32_t DrmConnector::id() const {
83 return id_;
84}
85
86int DrmConnector::display() const {
87 return display_;
88}
89
90void DrmConnector::set_display(int display) {
91 display_ = display;
92}
93
Robert Foss610d9892017-11-01 12:50:04 -050094bool DrmConnector::internal() const {
Sean Paul6a55e9f2015-04-30 15:31:06 -040095 return type_ == DRM_MODE_CONNECTOR_LVDS || type_ == DRM_MODE_CONNECTOR_eDP ||
Mykhailo Sopiha87769e62019-07-11 14:31:09 +030096 type_ == DRM_MODE_CONNECTOR_DSI ||
97 type_ == DRM_MODE_CONNECTOR_VIRTUAL || type_ == DRM_MODE_CONNECTOR_DPI;
Sean Paul6a55e9f2015-04-30 15:31:06 -040098}
99
Robert Foss610d9892017-11-01 12:50:04 -0500100bool DrmConnector::external() const {
Sean Paulf72cccd2018-08-27 13:59:08 -0400101 return type_ == DRM_MODE_CONNECTOR_HDMIA ||
102 type_ == DRM_MODE_CONNECTOR_DisplayPort ||
Mauro Rossi556bedf2018-01-06 00:59:59 +0100103 type_ == DRM_MODE_CONNECTOR_DVID || type_ == DRM_MODE_CONNECTOR_DVII ||
104 type_ == DRM_MODE_CONNECTOR_VGA;
Robert Foss610d9892017-11-01 12:50:04 -0500105}
106
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000107bool DrmConnector::writeback() const {
108#ifdef DRM_MODE_CONNECTOR_WRITEBACK
109 return type_ == DRM_MODE_CONNECTOR_WRITEBACK;
110#else
111 return false;
112#endif
113}
114
Robert Foss610d9892017-11-01 12:50:04 -0500115bool DrmConnector::valid_type() const {
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000116 return internal() || external() || writeback();
Robert Foss610d9892017-11-01 12:50:04 -0500117}
118
Roman Kovalivskyidf882992019-11-04 17:43:40 +0200119std::string DrmConnector::name() const {
120 constexpr std::array names = {"None", "VGA", "DVI-I", "DVI-D",
121 "DVI-A", "Composite", "SVIDEO", "LVDS",
122 "Component", "DIN", "DP", "HDMI-A",
123 "HDMI-B", "TV", "eDP", "Virtual",
124 "DSI"};
125
126 std::ostringstream name_buf;
127 name_buf << names[type_] << "-" << type_id_;
128 return name_buf.str();
129}
130
Sean Paul6a55e9f2015-04-30 15:31:06 -0400131int DrmConnector::UpdateModes() {
132 int fd = drm_->fd();
133
134 drmModeConnectorPtr c = drmModeGetConnector(fd, id_);
135 if (!c) {
136 ALOGE("Failed to get connector %d", id_);
137 return -ENODEV;
138 }
139
Sean Paul09216492015-10-26 15:36:37 -0400140 state_ = c->connection;
141
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200142 bool preferred_mode_found = false;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400143 std::vector<DrmMode> new_modes;
144 for (int i = 0; i < c->count_modes; ++i) {
145 bool exists = false;
Zach Reiznerff30b522015-10-28 19:08:45 -0700146 for (const DrmMode &mode : modes_) {
147 if (mode == c->modes[i]) {
148 new_modes.push_back(mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400149 exists = true;
150 break;
151 }
152 }
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200153 if (!exists) {
154 DrmMode m(&c->modes[i]);
155 m.set_id(drm_->next_mode_id());
156 new_modes.push_back(m);
157 }
John Stultza3429c72019-03-26 21:11:31 -0700158 // Use only the first DRM_MODE_TYPE_PREFERRED mode found
159 if (!preferred_mode_found &&
160 (new_modes.back().type() & DRM_MODE_TYPE_PREFERRED)) {
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200161 preferred_mode_id_ = new_modes.back().id();
162 preferred_mode_found = true;
163 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400164 }
165 modes_.swap(new_modes);
John Stultz9bd25f92019-03-28 15:18:53 -0700166 if (!preferred_mode_found && modes_.size() != 0) {
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200167 preferred_mode_id_ = modes_[0].id();
168 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400169 return 0;
170}
171
172const DrmMode &DrmConnector::active_mode() const {
173 return active_mode_;
174}
175
Sean Paul877be972015-06-03 14:08:27 -0400176void DrmConnector::set_active_mode(const DrmMode &mode) {
177 active_mode_ = mode;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400178}
179
180const DrmProperty &DrmConnector::dpms_property() const {
181 return dpms_property_;
182}
183
Sean Paul877be972015-06-03 14:08:27 -0400184const DrmProperty &DrmConnector::crtc_id_property() const {
185 return crtc_id_property_;
186}
187
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000188const DrmProperty &DrmConnector::writeback_pixel_formats() const {
189 return writeback_pixel_formats_;
190}
191
192const DrmProperty &DrmConnector::writeback_fb_id() const {
193 return writeback_fb_id_;
194}
195
196const DrmProperty &DrmConnector::writeback_out_fence() const {
197 return writeback_out_fence_;
198}
199
Sean Paul6a55e9f2015-04-30 15:31:06 -0400200DrmEncoder *DrmConnector::encoder() const {
201 return encoder_;
202}
203
204void DrmConnector::set_encoder(DrmEncoder *encoder) {
205 encoder_ = encoder;
206}
207
Sean Paul09216492015-10-26 15:36:37 -0400208drmModeConnection DrmConnector::state() const {
209 return state_;
210}
211
Sean Paul6a55e9f2015-04-30 15:31:06 -0400212uint32_t DrmConnector::mm_width() const {
213 return mm_width_;
214}
215
216uint32_t DrmConnector::mm_height() const {
217 return mm_height_;
218}
Sean Paulf72cccd2018-08-27 13:59:08 -0400219} // namespace android