add scale factor to tbm_bufmgr_debug_dump_all() and tbm_bufmgr_debug_queue_dump(...
[platform/core/uifw/libtbm.git] / src / tbm_bufmgr.c
index d02dd41..f101e73 100644 (file)
@@ -36,6 +36,8 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "tbm_bufmgr_backend.h"
 #include "list.h"
 
+#include <sys/resource.h>
+
 #ifdef DEBUG
 int bDebug;
 #endif
@@ -54,7 +56,7 @@ int b_dump_queue;
 static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t tbm_bufmgr_lock = PTHREAD_MUTEX_INITIALIZER;
 static __thread tbm_error_e tbm_last_error = TBM_ERROR_NONE;
-
+static double scale_factor = 0;
 static void _tbm_bufmgr_mutex_unlock(void);
 
 //#define TBM_BUFMGR_INIT_TIME
@@ -104,6 +106,7 @@ _tbm_set_last_result(tbm_error_e err)
        tbm_last_error = err;
 }
 
+/* LCOV_EXCL_START */
 static bool
 _tbm_bufmgr_mutex_init(void)
 {
@@ -113,7 +116,7 @@ _tbm_bufmgr_mutex_init(void)
                return true;
 
        if (pthread_mutex_init(&tbm_bufmgr_lock, NULL)) {
-               TBM_LOG_E("fail: tbm_bufmgr mutex init\n");
+               TBM_LOG_E("fail: Cannot pthread_mutex_init for tbm_bufmgr_lock.\n");
                return false;
        }
 
@@ -125,8 +128,10 @@ _tbm_bufmgr_mutex_init(void)
 static void
 _tbm_bufmgr_mutex_lock(void)
 {
-       if (!_tbm_bufmgr_mutex_init())
+       if (!_tbm_bufmgr_mutex_init()) {
+               TBM_LOG_E("fail: _tbm_bufmgr_mutex_init()\n");
                return;
+       }
 
        pthread_mutex_lock(&tbm_bufmgr_lock);
 }
@@ -137,57 +142,71 @@ _tbm_bufmgr_mutex_unlock(void)
        pthread_mutex_unlock(&tbm_bufmgr_lock);
 }
 
-char * tbm_flag_to_str(int f)
+static char *
+_tbm_flag_to_str(int f)
 {
        static char str[255];
-       int c = 0;
+
        if (f == TBM_BO_DEFAULT)
-                snprintf(str, 255, "DEFAULT\n");
+                snprintf(str, 255, "DEFAULT");
        else {
+               int c = 0;
+
                if (f & TBM_BO_SCANOUT)
-                       c = snprintf(&str[c], 255, "SCANOUT,");
-               if (f & TBM_BO_NONCACHABLE)
+                       c = snprintf(&str[c], 255, "SCANOUT");
+
+               if (f & TBM_BO_NONCACHABLE) {
+                       if (c)
+                               c = snprintf(&str[c], 255, ", ");
                        c = snprintf(&str[c], 255, "NONCACHABLE,");
-               if (f & TBM_BO_WC)
+               }
+
+               if (f & TBM_BO_WC) {
+                       if (c)
+                               c = snprintf(&str[c], 255, ", ");
                        c = snprintf(&str[c], 255, "WC");
+               }
        }
+
        return str;
 }
 
-/* LCOV_EXCL_START */
-static int last_chk_bo_cnt = 0;
 static void
 _tbm_util_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_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
-_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;
                }
        }
 
@@ -216,25 +235,24 @@ _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);
+       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_LOG_E("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_LOG_E("fail to get appname for pid(%ld)\n", pid);
                fclose(fp);
                return;
        }
+
        fclose(fp);
 
        len = strlen(cmdline);
@@ -250,33 +268,34 @@ _tbm_util_get_appname_from_pid(long pid, char *str)
 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;
-                       }
-               }
+       tbm_user_data *old_data = NULL;
+
+       if (LIST_IS_EMPTY(user_data_list))
+               return NULL;
+
+       LIST_FOR_EACH_ENTRY(old_data, user_data_list, item_link) {
+               if (old_data->key == key)
+                       return old_data;
        }
 
-       return user_data;
+       return NULL;
 }
 
 tbm_user_data
 *user_data_create(unsigned long key, tbm_data_free data_free_func)
 {
-       tbm_user_data *user_data = NULL;
+       tbm_user_data *user_data;
 
        user_data = calloc(1, sizeof(tbm_user_data));
-       if (!user_data)
+       if (!user_data) {
+               /* LCOV_EXCL_START */
+               TBM_LOG_E("fail to allocate an user_date\n");
                return NULL;
+               /* LCOV_EXCL_STOP */
+       }
 
        user_data->key = key;
        user_data->free_func = data_free_func;
-       user_data->data = (void *)0;
 
        return user_data;
 }
@@ -296,12 +315,10 @@ static int
 _bo_lock(tbm_bo bo, int device, int opt)
 {
        tbm_bufmgr bufmgr = bo->bufmgr;
-       int ret = 0;
+       int ret = 1;
 
        if (bufmgr->backend->bo_lock)
                ret = bufmgr->backend->bo_lock(bo, device, opt);
-       else
-               ret = 1;
 
        return ret;
 }
@@ -318,9 +335,8 @@ _bo_unlock(tbm_bo bo)
 static int
 _tbm_bo_lock(tbm_bo bo, int device, int opt)
 {
-       tbm_bufmgr bufmgr = NULL;
-       int old;
-       int ret = 0;
+       tbm_bufmgr bufmgr;
+       int old, ret;
 
        if (!bo)
                return 0;
@@ -334,10 +350,13 @@ _tbm_bo_lock(tbm_bo bo, int device, int opt)
        if (bo->lock_cnt < 0) {
                TBM_LOG_E("error bo:%p LOCK_CNT=%d\n",
                        bo, bo->lock_cnt);
+               return 0;
        }
 
        old = bo->lock_cnt;
-       if (bufmgr->lock_type == LOCK_TRY_ONCE) {
+
+       switch (bufmgr->lock_type) {
+       case LOCK_TRY_ONCE:
                if (bo->lock_cnt == 0) {
                        _tbm_bufmgr_mutex_unlock();
                        ret = _bo_lock(bo, device, opt);
@@ -346,19 +365,22 @@ _tbm_bo_lock(tbm_bo bo, int device, int opt)
                                bo->lock_cnt++;
                } else
                        ret = 1;
-       } else if (bufmgr->lock_type == LOCK_TRY_ALWAYS) {
+               break;
+       case LOCK_TRY_ALWAYS:
                _tbm_bufmgr_mutex_unlock();
                ret = _bo_lock(bo, device, opt);
                _tbm_bufmgr_mutex_lock();
                if (ret)
                        bo->lock_cnt++;
-       } else {
-               TBM_LOG_E("error bo:%p lock_type is wrong.\n",
-                       bo);
+               break;
+       default:
+               TBM_LOG_E("error bo:%p lock_type[%d] is wrong.\n",
+                               bo, bufmgr->lock_type);
+               ret = 0;
+               break;
        }
 
-       TBM_DBG_LOCK(">> LOCK bo:%p(%d->%d)\n",
-                bo, old, bo->lock_cnt);
+       TBM_DBG_LOCK(">> LOCK bo:%p(%d->%d)\n", bo, old, bo->lock_cnt);
 
        return ret;
 }
@@ -366,8 +388,7 @@ _tbm_bo_lock(tbm_bo bo, int device, int opt)
 static void
 _tbm_bo_unlock(tbm_bo bo)
 {
-       tbm_bufmgr bufmgr = NULL;
-
+       tbm_bufmgr bufmgr;
        int old;
 
        if (!bo)
@@ -380,49 +401,55 @@ _tbm_bo_unlock(tbm_bo bo)
                return;
 
        old = bo->lock_cnt;
-       if (bufmgr->lock_type == LOCK_TRY_ONCE) {
+
+       switch (bufmgr->lock_type) {
+       case 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) {
+               break;
+       case 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);
+               break;
+       default:
+               TBM_LOG_E("error bo:%p lock_type[%d] is wrong.\n",
+                               bo, bufmgr->lock_type);
+               break;
        }
 
        if (bo->lock_cnt < 0)
                bo->lock_cnt = 0;
 
-       TBM_DBG_LOCK(">> UNLOCK bo:%p(%d->%d)\n",
-                bo, old, bo->lock_cnt);
+       TBM_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;
+       tbm_bo old_data = NULL;
 
-       if (bo == NULL)
+       if (bo == NULL || gBufMgr == NULL) {
+               TBM_LOG_E("error: bo is NULL or tbm_bufmgr was deinited\n");
                return 0;
+       }
 
-       if (gBufMgr == NULL) {
-               TBM_LOG_E("error tbm_bufmgr was deinited\n");
+       if (LIST_IS_EMPTY(&gBufMgr->bo_list)) {
+               TBM_LOG_E("error: gBufMgr->bo_list is EMPTY.\n");
                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;
-               }
-
+       LIST_FOR_EACH_ENTRY(old_data, &gBufMgr->bo_list, item_link) {
+               if (old_data == bo)
+                       return 1;
        }
+
+       TBM_LOG_E("error: No valid bo(%p).\n", bo);
+
        return 0;
 }
 
@@ -455,108 +482,102 @@ _check_version(TBMModuleVersionInfo *data)
                        abimin, vermin);
                return 0;
        }
+
        return 1;
 }
 
 static int
 _tbm_bufmgr_load_module(tbm_bufmgr bufmgr, int fd, const char *file)
 {
-       char path[PATH_MAX] = { 0, };
-       TBMModuleData *initdata = NULL;
+       char path[PATH_MAX] = {0, };
+       TBMModuleVersionInfo *vers;
+       TBMModuleData *initdata;
+       ModuleInitProc init;
        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);
+               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;
+       if (!initdata) {
+               TBM_LOG_E("Error: module does not have data object.\n");
+               goto err;
+       }
 
-               vers = initdata->vers;
-               init = initdata->init;
+       vers = initdata->vers;
+       if (!vers) {
+               TBM_LOG_E("Error: module does not supply version information.\n");
+               goto err;
+       }
 
-               if (vers) {
-                       if (!_check_version(vers)) {
-                               dlclose(module_data);
-                               return 0;
-                       }
-               } else {
-                       TBM_LOG_E("Error: module does not supply version information.\n");
+       init = initdata->init;
+       if (!init) {
+               TBM_LOG_E("Error: module does not supply init symbol.\n");
+               goto err;
+       }
 
-                       dlclose(module_data);
-                       return 0;
-               }
+       if (!_check_version(vers)) {
+               TBM_LOG_E("Fail to check version.\n");
+               goto err;
+       }
 
-               if (init) {
-                       if (!init(bufmgr, fd)) {
-                               TBM_LOG_E("Fail to init module(%s)\n",
-                                       file);
-                               dlclose(module_data);
-                               return 0;
-                       }
+       if (!init(bufmgr, fd)) {
+               TBM_LOG_E("Fail to init module(%s)\n", file);
+               goto err;
+       }
 
-                       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;
-               }
-       } else {
-               TBM_LOG_E("Error: module does not have data object.\n");
-               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);
+               goto err;
        }
 
        bufmgr->module_data = module_data;
 
-       TBM_DBG("Success to load module(%s)\n",
-           file);
+       TBM_DBG("Success to load module(%s)\n", file);
 
        return 1;
+
+err:
+       dlclose(module_data);
+       return 0;
 }
 
 static int
 _tbm_load_module(tbm_bufmgr bufmgr, int fd)
 {
        struct dirent **namelist;
-       const char *p = NULL;
-       int n;
-       int ret = 0;
+       int ret = 0, n;
 
        /* load bufmgr priv from default lib */
-       ret = _tbm_bufmgr_load_module(bufmgr, fd, DEFAULT_LIB);
+       if (_tbm_bufmgr_load_module(bufmgr, fd, DEFAULT_LIB))
+               return 1;
 
        /* 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);
+       n = scandir(BUFMGR_MODULE_DIR, &namelist, 0, alphasort);
+       if (n < 0) {
+               TBM_LOG_E("no files : %s\n", BUFMGR_MODULE_DIR);
+               return 0;
+       }
+
+       while (n--) {
+               if (!ret && strstr(namelist[n]->d_name, PREFIX_LIB)) {
+                       const char *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 */
@@ -564,13 +585,10 @@ _tbm_load_module(tbm_bufmgr bufmgr, int fd)
 tbm_bufmgr
 tbm_bufmgr_init(int fd)
 {
-       char *env;
-
 #ifdef TBM_BUFMGR_INIT_TIME
        struct timeval start_tv, end_tv;
 #endif
-
-       pthread_mutex_lock(&gLock);
+       char *env;
 
 #ifdef TBM_BUFMGR_INIT_TIME
        /* get the start tv */
@@ -583,9 +601,8 @@ tbm_bufmgr_init(int fd)
        if (env) {
                bDlog = atoi(env);
                TBM_LOG_D("TBM_DLOG=%s\n", env);
-       } else {
+       } else
                bDlog = 1;
-       }
 #endif
 
 #ifdef DEBUG
@@ -593,9 +610,8 @@ tbm_bufmgr_init(int fd)
        if (env) {
                bDebug = atoi(env);
                TBM_LOG_D("TBM_DEBUG=%s\n", env);
-       } else {
+       } else
                bDebug = 0;
-       }
 #endif
 
 #ifdef TRACE
@@ -603,11 +619,17 @@ tbm_bufmgr_init(int fd)
        if (env) {
                bTrace = atoi(env);
                TBM_LOG_D("TBM_TRACE=%s\n", env);
-       } else {
+       } else
                bTrace = 0;
-       }
 #endif
-       /* LCOV_EXCL_STOP */
+
+       pthread_mutex_lock(&gLock);
+
+       if (fd >= 0) {
+               TBM_LOG_W("!!!!!WARNING:: The tbm_bufmgr_init DOSE NOT use argument fd ANYMORE.\n");
+               TBM_LOG_W("!!!!!WARNING:: IT WILL BE CHANGED like tbm_bufmgr_init(int fd) --> tbm_bufmgr_init(void).\n");
+       }
+
 
        /* initialize buffer manager */
        if (gBufMgr) {
@@ -632,15 +654,15 @@ tbm_bufmgr_init(int fd)
 
        /* 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");
                free(gBufMgr);
                gBufMgr = NULL;
                pthread_mutex_unlock(&gLock);
                return NULL;
-               /* LCOV_EXCL_STOP */
+
        }
+       /* LCOV_EXCL_STOP */
 
        /* log for tbm backend_flag */
        TBM_DBG("backend flag:%x:", gBufMgr->backend->flags);
@@ -662,8 +684,7 @@ tbm_bufmgr_init(int fd)
        else
                gBufMgr->lock_type = LOCK_TRY_ALWAYS;
 
-       TBM_DBG("BUFMGR_LOCK_TYPE=%s\n",
-           env ? env : "default:once");
+       TBM_DBG("BUFMGR_LOCK_TYPE=%s\n", env ? env : "default:once");
 
        TBM_TRACE("create tbm_bufmgr(%p) ref_count(%d) fd(%d)\n", gBufMgr, gBufMgr->ref_count, fd);
 
@@ -673,7 +694,7 @@ tbm_bufmgr_init(int fd)
        /* intialize surf_list */
        LIST_INITHEAD(&gBufMgr->surf_list);
 
-       /* intialize surf_list */
+       /* intialize surf_queue_list */
        LIST_INITHEAD(&gBufMgr->surf_queue_list);
 
        /* intialize debug_key_list */
@@ -695,12 +716,6 @@ 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;
-
        pthread_mutex_lock(&gLock);
 
        if (!gBufMgr) {
@@ -718,9 +733,10 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
 
        /* 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);
+                       TBM_LOG_E("Un-freed bo(%p, ref:%d)\n", bo, bo->ref_cnt);
                        bo->ref_cnt = 1;
                        tbm_bo_unref(bo);
                }
@@ -728,9 +744,10 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
 
        /* 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_LOG_E("Un-freed surf(%p, ref:%d)\n", surf, surf->refcnt);
                        tbm_surface_destroy(surf);
                }
        }
@@ -749,7 +766,6 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
                close(bufmgr->fd);
 
        free(bufmgr);
-       bufmgr = NULL;
        gBufMgr = NULL;
 
        pthread_mutex_unlock(&gLock);
@@ -758,7 +774,7 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
 int
 tbm_bo_size(tbm_bo bo)
 {
-       tbm_bufmgr bufmgr = NULL;
+       tbm_bufmgr bufmgr = gBufMgr;
        int size;
 
        _tbm_bufmgr_mutex_lock();
@@ -766,8 +782,6 @@ tbm_bo_size(tbm_bo bo)
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
 
-       bufmgr = gBufMgr;
-
        size = bufmgr->backend->bo_size(bo);
 
        TBM_TRACE("bo(%p) size(%d)\n", bo, size);
@@ -797,16 +811,13 @@ tbm_bo_ref(tbm_bo bo)
 void
 tbm_bo_unref(tbm_bo bo)
 {
-       tbm_bufmgr bufmgr = NULL;
-       tbm_user_data *old_data = NULL, *tmp = NULL;
+       tbm_bufmgr bufmgr = gBufMgr;
 
        _tbm_bufmgr_mutex_lock();
 
        TBM_BUFMGR_RETURN_IF_FAIL(gBufMgr);
        TBM_BUFMGR_RETURN_IF_FAIL(_tbm_bo_is_valid(bo));
 
-       bufmgr = gBufMgr;
-
        TBM_TRACE("bo(%p) ref_cnt(%d)\n", bo, bo->ref_cnt - 1);
 
        if (bo->ref_cnt <= 0) {
@@ -818,15 +829,17 @@ tbm_bo_unref(tbm_bo bo)
        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) {
+                       tbm_user_data *old_data = NULL, *tmp;
+
+                       LIST_FOR_EACH_ENTRY_SAFE(old_data, tmp,
+                                       &bo->user_data_list, item_link) {
                                TBM_DBG("free user_data\n");
                                user_data_delete(old_data);
                        }
                }
 
                while (bo->lock_cnt > 0) {
-                       TBM_LOG_E("error lock_cnt:%d\n",
-                               bo->lock_cnt);
+                       TBM_LOG_E("error lock_cnt:%d\n", bo->lock_cnt);
                        _bo_unlock(bo);
                        bo->lock_cnt--;
                }
@@ -847,8 +860,8 @@ tbm_bo_unref(tbm_bo bo)
 tbm_bo
 tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags)
 {
-       tbm_bo bo = NULL;
-       void *bo_priv = NULL;
+       void *bo_priv;
+       tbm_bo bo;
 
        _tbm_bufmgr_mutex_lock();
 
@@ -858,31 +871,39 @@ tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags)
 
        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));
+               /* LCOV_EXCL_START */
+               TBM_LOG_E("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);
                _tbm_bufmgr_mutex_unlock();
                return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
        _tbm_util_check_bo_cnt(bufmgr);
-       bufmgr->bo_cnt++;
 
        bo->bufmgr = bufmgr;
 
        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));
+               /* LCOV_EXCL_START */
+               TBM_LOG_E("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);
                _tbm_bufmgr_mutex_unlock();
                return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
+       bufmgr->bo_cnt++;
+
        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));
+       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);
 
@@ -896,10 +917,8 @@ tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags)
 tbm_bo
 tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
 {
-       tbm_bo bo = NULL;
-       tbm_bo bo2 = NULL;
-       tbm_bo tmp = NULL;
-       void *bo_priv = NULL;
+       void *bo_priv;
+       tbm_bo bo;
 
        _tbm_bufmgr_mutex_lock();
 
@@ -907,35 +926,44 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(bufmgr == gBufMgr, NULL);
 
        if (!bufmgr->backend->bo_import) {
+               /* LCOV_EXCL_START */
                _tbm_bufmgr_mutex_unlock();
                return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
        _tbm_util_check_bo_cnt(bufmgr);
 
        bo = calloc(1, sizeof(struct _tbm_bo));
        if (!bo) {
-               TBM_TRACE("error: fail to import of tbm_bo by key(%d)\n", key);
+               /* LCOV_EXCL_START */
+               TBM_LOG_E("error: fail to import of tbm_bo by key(%d)\n", key);
                _tbm_bufmgr_mutex_unlock();
                return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
        bo->bufmgr = bufmgr;
 
        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);
+               /* LCOV_EXCL_START */
+               TBM_LOG_E("error: fail to import of tbm_bo by key(%d)\n", key);
                _tbm_set_last_result(TBM_BO_ERROR_IMPORT_FAILED);
                free(bo);
                _tbm_bufmgr_mutex_unlock();
                return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
        if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(bo2, tmp, &bufmgr->bo_list, item_link) {
+               tbm_bo bo2 = NULL;
+
+               LIST_FOR_EACH_ENTRY(bo2, &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, bo2->ref_cnt, key,
+                                               _tbm_flag_to_str(bo2->flags));
                                bo2->ref_cnt++;
                                free(bo);
                                _tbm_bufmgr_mutex_unlock();
@@ -955,7 +983,7 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
                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));
+                         bo, bo->ref_cnt, key, _tbm_flag_to_str(bo->flags));
 
        LIST_INITHEAD(&bo->user_data_list);
 
@@ -969,10 +997,8 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
 tbm_bo
 tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
 {
-       tbm_bo bo = NULL;
-       tbm_bo bo2 = NULL;
-       tbm_bo tmp = NULL;
-       void *bo_priv = NULL;
+       void *bo_priv;
+       tbm_bo bo;
 
        _tbm_bufmgr_mutex_lock();
 
@@ -980,35 +1006,44 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(bufmgr == gBufMgr, NULL);
 
        if (!bufmgr->backend->bo_import_fd) {
+               /* LCOV_EXCL_START */
                _tbm_bufmgr_mutex_unlock();
                return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
        _tbm_util_check_bo_cnt(bufmgr);
 
        bo = calloc(1, sizeof(struct _tbm_bo));
        if (!bo) {
-               TBM_TRACE("error: fail to import tbm_bo by tbm_fd(%d)\n", fd);
+               /* LCOV_EXCL_START */
+               TBM_LOG_E("error: fail to import tbm_bo by tbm_fd(%d)\n", fd);
                _tbm_bufmgr_mutex_unlock();
                return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
        bo->bufmgr = bufmgr;
 
        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);
+               /* LCOV_EXCL_START */
+               TBM_LOG_E("error: fail to import tbm_bo by tbm_fd(%d)\n", fd);
                _tbm_set_last_result(TBM_BO_ERROR_IMPORT_FD_FAILED);
                free(bo);
                _tbm_bufmgr_mutex_unlock();
                return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
        if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(bo2, tmp, &bufmgr->bo_list, item_link) {
+               tbm_bo bo2 = NULL;
+
+               LIST_FOR_EACH_ENTRY(bo2, &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, bo2->ref_cnt, fd,
+                                               _tbm_flag_to_str(bo2->flags));
                                bo2->ref_cnt++;
                                free(bo);
                                _tbm_bufmgr_mutex_unlock();
@@ -1027,8 +1062,8 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
        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));
+       TBM_TRACE("import bo(%p) ref(%d) fd(%d) flag(%s)\n",
+                       bo, bo->ref_cnt, fd, _tbm_flag_to_str(bo->flags));
 
        LIST_INITHEAD(&bo->user_data_list);
 
@@ -1042,7 +1077,7 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
 tbm_key
 tbm_bo_export(tbm_bo bo)
 {
-       tbm_bufmgr bufmgr = NULL;
+       tbm_bufmgr bufmgr = gBufMgr;
        tbm_key ret;
 
        _tbm_bufmgr_mutex_lock();
@@ -1050,22 +1085,24 @@ tbm_bo_export(tbm_bo bo)
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
 
-       bufmgr = gBufMgr;
-
        if (!bufmgr->backend->bo_export) {
+               /* LCOV_EXCL_START */
                _tbm_bufmgr_mutex_unlock();
                return 0;
+               /* LCOV_EXCL_STOP */
        }
 
        ret = bufmgr->backend->bo_export(bo);
        if (!ret) {
+               /* LCOV_EXCL_START */
                _tbm_set_last_result(TBM_BO_ERROR_EXPORT_FAILED);
-               TBM_TRACE("error: bo(%p) tbm_key(%d)\n", bo, ret);
+               TBM_LOG_E("error: bo(%p) tbm_key(%d)\n", bo, ret);
                _tbm_bufmgr_mutex_unlock();
                return ret;
+               /* LCOV_EXCL_STOP */
        }
 
-       TBM_TRACE("bo(%p) tbm_key(%d)\n", bo, ret);
+       TBM_TRACE("bo(%p) tbm_key(%u)\n", bo, ret);
 
        _tbm_bufmgr_mutex_unlock();
 
@@ -1075,27 +1112,29 @@ tbm_bo_export(tbm_bo bo)
 tbm_fd
 tbm_bo_export_fd(tbm_bo bo)
 {
-       tbm_bufmgr bufmgr = NULL;
-       int ret;
+       tbm_bufmgr bufmgr = gBufMgr;
+       tbm_fd ret;
 
        _tbm_bufmgr_mutex_lock();
 
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), -1);
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), -1);
 
-       bufmgr = gBufMgr;
-
        if (!bufmgr->backend->bo_export_fd) {
+               /* LCOV_EXCL_START */
                _tbm_bufmgr_mutex_unlock();
                return -1;
+               /* LCOV_EXCL_STOP */
        }
 
        ret = bufmgr->backend->bo_export_fd(bo);
        if (ret < 0) {
+               /* LCOV_EXCL_START */
                _tbm_set_last_result(TBM_BO_ERROR_EXPORT_FD_FAILED);
-               TBM_TRACE("error: bo(%p) tbm_fd(%d)\n", bo, ret);
+               TBM_LOG_E("error: bo(%p) tbm_fd(%d)\n", bo, ret);
                _tbm_bufmgr_mutex_unlock();
                return ret;
+               /* LCOV_EXCL_STOP */
        }
 
        TBM_TRACE("bo(%p) tbm_fd(%d)\n", bo, ret);
@@ -1108,7 +1147,7 @@ tbm_bo_export_fd(tbm_bo bo)
 tbm_bo_handle
 tbm_bo_get_handle(tbm_bo bo, int device)
 {
-       tbm_bufmgr bufmgr = NULL;
+       tbm_bufmgr bufmgr = gBufMgr;
        tbm_bo_handle bo_handle;
 
        _tbm_bufmgr_mutex_lock();
@@ -1116,14 +1155,14 @@ tbm_bo_get_handle(tbm_bo bo, int device)
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), (tbm_bo_handle) NULL);
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), (tbm_bo_handle) NULL);
 
-       bufmgr = gBufMgr;
-
        bo_handle = bufmgr->backend->bo_get_handle(bo, device);
        if (bo_handle.ptr == NULL) {
+               /* LCOV_EXCL_START */
                _tbm_set_last_result(TBM_BO_ERROR_GET_HANDLE_FAILED);
-               TBM_TRACE("error: bo(%p) bo_handle(%p)\n", bo, bo_handle.ptr);
+               TBM_LOG_E("error: bo(%p) bo_handle(%p)\n", bo, bo_handle.ptr);
                _tbm_bufmgr_mutex_unlock();
                return (tbm_bo_handle) NULL;
+               /* LCOV_EXCL_STOP */
        }
 
        TBM_TRACE("bo(%p) bo_handle(%p)\n", bo, bo_handle.ptr);
@@ -1136,7 +1175,7 @@ tbm_bo_get_handle(tbm_bo bo, int device)
 tbm_bo_handle
 tbm_bo_map(tbm_bo bo, int device, int opt)
 {
-       tbm_bufmgr bufmgr = NULL;
+       tbm_bufmgr bufmgr = gBufMgr;
        tbm_bo_handle bo_handle;
 
        _tbm_bufmgr_mutex_lock();
@@ -1144,22 +1183,22 @@ tbm_bo_map(tbm_bo bo, int device, int opt)
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), (tbm_bo_handle) NULL);
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), (tbm_bo_handle) NULL);
 
-       bufmgr = gBufMgr;
-
        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);
+               TBM_LOG_E("error: fail to lock bo:%p)\n", bo);
                _tbm_bufmgr_mutex_unlock();
                return (tbm_bo_handle) NULL;
        }
 
        bo_handle = bufmgr->backend->bo_map(bo, device, opt);
        if (bo_handle.ptr == NULL) {
+               /* LCOV_EXCL_START */
                _tbm_set_last_result(TBM_BO_ERROR_MAP_FAILED);
-               TBM_TRACE("error: fail to map bo:%p\n", bo);
+               TBM_LOG_E("error: fail to map bo:%p\n", bo);
                _tbm_bo_unlock(bo);
                _tbm_bufmgr_mutex_unlock();
                return (tbm_bo_handle) NULL;
+               /* LCOV_EXCL_STOP */
        }
 
        /* increase the map_count */
@@ -1175,7 +1214,7 @@ tbm_bo_map(tbm_bo bo, int device, int opt)
 int
 tbm_bo_unmap(tbm_bo bo)
 {
-       tbm_bufmgr bufmgr = NULL;
+       tbm_bufmgr bufmgr = gBufMgr;
        int ret;
 
        _tbm_bufmgr_mutex_lock();
@@ -1183,14 +1222,14 @@ tbm_bo_unmap(tbm_bo bo)
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
 
-       bufmgr = gBufMgr;
-
        ret = bufmgr->backend->bo_unmap(bo);
        if (!ret) {
-               TBM_TRACE("error: bo(%p) map_cnt(%d)\n", bo, bo->map_cnt);
+               /* LCOV_EXCL_START */
+               TBM_LOG_E("error: bo(%p) map_cnt(%d)\n", bo, bo->map_cnt);
                _tbm_set_last_result(TBM_BO_ERROR_UNMAP_FAILED);
                _tbm_bufmgr_mutex_unlock();
                return ret;
+               /* LCOV_EXCL_STOP */
        }
 
        /* decrease the map_count */
@@ -1208,7 +1247,7 @@ tbm_bo_unmap(tbm_bo bo)
 int
 tbm_bo_swap(tbm_bo bo1, tbm_bo bo2)
 {
-       tbm_bufmgr bufmgr = NULL;
+       tbm_bufmgr bufmgr = gBufMgr;
        void *temp;
 
        _tbm_bufmgr_mutex_lock();
@@ -1217,13 +1256,11 @@ tbm_bo_swap(tbm_bo bo1, tbm_bo bo2)
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo1), 0);
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo2), 0);
 
-       bufmgr = gBufMgr;
-
        TBM_TRACE("before: bo1(%p) bo2(%p)\n", bo1, bo2);
 
        if (bufmgr->backend->bo_size(bo1) != bufmgr->backend->bo_size(bo2)) {
                _tbm_set_last_result(TBM_BO_ERROR_SWAP_FAILED);
-               TBM_TRACE("error: bo1(%p) bo2(%p)\n", bo1, bo2);
+               TBM_LOG_E("error: bo1(%p) bo2(%p)\n", bo1, bo2);
                _tbm_bufmgr_mutex_unlock();
                return 0;
        }
@@ -1242,17 +1279,15 @@ tbm_bo_swap(tbm_bo bo1, tbm_bo bo2)
 int
 tbm_bo_locked(tbm_bo bo)
 {
-       tbm_bufmgr bufmgr = NULL;
+       tbm_bufmgr bufmgr = gBufMgr;
 
        _tbm_bufmgr_mutex_lock();
 
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
 
-       bufmgr = gBufMgr;
-
        if (bufmgr->lock_type == LOCK_TRY_NEVER) {
-               TBM_TRACE("bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
+               TBM_LOG_E("bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt);
                _tbm_bufmgr_mutex_unlock();
                return 0;
        }
@@ -1290,7 +1325,7 @@ tbm_bo_add_user_data(tbm_bo bo, unsigned long key,
 
        data = user_data_create(key, data_free_func);
        if (!data) {
-               TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key);
+               TBM_LOG_E("error: bo(%p) key(%lu)\n", bo, key);
                _tbm_bufmgr_mutex_unlock();
                return 0;
        }
@@ -1329,7 +1364,6 @@ tbm_bo_set_user_data(tbm_bo bo, unsigned long key, void *data)
 
        if (old_data->data && old_data->free_func)
                old_data->free_func(old_data->data);
-
        old_data->data = data;
 
        TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data);
@@ -1375,7 +1409,7 @@ tbm_bo_get_user_data(tbm_bo bo, unsigned long key, void **data)
 int
 tbm_bo_delete_user_data(tbm_bo bo, unsigned long key)
 {
-       tbm_user_data *old_data = (void *)0;
+       tbm_user_data *old_data;
 
        _tbm_bufmgr_mutex_lock();
 
@@ -1407,14 +1441,14 @@ tbm_bo_delete_user_data(tbm_bo bo, unsigned long key)
 unsigned int
 tbm_bufmgr_get_capability(tbm_bufmgr bufmgr)
 {
-       int capabilities = TBM_BUFMGR_CAPABILITY_NONE;
+       unsigned int capabilities = TBM_BUFMGR_CAPABILITY_NONE;
 
        _tbm_bufmgr_mutex_lock();
 
        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("tbm_bufmgr(%p) capability(%d)\n", bufmgr, bufmgr->capabilities);
+       TBM_TRACE("tbm_bufmgr(%p) capability(%u)\n", bufmgr, bufmgr->capabilities);
 
        capabilities = bufmgr->capabilities;
 
@@ -1450,18 +1484,10 @@ tbm_get_last_error(void)
 }
 
 void
-tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
+tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr, char *str, int *len)
 {
-       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;
+       char app_name[255] = {0,}, title[512] = {0,};
+       tbm_surface_debug_data *debug_old_data = NULL;
 
        pthread_mutex_lock(&gLock);
 
@@ -1471,37 +1497,45 @@ tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
                return;
        }
 
-       TBM_DEBUG("\n");
+       TBM_SNPRINTF(str, len, "\n");
        _tbm_util_get_appname_from_pid(getpid(), app_name);
        _tbm_util_get_appname_brief(app_name);
-       TBM_DEBUG("============TBM DEBUG: %s(%d)===========================\n",
+       TBM_SNPRINTF(str, len, "============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) {
+               LIST_FOR_EACH_ENTRY(debug_old_data, &bufmgr->debug_key_list, item_link) {
                        strncat(title, "  ", 3);
                        strncat(title, debug_old_data->key, strlen(debug_old_data->key) + 1);
                }
        }
 
-       TBM_DEBUG("[tbm_surface information]\n");
-       TBM_DEBUG("%s\n", title);
+       TBM_SNPRINTF(str, len, "[tbm_surface information]\n");
+       TBM_SNPRINTF(str, len, "%s\n", title);
+
        /* show the tbm_surface information in surf_list */
        if (!LIST_IS_EMPTY(&bufmgr->surf_list)) {
-               char *value = NULL;
+               tbm_surface_h surf = NULL;
+               int surf_cnt = 0;
+
+               LIST_FOR_EACH_ENTRY(surf, &bufmgr->surf_list, item_link) {
+                       char data[512] = {0,};
+                       unsigned int pid;
+                       int i;
 
-               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();
                        }
 
+                       memset(app_name, 0x0, 255 * sizeof(char));
                        _tbm_util_get_appname_from_pid(pid, app_name);
                        _tbm_util_get_appname_brief(app_name);
 
-                       snprintf(data, 255, "%-2d  %-9p    %-4d  %-5d  %-6d  %-3d  %-6d   %-2d   %-2d    %-3d  %-8s  %-15s",
+                       snprintf(data, 255, "%-2d  %-9p    %-4d  %-5u  %-6u  %-3u  %-6u   %-2d   %-2d    %-3d  %-8s  %-15s",
                                  ++surf_cnt,
                                  surf,
                                  surf->refcnt,
@@ -1516,7 +1550,9 @@ tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
                                  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) {
+                               LIST_FOR_EACH_ENTRY(debug_old_data, &bufmgr->debug_key_list, item_link) {
+                                       char *value;
+
                                        strncat(data, "  ", 3);
 
                                        value = _tbm_surface_internal_get_debug_data(surf, debug_old_data->key);
@@ -1526,29 +1562,29 @@ tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
                                                strncat(data, "none", 5);
                                }
                        }
-                       TBM_DEBUG("%s\n", data);
+                       TBM_SNPRINTF(str, len, "%s\n", data);
 
                        for (i = 0; i < surf->num_bos; i++) {
-                               TBM_DEBUG(" bo:%-12p  %-26d%-10d\n",
+                               TBM_SNPRINTF(str, len, " bo:%-12p  %-26d%-10d\n",
                                          surf->bos[i],
                                          surf->bos[i]->ref_cnt,
                                          bufmgr->backend->bo_size(surf->bos[i]) / 1024);
                        }
-
-                       memset(app_name, 0x0, 255 * sizeof(char));
                }
-       } else {
-               TBM_DEBUG("no tbm_surfaces.\n");
-       }
-       TBM_DEBUG("\n");
+       } else
+               TBM_SNPRINTF(str, len, " no tbm_surfaces.\n");
+       TBM_SNPRINTF(str, len, "\n");
 
-       TBM_DEBUG("[tbm_bo information]\n");
-       TBM_DEBUG("no  bo          refcnt  size    lock_cnt  map_cnt  flags  surface\n");
+       TBM_SNPRINTF(str, len, "[tbm_bo information]\n");
+       TBM_SNPRINTF(str, len, "no  bo          refcnt  size    lock_cnt  map_cnt  flags  surface\n");
 
        /* 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",
+               int bo_cnt = 0;
+               tbm_bo bo = NULL;
+
+               LIST_FOR_EACH_ENTRY(bo, &bufmgr->bo_list, item_link) {
+                       TBM_SNPRINTF(str, len, "%-4d%-11p   %-4d  %-6d     %-5d     %-4u    %-3d  %-11p\n",
                                  ++bo_cnt,
                                  bo,
                                  bo->ref_cnt,
@@ -1558,17 +1594,25 @@ tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
                                  bo->flags,
                                  bo->surface);
                }
-       } else {
-               TBM_DEBUG("no tbm_bos.\n");
-       }
-       TBM_DEBUG("\n");
+       } else
+               TBM_SNPRINTF(str, len, "no tbm_bos.\n");
+       TBM_SNPRINTF(str, len, "\n");
 
-       TBM_DEBUG("===============================================================\n");
+       TBM_SNPRINTF(str, len, "===============================================================\n");
 
        pthread_mutex_unlock(&gLock);
 }
 
 void
+tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
+{
+       char str[1024*4];
+       int len = sizeof(str);
+       tbm_bufmgr_debug_tbm_info_get(bufmgr, str, &len);
+       TBM_DEBUG("     %s", str);
+}
+
+void
 tbm_bufmgr_debug_trace(tbm_bufmgr bufmgr, int onoff)
 {
        _tbm_bufmgr_mutex_lock();
@@ -1584,31 +1628,43 @@ tbm_bufmgr_debug_trace(tbm_bufmgr bufmgr, int onoff)
        _tbm_bufmgr_mutex_unlock();
 }
 
+void
+tbm_bufmgr_debug_dump_set_scale(double scale)
+{
+       pthread_mutex_lock(&gLock);
+       scale_factor = scale;
+       pthread_mutex_unlock(&gLock);
+}
+
 int
 tbm_bufmgr_debug_queue_dump(char *path, int count, int onoff)
 {
-       int w = 0, h = 0;
+       pthread_mutex_lock(&gLock);
 
-       TBM_RETURN_VAL_IF_FAIL(path != NULL, 0);
-       TBM_LOG_D("path=%s count=%d onoff=%d\n", path, count, onoff);
+       if (onoff == 0) {
+               TBM_LOG_D("count=%d onoff=%d\n", count, onoff);
+               b_dump_queue = 0;
+               tbm_surface_internal_dump_end();
+       } else {
+               int w, h;
 
-       pthread_mutex_lock(&gLock);
+               if (path == NULL) {
+                       TBM_LOG_E("path is null");
+                       pthread_mutex_unlock(&gLock);
+                       return 0;
+               }
+               TBM_LOG_D("path=%s count=%d onoff=%d\n", path, count, onoff);
 
-       if (onoff == 1) {
                if (_tbm_util_get_max_surface_size(&w, &h) == 0) {
-                       TBM_LOG_I("No tbm_surface.\n");
+                       TBM_LOG_E("Fail to get tbm_surface size.\n");
                        pthread_mutex_unlock(&gLock);
                        return 0;
                }
 
-               tbm_surface_internal_dump_start(path, w, h, count);
+               tbm_surface_internal_dump_with_scale_start(path, w, h, count, scale_factor);
+               scale_factor = 0;
+
                b_dump_queue = 1;
-       } else if (onoff == 0) {
-               tbm_surface_internal_dump_end();
-               b_dump_queue = 0;
-       } else {
-               pthread_mutex_unlock(&gLock);
-               return 0;
        }
 
        pthread_mutex_unlock(&gLock);
@@ -1618,8 +1674,8 @@ tbm_bufmgr_debug_queue_dump(char *path, int count, int onoff)
 int
 tbm_bufmgr_debug_dump_all(char *path)
 {
-       int w = 0, h = 0, count = 0;
-       tbm_surface_h surface = NULL, tmp = NULL;
+       int w, h, count = 0;
+       tbm_surface_h surface = NULL;
 
        TBM_RETURN_VAL_IF_FAIL(path != NULL, 0);
        TBM_LOG_D("path=%s\n", path);
@@ -1628,16 +1684,16 @@ tbm_bufmgr_debug_dump_all(char *path)
 
        count = _tbm_util_get_max_surface_size(&w, &h);
        if (count == 0) {
-               TBM_LOG_I("No tbm_surface.\n");
+               TBM_LOG_E("No tbm_surface.\n");
                pthread_mutex_unlock(&gLock);
                return 1;
        }
 
-       tbm_surface_internal_dump_start(path, w, h, count);
+       tbm_surface_internal_dump_with_scale_start(path, w, h, count, scale_factor);
+       scale_factor = 0;
 
-       LIST_FOR_EACH_ENTRY_SAFE(surface, tmp, &gBufMgr->surf_list, item_link) {
+       LIST_FOR_EACH_ENTRY(surface, &gBufMgr->surf_list, item_link)
                tbm_surface_internal_dump_buffer(surface, "dump_all");
-       }
 
        tbm_surface_internal_dump_end();
 
@@ -1678,14 +1734,16 @@ tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *NativeDisplay)
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
 
        if (!bufmgr->backend->bufmgr_bind_native_display) {
-               TBM_TRACE("error: tbm_bufmgr(%p) NativeDisplay(%p)\n", bufmgr, NativeDisplay);
+               TBM_TRACE("skip: tbm_bufmgr(%p) NativeDisplay(%p)\n",
+                               bufmgr, NativeDisplay);
                _tbm_bufmgr_mutex_unlock();
                return 1;
        }
 
        ret = bufmgr->backend->bufmgr_bind_native_display(bufmgr, NativeDisplay);
        if (!ret) {
-               TBM_TRACE("error: tbm_bufmgr(%p) NativeDisplay(%p)\n", bufmgr, NativeDisplay);
+               TBM_LOG_E("error: tbm_bufmgr(%p) NativeDisplay(%p)\n",
+                               bufmgr, NativeDisplay);
                _tbm_bufmgr_mutex_unlock();
                return 0;
        }
@@ -1696,4 +1754,14 @@ tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *NativeDisplay)
 
        return 1;
 }
+
+int tbm_bufmgr_get_fd_limit(void)
+{
+       struct rlimit lim;
+
+       if (getrlimit(RLIMIT_NOFILE, &lim))
+               return 1024;
+
+       return (int)lim.rlim_cur;
+}
 /* LCOV_EXCL_STOP */