blob: 69611b089ca62a08486e835e86d28f612ab85531 [file] [log] [blame]
Daniel Stonefbe6c1d2019-06-17 16:04:26 +01001/*
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
38static 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 */
52static enum weston_mode_aspect_ratio
53drm_to_weston_mode_aspect_ratio(uint32_t drm_mode_flags)
54{
Daniel Stone60937722019-11-26 10:48:12 +000055 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 Stonefbe6c1d2019-06-17 16:04:26 +010068}
69
70static const char *
71aspect_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
80static int
81drm_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
100int
101drm_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
122static bool
123check_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
Lucas Stach72e7a1e2019-11-25 23:31:57 +0000131static uint32_t
132get_panel_orientation(struct drm_head *head, drmModeObjectPropertiesPtr props)
133{
134 struct drm_property_info *orientation =
135 &head->props_conn[WDRM_CONNECTOR_PANEL_ORIENTATION];
136 uint64_t kms_val =
137 drm_property_get_value(orientation, props,
138 WDRM_PANEL_ORIENTATION_NORMAL);
139
140 switch (kms_val) {
141 case WDRM_PANEL_ORIENTATION_NORMAL:
142 return WL_OUTPUT_TRANSFORM_NORMAL;
143 case WDRM_PANEL_ORIENTATION_UPSIDE_DOWN:
144 return WL_OUTPUT_TRANSFORM_180;
145 case WDRM_PANEL_ORIENTATION_LEFT_SIDE_UP:
146 return WL_OUTPUT_TRANSFORM_90;
147 case WDRM_PANEL_ORIENTATION_RIGHT_SIDE_UP:
148 return WL_OUTPUT_TRANSFORM_270;
149 default:
150 assert(!"unknown property value in get_panel_orientation");
151 }
152}
153
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100154static int
155parse_modeline(const char *s, drmModeModeInfo *mode)
156{
157 char hsync[16];
158 char vsync[16];
159 float fclock;
160
161 memset(mode, 0, sizeof *mode);
162
163 mode->type = DRM_MODE_TYPE_USERDEF;
164 mode->hskew = 0;
165 mode->vscan = 0;
166 mode->vrefresh = 0;
167 mode->flags = 0;
168
169 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
170 &fclock,
171 &mode->hdisplay,
172 &mode->hsync_start,
173 &mode->hsync_end,
174 &mode->htotal,
175 &mode->vdisplay,
176 &mode->vsync_start,
177 &mode->vsync_end,
178 &mode->vtotal, hsync, vsync) != 11)
179 return -1;
180
181 mode->clock = fclock * 1000;
182 if (strcasecmp(hsync, "+hsync") == 0)
183 mode->flags |= DRM_MODE_FLAG_PHSYNC;
184 else if (strcasecmp(hsync, "-hsync") == 0)
185 mode->flags |= DRM_MODE_FLAG_NHSYNC;
186 else
187 return -1;
188
189 if (strcasecmp(vsync, "+vsync") == 0)
190 mode->flags |= DRM_MODE_FLAG_PVSYNC;
191 else if (strcasecmp(vsync, "-vsync") == 0)
192 mode->flags |= DRM_MODE_FLAG_NVSYNC;
193 else
194 return -1;
195
196 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
197 mode->hdisplay, mode->vdisplay, fclock);
198
199 return 0;
200}
201
202static void
203edid_parse_string(const uint8_t *data, char text[])
204{
205 int i;
206 int replaced = 0;
207
208 /* this is always 12 bytes, but we can't guarantee it's null
209 * terminated or not junk. */
210 strncpy(text, (const char *) data, 12);
211
212 /* guarantee our new string is null-terminated */
213 text[12] = '\0';
214
215 /* remove insane chars */
216 for (i = 0; text[i] != '\0'; i++) {
217 if (text[i] == '\n' ||
218 text[i] == '\r') {
219 text[i] = '\0';
220 break;
221 }
222 }
223
224 /* ensure string is printable */
225 for (i = 0; text[i] != '\0'; i++) {
226 if (!isprint(text[i])) {
227 text[i] = '-';
228 replaced++;
229 }
230 }
231
232 /* if the string is random junk, ignore the string */
233 if (replaced > 4)
234 text[0] = '\0';
235}
236
237#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
238#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
239#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
240#define EDID_OFFSET_DATA_BLOCKS 0x36
241#define EDID_OFFSET_LAST_BLOCK 0x6c
242#define EDID_OFFSET_PNPID 0x08
243#define EDID_OFFSET_SERIAL 0x0c
244
245static int
246edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
247{
248 int i;
249 uint32_t serial_number;
250
251 /* check header */
252 if (length < 128)
253 return -1;
254 if (data[0] != 0x00 || data[1] != 0xff)
255 return -1;
256
257 /* decode the PNP ID from three 5 bit words packed into 2 bytes
258 * /--08--\/--09--\
259 * 7654321076543210
260 * |\---/\---/\---/
261 * R C1 C2 C3 */
262 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
263 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
264 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
265 edid->pnp_id[3] = '\0';
266
267 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
268 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
269 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
270 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
271 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
272 if (serial_number > 0)
273 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
274
275 /* parse EDID data */
276 for (i = EDID_OFFSET_DATA_BLOCKS;
277 i <= EDID_OFFSET_LAST_BLOCK;
278 i += 18) {
279 /* ignore pixel clock data */
280 if (data[i] != 0)
281 continue;
282 if (data[i+2] != 0)
283 continue;
284
285 /* any useful blocks? */
286 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
287 edid_parse_string(&data[i+5],
288 edid->monitor_name);
289 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
290 edid_parse_string(&data[i+5],
291 edid->serial_number);
292 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
293 edid_parse_string(&data[i+5],
294 edid->eisa_id);
295 }
296 }
297 return 0;
298}
299
300/** Parse monitor make, model and serial from EDID
301 *
302 * \param head The head whose \c drm_edid to fill in.
303 * \param props The DRM connector properties to get the EDID from.
304 * \param[out] make The monitor make (PNP ID).
305 * \param[out] model The monitor model (name).
306 * \param[out] serial_number The monitor serial number.
307 *
308 * Each of \c *make, \c *model and \c *serial_number are set only if the
309 * information is found in the EDID. The pointers they are set to must not
310 * be free()'d explicitly, instead they get implicitly freed when the
311 * \c drm_head is destroyed.
312 */
313static void
314find_and_parse_output_edid(struct drm_head *head,
315 drmModeObjectPropertiesPtr props,
316 const char **make,
317 const char **model,
318 const char **serial_number)
319{
320 drmModePropertyBlobPtr edid_blob = NULL;
321 uint32_t blob_id;
322 int rc;
323
324 blob_id =
325 drm_property_get_value(&head->props_conn[WDRM_CONNECTOR_EDID],
326 props, 0);
327 if (!blob_id)
328 return;
329
330 edid_blob = drmModeGetPropertyBlob(head->backend->drm.fd, blob_id);
331 if (!edid_blob)
332 return;
333
334 rc = edid_parse(&head->edid,
335 edid_blob->data,
336 edid_blob->length);
337 if (!rc) {
338 if (head->edid.pnp_id[0] != '\0')
339 *make = head->edid.pnp_id;
340 if (head->edid.monitor_name[0] != '\0')
341 *model = head->edid.monitor_name;
342 if (head->edid.serial_number[0] != '\0')
343 *serial_number = head->edid.serial_number;
344 }
345 drmModeFreePropertyBlob(edid_blob);
346}
347
348static uint32_t
349drm_refresh_rate_mHz(const drmModeModeInfo *info)
350{
351 uint64_t refresh;
352
353 /* Calculate higher precision (mHz) refresh rate */
354 refresh = (info->clock * 1000000LL / info->htotal +
355 info->vtotal / 2) / info->vtotal;
356
357 if (info->flags & DRM_MODE_FLAG_INTERLACE)
358 refresh *= 2;
359 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
360 refresh /= 2;
361 if (info->vscan > 1)
362 refresh /= info->vscan;
363
364 return refresh;
365}
366
367/**
368 * Add a mode to output's mode list
369 *
370 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
371 * output's mode list.
372 *
373 * @param output DRM output to add mode to
374 * @param info DRM mode structure to add
375 * @returns Newly-allocated Weston/DRM mode structure
376 */
377static struct drm_mode *
378drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
379{
380 struct drm_mode *mode;
381
382 mode = malloc(sizeof *mode);
383 if (mode == NULL)
384 return NULL;
385
386 mode->base.flags = 0;
387 mode->base.width = info->hdisplay;
388 mode->base.height = info->vdisplay;
389
390 mode->base.refresh = drm_refresh_rate_mHz(info);
391 mode->mode_info = *info;
392 mode->blob_id = 0;
393
394 if (info->type & DRM_MODE_TYPE_PREFERRED)
395 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
396
397 mode->base.aspect_ratio = drm_to_weston_mode_aspect_ratio(info->flags);
398
399 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
400
401 return mode;
402}
403
404/**
405 * Destroys a mode, and removes it from the list.
406 */
407static void
408drm_output_destroy_mode(struct drm_backend *backend, struct drm_mode *mode)
409{
410 if (mode->blob_id)
411 drmModeDestroyPropertyBlob(backend->drm.fd, mode->blob_id);
412 wl_list_remove(&mode->base.link);
413 free(mode);
414}
415
416/** Destroy a list of drm_modes
417 *
418 * @param backend The backend for releasing mode property blobs.
419 * @param mode_list The list linked by drm_mode::base.link.
420 */
421void
422drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list)
423{
424 struct drm_mode *mode, *next;
425
426 wl_list_for_each_safe(mode, next, mode_list, base.link)
427 drm_output_destroy_mode(backend, mode);
428}
429
430void
431drm_output_print_modes(struct drm_output *output)
432{
433 struct weston_mode *m;
434 struct drm_mode *dm;
435 const char *aspect_ratio;
436
437 wl_list_for_each(m, &output->base.mode_list, link) {
438 dm = to_drm_mode(m);
439
440 aspect_ratio = aspect_ratio_to_string(m->aspect_ratio);
441 weston_log_continue(STAMP_SPACE "%dx%d@%.1f%s%s%s, %.1f MHz\n",
442 m->width, m->height, m->refresh / 1000.0,
443 aspect_ratio,
444 m->flags & WL_OUTPUT_MODE_PREFERRED ?
445 ", preferred" : "",
446 m->flags & WL_OUTPUT_MODE_CURRENT ?
447 ", current" : "",
448 dm->mode_info.clock / 1000.0);
449 }
450}
451
452
453/**
454 * Find the closest-matching mode for a given target
455 *
456 * Given a target mode, find the most suitable mode amongst the output's
457 * current mode list to use, preferring the current mode if possible, to
458 * avoid an expensive mode switch.
459 *
460 * @param output DRM output
461 * @param target_mode Mode to attempt to match
462 * @returns Pointer to a mode from the output's mode list
463 */
464struct drm_mode *
465drm_output_choose_mode(struct drm_output *output,
466 struct weston_mode *target_mode)
467{
468 struct drm_mode *tmp_mode = NULL, *mode_fall_back = NULL, *mode;
469 enum weston_mode_aspect_ratio src_aspect = WESTON_MODE_PIC_AR_NONE;
470 enum weston_mode_aspect_ratio target_aspect = WESTON_MODE_PIC_AR_NONE;
471 struct drm_backend *b;
472
473 b = to_drm_backend(output->base.compositor);
474 target_aspect = target_mode->aspect_ratio;
475 src_aspect = output->base.current_mode->aspect_ratio;
476 if (output->base.current_mode->width == target_mode->width &&
477 output->base.current_mode->height == target_mode->height &&
478 (output->base.current_mode->refresh == target_mode->refresh ||
479 target_mode->refresh == 0)) {
480 if (!b->aspect_ratio_supported || src_aspect == target_aspect)
481 return to_drm_mode(output->base.current_mode);
482 }
483
484 wl_list_for_each(mode, &output->base.mode_list, base.link) {
485
486 src_aspect = mode->base.aspect_ratio;
487 if (mode->mode_info.hdisplay == target_mode->width &&
488 mode->mode_info.vdisplay == target_mode->height) {
489 if (mode->base.refresh == target_mode->refresh ||
490 target_mode->refresh == 0) {
491 if (!b->aspect_ratio_supported ||
492 src_aspect == target_aspect)
493 return mode;
494 else if (!mode_fall_back)
495 mode_fall_back = mode;
496 } else if (!tmp_mode) {
497 tmp_mode = mode;
498 }
499 }
500 }
501
502 if (mode_fall_back)
503 return mode_fall_back;
504
505 return tmp_mode;
506}
507
508void
509update_head_from_connector(struct drm_head *head,
510 drmModeObjectProperties *props)
511{
512 const char *make = "unknown";
513 const char *model = "unknown";
514 const char *serial_number = "unknown";
515
516 find_and_parse_output_edid(head, props, &make, &model, &serial_number);
517 weston_head_set_monitor_strings(&head->base, make, model, serial_number);
518 weston_head_set_non_desktop(&head->base,
519 check_non_desktop(head, props));
520 weston_head_set_subpixel(&head->base,
521 drm_subpixel_to_wayland(head->connector->subpixel));
522
523 weston_head_set_physical_size(&head->base, head->connector->mmWidth,
524 head->connector->mmHeight);
525
Lucas Stach72e7a1e2019-11-25 23:31:57 +0000526 weston_head_set_transform(&head->base,
527 get_panel_orientation(head, props));
528
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100529 /* Unknown connection status is assumed disconnected. */
530 weston_head_set_connection_status(&head->base,
531 head->connector->connection == DRM_MODE_CONNECTED);
532}
533
534/**
535 * Choose suitable mode for an output
536 *
537 * Find the most suitable mode to use for initial setup (or reconfiguration on
538 * hotplug etc) for a DRM output.
539 *
540 * @param backend the DRM backend
541 * @param output DRM output to choose mode for
542 * @param mode Strategy and preference to use when choosing mode
543 * @param modeline Manually-entered mode (may be NULL)
544 * @param current_mode Mode currently being displayed on this output
545 * @returns A mode from the output's mode list, or NULL if none available
546 */
547static struct drm_mode *
548drm_output_choose_initial_mode(struct drm_backend *backend,
549 struct drm_output *output,
550 enum weston_drm_backend_output_mode mode,
551 const char *modeline,
552 const drmModeModeInfo *current_mode)
553{
554 struct drm_mode *preferred = NULL;
555 struct drm_mode *current = NULL;
556 struct drm_mode *configured = NULL;
557 struct drm_mode *config_fall_back = NULL;
558 struct drm_mode *best = NULL;
559 struct drm_mode *drm_mode;
560 drmModeModeInfo drm_modeline;
561 int32_t width = 0;
562 int32_t height = 0;
563 uint32_t refresh = 0;
564 uint32_t aspect_width = 0;
565 uint32_t aspect_height = 0;
566 enum weston_mode_aspect_ratio aspect_ratio = WESTON_MODE_PIC_AR_NONE;
567 int n;
568
569 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
570 n = sscanf(modeline, "%dx%d@%d %u:%u", &width, &height,
571 &refresh, &aspect_width, &aspect_height);
572 if (backend->aspect_ratio_supported && n == 5) {
573 if (aspect_width == 4 && aspect_height == 3)
574 aspect_ratio = WESTON_MODE_PIC_AR_4_3;
575 else if (aspect_width == 16 && aspect_height == 9)
576 aspect_ratio = WESTON_MODE_PIC_AR_16_9;
577 else if (aspect_width == 64 && aspect_height == 27)
578 aspect_ratio = WESTON_MODE_PIC_AR_64_27;
579 else if (aspect_width == 256 && aspect_height == 135)
580 aspect_ratio = WESTON_MODE_PIC_AR_256_135;
581 else
582 weston_log("Invalid modeline \"%s\" for output %s\n",
583 modeline, output->base.name);
584 }
585 if (n != 2 && n != 3 && n != 5) {
586 width = -1;
587
588 if (parse_modeline(modeline, &drm_modeline) == 0) {
589 configured = drm_output_add_mode(output, &drm_modeline);
590 if (!configured)
591 return NULL;
592 } else {
593 weston_log("Invalid modeline \"%s\" for output %s\n",
594 modeline, output->base.name);
595 }
596 }
597 }
598
599 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
600 if (width == drm_mode->base.width &&
601 height == drm_mode->base.height &&
602 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh)) {
603 if (!backend->aspect_ratio_supported ||
604 aspect_ratio == drm_mode->base.aspect_ratio)
605 configured = drm_mode;
606 else
607 config_fall_back = drm_mode;
608 }
609
610 if (memcmp(current_mode, &drm_mode->mode_info,
611 sizeof *current_mode) == 0)
612 current = drm_mode;
613
614 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
615 preferred = drm_mode;
616
617 best = drm_mode;
618 }
619
620 if (current == NULL && current_mode->clock != 0) {
621 current = drm_output_add_mode(output, current_mode);
622 if (!current)
623 return NULL;
624 }
625
626 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
627 configured = current;
628
629 if (configured)
630 return configured;
631
632 if (config_fall_back)
633 return config_fall_back;
634
635 if (preferred)
636 return preferred;
637
638 if (current)
639 return current;
640
641 if (best)
642 return best;
643
644 weston_log("no available modes for %s\n", output->base.name);
645 return NULL;
646}
647
648static uint32_t
649u32distance(uint32_t a, uint32_t b)
650{
651 if (a < b)
652 return b - a;
653 else
654 return a - b;
655}
656
657/** Choose equivalent mode
658 *
659 * If the two modes are not equivalent, return NULL.
660 * Otherwise return the mode that is more likely to work in place of both.
661 *
662 * None of the fuzzy matching criteria in this function have any justification.
663 *
664 * typedef struct _drmModeModeInfo {
665 * uint32_t clock;
666 * uint16_t hdisplay, hsync_start, hsync_end, htotal, hskew;
667 * uint16_t vdisplay, vsync_start, vsync_end, vtotal, vscan;
668 *
669 * uint32_t vrefresh;
670 *
671 * uint32_t flags;
672 * uint32_t type;
673 * char name[DRM_DISPLAY_MODE_LEN];
674 * } drmModeModeInfo, *drmModeModeInfoPtr;
675 */
676static const drmModeModeInfo *
677drm_mode_pick_equivalent(const drmModeModeInfo *a, const drmModeModeInfo *b)
678{
679 uint32_t refresh_a, refresh_b;
680
681 if (a->hdisplay != b->hdisplay || a->vdisplay != b->vdisplay)
682 return NULL;
683
684 if (a->flags != b->flags)
685 return NULL;
686
687 /* kHz */
688 if (u32distance(a->clock, b->clock) > 500)
689 return NULL;
690
691 refresh_a = drm_refresh_rate_mHz(a);
692 refresh_b = drm_refresh_rate_mHz(b);
693 if (u32distance(refresh_a, refresh_b) > 50)
694 return NULL;
695
696 if ((a->type ^ b->type) & DRM_MODE_TYPE_PREFERRED) {
697 if (a->type & DRM_MODE_TYPE_PREFERRED)
698 return a;
699 else
700 return b;
701 }
702
703 return a;
704}
705
706/* If the given mode info is not already in the list, add it.
707 * If it is in the list, either keep the existing or replace it,
708 * depending on which one is "better".
709 */
710static int
711drm_output_try_add_mode(struct drm_output *output, const drmModeModeInfo *info)
712{
713 struct weston_mode *base;
Scott Anderson60b65722020-01-28 17:18:33 +1300714 struct drm_mode *mode = NULL;
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100715 struct drm_backend *backend;
716 const drmModeModeInfo *chosen = NULL;
717
718 assert(info);
719
720 wl_list_for_each(base, &output->base.mode_list, link) {
721 mode = to_drm_mode(base);
722 chosen = drm_mode_pick_equivalent(&mode->mode_info, info);
723 if (chosen)
724 break;
725 }
726
727 if (chosen == info) {
Scott Anderson60b65722020-01-28 17:18:33 +1300728 assert(mode);
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100729 backend = to_drm_backend(output->base.compositor);
730 drm_output_destroy_mode(backend, mode);
731 chosen = NULL;
732 }
733
734 if (!chosen) {
735 mode = drm_output_add_mode(output, info);
736 if (!mode)
737 return -1;
738 }
739 /* else { the equivalent mode is already in the list } */
740
741 return 0;
742}
743
744/** Rewrite the output's mode list
745 *
746 * @param output The output.
747 * @return 0 on success, -1 on failure.
748 *
749 * Destroy all existing modes in the list, and reconstruct a new list from
750 * scratch, based on the currently attached heads.
751 *
752 * On failure the output's mode list may contain some modes.
753 */
754static int
755drm_output_update_modelist_from_heads(struct drm_output *output)
756{
757 struct drm_backend *backend = to_drm_backend(output->base.compositor);
758 struct weston_head *head_base;
759 struct drm_head *head;
760 int i;
761 int ret;
762
763 assert(!output->base.enabled);
764
765 drm_mode_list_destroy(backend, &output->base.mode_list);
766
767 wl_list_for_each(head_base, &output->base.head_list, output_link) {
768 head = to_drm_head(head_base);
769 for (i = 0; i < head->connector->count_modes; i++) {
770 ret = drm_output_try_add_mode(output,
771 &head->connector->modes[i]);
772 if (ret < 0)
773 return -1;
774 }
775 }
776
777 return 0;
778}
779
780int
781drm_output_set_mode(struct weston_output *base,
782 enum weston_drm_backend_output_mode mode,
783 const char *modeline)
784{
785 struct drm_output *output = to_drm_output(base);
786 struct drm_backend *b = to_drm_backend(base->compositor);
787 struct drm_head *head = to_drm_head(weston_output_get_first_head(base));
788
789 struct drm_mode *current;
790
791 if (output->virtual)
792 return -1;
793
794 if (drm_output_update_modelist_from_heads(output) < 0)
795 return -1;
796
797 current = drm_output_choose_initial_mode(b, output, mode, modeline,
798 &head->inherited_mode);
799 if (!current)
800 return -1;
801
802 output->base.current_mode = &current->base;
803 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
804
805 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
806 output->base.native_mode = output->base.current_mode;
807 output->base.native_scale = output->base.current_scale;
808
809 return 0;
810}