re-arrange the static functions 90/173090/2
authorSooChan Lim <sc1.lim@samsung.com>
Mon, 19 Mar 2018 11:47:07 +0000 (20:47 +0900)
committerSooChan Lim <sc1.lim@samsung.com>
Tue, 20 Mar 2018 03:40:05 +0000 (03:40 +0000)
Change-Id: I4d7cf09f166fc2e0a6de4080d73d02a90172335d

src/tbm_bufmgr_vc4.c

index 8601f2f..bdf4ddb 100644 (file)
@@ -56,13 +56,13 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #define USE_DMAIMPORT
 #define TBM_COLOR_FORMAT_COUNT 4
 
-#define VC4_DRM_NAME "vc4"
-
 #ifdef DEBUG
 #define LOG_TAG        "TBM_BACKEND"
 #include <dlog.h>
 static int bDebug;
 
+#define VC4_DRM_NAME "vc4"
+
 char *target_name()
 {
        static char app_name[128] = {0, };
@@ -958,224 +958,453 @@ _vc4_bo_handle(tbm_bo_vc4 bo_vc4, int device)
 }
 
 static int
-tbm_vc4_bo_get_size(tbm_backend_bo_data *bo_data, tbm_error_e *error)
+_new_calc_plane_nv12(int width, int height)
 {
-       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
+       int mbX, mbY;
 
-       if (!bo_vc4) {
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return 0;
-       }
+       mbX = DIV_ROUND_UP(width, S5P_FIMV_NUM_PIXELS_IN_MB_ROW);
+       mbY = DIV_ROUND_UP(height, S5P_FIMV_NUM_PIXELS_IN_MB_COL);
 
-       if (error)
-               *error = TBM_ERROR_NONE;
+       if (width * height < S5P_FIMV_MAX_FRAME_SIZE)
+               mbY = (mbY + 1) / 2 * 2;
 
-       return bo_vc4->size;
+       return ((mbX * S5P_FIMV_NUM_PIXELS_IN_MB_COL) * (mbY *
+                       S5P_FIMV_NUM_PIXELS_IN_MB_ROW));
 }
 
-static tbm_backend_bo_data *
-tbm_vc4_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, int size, tbm_bo_memory_type flags, tbm_error_e *error)
+static int
+_calc_yplane_nv12(int width, int height)
 {
-       tbm_bufmgr_vc4 bufmgr_vc4 = (tbm_bufmgr_vc4)bufmgr_data;
-       tbm_bo_vc4 bo_vc4;
-
-       if (bufmgr_vc4 == NULL) {
-               TBM_VC4_ERROR("bufmgr_data is null\n");
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return NULL;
-       }
-
-       bo_vc4 = calloc(1, sizeof(struct _tbm_bo_vc4));
-       if (!bo_vc4) {
-               TBM_VC4_ERROR("fail to allocate the bo_vc4 private\n");
-               if (error)
-                       *error = TBM_ERROR_OUT_OF_MEMORY;
-               return NULL;
-       }
-       bo_vc4->bufmgr_vc4 = bufmgr_vc4;
+       int mbX, mbY;
 
-       struct drm_vc4_create_bo arg = {0, };
+       mbX = SIZE_ALIGN(width + 24, S5P_FIMV_NV12MT_HALIGN);
+       mbY = SIZE_ALIGN(height + 16, S5P_FIMV_NV12MT_VALIGN);
 
-       arg.size = (__u32)size;
-       arg.flags = flags;/*currently no values for the flags,but it may be used in future extension*/
-       if (drmIoctl(bufmgr_vc4->fd, DRM_IOCTL_VC4_CREATE_BO, &arg)) {
-               TBM_VC4_ERROR("Cannot create bo_vc4(flag:%x, size:%d)\n", arg.flags,
-                              (unsigned int)arg.size);
-               free(bo_vc4);
-               if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
-               return NULL;
-       }
+       return SIZE_ALIGN(mbX * mbY, S5P_FIMV_DEC_BUF_ALIGN);
+}
 
-       bo_vc4->fd = bufmgr_vc4->fd;
-       bo_vc4->gem = (unsigned int)arg.handle;
-       bo_vc4->size = size;
-       bo_vc4->flags_tbm = flags;
-       bo_vc4->name = _get_name(bo_vc4->fd, bo_vc4->gem);
+static int
+_calc_uvplane_nv12(int width, int height)
+{
+       int mbX, mbY;
 
-       if (!_bo_init_cache_state(bufmgr_vc4, bo_vc4, 0)) {
-               TBM_VC4_ERROR("fail init cache state(%d)\n", bo_vc4->name);
-               free(bo_vc4);
-               if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
-               return NULL;
-       }
+       mbX = SIZE_ALIGN(width + 16, S5P_FIMV_NV12MT_HALIGN);
+       mbY = SIZE_ALIGN(height + 4, S5P_FIMV_NV12MT_VALIGN);
 
-       pthread_mutex_init(&bo_vc4->mutex, NULL);
+       return SIZE_ALIGN((mbX * mbY) >> 1, S5P_FIMV_DEC_BUF_ALIGN);
+}
 
-       if (bufmgr_vc4->use_dma_fence && !bo_vc4->dmabuf) {
-               struct drm_prime_handle arg = {0, };
+static int
+_new_calc_yplane_nv12(int width, int height)
+{
+       return SIZE_ALIGN(_new_calc_plane_nv12(width,
+                                               height) + S5P_FIMV_D_ALIGN_PLANE_SIZE,
+                          TBM_SURFACE_ALIGNMENT_PLANE_NV12);
+}
 
-               arg.handle = bo_vc4->gem;
-               if (drmIoctl(bo_vc4->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
-                       TBM_VC4_ERROR("Cannot dmabuf=%d\n", bo_vc4->gem);
-                       free(bo_vc4);
-                       if (error)
-                               *error = TBM_ERROR_OPERATION_FAILED;
-                       return NULL;
-               }
-               bo_vc4->dmabuf = arg.fd;
-       }
+static int
+_new_calc_uvplane_nv12(int width, int height)
+{
+       return SIZE_ALIGN((_new_calc_plane_nv12(width,
+                                               height) >> 1) + S5P_FIMV_D_ALIGN_PLANE_SIZE,
+                         TBM_SURFACE_ALIGNMENT_PLANE_NV12);
+}
 
-       /* add bo_vc4 to hash */
-       if (drmHashInsert(bufmgr_vc4->hashBos, bo_vc4->name, (void *)bo_vc4) < 0)
-               TBM_VC4_ERROR("Cannot insert bo_vc4 to Hash(%d)\n", bo_vc4->name);
+static tbm_bufmgr_capability
+tbm_vc4_bufmgr_get_capabilities(tbm_backend_bufmgr_data *bufmgr_data, tbm_error_e *error)
+{
+       tbm_bufmgr_capability capabilities = TBM_BUFMGR_CAPABILITY_NONE;
 
-       TBM_VC4_DEBUG("     bo_vc4:%p, gem:%d(%d), flags:%d(%d), size:%d\n",
-               bo_vc4,
-               bo_vc4->gem, bo_vc4->name,
-           bo_vc4->flags_tbm,
-               bo_vc4->size);
+       capabilities = TBM_BUFMGR_CAPABILITY_SHARE_KEY|TBM_BUFMGR_CAPABILITY_SHARE_FD;
 
        if (error)
                *error = TBM_ERROR_NONE;
 
-       return (tbm_backend_bo_data *)bo_vc4;
+       return capabilities;
 }
 
-static void
-tbm_vc4_bo_free(tbm_backend_bo_data *bo_data)
+static tbm_error_e
+tbm_vc4_bufmgr_bind_native_display(tbm_backend_bufmgr_data *bufmgr_data, tbm_native_display *native_display)
 {
-       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
-       tbm_bo_vc4 temp;
-       tbm_bufmgr_vc4 bufmgr_vc4;
-       char buf[STRERR_BUFSIZE];
-       int ret;
-
-       if (!bo_data)
-               return;
-
-       bufmgr_vc4 = bo_vc4->bufmgr_vc4;
-       if (!bufmgr_vc4)
-               return;
-
-       TBM_VC4_DEBUG("      bo_vc4:%p, gem:%d(%d), fd:%d, size:%d\n",
-           bo_vc4,
-           bo_vc4->gem, bo_vc4->name,
-           bo_vc4->dmabuf,
-           bo_vc4->size);
-
-       if (bo_vc4->pBase) {
-               if (munmap(bo_vc4->pBase, bo_vc4->size) == -1) {
-                       TBM_VC4_ERROR("bo_vc4:%p fail to munmap(%s)\n",
-                                      bo_vc4, strerror_r(errno, buf, STRERR_BUFSIZE));
-               }
-       }
+       tbm_bufmgr_vc4 bufmgr_vc4 = (tbm_bufmgr_vc4)bufmgr_data;
+       VC4_RETURN_VAL_IF_FAIL(bufmgr_vc4 != NULL, TBM_ERROR_INVALID_PARAMETER);
 
-       /* close dmabuf */
-       if (bo_vc4->dmabuf) {
-               close(bo_vc4->dmabuf);
-               bo_vc4->dmabuf = 0;
+       if (!tbm_drm_helper_wl_auth_server_init(native_display, bufmgr_vc4->fd,
+                                          bufmgr_vc4->device_name, 0)) {
+               TBM_VC4_ERROR("fail to tbm_drm_helper_wl_server_init\n");
+               return TBM_ERROR_OPERATION_FAILED;
        }
 
-       /* delete bo_vc4 from hash */
-       ret = drmHashLookup(bufmgr_vc4->hashBos, bo_vc4->name, (void **)&temp);
-       if (ret == 0)
-               drmHashDelete(bufmgr_vc4->hashBos, bo_vc4->name);
-       else
-               TBM_VC4_ERROR("Cannot find bo_vc4 to Hash(%d), ret=%d\n", bo_vc4->name, ret);
-
-       if (temp != bo_vc4)
-               TBM_VC4_ERROR("hashBos probably has several BOs with same name!!!\n");
-
-       _bo_destroy_cache_state(bufmgr_vc4, bo_vc4);
-
-       /* Free gem handle */
-       struct drm_gem_close arg = {0, };
-
-       memset(&arg, 0, sizeof(arg));
-       arg.handle = bo_vc4->gem;
-       if (drmIoctl(bo_vc4->fd, DRM_IOCTL_GEM_CLOSE, &arg))
-               TBM_VC4_ERROR("bo_vc4:%p fail to gem close.(%s)\n",
-                              bo_vc4, strerror_r(errno, buf, STRERR_BUFSIZE));
+       bufmgr_vc4->bind_display = native_display;
 
-       free(bo_vc4);
+       return TBM_ERROR_NONE;
 }
-
-static tbm_backend_bo_data *
-tbm_vc4_bufmgr_import_key(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tbm_error_e *error)
+static tbm_error_e
+tbm_vc4_bufmgr_get_supported_formats(tbm_backend_bufmgr_data *bufmgr_data,
+                                                       uint32_t **formats, uint32_t *num)
 {
        tbm_bufmgr_vc4 bufmgr_vc4 = (tbm_bufmgr_vc4)bufmgr_data;
-       tbm_bo_vc4 bo_vc4;
-       int ret;
+       uint32_t *color_formats;
 
-       if (bufmgr_vc4 == NULL) {
-               TBM_VC4_ERROR("bufmgr_data is null\n");
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return NULL;
-       }
+       VC4_RETURN_VAL_IF_FAIL(bufmgr_vc4 != NULL, TBM_ERROR_INVALID_PARAMETER);
 
-       ret = drmHashLookup(bufmgr_vc4->hashBos, key, (void **)&bo_vc4);
-       if (ret == 0) {
-               if (error)
-                       *error = TBM_ERROR_NONE;
-               return (tbm_backend_bo_data *)bo_vc4;
-       }
+       color_formats = (uint32_t *)calloc(1, sizeof(uint32_t) * TBM_COLOR_FORMAT_COUNT);
+       if (color_formats == NULL)
+               return TBM_ERROR_OUT_OF_MEMORY;
 
-       struct drm_gem_open arg = {0, };
+       memcpy(color_formats, tbm_vc4_color_format_list, sizeof(uint32_t)*TBM_COLOR_FORMAT_COUNT);
 
-       arg.name = key;
-       if (drmIoctl(bufmgr_vc4->fd, DRM_IOCTL_GEM_OPEN, &arg)) {
-               TBM_VC4_ERROR("Cannot open gem name=%d\n", key);
-               if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
-               return NULL;
-       }
+       *formats = color_formats;
+       *num = TBM_COLOR_FORMAT_COUNT;
 
-       bo_vc4 = calloc(1, sizeof(struct _tbm_bo_vc4));
-       if (!bo_vc4) {
-               TBM_VC4_ERROR("fail to allocate the bo_vc4 private\n");
-               if (error)
-                       *error = TBM_ERROR_OUT_OF_MEMORY;
-               return NULL;
-       }
+       TBM_VC4_DEBUG("supported format count = %d\n", *num);
 
-       bo_vc4->fd = bufmgr_vc4->fd;
-       bo_vc4->gem = arg.handle;
-       bo_vc4->size = arg.size;
-       bo_vc4->name = key;
-       bo_vc4->flags_tbm = 0;
+       return TBM_ERROR_NONE;
+}
 
-       if (!_bo_init_cache_state(bufmgr_vc4, bo_vc4, 1)) {
-               TBM_VC4_ERROR("fail init cache state(%d)\n", bo_vc4->name);
-               free(bo_vc4);
-               if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
-               return NULL;
-       }
+static tbm_error_e
+tbm_vc4_bufmgr_get_plane_data(tbm_backend_bufmgr_data *bufmgr_data,
+                                                       tbm_format format, int plane_idx, int width,
+                                                       int height, uint32_t *size, uint32_t *offset,
+                                                       uint32_t *pitch, int *bo_idx)
+{
+       tbm_bufmgr_vc4 bufmgr_vc4 = (tbm_bufmgr_vc4)bufmgr_data;
+       int bpp;
+       int _offset = 0;
+       int _pitch = 0;
+       int _size = 0;
+       int _bo_idx = 0;
 
-       if (!bo_vc4->dmabuf) {
-               struct drm_prime_handle arg = {0, };
+       VC4_RETURN_VAL_IF_FAIL(bufmgr_vc4 != NULL, TBM_ERROR_INVALID_PARAMETER);
+
+       switch (format) {
+               /* 16 bpp RGB */
+       case TBM_FORMAT_XRGB4444:
+       case TBM_FORMAT_XBGR4444:
+       case TBM_FORMAT_RGBX4444:
+       case TBM_FORMAT_BGRX4444:
+       case TBM_FORMAT_ARGB4444:
+       case TBM_FORMAT_ABGR4444:
+       case TBM_FORMAT_RGBA4444:
+       case TBM_FORMAT_BGRA4444:
+       case TBM_FORMAT_XRGB1555:
+       case TBM_FORMAT_XBGR1555:
+       case TBM_FORMAT_RGBX5551:
+       case TBM_FORMAT_BGRX5551:
+       case TBM_FORMAT_ARGB1555:
+       case TBM_FORMAT_ABGR1555:
+       case TBM_FORMAT_RGBA5551:
+       case TBM_FORMAT_BGRA5551:
+       case TBM_FORMAT_RGB565:
+               bpp = 16;
+               _offset = 0;
+               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB);
+               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+               _bo_idx = 0;
+               break;
+               /* 24 bpp RGB */
+       case TBM_FORMAT_RGB888:
+       case TBM_FORMAT_BGR888:
+               bpp = 24;
+               _offset = 0;
+               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB);
+               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+               _bo_idx = 0;
+               break;
+               /* 32 bpp RGB */
+       case TBM_FORMAT_XRGB8888:
+       case TBM_FORMAT_XBGR8888:
+       case TBM_FORMAT_RGBX8888:
+       case TBM_FORMAT_BGRX8888:
+       case TBM_FORMAT_ARGB8888:
+       case TBM_FORMAT_ABGR8888:
+       case TBM_FORMAT_RGBA8888:
+       case TBM_FORMAT_BGRA8888:
+               bpp = 32;
+               _offset = 0;
+               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB);
+               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+               _bo_idx = 0;
+               break;
+
+               /* packed YCbCr */
+       case TBM_FORMAT_YUYV:
+       case TBM_FORMAT_YVYU:
+       case TBM_FORMAT_UYVY:
+       case TBM_FORMAT_VYUY:
+       case TBM_FORMAT_AYUV:
+               bpp = 32;
+               _offset = 0;
+               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+               _bo_idx = 0;
+               break;
+
+               /*
+               * 2 plane YCbCr
+               * index 0 = Y plane, [7:0] Y
+               * index 1 = Cr:Cb plane, [15:0] Cr:Cb little endian
+               * or
+               * index 1 = Cb:Cr plane, [15:0] Cb:Cr little endian
+               */
+       case TBM_FORMAT_NV12:
+               bpp = 12;
+               if (plane_idx == 0) {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = MAX(_calc_yplane_nv12(width, height), _new_calc_yplane_nv12(width,
+                                       height));
+                       _bo_idx = 0;
+               } else if (plane_idx == 1) {
+                       _offset = MAX(_calc_yplane_nv12(width, height), _new_calc_yplane_nv12(width,
+                                       height));
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _size = MAX(_calc_uvplane_nv12(width, height), _new_calc_uvplane_nv12(width,
+                                       height));
+                       _bo_idx = 0;
+               }
+               break;
+       case TBM_FORMAT_NV21:
+               bpp = 12;
+               if (plane_idx == 0) {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+               } else if (plane_idx == 1) {
+                       _offset = width * height;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _size = SIZE_ALIGN(_pitch * (height / 2), TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+               }
+               break;
+
+       case TBM_FORMAT_NV16:
+       case TBM_FORMAT_NV61:
+               bpp = 16;
+               /*if(plane_idx == 0)*/
+               {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 0)
+                               break;
+               }
+               /*else if( plane_idx ==1 )*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+               }
+               break;
+
+               /*
+               * 3 plane YCbCr
+               * index 0: Y plane, [7:0] Y
+               * index 1: Cb plane, [7:0] Cb
+               * index 2: Cr plane, [7:0] Cr
+               * or
+               * index 1: Cr plane, [7:0] Cr
+               * index 2: Cb plane, [7:0] Cb
+               */
+
+               /*
+               * NATIVE_BUFFER_FORMAT_YV12
+               * NATIVE_BUFFER_FORMAT_I420
+               */
+       case TBM_FORMAT_YUV410:
+       case TBM_FORMAT_YVU410:
+               bpp = 9;
+               /*if(plane_idx == 0)*/
+               {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 0)
+                               break;
+               }
+               /*else if(plane_idx == 1)*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width / 4, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 4);
+                       _size = SIZE_ALIGN(_pitch * (height / 4), TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 1)
+                               break;
+               }
+               /*else if (plane_idx == 2)*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width / 4, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 4);
+                       _size = SIZE_ALIGN(_pitch * (height / 4), TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+               }
+               break;
+       case TBM_FORMAT_YUV411:
+       case TBM_FORMAT_YVU411:
+       case TBM_FORMAT_YUV420:
+       case TBM_FORMAT_YVU420:
+               bpp = 12;
+               /*if(plane_idx == 0)*/
+               {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 0)
+                               break;
+               }
+               /*else if(plane_idx == 1)*/
+               {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _size = SIZE_ALIGN(_pitch * (height / 2), TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 1;
+                       if (plane_idx == 1)
+                               break;
+               }
+               /*else if (plane_idx == 2)*/
+               {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _size = SIZE_ALIGN(_pitch * (height / 2), TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 2;
+               }
+               break;
+       case TBM_FORMAT_YUV422:
+       case TBM_FORMAT_YVU422:
+               bpp = 16;
+               /*if(plane_idx == 0)*/
+               {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 0)
+                               break;
+               }
+               /*else if(plane_idx == 1)*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _size = SIZE_ALIGN(_pitch * (height), TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 1)
+                               break;
+               }
+               /*else if (plane_idx == 2)*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _size = SIZE_ALIGN(_pitch * (height), TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+               }
+               break;
+       case TBM_FORMAT_YUV444:
+       case TBM_FORMAT_YVU444:
+               bpp = 24;
+               /*if(plane_idx == 0)*/
+               {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 0)
+                               break;
+               }
+               /*else if(plane_idx == 1)*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 1)
+                               break;
+               }
+               /*else if (plane_idx == 2)*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+               }
+               break;
+       default:
+               bpp = 0;
+               break;
+       }
+
+       *size = _size;
+       *offset = _offset;
+       *pitch = _pitch;
+       *bo_idx = _bo_idx;
+
+       return TBM_ERROR_NONE;
+}
+
+static tbm_backend_bo_data *
+tbm_vc4_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, int size, tbm_bo_memory_type flags, tbm_error_e *error)
+{
+       tbm_bufmgr_vc4 bufmgr_vc4 = (tbm_bufmgr_vc4)bufmgr_data;
+       tbm_bo_vc4 bo_vc4;
+
+       if (bufmgr_vc4 == NULL) {
+               TBM_VC4_ERROR("bufmgr_data is null\n");
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return NULL;
+       }
+
+       bo_vc4 = calloc(1, sizeof(struct _tbm_bo_vc4));
+       if (!bo_vc4) {
+               TBM_VC4_ERROR("fail to allocate the bo_vc4 private\n");
+               if (error)
+                       *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+       }
+       bo_vc4->bufmgr_vc4 = bufmgr_vc4;
+
+       struct drm_vc4_create_bo arg = {0, };
+
+       arg.size = (__u32)size;
+       arg.flags = flags;/*currently no values for the flags,but it may be used in future extension*/
+       if (drmIoctl(bufmgr_vc4->fd, DRM_IOCTL_VC4_CREATE_BO, &arg)) {
+               TBM_VC4_ERROR("Cannot create bo_vc4(flag:%x, size:%d)\n", arg.flags,
+                              (unsigned int)arg.size);
+               free(bo_vc4);
+               if (error)
+                       *error = TBM_ERROR_OPERATION_FAILED;
+               return NULL;
+       }
+
+       bo_vc4->fd = bufmgr_vc4->fd;
+       bo_vc4->gem = (unsigned int)arg.handle;
+       bo_vc4->size = size;
+       bo_vc4->flags_tbm = flags;
+       bo_vc4->name = _get_name(bo_vc4->fd, bo_vc4->gem);
+
+       if (!_bo_init_cache_state(bufmgr_vc4, bo_vc4, 0)) {
+               TBM_VC4_ERROR("fail init cache state(%d)\n", bo_vc4->name);
+               free(bo_vc4);
+               if (error)
+                       *error = TBM_ERROR_OPERATION_FAILED;
+               return NULL;
+       }
+
+       pthread_mutex_init(&bo_vc4->mutex, NULL);
+
+       if (bufmgr_vc4->use_dma_fence && !bo_vc4->dmabuf) {
+               struct drm_prime_handle arg = {0, };
 
                arg.handle = bo_vc4->gem;
                if (drmIoctl(bo_vc4->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
-                       TBM_VC4_ERROR("fail to DRM_IOCTL_PRIME_HANDLE_TO_FD gem=%d\n", bo_vc4->gem);
+                       TBM_VC4_ERROR("Cannot dmabuf=%d\n", bo_vc4->gem);
+                       free(bo_vc4);
                        if (error)
                                *error = TBM_ERROR_OPERATION_FAILED;
-                       free(bo_vc4);
                        return NULL;
                }
                bo_vc4->dmabuf = arg.fd;
@@ -1185,12 +1414,11 @@ tbm_vc4_bufmgr_import_key(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tbm
        if (drmHashInsert(bufmgr_vc4->hashBos, bo_vc4->name, (void *)bo_vc4) < 0)
                TBM_VC4_ERROR("Cannot insert bo_vc4 to Hash(%d)\n", bo_vc4->name);
 
-       TBM_VC4_DEBUG("    bo_vc4:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d\n",
-           bo_vc4,
-           bo_vc4->gem, bo_vc4->name,
-           bo_vc4->dmabuf,
+       TBM_VC4_DEBUG("     bo_vc4:%p, gem:%d(%d), flags:%d(%d), size:%d\n",
+               bo_vc4,
+               bo_vc4->gem, bo_vc4->name,
            bo_vc4->flags_tbm,
-           bo_vc4->size);
+               bo_vc4->size);
 
        if (error)
                *error = TBM_ERROR_NONE;
@@ -1260,890 +1488,661 @@ tbm_vc4_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_e
 
        open_arg.name = name;
        if (drmIoctl(bufmgr_vc4->fd, DRM_IOCTL_GEM_OPEN, &open_arg)) {
-               TBM_VC4_ERROR("Cannot get gem info from gem:%d, fd:%d (%s)\n",
-                              gem, key, strerror_r(errno, buf, STRERR_BUFSIZE));
-               if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
-               return NULL;
-       }
-
-       /* Free gem handle to avoid a memory leak*/
-       struct drm_gem_close close_arg = {0, };
-       memset(&close_arg, 0, sizeof(close_arg));
-       close_arg.handle = open_arg.handle;
-       if (drmIoctl(bufmgr_vc4->fd, DRM_IOCTL_GEM_CLOSE, &close_arg)) {
-               TBM_VC4_ERROR("Cannot close gem_handle.\n",
-                             strerror_r(errno, buf, STRERR_BUFSIZE));
-               if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
-               return NULL;
-       }
-
-       if (real_size == -1)
-               real_size = open_arg.size;
-
-       bo_vc4 = calloc(1, sizeof(struct _tbm_bo_vc4));
-       if (!bo_vc4) {
-               TBM_VC4_ERROR("bo_vc4:%p fail to allocate the bo_vc4\n", bo_vc4);
-               if (error)
-                       *error = TBM_ERROR_OUT_OF_MEMORY;
-               return NULL;
-       }
-       bo_vc4->bufmgr_vc4 = bufmgr_vc4;
-
-       bo_vc4->fd = bufmgr_vc4->fd;
-       bo_vc4->gem = gem;
-       bo_vc4->size = real_size;
-       bo_vc4->flags_tbm = 0;
-       bo_vc4->name = name;
-
-       if (!_bo_init_cache_state(bufmgr_vc4, bo_vc4, 1)) {
-               TBM_VC4_ERROR("fail init cache state(%d)\n", bo_vc4->name);
-               free(bo_vc4);
-               if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
-               return NULL;
-       }
-
-       /* add bo_vc4 to hash */
-       if (drmHashInsert(bufmgr_vc4->hashBos, bo_vc4->name, (void *)bo_vc4) < 0)
-               TBM_VC4_ERROR("bo_vc4:%p Cannot insert bo_vc4 to Hash(%d) from gem:%d, fd:%d\n",
-                              bo_vc4, bo_vc4->name, gem, key);
-
-       TBM_VC4_DEBUG(" bo_vc4:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d\n",
-           bo_vc4,
-           bo_vc4->gem, bo_vc4->name,
-           bo_vc4->dmabuf,
-           key,
-           bo_vc4->flags_tbm,
-           bo_vc4->size);
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return (tbm_backend_bo_data *)bo_vc4;
-}
-
-static tbm_key
-tbm_vc4_bo_export_key(tbm_backend_bo_data *bo_data, tbm_error_e *error)
-{
-       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
-
-       if (!bo_vc4) {
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return 0;
-       }
-
-       if (!bo_vc4->name) {
-               bo_vc4->name = _get_name(bo_vc4->fd, bo_vc4->gem);
-               if (!bo_vc4->name) {
-                       TBM_VC4_ERROR("error Cannot get name\n");
-                       if (error)
-                               *error = TBM_ERROR_INVALID_PARAMETER;
-                       return 0;
-               }
-       }
-
-       TBM_VC4_DEBUG("    bo_vc4:%p, gem:%d(%d), fd:%d, flags:%d, size:%d\n",
-           bo_vc4,
-           bo_vc4->gem, bo_vc4->name,
-           bo_vc4->dmabuf,
-           bo_vc4->flags_tbm,
-           bo_vc4->size);
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return (tbm_key)bo_vc4->name;
-}
-
-static tbm_fd
-tbm_vc4_bo_export_fd(tbm_backend_bo_data *bo_data, tbm_error_e *error)
-{
-       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
-       int ret;
-       char buf[STRERR_BUFSIZE];
-
-       if (!bo_vc4) {
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return -1;
-       }
-
-       struct drm_prime_handle arg = {0, };
-
-       arg.handle = bo_vc4->gem;
-       ret = drmIoctl(bo_vc4->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg);
-       if (ret) {
-               TBM_VC4_ERROR("bo_vc4:%p Cannot dmabuf=%d (%s)\n",
-                              bo_vc4, bo_vc4->gem, strerror_r(errno, buf, STRERR_BUFSIZE));
-               if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
-               return (tbm_fd) ret;
-       }
-
-       TBM_VC4_DEBUG(" bo_vc4:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d\n",
-           bo_vc4,
-           bo_vc4->gem, bo_vc4->name,
-           bo_vc4->dmabuf,
-           arg.fd,
-           bo_vc4->flags_tbm,
-           bo_vc4->size);
-
-       return (tbm_fd)arg.fd;
-}
-
-static tbm_bo_handle
-tbm_vc4_bo_get_handle(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, tbm_error_e *error)
-{
-       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
-       tbm_bo_handle bo_handle;
-
-       if (!bo_vc4) {
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return (tbm_bo_handle) NULL;
-       }
-
-       if (!bo_vc4->gem) {
-               TBM_VC4_ERROR("Cannot map gem=%d\n", bo_vc4->gem);
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return (tbm_bo_handle) NULL;
-       }
-
-       TBM_VC4_DEBUG("bo_vc4:%p, gem:%d(%d), fd:%d, flags:%d, size:%d, %s\n",
-           bo_vc4,
-           bo_vc4->gem, bo_vc4->name,
-           bo_vc4->dmabuf,
-           bo_vc4->flags_tbm,
-           bo_vc4->size,
-           STR_DEVICE[device]);
-
-       /*Get mapped bo_handle*/
-       bo_handle = _vc4_bo_handle(bo_vc4, device);
-       if (bo_handle.ptr == NULL) {
-               TBM_VC4_ERROR("Cannot get handle: gem:%d, device:%d\n",
-                       bo_vc4->gem, device);
-               if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
-               return (tbm_bo_handle) NULL;
-       }
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return bo_handle;
-}
-
-static tbm_bo_handle
-tbm_vc4_bo_map(tbm_backend_bo_data *bo_data, tbm_bo_device_type device,
-                               tbm_bo_access_option opt, tbm_error_e *error)
-{
-       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
-       tbm_bo_handle bo_handle;
-       tbm_bufmgr_vc4 bufmgr_vc4;
-
-       if (!bo_vc4) {
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return (tbm_bo_handle) NULL;
-       }
-
-       bufmgr_vc4 = bo_vc4->bufmgr_vc4;
-       if (!bufmgr_vc4) {
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return (tbm_bo_handle) NULL;
-       }
-
-       if (!bo_vc4->gem) {
-               TBM_VC4_ERROR("Cannot map gem=%d\n", bo_vc4->gem);
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return (tbm_bo_handle) NULL;
-       }
-
-       TBM_VC4_DEBUG("       bo_vc4:%p, gem:%d(%d), fd:%d, %s, %s\n",
-           bo_vc4,
-           bo_vc4->gem, bo_vc4->name,
-           bo_vc4->dmabuf,
-           STR_DEVICE[device],
-           STR_OPT[opt]);
-
-       /*Get mapped bo_handle*/
-       bo_handle = _vc4_bo_handle(bo_vc4, device);
-       if (bo_handle.ptr == NULL) {
-               TBM_VC4_ERROR("Cannot get handle: gem:%d, device:%d, opt:%d\n",
-                              bo_vc4->gem, device, opt);
-               if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
-               return (tbm_bo_handle) NULL;
-       }
-
-       if (bo_vc4->map_cnt == 0)
-               _bo_set_cache_state(bufmgr_vc4, bo_vc4, device, opt);
-
-       bo_vc4->last_map_device = device;
-
-       bo_vc4->map_cnt++;
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return bo_handle;
-}
-
-static tbm_error_e
-tbm_vc4_bo_unmap(tbm_backend_bo_data *bo_data)
-{
-       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
-       tbm_bufmgr_vc4 bufmgr_vc4;
-
-       if (!bo_vc4)
-               return TBM_ERROR_INVALID_PARAMETER;
+               TBM_VC4_ERROR("Cannot get gem info from gem:%d, fd:%d (%s)\n",
+                              gem, key, strerror_r(errno, buf, STRERR_BUFSIZE));
+               if (error)
+                       *error = TBM_ERROR_OPERATION_FAILED;
+               return NULL;
+       }
 
-       bufmgr_vc4 = bo_vc4->bufmgr_vc4;
-       if (!bufmgr_vc4)
-               return TBM_ERROR_INVALID_PARAMETER;
+       /* Free gem handle to avoid a memory leak*/
+       struct drm_gem_close close_arg = {0, };
+       memset(&close_arg, 0, sizeof(close_arg));
+       close_arg.handle = open_arg.handle;
+       if (drmIoctl(bufmgr_vc4->fd, DRM_IOCTL_GEM_CLOSE, &close_arg)) {
+               TBM_VC4_ERROR("Cannot close gem_handle.\n",
+                             strerror_r(errno, buf, STRERR_BUFSIZE));
+               if (error)
+                       *error = TBM_ERROR_OPERATION_FAILED;
+               return NULL;
+       }
 
-       if (!bo_vc4->gem)
-               return TBM_ERROR_INVALID_PARAMETER;
+       if (real_size == -1)
+               real_size = open_arg.size;
 
-       bo_vc4->map_cnt--;
+       bo_vc4 = calloc(1, sizeof(struct _tbm_bo_vc4));
+       if (!bo_vc4) {
+               TBM_VC4_ERROR("bo_vc4:%p fail to allocate the bo_vc4\n", bo_vc4);
+               if (error)
+                       *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+       }
+       bo_vc4->bufmgr_vc4 = bufmgr_vc4;
 
-       if (bo_vc4->map_cnt == 0)
-               _bo_save_cache_state(bufmgr_vc4, bo_vc4);
+       bo_vc4->fd = bufmgr_vc4->fd;
+       bo_vc4->gem = gem;
+       bo_vc4->size = real_size;
+       bo_vc4->flags_tbm = 0;
+       bo_vc4->name = name;
 
-#ifdef ENABLE_CACHECRTL
-       if (bo_vc4->last_map_device == TBM_DEVICE_CPU)
-               _vc4_cache_flush(bufmgr_vc4, bo_vc4, TBM_VC4_CACHE_FLUSH_ALL);
-#endif
+       if (!_bo_init_cache_state(bufmgr_vc4, bo_vc4, 1)) {
+               TBM_VC4_ERROR("fail init cache state(%d)\n", bo_vc4->name);
+               free(bo_vc4);
+               if (error)
+                       *error = TBM_ERROR_OPERATION_FAILED;
+               return NULL;
+       }
 
-       bo_vc4->last_map_device = -1;
+       /* add bo_vc4 to hash */
+       if (drmHashInsert(bufmgr_vc4->hashBos, bo_vc4->name, (void *)bo_vc4) < 0)
+               TBM_VC4_ERROR("bo_vc4:%p Cannot insert bo_vc4 to Hash(%d) from gem:%d, fd:%d\n",
+                              bo_vc4, bo_vc4->name, gem, key);
 
-       TBM_VC4_DEBUG("     bo_vc4:%p, gem:%d(%d), fd:%d\n",
+       TBM_VC4_DEBUG(" bo_vc4:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d\n",
            bo_vc4,
            bo_vc4->gem, bo_vc4->name,
-           bo_vc4->dmabuf);
+           bo_vc4->dmabuf,
+           key,
+           bo_vc4->flags_tbm,
+           bo_vc4->size);
 
-       return TBM_ERROR_NONE;
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return (tbm_backend_bo_data *)bo_vc4;
 }
 
-static tbm_error_e
-tbm_vc4_bo_lock(tbm_backend_bo_data *bo_data, tbm_bo_device_type device,
-                               tbm_bo_access_option opt)
+static tbm_backend_bo_data *
+tbm_vc4_bufmgr_import_key(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tbm_error_e *error)
 {
-#ifndef ALWAYS_BACKEND_CTRL
-       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
-       tbm_bufmgr_vc4 bufmgr_vc4;
-       struct dma_buf_fence fence;
-       struct flock filelock;
-       int ret = 0;
-       char buf[STRERR_BUFSIZE];
-
-       if (!bo_vc4)
-               return TBM_ERROR_INVALID_PARAMETER;
-
-       bufmgr_vc4 = bo_vc4->bufmgr_vc4;
-       if (!bufmgr_vc4)
-               return TBM_ERROR_INVALID_PARAMETER;
+       tbm_bufmgr_vc4 bufmgr_vc4 = (tbm_bufmgr_vc4)bufmgr_data;
+       tbm_bo_vc4 bo_vc4;
+       int ret;
 
-       if (device != TBM_DEVICE_3D && device != TBM_DEVICE_CPU) {
-               TBM_VC4_DEBUG("Not support device type,\n");
-               return TBM_ERROR_OPERATION_FAILED;
+       if (bufmgr_vc4 == NULL) {
+               TBM_VC4_ERROR("bufmgr_data is null\n");
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return NULL;
        }
 
-       memset(&fence, 0, sizeof(struct dma_buf_fence));
-
-       /* Check if the given type is valid or not. */
-       if (opt & TBM_OPTION_WRITE) {
-               if (device == TBM_DEVICE_3D)
-                       fence.type = DMA_BUF_ACCESS_WRITE | DMA_BUF_ACCESS_DMA;
-       } else if (opt & TBM_OPTION_READ) {
-               if (device == TBM_DEVICE_3D)
-                       fence.type = DMA_BUF_ACCESS_READ | DMA_BUF_ACCESS_DMA;
-       } else {
-               TBM_VC4_ERROR("Invalid argument\n");
-               return TBM_ERROR_INVALID_PARAMETER;
+       ret = drmHashLookup(bufmgr_vc4->hashBos, key, (void **)&bo_vc4);
+       if (ret == 0) {
+               if (error)
+                       *error = TBM_ERROR_NONE;
+               return (tbm_backend_bo_data *)bo_vc4;
        }
 
-       /* Check if the tbm manager supports dma fence or not. */
-       if (!bufmgr_vc4->use_dma_fence) {
-               TBM_VC4_ERROR("Not support DMA FENCE(%s)\n", strerror_r(errno, buf, STRERR_BUFSIZE));
-               return TBM_ERROR_OPERATION_FAILED;
+       struct drm_gem_open arg = {0, };
 
+       arg.name = key;
+       if (drmIoctl(bufmgr_vc4->fd, DRM_IOCTL_GEM_OPEN, &arg)) {
+               TBM_VC4_ERROR("Cannot open gem name=%d\n", key);
+               if (error)
+                       *error = TBM_ERROR_OPERATION_FAILED;
+               return NULL;
        }
 
-       if (device == TBM_DEVICE_3D) {
-               ret = ioctl(bo_vc4->dmabuf, DMABUF_IOCTL_GET_FENCE, &fence);
-               if (ret < 0) {
-                       TBM_VC4_ERROR("Cannot set GET FENCE(%s)\n", strerror_r(errno, buf, STRERR_BUFSIZE));
-                       return TBM_ERROR_OPERATION_FAILED;
-               }
-       } else {
-               if (opt & TBM_OPTION_WRITE)
-                       filelock.l_type = F_WRLCK;
-               else
-                       filelock.l_type = F_RDLCK;
-
-               filelock.l_whence = SEEK_CUR;
-               filelock.l_start = 0;
-               filelock.l_len = 0;
-
-               if (-1 == fcntl(bo_vc4->dmabuf, F_SETLKW, &filelock))
-                       return TBM_ERROR_OPERATION_FAILED;
+       bo_vc4 = calloc(1, sizeof(struct _tbm_bo_vc4));
+       if (!bo_vc4) {
+               TBM_VC4_ERROR("fail to allocate the bo_vc4 private\n");
+               if (error)
+                       *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
        }
 
-       pthread_mutex_lock(&bo_vc4->mutex);
+       bo_vc4->fd = bufmgr_vc4->fd;
+       bo_vc4->gem = arg.handle;
+       bo_vc4->size = arg.size;
+       bo_vc4->name = key;
+       bo_vc4->flags_tbm = 0;
 
-       if (device == TBM_DEVICE_3D) {
-               int i;
+       if (!_bo_init_cache_state(bufmgr_vc4, bo_vc4, 1)) {
+               TBM_VC4_ERROR("fail init cache state(%d)\n", bo_vc4->name);
+               free(bo_vc4);
+               if (error)
+                       *error = TBM_ERROR_OPERATION_FAILED;
+               return NULL;
+       }
 
-               for (i = 0; i < DMA_FENCE_LIST_MAX; i++) {
-                       if (bo_vc4->dma_fence[i].ctx == 0) {
-                               bo_vc4->dma_fence[i].type = fence.type;
-                               bo_vc4->dma_fence[i].ctx = fence.ctx;
-                               break;
-                       }
-               }
+       if (!bo_vc4->dmabuf) {
+               struct drm_prime_handle arg = {0, };
 
-               if (i == DMA_FENCE_LIST_MAX) {
-                       /*TODO: if dma_fence list is full, it needs realloc. I will fix this. by minseok3.kim*/
-                       TBM_VC4_ERROR("fence list is full\n");
+               arg.handle = bo_vc4->gem;
+               if (drmIoctl(bo_vc4->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
+                       TBM_VC4_ERROR("fail to DRM_IOCTL_PRIME_HANDLE_TO_FD gem=%d\n", bo_vc4->gem);
+                       if (error)
+                               *error = TBM_ERROR_OPERATION_FAILED;
+                       free(bo_vc4);
+                       return NULL;
                }
+               bo_vc4->dmabuf = arg.fd;
        }
 
-       pthread_mutex_unlock(&bo_vc4->mutex);
+       /* add bo_vc4 to hash */
+       if (drmHashInsert(bufmgr_vc4->hashBos, bo_vc4->name, (void *)bo_vc4) < 0)
+               TBM_VC4_ERROR("Cannot insert bo_vc4 to Hash(%d)\n", bo_vc4->name);
 
-       TBM_VC4_DEBUG("DMABUF_IOCTL_GET_FENCE! bo_vc4:%p, gem:%d(%d), fd:%ds\n",
+       TBM_VC4_DEBUG("    bo_vc4:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d\n",
            bo_vc4,
            bo_vc4->gem, bo_vc4->name,
-           bo_vc4->dmabuf);
-#endif /* ALWAYS_BACKEND_CTRL */
+           bo_vc4->dmabuf,
+           bo_vc4->flags_tbm,
+           bo_vc4->size);
 
-       return TBM_ERROR_NONE;
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return (tbm_backend_bo_data *)bo_vc4;
 }
 
-static tbm_error_e
-tbm_vc4_bo_unlock(tbm_backend_bo_data *bo_data)
+static void
+tbm_vc4_bo_free(tbm_backend_bo_data *bo_data)
 {
-#ifndef ALWAYS_BACKEND_CTRL
        tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
-       struct dma_buf_fence fence;
-       struct flock filelock;
-       unsigned int dma_type = 0;
-       int ret = 0;
+       tbm_bo_vc4 temp;
+       tbm_bufmgr_vc4 bufmgr_vc4;
        char buf[STRERR_BUFSIZE];
+       int ret;
+
+       if (!bo_data)
+               return;
 
        bufmgr_vc4 = bo_vc4->bufmgr_vc4;
        if (!bufmgr_vc4)
-               return TBM_ERROR_INVALID_PARAMETER;
+               return;
 
-       if (bo_vc4->dma_fence[0].type & DMA_BUF_ACCESS_DMA)
-               dma_type = 1;
+       TBM_VC4_DEBUG("      bo_vc4:%p, gem:%d(%d), fd:%d, size:%d\n",
+           bo_vc4,
+           bo_vc4->gem, bo_vc4->name,
+           bo_vc4->dmabuf,
+           bo_vc4->size);
 
-       if (!bo_vc4->dma_fence[0].ctx && dma_type) {
-               TBM_VC4_DEBUG("FENCE not support or ignored,\n");
-               return TBM_ERROR_OPERATION_FAILED;
+       if (bo_vc4->pBase) {
+               if (munmap(bo_vc4->pBase, bo_vc4->size) == -1) {
+                       TBM_VC4_ERROR("bo_vc4:%p fail to munmap(%s)\n",
+                                      bo_vc4, strerror_r(errno, buf, STRERR_BUFSIZE));
+               }
        }
 
-       if (!bo_vc4->dma_fence[0].ctx && dma_type) {
-               TBM_VC4_DEBUG("device type is not 3D/CPU,\n");
-               return TBM_ERROR_OPERATION_FAILED;
+       /* close dmabuf */
+       if (bo_vc4->dmabuf) {
+               close(bo_vc4->dmabuf);
+               bo_vc4->dmabuf = 0;
        }
 
-       pthread_mutex_lock(&bo_vc4->mutex);
+       /* delete bo_vc4 from hash */
+       ret = drmHashLookup(bufmgr_vc4->hashBos, bo_vc4->name, (void **)&temp);
+       if (ret == 0)
+               drmHashDelete(bufmgr_vc4->hashBos, bo_vc4->name);
+       else
+               TBM_VC4_ERROR("Cannot find bo_vc4 to Hash(%d), ret=%d\n", bo_vc4->name, ret);
 
-       if (dma_type) {
-               fence.type = bo_vc4->dma_fence[0].type;
-               fence.ctx = bo_vc4->dma_fence[0].ctx;
-               int i;
+       if (temp != bo_vc4)
+               TBM_VC4_ERROR("hashBos probably has several BOs with same name!!!\n");
+
+       _bo_destroy_cache_state(bufmgr_vc4, bo_vc4);
+
+       /* Free gem handle */
+       struct drm_gem_close arg = {0, };
 
-               for (i = 1; i < DMA_FENCE_LIST_MAX; i++) {
-                       bo_vc4->dma_fence[i - 1].type = bo_vc4->dma_fence[i].type;
-                       bo_vc4->dma_fence[i - 1].ctx = bo_vc4->dma_fence[i].ctx;
-               }
-               bo_vc4->dma_fence[DMA_FENCE_LIST_MAX - 1].type = 0;
-               bo_vc4->dma_fence[DMA_FENCE_LIST_MAX - 1].ctx = 0;
-       }
+       memset(&arg, 0, sizeof(arg));
+       arg.handle = bo_vc4->gem;
+       if (drmIoctl(bo_vc4->fd, DRM_IOCTL_GEM_CLOSE, &arg))
+               TBM_VC4_ERROR("bo_vc4:%p fail to gem close.(%s)\n",
+                              bo_vc4, strerror_r(errno, buf, STRERR_BUFSIZE));
 
-       pthread_mutex_unlock(&bo_vc4->mutex);
+       free(bo_vc4);
+}
 
-       if (dma_type) {
-               ret = ioctl(bo_vc4->dmabuf, DMABUF_IOCTL_PUT_FENCE, &fence);
-               if (ret < 0) {
-                       TBM_VC4_ERROR("Can not set PUT FENCE(%s)\n", strerror_r(errno, buf, STRERR_BUFSIZE));
-                       return TBM_ERROR_OPERATION_FAILED;
-               }
-       } else {
-               filelock.l_type = F_UNLCK;
-               filelock.l_whence = SEEK_CUR;
-               filelock.l_start = 0;
-               filelock.l_len = 0;
+static int
+tbm_vc4_bo_get_size(tbm_backend_bo_data *bo_data, tbm_error_e *error)
+{
+       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
 
-               if (-1 == fcntl(bo_vc4->dmabuf, F_SETLKW, &filelock))
-                       return TBM_ERROR_OPERATION_FAILED;
+       if (!bo_vc4) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return 0;
        }
 
-       TBM_VC4_DEBUG("DMABUF_IOCTL_PUT_FENCE! bo_vc4:%p, gem:%d(%d), fd:%ds\n",
-           bo_vc4,
-           bo_vc4->gem, bo_vc4->name,
-           bo_vc4->dmabuf);
-#endif /* ALWAYS_BACKEND_CTRL */
+       if (error)
+               *error = TBM_ERROR_NONE;
 
-       return TBM_ERROR_NONE;
+       return bo_vc4->size;
 }
 
-static void
-tbm_vc4_deinit(tbm_backend_bufmgr_data *bufmgr_data)
+static tbm_bo_memory_type
+tbm_vc4_bo_get_memory_type(tbm_backend_bo_data *bo_data, tbm_error_e *error)
 {
-       tbm_bufmgr_vc4 bufmgr_vc4 = (tbm_bufmgr_vc4)bufmgr_data;
-       tbm_bufmgr bufmgr;
-       tbm_error_e error;
-       unsigned long key;
-       void *value;
+       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
 
-       VC4_RETURN_IF_FAIL(bufmgr_vc4 != NULL);
+       if (!bo_vc4) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return TBM_BO_DEFAULT;
+       }
 
-       bufmgr = bufmgr_vc4->bufmgr;
+       if (error)
+               *error = TBM_ERROR_NONE;
 
-       tbm_backend_bufmgr_free_bufmgr_func(bufmgr, bufmgr_vc4->bufmgr_func);
-       tbm_backend_bufmgr_free_bo_func(bufmgr, bufmgr_vc4->bo_func);
+       return bo_vc4->flags_tbm;
+}
 
-       if (bufmgr_vc4->hashBos) {
-               while (drmHashFirst(bufmgr_vc4->hashBos, &key, &value) > 0) {
-                       free(value);
-                       drmHashDelete(bufmgr_vc4->hashBos, key);
-               }
+static tbm_bo_handle
+tbm_vc4_bo_get_handle(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, tbm_error_e *error)
+{
+       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
+       tbm_bo_handle bo_handle;
 
-               drmHashDestroy(bufmgr_vc4->hashBos);
-               bufmgr_vc4->hashBos = NULL;
+       if (!bo_vc4) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
        }
 
-       _bufmgr_deinit_cache_state(bufmgr_vc4);
-
-       if (bufmgr_vc4->bind_display)
-               tbm_drm_helper_wl_auth_server_deinit();
+       if (!bo_vc4->gem) {
+               TBM_VC4_ERROR("Cannot map gem=%d\n", bo_vc4->gem);
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
+       }
 
-       if (bufmgr_vc4->device_name)
-               free(bufmgr_vc4->device_name);
+       TBM_VC4_DEBUG("bo_vc4:%p, gem:%d(%d), fd:%d, flags:%d, size:%d, %s\n",
+           bo_vc4,
+           bo_vc4->gem, bo_vc4->name,
+           bo_vc4->dmabuf,
+           bo_vc4->flags_tbm,
+           bo_vc4->size,
+           STR_DEVICE[device]);
 
-       if (tbm_backend_bufmgr_query_display_server(bufmgr, &error))
-               tbm_drm_helper_unset_tbm_master_fd();
-       else
-               tbm_drm_helper_unset_fd();
+       /*Get mapped bo_handle*/
+       bo_handle = _vc4_bo_handle(bo_vc4, device);
+       if (bo_handle.ptr == NULL) {
+               TBM_VC4_ERROR("Cannot get handle: gem:%d, device:%d\n",
+                       bo_vc4->gem, device);
+               if (error)
+                       *error = TBM_ERROR_OPERATION_FAILED;
+               return (tbm_bo_handle) NULL;
+       }
 
-       close(bufmgr_vc4->fd);
+       if (error)
+               *error = TBM_ERROR_NONE;
 
-       free(bufmgr_vc4);
+       return bo_handle;
 }
 
-static tbm_error_e
-tbm_vc4_bufmgr_get_supported_formats(tbm_backend_bufmgr_data *bufmgr_data,
-                                                       uint32_t **formats, uint32_t *num)
+static tbm_bo_handle
+tbm_vc4_bo_map(tbm_backend_bo_data *bo_data, tbm_bo_device_type device,
+                               tbm_bo_access_option opt, tbm_error_e *error)
 {
-       tbm_bufmgr_vc4 bufmgr_vc4 = (tbm_bufmgr_vc4)bufmgr_data;
-       uint32_t *color_formats;
+       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
+       tbm_bo_handle bo_handle;
+       tbm_bufmgr_vc4 bufmgr_vc4;
 
-       VC4_RETURN_VAL_IF_FAIL(bufmgr_vc4 != NULL, TBM_ERROR_INVALID_PARAMETER);
+       if (!bo_vc4) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
+       }
 
-       color_formats = (uint32_t *)calloc(1, sizeof(uint32_t) * TBM_COLOR_FORMAT_COUNT);
-       if (color_formats == NULL)
-               return TBM_ERROR_OUT_OF_MEMORY;
+       bufmgr_vc4 = bo_vc4->bufmgr_vc4;
+       if (!bufmgr_vc4) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
+       }
 
-       memcpy(color_formats, tbm_vc4_color_format_list, sizeof(uint32_t)*TBM_COLOR_FORMAT_COUNT);
+       if (!bo_vc4->gem) {
+               TBM_VC4_ERROR("Cannot map gem=%d\n", bo_vc4->gem);
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
+       }
 
-       *formats = color_formats;
-       *num = TBM_COLOR_FORMAT_COUNT;
+       TBM_VC4_DEBUG("       bo_vc4:%p, gem:%d(%d), fd:%d, %s, %s\n",
+           bo_vc4,
+           bo_vc4->gem, bo_vc4->name,
+           bo_vc4->dmabuf,
+           STR_DEVICE[device],
+           STR_OPT[opt]);
 
-       TBM_VC4_DEBUG("supported format count = %d\n", *num);
+       /*Get mapped bo_handle*/
+       bo_handle = _vc4_bo_handle(bo_vc4, device);
+       if (bo_handle.ptr == NULL) {
+               TBM_VC4_ERROR("Cannot get handle: gem:%d, device:%d, opt:%d\n",
+                              bo_vc4->gem, device, opt);
+               if (error)
+                       *error = TBM_ERROR_OPERATION_FAILED;
+               return (tbm_bo_handle) NULL;
+       }
 
-       return TBM_ERROR_NONE;
-}
+       if (bo_vc4->map_cnt == 0)
+               _bo_set_cache_state(bufmgr_vc4, bo_vc4, device, opt);
 
-static int
-_new_calc_plane_nv12(int width, int height)
-{
-       int mbX, mbY;
+       bo_vc4->last_map_device = device;
 
-       mbX = DIV_ROUND_UP(width, S5P_FIMV_NUM_PIXELS_IN_MB_ROW);
-       mbY = DIV_ROUND_UP(height, S5P_FIMV_NUM_PIXELS_IN_MB_COL);
+       bo_vc4->map_cnt++;
 
-       if (width * height < S5P_FIMV_MAX_FRAME_SIZE)
-               mbY = (mbY + 1) / 2 * 2;
+       if (error)
+               *error = TBM_ERROR_NONE;
 
-       return ((mbX * S5P_FIMV_NUM_PIXELS_IN_MB_COL) * (mbY *
-                       S5P_FIMV_NUM_PIXELS_IN_MB_ROW));
+       return bo_handle;
 }
 
-static int
-_calc_yplane_nv12(int width, int height)
+static tbm_error_e
+tbm_vc4_bo_unmap(tbm_backend_bo_data *bo_data)
 {
-       int mbX, mbY;
+       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
+       tbm_bufmgr_vc4 bufmgr_vc4;
 
-       mbX = SIZE_ALIGN(width + 24, S5P_FIMV_NV12MT_HALIGN);
-       mbY = SIZE_ALIGN(height + 16, S5P_FIMV_NV12MT_VALIGN);
+       if (!bo_vc4)
+               return TBM_ERROR_INVALID_PARAMETER;
 
-       return SIZE_ALIGN(mbX * mbY, S5P_FIMV_DEC_BUF_ALIGN);
-}
+       bufmgr_vc4 = bo_vc4->bufmgr_vc4;
+       if (!bufmgr_vc4)
+               return TBM_ERROR_INVALID_PARAMETER;
 
-static int
-_calc_uvplane_nv12(int width, int height)
-{
-       int mbX, mbY;
+       if (!bo_vc4->gem)
+               return TBM_ERROR_INVALID_PARAMETER;
 
-       mbX = SIZE_ALIGN(width + 16, S5P_FIMV_NV12MT_HALIGN);
-       mbY = SIZE_ALIGN(height + 4, S5P_FIMV_NV12MT_VALIGN);
+       bo_vc4->map_cnt--;
 
-       return SIZE_ALIGN((mbX * mbY) >> 1, S5P_FIMV_DEC_BUF_ALIGN);
-}
+       if (bo_vc4->map_cnt == 0)
+               _bo_save_cache_state(bufmgr_vc4, bo_vc4);
 
-static int
-_new_calc_yplane_nv12(int width, int height)
-{
-       return SIZE_ALIGN(_new_calc_plane_nv12(width,
-                                               height) + S5P_FIMV_D_ALIGN_PLANE_SIZE,
-                          TBM_SURFACE_ALIGNMENT_PLANE_NV12);
-}
+#ifdef ENABLE_CACHECRTL
+       if (bo_vc4->last_map_device == TBM_DEVICE_CPU)
+               _vc4_cache_flush(bufmgr_vc4, bo_vc4, TBM_VC4_CACHE_FLUSH_ALL);
+#endif
 
-static int
-_new_calc_uvplane_nv12(int width, int height)
-{
-       return SIZE_ALIGN((_new_calc_plane_nv12(width,
-                                               height) >> 1) + S5P_FIMV_D_ALIGN_PLANE_SIZE,
-                         TBM_SURFACE_ALIGNMENT_PLANE_NV12);
-}
+       bo_vc4->last_map_device = -1;
 
-static tbm_error_e
-tbm_vc4_bufmgr_get_plane_data(tbm_backend_bufmgr_data *bufmgr_data,
-                                                       tbm_format format, int plane_idx, int width,
-                                                       int height, uint32_t *size, uint32_t *offset,
-                                                       uint32_t *pitch, int *bo_idx)
-{
-       tbm_bufmgr_vc4 bufmgr_vc4 = (tbm_bufmgr_vc4)bufmgr_data;
-       int bpp;
-       int _offset = 0;
-       int _pitch = 0;
-       int _size = 0;
-       int _bo_idx = 0;
+       TBM_VC4_DEBUG("     bo_vc4:%p, gem:%d(%d), fd:%d\n",
+           bo_vc4,
+           bo_vc4->gem, bo_vc4->name,
+           bo_vc4->dmabuf);
 
-       VC4_RETURN_VAL_IF_FAIL(bufmgr_vc4 != NULL, TBM_ERROR_INVALID_PARAMETER);
+       return TBM_ERROR_NONE;
+}
 
-       switch (format) {
-               /* 16 bpp RGB */
-       case TBM_FORMAT_XRGB4444:
-       case TBM_FORMAT_XBGR4444:
-       case TBM_FORMAT_RGBX4444:
-       case TBM_FORMAT_BGRX4444:
-       case TBM_FORMAT_ARGB4444:
-       case TBM_FORMAT_ABGR4444:
-       case TBM_FORMAT_RGBA4444:
-       case TBM_FORMAT_BGRA4444:
-       case TBM_FORMAT_XRGB1555:
-       case TBM_FORMAT_XBGR1555:
-       case TBM_FORMAT_RGBX5551:
-       case TBM_FORMAT_BGRX5551:
-       case TBM_FORMAT_ARGB1555:
-       case TBM_FORMAT_ABGR1555:
-       case TBM_FORMAT_RGBA5551:
-       case TBM_FORMAT_BGRA5551:
-       case TBM_FORMAT_RGB565:
-               bpp = 16;
-               _offset = 0;
-               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB);
-               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-               _bo_idx = 0;
-               break;
-               /* 24 bpp RGB */
-       case TBM_FORMAT_RGB888:
-       case TBM_FORMAT_BGR888:
-               bpp = 24;
-               _offset = 0;
-               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB);
-               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-               _bo_idx = 0;
-               break;
-               /* 32 bpp RGB */
-       case TBM_FORMAT_XRGB8888:
-       case TBM_FORMAT_XBGR8888:
-       case TBM_FORMAT_RGBX8888:
-       case TBM_FORMAT_BGRX8888:
-       case TBM_FORMAT_ARGB8888:
-       case TBM_FORMAT_ABGR8888:
-       case TBM_FORMAT_RGBA8888:
-       case TBM_FORMAT_BGRA8888:
-               bpp = 32;
-               _offset = 0;
-               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB);
-               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-               _bo_idx = 0;
-               break;
+static tbm_error_e
+tbm_vc4_bo_lock(tbm_backend_bo_data *bo_data, tbm_bo_device_type device,
+                               tbm_bo_access_option opt)
+{
+#ifndef ALWAYS_BACKEND_CTRL
+       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
+       tbm_bufmgr_vc4 bufmgr_vc4;
+       struct dma_buf_fence fence;
+       struct flock filelock;
+       int ret = 0;
+       char buf[STRERR_BUFSIZE];
 
-               /* packed YCbCr */
-       case TBM_FORMAT_YUYV:
-       case TBM_FORMAT_YVYU:
-       case TBM_FORMAT_UYVY:
-       case TBM_FORMAT_VYUY:
-       case TBM_FORMAT_AYUV:
-               bpp = 32;
-               _offset = 0;
-               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-               _bo_idx = 0;
-               break;
+       if (!bo_vc4)
+               return TBM_ERROR_INVALID_PARAMETER;
 
-               /*
-               * 2 plane YCbCr
-               * index 0 = Y plane, [7:0] Y
-               * index 1 = Cr:Cb plane, [15:0] Cr:Cb little endian
-               * or
-               * index 1 = Cb:Cr plane, [15:0] Cb:Cr little endian
-               */
-       case TBM_FORMAT_NV12:
-               bpp = 12;
-               if (plane_idx == 0) {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = MAX(_calc_yplane_nv12(width, height), _new_calc_yplane_nv12(width,
-                                       height));
-                       _bo_idx = 0;
-               } else if (plane_idx == 1) {
-                       _offset = MAX(_calc_yplane_nv12(width, height), _new_calc_yplane_nv12(width,
-                                       height));
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
-                       _size = MAX(_calc_uvplane_nv12(width, height), _new_calc_uvplane_nv12(width,
-                                       height));
-                       _bo_idx = 0;
-               }
-               break;
-       case TBM_FORMAT_NV21:
-               bpp = 12;
-               if (plane_idx == 0) {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-               } else if (plane_idx == 1) {
-                       _offset = width * height;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
-                       _size = SIZE_ALIGN(_pitch * (height / 2), TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-               }
-               break;
+       bufmgr_vc4 = bo_vc4->bufmgr_vc4;
+       if (!bufmgr_vc4)
+               return TBM_ERROR_INVALID_PARAMETER;
 
-       case TBM_FORMAT_NV16:
-       case TBM_FORMAT_NV61:
-               bpp = 16;
-               /*if(plane_idx == 0)*/
-               {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 0)
-                               break;
-               }
-               /*else if( plane_idx ==1 )*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-               }
-               break;
+       if (device != TBM_DEVICE_3D && device != TBM_DEVICE_CPU) {
+               TBM_VC4_DEBUG("Not support device type,\n");
+               return TBM_ERROR_OPERATION_FAILED;
+       }
 
-               /*
-               * 3 plane YCbCr
-               * index 0: Y plane, [7:0] Y
-               * index 1: Cb plane, [7:0] Cb
-               * index 2: Cr plane, [7:0] Cr
-               * or
-               * index 1: Cr plane, [7:0] Cr
-               * index 2: Cb plane, [7:0] Cb
-               */
+       memset(&fence, 0, sizeof(struct dma_buf_fence));
 
-               /*
-               * NATIVE_BUFFER_FORMAT_YV12
-               * NATIVE_BUFFER_FORMAT_I420
-               */
-       case TBM_FORMAT_YUV410:
-       case TBM_FORMAT_YVU410:
-               bpp = 9;
-               /*if(plane_idx == 0)*/
-               {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 0)
-                               break;
-               }
-               /*else if(plane_idx == 1)*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width / 4, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 4);
-                       _size = SIZE_ALIGN(_pitch * (height / 4), TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 1)
-                               break;
-               }
-               /*else if (plane_idx == 2)*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width / 4, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 4);
-                       _size = SIZE_ALIGN(_pitch * (height / 4), TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-               }
-               break;
-       case TBM_FORMAT_YUV411:
-       case TBM_FORMAT_YVU411:
-       case TBM_FORMAT_YUV420:
-       case TBM_FORMAT_YVU420:
-               bpp = 12;
-               /*if(plane_idx == 0)*/
-               {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 0)
-                               break;
-               }
-               /*else if(plane_idx == 1)*/
-               {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
-                       _size = SIZE_ALIGN(_pitch * (height / 2), TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 1;
-                       if (plane_idx == 1)
-                               break;
-               }
-               /*else if (plane_idx == 2)*/
-               {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
-                       _size = SIZE_ALIGN(_pitch * (height / 2), TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 2;
-               }
-               break;
-       case TBM_FORMAT_YUV422:
-       case TBM_FORMAT_YVU422:
-               bpp = 16;
-               /*if(plane_idx == 0)*/
-               {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 0)
-                               break;
-               }
-               /*else if(plane_idx == 1)*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
-                       _size = SIZE_ALIGN(_pitch * (height), TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 1)
-                               break;
-               }
-               /*else if (plane_idx == 2)*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
-                       _size = SIZE_ALIGN(_pitch * (height), TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
+       /* Check if the given type is valid or not. */
+       if (opt & TBM_OPTION_WRITE) {
+               if (device == TBM_DEVICE_3D)
+                       fence.type = DMA_BUF_ACCESS_WRITE | DMA_BUF_ACCESS_DMA;
+       } else if (opt & TBM_OPTION_READ) {
+               if (device == TBM_DEVICE_3D)
+                       fence.type = DMA_BUF_ACCESS_READ | DMA_BUF_ACCESS_DMA;
+       } else {
+               TBM_VC4_ERROR("Invalid argument\n");
+               return TBM_ERROR_INVALID_PARAMETER;
+       }
+
+       /* Check if the tbm manager supports dma fence or not. */
+       if (!bufmgr_vc4->use_dma_fence) {
+               TBM_VC4_ERROR("Not support DMA FENCE(%s)\n", strerror_r(errno, buf, STRERR_BUFSIZE));
+               return TBM_ERROR_OPERATION_FAILED;
+
+       }
+
+       if (device == TBM_DEVICE_3D) {
+               ret = ioctl(bo_vc4->dmabuf, DMABUF_IOCTL_GET_FENCE, &fence);
+               if (ret < 0) {
+                       TBM_VC4_ERROR("Cannot set GET FENCE(%s)\n", strerror_r(errno, buf, STRERR_BUFSIZE));
+                       return TBM_ERROR_OPERATION_FAILED;
                }
-               break;
-       case TBM_FORMAT_YUV444:
-       case TBM_FORMAT_YVU444:
-               bpp = 24;
-               /*if(plane_idx == 0)*/
-               {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 0)
+       } else {
+               if (opt & TBM_OPTION_WRITE)
+                       filelock.l_type = F_WRLCK;
+               else
+                       filelock.l_type = F_RDLCK;
+
+               filelock.l_whence = SEEK_CUR;
+               filelock.l_start = 0;
+               filelock.l_len = 0;
+
+               if (-1 == fcntl(bo_vc4->dmabuf, F_SETLKW, &filelock))
+                       return TBM_ERROR_OPERATION_FAILED;
+       }
+
+       pthread_mutex_lock(&bo_vc4->mutex);
+
+       if (device == TBM_DEVICE_3D) {
+               int i;
+
+               for (i = 0; i < DMA_FENCE_LIST_MAX; i++) {
+                       if (bo_vc4->dma_fence[i].ctx == 0) {
+                               bo_vc4->dma_fence[i].type = fence.type;
+                               bo_vc4->dma_fence[i].ctx = fence.ctx;
                                break;
+                       }
                }
-               /*else if(plane_idx == 1)*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 1)
-                               break;
+
+               if (i == DMA_FENCE_LIST_MAX) {
+                       /*TODO: if dma_fence list is full, it needs realloc. I will fix this. by minseok3.kim*/
+                       TBM_VC4_ERROR("fence list is full\n");
                }
-               /*else if (plane_idx == 2)*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
+       }
+
+       pthread_mutex_unlock(&bo_vc4->mutex);
+
+       TBM_VC4_DEBUG("DMABUF_IOCTL_GET_FENCE! bo_vc4:%p, gem:%d(%d), fd:%ds\n",
+           bo_vc4,
+           bo_vc4->gem, bo_vc4->name,
+           bo_vc4->dmabuf);
+#endif /* ALWAYS_BACKEND_CTRL */
+
+       return TBM_ERROR_NONE;
+}
+
+static tbm_error_e
+tbm_vc4_bo_unlock(tbm_backend_bo_data *bo_data)
+{
+#ifndef ALWAYS_BACKEND_CTRL
+       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
+       struct dma_buf_fence fence;
+       struct flock filelock;
+       unsigned int dma_type = 0;
+       int ret = 0;
+       char buf[STRERR_BUFSIZE];
+
+       bufmgr_vc4 = bo_vc4->bufmgr_vc4;
+       if (!bufmgr_vc4)
+               return TBM_ERROR_INVALID_PARAMETER;
+
+       if (bo_vc4->dma_fence[0].type & DMA_BUF_ACCESS_DMA)
+               dma_type = 1;
+
+       if (!bo_vc4->dma_fence[0].ctx && dma_type) {
+               TBM_VC4_DEBUG("FENCE not support or ignored,\n");
+               return TBM_ERROR_OPERATION_FAILED;
+       }
+
+       if (!bo_vc4->dma_fence[0].ctx && dma_type) {
+               TBM_VC4_DEBUG("device type is not 3D/CPU,\n");
+               return TBM_ERROR_OPERATION_FAILED;
+       }
+
+       pthread_mutex_lock(&bo_vc4->mutex);
+
+       if (dma_type) {
+               fence.type = bo_vc4->dma_fence[0].type;
+               fence.ctx = bo_vc4->dma_fence[0].ctx;
+               int i;
+
+               for (i = 1; i < DMA_FENCE_LIST_MAX; i++) {
+                       bo_vc4->dma_fence[i - 1].type = bo_vc4->dma_fence[i].type;
+                       bo_vc4->dma_fence[i - 1].ctx = bo_vc4->dma_fence[i].ctx;
                }
-               break;
-       default:
-               bpp = 0;
-               break;
+               bo_vc4->dma_fence[DMA_FENCE_LIST_MAX - 1].type = 0;
+               bo_vc4->dma_fence[DMA_FENCE_LIST_MAX - 1].ctx = 0;
        }
 
-       *size = _size;
-       *offset = _offset;
-       *pitch = _pitch;
-       *bo_idx = _bo_idx;
+       pthread_mutex_unlock(&bo_vc4->mutex);
+
+       if (dma_type) {
+               ret = ioctl(bo_vc4->dmabuf, DMABUF_IOCTL_PUT_FENCE, &fence);
+               if (ret < 0) {
+                       TBM_VC4_ERROR("Can not set PUT FENCE(%s)\n", strerror_r(errno, buf, STRERR_BUFSIZE));
+                       return TBM_ERROR_OPERATION_FAILED;
+               }
+       } else {
+               filelock.l_type = F_UNLCK;
+               filelock.l_whence = SEEK_CUR;
+               filelock.l_start = 0;
+               filelock.l_len = 0;
+
+               if (-1 == fcntl(bo_vc4->dmabuf, F_SETLKW, &filelock))
+                       return TBM_ERROR_OPERATION_FAILED;
+       }
+
+       TBM_VC4_DEBUG("DMABUF_IOCTL_PUT_FENCE! bo_vc4:%p, gem:%d(%d), fd:%ds\n",
+           bo_vc4,
+           bo_vc4->gem, bo_vc4->name,
+           bo_vc4->dmabuf);
+#endif /* ALWAYS_BACKEND_CTRL */
 
        return TBM_ERROR_NONE;
 }
 
-static tbm_bo_memory_type
-tbm_vc4_bo_get_memory_type(tbm_backend_bo_data *bo_data, tbm_error_e *error)
+static tbm_fd
+tbm_vc4_bo_export_fd(tbm_backend_bo_data *bo_data, tbm_error_e *error)
 {
        tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
+       int ret;
+       char buf[STRERR_BUFSIZE];
 
        if (!bo_vc4) {
                if (error)
                        *error = TBM_ERROR_INVALID_PARAMETER;
-               return TBM_BO_DEFAULT;
+               return -1;
        }
 
-       if (error)
-               *error = TBM_ERROR_NONE;
+       struct drm_prime_handle arg = {0, };
 
-       return bo_vc4->flags_tbm;
+       arg.handle = bo_vc4->gem;
+       ret = drmIoctl(bo_vc4->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg);
+       if (ret) {
+               TBM_VC4_ERROR("bo_vc4:%p Cannot dmabuf=%d (%s)\n",
+                              bo_vc4, bo_vc4->gem, strerror_r(errno, buf, STRERR_BUFSIZE));
+               if (error)
+                       *error = TBM_ERROR_OPERATION_FAILED;
+               return (tbm_fd) ret;
+       }
+
+       TBM_VC4_DEBUG(" bo_vc4:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d\n",
+           bo_vc4,
+           bo_vc4->gem, bo_vc4->name,
+           bo_vc4->dmabuf,
+           arg.fd,
+           bo_vc4->flags_tbm,
+           bo_vc4->size);
+
+       return (tbm_fd)arg.fd;
 }
 
-static tbm_bufmgr_capability
-tbm_vc4_bufmgr_get_capabilities(tbm_backend_bufmgr_data *bufmgr_data, tbm_error_e *error)
+static tbm_key
+tbm_vc4_bo_export_key(tbm_backend_bo_data *bo_data, tbm_error_e *error)
 {
-       tbm_bufmgr_capability capabilities = TBM_BUFMGR_CAPABILITY_NONE;
+       tbm_bo_vc4 bo_vc4 = (tbm_bo_vc4)bo_data;
 
-       capabilities = TBM_BUFMGR_CAPABILITY_SHARE_KEY|TBM_BUFMGR_CAPABILITY_SHARE_FD;
+       if (!bo_vc4) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return 0;
+       }
+
+       if (!bo_vc4->name) {
+               bo_vc4->name = _get_name(bo_vc4->fd, bo_vc4->gem);
+               if (!bo_vc4->name) {
+                       TBM_VC4_ERROR("error Cannot get name\n");
+                       if (error)
+                               *error = TBM_ERROR_INVALID_PARAMETER;
+                       return 0;
+               }
+       }
+
+       TBM_VC4_DEBUG("    bo_vc4:%p, gem:%d(%d), fd:%d, flags:%d, size:%d\n",
+           bo_vc4,
+           bo_vc4->gem, bo_vc4->name,
+           bo_vc4->dmabuf,
+           bo_vc4->flags_tbm,
+           bo_vc4->size);
 
        if (error)
                *error = TBM_ERROR_NONE;
 
-       return capabilities;
+       return (tbm_key)bo_vc4->name;
 }
 
-static tbm_error_e
-tbm_vc4_bufmgr_bind_native_display(tbm_backend_bufmgr_data *bufmgr_data, tbm_native_display *native_display)
+static void
+tbm_vc4_deinit(tbm_backend_bufmgr_data *bufmgr_data)
 {
        tbm_bufmgr_vc4 bufmgr_vc4 = (tbm_bufmgr_vc4)bufmgr_data;
-       VC4_RETURN_VAL_IF_FAIL(bufmgr_vc4 != NULL, TBM_ERROR_INVALID_PARAMETER);
+       tbm_bufmgr bufmgr;
+       tbm_error_e error;
+       unsigned long key;
+       void *value;
 
-       if (!tbm_drm_helper_wl_auth_server_init(native_display, bufmgr_vc4->fd,
-                                          bufmgr_vc4->device_name, 0)) {
-               TBM_VC4_ERROR("fail to tbm_drm_helper_wl_server_init\n");
-               return TBM_ERROR_OPERATION_FAILED;
+       VC4_RETURN_IF_FAIL(bufmgr_vc4 != NULL);
+
+       bufmgr = bufmgr_vc4->bufmgr;
+
+       tbm_backend_bufmgr_free_bufmgr_func(bufmgr, bufmgr_vc4->bufmgr_func);
+       tbm_backend_bufmgr_free_bo_func(bufmgr, bufmgr_vc4->bo_func);
+
+       if (bufmgr_vc4->hashBos) {
+               while (drmHashFirst(bufmgr_vc4->hashBos, &key, &value) > 0) {
+                       free(value);
+                       drmHashDelete(bufmgr_vc4->hashBos, key);
+               }
+
+               drmHashDestroy(bufmgr_vc4->hashBos);
+               bufmgr_vc4->hashBos = NULL;
        }
 
-       bufmgr_vc4->bind_display = native_display;
+       _bufmgr_deinit_cache_state(bufmgr_vc4);
 
-       return TBM_ERROR_NONE;
+       if (bufmgr_vc4->bind_display)
+               tbm_drm_helper_wl_auth_server_deinit();
+
+       if (bufmgr_vc4->device_name)
+               free(bufmgr_vc4->device_name);
+
+       if (tbm_backend_bufmgr_query_display_server(bufmgr, &error))
+               tbm_drm_helper_unset_tbm_master_fd();
+       else
+               tbm_drm_helper_unset_fd();
+
+       close(bufmgr_vc4->fd);
+
+       free(bufmgr_vc4);
 }
 
 static tbm_backend_bufmgr_data *