[Cogl] [PATCH] check the wayland buffer format when creating texture

Robert Bragg robert at sixbynine.org
Tue May 28 13:33:03 PDT 2013


From: Robert Bragg <robert at linux.intel.com>

The implementation of cogl_wayland_texture_2d_new_from_buffer now uses
eglQueryWaylandBuffer to query the format of the buffer before trying to
create a texture from the buffer.  This makes sure we don't try and
create a texture from YUV buffers for instance that may actually require
multiple textures. We now also report an error when we don't understand
the buffer type or format.
---
 cogl/cogl-texture-2d.c                          | 36 ++++++++++++++++++++++---
 cogl/winsys/cogl-winsys-egl-feature-functions.h |  4 +++
 cogl/winsys/cogl-winsys-egl-private.h           |  8 ++++++
 cogl/winsys/cogl-winsys-egl.c                   | 18 +++++++++++++
 4 files changed, 63 insertions(+), 3 deletions(-)

diff --git a/cogl/cogl-texture-2d.c b/cogl/cogl-texture-2d.c
index 7492e12..e0998e1 100644
--- a/cogl/cogl-texture-2d.c
+++ b/cogl/cogl-texture-2d.c
@@ -262,6 +262,8 @@ cogl_wayland_texture_2d_new_from_buffer (CoglContext *ctx,
                                          struct wl_buffer *buffer,
                                          CoglError **error)
 {
+  int format;
+
   if (wl_buffer_is_shm (buffer))
     {
       int stride = wl_shm_buffer_get_stride (buffer);
@@ -301,14 +303,35 @@ cogl_wayland_texture_2d_new_from_buffer (CoglContext *ctx,
                                             wl_shm_buffer_get_data (buffer),
                                             error);
     }
-  else
+  else if (_cogl_egl_query_wayland_buffer (ctx, buffer,
+                                           EGL_TEXTURE_FORMAT,
+                                           &format))
     {
       EGLImageKHR image;
-      CoglTexture2D *tex;
+      CoglTexture2D *tex = NULL;
+      CoglPixelFormat internal_format;
 
       _COGL_RETURN_VAL_IF_FAIL (_cogl_context_get_winsys (ctx)->constraints &
                                 COGL_RENDERER_CONSTRAINT_USES_EGL,
                                 NULL);
+
+      switch (format)
+        {
+          case EGL_TEXTURE_RGB:
+            internal_format = COGL_PIXEL_FORMAT_RGB_888;
+            break;
+          case EGL_TEXTURE_RGBA:
+            internal_format = COGL_PIXEL_FORMAT_RGBA_8888_PRE;
+            break;
+          default:
+            _cogl_set_error (error,
+                             COGL_SYSTEM_ERROR,
+                             COGL_SYSTEM_ERROR_UNSUPPORTED,
+                             "Can't create texture from unknown "
+                             "wayland buffer format %d\n", format);
+            return NULL;
+        }
+
       image = _cogl_egl_create_image (ctx,
                                       EGL_WAYLAND_BUFFER_WL,
                                       buffer,
@@ -316,12 +339,19 @@ cogl_wayland_texture_2d_new_from_buffer (CoglContext *ctx,
       tex = _cogl_egl_texture_2d_new_from_image (ctx,
                                                  buffer->width,
                                                  buffer->height,
-                                                 COGL_PIXEL_FORMAT_ARGB_8888_PRE,
+                                                 internal_format,
                                                  image,
                                                  error);
       _cogl_egl_destroy_image (ctx, image);
       return tex;
     }
+
+  _cogl_set_error (error,
+                   COGL_SYSTEM_ERROR,
+                   COGL_SYSTEM_ERROR_UNSUPPORTED,
+                   "Can't create texture from unknown "
+                   "wayland buffer type\n");
+  return NULL;
 }
 #endif /* COGL_HAS_WAYLAND_EGL_SERVER_SUPPORT */
 
diff --git a/cogl/winsys/cogl-winsys-egl-feature-functions.h b/cogl/winsys/cogl-winsys-egl-feature-functions.h
index 24cc805..385cf69 100644
--- a/cogl/winsys/cogl-winsys-egl-feature-functions.h
+++ b/cogl/winsys/cogl-winsys-egl-feature-functions.h
@@ -86,6 +86,10 @@ COGL_WINSYS_FEATURE_FUNCTION (EGLImageKHR, eglBindWaylandDisplay,
 COGL_WINSYS_FEATURE_FUNCTION (EGLBoolean, eglUnbindWaylandDisplay,
                               (EGLDisplay dpy,
                                struct wl_display *wayland_display))
+COGL_WINSYS_FEATURE_FUNCTION (EGLBoolean, eglQueryWaylandBuffer,
+                              (EGLDisplay dpy,
+                               struct wl_buffer *buffer,
+                               EGLint attribute, EGLint *value))
 COGL_WINSYS_FEATURE_END ()
 
 COGL_WINSYS_FEATURE_BEGIN (create_context,
diff --git a/cogl/winsys/cogl-winsys-egl-private.h b/cogl/winsys/cogl-winsys-egl-private.h
index 40c4747..0702407 100644
--- a/cogl/winsys/cogl-winsys-egl-private.h
+++ b/cogl/winsys/cogl-winsys-egl-private.h
@@ -158,6 +158,14 @@ _cogl_egl_destroy_image (CoglContext *ctx,
                          EGLImageKHR image);
 #endif
 
+#ifdef EGL_WL_bind_wayland_display
+CoglBool
+_cogl_egl_query_wayland_buffer (CoglContext *ctx,
+                                struct wl_buffer *buffer,
+                                int attribute,
+                                int *value);
+#endif
+
 CoglBool
 _cogl_winsys_egl_renderer_connect_common (CoglRenderer *renderer,
                                           CoglError **error);
diff --git a/cogl/winsys/cogl-winsys-egl.c b/cogl/winsys/cogl-winsys-egl.c
index c81790c..1a1ad52 100644
--- a/cogl/winsys/cogl-winsys-egl.c
+++ b/cogl/winsys/cogl-winsys-egl.c
@@ -993,3 +993,21 @@ _cogl_egl_destroy_image (CoglContext *ctx,
   egl_renderer->pf_eglDestroyImage (egl_renderer->edpy, image);
 }
 #endif
+
+#ifdef EGL_WL_bind_wayland_display
+CoglBool
+_cogl_egl_query_wayland_buffer (CoglContext *ctx,
+                                struct wl_buffer *buffer,
+                                int attribute,
+                                int *value)
+{
+  CoglRendererEGL *egl_renderer = ctx->display->renderer->winsys;
+
+  _COGL_RETURN_IF_FAIL (egl_renderer->pf_eglQueryWaylandBuffer);
+
+  return egl_renderer->pf_eglQueryWaylandBuffer (egl_renderer->edpy,
+                                                 buffer,
+                                                 attribute,
+                                                 value);
+}
+#endif
-- 
1.8.2.1



More information about the Cogl mailing list