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 <cutils/properties.h> 34 #include <log/log.h> 35 36 namespace android { 37 38 DrmDevice::DrmDevice() : event_listener_(this) { 39 } 40 41 DrmDevice::~DrmDevice() { 42 event_listener_.Exit(); 43 } 44 45 std::tuple<int, int> DrmDevice::Init(const char *path, int num_displays) { 46 /* TODO: Use drmOpenControl here instead */ 47 fd_.Set(open(path, O_RDWR)); 48 if (fd() < 0) { 49 ALOGE("Failed to open dri- %s", strerror(-errno)); 50 return std::make_tuple(-ENODEV, 0); 51 } 52 53 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); 54 if (ret) { 55 ALOGE("Failed to set universal plane cap %d", ret); 56 return std::make_tuple(ret, 0); 57 } 58 59 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1); 60 if (ret) { 61 ALOGE("Failed to set atomic cap %d", ret); 62 return std::make_tuple(ret, 0); 63 } 64 65 #ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS 66 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1); 67 if (ret) { 68 ALOGI("Failed to set writeback cap %d", ret); 69 ret = 0; 70 } 71 #endif 72 73 drmModeResPtr res = drmModeGetResources(fd()); 74 if (!res) { 75 ALOGE("Failed to get DrmDevice resources"); 76 return std::make_tuple(-ENODEV, 0); 77 } 78 79 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width, 80 res->min_height); 81 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width, 82 res->max_height); 83 84 // Assumes that the primary display will always be in the first 85 // drm_device opened. 86 bool found_primary = num_displays != 0; 87 88 for (int i = 0; !ret && i < res->count_crtcs; ++i) { 89 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]); 90 if (!c) { 91 ALOGE("Failed to get crtc %d", res->crtcs[i]); 92 ret = -ENODEV; 93 break; 94 } 95 96 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i)); 97 drmModeFreeCrtc(c); 98 99 ret = crtc->Init(); 100 if (ret) { 101 ALOGE("Failed to initialize crtc %d", res->crtcs[i]); 102 break; 103 } 104 crtcs_.emplace_back(std::move(crtc)); 105 } 106 107 std::vector<int> possible_clones; 108 for (int i = 0; !ret && i < res->count_encoders; ++i) { 109 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]); 110 if (!e) { 111 ALOGE("Failed to get encoder %d", res->encoders[i]); 112 ret = -ENODEV; 113 break; 114 } 115 116 std::vector<DrmCrtc *> possible_crtcs; 117 DrmCrtc *current_crtc = NULL; 118 for (auto &crtc : crtcs_) { 119 if ((1 << crtc->pipe()) & e->possible_crtcs) 120 possible_crtcs.push_back(crtc.get()); 121 122 if (crtc->id() == e->crtc_id) 123 current_crtc = crtc.get(); 124 } 125 126 std::unique_ptr<DrmEncoder> enc( 127 new DrmEncoder(e, current_crtc, possible_crtcs)); 128 possible_clones.push_back(e->possible_clones); 129 drmModeFreeEncoder(e); 130 131 encoders_.emplace_back(std::move(enc)); 132 } 133 134 for (unsigned int i = 0; i < encoders_.size(); i++) { 135 for (unsigned int j = 0; j < encoders_.size(); j++) 136 if (possible_clones[i] & (1 << j)) 137 encoders_[i]->AddPossibleClone(encoders_[j].get()); 138 } 139 140 for (int i = 0; !ret && i < res->count_connectors; ++i) { 141 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]); 142 if (!c) { 143 ALOGE("Failed to get connector %d", res->connectors[i]); 144 ret = -ENODEV; 145 break; 146 } 147 148 std::vector<DrmEncoder *> possible_encoders; 149 DrmEncoder *current_encoder = NULL; 150 for (int j = 0; j < c->count_encoders; ++j) { 151 for (auto &encoder : encoders_) { 152 if (encoder->id() == c->encoders[j]) 153 possible_encoders.push_back(encoder.get()); 154 if (encoder->id() == c->encoder_id) 155 current_encoder = encoder.get(); 156 } 157 } 158 159 std::unique_ptr<DrmConnector> conn( 160 new DrmConnector(this, c, current_encoder, possible_encoders)); 161 162 drmModeFreeConnector(c); 163 164 ret = conn->Init(); 165 if (ret) { 166 ALOGE("Init connector %d failed", res->connectors[i]); 167 break; 168 } 169 170 if (conn->writeback()) 171 writeback_connectors_.emplace_back(std::move(conn)); 172 else 173 connectors_.emplace_back(std::move(conn)); 174 } 175 176 // First look for primary amongst internal connectors 177 for (auto &conn : connectors_) { 178 if (conn->internal() && !found_primary) { 179 conn->set_display(num_displays); 180 displays_[num_displays] = num_displays; 181 ++num_displays; 182 found_primary = true; 183 break; 184 } 185 } 186 187 // Then pick first available as primary and for the others assign 188 // consecutive display_numbers. 189 for (auto &conn : connectors_) { 190 if (conn->external() || conn->internal()) { 191 if (!found_primary) { 192 conn->set_display(num_displays); 193 displays_[num_displays] = num_displays; 194 found_primary = true; 195 ++num_displays; 196 } else if (conn->display() < 0) { 197 conn->set_display(num_displays); 198 displays_[num_displays] = num_displays; 199 ++num_displays; 200 } 201 } 202 } 203 204 if (res) 205 drmModeFreeResources(res); 206 207 // Catch-all for the above loops 208 if (ret) 209 return std::make_tuple(ret, 0); 210 211 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd()); 212 if (!plane_res) { 213 ALOGE("Failed to get plane resources"); 214 return std::make_tuple(-ENOENT, 0); 215 } 216 217 for (uint32_t i = 0; i < plane_res->count_planes; ++i) { 218 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]); 219 if (!p) { 220 ALOGE("Failed to get plane %d", plane_res->planes[i]); 221 ret = -ENODEV; 222 break; 223 } 224 225 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p)); 226 227 drmModeFreePlane(p); 228 229 ret = plane->Init(); 230 if (ret) { 231 ALOGE("Init plane %d failed", plane_res->planes[i]); 232 break; 233 } 234 235 planes_.emplace_back(std::move(plane)); 236 } 237 drmModeFreePlaneResources(plane_res); 238 if (ret) 239 return std::make_tuple(ret, 0); 240 241 ret = event_listener_.Init(); 242 if (ret) { 243 ALOGE("Can't initialize event listener %d", ret); 244 return std::make_tuple(ret, 0); 245 } 246 247 for (auto &conn : connectors_) { 248 ret = CreateDisplayPipe(conn.get()); 249 if (ret) { 250 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret); 251 return std::make_tuple(ret, 0); 252 } 253 if (!AttachWriteback(conn.get())) { 254 ALOGI("Display %d has writeback attach to it", conn->display()); 255 } 256 } 257 return std::make_tuple(ret, displays_.size()); 258 } 259 260 bool DrmDevice::HandlesDisplay(int display) const { 261 return displays_.find(display) != displays_.end(); 262 } 263 264 DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const { 265 for (auto &conn : connectors_) { 266 if (conn->display() == display) 267 return conn.get(); 268 } 269 return NULL; 270 } 271 272 DrmConnector *DrmDevice::GetWritebackConnectorForDisplay(int display) const { 273 for (auto &conn : writeback_connectors_) { 274 if (conn->display() == display) 275 return conn.get(); 276 } 277 return NULL; 278 } 279 280 // TODO what happens when hotplugging 281 DrmConnector *DrmDevice::AvailableWritebackConnector(int display) const { 282 DrmConnector *writeback_conn = GetWritebackConnectorForDisplay(display); 283 DrmConnector *display_conn = GetConnectorForDisplay(display); 284 // If we have a writeback already attached to the same CRTC just use that, 285 // if possible. 286 if (display_conn && writeback_conn && 287 writeback_conn->encoder()->CanClone(display_conn->encoder())) 288 return writeback_conn; 289 290 // Use another CRTC if available and doesn't have any connector 291 for (auto &crtc : crtcs_) { 292 if (crtc->display() == display) 293 continue; 294 display_conn = GetConnectorForDisplay(crtc->display()); 295 // If we have a display connected don't use it for writeback 296 if (display_conn && display_conn->state() == DRM_MODE_CONNECTED) 297 continue; 298 writeback_conn = GetWritebackConnectorForDisplay(crtc->display()); 299 if (writeback_conn) 300 return writeback_conn; 301 } 302 return NULL; 303 } 304 305 DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const { 306 for (auto &crtc : crtcs_) { 307 if (crtc->display() == display) 308 return crtc.get(); 309 } 310 return NULL; 311 } 312 313 DrmPlane *DrmDevice::GetPlane(uint32_t id) const { 314 for (auto &plane : planes_) { 315 if (plane->id() == id) 316 return plane.get(); 317 } 318 return NULL; 319 } 320 321 const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const { 322 return crtcs_; 323 } 324 325 uint32_t DrmDevice::next_mode_id() { 326 return ++mode_id_; 327 } 328 329 int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) { 330 /* First try to use the currently-bound crtc */ 331 DrmCrtc *crtc = enc->crtc(); 332 if (crtc && crtc->can_bind(display)) { 333 crtc->set_display(display); 334 enc->set_crtc(crtc); 335 return 0; 336 } 337 338 /* Try to find a possible crtc which will work */ 339 for (DrmCrtc *crtc : enc->possible_crtcs()) { 340 /* We've already tried this earlier */ 341 if (crtc == enc->crtc()) 342 continue; 343 344 if (crtc->can_bind(display)) { 345 crtc->set_display(display); 346 enc->set_crtc(crtc); 347 return 0; 348 } 349 } 350 351 /* We can't use the encoder, but nothing went wrong, try another one */ 352 return -EAGAIN; 353 } 354 355 int DrmDevice::CreateDisplayPipe(DrmConnector *connector) { 356 int display = connector->display(); 357 /* Try to use current setup first */ 358 if (connector->encoder()) { 359 int ret = TryEncoderForDisplay(display, connector->encoder()); 360 if (!ret) { 361 return 0; 362 } else if (ret != -EAGAIN) { 363 ALOGE("Could not set mode %d/%d", display, ret); 364 return ret; 365 } 366 } 367 368 for (DrmEncoder *enc : connector->possible_encoders()) { 369 int ret = TryEncoderForDisplay(display, enc); 370 if (!ret) { 371 connector->set_encoder(enc); 372 return 0; 373 } else if (ret != -EAGAIN) { 374 ALOGE("Could not set mode %d/%d", display, ret); 375 return ret; 376 } 377 } 378 ALOGE("Could not find a suitable encoder/crtc for display %d", 379 connector->display()); 380 return -ENODEV; 381 } 382 383 // Attach writeback connector to the CRTC linked to the display_conn 384 int DrmDevice::AttachWriteback(DrmConnector *display_conn) { 385 DrmCrtc *display_crtc = display_conn->encoder()->crtc(); 386 if (GetWritebackConnectorForDisplay(display_crtc->display()) != NULL) { 387 ALOGE("Display already has writeback attach to it"); 388 return -EINVAL; 389 } 390 for (auto &writeback_conn : writeback_connectors_) { 391 if (writeback_conn->display() >= 0) 392 continue; 393 for (DrmEncoder *writeback_enc : writeback_conn->possible_encoders()) { 394 for (DrmCrtc *possible_crtc : writeback_enc->possible_crtcs()) { 395 if (possible_crtc != display_crtc) 396 continue; 397 // Use just encoders which had not been bound already 398 if (writeback_enc->can_bind(display_crtc->display())) { 399 writeback_enc->set_crtc(display_crtc); 400 writeback_conn->set_encoder(writeback_enc); 401 writeback_conn->set_display(display_crtc->display()); 402 writeback_conn->UpdateModes(); 403 return 0; 404 } 405 } 406 } 407 } 408 return -EINVAL; 409 } 410 411 int DrmDevice::CreatePropertyBlob(void *data, size_t length, 412 uint32_t *blob_id) { 413 struct drm_mode_create_blob create_blob; 414 memset(&create_blob, 0, sizeof(create_blob)); 415 create_blob.length = length; 416 create_blob.data = (__u64)data; 417 418 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob); 419 if (ret) { 420 ALOGE("Failed to create mode property blob %d", ret); 421 return ret; 422 } 423 *blob_id = create_blob.blob_id; 424 return 0; 425 } 426 427 int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) { 428 if (!blob_id) 429 return 0; 430 431 struct drm_mode_destroy_blob destroy_blob; 432 memset(&destroy_blob, 0, sizeof(destroy_blob)); 433 destroy_blob.blob_id = (__u32)blob_id; 434 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob); 435 if (ret) { 436 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret); 437 return ret; 438 } 439 return 0; 440 } 441 442 DrmEventListener *DrmDevice::event_listener() { 443 return &event_listener_; 444 } 445 446 int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type, 447 const char *prop_name, DrmProperty *property) { 448 drmModeObjectPropertiesPtr props; 449 450 props = drmModeObjectGetProperties(fd(), obj_id, obj_type); 451 if (!props) { 452 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type); 453 return -ENODEV; 454 } 455 456 bool found = false; 457 for (int i = 0; !found && (size_t)i < props->count_props; ++i) { 458 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]); 459 if (!strcmp(p->name, prop_name)) { 460 property->Init(p, props->prop_values[i]); 461 found = true; 462 } 463 drmModeFreeProperty(p); 464 } 465 466 drmModeFreeObjectProperties(props); 467 return found ? 0 : -ENOENT; 468 } 469 470 int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name, 471 DrmProperty *property) { 472 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property); 473 } 474 475 int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name, 476 DrmProperty *property) { 477 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property); 478 } 479 480 int DrmDevice::GetConnectorProperty(const DrmConnector &connector, 481 const char *prop_name, 482 DrmProperty *property) { 483 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name, 484 property); 485 } 486 } // namespace android 487