X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Ftbm_surface_internal.c;h=5089c60ef11bfe9f598802e907f56e165fd80dde;hb=86bbaa419184bd0a189896b347a4fcbd9c894c54;hp=ade66ef2f569abb83dbb7fc6069e6c3b25914949;hpb=b444dc75227261801e32b326490d9c7197dfbf30;p=platform%2Fcore%2Fuifw%2Flibtbm.git diff --git a/src/tbm_surface_internal.c b/src/tbm_surface_internal.c index ade66ef..5089c60 100644 --- a/src/tbm_surface_internal.c +++ b/src/tbm_surface_internal.c @@ -41,20 +41,36 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include +#define TBM_SURFACE_MAGIC 0xBF021234 + static tbm_bufmgr g_surface_bufmgr; -static pthread_mutex_t tbm_surface_lock; +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)) {\ TBM_ERR("'%s' failed.\n", #cond);\ + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);\ + _tbm_surface_mutex_unlock();\ + return;\ + } \ +} + +#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;\ } \ @@ -63,6 +79,28 @@ void _tbm_surface_mutex_unlock(void); #define TBM_SURFACE_RETURN_VAL_IF_FAIL(cond, val) {\ if (!(cond)) {\ TBM_ERR("'%s' failed.\n", #cond);\ + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);\ + _tbm_surface_mutex_unlock();\ + return val;\ + } \ +} + + +#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);\ + if (error)\ + *error = error_type;\ + _tbm_set_last_result(error_type);\ _tbm_surface_mutex_unlock();\ return val;\ } \ @@ -218,32 +256,9 @@ _tbm_surface_internal_format_to_str(tbm_format format) } } -static bool -_tbm_surface_mutex_init(void) -{ - static bool tbm_surface_mutex_init = false; - - if (tbm_surface_mutex_init) - return true; - - if (pthread_mutex_init(&tbm_surface_lock, NULL)) { - TBM_ERR("fail: pthread_mutex_init for tbm_surface_lock.\n"); - return false; - } - - tbm_surface_mutex_init = true; - - return true; -} - void _tbm_surface_mutex_lock(void) { - if (!_tbm_surface_mutex_init()) { - TBM_ERR("fail: _tbm_surface_mutex_init.\n"); - return; - } - pthread_mutex_lock(&tbm_surface_lock); } @@ -271,25 +286,30 @@ _deinit_surface_bufmgr(void) /* LCOV_EXCL_STOP */ static int -_tbm_surface_internal_is_valid(tbm_surface_h surface) +_tbm_surface_internal_magic_check(tbm_surface_h surface) { - tbm_surface_h old_data = NULL; + if (surface->magic != TBM_SURFACE_MAGIC) + return 0; - TBM_RETURN_VAL_IF_FAIL(g_surface_bufmgr, 0); - TBM_RETURN_VAL_IF_FAIL(surface, 0); + return 1; +} - if (!LIST_IS_EMPTY(&g_surface_bufmgr->surf_list)) { - LIST_FOR_EACH_ENTRY(old_data, &g_surface_bufmgr->surf_list, item_link) { - if (old_data == surface) { - TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p)\n", surface); - return 1; - } - } +static int +_tbm_surface_internal_is_valid(tbm_surface_h surface) +{ + if (!surface) { + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER); + TBM_ERR("error: No valid tbm_surface is NULL\n"); + return 0; } - TBM_ERR("error: No valid tbm_surface(%p)\n", surface); + if (!_tbm_surface_internal_magic_check(surface)) { + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER); + TBM_ERR("error: No valid tbm_surface(%p)\n", surface); + return 0; + } - return 0; + return 1; } static int @@ -301,7 +321,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); @@ -309,34 +328,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->backend_module_data) { - if (!bufmgr->bufmgr_func->bufmgr_get_plane_data) - 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); - return 0; - /* LCOV_EXCL_STOP */ - } - ret = 1; - } else { - if (!bufmgr->backend->surface_get_plane_data) - 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); - 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 @@ -346,6 +344,18 @@ _tbm_surface_internal_destroy(tbm_surface_h surface) tbm_bufmgr bufmgr = surface->bufmgr; tbm_user_data *old_data = NULL, *tmp = NULL; tbm_surface_debug_data *debug_old_data = NULL, *debug_tmp = NULL; + tbm_surface_destroy_func_info *func_info = NULL, *func_next = NULL; + + if (!LIST_IS_EMPTY(&surface->destroy_funcs)) { + LIST_FOR_EACH_ENTRY_SAFE(func_info, func_next, &surface->destroy_funcs, item_link) { + func_info->destroy_func(surface, func_info->user_data); + } + TBM_DBG("free destroy_funcs %p\n", surface); + LIST_FOR_EACH_ENTRY_SAFE(func_info, func_next, &surface->destroy_funcs, item_link) { + LIST_DEL(&func_info->item_link); + free(func_info); + } + } /* destory the user_data_list */ if (!LIST_IS_EMPTY(&surface->user_data_list)) { @@ -368,11 +378,17 @@ _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; - if (LIST_IS_EMPTY(&bufmgr->surf_list)) { + if (bufmgr && LIST_IS_EMPTY(&bufmgr->surf_list)) { LIST_DELINIT(&bufmgr->surf_list); if (!LIST_IS_EMPTY(&bufmgr->debug_key_list)) { @@ -484,122 +500,8 @@ _tbm_surface_internal_get_num_planes(tbm_format format) return num_planes; } -int -tbm_surface_internal_is_valid(tbm_surface_h surface) -{ - int ret = 0; - - _tbm_surface_mutex_lock(); - - /* Return silently if surface is null. */ - if (!surface) { - _tbm_surface_mutex_unlock(); - return 0; - } - - ret = _tbm_surface_internal_is_valid(surface); - - _tbm_surface_mutex_unlock(); - - return ret; -} - -int -tbm_surface_internal_query_supported_formats(uint32_t **formats, - uint32_t *num) -{ - TBM_RETURN_VAL_IF_FAIL(formats, 0); - TBM_RETURN_VAL_IF_FAIL(num, 0); - - struct _tbm_bufmgr *bufmgr; - int ret = 0; - bool bufmgr_initialized = false; - tbm_error_e error; - - _tbm_surface_mutex_lock(); - - if (!g_surface_bufmgr) { - _init_surface_bufmgr(); - LIST_INITHEAD(&g_surface_bufmgr->surf_list); - bufmgr_initialized = true; - } - - bufmgr = g_surface_bufmgr; - - if (bufmgr->backend_module_data) { - if (!bufmgr->bufmgr_func->bufmgr_get_supported_formats) - 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) - goto fail; - - ret = bufmgr->backend->surface_supported_format(formats, num); - if (!ret) { - /* LCOV_EXCL_START */ - TBM_ERR("Fail to surface_supported_format.\n"); - goto fail; - /* LCOV_EXCL_START */ - } - } - - TBM_TRACE_SURFACE_INTERNAL("tbm_bufmgr(%p) format num(%u)\n", g_surface_bufmgr, *num); - - if (bufmgr_initialized) { - LIST_DELINIT(&g_surface_bufmgr->surf_list); - _deinit_surface_bufmgr(); - } - - _tbm_surface_mutex_unlock(); - - return ret; - -/* LCOV_EXCL_START */ -fail: - if (bufmgr_initialized) { - LIST_DELINIT(&g_surface_bufmgr->surf_list); - _deinit_surface_bufmgr(); - } - _tbm_surface_mutex_unlock(); - - TBM_ERR("error: tbm_bufmgr(%p)\n", g_surface_bufmgr); - - return 0; -/* LCOV_EXCL_STOP */ -} - -int -tbm_surface_internal_get_num_planes(tbm_format format) -{ - int num_planes = 0; - - _tbm_surface_mutex_lock(); - _tbm_set_last_result(TBM_ERROR_NONE); - - num_planes = _tbm_surface_internal_get_num_planes(format); - if (!num_planes) { - TBM_ERR("error: tbm_error(%s)\n", tbm_error_str(tbm_get_last_error())); - _tbm_surface_mutex_unlock(); - return 0; - } - - TBM_TRACE_SURFACE_INTERNAL("tbm_format(%s) num_planes(%d)\n", _tbm_surface_internal_format_to_str(format), num_planes); - - _tbm_surface_mutex_unlock(); - - return num_planes; -} - -int -tbm_surface_internal_get_bpp(tbm_format format) +static int +_tbm_surface_internal_get_bpp(tbm_format format) { int bpp = 0; @@ -685,74 +587,54 @@ tbm_surface_internal_get_bpp(tbm_format format) bpp = 24; break; default: + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER); break; } - TBM_TRACE_SURFACE_INTERNAL("tbm_format(%s) bpp(%d)\n", _tbm_surface_internal_format_to_str(format), bpp); - return bpp; } -tbm_surface_h -tbm_surface_internal_create_with_flags(int width, int height, - int format, int flags) +static struct _tbm_surface * +_tbm_surface_internal_create_surface(tbm_bufmgr bufmgr, int width, int height, int format, int flags, tbm_error_e *error) { - TBM_RETURN_VAL_IF_FAIL(width > 0, NULL); - TBM_RETURN_VAL_IF_FAIL(height > 0, NULL); - - 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; - 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(); - - if (!g_surface_bufmgr) { - _init_surface_bufmgr(); - LIST_INITHEAD(&g_surface_bufmgr->surf_list); - bufmgr_initialized = true; - } - - bufmgr = g_surface_bufmgr; - if (!TBM_BUFMGR_IS_VALID(bufmgr)) { - TBM_ERR("The bufmgr is invalid\n"); - goto check_valid_fail; - } + 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); + 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. error(%s)\n", tbm_error_str(tbm_get_last_error())); + 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)) { + 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; } @@ -780,122 +662,511 @@ 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"); - goto alloc_bo_fail; - } + 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; + } + } - bo->bufmgr = surf->bufmgr; + _tbm_bo_set_surface(surf->bos[i], surf); + } - pthread_mutex_lock(&surf->bufmgr->lock); + *error = TBM_ERROR_NONE; - 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); - free(bo); - pthread_mutex_unlock(&surf->bufmgr->lock); - goto alloc_bo_fail; - } - bo->bo_data = bo_data; + return surf; - bo->ref_cnt = 1; - bo->flags = flags; - LIST_INITHEAD(&bo->user_data_list); +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: - LIST_ADD(&bo->item_link, &surf->bufmgr->bo_list); + return NULL; +} - pthread_mutex_unlock(&surf->bufmgr->lock); +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->bos[i] = bo; - /* LCOV_EXCL_STOP */ - } else { + 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_bufmgr_internal_alloc_bo_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\n", i); - goto alloc_bo_fail; + TBM_ERR("fail to alloc bo idx:%d", i); + *error = tbm_get_last_error(); + goto get_bo_fail; } } - } 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"); - goto alloc_bo_fail; - } - bo->bufmgr = surf->bufmgr; + _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_bufmgr_internal_alloc_bo_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); + } + + return surf; + +get_bo_fail: + for (j = 0; j < i; j++) { + if (surf->bos[j]) + tbm_bo_unref(surf->bos[j]); + } +get_memory_types_fail: +get_bos_fail: +query_plane_data_fail: + hal_tbm_surface_free(hal_surface); +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) +{ + int ret = 0; + + _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); + + /* Return silently if surface is null. */ + if (!surface) { + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER); + _tbm_surface_mutex_unlock(); + return 0; + } + + ret = _tbm_surface_internal_is_valid(surface); + + _tbm_surface_mutex_unlock(); + + return ret; +} + +int +tbm_surface_internal_query_supported_formats(uint32_t **formats, + uint32_t *num) +{ + struct _tbm_bufmgr *bufmgr; + bool bufmgr_initialized = false; + tbm_error_e error; + + _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); + + TBM_SURFACE_RETURN_VAL_IF_FAIL(formats, 0); + TBM_SURFACE_RETURN_VAL_IF_FAIL(num, 0); + + if (!g_surface_bufmgr) { + _init_surface_bufmgr(); + if (!g_surface_bufmgr) { + TBM_ERR("fail bufmgr initialization\n"); + _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION); + goto fail; + } + LIST_INITHEAD(&g_surface_bufmgr->surf_list); + bufmgr_initialized = true; + } + + bufmgr = g_surface_bufmgr; + + 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); + + if (bufmgr_initialized) { + LIST_DELINIT(&g_surface_bufmgr->surf_list); + _deinit_surface_bufmgr(); + } + + _tbm_surface_mutex_unlock(); + + return 1; + +/* LCOV_EXCL_START */ +fail: + if (bufmgr_initialized) { + LIST_DELINIT(&g_surface_bufmgr->surf_list); + _deinit_surface_bufmgr(); + } + _tbm_surface_mutex_unlock(); + + TBM_ERR("error: tbm_bufmgr(%p)\n", g_surface_bufmgr); + + return 0; +/* LCOV_EXCL_STOP */ +} + +int +tbm_surface_internal_get_num_planes(tbm_format format) +{ + int num_planes = 0; + + _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); + + num_planes = _tbm_surface_internal_get_num_planes(format); + if (!num_planes) { + TBM_ERR("error: tbm_error(%s)\n", tbm_error_str(tbm_get_last_error())); + _tbm_surface_mutex_unlock(); + return 0; + } + + TBM_TRACE_SURFACE_INTERNAL("tbm_format(%s) num_planes(%d)\n", _tbm_surface_internal_format_to_str(format), num_planes); + + _tbm_surface_mutex_unlock(); + + return num_planes; +} + +int +tbm_surface_internal_get_bpp(tbm_format format) +{ + int bpp = 0; + + _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); + + bpp = _tbm_surface_internal_get_bpp(format); + if (!bpp) { + TBM_ERR("error: tbm_error(%s)\n", tbm_error_str(tbm_get_last_error())); + _tbm_surface_mutex_unlock(); + return 0; + } + + _tbm_surface_mutex_unlock(); - pthread_mutex_lock(&surf->bufmgr->lock); + TBM_TRACE_SURFACE_INTERNAL("tbm_format(%s) bpp(%d)\n", _tbm_surface_internal_format_to_str(format), bpp); - bo_priv = bufmgr->backend->surface_bo_alloc(bo, width, height, format, flags, i); - if (!bo_priv) { - TBM_ERR("fail to alloc bo priv\n"); - free(bo); - pthread_mutex_unlock(&surf->bufmgr->lock); - goto alloc_bo_fail; - } - bo->priv = bo_priv; + return bpp; +} - bo->ref_cnt = 1; - bo->flags = flags; - LIST_INITHEAD(&bo->user_data_list); +tbm_surface_h +tbm_surface_internal_create_with_flags(int width, int height, + int format, int flags) +{ + struct _tbm_bufmgr *bufmgr; + struct _tbm_surface *surf = NULL; + tbm_error_e error = TBM_ERROR_INVALID_OPERATION; + bool bufmgr_initialized = false; - LIST_ADD(&bo->item_link, &surf->bufmgr->bo_list); + _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); - pthread_mutex_unlock(&surf->bufmgr->lock); + TBM_SURFACE_RETURN_VAL_IF_FAIL(width > 0, NULL); + TBM_SURFACE_RETURN_VAL_IF_FAIL(height > 0, NULL); - surf->bos[i] = bo; - /* 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; - } - } + if (!g_surface_bufmgr) { + _init_surface_bufmgr(); + if (!g_surface_bufmgr) { + TBM_ERR("fail bufmgr initialization\n"); + error = TBM_ERROR_INVALID_OPERATION; + goto check_valid_fail; } + LIST_INITHEAD(&g_surface_bufmgr->surf_list); + bufmgr_initialized = true; + } - _tbm_bo_set_surface(surf->bos[i], surf); + bufmgr = g_surface_bufmgr; + if (!TBM_BUFMGR_IS_VALID(bufmgr)) { + TBM_ERR("The bufmgr is invalid\n"); + error = TBM_ERROR_INVALID_PARAMETER; + goto check_valid_fail; + } + + 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; + } + } 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_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); + LIST_INITHEAD(&surf->destroy_funcs); 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: -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; @@ -905,21 +1176,27 @@ tbm_surface_h tbm_surface_internal_create_with_bos(tbm_surface_info_s *info, tbm_bo *bos, int num) { - TBM_RETURN_VAL_IF_FAIL(bos, NULL); - TBM_RETURN_VAL_IF_FAIL(info, NULL); - TBM_RETURN_VAL_IF_FAIL(info->num_planes > 0, NULL); - TBM_RETURN_VAL_IF_FAIL(num > 0, NULL); - TBM_RETURN_VAL_IF_FAIL(num == 1 || info->num_planes == num, NULL); - struct _tbm_bufmgr *bufmgr; struct _tbm_surface *surf = NULL; int i; bool bufmgr_initialized = false; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); + + TBM_SURFACE_RETURN_VAL_IF_FAIL(bos, NULL); + TBM_SURFACE_RETURN_VAL_IF_FAIL(info, NULL); + TBM_SURFACE_RETURN_VAL_IF_FAIL(info->num_planes > 0, NULL); + TBM_SURFACE_RETURN_VAL_IF_FAIL(num > 0, NULL); + TBM_SURFACE_RETURN_VAL_IF_FAIL(num == 1 || info->num_planes == num, NULL); if (!g_surface_bufmgr) { _init_surface_bufmgr(); + if (!g_surface_bufmgr) { + TBM_ERR("fail bufmgr initialization\n"); + _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION); + goto check_valid_fail; + } LIST_INITHEAD(&g_surface_bufmgr->surf_list); bufmgr_initialized = true; } @@ -927,6 +1204,7 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info, bufmgr = g_surface_bufmgr; if (!TBM_BUFMGR_IS_VALID(bufmgr)) { TBM_ERR("fail to validate the Bufmgr.\n"); + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER); goto check_valid_fail; } @@ -934,18 +1212,25 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info, if (!surf) { /* LCOV_EXCL_START */ TBM_ERR("fail to allocate struct _tbm_surface.\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 = info->width; surf->info.height = info->height; surf->info.format = info->format; if (info->bpp > 0) surf->info.bpp = info->bpp; - else - surf->info.bpp = tbm_surface_internal_get_bpp(info->format); + else { + surf->info.bpp = _tbm_surface_internal_get_bpp(info->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 = info->num_planes; surf->refcnt = 1; @@ -960,7 +1245,10 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info, uint32_t size = 0, offset = 0, stride = 0; int32_t bo_idx = 0; - _tbm_surface_internal_query_plane_data(surf, i, &size, &offset, &stride, &bo_idx); + if (!_tbm_surface_internal_query_plane_data(surf, i, &size, &offset, &stride, &bo_idx)) { + TBM_ERR("fail to get plane_data. error(%s)\n", tbm_error_str(tbm_get_last_error())); + goto plane_data_fail; + } surf->info.planes[i].size = size; } @@ -985,6 +1273,7 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info, for (i = 0; i < num; i++) { if (bos[i] == NULL) { TBM_ERR("bos[%d] is null.\n", i); + _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION); goto check_bo_fail; } @@ -997,6 +1286,7 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info, 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); @@ -1006,6 +1296,8 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info, /* LCOV_EXCL_START */ check_bo_fail: +plane_data_fail: +bpp_fail: for (i = 0; i < num; i++) { if (surf->bos[i]) tbm_bo_unref(surf->bos[i]); @@ -1031,6 +1323,7 @@ void tbm_surface_internal_destroy(tbm_surface_h surface) { _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface)); @@ -1046,6 +1339,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(); } @@ -1054,6 +1349,7 @@ void tbm_surface_internal_ref(tbm_surface_h surface) { _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface)); @@ -1068,6 +1364,7 @@ void tbm_surface_internal_unref(tbm_surface_h surface) { _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface)); @@ -1094,12 +1391,16 @@ tbm_surface_internal_get_num_bos(tbm_surface_h surface) int num; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); surf = (struct _tbm_surface *)surface; num = surf->num_bos; + if (!num) + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER); + TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) num_bos(%d)\n", surface, num); _tbm_surface_mutex_unlock(); @@ -1114,6 +1415,7 @@ tbm_surface_internal_get_bo(tbm_surface_h surface, int bo_idx) tbm_bo bo; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), NULL); TBM_SURFACE_RETURN_VAL_IF_FAIL(bo_idx > -1, NULL); @@ -1135,6 +1437,7 @@ tbm_surface_internal_get_size(tbm_surface_h surface) unsigned int size; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); @@ -1155,6 +1458,7 @@ tbm_surface_internal_get_plane_data(tbm_surface_h surface, int plane_idx, struct _tbm_surface *surf; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); TBM_SURFACE_RETURN_VAL_IF_FAIL(plane_idx > -1, 0); @@ -1163,6 +1467,7 @@ tbm_surface_internal_get_plane_data(tbm_surface_h surface, int plane_idx, if (plane_idx >= surf->info.num_planes) { TBM_TRACE_SURFACE_INTERNAL("error: tbm_surface(%p) plane_idx(%d)\n", surface, plane_idx); + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER); _tbm_surface_mutex_unlock(); return 0; } @@ -1197,6 +1502,7 @@ tbm_surface_internal_get_info(tbm_surface_h surface, int opt, int i, j; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); @@ -1267,6 +1573,7 @@ tbm_surface_internal_unmap(tbm_surface_h surface) int i; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface)); @@ -1287,6 +1594,7 @@ tbm_surface_internal_get_width(tbm_surface_h surface) unsigned int width; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); @@ -1307,6 +1615,7 @@ tbm_surface_internal_get_height(tbm_surface_h surface) unsigned int height; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); @@ -1328,6 +1637,7 @@ tbm_surface_internal_get_format(tbm_surface_h surface) tbm_format format; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); @@ -1348,6 +1658,7 @@ tbm_surface_internal_get_plane_bo_idx(tbm_surface_h surface, int plane_idx) int bo_idx; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); TBM_SURFACE_RETURN_VAL_IF_FAIL(plane_idx > -1, 0); @@ -1369,6 +1680,7 @@ tbm_surface_internal_add_user_data(tbm_surface_h surface, unsigned long key, tbm_user_data *data; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); @@ -1376,6 +1688,7 @@ tbm_surface_internal_add_user_data(tbm_surface_h surface, unsigned long key, data = user_data_lookup(&surface->user_data_list, key); if (data) { TBM_TRACE_SURFACE_INTERNAL("warning: user data already exist tbm_surface(%p) key(%lu)\n", surface, key); + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER); _tbm_surface_mutex_unlock(); return 0; } @@ -1403,12 +1716,14 @@ tbm_surface_internal_set_user_data(tbm_surface_h surface, unsigned long key, tbm_user_data *old_data; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); old_data = user_data_lookup(&surface->user_data_list, key); if (!old_data) { TBM_TRACE_SURFACE_INTERNAL("error: tbm_surface(%p) key(%lu)\n", surface, key); + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER); _tbm_surface_mutex_unlock(); return 0; } @@ -1432,11 +1747,13 @@ tbm_surface_internal_get_user_data(tbm_surface_h surface, unsigned long key, tbm_user_data *old_data; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); if (!data) { TBM_ERR("error: tbm_surface(%p) key(%lu)\n", surface, key); + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER); _tbm_surface_mutex_unlock(); return 0; } @@ -1445,6 +1762,7 @@ tbm_surface_internal_get_user_data(tbm_surface_h surface, unsigned long key, old_data = user_data_lookup(&surface->user_data_list, key); if (!old_data) { TBM_TRACE_SURFACE_INTERNAL("error: tbm_surface(%p) key(%lu)\n", surface, key); + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER); _tbm_surface_mutex_unlock(); return 0; } @@ -1465,12 +1783,14 @@ tbm_surface_internal_delete_user_data(tbm_surface_h surface, tbm_user_data *old_data = (void *)0; _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); old_data = user_data_lookup(&surface->user_data_list, key); if (!old_data) { TBM_TRACE_SURFACE_INTERNAL("error: tbm_surface(%p) key(%lu)\n", surface, key); + _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER); _tbm_surface_mutex_unlock(); return 0; } @@ -1497,6 +1817,7 @@ void tbm_surface_internal_set_debug_pid(tbm_surface_h surface, unsigned int pid) { _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface)); @@ -1511,8 +1832,11 @@ _tbm_surface_internal_debug_data_create(char *key, char *value) tbm_surface_debug_data *debug_data = NULL; debug_data = calloc(1, sizeof(tbm_surface_debug_data)); - if (!debug_data) + if (!debug_data) { + _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY); + TBM_ERR("fail to allocate the debug_data."); return NULL; + } if (key) debug_data->key = strdup(key); if (value) debug_data->value = strdup(value); @@ -1520,14 +1844,48 @@ _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(); + _tbm_set_last_result(TBM_ERROR_NONE); TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); TBM_SURFACE_RETURN_VAL_IF_FAIL(key, 0); @@ -1536,53 +1894,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_ADD(&debug_data->item_link, &bufmgr->debug_key_list); - _tbm_surface_mutex_unlock(); return 1; @@ -1665,7 +2000,7 @@ _tbm_surface_internal_dump_file_raw(const char *file, void *data1, int size1, } static void -_tbm_surface_internal_dump_file_png(const char *file, const void *data, int width, int height, int format) +_tbm_surface_internal_dump_file_png(const char *file, const void *data, int width, int height, int stride, int format) { unsigned int *blocks = (unsigned int *)data; FILE *fp; @@ -1695,6 +2030,14 @@ _tbm_surface_internal_dump_file_png(const char *file, const void *data, int widt return; } + if (setjmp(png_jmpbuf(pPngStruct))) { + /* if png has problem of writing the file, we get here */ + TBM_ERR("fail to write png file.\n"); + png_destroy_write_struct(&pPngStruct, &pPngInfo); + fclose(fp); + return; + } + png_init_io(pPngStruct, fp); if (format == TBM_FORMAT_XRGB8888) { pixel_size = 3; @@ -1746,7 +2089,7 @@ _tbm_surface_internal_dump_file_png(const char *file, const void *data, int widt row_pointers[y] = (png_bytep)row; for (x = 0; x < width; ++x) { - unsigned int curBlock = blocks[y * width + x]; + unsigned int curBlock = blocks[(y * (stride >> 2)) + x]; if (pixel_size == 3) { // XRGB8888 row[x * pixel_size] = (curBlock & 0xFF); @@ -1917,12 +2260,16 @@ tbm_surface_internal_dump_end(void) case TBM_FORMAT_ARGB8888: _tbm_surface_internal_dump_file_png(file, bo_handle.ptr, buf_info->info.planes[0].stride >> 2, - buf_info->info.height, TBM_FORMAT_ARGB8888); + buf_info->info.height, + buf_info->info.planes[0].stride, + TBM_FORMAT_ARGB8888); break; case TBM_FORMAT_XRGB8888: _tbm_surface_internal_dump_file_png(file, bo_handle.ptr, buf_info->info.planes[0].stride >> 2, - buf_info->info.height, TBM_FORMAT_XRGB8888); + buf_info->info.height, + buf_info->info.planes[0].stride, + TBM_FORMAT_XRGB8888); break; case TBM_FORMAT_YVU420: case TBM_FORMAT_YUV420: @@ -1957,7 +2304,8 @@ tbm_surface_internal_dump_end(void) } else if (buf_info->dirty_shm) _tbm_surface_internal_dump_file_png(file, bo_handle.ptr, buf_info->shm_stride >> 2, - buf_info->shm_h, 0); + buf_info->shm_h, + buf_info->shm_stride, 0); tbm_bo_unmap(buf_info->bo); tbm_bo_unref(buf_info->bo); @@ -2075,7 +2423,7 @@ static char *_tbm_surface_internal_get_keys(tbm_surface_h surface) memset(temp_key, 0x00, KEY_LEN + 1); bo = surf->bos[i]; snprintf(temp_key, KEY_LEN, "_%d", tbm_bo_export(bo)); - strncat(keys, temp_key, KEY_LEN); + strncat(keys, temp_key, KEY_LEN + 1); } _tbm_surface_mutex_unlock(); @@ -2136,7 +2484,12 @@ tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type) buf_info->info.width = info.width * scale_factor; buf_info->info.height = info.height * scale_factor; buf_info->info.format = info.format; - buf_info->info.bpp = tbm_surface_internal_get_bpp(buf_info->info.format); + buf_info->info.bpp = _tbm_surface_internal_get_bpp(buf_info->info.format); + if (!buf_info->info.bpp) { + TBM_ERR("fail to get bpp. error(%s)\n", tbm_error_str(tbm_get_last_error())); + tbm_surface_unmap(surface); + return; + } buf_info->info.num_planes = 1; buf_info->info.planes[0].stride = buf_info->info.width * bpp; buf_info->info.size = buf_info->info.width * buf_info->info.height * bpp; @@ -2388,13 +2741,17 @@ tbm_surface_internal_capture_buffer(tbm_surface_h surface, const char *path, con switch (info.format) { case TBM_FORMAT_ARGB8888: _tbm_surface_internal_dump_file_png(file, info.planes[0].ptr, - info.planes[0].stride >> 2, - info.height, TBM_FORMAT_ARGB8888); + info.width, + info.height, + info.planes[0].stride, + TBM_FORMAT_ARGB8888); break; case TBM_FORMAT_XRGB8888: _tbm_surface_internal_dump_file_png(file, info.planes[0].ptr, - info.planes[0].stride >> 2, - info.height, TBM_FORMAT_XRGB8888); + info.width, + info.height, + info.planes[0].stride, + TBM_FORMAT_XRGB8888); break; case TBM_FORMAT_YVU420: case TBM_FORMAT_YUV420: @@ -2457,10 +2814,204 @@ tbm_surface_internal_capture_shm_buffer(void *ptr, int w, int h, int stride, return 0; } - _tbm_surface_internal_dump_file_png(file, ptr, w, h, 0); + _tbm_surface_internal_dump_file_png(file, ptr, w, h, stride, 0); TBM_TRACE_SURFACE_INTERNAL("Capture %s \n", file); return 1; } + +int +tbm_surface_internal_set_damage(tbm_surface_h surface, int x, int y, int width, int height) +{ + struct _tbm_surface *surf; + + _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); + + TBM_SURFACE_RETURN_VAL_IF_FAIL(width > 0, 0); + TBM_SURFACE_RETURN_VAL_IF_FAIL(height > 0, 0); + TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); + + surf = (struct _tbm_surface *)surface; + + surf->damage.x = x; + surf->damage.y = y; + surf->damage.width = width; + surf->damage.height = height; + + TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) x(%d) y(%d) width(%d) height(%d)\n", + surface, x, y, width, height); + + _tbm_surface_mutex_unlock(); + + return 1; +} + +int +tbm_surface_internal_get_damage(tbm_surface_h surface, int *x, int *y, int *width, int *height) +{ + struct _tbm_surface *surf; + + _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); + + TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); + + surf = (struct _tbm_surface *)surface; + + if (x) *x = surf->damage.x; + if (y) *y = surf->damage.y; + if (width) *width = surf->damage.width; + if (height) *height = surf->damage.height; + + TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) x(%d) y(%d) width(%d) height(%d)\n", + surface, surf->damage.x, surf->damage.y, surf->damage.width, surf->damage.height); + + _tbm_surface_mutex_unlock(); + + return 1; +} + +int +tbm_surface_internal_add_destroy_handler(tbm_surface_h surface, tbm_surface_internal_destroy_handler func, void *user_data) +{ + struct _tbm_surface *surf; + tbm_surface_destroy_func_info *func_info = NULL; + + _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); + + TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0); + TBM_SURFACE_RETURN_VAL_IF_FAIL(func != NULL, 0); + + surf = (struct _tbm_surface *)surface; + LIST_FOR_EACH_ENTRY(func_info, &surf->destroy_funcs, item_link) { + if (func_info->destroy_func == func && func_info->user_data == user_data) { + TBM_ERR("can't add twice"); + _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION); + _tbm_surface_mutex_unlock(); + return 0; + } + } + + func_info = calloc(1, sizeof(tbm_surface_destroy_func_info)); + if (func_info == NULL) { + TBM_ERR("alloc failed"); + _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY); + _tbm_surface_mutex_unlock(); + return 0; + } + + func_info->destroy_func = func; + func_info->user_data = user_data; + + LIST_ADDTAIL(&func_info->item_link, &surf->destroy_funcs); + + _tbm_surface_mutex_unlock(); + + return 1; +} + +void +tbm_surface_internal_remove_destroy_handler(tbm_surface_h surface, tbm_surface_internal_destroy_handler func, void *user_data) +{ + struct _tbm_surface *surf; + tbm_surface_destroy_func_info *func_info = NULL, *next = NULL; + + _tbm_surface_mutex_lock(); + _tbm_set_last_result(TBM_ERROR_NONE); + + TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface)); + TBM_SURFACE_RETURN_IF_FAIL(func != NULL); + + surf = (struct _tbm_surface *)surface; + LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &surf->destroy_funcs, item_link) { + if (func_info->destroy_func != func || func_info->user_data != user_data) + continue; + + LIST_DEL(&func_info->item_link); + free(func_info); + + _tbm_surface_mutex_unlock(); + + return; + } + + _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); + + _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); + + _tbm_set_last_result(TBM_ERROR_NONE); + _tbm_surface_mutex_unlock(); + + return (tbm_surface_h)surf; +} + /*LCOV_EXCL_STOP*/