package version up to 2.4.3
[platform/adaptation/spreadtrum/libtbm-sprd.git] / src / tbm_bufmgr_sprd.c
index 874deb0..46e278e 100644 (file)
@@ -823,7 +823,7 @@ tbm_sprd_bufmgr_bind_native_display(tbm_backend_bufmgr_data *bufmgr_data, tbm_na
        if (!tbm_drm_helper_wl_auth_server_init(native_display, bufmgr_sprd->fd,
                                           bufmgr_sprd->device_name, 0)) {
                TBM_ERR("fail to tbm_drm_helper_wl_server_init\n");
-               return TBM_ERROR_OPERATION_FAILED;
+               return TBM_ERROR_INVALID_OPERATION;
        }
 
        bufmgr_sprd->bind_display = native_display;
@@ -956,7 +956,7 @@ tbm_sprd_bufmgr_get_plane_data(tbm_backend_bufmgr_data *bufmgr_data,
 //             else if (plane_idx == 1)
                {
                        _offset = _size;
-                       _pitch = SIZE_ALIGN(width , TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _pitch = SIZE_ALIGN(width , TBM_SURFACE_ALIGNMENT_PITCH_YUV);
                        _align_height = SIZE_ALIGN(height / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
                        _size = SIZE_ALIGN(_pitch * _align_height, TBM_SURFACE_ALIGNMENT_PLANE);
                        _bo_idx = 0;
@@ -978,7 +978,7 @@ tbm_sprd_bufmgr_get_plane_data(tbm_backend_bufmgr_data *bufmgr_data,
                //else if( plane_idx ==1 )
                {
                        _offset += _size;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
                        _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
                        _bo_idx = 0;
                }
@@ -1107,7 +1107,8 @@ tbm_sprd_bufmgr_get_plane_data(tbm_backend_bufmgr_data *bufmgr_data,
 }
 
 static tbm_backend_bo_data *
-tbm_sprd_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, int size, tbm_bo_memory_type flags, tbm_error_e *error)
+tbm_sprd_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, unsigned int size,
+                                       tbm_bo_memory_type flags, tbm_error_e *error)
 {
        tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data;
        tbm_bo_sprd bo_sprd;
@@ -1148,7 +1149,7 @@ tbm_sprd_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, int size, tbm_bo_
                        arg.flags, (unsigned int)arg.size);
                free(bo_sprd);
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                return NULL;
        }
 
@@ -1163,7 +1164,7 @@ tbm_sprd_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, int size, tbm_bo_
                TBM_ERR("error fail init cache state(%d)\n", bo_sprd->name);
                free(bo_sprd);
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                return NULL;
        }
 
@@ -1177,7 +1178,7 @@ tbm_sprd_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, int size, tbm_bo_
                        TBM_ERR("error Cannot dmabuf=%d\n", bo_sprd->gem);
                        free(bo_sprd);
                        if (error)
-                               *error = TBM_ERROR_OPERATION_FAILED;
+                               *error = TBM_ERROR_INVALID_OPERATION;
                        return NULL;
                }
                bo_sprd->dmabuf = arg.fd;
@@ -1223,7 +1224,7 @@ tbm_sprd_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_
                TBM_ERR("Cannot get gem handle from fd:%d (%s)\n",
                               arg.fd, strerror_r(errno, buf, STRERR_BUFSIZE));
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                return NULL;
        }
        gem = arg.handle;
@@ -1233,7 +1234,7 @@ tbm_sprd_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_
                TBM_ERR("Cannot get name from gem:%d, fd:%d (%s)\n",
                               gem, key, strerror_r(errno, buf, STRERR_BUFSIZE));
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                return NULL;
        }
 
@@ -1266,7 +1267,7 @@ tbm_sprd_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_
                TBM_ERR("Cannot get gem info from gem:%d, fd:%d (%s)\n",
                               gem, key, strerror_r(errno, buf, STRERR_BUFSIZE));
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                return NULL;
        }
 
@@ -1292,7 +1293,7 @@ tbm_sprd_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_
        if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 1)) {
                TBM_ERR("error fail init cache state(%d)\n", bo_sprd->name);
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                goto fail_init_cache;
        }
 
@@ -1347,7 +1348,7 @@ tbm_sprd_bufmgr_import_key(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tb
        if (drmIoctl(bufmgr_sprd->fd, DRM_IOCTL_GEM_OPEN, &arg)) {
                TBM_ERR("error Cannot open gem name=%d\n", key);
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                return NULL;
        }
 
@@ -1358,7 +1359,7 @@ tbm_sprd_bufmgr_import_key(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tb
                                sizeof(struct drm_sprd_gem_info))) {
                TBM_ERR("error Cannot get gem info=%d\n", key);
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                goto fail_get_gem;
        }
 
@@ -1386,7 +1387,7 @@ tbm_sprd_bufmgr_import_key(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tb
        if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 1)) {
                TBM_ERR("error fail init cache state(%d)\n", bo_sprd->name);
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                goto fail_init_cache;
        }
 
@@ -1397,7 +1398,7 @@ tbm_sprd_bufmgr_import_key(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tb
                if (drmIoctl(bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
                        TBM_ERR("error Cannot dmabuf=%d\n", bo_sprd->gem);
                        if (error)
-                               *error = TBM_ERROR_OPERATION_FAILED;
+                               *error = TBM_ERROR_INVALID_OPERATION;
                        goto fail_prime_handle_to_fd;
                }
                bo_sprd->dmabuf = arg.fd;
@@ -1558,7 +1559,7 @@ tbm_sprd_bo_get_handle(tbm_backend_bo_data *bo_data, tbm_bo_device_type device,
                TBM_ERR("Cannot get handle: gem:%d, device:%d\n",
                        bo_sprd->gem, device);
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                return (tbm_bo_handle) NULL;
        }
 
@@ -1609,7 +1610,7 @@ tbm_sprd_bo_map(tbm_backend_bo_data *bo_data, tbm_bo_device_type device,
                TBM_ERR("Cannot get handle: gem:%d, device:%d, opt:%d\n",
                        bo_sprd->gem, device, opt);
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                return (tbm_bo_handle) NULL;
        }
 
@@ -1673,7 +1674,7 @@ tbm_sprd_bo_export_fd(tbm_backend_bo_data *bo_data, tbm_error_e *error)
                TBM_ERR("bo_sprd:%p Cannot dmabuf=%d (%s)\n",
                               bo_sprd, bo_sprd->gem, strerror_r(errno, buf, STRERR_BUFSIZE));
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                return (tbm_fd) ret;
        }
 
@@ -1777,6 +1778,7 @@ tbm_sprd_init(tbm_bufmgr bufmgr, tbm_error_e *error)
        tbm_backend_bo_func *bo_func = NULL;
        int fp;
        tbm_error_e err;
+       int set_master = 0;
 
        if (!bufmgr) {
                TBM_ERR("bufmgr is null.\n");
@@ -1793,36 +1795,54 @@ tbm_sprd_init(tbm_bufmgr bufmgr, tbm_error_e *error)
                return NULL;
        }
 
-       if (tbm_backend_bufmgr_query_display_server(bufmgr, &err)) {
-               bufmgr_sprd->fd = tbm_drm_helper_get_master_fd();
+       /* check the master_fd which already had opened */
+       bufmgr_sprd->fd = tbm_drm_helper_get_master_fd();
+       if (bufmgr_sprd->fd < 0) {
+               bufmgr_sprd->fd = _tbm_sprd_open_drm();
                if (bufmgr_sprd->fd < 0) {
-                       bufmgr_sprd->fd = _tbm_sprd_open_drm();
-                       if (bufmgr_sprd->fd < 0) {
-                               TBM_ERR("fail to open drm!\n");
-                               if (error)
-                                       *error = TBM_ERROR_OPERATION_FAILED;
-                               goto fail_open_drm;
-                       }
+                       TBM_ERR("fail to open drm!\n");
+                       if (error)
+                               *error = TBM_ERROR_INVALID_OPERATION;
+                       goto fail_open_drm;
                }
 
-               tbm_drm_helper_set_tbm_master_fd(bufmgr_sprd->fd);
+               if (drmIsMaster(bufmgr_sprd->fd)) {
+                       tbm_drm_helper_set_tbm_master_fd(bufmgr_sprd->fd);
+                       set_master = 1;
 
+                       bufmgr_sprd->device_name = drmGetDeviceNameFromFd(bufmgr_sprd->fd);
+                       if (!bufmgr_sprd->device_name) {
+                               TBM_ERR("fail to get device name!\n");
+                               tbm_drm_helper_unset_tbm_master_fd();
+                               if (error)
+                                       *error = TBM_ERROR_INVALID_OPERATION;
+                               goto fail_get_device_name;
+                       }
+                       TBM_INFO("This is Master FD(%d) from open_drm.", bufmgr_sprd->fd);
+               } else {
+                       /* close the fd and get the authenticated fd from the master fd */
+                       close(bufmgr_sprd->fd);
+                       bufmgr_sprd->fd = -1;
+
+                       /* get the authenticated drm fd from the master fd */
+                       if (!tbm_drm_helper_get_auth_info(&(bufmgr_sprd->fd), &(bufmgr_sprd->device_name), NULL)) {
+                               TBM_ERR("fail to get auth drm info!\n");
+                               if (error)
+                                       *error = TBM_ERROR_INVALID_OPERATION;
+                               goto fail_get_auth_info;
+                       }
+                       TBM_INFO("This is Authenticated FD(%d)", bufmgr_sprd->fd);
+               }
+       } else {
                bufmgr_sprd->device_name = drmGetDeviceNameFromFd(bufmgr_sprd->fd);
                if (!bufmgr_sprd->device_name) {
                        TBM_ERR("fail to get device name!\n");
                        tbm_drm_helper_unset_tbm_master_fd();
                        if (error)
-                               *error = TBM_ERROR_OPERATION_FAILED;
+                               *error = TBM_ERROR_INVALID_OPERATION;
                        goto fail_get_device_name;
                }
-       } else {
-               if (!tbm_drm_helper_get_auth_info(&(bufmgr_sprd->fd), &(bufmgr_sprd->device_name), NULL)) {
-                       TBM_ERR("fail to get auth drm info!\n");
-                       if (error)
-                               *error = TBM_ERROR_OPERATION_FAILED;
-                       goto fail_get_auth_info;
-               }
-
+               TBM_INFO("This is Master FD from tbm_drm_helper_get_master_fd(%d)", bufmgr_sprd->fd);
        }
        tbm_drm_helper_set_fd(bufmgr_sprd->fd);
 
@@ -1841,7 +1861,7 @@ tbm_sprd_init(tbm_bufmgr bufmgr, tbm_error_e *error)
        if (!_bufmgr_init_cache_state(bufmgr_sprd)) {
                TBM_ERR("fail to init bufmgr cache state\n");
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                goto fail_init_cache_state;
        }
 
@@ -1870,7 +1890,7 @@ tbm_sprd_init(tbm_bufmgr bufmgr, tbm_error_e *error)
        if (err != TBM_ERROR_NONE) {
                TBM_ERR("fail to register bufmgr_func! err(%d)\n", err);
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                goto fail_register_bufmgr_func;
        }
        bufmgr_sprd->bufmgr_func = bufmgr_func;
@@ -1899,7 +1919,7 @@ tbm_sprd_init(tbm_bufmgr bufmgr, tbm_error_e *error)
        if (err != TBM_ERROR_NONE) {
                TBM_ERR("fail to register bo_func! err(%d)\n", err);
                if (error)
-                       *error = TBM_ERROR_OPERATION_FAILED;
+                       *error = TBM_ERROR_INVALID_OPERATION;
                goto fail_register_bo_func;
        }
        bufmgr_sprd->bo_func = bo_func;
@@ -1925,7 +1945,7 @@ fail_alloc_bufmgr_func:
        if (bufmgr_sprd->hashBos)
                drmHashDestroy(bufmgr_sprd->hashBos);
 fail_init_cache_state:
-       if (tbm_backend_bufmgr_query_display_server(bufmgr, &err))
+       if (set_master)
                tbm_drm_helper_unset_tbm_master_fd();
        tbm_drm_helper_unset_fd();
        if (bufmgr_sprd->device_name)
@@ -1941,7 +1961,7 @@ fail_open_drm:
 tbm_backend_module tbm_backend_module_data = {
        "sprd",
        "Samsung",
-       TBM_BACKEND_ABI_VERSION_2_0,
+       TBM_BACKEND_ABI_VERSION_3_0,
        tbm_sprd_init,
        tbm_sprd_deinit
 };