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 #include "drmconnector.h"
21 #include "drmcrtc.h"
22 #include "drmencoder.h"
23 #include "drmeventlistener.h"
24 #include "drmplane.h"
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdint.h>
29 #include <xf86drm.h>
30 #include <xf86drmMode.h>
31 #include <cinttypes>
32
33 #include <algorithm>
34 #include <array>
35 #include <string>
36
37 #include <cutils/properties.h>
38 #include <log/log.h>
39
trim_left(std::string & str)40 static 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
trim_right(std::string & str)46 static 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
trim(std::string & str)53 static void trim(std::string &str) {
54 trim_left(str);
55 trim_right(str);
56 }
57
58 namespace android {
59
read_primary_display_order_prop()60 static std::vector<std::string> read_primary_display_order_prop() {
61 std::array<char, PROPERTY_VALUE_MAX> display_order_buf;
62 property_get("hwc.drm.primary_display_order", display_order_buf.data(),
63 "...");
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
make_primary_display_candidates(std::vector<std::unique_ptr<DrmConnector>> & connectors)74 static 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
DrmDevice()118 DrmDevice::DrmDevice() : event_listener_(this) {
119 }
120
~DrmDevice()121 DrmDevice::~DrmDevice() {
122 event_listener_.Exit();
123 }
124
Init(const char * path,int num_displays)125 std::tuple<int, int> DrmDevice::Init(const char *path, int num_displays) {
126 /* TODO: Use drmOpenControl here instead */
127 fd_.Set(open(path, O_RDWR));
128 if (fd() < 0) {
129 ALOGE("Failed to open dri- %s", strerror(-errno));
130 return std::make_tuple(-ENODEV, 0);
131 }
132
133 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
134 if (ret) {
135 ALOGE("Failed to set universal plane cap %d", ret);
136 return std::make_tuple(ret, 0);
137 }
138
139 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
140 if (ret) {
141 ALOGE("Failed to set atomic cap %d", ret);
142 return std::make_tuple(ret, 0);
143 }
144
145 #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
153 drmModeResPtr res = drmModeGetResources(fd());
154 if (!res) {
155 ALOGE("Failed to get DrmDevice resources");
156 return std::make_tuple(-ENODEV, 0);
157 }
158
159 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);
163
164 // Assumes that the primary display will always be in the first
165 // drm_device opened.
166 bool found_primary = num_displays != 0;
167
168 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
169 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
170 if (!c) {
171 ALOGE("Failed to get crtc %d", res->crtcs[i]);
172 ret = -ENODEV;
173 break;
174 }
175
176 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
177 drmModeFreeCrtc(c);
178
179 ret = crtc->Init();
180 if (ret) {
181 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
182 break;
183 }
184 crtcs_.emplace_back(std::move(crtc));
185 }
186
187 std::vector<int> possible_clones;
188 for (int i = 0; !ret && i < res->count_encoders; ++i) {
189 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
190 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;
198 for (auto &crtc : crtcs_) {
199 if ((1 << crtc->pipe()) & e->possible_crtcs)
200 possible_crtcs.push_back(crtc.get());
201
202 if (crtc->id() == e->crtc_id)
203 current_crtc = crtc.get();
204 }
205
206 std::unique_ptr<DrmEncoder> enc(
207 new DrmEncoder(e, current_crtc, possible_crtcs));
208 possible_clones.push_back(e->possible_clones);
209 drmModeFreeEncoder(e);
210
211 encoders_.emplace_back(std::move(enc));
212 }
213
214 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
220 for (int i = 0; !ret && i < res->count_connectors; ++i) {
221 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
222 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) {
231 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();
236 }
237 }
238
239 std::unique_ptr<DrmConnector> conn(
240 new DrmConnector(this, c, current_encoder, possible_encoders));
241
242 drmModeFreeConnector(c);
243
244 ret = conn->Init();
245 if (ret) {
246 ALOGE("Init connector %d failed", res->connectors[i]);
247 break;
248 }
249
250 if (conn->writeback())
251 writeback_connectors_.emplace_back(std::move(conn));
252 else
253 connectors_.emplace_back(std::move(conn));
254 }
255
256 // Primary display priority:
257 // 1) hwc.drm.primary_display_order property
258 // 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(
270 "Failed to find primary display from \"hwc.drm.primary_display_order\" "
271 "property");
272 }
273
274 // If no priority display were found then pick first available as primary and
275 // for the others assign consecutive display_numbers.
276 for (auto &conn : connectors_) {
277 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 }
288 }
289 }
290
291 if (res)
292 drmModeFreeResources(res);
293
294 // Catch-all for the above loops
295 if (ret)
296 return std::make_tuple(ret, 0);
297
298 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
299 if (!plane_res) {
300 ALOGE("Failed to get plane resources");
301 return std::make_tuple(-ENOENT, 0);
302 }
303
304 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
305 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
306 if (!p) {
307 ALOGE("Failed to get plane %d", plane_res->planes[i]);
308 ret = -ENODEV;
309 break;
310 }
311
312 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
313
314 drmModeFreePlane(p);
315
316 ret = plane->Init();
317 if (ret) {
318 ALOGE("Init plane %d failed", plane_res->planes[i]);
319 break;
320 }
321
322 planes_.emplace_back(std::move(plane));
323 }
324 drmModeFreePlaneResources(plane_res);
325 if (ret)
326 return std::make_tuple(ret, 0);
327
328 ret = event_listener_.Init();
329 if (ret) {
330 ALOGE("Can't initialize event listener %d", ret);
331 return std::make_tuple(ret, 0);
332 }
333
334 for (auto &conn : connectors_) {
335 ret = CreateDisplayPipe(conn.get());
336 if (ret) {
337 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
338 return std::make_tuple(ret, 0);
339 }
340 if (!AttachWriteback(conn.get())) {
341 ALOGI("Display %d has writeback attach to it", conn->display());
342 }
343 }
344 return std::make_tuple(ret, displays_.size());
345 }
346
HandlesDisplay(int display) const347 bool DrmDevice::HandlesDisplay(int display) const {
348 return displays_.find(display) != displays_.end();
349 }
350
GetConnectorForDisplay(int display) const351 DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
352 for (auto &conn : connectors_) {
353 if (conn->display() == display)
354 return conn.get();
355 }
356 return NULL;
357 }
358
GetWritebackConnectorForDisplay(int display) const359 DrmConnector *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
367 // TODO what happens when hotplugging
AvailableWritebackConnector(int display) const368 DrmConnector *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
GetCrtcForDisplay(int display) const392 DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
393 for (auto &crtc : crtcs_) {
394 if (crtc->display() == display)
395 return crtc.get();
396 }
397 return NULL;
398 }
399
GetPlane(uint32_t id) const400 DrmPlane *DrmDevice::GetPlane(uint32_t id) const {
401 for (auto &plane : planes_) {
402 if (plane->id() == id)
403 return plane.get();
404 }
405 return NULL;
406 }
407
crtcs() const408 const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
409 return crtcs_;
410 }
411
next_mode_id()412 uint32_t DrmDevice::next_mode_id() {
413 return ++mode_id_;
414 }
415
TryEncoderForDisplay(int display,DrmEncoder * enc)416 int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
417 /* 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);
421 enc->set_crtc(crtc);
422 return 0;
423 }
424
425 /* Try to find a possible crtc which will work */
426 for (DrmCrtc *crtc : enc->possible_crtcs()) {
427 /* We've already tried this earlier */
428 if (crtc == enc->crtc())
429 continue;
430
431 if (crtc->can_bind(display)) {
432 crtc->set_display(display);
433 enc->set_crtc(crtc);
434 return 0;
435 }
436 }
437
438 /* We can't use the encoder, but nothing went wrong, try another one */
439 return -EAGAIN;
440 }
441
CreateDisplayPipe(DrmConnector * connector)442 int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
443 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 }
454
455 for (DrmEncoder *enc : connector->possible_encoders()) {
456 int ret = TryEncoderForDisplay(display, enc);
457 if (!ret) {
458 connector->set_encoder(enc);
459 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
470 // Attach writeback connector to the CRTC linked to the display_conn
AttachWriteback(DrmConnector * display_conn)471 int 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
CreatePropertyBlob(void * data,size_t length,uint32_t * blob_id)498 int DrmDevice::CreatePropertyBlob(void *data, size_t length,
499 uint32_t *blob_id) {
500 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
505 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
506 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
DestroyPropertyBlob(uint32_t blob_id)514 int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) {
515 if (!blob_id)
516 return 0;
517
518 struct drm_mode_destroy_blob destroy_blob;
519 memset(&destroy_blob, 0, sizeof(destroy_blob));
520 destroy_blob.blob_id = (__u32)blob_id;
521 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
522 if (ret) {
523 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
524 return ret;
525 }
526 return 0;
527 }
528
event_listener()529 DrmEventListener *DrmDevice::event_listener() {
530 return &event_listener_;
531 }
532
GetProperty(uint32_t obj_id,uint32_t obj_type,const char * prop_name,DrmProperty * property)533 int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
534 const char *prop_name, DrmProperty *property) {
535 drmModeObjectPropertiesPtr props;
536
537 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
538 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) {
545 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
546 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
GetPlaneProperty(const DrmPlane & plane,const char * prop_name,DrmProperty * property)557 int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
558 DrmProperty *property) {
559 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
560 }
561
GetCrtcProperty(const DrmCrtc & crtc,const char * prop_name,DrmProperty * property)562 int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
563 DrmProperty *property) {
564 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
565 }
566
GetConnectorProperty(const DrmConnector & connector,const char * prop_name,DrmProperty * property)567 int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
568 const char *prop_name,
569 DrmProperty *property) {
570 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
571 property);
572 }
573 } // namespace android
574