blob: 1c8427ce4c8bf0b2ef9d1b504090a75ec24378e4 [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 Stratiienko24a7fc42021-12-23 16:25:20 +020033#include "drm/DrmPlane.h"
Roman Stratiienkod518a052021-02-25 19:15:14 +020034#include "utils/log.h"
35#include "utils/properties.h"
36
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020037static void trim_left(std::string *str) {
38 str->erase(std::begin(*str),
39 std::find_if(std::begin(*str), std::end(*str),
40 [](int ch) { return std::isspace(ch) == 0; }));
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020041}
42
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020043static void trim_right(std::string *str) {
44 str->erase(std::find_if(std::rbegin(*str), std::rend(*str),
45 [](int ch) { return std::isspace(ch) == 0; })
46 .base(),
47 std::end(*str));
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020048}
49
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020050static void trim(std::string *str) {
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020051 trim_left(str);
52 trim_right(str);
53}
54
Sean Paul6a55e9f2015-04-30 15:31:06 -040055namespace android {
56
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020057static std::vector<std::string> read_primary_display_order_prop() {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020058 std::array<char, PROPERTY_VALUE_MAX> display_order_buf{};
Jason Macnakf1af9572020-08-20 11:49:51 -070059 property_get("vendor.hwc.drm.primary_display_order", display_order_buf.data(),
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020060 "...");
61
62 std::vector<std::string> display_order;
63 std::istringstream str(display_order_buf.data());
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020064 for (std::string conn_name; std::getline(str, conn_name, ',');) {
65 trim(&conn_name);
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020066 display_order.push_back(std::move(conn_name));
67 }
68 return display_order;
69}
70
71static std::vector<DrmConnector *> make_primary_display_candidates(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020072 const std::vector<std::unique_ptr<DrmConnector>> &connectors) {
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020073 std::vector<DrmConnector *> primary_candidates;
74 std::transform(std::begin(connectors), std::end(connectors),
75 std::back_inserter(primary_candidates),
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020076 [](const std::unique_ptr<DrmConnector> &conn) {
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020077 return conn.get();
78 });
79 primary_candidates.erase(std::remove_if(std::begin(primary_candidates),
80 std::end(primary_candidates),
81 [](const DrmConnector *conn) {
82 return conn->state() !=
83 DRM_MODE_CONNECTED;
84 }),
85 std::end(primary_candidates));
86
87 std::vector<std::string> display_order = read_primary_display_order_prop();
88 bool use_other = display_order.back() == "...";
89
90 // putting connectors from primary_display_order first
91 auto curr_connector = std::begin(primary_candidates);
92 for (const std::string &display_name : display_order) {
93 auto it = std::find_if(std::begin(primary_candidates),
94 std::end(primary_candidates),
95 [&display_name](const DrmConnector *conn) {
96 return conn->name() == display_name;
97 });
98 if (it != std::end(primary_candidates)) {
99 std::iter_swap(it, curr_connector);
100 ++curr_connector;
101 }
102 }
103
104 if (use_other) {
105 // then putting internal connectors second, everything else afterwards
106 std::partition(curr_connector, std::end(primary_candidates),
107 [](const DrmConnector *conn) { return conn->internal(); });
108 } else {
109 primary_candidates.erase(curr_connector, std::end(primary_candidates));
110 }
111
112 return primary_candidates;
113}
114
Roman Stratiienko1e053b42021-10-25 22:54:20 +0300115DrmDevice::DrmDevice() {
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200116 self.reset(this);
117 mDrmFbImporter = std::make_unique<DrmFbImporter>(self);
Sean Paul047b9b22015-07-28 14:15:42 -0400118}
119
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +0200120// NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100121std::tuple<int, int> DrmDevice::Init(const char *path, int num_displays) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400122 /* TODO: Use drmOpenControl here instead */
Roman Stratiienko0fade372021-02-20 13:59:55 +0200123 fd_ = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
Zach Reiznerff30b522015-10-28 19:08:45 -0700124 if (fd() < 0) {
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +0200125 // NOLINTNEXTLINE(concurrency-mt-unsafe): Fixme
Peter Collingbournec77052e2020-02-19 11:25:08 -0800126 ALOGE("Failed to open dri %s: %s", path, strerror(errno));
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100127 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400128 }
129
Zach Reiznerff30b522015-10-28 19:08:45 -0700130 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400131 if (ret) {
132 ALOGE("Failed to set universal plane cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100133 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400134 }
135
Zach Reiznerff30b522015-10-28 19:08:45 -0700136 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400137 if (ret) {
138 ALOGE("Failed to set atomic cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100139 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400140 }
141
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000142#ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS
143 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
144 if (ret) {
145 ALOGI("Failed to set writeback cap %d", ret);
146 ret = 0;
147 }
148#endif
149
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200150 uint64_t cap_value = 0;
151 if (drmGetCap(fd(), DRM_CAP_ADDFB2_MODIFIERS, &cap_value)) {
152 ALOGW("drmGetCap failed. Fallback to no modifier support.");
153 cap_value = 0;
154 }
155 HasAddFb2ModifiersSupport_ = cap_value != 0;
156
John Stultzfa002332021-02-02 01:34:45 +0000157 drmSetMaster(fd());
158 if (!drmIsMaster(fd())) {
159 ALOGE("DRM/KMS master access required");
160 return std::make_tuple(-EACCES, 0);
Roman Stratiienko3b24cd92021-01-13 10:32:04 +0200161 }
162
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300163 auto res = MakeDrmModeResUnique(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400164 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100165 ALOGE("Failed to get DrmDevice resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100166 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400167 }
168
Sean Paulf72cccd2018-08-27 13:59:08 -0400169 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
170 res->min_height);
171 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
172 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -0800173
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100174 // Assumes that the primary display will always be in the first
175 // drm_device opened.
176 bool found_primary = num_displays != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400177
178 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300179 auto c = MakeDrmModeCrtcUnique(fd(), res->crtcs[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400180 if (!c) {
181 ALOGE("Failed to get crtc %d", res->crtcs[i]);
182 ret = -ENODEV;
183 break;
184 }
185
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300186 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c.get(), i));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400187
Sean Paul877be972015-06-03 14:08:27 -0400188 ret = crtc->Init();
189 if (ret) {
190 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
Sean Paul877be972015-06-03 14:08:27 -0400191 break;
192 }
Zach Reiznerff30b522015-10-28 19:08:45 -0700193 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400194 }
195
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300196 std::vector<uint32_t> possible_clones;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400197 for (int i = 0; !ret && i < res->count_encoders; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300198 auto e = MakeDrmModeEncoderUnique(fd(), res->encoders[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400199 if (!e) {
200 ALOGE("Failed to get encoder %d", res->encoders[i]);
201 ret = -ENODEV;
202 break;
203 }
204
205 std::vector<DrmCrtc *> possible_crtcs;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200206 DrmCrtc *current_crtc = nullptr;
Zach Reiznerff30b522015-10-28 19:08:45 -0700207 for (auto &crtc : crtcs_) {
208 if ((1 << crtc->pipe()) & e->possible_crtcs)
209 possible_crtcs.push_back(crtc.get());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400210
Zach Reiznerff30b522015-10-28 19:08:45 -0700211 if (crtc->id() == e->crtc_id)
212 current_crtc = crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400213 }
214
Zach Reiznerff30b522015-10-28 19:08:45 -0700215 std::unique_ptr<DrmEncoder> enc(
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300216 new DrmEncoder(e.get(), current_crtc, possible_crtcs));
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000217 possible_clones.push_back(e->possible_clones);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400218
Zach Reiznerff30b522015-10-28 19:08:45 -0700219 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400220 }
221
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000222 for (unsigned int i = 0; i < encoders_.size(); i++) {
223 for (unsigned int j = 0; j < encoders_.size(); j++)
224 if (possible_clones[i] & (1 << j))
225 encoders_[i]->AddPossibleClone(encoders_[j].get());
226 }
227
Sean Paul6a55e9f2015-04-30 15:31:06 -0400228 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300229 auto c = MakeDrmModeConnectorUnique(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400230 if (!c) {
231 ALOGE("Failed to get connector %d", res->connectors[i]);
232 ret = -ENODEV;
233 break;
234 }
235
236 std::vector<DrmEncoder *> possible_encoders;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200237 DrmEncoder *current_encoder = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400238 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700239 for (auto &encoder : encoders_) {
240 if (encoder->id() == c->encoders[j])
241 possible_encoders.push_back(encoder.get());
242 if (encoder->id() == c->encoder_id)
243 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400244 }
245 }
246
Zach Reiznerff30b522015-10-28 19:08:45 -0700247 std::unique_ptr<DrmConnector> conn(
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300248 new DrmConnector(this, c.get(), current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400249
Sean Paul6a55e9f2015-04-30 15:31:06 -0400250 ret = conn->Init();
251 if (ret) {
252 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400253 break;
254 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400255
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000256 if (conn->writeback())
257 writeback_connectors_.emplace_back(std::move(conn));
258 else
259 connectors_.emplace_back(std::move(conn));
Robert Foss610d9892017-11-01 12:50:04 -0500260 }
261
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200262 // Primary display priority:
Jason Macnakf1af9572020-08-20 11:49:51 -0700263 // 1) vendor.hwc.drm.primary_display_order property
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200264 // 2) internal connectors
265 // 3) anything else
266 std::vector<DrmConnector *>
267 primary_candidates = make_primary_display_candidates(connectors_);
268 if (!primary_candidates.empty() && !found_primary) {
269 DrmConnector &conn = **std::begin(primary_candidates);
270 conn.set_display(num_displays);
271 displays_[num_displays] = num_displays;
272 ++num_displays;
273 found_primary = true;
274 } else {
275 ALOGE(
Jason Macnakf1af9572020-08-20 11:49:51 -0700276 "Failed to find primary display from "
277 "\"vendor.hwc.drm.primary_display_order\" property");
Sean Paul6a55e9f2015-04-30 15:31:06 -0400278 }
Robert Foss610d9892017-11-01 12:50:04 -0500279
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200280 // If no priority display were found then pick first available as primary and
281 // for the others assign consecutive display_numbers.
Robert Foss610d9892017-11-01 12:50:04 -0500282 for (auto &conn : connectors_) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100283 if (conn->external() || conn->internal()) {
284 if (!found_primary) {
285 conn->set_display(num_displays);
286 displays_[num_displays] = num_displays;
287 found_primary = true;
288 ++num_displays;
289 } else if (conn->display() < 0) {
290 conn->set_display(num_displays);
291 displays_[num_displays] = num_displays;
292 ++num_displays;
293 }
Robert Foss610d9892017-11-01 12:50:04 -0500294 }
295 }
296
Sean Paul6a55e9f2015-04-30 15:31:06 -0400297 // Catch-all for the above loops
298 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100299 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400300
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300301 auto plane_res = MakeDrmModePlaneResUnique(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400302 if (!plane_res) {
303 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100304 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400305 }
306
307 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Roman Stratiienkob671fab2022-01-29 00:50:22 +0200308 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
309 auto plane = DrmPlane::CreateInstance(*this, plane_res->planes[i]);
310
311 if (plane) {
312 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400313 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400314 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400315
Zach Reiznerff30b522015-10-28 19:08:45 -0700316 for (auto &conn : connectors_) {
317 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400318 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700319 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100320 return std::make_tuple(ret, 0);
Sean Paul57355412015-09-19 09:14:34 -0400321 }
322 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100323 return std::make_tuple(ret, displays_.size());
324}
325
326bool DrmDevice::HandlesDisplay(int display) const {
327 return displays_.find(display) != displays_.end();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400328}
329
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100330DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200331 for (const auto &conn : connectors_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700332 if (conn->display() == display)
333 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400334 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200335 return nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400336}
337
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100338DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200339 for (const auto &crtc : crtcs_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700340 if (crtc->display() == display)
341 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400342 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200343 return nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400344}
345
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100346const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400347 return crtcs_;
348}
349
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100350uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400351 return ++mode_id_;
352}
353
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100354int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400355 /* First try to use the currently-bound crtc */
356 DrmCrtc *crtc = enc->crtc();
357 if (crtc && crtc->can_bind(display)) {
358 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000359 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400360 return 0;
361 }
362
363 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700364 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400365 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700366 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400367 continue;
368
Zach Reiznerff30b522015-10-28 19:08:45 -0700369 if (crtc->can_bind(display)) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700370 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000371 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400372 return 0;
373 }
374 }
375
376 /* We can't use the encoder, but nothing went wrong, try another one */
377 return -EAGAIN;
378}
379
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100380int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400381 int display = connector->display();
382 /* Try to use current setup first */
383 if (connector->encoder()) {
384 int ret = TryEncoderForDisplay(display, connector->encoder());
385 if (!ret) {
386 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200387 }
388
389 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400390 ALOGE("Could not set mode %d/%d", display, ret);
391 return ret;
392 }
393 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400394
Zach Reiznerff30b522015-10-28 19:08:45 -0700395 for (DrmEncoder *enc : connector->possible_encoders()) {
396 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400397 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700398 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400399 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200400 }
401
402 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400403 ALOGE("Could not set mode %d/%d", display, ret);
404 return ret;
405 }
406 }
407 ALOGE("Could not find a suitable encoder/crtc for display %d",
408 connector->display());
409 return -ENODEV;
410}
411
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300412auto DrmDevice::RegisterUserPropertyBlob(void *data, size_t length) const
413 -> DrmModeUserPropertyBlobUnique {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200414 struct drm_mode_create_blob create_blob {};
Sean Paul877be972015-06-03 14:08:27 -0400415 create_blob.length = length;
416 create_blob.data = (__u64)data;
417
Zach Reiznerff30b522015-10-28 19:08:45 -0700418 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400419 if (ret) {
420 ALOGE("Failed to create mode property blob %d", ret);
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200421 return {};
Sean Paul877be972015-06-03 14:08:27 -0400422 }
Sean Paul877be972015-06-03 14:08:27 -0400423
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300424 return DrmModeUserPropertyBlobUnique(
425 new uint32_t(create_blob.blob_id), [this](const uint32_t *it) {
426 struct drm_mode_destroy_blob destroy_blob {};
427 destroy_blob.blob_id = (__u32)*it;
428 int err = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
429 if (err != 0) {
430 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", *it,
431 err);
432 }
433 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
434 delete it;
435 });
Sean Paul877be972015-06-03 14:08:27 -0400436}
437
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100438int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200439 const char *prop_name, DrmProperty *property) const {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200440 drmModeObjectPropertiesPtr props = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400441
Zach Reiznerff30b522015-10-28 19:08:45 -0700442 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400443 if (!props) {
444 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
445 return -ENODEV;
446 }
447
448 bool found = false;
449 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700450 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400451 if (!strcmp(p->name, prop_name)) {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300452 property->Init(obj_id, p, props->prop_values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400453 found = true;
454 }
455 drmModeFreeProperty(p);
456 }
457
458 drmModeFreeObjectProperties(props);
459 return found ? 0 : -ENOENT;
460}
461
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100462int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
Roman Stratiienko0b063882021-09-30 10:15:05 +0300463 DrmProperty *property) const {
Sean Paul877be972015-06-03 14:08:27 -0400464 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
465}
466
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100467int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
468 const char *prop_name,
Roman Stratiienko0b063882021-09-30 10:15:05 +0300469 DrmProperty *property) const {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400470 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
471 property);
472}
Matvii Zorinef3c7972020-08-11 15:15:44 +0300473
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200474std::string DrmDevice::GetName() const {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200475 auto *ver = drmGetVersion(fd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300476 if (!ver) {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200477 ALOGW("Failed to get drm version for fd=%d", fd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300478 return "generic";
479 }
480
481 std::string name(ver->name);
482 drmFreeVersion(ver);
483 return name;
484}
Roman Stratiienko56f4adc2021-09-29 12:47:12 +0300485
486auto DrmDevice::IsKMSDev(const char *path) -> bool {
487 auto fd = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
488 if (!fd) {
489 return false;
490 }
491
492 auto res = MakeDrmModeResUnique(fd.Get());
493 if (!res) {
494 return false;
495 }
496
497 bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
498 res->count_encoders > 0;
499
500 return is_kms;
501}
502
Sean Paulf72cccd2018-08-27 13:59:08 -0400503} // namespace android