1 /*
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
17 #define LOG_TAG "hwc-drm-device"
18
19 #include "DrmDevice.h"
20
21 #include <cutils/properties.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <log/log.h>
25 #include <stdint.h>
26 #include <xf86drm.h>
27 #include <xf86drmMode.h>
28
29 #include <algorithm>
30 #include <array>
31 #include <cinttypes>
32 #include <sstream>
33 #include <string>
34
trim_left(std::string & str)35 static 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
trim_right(std::string & str)41 static 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
trim(std::string & str)48 static void trim(std::string &str) {
49 trim_left(str);
50 trim_right(str);
51 }
52
53 namespace android {
54
read_primary_display_order_prop()55 static std::vector<std::string> read_primary_display_order_prop() {
56 std::array<char, PROPERTY_VALUE_MAX> display_order_buf;
57 property_get("vendor.hwc.drm.primary_display_order", display_order_buf.data(),
58 "...");
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
make_primary_display_candidates(std::vector<std::unique_ptr<DrmConnector>> & connectors)69 static 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
DrmDevice()113 DrmDevice::DrmDevice() : event_listener_(this) {
114 }
115
~DrmDevice()116 DrmDevice::~DrmDevice() {
117 event_listener_.Exit();
118 }
119
Init(const char * path,int num_displays)120 std::tuple<int, int> DrmDevice::Init(const char *path, int num_displays) {
121 /* TODO: Use drmOpenControl here instead */
122 fd_.Set(open(path, O_RDWR));
123 if (fd() < 0) {
124 ALOGE("Failed to open dri %s: %s", path, strerror(errno));
125 return std::make_tuple(-ENODEV, 0);
126 }
127
128 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
129 if (ret) {
130 ALOGE("Failed to set universal plane cap %d", ret);
131 return std::make_tuple(ret, 0);
132 }
133
134 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
135 if (ret) {
136 ALOGE("Failed to set atomic cap %d", ret);
137 return std::make_tuple(ret, 0);
138 }
139
140 #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
148 drmModeResPtr res = drmModeGetResources(fd());
149 if (!res) {
150 ALOGE("Failed to get DrmDevice resources");
151 return std::make_tuple(-ENODEV, 0);
152 }
153
154 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);
158
159 // Assumes that the primary display will always be in the first
160 // drm_device opened.
161 bool found_primary = num_displays != 0;
162
163 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
164 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
165 if (!c) {
166 ALOGE("Failed to get crtc %d", res->crtcs[i]);
167 ret = -ENODEV;
168 break;
169 }
170
171 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
172 drmModeFreeCrtc(c);
173
174 ret = crtc->Init();
175 if (ret) {
176 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
177 break;
178 }
179 crtcs_.emplace_back(std::move(crtc));
180 }
181
182 std::vector<int> possible_clones;
183 for (int i = 0; !ret && i < res->count_encoders; ++i) {
184 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
185 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;
193 for (auto &crtc : crtcs_) {
194 if ((1 << crtc->pipe()) & e->possible_crtcs)
195 possible_crtcs.push_back(crtc.get());
196
197 if (crtc->id() == e->crtc_id)
198 current_crtc = crtc.get();
199 }
200
201 std::unique_ptr<DrmEncoder> enc(
202 new DrmEncoder(e, current_crtc, possible_crtcs));
203 possible_clones.push_back(e->possible_clones);
204 drmModeFreeEncoder(e);
205
206 encoders_.emplace_back(std::move(enc));
207 }
208
209 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
215 for (int i = 0; !ret && i < res->count_connectors; ++i) {
216 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
217 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) {
226 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();
231 }
232 }
233
234 std::unique_ptr<DrmConnector> conn(
235 new DrmConnector(this, c, current_encoder, possible_encoders));
236
237 drmModeFreeConnector(c);
238
239 ret = conn->Init();
240 if (ret) {
241 ALOGE("Init connector %d failed", res->connectors[i]);
242 break;
243 }
244
245 if (conn->writeback())
246 writeback_connectors_.emplace_back(std::move(conn));
247 else
248 connectors_.emplace_back(std::move(conn));
249 }
250
251 // Primary display priority:
252 // 1) vendor.hwc.drm.primary_display_order property
253 // 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(
265 "Failed to find primary display from "
266 "\"vendor.hwc.drm.primary_display_order\" property");
267 }
268
269 // If no priority display were found then pick first available as primary and
270 // for the others assign consecutive display_numbers.
271 for (auto &conn : connectors_) {
272 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 }
283 }
284 }
285
286 if (res)
287 drmModeFreeResources(res);
288
289 // Catch-all for the above loops
290 if (ret)
291 return std::make_tuple(ret, 0);
292
293 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
294 if (!plane_res) {
295 ALOGE("Failed to get plane resources");
296 return std::make_tuple(-ENOENT, 0);
297 }
298
299 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
300 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
301 if (!p) {
302 ALOGE("Failed to get plane %d", plane_res->planes[i]);
303 ret = -ENODEV;
304 break;
305 }
306
307 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
308
309 drmModeFreePlane(p);
310
311 ret = plane->Init();
312 if (ret) {
313 ALOGE("Init plane %d failed", plane_res->planes[i]);
314 break;
315 }
316
317 planes_.emplace_back(std::move(plane));
318 }
319 drmModeFreePlaneResources(plane_res);
320 if (ret)
321 return std::make_tuple(ret, 0);
322
323 ret = event_listener_.Init();
324 if (ret) {
325 ALOGE("Can't initialize event listener %d", ret);
326 return std::make_tuple(ret, 0);
327 }
328
329 for (auto &conn : connectors_) {
330 ret = CreateDisplayPipe(conn.get());
331 if (ret) {
332 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
333 return std::make_tuple(ret, 0);
334 }
335 if (!AttachWriteback(conn.get())) {
336 ALOGI("Display %d has writeback attach to it", conn->display());
337 }
338 }
339 return std::make_tuple(ret, displays_.size());
340 }
341
HandlesDisplay(int display) const342 bool DrmDevice::HandlesDisplay(int display) const {
343 return displays_.find(display) != displays_.end();
344 }
345
GetConnectorForDisplay(int display) const346 DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
347 for (auto &conn : connectors_) {
348 if (conn->display() == display)
349 return conn.get();
350 }
351 return NULL;
352 }
353
GetWritebackConnectorForDisplay(int display) const354 DrmConnector *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
362 // TODO what happens when hotplugging
AvailableWritebackConnector(int display) const363 DrmConnector *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
GetCrtcForDisplay(int display) const387 DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
388 for (auto &crtc : crtcs_) {
389 if (crtc->display() == display)
390 return crtc.get();
391 }
392 return NULL;
393 }
394
GetPlane(uint32_t id) const395 DrmPlane *DrmDevice::GetPlane(uint32_t id) const {
396 for (auto &plane : planes_) {
397 if (plane->id() == id)
398 return plane.get();
399 }
400 return NULL;
401 }
402
crtcs() const403 const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
404 return crtcs_;
405 }
406
next_mode_id()407 uint32_t DrmDevice::next_mode_id() {
408 return ++mode_id_;
409 }
410
TryEncoderForDisplay(int display,DrmEncoder * enc)411 int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
412 /* 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);
416 enc->set_crtc(crtc);
417 return 0;
418 }
419
420 /* Try to find a possible crtc which will work */
421 for (DrmCrtc *crtc : enc->possible_crtcs()) {
422 /* We've already tried this earlier */
423 if (crtc == enc->crtc())
424 continue;
425
426 if (crtc->can_bind(display)) {
427 crtc->set_display(display);
428 enc->set_crtc(crtc);
429 return 0;
430 }
431 }
432
433 /* We can't use the encoder, but nothing went wrong, try another one */
434 return -EAGAIN;
435 }
436
CreateDisplayPipe(DrmConnector * connector)437 int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
438 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 }
449
450 for (DrmEncoder *enc : connector->possible_encoders()) {
451 int ret = TryEncoderForDisplay(display, enc);
452 if (!ret) {
453 connector->set_encoder(enc);
454 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
465 // Attach writeback connector to the CRTC linked to the display_conn
AttachWriteback(DrmConnector * display_conn)466 int 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
CreatePropertyBlob(void * data,size_t length,uint32_t * blob_id)493 int DrmDevice::CreatePropertyBlob(void *data, size_t length,
494 uint32_t *blob_id) {
495 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
500 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
501 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
DestroyPropertyBlob(uint32_t blob_id)509 int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) {
510 if (!blob_id)
511 return 0;
512
513 struct drm_mode_destroy_blob destroy_blob;
514 memset(&destroy_blob, 0, sizeof(destroy_blob));
515 destroy_blob.blob_id = (__u32)blob_id;
516 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
517 if (ret) {
518 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
519 return ret;
520 }
521 return 0;
522 }
523
event_listener()524 DrmEventListener *DrmDevice::event_listener() {
525 return &event_listener_;
526 }
527
GetProperty(uint32_t obj_id,uint32_t obj_type,const char * prop_name,DrmProperty * property)528 int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
529 const char *prop_name, DrmProperty *property) {
530 drmModeObjectPropertiesPtr props;
531
532 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
533 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) {
540 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
541 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
GetPlaneProperty(const DrmPlane & plane,const char * prop_name,DrmProperty * property)552 int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
553 DrmProperty *property) {
554 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
555 }
556
GetCrtcProperty(const DrmCrtc & crtc,const char * prop_name,DrmProperty * property)557 int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
558 DrmProperty *property) {
559 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
560 }
561
GetConnectorProperty(const DrmConnector & connector,const char * prop_name,DrmProperty * property)562 int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
563 const char *prop_name,
564 DrmProperty *property) {
565 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
566 property);
567 }
568
GetName() const569 const 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 }
580 } // namespace android
581