Apply tizen coding rule
[platform/core/uifw/libtbm.git] / src / tbm_bufmgr.c
index 981d50e..a060584 100644 (file)
@@ -36,14 +36,12 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "tbm_bufmgr_backend.h"
 #include "list.h"
 
-#define DEBUG
 #ifdef DEBUG
 int bDebug;
-#define DBG(...) {if (bDebug&0x1) TBM_LOG(__VA_ARGS__);}
-#define DBG_LOCK(...) {if (bDebug&0x2) TBM_LOG(__VA_ARGS__);}
-#else
-#define DBG(...)
-#define DBG_LOCK(...)
+#endif
+
+#ifdef HAVE_DLOG
+int bDlog;
 #endif
 
 #define PREFIX_LIB    "libtbm_"
@@ -79,6 +77,7 @@ _tbm_set_last_result(tbm_error_e err)
        tbm_last_error = err;
 }
 
+/* LCOV_EXCL_START */
 static void
 _tbm_util_get_appname_brief(char *brief)
 {
@@ -130,6 +129,7 @@ _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)
@@ -182,11 +182,10 @@ _bo_lock(tbm_bo bo, int device, int opt)
        tbm_bufmgr bufmgr = bo->bufmgr;
        int ret = 0;
 
-       if (bufmgr->backend->bo_lock) {
+       if (bufmgr->backend->bo_lock)
                ret = bufmgr->backend->bo_lock(bo, device, opt);
-       } else {
+       else
                ret = 1;
-       }
 
        return ret;
 }
@@ -196,9 +195,8 @@ _bo_unlock(tbm_bo bo)
 {
        tbm_bufmgr bufmgr = bo->bufmgr;
 
-       if (bufmgr->backend->bo_unlock) {
+       if (bufmgr->backend->bo_unlock)
                bufmgr->backend->bo_unlock(bo);
-       }
 }
 
 static int
@@ -218,9 +216,8 @@ _tbm_bo_lock(tbm_bo bo, int device, int opt)
                return 1;
 
        if (bo->lock_cnt < 0) {
-               TBM_LOG("[libtbm:%d] "
-                       "error %s:%d bo:%p LOCK_CNT=%d\n",
-                       getpid(), __func__, __LINE__, bo, bo->lock_cnt);
+               TBM_LOG_E("error bo:%p LOCK_CNT=%d\n",
+                       bo, bo->lock_cnt);
        }
 
        old = bo->lock_cnt;
@@ -240,12 +237,11 @@ _tbm_bo_lock(tbm_bo bo, int device, int opt)
                if (ret)
                        bo->lock_cnt++;
        } else {
-               TBM_LOG("[libtbm:%d] "
-                       "error %s:%d bo:%p lock_type is wrong.\n",
-                       getpid(), __func__, __LINE__, bo);
+               TBM_LOG_E("error bo:%p lock_type is wrong.\n",
+                       bo);
        }
 
-       DBG_LOCK("[libtbm:%d] >> LOCK bo:%p(%d->%d)\n", getpid(),
+       DBG_LOCK(">> LOCK bo:%p(%d->%d)\n",
                 bo, old, bo->lock_cnt);
 
        return ret;
@@ -280,15 +276,14 @@ _tbm_bo_unlock(tbm_bo bo)
                        _bo_unlock(bo);
                }
        } else {
-               TBM_LOG("[libtbm:%d] "
-                       "error %s:%d bo:%p lock_type is wrong.\n",
-                       getpid(), __func__, __LINE__, bo);
+               TBM_LOG_E("error bo:%p lock_type is wrong.\n",
+                       bo);
        }
 
        if (bo->lock_cnt < 0)
                bo->lock_cnt = 0;
 
-       DBG_LOCK("[libtbm:%d] << unlock bo:%p(%d->%d)\n", getpid(),
+       DBG_LOCK(">> UNLOCK bo:%p(%d->%d)\n",
                 bo, old, bo->lock_cnt);
 }
 
@@ -330,16 +325,14 @@ _tbm_bo_unref(tbm_bo bo)
                /* 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("[libtbm:%d] free user_data\n",
-                                   getpid());
+                               DBG("free user_data\n");
                                user_data_delete(old_data);
                        }
                }
 
                if (bo->lock_cnt > 0) {
-                       TBM_LOG("[libtbm:%d] "
-                               "error %s:%d lock_cnt:%d\n",
-                               getpid(), __func__, __LINE__, bo->lock_cnt);
+                       TBM_LOG_E("error lock_cnt:%d\n",
+                               bo->lock_cnt);
                        _bo_unlock(bo);
                }
 
@@ -354,6 +347,7 @@ _tbm_bo_unref(tbm_bo bo)
 
 }
 
+/* LCOV_EXCL_START */
 static int
 _check_version(TBMModuleVersionInfo *data)
 {
@@ -363,27 +357,23 @@ _check_version(TBMModuleVersionInfo *data)
        abimaj = GET_ABI_MAJOR(data->abiversion);
        abimin = GET_ABI_MINOR(data->abiversion);
 
-       DBG("[libtbm:%d] "
-           "TBM module %s: vendor=\"%s\" ABI=%d,%d\n",
-           getpid(), data->modname ? data->modname : "UNKNOWN!",
+       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("[libtbm:%d] "
-           "TBM ABI version %d.%d\n",
-           getpid(), vermaj, vermin);
+       DBG("TBM ABI version %d.%d\n",
+           vermaj, vermin);
 
        if (abimaj != vermaj) {
-               TBM_LOG("[libtbm:%d] "
-                       "TBM module ABI major ver(%d) doesn't match the TBM's ver(%d)\n",
-                       getpid(), 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("[libtbm:%d] "
-                       "TBM module ABI minor ver(%d) is newer than the TBM's ver(%d)\n",
-                       getpid(), 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;
@@ -400,9 +390,8 @@ _tbm_bufmgr_load_module(tbm_bufmgr bufmgr, int fd, const char *file)
 
        module_data = dlopen(path, RTLD_LAZY);
        if (!module_data) {
-               TBM_LOG("[libtbm:%d] "
-                       "failed to load module: %s(%s)\n",
-                       getpid(), dlerror(), file);
+               TBM_LOG_E("failed to load module: %s(%s)\n",
+                       dlerror(), file);
                return 0;
        }
 
@@ -420,9 +409,7 @@ _tbm_bufmgr_load_module(tbm_bufmgr bufmgr, int fd, const char *file)
                                return 0;
                        }
                } else {
-                       TBM_LOG("[libtbm:%d] "
-                               "Error: module does not supply version information.\n",
-                               getpid());
+                       TBM_LOG_E("Error: module does not supply version information.\n");
 
                        dlclose(module_data);
                        return 0;
@@ -430,40 +417,33 @@ _tbm_bufmgr_load_module(tbm_bufmgr bufmgr, int fd, const char *file)
 
                if (init) {
                        if (!init(bufmgr, fd)) {
-                               TBM_LOG("[libtbm:%d] "
-                                       "Fail to init module(%s)\n",
-                                       getpid(), file);
+                               TBM_LOG_E("Fail to init module(%s)\n",
+                                       file);
                                dlclose(module_data);
                                return 0;
                        }
 
                        if (!bufmgr->backend || !bufmgr->backend->priv) {
-                               TBM_LOG("[libtbm:%d] "
-                                       "Error: module(%s) wrong operation. Check backend or backend's priv.\n",
-                                       getpid(), file);
+                               TBM_LOG_E("Error: module(%s) wrong operation. Check backend or backend's priv.\n",
+                                       file);
                                dlclose(module_data);
                                return 0;
                        }
                } else {
-                       TBM_LOG("[libtbm:%d] "
-                               "Error: module does not supply init symbol.\n",
-                               getpid());
+                       TBM_LOG_E("Error: module does not supply init symbol.\n");
                        dlclose(module_data);
                        return 0;
                }
        } else {
-               TBM_LOG("[libtbm:%d] "
-                       "Error: module does not have data object.\n",
-                       getpid());
+               TBM_LOG_E("Error: module does not have data object.\n");
                dlclose(module_data);
                return 0;
        }
 
        bufmgr->module_data = module_data;
 
-       DBG("[libtbm:%d] "
-           "Success to load module(%s)\n",
-           getpid(), file);
+       DBG("Success to load module(%s)\n",
+           file);
 
        return 1;
 }
@@ -483,9 +463,8 @@ _tbm_load_module(tbm_bufmgr bufmgr, int fd)
        if (!ret) {
                n = scandir(BUFMGR_MODULE_DIR, &namelist, 0, alphasort);
                if (n < 0) {
-                       TBM_LOG("[libtbm:%d] "
-                               "no files : %s\n",
-                               getpid(), BUFMGR_MODULE_DIR);
+                       TBM_LOG_E("no files : %s\n",
+                               BUFMGR_MODULE_DIR);
                } else {
                        while (n--) {
                                if (!ret && strstr(namelist[n]->d_name, PREFIX_LIB)) {
@@ -501,6 +480,7 @@ _tbm_load_module(tbm_bufmgr bufmgr, int fd)
 
        return ret;
 }
+/* LCOV_EXCL_STOP */
 
 tbm_bufmgr
 tbm_bufmgr_init(int fd)
@@ -509,27 +489,39 @@ tbm_bufmgr_init(int fd)
 
        pthread_mutex_lock(&gLock);
 
+       /* 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 = 0;
+       }
+#endif
+
 #ifdef DEBUG
-       env = getenv("GEM_DEBUG");
+       env = getenv("TBM_DEBUG");
        if (env) {
                bDebug = atoi(env);
-               TBM_LOG("GEM_DEBUG=%s\n", env);
+               TBM_LOG_D("TBM_DEBUG=%s\n", env);
        } else {
                bDebug = 0;
        }
 #endif
+       /* LCOV_EXCL_STOP */
 
        /* initialize buffer manager */
        if (gBufMgr) {
                gBufMgr->ref_count++;
 
-               DBG("[libtbm:%d] bufmgr ref: fd=%d, ref_count:%d\n",
-                   getpid(), gBufMgr->fd, gBufMgr->ref_count);
+               DBG("bufmgr:%p ref: fd=%d, ref_count:%d\n",
+                   gBufMgr, gBufMgr->fd, gBufMgr->ref_count);
                pthread_mutex_unlock(&gLock);
                return gBufMgr;
        }
 
-       DBG("[libtbm:%d] bufmgr init\n", getpid());
+       DBG("bufmgr init\n");
 
        /* allocate bufmgr */
        gBufMgr = calloc(1, sizeof(struct _tbm_bufmgr));
@@ -543,26 +535,28 @@ 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("[libtbm:%d] error : Fail to load bufmgr backend\n", getpid());
+               TBM_LOG_E("error : Fail to load bufmgr backend\n");
 
                free(gBufMgr);
                gBufMgr = NULL;
                pthread_mutex_unlock(&gLock);
                return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
        /* log for tbm backend_flag */
-       DBG("[libtbm:%d] ", getpid());
        DBG("backend flag:%x:", gBufMgr->backend->flags);
        DBG("\n");
 
        gBufMgr->ref_count = 1;
 
-       DBG("[libtbm:%d] create tizen bufmgr: ref_count:%d\n",
-           getpid(), gBufMgr->ref_count);
+       DBG("create tizen bufmgr:%p ref_count:%d\n",
+           gBufMgr, gBufMgr->ref_count);
 
        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);
@@ -571,6 +565,7 @@ tbm_bufmgr_init(int fd)
                gBufMgr = NULL;
                pthread_mutex_unlock(&gLock);
                return NULL;
+               /* LCOV_EXCL_STOP */
        }
 
        /* setup the lock_type */
@@ -584,8 +579,8 @@ tbm_bufmgr_init(int fd)
        else
                gBufMgr->lock_type = LOCK_TRY_ALWAYS;
 
-       DBG("[libtbm:%d] BUFMGR_LOCK_TYPE=%s\n",
-           getpid(), env ? env : "default:once");
+       DBG("BUFMGR_LOCK_TYPE=%s\n",
+           env ? env : "default:once");
 
        /* intialize bo_list */
        LIST_INITHEAD(&gBufMgr->bo_list);
@@ -610,11 +605,16 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
 
        pthread_mutex_lock(&gLock);
 
+       if (!gBufMgr) {
+               TBM_LOG_E("gBufmgr already destroy: bufmgr:%p\n", bufmgr);
+               pthread_mutex_unlock(&gLock);
+               return;
+       }
+
        bufmgr->ref_count--;
        if (bufmgr->ref_count > 0) {
-               TBM_LOG("[libtbm:%d] "
-                       "tizen bufmgr destroy: bufmgr:%p, ref_count:%d\n",
-                       getpid(), bufmgr, bufmgr->ref_count);
+               DBG("tizen bufmgr destroy: bufmgr:%p, ref_count:%d\n",
+                       bufmgr, bufmgr->ref_count);
                pthread_mutex_unlock(&gLock);
                return;
        }
@@ -622,9 +622,8 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
        /* destroy bo_list */
        if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
                LIST_FOR_EACH_ENTRY_SAFE(bo, tmp, &bufmgr->bo_list, item_link) {
-                       TBM_LOG("[libtbm:%d] "
-                               "Un-freed bo(%p, ref:%d)\n",
-                               getpid(), 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);
                }
@@ -633,9 +632,8 @@ 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("[libtbm:%d] "
-                               "Destroy surf(%p)\n",
-                               getpid(), surf);
+                       TBM_LOG_E("Un-freed surf(%p, ref:%d)\n",
+                               surf, surf->refcnt);
                        tbm_surface_destroy(surf);
                }
        }
@@ -648,9 +646,8 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
 
        pthread_mutex_destroy(&bufmgr->lock);
 
-       DBG("[libtbm:%d] "
-           "tizen bufmgr destroy: bufmgr:%p\n",
-           getpid(), bufmgr);
+       DBG("tizen bufmgr destroy: bufmgr:%p\n",
+           bufmgr);
 
        dlclose(bufmgr->module_data);
 
@@ -781,9 +778,8 @@ tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key)
        if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
                LIST_FOR_EACH_ENTRY_SAFE(bo2, tmp, &bufmgr->bo_list, item_link) {
                        if (bo2->priv == bo_priv) {
-                               DBG("[libtbm:%d] "
-                                   "find bo(%p, ref:%d key:%d) in list\n",
-                                   getpid(), bo2, bo2->ref_cnt, key);
+                               DBG("find bo(%p, ref:%d key:%d) in list\n",
+                                   bo2, bo2->ref_cnt, key);
 
                                bo2->ref_cnt++;
                                free(bo);
@@ -841,9 +837,8 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd)
        if (!LIST_IS_EMPTY(&bufmgr->bo_list)) {
                LIST_FOR_EACH_ENTRY_SAFE(bo2, tmp, &bufmgr->bo_list, item_link) {
                        if (bo2->priv == bo_priv) {
-                               DBG("[libtbm:%d] "
-                                   "find bo(%p, ref:%d, fd:%d) in list\n",
-                                   getpid(), bo2, bo2->ref_cnt, fd);
+                               DBG("find bo(%p, ref:%d, fd:%d) in list\n",
+                                   bo2, bo2->ref_cnt, fd);
 
                                bo2->ref_cnt++;
                                free(bo);
@@ -950,9 +945,8 @@ tbm_bo_map(tbm_bo bo, int device, int opt)
 
        if (!_tbm_bo_lock(bo, device, opt)) {
                _tbm_set_last_result(TBM_BO_ERROR_LOCK_FAILED);
-               TBM_LOG("[libtbm:%d] "
-                       "error %s:%d fail to lock bo:%p)\n",
-                       getpid(), __func__, __LINE__, bo);
+               TBM_LOG_E("error fail to lock bo:%p)\n",
+                       bo);
                pthread_mutex_unlock(&bufmgr->lock);
                return (tbm_bo_handle) NULL;
        }
@@ -960,9 +954,8 @@ tbm_bo_map(tbm_bo bo, int device, int opt)
        bo_handle = bufmgr->backend->bo_map(bo, device, opt);
        if (bo_handle.ptr == NULL) {
                _tbm_set_last_result(TBM_BO_ERROR_MAP_FAILED);
-               TBM_LOG("[libtbm:%d] "
-                       "error %s:%d fail to map bo:%p\n",
-                       getpid(), __func__, __LINE__, bo);
+               TBM_LOG_E("error fail to map bo:%p\n",
+                       bo);
 
                _tbm_bo_unlock(bo);
                pthread_mutex_unlock(&bufmgr->lock);
@@ -1067,9 +1060,8 @@ tbm_bo_add_user_data(tbm_bo bo, unsigned long key,
        /* 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_LOG("[libtbm:%d] "
-                       "waring: %s:%d user data already exist. key:%ld\n",
-                       getpid(), __func__, __LINE__, key);
+               TBM_LOG_W("waring user data already exist. key:%ld\n",
+                       key);
                return 0;
        }
 
@@ -1144,12 +1136,6 @@ tbm_bo_delete_user_data(tbm_bo bo, unsigned long key)
        return 1;
 }
 
-tbm_error_e
-tbm_get_last_error(void)
-{
-       return tbm_last_error;
-}
-
 unsigned int
 tbm_bufmgr_get_capability(tbm_bufmgr bufmgr)
 {
@@ -1174,6 +1160,13 @@ tbm_bo_get_flags(tbm_bo 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)
 {
@@ -1268,8 +1261,8 @@ tbm_bufmgr_debug_show(tbm_bufmgr bufmgr)
 void
 tbm_bufmgr_debug_trace(tbm_bufmgr bufmgr, int onoff)
 {
-       TBM_LOG("bufmgr=%p onoff=%d\n", bufmgr, onoff);
-       TBM_LOG("Not implemented yet.\n");
+       TBM_LOG_D("bufmgr=%p onoff=%d\n", bufmgr, onoff);
+       TBM_LOG_D("Not implemented yet.\n");
 }
 
 /* internal function */
@@ -1307,4 +1300,4 @@ tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *NativeDisplay)
 
        return 1;
 }
-
+/* LCOV_EXCL_STOP */