blob: b2f350c391f948c26156cf3db4570bc307426c71 [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
19#include "drmconnector.h"
20#include "drmcrtc.h"
21#include "drmencoder.h"
Sean Paul047b9b22015-07-28 14:15:42 -040022#include "drmeventlistener.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040023#include "drmplane.h"
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010024#include "drmdevice.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040025
Sean Paulf741c672016-05-11 13:49:38 -040026#include <cinttypes>
Sean Paul6a55e9f2015-04-30 15:31:06 -040027#include <errno.h>
28#include <fcntl.h>
29#include <stdint.h>
30#include <xf86drm.h>
31#include <xf86drmMode.h>
32
John Stultz9057a6f2018-04-26 12:05:55 -070033#include <log/log.h>
Sean Paul6a55e9f2015-04-30 15:31:06 -040034#include <cutils/properties.h>
35
36namespace android {
37
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010038DrmDevice::DrmDevice() : event_listener_(this) {
Sean Paul047b9b22015-07-28 14:15:42 -040039}
40
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010041DrmDevice::~DrmDevice() {
Sean Paul047b9b22015-07-28 14:15:42 -040042 event_listener_.Exit();
Sean Paul6a55e9f2015-04-30 15:31:06 -040043}
44
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 */
Zach Reiznerff30b522015-10-28 19:08:45 -070047 fd_.Set(open(path, O_RDWR));
48 if (fd() < 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040049 ALOGE("Failed to open dri- %s", strerror(-errno));
Alexandru Gheorghec5463582018-03-27 15:52:02 +010050 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040051 }
52
Zach Reiznerff30b522015-10-28 19:08:45 -070053 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -040054 if (ret) {
55 ALOGE("Failed to set universal plane cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010056 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040057 }
58
Zach Reiznerff30b522015-10-28 19:08:45 -070059 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -040060 if (ret) {
61 ALOGE("Failed to set atomic cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010062 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040063 }
64
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000065#ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS
66 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
67 if (ret) {
68 ALOGI("Failed to set writeback cap %d", ret);
69 ret = 0;
70 }
71#endif
72
Zach Reiznerff30b522015-10-28 19:08:45 -070073 drmModeResPtr res = drmModeGetResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -040074 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010075 ALOGE("Failed to get DrmDevice resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010076 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040077 }
78
Sean Paul406dbfc2016-02-10 15:35:17 -080079 min_resolution_ =
80 std::pair<uint32_t, uint32_t>(res->min_width, res->min_height);
81 max_resolution_ =
82 std::pair<uint32_t, uint32_t>(res->max_width, res->max_height);
83
Alexandru Gheorghec5463582018-03-27 15:52:02 +010084 // Assumes that the primary display will always be in the first
85 // drm_device opened.
86 bool found_primary = num_displays != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040087
88 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -070089 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040090 if (!c) {
91 ALOGE("Failed to get crtc %d", res->crtcs[i]);
92 ret = -ENODEV;
93 break;
94 }
95
Zach Reiznerff30b522015-10-28 19:08:45 -070096 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
Sean Paul6a55e9f2015-04-30 15:31:06 -040097 drmModeFreeCrtc(c);
98
Sean Paul877be972015-06-03 14:08:27 -040099 ret = crtc->Init();
100 if (ret) {
101 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
Sean Paul877be972015-06-03 14:08:27 -0400102 break;
103 }
Zach Reiznerff30b522015-10-28 19:08:45 -0700104 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400105 }
106
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000107 std::vector<int> possible_clones;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400108 for (int i = 0; !ret && i < res->count_encoders; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700109 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400110 if (!e) {
111 ALOGE("Failed to get encoder %d", res->encoders[i]);
112 ret = -ENODEV;
113 break;
114 }
115
116 std::vector<DrmCrtc *> possible_crtcs;
117 DrmCrtc *current_crtc = NULL;
Zach Reiznerff30b522015-10-28 19:08:45 -0700118 for (auto &crtc : crtcs_) {
119 if ((1 << crtc->pipe()) & e->possible_crtcs)
120 possible_crtcs.push_back(crtc.get());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400121
Zach Reiznerff30b522015-10-28 19:08:45 -0700122 if (crtc->id() == e->crtc_id)
123 current_crtc = crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400124 }
125
Zach Reiznerff30b522015-10-28 19:08:45 -0700126 std::unique_ptr<DrmEncoder> enc(
127 new DrmEncoder(e, current_crtc, possible_crtcs));
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000128 possible_clones.push_back(e->possible_clones);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400129 drmModeFreeEncoder(e);
130
Zach Reiznerff30b522015-10-28 19:08:45 -0700131 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400132 }
133
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000134 for (unsigned int i = 0; i < encoders_.size(); i++) {
135 for (unsigned int j = 0; j < encoders_.size(); j++)
136 if (possible_clones[i] & (1 << j))
137 encoders_[i]->AddPossibleClone(encoders_[j].get());
138 }
139
Sean Paul6a55e9f2015-04-30 15:31:06 -0400140 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700141 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400142 if (!c) {
143 ALOGE("Failed to get connector %d", res->connectors[i]);
144 ret = -ENODEV;
145 break;
146 }
147
148 std::vector<DrmEncoder *> possible_encoders;
149 DrmEncoder *current_encoder = NULL;
150 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700151 for (auto &encoder : encoders_) {
152 if (encoder->id() == c->encoders[j])
153 possible_encoders.push_back(encoder.get());
154 if (encoder->id() == c->encoder_id)
155 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400156 }
157 }
158
Zach Reiznerff30b522015-10-28 19:08:45 -0700159 std::unique_ptr<DrmConnector> conn(
160 new DrmConnector(this, c, current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400161
162 drmModeFreeConnector(c);
163
Sean Paul6a55e9f2015-04-30 15:31:06 -0400164 ret = conn->Init();
165 if (ret) {
166 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400167 break;
168 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400169
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000170 if (conn->writeback())
171 writeback_connectors_.emplace_back(std::move(conn));
172 else
173 connectors_.emplace_back(std::move(conn));
Robert Foss610d9892017-11-01 12:50:04 -0500174 }
175
176 // First look for primary amongst internal connectors
177 for (auto &conn : connectors_) {
178 if (conn->internal() && !found_primary) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100179 conn->set_display(num_displays);
180 displays_[num_displays] = num_displays;
181 ++num_displays;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400182 found_primary = true;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100183 break;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400184 }
185 }
Robert Foss610d9892017-11-01 12:50:04 -0500186
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100187 // Then pick first available as primary and for the others assign
188 // consecutive display_numbers.
Robert Foss610d9892017-11-01 12:50:04 -0500189 for (auto &conn : connectors_) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100190 if (conn->external() || conn->internal()) {
191 if (!found_primary) {
192 conn->set_display(num_displays);
193 displays_[num_displays] = num_displays;
194 found_primary = true;
195 ++num_displays;
196 } else if (conn->display() < 0) {
197 conn->set_display(num_displays);
198 displays_[num_displays] = num_displays;
199 ++num_displays;
200 }
Robert Foss610d9892017-11-01 12:50:04 -0500201 }
202 }
203
Sean Paul6a55e9f2015-04-30 15:31:06 -0400204 if (res)
205 drmModeFreeResources(res);
206
207 // Catch-all for the above loops
208 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100209 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400210
Zach Reiznerff30b522015-10-28 19:08:45 -0700211 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400212 if (!plane_res) {
213 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100214 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400215 }
216
217 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700218 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400219 if (!p) {
220 ALOGE("Failed to get plane %d", plane_res->planes[i]);
221 ret = -ENODEV;
222 break;
223 }
224
Zach Reiznerff30b522015-10-28 19:08:45 -0700225 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400226
227 drmModeFreePlane(p);
228
Sean Paul6a55e9f2015-04-30 15:31:06 -0400229 ret = plane->Init();
230 if (ret) {
231 ALOGE("Init plane %d failed", plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400232 break;
233 }
234
Zach Reiznerff30b522015-10-28 19:08:45 -0700235 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400236 }
237 drmModeFreePlaneResources(plane_res);
238 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100239 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400240
Sean Paul047b9b22015-07-28 14:15:42 -0400241 ret = event_listener_.Init();
242 if (ret) {
243 ALOGE("Can't initialize event listener %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100244 return std::make_tuple(ret, 0);
Sean Paul047b9b22015-07-28 14:15:42 -0400245 }
246
Zach Reiznerff30b522015-10-28 19:08:45 -0700247 for (auto &conn : connectors_) {
248 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400249 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700250 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100251 return std::make_tuple(ret, 0);
Sean Paul57355412015-09-19 09:14:34 -0400252 }
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000253 if (!AttachWriteback(conn.get())) {
254 ALOGI("Display %d has writeback attach to it", conn->display());
255 }
Sean Paul57355412015-09-19 09:14:34 -0400256 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100257 return std::make_tuple(ret, displays_.size());
258}
259
260bool DrmDevice::HandlesDisplay(int display) const {
261 return displays_.find(display) != displays_.end();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400262}
263
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100264DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700265 for (auto &conn : connectors_) {
266 if (conn->display() == display)
267 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400268 }
269 return NULL;
270}
271
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000272DrmConnector *DrmDevice::GetWritebackConnectorForDisplay(int display) const {
273 for (auto &conn : writeback_connectors_) {
274 if (conn->display() == display)
275 return conn.get();
276 }
277 return NULL;
278}
279
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100280DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700281 for (auto &crtc : crtcs_) {
282 if (crtc->display() == display)
283 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400284 }
285 return NULL;
286}
287
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100288DrmPlane *DrmDevice::GetPlane(uint32_t id) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700289 for (auto &plane : planes_) {
290 if (plane->id() == id)
291 return plane.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400292 }
293 return NULL;
294}
295
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100296const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400297 return crtcs_;
298}
299
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100300uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400301 return ++mode_id_;
302}
303
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100304int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400305 /* First try to use the currently-bound crtc */
306 DrmCrtc *crtc = enc->crtc();
307 if (crtc && crtc->can_bind(display)) {
308 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000309 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400310 return 0;
311 }
312
313 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700314 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400315 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700316 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400317 continue;
318
Zach Reiznerff30b522015-10-28 19:08:45 -0700319 if (crtc->can_bind(display)) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700320 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000321 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400322 return 0;
323 }
324 }
325
326 /* We can't use the encoder, but nothing went wrong, try another one */
327 return -EAGAIN;
328}
329
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100330int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400331 int display = connector->display();
332 /* Try to use current setup first */
333 if (connector->encoder()) {
334 int ret = TryEncoderForDisplay(display, connector->encoder());
335 if (!ret) {
336 return 0;
337 } else if (ret != -EAGAIN) {
338 ALOGE("Could not set mode %d/%d", display, ret);
339 return ret;
340 }
341 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400342
Zach Reiznerff30b522015-10-28 19:08:45 -0700343 for (DrmEncoder *enc : connector->possible_encoders()) {
344 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400345 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700346 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400347 return 0;
348 } else if (ret != -EAGAIN) {
349 ALOGE("Could not set mode %d/%d", display, ret);
350 return ret;
351 }
352 }
353 ALOGE("Could not find a suitable encoder/crtc for display %d",
354 connector->display());
355 return -ENODEV;
356}
357
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000358// Attach writeback connector to the CRTC linked to the display_conn
359int DrmDevice::AttachWriteback(DrmConnector *display_conn) {
360 DrmCrtc *display_crtc = display_conn->encoder()->crtc();
361 if (GetWritebackConnectorForDisplay(display_crtc->display()) != NULL) {
362 ALOGE("Display already has writeback attach to it");
363 return -EINVAL;
364 }
365 for (auto &writeback_conn : writeback_connectors_) {
366 if (writeback_conn->display() >= 0)
367 continue;
368 for (DrmEncoder *writeback_enc : writeback_conn->possible_encoders()) {
369 for (DrmCrtc *possible_crtc : writeback_enc->possible_crtcs()) {
370 if (possible_crtc != display_crtc)
371 continue;
372 // Use just encoders which had not been bound already
373 if (writeback_enc->can_bind(display_crtc->display())) {
374 writeback_enc->set_crtc(display_crtc);
375 writeback_conn->set_encoder(writeback_enc);
376 writeback_conn->set_display(display_crtc->display());
377 writeback_conn->UpdateModes();
378 return 0;
379 }
380 }
381 }
382 }
383 return -EINVAL;
384}
385
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100386int DrmDevice::CreatePropertyBlob(void *data, size_t length,
387 uint32_t *blob_id) {
Sean Paul877be972015-06-03 14:08:27 -0400388 struct drm_mode_create_blob create_blob;
389 memset(&create_blob, 0, sizeof(create_blob));
390 create_blob.length = length;
391 create_blob.data = (__u64)data;
392
Zach Reiznerff30b522015-10-28 19:08:45 -0700393 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400394 if (ret) {
395 ALOGE("Failed to create mode property blob %d", ret);
396 return ret;
397 }
398 *blob_id = create_blob.blob_id;
399 return 0;
400}
401
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100402int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) {
Sean Paul57355412015-09-19 09:14:34 -0400403 if (!blob_id)
404 return 0;
405
Sean Paul877be972015-06-03 14:08:27 -0400406 struct drm_mode_destroy_blob destroy_blob;
407 memset(&destroy_blob, 0, sizeof(destroy_blob));
408 destroy_blob.blob_id = (__u32)blob_id;
Zach Reiznerff30b522015-10-28 19:08:45 -0700409 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
Sean Paul877be972015-06-03 14:08:27 -0400410 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400411 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
Sean Paul877be972015-06-03 14:08:27 -0400412 return ret;
413 }
414 return 0;
415}
416
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100417DrmEventListener *DrmDevice::event_listener() {
Sean Paul047b9b22015-07-28 14:15:42 -0400418 return &event_listener_;
419}
420
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100421int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
422 const char *prop_name, DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400423 drmModeObjectPropertiesPtr props;
424
Zach Reiznerff30b522015-10-28 19:08:45 -0700425 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400426 if (!props) {
427 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
428 return -ENODEV;
429 }
430
431 bool found = false;
432 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700433 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400434 if (!strcmp(p->name, prop_name)) {
435 property->Init(p, props->prop_values[i]);
436 found = true;
437 }
438 drmModeFreeProperty(p);
439 }
440
441 drmModeFreeObjectProperties(props);
442 return found ? 0 : -ENOENT;
443}
444
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100445int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
446 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400447 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
448}
449
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100450int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
451 DrmProperty *property) {
Sean Paul877be972015-06-03 14:08:27 -0400452 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
453}
454
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100455int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
456 const char *prop_name,
457 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400458 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
459 property);
460}
461}