st/egl: Add support for EGL_MESA_image_drm.
authorChia-I Wu <olv@lunarg.com>
Wed, 25 Aug 2010 17:49:18 +0000 (01:49 +0800)
committerChia-I Wu <olv@lunarg.com>
Thu, 26 Aug 2010 08:40:01 +0000 (16:40 +0800)
src/gallium/state_trackers/egl/common/egl_g3d.c
src/gallium/state_trackers/egl/common/egl_g3d_api.c
src/gallium/state_trackers/egl/common/egl_g3d_image.c
src/gallium/state_trackers/egl/common/egl_g3d_image.h

index 80929c3a12506b1f30c84ac26dfef1b3f789f42b..2d04695f978fee1658a14dfa3522d4f9eb6b224c 100644 (file)
@@ -537,6 +537,9 @@ egl_g3d_initialize(_EGLDriver *drv, _EGLDisplay *dpy,
    dpy->Extensions.KHR_surfaceless_gles2 = EGL_TRUE;
    dpy->Extensions.KHR_surfaceless_opengl = EGL_TRUE;
 
+   if (dpy->Platform == _EGL_PLATFORM_DRM)
+      dpy->Extensions.MESA_drm_image = EGL_TRUE;
+
    if (egl_g3d_add_configs(drv, dpy, 1) == 1) {
       _eglError(EGL_NOT_INITIALIZED, "eglInitialize(unable to add configs)");
       goto fail;
index fee9e3cadd304daf1a6c09dd588d5b4ed4b26383..3ec53653f449502194f99866eb6c2d6ae539114e 100644 (file)
@@ -809,6 +809,10 @@ egl_g3d_init_driver_api(_EGLDriver *drv)
 
    drv->API.CreateImageKHR = egl_g3d_create_image;
    drv->API.DestroyImageKHR = egl_g3d_destroy_image;
+#ifdef EGL_MESA_drm_image
+   drv->API.CreateDRMImageMESA = egl_g3d_create_drm_image;
+   drv->API.ExportDRMImageMESA = egl_g3d_export_drm_image;
+#endif
 
 #ifdef EGL_KHR_reusable_sync
    drv->API.CreateSyncKHR = egl_g3d_create_sync;
index 1e13cfcf7e9fbdda5bc9ffa3e6614e6986e05c9a..558638e72f014ed2e93bc992a5d7ee132d17b364 100644 (file)
 #include "util/u_rect.h"
 #include "util/u_inlines.h"
 #include "eglcurrent.h"
+#include "egllog.h"
 
 #include "native.h"
 #include "egl_g3d.h"
 #include "egl_g3d_api.h"
 #include "egl_g3d_image.h"
 
+/* move this to native display? */
+#include "state_tracker/drm_driver.h"
+
 /**
  * Reference and return the front left buffer of the native pixmap.
  */
@@ -67,6 +71,165 @@ egl_g3d_reference_native_pixmap(_EGLDisplay *dpy, EGLNativePixmapType pix)
    return textures[natt];
 }
 
+#ifdef EGL_MESA_drm_image
+
+static struct pipe_resource *
+egl_g3d_create_drm_buffer(_EGLDisplay *dpy, 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;
+      }
+
+      if (err != EGL_SUCCESS) {
+         _eglLog(_EGL_DEBUG, "bad image attribute 0x%04x", attr);
+         return NULL;
+      }
+   }
+
+   if (width <= 0 || height <= 0) {
+      _eglLog(_EGL_DEBUG, "bad width or height (%dx%d)", width, height);
+      return NULL;
+   }
+
+   switch (format) {
+   case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
+      format = PIPE_FORMAT_B8G8R8A8_UNORM;
+      break;
+   default:
+      _eglLog(_EGL_DEBUG, "bad image format value 0x%04x", format);
+      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);
+      return NULL;
+   }
+
+   memset(&templ, 0, sizeof(templ));
+   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.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)
+      templ.bind |= PIPE_BIND_SCANOUT;
+   if (use & EGL_DRM_BUFFER_USE_SHARE_MESA)
+      templ.bind |= PIPE_BIND_SHARED;
+
+   return screen->resource_create(screen, &templ);
+}
+
+static struct pipe_resource *
+egl_g3d_reference_drm_buffer(_EGLDisplay *dpy, EGLint name,
+                             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;
+
+   /* 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 (width <= 0 || height <= 0 || stride <= 0) {
+      _eglLog(_EGL_DEBUG, "bad width, height, or stride (%dx%dx%d)",
+            width, height, stride);
+      return NULL;
+   }
+
+   switch (format) {
+   case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
+      format = PIPE_FORMAT_B8G8R8A8_UNORM;
+      break;
+   default:
+      _eglLog(_EGL_DEBUG, "bad image format value 0x%04x", format);
+      return NULL;
+      break;
+   }
+
+   memset(&templ, 0, sizeof(templ));
+   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.depth0 = 1;
+
+   memset(&wsh, 0, sizeof(wsh));
+   wsh.handle = (unsigned) name;
+   wsh.stride = stride;
+
+   return screen->resource_from_handle(screen, &templ, &wsh);
+}
+
+#endif /* EGL_MESA_drm_image */
+
 _EGLImage *
 egl_g3d_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
                      EGLenum target, EGLClientBuffer buffer,
@@ -92,6 +255,11 @@ egl_g3d_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
       ptex = egl_g3d_reference_native_pixmap(dpy,
             (EGLNativePixmapType) buffer);
       break;
+#ifdef EGL_MESA_drm_image
+   case EGL_DRM_BUFFER_MESA:
+      ptex = egl_g3d_reference_drm_buffer(dpy, (EGLint) buffer, attribs);
+      break;
+#endif
    default:
       ptex = NULL;
       break;
@@ -134,3 +302,80 @@ egl_g3d_destroy_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img)
 
    return EGL_TRUE;
 }
+
+_EGLImage *
+egl_g3d_create_drm_image(_EGLDriver *drv, _EGLDisplay *dpy,
+                         const EGLint *attribs)
+{
+   struct egl_g3d_image *gimg;
+   struct pipe_resource *ptex;
+
+   gimg = CALLOC_STRUCT(egl_g3d_image);
+   if (!gimg) {
+      _eglError(EGL_BAD_ALLOC, "eglCreateDRMImageKHR");
+      return NULL;
+   }
+
+   if (!_eglInitImage(&gimg->base, dpy, attribs)) {
+      FREE(gimg);
+      return NULL;
+   }
+
+#ifdef EGL_MESA_drm_image
+   ptex = egl_g3d_create_drm_buffer(dpy, attribs);
+#else
+   ptex = NULL;
+#endif
+   if (!ptex) {
+      FREE(gimg);
+      return NULL;
+   }
+
+   /* transfer the ownership to the image */
+   gimg->texture = ptex;
+   gimg->face = 0;
+   gimg->level = 0;
+   gimg->zslice = 0;
+
+   return &gimg->base;
+}
+
+EGLBoolean
+egl_g3d_export_drm_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img,
+                        EGLint *name, EGLint *handle, EGLint *stride)
+{
+   struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
+   struct egl_g3d_image *gimg = egl_g3d_image(img);
+   struct pipe_screen *screen = gdpy->native->screen;
+   struct winsys_handle wsh;
+
+   /* winsys_handle is in theory platform-specific */
+   if (dpy->Platform != _EGL_PLATFORM_DRM)
+      return EGL_FALSE;
+
+   /* get shared handle */
+   if (name) {
+      memset(&handle, 0, sizeof(handle));
+      wsh.type = DRM_API_HANDLE_TYPE_SHARED;
+      if (!screen->resource_get_handle(screen, gimg->texture, &wsh)) {
+         return EGL_FALSE;
+      }
+
+      *name = wsh.handle;
+   }
+
+   /* get KMS handle */
+   if (handle || stride) {
+      memset(&wsh, 0, sizeof(wsh));
+      wsh.type = DRM_API_HANDLE_TYPE_KMS;
+      if (!screen->resource_get_handle(screen, gimg->texture, &wsh))
+         return EGL_FALSE;
+
+      if (handle)
+         *handle = wsh.handle;
+      if (stride)
+         *stride = wsh.stride;
+   }
+
+   return EGL_TRUE;
+}
index adda9333715d69d3259f2f00aa2aae17a482b4c2..f051da82837da5cad789f405ce6788743bb38942 100644 (file)
@@ -39,4 +39,12 @@ egl_g3d_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
 EGLBoolean
 egl_g3d_destroy_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *image);
 
+_EGLImage *
+egl_g3d_create_drm_image(_EGLDriver *drv, _EGLDisplay *dpy,
+                         const EGLint *attribs);
+
+EGLBoolean
+egl_g3d_export_drm_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img,
+                        EGLint *name, EGLint *handle, EGLint *stride);
+
 #endif /* _EGL_G3D_IMAGE_H_ */