blob: 5d46c3643e41572ace515380c52195c9423e7e0f [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 Stratiienkob2e9fe22020-10-03 10:52:36 +030032#include <sstream>
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020033#include <string>
34
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020035static void trim_left(std::string &str) {
36 str.erase(std::begin(str),
37 std::find_if(std::begin(str), std::end(str),
38 [](int ch) { return !std::isspace(ch); }));
39}
40
41static void trim_right(std::string &str) {
42 str.erase(std::find_if(std::rbegin(str), std::rend(str),
43 [](int ch) { return !std::isspace(ch); })
44 .base(),
45 std::end(str));
46}
47
48static void trim(std::string &str) {
49 trim_left(str);
50 trim_right(str);
51}
52
Sean Paul6a55e9f2015-04-30 15:31:06 -040053namespace android {
54
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020055static std::vector<std::string> read_primary_display_order_prop() {
56 std::array<char, PROPERTY_VALUE_MAX> display_order_buf;
Jason Macnakf1af9572020-08-20 11:49:51 -070057 property_get("vendor.hwc.drm.primary_display_order", display_order_buf.data(),
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020058 "...");
59
60 std::vector<std::string> display_order;
61 std::istringstream str(display_order_buf.data());
62 for (std::string conn_name = ""; std::getline(str, conn_name, ',');) {
63 trim(conn_name);
64 display_order.push_back(std::move(conn_name));
65 }
66 return display_order;
67}
68
69static std::vector<DrmConnector *> make_primary_display_candidates(
70 std::vector<std::unique_ptr<DrmConnector>> &connectors) {
71 std::vector<DrmConnector *> primary_candidates;
72 std::transform(std::begin(connectors), std::end(connectors),
73 std::back_inserter(primary_candidates),
74 [](std::unique_ptr<DrmConnector> &conn) {
75 return conn.get();
76 });
77 primary_candidates.erase(std::remove_if(std::begin(primary_candidates),
78 std::end(primary_candidates),
79 [](const DrmConnector *conn) {
80 return conn->state() !=
81 DRM_MODE_CONNECTED;
82 }),
83 std::end(primary_candidates));
84
85 std::vector<std::string> display_order = read_primary_display_order_prop();
86 bool use_other = display_order.back() == "...";
87
88 // putting connectors from primary_display_order first
89 auto curr_connector = std::begin(primary_candidates);
90 for (const std::string &display_name : display_order) {
91 auto it = std::find_if(std::begin(primary_candidates),
92 std::end(primary_candidates),
93 [&display_name](const DrmConnector *conn) {
94 return conn->name() == display_name;
95 });
96 if (it != std::end(primary_candidates)) {
97 std::iter_swap(it, curr_connector);
98 ++curr_connector;
99 }
100 }
101
102 if (use_other) {
103 // then putting internal connectors second, everything else afterwards
104 std::partition(curr_connector, std::end(primary_candidates),
105 [](const DrmConnector *conn) { return conn->internal(); });
106 } else {
107 primary_candidates.erase(curr_connector, std::end(primary_candidates));
108 }
109
110 return primary_candidates;
111}
112
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100113DrmDevice::DrmDevice() : event_listener_(this) {
Sean Paul047b9b22015-07-28 14:15:42 -0400114}
115
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100116DrmDevice::~DrmDevice() {
Sean Paul047b9b22015-07-28 14:15:42 -0400117 event_listener_.Exit();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400118}
119
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100120std::tuple<int, int> DrmDevice::Init(const char *path, int num_displays) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400121 /* TODO: Use drmOpenControl here instead */
Zach Reiznerff30b522015-10-28 19:08:45 -0700122 fd_.Set(open(path, O_RDWR));
123 if (fd() < 0) {
Peter Collingbournec77052e2020-02-19 11:25:08 -0800124 ALOGE("Failed to open dri %s: %s", path, strerror(errno));
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100125 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400126 }
127
Zach Reiznerff30b522015-10-28 19:08:45 -0700128 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400129 if (ret) {
130 ALOGE("Failed to set universal plane cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100131 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400132 }
133
Zach Reiznerff30b522015-10-28 19:08:45 -0700134 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400135 if (ret) {
136 ALOGE("Failed to set atomic cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100137 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400138 }
139
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000140#ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS
141 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
142 if (ret) {
143 ALOGI("Failed to set writeback cap %d", ret);
144 ret = 0;
145 }
146#endif
147
Roman Stratiienko3b24cd92021-01-13 10:32:04 +0200148 ret = drmSetMaster(fd());
149 if (ret) {
150 ALOGE("drmSetMaster() failed with errno: %d", errno);
151 return std::make_tuple(ret, 0);
152 }
153
Zach Reiznerff30b522015-10-28 19:08:45 -0700154 drmModeResPtr res = drmModeGetResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400155 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100156 ALOGE("Failed to get DrmDevice resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100157 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400158 }
159
Sean Paulf72cccd2018-08-27 13:59:08 -0400160 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
161 res->min_height);
162 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
163 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -0800164
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100165 // Assumes that the primary display will always be in the first
166 // drm_device opened.
167 bool found_primary = num_displays != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400168
169 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700170 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400171 if (!c) {
172 ALOGE("Failed to get crtc %d", res->crtcs[i]);
173 ret = -ENODEV;
174 break;
175 }
176
Zach Reiznerff30b522015-10-28 19:08:45 -0700177 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400178 drmModeFreeCrtc(c);
179
Sean Paul877be972015-06-03 14:08:27 -0400180 ret = crtc->Init();
181 if (ret) {
182 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
Sean Paul877be972015-06-03 14:08:27 -0400183 break;
184 }
Zach Reiznerff30b522015-10-28 19:08:45 -0700185 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400186 }
187
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000188 std::vector<int> possible_clones;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400189 for (int i = 0; !ret && i < res->count_encoders; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700190 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400191 if (!e) {
192 ALOGE("Failed to get encoder %d", res->encoders[i]);
193 ret = -ENODEV;
194 break;
195 }
196
197 std::vector<DrmCrtc *> possible_crtcs;
198 DrmCrtc *current_crtc = NULL;
Zach Reiznerff30b522015-10-28 19:08:45 -0700199 for (auto &crtc : crtcs_) {
200 if ((1 << crtc->pipe()) & e->possible_crtcs)
201 possible_crtcs.push_back(crtc.get());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400202
Zach Reiznerff30b522015-10-28 19:08:45 -0700203 if (crtc->id() == e->crtc_id)
204 current_crtc = crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400205 }
206
Zach Reiznerff30b522015-10-28 19:08:45 -0700207 std::unique_ptr<DrmEncoder> enc(
208 new DrmEncoder(e, current_crtc, possible_crtcs));
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000209 possible_clones.push_back(e->possible_clones);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400210 drmModeFreeEncoder(e);
211
Zach Reiznerff30b522015-10-28 19:08:45 -0700212 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400213 }
214
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000215 for (unsigned int i = 0; i < encoders_.size(); i++) {
216 for (unsigned int j = 0; j < encoders_.size(); j++)
217 if (possible_clones[i] & (1 << j))
218 encoders_[i]->AddPossibleClone(encoders_[j].get());
219 }
220
Sean Paul6a55e9f2015-04-30 15:31:06 -0400221 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700222 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400223 if (!c) {
224 ALOGE("Failed to get connector %d", res->connectors[i]);
225 ret = -ENODEV;
226 break;
227 }
228
229 std::vector<DrmEncoder *> possible_encoders;
230 DrmEncoder *current_encoder = NULL;
231 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700232 for (auto &encoder : encoders_) {
233 if (encoder->id() == c->encoders[j])
234 possible_encoders.push_back(encoder.get());
235 if (encoder->id() == c->encoder_id)
236 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400237 }
238 }
239
Zach Reiznerff30b522015-10-28 19:08:45 -0700240 std::unique_ptr<DrmConnector> conn(
241 new DrmConnector(this, c, current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400242
243 drmModeFreeConnector(c);
244
Sean Paul6a55e9f2015-04-30 15:31:06 -0400245 ret = conn->Init();
246 if (ret) {
247 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400248 break;
249 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400250
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000251 if (conn->writeback())
252 writeback_connectors_.emplace_back(std::move(conn));
253 else
254 connectors_.emplace_back(std::move(conn));
Robert Foss610d9892017-11-01 12:50:04 -0500255 }
256
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200257 // Primary display priority:
Jason Macnakf1af9572020-08-20 11:49:51 -0700258 // 1) vendor.hwc.drm.primary_display_order property
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200259 // 2) internal connectors
260 // 3) anything else
261 std::vector<DrmConnector *>
262 primary_candidates = make_primary_display_candidates(connectors_);
263 if (!primary_candidates.empty() && !found_primary) {
264 DrmConnector &conn = **std::begin(primary_candidates);
265 conn.set_display(num_displays);
266 displays_[num_displays] = num_displays;
267 ++num_displays;
268 found_primary = true;
269 } else {
270 ALOGE(
Jason Macnakf1af9572020-08-20 11:49:51 -0700271 "Failed to find primary display from "
272 "\"vendor.hwc.drm.primary_display_order\" property");
Sean Paul6a55e9f2015-04-30 15:31:06 -0400273 }
Robert Foss610d9892017-11-01 12:50:04 -0500274
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200275 // If no priority display were found then pick first available as primary and
276 // for the others assign consecutive display_numbers.
Robert Foss610d9892017-11-01 12:50:04 -0500277 for (auto &conn : connectors_) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100278 if (conn->external() || conn->internal()) {
279 if (!found_primary) {
280 conn->set_display(num_displays);
281 displays_[num_displays] = num_displays;
282 found_primary = true;
283 ++num_displays;
284 } else if (conn->display() < 0) {
285 conn->set_display(num_displays);
286 displays_[num_displays] = num_displays;
287 ++num_displays;
288 }
Robert Foss610d9892017-11-01 12:50:04 -0500289 }
290 }
291
Sean Paul6a55e9f2015-04-30 15:31:06 -0400292 if (res)
293 drmModeFreeResources(res);
294
295 // Catch-all for the above loops
296 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100297 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400298
Zach Reiznerff30b522015-10-28 19:08:45 -0700299 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400300 if (!plane_res) {
301 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100302 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400303 }
304
305 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700306 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400307 if (!p) {
308 ALOGE("Failed to get plane %d", plane_res->planes[i]);
309 ret = -ENODEV;
310 break;
311 }
312
Zach Reiznerff30b522015-10-28 19:08:45 -0700313 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400314
315 drmModeFreePlane(p);
316
Sean Paul6a55e9f2015-04-30 15:31:06 -0400317 ret = plane->Init();
318 if (ret) {
319 ALOGE("Init plane %d failed", plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400320 break;
321 }
322
Zach Reiznerff30b522015-10-28 19:08:45 -0700323 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400324 }
325 drmModeFreePlaneResources(plane_res);
326 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100327 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400328
Sean Paul047b9b22015-07-28 14:15:42 -0400329 ret = event_listener_.Init();
330 if (ret) {
331 ALOGE("Can't initialize event listener %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100332 return std::make_tuple(ret, 0);
Sean Paul047b9b22015-07-28 14:15:42 -0400333 }
334
Zach Reiznerff30b522015-10-28 19:08:45 -0700335 for (auto &conn : connectors_) {
336 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400337 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700338 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100339 return std::make_tuple(ret, 0);
Sean Paul57355412015-09-19 09:14:34 -0400340 }
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000341 if (!AttachWriteback(conn.get())) {
342 ALOGI("Display %d has writeback attach to it", conn->display());
343 }
Sean Paul57355412015-09-19 09:14:34 -0400344 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100345 return std::make_tuple(ret, displays_.size());
346}
347
348bool DrmDevice::HandlesDisplay(int display) const {
349 return displays_.find(display) != displays_.end();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400350}
351
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100352DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700353 for (auto &conn : connectors_) {
354 if (conn->display() == display)
355 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400356 }
357 return NULL;
358}
359
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000360DrmConnector *DrmDevice::GetWritebackConnectorForDisplay(int display) const {
361 for (auto &conn : writeback_connectors_) {
362 if (conn->display() == display)
363 return conn.get();
364 }
365 return NULL;
366}
367
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100368// TODO what happens when hotplugging
369DrmConnector *DrmDevice::AvailableWritebackConnector(int display) const {
370 DrmConnector *writeback_conn = GetWritebackConnectorForDisplay(display);
371 DrmConnector *display_conn = GetConnectorForDisplay(display);
372 // If we have a writeback already attached to the same CRTC just use that,
373 // if possible.
374 if (display_conn && writeback_conn &&
375 writeback_conn->encoder()->CanClone(display_conn->encoder()))
376 return writeback_conn;
377
378 // Use another CRTC if available and doesn't have any connector
379 for (auto &crtc : crtcs_) {
380 if (crtc->display() == display)
381 continue;
382 display_conn = GetConnectorForDisplay(crtc->display());
383 // If we have a display connected don't use it for writeback
384 if (display_conn && display_conn->state() == DRM_MODE_CONNECTED)
385 continue;
386 writeback_conn = GetWritebackConnectorForDisplay(crtc->display());
387 if (writeback_conn)
388 return writeback_conn;
389 }
390 return NULL;
391}
392
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100393DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700394 for (auto &crtc : crtcs_) {
395 if (crtc->display() == display)
396 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400397 }
398 return NULL;
399}
400
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100401DrmPlane *DrmDevice::GetPlane(uint32_t id) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700402 for (auto &plane : planes_) {
403 if (plane->id() == id)
404 return plane.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400405 }
406 return NULL;
407}
408
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100409const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400410 return crtcs_;
411}
412
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100413uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400414 return ++mode_id_;
415}
416
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100417int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400418 /* First try to use the currently-bound crtc */
419 DrmCrtc *crtc = enc->crtc();
420 if (crtc && crtc->can_bind(display)) {
421 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000422 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400423 return 0;
424 }
425
426 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700427 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400428 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700429 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400430 continue;
431
Zach Reiznerff30b522015-10-28 19:08:45 -0700432 if (crtc->can_bind(display)) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700433 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000434 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400435 return 0;
436 }
437 }
438
439 /* We can't use the encoder, but nothing went wrong, try another one */
440 return -EAGAIN;
441}
442
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100443int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400444 int display = connector->display();
445 /* Try to use current setup first */
446 if (connector->encoder()) {
447 int ret = TryEncoderForDisplay(display, connector->encoder());
448 if (!ret) {
449 return 0;
450 } else if (ret != -EAGAIN) {
451 ALOGE("Could not set mode %d/%d", display, ret);
452 return ret;
453 }
454 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400455
Zach Reiznerff30b522015-10-28 19:08:45 -0700456 for (DrmEncoder *enc : connector->possible_encoders()) {
457 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400458 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700459 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400460 return 0;
461 } else if (ret != -EAGAIN) {
462 ALOGE("Could not set mode %d/%d", display, ret);
463 return ret;
464 }
465 }
466 ALOGE("Could not find a suitable encoder/crtc for display %d",
467 connector->display());
468 return -ENODEV;
469}
470
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000471// Attach writeback connector to the CRTC linked to the display_conn
472int DrmDevice::AttachWriteback(DrmConnector *display_conn) {
473 DrmCrtc *display_crtc = display_conn->encoder()->crtc();
474 if (GetWritebackConnectorForDisplay(display_crtc->display()) != NULL) {
475 ALOGE("Display already has writeback attach to it");
476 return -EINVAL;
477 }
478 for (auto &writeback_conn : writeback_connectors_) {
479 if (writeback_conn->display() >= 0)
480 continue;
481 for (DrmEncoder *writeback_enc : writeback_conn->possible_encoders()) {
482 for (DrmCrtc *possible_crtc : writeback_enc->possible_crtcs()) {
483 if (possible_crtc != display_crtc)
484 continue;
485 // Use just encoders which had not been bound already
486 if (writeback_enc->can_bind(display_crtc->display())) {
487 writeback_enc->set_crtc(display_crtc);
488 writeback_conn->set_encoder(writeback_enc);
489 writeback_conn->set_display(display_crtc->display());
490 writeback_conn->UpdateModes();
491 return 0;
492 }
493 }
494 }
495 }
496 return -EINVAL;
497}
498
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100499int DrmDevice::CreatePropertyBlob(void *data, size_t length,
500 uint32_t *blob_id) {
Sean Paul877be972015-06-03 14:08:27 -0400501 struct drm_mode_create_blob create_blob;
502 memset(&create_blob, 0, sizeof(create_blob));
503 create_blob.length = length;
504 create_blob.data = (__u64)data;
505
Zach Reiznerff30b522015-10-28 19:08:45 -0700506 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400507 if (ret) {
508 ALOGE("Failed to create mode property blob %d", ret);
509 return ret;
510 }
511 *blob_id = create_blob.blob_id;
512 return 0;
513}
514
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100515int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) {
Sean Paul57355412015-09-19 09:14:34 -0400516 if (!blob_id)
517 return 0;
518
Sean Paul877be972015-06-03 14:08:27 -0400519 struct drm_mode_destroy_blob destroy_blob;
520 memset(&destroy_blob, 0, sizeof(destroy_blob));
521 destroy_blob.blob_id = (__u32)blob_id;
Zach Reiznerff30b522015-10-28 19:08:45 -0700522 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
Sean Paul877be972015-06-03 14:08:27 -0400523 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400524 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
Sean Paul877be972015-06-03 14:08:27 -0400525 return ret;
526 }
527 return 0;
528}
529
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100530DrmEventListener *DrmDevice::event_listener() {
Sean Paul047b9b22015-07-28 14:15:42 -0400531 return &event_listener_;
532}
533
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100534int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
535 const char *prop_name, DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400536 drmModeObjectPropertiesPtr props;
537
Zach Reiznerff30b522015-10-28 19:08:45 -0700538 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400539 if (!props) {
540 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
541 return -ENODEV;
542 }
543
544 bool found = false;
545 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700546 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400547 if (!strcmp(p->name, prop_name)) {
548 property->Init(p, props->prop_values[i]);
549 found = true;
550 }
551 drmModeFreeProperty(p);
552 }
553
554 drmModeFreeObjectProperties(props);
555 return found ? 0 : -ENOENT;
556}
557
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100558int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
559 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400560 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
561}
562
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100563int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
564 DrmProperty *property) {
Sean Paul877be972015-06-03 14:08:27 -0400565 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
566}
567
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100568int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
569 const char *prop_name,
570 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400571 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
572 property);
573}
Matvii Zorinef3c7972020-08-11 15:15:44 +0300574
575const std::string DrmDevice::GetName() const {
576 auto ver = drmGetVersion(fd_.get());
577 if (!ver) {
578 ALOGW("Failed to get drm version for fd=%d", fd_.get());
579 return "generic";
580 }
581
582 std::string name(ver->name);
583 drmFreeVersion(ver);
584 return name;
585}
Sean Paulf72cccd2018-08-27 13:59:08 -0400586} // namespace android