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-resources" 18 19 #include "drmconnector.h" 20 #include "drmcrtc.h" 21 #include "drmencoder.h" 22 #include "drmeventlistener.h" 23 #include "drmplane.h" 24 #include "drmresources.h" 25 26 #include <cinttypes> 27 #include <errno.h> 28 #include <fcntl.h> 29 #include <stdint.h> 30 #include <xf86drm.h> 31 #include <xf86drmMode.h> 32 33 #include <cutils/log.h> 34 #include <cutils/properties.h> 35 36 namespace android { 37 38 DrmResources::DrmResources() : compositor_(this), event_listener_(this) { 39 } 40 41 DrmResources::~DrmResources() { 42 event_listener_.Exit(); 43 } 44 45 int DrmResources::Init() { 46 char path[PROPERTY_VALUE_MAX]; 47 property_get("hwc.drm.device", path, "/dev/dri/card0"); 48 49 /* TODO: Use drmOpenControl here instead */ 50 fd_.Set(open(path, O_RDWR)); 51 if (fd() < 0) { 52 ALOGE("Failed to open dri- %s", strerror(-errno)); 53 return -ENODEV; 54 } 55 56 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); 57 if (ret) { 58 ALOGE("Failed to set universal plane cap %d", ret); 59 return ret; 60 } 61 62 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1); 63 if (ret) { 64 ALOGE("Failed to set atomic cap %d", ret); 65 return ret; 66 } 67 68 drmModeResPtr res = drmModeGetResources(fd()); 69 if (!res) { 70 ALOGE("Failed to get DrmResources resources"); 71 return -ENODEV; 72 } 73 74 min_resolution_ = 75 std::pair<uint32_t, uint32_t>(res->min_width, res->min_height); 76 max_resolution_ = 77 std::pair<uint32_t, uint32_t>(res->max_width, res->max_height); 78 79 bool found_primary = false; 80 int display_num = 1; 81 82 for (int i = 0; !ret && i < res->count_crtcs; ++i) { 83 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]); 84 if (!c) { 85 ALOGE("Failed to get crtc %d", res->crtcs[i]); 86 ret = -ENODEV; 87 break; 88 } 89 90 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i)); 91 drmModeFreeCrtc(c); 92 93 ret = crtc->Init(); 94 if (ret) { 95 ALOGE("Failed to initialize crtc %d", res->crtcs[i]); 96 break; 97 } 98 crtcs_.emplace_back(std::move(crtc)); 99 } 100 101 for (int i = 0; !ret && i < res->count_encoders; ++i) { 102 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]); 103 if (!e) { 104 ALOGE("Failed to get encoder %d", res->encoders[i]); 105 ret = -ENODEV; 106 break; 107 } 108 109 std::vector<DrmCrtc *> possible_crtcs; 110 DrmCrtc *current_crtc = NULL; 111 for (auto &crtc : crtcs_) { 112 if ((1 << crtc->pipe()) & e->possible_crtcs) 113 possible_crtcs.push_back(crtc.get()); 114 115 if (crtc->id() == e->crtc_id) 116 current_crtc = crtc.get(); 117 } 118 119 std::unique_ptr<DrmEncoder> enc( 120 new DrmEncoder(e, current_crtc, possible_crtcs)); 121 122 drmModeFreeEncoder(e); 123 124 encoders_.emplace_back(std::move(enc)); 125 } 126 127 for (int i = 0; !ret && i < res->count_connectors; ++i) { 128 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]); 129 if (!c) { 130 ALOGE("Failed to get connector %d", res->connectors[i]); 131 ret = -ENODEV; 132 break; 133 } 134 135 std::vector<DrmEncoder *> possible_encoders; 136 DrmEncoder *current_encoder = NULL; 137 for (int j = 0; j < c->count_encoders; ++j) { 138 for (auto &encoder : encoders_) { 139 if (encoder->id() == c->encoders[j]) 140 possible_encoders.push_back(encoder.get()); 141 if (encoder->id() == c->encoder_id) 142 current_encoder = encoder.get(); 143 } 144 } 145 146 std::unique_ptr<DrmConnector> conn( 147 new DrmConnector(this, c, current_encoder, possible_encoders)); 148 149 drmModeFreeConnector(c); 150 151 ret = conn->Init(); 152 if (ret) { 153 ALOGE("Init connector %d failed", res->connectors[i]); 154 break; 155 } 156 157 if (conn->built_in() && !found_primary) { 158 conn->set_display(0); 159 found_primary = true; 160 } else { 161 conn->set_display(display_num); 162 ++display_num; 163 } 164 165 connectors_.emplace_back(std::move(conn)); 166 } 167 if (res) 168 drmModeFreeResources(res); 169 170 // Catch-all for the above loops 171 if (ret) 172 return ret; 173 174 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd()); 175 if (!plane_res) { 176 ALOGE("Failed to get plane resources"); 177 return -ENOENT; 178 } 179 180 for (uint32_t i = 0; i < plane_res->count_planes; ++i) { 181 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]); 182 if (!p) { 183 ALOGE("Failed to get plane %d", plane_res->planes[i]); 184 ret = -ENODEV; 185 break; 186 } 187 188 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p)); 189 190 drmModeFreePlane(p); 191 192 ret = plane->Init(); 193 if (ret) { 194 ALOGE("Init plane %d failed", plane_res->planes[i]); 195 break; 196 } 197 198 planes_.emplace_back(std::move(plane)); 199 } 200 drmModeFreePlaneResources(plane_res); 201 if (ret) 202 return ret; 203 204 ret = compositor_.Init(); 205 if (ret) 206 return ret; 207 208 ret = event_listener_.Init(); 209 if (ret) { 210 ALOGE("Can't initialize event listener %d", ret); 211 return ret; 212 } 213 214 for (auto &conn : connectors_) { 215 ret = CreateDisplayPipe(conn.get()); 216 if (ret) { 217 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret); 218 return ret; 219 } 220 } 221 return 0; 222 } 223 224 DrmConnector *DrmResources::GetConnectorForDisplay(int display) const { 225 for (auto &conn : connectors_) { 226 if (conn->display() == display) 227 return conn.get(); 228 } 229 return NULL; 230 } 231 232 DrmCrtc *DrmResources::GetCrtcForDisplay(int display) const { 233 for (auto &crtc : crtcs_) { 234 if (crtc->display() == display) 235 return crtc.get(); 236 } 237 return NULL; 238 } 239 240 DrmPlane *DrmResources::GetPlane(uint32_t id) const { 241 for (auto &plane : planes_) { 242 if (plane->id() == id) 243 return plane.get(); 244 } 245 return NULL; 246 } 247 248 uint32_t DrmResources::next_mode_id() { 249 return ++mode_id_; 250 } 251 252 int DrmResources::TryEncoderForDisplay(int display, DrmEncoder *enc) { 253 /* First try to use the currently-bound crtc */ 254 DrmCrtc *crtc = enc->crtc(); 255 if (crtc && crtc->can_bind(display)) { 256 crtc->set_display(display); 257 return 0; 258 } 259 260 /* Try to find a possible crtc which will work */ 261 for (DrmCrtc *crtc : enc->possible_crtcs()) { 262 /* We've already tried this earlier */ 263 if (crtc == enc->crtc()) 264 continue; 265 266 if (crtc->can_bind(display)) { 267 enc->set_crtc(crtc); 268 crtc->set_display(display); 269 return 0; 270 } 271 } 272 273 /* We can't use the encoder, but nothing went wrong, try another one */ 274 return -EAGAIN; 275 } 276 277 int DrmResources::CreateDisplayPipe(DrmConnector *connector) { 278 int display = connector->display(); 279 /* Try to use current setup first */ 280 if (connector->encoder()) { 281 int ret = TryEncoderForDisplay(display, connector->encoder()); 282 if (!ret) { 283 return 0; 284 } else if (ret != -EAGAIN) { 285 ALOGE("Could not set mode %d/%d", display, ret); 286 return ret; 287 } 288 } 289 290 for (DrmEncoder *enc : connector->possible_encoders()) { 291 int ret = TryEncoderForDisplay(display, enc); 292 if (!ret) { 293 connector->set_encoder(enc); 294 return 0; 295 } else if (ret != -EAGAIN) { 296 ALOGE("Could not set mode %d/%d", display, ret); 297 return ret; 298 } 299 } 300 ALOGE("Could not find a suitable encoder/crtc for display %d", 301 connector->display()); 302 return -ENODEV; 303 } 304 305 int DrmResources::CreatePropertyBlob(void *data, size_t length, 306 uint32_t *blob_id) { 307 struct drm_mode_create_blob create_blob; 308 memset(&create_blob, 0, sizeof(create_blob)); 309 create_blob.length = length; 310 create_blob.data = (__u64)data; 311 312 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob); 313 if (ret) { 314 ALOGE("Failed to create mode property blob %d", ret); 315 return ret; 316 } 317 *blob_id = create_blob.blob_id; 318 return 0; 319 } 320 321 int DrmResources::DestroyPropertyBlob(uint32_t blob_id) { 322 if (!blob_id) 323 return 0; 324 325 struct drm_mode_destroy_blob destroy_blob; 326 memset(&destroy_blob, 0, sizeof(destroy_blob)); 327 destroy_blob.blob_id = (__u32)blob_id; 328 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob); 329 if (ret) { 330 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret); 331 return ret; 332 } 333 return 0; 334 } 335 336 int DrmResources::SetDisplayActiveMode(int display, const DrmMode &mode) { 337 std::unique_ptr<DrmComposition> comp(compositor_.CreateComposition(NULL)); 338 if (!comp) { 339 ALOGE("Failed to create composition for dpms on %d", display); 340 return -ENOMEM; 341 } 342 int ret = comp->SetDisplayMode(display, mode); 343 if (ret) { 344 ALOGE("Failed to add mode to composition on %d %d", display, ret); 345 return ret; 346 } 347 ret = compositor_.QueueComposition(std::move(comp)); 348 if (ret) { 349 ALOGE("Failed to queue dpms composition on %d %d", display, ret); 350 return ret; 351 } 352 return 0; 353 } 354 355 int DrmResources::SetDpmsMode(int display, uint64_t mode) { 356 if (mode != DRM_MODE_DPMS_ON && mode != DRM_MODE_DPMS_OFF) { 357 ALOGE("Invalid dpms mode %" PRIu64, mode); 358 return -EINVAL; 359 } 360 361 std::unique_ptr<DrmComposition> comp(compositor_.CreateComposition(NULL)); 362 if (!comp) { 363 ALOGE("Failed to create composition for dpms on %d", display); 364 return -ENOMEM; 365 } 366 int ret = comp->SetDpmsMode(display, mode); 367 if (ret) { 368 ALOGE("Failed to add dpms %" PRIu64 " to composition on %d %d", mode, 369 display, ret); 370 return ret; 371 } 372 ret = compositor_.QueueComposition(std::move(comp)); 373 if (ret) { 374 ALOGE("Failed to queue dpms composition on %d %d", display, ret); 375 return ret; 376 } 377 return 0; 378 } 379 380 DrmCompositor *DrmResources::compositor() { 381 return &compositor_; 382 } 383 384 DrmEventListener *DrmResources::event_listener() { 385 return &event_listener_; 386 } 387 388 int DrmResources::GetProperty(uint32_t obj_id, uint32_t obj_type, 389 const char *prop_name, DrmProperty *property) { 390 drmModeObjectPropertiesPtr props; 391 392 props = drmModeObjectGetProperties(fd(), obj_id, obj_type); 393 if (!props) { 394 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type); 395 return -ENODEV; 396 } 397 398 bool found = false; 399 for (int i = 0; !found && (size_t)i < props->count_props; ++i) { 400 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]); 401 if (!strcmp(p->name, prop_name)) { 402 property->Init(p, props->prop_values[i]); 403 found = true; 404 } 405 drmModeFreeProperty(p); 406 } 407 408 drmModeFreeObjectProperties(props); 409 return found ? 0 : -ENOENT; 410 } 411 412 int DrmResources::GetPlaneProperty(const DrmPlane &plane, const char *prop_name, 413 DrmProperty *property) { 414 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property); 415 } 416 417 int DrmResources::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name, 418 DrmProperty *property) { 419 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property); 420 } 421 422 int DrmResources::GetConnectorProperty(const DrmConnector &connector, 423 const char *prop_name, 424 DrmProperty *property) { 425 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name, 426 property); 427 } 428 } 429