blob: a833c67cb9b0cc883d5ef2e4a89539faf129e793 [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
Sean Paul6a55e9f2015-04-30 15:31:06 -040037namespace android {
38
Roman Stratiienko1e053b42021-10-25 22:54:20 +030039DrmDevice::DrmDevice() {
Roman Stratiienko8666dc92021-02-09 17:49:55 +020040 self.reset(this);
41 mDrmFbImporter = std::make_unique<DrmFbImporter>(self);
Sean Paul047b9b22015-07-28 14:15:42 -040042}
43
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +020044// NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
Alexandru Gheorghec5463582018-03-27 15:52:02 +010045std::tuple<int, int> DrmDevice::Init(const char *path, int num_displays) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040046 /* TODO: Use drmOpenControl here instead */
Roman Stratiienko0fade372021-02-20 13:59:55 +020047 fd_ = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
Zach Reiznerff30b522015-10-28 19:08:45 -070048 if (fd() < 0) {
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +020049 // NOLINTNEXTLINE(concurrency-mt-unsafe): Fixme
Peter Collingbournec77052e2020-02-19 11:25:08 -080050 ALOGE("Failed to open dri %s: %s", path, strerror(errno));
Alexandru Gheorghec5463582018-03-27 15:52:02 +010051 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040052 }
53
Zach Reiznerff30b522015-10-28 19:08:45 -070054 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -040055 if (ret) {
56 ALOGE("Failed to set universal plane cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010057 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040058 }
59
Zach Reiznerff30b522015-10-28 19:08:45 -070060 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -040061 if (ret) {
62 ALOGE("Failed to set atomic cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010063 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040064 }
65
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000066#ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS
67 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
68 if (ret) {
69 ALOGI("Failed to set writeback cap %d", ret);
70 ret = 0;
71 }
72#endif
73
Roman Stratiienko8666dc92021-02-09 17:49:55 +020074 uint64_t cap_value = 0;
75 if (drmGetCap(fd(), DRM_CAP_ADDFB2_MODIFIERS, &cap_value)) {
76 ALOGW("drmGetCap failed. Fallback to no modifier support.");
77 cap_value = 0;
78 }
79 HasAddFb2ModifiersSupport_ = cap_value != 0;
80
John Stultzfa002332021-02-02 01:34:45 +000081 drmSetMaster(fd());
82 if (!drmIsMaster(fd())) {
83 ALOGE("DRM/KMS master access required");
84 return std::make_tuple(-EACCES, 0);
Roman Stratiienko3b24cd92021-01-13 10:32:04 +020085 }
86
Roman Stratiienko3e8ce572021-09-29 12:46:28 +030087 auto res = MakeDrmModeResUnique(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -040088 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010089 ALOGE("Failed to get DrmDevice resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010090 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040091 }
92
Sean Paulf72cccd2018-08-27 13:59:08 -040093 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
94 res->min_height);
95 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
96 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -080097
Sean Paul6a55e9f2015-04-30 15:31:06 -040098 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +030099 auto c = MakeDrmModeCrtcUnique(fd(), res->crtcs[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400100 if (!c) {
101 ALOGE("Failed to get crtc %d", res->crtcs[i]);
102 ret = -ENODEV;
103 break;
104 }
105
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300106 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c.get(), i));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400107
Sean Paul877be972015-06-03 14:08:27 -0400108 ret = crtc->Init();
109 if (ret) {
110 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
Sean Paul877be972015-06-03 14:08:27 -0400111 break;
112 }
Zach Reiznerff30b522015-10-28 19:08:45 -0700113 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400114 }
115
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300116 std::vector<uint32_t> possible_clones;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400117 for (int i = 0; !ret && i < res->count_encoders; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300118 auto e = MakeDrmModeEncoderUnique(fd(), res->encoders[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400119 if (!e) {
120 ALOGE("Failed to get encoder %d", res->encoders[i]);
121 ret = -ENODEV;
122 break;
123 }
124
125 std::vector<DrmCrtc *> possible_crtcs;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200126 DrmCrtc *current_crtc = nullptr;
Zach Reiznerff30b522015-10-28 19:08:45 -0700127 for (auto &crtc : crtcs_) {
128 if ((1 << crtc->pipe()) & e->possible_crtcs)
129 possible_crtcs.push_back(crtc.get());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400130
Zach Reiznerff30b522015-10-28 19:08:45 -0700131 if (crtc->id() == e->crtc_id)
132 current_crtc = crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400133 }
134
Zach Reiznerff30b522015-10-28 19:08:45 -0700135 std::unique_ptr<DrmEncoder> enc(
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300136 new DrmEncoder(e.get(), current_crtc, possible_crtcs));
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000137 possible_clones.push_back(e->possible_clones);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400138
Zach Reiznerff30b522015-10-28 19:08:45 -0700139 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400140 }
141
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000142 for (unsigned int i = 0; i < encoders_.size(); i++) {
143 for (unsigned int j = 0; j < encoders_.size(); j++)
144 if (possible_clones[i] & (1 << j))
145 encoders_[i]->AddPossibleClone(encoders_[j].get());
146 }
147
Sean Paul6a55e9f2015-04-30 15:31:06 -0400148 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300149 auto c = MakeDrmModeConnectorUnique(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400150 if (!c) {
151 ALOGE("Failed to get connector %d", res->connectors[i]);
152 ret = -ENODEV;
153 break;
154 }
155
156 std::vector<DrmEncoder *> possible_encoders;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200157 DrmEncoder *current_encoder = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400158 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700159 for (auto &encoder : encoders_) {
160 if (encoder->id() == c->encoders[j])
161 possible_encoders.push_back(encoder.get());
162 if (encoder->id() == c->encoder_id)
163 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400164 }
165 }
166
Zach Reiznerff30b522015-10-28 19:08:45 -0700167 std::unique_ptr<DrmConnector> conn(
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300168 new DrmConnector(this, c.get(), current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400169
Sean Paul6a55e9f2015-04-30 15:31:06 -0400170 ret = conn->Init();
171 if (ret) {
172 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400173 break;
174 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400175
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000176 if (conn->writeback())
177 writeback_connectors_.emplace_back(std::move(conn));
178 else
179 connectors_.emplace_back(std::move(conn));
Robert Foss610d9892017-11-01 12:50:04 -0500180 }
181
Roman Stratiienkoedb97ed2022-01-29 19:53:14 +0200182 auto add_displays = [this, &num_displays](bool internal, bool connected) {
183 for (auto &conn : connectors_) {
184 bool is_connected = conn->state() == DRM_MODE_CONNECTED;
185 if ((internal ? conn->internal() : conn->external()) &&
186 (connected ? is_connected : !is_connected)) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100187 conn->set_display(num_displays);
188 displays_[num_displays] = num_displays;
189 ++num_displays;
190 }
Robert Foss610d9892017-11-01 12:50:04 -0500191 }
Roman Stratiienkoedb97ed2022-01-29 19:53:14 +0200192 };
193
194 /* Put internal first to ensure Primary display will be internal
195 * in case at least 1 internal is available
196 */
197 add_displays(/*internal = */ true, /*connected = */ true);
198 add_displays(/*internal = */ false, /*connected = */ true);
199 add_displays(/*internal = */ true, /*connected = */ false);
200 add_displays(/*internal = */ false, /*connected = */ false);
Robert Foss610d9892017-11-01 12:50:04 -0500201
Sean Paul6a55e9f2015-04-30 15:31:06 -0400202 // Catch-all for the above loops
203 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100204 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400205
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300206 auto plane_res = MakeDrmModePlaneResUnique(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400207 if (!plane_res) {
208 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100209 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400210 }
211
212 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Roman Stratiienkob671fab2022-01-29 00:50:22 +0200213 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
214 auto plane = DrmPlane::CreateInstance(*this, plane_res->planes[i]);
215
216 if (plane) {
217 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400218 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400219 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400220
Zach Reiznerff30b522015-10-28 19:08:45 -0700221 for (auto &conn : connectors_) {
222 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400223 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700224 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100225 return std::make_tuple(ret, 0);
Sean Paul57355412015-09-19 09:14:34 -0400226 }
227 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100228 return std::make_tuple(ret, displays_.size());
229}
230
231bool DrmDevice::HandlesDisplay(int display) const {
232 return displays_.find(display) != displays_.end();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400233}
234
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100235DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200236 for (const auto &conn : connectors_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700237 if (conn->display() == display)
238 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400239 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200240 return nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400241}
242
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100243DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200244 for (const auto &crtc : crtcs_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700245 if (crtc->display() == display)
246 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400247 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200248 return nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400249}
250
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100251const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400252 return crtcs_;
253}
254
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100255uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400256 return ++mode_id_;
257}
258
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100259int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400260 /* First try to use the currently-bound crtc */
261 DrmCrtc *crtc = enc->crtc();
262 if (crtc && crtc->can_bind(display)) {
263 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000264 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400265 return 0;
266 }
267
268 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700269 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400270 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700271 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400272 continue;
273
Zach Reiznerff30b522015-10-28 19:08:45 -0700274 if (crtc->can_bind(display)) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700275 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000276 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400277 return 0;
278 }
279 }
280
281 /* We can't use the encoder, but nothing went wrong, try another one */
282 return -EAGAIN;
283}
284
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100285int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400286 int display = connector->display();
287 /* Try to use current setup first */
288 if (connector->encoder()) {
289 int ret = TryEncoderForDisplay(display, connector->encoder());
290 if (!ret) {
291 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200292 }
293
294 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400295 ALOGE("Could not set mode %d/%d", display, ret);
296 return ret;
297 }
298 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400299
Zach Reiznerff30b522015-10-28 19:08:45 -0700300 for (DrmEncoder *enc : connector->possible_encoders()) {
301 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400302 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700303 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400304 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200305 }
306
307 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400308 ALOGE("Could not set mode %d/%d", display, ret);
309 return ret;
310 }
311 }
312 ALOGE("Could not find a suitable encoder/crtc for display %d",
313 connector->display());
314 return -ENODEV;
315}
316
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300317auto DrmDevice::RegisterUserPropertyBlob(void *data, size_t length) const
318 -> DrmModeUserPropertyBlobUnique {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200319 struct drm_mode_create_blob create_blob {};
Sean Paul877be972015-06-03 14:08:27 -0400320 create_blob.length = length;
321 create_blob.data = (__u64)data;
322
Zach Reiznerff30b522015-10-28 19:08:45 -0700323 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400324 if (ret) {
325 ALOGE("Failed to create mode property blob %d", ret);
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200326 return {};
Sean Paul877be972015-06-03 14:08:27 -0400327 }
Sean Paul877be972015-06-03 14:08:27 -0400328
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300329 return DrmModeUserPropertyBlobUnique(
330 new uint32_t(create_blob.blob_id), [this](const uint32_t *it) {
331 struct drm_mode_destroy_blob destroy_blob {};
332 destroy_blob.blob_id = (__u32)*it;
333 int err = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
334 if (err != 0) {
335 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", *it,
336 err);
337 }
338 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
339 delete it;
340 });
Sean Paul877be972015-06-03 14:08:27 -0400341}
342
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100343int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200344 const char *prop_name, DrmProperty *property) const {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200345 drmModeObjectPropertiesPtr props = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400346
Zach Reiznerff30b522015-10-28 19:08:45 -0700347 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400348 if (!props) {
349 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
350 return -ENODEV;
351 }
352
353 bool found = false;
354 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700355 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400356 if (!strcmp(p->name, prop_name)) {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300357 property->Init(obj_id, p, props->prop_values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400358 found = true;
359 }
360 drmModeFreeProperty(p);
361 }
362
363 drmModeFreeObjectProperties(props);
364 return found ? 0 : -ENOENT;
365}
366
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100367int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
Roman Stratiienko0b063882021-09-30 10:15:05 +0300368 DrmProperty *property) const {
Sean Paul877be972015-06-03 14:08:27 -0400369 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
370}
371
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100372int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
373 const char *prop_name,
Roman Stratiienko0b063882021-09-30 10:15:05 +0300374 DrmProperty *property) const {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400375 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
376 property);
377}
Matvii Zorinef3c7972020-08-11 15:15:44 +0300378
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200379std::string DrmDevice::GetName() const {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200380 auto *ver = drmGetVersion(fd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300381 if (!ver) {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200382 ALOGW("Failed to get drm version for fd=%d", fd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300383 return "generic";
384 }
385
386 std::string name(ver->name);
387 drmFreeVersion(ver);
388 return name;
389}
Roman Stratiienko56f4adc2021-09-29 12:47:12 +0300390
391auto DrmDevice::IsKMSDev(const char *path) -> bool {
392 auto fd = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
393 if (!fd) {
394 return false;
395 }
396
397 auto res = MakeDrmModeResUnique(fd.Get());
398 if (!res) {
399 return false;
400 }
401
402 bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
403 res->count_encoders > 0;
404
405 return is_kms;
406}
407
Sean Paulf72cccd2018-08-27 13:59:08 -0400408} // namespace android