egl: Parse image attributes with _eglParseImageAttribList.
authorChia-I Wu <olv@lunarg.com>
Fri, 22 Oct 2010 09:09:40 +0000 (17:09 +0800)
committerChia-I Wu <olv@lunarg.com>
Fri, 22 Oct 2010 10:35:09 +0000 (18:35 +0800)
Avoid code duplications.

src/egl/drivers/dri2/egl_dri2.c
src/egl/main/eglimage.c
src/egl/main/eglimage.h
src/gallium/state_trackers/egl/common/egl_g3d_image.c

index f2b532d92c86424b05bff5f065445e44c3d7b6b9..eb8996f7a7551bfc1463024d5ba761554a5c5bf7 100644 (file)
@@ -1659,56 +1659,28 @@ dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_image *dri2_img;
-   EGLint width, height, format, name, stride, pitch, i, err;
+   EGLint format, name, pitch, err;
+   _EGLImageAttribs attrs;
 
    (void) ctx;
 
    name = (EGLint) buffer;
 
-   err = EGL_SUCCESS;
-   width = 0;
-   height = 0;
-   format = 0;
-   stride = 0;
-
-   for (i = 0; attr_list[i] != EGL_NONE; i++) {
-      EGLint attr = attr_list[i++];
-      EGLint val = attr_list[i];
-
-      switch (attr) {
-      case EGL_WIDTH:
-        width = val;
-         break;
-      case EGL_HEIGHT:
-        height = val;
-         break;
-      case EGL_DRM_BUFFER_FORMAT_MESA:
-        format = val;
-         break;
-      case EGL_DRM_BUFFER_STRIDE_MESA:
-        stride = val;
-         break;
-      default:
-         err = EGL_BAD_ATTRIBUTE;
-         break;
-      }
-
-      if (err != EGL_SUCCESS) {
-         _eglLog(_EGL_WARNING, "bad image attribute 0x%04x", attr);
-        return NULL;
-      }
-   }
+   err = _eglParseImageAttribList(&attrs, disp, attr_list);
+   if (err != EGL_SUCCESS)
+      return NULL;
 
-   if (width <= 0 || height <= 0 || stride <= 0) {
+   if (attrs.Width <= 0 || attrs.Height <= 0 ||
+       attrs.DRMBufferStrideMESA <= 0) {
       _eglError(EGL_BAD_PARAMETER,
                "bad width, height or stride");
       return NULL;
    }
 
-   switch (format) {
+   switch (attrs.DRMBufferFormatMESA) {
    case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
       format = __DRI_IMAGE_FORMAT_ARGB8888;
-      pitch = stride;
+      pitch = attrs.DRMBufferStrideMESA;
       break;
    default:
       _eglError(EGL_BAD_PARAMETER,
@@ -1729,8 +1701,8 @@ dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
 
    dri2_img->dri_image =
       dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
-                                          width,
-                                          height,
+                                          attrs.Width,
+                                          attrs.Height,
                                           format,
                                           name,
                                           pitch,
@@ -1784,8 +1756,9 @@ dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp,
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_image *dri2_img;
-   int width, height, format, i;
-   unsigned int use, dri_use, valid_mask;
+   _EGLImageAttribs attrs;
+   unsigned int dri_use, valid_mask;
+   int format;
    EGLint err = EGL_SUCCESS;
 
    (void) drv;
@@ -1806,69 +1779,45 @@ dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp,
       goto cleanup_img;
    }
 
-   width = 0;
-   height = 0;
-   format = 0;
-   use = 0;
-   for (i = 0; attr_list[i] != EGL_NONE; i++) {
-      EGLint attr = attr_list[i++];
-      EGLint val = attr_list[i];
-
-      switch (attr) {
-      case EGL_WIDTH:
-        width = val;
-         break;
-      case EGL_HEIGHT:
-        height = val;
-         break;
-      case EGL_DRM_BUFFER_FORMAT_MESA:
-        format = val;
-         break;
-      case EGL_DRM_BUFFER_USE_MESA:
-        use = val;
-         break;
-      default:
-         err = EGL_BAD_ATTRIBUTE;
-         break;
-      }
-
-      if (err != EGL_SUCCESS) {
-         _eglLog(_EGL_WARNING, "bad image attribute 0x%04x", attr);
-        goto cleanup_img;
-      }
-   }
+   err = _eglParseImageAttribList(&attrs, disp, attr_list);
+   if (err != EGL_SUCCESS)
+      goto cleanup_img;
 
-   if (width <= 0 || height <= 0) {
-      _eglLog(_EGL_WARNING, "bad width or height (%dx%d)", width, height);
+   if (attrs.Width <= 0 || attrs.Height <= 0) {
+      _eglLog(_EGL_WARNING, "bad width or height (%dx%d)",
+            attrs.Width, attrs.Height);
       goto cleanup_img;
    }
 
-   switch (format) {
+   switch (attrs.DRMBufferFormatMESA) {
    case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
       format = __DRI_IMAGE_FORMAT_ARGB8888;
       break;
    default:
-      _eglLog(_EGL_WARNING, "bad image format value 0x%04x", format);
+      _eglLog(_EGL_WARNING, "bad image format value 0x%04x",
+            attrs.DRMBufferFormatMESA);
       goto cleanup_img;
    }
 
    valid_mask =
       EGL_DRM_BUFFER_USE_SCANOUT_MESA |
       EGL_DRM_BUFFER_USE_SHARE_MESA; 
-   if (use & ~valid_mask) {
-      _eglLog(_EGL_WARNING, "bad image use bit 0x%04x", use & ~valid_mask);
+   if (attrs.DRMBufferUseMESA & ~valid_mask) {
+      _eglLog(_EGL_WARNING, "bad image use bit 0x%04x",
+            attrs.DRMBufferUseMESA & ~valid_mask);
       goto cleanup_img;
    }
 
    dri_use = 0;
-   if (use & EGL_DRM_BUFFER_USE_SHARE_MESA)
+   if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
       dri_use |= __DRI_IMAGE_USE_SHARE;
-   if (use & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
+   if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
       dri_use |= __DRI_IMAGE_USE_SCANOUT;
 
    dri2_img->dri_image = 
       dri2_dpy->image->createImage(dri2_dpy->dri_screen,
-                                  width, height, format, dri_use, dri2_img);
+                                  attrs.Width, attrs.Height,
+                                   format, dri_use, dri2_img);
    if (dri2_img->dri_image == NULL) {
       err = EGL_BAD_ALLOC;
       goto cleanup_img;
index e11a1836ff6f551c9ea14603756c5b526544d76d..8244ef3b5184673abdebf08e98d10d9bff7ecf39 100644 (file)
@@ -45,6 +45,24 @@ _eglParseImageAttribList(_EGLImageAttribs *attrs, _EGLDisplay *dpy,
       case EGL_GL_TEXTURE_ZOFFSET_KHR:
          attrs->GLTextureZOffset = val;
          break;
+
+      /* EGL_MESA_drm_image */
+      case EGL_WIDTH:
+         attrs->Width = val;
+         break;
+      case EGL_HEIGHT:
+         attrs->Height = val;
+         break;
+      case EGL_DRM_BUFFER_FORMAT_MESA:
+         attrs->DRMBufferFormatMESA = val;
+         break;
+      case EGL_DRM_BUFFER_USE_MESA:
+         attrs->DRMBufferUseMESA = val;
+         break;
+      case EGL_DRM_BUFFER_STRIDE_MESA:
+         attrs->DRMBufferStrideMESA = val;
+         break;
+
       default:
          /* unknown attrs are ignored */
          break;
index 356395a210a3d2ec6183d3eec138039e45df523c..e457bf4ef69618fa2ded6b630a70b610a9e0425e 100644 (file)
@@ -14,6 +14,13 @@ struct _egl_image_attribs
    /* EGL_KHR_gl_image */
    EGLint GLTextureLevel;
    EGLint GLTextureZOffset;
+
+   /* EGL_MESA_drm_image */
+   EGLint Width;
+   EGLint Height;
+   EGLint DRMBufferFormatMESA;
+   EGLint DRMBufferUseMESA;
+   EGLint DRMBufferStrideMESA;
 };
 
 /**
index dcabac6bb4ef6fb8c2ec97dddc818f1062d8a67c..be9c88e5e478654f309791be002914d0db11cfa4 100644 (file)
@@ -74,62 +74,40 @@ egl_g3d_reference_native_pixmap(_EGLDisplay *dpy, EGLNativePixmapType pix)
 #ifdef EGL_MESA_drm_image
 
 static struct pipe_resource *
-egl_g3d_create_drm_buffer(_EGLDisplay *dpy, const EGLint *attribs)
+egl_g3d_create_drm_buffer(_EGLDisplay *dpy, _EGLImage *img,
+                          const EGLint *attribs)
 {
    struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
    struct pipe_screen *screen = gdpy->native->screen;
    struct pipe_resource templ;
-   EGLint width = 0, height = 0, format = 0, use = 0;
-   EGLint valid_use;
-   EGLint i, err = EGL_SUCCESS;
-
-   for (i = 0; attribs[i] != EGL_NONE; i++) {
-      EGLint attr = attribs[i++];
-      EGLint val = attribs[i];
-
-      switch (attr) {
-      case EGL_WIDTH:
-        width = val;
-         break;
-      case EGL_HEIGHT:
-        height = val;
-         break;
-      case EGL_DRM_BUFFER_FORMAT_MESA:
-        format = val;
-         break;
-      case EGL_DRM_BUFFER_USE_MESA:
-        use = val;
-         break;
-      default:
-         err = EGL_BAD_ATTRIBUTE;
-         break;
-      }
+   _EGLImageAttribs attrs;
+   EGLint format, valid_use;
 
-      if (err != EGL_SUCCESS) {
-         _eglLog(_EGL_DEBUG, "bad image attribute 0x%04x", attr);
-         return NULL;
-      }
-   }
+   if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
+      return NULL;
 
-   if (width <= 0 || height <= 0) {
-      _eglLog(_EGL_DEBUG, "bad width or height (%dx%d)", width, height);
+   if (attrs.Width <= 0 || attrs.Height <= 0) {
+      _eglLog(_EGL_DEBUG, "bad width or height (%dx%d)",
+            attrs.Width, attrs.Height);
       return NULL;
    }
 
-   switch (format) {
+   switch (attrs.DRMBufferFormatMESA) {
    case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
       format = PIPE_FORMAT_B8G8R8A8_UNORM;
       break;
    default:
-      _eglLog(_EGL_DEBUG, "bad image format value 0x%04x", format);
+      _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
+            attrs.DRMBufferFormatMESA);
       return NULL;
       break;
    }
 
    valid_use = EGL_DRM_BUFFER_USE_SCANOUT_MESA |
                EGL_DRM_BUFFER_USE_SHARE_MESA;
-   if (use & ~valid_use) {
-      _eglLog(_EGL_DEBUG, "bad image use bit 0x%04x", use);
+   if (attrs.DRMBufferUseMESA & ~valid_use) {
+      _eglLog(_EGL_DEBUG, "bad image use bit 0x%04x",
+            attrs.DRMBufferUseMESA);
       return NULL;
    }
 
@@ -137,18 +115,18 @@ egl_g3d_create_drm_buffer(_EGLDisplay *dpy, const EGLint *attribs)
    templ.target = PIPE_TEXTURE_2D;
    templ.format = format;
    templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
-   templ.width0 = width;
-   templ.height0 = height;
+   templ.width0 = attrs.Width;
+   templ.height0 = attrs.Height;
    templ.depth0 = 1;
 
    /*
     * XXX fix apps (e.g. wayland) and pipe drivers (e.g. i915) and remove the
     * size check
     */
-   if ((use & EGL_DRM_BUFFER_USE_SCANOUT_MESA) &&
-       width >= 640 && height >= 480)
+   if ((attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) &&
+       attrs.Width >= 640 && attrs.Height >= 480)
       templ.bind |= PIPE_BIND_SCANOUT;
-   if (use & EGL_DRM_BUFFER_USE_SHARE_MESA)
+   if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
       templ.bind |= PIPE_BIND_SHARED;
 
    return screen->resource_create(screen, &templ);
@@ -156,59 +134,36 @@ egl_g3d_create_drm_buffer(_EGLDisplay *dpy, const EGLint *attribs)
 
 static struct pipe_resource *
 egl_g3d_reference_drm_buffer(_EGLDisplay *dpy, EGLint name,
-                             const EGLint *attribs)
+                             _EGLImage *img, const EGLint *attribs)
 {
    struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
    struct pipe_screen *screen = gdpy->native->screen;
    struct pipe_resource templ;
    struct winsys_handle wsh;
-   EGLint width = 0, height = 0, format = 0, stride = 0;
-   EGLint i, err = EGL_SUCCESS;
+   _EGLImageAttribs attrs;
+   EGLint format;
 
    /* winsys_handle is in theory platform-specific */
    if (dpy->Platform != _EGL_PLATFORM_DRM)
       return NULL;
 
-   for (i = 0; attribs[i] != EGL_NONE; i++) {
-      EGLint attr = attribs[i++];
-      EGLint val = attribs[i];
-
-      switch (attr) {
-      case EGL_WIDTH:
-        width = val;
-         break;
-      case EGL_HEIGHT:
-        height = val;
-         break;
-      case EGL_DRM_BUFFER_FORMAT_MESA:
-        format = val;
-         break;
-      case EGL_DRM_BUFFER_STRIDE_MESA:
-        stride = val;
-         break;
-      default:
-         err = EGL_BAD_ATTRIBUTE;
-         break;
-      }
-
-      if (err != EGL_SUCCESS) {
-         _eglLog(_EGL_DEBUG, "bad image attribute 0x%04x", attr);
-         return NULL;
-      }
-   }
+   if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
+      return NULL;
 
-   if (width <= 0 || height <= 0 || stride <= 0) {
+   if (attrs.Width <= 0 || attrs.Height <= 0 ||
+       attrs.DRMBufferStrideMESA <= 0) {
       _eglLog(_EGL_DEBUG, "bad width, height, or stride (%dx%dx%d)",
-            width, height, stride);
+            attrs.Width, attrs.Height, attrs.DRMBufferStrideMESA);
       return NULL;
    }
 
-   switch (format) {
+   switch (attrs.DRMBufferFormatMESA) {
    case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
       format = PIPE_FORMAT_B8G8R8A8_UNORM;
       break;
    default:
-      _eglLog(_EGL_DEBUG, "bad image format value 0x%04x", format);
+      _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
+            attrs.DRMBufferFormatMESA);
       return NULL;
       break;
    }
@@ -217,13 +172,13 @@ egl_g3d_reference_drm_buffer(_EGLDisplay *dpy, EGLint name,
    templ.target = PIPE_TEXTURE_2D;
    templ.format = format;
    templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
-   templ.width0 = width;
-   templ.height0 = height;
+   templ.width0 = attrs.Width;
+   templ.height0 = attrs.Height;
    templ.depth0 = 1;
 
    memset(&wsh, 0, sizeof(wsh));
    wsh.handle = (unsigned) name;
-   wsh.stride = stride;
+   wsh.stride = attrs.DRMBufferStrideMESA;
 
    return screen->resource_from_handle(screen, &templ, &wsh);
 }
@@ -257,7 +212,8 @@ egl_g3d_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
       break;
 #ifdef EGL_MESA_drm_image
    case EGL_DRM_BUFFER_MESA:
-      ptex = egl_g3d_reference_drm_buffer(dpy, (EGLint) buffer, attribs);
+      ptex = egl_g3d_reference_drm_buffer(dpy,
+            (EGLint) buffer, &gimg->base, attribs);
       break;
 #endif
    default:
@@ -322,7 +278,7 @@ egl_g3d_create_drm_image(_EGLDriver *drv, _EGLDisplay *dpy,
    }
 
 #ifdef EGL_MESA_drm_image
-   ptex = egl_g3d_create_drm_buffer(dpy, attribs);
+   ptex = egl_g3d_create_drm_buffer(dpy, &gimg->base, attribs);
 #else
    ptex = NULL;
 #endif