blob: bf1a5e2cbc86c0723e0f105774dc54acb72ff095 [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
Zach Reiznerff30b522015-10-28 19:08:45 -0700148 drmModeResPtr res = drmModeGetResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400149 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100150 ALOGE("Failed to get DrmDevice resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100151 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400152 }
153
Sean Paulf72cccd2018-08-27 13:59:08 -0400154 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
155 res->min_height);
156 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
157 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -0800158
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100159 // Assumes that the primary display will always be in the first
160 // drm_device opened.
161 bool found_primary = num_displays != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400162
163 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700164 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400165 if (!c) {
166 ALOGE("Failed to get crtc %d", res->crtcs[i]);
167 ret = -ENODEV;
168 break;
169 }
170
Zach Reiznerff30b522015-10-28 19:08:45 -0700171 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400172 drmModeFreeCrtc(c);
173
Sean Paul877be972015-06-03 14:08:27 -0400174 ret = crtc->Init();
175 if (ret) {
176 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
Sean Paul877be972015-06-03 14:08:27 -0400177 break;
178 }
Zach Reiznerff30b522015-10-28 19:08:45 -0700179 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400180 }
181
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000182 std::vector<int> possible_clones;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400183 for (int i = 0; !ret && i < res->count_encoders; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700184 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400185 if (!e) {
186 ALOGE("Failed to get encoder %d", res->encoders[i]);
187 ret = -ENODEV;
188 break;
189 }
190
191 std::vector<DrmCrtc *> possible_crtcs;
192 DrmCrtc *current_crtc = NULL;
Zach Reiznerff30b522015-10-28 19:08:45 -0700193 for (auto &crtc : crtcs_) {
194 if ((1 << crtc->pipe()) & e->possible_crtcs)
195 possible_crtcs.push_back(crtc.get());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400196
Zach Reiznerff30b522015-10-28 19:08:45 -0700197 if (crtc->id() == e->crtc_id)
198 current_crtc = crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400199 }
200
Zach Reiznerff30b522015-10-28 19:08:45 -0700201 std::unique_ptr<DrmEncoder> enc(
202 new DrmEncoder(e, current_crtc, possible_crtcs));
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000203 possible_clones.push_back(e->possible_clones);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400204 drmModeFreeEncoder(e);
205
Zach Reiznerff30b522015-10-28 19:08:45 -0700206 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400207 }
208
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000209 for (unsigned int i = 0; i < encoders_.size(); i++) {
210 for (unsigned int j = 0; j < encoders_.size(); j++)
211 if (possible_clones[i] & (1 << j))
212 encoders_[i]->AddPossibleClone(encoders_[j].get());
213 }
214
Sean Paul6a55e9f2015-04-30 15:31:06 -0400215 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700216 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400217 if (!c) {
218 ALOGE("Failed to get connector %d", res->connectors[i]);
219 ret = -ENODEV;
220 break;
221 }
222
223 std::vector<DrmEncoder *> possible_encoders;
224 DrmEncoder *current_encoder = NULL;
225 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700226 for (auto &encoder : encoders_) {
227 if (encoder->id() == c->encoders[j])
228 possible_encoders.push_back(encoder.get());
229 if (encoder->id() == c->encoder_id)
230 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400231 }
232 }
233
Zach Reiznerff30b522015-10-28 19:08:45 -0700234 std::unique_ptr<DrmConnector> conn(
235 new DrmConnector(this, c, current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400236
237 drmModeFreeConnector(c);
238
Sean Paul6a55e9f2015-04-30 15:31:06 -0400239 ret = conn->Init();
240 if (ret) {
241 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400242 break;
243 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400244
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000245 if (conn->writeback())
246 writeback_connectors_.emplace_back(std::move(conn));
247 else
248 connectors_.emplace_back(std::move(conn));
Robert Foss610d9892017-11-01 12:50:04 -0500249 }
250
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200251 // Primary display priority:
Jason Macnakf1af9572020-08-20 11:49:51 -0700252 // 1) vendor.hwc.drm.primary_display_order property
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200253 // 2) internal connectors
254 // 3) anything else
255 std::vector<DrmConnector *>
256 primary_candidates = make_primary_display_candidates(connectors_);
257 if (!primary_candidates.empty() && !found_primary) {
258 DrmConnector &conn = **std::begin(primary_candidates);
259 conn.set_display(num_displays);
260 displays_[num_displays] = num_displays;
261 ++num_displays;
262 found_primary = true;
263 } else {
264 ALOGE(
Jason Macnakf1af9572020-08-20 11:49:51 -0700265 "Failed to find primary display from "
266 "\"vendor.hwc.drm.primary_display_order\" property");
Sean Paul6a55e9f2015-04-30 15:31:06 -0400267 }
Robert Foss610d9892017-11-01 12:50:04 -0500268
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200269 // If no priority display were found then pick first available as primary and
270 // for the others assign consecutive display_numbers.
Robert Foss610d9892017-11-01 12:50:04 -0500271 for (auto &conn : connectors_) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100272 if (conn->external() || conn->internal()) {
273 if (!found_primary) {
274 conn->set_display(num_displays);
275 displays_[num_displays] = num_displays;
276 found_primary = true;
277 ++num_displays;
278 } else if (conn->display() < 0) {
279 conn->set_display(num_displays);
280 displays_[num_displays] = num_displays;
281 ++num_displays;
282 }
Robert Foss610d9892017-11-01 12:50:04 -0500283 }
284 }
285
Sean Paul6a55e9f2015-04-30 15:31:06 -0400286 if (res)
287 drmModeFreeResources(res);
288
289 // Catch-all for the above loops
290 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100291 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400292
Zach Reiznerff30b522015-10-28 19:08:45 -0700293 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400294 if (!plane_res) {
295 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100296 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400297 }
298
299 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700300 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400301 if (!p) {
302 ALOGE("Failed to get plane %d", plane_res->planes[i]);
303 ret = -ENODEV;
304 break;
305 }
306
Zach Reiznerff30b522015-10-28 19:08:45 -0700307 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400308
309 drmModeFreePlane(p);
310
Sean Paul6a55e9f2015-04-30 15:31:06 -0400311 ret = plane->Init();
312 if (ret) {
313 ALOGE("Init plane %d failed", plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400314 break;
315 }
316
Zach Reiznerff30b522015-10-28 19:08:45 -0700317 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400318 }
319 drmModeFreePlaneResources(plane_res);
320 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100321 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400322
Sean Paul047b9b22015-07-28 14:15:42 -0400323 ret = event_listener_.Init();
324 if (ret) {
325 ALOGE("Can't initialize event listener %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100326 return std::make_tuple(ret, 0);
Sean Paul047b9b22015-07-28 14:15:42 -0400327 }
328
Zach Reiznerff30b522015-10-28 19:08:45 -0700329 for (auto &conn : connectors_) {
330 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400331 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700332 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100333 return std::make_tuple(ret, 0);
Sean Paul57355412015-09-19 09:14:34 -0400334 }
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000335 if (!AttachWriteback(conn.get())) {
336 ALOGI("Display %d has writeback attach to it", conn->display());
337 }
Sean Paul57355412015-09-19 09:14:34 -0400338 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100339 return std::make_tuple(ret, displays_.size());
340}
341
342bool DrmDevice::HandlesDisplay(int display) const {
343 return displays_.find(display) != displays_.end();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400344}
345
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100346DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700347 for (auto &conn : connectors_) {
348 if (conn->display() == display)
349 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400350 }
351 return NULL;
352}
353
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000354DrmConnector *DrmDevice::GetWritebackConnectorForDisplay(int display) const {
355 for (auto &conn : writeback_connectors_) {
356 if (conn->display() == display)
357 return conn.get();
358 }
359 return NULL;
360}
361
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100362// TODO what happens when hotplugging
363DrmConnector *DrmDevice::AvailableWritebackConnector(int display) const {
364 DrmConnector *writeback_conn = GetWritebackConnectorForDisplay(display);
365 DrmConnector *display_conn = GetConnectorForDisplay(display);
366 // If we have a writeback already attached to the same CRTC just use that,
367 // if possible.
368 if (display_conn && writeback_conn &&
369 writeback_conn->encoder()->CanClone(display_conn->encoder()))
370 return writeback_conn;
371
372 // Use another CRTC if available and doesn't have any connector
373 for (auto &crtc : crtcs_) {
374 if (crtc->display() == display)
375 continue;
376 display_conn = GetConnectorForDisplay(crtc->display());
377 // If we have a display connected don't use it for writeback
378 if (display_conn && display_conn->state() == DRM_MODE_CONNECTED)
379 continue;
380 writeback_conn = GetWritebackConnectorForDisplay(crtc->display());
381 if (writeback_conn)
382 return writeback_conn;
383 }
384 return NULL;
385}
386
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100387DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700388 for (auto &crtc : crtcs_) {
389 if (crtc->display() == display)
390 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400391 }
392 return NULL;
393}
394
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100395DrmPlane *DrmDevice::GetPlane(uint32_t id) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700396 for (auto &plane : planes_) {
397 if (plane->id() == id)
398 return plane.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400399 }
400 return NULL;
401}
402
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100403const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400404 return crtcs_;
405}
406
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100407uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400408 return ++mode_id_;
409}
410
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100411int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400412 /* First try to use the currently-bound crtc */
413 DrmCrtc *crtc = enc->crtc();
414 if (crtc && crtc->can_bind(display)) {
415 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000416 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400417 return 0;
418 }
419
420 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700421 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400422 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700423 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400424 continue;
425
Zach Reiznerff30b522015-10-28 19:08:45 -0700426 if (crtc->can_bind(display)) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700427 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000428 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400429 return 0;
430 }
431 }
432
433 /* We can't use the encoder, but nothing went wrong, try another one */
434 return -EAGAIN;
435}
436
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100437int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400438 int display = connector->display();
439 /* Try to use current setup first */
440 if (connector->encoder()) {
441 int ret = TryEncoderForDisplay(display, connector->encoder());
442 if (!ret) {
443 return 0;
444 } else if (ret != -EAGAIN) {
445 ALOGE("Could not set mode %d/%d", display, ret);
446 return ret;
447 }
448 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400449
Zach Reiznerff30b522015-10-28 19:08:45 -0700450 for (DrmEncoder *enc : connector->possible_encoders()) {
451 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400452 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700453 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400454 return 0;
455 } else if (ret != -EAGAIN) {
456 ALOGE("Could not set mode %d/%d", display, ret);
457 return ret;
458 }
459 }
460 ALOGE("Could not find a suitable encoder/crtc for display %d",
461 connector->display());
462 return -ENODEV;
463}
464
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000465// Attach writeback connector to the CRTC linked to the display_conn
466int DrmDevice::AttachWriteback(DrmConnector *display_conn) {
467 DrmCrtc *display_crtc = display_conn->encoder()->crtc();
468 if (GetWritebackConnectorForDisplay(display_crtc->display()) != NULL) {
469 ALOGE("Display already has writeback attach to it");
470 return -EINVAL;
471 }
472 for (auto &writeback_conn : writeback_connectors_) {
473 if (writeback_conn->display() >= 0)
474 continue;
475 for (DrmEncoder *writeback_enc : writeback_conn->possible_encoders()) {
476 for (DrmCrtc *possible_crtc : writeback_enc->possible_crtcs()) {
477 if (possible_crtc != display_crtc)
478 continue;
479 // Use just encoders which had not been bound already
480 if (writeback_enc->can_bind(display_crtc->display())) {
481 writeback_enc->set_crtc(display_crtc);
482 writeback_conn->set_encoder(writeback_enc);
483 writeback_conn->set_display(display_crtc->display());
484 writeback_conn->UpdateModes();
485 return 0;
486 }
487 }
488 }
489 }
490 return -EINVAL;
491}
492
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100493int DrmDevice::CreatePropertyBlob(void *data, size_t length,
494 uint32_t *blob_id) {
Sean Paul877be972015-06-03 14:08:27 -0400495 struct drm_mode_create_blob create_blob;
496 memset(&create_blob, 0, sizeof(create_blob));
497 create_blob.length = length;
498 create_blob.data = (__u64)data;
499
Zach Reiznerff30b522015-10-28 19:08:45 -0700500 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400501 if (ret) {
502 ALOGE("Failed to create mode property blob %d", ret);
503 return ret;
504 }
505 *blob_id = create_blob.blob_id;
506 return 0;
507}
508
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100509int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) {
Sean Paul57355412015-09-19 09:14:34 -0400510 if (!blob_id)
511 return 0;
512
Sean Paul877be972015-06-03 14:08:27 -0400513 struct drm_mode_destroy_blob destroy_blob;
514 memset(&destroy_blob, 0, sizeof(destroy_blob));
515 destroy_blob.blob_id = (__u32)blob_id;
Zach Reiznerff30b522015-10-28 19:08:45 -0700516 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
Sean Paul877be972015-06-03 14:08:27 -0400517 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400518 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
Sean Paul877be972015-06-03 14:08:27 -0400519 return ret;
520 }
521 return 0;
522}
523
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100524DrmEventListener *DrmDevice::event_listener() {
Sean Paul047b9b22015-07-28 14:15:42 -0400525 return &event_listener_;
526}
527
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100528int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
529 const char *prop_name, DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400530 drmModeObjectPropertiesPtr props;
531
Zach Reiznerff30b522015-10-28 19:08:45 -0700532 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400533 if (!props) {
534 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
535 return -ENODEV;
536 }
537
538 bool found = false;
539 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700540 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400541 if (!strcmp(p->name, prop_name)) {
542 property->Init(p, props->prop_values[i]);
543 found = true;
544 }
545 drmModeFreeProperty(p);
546 }
547
548 drmModeFreeObjectProperties(props);
549 return found ? 0 : -ENOENT;
550}
551
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100552int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
553 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400554 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
555}
556
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100557int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
558 DrmProperty *property) {
Sean Paul877be972015-06-03 14:08:27 -0400559 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
560}
561
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100562int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
563 const char *prop_name,
564 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400565 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
566 property);
567}
Matvii Zorinef3c7972020-08-11 15:15:44 +0300568
569const std::string DrmDevice::GetName() const {
570 auto ver = drmGetVersion(fd_.get());
571 if (!ver) {
572 ALOGW("Failed to get drm version for fd=%d", fd_.get());
573 return "generic";
574 }
575
576 std::string name(ver->name);
577 drmFreeVersion(ver);
578 return name;
579}
Sean Paulf72cccd2018-08-27 13:59:08 -0400580} // namespace android