Re: [Mesa-dev] [v4 10/10] egl: dri2: support for creating images out of dma buffers

2013-05-24 Thread Chad Versace

On 05/23/2013 10:15 PM, Pohjolainen, Topi wrote:

On Thu, May 23, 2013 at 09:39:30PM -0700, Chad Versace wrote:

When touching the src/egl/drivers/dri2 directory, use a commit subject
that looks like "egl/dri2: STUFF", not "egl: dri2: STUFF".


[snip]


+/**
+ * The spec says:
+ *
+ * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target,
+ *  the EGL takes ownership of the file descriptor and is responsible for
+ *  closing it, which it may do at any time while the EGLDisplay is
+ *  initialized."
+ */
+static void
+dri2_take_dma_buf_ownership(const int *fds, unsigned num_fds)
+{
+   int already_closed[num_fds];
+   unsigned num_closed = 0;
+   unsigned i, j;
+
+   for (i = 0; i < num_fds; ++i) {
+  /**
+   * The same file descriptor can be referenced multiple times in case more
+   * than one plane is found in the same buffer, just with a different
+   * offset.
+   */
+  for (j = 0; j < num_closed; ++j) {
+ if (already_closed[j] == fds[i])


The condition above has undefined behavior, ...



There is the explicit counter 'num_closed' telling how many valid elements there
are in 'already_closed'.


My mistake.
___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/mesa-dev


Re: [Mesa-dev] [v4 10/10] egl: dri2: support for creating images out of dma buffers

2013-05-23 Thread Pohjolainen, Topi
On Thu, May 23, 2013 at 09:39:30PM -0700, Chad Versace wrote:
> When touching the src/egl/drivers/dri2 directory, use a commit subject
> that looks like "egl/dri2: STUFF", not "egl: dri2: STUFF".
> 
> On 05/02/2013 12:08 AM, Topi Pohjolainen wrote:
> >v2:
> >- upon success close the given file descriptors
> >
> >v3:
> >- use specific entry for dma buffers instead of the basic for
> >  primes, and enable the extension based on the availability
> >  of the hook
> >
> >Signed-off-by: Topi Pohjolainen 
> >---
> >  src/egl/drivers/dri2/egl_dri2.c | 280 
> > 
> >  1 file changed, 280 insertions(+)
> >
> >diff --git a/src/egl/drivers/dri2/egl_dri2.c 
> >b/src/egl/drivers/dri2/egl_dri2.c
> >index 1011f27..cfa7cf0 100644
> >--- a/src/egl/drivers/dri2/egl_dri2.c
> >+++ b/src/egl/drivers/dri2/egl_dri2.c
> >@@ -34,6 +34,7 @@
> >  #include 
> >  #include 
> >  #include 
> >+#include 
> >  #include 
> >  #include 
> >  #include 
> >@@ -507,6 +508,10 @@ dri2_setup_screen(_EGLDisplay *disp)
> >   disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
> >   disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
> >}
> >+  if (dri2_dpy->image->base.version >= 8 &&
> >+  dri2_dpy->image->createImageFromDmaBufs) {
> >+ disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;
> >+  }
> > }
> >  }
> >
> >@@ -1170,6 +1175,279 @@ dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, 
> >_EGLContext *ctx,
> > return dri2_create_image(disp, dri_image);
> >  }
> >
> >+static EGLBoolean
> >+dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
> >+{
> >+   unsigned i;
> >+
> >+   /**
> >+ * The spec says:
> >+ *
> >+ * "Required attributes and their values are as follows:
> >+ *
> >+ *  * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in 
> >pixels
> >+ *
> >+ *  * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as 
> >specified
> >+ *by drm_fourcc.h and used as the pixel_format parameter of the
> >+ *drm_mode_fb_cmd2 ioctl."
> >+ *
> >+ *  * EGL_DMA_BUF_PLANE0_FD_EXT: The dma_buf file descriptor of plane 0 
> >of
> >+ *the image.
> >+ *
> >+ *  * EGL_DMA_BUF_PLANE0_OFFSET_EXT: The offset from the start of the
> >+ *dma_buf of the first sample in plane 0, in bytes.
> >+ *
> >+ *  * EGL_DMA_BUF_PLANE0_PITCH_EXT: The number of bytes between the 
> >start of
> >+ *subsequent rows of samples in plane 0. May have special meaning 
> >for
> >+ *non-linear formats."
> >+ *
> >+ * "* If  is EGL_LINUX_DMA_BUF_EXT, and the list of attributes 
> >is
> >+ *incomplete, EGL_BAD_PARAMETER is generated."
> >+ */
> >+   if (attrs->Width <= 0 || attrs->Height <= 0 ||
> >+   !attrs->DMABufFourCC.IsPresent ||
> >+   !attrs->DMABufPlaneFds[0].IsPresent ||
> >+   !attrs->DMABufPlaneOffsets[0].IsPresent ||
> >+   !attrs->DMABufPlanePitches[0].IsPresent) {
> >+  _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
> >+  return EGL_FALSE;
> >+   }
> >+
> >+   /**
> >+* Also:
> >+*
> >+* "If  is EGL_LINUX_DMA_BUF_EXT and one or more of the values
> >+*  specified for a plane's pitch or offset isn't supported by EGL,
> >+*  EGL_BAD_ACCESS is generated."
> >+*/
> >+   for (i = 0; i < sizeof(attrs->DMABufPlanePitches) /
> >+   sizeof(attrs->DMABufPlanePitches[0]); ++i) {
> 
> Use ARRAY_SIZE here.

Will do.

>   
> >+  if (attrs->DMABufPlanePitches[i].IsPresent &&
> >+  attrs->DMABufPlanePitches[i].Value <= 0) {
> >+ _eglError(EGL_BAD_ACCESS, "invalid pitch");
> >+ return EGL_FALSE;
> >+  }
> >+   }
> >+
> >+   return EGL_TRUE;
> >+}
> >+
> >+/* Returns the total number of file descriptors zero indicating an error. */
> 
> /* Returns the total number of file descriptors. Zero indicates an error. */

Ok.

> 
> >+static unsigned
> >+dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
> >+{
> >+   switch (attrs->DMABufFourCC.Value) {
> >+   case DRM_FORMAT_RGB332:
> >+   case DRM_FORMAT_BGR233:
> >+   case DRM_FORMAT_XRGB:
> >+   case DRM_FORMAT_XBGR:
> >+   case DRM_FORMAT_RGBX:
> >+   case DRM_FORMAT_BGRX:
> >+   case DRM_FORMAT_ARGB:
> >+   case DRM_FORMAT_ABGR:
> >+   case DRM_FORMAT_RGBA:
> >+   case DRM_FORMAT_BGRA:
> >+   case DRM_FORMAT_XRGB1555:
> >+   case DRM_FORMAT_XBGR1555:
> >+   case DRM_FORMAT_RGBX5551:
> >+   case DRM_FORMAT_BGRX5551:
> >+   case DRM_FORMAT_ARGB1555:
> >+   case DRM_FORMAT_ABGR1555:
> >+   case DRM_FORMAT_RGBA5551:
> >+   case DRM_FORMAT_BGRA5551:
> >+   case DRM_FORMAT_RGB565:
> >+   case DRM_FORMAT_BGR565:
> >+   case DRM_FORMAT_RGB888:
> >+   case DRM_FORMAT_BGR888:
> >+   case DRM_FORMAT_XRGB:
> >+   case DRM_FORMAT_XBGR:
> >+   case DRM_FORMAT_RGBX:
> >+   case DRM_FORMAT_BGRX:
> >+   case DRM_FORMAT_ARGB:
> >+   case DRM

Re: [Mesa-dev] [v4 10/10] egl: dri2: support for creating images out of dma buffers

2013-05-23 Thread Chad Versace

When touching the src/egl/drivers/dri2 directory, use a commit subject
that looks like "egl/dri2: STUFF", not "egl: dri2: STUFF".

On 05/02/2013 12:08 AM, Topi Pohjolainen wrote:

v2:
- upon success close the given file descriptors

v3:
- use specific entry for dma buffers instead of the basic for
  primes, and enable the extension based on the availability
  of the hook

Signed-off-by: Topi Pohjolainen 
---
  src/egl/drivers/dri2/egl_dri2.c | 280 
  1 file changed, 280 insertions(+)

diff --git a/src/egl/drivers/dri2/egl_dri2.c b/src/egl/drivers/dri2/egl_dri2.c
index 1011f27..cfa7cf0 100644
--- a/src/egl/drivers/dri2/egl_dri2.c
+++ b/src/egl/drivers/dri2/egl_dri2.c
@@ -34,6 +34,7 @@
  #include 
  #include 
  #include 
+#include 
  #include 
  #include 
  #include 
@@ -507,6 +508,10 @@ dri2_setup_screen(_EGLDisplay *disp)
   disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
   disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
}
+  if (dri2_dpy->image->base.version >= 8 &&
+  dri2_dpy->image->createImageFromDmaBufs) {
+ disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;
+  }
 }
  }

@@ -1170,6 +1175,279 @@ dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, 
_EGLContext *ctx,
 return dri2_create_image(disp, dri_image);
  }

+static EGLBoolean
+dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
+{
+   unsigned i;
+
+   /**
+ * The spec says:
+ *
+ * "Required attributes and their values are as follows:
+ *
+ *  * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in 
pixels
+ *
+ *  * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as 
specified
+ *by drm_fourcc.h and used as the pixel_format parameter of the
+ *drm_mode_fb_cmd2 ioctl."
+ *
+ *  * EGL_DMA_BUF_PLANE0_FD_EXT: The dma_buf file descriptor of plane 0 of
+ *the image.
+ *
+ *  * EGL_DMA_BUF_PLANE0_OFFSET_EXT: The offset from the start of the
+ *dma_buf of the first sample in plane 0, in bytes.
+ *
+ *  * EGL_DMA_BUF_PLANE0_PITCH_EXT: The number of bytes between the start 
of
+ *subsequent rows of samples in plane 0. May have special meaning for
+ *non-linear formats."
+ *
+ * "* If  is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
+ *incomplete, EGL_BAD_PARAMETER is generated."
+ */
+   if (attrs->Width <= 0 || attrs->Height <= 0 ||
+   !attrs->DMABufFourCC.IsPresent ||
+   !attrs->DMABufPlaneFds[0].IsPresent ||
+   !attrs->DMABufPlaneOffsets[0].IsPresent ||
+   !attrs->DMABufPlanePitches[0].IsPresent) {
+  _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
+  return EGL_FALSE;
+   }
+
+   /**
+* Also:
+*
+* "If  is EGL_LINUX_DMA_BUF_EXT and one or more of the values
+*  specified for a plane's pitch or offset isn't supported by EGL,
+*  EGL_BAD_ACCESS is generated."
+*/
+   for (i = 0; i < sizeof(attrs->DMABufPlanePitches) /
+   sizeof(attrs->DMABufPlanePitches[0]); ++i) {


Use ARRAY_SIZE here.


+  if (attrs->DMABufPlanePitches[i].IsPresent &&
+  attrs->DMABufPlanePitches[i].Value <= 0) {
+ _eglError(EGL_BAD_ACCESS, "invalid pitch");
+ return EGL_FALSE;
+  }
+   }
+
+   return EGL_TRUE;
+}
+
+/* Returns the total number of file descriptors zero indicating an error. */


/* Returns the total number of file descriptors. Zero indicates an error. */


+static unsigned
+dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
+{
+   switch (attrs->DMABufFourCC.Value) {
+   case DRM_FORMAT_RGB332:
+   case DRM_FORMAT_BGR233:
+   case DRM_FORMAT_XRGB:
+   case DRM_FORMAT_XBGR:
+   case DRM_FORMAT_RGBX:
+   case DRM_FORMAT_BGRX:
+   case DRM_FORMAT_ARGB:
+   case DRM_FORMAT_ABGR:
+   case DRM_FORMAT_RGBA:
+   case DRM_FORMAT_BGRA:
+   case DRM_FORMAT_XRGB1555:
+   case DRM_FORMAT_XBGR1555:
+   case DRM_FORMAT_RGBX5551:
+   case DRM_FORMAT_BGRX5551:
+   case DRM_FORMAT_ARGB1555:
+   case DRM_FORMAT_ABGR1555:
+   case DRM_FORMAT_RGBA5551:
+   case DRM_FORMAT_BGRA5551:
+   case DRM_FORMAT_RGB565:
+   case DRM_FORMAT_BGR565:
+   case DRM_FORMAT_RGB888:
+   case DRM_FORMAT_BGR888:
+   case DRM_FORMAT_XRGB:
+   case DRM_FORMAT_XBGR:
+   case DRM_FORMAT_RGBX:
+   case DRM_FORMAT_BGRX:
+   case DRM_FORMAT_ARGB:
+   case DRM_FORMAT_ABGR:
+   case DRM_FORMAT_RGBA:
+   case DRM_FORMAT_BGRA:
+   case DRM_FORMAT_XRGB2101010:
+   case DRM_FORMAT_XBGR2101010:
+   case DRM_FORMAT_RGBX1010102:
+   case DRM_FORMAT_BGRX1010102:
+   case DRM_FORMAT_ARGB2101010:
+   case DRM_FORMAT_ABGR2101010:
+   case DRM_FORMAT_RGBA1010102:
+   case DRM_FORMAT_BGRA1010102:
+   case DRM_FORMAT_YUYV:
+   case DRM_FORMAT_YVYU:
+   case DRM_FORMAT_UYVY:
+   case DRM_FORMAT_VYUY:
+  /* There must be one and only one plane present */
+  if 

[Mesa-dev] [v4 10/10] egl: dri2: support for creating images out of dma buffers

2013-05-02 Thread Topi Pohjolainen
v2:
   - upon success close the given file descriptors

v3:
   - use specific entry for dma buffers instead of the basic for
 primes, and enable the extension based on the availability
 of the hook

Signed-off-by: Topi Pohjolainen 
---
 src/egl/drivers/dri2/egl_dri2.c | 280 
 1 file changed, 280 insertions(+)

diff --git a/src/egl/drivers/dri2/egl_dri2.c b/src/egl/drivers/dri2/egl_dri2.c
index 1011f27..cfa7cf0 100644
--- a/src/egl/drivers/dri2/egl_dri2.c
+++ b/src/egl/drivers/dri2/egl_dri2.c
@@ -34,6 +34,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -507,6 +508,10 @@ dri2_setup_screen(_EGLDisplay *disp)
  disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
  disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
   }
+  if (dri2_dpy->image->base.version >= 8 &&
+  dri2_dpy->image->createImageFromDmaBufs) {
+ disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;
+  }
}
 }
 
@@ -1170,6 +1175,279 @@ dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, 
_EGLContext *ctx,
return dri2_create_image(disp, dri_image);
 }
 
+static EGLBoolean
+dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
+{
+   unsigned i;
+
+   /**
+ * The spec says:
+ *
+ * "Required attributes and their values are as follows:
+ *
+ *  * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in 
pixels
+ *
+ *  * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as 
specified
+ *by drm_fourcc.h and used as the pixel_format parameter of the
+ *drm_mode_fb_cmd2 ioctl."
+ *
+ *  * EGL_DMA_BUF_PLANE0_FD_EXT: The dma_buf file descriptor of plane 0 of
+ *the image.
+ *
+ *  * EGL_DMA_BUF_PLANE0_OFFSET_EXT: The offset from the start of the
+ *dma_buf of the first sample in plane 0, in bytes.
+ * 
+ *  * EGL_DMA_BUF_PLANE0_PITCH_EXT: The number of bytes between the start 
of
+ *subsequent rows of samples in plane 0. May have special meaning for
+ *non-linear formats."
+ *
+ * "* If  is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
+ *incomplete, EGL_BAD_PARAMETER is generated."
+ */
+   if (attrs->Width <= 0 || attrs->Height <= 0 ||
+   !attrs->DMABufFourCC.IsPresent ||
+   !attrs->DMABufPlaneFds[0].IsPresent ||
+   !attrs->DMABufPlaneOffsets[0].IsPresent ||
+   !attrs->DMABufPlanePitches[0].IsPresent) {
+  _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
+  return EGL_FALSE;
+   }
+
+   /**
+* Also:
+*
+* "If  is EGL_LINUX_DMA_BUF_EXT and one or more of the values
+*  specified for a plane's pitch or offset isn't supported by EGL,
+*  EGL_BAD_ACCESS is generated."
+*/
+   for (i = 0; i < sizeof(attrs->DMABufPlanePitches) /
+   sizeof(attrs->DMABufPlanePitches[0]); ++i) {
+  if (attrs->DMABufPlanePitches[i].IsPresent &&
+  attrs->DMABufPlanePitches[i].Value <= 0) {
+ _eglError(EGL_BAD_ACCESS, "invalid pitch");
+ return EGL_FALSE;
+  }
+   }
+
+   return EGL_TRUE;
+}
+
+/* Returns the total number of file descriptors zero indicating an error. */
+static unsigned
+dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
+{
+   switch (attrs->DMABufFourCC.Value) {
+   case DRM_FORMAT_RGB332:
+   case DRM_FORMAT_BGR233:
+   case DRM_FORMAT_XRGB:
+   case DRM_FORMAT_XBGR:
+   case DRM_FORMAT_RGBX:
+   case DRM_FORMAT_BGRX:
+   case DRM_FORMAT_ARGB:
+   case DRM_FORMAT_ABGR:
+   case DRM_FORMAT_RGBA:
+   case DRM_FORMAT_BGRA:
+   case DRM_FORMAT_XRGB1555:
+   case DRM_FORMAT_XBGR1555:
+   case DRM_FORMAT_RGBX5551:
+   case DRM_FORMAT_BGRX5551:
+   case DRM_FORMAT_ARGB1555:
+   case DRM_FORMAT_ABGR1555:
+   case DRM_FORMAT_RGBA5551:
+   case DRM_FORMAT_BGRA5551:
+   case DRM_FORMAT_RGB565:
+   case DRM_FORMAT_BGR565:
+   case DRM_FORMAT_RGB888:
+   case DRM_FORMAT_BGR888:
+   case DRM_FORMAT_XRGB:
+   case DRM_FORMAT_XBGR:
+   case DRM_FORMAT_RGBX:
+   case DRM_FORMAT_BGRX:
+   case DRM_FORMAT_ARGB:
+   case DRM_FORMAT_ABGR:
+   case DRM_FORMAT_RGBA:
+   case DRM_FORMAT_BGRA:
+   case DRM_FORMAT_XRGB2101010:
+   case DRM_FORMAT_XBGR2101010:
+   case DRM_FORMAT_RGBX1010102:
+   case DRM_FORMAT_BGRX1010102:
+   case DRM_FORMAT_ARGB2101010:
+   case DRM_FORMAT_ABGR2101010:
+   case DRM_FORMAT_RGBA1010102:
+   case DRM_FORMAT_BGRA1010102:
+   case DRM_FORMAT_YUYV:
+   case DRM_FORMAT_YVYU:
+   case DRM_FORMAT_UYVY:
+   case DRM_FORMAT_VYUY:
+  /* There must be one and only one plane present */
+  if (attrs->DMABufPlaneFds[0].IsPresent &&
+  attrs->DMABufPlaneOffsets[0].IsPresent &&
+  attrs->DMABufPlanePitches[0].IsPresent &&
+  !attrs->DMABufPlaneFds[1].IsPresent &&
+  !attrs->DMABufPlaneOffsets[1].IsPresent &&
+  !attrs->DMABufPlanePitches[1].IsPresent &&
+