From 9129426e8b99ebfcdf3849683b3aed3ebd351f7f Mon Sep 17 00:00:00 2001
From: Jeffy Chen <jeffy.chen@rock-chips.com>
Date: Thu, 6 Dec 2018 18:53:05 +0800
Subject: [PATCH 09/79] HACK: gl-renderer: Workaround dmabuf import/attach for
 old mali

Tested on px3se with libmali-utgard-400-r7p0-r3p0-wayland.so, the
mpp + waylandsink works.

Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
---
 libweston/renderer-gl/gl-renderer-internal.h |  2 ++
 libweston/renderer-gl/gl-renderer.c          | 34 ++++++++++++++++----
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/libweston/renderer-gl/gl-renderer-internal.h b/libweston/renderer-gl/gl-renderer-internal.h
index 72101b4..e7e1693 100644
--- a/libweston/renderer-gl/gl-renderer-internal.h
+++ b/libweston/renderer-gl/gl-renderer-internal.h
@@ -186,6 +186,8 @@ struct gl_renderer {
 	 */
 	struct wl_list shader_list;
 	struct weston_log_scope *shader_scope;
+
+	bool is_mali_egl;
 };
 
 static inline struct gl_renderer *
diff --git a/libweston/renderer-gl/gl-renderer.c b/libweston/renderer-gl/gl-renderer.c
index 7231f73..053fd52 100644
--- a/libweston/renderer-gl/gl-renderer.c
+++ b/libweston/renderer-gl/gl-renderer.c
@@ -2257,6 +2257,14 @@ import_simple_dmabuf(struct gl_renderer *gr,
 	attribs[atti++] = EGL_IMAGE_PRESERVED_KHR;
 	attribs[atti++] = EGL_TRUE;
 
+	/* Old mali needs extra attributes */
+	if (gr->is_mali_egl) {
+		attribs[atti++] = EGL_YUV_COLOR_SPACE_HINT_EXT;
+		attribs[atti++] = EGL_ITU_REC601_EXT;
+		attribs[atti++] = EGL_SAMPLE_RANGE_HINT_EXT;
+		attribs[atti++] = EGL_YUV_NARROW_RANGE_EXT;
+	}
+
 	if (attributes->modifier[0] != DRM_FORMAT_MOD_INVALID) {
 		if (!gr->has_dmabuf_import_modifiers)
 			return NULL;
@@ -2661,7 +2669,7 @@ gl_renderer_query_dmabuf_formats(struct weston_compositor *wc,
 
 	if (!gr->has_dmabuf_import_modifiers ||
 	    !gr->query_dmabuf_formats(gr->egl_display, 0, NULL, &num)) {
-		num = gr->has_gl_texture_rg ? ARRAY_LENGTH(fallback_formats) : 2;
+		num = (gr->has_gl_texture_rg || gr->is_mali_egl) ? ARRAY_LENGTH(fallback_formats) : 2;
 		fallback = true;
 	}
 
@@ -2791,7 +2799,7 @@ dmabuf_is_opaque(struct linux_dmabuf_buffer *dmabuf)
 	return pixel_format_is_opaque(info);
 }
 
-static void
+static int
 gl_renderer_attach_dmabuf(struct weston_surface *surface,
 			  struct weston_buffer *buffer,
 			  struct linux_dmabuf_buffer *dmabuf)
@@ -2805,7 +2813,7 @@ gl_renderer_attach_dmabuf(struct weston_surface *surface,
 	if (!gr->has_dmabuf_import) {
 		linux_dmabuf_buffer_send_server_error(dmabuf,
 				"EGL dmabuf import not supported");
-		return;
+		return -1;
 	}
 
 	buffer->width = dmabuf->attributes.width;
@@ -2839,7 +2847,7 @@ gl_renderer_attach_dmabuf(struct weston_surface *surface,
 	 * Here we release the cache reference which has to be final.
 	 */
 	if (dmabuf->direct_display)
-		return;
+		return -1;
 
 	image = linux_dmabuf_buffer_get_user_data(dmabuf);
 
@@ -2859,6 +2867,8 @@ gl_renderer_attach_dmabuf(struct weston_surface *surface,
 	}
 
 	gs->shader_variant = image->shader_variant;
+
+	return 0;
 }
 
 static const struct weston_drm_format_array *
@@ -2960,7 +2970,16 @@ gl_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
 	else if (gr->has_bind_display &&
 		 gr->query_buffer(gr->egl_display, (void *)buffer->resource,
 				  EGL_TEXTURE_FORMAT, &format))
+	{
+		/* Prefer attaching as dmabuf for old mali */
+		dmabuf = linux_dmabuf_buffer_get(buffer->resource);
+		if (dmabuf && gr->is_mali_egl) {
+			if (!gl_renderer_attach_dmabuf(es, buffer, dmabuf))
+				return;
+		}
+
 		gl_renderer_attach_egl(es, buffer, format);
+	}
 	else if ((dmabuf = linux_dmabuf_buffer_get(buffer->resource)))
 		gl_renderer_attach_dmabuf(es, buffer, dmabuf);
 	else {
@@ -3253,7 +3272,7 @@ gl_renderer_log_extensions(const char *name, const char *extensions)
 }
 
 static void
-log_egl_info(EGLDisplay egldpy)
+log_egl_info(struct gl_renderer *gr, EGLDisplay egldpy)
 {
 	const char *str;
 
@@ -3263,6 +3282,9 @@ log_egl_info(EGLDisplay egldpy)
 	str = eglQueryString(egldpy, EGL_VENDOR);
 	weston_log("EGL vendor: %s\n", str ? str : "(null)");
 
+	if (!strcmp(str, "ARM"))
+		gr->is_mali_egl = 1;
+
 	str = eglQueryString(egldpy, EGL_CLIENT_APIS);
 	weston_log("EGL client APIs: %s\n", str ? str : "(null)");
 
@@ -3681,7 +3703,7 @@ gl_renderer_display_create(struct weston_compositor *ec,
 
 	weston_drm_format_array_init(&gr->supported_formats);
 
-	log_egl_info(gr->egl_display);
+	log_egl_info(gr, gr->egl_display);
 
 	ec->renderer = &gr->base;
 
-- 
2.20.1

