[PATCH] gl-renderer: Create a high priority context

Chris Wilson chris at chris-wilson.co.uk
Thu Mar 1 08:28:30 UTC 2018


EGL_IMG_context_priority allows the client to request that their
rendering be considered high priority. For ourselves, this is important
as we are interactive and any delay in our rendering causes input-output
jitter; a less than smooth user interactive. So if the driver supports
setting the context priority, try and create our EGLContext as high
priority. The driver may reject our request due to system restrictions,
in which case it will fallback to normal priority, but if successful it
will reschedule our rendering and all of its dependencies to execute
earlier, especially important when the GPU is being hogged by background
clients.

Signed-off-by: Chris Wilson <chris at chris-wilson.co.uk>
Cc: Daniel Stone <daniels at collabora.com>
Reviewed-by: Daniel Stone <daniels at collabora.com>
---

The previously mentioned bug affecting i965_dri.so not accepting
CONTEXT_ATTRIB_PRIORITY should now be fixed in the stable release, so
the window of nasty surprises should be closed. :)
-Chris

---
 libweston/gl-renderer.c | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)

diff --git a/libweston/gl-renderer.c b/libweston/gl-renderer.c
index d3ed4a10..a6b29a92 100644
--- a/libweston/gl-renderer.c
+++ b/libweston/gl-renderer.c
@@ -223,6 +223,8 @@ struct gl_renderer {
 	PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
 	int has_bind_display;
 
+	int has_context_priority;
+
 	int has_egl_image_external;
 
 	int has_egl_buffer_age;
@@ -3189,6 +3191,9 @@ gl_renderer_setup_egl_extensions(struct weston_compositor *ec)
 		return -1;
 	}
 
+	if (weston_check_egl_extension(extensions, "EGL_IMG_context_priority"))
+		gr->has_context_priority = 1;
+
 	if (weston_check_egl_extension(extensions, "EGL_WL_bind_wayland_display"))
 		gr->has_bind_display = 1;
 	if (gr->has_bind_display) {
@@ -3631,10 +3636,10 @@ gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
 	EGLConfig context_config;
 	EGLBoolean ret;
 
-	EGLint context_attribs[] = {
+	EGLint context_attribs[16] = {
 		EGL_CONTEXT_CLIENT_VERSION, 0,
-		EGL_NONE
 	};
+	unsigned int nattr = 2;
 
 	if (!eglBindAPI(EGL_OPENGL_ES_API)) {
 		weston_log("failed to bind EGL_OPENGL_ES_API\n");
@@ -3642,6 +3647,21 @@ gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
 		return -1;
 	}
 
+	/*
+	 * Being the compositor we require minimum output latency,
+	 * so request a high priority context for ourselves - that should
+	 * reschedule all of our rendering and its dependencies to be completed
+	 * first. If the driver doesn't permit us to create a high priority
+	 * context, it will fallback to the default priority (MEDIUM).
+	 */
+	if (gr->has_context_priority) {
+		context_attribs[nattr++] = EGL_CONTEXT_PRIORITY_LEVEL_IMG;
+		context_attribs[nattr++] = EGL_CONTEXT_PRIORITY_HIGH_IMG;
+	}
+
+	assert(nattr < ARRAY_LENGTH(context_attribs));
+	context_attribs[nattr] = EGL_NONE;
+
 	context_config = gr->egl_config;
 
 	if (gr->has_configless_context)
@@ -3665,6 +3685,18 @@ gl_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
 		}
 	}
 
+	if (gr->has_context_priority) {
+		EGLint value = EGL_CONTEXT_PRIORITY_MEDIUM_IMG;
+
+		eglQueryContext(gr->egl_display, gr->egl_context,
+				EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value);
+
+		if (value != EGL_CONTEXT_PRIORITY_HIGH_IMG) {
+			weston_log("Failed to obtain a high priority context.\n");
+			/* Not an error, continue on as normal */
+		}
+	}
+
 	ret = eglMakeCurrent(gr->egl_display, egl_surface,
 			     egl_surface, gr->egl_context);
 	if (ret == EGL_FALSE) {
-- 
2.16.2



More information about the wayland-devel mailing list