tbm_bufmgr: add tbm_bufmgr_internal_import_bo_with_fd and use it
[platform/core/uifw/libtbm.git] / src / tbm_bufmgr.c
index 65c3ba6..a056c03 100644 (file)
@@ -33,107 +33,109 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 #include "tbm_bufmgr.h"
 #include "tbm_bufmgr_int.h"
-#include "tbm_bufmgr_backend.h"
+#include "tbm_drm_helper.h"
 #include "list.h"
 
-#ifdef DEBUG
-int bDebug;
-#endif
+#include <sys/resource.h>
 
-#ifdef TRACE
-int bTrace;
-#endif
+int trace_mask = 0;
 
 #ifdef HAVE_DLOG
 int bDlog;
 #endif
 
+tbm_bufmgr gBufMgr;
 int b_dump_queue;
 
-#define PREFIX_LIB    "libtbm_"
-#define SUFFIX_LIB    ".so"
-#define DEFAULT_LIB   PREFIX_LIB"default"SUFFIX_LIB
-
-/* values to indicate unspecified fields in XF86ModReqInfo. */
-#define MAJOR_UNSPEC        0xFF
-#define MINOR_UNSPEC        0xFF
-#define PATCH_UNSPEC        0xFFFF
-#define ABI_VERS_UNSPEC   0xFFFFFFFF
-
-#define MODULE_VERSION_NUMERIC(maj, min, patch) \
-                       ((((maj) & 0xFF) << 24) | (((min) & 0xFF) << 16) | (patch & 0xFFFF))
-#define GET_MODULE_MAJOR_VERSION(vers)    (((vers) >> 24) & 0xFF)
-#define GET_MODULE_MINOR_VERSION(vers)    (((vers) >> 16) & 0xFF)
-#define GET_MODULE_PATCHLEVEL(vers)    ((vers) & 0xFFFF)
-
-enum {
-       LOCK_TRY_ONCE,
-       LOCK_TRY_ALWAYS,
-       LOCK_TRY_NEVER
-};
-
-pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
-tbm_bufmgr gBufMgr;
+static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t tbm_bufmgr_lock = PTHREAD_MUTEX_INITIALIZER;
+static double scale_factor = 0;
+void _tbm_bufmgr_mutex_unlock(void);
 
-static __thread tbm_error_e tbm_last_error = TBM_ERROR_NONE;
+//#define TBM_BUFMGR_INIT_TIME
 
-static void
-_tbm_set_last_result(tbm_error_e err)
-{
-       tbm_last_error = err;
+#define MAX_SIZE_N(dest)       (sizeof(dest) - strlen(dest) - 1)
+
+/* check condition */
+#define TBM_BUFMGR_RETURN_IF_FAIL(cond) {\
+       if (!(cond)) {\
+               TBM_ERR("'%s' failed.\n", #cond);\
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);\
+               _tbm_bufmgr_mutex_unlock();\
+               return;\
+       } \
 }
 
-char * tbm_flag_to_str(int f)
-{
-       static char str[255];
-       int c = 0;
-       if (f == TBM_BO_DEFAULT)
-                snprintf(str, 255, "DEFAULT\n");
-       else {
-               if (f & TBM_BO_SCANOUT)
-                       c = snprintf(&str[c], 255, "SCANOUT,");
-               if (f & TBM_BO_NONCACHABLE)
-                       c = snprintf(&str[c], 255, "NONCACHABLE,");
-               if (f & TBM_BO_WC)
-                       c = snprintf(&str[c], 255, "WC");
-       }
-       return str;
+#define TBM_BUFMGR_RETURN_VAL_IF_FAIL(cond, val) {\
+       if (!(cond)) {\
+               TBM_ERR("'%s' failed.\n", #cond);\
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);\
+               _tbm_bufmgr_mutex_unlock();\
+               return val;\
+       } \
 }
 
 /* LCOV_EXCL_START */
-static int last_chk_bo_cnt = 0;
+
 static void
-_tbm_util_check_bo_cnt(tbm_bufmgr bufmgr)
+_tbm_bufmgr_check_bo_cnt(tbm_bufmgr bufmgr)
 {
-       if (bufmgr->bo_cnt >= 500 && ((bufmgr->bo_cnt % 20) == 0)) {
-               if (bufmgr->bo_cnt > last_chk_bo_cnt) {
-                       TBM_DEBUG("============TBM BO CNT DEBUG: bo_cnt=%d\n", bufmgr->bo_cnt);
-                       tbm_bufmgr_debug_show(bufmgr);
-                       last_chk_bo_cnt = bufmgr->bo_cnt;
-               }
+       static int last_chk_bo_cnt = 0;
+
+       if ((bufmgr->bo_cnt >= 500) && ((bufmgr->bo_cnt % 20) == 0) &&
+               (bufmgr->bo_cnt > last_chk_bo_cnt)) {
+               TBM_DBG("============TBM BO CNT DEBUG: bo_cnt=%d\n", bufmgr->bo_cnt);
+               tbm_bufmgr_debug_show(bufmgr);
+               last_chk_bo_cnt = bufmgr->bo_cnt;
        }
 }
 
+static void
+_tbm_bufmgr_initialize_bo(tbm_bufmgr bufmgr, tbm_bo bo, int flags)
+{
+       bo->bufmgr = bufmgr;
+       bo->flags = flags;
+       bo->magic = TBM_BO_MAGIC;
+       bo->ref_cnt = 1;
+
+       LIST_INITHEAD(&bo->user_data_list);
+
+       bufmgr->bo_cnt++;
+       LIST_ADD(&bo->item_link, &bufmgr->bo_list);
+}
+
+void
+_tbm_bufmgr_mutex_lock(void)
+{
+       pthread_mutex_lock(&tbm_bufmgr_lock);
+}
+
+void
+_tbm_bufmgr_mutex_unlock(void)
+{
+       pthread_mutex_unlock(&tbm_bufmgr_lock);
+}
+
 static int
-_tbm_util_get_max_surface_size(int * w, int * h)
+_tbm_util_get_max_surface_size(int *w, int *h)
 {
-       int count = 0;
-       tbm_surface_h surface = NULL, tmp = NULL;
        tbm_surface_info_s info;
+       tbm_surface_h surface = NULL;
+       int count = 0;
 
        *w = 0;
        *h = 0;
 
-       if (gBufMgr == NULL)
+       if (gBufMgr == NULL || LIST_IS_EMPTY(&gBufMgr->surf_list))
                return count;
 
-       if (!LIST_IS_EMPTY(&gBufMgr->surf_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(surface, tmp, &gBufMgr->surf_list, item_link) {
-                       if (tbm_surface_get_info(surface, &info) == TBM_SURFACE_ERROR_NONE) {
-                               count++;
-                               if (*w < info.width) *w = info.width;
-                               if (*h < info.height) *h = info.height;
-                       }
+       LIST_FOR_EACH_ENTRY(surface, &gBufMgr->surf_list, item_link) {
+               if (tbm_surface_get_info(surface, &info) == TBM_SURFACE_ERROR_NONE) {
+                       count++;
+                       if (*w < info.width)
+                               *w = info.width;
+                       if (*h < info.height)
+                               *h = info.height;
                }
        }
 
@@ -162,25 +164,26 @@ _tbm_util_get_appname_brief(char *brief)
 static void
 _tbm_util_get_appname_from_pid(long pid, char *str)
 {
+       char fn_cmdline[255] = {0, }, cmdline[255];
        FILE *fp;
        int len;
-       long app_pid = pid;
-       char fn_cmdline[255] = {0,};
-       char cmdline[255] = {0,};
 
-       snprintf(fn_cmdline, sizeof(fn_cmdline), "/proc/%ld/cmdline", app_pid);
+       if (pid <= 0) return;
+
+       snprintf(fn_cmdline, sizeof(fn_cmdline), "/proc/%ld/cmdline", pid);
 
        fp = fopen(fn_cmdline, "r");
        if (fp == 0) {
-               fprintf(stderr, "cannot file open /proc/%ld/cmdline", app_pid);
+               TBM_ERR("cannot file open %s\n", fn_cmdline);
                return;
        }
 
        if (!fgets(cmdline, 255, fp)) {
-               fprintf(stderr, "fail to get appname for pid(%ld)\n", app_pid);
+               TBM_ERR("fail to get appname for pid(%ld)\n", pid);
                fclose(fp);
                return;
        }
+
        fclose(fp);
 
        len = strlen(cmdline);
@@ -191,470 +194,166 @@ _tbm_util_get_appname_from_pid(long pid, char *str)
 
        snprintf(str, sizeof(cmdline), "%s", cmdline);
 }
-/* LCOV_EXCL_STOP */
-
-tbm_user_data
-*user_data_lookup(struct list_head *user_data_list, unsigned long key)
-{
-       tbm_user_data *user_data = NULL;
-       tbm_user_data *old_data = NULL, *tmp = NULL;
-
-       if (!LIST_IS_EMPTY(user_data_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(old_data, tmp, user_data_list, item_link) {
-                       if (old_data->key == key) {
-                               user_data = old_data;
-                               return user_data;
-                       }
-               }
-       }
-
-       return user_data;
-}
-
-tbm_user_data
-*user_data_create(unsigned long key, tbm_data_free data_free_func)
-{
-       tbm_user_data *user_data = NULL;
-
-       user_data = calloc(1, sizeof(tbm_user_data));
-       if (!user_data)
-               return NULL;
-
-       user_data->key = key;
-       user_data->free_func = data_free_func;
-       user_data->data = (void *)0;
-
-       return user_data;
-}
-
-void
-user_data_delete(tbm_user_data *user_data)
-{
-       if (user_data->data && user_data->free_func)
-               user_data->free_func(user_data->data);
-
-       LIST_DEL(&user_data->item_link);
-
-       free(user_data);
-}
-
-static int
-_bo_lock(tbm_bo bo, int device, int opt)
-{
-       tbm_bufmgr bufmgr = bo->bufmgr;
-       int ret = 0;
-
-       if (bufmgr->backend->bo_lock)
-               ret = bufmgr->backend->bo_lock(bo, device, opt);
-       else
-               ret = 1;
-
-       return ret;
-}
-
-static void
-_bo_unlock(tbm_bo bo)
-{
-       tbm_bufmgr bufmgr = bo->bufmgr;
-
-       if (bufmgr->backend->bo_unlock)
-               bufmgr->backend->bo_unlock(bo);
-}
-
-static int
-_tbm_bo_lock(tbm_bo bo, int device, int opt)
-{
-       tbm_bufmgr bufmgr = NULL;
-       int old;
-       int ret = 0;
-
-       if (!bo)
-               return 0;
-
-       bufmgr = bo->bufmgr;
-
-       /* do not try to lock the bo */
-       if (bufmgr->lock_type == LOCK_TRY_NEVER)
-               return 1;
-
-       if (bo->lock_cnt < 0) {
-               TBM_LOG_E("error bo:%p LOCK_CNT=%d\n",
-                       bo, bo->lock_cnt);
-       }
-
-       old = bo->lock_cnt;
-       if (bufmgr->lock_type == LOCK_TRY_ONCE) {
-               if (bo->lock_cnt == 0) {
-                       pthread_mutex_unlock(&bufmgr->lock);
-                       ret = _bo_lock(bo, device, opt);
-                       pthread_mutex_lock(&bufmgr->lock);
-                       if (ret)
-                               bo->lock_cnt++;
-               } else
-                       ret = 1;
-       } else if (bufmgr->lock_type == LOCK_TRY_ALWAYS) {
-               pthread_mutex_unlock(&bufmgr->lock);
-               ret = _bo_lock(bo, device, opt);
-               pthread_mutex_lock(&bufmgr->lock);
-               if (ret)
-                       bo->lock_cnt++;
-       } else {
-               TBM_LOG_E("error bo:%p lock_type is wrong.\n",
-                       bo);
-       }
-
-       DBG_LOCK(">> LOCK bo:%p(%d->%d)\n",
-                bo, old, bo->lock_cnt);
-
-       return ret;
-}
-
-static void
-_tbm_bo_unlock(tbm_bo bo)
-{
-       tbm_bufmgr bufmgr = NULL;
-
-       int old;
-
-       if (!bo)
-               return;
-
-       bufmgr = bo->bufmgr;
-
-       /* do not try to unlock the bo */
-       if (bufmgr->lock_type == LOCK_TRY_NEVER)
-               return;
-
-       old = bo->lock_cnt;
-       if (bufmgr->lock_type == LOCK_TRY_ONCE) {
-               if (bo->lock_cnt > 0) {
-                       bo->lock_cnt--;
-                       if (bo->lock_cnt == 0)
-                               _bo_unlock(bo);
-               }
-       } else if (bufmgr->lock_type == LOCK_TRY_ALWAYS) {
-               if (bo->lock_cnt > 0) {
-                       bo->lock_cnt--;
-                       _bo_unlock(bo);
-               }
-       } else {
-               TBM_LOG_E("error bo:%p lock_type is wrong.\n",
-                       bo);
-       }
-
-       if (bo->lock_cnt < 0)
-               bo->lock_cnt = 0;
-
-       DBG_LOCK(">> UNLOCK bo:%p(%d->%d)\n",
-                bo, old, bo->lock_cnt);
-}
-
-static int
-_tbm_bo_is_valid(tbm_bo bo)
-{
-       tbm_bo old_data = NULL, tmp = NULL;
-
-       if (bo == NULL)
-               return 0;
-
-       if (!LIST_IS_EMPTY(&gBufMgr->bo_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(old_data, tmp, &gBufMgr->bo_list, item_link) {
-                       if (old_data == bo)
-                               return 1;
-               }
-
-       }
-       return 0;
-}
-
-static void
-_tbm_bo_ref(tbm_bo bo)
-{
-       bo->ref_cnt++;
-}
 
 static void
-_tbm_bo_unref(tbm_bo bo)
-{
-       tbm_bufmgr bufmgr = bo->bufmgr;
-       tbm_user_data *old_data = NULL, *tmp = NULL;
-
-       if (bo->ref_cnt <= 0)
-               return;
-
-       bo->ref_cnt--;
-       if (bo->ref_cnt == 0) {
-               /* destory the user_data_list */
-               if (!LIST_IS_EMPTY(&bo->user_data_list)) {
-                       LIST_FOR_EACH_ENTRY_SAFE(old_data, tmp, &bo->user_data_list, item_link) {
-                               DBG("free user_data\n");
-                               user_data_delete(old_data);
-                       }
-               }
-
-               if (bo->lock_cnt > 0) {
-                       TBM_LOG_E("error lock_cnt:%d\n",
-                               bo->lock_cnt);
-                       _bo_unlock(bo);
-               }
-
-               /* call the bo_free */
-               bufmgr->backend->bo_free(bo);
-               bo->priv = NULL;
-
-               LIST_DEL(&bo->item_link);
-               free(bo);
-               bo = NULL;
-
-               bufmgr->bo_cnt--;
-       }
-
-}
-
-/* LCOV_EXCL_START */
-static int
-_check_version(TBMModuleVersionInfo *data)
-{
-       int abimaj, abimin;
-       int vermaj, vermin;
-
-       abimaj = GET_ABI_MAJOR(data->abiversion);
-       abimin = GET_ABI_MINOR(data->abiversion);
-
-       DBG("TBM module %s: vendor=\"%s\" ABI=%d,%d\n",
-           data->modname ? data->modname : "UNKNOWN!",
-           data->vendor ? data->vendor : "UNKNOWN!", abimaj, abimin);
-
-       vermaj = GET_ABI_MAJOR(TBM_ABI_VERSION);
-       vermin = GET_ABI_MINOR(TBM_ABI_VERSION);
-
-       DBG("TBM ABI version %d.%d\n",
-           vermaj, vermin);
-
-       if (abimaj != vermaj) {
-               TBM_LOG_E("TBM module ABI major ver(%d) doesn't match the TBM's ver(%d)\n",
-                       abimaj, vermaj);
-               return 0;
-       } else if (abimin > vermin) {
-               TBM_LOG_E("TBM module ABI minor ver(%d) is newer than the TBM's ver(%d)\n",
-                       abimin, vermin);
-               return 0;
-       }
-       return 1;
-}
-
-static int
-_tbm_bufmgr_load_module(tbm_bufmgr bufmgr, int fd, const char *file)
+_tbm_bufmgr_copy_module_data(tbm_bufmgr bufmgr, tbm_module *module, int reset)
 {
-       char path[PATH_MAX] = { 0, };
-       TBMModuleData *initdata = NULL;
-       void *module_data;
-
-       snprintf(path, sizeof(path), BUFMGR_MODULE_DIR "/%s", file);
-
-       module_data = dlopen(path, RTLD_LAZY);
-       if (!module_data) {
-               TBM_LOG_E("failed to load module: %s(%s)\n",
-                       dlerror(), file);
-               return 0;
-       }
-
-       initdata = dlsym(module_data, "tbmModuleData");
-       if (initdata) {
-               ModuleInitProc init;
-               TBMModuleVersionInfo *vers;
-
-               vers = initdata->vers;
-               init = initdata->init;
-
-               if (vers) {
-                       if (!_check_version(vers)) {
-                               dlclose(module_data);
-                               return 0;
-                       }
-               } else {
-                       TBM_LOG_E("Error: module does not supply version information.\n");
-
-                       dlclose(module_data);
-                       return 0;
-               }
-
-               if (init) {
-                       if (!init(bufmgr, fd)) {
-                               TBM_LOG_E("Fail to init module(%s)\n",
-                                       file);
-                               dlclose(module_data);
-                               return 0;
-                       }
-
-                       if (!bufmgr->backend || !bufmgr->backend->priv) {
-                               TBM_LOG_E("Error: module(%s) wrong operation. Check backend or backend's priv.\n",
-                                       file);
-                               dlclose(module_data);
-                               return 0;
-                       }
-               } else {
-                       TBM_LOG_E("Error: module does not supply init symbol.\n");
-                       dlclose(module_data);
-                       return 0;
-               }
+       if (!reset) {
+               bufmgr->module_data = module->module_data;
+               bufmgr->backend = module->backend;
+
+               bufmgr->backend_module_data = module->backend_module_data;
+               bufmgr->bufmgr_data = module->bufmgr_data;
+               bufmgr->bufmgr_func = module->bufmgr_func;
+               bufmgr->bo_func = module->bo_func;
+
+               bufmgr->use_hal_tbm = module->use_hal_tbm;
+               bufmgr->auth_wl_socket_created = module->auth_wl_socket_created;
+               bufmgr->auth_fd = module->auth_fd;
+               bufmgr->hal_backend = module->hal_backend;
+               bufmgr->hal_bufmgr = module->hal_bufmgr;
        } else {
-               TBM_LOG_E("Error: module does not have data object.\n");
-               dlclose(module_data);
-               return 0;
+               bufmgr->module_data = NULL;
+               bufmgr->backend = NULL;
+
+               bufmgr->backend_module_data = NULL;
+               bufmgr->bufmgr_data = NULL;
+               bufmgr->bufmgr_func = NULL;
+               bufmgr->bo_func = NULL;
+
+               bufmgr->use_hal_tbm = 0;
+               bufmgr->auth_wl_socket_created = 0;
+               bufmgr->auth_fd = -1;
+               bufmgr->hal_backend = NULL;
+               bufmgr->hal_bufmgr = NULL;
        }
-
-       bufmgr->module_data = module_data;
-
-       DBG("Success to load module(%s)\n",
-           file);
-
-       return 1;
 }
 
-static int
-_tbm_load_module(tbm_bufmgr bufmgr, int fd)
-{
-       struct dirent **namelist;
-       const char *p = NULL;
-       int n;
-       int ret = 0;
-
-       /* load bufmgr priv from default lib */
-       ret = _tbm_bufmgr_load_module(bufmgr, fd, DEFAULT_LIB);
-
-       /* load bufmgr priv from configured path */
-       if (!ret) {
-               n = scandir(BUFMGR_MODULE_DIR, &namelist, 0, alphasort);
-               if (n < 0) {
-                       TBM_LOG_E("no files : %s\n",
-                               BUFMGR_MODULE_DIR);
-               } else {
-                       while (n--) {
-                               if (!ret && strstr(namelist[n]->d_name, PREFIX_LIB)) {
-                                       p = strstr(namelist[n]->d_name, SUFFIX_LIB);
-                                       if (p && !strcmp(p, SUFFIX_LIB))
-                                               ret = _tbm_bufmgr_load_module(bufmgr, fd, namelist[n]->d_name);
-                               }
-                               free(namelist[n]);
-                       }
-                       free(namelist);
-               }
-       }
 
-       return ret;
-}
 /* LCOV_EXCL_STOP */
 
-tbm_bufmgr
-tbm_bufmgr_init(int fd)
+static tbm_bufmgr
+_tbm_bufmgr_init(int fd, int server)
 {
-       char *env;
-
-       pthread_mutex_lock(&gLock);
+       tbm_error_e error = TBM_ERROR_NONE;
 
-       /* LCOV_EXCL_START */
-#ifdef HAVE_DLOG
-       env = getenv("TBM_DLOG");
-       if (env) {
-               bDlog = atoi(env);
-               TBM_LOG_D("TBM_DLOG=%s\n", env);
-       } else {
-               bDlog = 1;
-       }
+#ifdef TBM_BUFMGR_INIT_TIME
+       struct timeval start_tv, end_tv;
 #endif
+       char *env;
 
-#ifdef DEBUG
-       env = getenv("TBM_DEBUG");
-       if (env) {
-               bDebug = atoi(env);
-               TBM_LOG_D("TBM_DEBUG=%s\n", env);
-       } else {
-               bDebug = 0;
-       }
+#ifdef TBM_BUFMGR_INIT_TIME
+       /* get the start tv */
+       gettimeofday(&start_tv, NULL);
 #endif
 
+       bDlog = 1;
+
+       /* LCOV_EXCL_START */
 #ifdef TRACE
        env = getenv("TBM_TRACE");
        if (env) {
-               bTrace = atoi(env);
-               TBM_LOG_D("TBM_TRACE=%s\n", env);
-       } else {
-               bTrace = 0;
-       }
+               trace_mask = atoi(env);
+               TBM_DBG("TBM_TRACE=%s\n", env);
+       } else
+               trace_mask = 0;
 #endif
-       /* LCOV_EXCL_STOP */
+
+       pthread_mutex_lock(&gLock);
+
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       if (fd >= 0) {
+               TBM_WRN("!!!!!WARNING:: The tbm_bufmgr_init DOSE NOT use argument fd ANYMORE.\n");
+               TBM_WRN("!!!!!WARNING:: IT WILL BE CHANGED like tbm_bufmgr_init(int fd) --> tbm_bufmgr_init(void).\n");
+       }
+
 
        /* initialize buffer manager */
        if (gBufMgr) {
                gBufMgr->ref_count++;
-               TBM_TRACE("reuse  tbm_bufmgr(%p) ref_count(%d) fd(%d)\n", gBufMgr, gBufMgr->ref_count, gBufMgr->fd);
+               TBM_DBG("reuse  tbm_bufmgr(%p) ref_count(%d) fd(%d)\n", gBufMgr, gBufMgr->ref_count, gBufMgr->fd);
                pthread_mutex_unlock(&gLock);
                return gBufMgr;
        }
 
-       DBG("bufmgr init\n");
+       TBM_DBG("bufmgr init\n");
 
        /* allocate bufmgr */
        gBufMgr = calloc(1, sizeof(struct _tbm_bufmgr));
        if (!gBufMgr) {
-               _tbm_set_last_result(TBM_BO_ERROR_HEAP_ALLOC_FAILED);
-               TBM_TRACE("error: fail to alloc bufmgr fd(%d)\n", fd);
+               TBM_ERR("error: fail to alloc bufmgr fd(%d)\n", fd);
+               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
                pthread_mutex_unlock(&gLock);
                return NULL;
        }
 
        gBufMgr->fd = fd;
 
+       /* set the display_server flag before loading the backend module */
+       if (server) {
+               TBM_INFO("The tbm_bufmgr(%p) is used by display server. Need to bind the native_display.\n", gBufMgr);
+               gBufMgr->display_server = 1;
+       }
+
        /* load bufmgr priv from env */
-       if (!_tbm_load_module(gBufMgr, gBufMgr->fd)) {
-               /* LCOV_EXCL_START */
-               _tbm_set_last_result(TBM_BO_ERROR_LOAD_MODULE_FAILED);
-               TBM_LOG_E("error : Fail to load bufmgr backend\n");
+       gBufMgr->module = tbm_module_load(gBufMgr->fd);
+       if (!gBufMgr->module) {
+               TBM_ERR("error : Fail to load bufmgr backend\n");
+               _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
                free(gBufMgr);
                gBufMgr = NULL;
                pthread_mutex_unlock(&gLock);
                return NULL;
-               /* LCOV_EXCL_STOP */
-       }
 
-       /* log for tbm backend_flag */
-       DBG("backend flag:%x:", gBufMgr->backend->flags);
-       DBG("\n");
+       }
 
-       gBufMgr->ref_count = 1;
+       // TODO: this is temporary. it will be removed after finishing refactoring the tbm_module.
+       _tbm_bufmgr_copy_module_data(gBufMgr, gBufMgr->module, 0);
 
-       DBG("create tizen bufmgr:%p ref_count:%d\n",
-           gBufMgr, gBufMgr->ref_count);
+       /* check the essential capabilities of tbm_module */
+       gBufMgr->capabilities = tbm_module_bufmgr_get_capabilities(gBufMgr->module, &error);
+       if (gBufMgr->capabilities == TBM_BUFMGR_CAPABILITY_NONE) {
+               TBM_ERR("The capabilities of the backend module is TBM_BUFMGR_CAPABILITY_NONE.");
+               TBM_ERR("TBM_BUFMGR_CAPABILITY_SHARE_FD is the essential capability.");
+               tbm_module_unload(gBufMgr->module);
+               _tbm_set_last_result(error);
+               free(gBufMgr);
+               gBufMgr = NULL;
+               pthread_mutex_unlock(&gLock);
+               return NULL;
+       }
 
-       if (pthread_mutex_init(&gBufMgr->lock, NULL) != 0) {
-               /* LCOV_EXCL_START */
-               _tbm_set_last_result(TBM_BO_ERROR_THREAD_INIT_FAILED);
-               gBufMgr->backend->bufmgr_deinit(gBufMgr->backend->priv);
-               tbm_backend_free(gBufMgr->backend);
-               dlclose(gBufMgr->module_data);
+       if (!(gBufMgr->capabilities & TBM_BUFMGR_CAPABILITY_SHARE_FD)) {
+               TBM_ERR("The capabilities of the backend module had no TBM_BUFMGR_CAPABILITY_SHARE_FD.");
+               TBM_ERR("The tbm backend has to get TBM_BUFMGR_CAPABILITY_SHARE_FD. ");
+               tbm_module_unload(gBufMgr->module);
+               _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
                free(gBufMgr);
                gBufMgr = NULL;
                pthread_mutex_unlock(&gLock);
                return NULL;
-               /* LCOV_EXCL_STOP */
        }
 
-       /* setup the lock_type */
+       /* LCOV_EXCL_STOP */
+
+       gBufMgr->ref_count = 1;
+
+       TBM_INFO("create tizen bufmgr:%p ref_count:%d\n",
+           gBufMgr, gBufMgr->ref_count);
+
+       /* setup the bo_lock_type */
        env = getenv("BUFMGR_LOCK_TYPE");
        if (env && !strcmp(env, "always"))
-               gBufMgr->lock_type = LOCK_TRY_ALWAYS;
+               gBufMgr->bo_lock_type = TBM_BUFMGR_BO_LOCK_TYPE_ALWAYS;
        else if (env && !strcmp(env, "none"))
-               gBufMgr->lock_type = LOCK_TRY_NEVER;
+               gBufMgr->bo_lock_type = TBM_BUFMGR_BO_LOCK_TYPE_NEVER;
        else if (env && !strcmp(env, "once"))
-               gBufMgr->lock_type = LOCK_TRY_ONCE;
+               gBufMgr->bo_lock_type = TBM_BUFMGR_BO_LOCK_TYPE_ONCE;
        else
-               gBufMgr->lock_type = LOCK_TRY_ALWAYS;
-
-       DBG("BUFMGR_LOCK_TYPE=%s\n",
-           env ? env : "default:once");
+               gBufMgr->bo_lock_type = TBM_BUFMGR_BO_LOCK_TYPE_ALWAYS;
 
-       TBM_TRACE("create tbm_bufmgr(%p) ref_count(%d) fd(%d)\n", gBufMgr, gBufMgr->ref_count, fd);
+       TBM_DBG("BUFMGR_LOCK_TYPE=%s\n", env ? env : "default:once");
 
        /* intialize bo_list */
        LIST_INITHEAD(&gBufMgr->bo_list);
@@ -662,882 +361,800 @@ tbm_bufmgr_init(int fd)
        /* intialize surf_list */
        LIST_INITHEAD(&gBufMgr->surf_list);
 
+       /* intialize surf_queue_list */
+       LIST_INITHEAD(&gBufMgr->surf_queue_list);
+
        /* intialize debug_key_list */
        LIST_INITHEAD(&gBufMgr->debug_key_list);
 
+#ifdef TBM_BUFMGR_INIT_TIME
+       /* get the end tv */
+       gettimeofday(&end_tv, NULL);
+       TBM_INFO("tbm_bufmgr_init time: %ld ms", ((end_tv.tv_sec * 1000 + end_tv.tv_usec / 1000) - (start_tv.tv_sec * 1000 + start_tv.tv_usec / 1000)));
+#endif
+
        pthread_mutex_unlock(&gLock);
+
        return gBufMgr;
 }
 
+tbm_bufmgr
+tbm_bufmgr_init(int fd)
+{
+       tbm_bufmgr bufmgr;
+
+       bufmgr = _tbm_bufmgr_init(fd, 0);
+
+       return bufmgr;
+}
+
 void
 tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
 {
        TBM_RETURN_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr));
 
-       tbm_bo bo = NULL;
-       tbm_bo tmp = NULL;
-
-       tbm_surface_h surf = NULL;
-       tbm_surface_h tmp_surf = NULL;
-
+       _tbm_bufmgr_mutex_lock();
        pthread_mutex_lock(&gLock);
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        if (!gBufMgr) {
-               TBM_LOG_E("gBufmgr already destroy: bufmgr:%p\n", bufmgr);
+               TBM_ERR("gBufmgr already destroy: bufmgr:%p\n", bufmgr);
                pthread_mutex_unlock(&gLock);
+               _tbm_bufmgr_mutex_unlock();
                return;
        }
 
        bufmgr->ref_count--;
        if (bufmgr->ref_count > 0) {
-               TBM_TRACE("reduce a ref_count(%d) of tbm_bufmgr(%p)\n", bufmgr->ref_count, bufmgr);
+               TBM_DBG("reduce a ref_count(%d) of tbm_bufmgr(%p)\n", bufmgr->ref_count, bufmgr);
                pthread_mutex_unlock(&gLock);
+               _tbm_bufmgr_mutex_unlock();
                return;
        }
 
        /* destroy bo_list */
        if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
+               tbm_bo bo = NULL, tmp;
+
                LIST_FOR_EACH_ENTRY_SAFE(bo, tmp, &bufmgr->bo_list, item_link) {
-                       TBM_LOG_E("Un-freed bo(%p, ref:%d)\n",
-                               bo, bo->ref_cnt);
-                       bo->ref_cnt = 1;
-                       tbm_bo_unref(bo);
+                       TBM_ERR("Un-freed bo(%p, ref:%d)\n", bo, bo->ref_cnt);
+                       _tbm_bo_free(bo);
                }
+               LIST_DELINIT(&bufmgr->bo_list);
        }
 
        /* destroy surf_list */
        if (!LIST_IS_EMPTY(&bufmgr->surf_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(surf, tmp_surf, &bufmgr->surf_list, item_link) {
-                       TBM_LOG_E("Un-freed surf(%p, ref:%d)\n",
-                               surf, surf->refcnt);
+               tbm_surface_h surf = NULL, tmp;
+
+               LIST_FOR_EACH_ENTRY_SAFE(surf, tmp, &bufmgr->surf_list, item_link) {
+                       TBM_ERR("Un-freed surf(%p, ref:%d)\n", surf, surf->refcnt);
                        tbm_surface_destroy(surf);
                }
+               LIST_DELINIT(&bufmgr->surf_list);
        }
 
-       /* destroy bufmgr priv */
-       bufmgr->backend->bufmgr_deinit(bufmgr->backend->priv);
-       bufmgr->backend->priv = NULL;
-       tbm_backend_free(bufmgr->backend);
-       bufmgr->backend = NULL;
-
-       pthread_mutex_destroy(&bufmgr->lock);
+       tbm_module_unload(bufmgr->module);
 
-       TBM_TRACE("destroy tbm_bufmgr(%p)\n", bufmgr);
-
-       dlclose(bufmgr->module_data);
+       // TODO: this is temporary. it will be removed after finishing refactoring the tbm_module.
+       _tbm_bufmgr_copy_module_data(bufmgr, bufmgr->module, 1);
 
        if (bufmgr->fd > 0)
                close(bufmgr->fd);
 
+       TBM_INFO("destroy tbm_bufmgr(%p)\n", bufmgr);
+
        free(bufmgr);
-       bufmgr = NULL;
        gBufMgr = NULL;
 
        pthread_mutex_unlock(&gLock);
+       _tbm_bufmgr_mutex_unlock();
 }
 
-int
-tbm_bo_size(tbm_bo bo)
+unsigned int
+tbm_bufmgr_get_capability(tbm_bufmgr bufmgr)
 {
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
-
-       tbm_bufmgr bufmgr = bo->bufmgr;
-       int size;
+       unsigned int capabilities = TBM_BUFMGR_CAPABILITY_NONE;
 
-       pthread_mutex_lock(&bufmgr->lock);
+       _tbm_bufmgr_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
-       size = bufmgr->backend->bo_size(bo);
+       TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), TBM_BUFMGR_CAPABILITY_NONE);
+       TBM_BUFMGR_RETURN_VAL_IF_FAIL(bufmgr == gBufMgr, TBM_BUFMGR_CAPABILITY_NONE);
 
-       TBM_TRACE("bo(%p) size(%d)\n", bo, size);
+       capabilities = bufmgr->capabilities;
 
-       pthread_mutex_unlock(&bufmgr->lock);
+       _tbm_bufmgr_mutex_unlock();
 
-       return size;
+       return capabilities;
 }
 
-tbm_bo
-tbm_bo_ref(tbm_bo bo)
+/* LCOV_EXCL_START */
+char *
+tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr)
 {
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), NULL);
+       char app_name[255] = {0,}, title[512] = {0,};
+       tbm_surface_debug_data *debug_old_data = NULL;
+       char *str;
+       int len = 1024*4;
+       int c = 0;
+       int size;
+       tbm_error_e error;
+       long pid = 0;
 
-       tbm_bufmgr bufmgr = bo->bufmgr;
+       pthread_mutex_lock(&gLock);
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
-       pthread_mutex_lock(&bufmgr->lock);
-
-       _tbm_bo_ref(bo);
-
-       TBM_TRACE("bo(%p) ref_cnt(%d)\n", bo, bo->ref_cnt);
-
-       pthread_mutex_unlock(&bufmgr->lock);
-
-       return bo;
-}
-
-void
-tbm_bo_unref(tbm_bo bo)
-{
-       TBM_RETURN_IF_FAIL(_tbm_bo_is_valid(bo));
-
-       tbm_bufmgr bufmgr = bo->bufmgr;
-
-       pthread_mutex_lock(&bufmgr->lock);
-
-       TBM_TRACE("bo(%p) ref_cnt(%d)\n", bo, bo->ref_cnt - 1);
-
-       _tbm_bo_unref(bo);
-
-       pthread_mutex_unlock(&bufmgr->lock);
-}
-
-tbm_bo
-tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags)
-{
-       TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr) && (size > 0), NULL);
-
-       tbm_bo bo = NULL;
-       void *bo_priv = NULL;
-
-       bo = calloc(1, sizeof(struct _tbm_bo));
-       if (!bo) {
-               TBM_TRACE("error: fail to create of tbm_bo size(%d) flag(%s)\n", size, tbm_flag_to_str(flags));
-               _tbm_set_last_result(TBM_BO_ERROR_HEAP_ALLOC_FAILED);
-               return NULL;
-       }
-
-       _tbm_util_check_bo_cnt(bufmgr);
-       bufmgr->bo_cnt++;
-
-       bo->bufmgr = bufmgr;
-
-       pthread_mutex_lock(&bufmgr->lock);
-
-       bo_priv = bufmgr->backend->bo_alloc(bo, size, flags);
-       if (!bo_priv) {
-               TBM_TRACE("error: fail to create of tbm_bo size(%d) flag(%s)\n", size, tbm_flag_to_str(flags));
-               _tbm_set_last_result(TBM_BO_ERROR_BO_ALLOC_FAILED);
-               free(bo);
-               pthread_mutex_unlock(&bufmgr->lock);
+       if (!TBM_BUFMGR_IS_VALID(bufmgr) || (bufmgr != gBufMgr)) {
+               TBM_ERR("invalid bufmgr\n");
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
+               pthread_mutex_unlock(&gLock);
                return NULL;
        }
 
-       bo->ref_cnt = 1;
-       bo->flags = flags;
-       bo->priv = bo_priv;
-
-       TBM_TRACE("bo(%p) size(%d) refcnt(%d), flag(%s)\n", bo, size, bo->ref_cnt, tbm_flag_to_str(bo->flags));
-
-       LIST_INITHEAD(&bo->user_data_list);
-
-       LIST_ADD(&bo->item_link, &bufmgr->bo_list);
-
-       pthread_mutex_unlock(&bufmgr->lock);
-
-       return bo;
-}
-
-tbm_bo
-tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
-{
-       TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
-
-       tbm_bo bo = NULL;
-       tbm_bo bo2 = NULL;
-       tbm_bo tmp = NULL;
-       void *bo_priv = NULL;
-
-       _tbm_util_check_bo_cnt(bufmgr);
-
-       pthread_mutex_lock(&bufmgr->lock);
-
-       bo = calloc(1, sizeof(struct _tbm_bo));
-       if (!bo) {
-               TBM_TRACE("error: fail to import of tbm_bo by key(%d)\n", key);
-               pthread_mutex_unlock(&bufmgr->lock);
+       str = malloc(len);
+       if (!str) {
+               TBM_ERR("Fail to allocate the string.\n");
+               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+               pthread_mutex_unlock(&gLock);
                return NULL;
        }
 
-       bufmgr->bo_cnt++;
-
-       bo->bufmgr = bufmgr;
+       TBM_SNRPRINTF(str, len, c, "\n");
+       pid = syscall(SYS_getpid);
+       _tbm_util_get_appname_from_pid(pid, app_name);
+       _tbm_util_get_appname_brief(app_name);
+       TBM_SNRPRINTF(str, len, c, "===========================================TBM DEBUG: %s(%ld)===========================================\n",
+                 app_name, pid);
 
-       bo_priv = bufmgr->backend->bo_import(bo, key);
-       if (!bo_priv) {
-               TBM_TRACE("error: fail to import of tbm_bo by key(%d)\n", key);
-               _tbm_set_last_result(TBM_BO_ERROR_IMPORT_FAILED);
-               free(bo);
-               pthread_mutex_unlock(&bufmgr->lock);
-               return NULL;
-       }
+       snprintf(title, 255, "%s", "no  surface     refcnt  width  height  bpp  size    n_b  n_p  flags    format    app_name              ");
 
-       if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(bo2, tmp, &bufmgr->bo_list, item_link) {
-                       if (bo2->priv == bo_priv) {
-                               TBM_TRACE("find bo(%p) ref(%d) key(%d) flag(%s) in list\n",
-                                                       bo2, bo2->ref_cnt, key, tbm_flag_to_str(bo2->flags));
-                               bo2->ref_cnt++;
-                               free(bo);
-                               pthread_mutex_unlock(&bufmgr->lock);
-                               return bo2;
-                       }
+       if (!LIST_IS_EMPTY(&bufmgr->debug_key_list)) {
+               LIST_FOR_EACH_ENTRY(debug_old_data, &bufmgr->debug_key_list, item_link) {
+                       strncat(title, "  ", MAX_SIZE_N(title));
+                       strncat(title, debug_old_data->key, MAX_SIZE_N(title));
                }
        }
 
-       bo->ref_cnt = 1;
-       bo->priv = bo_priv;
-
-       if (bufmgr->backend->bo_get_flags)
-               bo->flags = bufmgr->backend->bo_get_flags(bo);
-       else
-               bo->flags = TBM_BO_DEFAULT;
-
-       TBM_TRACE("import new bo(%p) ref(%d) key(%d) flag(%s) in list\n",
-                         bo, bo->ref_cnt, key, tbm_flag_to_str(bo->flags));
-
-       LIST_INITHEAD(&bo->user_data_list);
+       TBM_SNRPRINTF(str, len, c, "[tbm_surface information]\n");
+       TBM_SNRPRINTF(str, len, c, "%s\n", title);
 
-       LIST_ADD(&bo->item_link, &bufmgr->bo_list);
-
-       pthread_mutex_unlock(&bufmgr->lock);
+       /* show the tbm_surface information in surf_list */
+       if (!LIST_IS_EMPTY(&bufmgr->surf_list)) {
+               tbm_surface_h surf = NULL;
+               int surf_cnt = 0;
 
-       return bo;
-}
+               LIST_FOR_EACH_ENTRY(surf, &bufmgr->surf_list, item_link) {
+                       char data[512] = {0,};
+                       unsigned int surf_pid = 0;
+                       int i;
 
-tbm_bo
-tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
-{
-       TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
+                       surf_pid = _tbm_surface_internal_get_debug_pid(surf);
+                       if (!surf_pid) {
+                               /* if pid is null, set the self_pid */
+                               surf_pid = syscall(SYS_getpid);;
+                       }
 
-       tbm_bo bo = NULL;
-       tbm_bo bo2 = NULL;
-       tbm_bo tmp = NULL;
-       void *bo_priv = NULL;
+                       memset(app_name, 0x0, 255 * sizeof(char));
+                       if (geteuid() == 0) {
+                               _tbm_util_get_appname_from_pid(surf_pid, app_name);
+                               _tbm_util_get_appname_brief(app_name);
+                       } else {
+                               snprintf(app_name, sizeof(app_name), "%d", surf_pid);
+                       }
 
-       _tbm_util_check_bo_cnt(bufmgr);
+                       snprintf(data, 255, "%-3d %-11p   %-5d %-6u %-7u %-4u %-7u  %-3d  %-3d %-8d %-9s %-22s",
+                                 ++surf_cnt,
+                                 surf,
+                                 surf->refcnt,
+                                 surf->info.width,
+                                 surf->info.height,
+                                 surf->info.bpp,
+                                 surf->info.size / 1024,
+                                 surf->num_bos,
+                                 surf->num_planes,
+                                 surf->flags,
+                                 _tbm_surface_internal_format_to_str(surf->info.format) + 11,
+                                 app_name);
 
-       pthread_mutex_lock(&bufmgr->lock);
+                       if (!LIST_IS_EMPTY(&bufmgr->debug_key_list)) {
+                               LIST_FOR_EACH_ENTRY(debug_old_data, &bufmgr->debug_key_list, item_link) {
+                                       char *value;
 
-       bo = calloc(1, sizeof(struct _tbm_bo));
-       if (!bo) {
-               TBM_TRACE("error: fail to import tbm_bo by tbm_fd(%d)\n", fd);
-               pthread_mutex_unlock(&bufmgr->lock);
-               return NULL;
-       }
+                                       strncat(data, "  ", MAX_SIZE_N(title));
 
-       bufmgr->bo_cnt++;
+                                       value = _tbm_surface_internal_get_debug_data(surf, debug_old_data->key);
+                                       if (value)
+                                               strncat(data, value, MAX_SIZE_N(title));
+                                       else
+                                               strncat(data, "none", MAX_SIZE_N(title));
+                               }
+                       }
+                       TBM_SNRPRINTF(str, len, c, "%s\n", data);
 
-       bo->bufmgr = bufmgr;
+                       for (i = 0; i < surf->num_bos; i++) {
+                               if (bufmgr->use_hal_tbm) {
+                                       size = hal_tbm_bo_get_size((hal_tbm_bo *)surf->bos[i]->bo_data, (hal_tbm_error *)&error);
+                                       if (error != TBM_ERROR_NONE)
+                                               TBM_WRN("fail to get the size of bo.");
+                               } else if (bufmgr->backend_module_data) {
+                                       size = bufmgr->bo_func->bo_get_size(surf->bos[i]->bo_data, &error);
+                                       if (error != TBM_ERROR_NONE)
+                                               TBM_WRN("fail to get the size of bo.");
+                               } else
+                                       size = bufmgr->backend->bo_size(surf->bos[i]);
+                               TBM_SNRPRINTF(str, len, c, " bo:%-12p  %-26d%-10d\n",
+                                         surf->bos[i],
+                                         surf->bos[i]->ref_cnt,
+                                         size / 1024);
+                       }
+               }
+       } else
+               TBM_SNRPRINTF(str, len, c, " no tbm_surfaces.\n");
+       TBM_SNRPRINTF(str, len, c, "\n");
 
-       bo_priv = bufmgr->backend->bo_import_fd(bo, fd);
-       if (!bo_priv) {
-               TBM_TRACE("error: fail to import tbm_bo by tbm_fd(%d)\n", fd);
-               _tbm_set_last_result(TBM_BO_ERROR_IMPORT_FD_FAILED);
-               free(bo);
-               pthread_mutex_unlock(&bufmgr->lock);
-               return NULL;
-       }
+       TBM_SNRPRINTF(str, len, c, "[tbm_bo information]\n");
+       TBM_SNRPRINTF(str, len, c, "no  bo          refcnt  size    lock_cnt  map_cnt  flags   surface     name\n");
 
+       /* show the tbm_bo information in bo_list */
        if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(bo2, tmp, &bufmgr->bo_list, item_link) {
-                       if (bo2->priv == bo_priv) {
-                               TBM_TRACE("find bo(%p) ref(%d) fd(%d) flag(%s) in list\n",
-                                                       bo2, bo2->ref_cnt, fd, tbm_flag_to_str(bo2->flags));
-                               bo2->ref_cnt++;
-                               free(bo);
-                               pthread_mutex_unlock(&bufmgr->lock);
-                               return bo2;
+               int bo_cnt = 0;
+               tbm_bo bo = NULL;
+               tbm_key key = 0;
+
+               LIST_FOR_EACH_ENTRY(bo, &bufmgr->bo_list, item_link) {
+                       if (bo->bufmgr->use_hal_tbm) {
+                               size = hal_tbm_bo_get_size((hal_tbm_bo *)bo->bo_data, (hal_tbm_error *)&error);
+                               if (error != TBM_ERROR_NONE)
+                                       TBM_WRN("fail to get the size of bo.");
+                               key = (tbm_key)hal_tbm_bo_export_key((hal_tbm_bo *)bo->bo_data, (hal_tbm_error *)&error);
+                       } else if (bufmgr->backend_module_data) {
+                               size = bufmgr->bo_func->bo_get_size(bo->bo_data, &error);
+                               if (error != TBM_ERROR_NONE)
+                                       TBM_WRN("fail to get the size of bo.");
+                               key = bufmgr->bo_func->bo_export_key(bo->bo_data, &error);
+                               if (error != TBM_ERROR_NONE)
+                                       TBM_WRN("fail to get the tdm_key of bo.");
+                       } else {
+                               size = bufmgr->backend->bo_size(bo);
+                               key = bufmgr->backend->bo_export(bo);
                        }
+                       TBM_SNRPRINTF(str, len, c, "%-3d %-11p   %-5d %-7d    %-6d    %-5u %-7d %-11p  %-4d\n",
+                                 ++bo_cnt,
+                                 bo,
+                                 bo->ref_cnt,
+                                 size / 1024,
+                                 bo->lock_cnt,
+                                 bo->map_cnt,
+                                 bo->flags,
+                                 bo->surface,
+                                 key);
                }
-       }
-
-       bo->ref_cnt = 1;
-       bo->priv = bo_priv;
-
-       if (bufmgr->backend->bo_get_flags)
-               bo->flags = bufmgr->backend->bo_get_flags(bo);
-       else
-               bo->flags = TBM_BO_DEFAULT;
-
-       TBM_TRACE("import bo(%p) ref(%d) fd(%d) flag(%s)in list\n",
-                               bo, bo->ref_cnt, fd, tbm_flag_to_str(bo->flags));
+       } else
+               TBM_SNRPRINTF(str, len, c, "no tbm_bos.\n");
+       TBM_SNRPRINTF(str, len, c, "\n");
 
-       LIST_INITHEAD(&bo->user_data_list);
-
-       LIST_ADD(&bo->item_link, &bufmgr->bo_list);
+       TBM_SNRPRINTF(str, len, c, "========================================================================================================\n");
 
-       pthread_mutex_unlock(&bufmgr->lock);
+       pthread_mutex_unlock(&gLock);
 
-       return bo;
+       return str;
 }
 
-tbm_key
-tbm_bo_export(tbm_bo bo)
+void
+tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
 {
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
-
-       tbm_bufmgr bufmgr;
-       tbm_key ret;
-
-       bufmgr = bo->bufmgr;
-
-       pthread_mutex_lock(&bufmgr->lock);
-
-       ret = bufmgr->backend->bo_export(bo);
-       if (!ret) {
-               _tbm_set_last_result(TBM_BO_ERROR_EXPORT_FAILED);
-               TBM_TRACE("error: bo(%p) tbm_key(%d)\n", bo, ret);
-               pthread_mutex_unlock(&bufmgr->lock);
-               return ret;
+       char * str;
+       str = tbm_bufmgr_debug_tbm_info_get(bufmgr);
+       if (str) {
+               TBM_DBG("       %s", str);
+               free(str);
        }
-
-       TBM_TRACE("bo(%p) tbm_key(%d)\n", bo, ret);
-
-       pthread_mutex_unlock(&bufmgr->lock);
-
-       return ret;
 }
 
-tbm_fd
-tbm_bo_export_fd(tbm_bo bo)
+void
+tbm_bufmgr_debug_trace(tbm_bufmgr bufmgr, int onoff)
 {
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), -1);
+       _tbm_bufmgr_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
-       tbm_bufmgr bufmgr;
-       int ret;
+       TBM_BUFMGR_RETURN_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr));
+       TBM_BUFMGR_RETURN_IF_FAIL(bufmgr == gBufMgr);
 
-       bufmgr = bo->bufmgr;
+#ifdef TRACE
+       TBM_LOG_D("bufmgr=%p onoff=%d\n", bufmgr, onoff);
+       bTrace = onoff;
+#endif
 
-       pthread_mutex_lock(&bufmgr->lock);
+       _tbm_bufmgr_mutex_unlock();
+}
 
-       ret = bufmgr->backend->bo_export_fd(bo);
-       if (ret < 0) {
-               _tbm_set_last_result(TBM_BO_ERROR_EXPORT_FD_FAILED);
-               TBM_TRACE("error: bo(%p) tbm_fd(%d)\n", bo, ret);
-               pthread_mutex_unlock(&bufmgr->lock);
-               return ret;
+void
+tbm_bufmgr_debug_set_trace_mask(tbm_bufmgr bufmgr, tbm_bufmgr_debug_trace_mask mask, int set)
+{
+       _tbm_bufmgr_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       TBM_BUFMGR_RETURN_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr));
+       TBM_BUFMGR_RETURN_IF_FAIL(bufmgr == gBufMgr);
+
+       if (set == 1) {
+               trace_mask |= mask;
+
+               TBM_INFO("bufmgr=%p sets the trace_mask=%d\n", bufmgr, mask);
+               if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_BO)
+                       TBM_INFO("  TBM_BUFGMR_DEBUG_TRACE_BO");
+               if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_SURFACE_INTERNAL)
+                       TBM_INFO("  TBM_BUFGMR_DEBUG_TRACE_SURFACE_INTERNAL");
+               if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_SURFACE)
+                       TBM_INFO("  TBM_BUFGMR_DEBUG_TRACE_SURFACE");
+               if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_SURFACE_QUEUE)
+                       TBM_INFO("  TBM_BUFGMR_DEBUG_TRACE_SURFACE_QUEUE");
+       } else if (set == 0) {
+               trace_mask &= ~mask;
+
+               TBM_INFO("bufmgr=%p unsets the trace_mask=%d\n", bufmgr, mask);
+               if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_BO)
+                       TBM_INFO("  TBM_BUFGMR_DEBUG_TRACE_BO");
+               if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_SURFACE_INTERNAL)
+                       TBM_INFO("  TBM_BUFGMR_DEBUG_TRACE_SURFACE_INTERNAL");
+               if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_SURFACE)
+                       TBM_INFO("  TBM_BUFGMR_DEBUG_TRACE_SURFACE");
+               if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_SURFACE_QUEUE)
+                       TBM_INFO("  TBM_BUFGMR_DEBUG_TRACE_SURFACE_QUEUE");
+       } else {
+               TBM_WRN("set value is wrong.(set=%d)", set);
        }
 
-       TBM_TRACE("bo(%p) tbm_fd(%d)\n", bo, ret);
-
-       pthread_mutex_unlock(&bufmgr->lock);
-
-       return ret;
+       _tbm_bufmgr_mutex_unlock();
 }
 
-tbm_bo_handle
-tbm_bo_get_handle(tbm_bo bo, int device)
+void
+tbm_bufmgr_debug_dump_set_scale(double scale)
 {
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), (tbm_bo_handle) 0);
-
-       tbm_bufmgr bufmgr;
-       tbm_bo_handle bo_handle;
+       pthread_mutex_lock(&gLock);
+       _tbm_set_last_result(TBM_ERROR_NONE);
+       scale_factor = scale;
+       pthread_mutex_unlock(&gLock);
+}
 
-       bufmgr = bo->bufmgr;
+int
+tbm_bufmgr_debug_get_ref_count(void)
+{
+       int refcnt;
 
-       pthread_mutex_lock(&bufmgr->lock);
+       pthread_mutex_lock(&gLock);
 
-       bo_handle = bufmgr->backend->bo_get_handle(bo, device);
-       if (bo_handle.ptr == NULL) {
-               _tbm_set_last_result(TBM_BO_ERROR_GET_HANDLE_FAILED);
-               TBM_TRACE("error: bo(%p) bo_handle(%p)\n", bo, bo_handle.ptr);
-               pthread_mutex_unlock(&bufmgr->lock);
-               return (tbm_bo_handle) NULL;
-       }
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
-       TBM_TRACE("bo(%p) bo_handle(%p)\n", bo, bo_handle.ptr);
+       refcnt = (gBufMgr) ? gBufMgr->ref_count : 0;
 
-       pthread_mutex_unlock(&bufmgr->lock);
+       pthread_mutex_unlock(&gLock);
 
-       return bo_handle;
+       return refcnt;
 }
 
-tbm_bo_handle
-tbm_bo_map(tbm_bo bo, int device, int opt)
+int
+tbm_bufmgr_debug_queue_dump(char *path, int count, int onoff)
 {
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), (tbm_bo_handle) 0);
+       pthread_mutex_lock(&gLock);
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
-       tbm_bufmgr bufmgr;
-       tbm_bo_handle bo_handle;
+       if (onoff == 0) {
+               TBM_DBG("count=%d onoff=%d\n", count, onoff);
+               b_dump_queue = 0;
+               tbm_surface_internal_dump_end();
+       } else {
+               int w, h;
 
-       bufmgr = bo->bufmgr;
+               if (path == NULL) {
+                       TBM_ERR("path is null");
+                       pthread_mutex_unlock(&gLock);
+                       return 0;
+               }
+               TBM_DBG("path=%s count=%d onoff=%d\n", path, count, onoff);
 
-       pthread_mutex_lock(&bufmgr->lock);
+               if (_tbm_util_get_max_surface_size(&w, &h) == 0) {
+                       TBM_ERR("Fail to get tbm_surface size.\n");
+                       pthread_mutex_unlock(&gLock);
+                       return 0;
+               }
 
-       if (!_tbm_bo_lock(bo, device, opt)) {
-               _tbm_set_last_result(TBM_BO_ERROR_LOCK_FAILED);
-               TBM_TRACE("error: fail to lock bo:%p)\n", bo);
-               pthread_mutex_unlock(&bufmgr->lock);
-               return (tbm_bo_handle) NULL;
-       }
+               tbm_surface_internal_dump_with_scale_start(path, w, h, count, scale_factor);
+               scale_factor = 0;
 
-       bo_handle = bufmgr->backend->bo_map(bo, device, opt);
-       if (bo_handle.ptr == NULL) {
-               _tbm_set_last_result(TBM_BO_ERROR_MAP_FAILED);
-               TBM_TRACE("error: fail to map bo:%p\n", bo);
-               _tbm_bo_unlock(bo);
-               pthread_mutex_unlock(&bufmgr->lock);
-               return (tbm_bo_handle) NULL;
+               b_dump_queue = 1;
        }
 
-       /* increase the map_count */
-       bo->map_cnt++;
-
-       TBM_TRACE("bo(%p) map_cnt(%d)\n", bo, bo->map_cnt);
-
-       pthread_mutex_unlock(&bufmgr->lock);
+       pthread_mutex_unlock(&gLock);
 
-       return bo_handle;
+       return 1;
 }
 
 int
-tbm_bo_unmap(tbm_bo bo)
+tbm_bufmgr_debug_dump_all(char *path)
 {
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
+       int w, h, count = 0;
+       tbm_surface_h surface = NULL;
 
-       tbm_bufmgr bufmgr;
-       int ret;
+       pthread_mutex_lock(&gLock);
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
-       bufmgr = bo->bufmgr;
+       if (!path) {
+               TBM_ERR("path is null.\n");
+               pthread_mutex_unlock(&gLock);
+               return 0;
+       }
 
-       pthread_mutex_lock(&bufmgr->lock);
+       TBM_DBG("path=%s\n", path);
 
-       ret = bufmgr->backend->bo_unmap(bo);
-       if (!ret) {
-               TBM_TRACE("error: bo(%p) map_cnt(%d)\n", bo, bo->map_cnt);
-               _tbm_set_last_result(TBM_BO_ERROR_UNMAP_FAILED);
-               pthread_mutex_unlock(&bufmgr->lock);
-               return ret;
+       count = _tbm_util_get_max_surface_size(&w, &h);
+       if (count == 0) {
+               TBM_ERR("No tbm_surface.\n");
+               pthread_mutex_unlock(&gLock);
+               return 0;
        }
 
-       /* decrease the map_count */
-       bo->map_cnt--;
+       tbm_surface_internal_dump_with_scale_start(path, w, h, count, scale_factor);
+       scale_factor = 0;
 
-       TBM_TRACE("bo(%p) map_cnt(%d)\n", bo, bo->map_cnt);
+       LIST_FOR_EACH_ENTRY(surface, &gBufMgr->surf_list, item_link)
+               tbm_surface_internal_dump_buffer(surface, "dump_all");
 
-       _tbm_bo_unlock(bo);
+       tbm_surface_internal_dump_end();
 
-       pthread_mutex_unlock(&bufmgr->lock);
+       pthread_mutex_unlock(&gLock);
 
-       return ret;
+       return 1;
+}
+
+/* internal function */
+tbm_bufmgr
+_tbm_bufmgr_get_bufmgr(void)
+{
+       return gBufMgr;
 }
 
 int
-tbm_bo_swap(tbm_bo bo1, tbm_bo bo2)
+tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *native_display)
 {
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo1), 0);
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo2), 0);
+       tbm_error_e error;
 
-       void *temp;
+       _tbm_bufmgr_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
-       pthread_mutex_lock(&bo1->bufmgr->lock);
+       TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), 0);
 
-       TBM_TRACE("before: bo1(%p) bo2(%p)\n", bo1, bo2);
+       error = tbm_module_bufmgr_bind_native_display(bufmgr->module, native_display);
+       if (error != TBM_ERROR_NONE) {
+               _tbm_set_last_result(error);
+               _tbm_bufmgr_mutex_unlock();
+
+               if (error == TBM_ERROR_NOT_SUPPORTED) {
+                       TBM_WRN("Not supported, so skip: tbm_bufmgr(%p) native_display(%p)", bufmgr, native_display);
+                       return 1;
+               }
 
-       if (bo1->bufmgr->backend->bo_size(bo1) != bo2->bufmgr->backend->bo_size(bo2)) {
-               _tbm_set_last_result(TBM_BO_ERROR_SWAP_FAILED);
-               TBM_TRACE("error: bo1(%p) bo2(%p)\n", bo1, bo2);
-       pthread_mutex_unlock(&bo1->bufmgr->lock);
                return 0;
        }
 
-       TBM_TRACE("after: bo1(%p) bo2(%p)\n", bo1, bo2);
-
-       temp = bo1->priv;
-       bo1->priv = bo2->priv;
-       bo2->priv = temp;
+       TBM_INFO("tbm_bufmgr(%p) native_display(%p)\n", bufmgr, native_display);
 
-       pthread_mutex_unlock(&bo1->bufmgr->lock);
+       _tbm_bufmgr_mutex_unlock();
 
        return 1;
 }
 
-int
-tbm_bo_locked(tbm_bo bo)
+tbm_bufmgr
+tbm_bufmgr_server_init(void)
 {
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
-
        tbm_bufmgr bufmgr;
 
-       bufmgr = bo->bufmgr;
-
-       if (bufmgr->lock_type == LOCK_TRY_NEVER) {
-               TBM_TRACE("bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
-               return 0;
-       }
-
-       pthread_mutex_lock(&bufmgr->lock);
+       bufmgr = _tbm_bufmgr_init(-1, 1);
 
-
-       if (bo->lock_cnt > 0) {
-               TBM_TRACE("error: bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
-               pthread_mutex_unlock(&bufmgr->lock);
-               return 1;
-       }
-
-       TBM_TRACE("bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
-       pthread_mutex_unlock(&bufmgr->lock);
-
-       return 0;
+       return bufmgr;
 }
 
 int
-tbm_bo_add_user_data(tbm_bo bo, unsigned long key,
-                    tbm_data_free data_free_func)
+tbm_bufmgr_set_bo_lock_type(tbm_bufmgr bufmgr, tbm_bufmgr_bo_lock_type bo_lock_type)
 {
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
+       _tbm_bufmgr_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
-       tbm_user_data *data;
-
-       /* check if the data according to the key exist if so, return false. */
-       data = user_data_lookup(&bo->user_data_list, key);
-       if (data) {
-               TBM_TRACE("warning: user data already exist key(%ld)\n", key);
-               return 0;
-       }
+       TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
+       TBM_BUFMGR_RETURN_VAL_IF_FAIL(bufmgr == gBufMgr, 0);
 
-       data = user_data_create(key, data_free_func);
-       if (!data) {
-               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
-               return 0;
-       }
+       pthread_mutex_lock(&gLock);
+       gBufMgr->bo_lock_type = bo_lock_type;
+       pthread_mutex_unlock(&gLock);
 
-       TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, data->data);
+       TBM_INFO("The bo_lock_type of the bo is %d\n", bo_lock_type);
 
-       LIST_ADD(&data->item_link, &bo->user_data_list);
+       _tbm_bufmgr_mutex_unlock();
 
        return 1;
 }
 
-int
-tbm_bo_set_user_data(tbm_bo bo, unsigned long key, void *data)
-{
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
-
-       tbm_user_data *old_data;
 
-       if (LIST_IS_EMPTY(&bo->user_data_list)) {
-               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
-               return 0;
-       }
-
-       old_data = user_data_lookup(&bo->user_data_list, key);
-       if (!old_data) {
-               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
-               return 0;
-       }
-
-       if (old_data->data && old_data->free_func)
-               old_data->free_func(old_data->data);
-
-       old_data->data = data;
+int tbm_bufmgr_get_fd_limit(void)
+{
+       struct rlimit lim;
 
-       TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data);
+       if (getrlimit(RLIMIT_NOFILE, &lim))
+               return 1024;
 
-       return 1;
+       return (int)lim.rlim_cur;
 }
 
-int
-tbm_bo_get_user_data(tbm_bo bo, unsigned long key, void **data)
+tbm_bufmgr tbm_bufmgr_get(void)
 {
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
+       return gBufMgr;
+}
 
-       tbm_user_data *old_data;
 
-       if (!data || LIST_IS_EMPTY(&bo->user_data_list)) {
-               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
-               return 0;
-       }
+tbm_bo
+tbm_bufmgr_internal_find_bo(tbm_bufmgr bufmgr, tbm_bo bo)
+{
+       tbm_bo bo2 = NULL;
 
-       old_data = user_data_lookup(&bo->user_data_list, key);
-       if (!old_data) {
-               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
-               *data = NULL;
-               return 0;
-       }
+       TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), NULL);
+       TBM_RETURN_VAL_IF_FAIL(bufmgr == gBufMgr, NULL);
 
-       *data = old_data->data;
+       if (LIST_IS_EMPTY(&bufmgr->bo_list))
+               return NULL;
 
-       TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data);
+       LIST_FOR_EACH_ENTRY(bo2, &bufmgr->bo_list, item_link) {
+               if (bo2->bo_data == bo->bo_data) {
+                       return bo2;
+               }
+       }
 
-       return 1;
+       return NULL;
 }
 
-int
-tbm_bo_delete_user_data(tbm_bo bo, unsigned long key)
+tbm_bo
+tbm_bufmgr_internal_alloc_bo(tbm_bufmgr bufmgr, int size, int flags, tbm_error_e *error)
 {
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
+       tbm_bo bo;
+       tbm_backend_bo_data *bo_data;
 
-       tbm_user_data *old_data = (void *)0;
+       _tbm_bufmgr_check_bo_cnt(bufmgr);
 
-       if (LIST_IS_EMPTY(&bo->user_data_list)) {
-               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
-               return 0;
+       bo = calloc(1, sizeof(struct _tbm_bo));
+       if (!bo) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("memory allocationc failed.");
+               *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
-       old_data = user_data_lookup(&bo->user_data_list, key);
-       if (!old_data) {
-               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
-               return 0;
+       bo_data = tbm_module_bufmgr_bo_alloc(bufmgr->module, bo, size, flags, error);
+       if (!bo_data) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("tbm_module_bufmgr_bo_alloc failed. size:%d flags:%s error:%d", size, _tbm_flag_to_str(flags), *error);
+               free(bo);
+               return NULL;
+               /* LCOV_EXCL_STOP */
        }
+       bo->bo_data = bo_data;
+       bo->priv = (void *)bo_data; // TODO: this will be DEPRECATED.
 
-       TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data);
-
-       user_data_delete(old_data);
-
-       return 1;
-}
-
-unsigned int
-tbm_bufmgr_get_capability(tbm_bufmgr bufmgr)
-{
-       TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), 0);
-
-       TBM_TRACE("tbm_bufmgr(%p) capability(%d)\n", bufmgr, bufmgr->capabilities);
+       _tbm_bufmgr_initialize_bo(bufmgr, bo, flags);
 
-       return bufmgr->capabilities;
+       return bo;
 }
 
-int
-tbm_bo_get_flags(tbm_bo bo)
-{
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
-
-       TBM_TRACE("bo(%p)\n", bo);
-
-       return bo->flags;
-}
 
 /* LCOV_EXCL_START */
-tbm_error_e
-tbm_get_last_error(void)
-{
-       return tbm_last_error;
-}
 
-void
-tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
+tbm_bo
+tbm_bufmgr_internal_alloc_bo_with_format(tbm_bufmgr bufmgr, int format, int bo_idx, int width,
+                                               int height, int bpp, tbm_bo_memory_type flags, tbm_error_e *error)
 {
-       TBM_RETURN_IF_FAIL(bufmgr != NULL);
-       tbm_bo bo = NULL, tmp_bo = NULL;
-       int bo_cnt = 0;
-
-       tbm_surface_h surf = NULL, tmp_surf = NULL;
-       int surf_cnt = 0;
-       int i;
-       char app_name[255] = {0,};
-       unsigned int pid = 0;
-       char title[255] = {0,};
-       char data[255] = {0,};
-       tbm_surface_debug_data *debug_old_data = NULL, *debug_tmp = NULL;
-
-       pthread_mutex_lock(&gLock);
-
-       TBM_DEBUG("\n");
-       _tbm_util_get_appname_from_pid(getpid(), app_name);
-       _tbm_util_get_appname_brief(app_name);
-       TBM_DEBUG("============TBM DEBUG: %s(%d)===========================\n",
-                 app_name, getpid());
-       memset(app_name, 0x0, 255 * sizeof(char));
-       snprintf(title, 255, "%s", "no  surface     refcnt  width  height  bpp  size    n_b  n_p  flags  format    app_name       ");
-       if (!LIST_IS_EMPTY(&bufmgr->debug_key_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(debug_old_data, debug_tmp, &bufmgr->debug_key_list, item_link) {
-                       strncat(title, "  ", 2);
-                       strncat(title, debug_old_data->key, strlen(debug_old_data->key) + 1);
-               }
-       }
-
-       TBM_DEBUG("[tbm_surface information]\n");
-       TBM_DEBUG("%s\n", title);
-       /* show the tbm_surface information in surf_list */
-       if (!LIST_IS_EMPTY(&bufmgr->surf_list)) {
-               char *value = NULL;
-
-               LIST_FOR_EACH_ENTRY_SAFE(surf, tmp_surf, &bufmgr->surf_list, item_link) {
-                       pid = _tbm_surface_internal_get_debug_pid(surf);
-                       if (!pid) {
-                               /* if pid is null, set the self_pid */
-                               pid = getpid();
-                       }
+       tbm_bo bo = NULL;
 
-                       _tbm_util_get_appname_from_pid(pid, app_name);
-                       _tbm_util_get_appname_brief(app_name);
+       _tbm_bufmgr_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
-                       snprintf(data, 255, "%-2d  %-9p    %-4d  %-5d  %-6d  %-3d  %-6d   %-2d   %-2d    %-3d  %-8s  %-15s",
-                                 ++surf_cnt,
-                                 surf,
-                                 surf->refcnt,
-                                 surf->info.width,
-                                 surf->info.height,
-                                 surf->info.bpp,
-                                 surf->info.size / 1024,
-                                 surf->num_bos,
-                                 surf->num_planes,
-                                 surf->flags,
-                                 _tbm_surface_internal_format_to_str(surf->info.format) + 11,
-                                 app_name);
+       TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
 
-                       if (!LIST_IS_EMPTY(&bufmgr->debug_key_list)) {
-                               LIST_FOR_EACH_ENTRY_SAFE(debug_old_data, debug_tmp, &bufmgr->debug_key_list, item_link) {
-                                       strncat(data, "  ", 2);
+       _tbm_bufmgr_check_bo_cnt(bufmgr);
 
-                                       value = _tbm_surface_internal_get_debug_data(surf, debug_old_data->key);
-                                       if (value)
-                                               strncat(data, value, strlen(value) + 1);
-                                       else
-                                               strncat(data, "none", strlen("none") + 1);
-                               }
-                       }
-                       TBM_DEBUG("%s\n", data);
-
-                       for (i = 0; i < surf->num_bos; i++) {
-                               TBM_DEBUG(" bo:%-12p  %-26d%-10d\n",
-                                         surf->bos[i],
-                                         surf->bos[i]->ref_cnt,
-                                         tbm_bo_size(surf->bos[i]) / 1024);
-                       }
+       bo = calloc(1, sizeof(struct _tbm_bo));
+       if (!bo) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("memory allocationc failed.");
+               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+               /* LCOV_EXCL_STOP */
+               goto fail;
+       }
 
-                       memset(app_name, 0x0, 255 * sizeof(char));
-               }
-       } else {
-               TBM_DEBUG("no tbm_surfaces.\n");
+       bo->bo_data = tbm_module_bufmgr_bo_alloc_with_format(bufmgr->module, format, bo_idx, width, height, bpp, flags, error);
+       if (!bo->bo_data) {
+               /* LCOV_EXCL_START */
+               _tbm_set_last_result(*error);
+               /* LCOV_EXCL_STOP */
+               goto fail;
        }
-       TBM_DEBUG("\n");
 
-       TBM_DEBUG("[tbm_bo information]\n");
-       TBM_DEBUG("no  bo          refcnt  size    lock_cnt  map_cnt  flags  surface\n");
+       _tbm_bufmgr_initialize_bo(bufmgr, bo, flags);
 
-       /* show the tbm_bo information in bo_list */
-       if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(bo, tmp_bo, &bufmgr->bo_list, item_link) {
-                       TBM_DEBUG("%-4d%-11p   %-4d  %-6d     %-5d     %-4d    %-3d  %-11p\n",
-                                 ++bo_cnt,
-                                 bo,
-                                 bo->ref_cnt,
-                                 tbm_bo_size(bo) / 1024,
-                                 bo->lock_cnt,
-                                 bo->map_cnt,
-                                 bo->flags,
-                                 bo->surface);
-               }
-       } else {
-               TBM_DEBUG("no tbm_bos.\n");
-       }
-       TBM_DEBUG("\n");
+       _tbm_bufmgr_mutex_unlock();
 
-       TBM_DEBUG("===============================================================\n");
+       return bo;
 
-       pthread_mutex_unlock(&gLock);
+fail:
+       if (bo)
+               free(bo);
+       _tbm_bufmgr_mutex_unlock();
 
+       return NULL;
 }
 
-void
-tbm_bufmgr_debug_trace(tbm_bufmgr bufmgr, int onoff)
+tbm_bo
+tbm_bufmgr_internal_alloc_bo_with_bo_data(tbm_bufmgr bufmgr, tbm_backend_bo_data *bo_data, int flags)
 {
-#ifdef TRACE
-       TBM_LOG_D("bufmgr=%p onoff=%d\n", bufmgr, onoff);
-       bTrace = onoff;
-#endif
-}
+       tbm_bo bo, bo2 = NULL;
 
-int
-tbm_bufmgr_debug_queue_dump(char *path, int count, int onoff)
-{
-       TBM_LOG_D("path=%s count=%d onoff=%d\n", path, count, onoff);
+       _tbm_bufmgr_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
-       if (onoff == 1) {
+       TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
+       TBM_BUFMGR_RETURN_VAL_IF_FAIL(bo_data, NULL);
 
-               TBM_RETURN_VAL_IF_FAIL(path != NULL, 0);
+       _tbm_bufmgr_check_bo_cnt(bufmgr);
 
-               int w = 0, h = 0;
-               if (_tbm_util_get_max_surface_size(&w, &h) == 0) {
-                       TBM_LOG_I("No tbm_surface.\n");
-                       return 0;
-               }
+       bo = calloc(1, sizeof(struct _tbm_bo));
+       if (!bo) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("memory allocationc failed.");
+               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+               _tbm_bufmgr_mutex_unlock();
+               return NULL;
+               /* LCOV_EXCL_STOP */
+       }
+       bo->bo_data = bo_data;
+       bo->get_from_hal_surface = 1;
 
-               tbm_surface_internal_dump_start(path, w, h, count);
-               b_dump_queue = 1;
+       // return an existed bo if the bo is already created with the same bo_data.
+       bo2 = tbm_bufmgr_internal_find_bo(bufmgr, bo);
+       if (bo2) {
+               bo2->ref_cnt++;
+               free(bo);
+               _tbm_bufmgr_mutex_unlock();
+               return bo2;
+       }
 
-       } else if (onoff == 0) {
+       _tbm_bufmgr_initialize_bo(bufmgr, bo, flags);
 
-               tbm_surface_internal_dump_end();
-               b_dump_queue = 0;
+       TBM_TRACE_BO("bo(%p) refcnt(%d), flag(%s)", bo, bo->ref_cnt, _tbm_flag_to_str(bo->flags));
 
-       } else {
-               return 0;
-       }
+       _tbm_bufmgr_mutex_unlock();
 
-       return 1;
+       return bo;
 }
 
-int
-tbm_bufmgr_debug_dump_all(char *path)
+tbm_bo
+tbm_bufmgr_internal_import_bo_with_key(tbm_bufmgr bufmgr, unsigned int key, tbm_error_e *error)
 {
-       TBM_RETURN_VAL_IF_FAIL(path != NULL, 0);
+       tbm_bo bo, bo2;
+       int flags;
 
-       TBM_LOG_D("path=%s\n", path);
-       int w = 0, h = 0, count = 0;
-       tbm_surface_h surface = NULL, tmp = NULL;
+       _tbm_bufmgr_check_bo_cnt(bufmgr);
 
-       count = _tbm_util_get_max_surface_size(&w, &h);
-       if (count == 0) {
-               TBM_LOG_I("No tbm_surface.\n");
-               return 1;
+       bo = calloc(1, sizeof(struct _tbm_bo));
+       if (!bo) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("memory allocationc failed.");
+               *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
-       tbm_surface_internal_dump_start(path, w, h, count);
-
-       LIST_FOR_EACH_ENTRY_SAFE(surface, tmp, &gBufMgr->surf_list, item_link) {
-               tbm_surface_internal_dump_buffer(surface, "dump_all");
+       bo->bo_data = tbm_module_bufmgr_bo_import_key(bufmgr->module, bo, key, error);
+       if (!bo->bo_data) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("tbm_module_bufmgr_bo_import_key failed. tbm_key:%d", key);
+               free(bo);
+               return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
-       tbm_surface_internal_dump_end();
-
-       return 1;
-
-}
-
-/* internal function */
-tbm_bufmgr
-_tbm_bufmgr_get_bufmgr(void)
-{
-       return gBufMgr;
-}
+       // return the existed bo2 if bo->bo_data and bo2->bo_data is the same
+       bo2 = tbm_bufmgr_internal_find_bo(bufmgr, bo);
+       if (bo2) {
+               TBM_TRACE_BO("find bo(%p) ref(%d) key(%d) flag(%s) in list",
+                                       bo2, bo2->ref_cnt, key, _tbm_flag_to_str(bo2->flags));
+               bo2->ref_cnt++;
+               free(bo);
+               return bo2;
+       }
 
-int
-_tbm_bo_set_surface(tbm_bo bo, tbm_surface_h surface)
-{
-       TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
+       // TODO: refactoring tbm_module_bo
+       if (bufmgr->use_hal_tbm) {
+               flags = (tbm_bo_memory_type)hal_tbm_bo_get_memory_types((hal_tbm_bo *)bo->bo_data, (hal_tbm_error *)error);
+               if (*error != TBM_ERROR_NONE) {
+                       TBM_ERR("fail to get the bo flags(memory_types)");
+                       flags = TBM_BO_DEFAULT;
+               }
+       } else if (bufmgr->backend_module_data) {
+               flags = bufmgr->bo_func->bo_get_memory_types(bo->bo_data, error);
+               if (error != TBM_ERROR_NONE) {
+                       TBM_ERR("fail to get the bo flags(memory_types)");
+                       flags = TBM_BO_DEFAULT;
+               }
+       } else {
+               if (bufmgr->backend->bo_get_flags)
+                       flags = bufmgr->backend->bo_get_flags(bo);
+               else
+                       flags = TBM_BO_DEFAULT;
+       }
 
-       bo->surface = surface;
+       _tbm_bufmgr_initialize_bo(bufmgr, bo, flags);
 
-       return 1;
+       return bo;
 }
 
-int
-tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *NativeDisplay)
+tbm_bo
+tbm_bufmgr_internal_import_bo_with_fd(tbm_bufmgr bufmgr, tbm_fd fd, tbm_error_e *error)
 {
-       TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), 0);
+       tbm_bo bo, bo2;
+       int flags;
 
-       int ret;
+       _tbm_bufmgr_check_bo_cnt(bufmgr);
 
-       pthread_mutex_lock(&bufmgr->lock);
+       bo = calloc(1, sizeof(struct _tbm_bo));
+       if (!bo) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("memory allocationc failed.");
+               *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+               /* LCOV_EXCL_STOP */
+       }
 
-       if (!bufmgr->backend->bufmgr_bind_native_display) {
-               TBM_TRACE("error: tbm_bufmgr(%p) NativeDisplay(%p)\n", bufmgr, NativeDisplay);
-               pthread_mutex_unlock(&bufmgr->lock);
-               return 1;
+       bo->bo_data = tbm_module_bufmgr_bo_import_fd(bufmgr->module, bo, fd, error);
+       if (!bo->bo_data) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("tbm_module_bufmgr_bo_import_fd failed. tbm_fd:%d", fd);
+               free(bo);
+               return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
-       ret = bufmgr->backend->bufmgr_bind_native_display(bufmgr, NativeDisplay);
-       if (!ret) {
-               TBM_TRACE("error: tbm_bufmgr(%p) NativeDisplay(%p)\n", bufmgr, NativeDisplay);
-               pthread_mutex_unlock(&bufmgr->lock);
-               return 0;
+       // return the existed bo2 if bo->bo_data and bo2->bo_data is the same
+       bo2 = tbm_bufmgr_internal_find_bo(bufmgr, bo);
+       if (bo2) {
+               TBM_TRACE_BO("find bo(%p) ref(%d) fd(%d) flag(%s) in list",
+                                       bo2, bo2->ref_cnt, fd, _tbm_flag_to_str(bo2->flags));
+               bo2->ref_cnt++;
+               free(bo);
+               return bo2;
        }
 
-       TBM_TRACE("tbm_bufmgr(%p) NativeDisplay(%p)\n", bufmgr, NativeDisplay);
+       // TODO: refactoring tbm_module_bo
+       if (bufmgr->use_hal_tbm) {
+               flags = (tbm_bo_memory_type)hal_tbm_bo_get_memory_types((hal_tbm_bo *)bo->bo_data, (hal_tbm_error *)error);
+               if (error != TBM_ERROR_NONE) {
+                       TBM_ERR("fail to get the bo flags(memory_types)");
+                       flags = TBM_BO_DEFAULT;
+               }
+       } else if (bufmgr->backend_module_data) {
+               flags = bufmgr->bo_func->bo_get_memory_types(bo->bo_data, error);
+               if (error != TBM_ERROR_NONE) {
+                       TBM_ERR("fail to get the bo flags(memory_types)");
+                       flags = TBM_BO_DEFAULT;
+               }
+       } else {
+               if (bufmgr->backend->bo_get_flags)
+                       flags = bufmgr->backend->bo_get_flags(bo);
+               else
+                       flags = TBM_BO_DEFAULT;
+       }
 
-       pthread_mutex_unlock(&bufmgr->lock);
+       _tbm_bufmgr_initialize_bo(bufmgr, bo, flags);
 
-       return 1;
+       return bo;
 }
+
 /* LCOV_EXCL_STOP */