blob: 35ed45d2973caf2ba0ef7ef1deb98359c44c4bbf [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
Roman Stratiienko1e053b42021-10-25 22:54:20 +0300114DrmDevice::DrmDevice() {
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
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +0200119// NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100120std::tuple<int, int> DrmDevice::Init(const char *path, int num_displays) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400121 /* TODO: Use drmOpenControl here instead */
Roman Stratiienko0fade372021-02-20 13:59:55 +0200122 fd_ = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
Zach Reiznerff30b522015-10-28 19:08:45 -0700123 if (fd() < 0) {
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +0200124 // NOLINTNEXTLINE(concurrency-mt-unsafe): Fixme
Peter Collingbournec77052e2020-02-19 11:25:08 -0800125 ALOGE("Failed to open dri %s: %s", path, strerror(errno));
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100126 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400127 }
128
Zach Reiznerff30b522015-10-28 19:08:45 -0700129 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400130 if (ret) {
131 ALOGE("Failed to set universal plane cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100132 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400133 }
134
Zach Reiznerff30b522015-10-28 19:08:45 -0700135 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400136 if (ret) {
137 ALOGE("Failed to set atomic cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100138 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400139 }
140
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000141#ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS
142 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
143 if (ret) {
144 ALOGI("Failed to set writeback cap %d", ret);
145 ret = 0;
146 }
147#endif
148
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200149 uint64_t cap_value = 0;
150 if (drmGetCap(fd(), DRM_CAP_ADDFB2_MODIFIERS, &cap_value)) {
151 ALOGW("drmGetCap failed. Fallback to no modifier support.");
152 cap_value = 0;
153 }
154 HasAddFb2ModifiersSupport_ = cap_value != 0;
155
John Stultzfa002332021-02-02 01:34:45 +0000156 drmSetMaster(fd());
157 if (!drmIsMaster(fd())) {
158 ALOGE("DRM/KMS master access required");
159 return std::make_tuple(-EACCES, 0);
Roman Stratiienko3b24cd92021-01-13 10:32:04 +0200160 }
161
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300162 auto res = MakeDrmModeResUnique(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400163 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100164 ALOGE("Failed to get DrmDevice resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100165 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400166 }
167
Sean Paulf72cccd2018-08-27 13:59:08 -0400168 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
169 res->min_height);
170 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
171 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -0800172
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100173 // Assumes that the primary display will always be in the first
174 // drm_device opened.
175 bool found_primary = num_displays != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400176
177 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300178 auto c = MakeDrmModeCrtcUnique(fd(), res->crtcs[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400179 if (!c) {
180 ALOGE("Failed to get crtc %d", res->crtcs[i]);
181 ret = -ENODEV;
182 break;
183 }
184
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300185 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c.get(), i));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400186
Sean Paul877be972015-06-03 14:08:27 -0400187 ret = crtc->Init();
188 if (ret) {
189 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
Sean Paul877be972015-06-03 14:08:27 -0400190 break;
191 }
Zach Reiznerff30b522015-10-28 19:08:45 -0700192 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400193 }
194
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300195 std::vector<uint32_t> possible_clones;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400196 for (int i = 0; !ret && i < res->count_encoders; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300197 auto e = MakeDrmModeEncoderUnique(fd(), res->encoders[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400198 if (!e) {
199 ALOGE("Failed to get encoder %d", res->encoders[i]);
200 ret = -ENODEV;
201 break;
202 }
203
204 std::vector<DrmCrtc *> possible_crtcs;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200205 DrmCrtc *current_crtc = nullptr;
Zach Reiznerff30b522015-10-28 19:08:45 -0700206 for (auto &crtc : crtcs_) {
207 if ((1 << crtc->pipe()) & e->possible_crtcs)
208 possible_crtcs.push_back(crtc.get());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400209
Zach Reiznerff30b522015-10-28 19:08:45 -0700210 if (crtc->id() == e->crtc_id)
211 current_crtc = crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400212 }
213
Zach Reiznerff30b522015-10-28 19:08:45 -0700214 std::unique_ptr<DrmEncoder> enc(
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300215 new DrmEncoder(e.get(), current_crtc, possible_crtcs));
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000216 possible_clones.push_back(e->possible_clones);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400217
Zach Reiznerff30b522015-10-28 19:08:45 -0700218 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400219 }
220
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000221 for (unsigned int i = 0; i < encoders_.size(); i++) {
222 for (unsigned int j = 0; j < encoders_.size(); j++)
223 if (possible_clones[i] & (1 << j))
224 encoders_[i]->AddPossibleClone(encoders_[j].get());
225 }
226
Sean Paul6a55e9f2015-04-30 15:31:06 -0400227 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300228 auto c = MakeDrmModeConnectorUnique(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400229 if (!c) {
230 ALOGE("Failed to get connector %d", res->connectors[i]);
231 ret = -ENODEV;
232 break;
233 }
234
235 std::vector<DrmEncoder *> possible_encoders;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200236 DrmEncoder *current_encoder = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400237 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700238 for (auto &encoder : encoders_) {
239 if (encoder->id() == c->encoders[j])
240 possible_encoders.push_back(encoder.get());
241 if (encoder->id() == c->encoder_id)
242 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400243 }
244 }
245
Zach Reiznerff30b522015-10-28 19:08:45 -0700246 std::unique_ptr<DrmConnector> conn(
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300247 new DrmConnector(this, c.get(), current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400248
Sean Paul6a55e9f2015-04-30 15:31:06 -0400249 ret = conn->Init();
250 if (ret) {
251 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400252 break;
253 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400254
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000255 if (conn->writeback())
256 writeback_connectors_.emplace_back(std::move(conn));
257 else
258 connectors_.emplace_back(std::move(conn));
Robert Foss610d9892017-11-01 12:50:04 -0500259 }
260
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200261 // Primary display priority:
Jason Macnakf1af9572020-08-20 11:49:51 -0700262 // 1) vendor.hwc.drm.primary_display_order property
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200263 // 2) internal connectors
264 // 3) anything else
265 std::vector<DrmConnector *>
266 primary_candidates = make_primary_display_candidates(connectors_);
267 if (!primary_candidates.empty() && !found_primary) {
268 DrmConnector &conn = **std::begin(primary_candidates);
269 conn.set_display(num_displays);
270 displays_[num_displays] = num_displays;
271 ++num_displays;
272 found_primary = true;
273 } else {
274 ALOGE(
Jason Macnakf1af9572020-08-20 11:49:51 -0700275 "Failed to find primary display from "
276 "\"vendor.hwc.drm.primary_display_order\" property");
Sean Paul6a55e9f2015-04-30 15:31:06 -0400277 }
Robert Foss610d9892017-11-01 12:50:04 -0500278
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200279 // If no priority display were found then pick first available as primary and
280 // for the others assign consecutive display_numbers.
Robert Foss610d9892017-11-01 12:50:04 -0500281 for (auto &conn : connectors_) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100282 if (conn->external() || conn->internal()) {
283 if (!found_primary) {
284 conn->set_display(num_displays);
285 displays_[num_displays] = num_displays;
286 found_primary = true;
287 ++num_displays;
288 } else if (conn->display() < 0) {
289 conn->set_display(num_displays);
290 displays_[num_displays] = num_displays;
291 ++num_displays;
292 }
Robert Foss610d9892017-11-01 12:50:04 -0500293 }
294 }
295
Sean Paul6a55e9f2015-04-30 15:31:06 -0400296 // Catch-all for the above loops
297 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100298 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400299
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300300 auto plane_res = MakeDrmModePlaneResUnique(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400301 if (!plane_res) {
302 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100303 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400304 }
305
306 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300307 auto p = MakeDrmModePlaneUnique(fd(), plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400308 if (!p) {
309 ALOGE("Failed to get plane %d", plane_res->planes[i]);
310 ret = -ENODEV;
311 break;
312 }
313
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300314 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p.get()));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400315
Sean Paul6a55e9f2015-04-30 15:31:06 -0400316 ret = plane->Init();
317 if (ret) {
318 ALOGE("Init plane %d failed", plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400319 break;
320 }
321
Zach Reiznerff30b522015-10-28 19:08:45 -0700322 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400323 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400324 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100325 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400326
Zach Reiznerff30b522015-10-28 19:08:45 -0700327 for (auto &conn : connectors_) {
328 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400329 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700330 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100331 return std::make_tuple(ret, 0);
Sean Paul57355412015-09-19 09:14:34 -0400332 }
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000333 if (!AttachWriteback(conn.get())) {
334 ALOGI("Display %d has writeback attach to it", conn->display());
335 }
Sean Paul57355412015-09-19 09:14:34 -0400336 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100337 return std::make_tuple(ret, displays_.size());
338}
339
340bool DrmDevice::HandlesDisplay(int display) const {
341 return displays_.find(display) != displays_.end();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400342}
343
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100344DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200345 for (const auto &conn : connectors_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700346 if (conn->display() == display)
347 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400348 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200349 return nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400350}
351
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000352DrmConnector *DrmDevice::GetWritebackConnectorForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200353 for (const auto &conn : writeback_connectors_) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000354 if (conn->display() == display)
355 return conn.get();
356 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200357 return nullptr;
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000358}
359
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200360// TODO(nobody): what happens when hotplugging
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100361DrmConnector *DrmDevice::AvailableWritebackConnector(int display) const {
362 DrmConnector *writeback_conn = GetWritebackConnectorForDisplay(display);
363 DrmConnector *display_conn = GetConnectorForDisplay(display);
364 // If we have a writeback already attached to the same CRTC just use that,
365 // if possible.
366 if (display_conn && writeback_conn &&
367 writeback_conn->encoder()->CanClone(display_conn->encoder()))
368 return writeback_conn;
369
370 // Use another CRTC if available and doesn't have any connector
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200371 for (const auto &crtc : crtcs_) {
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100372 if (crtc->display() == display)
373 continue;
374 display_conn = GetConnectorForDisplay(crtc->display());
375 // If we have a display connected don't use it for writeback
376 if (display_conn && display_conn->state() == DRM_MODE_CONNECTED)
377 continue;
378 writeback_conn = GetWritebackConnectorForDisplay(crtc->display());
379 if (writeback_conn)
380 return writeback_conn;
381 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200382 return nullptr;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100383}
384
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100385DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200386 for (const auto &crtc : crtcs_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700387 if (crtc->display() == display)
388 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400389 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200390 return nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400391}
392
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100393DrmPlane *DrmDevice::GetPlane(uint32_t id) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200394 for (const auto &plane : planes_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700395 if (plane->id() == id)
396 return plane.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 +0100401const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400402 return crtcs_;
403}
404
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100405uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400406 return ++mode_id_;
407}
408
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100409int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400410 /* First try to use the currently-bound crtc */
411 DrmCrtc *crtc = enc->crtc();
412 if (crtc && crtc->can_bind(display)) {
413 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000414 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400415 return 0;
416 }
417
418 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700419 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400420 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700421 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400422 continue;
423
Zach Reiznerff30b522015-10-28 19:08:45 -0700424 if (crtc->can_bind(display)) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700425 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000426 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400427 return 0;
428 }
429 }
430
431 /* We can't use the encoder, but nothing went wrong, try another one */
432 return -EAGAIN;
433}
434
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100435int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400436 int display = connector->display();
437 /* Try to use current setup first */
438 if (connector->encoder()) {
439 int ret = TryEncoderForDisplay(display, connector->encoder());
440 if (!ret) {
441 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200442 }
443
444 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400445 ALOGE("Could not set mode %d/%d", display, ret);
446 return ret;
447 }
448 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400449
Zach Reiznerff30b522015-10-28 19:08:45 -0700450 for (DrmEncoder *enc : connector->possible_encoders()) {
451 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400452 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700453 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400454 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200455 }
456
457 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400458 ALOGE("Could not set mode %d/%d", display, ret);
459 return ret;
460 }
461 }
462 ALOGE("Could not find a suitable encoder/crtc for display %d",
463 connector->display());
464 return -ENODEV;
465}
466
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000467// Attach writeback connector to the CRTC linked to the display_conn
468int DrmDevice::AttachWriteback(DrmConnector *display_conn) {
469 DrmCrtc *display_crtc = display_conn->encoder()->crtc();
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200470 if (GetWritebackConnectorForDisplay(display_crtc->display()) != nullptr) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000471 ALOGE("Display already has writeback attach to it");
472 return -EINVAL;
473 }
474 for (auto &writeback_conn : writeback_connectors_) {
475 if (writeback_conn->display() >= 0)
476 continue;
477 for (DrmEncoder *writeback_enc : writeback_conn->possible_encoders()) {
478 for (DrmCrtc *possible_crtc : writeback_enc->possible_crtcs()) {
479 if (possible_crtc != display_crtc)
480 continue;
481 // Use just encoders which had not been bound already
482 if (writeback_enc->can_bind(display_crtc->display())) {
483 writeback_enc->set_crtc(display_crtc);
484 writeback_conn->set_encoder(writeback_enc);
485 writeback_conn->set_display(display_crtc->display());
486 writeback_conn->UpdateModes();
487 return 0;
488 }
489 }
490 }
491 }
492 return -EINVAL;
493}
494
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300495auto DrmDevice::RegisterUserPropertyBlob(void *data, size_t length) const
496 -> DrmModeUserPropertyBlobUnique {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200497 struct drm_mode_create_blob create_blob {};
Sean Paul877be972015-06-03 14:08:27 -0400498 create_blob.length = length;
499 create_blob.data = (__u64)data;
500
Zach Reiznerff30b522015-10-28 19:08:45 -0700501 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400502 if (ret) {
503 ALOGE("Failed to create mode property blob %d", ret);
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300504 return DrmModeUserPropertyBlobUnique();
Sean Paul877be972015-06-03 14:08:27 -0400505 }
Sean Paul877be972015-06-03 14:08:27 -0400506
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300507 return DrmModeUserPropertyBlobUnique(
508 new uint32_t(create_blob.blob_id), [this](const uint32_t *it) {
509 struct drm_mode_destroy_blob destroy_blob {};
510 destroy_blob.blob_id = (__u32)*it;
511 int err = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
512 if (err != 0) {
513 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", *it,
514 err);
515 }
516 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
517 delete it;
518 });
Sean Paul877be972015-06-03 14:08:27 -0400519}
520
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100521int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200522 const char *prop_name, DrmProperty *property) const {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200523 drmModeObjectPropertiesPtr props = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400524
Zach Reiznerff30b522015-10-28 19:08:45 -0700525 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400526 if (!props) {
527 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
528 return -ENODEV;
529 }
530
531 bool found = false;
532 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700533 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400534 if (!strcmp(p->name, prop_name)) {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300535 property->Init(obj_id, p, props->prop_values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400536 found = true;
537 }
538 drmModeFreeProperty(p);
539 }
540
541 drmModeFreeObjectProperties(props);
542 return found ? 0 : -ENOENT;
543}
544
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100545int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
Roman Stratiienko0b063882021-09-30 10:15:05 +0300546 DrmProperty *property) const {
Sean Paul877be972015-06-03 14:08:27 -0400547 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
548}
549
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100550int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
551 const char *prop_name,
Roman Stratiienko0b063882021-09-30 10:15:05 +0300552 DrmProperty *property) const {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400553 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
554 property);
555}
Matvii Zorinef3c7972020-08-11 15:15:44 +0300556
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200557std::string DrmDevice::GetName() const {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200558 auto *ver = drmGetVersion(fd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300559 if (!ver) {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200560 ALOGW("Failed to get drm version for fd=%d", fd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300561 return "generic";
562 }
563
564 std::string name(ver->name);
565 drmFreeVersion(ver);
566 return name;
567}
Roman Stratiienko56f4adc2021-09-29 12:47:12 +0300568
569auto DrmDevice::IsKMSDev(const char *path) -> bool {
570 auto fd = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
571 if (!fd) {
572 return false;
573 }
574
575 auto res = MakeDrmModeResUnique(fd.Get());
576 if (!res) {
577 return false;
578 }
579
580 bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
581 res->count_encoders > 0;
582
583 return is_kms;
584}
585
Sean Paulf72cccd2018-08-27 13:59:08 -0400586} // namespace android