- if (fd < 0)
- {
- _tbm_set_last_result (TBM_BO_ERROR_GET_FD_FAILED);
- TBM_LOG ("[libtbm:%d] Fail get drm fd\n", getpid());
- pthread_mutex_unlock (&gLock);
- return NULL;
- }
- fd_flag = 1;
- }
-
- DBG ("[libtbm:%d] bufmgr init: fd=%d\n", getpid(), fd);
-
- /* allocate bufmgr */
- gBufMgr = calloc (1, sizeof(struct _tbm_bufmgr));
- if (!gBufMgr)
- {
- _tbm_set_last_result (TBM_BO_ERROR_HEAP_ALLOC_FAILED);
- if (fd_flag)
- close(fd);
-
- pthread_mutex_unlock (&gLock);
- return NULL;
- }
-
- gBufMgr->fd_flag = fd_flag;
-
- if (fd_flag)
- {
- gBufMgr->fd = fd;
- }
- else
- {
- gBufMgr->fd = dup(fd);
- if (gBufMgr->fd < 0)
- {
- _tbm_set_last_result (TBM_BO_ERROR_DUP_FD_FAILED);
- TBM_LOG ("[libtbm:%d] Fail to duplicate(dup) the drm fd\n", getpid());
- free (gBufMgr);
- gBufMgr = NULL;
- pthread_mutex_unlock (&gLock);
- return NULL;
- }
- DBG ("[libtbm:%d] duplicate the drm_fd(%d), bufmgr use fd(%d).\n",
- getpid(), fd, gBufMgr->fd);
- }
-
- /* load bufmgr priv from env */
- if (!_tbm_load_module(gBufMgr, gBufMgr->fd))
- {
- _tbm_set_last_result (TBM_BO_ERROR_LOAD_MODULE_FAILED);
- TBM_LOG ("[libtbm:%d] "
- "error : Fail to load bufmgr backend\n",
- getpid());
- close (gBufMgr->fd);
- free (gBufMgr);
- gBufMgr = NULL;
- pthread_mutex_unlock (&gLock);
- return NULL;
- }
- else
- {
- backend_flag = gBufMgr->backend->flags;
- /* log for tbm backend_flag */
- DBG ("[libtbm:%d] ", getpid());
- DBG ("cache_crtl:");
- if (backend_flag&TBM_CACHE_CTRL_BACKEND) {
- DBG ("BACKEND ");
- } else {
- DBG ("TBM ");
- }
- DBG ("lock_crtl:");
- if (backend_flag&TBM_LOCK_CTRL_BACKEND) {
- DBG ("BACKEND ");
- } else {
- DBG ("TBM ");
- }
- DBG ("\n");
- }
-
- gBufMgr->ref_count = 1;
-
- DBG ("[libtbm:%d] create tizen bufmgr: ref_count:%d\n", getpid(), gBufMgr->ref_count);
-
- if (pthread_mutex_init (&gBufMgr->lock, NULL) != 0)
- {
- _tbm_set_last_result (TBM_BO_ERROR_THREAD_INIT_FAILED);
- gBufMgr->backend->bufmgr_deinit (gBufMgr->backend->priv);
- tbm_backend_free (gBufMgr->backend);
- dlclose (gBufMgr->module_data);
- close (gBufMgr->fd);
- free (gBufMgr);
- gBufMgr = NULL;
- pthread_mutex_unlock (&gLock);
- return NULL;
- }
-
- /* intialize the tizen global status */
- if (!_tbm_bufmgr_init_state (gBufMgr))
- {
- _tbm_set_last_result (TBM_BO_ERROR_INIT_STATE_FAILED);
- TBM_LOG ("[libtbm:%d] "
- "error: Fail to init state\n",
- getpid());
- gBufMgr->backend->bufmgr_deinit (gBufMgr->backend->priv);
- tbm_backend_free (gBufMgr->backend);
- pthread_mutex_destroy (&gBufMgr->lock);
- dlclose (gBufMgr->module_data);
- close (gBufMgr->fd);
- free (gBufMgr);
- gBufMgr = NULL;
- pthread_mutex_unlock (&gLock);
- return NULL;
- }
-
- /* setup the lock_type */
- env = getenv ("BUFMGR_LOCK_TYPE");
- if (env && !strcmp (env, "always"))
- gBufMgr->lock_type = LOCK_TRY_ALWAYS;
- else if(env && !strcmp(env, "none"))
- gBufMgr->lock_type = LOCK_TRY_NEVER;
- else if(env && !strcmp(env, "once"))
- gBufMgr->lock_type = LOCK_TRY_ONCE;
- else
- gBufMgr->lock_type = LOCK_TRY_ALWAYS;
-
- DBG ("[libtbm:%d] BUFMGR_LOCK_TYPE=%s\n", getpid(), env?env:"default:once");
-
- /* setup the map_cache */
- env = getenv ("BUFMGR_MAP_CACHE");
- if (env && !strcmp (env, "false"))
- gBufMgr->use_map_cache = 0;
- else
- gBufMgr->use_map_cache = 1;
- DBG ("[libtbm:%d] BUFMGR_MAP_CACHE=%s\n", getpid(), env?env:"default:true");
-
- /* intialize bo_list */
- LIST_INITHEAD (&gBufMgr->bo_list);
-
- /* intialize surf_list */
- LIST_INITHEAD (&gBufMgr->surf_list);
-
- pthread_mutex_unlock (&gLock);
- return gBufMgr;
+ /* LCOV_EXCL_STOP */
+
+ /* initialize buffer manager */
+ if (gBufMgr) {
+ gBufMgr->ref_count++;
+ TBM_TRACE("reuse tbm_bufmgr(%p) ref_count(%d) fd(%d)\n", gBufMgr, gBufMgr->ref_count, gBufMgr->fd);
+ pthread_mutex_unlock(&gLock);
+ return gBufMgr;
+ }
+
+ DBG("bufmgr init\n");
+
+ /* allocate bufmgr */
+ gBufMgr = calloc(1, sizeof(struct _tbm_bufmgr));
+ if (!gBufMgr) {
+ _tbm_set_last_result(TBM_BO_ERROR_HEAP_ALLOC_FAILED);
+ TBM_TRACE("error: fail to alloc bufmgr fd(%d)\n", fd);
+ pthread_mutex_unlock(&gLock);
+ return NULL;
+ }
+
+ gBufMgr->fd = 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 */
+ }
+
+ /* log for tbm backend_flag */
+ DBG("backend flag:%x:", gBufMgr->backend->flags);
+ DBG("\n");
+
+ gBufMgr->ref_count = 1;
+
+ DBG("create tizen bufmgr:%p ref_count:%d\n",
+ gBufMgr, gBufMgr->ref_count);
+
+ /* setup the lock_type */
+ env = getenv("BUFMGR_LOCK_TYPE");
+ if (env && !strcmp(env, "always"))
+ gBufMgr->lock_type = LOCK_TRY_ALWAYS;
+ else if (env && !strcmp(env, "none"))
+ gBufMgr->lock_type = LOCK_TRY_NEVER;
+ else if (env && !strcmp(env, "once"))
+ gBufMgr->lock_type = LOCK_TRY_ONCE;
+ else
+ gBufMgr->lock_type = LOCK_TRY_ALWAYS;
+
+ 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);
+
+ /* intialize bo_list */
+ LIST_INITHEAD(&gBufMgr->bo_list);
+
+ /* intialize surf_list */
+ LIST_INITHEAD(&gBufMgr->surf_list);
+
+ /* intialize surf_list */
+ LIST_INITHEAD(&gBufMgr->surf_queue_list);
+
+ /* intialize debug_key_list */
+ LIST_INITHEAD(&gBufMgr->debug_key_list);
+
+ pthread_mutex_unlock(&gLock);
+
+ return gBufMgr;