tbm_bufmgr: change tbm_bo_alloc_with_format name
[platform/core/uifw/libtbm.git] / src / tbm_surface_internal.c
index 17aa777..8c6fc39 100644 (file)
@@ -57,6 +57,25 @@ void _tbm_surface_mutex_unlock(void);
        } \
 }
 
+#define TBM_SURFACE_RETURN_ERR_IF_FAIL(cond, error_type) {\
+       if (!(cond)) {\
+               TBM_ERR("'%s' failed.\n", #cond);\
+               _tbm_set_last_result(error_type);\
+               _tbm_surface_mutex_unlock();\
+               return;\
+       } \
+}
+
+#define TBM_SURFACE_RETURN_SET_ERR_IF_FAIL(cond, error, error_type) {\
+       if (!(cond)) {\
+               TBM_ERR("'%s' failed.\n", #cond);\
+               error = error_type;\
+               _tbm_set_last_result(error_type);\
+               _tbm_surface_mutex_unlock();\
+               return;\
+       } \
+}
+
 #define TBM_SURFACE_RETURN_VAL_IF_FAIL(cond, val) {\
        if (!(cond)) {\
                TBM_ERR("'%s' failed.\n", #cond);\
@@ -66,6 +85,26 @@ void _tbm_surface_mutex_unlock(void);
        } \
 }
 
+
+#define TBM_SURFACE_RETURN_VAL_ERR_IF_FAIL(cond, val, error_type) {\
+       if (!(cond)) {\
+               TBM_ERR("'%s' failed.\n", #cond);\
+               _tbm_set_last_result(error_type);\
+               _tbm_surface_mutex_unlock();\
+               return val;\
+       } \
+}
+
+#define TBM_SURFACE_RETURN_VAL_SET_ERR_IF_FAIL(cond, val, error, error_type) {\
+       if (!(cond)) {\
+               TBM_ERR("'%s' failed.\n", #cond);\
+               error = error_type;\
+               _tbm_set_last_result(error_type);\
+               _tbm_surface_mutex_unlock();\
+               return val;\
+       } \
+}
+
 /* LCOV_EXCL_START */
 static double
 _tbm_surface_internal_get_time(void)
@@ -281,7 +320,6 @@ _tbm_surface_internal_query_plane_data(tbm_surface_h surface,
 
        struct _tbm_surface *surf = (struct _tbm_surface *)surface;
        struct _tbm_bufmgr *bufmgr = surf->bufmgr;
-       int ret = 0;
        tbm_error_e error;
 
        TBM_RETURN_VAL_IF_FAIL(bufmgr != NULL, 0);
@@ -289,54 +327,13 @@ _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->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;
-               }
-
-               error = bufmgr->bufmgr_func->bufmgr_get_plane_data(bufmgr->bufmgr_data, surf->info.format, plane_idx,
-                                               surf->info.width, surf->info.height, size, offset, pitch, bo_idx);
-               if (error != TBM_ERROR_NONE) {
-                       /* LCOV_EXCL_START */
-                       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->surface_get_plane_data) {
-                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
-                       return 0;
-               }
-
-               ret = bufmgr->backend->surface_get_plane_data(surf->info.width,
-                               surf->info.height, surf->info.format, plane_idx, size, offset, pitch, bo_idx);
-               if (!ret) {
-                       /* LCOV_EXCL_START */
-                       TBM_ERR("Fail to surface_get_plane_data. surface(%p)\n", surface);
-                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                       return 0;
-                       /* LCOV_EXCL_STOP */
-               }
+       error = tbm_module_bufmgr_get_plane_data(bufmgr->module, surf->info.format, plane_idx, surf->info.width, surf->info.height, size, offset, pitch, bo_idx);
+       if (error != TBM_ERROR_NONE) {
+               _tbm_set_last_result(error);
+               return 0;
        }
 
-       return ret;
+       return 1;
 }
 
 static void
@@ -382,6 +379,11 @@ _tbm_surface_internal_destroy(tbm_surface_h surface)
        LIST_DEL(&surface->item_link);
        surface->magic = 0;
 
+       if (surface->hal_surface) {
+               hal_tbm_surface_free(surface->hal_surface);
+               surface->hal_surface = NULL;
+       }
+
        free(surface);
        surface = NULL;
 
@@ -591,6 +593,384 @@ _tbm_surface_internal_get_bpp(tbm_format format)
        return bpp;
 }
 
+static struct _tbm_surface *
+_tbm_surface_internal_create_surface(tbm_bufmgr bufmgr, int width, int height, int format, int flags, tbm_error_e *error)
+{
+       struct _tbm_surface *surf = NULL;
+       uint32_t size = 0, offset = 0, stride = 0, bo_size = 0;
+       int i, j, bo_idx;
+
+       surf = calloc(1, sizeof(struct _tbm_surface));
+       if (!surf) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("fail to alloc surf\n");
+               *error = TBM_ERROR_OUT_OF_MEMORY;
+               goto alloc_surf_fail;
+               /* LCOV_EXCL_STOP */
+       }
+
+       surf->magic = TBM_SURFACE_MAGIC;
+       surf->bufmgr = bufmgr;
+       surf->info.width = width;
+       surf->info.height = height;
+       surf->info.format = format;
+       surf->info.bpp = _tbm_surface_internal_get_bpp(format);
+       if (!surf->info.bpp) {
+               TBM_ERR("fail to get bpp from format(%d), error(%s)\n", format, tbm_error_str(*error));
+               *error = tbm_get_last_error();
+               goto bpp_fail;
+       }
+
+       surf->info.num_planes = _tbm_surface_internal_get_num_planes(format);
+       if (!surf->info.num_planes) {
+               TBM_ERR("fail to get num_planes from format(%d), error(%s)\n", format, tbm_error_str(*error));
+               *error = tbm_get_last_error();
+               goto num_planes_fail;
+       }
+       surf->refcnt = 1;
+
+       /* get size, stride and offset bo_idx */
+       for (i = 0; i < surf->info.num_planes; i++) {
+               if (!_tbm_surface_internal_query_plane_data(surf, i, &size, &offset, &stride, &bo_idx)) {
+                       TBM_ERR("fail to query plane data\n");
+                       *error = tbm_get_last_error();
+                       goto query_plane_data_fail;
+               }
+
+               surf->info.planes[i].size = size;
+               surf->info.planes[i].offset = offset;
+               surf->info.planes[i].stride = stride;
+               surf->planes_bo_idx[i] = bo_idx;
+       }
+
+       surf->num_bos = 1;
+
+       for (i = 0; i < surf->info.num_planes; i++) {
+               surf->info.size += surf->info.planes[i].size;
+
+               if (surf->num_bos < surf->planes_bo_idx[i] + 1)
+                       surf->num_bos = surf->planes_bo_idx[i] + 1;
+       }
+
+       surf->flags = flags;
+
+       for (i = 0; i < surf->num_bos; i++) {
+               bo_size = 0;
+               for (j = 0; j < surf->info.num_planes; j++) {
+                       if (surf->planes_bo_idx[j] == i)
+                               bo_size += surf->info.planes[j].size;
+               }
+
+               surf->bos[i] = tbm_bufmgr_internal_alloc_bo_with_format(bufmgr, format, i, width, height, surf->info.bpp/8, flags, error);
+               if (!surf->bos[i]) {
+                       surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
+                       if (!surf->bos[i]) {
+                               TBM_ERR("fail to alloc bo idx:%d\n", i);
+                               *error = tbm_get_last_error();
+                               goto alloc_bo_fail;
+                       }
+               }
+
+               _tbm_bo_set_surface(surf->bos[i], surf);
+       }
+
+       *error = TBM_ERROR_NONE;
+
+       return surf;
+
+alloc_bo_fail:
+       for (j = 0; j < i; j++) {
+               if (surf->bos[j])
+                       tbm_bo_unref(surf->bos[j]);
+       }
+query_plane_data_fail:
+bpp_fail:
+num_planes_fail:
+       free(surf);
+alloc_surf_fail:
+
+       return NULL;
+}
+
+static struct _tbm_surface *
+_tbm_surface_internal_hal_tbm_create_surface(tbm_bufmgr bufmgr, int width, int height, int format, int flags, tbm_error_e *error)
+{
+       struct _tbm_surface *surf = NULL;
+       uint32_t size = 0, offset = 0, stride = 0, bo_size = 0;
+       int i, j, bo_idx;
+       hal_tbm_surface *hal_surface = NULL;
+       hal_tbm_bo **hal_bos = NULL;
+       int num_bos = 0;
+
+       surf = calloc(1, sizeof(struct _tbm_surface));
+       if (!surf) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("fail to alloc surf");
+               *error = TBM_ERROR_OUT_OF_MEMORY;
+               goto alloc_surf_fail;
+               /* LCOV_EXCL_STOP */
+       }
+       surf->refcnt = 1;
+
+       surf->magic = TBM_SURFACE_MAGIC;
+       surf->bufmgr = bufmgr;
+       surf->info.width = width;
+       surf->info.height = height;
+       surf->info.format = format;
+       surf->info.bpp = _tbm_surface_internal_get_bpp(format);
+       if (!surf->info.bpp) {
+               TBM_ERR("fail to get bpp from format(%d), error(%s)", format, tbm_error_str(*error));
+               *error = tbm_get_last_error();
+               goto bpp_fail;
+       }
+       surf->flags = flags;
+
+       // get number of planes
+       surf->info.num_planes = _tbm_surface_internal_get_num_planes(format);
+       if (!surf->info.num_planes) {
+               TBM_ERR("fail to get num_planes from format(%d), error(%s)", format, tbm_error_str(*error));
+               *error = tbm_get_last_error();
+               goto num_planes_fail;
+       }
+
+       hal_surface = hal_tbm_bufmgr_alloc_surface(bufmgr->hal_bufmgr, (uint32_t)width, (uint32_t)height, (hal_tbm_format)format, (hal_tbm_bo_memory_type)flags, NULL, 0, (hal_tbm_error *)error);
+       if (hal_surface) {
+               // set hal_surface
+               surf->hal_surface = hal_surface;
+
+               // set infomation of planes
+               for (i = 0; i < surf->info.num_planes; i++) {
+                       *error = (tbm_error_e)hal_tbm_surface_get_plane_data(hal_surface, i, &size, &offset, &stride, &bo_idx);
+                       if (*error != TBM_ERROR_NONE) {
+                               goto query_plane_data_fail;
+                       }
+                       surf->info.planes[i].size = size;
+                       surf->info.planes[i].offset = offset;
+                       surf->info.planes[i].stride = stride;
+                       surf->planes_bo_idx[i] = bo_idx;
+               }
+
+               // set infomation of bos
+               hal_bos = hal_tbm_surface_get_bos(hal_surface, &num_bos, (hal_tbm_error *)error);
+               if (!hal_bos) {
+                       TBM_ERR("fail to get bos, error(%s)", tbm_error_str(*error));
+                       goto get_bos_fail;
+               }
+               surf->num_bos = num_bos;
+
+               for (i = 0; i < surf->info.num_planes; i++) {
+                       surf->info.size += surf->info.planes[i].size;
+
+                       if (surf->num_bos < surf->planes_bo_idx[i] + 1)
+                               surf->num_bos = surf->planes_bo_idx[i] + 1;
+               }
+
+               for (i = 0; i < num_bos; i++) {
+                       surf->bos[i] = tbm_bo_alloc_with_bo_data(bufmgr, (tbm_backend_bo_data *)hal_bos[i], flags);
+                       if (!surf->bos[i]) {
+                               TBM_ERR("fail to alloc bo idx:%d", i);
+                               *error = tbm_get_last_error();
+                               goto get_bo_fail;
+                       }
+
+                       _tbm_bo_set_surface(surf->bos[i], surf);
+               }
+       } else {
+               // set infomation of planes
+               for (i = 0; i < surf->info.num_planes; i++) {
+                       if (!_tbm_surface_internal_query_plane_data(surf, i, &size, &offset, &stride, &bo_idx)) {
+                               TBM_ERR("fail to query plane data");
+                               *error = tbm_get_last_error();
+                               goto query_plane_data_fail;
+                       }
+                       surf->info.planes[i].size = size;
+                       surf->info.planes[i].offset = offset;
+                       surf->info.planes[i].stride = stride;
+                       surf->planes_bo_idx[i] = bo_idx;
+               }
+
+               // count number of bos
+               surf->num_bos = 1;
+               for (i = 0; i < surf->info.num_planes; i++) {
+                       surf->info.size += surf->info.planes[i].size;
+
+                       if (surf->num_bos < surf->planes_bo_idx[i] + 1)
+                               surf->num_bos = surf->planes_bo_idx[i] + 1;
+               }
+
+               // set infomation of bos
+               for (i = 0; i < surf->num_bos; i++) {
+                       bo_size = 0;
+                       for (j = 0; j < surf->info.num_planes; j++) {
+                               if (surf->planes_bo_idx[j] == i)
+                                       bo_size += surf->info.planes[j].size;
+                       }
+
+                       surf->bos[i] = tbm_bufmgr_internal_alloc_bo_with_format(bufmgr, format, i, width, height, surf->info.bpp / 8, flags, error);
+                       if (*error == TBM_ERROR_NOT_SUPPORTED) {
+                               surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to alloc bo idx:%d", i);
+                                       *error = tbm_get_last_error();
+                                       goto get_bo_fail;
+                               }
+                       }
+
+                       _tbm_bo_set_surface(surf->bos[i], surf);
+               }
+       }
+
+       *error = TBM_ERROR_NONE;
+
+       return surf;
+
+get_bo_fail:
+get_bos_fail:
+query_plane_data_fail:
+       if (hal_surface) {
+               hal_tbm_surface_free(hal_surface);
+       } else {
+               for (j = 0; j < i; j++) {
+                       if (surf->bos[j])
+                               tbm_bo_unref(surf->bos[j]);
+               }
+       }
+num_planes_fail:
+bpp_fail:
+       if (surf)
+               free(surf);
+alloc_surf_fail:
+
+       return NULL;
+}
+
+static struct _tbm_surface *
+_tbm_surface_internal_hal_tbm_import_surface(tbm_bufmgr bufmgr, int width, int height, int format, tbm_surface_buffer_data *buffer_data, tbm_error_e *error)
+{
+       struct _tbm_surface *surf = NULL;
+       uint32_t size = 0, offset = 0, stride = 0;
+       int i, j, bo_idx;
+       hal_tbm_surface *hal_surface = NULL;
+       hal_tbm_bo **hal_bos = NULL;
+       int num_bos = 0;
+       int flags;
+
+       surf = calloc(1, sizeof(struct _tbm_surface));
+       if (!surf) {
+               /* LCOV_EXCL_START */
+               TBM_ERR("fail to alloc surf");
+               *error = TBM_ERROR_OUT_OF_MEMORY;
+               goto alloc_surf_fail;
+               /* LCOV_EXCL_STOP */
+       }
+       surf->refcnt = 1;
+
+       surf->magic = TBM_SURFACE_MAGIC;
+       surf->bufmgr = bufmgr;
+       surf->info.width = width;
+       surf->info.height = height;
+       surf->info.format = format;
+       surf->info.bpp = _tbm_surface_internal_get_bpp(format);
+       if (!surf->info.bpp) {
+               TBM_ERR("fail to get bpp from format(%d), error(%s)", format, tbm_error_str(*error));
+               *error = tbm_get_last_error();
+               goto bpp_fail;
+       }
+
+       // get number of planes
+       surf->info.num_planes = _tbm_surface_internal_get_num_planes(format);
+       if (!surf->info.num_planes) {
+               TBM_ERR("fail to get num_planes from format(%d), error(%s)", format, tbm_error_str(*error));
+               *error = tbm_get_last_error();
+               goto num_planes_fail;
+       }
+
+       // import surface
+       hal_surface = hal_tbm_bufmgr_import_surface(bufmgr->hal_bufmgr, 
+                                                                                       (uint32_t)width,
+                                                                                       (uint32_t)height,
+                                                                                       (hal_tbm_format)format,
+                                                                                       (hal_tbm_surface_buffer_data *)buffer_data,
+                                                                                       (hal_tbm_error *)error);
+       if (!hal_surface) {
+               TBM_ERR("hal_tbm_bufmgr_import_surface failed.(width:%d height:%d format:%d error:%s)",
+                               width, height, format, tbm_error_str(*error));
+               goto import_surface_fail;
+       }
+       surf->hal_surface = hal_surface;
+
+       // set infomation of planes
+       for (i = 0; i < surf->info.num_planes; i++) {
+               *error = (tbm_error_e)hal_tbm_surface_get_plane_data(hal_surface, i, &size, &offset, &stride, &bo_idx);
+               if (*error != TBM_ERROR_NONE) {
+                       goto query_plane_data_fail;
+               }
+               surf->info.planes[i].size = size;
+               surf->info.planes[i].offset = offset;
+               surf->info.planes[i].stride = stride;
+               surf->planes_bo_idx[i] = bo_idx;
+       }
+
+       // set infomation of bos
+       hal_bos = hal_tbm_surface_get_bos(hal_surface, &num_bos, (hal_tbm_error *)error);
+       if (!hal_bos) {
+               TBM_ERR("fail to get bos, error(%s)", tbm_error_str(*error));
+               goto get_bos_fail;
+       }
+       surf->num_bos = num_bos;
+
+       for (i = 0; i < surf->info.num_planes; i++) {
+               surf->info.size += surf->info.planes[i].size;
+
+               if (surf->num_bos < surf->planes_bo_idx[i] + 1)
+                       surf->num_bos = surf->planes_bo_idx[i] + 1;
+       }
+
+       // get memory_types(bo flags)
+       flags =  (int)hal_tbm_bo_get_memory_types(hal_bos[0], (hal_tbm_error *)error);
+       if (*error != TBM_ERROR_NONE) {
+               TBM_ERR("hal_tbm_bo_get_memory_types failed.");
+               goto get_memory_types_fail;
+       }
+       surf->flags = flags;
+
+       for (i = 0; i < num_bos; i++) {
+               surf->bos[i] = tbm_bo_alloc_with_bo_data(bufmgr, (tbm_backend_bo_data *)hal_bos[i], flags);
+               if (!surf->bos[i]) {
+                       TBM_ERR("fail to alloc bo idx:%d", i);
+                       *error = tbm_get_last_error();
+                       goto get_bo_fail;
+               }
+
+               _tbm_bo_set_surface(surf->bos[i], surf);
+       }
+
+       *error = TBM_ERROR_NONE;
+
+       return surf;
+
+get_bo_fail:
+get_memory_types_fail:
+get_bos_fail:
+query_plane_data_fail:
+       if (hal_surface) {
+               hal_tbm_surface_free(hal_surface);
+       } else {
+               for (j = 0; j < i; j++) {
+                       if (surf->bos[j])
+                               tbm_bo_unref(surf->bos[j]);
+               }
+       }
+import_surface_fail:
+num_planes_fail:
+bpp_fail:
+       if (surf)
+               free(surf);
+alloc_surf_fail:
+
+       return NULL;
+}
+
 int
 tbm_surface_internal_is_valid(tbm_surface_h surface)
 {
@@ -618,7 +998,6 @@ tbm_surface_internal_query_supported_formats(uint32_t **formats,
                uint32_t *num)
 {
        struct _tbm_bufmgr *bufmgr;
-       int ret = 0;
        bool bufmgr_initialized = false;
        tbm_error_e error;
 
@@ -641,46 +1020,10 @@ tbm_surface_internal_query_supported_formats(uint32_t **formats,
 
        bufmgr = g_surface_bufmgr;
 
-       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 */
-               ret = 1;
-       } else if (bufmgr->backend_module_data) {
-               if (!bufmgr->bufmgr_func->bufmgr_get_supported_formats) {
-                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
-                       goto fail;
-               }
-
-               error = bufmgr->bufmgr_func->bufmgr_get_supported_formats(bufmgr->bufmgr_data, formats, num);
-               if (error != TBM_ERROR_NONE) {
-                       /* LCOV_EXCL_START */
-                       TBM_ERR("Fail to surface_supported_format. error(%d)\n", error);
-                       goto fail;
-                       /* LCOV_EXCL_START */
-               }
-               ret = 1;
-       } else {
-               if (!bufmgr->backend->surface_supported_format) {
-                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
-                       goto fail;
-               }
-
-               ret = bufmgr->backend->surface_supported_format(formats, num);
-               if (!ret)  {
-                       /* LCOV_EXCL_START */
-                       TBM_ERR("Fail to surface_supported_format.\n");
-                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
-                       goto fail;
-                       /* LCOV_EXCL_START */
-               }
+       error = tbm_module_bufmgr_get_supported_formats(bufmgr->module, formats, num);
+       if (error != TBM_ERROR_NONE) {
+               _tbm_set_last_result(error);
+               goto fail;
        }
 
        TBM_TRACE_SURFACE_INTERNAL("tbm_bufmgr(%p) format num(%u)\n", g_surface_bufmgr, *num);
@@ -692,7 +1035,7 @@ tbm_surface_internal_query_supported_formats(uint32_t **formats,
 
        _tbm_surface_mutex_unlock();
 
-       return ret;
+       return 1;
 
 /* LCOV_EXCL_START */
 fail:
@@ -758,14 +1101,8 @@ tbm_surface_internal_create_with_flags(int width, int height,
 {
        struct _tbm_bufmgr *bufmgr;
        struct _tbm_surface *surf = NULL;
-       uint32_t size = 0;
-       uint32_t offset = 0;
-       uint32_t stride = 0;
-       uint32_t bo_size = 0;
-       int bo_idx;
-       int i, j;
+       tbm_error_e error = TBM_ERROR_INVALID_OPERATION;
        bool bufmgr_initialized = false;
-       tbm_error_e error;
 
        _tbm_surface_mutex_lock();
        _tbm_set_last_result(TBM_ERROR_NONE);
@@ -777,7 +1114,7 @@ tbm_surface_internal_create_with_flags(int width, int height,
                _init_surface_bufmgr();
                if (!g_surface_bufmgr) {
                        TBM_ERR("fail bufmgr initialization\n");
-                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
+                       error = TBM_ERROR_INVALID_OPERATION;
                        goto check_valid_fail;
                }
                LIST_INITHEAD(&g_surface_bufmgr->surf_list);
@@ -787,124 +1124,26 @@ tbm_surface_internal_create_with_flags(int width, int height,
        bufmgr = g_surface_bufmgr;
        if (!TBM_BUFMGR_IS_VALID(bufmgr)) {
                TBM_ERR("The bufmgr is invalid\n");
-               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
+               error = TBM_ERROR_INVALID_PARAMETER;
                goto check_valid_fail;
        }
 
-       surf = calloc(1, sizeof(struct _tbm_surface));
-       if (!surf) {
-               /* LCOV_EXCL_START */
-               TBM_ERR("fail to alloc surf\n");
-               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
-               goto alloc_surf_fail;
-               /* LCOV_EXCL_STOP */
-       }
-
-       surf->magic = TBM_SURFACE_MAGIC;
-       surf->bufmgr = bufmgr;
-       surf->info.width = width;
-       surf->info.height = height;
-       surf->info.format = format;
-       surf->info.bpp = _tbm_surface_internal_get_bpp(format);
-       if (!surf->info.bpp) {
-               TBM_ERR("fail to get bpp. error(%s)\n", tbm_error_str(tbm_get_last_error()));
-               goto bpp_fail;
-       }
-       surf->info.num_planes = _tbm_surface_internal_get_num_planes(format);
-       if (!surf->info.num_planes) {
-               TBM_ERR("fail to get num_planes. error(%s)\n", tbm_error_str(tbm_get_last_error()));
-               goto num_planes_fail;
-       }
-       surf->refcnt = 1;
-
-       /* get size, stride and offset bo_idx */
-       for (i = 0; i < surf->info.num_planes; i++) {
-               if (!_tbm_surface_internal_query_plane_data(surf, i, &size,
-                                               &offset, &stride, &bo_idx)) {
-                       TBM_ERR("fail to query plane data\n");
-                       goto query_plane_data_fail;
-               }
-
-               surf->info.planes[i].size = size;
-               surf->info.planes[i].offset = offset;
-               surf->info.planes[i].stride = stride;
-               surf->planes_bo_idx[i] = bo_idx;
-       }
-
-       surf->num_bos = 1;
-
-       for (i = 0; i < surf->info.num_planes; i++) {
-               surf->info.size += surf->info.planes[i].size;
-
-               if (surf->num_bos < surf->planes_bo_idx[i] + 1)
-                       surf->num_bos = surf->planes_bo_idx[i] + 1;
-       }
-
-       surf->flags = flags;
-
-       for (i = 0; i < surf->num_bos; i++) {
-               bo_size = 0;
-               for (j = 0; j < surf->info.num_planes; j++) {
-                       if (surf->planes_bo_idx[j] == i)
-                               bo_size += surf->info.planes[j].size;
+       if (bufmgr->use_hal_tbm) {
+               surf = _tbm_surface_internal_hal_tbm_create_surface(bufmgr, width, height, format, flags, &error);
+               if (!surf) {
+                       TBM_ERR("_tbm_surface_internal_hal_tbm_create_surface failed.");
+                       goto surface_alloc_fail;
                }
-
-               if (bufmgr->use_hal_tbm) {
-                       surf->bos[i] = tbm_bo_alloc_with_format(bufmgr, format, i, width, height, surf->info.bpp/8, flags, &error);
-                       if (error == TBM_ERROR_NOT_SUPPORTED) {
-                               surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
-                               if (!surf->bos[i]) {
-                                       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, surf->info.bpp/8, flags, &error);
-                               if (!surf->bos[i]) {
-                                       TBM_ERR("fail to tbm_bo_alloc_with_format idx:%d\n", i);
-                                       goto alloc_bo_fail;
-                               }
-                               /* 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;
-                               }
-                               /* LCOV_EXCL_STOP */
-                       } else {
-                               surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
-                               if (!surf->bos[i]) {
-                                       TBM_ERR("fail to alloc bo idx:%d\n", i);
-                                       goto alloc_bo_fail;
-                               }
-                       }
-               } else {
-                       if (bufmgr->backend->surface_bo_alloc) {
-                               /* LCOV_EXCL_START */
-                               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;
-                               /* LCOV_EXCL_STOP */
-                               }
-                       } else {
-                               surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
-                               if (!surf->bos[i]) {
-                                       TBM_ERR("fail to alloc bo idx:%d\n", i);
-                                       goto alloc_bo_fail;
-                               }
-                       }
+       } else {
+               surf = _tbm_surface_internal_create_surface(bufmgr, width, height, format, flags, &error);
+               if (!surf) {
+                       TBM_ERR("_tbm_surface_internal_create_surface failed.");
+                       goto surface_alloc_fail;
                }
-
-               _tbm_bo_set_surface(surf->bos[i], surf);
        }
 
-       TBM_TRACE_SURFACE_INTERNAL("width(%d) height(%d) format(%s) flags(%d) tbm_surface(%p)\n", width, height,
-                       _tbm_surface_internal_format_to_str(format), flags, surf);
+       TBM_TRACE_SURFACE_INTERNAL("width(%d) height(%d) format(%s) flags(%d) tbm_surface(%p)\n",
+                       width, height, _tbm_surface_internal_format_to_str(format), flags, surf);
 
        LIST_INITHEAD(&surf->user_data_list);
        LIST_INITHEAD(&surf->debug_data_list);
@@ -912,31 +1151,26 @@ tbm_surface_internal_create_with_flags(int width, int height,
 
        LIST_ADD(&surf->item_link, &bufmgr->surf_list);
 
+       _tbm_set_last_result(error);
        _tbm_surface_mutex_unlock();
 
        return surf;
 
 /* LCOV_EXCL_START */
-alloc_bo_fail:
-       for (j = 0; j < i; j++) {
-               if (surf->bos[j])
-                       tbm_bo_unref(surf->bos[j]);
-       }
-query_plane_data_fail:
-bpp_fail:
-num_planes_fail:
-       free(surf);
-alloc_surf_fail:
+
+surface_alloc_fail:
 check_valid_fail:
        if (bufmgr_initialized && bufmgr) {
                LIST_DELINIT(&bufmgr->surf_list);
                _deinit_surface_bufmgr();
        }
-       _tbm_surface_mutex_unlock();
 
        TBM_ERR("error: width(%d) height(%d) format(%s) flags(%d)\n",
-                       width, height,
-                       _tbm_surface_internal_format_to_str(format), flags);
+               width, height, _tbm_surface_internal_format_to_str(format), flags);
+
+       _tbm_set_last_result(error);
+       _tbm_surface_mutex_unlock();
+
 /* LCOV_EXCL_STOP */
 
        return NULL;
@@ -1109,6 +1343,8 @@ tbm_surface_internal_destroy(tbm_surface_h surface)
 
        if (surface->refcnt == 0)
                _tbm_surface_internal_destroy(surface);
+       else // if (surface->refcnt < 0)
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
 
        _tbm_surface_mutex_unlock();
 }
@@ -2708,4 +2944,84 @@ tbm_surface_internal_remove_destroy_handler(tbm_surface_h surface, tbm_surface_i
 
        _tbm_surface_mutex_unlock();
 }
+
+tbm_surface_buffer_data *
+tbm_surface_internal_export(tbm_surface_h surface, tbm_error_e *error)
+{
+       tbm_surface_buffer_data *buffer_data = NULL;
+       struct _tbm_surface *surf;
+       struct _tbm_bufmgr *bufmgr;
+
+       _tbm_surface_mutex_lock();
+
+       surf = (struct _tbm_surface *)surface;
+       TBM_SURFACE_RETURN_VAL_SET_ERR_IF_FAIL(_tbm_surface_internal_is_valid(surface), NULL, *error, TBM_ERROR_INVALID_PARAMETER);
+
+       bufmgr = surf->bufmgr;
+       TBM_SURFACE_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr != NULL, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
+
+       // this function supports when it comes to be use_hal.
+       TBM_SURFACE_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr->use_hal_tbm, NULL, *error, TBM_ERROR_NOT_SUPPORTED);
+
+       // export a surface
+       buffer_data = (tbm_surface_buffer_data *)hal_tbm_surface_export((hal_tbm_surface *)surf->hal_surface,
+                                                                                                                                       (hal_tbm_error *)error);
+       TBM_SURFACE_RETURN_VAL_ERR_IF_FAIL(buffer_data != NULL, NULL, *error);
+
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) buffer_data(%p)", surface, buffer_data);
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       _tbm_set_last_result(TBM_ERROR_NONE);
+       _tbm_surface_mutex_unlock();
+
+       return buffer_data;
+}
+
+tbm_surface_h
+tbm_surface_internal_import(tbm_surface_info_s *surface_info, tbm_surface_buffer_data *buffer_data, tbm_error_e *error)
+{
+       struct _tbm_surface *surf;
+       struct _tbm_bufmgr *bufmgr;
+
+       _tbm_surface_mutex_lock();
+
+       bufmgr = g_surface_bufmgr;
+       TBM_SURFACE_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr != NULL, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
+
+       // this function supports when it comes to be use_hal.
+       TBM_SURFACE_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr->use_hal_tbm, NULL, *error, TBM_ERROR_NOT_SUPPORTED);
+
+       TBM_SURFACE_RETURN_VAL_SET_ERR_IF_FAIL(surface_info != NULL, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
+       TBM_SURFACE_RETURN_VAL_SET_ERR_IF_FAIL(surface_info->width > 0, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
+       TBM_SURFACE_RETURN_VAL_SET_ERR_IF_FAIL(surface_info->height > 0, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
+       TBM_SURFACE_RETURN_VAL_SET_ERR_IF_FAIL(buffer_data != NULL, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
+
+       // import a surface
+       surf = _tbm_surface_internal_hal_tbm_import_surface(bufmgr,
+                                                                                                       (int)surface_info->width,
+                                                                                                       (int)surface_info->height,
+                                                                                                       (int)surface_info->format,
+                                                                                                       buffer_data,
+                                                                                                       error);
+       TBM_SURFACE_RETURN_VAL_ERR_IF_FAIL(surf != NULL, NULL, *error);
+
+       LIST_INITHEAD(&surf->user_data_list);
+       LIST_INITHEAD(&surf->debug_data_list);
+       LIST_INITHEAD(&surf->destroy_funcs);
+
+       LIST_ADD(&surf->item_link, &bufmgr->surf_list);
+
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p)", surf);
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       _tbm_set_last_result(TBM_ERROR_NONE);
+       _tbm_surface_mutex_unlock();
+
+       return (tbm_surface_h)surf;
+}
+
 /*LCOV_EXCL_STOP*/