[Mesa-dev] [PATCH v2] gbm: convert gbm bo format to fourcc format on dma-buf import

Ray Strode halfline at gmail.com
Fri Aug 28 11:50:21 PDT 2015


From: Ray Strode <rstrode at redhat.com>

At the moment if a gbm buffer is imported and the gbm buffer
has an old-style GBM_BO_FORMAT format, the import will crash,
since it's passed directly to DRI functions that expect
a fourcc format (as provided by the newer GBM_FORMAT
definitions)

This commit addresses the problem in two ways:

1) it prevents invalid formats from leading to a crash by
returning EINVAL if the image couldn't be created

2) it translates GBM_BO_FORMAT formats into the comparable
GBM_FORMAT formats.
---
 src/gbm/backends/dri/gbm_dri.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/gbm/backends/dri/gbm_dri.c b/src/gbm/backends/dri/gbm_dri.c
index ccc3cc6..57cdeac 100644
--- a/src/gbm/backends/dri/gbm_dri.c
+++ b/src/gbm/backends/dri/gbm_dri.c
@@ -679,68 +679,84 @@ gbm_dri_bo_import(struct gbm_device *gbm,
       default:
          return NULL;
       }
       break;
    }
 #endif
 
    case GBM_BO_IMPORT_EGL_IMAGE:
    {
       int dri_format;
       if (dri->lookup_image == NULL) {
          errno = EINVAL;
          return NULL;
       }
 
       image = dri->lookup_image(dri->screen, buffer, dri->lookup_user_data);
       image = dri->image->dupImage(image, NULL);
       dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &dri_format);
       gbm_format = gbm_dri_to_gbm_format(dri_format);
       if (gbm_format == 0) {
          errno = EINVAL;
          return NULL;
       }
       break;
    }
 
    case GBM_BO_IMPORT_FD:
    {
       struct gbm_import_fd_data *fd_data = buffer;
       int stride = fd_data->stride, offset = 0;
+      int dri_format;
+
+      switch (fd_data->format) {
+      case GBM_BO_FORMAT_XRGB8888:
+         dri_format = GBM_FORMAT_XRGB8888;
+         break;
+      case GBM_BO_FORMAT_ARGB8888:
+         dri_format = GBM_FORMAT_ARGB8888;
+         break;
+      default:
+         dri_format = fd_data->format;
+      }
 
       image = dri->image->createImageFromFds(dri->screen,
                                              fd_data->width,
                                              fd_data->height,
-                                             fd_data->format,
+                                             dri_format,
                                              &fd_data->fd, 1,
                                              &stride, &offset,
                                              NULL);
+      if (image == NULL) {
+         errno = EINVAL;
+         return NULL;
+      }
       gbm_format = fd_data->format;
       break;
    }
 
    default:
       errno = ENOSYS;
       return NULL;
    }
 
 
    bo = calloc(1, sizeof *bo);
    if (bo == NULL)
       return NULL;
 
    bo->image = image;
 
    if (usage & GBM_BO_USE_SCANOUT)
       dri_use |= __DRI_IMAGE_USE_SCANOUT;
    if (usage & GBM_BO_USE_CURSOR)
       dri_use |= __DRI_IMAGE_USE_CURSOR;
    if (dri->image->base.version >= 2 &&
        !dri->image->validateUsage(bo->image, dri_use)) {
       errno = EINVAL;
       free(bo);
       return NULL;
    }
 
    bo->base.base.gbm = gbm;
    bo->base.base.format = gbm_format;
 
-- 
2.5.0



More information about the mesa-dev mailing list