adapt hal-api-tbm
[platform/core/uifw/libtbm.git] / src / tbm_surface_internal.c
index a682fd1..0e6f1f5 100644 (file)
@@ -47,12 +47,6 @@ static tbm_bufmgr g_surface_bufmgr;
 static pthread_mutex_t tbm_surface_lock = PTHREAD_MUTEX_INITIALIZER;
 void _tbm_surface_mutex_unlock(void);
 
-#define C(b, m)              (((b) >> (m)) & 0xFF)
-#define B(c, s)              ((((unsigned int)(c)) & 0xff) << (s))
-#define FOURCC(a, b, c, d)     (B(d, 24) | B(c, 16) | B(b, 8) | B(a, 0))
-#define FOURCC_STR(id)      C(id, 0), C(id, 8), C(id, 16), C(id, 24)
-#define FOURCC_ID(str)      FOURCC(((char*)str)[0], ((char*)str)[1], ((char*)str)[2], ((char*)str)[3])
-
 /* check condition */
 #define TBM_SURFACE_RETURN_IF_FAIL(cond) {\
        if (!(cond)) {\
@@ -295,7 +289,21 @@ _tbm_surface_internal_query_plane_data(tbm_surface_h surface,
        TBM_RETURN_VAL_IF_FAIL(surf->info.height > 0, 0);
        TBM_RETURN_VAL_IF_FAIL(surf->info.format > 0, 0);
 
-       if (bufmgr->backend_module_data) {
+       if (bufmgr->use_hal_tbm) {
+               error = (tbm_error_e)hal_tbm_bufmgr_get_plane_data(bufmgr->hal_bufmgr, (hal_tbm_format)surf->info.format,
+                                                               plane_idx, surf->info.width, surf->info.height, size, offset, pitch, bo_idx);
+               /* LCOV_EXCL_START */
+               if (error == TBM_ERROR_NOT_SUPPORTED) {
+                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
+                       return 0;
+               } else if (error != TBM_ERROR_NONE) {
+                       TBM_ERR("Fail to surface_get_plane_data. surface(%p) error(%d)\n", surface, error);
+                       _tbm_set_last_result(error);
+                       return 0;
+               }
+               /* LCOV_EXCL_STOP */
+               ret = 1;
+       } else if (bufmgr->backend_module_data) {
                if (!bufmgr->bufmgr_func->bufmgr_get_plane_data) {
                        _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
                        return 0;
@@ -616,7 +624,18 @@ tbm_surface_internal_query_supported_formats(uint32_t **formats,
 
        bufmgr = g_surface_bufmgr;
 
-       if (bufmgr->backend_module_data) {
+       if (bufmgr->use_hal_tbm) {
+               error = (tbm_error_e)hal_tbm_bufmgr_get_supported_formats(bufmgr->hal_bufmgr, formats, num);
+               /* LCOV_EXCL_START */
+               if (error == TBM_ERROR_NOT_SUPPORTED) {
+                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
+                       goto fail;
+               } else if (error != TBM_ERROR_NONE) {
+                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
+                       goto fail;
+               }
+               /* LCOV_EXCL_STOP */
+       } else if (bufmgr->backend_module_data) {
                if (!bufmgr->bufmgr_func->bufmgr_get_supported_formats) {
                        _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
                        goto fail;
@@ -728,9 +747,6 @@ tbm_surface_internal_create_with_flags(int width, int height,
        int bo_idx;
        int i, j;
        bool bufmgr_initialized = false;
-       tbm_bo bo = NULL;
-       void *bo_priv = NULL;
-       tbm_backend_bo_data *bo_data = NULL;
        tbm_error_e error;
 
        _tbm_surface_mutex_lock();
@@ -809,73 +825,46 @@ tbm_surface_internal_create_with_flags(int width, int height,
                                bo_size += surf->info.planes[j].size;
                }
 
-               if (bufmgr->backend_module_data) {
-                       if (bufmgr->bufmgr_func->bufmgr_alloc_bo_with_format) {
-                               /* LCOV_EXCL_START */
-                               bo = calloc(1, sizeof(struct _tbm_bo));
-                               if (!bo) {
-                                       TBM_ERR("fail to alloc bo struct\n");
-                                       _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
-                                       goto alloc_bo_fail;
+               if (bufmgr->use_hal_tbm) {
+                       surf->bos[i] = tbm_bo_alloc_with_format(bufmgr, format, i, width, height, flags, &error);
+                       if (error == TBM_ERROR_NOT_SUPPORTED) {
+                               if (flags & TBM_BO_TILED) {
+                                       surf->bos[i] = tbm_bo_alloc_with_tiled_format(bufmgr, width, height, surf->info.bpp/8, format, flags, i, &error);
+                                       if (error == TBM_ERROR_NOT_SUPPORTED) {
+                                               surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
+                                       } else if (error != TBM_ERROR_NONE) {
+                                               TBM_ERR("fail to alloc bo idx:%d\n", i);
+                                               goto alloc_bo_fail;
+                                       }
+                               } else {
+                                       surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
                                }
 
-                               bo->bufmgr = surf->bufmgr;
-
-                               _tbm_bufmgr_mutex_lock();
-
-                               bo_data = bufmgr->bufmgr_func->bufmgr_alloc_bo_with_format(bufmgr->bufmgr_data, format, i,
-                                                                                                                               width, height, flags, &error);
-                               if (!bo_data) {
-                                       TBM_ERR("fail to alloc bo priv. error(%d)\n", error);
-                                       _tbm_set_last_result(error);
-                                       free(bo);
-                                       _tbm_bufmgr_mutex_unlock();
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to alloc bo idx:%d\n", i);
                                        goto alloc_bo_fail;
                                }
-                               bo->bo_data = bo_data;
-
-                               bo->ref_cnt = 1;
-                               bo->flags = flags;
-                               LIST_INITHEAD(&bo->user_data_list);
-
-                               LIST_ADD(&bo->item_link, &surf->bufmgr->bo_list);
-
-                               _tbm_bufmgr_mutex_unlock();
-
-                               surf->bos[i] = bo;
-                               /* LCOV_EXCL_STOP */
-                       } else if (bufmgr->bufmgr_func->bufmgr_alloc_bo_with_tiled_format && (flags & TBM_BO_TILED)) {
-                               bo = calloc(1, sizeof(struct _tbm_bo));
-                               if (!bo) {
-                                       TBM_ERR("fail to alloc bo struct\n");
-                                       _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+                       } else if (error != TBM_ERROR_NONE) {
+                               TBM_ERR("fail to alloc bo idx:%d\n", i);
+                               goto alloc_bo_fail;
+                       }
+               } else if (bufmgr->backend_module_data) {
+                       if (bufmgr->bufmgr_func->bufmgr_alloc_bo_with_format) {
+                               /* LCOV_EXCL_START */
+                               surf->bos[i] = tbm_bo_alloc_with_format(bufmgr, format, i, width, height, flags, &error);
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to tbm_bo_alloc_with_format idx:%d\n", i);
                                        goto alloc_bo_fail;
                                }
-
-                               bo->bufmgr = surf->bufmgr;
-
-                               _tbm_bufmgr_mutex_lock();
-
-                               bo_data = bufmgr->bufmgr_func->bufmgr_alloc_bo_with_tiled_format(bufmgr->bufmgr_data, width, height, surf->info.bpp/8, format, flags, i, &error);
-                               if (!bo_data) {
-                                       TBM_ERR("fail to alloc bo priv. error(%d)\n", error);
-                                       _tbm_set_last_result(error);
-                                       free(bo);
-                                       _tbm_bufmgr_mutex_unlock();
+                               /* LCOV_EXCL_STOP */
+                       } else if (bufmgr->bufmgr_func->bufmgr_alloc_bo_with_tiled_format && (flags & TBM_BO_TILED)) {
+                               /* LCOV_EXCL_START */
+                               surf->bos[i] = tbm_bo_alloc_with_tiled_format(bufmgr, width, height, surf->info.bpp/8, format, flags, i, &error);
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to tbm_bo_alloc_with_tiled_format idx:%d\n", i);
                                        goto alloc_bo_fail;
                                }
-                               bo->bo_data = bo_data;
-
-                               bo->ref_cnt = 1;
-                               bo->flags = flags;
-                               LIST_INITHEAD(&bo->user_data_list);
-
-                               LIST_ADD(&bo->item_link, &surf->bufmgr->bo_list);
-
-                               _tbm_bufmgr_mutex_unlock();
-
-                               surf->bos[i] = bo;
-
+                               /* LCOV_EXCL_STOP */
                        } else {
                                surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
                                if (!surf->bos[i]) {
@@ -886,37 +875,12 @@ tbm_surface_internal_create_with_flags(int width, int height,
                } else {
                        if (bufmgr->backend->surface_bo_alloc) {
                                /* LCOV_EXCL_START */
-                               bo = calloc(1, sizeof(struct _tbm_bo));
-                               if (!bo) {
-                                       TBM_ERR("fail to alloc bo struct\n");
-                                       _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
-                                       goto alloc_bo_fail;
-                               }
-
-                               bo->bufmgr = surf->bufmgr;
-
-                               _tbm_bufmgr_mutex_lock();
-
-                               bo_priv = bufmgr->backend->surface_bo_alloc(bo, width, height, format, flags, i);
-                               if (!bo_priv) {
-                                       TBM_ERR("fail to alloc bo priv\n");
-                                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                                       free(bo);
-                                       _tbm_bufmgr_mutex_unlock();
+                               surf->bos[i] = tbm_bo_alloc_with_surface(bufmgr, width, height, format, flags, i);
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to tbm_bo_alloc_with_surface idx:%d\n", i);
                                        goto alloc_bo_fail;
-                               }
-                               bo->priv = bo_priv;
-
-                               bo->ref_cnt = 1;
-                               bo->flags = flags;
-                               LIST_INITHEAD(&bo->user_data_list);
-
-                               LIST_ADD(&bo->item_link, &surf->bufmgr->bo_list);
-
-                               _tbm_bufmgr_mutex_unlock();
-
-                               surf->bos[i] = bo;
                                /* LCOV_EXCL_STOP */
+                               }
                        } else {
                                surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
                                if (!surf->bos[i]) {
@@ -1631,11 +1595,44 @@ _tbm_surface_internal_debug_data_create(char *key, char *value)
        return debug_data;
 }
 
+static void
+_tbm_surface_internal_debug_data_value_update(tbm_surface_debug_data *debug_data, char *value)
+{
+       if (!debug_data->value && !value)
+               return;
+
+       if (debug_data->value && value && !strncmp(debug_data->value, value, strlen(debug_data->value)))
+               return;
+
+       if (debug_data->value)
+               free(debug_data->value);
+
+       if (value)
+               debug_data->value = strdup(value);
+       else
+               debug_data->value = NULL;
+}
+
+static tbm_surface_debug_data *
+_tbm_surface_internal_debug_data_find(struct list_head *list, char *key)
+{
+       tbm_surface_debug_data *debug_data = NULL;
+
+       if (LIST_IS_EMPTY(list))
+               return NULL;
+
+       LIST_FOR_EACH_ENTRY(debug_data, list, item_link) {
+               if (!strncmp(debug_data->key, key, strlen(debug_data->key)))
+                       return debug_data;
+       }
+
+       return NULL;
+}
+
 int
 tbm_surface_internal_set_debug_data(tbm_surface_h surface, char *key, char *value)
 {
        tbm_surface_debug_data *debug_data = NULL;
-       tbm_surface_debug_data *old_data = NULL, *tmp = NULL;
        tbm_bufmgr bufmgr = NULL;
 
        _tbm_surface_mutex_lock();
@@ -1648,53 +1645,30 @@ tbm_surface_internal_set_debug_data(tbm_surface_h surface, char *key, char *valu
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(bufmgr, 0);
 
-       if (!LIST_IS_EMPTY(&surface->debug_data_list)) {
-               LIST_FOR_EACH_ENTRY(old_data, &surface->debug_data_list, item_link) {
-                       if (old_data) {
-                               if (!strcmp(old_data->key, key)) {
-                                       if (old_data->value && value && !strncmp(old_data->value, value, strlen(old_data->value))) {
-                                               TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) Already exist key(%s) and value(%s)!\n", surface, key, value);
-                                               goto add_debug_key_list;
-                                       }
-
-                                       if (old_data->value)
-                                               free(old_data->value);
-
-                                       if (value)
-                                               old_data->value = strdup(value);
-                                       else
-                                               old_data->value = NULL;
-
-                                       goto add_debug_key_list;
-                               }
-                       }
+       debug_data = _tbm_surface_internal_debug_data_find(&surface->debug_data_list, key);
+       if (debug_data) {
+               _tbm_surface_internal_debug_data_value_update(debug_data, value);
+       } else {
+               debug_data = _tbm_surface_internal_debug_data_create(key, value);
+               if (!debug_data) {
+                       TBM_ERR("error: tbm_surface(%p) key(%s) value(%s)\n", surface, key, value);
+                       _tbm_surface_mutex_unlock();
+                       return 0;
                }
+
+               LIST_ADD(&debug_data->item_link, &surface->debug_data_list);
        }
 
-       debug_data = _tbm_surface_internal_debug_data_create(key, value);
+       /* add new debug key to list */
+       debug_data = _tbm_surface_internal_debug_data_find(&bufmgr->debug_key_list, key);
        if (!debug_data) {
-               TBM_ERR("error: tbm_surface(%p) key(%s) value(%s)\n", surface, key, value);
-               _tbm_surface_mutex_unlock();
-               return 0;
+               debug_data = _tbm_surface_internal_debug_data_create(key, NULL);
+               if (debug_data)
+                       LIST_ADDTAIL(&debug_data->item_link, &bufmgr->debug_key_list);
        }
 
        TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) key(%s) value(%s)\n", surface, key, value);
 
-       LIST_ADD(&debug_data->item_link, &surface->debug_data_list);
-
-add_debug_key_list:
-       if (!LIST_IS_EMPTY(&bufmgr->debug_key_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(old_data, tmp, &bufmgr->debug_key_list, item_link) {
-                       if (!strcmp(old_data->key, key)) {
-                               _tbm_surface_mutex_unlock();
-                               return 1;
-                       }
-               }
-       }
-
-       debug_data = _tbm_surface_internal_debug_data_create(key, NULL);
-       LIST_ADDTAIL(&debug_data->item_link, &bufmgr->debug_key_list);
-
        _tbm_surface_mutex_unlock();
 
        return 1;