[Mesa-dev] [PATCH] gallium/dri2: Fix RGB565 EGLImage creation

Nicolas Dufresne nicolas.dufresne at collabora.com
Thu Jan 7 08:53:37 PST 2016


When creating egl images we do a bytes to pixel conversion by deviding
by 4 regardless of the pixel format. This does not work for RGB565. In
this patch, we avoid useless conversion and use proper API when the
conversion cannot be avoided.

Signed-off-by: Nicolas Dufresne <nicolas.dufresne at collabora.com>
---
 src/gallium/state_trackers/dri/dri2.c | 107 +++++++++++++++++-----------------
 1 file changed, 52 insertions(+), 55 deletions(-)

diff --git a/src/gallium/state_trackers/dri/dri2.c b/src/gallium/state_trackers/dri/dri2.c
index a11a6cb..7b5a755 100644
--- a/src/gallium/state_trackers/dri/dri2.c
+++ b/src/gallium/state_trackers/dri/dri2.c
@@ -101,6 +101,31 @@ static int convert_to_fourcc(int format)
    return format;
 }
 
+static enum pipe_format dri2_format_to_pipe_format (int format)
+{
+   enum pipe_format pf;
+
+   switch (format) {
+   case __DRI_IMAGE_FORMAT_RGB565:
+      pf = PIPE_FORMAT_B5G6R5_UNORM;
+      break;
+   case __DRI_IMAGE_FORMAT_XRGB8888:
+      pf = PIPE_FORMAT_BGRX8888_UNORM;
+      break;
+   case __DRI_IMAGE_FORMAT_ARGB8888:
+      pf = PIPE_FORMAT_BGRA8888_UNORM;
+      break;
+   case __DRI_IMAGE_FORMAT_ABGR8888:
+      pf = PIPE_FORMAT_RGBA8888_UNORM;
+      break;
+   default:
+      pf = PIPE_FORMAT_NONE;
+      break;
+   }
+
+   return pf;
+}
+
 /**
  * DRI2 flush extension.
  */
@@ -708,7 +733,7 @@ dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
 static __DRIimage *
 dri2_create_image_from_winsys(__DRIscreen *_screen,
                               int width, int height, int format,
-                              struct winsys_handle *whandle, int pitch,
+                              struct winsys_handle *whandle,
                               void *loaderPrivate)
 {
    struct dri_screen *screen = dri_screen(_screen);
@@ -719,23 +744,7 @@ dri2_create_image_from_winsys(__DRIscreen *_screen,
 
    tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
 
-   switch (format) {
-   case __DRI_IMAGE_FORMAT_RGB565:
-      pf = PIPE_FORMAT_B5G6R5_UNORM;
-      break;
-   case __DRI_IMAGE_FORMAT_XRGB8888:
-      pf = PIPE_FORMAT_BGRX8888_UNORM;
-      break;
-   case __DRI_IMAGE_FORMAT_ARGB8888:
-      pf = PIPE_FORMAT_BGRA8888_UNORM;
-      break;
-   case __DRI_IMAGE_FORMAT_ABGR8888:
-      pf = PIPE_FORMAT_RGBA8888_UNORM;
-      break;
-   default:
-      pf = PIPE_FORMAT_NONE;
-      break;
-   }
+   pf = dri2_format_to_pipe_format (format);
    if (pf == PIPE_FORMAT_NONE)
       return NULL;
 
@@ -753,8 +762,6 @@ dri2_create_image_from_winsys(__DRIscreen *_screen,
    templ.depth0 = 1;
    templ.array_size = 1;
 
-   whandle->stride = pitch * util_format_get_blocksize(pf);
-
    img->texture = screen->base.screen->resource_from_handle(screen->base.screen,
          &templ, whandle);
    if (!img->texture) {
@@ -776,19 +783,26 @@ dri2_create_image_from_name(__DRIscreen *_screen,
                             int name, int pitch, void *loaderPrivate)
 {
    struct winsys_handle whandle;
+   enum pipe_format pf;
 
    memset(&whandle, 0, sizeof(whandle));
    whandle.type = DRM_API_HANDLE_TYPE_SHARED;
    whandle.handle = name;
 
+   pf = dri2_format_to_pipe_format (format);
+   if (pf == PIPE_FORMAT_NONE)
+      return NULL;
+
+   whandle.stride = pitch * util_format_get_blocksize(pf);
+
    return dri2_create_image_from_winsys(_screen, width, height, format,
-                                        &whandle, pitch, loaderPrivate);
+                                        &whandle, loaderPrivate);
 }
 
 static __DRIimage *
 dri2_create_image_from_fd(__DRIscreen *_screen,
                           int width, int height, int format,
-                          int fd, int pitch, void *loaderPrivate)
+                          int fd, int stride, void *loaderPrivate)
 {
    struct winsys_handle whandle;
 
@@ -798,9 +812,10 @@ dri2_create_image_from_fd(__DRIscreen *_screen,
    memset(&whandle, 0, sizeof(whandle));
    whandle.type = DRM_API_HANDLE_TYPE_FD;
    whandle.handle = (unsigned)fd;
+   whandle.stride = stride;
 
    return dri2_create_image_from_winsys(_screen, width, height, format,
-                                        &whandle, pitch, loaderPrivate);
+                                        &whandle, loaderPrivate);
 }
 
 static __DRIimage *
@@ -840,23 +855,8 @@ dri2_create_image(__DRIscreen *_screen,
       tex_usage |= PIPE_BIND_CURSOR;
    }
 
-   switch (format) {
-   case __DRI_IMAGE_FORMAT_RGB565:
-      pf = PIPE_FORMAT_B5G6R5_UNORM;
-      break;
-   case __DRI_IMAGE_FORMAT_XRGB8888:
-      pf = PIPE_FORMAT_BGRX8888_UNORM;
-      break;
-   case __DRI_IMAGE_FORMAT_ARGB8888:
-      pf = PIPE_FORMAT_BGRA8888_UNORM;
-      break;
-   case __DRI_IMAGE_FORMAT_ABGR8888:
-      pf = PIPE_FORMAT_RGBA8888_UNORM;
-      break;
-   default:
-      pf = PIPE_FORMAT_NONE;
-      break;
-   }
+
+   pf = dri2_format_to_pipe_format (format);
    if (pf == PIPE_FORMAT_NONE)
       return NULL;
 
@@ -986,7 +986,8 @@ dri2_from_names(__DRIscreen *screen, int width, int height, int format,
                 void *loaderPrivate)
 {
    __DRIimage *img;
-   int stride, dri_components;
+   int dri_components;
+   struct winsys_handle whandle;
 
    if (num_names != 1)
       return NULL;
@@ -997,11 +998,13 @@ dri2_from_names(__DRIscreen *screen, int width, int height, int format,
    if (format == -1)
       return NULL;
 
-   /* Strides are in bytes not pixels. */
-   stride = strides[0] /4;
+   memset(&whandle, 0, sizeof(whandle));
+   whandle.type = DRM_API_HANDLE_TYPE_SHARED;
+   whandle.handle = names[0];
+   whandle.stride = strides[0];
 
-   img = dri2_create_image_from_name(screen, width, height, format,
-                                     names[0], stride, loaderPrivate);
+   img = dri2_create_image_from_winsys(screen, width, height, format,
+                                        &whandle, loaderPrivate);
    if (img == NULL)
       return NULL;
 
@@ -1101,7 +1104,7 @@ dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
               void *loaderPrivate)
 {
    __DRIimage *img;
-   int format, stride, dri_components;
+   int format, dri_components;
 
    if (num_fds != 1)
       return NULL;
@@ -1112,11 +1115,8 @@ dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
    if (format == -1)
       return NULL;
 
-   /* Strides are in bytes not pixels. */
-   stride = strides[0] /4;
-
    img = dri2_create_image_from_fd(screen, width, height, format,
-                                   fds[0], stride, loaderPrivate);
+                                   fds[0], strides[0], loaderPrivate);
    if (img == NULL)
       return NULL;
 
@@ -1137,7 +1137,7 @@ dri2_from_dma_bufs(__DRIscreen *screen,
                    void *loaderPrivate)
 {
    __DRIimage *img;
-   int format, stride, dri_components;
+   int format, dri_components;
 
    if (num_fds != 1 || offsets[0] != 0) {
       *error = __DRI_IMAGE_ERROR_BAD_MATCH;
@@ -1150,11 +1150,8 @@ dri2_from_dma_bufs(__DRIscreen *screen,
       return NULL;
    }
 
-   /* Strides are in bytes not pixels. */
-   stride = strides[0] /4;
-
    img = dri2_create_image_from_fd(screen, width, height, format,
-                                   fds[0], stride, loaderPrivate);
+                                   fds[0], strides[0], loaderPrivate);
    if (img == NULL) {
       *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
       return NULL;
-- 
2.5.0



More information about the mesa-dev mailing list