blob: 91fe1584895a84859b66e0e314b496db057bf246 [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
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020033#include <algorithm>
34#include <array>
35#include <string>
36
Sean Paul6a55e9f2015-04-30 15:31:06 -040037#include <cutils/properties.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040038#include <log/log.h>
Sean Paul6a55e9f2015-04-30 15:31:06 -040039
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020040static void trim_left(std::string &str) {
41 str.erase(std::begin(str),
42 std::find_if(std::begin(str), std::end(str),
43 [](int ch) { return !std::isspace(ch); }));
44}
45
46static void trim_right(std::string &str) {
47 str.erase(std::find_if(std::rbegin(str), std::rend(str),
48 [](int ch) { return !std::isspace(ch); })
49 .base(),
50 std::end(str));
51}
52
53static void trim(std::string &str) {
54 trim_left(str);
55 trim_right(str);
56}
57
Sean Paul6a55e9f2015-04-30 15:31:06 -040058namespace android {
59
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020060static std::vector<std::string> read_primary_display_order_prop() {
61 std::array<char, PROPERTY_VALUE_MAX> display_order_buf;
Jason Macnakf1af9572020-08-20 11:49:51 -070062 property_get("vendor.hwc.drm.primary_display_order", display_order_buf.data(),
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020063 "...");
64
65 std::vector<std::string> display_order;
66 std::istringstream str(display_order_buf.data());
67 for (std::string conn_name = ""; std::getline(str, conn_name, ',');) {
68 trim(conn_name);
69 display_order.push_back(std::move(conn_name));
70 }
71 return display_order;
72}
73
74static std::vector<DrmConnector *> make_primary_display_candidates(
75 std::vector<std::unique_ptr<DrmConnector>> &connectors) {
76 std::vector<DrmConnector *> primary_candidates;
77 std::transform(std::begin(connectors), std::end(connectors),
78 std::back_inserter(primary_candidates),
79 [](std::unique_ptr<DrmConnector> &conn) {
80 return conn.get();
81 });
82 primary_candidates.erase(std::remove_if(std::begin(primary_candidates),
83 std::end(primary_candidates),
84 [](const DrmConnector *conn) {
85 return conn->state() !=
86 DRM_MODE_CONNECTED;
87 }),
88 std::end(primary_candidates));
89
90 std::vector<std::string> display_order = read_primary_display_order_prop();
91 bool use_other = display_order.back() == "...";
92
93 // putting connectors from primary_display_order first
94 auto curr_connector = std::begin(primary_candidates);
95 for (const std::string &display_name : display_order) {
96 auto it = std::find_if(std::begin(primary_candidates),
97 std::end(primary_candidates),
98 [&display_name](const DrmConnector *conn) {
99 return conn->name() == display_name;
100 });
101 if (it != std::end(primary_candidates)) {
102 std::iter_swap(it, curr_connector);
103 ++curr_connector;
104 }
105 }
106
107 if (use_other) {
108 // then putting internal connectors second, everything else afterwards
109 std::partition(curr_connector, std::end(primary_candidates),
110 [](const DrmConnector *conn) { return conn->internal(); });
111 } else {
112 primary_candidates.erase(curr_connector, std::end(primary_candidates));
113 }
114
115 return primary_candidates;
116}
117
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100118DrmDevice::DrmDevice() : event_listener_(this) {
Sean Paul047b9b22015-07-28 14:15:42 -0400119}
120
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100121DrmDevice::~DrmDevice() {
Sean Paul047b9b22015-07-28 14:15:42 -0400122 event_listener_.Exit();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400123}
124
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100125std::tuple<int, int> DrmDevice::Init(const char *path, int num_displays) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400126 /* TODO: Use drmOpenControl here instead */
Zach Reiznerff30b522015-10-28 19:08:45 -0700127 fd_.Set(open(path, O_RDWR));
128 if (fd() < 0) {
Peter Collingbournec77052e2020-02-19 11:25:08 -0800129 ALOGE("Failed to open dri %s: %s", path, strerror(errno));
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100130 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400131 }
132
Zach Reiznerff30b522015-10-28 19:08:45 -0700133 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400134 if (ret) {
135 ALOGE("Failed to set universal plane 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
Zach Reiznerff30b522015-10-28 19:08:45 -0700139 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400140 if (ret) {
141 ALOGE("Failed to set atomic cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100142 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400143 }
144
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000145#ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS
146 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
147 if (ret) {
148 ALOGI("Failed to set writeback cap %d", ret);
149 ret = 0;
150 }
151#endif
152
Zach Reiznerff30b522015-10-28 19:08:45 -0700153 drmModeResPtr res = drmModeGetResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400154 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100155 ALOGE("Failed to get DrmDevice resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100156 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400157 }
158
Sean Paulf72cccd2018-08-27 13:59:08 -0400159 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
160 res->min_height);
161 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
162 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -0800163
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100164 // Assumes that the primary display will always be in the first
165 // drm_device opened.
166 bool found_primary = num_displays != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400167
168 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700169 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400170 if (!c) {
171 ALOGE("Failed to get crtc %d", res->crtcs[i]);
172 ret = -ENODEV;
173 break;
174 }
175
Zach Reiznerff30b522015-10-28 19:08:45 -0700176 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400177 drmModeFreeCrtc(c);
178
Sean Paul877be972015-06-03 14:08:27 -0400179 ret = crtc->Init();
180 if (ret) {
181 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
Sean Paul877be972015-06-03 14:08:27 -0400182 break;
183 }
Zach Reiznerff30b522015-10-28 19:08:45 -0700184 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400185 }
186
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000187 std::vector<int> possible_clones;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400188 for (int i = 0; !ret && i < res->count_encoders; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700189 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400190 if (!e) {
191 ALOGE("Failed to get encoder %d", res->encoders[i]);
192 ret = -ENODEV;
193 break;
194 }
195
196 std::vector<DrmCrtc *> possible_crtcs;
197 DrmCrtc *current_crtc = NULL;
Zach Reiznerff30b522015-10-28 19:08:45 -0700198 for (auto &crtc : crtcs_) {
199 if ((1 << crtc->pipe()) & e->possible_crtcs)
200 possible_crtcs.push_back(crtc.get());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400201
Zach Reiznerff30b522015-10-28 19:08:45 -0700202 if (crtc->id() == e->crtc_id)
203 current_crtc = crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400204 }
205
Zach Reiznerff30b522015-10-28 19:08:45 -0700206 std::unique_ptr<DrmEncoder> enc(
207 new DrmEncoder(e, current_crtc, possible_crtcs));
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000208 possible_clones.push_back(e->possible_clones);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400209 drmModeFreeEncoder(e);
210
Zach Reiznerff30b522015-10-28 19:08:45 -0700211 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400212 }
213
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000214 for (unsigned int i = 0; i < encoders_.size(); i++) {
215 for (unsigned int j = 0; j < encoders_.size(); j++)
216 if (possible_clones[i] & (1 << j))
217 encoders_[i]->AddPossibleClone(encoders_[j].get());
218 }
219
Sean Paul6a55e9f2015-04-30 15:31:06 -0400220 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700221 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400222 if (!c) {
223 ALOGE("Failed to get connector %d", res->connectors[i]);
224 ret = -ENODEV;
225 break;
226 }
227
228 std::vector<DrmEncoder *> possible_encoders;
229 DrmEncoder *current_encoder = NULL;
230 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700231 for (auto &encoder : encoders_) {
232 if (encoder->id() == c->encoders[j])
233 possible_encoders.push_back(encoder.get());
234 if (encoder->id() == c->encoder_id)
235 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400236 }
237 }
238
Zach Reiznerff30b522015-10-28 19:08:45 -0700239 std::unique_ptr<DrmConnector> conn(
240 new DrmConnector(this, c, current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400241
242 drmModeFreeConnector(c);
243
Sean Paul6a55e9f2015-04-30 15:31:06 -0400244 ret = conn->Init();
245 if (ret) {
246 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400247 break;
248 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400249
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000250 if (conn->writeback())
251 writeback_connectors_.emplace_back(std::move(conn));
252 else
253 connectors_.emplace_back(std::move(conn));
Robert Foss610d9892017-11-01 12:50:04 -0500254 }
255
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200256 // Primary display priority:
Jason Macnakf1af9572020-08-20 11:49:51 -0700257 // 1) vendor.hwc.drm.primary_display_order property
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200258 // 2) internal connectors
259 // 3) anything else
260 std::vector<DrmConnector *>
261 primary_candidates = make_primary_display_candidates(connectors_);
262 if (!primary_candidates.empty() && !found_primary) {
263 DrmConnector &conn = **std::begin(primary_candidates);
264 conn.set_display(num_displays);
265 displays_[num_displays] = num_displays;
266 ++num_displays;
267 found_primary = true;
268 } else {
269 ALOGE(
Jason Macnakf1af9572020-08-20 11:49:51 -0700270 "Failed to find primary display from "
271 "\"vendor.hwc.drm.primary_display_order\" property");
Sean Paul6a55e9f2015-04-30 15:31:06 -0400272 }
Robert Foss610d9892017-11-01 12:50:04 -0500273
Roman Kovalivskyid07c3702019-11-04 17:54:31 +0200274 // If no priority display were found then pick first available as primary and
275 // for the others assign consecutive display_numbers.
Robert Foss610d9892017-11-01 12:50:04 -0500276 for (auto &conn : connectors_) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100277 if (conn->external() || conn->internal()) {
278 if (!found_primary) {
279 conn->set_display(num_displays);
280 displays_[num_displays] = num_displays;
281 found_primary = true;
282 ++num_displays;
283 } else if (conn->display() < 0) {
284 conn->set_display(num_displays);
285 displays_[num_displays] = num_displays;
286 ++num_displays;
287 }
Robert Foss610d9892017-11-01 12:50:04 -0500288 }
289 }
290
Sean Paul6a55e9f2015-04-30 15:31:06 -0400291 if (res)
292 drmModeFreeResources(res);
293
294 // Catch-all for the above loops
295 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100296 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400297
Zach Reiznerff30b522015-10-28 19:08:45 -0700298 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400299 if (!plane_res) {
300 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100301 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400302 }
303
304 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700305 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400306 if (!p) {
307 ALOGE("Failed to get plane %d", plane_res->planes[i]);
308 ret = -ENODEV;
309 break;
310 }
311
Zach Reiznerff30b522015-10-28 19:08:45 -0700312 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400313
314 drmModeFreePlane(p);
315
Sean Paul6a55e9f2015-04-30 15:31:06 -0400316 ret = plane->Init();
317 if (ret) {
318 ALOGE("Init plane %d failed", plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400319 break;
320 }
321
Zach Reiznerff30b522015-10-28 19:08:45 -0700322 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400323 }
324 drmModeFreePlaneResources(plane_res);
325 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100326 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400327
Sean Paul047b9b22015-07-28 14:15:42 -0400328 ret = event_listener_.Init();
329 if (ret) {
330 ALOGE("Can't initialize event listener %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100331 return std::make_tuple(ret, 0);
Sean Paul047b9b22015-07-28 14:15:42 -0400332 }
333
Zach Reiznerff30b522015-10-28 19:08:45 -0700334 for (auto &conn : connectors_) {
335 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400336 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700337 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100338 return std::make_tuple(ret, 0);
Sean Paul57355412015-09-19 09:14:34 -0400339 }
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000340 if (!AttachWriteback(conn.get())) {
341 ALOGI("Display %d has writeback attach to it", conn->display());
342 }
Sean Paul57355412015-09-19 09:14:34 -0400343 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100344 return std::make_tuple(ret, displays_.size());
345}
346
347bool DrmDevice::HandlesDisplay(int display) const {
348 return displays_.find(display) != displays_.end();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400349}
350
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100351DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700352 for (auto &conn : connectors_) {
353 if (conn->display() == display)
354 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400355 }
356 return NULL;
357}
358
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000359DrmConnector *DrmDevice::GetWritebackConnectorForDisplay(int display) const {
360 for (auto &conn : writeback_connectors_) {
361 if (conn->display() == display)
362 return conn.get();
363 }
364 return NULL;
365}
366
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100367// TODO what happens when hotplugging
368DrmConnector *DrmDevice::AvailableWritebackConnector(int display) const {
369 DrmConnector *writeback_conn = GetWritebackConnectorForDisplay(display);
370 DrmConnector *display_conn = GetConnectorForDisplay(display);
371 // If we have a writeback already attached to the same CRTC just use that,
372 // if possible.
373 if (display_conn && writeback_conn &&
374 writeback_conn->encoder()->CanClone(display_conn->encoder()))
375 return writeback_conn;
376
377 // Use another CRTC if available and doesn't have any connector
378 for (auto &crtc : crtcs_) {
379 if (crtc->display() == display)
380 continue;
381 display_conn = GetConnectorForDisplay(crtc->display());
382 // If we have a display connected don't use it for writeback
383 if (display_conn && display_conn->state() == DRM_MODE_CONNECTED)
384 continue;
385 writeback_conn = GetWritebackConnectorForDisplay(crtc->display());
386 if (writeback_conn)
387 return writeback_conn;
388 }
389 return NULL;
390}
391
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100392DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700393 for (auto &crtc : crtcs_) {
394 if (crtc->display() == display)
395 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400396 }
397 return NULL;
398}
399
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100400DrmPlane *DrmDevice::GetPlane(uint32_t id) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700401 for (auto &plane : planes_) {
402 if (plane->id() == id)
403 return plane.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400404 }
405 return NULL;
406}
407
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100408const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400409 return crtcs_;
410}
411
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100412uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400413 return ++mode_id_;
414}
415
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100416int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400417 /* First try to use the currently-bound crtc */
418 DrmCrtc *crtc = enc->crtc();
419 if (crtc && crtc->can_bind(display)) {
420 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000421 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400422 return 0;
423 }
424
425 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700426 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400427 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700428 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400429 continue;
430
Zach Reiznerff30b522015-10-28 19:08:45 -0700431 if (crtc->can_bind(display)) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700432 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000433 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400434 return 0;
435 }
436 }
437
438 /* We can't use the encoder, but nothing went wrong, try another one */
439 return -EAGAIN;
440}
441
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100442int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400443 int display = connector->display();
444 /* Try to use current setup first */
445 if (connector->encoder()) {
446 int ret = TryEncoderForDisplay(display, connector->encoder());
447 if (!ret) {
448 return 0;
449 } else if (ret != -EAGAIN) {
450 ALOGE("Could not set mode %d/%d", display, ret);
451 return ret;
452 }
453 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400454
Zach Reiznerff30b522015-10-28 19:08:45 -0700455 for (DrmEncoder *enc : connector->possible_encoders()) {
456 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400457 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700458 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400459 return 0;
460 } else if (ret != -EAGAIN) {
461 ALOGE("Could not set mode %d/%d", display, ret);
462 return ret;
463 }
464 }
465 ALOGE("Could not find a suitable encoder/crtc for display %d",
466 connector->display());
467 return -ENODEV;
468}
469
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000470// Attach writeback connector to the CRTC linked to the display_conn
471int DrmDevice::AttachWriteback(DrmConnector *display_conn) {
472 DrmCrtc *display_crtc = display_conn->encoder()->crtc();
473 if (GetWritebackConnectorForDisplay(display_crtc->display()) != NULL) {
474 ALOGE("Display already has writeback attach to it");
475 return -EINVAL;
476 }
477 for (auto &writeback_conn : writeback_connectors_) {
478 if (writeback_conn->display() >= 0)
479 continue;
480 for (DrmEncoder *writeback_enc : writeback_conn->possible_encoders()) {
481 for (DrmCrtc *possible_crtc : writeback_enc->possible_crtcs()) {
482 if (possible_crtc != display_crtc)
483 continue;
484 // Use just encoders which had not been bound already
485 if (writeback_enc->can_bind(display_crtc->display())) {
486 writeback_enc->set_crtc(display_crtc);
487 writeback_conn->set_encoder(writeback_enc);
488 writeback_conn->set_display(display_crtc->display());
489 writeback_conn->UpdateModes();
490 return 0;
491 }
492 }
493 }
494 }
495 return -EINVAL;
496}
497
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100498int DrmDevice::CreatePropertyBlob(void *data, size_t length,
499 uint32_t *blob_id) {
Sean Paul877be972015-06-03 14:08:27 -0400500 struct drm_mode_create_blob create_blob;
501 memset(&create_blob, 0, sizeof(create_blob));
502 create_blob.length = length;
503 create_blob.data = (__u64)data;
504
Zach Reiznerff30b522015-10-28 19:08:45 -0700505 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400506 if (ret) {
507 ALOGE("Failed to create mode property blob %d", ret);
508 return ret;
509 }
510 *blob_id = create_blob.blob_id;
511 return 0;
512}
513
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100514int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) {
Sean Paul57355412015-09-19 09:14:34 -0400515 if (!blob_id)
516 return 0;
517
Sean Paul877be972015-06-03 14:08:27 -0400518 struct drm_mode_destroy_blob destroy_blob;
519 memset(&destroy_blob, 0, sizeof(destroy_blob));
520 destroy_blob.blob_id = (__u32)blob_id;
Zach Reiznerff30b522015-10-28 19:08:45 -0700521 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
Sean Paul877be972015-06-03 14:08:27 -0400522 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400523 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
Sean Paul877be972015-06-03 14:08:27 -0400524 return ret;
525 }
526 return 0;
527}
528
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100529DrmEventListener *DrmDevice::event_listener() {
Sean Paul047b9b22015-07-28 14:15:42 -0400530 return &event_listener_;
531}
532
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100533int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
534 const char *prop_name, DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400535 drmModeObjectPropertiesPtr props;
536
Zach Reiznerff30b522015-10-28 19:08:45 -0700537 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400538 if (!props) {
539 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
540 return -ENODEV;
541 }
542
543 bool found = false;
544 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700545 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400546 if (!strcmp(p->name, prop_name)) {
547 property->Init(p, props->prop_values[i]);
548 found = true;
549 }
550 drmModeFreeProperty(p);
551 }
552
553 drmModeFreeObjectProperties(props);
554 return found ? 0 : -ENOENT;
555}
556
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100557int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
558 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400559 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
560}
561
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100562int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
563 DrmProperty *property) {
Sean Paul877be972015-06-03 14:08:27 -0400564 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
565}
566
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100567int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
568 const char *prop_name,
569 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400570 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
571 property);
572}
Matvii Zorinef3c7972020-08-11 15:15:44 +0300573
574const std::string DrmDevice::GetName() const {
575 auto ver = drmGetVersion(fd_.get());
576 if (!ver) {
577 ALOGW("Failed to get drm version for fd=%d", fd_.get());
578 return "generic";
579 }
580
581 std::string name(ver->name);
582 drmFreeVersion(ver);
583 return name;
584}
Sean Paulf72cccd2018-08-27 13:59:08 -0400585} // namespace android