Daniel Stone | fbe6c1d | 2019-06-17 16:04:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * Copyright © 2011 Intel Corporation |
| 4 | * Copyright © 2017, 2018 Collabora, Ltd. |
| 5 | * Copyright © 2017, 2018 General Electric Company |
| 6 | * Copyright (c) 2018 DisplayLink (UK) Ltd. |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining |
| 9 | * a copy of this software and associated documentation files (the |
| 10 | * "Software"), to deal in the Software without restriction, including |
| 11 | * without limitation the rights to use, copy, modify, merge, publish, |
| 12 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 13 | * permit persons to whom the Software is furnished to do so, subject to |
| 14 | * the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice (including the |
| 17 | * next paragraph) shall be included in all copies or substantial |
| 18 | * portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 24 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 25 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 26 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #include "config.h" |
| 31 | |
| 32 | #include <xf86drm.h> |
| 33 | #include <xf86drmMode.h> |
| 34 | #include <drm_fourcc.h> |
| 35 | |
| 36 | #include "drm-internal.h" |
| 37 | |
| 38 | static const char *const aspect_ratio_as_string[] = { |
| 39 | [WESTON_MODE_PIC_AR_NONE] = "", |
| 40 | [WESTON_MODE_PIC_AR_4_3] = " 4:3", |
| 41 | [WESTON_MODE_PIC_AR_16_9] = " 16:9", |
| 42 | [WESTON_MODE_PIC_AR_64_27] = " 64:27", |
| 43 | [WESTON_MODE_PIC_AR_256_135] = " 256:135", |
| 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * Get the aspect-ratio from drmModeModeInfo mode flags. |
| 48 | * |
| 49 | * @param drm_mode_flags- flags from drmModeModeInfo structure. |
| 50 | * @returns aspect-ratio as encoded in enum 'weston_mode_aspect_ratio'. |
| 51 | */ |
| 52 | static enum weston_mode_aspect_ratio |
| 53 | drm_to_weston_mode_aspect_ratio(uint32_t drm_mode_flags) |
| 54 | { |
Daniel Stone | 6093772 | 2019-11-26 10:48:12 +0000 | [diff] [blame] | 55 | switch (drm_mode_flags & DRM_MODE_FLAG_PIC_AR_MASK) { |
| 56 | case DRM_MODE_FLAG_PIC_AR_4_3: |
| 57 | return WESTON_MODE_PIC_AR_4_3; |
| 58 | case DRM_MODE_FLAG_PIC_AR_16_9: |
| 59 | return WESTON_MODE_PIC_AR_16_9; |
| 60 | case DRM_MODE_FLAG_PIC_AR_64_27: |
| 61 | return WESTON_MODE_PIC_AR_64_27; |
| 62 | case DRM_MODE_FLAG_PIC_AR_256_135: |
| 63 | return WESTON_MODE_PIC_AR_256_135; |
| 64 | case DRM_MODE_FLAG_PIC_AR_NONE: |
| 65 | default: |
| 66 | return WESTON_MODE_PIC_AR_NONE; |
| 67 | } |
Daniel Stone | fbe6c1d | 2019-06-17 16:04:26 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static const char * |
| 71 | aspect_ratio_to_string(enum weston_mode_aspect_ratio ratio) |
| 72 | { |
| 73 | if (ratio < 0 || ratio >= ARRAY_LENGTH(aspect_ratio_as_string) || |
| 74 | !aspect_ratio_as_string[ratio]) |
| 75 | return " (unknown aspect ratio)"; |
| 76 | |
| 77 | return aspect_ratio_as_string[ratio]; |
| 78 | } |
| 79 | |
| 80 | static int |
| 81 | drm_subpixel_to_wayland(int drm_value) |
| 82 | { |
| 83 | switch (drm_value) { |
| 84 | default: |
| 85 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 86 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 87 | case DRM_MODE_SUBPIXEL_NONE: |
| 88 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 89 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 90 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 91 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 92 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 93 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 94 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 95 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 96 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | int |
| 101 | drm_mode_ensure_blob(struct drm_backend *backend, struct drm_mode *mode) |
| 102 | { |
| 103 | int ret; |
| 104 | |
| 105 | if (mode->blob_id) |
| 106 | return 0; |
| 107 | |
| 108 | ret = drmModeCreatePropertyBlob(backend->drm.fd, |
| 109 | &mode->mode_info, |
| 110 | sizeof(mode->mode_info), |
| 111 | &mode->blob_id); |
| 112 | if (ret != 0) |
| 113 | weston_log("failed to create mode property blob: %s\n", |
| 114 | strerror(errno)); |
| 115 | |
| 116 | drm_debug(backend, "\t\t\t[atomic] created new mode blob %lu for %s\n", |
| 117 | (unsigned long) mode->blob_id, mode->mode_info.name); |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | static bool |
| 123 | check_non_desktop(struct drm_head *head, drmModeObjectPropertiesPtr props) |
| 124 | { |
| 125 | struct drm_property_info *non_desktop_info = |
| 126 | &head->props_conn[WDRM_CONNECTOR_NON_DESKTOP]; |
| 127 | |
| 128 | return drm_property_get_value(non_desktop_info, props, 0); |
| 129 | } |
| 130 | |
| 131 | static int |
| 132 | parse_modeline(const char *s, drmModeModeInfo *mode) |
| 133 | { |
| 134 | char hsync[16]; |
| 135 | char vsync[16]; |
| 136 | float fclock; |
| 137 | |
| 138 | memset(mode, 0, sizeof *mode); |
| 139 | |
| 140 | mode->type = DRM_MODE_TYPE_USERDEF; |
| 141 | mode->hskew = 0; |
| 142 | mode->vscan = 0; |
| 143 | mode->vrefresh = 0; |
| 144 | mode->flags = 0; |
| 145 | |
| 146 | if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s", |
| 147 | &fclock, |
| 148 | &mode->hdisplay, |
| 149 | &mode->hsync_start, |
| 150 | &mode->hsync_end, |
| 151 | &mode->htotal, |
| 152 | &mode->vdisplay, |
| 153 | &mode->vsync_start, |
| 154 | &mode->vsync_end, |
| 155 | &mode->vtotal, hsync, vsync) != 11) |
| 156 | return -1; |
| 157 | |
| 158 | mode->clock = fclock * 1000; |
| 159 | if (strcasecmp(hsync, "+hsync") == 0) |
| 160 | mode->flags |= DRM_MODE_FLAG_PHSYNC; |
| 161 | else if (strcasecmp(hsync, "-hsync") == 0) |
| 162 | mode->flags |= DRM_MODE_FLAG_NHSYNC; |
| 163 | else |
| 164 | return -1; |
| 165 | |
| 166 | if (strcasecmp(vsync, "+vsync") == 0) |
| 167 | mode->flags |= DRM_MODE_FLAG_PVSYNC; |
| 168 | else if (strcasecmp(vsync, "-vsync") == 0) |
| 169 | mode->flags |= DRM_MODE_FLAG_NVSYNC; |
| 170 | else |
| 171 | return -1; |
| 172 | |
| 173 | snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f", |
| 174 | mode->hdisplay, mode->vdisplay, fclock); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static void |
| 180 | edid_parse_string(const uint8_t *data, char text[]) |
| 181 | { |
| 182 | int i; |
| 183 | int replaced = 0; |
| 184 | |
| 185 | /* this is always 12 bytes, but we can't guarantee it's null |
| 186 | * terminated or not junk. */ |
| 187 | strncpy(text, (const char *) data, 12); |
| 188 | |
| 189 | /* guarantee our new string is null-terminated */ |
| 190 | text[12] = '\0'; |
| 191 | |
| 192 | /* remove insane chars */ |
| 193 | for (i = 0; text[i] != '\0'; i++) { |
| 194 | if (text[i] == '\n' || |
| 195 | text[i] == '\r') { |
| 196 | text[i] = '\0'; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* ensure string is printable */ |
| 202 | for (i = 0; text[i] != '\0'; i++) { |
| 203 | if (!isprint(text[i])) { |
| 204 | text[i] = '-'; |
| 205 | replaced++; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* if the string is random junk, ignore the string */ |
| 210 | if (replaced > 4) |
| 211 | text[0] = '\0'; |
| 212 | } |
| 213 | |
| 214 | #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe |
| 215 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc |
| 216 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff |
| 217 | #define EDID_OFFSET_DATA_BLOCKS 0x36 |
| 218 | #define EDID_OFFSET_LAST_BLOCK 0x6c |
| 219 | #define EDID_OFFSET_PNPID 0x08 |
| 220 | #define EDID_OFFSET_SERIAL 0x0c |
| 221 | |
| 222 | static int |
| 223 | edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length) |
| 224 | { |
| 225 | int i; |
| 226 | uint32_t serial_number; |
| 227 | |
| 228 | /* check header */ |
| 229 | if (length < 128) |
| 230 | return -1; |
| 231 | if (data[0] != 0x00 || data[1] != 0xff) |
| 232 | return -1; |
| 233 | |
| 234 | /* decode the PNP ID from three 5 bit words packed into 2 bytes |
| 235 | * /--08--\/--09--\ |
| 236 | * 7654321076543210 |
| 237 | * |\---/\---/\---/ |
| 238 | * R C1 C2 C3 */ |
| 239 | edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1; |
| 240 | edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1; |
| 241 | edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1; |
| 242 | edid->pnp_id[3] = '\0'; |
| 243 | |
| 244 | /* maybe there isn't a ASCII serial number descriptor, so use this instead */ |
| 245 | serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0]; |
| 246 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100; |
| 247 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000; |
| 248 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000; |
| 249 | if (serial_number > 0) |
| 250 | sprintf(edid->serial_number, "%lu", (unsigned long) serial_number); |
| 251 | |
| 252 | /* parse EDID data */ |
| 253 | for (i = EDID_OFFSET_DATA_BLOCKS; |
| 254 | i <= EDID_OFFSET_LAST_BLOCK; |
| 255 | i += 18) { |
| 256 | /* ignore pixel clock data */ |
| 257 | if (data[i] != 0) |
| 258 | continue; |
| 259 | if (data[i+2] != 0) |
| 260 | continue; |
| 261 | |
| 262 | /* any useful blocks? */ |
| 263 | if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) { |
| 264 | edid_parse_string(&data[i+5], |
| 265 | edid->monitor_name); |
| 266 | } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) { |
| 267 | edid_parse_string(&data[i+5], |
| 268 | edid->serial_number); |
| 269 | } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) { |
| 270 | edid_parse_string(&data[i+5], |
| 271 | edid->eisa_id); |
| 272 | } |
| 273 | } |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /** Parse monitor make, model and serial from EDID |
| 278 | * |
| 279 | * \param head The head whose \c drm_edid to fill in. |
| 280 | * \param props The DRM connector properties to get the EDID from. |
| 281 | * \param[out] make The monitor make (PNP ID). |
| 282 | * \param[out] model The monitor model (name). |
| 283 | * \param[out] serial_number The monitor serial number. |
| 284 | * |
| 285 | * Each of \c *make, \c *model and \c *serial_number are set only if the |
| 286 | * information is found in the EDID. The pointers they are set to must not |
| 287 | * be free()'d explicitly, instead they get implicitly freed when the |
| 288 | * \c drm_head is destroyed. |
| 289 | */ |
| 290 | static void |
| 291 | find_and_parse_output_edid(struct drm_head *head, |
| 292 | drmModeObjectPropertiesPtr props, |
| 293 | const char **make, |
| 294 | const char **model, |
| 295 | const char **serial_number) |
| 296 | { |
| 297 | drmModePropertyBlobPtr edid_blob = NULL; |
| 298 | uint32_t blob_id; |
| 299 | int rc; |
| 300 | |
| 301 | blob_id = |
| 302 | drm_property_get_value(&head->props_conn[WDRM_CONNECTOR_EDID], |
| 303 | props, 0); |
| 304 | if (!blob_id) |
| 305 | return; |
| 306 | |
| 307 | edid_blob = drmModeGetPropertyBlob(head->backend->drm.fd, blob_id); |
| 308 | if (!edid_blob) |
| 309 | return; |
| 310 | |
| 311 | rc = edid_parse(&head->edid, |
| 312 | edid_blob->data, |
| 313 | edid_blob->length); |
| 314 | if (!rc) { |
| 315 | if (head->edid.pnp_id[0] != '\0') |
| 316 | *make = head->edid.pnp_id; |
| 317 | if (head->edid.monitor_name[0] != '\0') |
| 318 | *model = head->edid.monitor_name; |
| 319 | if (head->edid.serial_number[0] != '\0') |
| 320 | *serial_number = head->edid.serial_number; |
| 321 | } |
| 322 | drmModeFreePropertyBlob(edid_blob); |
| 323 | } |
| 324 | |
| 325 | static uint32_t |
| 326 | drm_refresh_rate_mHz(const drmModeModeInfo *info) |
| 327 | { |
| 328 | uint64_t refresh; |
| 329 | |
| 330 | /* Calculate higher precision (mHz) refresh rate */ |
| 331 | refresh = (info->clock * 1000000LL / info->htotal + |
| 332 | info->vtotal / 2) / info->vtotal; |
| 333 | |
| 334 | if (info->flags & DRM_MODE_FLAG_INTERLACE) |
| 335 | refresh *= 2; |
| 336 | if (info->flags & DRM_MODE_FLAG_DBLSCAN) |
| 337 | refresh /= 2; |
| 338 | if (info->vscan > 1) |
| 339 | refresh /= info->vscan; |
| 340 | |
| 341 | return refresh; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Add a mode to output's mode list |
| 346 | * |
| 347 | * Copy the supplied DRM mode into a Weston mode structure, and add it to the |
| 348 | * output's mode list. |
| 349 | * |
| 350 | * @param output DRM output to add mode to |
| 351 | * @param info DRM mode structure to add |
| 352 | * @returns Newly-allocated Weston/DRM mode structure |
| 353 | */ |
| 354 | static struct drm_mode * |
| 355 | drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info) |
| 356 | { |
| 357 | struct drm_mode *mode; |
| 358 | |
| 359 | mode = malloc(sizeof *mode); |
| 360 | if (mode == NULL) |
| 361 | return NULL; |
| 362 | |
| 363 | mode->base.flags = 0; |
| 364 | mode->base.width = info->hdisplay; |
| 365 | mode->base.height = info->vdisplay; |
| 366 | |
| 367 | mode->base.refresh = drm_refresh_rate_mHz(info); |
| 368 | mode->mode_info = *info; |
| 369 | mode->blob_id = 0; |
| 370 | |
| 371 | if (info->type & DRM_MODE_TYPE_PREFERRED) |
| 372 | mode->base.flags |= WL_OUTPUT_MODE_PREFERRED; |
| 373 | |
| 374 | mode->base.aspect_ratio = drm_to_weston_mode_aspect_ratio(info->flags); |
| 375 | |
| 376 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 377 | |
| 378 | return mode; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Destroys a mode, and removes it from the list. |
| 383 | */ |
| 384 | static void |
| 385 | drm_output_destroy_mode(struct drm_backend *backend, struct drm_mode *mode) |
| 386 | { |
| 387 | if (mode->blob_id) |
| 388 | drmModeDestroyPropertyBlob(backend->drm.fd, mode->blob_id); |
| 389 | wl_list_remove(&mode->base.link); |
| 390 | free(mode); |
| 391 | } |
| 392 | |
| 393 | /** Destroy a list of drm_modes |
| 394 | * |
| 395 | * @param backend The backend for releasing mode property blobs. |
| 396 | * @param mode_list The list linked by drm_mode::base.link. |
| 397 | */ |
| 398 | void |
| 399 | drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list) |
| 400 | { |
| 401 | struct drm_mode *mode, *next; |
| 402 | |
| 403 | wl_list_for_each_safe(mode, next, mode_list, base.link) |
| 404 | drm_output_destroy_mode(backend, mode); |
| 405 | } |
| 406 | |
| 407 | void |
| 408 | drm_output_print_modes(struct drm_output *output) |
| 409 | { |
| 410 | struct weston_mode *m; |
| 411 | struct drm_mode *dm; |
| 412 | const char *aspect_ratio; |
| 413 | |
| 414 | wl_list_for_each(m, &output->base.mode_list, link) { |
| 415 | dm = to_drm_mode(m); |
| 416 | |
| 417 | aspect_ratio = aspect_ratio_to_string(m->aspect_ratio); |
| 418 | weston_log_continue(STAMP_SPACE "%dx%d@%.1f%s%s%s, %.1f MHz\n", |
| 419 | m->width, m->height, m->refresh / 1000.0, |
| 420 | aspect_ratio, |
| 421 | m->flags & WL_OUTPUT_MODE_PREFERRED ? |
| 422 | ", preferred" : "", |
| 423 | m->flags & WL_OUTPUT_MODE_CURRENT ? |
| 424 | ", current" : "", |
| 425 | dm->mode_info.clock / 1000.0); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | |
| 430 | /** |
| 431 | * Find the closest-matching mode for a given target |
| 432 | * |
| 433 | * Given a target mode, find the most suitable mode amongst the output's |
| 434 | * current mode list to use, preferring the current mode if possible, to |
| 435 | * avoid an expensive mode switch. |
| 436 | * |
| 437 | * @param output DRM output |
| 438 | * @param target_mode Mode to attempt to match |
| 439 | * @returns Pointer to a mode from the output's mode list |
| 440 | */ |
| 441 | struct drm_mode * |
| 442 | drm_output_choose_mode(struct drm_output *output, |
| 443 | struct weston_mode *target_mode) |
| 444 | { |
| 445 | struct drm_mode *tmp_mode = NULL, *mode_fall_back = NULL, *mode; |
| 446 | enum weston_mode_aspect_ratio src_aspect = WESTON_MODE_PIC_AR_NONE; |
| 447 | enum weston_mode_aspect_ratio target_aspect = WESTON_MODE_PIC_AR_NONE; |
| 448 | struct drm_backend *b; |
| 449 | |
| 450 | b = to_drm_backend(output->base.compositor); |
| 451 | target_aspect = target_mode->aspect_ratio; |
| 452 | src_aspect = output->base.current_mode->aspect_ratio; |
| 453 | if (output->base.current_mode->width == target_mode->width && |
| 454 | output->base.current_mode->height == target_mode->height && |
| 455 | (output->base.current_mode->refresh == target_mode->refresh || |
| 456 | target_mode->refresh == 0)) { |
| 457 | if (!b->aspect_ratio_supported || src_aspect == target_aspect) |
| 458 | return to_drm_mode(output->base.current_mode); |
| 459 | } |
| 460 | |
| 461 | wl_list_for_each(mode, &output->base.mode_list, base.link) { |
| 462 | |
| 463 | src_aspect = mode->base.aspect_ratio; |
| 464 | if (mode->mode_info.hdisplay == target_mode->width && |
| 465 | mode->mode_info.vdisplay == target_mode->height) { |
| 466 | if (mode->base.refresh == target_mode->refresh || |
| 467 | target_mode->refresh == 0) { |
| 468 | if (!b->aspect_ratio_supported || |
| 469 | src_aspect == target_aspect) |
| 470 | return mode; |
| 471 | else if (!mode_fall_back) |
| 472 | mode_fall_back = mode; |
| 473 | } else if (!tmp_mode) { |
| 474 | tmp_mode = mode; |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | if (mode_fall_back) |
| 480 | return mode_fall_back; |
| 481 | |
| 482 | return tmp_mode; |
| 483 | } |
| 484 | |
| 485 | void |
| 486 | update_head_from_connector(struct drm_head *head, |
| 487 | drmModeObjectProperties *props) |
| 488 | { |
| 489 | const char *make = "unknown"; |
| 490 | const char *model = "unknown"; |
| 491 | const char *serial_number = "unknown"; |
| 492 | |
| 493 | find_and_parse_output_edid(head, props, &make, &model, &serial_number); |
| 494 | weston_head_set_monitor_strings(&head->base, make, model, serial_number); |
| 495 | weston_head_set_non_desktop(&head->base, |
| 496 | check_non_desktop(head, props)); |
| 497 | weston_head_set_subpixel(&head->base, |
| 498 | drm_subpixel_to_wayland(head->connector->subpixel)); |
| 499 | |
| 500 | weston_head_set_physical_size(&head->base, head->connector->mmWidth, |
| 501 | head->connector->mmHeight); |
| 502 | |
| 503 | /* Unknown connection status is assumed disconnected. */ |
| 504 | weston_head_set_connection_status(&head->base, |
| 505 | head->connector->connection == DRM_MODE_CONNECTED); |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Choose suitable mode for an output |
| 510 | * |
| 511 | * Find the most suitable mode to use for initial setup (or reconfiguration on |
| 512 | * hotplug etc) for a DRM output. |
| 513 | * |
| 514 | * @param backend the DRM backend |
| 515 | * @param output DRM output to choose mode for |
| 516 | * @param mode Strategy and preference to use when choosing mode |
| 517 | * @param modeline Manually-entered mode (may be NULL) |
| 518 | * @param current_mode Mode currently being displayed on this output |
| 519 | * @returns A mode from the output's mode list, or NULL if none available |
| 520 | */ |
| 521 | static struct drm_mode * |
| 522 | drm_output_choose_initial_mode(struct drm_backend *backend, |
| 523 | struct drm_output *output, |
| 524 | enum weston_drm_backend_output_mode mode, |
| 525 | const char *modeline, |
| 526 | const drmModeModeInfo *current_mode) |
| 527 | { |
| 528 | struct drm_mode *preferred = NULL; |
| 529 | struct drm_mode *current = NULL; |
| 530 | struct drm_mode *configured = NULL; |
| 531 | struct drm_mode *config_fall_back = NULL; |
| 532 | struct drm_mode *best = NULL; |
| 533 | struct drm_mode *drm_mode; |
| 534 | drmModeModeInfo drm_modeline; |
| 535 | int32_t width = 0; |
| 536 | int32_t height = 0; |
| 537 | uint32_t refresh = 0; |
| 538 | uint32_t aspect_width = 0; |
| 539 | uint32_t aspect_height = 0; |
| 540 | enum weston_mode_aspect_ratio aspect_ratio = WESTON_MODE_PIC_AR_NONE; |
| 541 | int n; |
| 542 | |
| 543 | if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) { |
| 544 | n = sscanf(modeline, "%dx%d@%d %u:%u", &width, &height, |
| 545 | &refresh, &aspect_width, &aspect_height); |
| 546 | if (backend->aspect_ratio_supported && n == 5) { |
| 547 | if (aspect_width == 4 && aspect_height == 3) |
| 548 | aspect_ratio = WESTON_MODE_PIC_AR_4_3; |
| 549 | else if (aspect_width == 16 && aspect_height == 9) |
| 550 | aspect_ratio = WESTON_MODE_PIC_AR_16_9; |
| 551 | else if (aspect_width == 64 && aspect_height == 27) |
| 552 | aspect_ratio = WESTON_MODE_PIC_AR_64_27; |
| 553 | else if (aspect_width == 256 && aspect_height == 135) |
| 554 | aspect_ratio = WESTON_MODE_PIC_AR_256_135; |
| 555 | else |
| 556 | weston_log("Invalid modeline \"%s\" for output %s\n", |
| 557 | modeline, output->base.name); |
| 558 | } |
| 559 | if (n != 2 && n != 3 && n != 5) { |
| 560 | width = -1; |
| 561 | |
| 562 | if (parse_modeline(modeline, &drm_modeline) == 0) { |
| 563 | configured = drm_output_add_mode(output, &drm_modeline); |
| 564 | if (!configured) |
| 565 | return NULL; |
| 566 | } else { |
| 567 | weston_log("Invalid modeline \"%s\" for output %s\n", |
| 568 | modeline, output->base.name); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) { |
| 574 | if (width == drm_mode->base.width && |
| 575 | height == drm_mode->base.height && |
| 576 | (refresh == 0 || refresh == drm_mode->mode_info.vrefresh)) { |
| 577 | if (!backend->aspect_ratio_supported || |
| 578 | aspect_ratio == drm_mode->base.aspect_ratio) |
| 579 | configured = drm_mode; |
| 580 | else |
| 581 | config_fall_back = drm_mode; |
| 582 | } |
| 583 | |
| 584 | if (memcmp(current_mode, &drm_mode->mode_info, |
| 585 | sizeof *current_mode) == 0) |
| 586 | current = drm_mode; |
| 587 | |
| 588 | if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED) |
| 589 | preferred = drm_mode; |
| 590 | |
| 591 | best = drm_mode; |
| 592 | } |
| 593 | |
| 594 | if (current == NULL && current_mode->clock != 0) { |
| 595 | current = drm_output_add_mode(output, current_mode); |
| 596 | if (!current) |
| 597 | return NULL; |
| 598 | } |
| 599 | |
| 600 | if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT) |
| 601 | configured = current; |
| 602 | |
| 603 | if (configured) |
| 604 | return configured; |
| 605 | |
| 606 | if (config_fall_back) |
| 607 | return config_fall_back; |
| 608 | |
| 609 | if (preferred) |
| 610 | return preferred; |
| 611 | |
| 612 | if (current) |
| 613 | return current; |
| 614 | |
| 615 | if (best) |
| 616 | return best; |
| 617 | |
| 618 | weston_log("no available modes for %s\n", output->base.name); |
| 619 | return NULL; |
| 620 | } |
| 621 | |
| 622 | static uint32_t |
| 623 | u32distance(uint32_t a, uint32_t b) |
| 624 | { |
| 625 | if (a < b) |
| 626 | return b - a; |
| 627 | else |
| 628 | return a - b; |
| 629 | } |
| 630 | |
| 631 | /** Choose equivalent mode |
| 632 | * |
| 633 | * If the two modes are not equivalent, return NULL. |
| 634 | * Otherwise return the mode that is more likely to work in place of both. |
| 635 | * |
| 636 | * None of the fuzzy matching criteria in this function have any justification. |
| 637 | * |
| 638 | * typedef struct _drmModeModeInfo { |
| 639 | * uint32_t clock; |
| 640 | * uint16_t hdisplay, hsync_start, hsync_end, htotal, hskew; |
| 641 | * uint16_t vdisplay, vsync_start, vsync_end, vtotal, vscan; |
| 642 | * |
| 643 | * uint32_t vrefresh; |
| 644 | * |
| 645 | * uint32_t flags; |
| 646 | * uint32_t type; |
| 647 | * char name[DRM_DISPLAY_MODE_LEN]; |
| 648 | * } drmModeModeInfo, *drmModeModeInfoPtr; |
| 649 | */ |
| 650 | static const drmModeModeInfo * |
| 651 | drm_mode_pick_equivalent(const drmModeModeInfo *a, const drmModeModeInfo *b) |
| 652 | { |
| 653 | uint32_t refresh_a, refresh_b; |
| 654 | |
| 655 | if (a->hdisplay != b->hdisplay || a->vdisplay != b->vdisplay) |
| 656 | return NULL; |
| 657 | |
| 658 | if (a->flags != b->flags) |
| 659 | return NULL; |
| 660 | |
| 661 | /* kHz */ |
| 662 | if (u32distance(a->clock, b->clock) > 500) |
| 663 | return NULL; |
| 664 | |
| 665 | refresh_a = drm_refresh_rate_mHz(a); |
| 666 | refresh_b = drm_refresh_rate_mHz(b); |
| 667 | if (u32distance(refresh_a, refresh_b) > 50) |
| 668 | return NULL; |
| 669 | |
| 670 | if ((a->type ^ b->type) & DRM_MODE_TYPE_PREFERRED) { |
| 671 | if (a->type & DRM_MODE_TYPE_PREFERRED) |
| 672 | return a; |
| 673 | else |
| 674 | return b; |
| 675 | } |
| 676 | |
| 677 | return a; |
| 678 | } |
| 679 | |
| 680 | /* If the given mode info is not already in the list, add it. |
| 681 | * If it is in the list, either keep the existing or replace it, |
| 682 | * depending on which one is "better". |
| 683 | */ |
| 684 | static int |
| 685 | drm_output_try_add_mode(struct drm_output *output, const drmModeModeInfo *info) |
| 686 | { |
| 687 | struct weston_mode *base; |
Scott Anderson | 60b6572 | 2020-01-28 17:18:33 +1300 | [diff] [blame^] | 688 | struct drm_mode *mode = NULL; |
Daniel Stone | fbe6c1d | 2019-06-17 16:04:26 +0100 | [diff] [blame] | 689 | struct drm_backend *backend; |
| 690 | const drmModeModeInfo *chosen = NULL; |
| 691 | |
| 692 | assert(info); |
| 693 | |
| 694 | wl_list_for_each(base, &output->base.mode_list, link) { |
| 695 | mode = to_drm_mode(base); |
| 696 | chosen = drm_mode_pick_equivalent(&mode->mode_info, info); |
| 697 | if (chosen) |
| 698 | break; |
| 699 | } |
| 700 | |
| 701 | if (chosen == info) { |
Scott Anderson | 60b6572 | 2020-01-28 17:18:33 +1300 | [diff] [blame^] | 702 | assert(mode); |
Daniel Stone | fbe6c1d | 2019-06-17 16:04:26 +0100 | [diff] [blame] | 703 | backend = to_drm_backend(output->base.compositor); |
| 704 | drm_output_destroy_mode(backend, mode); |
| 705 | chosen = NULL; |
| 706 | } |
| 707 | |
| 708 | if (!chosen) { |
| 709 | mode = drm_output_add_mode(output, info); |
| 710 | if (!mode) |
| 711 | return -1; |
| 712 | } |
| 713 | /* else { the equivalent mode is already in the list } */ |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | /** Rewrite the output's mode list |
| 719 | * |
| 720 | * @param output The output. |
| 721 | * @return 0 on success, -1 on failure. |
| 722 | * |
| 723 | * Destroy all existing modes in the list, and reconstruct a new list from |
| 724 | * scratch, based on the currently attached heads. |
| 725 | * |
| 726 | * On failure the output's mode list may contain some modes. |
| 727 | */ |
| 728 | static int |
| 729 | drm_output_update_modelist_from_heads(struct drm_output *output) |
| 730 | { |
| 731 | struct drm_backend *backend = to_drm_backend(output->base.compositor); |
| 732 | struct weston_head *head_base; |
| 733 | struct drm_head *head; |
| 734 | int i; |
| 735 | int ret; |
| 736 | |
| 737 | assert(!output->base.enabled); |
| 738 | |
| 739 | drm_mode_list_destroy(backend, &output->base.mode_list); |
| 740 | |
| 741 | wl_list_for_each(head_base, &output->base.head_list, output_link) { |
| 742 | head = to_drm_head(head_base); |
| 743 | for (i = 0; i < head->connector->count_modes; i++) { |
| 744 | ret = drm_output_try_add_mode(output, |
| 745 | &head->connector->modes[i]); |
| 746 | if (ret < 0) |
| 747 | return -1; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | int |
| 755 | drm_output_set_mode(struct weston_output *base, |
| 756 | enum weston_drm_backend_output_mode mode, |
| 757 | const char *modeline) |
| 758 | { |
| 759 | struct drm_output *output = to_drm_output(base); |
| 760 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 761 | struct drm_head *head = to_drm_head(weston_output_get_first_head(base)); |
| 762 | |
| 763 | struct drm_mode *current; |
| 764 | |
| 765 | if (output->virtual) |
| 766 | return -1; |
| 767 | |
| 768 | if (drm_output_update_modelist_from_heads(output) < 0) |
| 769 | return -1; |
| 770 | |
| 771 | current = drm_output_choose_initial_mode(b, output, mode, modeline, |
| 772 | &head->inherited_mode); |
| 773 | if (!current) |
| 774 | return -1; |
| 775 | |
| 776 | output->base.current_mode = ¤t->base; |
| 777 | output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT; |
| 778 | |
| 779 | /* Set native_ fields, so weston_output_mode_switch_to_native() works */ |
| 780 | output->base.native_mode = output->base.current_mode; |
| 781 | output->base.native_scale = output->base.current_scale; |
| 782 | |
| 783 | return 0; |
| 784 | } |