DRI3: Add checking of size and pitch modify libtbm. 74/28374/1
authorRoman Marchenko <r.marchenko@samsung.com>
Mon, 6 Oct 2014 07:37:45 +0000 (10:37 +0300)
committerRoman Marchenko <r.marchenko@samsung.com>
Mon, 6 Oct 2014 07:39:20 +0000 (10:39 +0300)
Move code(export and import functions) from driver to libtbm.
Code formatting.

Change-Id: I5a24923c7282034d269d4aa92e3bf5baa3548886
Signed-off-by: Roman Marchenko <r.marchenko@samsung.com>
src/accel/sec_dri3.c

index cc9b6b3..fb55bb6 100644 (file)
 #include "sec_accel.h"
 
 // -------------------------- Private functions--------------------------------
-/* TODO : _boCreateFromFd and  int _tbm_bo_import_fd mast be moved to _tbm_bo_export_fd */
 
-tbm_bo _tbm_bo_import_fd(int drm_fd, tbm_bufmgr bufmgr, int prime_fd, int size)
+// -------------------------- Callback functions--------------------------------
+static int
+SECDRI3Open(ScreenPtr screen, RRProviderPtr provider, int *fdp)
 {
-       //getting handle from fd
-       unsigned int gem = 0;
-       if (drmPrimeFDToHandle(drm_fd, prime_fd, &gem))
-       {
-               return NULL;
-       }
-
-       //getting name from handle(gem)
-    struct drm_gem_flink arg_flink = {0,};
-    arg_flink.handle = gem;
-    if (drmIoctl(drm_fd, DRM_IOCTL_GEM_FLINK, &arg_flink))
+    ScrnInfoPtr pScrn = xf86ScreenToScrn(screen);
+    SECPtr pExynos = SECPTR(pScrn);
+    drm_magic_t magic;
+    int fd;
+
+    /* Open the device for the client */
+    fd = open(pExynos->drm_device_name, O_RDWR | O_CLOEXEC);
+    if (fd == -1 && errno == EINVAL)
+    {
+        fd = open(pExynos->drm_device_name, O_RDWR);
+        if (fd != -1)
+            fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+    }
+
+    if (fd < 0)
+        return BadAlloc;
+
+    /* Go through the auth dance locally */
+    if (drmGetMagic(fd, &magic) < 0)
     {
-       return NULL;
+        close(fd);
+        return BadMatch;
     }
 
-    //creating tbm_bo from name
-    return tbm_bo_import(bufmgr, arg_flink.name);
+    /* And we're done */
+    *fdp = fd;
+    return Success;
 }
 
-int _tbm_bo_export_fd(tbm_bo bo, int *fd)
+int
+_check_bpp(CARD8 bpp)
 {
-       tbm_bo_handle handle = tbm_bo_get_handle(bo, TBM_DEVICE_MM);
-       *fd = handle.s32;
-       return 0;
+    switch (bpp)
+    {
+    case 8:
+    case 16:
+    case 32:
+        return 1;
+    default:
+        return 0;
+    }
 }
 
+static PixmapPtr 
+SECDRI3PixmapFromFd(ScreenPtr pScreen,
+                    int fd,
+                    CARD16 width,
+                    CARD16 height,
+                    CARD16 stride,
+                    CARD8 depth,
+                    CARD8 bpp)
+{
+    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
+    SECPtr pSec = SECPTR(pScrn);
 
-// -------------------------- Callback functions--------------------------------
-static int
-SECDRI3Open(ScreenPtr screen, RRProviderPtr provider, int *fdp) {
-       ScrnInfoPtr pScrn = xf86ScreenToScrn(screen);
-       SECPtr pExynos = SECPTR(pScrn);
-       drm_magic_t magic;
-       int fd;
-
-       /* Open the device for the client */
-       fd = open(pExynos->drm_device_name, O_RDWR | O_CLOEXEC);
-       if (fd == -1 && errno == EINVAL) {
-               fd = open(pExynos->drm_device_name, O_RDWR);
-               if (fd != -1)
-                       fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
-       }
-
-       if (fd < 0)
-               return BadAlloc;
-
-       /* Go through the auth dance locally */
-       if (drmGetMagic(fd, &magic) < 0) {
-               close(fd);
-               return BadMatch;
-       }
-
-       /* And we're done */
-       *fdp = fd;
-       return Success;
-}
+    PixmapPtr pPixmap = NULL;
 
-static PixmapPtr SECDRI3PixmapFromFd (ScreenPtr pScreen,
-                                      int fd,
-                                      CARD16 width,
-                                      CARD16 height,
-                                      CARD16 stride,
-                                      CARD8 depth,
-                                      CARD8 bpp)
-{
-       ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
-       SECPtr pSec = SECPTR(pScrn);
+    XDBG_RETURN_VAL_IF_FAIL((width <= INT16_MAX && height <= INT16_MAX), NULL);
 
+    XDBG_RETURN_VAL_IF_FAIL(((uint32_t )width * bpp <= (uint32_t )stride * 8), NULL);
 
-       PixmapPtr pPixmap = NULL;
+    XDBG_RETURN_VAL_IF_FAIL((depth > 8), NULL);
 
-       if (depth == 1)
-               return NULL;
+    XDBG_RETURN_VAL_IF_FAIL(_check_bpp(bpp), NULL);
 
-    //create bo
-    tbm_bo tbo = _tbm_bo_import_fd(pSec->drm_fd, pSec->tbm_bufmgr,
-                                                               fd, (uint32_t) height * stride);
+    tbm_bo tbo = tbm_bo_import_fd(pSec->tbm_bufmgr, fd);
 
-    if (tbo == NULL)
-       goto no_bo;
+    XDBG_RETURN_VAL_IF_FAIL(tbo != NULL, NULL);
 
-    pPixmap = (*pScreen->CreatePixmap) (pScreen, 0, 0, depth,
-                                                                       CREATE_PIXMAP_USAGE_SUB_FB);
+    uint32_t real_size = tbm_bo_size(tbo);
+    uint32_t target_size = (uint32_t) height * stride;
 
-    if (!pPixmap)
-       goto no_pixmap;
+    if (real_size < target_size)
+    {
+        XDBG_WARNING(MDRI3, "the real size of bo(%p) less then target: %d, %d\n", tbo, real_size, target_size);
+        goto free_bo;
+    }
+
+    pPixmap = (*pScreen->CreatePixmap)(pScreen, 0, 0, depth,
+    CREATE_PIXMAP_USAGE_SUB_FB);
+
+    XDBG_GOTO_IF_FAIL(pPixmap != NULL, free_bo);
 
-       tbm_bo_ref(tbo);
     if (!pScreen->ModifyPixmapHeader(pPixmap, width, height, 0, 0, stride, tbo))
-       goto no_modify;
+        goto free_pix;
 
     return pPixmap;
 
-no_modify:
-       (*pScreen->DestroyPixmap)(pPixmap);
-no_pixmap:
-       tbm_bo_unref(tbo);
-no_bo:
+free_pix:
+    (*pScreen->DestroyPixmap)(pPixmap);
+free_bo:
+    tbm_bo_unref(tbo);
+
     return NULL;
 }
 
-static int SECDRI3FdFromPixmap (ScreenPtr pScreen,
-                                PixmapPtr pPixmap,
-                                CARD16 *stride,
-                                CARD32 *size)
+static int
+SECDRI3FdFromPixmap(ScreenPtr pScreen,
+                    PixmapPtr pPixmap,
+                    CARD16 *stride,
+                    CARD32 *size)
 {
-       SECPixmapPriv * priv = NULL;
-       int fd;
-       int ret;
-
-       priv = exaGetPixmapDriverPrivate(pPixmap);
-       if (!priv)
-               return -1;
-       ret = _tbm_bo_export_fd(priv->bo, &fd);
-       if (ret < 0)
-               return -1;
-       *stride = priv->stride;
-       *size = tbm_bo_size(priv->bo);
-       return fd;
+    SECPixmapPriv * priv = NULL;
+    int fd;
+
+    priv = exaGetPixmapDriverPrivate(pPixmap);
+    if (!priv)
+        return -1;
+    fd = tbm_bo_export_fd(priv->bo);
+    if (fd <= 0)
+        return -1;
+    *stride = priv->stride;
+    *size = tbm_bo_size(priv->bo);
+    return fd;
 }
 
 static dri3_screen_info_rec sec_dri3_screen_info = {