blob: 2915aae60dde364b92b11adafa501de993f04eb3 [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
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300123check_non_desktop(struct drm_connector *connector, drmModeObjectPropertiesPtr props)
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100124{
125 struct drm_property_info *non_desktop_info =
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300126 &connector->props[WDRM_CONNECTOR_NON_DESKTOP];
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100127
128 return drm_property_get_value(non_desktop_info, props, 0);
129}
130
Lucas Stach72e7a1e2019-11-25 23:31:57 +0000131static uint32_t
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300132get_panel_orientation(struct drm_connector *connector, drmModeObjectPropertiesPtr props)
Lucas Stach72e7a1e2019-11-25 23:31:57 +0000133{
134 struct drm_property_info *orientation =
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300135 &connector->props[WDRM_CONNECTOR_PANEL_ORIENTATION];
Lucas Stach72e7a1e2019-11-25 23:31:57 +0000136 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 =
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300325 drm_property_get_value(
326 &head->connector.props[WDRM_CONNECTOR_EDID],
327 props, 0);
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100328 if (!blob_id)
329 return;
330
331 edid_blob = drmModeGetPropertyBlob(head->backend->drm.fd, blob_id);
332 if (!edid_blob)
333 return;
334
335 rc = edid_parse(&head->edid,
336 edid_blob->data,
337 edid_blob->length);
338 if (!rc) {
339 if (head->edid.pnp_id[0] != '\0')
340 *make = head->edid.pnp_id;
341 if (head->edid.monitor_name[0] != '\0')
342 *model = head->edid.monitor_name;
343 if (head->edid.serial_number[0] != '\0')
344 *serial_number = head->edid.serial_number;
345 }
346 drmModeFreePropertyBlob(edid_blob);
347}
348
349static uint32_t
350drm_refresh_rate_mHz(const drmModeModeInfo *info)
351{
352 uint64_t refresh;
353
354 /* Calculate higher precision (mHz) refresh rate */
355 refresh = (info->clock * 1000000LL / info->htotal +
356 info->vtotal / 2) / info->vtotal;
357
358 if (info->flags & DRM_MODE_FLAG_INTERLACE)
359 refresh *= 2;
360 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
361 refresh /= 2;
362 if (info->vscan > 1)
363 refresh /= info->vscan;
364
365 return refresh;
366}
367
368/**
369 * Add a mode to output's mode list
370 *
371 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
372 * output's mode list.
373 *
374 * @param output DRM output to add mode to
375 * @param info DRM mode structure to add
376 * @returns Newly-allocated Weston/DRM mode structure
377 */
378static struct drm_mode *
379drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
380{
381 struct drm_mode *mode;
382
383 mode = malloc(sizeof *mode);
384 if (mode == NULL)
385 return NULL;
386
387 mode->base.flags = 0;
388 mode->base.width = info->hdisplay;
389 mode->base.height = info->vdisplay;
390
391 mode->base.refresh = drm_refresh_rate_mHz(info);
392 mode->mode_info = *info;
393 mode->blob_id = 0;
394
395 if (info->type & DRM_MODE_TYPE_PREFERRED)
396 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
397
398 mode->base.aspect_ratio = drm_to_weston_mode_aspect_ratio(info->flags);
399
400 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
401
402 return mode;
403}
404
405/**
406 * Destroys a mode, and removes it from the list.
407 */
408static void
409drm_output_destroy_mode(struct drm_backend *backend, struct drm_mode *mode)
410{
411 if (mode->blob_id)
412 drmModeDestroyPropertyBlob(backend->drm.fd, mode->blob_id);
413 wl_list_remove(&mode->base.link);
414 free(mode);
415}
416
417/** Destroy a list of drm_modes
418 *
419 * @param backend The backend for releasing mode property blobs.
420 * @param mode_list The list linked by drm_mode::base.link.
421 */
422void
423drm_mode_list_destroy(struct drm_backend *backend, struct wl_list *mode_list)
424{
425 struct drm_mode *mode, *next;
426
427 wl_list_for_each_safe(mode, next, mode_list, base.link)
428 drm_output_destroy_mode(backend, mode);
429}
430
431void
432drm_output_print_modes(struct drm_output *output)
433{
434 struct weston_mode *m;
435 struct drm_mode *dm;
436 const char *aspect_ratio;
437
438 wl_list_for_each(m, &output->base.mode_list, link) {
439 dm = to_drm_mode(m);
440
441 aspect_ratio = aspect_ratio_to_string(m->aspect_ratio);
442 weston_log_continue(STAMP_SPACE "%dx%d@%.1f%s%s%s, %.1f MHz\n",
443 m->width, m->height, m->refresh / 1000.0,
444 aspect_ratio,
445 m->flags & WL_OUTPUT_MODE_PREFERRED ?
446 ", preferred" : "",
447 m->flags & WL_OUTPUT_MODE_CURRENT ?
448 ", current" : "",
449 dm->mode_info.clock / 1000.0);
450 }
451}
452
453
454/**
455 * Find the closest-matching mode for a given target
456 *
457 * Given a target mode, find the most suitable mode amongst the output's
458 * current mode list to use, preferring the current mode if possible, to
459 * avoid an expensive mode switch.
460 *
461 * @param output DRM output
462 * @param target_mode Mode to attempt to match
463 * @returns Pointer to a mode from the output's mode list
464 */
465struct drm_mode *
466drm_output_choose_mode(struct drm_output *output,
467 struct weston_mode *target_mode)
468{
469 struct drm_mode *tmp_mode = NULL, *mode_fall_back = NULL, *mode;
470 enum weston_mode_aspect_ratio src_aspect = WESTON_MODE_PIC_AR_NONE;
471 enum weston_mode_aspect_ratio target_aspect = WESTON_MODE_PIC_AR_NONE;
472 struct drm_backend *b;
473
474 b = to_drm_backend(output->base.compositor);
475 target_aspect = target_mode->aspect_ratio;
476 src_aspect = output->base.current_mode->aspect_ratio;
477 if (output->base.current_mode->width == target_mode->width &&
478 output->base.current_mode->height == target_mode->height &&
479 (output->base.current_mode->refresh == target_mode->refresh ||
480 target_mode->refresh == 0)) {
481 if (!b->aspect_ratio_supported || src_aspect == target_aspect)
482 return to_drm_mode(output->base.current_mode);
483 }
484
485 wl_list_for_each(mode, &output->base.mode_list, base.link) {
486
487 src_aspect = mode->base.aspect_ratio;
488 if (mode->mode_info.hdisplay == target_mode->width &&
489 mode->mode_info.vdisplay == target_mode->height) {
490 if (mode->base.refresh == target_mode->refresh ||
491 target_mode->refresh == 0) {
492 if (!b->aspect_ratio_supported ||
493 src_aspect == target_aspect)
494 return mode;
495 else if (!mode_fall_back)
496 mode_fall_back = mode;
497 } else if (!tmp_mode) {
498 tmp_mode = mode;
499 }
500 }
501 }
502
503 if (mode_fall_back)
504 return mode_fall_back;
505
506 return tmp_mode;
507}
508
509void
510update_head_from_connector(struct drm_head *head,
511 drmModeObjectProperties *props)
512{
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300513 struct drm_connector *connector = &head->connector;
514 drmModeConnector *conn = connector->conn;
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100515 const char *make = "unknown";
516 const char *model = "unknown";
517 const char *serial_number = "unknown";
518
519 find_and_parse_output_edid(head, props, &make, &model, &serial_number);
520 weston_head_set_monitor_strings(&head->base, make, model, serial_number);
521 weston_head_set_non_desktop(&head->base,
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300522 check_non_desktop(connector, props));
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100523 weston_head_set_subpixel(&head->base,
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300524 drm_subpixel_to_wayland(conn->subpixel));
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100525
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300526 weston_head_set_physical_size(&head->base, conn->mmWidth, conn->mmHeight);
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100527
Lucas Stach72e7a1e2019-11-25 23:31:57 +0000528 weston_head_set_transform(&head->base,
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300529 get_panel_orientation(connector, props));
Lucas Stach72e7a1e2019-11-25 23:31:57 +0000530
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100531 /* Unknown connection status is assumed disconnected. */
532 weston_head_set_connection_status(&head->base,
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300533 conn->connection == DRM_MODE_CONNECTED);
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100534}
535
536/**
537 * Choose suitable mode for an output
538 *
539 * Find the most suitable mode to use for initial setup (or reconfiguration on
540 * hotplug etc) for a DRM output.
541 *
542 * @param backend the DRM backend
543 * @param output DRM output to choose mode for
544 * @param mode Strategy and preference to use when choosing mode
545 * @param modeline Manually-entered mode (may be NULL)
546 * @param current_mode Mode currently being displayed on this output
547 * @returns A mode from the output's mode list, or NULL if none available
548 */
549static struct drm_mode *
550drm_output_choose_initial_mode(struct drm_backend *backend,
551 struct drm_output *output,
552 enum weston_drm_backend_output_mode mode,
553 const char *modeline,
554 const drmModeModeInfo *current_mode)
555{
556 struct drm_mode *preferred = NULL;
557 struct drm_mode *current = NULL;
558 struct drm_mode *configured = NULL;
559 struct drm_mode *config_fall_back = NULL;
560 struct drm_mode *best = NULL;
561 struct drm_mode *drm_mode;
562 drmModeModeInfo drm_modeline;
563 int32_t width = 0;
564 int32_t height = 0;
565 uint32_t refresh = 0;
566 uint32_t aspect_width = 0;
567 uint32_t aspect_height = 0;
568 enum weston_mode_aspect_ratio aspect_ratio = WESTON_MODE_PIC_AR_NONE;
569 int n;
570
571 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
572 n = sscanf(modeline, "%dx%d@%d %u:%u", &width, &height,
573 &refresh, &aspect_width, &aspect_height);
574 if (backend->aspect_ratio_supported && n == 5) {
575 if (aspect_width == 4 && aspect_height == 3)
576 aspect_ratio = WESTON_MODE_PIC_AR_4_3;
577 else if (aspect_width == 16 && aspect_height == 9)
578 aspect_ratio = WESTON_MODE_PIC_AR_16_9;
579 else if (aspect_width == 64 && aspect_height == 27)
580 aspect_ratio = WESTON_MODE_PIC_AR_64_27;
581 else if (aspect_width == 256 && aspect_height == 135)
582 aspect_ratio = WESTON_MODE_PIC_AR_256_135;
583 else
584 weston_log("Invalid modeline \"%s\" for output %s\n",
585 modeline, output->base.name);
586 }
587 if (n != 2 && n != 3 && n != 5) {
588 width = -1;
589
590 if (parse_modeline(modeline, &drm_modeline) == 0) {
591 configured = drm_output_add_mode(output, &drm_modeline);
592 if (!configured)
593 return NULL;
594 } else {
595 weston_log("Invalid modeline \"%s\" for output %s\n",
596 modeline, output->base.name);
597 }
598 }
599 }
600
601 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
602 if (width == drm_mode->base.width &&
603 height == drm_mode->base.height &&
604 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh)) {
605 if (!backend->aspect_ratio_supported ||
606 aspect_ratio == drm_mode->base.aspect_ratio)
607 configured = drm_mode;
608 else
609 config_fall_back = drm_mode;
610 }
611
612 if (memcmp(current_mode, &drm_mode->mode_info,
613 sizeof *current_mode) == 0)
614 current = drm_mode;
615
616 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
617 preferred = drm_mode;
618
619 best = drm_mode;
620 }
621
622 if (current == NULL && current_mode->clock != 0) {
623 current = drm_output_add_mode(output, current_mode);
624 if (!current)
625 return NULL;
626 }
627
628 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
629 configured = current;
630
631 if (configured)
632 return configured;
633
634 if (config_fall_back)
635 return config_fall_back;
636
637 if (preferred)
638 return preferred;
639
640 if (current)
641 return current;
642
643 if (best)
644 return best;
645
646 weston_log("no available modes for %s\n", output->base.name);
647 return NULL;
648}
649
650static uint32_t
651u32distance(uint32_t a, uint32_t b)
652{
653 if (a < b)
654 return b - a;
655 else
656 return a - b;
657}
658
659/** Choose equivalent mode
660 *
661 * If the two modes are not equivalent, return NULL.
662 * Otherwise return the mode that is more likely to work in place of both.
663 *
664 * None of the fuzzy matching criteria in this function have any justification.
665 *
666 * typedef struct _drmModeModeInfo {
667 * uint32_t clock;
668 * uint16_t hdisplay, hsync_start, hsync_end, htotal, hskew;
669 * uint16_t vdisplay, vsync_start, vsync_end, vtotal, vscan;
670 *
671 * uint32_t vrefresh;
672 *
673 * uint32_t flags;
674 * uint32_t type;
675 * char name[DRM_DISPLAY_MODE_LEN];
676 * } drmModeModeInfo, *drmModeModeInfoPtr;
677 */
678static const drmModeModeInfo *
679drm_mode_pick_equivalent(const drmModeModeInfo *a, const drmModeModeInfo *b)
680{
681 uint32_t refresh_a, refresh_b;
682
683 if (a->hdisplay != b->hdisplay || a->vdisplay != b->vdisplay)
684 return NULL;
685
686 if (a->flags != b->flags)
687 return NULL;
688
689 /* kHz */
690 if (u32distance(a->clock, b->clock) > 500)
691 return NULL;
692
693 refresh_a = drm_refresh_rate_mHz(a);
694 refresh_b = drm_refresh_rate_mHz(b);
695 if (u32distance(refresh_a, refresh_b) > 50)
696 return NULL;
697
698 if ((a->type ^ b->type) & DRM_MODE_TYPE_PREFERRED) {
699 if (a->type & DRM_MODE_TYPE_PREFERRED)
700 return a;
701 else
702 return b;
703 }
704
705 return a;
706}
707
708/* If the given mode info is not already in the list, add it.
709 * If it is in the list, either keep the existing or replace it,
710 * depending on which one is "better".
711 */
712static int
713drm_output_try_add_mode(struct drm_output *output, const drmModeModeInfo *info)
714{
715 struct weston_mode *base;
Scott Anderson60b65722020-01-28 17:18:33 +1300716 struct drm_mode *mode = NULL;
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100717 struct drm_backend *backend;
718 const drmModeModeInfo *chosen = NULL;
719
720 assert(info);
721
722 wl_list_for_each(base, &output->base.mode_list, link) {
723 mode = to_drm_mode(base);
724 chosen = drm_mode_pick_equivalent(&mode->mode_info, info);
725 if (chosen)
726 break;
727 }
728
729 if (chosen == info) {
Scott Anderson60b65722020-01-28 17:18:33 +1300730 assert(mode);
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100731 backend = to_drm_backend(output->base.compositor);
732 drm_output_destroy_mode(backend, mode);
733 chosen = NULL;
734 }
735
736 if (!chosen) {
737 mode = drm_output_add_mode(output, info);
738 if (!mode)
739 return -1;
740 }
741 /* else { the equivalent mode is already in the list } */
742
743 return 0;
744}
745
746/** Rewrite the output's mode list
747 *
748 * @param output The output.
749 * @return 0 on success, -1 on failure.
750 *
751 * Destroy all existing modes in the list, and reconstruct a new list from
752 * scratch, based on the currently attached heads.
753 *
754 * On failure the output's mode list may contain some modes.
755 */
756static int
757drm_output_update_modelist_from_heads(struct drm_output *output)
758{
759 struct drm_backend *backend = to_drm_backend(output->base.compositor);
760 struct weston_head *head_base;
761 struct drm_head *head;
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300762 drmModeConnector *conn;
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100763 int i;
764 int ret;
765
766 assert(!output->base.enabled);
767
768 drm_mode_list_destroy(backend, &output->base.mode_list);
769
770 wl_list_for_each(head_base, &output->base.head_list, output_link) {
771 head = to_drm_head(head_base);
Leandro Ribeiroe6369902020-06-17 11:09:47 -0300772 conn = head->connector.conn;
773 for (i = 0; i < conn->count_modes; i++) {
774 ret = drm_output_try_add_mode(output, &conn->modes[i]);
Daniel Stonefbe6c1d2019-06-17 16:04:26 +0100775 if (ret < 0)
776 return -1;
777 }
778 }
779
780 return 0;
781}
782
783int
784drm_output_set_mode(struct weston_output *base,
785 enum weston_drm_backend_output_mode mode,
786 const char *modeline)
787{
788 struct drm_output *output = to_drm_output(base);
789 struct drm_backend *b = to_drm_backend(base->compositor);
790 struct drm_head *head = to_drm_head(weston_output_get_first_head(base));
791
792 struct drm_mode *current;
793
794 if (output->virtual)
795 return -1;
796
797 if (drm_output_update_modelist_from_heads(output) < 0)
798 return -1;
799
800 current = drm_output_choose_initial_mode(b, output, mode, modeline,
801 &head->inherited_mode);
802 if (!current)
803 return -1;
804
805 output->base.current_mode = &current->base;
806 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
807
808 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
809 output->base.native_mode = output->base.current_mode;
810 output->base.native_scale = output->base.current_scale;
811
812 return 0;
813}