blob: d9198d49d0b4c45e9a4e2c87d274e142e10c771f [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 Stratiienkoe2f2c922021-02-13 10:57:47 +0200125 fd_.Set(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
Zach Reiznerff30b522015-10-28 19:08:45 -0700164 drmModeResPtr res = drmModeGetResources(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) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700180 drmModeCrtcPtr c = drmModeGetCrtc(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
Zach Reiznerff30b522015-10-28 19:08:45 -0700187 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400188 drmModeFreeCrtc(c);
189
Sean Paul877be972015-06-03 14:08:27 -0400190 ret = crtc->Init();
191 if (ret) {
192 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
Sean Paul877be972015-06-03 14:08:27 -0400193 break;
194 }
Zach Reiznerff30b522015-10-28 19:08:45 -0700195 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400196 }
197
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000198 std::vector<int> possible_clones;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400199 for (int i = 0; !ret && i < res->count_encoders; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700200 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400201 if (!e) {
202 ALOGE("Failed to get encoder %d", res->encoders[i]);
203 ret = -ENODEV;
204 break;
205 }
206
207 std::vector<DrmCrtc *> possible_crtcs;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200208 DrmCrtc *current_crtc = nullptr;
Zach Reiznerff30b522015-10-28 19:08:45 -0700209 for (auto &crtc : crtcs_) {
210 if ((1 << crtc->pipe()) & e->possible_crtcs)
211 possible_crtcs.push_back(crtc.get());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400212
Zach Reiznerff30b522015-10-28 19:08:45 -0700213 if (crtc->id() == e->crtc_id)
214 current_crtc = crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400215 }
216
Zach Reiznerff30b522015-10-28 19:08:45 -0700217 std::unique_ptr<DrmEncoder> enc(
218 new DrmEncoder(e, current_crtc, possible_crtcs));
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000219 possible_clones.push_back(e->possible_clones);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400220 drmModeFreeEncoder(e);
221
Zach Reiznerff30b522015-10-28 19:08:45 -0700222 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400223 }
224
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000225 for (unsigned int i = 0; i < encoders_.size(); i++) {
226 for (unsigned int j = 0; j < encoders_.size(); j++)
227 if (possible_clones[i] & (1 << j))
228 encoders_[i]->AddPossibleClone(encoders_[j].get());
229 }
230
Sean Paul6a55e9f2015-04-30 15:31:06 -0400231 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700232 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400233 if (!c) {
234 ALOGE("Failed to get connector %d", res->connectors[i]);
235 ret = -ENODEV;
236 break;
237 }
238
239 std::vector<DrmEncoder *> possible_encoders;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200240 DrmEncoder *current_encoder = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400241 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700242 for (auto &encoder : encoders_) {
243 if (encoder->id() == c->encoders[j])
244 possible_encoders.push_back(encoder.get());
245 if (encoder->id() == c->encoder_id)
246 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400247 }
248 }
249
Zach Reiznerff30b522015-10-28 19:08:45 -0700250 std::unique_ptr<DrmConnector> conn(
251 new DrmConnector(this, c, current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400252
253 drmModeFreeConnector(c);
254
Sean Paul6a55e9f2015-04-30 15:31:06 -0400255 ret = conn->Init();
256 if (ret) {
257 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400258 break;
259 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400260
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000261 if (conn->writeback())
262 writeback_connectors_.emplace_back(std::move(conn));
263 else
264 connectors_.emplace_back(std::move(conn));
Robert Foss610d9892017-11-01 12:50:04 -0500265 }
266
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200267 // Primary display priority:
Jason Macnakf1af9572020-08-20 11:49:51 -0700268 // 1) vendor.hwc.drm.primary_display_order property
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200269 // 2) internal connectors
270 // 3) anything else
271 std::vector<DrmConnector *>
272 primary_candidates = make_primary_display_candidates(connectors_);
273 if (!primary_candidates.empty() && !found_primary) {
274 DrmConnector &conn = **std::begin(primary_candidates);
275 conn.set_display(num_displays);
276 displays_[num_displays] = num_displays;
277 ++num_displays;
278 found_primary = true;
279 } else {
280 ALOGE(
Jason Macnakf1af9572020-08-20 11:49:51 -0700281 "Failed to find primary display from "
282 "\"vendor.hwc.drm.primary_display_order\" property");
Sean Paul6a55e9f2015-04-30 15:31:06 -0400283 }
Robert Foss610d9892017-11-01 12:50:04 -0500284
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200285 // If no priority display were found then pick first available as primary and
286 // for the others assign consecutive display_numbers.
Robert Foss610d9892017-11-01 12:50:04 -0500287 for (auto &conn : connectors_) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100288 if (conn->external() || conn->internal()) {
289 if (!found_primary) {
290 conn->set_display(num_displays);
291 displays_[num_displays] = num_displays;
292 found_primary = true;
293 ++num_displays;
294 } else if (conn->display() < 0) {
295 conn->set_display(num_displays);
296 displays_[num_displays] = num_displays;
297 ++num_displays;
298 }
Robert Foss610d9892017-11-01 12:50:04 -0500299 }
300 }
301
Sean Paul6a55e9f2015-04-30 15:31:06 -0400302 if (res)
303 drmModeFreeResources(res);
304
305 // Catch-all for the above loops
306 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100307 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400308
Zach Reiznerff30b522015-10-28 19:08:45 -0700309 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400310 if (!plane_res) {
311 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100312 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400313 }
314
315 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700316 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400317 if (!p) {
318 ALOGE("Failed to get plane %d", plane_res->planes[i]);
319 ret = -ENODEV;
320 break;
321 }
322
Zach Reiznerff30b522015-10-28 19:08:45 -0700323 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400324
325 drmModeFreePlane(p);
326
Sean Paul6a55e9f2015-04-30 15:31:06 -0400327 ret = plane->Init();
328 if (ret) {
329 ALOGE("Init plane %d failed", plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400330 break;
331 }
332
Zach Reiznerff30b522015-10-28 19:08:45 -0700333 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400334 }
335 drmModeFreePlaneResources(plane_res);
336 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100337 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400338
Sean Paul047b9b22015-07-28 14:15:42 -0400339 ret = event_listener_.Init();
340 if (ret) {
341 ALOGE("Can't initialize event listener %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100342 return std::make_tuple(ret, 0);
Sean Paul047b9b22015-07-28 14:15:42 -0400343 }
344
Zach Reiznerff30b522015-10-28 19:08:45 -0700345 for (auto &conn : connectors_) {
346 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400347 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700348 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100349 return std::make_tuple(ret, 0);
Sean Paul57355412015-09-19 09:14:34 -0400350 }
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000351 if (!AttachWriteback(conn.get())) {
352 ALOGI("Display %d has writeback attach to it", conn->display());
353 }
Sean Paul57355412015-09-19 09:14:34 -0400354 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100355 return std::make_tuple(ret, displays_.size());
356}
357
358bool DrmDevice::HandlesDisplay(int display) const {
359 return displays_.find(display) != displays_.end();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400360}
361
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100362DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200363 for (const auto &conn : connectors_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700364 if (conn->display() == display)
365 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400366 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200367 return nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400368}
369
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000370DrmConnector *DrmDevice::GetWritebackConnectorForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200371 for (const auto &conn : writeback_connectors_) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000372 if (conn->display() == display)
373 return conn.get();
374 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200375 return nullptr;
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000376}
377
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200378// TODO(nobody): what happens when hotplugging
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100379DrmConnector *DrmDevice::AvailableWritebackConnector(int display) const {
380 DrmConnector *writeback_conn = GetWritebackConnectorForDisplay(display);
381 DrmConnector *display_conn = GetConnectorForDisplay(display);
382 // If we have a writeback already attached to the same CRTC just use that,
383 // if possible.
384 if (display_conn && writeback_conn &&
385 writeback_conn->encoder()->CanClone(display_conn->encoder()))
386 return writeback_conn;
387
388 // Use another CRTC if available and doesn't have any connector
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200389 for (const auto &crtc : crtcs_) {
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100390 if (crtc->display() == display)
391 continue;
392 display_conn = GetConnectorForDisplay(crtc->display());
393 // If we have a display connected don't use it for writeback
394 if (display_conn && display_conn->state() == DRM_MODE_CONNECTED)
395 continue;
396 writeback_conn = GetWritebackConnectorForDisplay(crtc->display());
397 if (writeback_conn)
398 return writeback_conn;
399 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200400 return nullptr;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100401}
402
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100403DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200404 for (const auto &crtc : crtcs_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700405 if (crtc->display() == display)
406 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400407 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200408 return nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400409}
410
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100411DrmPlane *DrmDevice::GetPlane(uint32_t id) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200412 for (const auto &plane : planes_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700413 if (plane->id() == id)
414 return plane.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400415 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200416 return nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400417}
418
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100419const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400420 return crtcs_;
421}
422
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100423uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400424 return ++mode_id_;
425}
426
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100427int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400428 /* First try to use the currently-bound crtc */
429 DrmCrtc *crtc = enc->crtc();
430 if (crtc && crtc->can_bind(display)) {
431 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000432 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400433 return 0;
434 }
435
436 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700437 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400438 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700439 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400440 continue;
441
Zach Reiznerff30b522015-10-28 19:08:45 -0700442 if (crtc->can_bind(display)) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700443 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000444 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400445 return 0;
446 }
447 }
448
449 /* We can't use the encoder, but nothing went wrong, try another one */
450 return -EAGAIN;
451}
452
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100453int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400454 int display = connector->display();
455 /* Try to use current setup first */
456 if (connector->encoder()) {
457 int ret = TryEncoderForDisplay(display, connector->encoder());
458 if (!ret) {
459 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200460 }
461
462 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400463 ALOGE("Could not set mode %d/%d", display, ret);
464 return ret;
465 }
466 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400467
Zach Reiznerff30b522015-10-28 19:08:45 -0700468 for (DrmEncoder *enc : connector->possible_encoders()) {
469 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400470 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700471 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400472 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200473 }
474
475 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400476 ALOGE("Could not set mode %d/%d", display, ret);
477 return ret;
478 }
479 }
480 ALOGE("Could not find a suitable encoder/crtc for display %d",
481 connector->display());
482 return -ENODEV;
483}
484
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000485// Attach writeback connector to the CRTC linked to the display_conn
486int DrmDevice::AttachWriteback(DrmConnector *display_conn) {
487 DrmCrtc *display_crtc = display_conn->encoder()->crtc();
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200488 if (GetWritebackConnectorForDisplay(display_crtc->display()) != nullptr) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000489 ALOGE("Display already has writeback attach to it");
490 return -EINVAL;
491 }
492 for (auto &writeback_conn : writeback_connectors_) {
493 if (writeback_conn->display() >= 0)
494 continue;
495 for (DrmEncoder *writeback_enc : writeback_conn->possible_encoders()) {
496 for (DrmCrtc *possible_crtc : writeback_enc->possible_crtcs()) {
497 if (possible_crtc != display_crtc)
498 continue;
499 // Use just encoders which had not been bound already
500 if (writeback_enc->can_bind(display_crtc->display())) {
501 writeback_enc->set_crtc(display_crtc);
502 writeback_conn->set_encoder(writeback_enc);
503 writeback_conn->set_display(display_crtc->display());
504 writeback_conn->UpdateModes();
505 return 0;
506 }
507 }
508 }
509 }
510 return -EINVAL;
511}
512
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100513int DrmDevice::CreatePropertyBlob(void *data, size_t length,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200514 uint32_t *blob_id) const {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200515 struct drm_mode_create_blob create_blob {};
Sean Paul877be972015-06-03 14:08:27 -0400516 create_blob.length = length;
517 create_blob.data = (__u64)data;
518
Zach Reiznerff30b522015-10-28 19:08:45 -0700519 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400520 if (ret) {
521 ALOGE("Failed to create mode property blob %d", ret);
522 return ret;
523 }
524 *blob_id = create_blob.blob_id;
525 return 0;
526}
527
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200528int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) const {
Sean Paul57355412015-09-19 09:14:34 -0400529 if (!blob_id)
530 return 0;
531
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200532 struct drm_mode_destroy_blob destroy_blob {};
Sean Paul877be972015-06-03 14:08:27 -0400533 destroy_blob.blob_id = (__u32)blob_id;
Zach Reiznerff30b522015-10-28 19:08:45 -0700534 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
Sean Paul877be972015-06-03 14:08:27 -0400535 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400536 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
Sean Paul877be972015-06-03 14:08:27 -0400537 return ret;
538 }
539 return 0;
540}
541
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100542DrmEventListener *DrmDevice::event_listener() {
Sean Paul047b9b22015-07-28 14:15:42 -0400543 return &event_listener_;
544}
545
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100546int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200547 const char *prop_name, DrmProperty *property) const {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200548 drmModeObjectPropertiesPtr props = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400549
Zach Reiznerff30b522015-10-28 19:08:45 -0700550 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400551 if (!props) {
552 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
553 return -ENODEV;
554 }
555
556 bool found = false;
557 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700558 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400559 if (!strcmp(p->name, prop_name)) {
560 property->Init(p, props->prop_values[i]);
561 found = true;
562 }
563 drmModeFreeProperty(p);
564 }
565
566 drmModeFreeObjectProperties(props);
567 return found ? 0 : -ENOENT;
568}
569
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100570int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
571 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400572 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
573}
574
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100575int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
576 DrmProperty *property) {
Sean Paul877be972015-06-03 14:08:27 -0400577 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
578}
579
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100580int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
581 const char *prop_name,
582 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400583 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
584 property);
585}
Matvii Zorinef3c7972020-08-11 15:15:44 +0300586
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200587std::string DrmDevice::GetName() const {
588 auto *ver = drmGetVersion(fd_.get());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300589 if (!ver) {
590 ALOGW("Failed to get drm version for fd=%d", fd_.get());
591 return "generic";
592 }
593
594 std::string name(ver->name);
595 drmFreeVersion(ver);
596 return name;
597}
Sean Paulf72cccd2018-08-27 13:59:08 -0400598} // namespace android