modetest.c revision 3b115362
1/* 2 * DRM based mode setting test program 3 * Copyright 2008 Tungsten Graphics 4 * Jakob Bornecrantz <jakob@tungstengraphics.com> 5 * Copyright 2008 Intel Corporation 6 * Jesse Barnes <jesse.barnes@intel.com> 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the "Software"), 10 * to deal in the Software without restriction, including without limitation 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 * and/or sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included in 16 * all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 24 * IN THE SOFTWARE. 25 */ 26 27/* 28 * This fairly simple test program dumps output in a similar format to the 29 * "xrandr" tool everyone knows & loves. It's necessarily slightly different 30 * since the kernel separates outputs into encoder and connector structures, 31 * each with their own unique ID. The program also allows test testing of the 32 * memory management and mode setting APIs by allowing the user to specify a 33 * connector and mode to use for mode setting. If all works as expected, a 34 * blue background should be painted on the monitor attached to the specified 35 * connector after the selected mode is set. 36 * 37 * TODO: use cairo to write the mode info on the selected output once 38 * the mode has been programmed, along with possible test patterns. 39 */ 40 41#include <assert.h> 42#include <ctype.h> 43#include <stdbool.h> 44#include <stdio.h> 45#include <stdlib.h> 46#include <stdint.h> 47#include <inttypes.h> 48#include <unistd.h> 49#include <string.h> 50#include <strings.h> 51#include <errno.h> 52#include <poll.h> 53#include <sys/time.h> 54#if HAVE_SYS_SELECT_H 55#include <sys/select.h> 56#endif 57#include <math.h> 58 59#include "xf86drm.h" 60#include "xf86drmMode.h" 61#include "drm_fourcc.h" 62 63#include "util/common.h" 64#include "util/format.h" 65#include "util/kms.h" 66#include "util/pattern.h" 67 68#include "buffers.h" 69#include "cursor.h" 70 71static enum util_fill_pattern primary_fill = UTIL_PATTERN_SMPTE; 72static enum util_fill_pattern secondary_fill = UTIL_PATTERN_TILES; 73 74struct crtc { 75 drmModeCrtc *crtc; 76 drmModeObjectProperties *props; 77 drmModePropertyRes **props_info; 78 drmModeModeInfo *mode; 79}; 80 81struct encoder { 82 drmModeEncoder *encoder; 83}; 84 85struct connector { 86 drmModeConnector *connector; 87 drmModeObjectProperties *props; 88 drmModePropertyRes **props_info; 89 char *name; 90}; 91 92struct fb { 93 drmModeFB *fb; 94}; 95 96struct plane { 97 drmModePlane *plane; 98 drmModeObjectProperties *props; 99 drmModePropertyRes **props_info; 100}; 101 102struct resources { 103 struct crtc *crtcs; 104 int count_crtcs; 105 struct encoder *encoders; 106 int count_encoders; 107 struct connector *connectors; 108 int count_connectors; 109 struct fb *fbs; 110 int count_fbs; 111 struct plane *planes; 112 uint32_t count_planes; 113}; 114 115struct device { 116 int fd; 117 118 struct resources *resources; 119 120 struct { 121 unsigned int width; 122 unsigned int height; 123 124 unsigned int fb_id; 125 struct bo *bo; 126 struct bo *cursor_bo; 127 } mode; 128 129 int use_atomic; 130 drmModeAtomicReq *req; 131}; 132 133static inline int64_t U642I64(uint64_t val) 134{ 135 return (int64_t)*((int64_t *)&val); 136} 137 138static float mode_vrefresh(drmModeModeInfo *mode) 139{ 140 return mode->clock * 1000.00 141 / (mode->htotal * mode->vtotal); 142} 143 144#define bit_name_fn(res) \ 145const char * res##_str(int type) { \ 146 unsigned int i; \ 147 const char *sep = ""; \ 148 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \ 149 if (type & (1 << i)) { \ 150 printf("%s%s", sep, res##_names[i]); \ 151 sep = ", "; \ 152 } \ 153 } \ 154 return NULL; \ 155} 156 157static const char *mode_type_names[] = { 158 "builtin", 159 "clock_c", 160 "crtc_c", 161 "preferred", 162 "default", 163 "userdef", 164 "driver", 165}; 166 167static bit_name_fn(mode_type) 168 169static const char *mode_flag_names[] = { 170 "phsync", 171 "nhsync", 172 "pvsync", 173 "nvsync", 174 "interlace", 175 "dblscan", 176 "csync", 177 "pcsync", 178 "ncsync", 179 "hskew", 180 "bcast", 181 "pixmux", 182 "dblclk", 183 "clkdiv2" 184}; 185 186static bit_name_fn(mode_flag) 187 188static void dump_fourcc(uint32_t fourcc) 189{ 190 char *name = drmGetFormatName(fourcc); 191 printf(" %s", name); 192 free(name); 193} 194 195static void dump_encoders(struct device *dev) 196{ 197 drmModeEncoder *encoder; 198 int i; 199 200 printf("Encoders:\n"); 201 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n"); 202 for (i = 0; i < dev->resources->count_encoders; i++) { 203 encoder = dev->resources->encoders[i].encoder; 204 if (!encoder) 205 continue; 206 207 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n", 208 encoder->encoder_id, 209 encoder->crtc_id, 210 util_lookup_encoder_type_name(encoder->encoder_type), 211 encoder->possible_crtcs, 212 encoder->possible_clones); 213 } 214 printf("\n"); 215} 216 217static void dump_mode(drmModeModeInfo *mode, int index) 218{ 219 printf(" #%i %s %.2f %d %d %d %d %d %d %d %d %d", 220 index, 221 mode->name, 222 mode_vrefresh(mode), 223 mode->hdisplay, 224 mode->hsync_start, 225 mode->hsync_end, 226 mode->htotal, 227 mode->vdisplay, 228 mode->vsync_start, 229 mode->vsync_end, 230 mode->vtotal, 231 mode->clock); 232 233 printf(" flags: "); 234 mode_flag_str(mode->flags); 235 printf("; type: "); 236 mode_type_str(mode->type); 237 printf("\n"); 238} 239 240static void dump_blob(struct device *dev, uint32_t blob_id) 241{ 242 uint32_t i; 243 unsigned char *blob_data; 244 drmModePropertyBlobPtr blob; 245 246 blob = drmModeGetPropertyBlob(dev->fd, blob_id); 247 if (!blob) { 248 printf("\n"); 249 return; 250 } 251 252 blob_data = blob->data; 253 254 for (i = 0; i < blob->length; i++) { 255 if (i % 16 == 0) 256 printf("\n\t\t\t"); 257 printf("%.2hhx", blob_data[i]); 258 } 259 printf("\n"); 260 261 drmModeFreePropertyBlob(blob); 262} 263 264static const char *modifier_to_string(uint64_t modifier) 265{ 266 static char mod_string[4096]; 267 268 char *modifier_name = drmGetFormatModifierName(modifier); 269 char *vendor_name = drmGetFormatModifierVendor(modifier); 270 memset(mod_string, 0x00, sizeof(mod_string)); 271 272 if (!modifier_name) { 273 if (vendor_name) 274 snprintf(mod_string, sizeof(mod_string), "%s_%s", 275 vendor_name, "UNKNOWN_MODIFIER"); 276 else 277 snprintf(mod_string, sizeof(mod_string), "%s_%s", 278 "UNKNOWN_VENDOR", "UNKNOWN_MODIFIER"); 279 /* safe, as free is no-op for NULL */ 280 free(vendor_name); 281 return mod_string; 282 } 283 284 if (modifier == DRM_FORMAT_MOD_LINEAR) { 285 snprintf(mod_string, sizeof(mod_string), "%s", modifier_name); 286 free(modifier_name); 287 free(vendor_name); 288 return mod_string; 289 } 290 291 snprintf(mod_string, sizeof(mod_string), "%s_%s", 292 vendor_name, modifier_name); 293 294 free(modifier_name); 295 free(vendor_name); 296 return mod_string; 297} 298 299static void dump_in_formats(struct device *dev, uint32_t blob_id) 300{ 301 drmModeFormatModifierIterator iter = {0}; 302 drmModePropertyBlobPtr blob; 303 uint32_t fmt = 0; 304 305 printf("\t\tin_formats blob decoded:\n"); 306 blob = drmModeGetPropertyBlob(dev->fd, blob_id); 307 if (!blob) { 308 printf("\n"); 309 return; 310 } 311 312 while (drmModeFormatModifierBlobIterNext(blob, &iter)) { 313 if (!fmt || fmt != iter.fmt) { 314 printf("%s\t\t\t", !fmt ? "" : "\n"); 315 fmt = iter.fmt; 316 dump_fourcc(fmt); 317 printf(": "); 318 } 319 320 printf(" %s", modifier_to_string(iter.mod)); 321 } 322 323 printf("\n"); 324 325 drmModeFreePropertyBlob(blob); 326} 327 328static void dump_prop(struct device *dev, drmModePropertyPtr prop, 329 uint32_t prop_id, uint64_t value) 330{ 331 int i; 332 printf("\t%d", prop_id); 333 if (!prop) { 334 printf("\n"); 335 return; 336 } 337 338 printf(" %s:\n", prop->name); 339 340 printf("\t\tflags:"); 341 if (prop->flags & DRM_MODE_PROP_PENDING) 342 printf(" pending"); 343 if (prop->flags & DRM_MODE_PROP_IMMUTABLE) 344 printf(" immutable"); 345 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) 346 printf(" signed range"); 347 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) 348 printf(" range"); 349 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) 350 printf(" enum"); 351 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) 352 printf(" bitmask"); 353 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) 354 printf(" blob"); 355 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT)) 356 printf(" object"); 357 printf("\n"); 358 359 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) { 360 printf("\t\tvalues:"); 361 for (i = 0; i < prop->count_values; i++) 362 printf(" %"PRId64, U642I64(prop->values[i])); 363 printf("\n"); 364 } 365 366 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) { 367 printf("\t\tvalues:"); 368 for (i = 0; i < prop->count_values; i++) 369 printf(" %"PRIu64, prop->values[i]); 370 printf("\n"); 371 } 372 373 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) { 374 printf("\t\tenums:"); 375 for (i = 0; i < prop->count_enums; i++) 376 printf(" %s=%"PRIu64, prop->enums[i].name, 377 (uint64_t)prop->enums[i].value); 378 printf("\n"); 379 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) { 380 printf("\t\tvalues:"); 381 for (i = 0; i < prop->count_enums; i++) 382 printf(" %s=0x%llx", prop->enums[i].name, 383 (1LL << prop->enums[i].value)); 384 printf("\n"); 385 } else { 386 assert(prop->count_enums == 0); 387 } 388 389 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) { 390 printf("\t\tblobs:\n"); 391 for (i = 0; i < prop->count_blobs; i++) 392 dump_blob(dev, prop->blob_ids[i]); 393 printf("\n"); 394 } else { 395 assert(prop->count_blobs == 0); 396 } 397 398 printf("\t\tvalue:"); 399 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) 400 dump_blob(dev, value); 401 else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) 402 printf(" %"PRId64"\n", value); 403 else 404 printf(" %"PRIu64"\n", value); 405 406 if (strcmp(prop->name, "IN_FORMATS") == 0) 407 dump_in_formats(dev, value); 408} 409 410static void dump_connectors(struct device *dev) 411{ 412 int i, j; 413 414 printf("Connectors:\n"); 415 printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n"); 416 for (i = 0; i < dev->resources->count_connectors; i++) { 417 struct connector *_connector = &dev->resources->connectors[i]; 418 drmModeConnector *connector = _connector->connector; 419 if (!connector) 420 continue; 421 422 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t", 423 connector->connector_id, 424 connector->encoder_id, 425 util_lookup_connector_status_name(connector->connection), 426 _connector->name, 427 connector->mmWidth, connector->mmHeight, 428 connector->count_modes); 429 430 for (j = 0; j < connector->count_encoders; j++) 431 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]); 432 printf("\n"); 433 434 if (connector->count_modes) { 435 printf(" modes:\n"); 436 printf("\tindex name refresh (Hz) hdisp hss hse htot vdisp " 437 "vss vse vtot\n"); 438 for (j = 0; j < connector->count_modes; j++) 439 dump_mode(&connector->modes[j], j); 440 } 441 442 if (_connector->props) { 443 printf(" props:\n"); 444 for (j = 0; j < (int)_connector->props->count_props; j++) 445 dump_prop(dev, _connector->props_info[j], 446 _connector->props->props[j], 447 _connector->props->prop_values[j]); 448 } 449 } 450 printf("\n"); 451} 452 453static void dump_crtcs(struct device *dev) 454{ 455 int i; 456 uint32_t j; 457 458 printf("CRTCs:\n"); 459 printf("id\tfb\tpos\tsize\n"); 460 for (i = 0; i < dev->resources->count_crtcs; i++) { 461 struct crtc *_crtc = &dev->resources->crtcs[i]; 462 drmModeCrtc *crtc = _crtc->crtc; 463 if (!crtc) 464 continue; 465 466 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n", 467 crtc->crtc_id, 468 crtc->buffer_id, 469 crtc->x, crtc->y, 470 crtc->width, crtc->height); 471 dump_mode(&crtc->mode, 0); 472 473 if (_crtc->props) { 474 printf(" props:\n"); 475 for (j = 0; j < _crtc->props->count_props; j++) 476 dump_prop(dev, _crtc->props_info[j], 477 _crtc->props->props[j], 478 _crtc->props->prop_values[j]); 479 } else { 480 printf(" no properties found\n"); 481 } 482 } 483 printf("\n"); 484} 485 486static void dump_framebuffers(struct device *dev) 487{ 488 drmModeFB *fb; 489 int i; 490 491 printf("Frame buffers:\n"); 492 printf("id\tsize\tpitch\n"); 493 for (i = 0; i < dev->resources->count_fbs; i++) { 494 fb = dev->resources->fbs[i].fb; 495 if (!fb) 496 continue; 497 498 printf("%u\t(%ux%u)\t%u\n", 499 fb->fb_id, 500 fb->width, fb->height, 501 fb->pitch); 502 } 503 printf("\n"); 504} 505 506static void dump_planes(struct device *dev) 507{ 508 unsigned int i, j; 509 510 printf("Planes:\n"); 511 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n"); 512 513 for (i = 0; i < dev->resources->count_planes; i++) { 514 struct plane *plane = &dev->resources->planes[i]; 515 drmModePlane *ovr = plane->plane; 516 if (!ovr) 517 continue; 518 519 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n", 520 ovr->plane_id, ovr->crtc_id, ovr->fb_id, 521 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y, 522 ovr->gamma_size, ovr->possible_crtcs); 523 524 if (!ovr->count_formats) 525 continue; 526 527 printf(" formats:"); 528 for (j = 0; j < ovr->count_formats; j++) 529 dump_fourcc(ovr->formats[j]); 530 printf("\n"); 531 532 if (plane->props) { 533 printf(" props:\n"); 534 for (j = 0; j < plane->props->count_props; j++) 535 dump_prop(dev, plane->props_info[j], 536 plane->props->props[j], 537 plane->props->prop_values[j]); 538 } else { 539 printf(" no properties found\n"); 540 } 541 } 542 printf("\n"); 543 544 return; 545} 546 547static void free_resources(struct resources *res) 548{ 549 int i; 550 551 if (!res) 552 return; 553 554#define free_resource(_res, type, Type) \ 555 do { \ 556 if (!(_res)->type##s) \ 557 break; \ 558 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \ 559 if (!(_res)->type##s[i].type) \ 560 break; \ 561 drmModeFree##Type((_res)->type##s[i].type); \ 562 } \ 563 free((_res)->type##s); \ 564 } while (0) 565 566#define free_properties(_res, type) \ 567 do { \ 568 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \ 569 unsigned int j; \ 570 for (j = 0; j < res->type##s[i].props->count_props; ++j)\ 571 drmModeFreeProperty(res->type##s[i].props_info[j]);\ 572 free(res->type##s[i].props_info); \ 573 drmModeFreeObjectProperties(res->type##s[i].props); \ 574 } \ 575 } while (0) 576 577 free_properties(res, plane); 578 free_resource(res, plane, Plane); 579 580 free_properties(res, connector); 581 free_properties(res, crtc); 582 583 for (i = 0; i < res->count_connectors; i++) 584 free(res->connectors[i].name); 585 586 free_resource(res, fb, FB); 587 free_resource(res, connector, Connector); 588 free_resource(res, encoder, Encoder); 589 free_resource(res, crtc, Crtc); 590 591 free(res); 592} 593 594static struct resources *get_resources(struct device *dev) 595{ 596 drmModeRes *_res; 597 drmModePlaneRes *plane_res; 598 struct resources *res; 599 int i; 600 601 res = calloc(1, sizeof(*res)); 602 if (res == 0) 603 return NULL; 604 605 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); 606 607 _res = drmModeGetResources(dev->fd); 608 if (!_res) { 609 fprintf(stderr, "drmModeGetResources failed: %s\n", 610 strerror(errno)); 611 free(res); 612 return NULL; 613 } 614 615 res->count_crtcs = _res->count_crtcs; 616 res->count_encoders = _res->count_encoders; 617 res->count_connectors = _res->count_connectors; 618 res->count_fbs = _res->count_fbs; 619 620 res->crtcs = calloc(res->count_crtcs, sizeof(*res->crtcs)); 621 res->encoders = calloc(res->count_encoders, sizeof(*res->encoders)); 622 res->connectors = calloc(res->count_connectors, sizeof(*res->connectors)); 623 res->fbs = calloc(res->count_fbs, sizeof(*res->fbs)); 624 625 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs) { 626 drmModeFreeResources(_res); 627 goto error; 628 } 629 630#define get_resource(_res, __res, type, Type) \ 631 do { \ 632 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \ 633 uint32_t type##id = (__res)->type##s[i]; \ 634 (_res)->type##s[i].type = \ 635 drmModeGet##Type(dev->fd, type##id); \ 636 if (!(_res)->type##s[i].type) \ 637 fprintf(stderr, "could not get %s %i: %s\n", \ 638 #type, type##id, \ 639 strerror(errno)); \ 640 } \ 641 } while (0) 642 643 get_resource(res, _res, crtc, Crtc); 644 get_resource(res, _res, encoder, Encoder); 645 get_resource(res, _res, connector, Connector); 646 get_resource(res, _res, fb, FB); 647 648 drmModeFreeResources(_res); 649 650 /* Set the name of all connectors based on the type name and the per-type ID. */ 651 for (i = 0; i < res->count_connectors; i++) { 652 struct connector *connector = &res->connectors[i]; 653 drmModeConnector *conn = connector->connector; 654 int num; 655 656 num = asprintf(&connector->name, "%s-%u", 657 drmModeGetConnectorTypeName(conn->connector_type), 658 conn->connector_type_id); 659 if (num < 0) 660 goto error; 661 } 662 663#define get_properties(_res, type, Type) \ 664 do { \ 665 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \ 666 struct type *obj = &res->type##s[i]; \ 667 unsigned int j; \ 668 obj->props = \ 669 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \ 670 DRM_MODE_OBJECT_##Type); \ 671 if (!obj->props) { \ 672 fprintf(stderr, \ 673 "could not get %s %i properties: %s\n", \ 674 #type, obj->type->type##_id, \ 675 strerror(errno)); \ 676 continue; \ 677 } \ 678 obj->props_info = calloc(obj->props->count_props, \ 679 sizeof(*obj->props_info)); \ 680 if (!obj->props_info) \ 681 continue; \ 682 for (j = 0; j < obj->props->count_props; ++j) \ 683 obj->props_info[j] = \ 684 drmModeGetProperty(dev->fd, obj->props->props[j]); \ 685 } \ 686 } while (0) 687 688 get_properties(res, crtc, CRTC); 689 get_properties(res, connector, CONNECTOR); 690 691 for (i = 0; i < res->count_crtcs; ++i) 692 res->crtcs[i].mode = &res->crtcs[i].crtc->mode; 693 694 plane_res = drmModeGetPlaneResources(dev->fd); 695 if (!plane_res) { 696 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n", 697 strerror(errno)); 698 return res; 699 } 700 701 res->count_planes = plane_res->count_planes; 702 703 res->planes = calloc(res->count_planes, sizeof(*res->planes)); 704 if (!res->planes) { 705 drmModeFreePlaneResources(plane_res); 706 goto error; 707 } 708 709 get_resource(res, plane_res, plane, Plane); 710 drmModeFreePlaneResources(plane_res); 711 get_properties(res, plane, PLANE); 712 713 return res; 714 715error: 716 free_resources(res); 717 return NULL; 718} 719 720static struct crtc *get_crtc_by_id(struct device *dev, uint32_t id) 721{ 722 int i; 723 724 for (i = 0; i < dev->resources->count_crtcs; ++i) { 725 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc; 726 if (crtc && crtc->crtc_id == id) 727 return &dev->resources->crtcs[i]; 728 } 729 730 return NULL; 731} 732 733static uint32_t get_crtc_mask(struct device *dev, struct crtc *crtc) 734{ 735 unsigned int i; 736 737 for (i = 0; i < (unsigned int)dev->resources->count_crtcs; i++) { 738 if (crtc->crtc->crtc_id == dev->resources->crtcs[i].crtc->crtc_id) 739 return 1 << i; 740 } 741 /* Unreachable: crtc->crtc is one of resources->crtcs[] */ 742 /* Don't return zero or static analysers will complain */ 743 abort(); 744 return 0; 745} 746 747static drmModeConnector *get_connector_by_name(struct device *dev, const char *name) 748{ 749 struct connector *connector; 750 int i; 751 752 for (i = 0; i < dev->resources->count_connectors; i++) { 753 connector = &dev->resources->connectors[i]; 754 755 if (strcmp(connector->name, name) == 0) 756 return connector->connector; 757 } 758 759 return NULL; 760} 761 762static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id) 763{ 764 drmModeConnector *connector; 765 int i; 766 767 for (i = 0; i < dev->resources->count_connectors; i++) { 768 connector = dev->resources->connectors[i].connector; 769 if (connector && connector->connector_id == id) 770 return connector; 771 } 772 773 return NULL; 774} 775 776static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id) 777{ 778 drmModeEncoder *encoder; 779 int i; 780 781 for (i = 0; i < dev->resources->count_encoders; i++) { 782 encoder = dev->resources->encoders[i].encoder; 783 if (encoder && encoder->encoder_id == id) 784 return encoder; 785 } 786 787 return NULL; 788} 789 790/* ----------------------------------------------------------------------------- 791 * Pipes and planes 792 */ 793 794/* 795 * Mode setting with the kernel interfaces is a bit of a chore. 796 * First you have to find the connector in question and make sure the 797 * requested mode is available. 798 * Then you need to find the encoder attached to that connector so you 799 * can bind it with a free crtc. 800 */ 801struct pipe_arg { 802 const char **cons; 803 uint32_t *con_ids; 804 unsigned int num_cons; 805 uint32_t crtc_id; 806 char mode_str[64]; 807 char format_str[5]; 808 float vrefresh; 809 unsigned int fourcc; 810 drmModeModeInfo *mode; 811 struct crtc *crtc; 812 unsigned int fb_id[2], current_fb_id; 813 struct timeval start; 814 815 int swap_count; 816}; 817 818struct plane_arg { 819 uint32_t plane_id; /* the id of plane to use */ 820 uint32_t crtc_id; /* the id of CRTC to bind to */ 821 bool has_position; 822 int32_t x, y; 823 uint32_t w, h; 824 double scale; 825 unsigned int fb_id; 826 unsigned int old_fb_id; 827 struct bo *bo; 828 struct bo *old_bo; 829 char format_str[5]; /* need to leave room for terminating \0 */ 830 unsigned int fourcc; 831}; 832 833static drmModeModeInfo * 834connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str, 835 const float vrefresh) 836{ 837 drmModeConnector *connector; 838 drmModeModeInfo *mode; 839 int i; 840 841 connector = get_connector_by_id(dev, con_id); 842 if (!connector || !connector->count_modes) 843 return NULL; 844 845 /* Pick by Index */ 846 if (mode_str[0] == '#') { 847 int index = atoi(mode_str + 1); 848 849 if (index >= connector->count_modes || index < 0) 850 return NULL; 851 return &connector->modes[index]; 852 } 853 854 /* Pick by Name */ 855 for (i = 0; i < connector->count_modes; i++) { 856 mode = &connector->modes[i]; 857 if (!strcmp(mode->name, mode_str)) { 858 /* If the vertical refresh frequency is not specified 859 * then return the first mode that match with the name. 860 * Else, return the mode that match the name and 861 * the specified vertical refresh frequency. 862 */ 863 if (vrefresh == 0) 864 return mode; 865 else if (fabs(mode_vrefresh(mode) - vrefresh) < 0.005) 866 return mode; 867 } 868 } 869 870 return NULL; 871} 872 873static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe) 874{ 875 uint32_t possible_crtcs = ~0; 876 uint32_t active_crtcs = 0; 877 unsigned int crtc_idx; 878 unsigned int i; 879 int j; 880 881 for (i = 0; i < pipe->num_cons; ++i) { 882 uint32_t crtcs_for_connector = 0; 883 drmModeConnector *connector; 884 drmModeEncoder *encoder; 885 struct crtc *crtc; 886 887 connector = get_connector_by_id(dev, pipe->con_ids[i]); 888 if (!connector) 889 return NULL; 890 891 for (j = 0; j < connector->count_encoders; ++j) { 892 encoder = get_encoder_by_id(dev, connector->encoders[j]); 893 if (!encoder) 894 continue; 895 896 crtcs_for_connector |= encoder->possible_crtcs; 897 crtc = get_crtc_by_id(dev, encoder->crtc_id); 898 if (!crtc) 899 continue; 900 active_crtcs |= get_crtc_mask(dev, crtc); 901 } 902 903 possible_crtcs &= crtcs_for_connector; 904 } 905 906 if (!possible_crtcs) 907 return NULL; 908 909 /* Return the first possible and active CRTC if one exists, or the first 910 * possible CRTC otherwise. 911 */ 912 if (possible_crtcs & active_crtcs) 913 crtc_idx = ffs(possible_crtcs & active_crtcs); 914 else 915 crtc_idx = ffs(possible_crtcs); 916 917 return &dev->resources->crtcs[crtc_idx - 1]; 918} 919 920static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe) 921{ 922 drmModeModeInfo *mode = NULL; 923 int i; 924 925 pipe->mode = NULL; 926 927 for (i = 0; i < (int)pipe->num_cons; i++) { 928 mode = connector_find_mode(dev, pipe->con_ids[i], 929 pipe->mode_str, pipe->vrefresh); 930 if (mode == NULL) { 931 if (pipe->vrefresh) 932 fprintf(stderr, 933 "failed to find mode " 934 "\"%s-%.2fHz\" for connector %s\n", 935 pipe->mode_str, pipe->vrefresh, pipe->cons[i]); 936 else 937 fprintf(stderr, 938 "failed to find mode \"%s\" for connector %s\n", 939 pipe->mode_str, pipe->cons[i]); 940 return -EINVAL; 941 } 942 } 943 944 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise 945 * locate a CRTC that can be attached to all the connectors. 946 */ 947 if (pipe->crtc_id != (uint32_t)-1) { 948 pipe->crtc = get_crtc_by_id(dev, pipe->crtc_id); 949 } else { 950 pipe->crtc = pipe_find_crtc(dev, pipe); 951 pipe->crtc_id = pipe->crtc->crtc->crtc_id; 952 } 953 954 if (!pipe->crtc) { 955 fprintf(stderr, "failed to find CRTC for pipe\n"); 956 return -EINVAL; 957 } 958 959 pipe->mode = mode; 960 pipe->crtc->mode = mode; 961 962 return 0; 963} 964 965/* ----------------------------------------------------------------------------- 966 * Properties 967 */ 968 969struct property_arg { 970 uint32_t obj_id; 971 uint32_t obj_type; 972 char name[DRM_PROP_NAME_LEN+1]; 973 uint32_t prop_id; 974 uint64_t value; 975 bool optional; 976}; 977 978static bool set_property(struct device *dev, struct property_arg *p) 979{ 980 drmModeObjectProperties *props = NULL; 981 drmModePropertyRes **props_info = NULL; 982 const char *obj_type; 983 int ret; 984 int i; 985 986 p->obj_type = 0; 987 p->prop_id = 0; 988 989#define find_object(_res, type, Type) \ 990 do { \ 991 for (i = 0; i < (int)(_res)->count_##type##s; ++i) { \ 992 struct type *obj = &(_res)->type##s[i]; \ 993 if (obj->type->type##_id != p->obj_id) \ 994 continue; \ 995 p->obj_type = DRM_MODE_OBJECT_##Type; \ 996 obj_type = #Type; \ 997 props = obj->props; \ 998 props_info = obj->props_info; \ 999 } \ 1000 } while(0) \ 1001 1002 find_object(dev->resources, crtc, CRTC); 1003 if (p->obj_type == 0) 1004 find_object(dev->resources, connector, CONNECTOR); 1005 if (p->obj_type == 0) 1006 find_object(dev->resources, plane, PLANE); 1007 if (p->obj_type == 0) { 1008 fprintf(stderr, "Object %i not found, can't set property\n", 1009 p->obj_id); 1010 return false; 1011 } 1012 1013 if (!props) { 1014 fprintf(stderr, "%s %i has no properties\n", 1015 obj_type, p->obj_id); 1016 return false; 1017 } 1018 1019 for (i = 0; i < (int)props->count_props; ++i) { 1020 if (!props_info[i]) 1021 continue; 1022 if (strcmp(props_info[i]->name, p->name) == 0) 1023 break; 1024 } 1025 1026 if (i == (int)props->count_props) { 1027 if (!p->optional) 1028 fprintf(stderr, "%s %i has no %s property\n", 1029 obj_type, p->obj_id, p->name); 1030 return false; 1031 } 1032 1033 p->prop_id = props->props[i]; 1034 1035 if (!dev->use_atomic) 1036 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type, 1037 p->prop_id, p->value); 1038 else 1039 ret = drmModeAtomicAddProperty(dev->req, p->obj_id, p->prop_id, p->value); 1040 1041 if (ret < 0) 1042 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n", 1043 obj_type, p->obj_id, p->name, p->value, strerror(errno)); 1044 1045 return true; 1046} 1047 1048/* -------------------------------------------------------------------------- */ 1049 1050static void 1051page_flip_handler(int fd, unsigned int frame, 1052 unsigned int sec, unsigned int usec, void *data) 1053{ 1054 struct pipe_arg *pipe; 1055 unsigned int new_fb_id; 1056 struct timeval end; 1057 double t; 1058 1059 pipe = data; 1060 if (pipe->current_fb_id == pipe->fb_id[0]) 1061 new_fb_id = pipe->fb_id[1]; 1062 else 1063 new_fb_id = pipe->fb_id[0]; 1064 1065 drmModePageFlip(fd, pipe->crtc_id, new_fb_id, 1066 DRM_MODE_PAGE_FLIP_EVENT, pipe); 1067 pipe->current_fb_id = new_fb_id; 1068 pipe->swap_count++; 1069 if (pipe->swap_count == 60) { 1070 gettimeofday(&end, NULL); 1071 t = end.tv_sec + end.tv_usec * 1e-6 - 1072 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6); 1073 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t); 1074 pipe->swap_count = 0; 1075 pipe->start = end; 1076 } 1077} 1078 1079static bool format_support(const drmModePlanePtr ovr, uint32_t fmt) 1080{ 1081 unsigned int i; 1082 1083 for (i = 0; i < ovr->count_formats; ++i) { 1084 if (ovr->formats[i] == fmt) 1085 return true; 1086 } 1087 1088 return false; 1089} 1090 1091static void add_property(struct device *dev, uint32_t obj_id, 1092 const char *name, uint64_t value) 1093{ 1094 struct property_arg p; 1095 1096 p.obj_id = obj_id; 1097 strcpy(p.name, name); 1098 p.value = value; 1099 1100 set_property(dev, &p); 1101} 1102 1103static bool add_property_optional(struct device *dev, uint32_t obj_id, 1104 const char *name, uint64_t value) 1105{ 1106 struct property_arg p; 1107 1108 p.obj_id = obj_id; 1109 strcpy(p.name, name); 1110 p.value = value; 1111 p.optional = true; 1112 1113 return set_property(dev, &p); 1114} 1115 1116static void set_gamma(struct device *dev, unsigned crtc_id, unsigned fourcc) 1117{ 1118 unsigned blob_id = 0; 1119 /* TODO: support 1024-sized LUTs, when the use-case arises */ 1120 struct drm_color_lut gamma_lut[256]; 1121 int i, ret; 1122 1123 if (fourcc == DRM_FORMAT_C8) { 1124 /* TODO: Add C8 support for more patterns */ 1125 util_smpte_c8_gamma(256, gamma_lut); 1126 drmModeCreatePropertyBlob(dev->fd, gamma_lut, sizeof(gamma_lut), &blob_id); 1127 } else { 1128 for (i = 0; i < 256; i++) { 1129 gamma_lut[i].red = 1130 gamma_lut[i].green = 1131 gamma_lut[i].blue = i << 8; 1132 } 1133 } 1134 1135 add_property_optional(dev, crtc_id, "DEGAMMA_LUT", 0); 1136 add_property_optional(dev, crtc_id, "CTM", 0); 1137 if (!add_property_optional(dev, crtc_id, "GAMMA_LUT", blob_id)) { 1138 uint16_t r[256], g[256], b[256]; 1139 1140 for (i = 0; i < 256; i++) { 1141 r[i] = gamma_lut[i].red; 1142 g[i] = gamma_lut[i].green; 1143 b[i] = gamma_lut[i].blue; 1144 } 1145 1146 ret = drmModeCrtcSetGamma(dev->fd, crtc_id, 256, r, g, b); 1147 if (ret) 1148 fprintf(stderr, "failed to set gamma: %s\n", strerror(errno)); 1149 } 1150} 1151 1152static int 1153bo_fb_create(int fd, unsigned int fourcc, const uint32_t w, const uint32_t h, 1154 enum util_fill_pattern pat, struct bo **out_bo, unsigned int *out_fb_id) 1155{ 1156 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0}; 1157 struct bo *bo; 1158 unsigned int fb_id; 1159 1160 bo = bo_create(fd, fourcc, w, h, handles, pitches, offsets, pat); 1161 1162 if (bo == NULL) 1163 return -1; 1164 1165 if (drmModeAddFB2(fd, w, h, fourcc, handles, pitches, offsets, &fb_id, 0)) { 1166 fprintf(stderr, "failed to add fb (%ux%u): %s\n", w, h, strerror(errno)); 1167 bo_destroy(bo); 1168 return -1; 1169 } 1170 *out_bo = bo; 1171 *out_fb_id = fb_id; 1172 return 0; 1173} 1174 1175static int atomic_set_plane(struct device *dev, struct plane_arg *p, 1176 int pattern, bool update) 1177{ 1178 struct bo *plane_bo; 1179 int crtc_x, crtc_y, crtc_w, crtc_h; 1180 struct crtc *crtc = NULL; 1181 unsigned int old_fb_id; 1182 1183 /* Find an unused plane which can be connected to our CRTC. Find the 1184 * CRTC index first, then iterate over available planes. 1185 */ 1186 crtc = get_crtc_by_id(dev, p->crtc_id); 1187 if (!crtc) { 1188 fprintf(stderr, "CRTC %u not found\n", p->crtc_id); 1189 return -1; 1190 } 1191 1192 if (!update) 1193 fprintf(stderr, "testing %dx%d@%s on plane %u, crtc %u\n", 1194 p->w, p->h, p->format_str, p->plane_id, p->crtc_id); 1195 1196 plane_bo = p->old_bo; 1197 p->old_bo = p->bo; 1198 1199 if (!plane_bo) { 1200 if (bo_fb_create(dev->fd, p->fourcc, p->w, p->h, 1201 pattern, &plane_bo, &p->fb_id)) 1202 return -1; 1203 } 1204 1205 p->bo = plane_bo; 1206 1207 old_fb_id = p->fb_id; 1208 p->old_fb_id = old_fb_id; 1209 1210 crtc_w = p->w * p->scale; 1211 crtc_h = p->h * p->scale; 1212 if (!p->has_position) { 1213 /* Default to the middle of the screen */ 1214 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2; 1215 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2; 1216 } else { 1217 crtc_x = p->x; 1218 crtc_y = p->y; 1219 } 1220 1221 add_property(dev, p->plane_id, "FB_ID", p->fb_id); 1222 add_property(dev, p->plane_id, "CRTC_ID", p->crtc_id); 1223 add_property(dev, p->plane_id, "SRC_X", 0); 1224 add_property(dev, p->plane_id, "SRC_Y", 0); 1225 add_property(dev, p->plane_id, "SRC_W", p->w << 16); 1226 add_property(dev, p->plane_id, "SRC_H", p->h << 16); 1227 add_property(dev, p->plane_id, "CRTC_X", crtc_x); 1228 add_property(dev, p->plane_id, "CRTC_Y", crtc_y); 1229 add_property(dev, p->plane_id, "CRTC_W", crtc_w); 1230 add_property(dev, p->plane_id, "CRTC_H", crtc_h); 1231 1232 return 0; 1233} 1234 1235static int set_plane(struct device *dev, struct plane_arg *p) 1236{ 1237 drmModePlane *ovr; 1238 uint32_t plane_id; 1239 int crtc_x, crtc_y, crtc_w, crtc_h; 1240 struct crtc *crtc = NULL; 1241 unsigned int i, crtc_mask; 1242 1243 /* Find an unused plane which can be connected to our CRTC. Find the 1244 * CRTC index first, then iterate over available planes. 1245 */ 1246 crtc = get_crtc_by_id(dev, p->crtc_id); 1247 if (!crtc) { 1248 fprintf(stderr, "CRTC %u not found\n", p->crtc_id); 1249 return -1; 1250 } 1251 crtc_mask = get_crtc_mask(dev, crtc); 1252 plane_id = p->plane_id; 1253 1254 for (i = 0; i < dev->resources->count_planes; i++) { 1255 ovr = dev->resources->planes[i].plane; 1256 if (!ovr) 1257 continue; 1258 1259 if (plane_id && plane_id != ovr->plane_id) 1260 continue; 1261 1262 if (!format_support(ovr, p->fourcc)) 1263 continue; 1264 1265 if ((ovr->possible_crtcs & crtc_mask) && 1266 (ovr->crtc_id == 0 || ovr->crtc_id == p->crtc_id)) { 1267 plane_id = ovr->plane_id; 1268 break; 1269 } 1270 } 1271 1272 if (i == dev->resources->count_planes) { 1273 fprintf(stderr, "no unused plane available for CRTC %u\n", 1274 p->crtc_id); 1275 return -1; 1276 } 1277 1278 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n", 1279 p->w, p->h, p->format_str, plane_id); 1280 1281 /* just use single plane format for now.. */ 1282 if (bo_fb_create(dev->fd, p->fourcc, p->w, p->h, 1283 secondary_fill, &p->bo, &p->fb_id)) 1284 return -1; 1285 1286 crtc_w = p->w * p->scale; 1287 crtc_h = p->h * p->scale; 1288 if (!p->has_position) { 1289 /* Default to the middle of the screen */ 1290 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2; 1291 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2; 1292 } else { 1293 crtc_x = p->x; 1294 crtc_y = p->y; 1295 } 1296 1297 /* note src coords (last 4 args) are in Q16 format */ 1298 if (drmModeSetPlane(dev->fd, plane_id, p->crtc_id, p->fb_id, 1299 0, crtc_x, crtc_y, crtc_w, crtc_h, 1300 0, 0, p->w << 16, p->h << 16)) { 1301 fprintf(stderr, "failed to enable plane: %s\n", 1302 strerror(errno)); 1303 return -1; 1304 } 1305 1306 ovr->crtc_id = p->crtc_id; 1307 1308 return 0; 1309} 1310 1311static void atomic_set_planes(struct device *dev, struct plane_arg *p, 1312 unsigned int count, bool update) 1313{ 1314 unsigned int i, pattern = primary_fill; 1315 1316 /* set up planes */ 1317 for (i = 0; i < count; i++) { 1318 if (i > 0) 1319 pattern = secondary_fill; 1320 else 1321 set_gamma(dev, p[i].crtc_id, p[i].fourcc); 1322 1323 if (atomic_set_plane(dev, &p[i], pattern, update)) 1324 return; 1325 } 1326} 1327 1328static void 1329atomic_test_page_flip(struct device *dev, struct pipe_arg *pipe_args, 1330 struct plane_arg *plane_args, unsigned int plane_count) 1331{ 1332 int ret; 1333 1334 gettimeofday(&pipe_args->start, NULL); 1335 pipe_args->swap_count = 0; 1336 1337 while (true) { 1338 drmModeAtomicFree(dev->req); 1339 dev->req = drmModeAtomicAlloc(); 1340 atomic_set_planes(dev, plane_args, plane_count, true); 1341 1342 ret = drmModeAtomicCommit(dev->fd, dev->req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL); 1343 if (ret) { 1344 fprintf(stderr, "Atomic Commit failed [2]\n"); 1345 return; 1346 } 1347 1348 pipe_args->swap_count++; 1349 if (pipe_args->swap_count == 60) { 1350 struct timeval end; 1351 double t; 1352 1353 gettimeofday(&end, NULL); 1354 t = end.tv_sec + end.tv_usec * 1e-6 - 1355 (pipe_args->start.tv_sec + pipe_args->start.tv_usec * 1e-6); 1356 fprintf(stderr, "freq: %.02fHz\n", pipe_args->swap_count / t); 1357 pipe_args->swap_count = 0; 1358 pipe_args->start = end; 1359 } 1360 } 1361} 1362 1363static void atomic_clear_planes(struct device *dev, struct plane_arg *p, unsigned int count) 1364{ 1365 unsigned int i; 1366 1367 for (i = 0; i < count; i++) { 1368 add_property(dev, p[i].plane_id, "FB_ID", 0); 1369 add_property(dev, p[i].plane_id, "CRTC_ID", 0); 1370 add_property(dev, p[i].plane_id, "SRC_X", 0); 1371 add_property(dev, p[i].plane_id, "SRC_Y", 0); 1372 add_property(dev, p[i].plane_id, "SRC_W", 0); 1373 add_property(dev, p[i].plane_id, "SRC_H", 0); 1374 add_property(dev, p[i].plane_id, "CRTC_X", 0); 1375 add_property(dev, p[i].plane_id, "CRTC_Y", 0); 1376 add_property(dev, p[i].plane_id, "CRTC_W", 0); 1377 add_property(dev, p[i].plane_id, "CRTC_H", 0); 1378 } 1379} 1380 1381static void atomic_clear_FB(struct device *dev, struct plane_arg *p, unsigned int count) 1382{ 1383 unsigned int i; 1384 1385 for (i = 0; i < count; i++) { 1386 if (p[i].fb_id) { 1387 drmModeRmFB(dev->fd, p[i].fb_id); 1388 p[i].fb_id = 0; 1389 } 1390 if (p[i].old_fb_id) { 1391 drmModeRmFB(dev->fd, p[i].old_fb_id); 1392 p[i].old_fb_id = 0; 1393 } 1394 if (p[i].bo) { 1395 bo_destroy(p[i].bo); 1396 p[i].bo = NULL; 1397 } 1398 if (p[i].old_bo) { 1399 bo_destroy(p[i].old_bo); 1400 p[i].old_bo = NULL; 1401 } 1402 1403 } 1404} 1405 1406static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count) 1407{ 1408 unsigned int i; 1409 1410 for (i = 0; i < count; i++) { 1411 if (p[i].fb_id) 1412 drmModeRmFB(dev->fd, p[i].fb_id); 1413 if (p[i].bo) 1414 bo_destroy(p[i].bo); 1415 } 1416} 1417 1418static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe) 1419{ 1420 drmModeConnector *connector; 1421 unsigned int i; 1422 uint32_t id; 1423 char *endp; 1424 1425 for (i = 0; i < pipe->num_cons; i++) { 1426 id = strtoul(pipe->cons[i], &endp, 10); 1427 if (endp == pipe->cons[i]) { 1428 connector = get_connector_by_name(dev, pipe->cons[i]); 1429 if (!connector) { 1430 fprintf(stderr, "no connector named '%s'\n", 1431 pipe->cons[i]); 1432 return -ENODEV; 1433 } 1434 1435 id = connector->connector_id; 1436 } 1437 1438 pipe->con_ids[i] = id; 1439 } 1440 1441 return 0; 1442} 1443 1444static int pipe_attempt_connector(struct device *dev, drmModeConnector *con, 1445 struct pipe_arg *pipe) 1446{ 1447 char *con_str; 1448 int i; 1449 1450 con_str = calloc(8, sizeof(char)); 1451 if (!con_str) 1452 return -1; 1453 1454 sprintf(con_str, "%d", con->connector_id); 1455 strcpy(pipe->format_str, "XR24"); 1456 pipe->fourcc = util_format_fourcc(pipe->format_str); 1457 pipe->num_cons = 1; 1458 pipe->con_ids = calloc(1, sizeof(*pipe->con_ids)); 1459 pipe->cons = calloc(1, sizeof(*pipe->cons)); 1460 1461 if (!pipe->con_ids || !pipe->cons) 1462 goto free_con_str; 1463 1464 pipe->con_ids[0] = con->connector_id; 1465 pipe->cons[0] = (const char*)con_str; 1466 1467 pipe->crtc = pipe_find_crtc(dev, pipe); 1468 if (!pipe->crtc) 1469 goto free_all; 1470 1471 pipe->crtc_id = pipe->crtc->crtc->crtc_id; 1472 1473 /* Return the first mode if no preferred. */ 1474 pipe->mode = &con->modes[0]; 1475 1476 for (i = 0; i < con->count_modes; i++) { 1477 drmModeModeInfo *current_mode = &con->modes[i]; 1478 1479 if (current_mode->type & DRM_MODE_TYPE_PREFERRED) { 1480 pipe->mode = current_mode; 1481 break; 1482 } 1483 } 1484 1485 sprintf(pipe->mode_str, "%dx%d", pipe->mode->hdisplay, pipe->mode->vdisplay); 1486 1487 return 0; 1488 1489free_all: 1490 free(pipe->cons); 1491 free(pipe->con_ids); 1492free_con_str: 1493 free(con_str); 1494 return -1; 1495} 1496 1497static int pipe_find_preferred(struct device *dev, struct pipe_arg **out_pipes) 1498{ 1499 struct pipe_arg *pipes; 1500 struct resources *res = dev->resources; 1501 drmModeConnector *con = NULL; 1502 int i, connected = 0, attempted = 0; 1503 1504 for (i = 0; i < res->count_connectors; i++) { 1505 con = res->connectors[i].connector; 1506 if (!con || con->connection != DRM_MODE_CONNECTED) 1507 continue; 1508 connected++; 1509 } 1510 if (!connected) { 1511 printf("no connected connector!\n"); 1512 return 0; 1513 } 1514 1515 pipes = calloc(connected, sizeof(struct pipe_arg)); 1516 if (!pipes) 1517 return 0; 1518 1519 for (i = 0; i < res->count_connectors && attempted < connected; i++) { 1520 con = res->connectors[i].connector; 1521 if (!con || con->connection != DRM_MODE_CONNECTED) 1522 continue; 1523 1524 if (pipe_attempt_connector(dev, con, &pipes[attempted]) < 0) { 1525 printf("failed fetching preferred mode for connector\n"); 1526 continue; 1527 } 1528 attempted++; 1529 } 1530 1531 *out_pipes = pipes; 1532 return attempted; 1533} 1534 1535static struct plane *get_primary_plane_by_crtc(struct device *dev, struct crtc *crtc) 1536{ 1537 unsigned int i; 1538 1539 for (i = 0; i < dev->resources->count_planes; i++) { 1540 struct plane *plane = &dev->resources->planes[i]; 1541 drmModePlane *ovr = plane->plane; 1542 if (!ovr) 1543 continue; 1544 1545 // XXX: add is_primary_plane and (?) format checks 1546 1547 if (ovr->possible_crtcs & get_crtc_mask(dev, crtc)) 1548 return plane; 1549 } 1550 return NULL; 1551} 1552 1553static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count) 1554{ 1555 unsigned int i, j; 1556 int ret, x = 0; 1557 int preferred = count == 0; 1558 1559 for (i = 0; i < count; i++) { 1560 struct pipe_arg *pipe = &pipes[i]; 1561 1562 ret = pipe_resolve_connectors(dev, pipe); 1563 if (ret < 0) 1564 return; 1565 1566 ret = pipe_find_crtc_and_mode(dev, pipe); 1567 if (ret < 0) 1568 continue; 1569 } 1570 if (preferred) { 1571 struct pipe_arg *pipe_args; 1572 1573 count = pipe_find_preferred(dev, &pipe_args); 1574 if (!count) { 1575 fprintf(stderr, "can't find any preferred connector/mode.\n"); 1576 return; 1577 } 1578 pipes = pipe_args; 1579 } 1580 1581 if (!dev->use_atomic) { 1582 for (i = 0; i < count; i++) { 1583 struct pipe_arg *pipe = &pipes[i]; 1584 1585 if (pipe->mode == NULL) 1586 continue; 1587 1588 if (!preferred) { 1589 dev->mode.width += pipe->mode->hdisplay; 1590 if (dev->mode.height < pipe->mode->vdisplay) 1591 dev->mode.height = pipe->mode->vdisplay; 1592 } else { 1593 /* XXX: Use a clone mode, more like atomic. We could do per 1594 * connector bo/fb, so we don't have the stretched image. 1595 */ 1596 if (dev->mode.width < pipe->mode->hdisplay) 1597 dev->mode.width = pipe->mode->hdisplay; 1598 if (dev->mode.height < pipe->mode->vdisplay) 1599 dev->mode.height = pipe->mode->vdisplay; 1600 } 1601 } 1602 1603 if (bo_fb_create(dev->fd, pipes[0].fourcc, dev->mode.width, dev->mode.height, 1604 primary_fill, &dev->mode.bo, &dev->mode.fb_id)) 1605 return; 1606 } 1607 1608 for (i = 0; i < count; i++) { 1609 struct pipe_arg *pipe = &pipes[i]; 1610 uint32_t blob_id; 1611 1612 if (pipe->mode == NULL) 1613 continue; 1614 1615 printf("setting mode %s-%.2fHz on connectors ", 1616 pipe->mode->name, mode_vrefresh(pipe->mode)); 1617 for (j = 0; j < pipe->num_cons; ++j) { 1618 printf("%s, ", pipe->cons[j]); 1619 if (dev->use_atomic) 1620 add_property(dev, pipe->con_ids[j], "CRTC_ID", pipe->crtc_id); 1621 } 1622 printf("crtc %d\n", pipe->crtc_id); 1623 1624 if (!dev->use_atomic) { 1625 ret = drmModeSetCrtc(dev->fd, pipe->crtc_id, dev->mode.fb_id, 1626 x, 0, pipe->con_ids, pipe->num_cons, 1627 pipe->mode); 1628 1629 /* XXX: Actually check if this is needed */ 1630 drmModeDirtyFB(dev->fd, dev->mode.fb_id, NULL, 0); 1631 1632 if (!preferred) 1633 x += pipe->mode->hdisplay; 1634 1635 if (ret) { 1636 fprintf(stderr, "failed to set mode: %s\n", strerror(errno)); 1637 return; 1638 } 1639 1640 set_gamma(dev, pipe->crtc_id, pipe->fourcc); 1641 } else { 1642 drmModeCreatePropertyBlob(dev->fd, pipe->mode, sizeof(*pipe->mode), &blob_id); 1643 add_property(dev, pipe->crtc_id, "MODE_ID", blob_id); 1644 add_property(dev, pipe->crtc_id, "ACTIVE", 1); 1645 1646 /* By default atomic modeset does not set a primary plane, shrug */ 1647 if (preferred) { 1648 struct plane *plane = get_primary_plane_by_crtc(dev, pipe->crtc); 1649 struct plane_arg plane_args = { 1650 .plane_id = plane->plane->plane_id, 1651 .crtc_id = pipe->crtc_id, 1652 .w = pipe->mode->hdisplay, 1653 .h = pipe->mode->vdisplay, 1654 .scale = 1.0, 1655 .format_str = "XR24", 1656 .fourcc = util_format_fourcc(pipe->format_str), 1657 }; 1658 1659 atomic_set_planes(dev, &plane_args, 1, false); 1660 } 1661 } 1662 } 1663} 1664 1665static void atomic_clear_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count) 1666{ 1667 unsigned int i; 1668 unsigned int j; 1669 1670 for (i = 0; i < count; i++) { 1671 struct pipe_arg *pipe = &pipes[i]; 1672 1673 if (pipe->mode == NULL) 1674 continue; 1675 1676 for (j = 0; j < pipe->num_cons; ++j) 1677 add_property(dev, pipe->con_ids[j], "CRTC_ID",0); 1678 1679 add_property(dev, pipe->crtc_id, "MODE_ID", 0); 1680 add_property(dev, pipe->crtc_id, "ACTIVE", 0); 1681 } 1682} 1683 1684static void clear_mode(struct device *dev) 1685{ 1686 if (dev->mode.fb_id) 1687 drmModeRmFB(dev->fd, dev->mode.fb_id); 1688 if (dev->mode.bo) 1689 bo_destroy(dev->mode.bo); 1690} 1691 1692static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count) 1693{ 1694 unsigned int i; 1695 1696 /* set up planes/overlays */ 1697 for (i = 0; i < count; i++) 1698 if (set_plane(dev, &p[i])) 1699 return; 1700} 1701 1702static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count) 1703{ 1704 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0}; 1705 uint32_t cw = 64; 1706 uint32_t ch = 64; 1707 struct bo *bo; 1708 uint64_t value; 1709 unsigned int i; 1710 int ret; 1711 1712 ret = drmGetCap(dev->fd, DRM_CAP_CURSOR_WIDTH, &value); 1713 if (!ret) 1714 cw = value; 1715 1716 ret = drmGetCap(dev->fd, DRM_CAP_CURSOR_HEIGHT, &value); 1717 if (!ret) 1718 ch = value; 1719 1720 1721 /* create cursor bo.. just using PATTERN_PLAIN as it has 1722 * translucent alpha 1723 */ 1724 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches, 1725 offsets, UTIL_PATTERN_PLAIN); 1726 if (bo == NULL) 1727 return; 1728 1729 dev->mode.cursor_bo = bo; 1730 1731 for (i = 0; i < count; i++) { 1732 struct pipe_arg *pipe = &pipes[i]; 1733 ret = cursor_init(dev->fd, handles[0], 1734 pipe->crtc_id, 1735 pipe->mode->hdisplay, pipe->mode->vdisplay, 1736 cw, ch); 1737 if (ret) { 1738 fprintf(stderr, "failed to init cursor for CRTC[%u]\n", 1739 pipe->crtc_id); 1740 return; 1741 } 1742 } 1743 1744 cursor_start(); 1745} 1746 1747static void clear_cursors(struct device *dev) 1748{ 1749 cursor_stop(); 1750 1751 if (dev->mode.cursor_bo) 1752 bo_destroy(dev->mode.cursor_bo); 1753} 1754 1755static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count) 1756{ 1757 unsigned int other_fb_id; 1758 struct bo *other_bo; 1759 drmEventContext evctx; 1760 unsigned int i; 1761 int ret; 1762 1763 if (bo_fb_create(dev->fd, pipes[0].fourcc, dev->mode.width, dev->mode.height, 1764 UTIL_PATTERN_PLAIN, &other_bo, &other_fb_id)) 1765 return; 1766 1767 for (i = 0; i < count; i++) { 1768 struct pipe_arg *pipe = &pipes[i]; 1769 1770 if (pipe->mode == NULL) 1771 continue; 1772 1773 ret = drmModePageFlip(dev->fd, pipe->crtc_id, 1774 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT, 1775 pipe); 1776 if (ret) { 1777 fprintf(stderr, "failed to page flip: %s\n", strerror(errno)); 1778 goto err_rmfb; 1779 } 1780 gettimeofday(&pipe->start, NULL); 1781 pipe->swap_count = 0; 1782 pipe->fb_id[0] = dev->mode.fb_id; 1783 pipe->fb_id[1] = other_fb_id; 1784 pipe->current_fb_id = other_fb_id; 1785 } 1786 1787 memset(&evctx, 0, sizeof evctx); 1788 evctx.version = DRM_EVENT_CONTEXT_VERSION; 1789 evctx.vblank_handler = NULL; 1790 evctx.page_flip_handler = page_flip_handler; 1791 1792 while (1) { 1793#if 0 1794 struct pollfd pfd[2]; 1795 1796 pfd[0].fd = 0; 1797 pfd[0].events = POLLIN; 1798 pfd[1].fd = fd; 1799 pfd[1].events = POLLIN; 1800 1801 if (poll(pfd, 2, -1) < 0) { 1802 fprintf(stderr, "poll error\n"); 1803 break; 1804 } 1805 1806 if (pfd[0].revents) 1807 break; 1808#else 1809 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 }; 1810 fd_set fds; 1811 1812 FD_ZERO(&fds); 1813 FD_SET(0, &fds); 1814 FD_SET(dev->fd, &fds); 1815 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout); 1816 1817 if (ret <= 0) { 1818 fprintf(stderr, "select timed out or error (ret %d)\n", 1819 ret); 1820 continue; 1821 } else if (FD_ISSET(0, &fds)) { 1822 break; 1823 } 1824#endif 1825 1826 drmHandleEvent(dev->fd, &evctx); 1827 } 1828 1829err_rmfb: 1830 drmModeRmFB(dev->fd, other_fb_id); 1831 bo_destroy(other_bo); 1832} 1833 1834#define min(a, b) ((a) < (b) ? (a) : (b)) 1835 1836static int parse_connector(struct pipe_arg *pipe, const char *arg) 1837{ 1838 unsigned int len; 1839 unsigned int i; 1840 const char *p; 1841 char *endp; 1842 1843 pipe->vrefresh = 0; 1844 pipe->crtc_id = (uint32_t)-1; 1845 strcpy(pipe->format_str, "XR24"); 1846 1847 /* Count the number of connectors and allocate them. */ 1848 pipe->num_cons = 1; 1849 for (p = arg; *p && *p != ':' && *p != '@'; ++p) { 1850 if (*p == ',') 1851 pipe->num_cons++; 1852 } 1853 1854 pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids)); 1855 pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons)); 1856 if (pipe->con_ids == NULL || pipe->cons == NULL) 1857 return -1; 1858 1859 /* Parse the connectors. */ 1860 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) { 1861 endp = strpbrk(p, ",@:"); 1862 if (!endp) 1863 break; 1864 1865 pipe->cons[i] = strndup(p, endp - p); 1866 1867 if (*endp != ',') 1868 break; 1869 } 1870 1871 if (i != pipe->num_cons - 1) 1872 return -1; 1873 1874 /* Parse the remaining parameters. */ 1875 if (!endp) 1876 return -1; 1877 if (*endp == '@') { 1878 arg = endp + 1; 1879 pipe->crtc_id = strtoul(arg, &endp, 10); 1880 } 1881 if (*endp != ':') 1882 return -1; 1883 1884 arg = endp + 1; 1885 1886 /* Search for the vertical refresh or the format. */ 1887 p = strpbrk(arg, "-@"); 1888 if (p == NULL) 1889 p = arg + strlen(arg); 1890 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg)); 1891 strncpy(pipe->mode_str, arg, len); 1892 pipe->mode_str[len] = '\0'; 1893 1894 if (*p == '-') { 1895 pipe->vrefresh = strtof(p + 1, &endp); 1896 p = endp; 1897 } 1898 1899 if (*p == '@') { 1900 strncpy(pipe->format_str, p + 1, 4); 1901 pipe->format_str[4] = '\0'; 1902 } 1903 1904 pipe->fourcc = util_format_fourcc(pipe->format_str); 1905 if (pipe->fourcc == 0) { 1906 fprintf(stderr, "unknown format %s\n", pipe->format_str); 1907 return -1; 1908 } 1909 1910 return 0; 1911} 1912 1913static int parse_plane(struct plane_arg *plane, const char *p) 1914{ 1915 char *end; 1916 1917 plane->plane_id = strtoul(p, &end, 10); 1918 if (*end != '@') 1919 return -EINVAL; 1920 1921 p = end + 1; 1922 plane->crtc_id = strtoul(p, &end, 10); 1923 if (*end != ':') 1924 return -EINVAL; 1925 1926 p = end + 1; 1927 plane->w = strtoul(p, &end, 10); 1928 if (*end != 'x') 1929 return -EINVAL; 1930 1931 p = end + 1; 1932 plane->h = strtoul(p, &end, 10); 1933 1934 if (*end == '+' || *end == '-') { 1935 plane->x = strtol(end, &end, 10); 1936 if (*end != '+' && *end != '-') 1937 return -EINVAL; 1938 plane->y = strtol(end, &end, 10); 1939 1940 plane->has_position = true; 1941 } 1942 1943 if (*end == '*') { 1944 p = end + 1; 1945 plane->scale = strtod(p, &end); 1946 if (plane->scale <= 0.0) 1947 return -EINVAL; 1948 } else { 1949 plane->scale = 1.0; 1950 } 1951 1952 if (*end == '@') { 1953 strncpy(plane->format_str, end + 1, 4); 1954 plane->format_str[4] = '\0'; 1955 } else { 1956 strcpy(plane->format_str, "XR24"); 1957 } 1958 1959 plane->fourcc = util_format_fourcc(plane->format_str); 1960 if (plane->fourcc == 0) { 1961 fprintf(stderr, "unknown format %s\n", plane->format_str); 1962 return -EINVAL; 1963 } 1964 1965 return 0; 1966} 1967 1968static int parse_property(struct property_arg *p, const char *arg) 1969{ 1970 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3) 1971 return -1; 1972 1973 p->obj_type = 0; 1974 p->name[DRM_PROP_NAME_LEN] = '\0'; 1975 1976 return 0; 1977} 1978 1979static void parse_fill_patterns(char *arg) 1980{ 1981 char *fill = strtok(arg, ","); 1982 if (!fill) 1983 return; 1984 primary_fill = util_pattern_enum(fill); 1985 fill = strtok(NULL, ","); 1986 if (!fill) 1987 return; 1988 secondary_fill = util_pattern_enum(fill); 1989} 1990 1991static void usage(char *name) 1992{ 1993 fprintf(stderr, "usage: %s [-acDdefMPpsCvrw]\n", name); 1994 1995 fprintf(stderr, "\n Query options:\n\n"); 1996 fprintf(stderr, "\t-c\tlist connectors\n"); 1997 fprintf(stderr, "\t-e\tlist encoders\n"); 1998 fprintf(stderr, "\t-f\tlist framebuffers\n"); 1999 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n"); 2000 2001 fprintf(stderr, "\n Test options:\n\n"); 2002 fprintf(stderr, "\t-P <plane_id>@<crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n"); 2003 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:[#<mode index>]<mode>[-<vrefresh>][@<format>]\tset a mode\n"); 2004 fprintf(stderr, "\t-C\ttest hw cursor\n"); 2005 fprintf(stderr, "\t-v\ttest vsynced page flipping\n"); 2006 fprintf(stderr, "\t-r\tset the preferred mode for all connectors\n"); 2007 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n"); 2008 fprintf(stderr, "\t-a \tuse atomic API\n"); 2009 fprintf(stderr, "\t-F pattern1,pattern2\tspecify fill patterns\n"); 2010 2011 fprintf(stderr, "\n Generic options:\n\n"); 2012 fprintf(stderr, "\t-d\tdrop master after mode set\n"); 2013 fprintf(stderr, "\t-M module\tuse the given driver\n"); 2014 fprintf(stderr, "\t-D device\tuse the given device\n"); 2015 2016 fprintf(stderr, "\n\tDefault is to dump all info.\n"); 2017 exit(0); 2018} 2019 2020static char optstr[] = "acdD:efF:M:P:ps:Cvrw:"; 2021 2022int main(int argc, char **argv) 2023{ 2024 struct device dev; 2025 2026 int c; 2027 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0; 2028 int drop_master = 0; 2029 int test_vsync = 0; 2030 int test_cursor = 0; 2031 int set_preferred = 0; 2032 int use_atomic = 0; 2033 char *device = NULL; 2034 char *module = NULL; 2035 unsigned int i; 2036 unsigned int count = 0, plane_count = 0; 2037 unsigned int prop_count = 0; 2038 struct pipe_arg *pipe_args = NULL; 2039 struct plane_arg *plane_args = NULL; 2040 struct property_arg *prop_args = NULL; 2041 unsigned int args = 0; 2042 int ret; 2043 2044 memset(&dev, 0, sizeof dev); 2045 2046 opterr = 0; 2047 while ((c = getopt(argc, argv, optstr)) != -1) { 2048 args++; 2049 2050 switch (c) { 2051 case 'a': 2052 use_atomic = 1; 2053 /* Preserve the default behaviour of dumping all information. */ 2054 args--; 2055 break; 2056 case 'c': 2057 connectors = 1; 2058 break; 2059 case 'D': 2060 device = optarg; 2061 /* Preserve the default behaviour of dumping all information. */ 2062 args--; 2063 break; 2064 case 'd': 2065 drop_master = 1; 2066 break; 2067 case 'e': 2068 encoders = 1; 2069 break; 2070 case 'f': 2071 framebuffers = 1; 2072 break; 2073 case 'F': 2074 parse_fill_patterns(optarg); 2075 break; 2076 case 'M': 2077 module = optarg; 2078 /* Preserve the default behaviour of dumping all information. */ 2079 args--; 2080 break; 2081 case 'P': 2082 plane_args = realloc(plane_args, 2083 (plane_count + 1) * sizeof *plane_args); 2084 if (plane_args == NULL) { 2085 fprintf(stderr, "memory allocation failed\n"); 2086 return 1; 2087 } 2088 memset(&plane_args[plane_count], 0, sizeof(*plane_args)); 2089 2090 if (parse_plane(&plane_args[plane_count], optarg) < 0) 2091 usage(argv[0]); 2092 2093 plane_count++; 2094 break; 2095 case 'p': 2096 crtcs = 1; 2097 planes = 1; 2098 break; 2099 case 's': 2100 pipe_args = realloc(pipe_args, 2101 (count + 1) * sizeof *pipe_args); 2102 if (pipe_args == NULL) { 2103 fprintf(stderr, "memory allocation failed\n"); 2104 return 1; 2105 } 2106 memset(&pipe_args[count], 0, sizeof(*pipe_args)); 2107 2108 if (parse_connector(&pipe_args[count], optarg) < 0) 2109 usage(argv[0]); 2110 2111 count++; 2112 break; 2113 case 'C': 2114 test_cursor = 1; 2115 break; 2116 case 'v': 2117 test_vsync = 1; 2118 break; 2119 case 'r': 2120 set_preferred = 1; 2121 break; 2122 case 'w': 2123 prop_args = realloc(prop_args, 2124 (prop_count + 1) * sizeof *prop_args); 2125 if (prop_args == NULL) { 2126 fprintf(stderr, "memory allocation failed\n"); 2127 return 1; 2128 } 2129 memset(&prop_args[prop_count], 0, sizeof(*prop_args)); 2130 2131 if (parse_property(&prop_args[prop_count], optarg) < 0) 2132 usage(argv[0]); 2133 2134 prop_count++; 2135 break; 2136 default: 2137 usage(argv[0]); 2138 break; 2139 } 2140 } 2141 2142 /* Dump all the details when no* arguments are provided. */ 2143 if (!args) 2144 encoders = connectors = crtcs = planes = framebuffers = 1; 2145 2146 if (test_vsync && !count) { 2147 fprintf(stderr, "page flipping requires at least one -s option.\n"); 2148 return -1; 2149 } 2150 if (set_preferred && count) { 2151 fprintf(stderr, "cannot use -r (preferred) when -s (mode) is set\n"); 2152 return -1; 2153 } 2154 2155 if (set_preferred && plane_count) { 2156 fprintf(stderr, "cannot use -r (preferred) when -P (plane) is set\n"); 2157 return -1; 2158 } 2159 2160 dev.fd = util_open(device, module); 2161 if (dev.fd < 0) 2162 return -1; 2163 2164 if (use_atomic) { 2165 ret = drmSetClientCap(dev.fd, DRM_CLIENT_CAP_ATOMIC, 1); 2166 if (ret) { 2167 fprintf(stderr, "no atomic modesetting support: %s\n", strerror(errno)); 2168 drmClose(dev.fd); 2169 return -1; 2170 } 2171 } 2172 2173 dev.use_atomic = use_atomic; 2174 2175 dev.resources = get_resources(&dev); 2176 if (!dev.resources) { 2177 drmClose(dev.fd); 2178 return 1; 2179 } 2180 2181#define dump_resource(dev, res) if (res) dump_##res(dev) 2182 2183 dump_resource(&dev, encoders); 2184 dump_resource(&dev, connectors); 2185 dump_resource(&dev, crtcs); 2186 dump_resource(&dev, planes); 2187 dump_resource(&dev, framebuffers); 2188 2189 for (i = 0; i < prop_count; ++i) 2190 set_property(&dev, &prop_args[i]); 2191 2192 if (dev.use_atomic) { 2193 dev.req = drmModeAtomicAlloc(); 2194 2195 if (set_preferred || (count && plane_count)) { 2196 uint64_t cap = 0; 2197 2198 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap); 2199 if (ret || cap == 0) { 2200 fprintf(stderr, "driver doesn't support the dumb buffer API\n"); 2201 return 1; 2202 } 2203 2204 if (set_preferred || count) 2205 set_mode(&dev, pipe_args, count); 2206 2207 if (plane_count) 2208 atomic_set_planes(&dev, plane_args, plane_count, false); 2209 2210 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL); 2211 if (ret) { 2212 fprintf(stderr, "Atomic Commit failed [1]\n"); 2213 return 1; 2214 } 2215 2216 if (test_vsync) 2217 atomic_test_page_flip(&dev, pipe_args, plane_args, plane_count); 2218 2219 if (drop_master) 2220 drmDropMaster(dev.fd); 2221 2222 getchar(); 2223 2224 drmModeAtomicFree(dev.req); 2225 dev.req = drmModeAtomicAlloc(); 2226 2227 /* XXX: properly teardown the preferred mode/plane state */ 2228 if (plane_count) 2229 atomic_clear_planes(&dev, plane_args, plane_count); 2230 2231 if (count) 2232 atomic_clear_mode(&dev, pipe_args, count); 2233 2234 ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL); 2235 if (ret) 2236 fprintf(stderr, "Atomic Commit failed\n"); 2237 2238 if (plane_count) 2239 atomic_clear_FB(&dev, plane_args, plane_count); 2240 } 2241 2242 drmModeAtomicFree(dev.req); 2243 } else { 2244 if (set_preferred || count || plane_count) { 2245 uint64_t cap = 0; 2246 2247 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap); 2248 if (ret || cap == 0) { 2249 fprintf(stderr, "driver doesn't support the dumb buffer API\n"); 2250 return 1; 2251 } 2252 2253 if (set_preferred || count) 2254 set_mode(&dev, pipe_args, count); 2255 2256 if (plane_count) 2257 set_planes(&dev, plane_args, plane_count); 2258 2259 if (test_cursor) 2260 set_cursors(&dev, pipe_args, count); 2261 2262 if (test_vsync) 2263 test_page_flip(&dev, pipe_args, count); 2264 2265 if (drop_master) 2266 drmDropMaster(dev.fd); 2267 2268 getchar(); 2269 2270 if (test_cursor) 2271 clear_cursors(&dev); 2272 2273 if (plane_count) 2274 clear_planes(&dev, plane_args, plane_count); 2275 2276 if (set_preferred || count) 2277 clear_mode(&dev); 2278 } 2279 } 2280 2281 free_resources(dev.resources); 2282 drmClose(dev.fd); 2283 2284 return 0; 2285} 2286