OSDN Git Service

gralloc0_register_buffer: initialize gralloc0 when needed
[android-x86/external-minigbm.git] / cros_gralloc / cros_gralloc_helpers.cc
index afb1a96..a718418 100644 (file)
@@ -6,54 +6,21 @@
 
 #include "cros_gralloc_helpers.h"
 
+#include "i915_private_android.h"
+
 #include <cstdlib>
-#include <cutils/log.h>
-#include <fcntl.h>
-#include <xf86drm.h>
+#include <log/log.h>
+#include <sync/sync.h>
+#include <errno.h>
+#include <unistd.h>
 
-uint64_t cros_gralloc_convert_flags(int flags)
+const char* drmFormat2Str(int drm_format)
 {
-       uint64_t usage = BO_USE_NONE;
-
-       if (flags & GRALLOC_USAGE_CURSOR)
-               usage |= BO_USE_NONE;
-       if ((flags & sw_read()) == GRALLOC_USAGE_SW_READ_RARELY)
-               usage |= BO_USE_SW_READ_RARELY;
-       if ((flags & sw_read()) == GRALLOC_USAGE_SW_READ_OFTEN)
-               usage |= BO_USE_SW_READ_OFTEN;
-       if ((flags & sw_write()) == GRALLOC_USAGE_SW_WRITE_RARELY)
-               usage |= BO_USE_SW_WRITE_RARELY;
-       if ((flags & sw_write()) == GRALLOC_USAGE_SW_WRITE_OFTEN)
-               usage |= BO_USE_SW_WRITE_OFTEN;
-       if (flags & GRALLOC_USAGE_HW_TEXTURE)
-               usage |= BO_USE_RENDERING;
-       if (flags & GRALLOC_USAGE_HW_RENDER)
-               usage |= BO_USE_RENDERING;
-       if (flags & GRALLOC_USAGE_HW_2D)
-               usage |= BO_USE_RENDERING;
-       if (flags & GRALLOC_USAGE_HW_COMPOSER)
-       /* HWC wants to use display hardware, but can defer to OpenGL. */
-               usage |= BO_USE_SCANOUT | BO_USE_RENDERING;
-       if (flags & GRALLOC_USAGE_HW_FB)
-               usage |= BO_USE_SCANOUT | BO_USE_RENDERING;
-       if (flags & GRALLOC_USAGE_EXTERNAL_DISP)
-       /* We're ignoring this flag until we decide what to with display link */
-               usage |= BO_USE_NONE;
-       if (flags & GRALLOC_USAGE_PROTECTED)
-               usage |= BO_USE_PROTECTED;
-       if (flags & GRALLOC_USAGE_HW_VIDEO_ENCODER)
-               /*HACK: See b/30054495 */
-               usage |= BO_USE_SW_READ_OFTEN;
-       if (flags & GRALLOC_USAGE_HW_CAMERA_WRITE)
-               usage |= BO_USE_HW_CAMERA_WRITE;
-       if (flags & GRALLOC_USAGE_HW_CAMERA_READ)
-               usage |= BO_USE_HW_CAMERA_READ;
-       if (flags & GRALLOC_USAGE_HW_CAMERA_ZSL)
-               usage |= BO_USE_HW_CAMERA_ZSL;
-       if (flags & GRALLOC_USAGE_RENDERSCRIPT)
-               usage |= BO_USE_RENDERSCRIPT;
-
-       return usage;
+    static char buf[5];
+    char *pDrmFormat = (char*) &drm_format;
+    snprintf(buf, sizeof(buf), "%c%c%c%c", *pDrmFormat, *(pDrmFormat + 1),
+             *(pDrmFormat + 2), *(pDrmFormat + 3));
+    return buf;
 }
 
 uint32_t cros_gralloc_convert_format(int format)
@@ -79,82 +46,57 @@ uint32_t cros_gralloc_convert_format(int format)
        case HAL_PIXEL_FORMAT_YCbCr_420_888:
                return DRM_FORMAT_FLEX_YCbCr_420_888;
        case HAL_PIXEL_FORMAT_YV12:
-               return DRM_FORMAT_YVU420;
-       }
-
-       return DRM_FORMAT_NONE;
-}
-
-static int32_t cros_gralloc_query_rendernode(struct driver **drv,
-                                            const char *undesired)
-{
+               return DRM_FORMAT_YVU420_ANDROID;
        /*
-        * Create a driver from rendernode while filtering out
-        * the specified undesired driver.
-        *
-        * TODO(gsingh): Enable render nodes on udl/evdi.
+        * Choose DRM_FORMAT_R8 because <system/graphics.h> requires the buffers
+        * with a format HAL_PIXEL_FORMAT_BLOB have a height of 1, and width
+        * equal to their size in bytes.
         */
-
-       int fd;
-       drmVersionPtr version;
-       char const *str = "%s/renderD%d";
-       int32_t num_nodes = 63;
-       int32_t min_node = 128;
-       int32_t max_node = (min_node + num_nodes);
-
-       for (int i = min_node; i < max_node; i++) {
-               char *node;
-
-               if (asprintf(&node, str, DRM_DIR_NAME, i) < 0)
-                       continue;
-
-               fd = open(node, O_RDWR, 0);
-               free(node);
-
-               if (fd < 0)
-                       continue;
-
-               version = drmGetVersion(fd);
-               if (!version)
-                       continue;
-
-               if (undesired && !strcmp(version->name, undesired)) {
-                       drmFreeVersion(version);
-                       continue;
-               }
-
-               drmFreeVersion(version);
-               *drv = drv_create(fd);
-
-               if (*drv)
-                       return CROS_GRALLOC_ERROR_NONE;
+       case HAL_PIXEL_FORMAT_BLOB:
+               return DRM_FORMAT_R8;
        }
 
-       return CROS_GRALLOC_ERROR_NO_RESOURCES;
+       return i915_private_convert_format(format);
 }
 
-int32_t cros_gralloc_rendernode_open(struct driver **drv)
+cros_gralloc_handle_t cros_gralloc_convert_handle(buffer_handle_t handle)
 {
-       int32_t ret;
-       ret = cros_gralloc_query_rendernode(drv, "vgem");
-
-       /* Allow vgem driver if no hardware is found. */
-       if (ret)
-               ret = cros_gralloc_query_rendernode(drv, NULL);
+       auto hnd = reinterpret_cast<cros_gralloc_handle_t>(handle);
+       if (!hnd || hnd->magic != cros_gralloc_magic)
+               return nullptr;
 
-       return ret;
+       return hnd;
 }
 
-int32_t cros_gralloc_validate_handle(struct cros_gralloc_handle *hnd)
+int32_t cros_gralloc_sync_wait(int32_t acquire_fence)
 {
-       if (!hnd || hnd->magic != cros_gralloc_magic())
-               return CROS_GRALLOC_ERROR_BAD_HANDLE;
+       if (acquire_fence < 0)
+               return 0;
+
+       /*
+        * Wait initially for 1000 ms, and then wait indefinitely. The SYNC_IOC_WAIT
+        * documentation states the caller waits indefinitely on the fence if timeout < 0.
+        */
+       int err = sync_wait(acquire_fence, 1000);
+       if (err < 0) {
+               cros_gralloc_error("Timed out on sync wait, err = %s", strerror(errno));
+               err = sync_wait(acquire_fence, -1);
+               if (err < 0) {
+                       cros_gralloc_error("sync wait error = %s", strerror(errno));
+                       return -errno;
+               }
+       }
+
+       err = close(acquire_fence);
+       if (err) {
+               cros_gralloc_error("Unable to close fence fd, err = %s", strerror(errno));
+               return -errno;
+       }
 
-       return CROS_GRALLOC_ERROR_NONE;
+       return 0;
 }
 
-void cros_gralloc_log(const char *prefix, const char *file, int line,
-                     const char *format, ...)
+void cros_gralloc_log(const char *prefix, const char *file, int line, const char *format, ...)
 {
        char buf[50];
        snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);