remove getenv() function.
[platform/core/uifw/libtbm.git] / src / tbm_bufmgr.c
index d8978e6..d80f7ec 100644 (file)
@@ -50,7 +50,7 @@ int b_dump_queue;
 static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t tbm_bufmgr_lock = PTHREAD_MUTEX_INITIALIZER;
 static double scale_factor = 0;
-static void _tbm_bufmgr_mutex_unlock(void);
+void _tbm_bufmgr_mutex_unlock(void);
 
 //#define TBM_BUFMGR_INIT_TIME
 
@@ -92,36 +92,14 @@ static void _tbm_bufmgr_mutex_unlock(void);
 }
 
 /* LCOV_EXCL_START */
-static bool
-_tbm_bufmgr_mutex_init(void)
-{
-       static bool tbm_bufmgr_mutex_init = false;
-
-       if (tbm_bufmgr_mutex_init)
-               return true;
-
-       if (pthread_mutex_init(&tbm_bufmgr_lock, NULL)) {
-               TBM_ERR("fail: Cannot pthread_mutex_init for tbm_bufmgr_lock.\n");
-               return false;
-       }
-
-       tbm_bufmgr_mutex_init = true;
 
-       return true;
-}
-
-static void
+void
 _tbm_bufmgr_mutex_lock(void)
 {
-       if (!_tbm_bufmgr_mutex_init()) {
-               TBM_ERR("fail: _tbm_bufmgr_mutex_init()\n");
-               return;
-       }
-
        pthread_mutex_lock(&tbm_bufmgr_lock);
 }
 
-static void
+void
 _tbm_bufmgr_mutex_unlock(void)
 {
        pthread_mutex_unlock(&tbm_bufmgr_lock);
@@ -179,6 +157,8 @@ _tbm_util_get_appname_from_pid(long pid, char *str)
        FILE *fp;
        int len;
 
+       if (pid <= 0) return;
+
        snprintf(fn_cmdline, sizeof(fn_cmdline), "/proc/%ld/cmdline", pid);
 
        fp = fopen(fn_cmdline, "r");
@@ -207,29 +187,29 @@ _tbm_util_get_appname_from_pid(long pid, char *str)
 static int
 _check_version(TBMModuleVersionInfo *data)
 {
-       int abimaj, abimin;
-       int vermaj, vermin;
+       int backend_module_major, backend_module_minor;
+       int tbm_backend_major, tbm_backend_minor;
 
-       abimaj = GET_ABI_MAJOR(data->abiversion);
-       abimin = GET_ABI_MINOR(data->abiversion);
+       backend_module_major = GET_ABI_MAJOR(data->abiversion);
+       backend_module_minor = GET_ABI_MINOR(data->abiversion);
 
        TBM_DBG("TBM module %s: vendor=\"%s\" ABI=%d,%d\n",
            data->modname ? data->modname : "UNKNOWN!",
-           data->vendor ? data->vendor : "UNKNOWN!", abimaj, abimin);
+           data->vendor ? data->vendor : "UNKNOWN!", backend_module_major, backend_module_minor);
 
-       vermaj = GET_ABI_MAJOR(TBM_ABI_VERSION);
-       vermin = GET_ABI_MINOR(TBM_ABI_VERSION);
+       tbm_backend_major = GET_ABI_MAJOR(TBM_ABI_VERSION);
+       tbm_backend_minor = GET_ABI_MINOR(TBM_ABI_VERSION);
 
        TBM_DBG("TBM ABI version %d.%d\n",
-           vermaj, vermin);
+           tbm_backend_major, tbm_backend_minor);
 
-       if (abimaj != vermaj) {
+       if (backend_module_major != tbm_backend_major) {
                TBM_ERR("TBM module ABI major ver(%d) doesn't match the TBM's ver(%d)\n",
-                       abimaj, vermaj);
+                       backend_module_major, tbm_backend_major);
                return 0;
-       } else if (abimin > vermin) {
+       } else if (backend_module_minor > tbm_backend_minor) {
                TBM_ERR("TBM module ABI minor ver(%d) is newer than the TBM's ver(%d)\n",
-                       abimin, vermin);
+                       backend_module_minor, tbm_backend_minor);
                return 0;
        }
 
@@ -282,8 +262,8 @@ _tbm_backend_load_module(tbm_bufmgr bufmgr, const char *file)
        void *module_data = NULL;
        tbm_backend_module *backend_module_data = NULL;
        tbm_backend_bufmgr_data *bufmgr_data = NULL;
-       int abimaj, abimin;
-       int vermaj, vermin;
+       int backend_module_major, backend_module_minor;
+       int tbm_backend_major, tbm_backend_minor;
        tbm_error_e error;
 
        snprintf(path, sizeof(path), BUFMGR_MODULE_DIR "/%s", file);
@@ -300,25 +280,25 @@ _tbm_backend_load_module(tbm_bufmgr bufmgr, const char *file)
                goto err;
        }
 
-       abimaj = GET_ABI_MAJOR(backend_module_data->abi_version);
-       abimin = GET_ABI_MINOR(backend_module_data->abi_version);
+       tbm_backend_major = GET_ABI_MAJOR(TBM_BACKEND_ABI_LATEST_VERSION);
+       tbm_backend_minor = GET_ABI_MINOR(TBM_BACKEND_ABI_LATEST_VERSION);
+       TBM_INFO("TBM Backend ABI version %d.%d\n", tbm_backend_major, tbm_backend_minor);
 
-       TBM_DBG("TBM module %s: vendor=\"%s\" ABI=%d,%d\n",
+       backend_module_major = GET_ABI_MAJOR(backend_module_data->abi_version);
+       backend_module_minor = GET_ABI_MINOR(backend_module_data->abi_version);
+
+       TBM_INFO("TBM module %s: vendor=\"%s\" Backend ABI version=%d.%d\n",
            backend_module_data->name ? backend_module_data->name : "UNKNOWN!",
            backend_module_data->vendor ? backend_module_data->vendor : "UNKNOWN!",
-               abimaj, abimin);
+               backend_module_major, backend_module_minor);
 
-       vermaj = GET_ABI_MAJOR(TBM_BACKEND_ABI_LATEST_VERSION);
-       vermin = GET_ABI_MINOR(TBM_BACKEND_ABI_LATEST_VERSION);
-       TBM_DBG("TBM ABI version %d.%d\n", vermaj, vermin);
-
-       if (abimaj > vermaj) {
+       if (backend_module_major > tbm_backend_major) {
                TBM_ERR("TBM module ABI major ver(%d) is newer than the TBM's ver(%d)\n",
-                       abimaj, vermaj);
+                       backend_module_major, tbm_backend_major);
                goto err;
-       } else if (abimin > vermin) {
+       } else if (backend_module_minor > tbm_backend_minor) {
                TBM_ERR("TBM module ABI minor ver(%d) is newer than the TBM's ver(%d)\n",
-                       abimin, vermin);
+                       backend_module_minor, tbm_backend_minor);
                goto err;
        }
 
@@ -368,6 +348,7 @@ _tbm_backend_load_module(tbm_bufmgr bufmgr, const char *file)
        bufmgr->bufmgr_data = bufmgr_data;
 
        TBM_DBG("Success to load module(%s)\n", file);
+       TBM_STDOUT_INFO("Success to load module(%s)", file);
 
        return 1;
 
@@ -498,24 +479,8 @@ _tbm_bufmgr_init(int fd, int server)
        gettimeofday(&start_tv, NULL);
 #endif
 
-       /* LCOV_EXCL_START */
-#ifdef HAVE_DLOG
-       env = getenv("TBM_DLOG");
-       if (env) {
-               bDlog = atoi(env);
-               TBM_DBG("TBM_DLOG=%s\n", env);
-       } else
-               bDlog = 1;
-#endif
-
-#ifdef TRACE
-       env = getenv("TBM_TRACE");
-       if (env) {
-               trace_mask = atoi(env);
-               TBM_DBG("TBM_TRACE=%s\n", env);
-       } else
-               trace_mask = 0;
-#endif
+       bDlog = 1;
+       trace_mask = 0;
 
        pthread_mutex_lock(&gLock);
 
@@ -530,7 +495,7 @@ _tbm_bufmgr_init(int fd, int server)
        /* initialize buffer manager */
        if (gBufMgr) {
                gBufMgr->ref_count++;
-               TBM_INFO("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;
        }
@@ -555,6 +520,7 @@ _tbm_bufmgr_init(int fd, int server)
        }
 
        /* load bufmgr priv from env */
+       TBM_STDOUT_INFO("loading backend module");
        if (!_tbm_load_module(gBufMgr, gBufMgr->fd)) {
                TBM_ERR("error : Fail to load bufmgr backend\n");
                _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
@@ -564,6 +530,7 @@ _tbm_bufmgr_init(int fd, int server)
                return NULL;
 
        }
+       TBM_STDOUT_INFO("loading backend module done");
        /* LCOV_EXCL_STOP */
 
        gBufMgr->ref_count = 1;
@@ -635,7 +602,7 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
 
        bufmgr->ref_count--;
        if (bufmgr->ref_count > 0) {
-               TBM_INFO("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;
@@ -721,6 +688,7 @@ tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr)
        int c = 0;
        int size;
        tbm_error_e error;
+       long pid = 0;
 
        pthread_mutex_lock(&gLock);
        _tbm_set_last_result(TBM_ERROR_NONE);
@@ -741,12 +709,13 @@ tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr)
        }
 
        TBM_SNRPRINTF(str, len, c, "\n");
-       _tbm_util_get_appname_from_pid(getpid(), app_name);
+       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(%d)===========================\n",
-                 app_name, getpid());
+       TBM_SNRPRINTF(str, len, c, "===========================================TBM DEBUG: %s(%ld)===========================================\n",
+                 app_name, pid);
 
-       snprintf(title, 255, "%s", "no  surface     refcnt  width  height  bpp  size    n_b  n_p  flags  format    app_name       ");
+       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(debug_old_data, &bufmgr->debug_key_list, item_link) {
@@ -765,20 +734,24 @@ tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr)
 
                LIST_FOR_EACH_ENTRY(surf, &bufmgr->surf_list, item_link) {
                        char data[512] = {0,};
-                       unsigned int pid;
+                       unsigned int surf_pid = 0;
                        int i;
 
-                       pid = _tbm_surface_internal_get_debug_pid(surf);
-                       if (!pid) {
+                       surf_pid = _tbm_surface_internal_get_debug_pid(surf);
+                       if (!surf_pid) {
                                /* if pid is null, set the self_pid */
-                               pid = getpid();
+                               surf_pid = syscall(SYS_getpid);;
                        }
 
                        memset(app_name, 0x0, 255 * sizeof(char));
-                       _tbm_util_get_appname_from_pid(pid, app_name);
-                       _tbm_util_get_appname_brief(app_name);
+                       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);
+                       }
 
-                       snprintf(data, 255, "%-2d  %-9p    %-4d  %-5u  %-6u  %-3u  %-6u   %-2d   %-2d    %-3d  %-8s  %-15s",
+                       snprintf(data, 255, "%-3d %-11p   %-5d %-6u %-7u %-4u %-7u  %-3d  %-3d %-8d %-9s %-22s",
                                  ++surf_cnt,
                                  surf,
                                  surf->refcnt,
@@ -825,7 +798,7 @@ tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr)
        TBM_SNRPRINTF(str, len, c, "\n");
 
        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");
+       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)) {
@@ -845,7 +818,7 @@ tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr)
                                size = bufmgr->backend->bo_size(bo);
                                key = bufmgr->backend->bo_export(bo);
                        }
-                       TBM_SNRPRINTF(str, len, c, "%-4d%-11p   %-4d  %-6d     %-5d     %-4u    %-3d  %-11p  %-4d\n",
+                       TBM_SNRPRINTF(str, len, c, "%-3d %-11p   %-5d %-7d    %-6d    %-5u %-7d %-11p  %-4d\n",
                                  ++bo_cnt,
                                  bo,
                                  bo->ref_cnt,
@@ -860,7 +833,7 @@ tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr)
                TBM_SNRPRINTF(str, len, c, "no tbm_bos.\n");
        TBM_SNRPRINTF(str, len, c, "\n");
 
-       TBM_SNRPRINTF(str, len, c, "===============================================================\n");
+       TBM_SNRPRINTF(str, len, c, "========================================================================================================\n");
 
        pthread_mutex_unlock(&gLock);
 
@@ -1006,14 +979,19 @@ tbm_bufmgr_debug_dump_all(char *path)
        pthread_mutex_lock(&gLock);
        _tbm_set_last_result(TBM_ERROR_NONE);
 
-       TBM_RETURN_VAL_IF_FAIL(path != NULL, 0);
+       if (!path) {
+               TBM_ERR("path is null.\n");
+               pthread_mutex_unlock(&gLock);
+               return 0;
+       }
+
        TBM_DBG("path=%s\n", path);
 
        count = _tbm_util_get_max_surface_size(&w, &h);
        if (count == 0) {
                TBM_ERR("No tbm_surface.\n");
                pthread_mutex_unlock(&gLock);
-               return 1;
+               return 0;
        }
 
        tbm_surface_internal_dump_with_scale_start(path, w, h, count, scale_factor);