1 /*
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"
20 #include "drmdevice.h"
21
22 #include <errno.h>
23 #include <stdint.h>
24
25 #include <array>
26 #include <sstream>
27
28 #include <log/log.h>
29 #include <xf86drmMode.h>
30
31 namespace android {
32
33 constexpr size_t TYPES_COUNT = 17;
34
DrmConnector(DrmDevice * drm,drmModeConnectorPtr c,DrmEncoder * current_encoder,std::vector<DrmEncoder * > & possible_encoders)35 DrmConnector::DrmConnector(DrmDevice *drm, drmModeConnectorPtr c,
36 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),
43 type_id_(c->connector_type_id),
44 state_(c->connection),
45 mm_width_(c->mmWidth),
46 mm_height_(c->mmHeight),
47 possible_encoders_(possible_encoders) {
48 }
49
Init()50 int 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 }
56 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 }
61 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 }
81 return 0;
82 }
83
id() const84 uint32_t DrmConnector::id() const {
85 return id_;
86 }
87
display() const88 int DrmConnector::display() const {
89 return display_;
90 }
91
set_display(int display)92 void DrmConnector::set_display(int display) {
93 display_ = display;
94 }
95
internal() const96 bool DrmConnector::internal() const {
97 return type_ == DRM_MODE_CONNECTOR_LVDS || type_ == DRM_MODE_CONNECTOR_eDP ||
98 type_ == DRM_MODE_CONNECTOR_DSI ||
99 type_ == DRM_MODE_CONNECTOR_VIRTUAL || type_ == DRM_MODE_CONNECTOR_DPI;
100 }
101
external() const102 bool DrmConnector::external() const {
103 return type_ == DRM_MODE_CONNECTOR_HDMIA ||
104 type_ == DRM_MODE_CONNECTOR_DisplayPort ||
105 type_ == DRM_MODE_CONNECTOR_DVID || type_ == DRM_MODE_CONNECTOR_DVII ||
106 type_ == DRM_MODE_CONNECTOR_VGA;
107 }
108
writeback() const109 bool 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
valid_type() const117 bool DrmConnector::valid_type() const {
118 return internal() || external() || writeback();
119 }
120
name() const121 std::string DrmConnector::name() const {
122 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"};
126
127 if (type_ < TYPES_COUNT) {
128 std::ostringstream name_buf;
129 name_buf << names[type_] << "-" << type_id_;
130 return name_buf.str();
131 } else {
132 ALOGE("Unknown type in connector %d, could not make his name", id_);
133 return "None";
134 }
135 }
136
UpdateModes()137 int DrmConnector::UpdateModes() {
138 int fd = drm_->fd();
139
140 drmModeConnectorPtr c = drmModeGetConnector(fd, id_);
141 if (!c) {
142 ALOGE("Failed to get connector %d", id_);
143 return -ENODEV;
144 }
145
146 state_ = c->connection;
147
148 bool preferred_mode_found = false;
149 std::vector<DrmMode> new_modes;
150 for (int i = 0; i < c->count_modes; ++i) {
151 bool exists = false;
152 for (const DrmMode &mode : modes_) {
153 if (mode == c->modes[i]) {
154 new_modes.push_back(mode);
155 exists = true;
156 break;
157 }
158 }
159 if (!exists) {
160 DrmMode m(&c->modes[i]);
161 m.set_id(drm_->next_mode_id());
162 new_modes.push_back(m);
163 }
164 // Use only the first DRM_MODE_TYPE_PREFERRED mode found
165 if (!preferred_mode_found &&
166 (new_modes.back().type() & DRM_MODE_TYPE_PREFERRED)) {
167 preferred_mode_id_ = new_modes.back().id();
168 preferred_mode_found = true;
169 }
170 }
171 modes_.swap(new_modes);
172 if (!preferred_mode_found && modes_.size() != 0) {
173 preferred_mode_id_ = modes_[0].id();
174 }
175 return 0;
176 }
177
active_mode() const178 const DrmMode &DrmConnector::active_mode() const {
179 return active_mode_;
180 }
181
set_active_mode(const DrmMode & mode)182 void DrmConnector::set_active_mode(const DrmMode &mode) {
183 active_mode_ = mode;
184 }
185
dpms_property() const186 const DrmProperty &DrmConnector::dpms_property() const {
187 return dpms_property_;
188 }
189
crtc_id_property() const190 const DrmProperty &DrmConnector::crtc_id_property() const {
191 return crtc_id_property_;
192 }
193
writeback_pixel_formats() const194 const DrmProperty &DrmConnector::writeback_pixel_formats() const {
195 return writeback_pixel_formats_;
196 }
197
writeback_fb_id() const198 const DrmProperty &DrmConnector::writeback_fb_id() const {
199 return writeback_fb_id_;
200 }
201
writeback_out_fence() const202 const DrmProperty &DrmConnector::writeback_out_fence() const {
203 return writeback_out_fence_;
204 }
205
encoder() const206 DrmEncoder *DrmConnector::encoder() const {
207 return encoder_;
208 }
209
set_encoder(DrmEncoder * encoder)210 void DrmConnector::set_encoder(DrmEncoder *encoder) {
211 encoder_ = encoder;
212 }
213
state() const214 drmModeConnection DrmConnector::state() const {
215 return state_;
216 }
217
mm_width() const218 uint32_t DrmConnector::mm_width() const {
219 return mm_width_;
220 }
221
mm_height() const222 uint32_t DrmConnector::mm_height() const {
223 return mm_height_;
224 }
225 } // namespace android
226