blob: 543827d9c8c6582eaa8b5757461d05c3f07f6946 [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
John Stultz9057a6f2018-04-26 12:05:55 -070025#include <log/log.h>
Sean Paul6a55e9f2015-04-30 15:31:06 -040026#include <xf86drmMode.h>
27
28namespace android {
29
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010030DrmConnector::DrmConnector(DrmDevice *drm, drmModeConnectorPtr c,
Sean Paul6a55e9f2015-04-30 15:31:06 -040031 DrmEncoder *current_encoder,
32 std::vector<DrmEncoder *> &possible_encoders)
33 : drm_(drm),
34 id_(c->connector_id),
35 encoder_(current_encoder),
36 display_(-1),
37 type_(c->connector_type),
38 state_(c->connection),
39 mm_width_(c->mmWidth),
40 mm_height_(c->mmHeight),
41 possible_encoders_(possible_encoders) {
42}
43
Sean Paul6a55e9f2015-04-30 15:31:06 -040044int DrmConnector::Init() {
45 int ret = drm_->GetConnectorProperty(*this, "DPMS", &dpms_property_);
46 if (ret) {
47 ALOGE("Could not get DPMS property\n");
48 return ret;
49 }
Sean Paul877be972015-06-03 14:08:27 -040050 ret = drm_->GetConnectorProperty(*this, "CRTC_ID", &crtc_id_property_);
51 if (ret) {
52 ALOGE("Could not get CRTC_ID property\n");
53 return ret;
54 }
Alexandru Gheorghe364f9572018-03-21 11:40:01 +000055 if (writeback()) {
56 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_PIXEL_FORMATS",
57 &writeback_pixel_formats_);
58 if (ret) {
59 ALOGE("Could not get WRITEBACK_PIXEL_FORMATS connector_id = %d\n", id_);
60 return ret;
61 }
62 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_FB_ID",
63 &writeback_fb_id_);
64 if (ret) {
65 ALOGE("Could not get WRITEBACK_FB_ID connector_id = %d\n", id_);
66 return ret;
67 }
68 ret = drm_->GetConnectorProperty(*this, "WRITEBACK_OUT_FENCE_PTR",
69 &writeback_out_fence_);
70 if (ret) {
71 ALOGE("Could not get WRITEBACK_OUT_FENCE_PTR connector_id = %d\n", id_);
72 return ret;
73 }
74 }
Sean Paul6a55e9f2015-04-30 15:31:06 -040075 return 0;
76}
77
78uint32_t DrmConnector::id() const {
79 return id_;
80}
81
82int DrmConnector::display() const {
83 return display_;
84}
85
86void DrmConnector::set_display(int display) {
87 display_ = display;
88}
89
Robert Foss610d9892017-11-01 12:50:04 -050090bool DrmConnector::internal() const {
Sean Paul6a55e9f2015-04-30 15:31:06 -040091 return type_ == DRM_MODE_CONNECTOR_LVDS || type_ == DRM_MODE_CONNECTOR_eDP ||
Mykhailo Sopiha87769e62019-07-11 14:31:09 +030092 type_ == DRM_MODE_CONNECTOR_DSI ||
93 type_ == DRM_MODE_CONNECTOR_VIRTUAL || type_ == DRM_MODE_CONNECTOR_DPI;
Sean Paul6a55e9f2015-04-30 15:31:06 -040094}
95
Robert Foss610d9892017-11-01 12:50:04 -050096bool DrmConnector::external() const {
Sean Paulf72cccd2018-08-27 13:59:08 -040097 return type_ == DRM_MODE_CONNECTOR_HDMIA ||
98 type_ == DRM_MODE_CONNECTOR_DisplayPort ||
Mauro Rossi556bedf2018-01-06 00:59:59 +010099 type_ == DRM_MODE_CONNECTOR_DVID || type_ == DRM_MODE_CONNECTOR_DVII ||
100 type_ == DRM_MODE_CONNECTOR_VGA;
Robert Foss610d9892017-11-01 12:50:04 -0500101}
102
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000103bool DrmConnector::writeback() const {
104#ifdef DRM_MODE_CONNECTOR_WRITEBACK
105 return type_ == DRM_MODE_CONNECTOR_WRITEBACK;
106#else
107 return false;
108#endif
109}
110
Robert Foss610d9892017-11-01 12:50:04 -0500111bool DrmConnector::valid_type() const {
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000112 return internal() || external() || writeback();
Robert Foss610d9892017-11-01 12:50:04 -0500113}
114
Sean Paul6a55e9f2015-04-30 15:31:06 -0400115int DrmConnector::UpdateModes() {
116 int fd = drm_->fd();
117
118 drmModeConnectorPtr c = drmModeGetConnector(fd, id_);
119 if (!c) {
120 ALOGE("Failed to get connector %d", id_);
121 return -ENODEV;
122 }
123
Sean Paul09216492015-10-26 15:36:37 -0400124 state_ = c->connection;
125
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200126 bool preferred_mode_found = false;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400127 std::vector<DrmMode> new_modes;
128 for (int i = 0; i < c->count_modes; ++i) {
129 bool exists = false;
Zach Reiznerff30b522015-10-28 19:08:45 -0700130 for (const DrmMode &mode : modes_) {
131 if (mode == c->modes[i]) {
132 new_modes.push_back(mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400133 exists = true;
134 break;
135 }
136 }
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200137 if (!exists) {
138 DrmMode m(&c->modes[i]);
139 m.set_id(drm_->next_mode_id());
140 new_modes.push_back(m);
141 }
John Stultza3429c72019-03-26 21:11:31 -0700142 // Use only the first DRM_MODE_TYPE_PREFERRED mode found
143 if (!preferred_mode_found &&
144 (new_modes.back().type() & DRM_MODE_TYPE_PREFERRED)) {
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200145 preferred_mode_id_ = new_modes.back().id();
146 preferred_mode_found = true;
147 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400148 }
149 modes_.swap(new_modes);
John Stultz9bd25f92019-03-28 15:18:53 -0700150 if (!preferred_mode_found && modes_.size() != 0) {
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200151 preferred_mode_id_ = modes_[0].id();
152 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400153 return 0;
154}
155
156const DrmMode &DrmConnector::active_mode() const {
157 return active_mode_;
158}
159
Sean Paul877be972015-06-03 14:08:27 -0400160void DrmConnector::set_active_mode(const DrmMode &mode) {
161 active_mode_ = mode;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400162}
163
164const DrmProperty &DrmConnector::dpms_property() const {
165 return dpms_property_;
166}
167
Sean Paul877be972015-06-03 14:08:27 -0400168const DrmProperty &DrmConnector::crtc_id_property() const {
169 return crtc_id_property_;
170}
171
Alexandru Gheorghe364f9572018-03-21 11:40:01 +0000172const DrmProperty &DrmConnector::writeback_pixel_formats() const {
173 return writeback_pixel_formats_;
174}
175
176const DrmProperty &DrmConnector::writeback_fb_id() const {
177 return writeback_fb_id_;
178}
179
180const DrmProperty &DrmConnector::writeback_out_fence() const {
181 return writeback_out_fence_;
182}
183
Sean Paul6a55e9f2015-04-30 15:31:06 -0400184DrmEncoder *DrmConnector::encoder() const {
185 return encoder_;
186}
187
188void DrmConnector::set_encoder(DrmEncoder *encoder) {
189 encoder_ = encoder;
190}
191
Sean Paul09216492015-10-26 15:36:37 -0400192drmModeConnection DrmConnector::state() const {
193 return state_;
194}
195
Sean Paul6a55e9f2015-04-30 15:31:06 -0400196uint32_t DrmConnector::mm_width() const {
197 return mm_width_;
198}
199
200uint32_t DrmConnector::mm_height() const {
201 return mm_height_;
202}
Sean Paulf72cccd2018-08-27 13:59:08 -0400203} // namespace android