use dlog default
[platform/core/uifw/libtbm.git] / src / tbm_bufmgr.c
index 1e6b471..47f30c6 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
 
@@ -76,6 +76,7 @@ static void _tbm_bufmgr_mutex_unlock(void);
 #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;\
        } \
@@ -84,42 +85,21 @@ static void _tbm_bufmgr_mutex_unlock(void);
 #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 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);
@@ -177,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");
@@ -205,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;
        }
 
@@ -280,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);
@@ -298,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);
-
-       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);
+               backend_module_major, backend_module_minor);
 
-       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;
        }
 
@@ -366,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;
 
@@ -496,16 +479,9 @@ _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
+       bDlog = 1;
 
+       /* LCOV_EXCL_START */
 #ifdef TRACE
        env = getenv("TBM_TRACE");
        if (env) {
@@ -517,6 +493,8 @@ _tbm_bufmgr_init(int fd, int server)
 
        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");
@@ -526,7 +504,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;
        }
@@ -537,6 +515,7 @@ _tbm_bufmgr_init(int fd, int server)
        gBufMgr = calloc(1, sizeof(struct _tbm_bufmgr));
        if (!gBufMgr) {
                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;
        }
@@ -550,14 +529,17 @@ _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);
                free(gBufMgr);
                gBufMgr = NULL;
                pthread_mutex_unlock(&gLock);
                return NULL;
 
        }
+       TBM_STDOUT_INFO("loading backend module done");
        /* LCOV_EXCL_STOP */
 
        gBufMgr->ref_count = 1;
@@ -618,6 +600,7 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr)
 
        _tbm_bufmgr_mutex_lock();
        pthread_mutex_lock(&gLock);
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        if (!gBufMgr) {
                TBM_ERR("gBufmgr already destroy: bufmgr:%p\n", bufmgr);
@@ -628,7 +611,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;
@@ -691,6 +674,7 @@ tbm_bufmgr_get_capability(tbm_bufmgr bufmgr)
        unsigned int capabilities = TBM_BUFMGR_CAPABILITY_NONE;
 
        _tbm_bufmgr_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        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);
@@ -713,11 +697,14 @@ 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);
 
        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;
        }
@@ -725,15 +712,17 @@ tbm_bufmgr_debug_tbm_info_get(tbm_bufmgr bufmgr)
        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;
        }
 
        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       ");
 
@@ -754,18 +743,22 @@ 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",
                                  ++surf_cnt,
@@ -871,6 +864,7 @@ void
 tbm_bufmgr_debug_trace(tbm_bufmgr bufmgr, int onoff)
 {
        _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);
@@ -887,6 +881,7 @@ 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);
@@ -926,6 +921,7 @@ void
 tbm_bufmgr_debug_dump_set_scale(double scale)
 {
        pthread_mutex_lock(&gLock);
+       _tbm_set_last_result(TBM_ERROR_NONE);
        scale_factor = scale;
        pthread_mutex_unlock(&gLock);
 }
@@ -933,13 +929,24 @@ tbm_bufmgr_debug_dump_set_scale(double scale)
 int
 tbm_bufmgr_debug_get_ref_count(void)
 {
-       return (gBufMgr) ? gBufMgr->ref_count : 0;
+       int refcnt;
+
+       pthread_mutex_lock(&gLock);
+
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       refcnt = (gBufMgr) ? gBufMgr->ref_count : 0;
+
+       pthread_mutex_unlock(&gLock);
+
+       return refcnt;
 }
 
 int
 tbm_bufmgr_debug_queue_dump(char *path, int count, int onoff)
 {
        pthread_mutex_lock(&gLock);
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        if (onoff == 0) {
                TBM_DBG("count=%d onoff=%d\n", count, onoff);
@@ -968,6 +975,7 @@ tbm_bufmgr_debug_queue_dump(char *path, int count, int onoff)
        }
 
        pthread_mutex_unlock(&gLock);
+
        return 1;
 }
 
@@ -977,16 +985,22 @@ tbm_bufmgr_debug_dump_all(char *path)
        int w, h, count = 0;
        tbm_surface_h surface = NULL;
 
-       TBM_RETURN_VAL_IF_FAIL(path != NULL, 0);
-       TBM_DBG("path=%s\n", path);
-
        pthread_mutex_lock(&gLock);
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       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);
@@ -1016,6 +1030,7 @@ tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *native_display)
        tbm_error_e error;
 
        _tbm_bufmgr_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
 
@@ -1023,6 +1038,7 @@ tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *native_display)
                if (!bufmgr->bufmgr_func->bufmgr_bind_native_display) {
                        TBM_WRN("skip: tbm_bufmgr(%p) native_display(%p)\n",
                                        bufmgr, native_display);
+                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
                        _tbm_bufmgr_mutex_unlock();
                        return 1;
                }
@@ -1031,6 +1047,7 @@ tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *native_display)
                if (error != TBM_ERROR_NONE) {
                        TBM_ERR("error: tbm_bufmgr(%p) native_display(%p) error(%d)\n",
                                        bufmgr, native_display, error);
+                       _tbm_set_last_result(error);
                        _tbm_bufmgr_mutex_unlock();
                        return 0;
                }
@@ -1039,6 +1056,7 @@ tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *native_display)
                if (!bufmgr->backend->bufmgr_bind_native_display) {
                        TBM_WRN("skip: tbm_bufmgr(%p) native_display(%p)\n",
                                        bufmgr, native_display);
+                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
                        _tbm_bufmgr_mutex_unlock();
                        return 1;
                }
@@ -1047,6 +1065,7 @@ tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *native_display)
                if (!ret) {
                        TBM_ERR("error: tbm_bufmgr(%p) native_display(%p)\n",
                                        bufmgr, native_display);
+                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
                        _tbm_bufmgr_mutex_unlock();
                        return 0;
                }
@@ -1073,11 +1092,14 @@ int
 tbm_bufmgr_set_bo_lock_type(tbm_bufmgr bufmgr, tbm_bufmgr_bo_lock_type bo_lock_type)
 {
        _tbm_bufmgr_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0);
        TBM_BUFMGR_RETURN_VAL_IF_FAIL(bufmgr == gBufMgr, 0);
 
+       pthread_mutex_lock(&gLock);
        gBufMgr->bo_lock_type = bo_lock_type;
+       pthread_mutex_unlock(&gLock);
 
        TBM_INFO("The bo_lock_type of the bo is %d\n", bo_lock_type);