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