blob: be648b43d8166d5c6ec54227d19dd1f069213125 [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
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010017#define LOG_TAG "hwc-drm-device"
Sean Paul6a55e9f2015-04-30 15:31:06 -040018
Roman Stratiienko13cc3662020-08-29 21:35:39 +030019#include "DrmDevice.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040020
Sean Paul6a55e9f2015-04-30 15:31:06 -040021#include <fcntl.h>
Sean Paul6a55e9f2015-04-30 15:31:06 -040022#include <xf86drm.h>
23#include <xf86drmMode.h>
24
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020025#include <algorithm>
26#include <array>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020027#include <cerrno>
Roman Stratiienko13cc3662020-08-29 21:35:39 +030028#include <cinttypes>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020029#include <cstdint>
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030030#include <sstream>
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020031#include <string>
32
Roman Stratiienkod518a052021-02-25 19:15:14 +020033#include "utils/log.h"
34#include "utils/properties.h"
35
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020036static void trim_left(std::string *str) {
37 str->erase(std::begin(*str),
38 std::find_if(std::begin(*str), std::end(*str),
39 [](int ch) { return std::isspace(ch) == 0; }));
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020040}
41
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020042static void trim_right(std::string *str) {
43 str->erase(std::find_if(std::rbegin(*str), std::rend(*str),
44 [](int ch) { return std::isspace(ch) == 0; })
45 .base(),
46 std::end(*str));
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020047}
48
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020049static void trim(std::string *str) {
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020050 trim_left(str);
51 trim_right(str);
52}
53
Sean Paul6a55e9f2015-04-30 15:31:06 -040054namespace android {
55
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020056static std::vector<std::string> read_primary_display_order_prop() {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020057 std::array<char, PROPERTY_VALUE_MAX> display_order_buf{};
Jason Macnakf1af9572020-08-20 11:49:51 -070058 property_get("vendor.hwc.drm.primary_display_order", display_order_buf.data(),
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020059 "...");
60
61 std::vector<std::string> display_order;
62 std::istringstream str(display_order_buf.data());
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020063 for (std::string conn_name; std::getline(str, conn_name, ',');) {
64 trim(&conn_name);
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020065 display_order.push_back(std::move(conn_name));
66 }
67 return display_order;
68}
69
70static std::vector<DrmConnector *> make_primary_display_candidates(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020071 const std::vector<std::unique_ptr<DrmConnector>> &connectors) {
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020072 std::vector<DrmConnector *> primary_candidates;
73 std::transform(std::begin(connectors), std::end(connectors),
74 std::back_inserter(primary_candidates),
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020075 [](const std::unique_ptr<DrmConnector> &conn) {
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020076 return conn.get();
77 });
78 primary_candidates.erase(std::remove_if(std::begin(primary_candidates),
79 std::end(primary_candidates),
80 [](const DrmConnector *conn) {
81 return conn->state() !=
82 DRM_MODE_CONNECTED;
83 }),
84 std::end(primary_candidates));
85
86 std::vector<std::string> display_order = read_primary_display_order_prop();
87 bool use_other = display_order.back() == "...";
88
89 // putting connectors from primary_display_order first
90 auto curr_connector = std::begin(primary_candidates);
91 for (const std::string &display_name : display_order) {
92 auto it = std::find_if(std::begin(primary_candidates),
93 std::end(primary_candidates),
94 [&display_name](const DrmConnector *conn) {
95 return conn->name() == display_name;
96 });
97 if (it != std::end(primary_candidates)) {
98 std::iter_swap(it, curr_connector);
99 ++curr_connector;
100 }
101 }
102
103 if (use_other) {
104 // then putting internal connectors second, everything else afterwards
105 std::partition(curr_connector, std::end(primary_candidates),
106 [](const DrmConnector *conn) { return conn->internal(); });
107 } else {
108 primary_candidates.erase(curr_connector, std::end(primary_candidates));
109 }
110
111 return primary_candidates;
112}
113
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100114DrmDevice::DrmDevice() : event_listener_(this) {
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200115 self.reset(this);
116 mDrmFbImporter = std::make_unique<DrmFbImporter>(self);
Sean Paul047b9b22015-07-28 14:15:42 -0400117}
118
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100119DrmDevice::~DrmDevice() {
Sean Paul047b9b22015-07-28 14:15:42 -0400120 event_listener_.Exit();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400121}
122
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100123std::tuple<int, int> DrmDevice::Init(const char *path, int num_displays) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400124 /* TODO: Use drmOpenControl here instead */
Roman Stratiienko0fade372021-02-20 13:59:55 +0200125 fd_ = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
Zach Reiznerff30b522015-10-28 19:08:45 -0700126 if (fd() < 0) {
Peter Collingbournec77052e2020-02-19 11:25:08 -0800127 ALOGE("Failed to open dri %s: %s", path, strerror(errno));
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100128 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400129 }
130
Zach Reiznerff30b522015-10-28 19:08:45 -0700131 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400132 if (ret) {
133 ALOGE("Failed to set universal plane cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100134 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400135 }
136
Zach Reiznerff30b522015-10-28 19:08:45 -0700137 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400138 if (ret) {
139 ALOGE("Failed to set atomic cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100140 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400141 }
142
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000143#ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS
144 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
145 if (ret) {
146 ALOGI("Failed to set writeback cap %d", ret);
147 ret = 0;
148 }
149#endif
150
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200151 uint64_t cap_value = 0;
152 if (drmGetCap(fd(), DRM_CAP_ADDFB2_MODIFIERS, &cap_value)) {
153 ALOGW("drmGetCap failed. Fallback to no modifier support.");
154 cap_value = 0;
155 }
156 HasAddFb2ModifiersSupport_ = cap_value != 0;
157
John Stultzfa002332021-02-02 01:34:45 +0000158 drmSetMaster(fd());
159 if (!drmIsMaster(fd())) {
160 ALOGE("DRM/KMS master access required");
161 return std::make_tuple(-EACCES, 0);
Roman Stratiienko3b24cd92021-01-13 10:32:04 +0200162 }
163
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300164 auto res = MakeDrmModeResUnique(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400165 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100166 ALOGE("Failed to get DrmDevice resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100167 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400168 }
169
Sean Paulf72cccd2018-08-27 13:59:08 -0400170 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
171 res->min_height);
172 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
173 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -0800174
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100175 // Assumes that the primary display will always be in the first
176 // drm_device opened.
177 bool found_primary = num_displays != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400178
179 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300180 auto c = MakeDrmModeCrtcUnique(fd(), res->crtcs[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400181 if (!c) {
182 ALOGE("Failed to get crtc %d", res->crtcs[i]);
183 ret = -ENODEV;
184 break;
185 }
186
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300187 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c.get(), i));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400188
Sean Paul877be972015-06-03 14:08:27 -0400189 ret = crtc->Init();
190 if (ret) {
191 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
Sean Paul877be972015-06-03 14:08:27 -0400192 break;
193 }
Zach Reiznerff30b522015-10-28 19:08:45 -0700194 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400195 }
196
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300197 std::vector<uint32_t> possible_clones;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400198 for (int i = 0; !ret && i < res->count_encoders; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300199 auto e = MakeDrmModeEncoderUnique(fd(), res->encoders[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400200 if (!e) {
201 ALOGE("Failed to get encoder %d", res->encoders[i]);
202 ret = -ENODEV;
203 break;
204 }
205
206 std::vector<DrmCrtc *> possible_crtcs;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200207 DrmCrtc *current_crtc = nullptr;
Zach Reiznerff30b522015-10-28 19:08:45 -0700208 for (auto &crtc : crtcs_) {
209 if ((1 << crtc->pipe()) & e->possible_crtcs)
210 possible_crtcs.push_back(crtc.get());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400211
Zach Reiznerff30b522015-10-28 19:08:45 -0700212 if (crtc->id() == e->crtc_id)
213 current_crtc = crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400214 }
215
Zach Reiznerff30b522015-10-28 19:08:45 -0700216 std::unique_ptr<DrmEncoder> enc(
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300217 new DrmEncoder(e.get(), current_crtc, possible_crtcs));
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000218 possible_clones.push_back(e->possible_clones);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400219
Zach Reiznerff30b522015-10-28 19:08:45 -0700220 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400221 }
222
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000223 for (unsigned int i = 0; i < encoders_.size(); i++) {
224 for (unsigned int j = 0; j < encoders_.size(); j++)
225 if (possible_clones[i] & (1 << j))
226 encoders_[i]->AddPossibleClone(encoders_[j].get());
227 }
228
Sean Paul6a55e9f2015-04-30 15:31:06 -0400229 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300230 auto c = MakeDrmModeConnectorUnique(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400231 if (!c) {
232 ALOGE("Failed to get connector %d", res->connectors[i]);
233 ret = -ENODEV;
234 break;
235 }
236
237 std::vector<DrmEncoder *> possible_encoders;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200238 DrmEncoder *current_encoder = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400239 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700240 for (auto &encoder : encoders_) {
241 if (encoder->id() == c->encoders[j])
242 possible_encoders.push_back(encoder.get());
243 if (encoder->id() == c->encoder_id)
244 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400245 }
246 }
247
Zach Reiznerff30b522015-10-28 19:08:45 -0700248 std::unique_ptr<DrmConnector> conn(
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300249 new DrmConnector(this, c.get(), current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400250
Sean Paul6a55e9f2015-04-30 15:31:06 -0400251 ret = conn->Init();
252 if (ret) {
253 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400254 break;
255 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400256
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000257 if (conn->writeback())
258 writeback_connectors_.emplace_back(std::move(conn));
259 else
260 connectors_.emplace_back(std::move(conn));
Robert Foss610d9892017-11-01 12:50:04 -0500261 }
262
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200263 // Primary display priority:
Jason Macnakf1af9572020-08-20 11:49:51 -0700264 // 1) vendor.hwc.drm.primary_display_order property
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200265 // 2) internal connectors
266 // 3) anything else
267 std::vector<DrmConnector *>
268 primary_candidates = make_primary_display_candidates(connectors_);
269 if (!primary_candidates.empty() && !found_primary) {
270 DrmConnector &conn = **std::begin(primary_candidates);
271 conn.set_display(num_displays);
272 displays_[num_displays] = num_displays;
273 ++num_displays;
274 found_primary = true;
275 } else {
276 ALOGE(
Jason Macnakf1af9572020-08-20 11:49:51 -0700277 "Failed to find primary display from "
278 "\"vendor.hwc.drm.primary_display_order\" property");
Sean Paul6a55e9f2015-04-30 15:31:06 -0400279 }
Robert Foss610d9892017-11-01 12:50:04 -0500280
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200281 // If no priority display were found then pick first available as primary and
282 // for the others assign consecutive display_numbers.
Robert Foss610d9892017-11-01 12:50:04 -0500283 for (auto &conn : connectors_) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100284 if (conn->external() || conn->internal()) {
285 if (!found_primary) {
286 conn->set_display(num_displays);
287 displays_[num_displays] = num_displays;
288 found_primary = true;
289 ++num_displays;
290 } else if (conn->display() < 0) {
291 conn->set_display(num_displays);
292 displays_[num_displays] = num_displays;
293 ++num_displays;
294 }
Robert Foss610d9892017-11-01 12:50:04 -0500295 }
296 }
297
Sean Paul6a55e9f2015-04-30 15:31:06 -0400298 // Catch-all for the above loops
299 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100300 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400301
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300302 auto plane_res = MakeDrmModePlaneResUnique(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400303 if (!plane_res) {
304 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100305 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400306 }
307
308 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300309 auto p = MakeDrmModePlaneUnique(fd(), plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400310 if (!p) {
311 ALOGE("Failed to get plane %d", plane_res->planes[i]);
312 ret = -ENODEV;
313 break;
314 }
315
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300316 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p.get()));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400317
Sean Paul6a55e9f2015-04-30 15:31:06 -0400318 ret = plane->Init();
319 if (ret) {
320 ALOGE("Init plane %d failed", plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400321 break;
322 }
323
Zach Reiznerff30b522015-10-28 19:08:45 -0700324 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400325 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400326 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100327 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400328
Sean Paul047b9b22015-07-28 14:15:42 -0400329 ret = event_listener_.Init();
330 if (ret) {
331 ALOGE("Can't initialize event listener %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100332 return std::make_tuple(ret, 0);
Sean Paul047b9b22015-07-28 14:15:42 -0400333 }
334
Zach Reiznerff30b522015-10-28 19:08:45 -0700335 for (auto &conn : connectors_) {
336 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400337 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700338 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100339 return std::make_tuple(ret, 0);
Sean Paul57355412015-09-19 09:14:34 -0400340 }
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000341 if (!AttachWriteback(conn.get())) {
342 ALOGI("Display %d has writeback attach to it", conn->display());
343 }
Sean Paul57355412015-09-19 09:14:34 -0400344 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100345 return std::make_tuple(ret, displays_.size());
346}
347
348bool DrmDevice::HandlesDisplay(int display) const {
349 return displays_.find(display) != displays_.end();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400350}
351
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100352DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200353 for (const auto &conn : connectors_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700354 if (conn->display() == display)
355 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400356 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200357 return nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400358}
359
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000360DrmConnector *DrmDevice::GetWritebackConnectorForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200361 for (const auto &conn : writeback_connectors_) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000362 if (conn->display() == display)
363 return conn.get();
364 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200365 return nullptr;
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000366}
367
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200368// TODO(nobody): what happens when hotplugging
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100369DrmConnector *DrmDevice::AvailableWritebackConnector(int display) const {
370 DrmConnector *writeback_conn = GetWritebackConnectorForDisplay(display);
371 DrmConnector *display_conn = GetConnectorForDisplay(display);
372 // If we have a writeback already attached to the same CRTC just use that,
373 // if possible.
374 if (display_conn && writeback_conn &&
375 writeback_conn->encoder()->CanClone(display_conn->encoder()))
376 return writeback_conn;
377
378 // Use another CRTC if available and doesn't have any connector
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200379 for (const auto &crtc : crtcs_) {
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100380 if (crtc->display() == display)
381 continue;
382 display_conn = GetConnectorForDisplay(crtc->display());
383 // If we have a display connected don't use it for writeback
384 if (display_conn && display_conn->state() == DRM_MODE_CONNECTED)
385 continue;
386 writeback_conn = GetWritebackConnectorForDisplay(crtc->display());
387 if (writeback_conn)
388 return writeback_conn;
389 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200390 return nullptr;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100391}
392
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100393DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200394 for (const auto &crtc : crtcs_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700395 if (crtc->display() == display)
396 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400397 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200398 return nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400399}
400
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100401DrmPlane *DrmDevice::GetPlane(uint32_t id) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200402 for (const auto &plane : planes_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700403 if (plane->id() == id)
404 return plane.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400405 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200406 return nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400407}
408
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100409const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400410 return crtcs_;
411}
412
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100413uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400414 return ++mode_id_;
415}
416
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100417int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400418 /* First try to use the currently-bound crtc */
419 DrmCrtc *crtc = enc->crtc();
420 if (crtc && crtc->can_bind(display)) {
421 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000422 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400423 return 0;
424 }
425
426 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700427 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400428 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700429 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400430 continue;
431
Zach Reiznerff30b522015-10-28 19:08:45 -0700432 if (crtc->can_bind(display)) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700433 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000434 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400435 return 0;
436 }
437 }
438
439 /* We can't use the encoder, but nothing went wrong, try another one */
440 return -EAGAIN;
441}
442
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100443int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400444 int display = connector->display();
445 /* Try to use current setup first */
446 if (connector->encoder()) {
447 int ret = TryEncoderForDisplay(display, connector->encoder());
448 if (!ret) {
449 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200450 }
451
452 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400453 ALOGE("Could not set mode %d/%d", display, ret);
454 return ret;
455 }
456 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400457
Zach Reiznerff30b522015-10-28 19:08:45 -0700458 for (DrmEncoder *enc : connector->possible_encoders()) {
459 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400460 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700461 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400462 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200463 }
464
465 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400466 ALOGE("Could not set mode %d/%d", display, ret);
467 return ret;
468 }
469 }
470 ALOGE("Could not find a suitable encoder/crtc for display %d",
471 connector->display());
472 return -ENODEV;
473}
474
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000475// Attach writeback connector to the CRTC linked to the display_conn
476int DrmDevice::AttachWriteback(DrmConnector *display_conn) {
477 DrmCrtc *display_crtc = display_conn->encoder()->crtc();
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200478 if (GetWritebackConnectorForDisplay(display_crtc->display()) != nullptr) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000479 ALOGE("Display already has writeback attach to it");
480 return -EINVAL;
481 }
482 for (auto &writeback_conn : writeback_connectors_) {
483 if (writeback_conn->display() >= 0)
484 continue;
485 for (DrmEncoder *writeback_enc : writeback_conn->possible_encoders()) {
486 for (DrmCrtc *possible_crtc : writeback_enc->possible_crtcs()) {
487 if (possible_crtc != display_crtc)
488 continue;
489 // Use just encoders which had not been bound already
490 if (writeback_enc->can_bind(display_crtc->display())) {
491 writeback_enc->set_crtc(display_crtc);
492 writeback_conn->set_encoder(writeback_enc);
493 writeback_conn->set_display(display_crtc->display());
494 writeback_conn->UpdateModes();
495 return 0;
496 }
497 }
498 }
499 }
500 return -EINVAL;
501}
502
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100503int DrmDevice::CreatePropertyBlob(void *data, size_t length,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200504 uint32_t *blob_id) const {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200505 struct drm_mode_create_blob create_blob {};
Sean Paul877be972015-06-03 14:08:27 -0400506 create_blob.length = length;
507 create_blob.data = (__u64)data;
508
Zach Reiznerff30b522015-10-28 19:08:45 -0700509 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400510 if (ret) {
511 ALOGE("Failed to create mode property blob %d", ret);
512 return ret;
513 }
514 *blob_id = create_blob.blob_id;
515 return 0;
516}
517
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200518int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) const {
Sean Paul57355412015-09-19 09:14:34 -0400519 if (!blob_id)
520 return 0;
521
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200522 struct drm_mode_destroy_blob destroy_blob {};
Sean Paul877be972015-06-03 14:08:27 -0400523 destroy_blob.blob_id = (__u32)blob_id;
Zach Reiznerff30b522015-10-28 19:08:45 -0700524 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
Sean Paul877be972015-06-03 14:08:27 -0400525 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400526 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
Sean Paul877be972015-06-03 14:08:27 -0400527 return ret;
528 }
529 return 0;
530}
531
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100532DrmEventListener *DrmDevice::event_listener() {
Sean Paul047b9b22015-07-28 14:15:42 -0400533 return &event_listener_;
534}
535
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100536int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200537 const char *prop_name, DrmProperty *property) const {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200538 drmModeObjectPropertiesPtr props = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400539
Zach Reiznerff30b522015-10-28 19:08:45 -0700540 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400541 if (!props) {
542 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
543 return -ENODEV;
544 }
545
546 bool found = false;
547 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700548 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400549 if (!strcmp(p->name, prop_name)) {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300550 property->Init(obj_id, p, props->prop_values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400551 found = true;
552 }
553 drmModeFreeProperty(p);
554 }
555
556 drmModeFreeObjectProperties(props);
557 return found ? 0 : -ENOENT;
558}
559
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100560int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
561 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400562 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
563}
564
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100565int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
566 DrmProperty *property) {
Sean Paul877be972015-06-03 14:08:27 -0400567 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
568}
569
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100570int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
571 const char *prop_name,
572 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400573 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
574 property);
575}
Matvii Zorinef3c7972020-08-11 15:15:44 +0300576
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200577std::string DrmDevice::GetName() const {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200578 auto *ver = drmGetVersion(fd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300579 if (!ver) {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200580 ALOGW("Failed to get drm version for fd=%d", fd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300581 return "generic";
582 }
583
584 std::string name(ver->name);
585 drmFreeVersion(ver);
586 return name;
587}
Roman Stratiienko56f4adc2021-09-29 12:47:12 +0300588
589auto DrmDevice::IsKMSDev(const char *path) -> bool {
590 auto fd = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
591 if (!fd) {
592 return false;
593 }
594
595 auto res = MakeDrmModeResUnique(fd.Get());
596 if (!res) {
597 return false;
598 }
599
600 bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
601 res->count_encoders > 0;
602
603 return is_kms;
604}
605
Sean Paulf72cccd2018-08-27 13:59:08 -0400606} // namespace android