[PATCH 2/2] compositor-drm: Add aspect-ratio parsing support

Nautiyal, Ankit K ankit.k.nautiyal at intel.com
Thu Jul 5 15:43:49 UTC 2018


From: Ankit Nautiyal <ankit.k.nautiyal at intel.com>

The flag bits 19-22 of the connector modes, provide the aspect-ratio
information. This information can be stored in flags bits of the
weston mode structure, so that it can used for setting a mode with a
particular aspect-ratio.
Currently, DRM layer supports aspect-ratio with atomic-modesetting by
default. For legacy modeset path, the user-space needs to set the
drm client cap for aspect-ratio, if it wants aspect-ratio information
in modes.

This patch:
- preserves aspect-ratio flags from kernel video modes and
  accommodates it in wayland mode.
- uses aspect-ratio to pick the appropriate mode during modeset.
- changes the mode format in configuration file weston.ini to
  accommodate aspect-ratio information as:
  WIDTHxHEIGHT at REFRESH-RATE ASPECT-RATIO
  The aspect-ratio can take the following values :
  4:3, 16:9, 64:27, 256:135.

v2: As per recommendation from Pekka Paalanen, Quentin Glidic,
Daniel Stone, dropped the aspect-ratio info from wayland protocol,
thereby avoiding exposure of aspect-ratio to the client.

v3: As suggested by Pekka Paalanen, added aspect_ratio field to store
aspect-ratio information from the drm. Also added drm client
capability for aspect-ratio, as recommended by Daniel Vetter.

v4: Minor modifications and fixes as suggested by Pekka Paalanen.

v5: Rebased, fixed some styling issues, and added aspect-ratio
information while printing weston_modes.

v6: Moved the man pages changes to a different patch. Minor
reorganization of code as suggested by Pekka Paalanen.

Signed-off-by: Ankit Nautiyal <ankit.k.nautiyal at intel.com>

Acked-by: Pekka Paalanen <pekka.paalanen at collabora.co.uk> (v4)
---
 libweston/compositor-drm.c | 121 ++++++++++++++++++++++++++++++++++---
 libweston/compositor.h     |  15 +++++
 2 files changed, 126 insertions(+), 10 deletions(-)

diff --git a/libweston/compositor-drm.c b/libweston/compositor-drm.c
index b96e2967..e4d53fa4 100644
--- a/libweston/compositor-drm.c
+++ b/libweston/compositor-drm.c
@@ -73,6 +73,10 @@
 #define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2
 #endif
 
+#ifndef DRM_CLIENT_CAP_ASPECT_RATIO
+#define DRM_CLIENT_CAP_ASPECT_RATIO	4
+#endif
+
 #ifndef DRM_CAP_CURSOR_WIDTH
 #define DRM_CAP_CURSOR_WIDTH 0x8
 #endif
@@ -86,6 +90,16 @@
 #endif
 
 #define MAX_CLONED_CONNECTORS 4
+#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
+
+/**
+ * aspect ratio info taken from the drmModeModeInfo flag bits 19-22,
+ * which should be used to fill the aspect ratio field in weston_mode.
+ */
+#define DRM_MODE_FLAG_PIC_AR_BITS_POS	19
+#ifndef DRM_MODE_FLAG_PIC_AR_MASK
+#define DRM_MODE_FLAG_PIC_AR_MASK (0xF << DRM_MODE_FLAG_PIC_AR_BITS_POS)
+#endif
 
 /**
  * Represents the values of an enum-type KMS property
@@ -273,6 +287,8 @@ struct drm_backend {
 	uint32_t pageflip_timeout;
 
 	bool shutting_down;
+
+	bool aspect_ratio_supported;
 };
 
 struct drm_mode {
@@ -465,6 +481,14 @@ struct drm_output {
 	struct wl_event_source *pageflip_timer;
 };
 
+static const char *const aspect_ratio_as_string[] = {
+	[WESTON_MODE_PIC_AR_NONE] = "",
+	[WESTON_MODE_PIC_AR_4_3] = " 4:3",
+	[WESTON_MODE_PIC_AR_16_9] = " 16:9",
+	[WESTON_MODE_PIC_AR_64_27] = " 64:27",
+	[WESTON_MODE_PIC_AR_256_135] = " 256:135",
+};
+
 static struct gl_renderer_interface *gl_renderer;
 
 static const char default_seat[] = "seat0";
@@ -3042,6 +3066,29 @@ err:
 	drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
 }
 
+/*
+ * Get the aspect-ratio from drmModeModeInfo mode flags.
+ *
+ * @param drm_mode_flags- flags from drmModeModeInfo structure.
+ * @returns aspect-ratio as encoded in enum 'weston_mode_aspect_ratio'.
+ */
+static enum weston_mode_aspect_ratio
+drm_to_weston_mode_aspect_ratio(uint32_t drm_mode_flags)
+{
+	return (drm_mode_flags & DRM_MODE_FLAG_PIC_AR_MASK) >>
+		DRM_MODE_FLAG_PIC_AR_BITS_POS;
+}
+
+static const char *
+aspect_ratio_to_string(enum weston_mode_aspect_ratio ratio)
+{
+	if (ratio < 0 || ratio >= ARRAY_SIZE(aspect_ratio_as_string) ||
+	    !aspect_ratio_as_string[ratio])
+		return " (unknown aspect ratio)";
+
+	return aspect_ratio_as_string[ratio];
+}
+
 static void
 drm_assign_planes(struct weston_output *output_base, void *repaint_data)
 {
@@ -3172,25 +3219,43 @@ drm_assign_planes(struct weston_output *output_base, void *repaint_data)
 static struct drm_mode *
 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
 {
-	struct drm_mode *tmp_mode = NULL, *mode;
+	struct drm_mode *tmp_mode = NULL, *mode_fall_back = NULL, *mode;
+	enum weston_mode_aspect_ratio src_aspect = WESTON_MODE_PIC_AR_NONE;
+	enum weston_mode_aspect_ratio target_aspect = WESTON_MODE_PIC_AR_NONE;
+	struct drm_backend *b;
 
+	b = to_drm_backend(output->base.compositor);
+	target_aspect = target_mode->aspect_ratio;
+	src_aspect = output->base.current_mode->aspect_ratio;
 	if (output->base.current_mode->width == target_mode->width &&
 	    output->base.current_mode->height == target_mode->height &&
 	    (output->base.current_mode->refresh == target_mode->refresh ||
-	     target_mode->refresh == 0))
-		return to_drm_mode(output->base.current_mode);
+	     target_mode->refresh == 0)) {
+		if (!b->aspect_ratio_supported || src_aspect == target_aspect)
+			return to_drm_mode(output->base.current_mode);
+	}
 
 	wl_list_for_each(mode, &output->base.mode_list, base.link) {
+
+		src_aspect = mode->base.aspect_ratio;
 		if (mode->mode_info.hdisplay == target_mode->width &&
 		    mode->mode_info.vdisplay == target_mode->height) {
 			if (mode->base.refresh == target_mode->refresh ||
 			    target_mode->refresh == 0) {
-				return mode;
-			} else if (!tmp_mode)
+				if (!b->aspect_ratio_supported ||
+				    src_aspect == target_aspect)
+					return mode;
+				else if (!mode_fall_back)
+					mode_fall_back = mode;
+			} else if (!tmp_mode) {
 				tmp_mode = mode;
+			}
 		}
 	}
 
+	if (mode_fall_back)
+		return mode_fall_back;
+
 	return tmp_mode;
 }
 
@@ -3328,6 +3393,11 @@ init_kms_caps(struct drm_backend *b)
 	weston_log("DRM: %s atomic modesetting\n",
 		   b->atomic_modeset ? "supports" : "does not support");
 
+	ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_ASPECT_RATIO, 1);
+	b->aspect_ratio_supported = (ret == 0);
+	weston_log("DRM: %s picture aspect ratio\n",
+		   b->aspect_ratio_supported ? "supports" : "does not support");
+
 	return 0;
 }
 
@@ -3732,6 +3802,8 @@ drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
 	if (info->type & DRM_MODE_TYPE_PREFERRED)
 		mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
 
+	mode->base.aspect_ratio = drm_to_weston_mode_aspect_ratio(info->flags);
+
 	wl_list_insert(output->base.mode_list.prev, &mode->base.link);
 
 	return mode;
@@ -4605,17 +4677,35 @@ drm_output_choose_initial_mode(struct drm_backend *backend,
 	struct drm_mode *preferred = NULL;
 	struct drm_mode *current = NULL;
 	struct drm_mode *configured = NULL;
+	struct drm_mode *config_fall_back = NULL;
 	struct drm_mode *best = NULL;
 	struct drm_mode *drm_mode;
 	drmModeModeInfo drm_modeline;
 	int32_t width = 0;
 	int32_t height = 0;
 	uint32_t refresh = 0;
+	uint32_t aspect_width = 0;
+	uint32_t aspect_height = 0;
+	enum weston_mode_aspect_ratio aspect_ratio = WESTON_MODE_PIC_AR_NONE;
 	int n;
 
 	if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
-		n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
-		if (n != 2 && n != 3) {
+		n = sscanf(modeline, "%dx%d@%d %u:%u", &width, &height,
+			   &refresh, &aspect_width, &aspect_height);
+		if (backend->aspect_ratio_supported && n == 5) {
+			if (aspect_width == 4 && aspect_height == 3)
+				aspect_ratio = WESTON_MODE_PIC_AR_4_3;
+			else if (aspect_width == 16 && aspect_height == 9)
+				aspect_ratio = WESTON_MODE_PIC_AR_16_9;
+			else if (aspect_width == 64 && aspect_height == 27)
+				aspect_ratio = WESTON_MODE_PIC_AR_64_27;
+			else if (aspect_width == 256 && aspect_height == 135)
+				aspect_ratio = WESTON_MODE_PIC_AR_256_135;
+			else
+				weston_log("Invalid modeline \"%s\" for output %s\n",
+					   modeline, output->base.name);
+		}
+		if (n != 2 && n != 3 && n != 5) {
 			width = -1;
 
 			if (parse_modeline(modeline, &drm_modeline) == 0) {
@@ -4632,8 +4722,13 @@ drm_output_choose_initial_mode(struct drm_backend *backend,
 	wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
 		if (width == drm_mode->base.width &&
 		    height == drm_mode->base.height &&
-		    (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
-			configured = drm_mode;
+		    (refresh == 0 || refresh == drm_mode->mode_info.vrefresh)) {
+			if (!backend->aspect_ratio_supported ||
+			    aspect_ratio == drm_mode->base.aspect_ratio)
+				configured = drm_mode;
+			else
+				config_fall_back = drm_mode;
+		}
 
 		if (memcmp(current_mode, &drm_mode->mode_info,
 			   sizeof *current_mode) == 0)
@@ -4657,6 +4752,9 @@ drm_output_choose_initial_mode(struct drm_backend *backend,
 	if (configured)
 		return configured;
 
+	if (config_fall_back)
+		return config_fall_back;
+
 	if (preferred)
 		return preferred;
 
@@ -5039,12 +5137,15 @@ drm_output_print_modes(struct drm_output *output)
 {
 	struct weston_mode *m;
 	struct drm_mode *dm;
+	const char *aspect_ratio;
 
 	wl_list_for_each(m, &output->base.mode_list, link) {
 		dm = to_drm_mode(m);
 
-		weston_log_continue(STAMP_SPACE "%dx%d@%.1f%s%s, %.1f MHz\n",
+		aspect_ratio = aspect_ratio_to_string(m->aspect_ratio);
+		weston_log_continue(STAMP_SPACE "%dx%d@%.1f%s%s%s, %.1f MHz\n",
 				    m->width, m->height, m->refresh / 1000.0,
+				    aspect_ratio,
 				    m->flags & WL_OUTPUT_MODE_PREFERRED ?
 				    ", preferred" : "",
 				    m->flags & WL_OUTPUT_MODE_CURRENT ?
diff --git a/libweston/compositor.h b/libweston/compositor.h
index fbf8a73d..fd0ff7b5 100644
--- a/libweston/compositor.h
+++ b/libweston/compositor.h
@@ -99,8 +99,23 @@ enum weston_led {
 	LED_SCROLL_LOCK = (1 << 2),
 };
 
+enum weston_mode_aspect_ratio {
+	/** The picture aspect ratio values, for the aspect_ratio field of
+	 *  weston_mode. The values here, are taken from
+	 *  DRM_MODE_PICTURE_ASPECT_* from drm_mode.h.
+	 */
+	WESTON_MODE_PIC_AR_NONE = 0,	/* DRM_MODE_PICTURE_ASPECT_NONE */
+	WESTON_MODE_PIC_AR_4_3 = 1,	/* DRM_MODE_PICTURE_ASPECT_4_3 */
+	WESTON_MODE_PIC_AR_16_9 = 2,	/* DRM_MODE_PICTURE_ASPECT_16_9 */
+	WESTON_MODE_PIC_AR_64_27 = 3,	/* DRM_MODE_PICTURE_ASPECT_64_27 */
+	WESTON_MODE_PIC_AR_256_135 = 4,	/* DRM_MODE_PICTURE_ASPECT_256_135*/
+};
+
+
+
 struct weston_mode {
 	uint32_t flags;
+	enum weston_mode_aspect_ratio aspect_ratio;
 	int32_t width, height;
 	uint32_t refresh;
 	struct wl_list link;
-- 
2.17.1



More information about the wayland-devel mailing list