-static int _tbm_load_module (tbm_bufmgr bufmgr, int fd)
-{
- struct dirent **namelist;
- const char *p = NULL;
- int n;
- int ret = 0;
-
- /* load bufmgr priv from default lib */
- ret = _tbm_bufmgr_load_module (bufmgr, fd, DEFAULT_LIB);
-
- /* load bufmgr priv from configured path */
- 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);
- else
- {
- while(n--)
- {
- if (!ret && strstr (namelist[n]->d_name, PREFIX_LIB))
- {
- p = strstr (namelist[n]->d_name, SUFFIX_LIB);
- if (p != NULL)
- {
- if (!strcmp (p, SUFFIX_LIB))
- {
- ret = _tbm_bufmgr_load_module (bufmgr, fd, namelist[n]->d_name);
- }
- }
- }
- free(namelist[n]);
- }
- free(namelist);
- }
- }
-
- return ret;
+ 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");
+ }
+
+
+ /* initialize buffer manager */
+ if (gBufMgr) {
+ gBufMgr->ref_count++;
+ TBM_DBG("reuse tbm_bufmgr(%p) ref_count(%d) fd(%d)\n", gBufMgr, gBufMgr->ref_count, gBufMgr->fd);
+ pthread_mutex_unlock(&gLock);
+ return gBufMgr;
+ }
+
+ TBM_DBG("bufmgr init\n");
+
+ /* allocate bufmgr */
+ 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;
+ }
+
+ gBufMgr->fd = fd;
+
+ /* set the display_server flag before loading the backend module */
+ if (server) {
+ TBM_INFO("The tbm_bufmgr(%p) is used by display server. Need to bind the native_display.\n", gBufMgr);
+ gBufMgr->display_server = 1;
+ }
+
+ /* load bufmgr priv from env */
+ gBufMgr->module = tbm_module_load(gBufMgr->fd);
+ if (!gBufMgr->module) {
+ 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;
+
+ }
+
+ // TODO: this is temporary. it will be removed after finishing refactoring the tbm_module.
+ _tbm_bufmgr_copy_module_data(gBufMgr, gBufMgr->module, 0);
+
+ /* check the essential capabilities of tbm_module */
+ gBufMgr->capabilities = tbm_module_bufmgr_get_capabilities(gBufMgr->module, &error);
+ if (gBufMgr->capabilities == TBM_BUFMGR_CAPABILITY_NONE) {
+ TBM_ERR("The capabilities of the backend module is TBM_BUFMGR_CAPABILITY_NONE.");
+ TBM_ERR("TBM_BUFMGR_CAPABILITY_SHARE_FD is the essential capability.");
+ tbm_module_unload(gBufMgr->module);
+ _tbm_set_last_result(error);
+ free(gBufMgr);
+ gBufMgr = NULL;
+ pthread_mutex_unlock(&gLock);
+ return NULL;
+ }
+
+ if (!(gBufMgr->capabilities & TBM_BUFMGR_CAPABILITY_SHARE_FD)) {
+ TBM_ERR("The capabilities of the backend module had no TBM_BUFMGR_CAPABILITY_SHARE_FD.");
+ TBM_ERR("The tbm backend has to get TBM_BUFMGR_CAPABILITY_SHARE_FD. ");
+ tbm_module_unload(gBufMgr->module);
+ _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
+ free(gBufMgr);
+ gBufMgr = NULL;
+ pthread_mutex_unlock(&gLock);
+ return NULL;
+ }
+
+ /* LCOV_EXCL_STOP */
+
+ gBufMgr->ref_count = 1;
+
+ TBM_INFO("create tizen bufmgr:%p ref_count:%d\n",
+ gBufMgr, gBufMgr->ref_count);
+
+ /* setup the bo_lock_type */
+ env = getenv("BUFMGR_LOCK_TYPE");
+ if (env && !strcmp(env, "always"))
+ gBufMgr->bo_lock_type = TBM_BUFMGR_BO_LOCK_TYPE_ALWAYS;
+ else if (env && !strcmp(env, "none"))
+ gBufMgr->bo_lock_type = TBM_BUFMGR_BO_LOCK_TYPE_NEVER;
+ else if (env && !strcmp(env, "once"))
+ gBufMgr->bo_lock_type = TBM_BUFMGR_BO_LOCK_TYPE_ONCE;
+ else
+ gBufMgr->bo_lock_type = TBM_BUFMGR_BO_LOCK_TYPE_ALWAYS;
+
+ TBM_DBG("BUFMGR_LOCK_TYPE=%s\n", env ? env : "default:once");
+
+ /* intialize bo_list */
+ LIST_INITHEAD(&gBufMgr->bo_list);
+
+ /* intialize surf_list */
+ LIST_INITHEAD(&gBufMgr->surf_list);
+
+ /* intialize surf_queue_list */
+ LIST_INITHEAD(&gBufMgr->surf_queue_list);
+
+ /* intialize debug_key_list */
+ LIST_INITHEAD(&gBufMgr->debug_key_list);
+
+#ifdef TBM_BUFMGR_INIT_TIME
+ /* get the end tv */
+ gettimeofday(&end_tv, NULL);
+ TBM_INFO("tbm_bufmgr_init time: %ld ms", ((end_tv.tv_sec * 1000 + end_tv.tv_usec / 1000) - (start_tv.tv_sec * 1000 + start_tv.tv_usec / 1000)));
+#endif
+
+ pthread_mutex_unlock(&gLock);
+
+ return gBufMgr;