blob: 2007fdd753e639876a21480f899ec43736bdbb73 [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
Sean Paulf72cccd2018-08-27 13:59:08 -040019#include "drmdevice.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040020#include "drmconnector.h"
21#include "drmcrtc.h"
22#include "drmencoder.h"
Sean Paul047b9b22015-07-28 14:15:42 -040023#include "drmeventlistener.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040024#include "drmplane.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040025
26#include <errno.h>
27#include <fcntl.h>
28#include <stdint.h>
29#include <xf86drm.h>
30#include <xf86drmMode.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040031#include <cinttypes>
Sean Paul6a55e9f2015-04-30 15:31:06 -040032
Sean Paul6a55e9f2015-04-30 15:31:06 -040033#include <cutils/properties.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040034#include <log/log.h>
Sean Paul6a55e9f2015-04-30 15:31:06 -040035
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 Paulf72cccd2018-08-27 13:59:08 -040079 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
80 res->min_height);
81 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
82 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -080083
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 Gheorgheb6a675e2018-03-27 16:10:55 +0100280// TODO what happens when hotplugging
281DrmConnector *DrmDevice::AvailableWritebackConnector(int display) const {
282 DrmConnector *writeback_conn = GetWritebackConnectorForDisplay(display);
283 DrmConnector *display_conn = GetConnectorForDisplay(display);
284 // If we have a writeback already attached to the same CRTC just use that,
285 // if possible.
286 if (display_conn && writeback_conn &&
287 writeback_conn->encoder()->CanClone(display_conn->encoder()))
288 return writeback_conn;
289
290 // Use another CRTC if available and doesn't have any connector
291 for (auto &crtc : crtcs_) {
292 if (crtc->display() == display)
293 continue;
294 display_conn = GetConnectorForDisplay(crtc->display());
295 // If we have a display connected don't use it for writeback
296 if (display_conn && display_conn->state() == DRM_MODE_CONNECTED)
297 continue;
298 writeback_conn = GetWritebackConnectorForDisplay(crtc->display());
299 if (writeback_conn)
300 return writeback_conn;
301 }
302 return NULL;
303}
304
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100305DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700306 for (auto &crtc : crtcs_) {
307 if (crtc->display() == display)
308 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400309 }
310 return NULL;
311}
312
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100313DrmPlane *DrmDevice::GetPlane(uint32_t id) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700314 for (auto &plane : planes_) {
315 if (plane->id() == id)
316 return plane.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400317 }
318 return NULL;
319}
320
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100321const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400322 return crtcs_;
323}
324
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100325uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400326 return ++mode_id_;
327}
328
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100329int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400330 /* First try to use the currently-bound crtc */
331 DrmCrtc *crtc = enc->crtc();
332 if (crtc && crtc->can_bind(display)) {
333 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000334 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400335 return 0;
336 }
337
338 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700339 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400340 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700341 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400342 continue;
343
Zach Reiznerff30b522015-10-28 19:08:45 -0700344 if (crtc->can_bind(display)) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700345 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000346 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400347 return 0;
348 }
349 }
350
351 /* We can't use the encoder, but nothing went wrong, try another one */
352 return -EAGAIN;
353}
354
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100355int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400356 int display = connector->display();
357 /* Try to use current setup first */
358 if (connector->encoder()) {
359 int ret = TryEncoderForDisplay(display, connector->encoder());
360 if (!ret) {
361 return 0;
362 } else if (ret != -EAGAIN) {
363 ALOGE("Could not set mode %d/%d", display, ret);
364 return ret;
365 }
366 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400367
Zach Reiznerff30b522015-10-28 19:08:45 -0700368 for (DrmEncoder *enc : connector->possible_encoders()) {
369 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400370 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700371 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400372 return 0;
373 } else if (ret != -EAGAIN) {
374 ALOGE("Could not set mode %d/%d", display, ret);
375 return ret;
376 }
377 }
378 ALOGE("Could not find a suitable encoder/crtc for display %d",
379 connector->display());
380 return -ENODEV;
381}
382
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000383// Attach writeback connector to the CRTC linked to the display_conn
384int DrmDevice::AttachWriteback(DrmConnector *display_conn) {
385 DrmCrtc *display_crtc = display_conn->encoder()->crtc();
386 if (GetWritebackConnectorForDisplay(display_crtc->display()) != NULL) {
387 ALOGE("Display already has writeback attach to it");
388 return -EINVAL;
389 }
390 for (auto &writeback_conn : writeback_connectors_) {
391 if (writeback_conn->display() >= 0)
392 continue;
393 for (DrmEncoder *writeback_enc : writeback_conn->possible_encoders()) {
394 for (DrmCrtc *possible_crtc : writeback_enc->possible_crtcs()) {
395 if (possible_crtc != display_crtc)
396 continue;
397 // Use just encoders which had not been bound already
398 if (writeback_enc->can_bind(display_crtc->display())) {
399 writeback_enc->set_crtc(display_crtc);
400 writeback_conn->set_encoder(writeback_enc);
401 writeback_conn->set_display(display_crtc->display());
402 writeback_conn->UpdateModes();
403 return 0;
404 }
405 }
406 }
407 }
408 return -EINVAL;
409}
410
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100411int DrmDevice::CreatePropertyBlob(void *data, size_t length,
412 uint32_t *blob_id) {
Sean Paul877be972015-06-03 14:08:27 -0400413 struct drm_mode_create_blob create_blob;
414 memset(&create_blob, 0, sizeof(create_blob));
415 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);
421 return ret;
422 }
423 *blob_id = create_blob.blob_id;
424 return 0;
425}
426
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100427int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) {
Sean Paul57355412015-09-19 09:14:34 -0400428 if (!blob_id)
429 return 0;
430
Sean Paul877be972015-06-03 14:08:27 -0400431 struct drm_mode_destroy_blob destroy_blob;
432 memset(&destroy_blob, 0, sizeof(destroy_blob));
433 destroy_blob.blob_id = (__u32)blob_id;
Zach Reiznerff30b522015-10-28 19:08:45 -0700434 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
Sean Paul877be972015-06-03 14:08:27 -0400435 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400436 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
Sean Paul877be972015-06-03 14:08:27 -0400437 return ret;
438 }
439 return 0;
440}
441
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100442DrmEventListener *DrmDevice::event_listener() {
Sean Paul047b9b22015-07-28 14:15:42 -0400443 return &event_listener_;
444}
445
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100446int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
447 const char *prop_name, DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400448 drmModeObjectPropertiesPtr props;
449
Zach Reiznerff30b522015-10-28 19:08:45 -0700450 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400451 if (!props) {
452 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
453 return -ENODEV;
454 }
455
456 bool found = false;
457 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700458 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400459 if (!strcmp(p->name, prop_name)) {
460 property->Init(p, props->prop_values[i]);
461 found = true;
462 }
463 drmModeFreeProperty(p);
464 }
465
466 drmModeFreeObjectProperties(props);
467 return found ? 0 : -ENOENT;
468}
469
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100470int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
471 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400472 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
473}
474
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100475int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
476 DrmProperty *property) {
Sean Paul877be972015-06-03 14:08:27 -0400477 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
478}
479
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100480int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
481 const char *prop_name,
482 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400483 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
484 property);
485}
Sean Paulf72cccd2018-08-27 13:59:08 -0400486} // namespace android