blob: 28ecfda5ff86743d771a6fb98cd49a2edd77992c [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
Roman Stratiienko13cc3662020-08-29 21:35:39 +030021#include <cutils/properties.h>
Sean Paul6a55e9f2015-04-30 15:31:06 -040022#include <errno.h>
23#include <fcntl.h>
Roman Stratiienko13cc3662020-08-29 21:35:39 +030024#include <log/log.h>
Sean Paul6a55e9f2015-04-30 15:31:06 -040025#include <stdint.h>
26#include <xf86drm.h>
27#include <xf86drmMode.h>
28
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020029#include <algorithm>
30#include <array>
Roman Stratiienko13cc3662020-08-29 21:35:39 +030031#include <cinttypes>
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020032#include <string>
33
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020034static void trim_left(std::string &str) {
35 str.erase(std::begin(str),
36 std::find_if(std::begin(str), std::end(str),
37 [](int ch) { return !std::isspace(ch); }));
38}
39
40static void trim_right(std::string &str) {
41 str.erase(std::find_if(std::rbegin(str), std::rend(str),
42 [](int ch) { return !std::isspace(ch); })
43 .base(),
44 std::end(str));
45}
46
47static void trim(std::string &str) {
48 trim_left(str);
49 trim_right(str);
50}
51
Sean Paul6a55e9f2015-04-30 15:31:06 -040052namespace android {
53
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020054static std::vector<std::string> read_primary_display_order_prop() {
55 std::array<char, PROPERTY_VALUE_MAX> display_order_buf;
Jason Macnakf1af9572020-08-20 11:49:51 -070056 property_get("vendor.hwc.drm.primary_display_order", display_order_buf.data(),
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020057 "...");
58
59 std::vector<std::string> display_order;
60 std::istringstream str(display_order_buf.data());
61 for (std::string conn_name = ""; std::getline(str, conn_name, ',');) {
62 trim(conn_name);
63 display_order.push_back(std::move(conn_name));
64 }
65 return display_order;
66}
67
68static std::vector<DrmConnector *> make_primary_display_candidates(
69 std::vector<std::unique_ptr<DrmConnector>> &connectors) {
70 std::vector<DrmConnector *> primary_candidates;
71 std::transform(std::begin(connectors), std::end(connectors),
72 std::back_inserter(primary_candidates),
73 [](std::unique_ptr<DrmConnector> &conn) {
74 return conn.get();
75 });
76 primary_candidates.erase(std::remove_if(std::begin(primary_candidates),
77 std::end(primary_candidates),
78 [](const DrmConnector *conn) {
79 return conn->state() !=
80 DRM_MODE_CONNECTED;
81 }),
82 std::end(primary_candidates));
83
84 std::vector<std::string> display_order = read_primary_display_order_prop();
85 bool use_other = display_order.back() == "...";
86
87 // putting connectors from primary_display_order first
88 auto curr_connector = std::begin(primary_candidates);
89 for (const std::string &display_name : display_order) {
90 auto it = std::find_if(std::begin(primary_candidates),
91 std::end(primary_candidates),
92 [&display_name](const DrmConnector *conn) {
93 return conn->name() == display_name;
94 });
95 if (it != std::end(primary_candidates)) {
96 std::iter_swap(it, curr_connector);
97 ++curr_connector;
98 }
99 }
100
101 if (use_other) {
102 // then putting internal connectors second, everything else afterwards
103 std::partition(curr_connector, std::end(primary_candidates),
104 [](const DrmConnector *conn) { return conn->internal(); });
105 } else {
106 primary_candidates.erase(curr_connector, std::end(primary_candidates));
107 }
108
109 return primary_candidates;
110}
111
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100112DrmDevice::DrmDevice() : event_listener_(this) {
Sean Paul047b9b22015-07-28 14:15:42 -0400113}
114
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100115DrmDevice::~DrmDevice() {
Sean Paul047b9b22015-07-28 14:15:42 -0400116 event_listener_.Exit();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400117}
118
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100119std::tuple<int, int> DrmDevice::Init(const char *path, int num_displays) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400120 /* TODO: Use drmOpenControl here instead */
Zach Reiznerff30b522015-10-28 19:08:45 -0700121 fd_.Set(open(path, O_RDWR));
122 if (fd() < 0) {
Peter Collingbournec77052e2020-02-19 11:25:08 -0800123 ALOGE("Failed to open dri %s: %s", path, strerror(errno));
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100124 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400125 }
126
Zach Reiznerff30b522015-10-28 19:08:45 -0700127 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400128 if (ret) {
129 ALOGE("Failed to set universal plane cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100130 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400131 }
132
Zach Reiznerff30b522015-10-28 19:08:45 -0700133 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400134 if (ret) {
135 ALOGE("Failed to set atomic cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100136 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400137 }
138
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000139#ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS
140 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
141 if (ret) {
142 ALOGI("Failed to set writeback cap %d", ret);
143 ret = 0;
144 }
145#endif
146
Zach Reiznerff30b522015-10-28 19:08:45 -0700147 drmModeResPtr res = drmModeGetResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400148 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100149 ALOGE("Failed to get DrmDevice resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100150 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400151 }
152
Sean Paulf72cccd2018-08-27 13:59:08 -0400153 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
154 res->min_height);
155 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
156 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -0800157
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100158 // Assumes that the primary display will always be in the first
159 // drm_device opened.
160 bool found_primary = num_displays != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400161
162 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700163 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400164 if (!c) {
165 ALOGE("Failed to get crtc %d", res->crtcs[i]);
166 ret = -ENODEV;
167 break;
168 }
169
Zach Reiznerff30b522015-10-28 19:08:45 -0700170 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400171 drmModeFreeCrtc(c);
172
Sean Paul877be972015-06-03 14:08:27 -0400173 ret = crtc->Init();
174 if (ret) {
175 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
Sean Paul877be972015-06-03 14:08:27 -0400176 break;
177 }
Zach Reiznerff30b522015-10-28 19:08:45 -0700178 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400179 }
180
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000181 std::vector<int> possible_clones;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400182 for (int i = 0; !ret && i < res->count_encoders; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700183 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400184 if (!e) {
185 ALOGE("Failed to get encoder %d", res->encoders[i]);
186 ret = -ENODEV;
187 break;
188 }
189
190 std::vector<DrmCrtc *> possible_crtcs;
191 DrmCrtc *current_crtc = NULL;
Zach Reiznerff30b522015-10-28 19:08:45 -0700192 for (auto &crtc : crtcs_) {
193 if ((1 << crtc->pipe()) & e->possible_crtcs)
194 possible_crtcs.push_back(crtc.get());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400195
Zach Reiznerff30b522015-10-28 19:08:45 -0700196 if (crtc->id() == e->crtc_id)
197 current_crtc = crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400198 }
199
Zach Reiznerff30b522015-10-28 19:08:45 -0700200 std::unique_ptr<DrmEncoder> enc(
201 new DrmEncoder(e, current_crtc, possible_crtcs));
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000202 possible_clones.push_back(e->possible_clones);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400203 drmModeFreeEncoder(e);
204
Zach Reiznerff30b522015-10-28 19:08:45 -0700205 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400206 }
207
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000208 for (unsigned int i = 0; i < encoders_.size(); i++) {
209 for (unsigned int j = 0; j < encoders_.size(); j++)
210 if (possible_clones[i] & (1 << j))
211 encoders_[i]->AddPossibleClone(encoders_[j].get());
212 }
213
Sean Paul6a55e9f2015-04-30 15:31:06 -0400214 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700215 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400216 if (!c) {
217 ALOGE("Failed to get connector %d", res->connectors[i]);
218 ret = -ENODEV;
219 break;
220 }
221
222 std::vector<DrmEncoder *> possible_encoders;
223 DrmEncoder *current_encoder = NULL;
224 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700225 for (auto &encoder : encoders_) {
226 if (encoder->id() == c->encoders[j])
227 possible_encoders.push_back(encoder.get());
228 if (encoder->id() == c->encoder_id)
229 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400230 }
231 }
232
Zach Reiznerff30b522015-10-28 19:08:45 -0700233 std::unique_ptr<DrmConnector> conn(
234 new DrmConnector(this, c, current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400235
236 drmModeFreeConnector(c);
237
Sean Paul6a55e9f2015-04-30 15:31:06 -0400238 ret = conn->Init();
239 if (ret) {
240 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400241 break;
242 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400243
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000244 if (conn->writeback())
245 writeback_connectors_.emplace_back(std::move(conn));
246 else
247 connectors_.emplace_back(std::move(conn));
Robert Foss610d9892017-11-01 12:50:04 -0500248 }
249
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200250 // Primary display priority:
Jason Macnakf1af9572020-08-20 11:49:51 -0700251 // 1) vendor.hwc.drm.primary_display_order property
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200252 // 2) internal connectors
253 // 3) anything else
254 std::vector<DrmConnector *>
255 primary_candidates = make_primary_display_candidates(connectors_);
256 if (!primary_candidates.empty() && !found_primary) {
257 DrmConnector &conn = **std::begin(primary_candidates);
258 conn.set_display(num_displays);
259 displays_[num_displays] = num_displays;
260 ++num_displays;
261 found_primary = true;
262 } else {
263 ALOGE(
Jason Macnakf1af9572020-08-20 11:49:51 -0700264 "Failed to find primary display from "
265 "\"vendor.hwc.drm.primary_display_order\" property");
Sean Paul6a55e9f2015-04-30 15:31:06 -0400266 }
Robert Foss610d9892017-11-01 12:50:04 -0500267
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200268 // If no priority display were found then pick first available as primary and
269 // for the others assign consecutive display_numbers.
Robert Foss610d9892017-11-01 12:50:04 -0500270 for (auto &conn : connectors_) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100271 if (conn->external() || conn->internal()) {
272 if (!found_primary) {
273 conn->set_display(num_displays);
274 displays_[num_displays] = num_displays;
275 found_primary = true;
276 ++num_displays;
277 } else if (conn->display() < 0) {
278 conn->set_display(num_displays);
279 displays_[num_displays] = num_displays;
280 ++num_displays;
281 }
Robert Foss610d9892017-11-01 12:50:04 -0500282 }
283 }
284
Sean Paul6a55e9f2015-04-30 15:31:06 -0400285 if (res)
286 drmModeFreeResources(res);
287
288 // Catch-all for the above loops
289 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100290 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400291
Zach Reiznerff30b522015-10-28 19:08:45 -0700292 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400293 if (!plane_res) {
294 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100295 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400296 }
297
298 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700299 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400300 if (!p) {
301 ALOGE("Failed to get plane %d", plane_res->planes[i]);
302 ret = -ENODEV;
303 break;
304 }
305
Zach Reiznerff30b522015-10-28 19:08:45 -0700306 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400307
308 drmModeFreePlane(p);
309
Sean Paul6a55e9f2015-04-30 15:31:06 -0400310 ret = plane->Init();
311 if (ret) {
312 ALOGE("Init plane %d failed", plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400313 break;
314 }
315
Zach Reiznerff30b522015-10-28 19:08:45 -0700316 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400317 }
318 drmModeFreePlaneResources(plane_res);
319 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100320 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400321
Sean Paul047b9b22015-07-28 14:15:42 -0400322 ret = event_listener_.Init();
323 if (ret) {
324 ALOGE("Can't initialize event listener %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100325 return std::make_tuple(ret, 0);
Sean Paul047b9b22015-07-28 14:15:42 -0400326 }
327
Zach Reiznerff30b522015-10-28 19:08:45 -0700328 for (auto &conn : connectors_) {
329 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400330 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700331 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100332 return std::make_tuple(ret, 0);
Sean Paul57355412015-09-19 09:14:34 -0400333 }
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000334 if (!AttachWriteback(conn.get())) {
335 ALOGI("Display %d has writeback attach to it", conn->display());
336 }
Sean Paul57355412015-09-19 09:14:34 -0400337 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100338 return std::make_tuple(ret, displays_.size());
339}
340
341bool DrmDevice::HandlesDisplay(int display) const {
342 return displays_.find(display) != displays_.end();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400343}
344
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100345DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700346 for (auto &conn : connectors_) {
347 if (conn->display() == display)
348 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400349 }
350 return NULL;
351}
352
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000353DrmConnector *DrmDevice::GetWritebackConnectorForDisplay(int display) const {
354 for (auto &conn : writeback_connectors_) {
355 if (conn->display() == display)
356 return conn.get();
357 }
358 return NULL;
359}
360
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100361// TODO what happens when hotplugging
362DrmConnector *DrmDevice::AvailableWritebackConnector(int display) const {
363 DrmConnector *writeback_conn = GetWritebackConnectorForDisplay(display);
364 DrmConnector *display_conn = GetConnectorForDisplay(display);
365 // If we have a writeback already attached to the same CRTC just use that,
366 // if possible.
367 if (display_conn && writeback_conn &&
368 writeback_conn->encoder()->CanClone(display_conn->encoder()))
369 return writeback_conn;
370
371 // Use another CRTC if available and doesn't have any connector
372 for (auto &crtc : crtcs_) {
373 if (crtc->display() == display)
374 continue;
375 display_conn = GetConnectorForDisplay(crtc->display());
376 // If we have a display connected don't use it for writeback
377 if (display_conn && display_conn->state() == DRM_MODE_CONNECTED)
378 continue;
379 writeback_conn = GetWritebackConnectorForDisplay(crtc->display());
380 if (writeback_conn)
381 return writeback_conn;
382 }
383 return NULL;
384}
385
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100386DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700387 for (auto &crtc : crtcs_) {
388 if (crtc->display() == display)
389 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400390 }
391 return NULL;
392}
393
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100394DrmPlane *DrmDevice::GetPlane(uint32_t id) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700395 for (auto &plane : planes_) {
396 if (plane->id() == id)
397 return plane.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400398 }
399 return NULL;
400}
401
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100402const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400403 return crtcs_;
404}
405
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100406uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400407 return ++mode_id_;
408}
409
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100410int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400411 /* First try to use the currently-bound crtc */
412 DrmCrtc *crtc = enc->crtc();
413 if (crtc && crtc->can_bind(display)) {
414 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000415 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400416 return 0;
417 }
418
419 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700420 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400421 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700422 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400423 continue;
424
Zach Reiznerff30b522015-10-28 19:08:45 -0700425 if (crtc->can_bind(display)) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700426 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000427 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400428 return 0;
429 }
430 }
431
432 /* We can't use the encoder, but nothing went wrong, try another one */
433 return -EAGAIN;
434}
435
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100436int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400437 int display = connector->display();
438 /* Try to use current setup first */
439 if (connector->encoder()) {
440 int ret = TryEncoderForDisplay(display, connector->encoder());
441 if (!ret) {
442 return 0;
443 } else if (ret != -EAGAIN) {
444 ALOGE("Could not set mode %d/%d", display, ret);
445 return ret;
446 }
447 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400448
Zach Reiznerff30b522015-10-28 19:08:45 -0700449 for (DrmEncoder *enc : connector->possible_encoders()) {
450 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400451 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700452 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400453 return 0;
454 } else if (ret != -EAGAIN) {
455 ALOGE("Could not set mode %d/%d", display, ret);
456 return ret;
457 }
458 }
459 ALOGE("Could not find a suitable encoder/crtc for display %d",
460 connector->display());
461 return -ENODEV;
462}
463
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000464// Attach writeback connector to the CRTC linked to the display_conn
465int DrmDevice::AttachWriteback(DrmConnector *display_conn) {
466 DrmCrtc *display_crtc = display_conn->encoder()->crtc();
467 if (GetWritebackConnectorForDisplay(display_crtc->display()) != NULL) {
468 ALOGE("Display already has writeback attach to it");
469 return -EINVAL;
470 }
471 for (auto &writeback_conn : writeback_connectors_) {
472 if (writeback_conn->display() >= 0)
473 continue;
474 for (DrmEncoder *writeback_enc : writeback_conn->possible_encoders()) {
475 for (DrmCrtc *possible_crtc : writeback_enc->possible_crtcs()) {
476 if (possible_crtc != display_crtc)
477 continue;
478 // Use just encoders which had not been bound already
479 if (writeback_enc->can_bind(display_crtc->display())) {
480 writeback_enc->set_crtc(display_crtc);
481 writeback_conn->set_encoder(writeback_enc);
482 writeback_conn->set_display(display_crtc->display());
483 writeback_conn->UpdateModes();
484 return 0;
485 }
486 }
487 }
488 }
489 return -EINVAL;
490}
491
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100492int DrmDevice::CreatePropertyBlob(void *data, size_t length,
493 uint32_t *blob_id) {
Sean Paul877be972015-06-03 14:08:27 -0400494 struct drm_mode_create_blob create_blob;
495 memset(&create_blob, 0, sizeof(create_blob));
496 create_blob.length = length;
497 create_blob.data = (__u64)data;
498
Zach Reiznerff30b522015-10-28 19:08:45 -0700499 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400500 if (ret) {
501 ALOGE("Failed to create mode property blob %d", ret);
502 return ret;
503 }
504 *blob_id = create_blob.blob_id;
505 return 0;
506}
507
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100508int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) {
Sean Paul57355412015-09-19 09:14:34 -0400509 if (!blob_id)
510 return 0;
511
Sean Paul877be972015-06-03 14:08:27 -0400512 struct drm_mode_destroy_blob destroy_blob;
513 memset(&destroy_blob, 0, sizeof(destroy_blob));
514 destroy_blob.blob_id = (__u32)blob_id;
Zach Reiznerff30b522015-10-28 19:08:45 -0700515 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
Sean Paul877be972015-06-03 14:08:27 -0400516 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400517 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
Sean Paul877be972015-06-03 14:08:27 -0400518 return ret;
519 }
520 return 0;
521}
522
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100523DrmEventListener *DrmDevice::event_listener() {
Sean Paul047b9b22015-07-28 14:15:42 -0400524 return &event_listener_;
525}
526
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100527int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
528 const char *prop_name, DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400529 drmModeObjectPropertiesPtr props;
530
Zach Reiznerff30b522015-10-28 19:08:45 -0700531 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400532 if (!props) {
533 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
534 return -ENODEV;
535 }
536
537 bool found = false;
538 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700539 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400540 if (!strcmp(p->name, prop_name)) {
541 property->Init(p, props->prop_values[i]);
542 found = true;
543 }
544 drmModeFreeProperty(p);
545 }
546
547 drmModeFreeObjectProperties(props);
548 return found ? 0 : -ENOENT;
549}
550
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100551int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
552 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400553 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
554}
555
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100556int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
557 DrmProperty *property) {
Sean Paul877be972015-06-03 14:08:27 -0400558 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
559}
560
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100561int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
562 const char *prop_name,
563 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400564 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
565 property);
566}
Matvii Zorinef3c7972020-08-11 15:15:44 +0300567
568const std::string DrmDevice::GetName() const {
569 auto ver = drmGetVersion(fd_.get());
570 if (!ver) {
571 ALOGW("Failed to get drm version for fd=%d", fd_.get());
572 return "generic";
573 }
574
575 std::string name(ver->name);
576 drmFreeVersion(ver);
577 return name;
578}
Sean Paulf72cccd2018-08-27 13:59:08 -0400579} // namespace android