-#ifdef DEBUG
- env = getenv("GEM_DEBUG");
- if (env) {
- bDebug = atoi(env);
- TBM_LOG("GEM_DEBUG=%s\n", env);
- } else
- bDebug = 0;
-#endif
-
- /* initialize buffer manager */
- if (gBufMgr) {
- DBG("[libtbm:%d] use previous gBufMgr\n", getpid());
- if (!gBufMgr->fd_flag) {
- if (fd >= 0) {
- if (dup2(gBufMgr->fd, fd) < 0) {
- _tbm_set_last_result(TBM_BO_ERROR_DUP_FD_FAILED);
- TBM_LOG("[libtbm:%d] Fail to duplicate(dup2) the drm fd\n",
- getpid());
- pthread_mutex_unlock(&gLock);
- return NULL;
- }
- DBG("[libtbm:%d] duplicate the drm_fd(%d), new drm_fd(%d).\n",
- getpid(), gBufMgr->fd, fd);
- }
- }
- gBufMgr->ref_count++;
-
- DBG("[libtbm:%d] bufmgr ref: fd=%d, ref_count:%d\n",
- getpid(), gBufMgr->fd, gBufMgr->ref_count);
- pthread_mutex_unlock(&gLock);
- return gBufMgr;
- }
-
- if (fd < 0) {
- 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);
- pthread_mutex_unlock(&gLock);
- return NULL;
- }
-
- gBufMgr->fd_flag = fd_flag;
-
- if (fd_flag) {
- gBufMgr->fd = -1;
- } 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());
-
- if (gBufMgr->fd > 0)
- 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);
-
- if (gBufMgr->fd > 0)
- 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);
-
- if (gBufMgr->fd > 0)
- 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);