only display server unset tbm master fd if tbm_bufmgr_init fail
[platform/adaptation/spreadtrum/libtbm-sprd.git] / src / tbm_bufmgr_sprd.c
index aa93b82..bd5be03 100755 (executable)
@@ -258,7 +258,7 @@ uint32_t tbm_sprd_color_format_list[TBM_COLOR_FORMAT_COUNT] = { TBM_FORMAT_RGBA8
                                                                TBM_FORMAT_YUV420,
                                                                TBM_FORMAT_YVU420
                                                              };
-#ifdef USE_CACHE
+
 static inline int
 _tgl_init(int fd, unsigned int key)
 {
@@ -326,6 +326,7 @@ _tgl_unlock(int fd, unsigned int key)
        return 1;
 }
 
+#ifdef USE_CACHE
 static inline int
 _tgl_set_data(int fd, unsigned int key, unsigned int val)
 {
@@ -459,10 +460,10 @@ _tbm_sprd_open_drm()
        return fd;
 }
 
+#ifdef USE_CACHE
 static int
 _sprd_bo_cache_flush (tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd, int flags)
 {
-#ifdef USE_CACHE
        SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
        SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
 
@@ -510,25 +511,25 @@ _sprd_bo_cache_flush (tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd, int flag
                              getpid(), __FUNCTION__, __LINE__);
                return 0;
        }
-#endif
 
        return 1;
 }
+#endif
 
 static int
 _bo_init_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd, int import)
 {
-#ifdef USE_CACHE
        SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
        SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
 
-       tbm_bo_cache_state cache_state;
-
        if (bufmgr_sprd->use_dma_fence)
                return 1;
 
        _tgl_init(bufmgr_sprd->tgl_fd, bo_sprd->name);
 
+#ifdef USE_CACHE
+       tbm_bo_cache_state cache_state;
+
        if (import == 0) {
                cache_state.data.isDirtied = DEVICE_NONE;
                cache_state.data.isCached = 0;
@@ -634,7 +635,6 @@ _bo_save_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd)
 static void
 _bo_destroy_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd)
 {
-#ifdef USE_CACHE
        SPRD_RETURN_IF_FAIL (bo_sprd != NULL);
        SPRD_RETURN_IF_FAIL (bufmgr_sprd != NULL);
 
@@ -642,13 +642,11 @@ _bo_destroy_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd)
                return;
 
        _tgl_destroy(bufmgr_sprd->tgl_fd, bo_sprd->name);
-#endif
 }
 
 static int
 _bufmgr_init_cache_state(tbm_bufmgr_sprd bufmgr_sprd)
 {
-#ifdef USE_CACHE
        SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0);
 
        if (bufmgr_sprd->use_dma_fence)
@@ -669,6 +667,7 @@ _bufmgr_init_cache_state(tbm_bufmgr_sprd bufmgr_sprd)
                }
        }
 
+#ifdef USE_CACHE
        if (!_tgl_init(bufmgr_sprd->tgl_fd, GLOBAL_KEY)) {
                TBM_SPRD_LOG("[libtbm-sprd:%d] "
                               "error: Fail to initialize the tgl\n",
@@ -685,7 +684,6 @@ _bufmgr_init_cache_state(tbm_bufmgr_sprd bufmgr_sprd)
 static void
 _bufmgr_deinit_cache_state(tbm_bufmgr_sprd bufmgr_sprd)
 {
-#ifdef USE_CACHE
        SPRD_RETURN_IF_FAIL(bufmgr_sprd != NULL);
 
        if (bufmgr_sprd->use_dma_fence)
@@ -693,7 +691,6 @@ _bufmgr_deinit_cache_state(tbm_bufmgr_sprd bufmgr_sprd)
 
        if (bufmgr_sprd->tgl_fd >= 0)
                close(bufmgr_sprd->tgl_fd);
-#endif
 }
 
 #ifndef USE_CONTIG_ONLY
@@ -1391,10 +1388,8 @@ tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
 {
        SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
 
-#if USE_BACKEND_LOCK
        tbm_bufmgr_sprd bufmgr_sprd;
        tbm_bo_sprd bo_sprd;
-       int ret = 0;
 
        bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
        SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
@@ -1402,7 +1397,11 @@ tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
        bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
        SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
 
+#if USE_BACKEND_LOCK
+       int ret = 0;
+
        if (bufmgr_sprd->use_dma_fence) {
+
                struct dma_buf_fence fence;
 
                memset(&fence, 0, sizeof(struct dma_buf_fence));
@@ -1472,6 +1471,7 @@ tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
                     getpid(),
                     __FUNCTION__, bo_sprd->name, bo_sprd->dmabuf);
        } else {
+
                ret = _tgl_lock(bufmgr_sprd->tgl_fd, bo_sprd->name);
 
                DBG ("[libtbm-sprd:%d] lock tgl flink_id:%d\n",
@@ -1479,8 +1479,8 @@ tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
 
                return ret;
        }
-
 #endif
+
        return 1;
 }
 
@@ -1489,10 +1489,8 @@ tbm_sprd_bo_unlock(tbm_bo bo)
 {
        SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
 
-#if USE_BACKEND_LOCK
        tbm_bufmgr_sprd bufmgr_sprd;
        tbm_bo_sprd bo_sprd;
-       int ret = 0;
 
        bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
        SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
@@ -1500,6 +1498,9 @@ tbm_sprd_bo_unlock(tbm_bo bo)
        bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
        SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
 
+#if USE_BACKEND_LOCK
+       int ret = 0;
+
        if (bufmgr_sprd->use_dma_fence) {
                struct dma_buf_fence fence;
 
@@ -1547,6 +1548,7 @@ tbm_sprd_bo_unlock(tbm_bo bo)
                return ret;
        }
 #endif
+
        return 1;
 }
 
@@ -1919,14 +1921,11 @@ init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
        }
 
        if (tbm_backend_is_display_server()) {
-               int master_fd = -1;
-
                bufmgr_sprd->fd = -1;
-               master_fd = tbm_drm_helper_get_master_fd();
-               if (master_fd < 0)
+
+               bufmgr_sprd->fd = tbm_drm_helper_get_master_fd();
+               if (bufmgr_sprd->fd < 0)
                        bufmgr_sprd->fd = _tbm_sprd_open_drm();
-               else
-                       bufmgr_sprd->fd = master_fd;
 
                if (bufmgr_sprd->fd < 0) {
                        TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to create drm!\n", getpid());
@@ -1941,6 +1940,7 @@ init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
                if (!bufmgr_sprd->device_name)
                {
                        TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to get device name!\n", getpid());
+                       tbm_drm_helper_unset_tbm_master_fd();
                        close(bufmgr_sprd->fd);
                        free (bufmgr_sprd);
                        return 0;
@@ -1973,7 +1973,9 @@ init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
        if (!_bufmgr_init_cache_state(bufmgr_sprd)) {
                TBM_SPRD_LOG ("[libtbm-sprd:%d] error: init bufmgr cache state failed!\n", getpid());
 
-               tbm_drm_helper_unset_tbm_master_fd();
+               if (tbm_backend_is_display_server())
+                       tbm_drm_helper_unset_tbm_master_fd();
+
                close(bufmgr_sprd->fd);
 
                free(bufmgr_sprd);
@@ -1986,7 +1988,9 @@ init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
 
                _bufmgr_deinit_cache_state(bufmgr_sprd);
 
-               tbm_drm_helper_unset_tbm_master_fd();
+               if (tbm_backend_is_display_server())
+                       tbm_drm_helper_unset_tbm_master_fd();
+
                close(bufmgr_sprd->fd);
 
                free (bufmgr_sprd);
@@ -2018,7 +2022,9 @@ init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
 
                _bufmgr_deinit_cache_state(bufmgr_sprd);
 
-               tbm_drm_helper_unset_tbm_master_fd();
+               if (tbm_backend_is_display_server())
+                       tbm_drm_helper_unset_tbm_master_fd();
+
                close(bufmgr_sprd->fd);
 
                free (bufmgr_sprd);