gbm: Add new gbm_bo_import entry point
authorKristian Høgsberg <krh@bitplanet.net>
Fri, 13 Jul 2012 15:19:24 +0000 (11:19 -0400)
committerKristian Høgsberg <krh@bitplanet.net>
Mon, 16 Jul 2012 20:29:15 +0000 (16:29 -0400)
This generalizes and replaces gbm_bo_create_for_egl_image.  gbm_bo_import
will create a gbm_bo from either an EGLImage or a struct wl_buffer.

include/GL/internal/dri_interface.h
src/egl/wayland/wayland-drm/wayland-drm.h
src/gbm/Makefile.am
src/gbm/backends/dri/gbm_dri.c
src/gbm/main/gbm.c
src/gbm/main/gbm.h
src/gbm/main/gbmint.h
src/mesa/drivers/dri/intel/intel_screen.c

index 82e5fde7f033d1363d308e28092001431661ef9d..d3a66c52d2cbc00d901d05228792d6548d99153b 100644 (file)
@@ -912,7 +912,7 @@ struct __DRIdri2ExtensionRec {
  * extensions.
  */
 #define __DRI_IMAGE "DRI_IMAGE"
-#define __DRI_IMAGE_VERSION 4
+#define __DRI_IMAGE_VERSION 5
 
 /**
  * These formats correspond to the similarly named MESA_FORMAT_*
@@ -946,6 +946,8 @@ struct __DRIdri2ExtensionRec {
 #define __DRI_IMAGE_ATTRIB_HANDLE      0x2001
 #define __DRI_IMAGE_ATTRIB_NAME                0x2002
 #define __DRI_IMAGE_ATTRIB_FORMAT      0x2003 /* available in versions 3+ */
+#define __DRI_IMAGE_ATTRIB_WIDTH       0x2004 /* available in versions 5+ */
+#define __DRI_IMAGE_ATTRIB_HEIGHT      0x2005
 
 typedef struct __DRIimageRec          __DRIimage;
 typedef struct __DRIimageExtensionRec __DRIimageExtension;
index 4ef286fe6c56adb80d25cad58598a0b8b2bb7b92..58b9735fb7b55d093ae85df61a39be3176cb96f1 100644 (file)
@@ -1,9 +1,6 @@
 #ifndef WAYLAND_DRM_H
 #define WAYLAND_DRM_H
 
-#include "egldisplay.h"
-#include "eglimage.h"
-
 #include <wayland-server.h>
 #include "wayland-drm-server-protocol.h"
 
index dede5bc1069f39ceb784610e77a3db05e496fd4e..5ca28395a171098bcd7dc77512f2b3fc312b67e5 100644 (file)
@@ -20,6 +20,10 @@ libgbm_la_SOURCES = \
 libgbm_la_LDFLAGS = -version-info 1:0
 libgbm_la_LIBADD = $(LIBUDEV_LIBS) $(DLOPEN_LIBS)
 
+if HAVE_EGL_PLATFORM_WAYLAND
+AM_CPPFLAGS = -DHAVE_WAYLAND_PLATFORM
+endif
+
 if HAVE_DRI
 noinst_LTLIBRARIES = libgbm_dri.la
 libgbm_dri_la_SOURCES = \
index e5ddfb6ce69ebf78074062b4fa9d3838f4367312..3aa0616fe923c836d2fd483a2ba507b9c08c16cc 100644 (file)
 
 #include "gbmint.h"
 
+/* For importing wl_buffer */
+#if HAVE_WAYLAND_PLATFORM
+#include "../../../egl/wayland/wayland-drm/wayland-drm.h"
+#endif
+
 static __DRIimage *
 dri_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
 {
@@ -340,36 +345,76 @@ gbm_dri_to_gbm_format(uint32_t dri_format)
 }
 
 static struct gbm_bo *
-gbm_dri_bo_create_from_egl_image(struct gbm_device *gbm,
-                                 void *egl_dpy, void *egl_img,
-                                 uint32_t width, uint32_t height,
-                                 uint32_t usage)
+gbm_dri_bo_import(struct gbm_device *gbm,
+                  uint32_t type, void *buffer, uint32_t usage)
 {
    struct gbm_dri_device *dri = gbm_dri_device(gbm);
    struct gbm_dri_bo *bo;
-   int dri_format;
+   __DRIimage *image;
    unsigned dri_use = 0;
+   int dri_format, width, height, gbm_format, stride, cpp, offset;
+
+   switch (type) {
+#if HAVE_WAYLAND_PLATFORM
+   case GBM_BO_IMPORT_WL_BUFFER:
+   {
+      struct wl_drm_buffer *wb = (struct wl_drm_buffer *) buffer;
+
+      image = wb->driver_buffer;
+      stride = wb->stride[0];
+      offset = wb->offset[0];
+      cpp = 4;
+      switch (wb->format) {
+      case WL_DRM_FORMAT_XRGB8888:
+         dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
+         gbm_format = GBM_FORMAT_XRGB8888;
+         break;
+      case WL_DRM_FORMAT_ARGB8888:
+         dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
+         gbm_format = GBM_FORMAT_ARGB8888;
+         break;
+      case WL_DRM_FORMAT_YUYV:
+         dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
+         gbm_format = GBM_FORMAT_YUYV;
+         break;
+      default:
+         return NULL;
+      }
+      break;
+   }
+#endif
 
-   (void) egl_dpy;
+   case GBM_BO_IMPORT_EGL_IMAGE:
+   {
+      if (dri->lookup_image == NULL)
+         return NULL;
+
+      image = dri->lookup_image(dri->screen, buffer, dri->lookup_user_data);
+      dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &dri_format);
+      gbm_format = gbm_dri_to_gbm_format(dri_format);
+      dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
+      offset = 0;
+      cpp = 4;
+      break;
+   }
 
-   if (dri->lookup_image == NULL)
+   default:
       return NULL;
+   }
+
 
    bo = calloc(1, sizeof *bo);
    if (bo == NULL)
       return NULL;
 
-   bo->base.base.gbm = gbm;
-   bo->base.base.width = width;
-   bo->base.base.height = height;
+   dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
+   dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT, &height);
+
+   bo->image = dri->image->createSubImage(image,
+                                          width, height, dri_format,
+                                          offset, stride / cpp, NULL);
 
-   __DRIimage *tmp = dri->lookup_image(dri->screen, egl_img,
-                                       dri->lookup_user_data);
 
-   bo->image = dri->image->dupImage(tmp, bo);
-   if (bo->image == NULL)
-      return NULL;
-   
    if (usage & GBM_BO_USE_SCANOUT)
       dri_use |= __DRI_IMAGE_USE_SCANOUT;
    if (usage & GBM_BO_USE_CURSOR_64X64)
@@ -380,14 +425,13 @@ gbm_dri_bo_create_from_egl_image(struct gbm_device *gbm,
       return NULL;
    }
 
+   bo->base.base.gbm = gbm;
+   bo->base.base.width = width;
+   bo->base.base.height = height;
+   bo->base.base.pitch = stride;
+   bo->base.base.format = gbm_format;
    dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
                           &bo->base.base.handle.s32);
-   dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
-                          (int *) &bo->base.base.pitch);
-   dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_FORMAT,
-                         &dri_format);
-
-   bo->base.base.format = gbm_dri_to_gbm_format(dri_format);
 
    return &bo->base.base;
 }
@@ -506,7 +550,7 @@ dri_device_create(int fd)
 
    dri->base.base.fd = fd;
    dri->base.base.bo_create = gbm_dri_bo_create;
-   dri->base.base.bo_create_from_egl_image = gbm_dri_bo_create_from_egl_image;
+   dri->base.base.bo_import = gbm_dri_bo_import;
    dri->base.base.is_format_supported = gbm_dri_is_format_supported;
    dri->base.base.bo_write = gbm_dri_bo_write;
    dri->base.base.bo_destroy = gbm_dri_bo_destroy;
index 3994f86aafc0dcecd08da8d38207c390ef2344ab..2a830336d459023120b45137dbdcf2cb2004021f 100644 (file)
@@ -337,34 +337,34 @@ gbm_bo_create(struct gbm_device *gbm,
 }
 
 /**
- * Create a buffer object representing the contents of an EGLImage
+ * Create a gbm buffer object from an foreign object
+ *
+ * This function imports a foreign object and creates a new gbm bo for it.
+ * This enabled using the foreign object with a display API such as KMS.
+ * Currently two types of foreign objects are supported, indicated by the type
+ * argument:
+ *
+ *   GBM_BO_IMPORT_WL_BUFFER
+ *   GBM_BO_IMPORT_EGL_IMAGE
+ *
+ * The the gbm bo shares the underlying pixels but its life-time is
+ * independent of the foreign object.
  *
  * \param gbm The gbm device returned from gbm_create_device()
- * \param egl_dpy The EGLDisplay on which the EGLImage was created
- * \param egl_image The EGLImage to create the buffer from
- * \param width The width to use in the creation of the buffer object
- * \param height The height to use in the creation of the buffer object
+ * \param gbm The type of object we're importing
+ * \param gbm Pointer to the external object
  * \param usage The union of the usage flags for this buffer
  *
  * \return A newly allocated buffer object that should be freed with
  * gbm_bo_destroy() when no longer needed.
  *
  * \sa enum gbm_bo_flags for the list of usage flags
- *
- * \note The expectation is that this function will use an efficient method
- * for making the contents of the EGLImage available as a buffer object.
  */
 GBM_EXPORT struct gbm_bo *
-gbm_bo_create_from_egl_image(struct gbm_device *gbm,
-                             void *egl_dpy, void *egl_image,
-                             uint32_t width, uint32_t height,
-                             uint32_t usage)
+gbm_bo_import(struct gbm_device *gbm,
+              uint32_t type, void *buffer, uint32_t usage)
 {
-   if (width == 0 || height == 0)
-      return NULL;
-
-   return gbm->bo_create_from_egl_image(gbm, egl_dpy, egl_image,
-                                        width, height, usage);
+   return gbm->bo_import(gbm, type, buffer, usage);
 }
 
 /**
index af5dc5aee8cdff130c240a435d378daee303fedd..52afdf7fd13c1fa15a8d8e4805fec8dff302ed3f 100644 (file)
@@ -230,11 +230,12 @@ gbm_bo_create(struct gbm_device *gbm,
               uint32_t width, uint32_t height,
               uint32_t format, uint32_t flags);
 
+#define GBM_BO_IMPORT_WL_BUFFER         0x5501
+#define GBM_BO_IMPORT_EGL_IMAGE         0x5502
+
 struct gbm_bo *
-gbm_bo_create_from_egl_image(struct gbm_device *gbm,
-                             void *egl_dpy, void *egl_img,
-                             uint32_t width, uint32_t height,
-                             uint32_t usage);
+gbm_bo_import(struct gbm_device *gbm, uint32_t type,
+              void *buffer, uint32_t usage);
 
 uint32_t
 gbm_bo_get_width(struct gbm_bo *bo);
index 8eb8671aeb2cb1105315e8b5ae67c1a9d606bc5d..ab23f0af073aacdfa5348d16e5e8c17a82c6ca5f 100644 (file)
@@ -66,10 +66,8 @@ struct gbm_device {
                                uint32_t width, uint32_t height,
                                uint32_t format,
                                uint32_t usage);
-   struct gbm_bo *(*bo_create_from_egl_image)(struct gbm_device *gbm,
-                                              void *egl_dpy, void *egl_img,
-                                              uint32_t width, uint32_t height,
-                                              uint32_t usage);
+   struct gbm_bo *(*bo_import)(struct gbm_device *gbm, uint32_t type,
+                               void *buffer, uint32_t usage);
    int (*bo_write)(struct gbm_bo *bo, const void *buf, size_t data);
    void (*bo_destroy)(struct gbm_bo *bo);
 
index bcd85e93dc4464a9a2b501885068a4ecdd0cde90..6daeb059ce2f7da076a3800921befcd0bcd0fc11 100644 (file)
@@ -357,7 +357,13 @@ intel_query_image(__DRIimage *image, int attrib, int *value)
    case __DRI_IMAGE_ATTRIB_FORMAT:
       *value = image->dri_format;
       return true;
-   default:
+   case __DRI_IMAGE_ATTRIB_WIDTH:
+      *value = image->region->width;
+      return true;
+   case __DRI_IMAGE_ATTRIB_HEIGHT:
+      *value = image->region->height;
+      return true;
+  default:
       return false;
    }
 }