+ 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)
+{
+ 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;
+ int ret = 0;
+ 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;
+
+ 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 */
+ }
+ }
+
+ 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)
+{
+ 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();
+
+ 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)
+{
+ struct _tbm_bufmgr *bufmgr;
+ struct _tbm_surface *surf = NULL;
+ tbm_error_e error = TBM_ERROR_INVALID_OPERATION;
+ bool bufmgr_initialized = false;
+
+ _tbm_surface_mutex_lock();
+ _tbm_set_last_result(TBM_ERROR_NONE);
+
+ TBM_SURFACE_RETURN_VAL_IF_FAIL(width > 0, NULL);
+ TBM_SURFACE_RETURN_VAL_IF_FAIL(height > 0, NULL);
+
+ 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;
+ }
+
+ 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);
+
+ 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 */
+
+surface_alloc_fail:
+check_valid_fail:
+ if (bufmgr_initialized && bufmgr) {
+ LIST_DELINIT(&bufmgr->surf_list);
+ _deinit_surface_bufmgr();
+ }
+
+ TBM_ERR("error: width(%d) height(%d) format(%s) flags(%d)\n",
+ width, height, _tbm_surface_internal_format_to_str(format), flags);
+
+ _tbm_set_last_result(error);
+ _tbm_surface_mutex_unlock();
+
+/* LCOV_EXCL_STOP */
+
+ return NULL;
+}
+
+tbm_surface_h
+tbm_surface_internal_create_with_bos(tbm_surface_info_s *info,
+ tbm_bo *bos, int num)
+{
+ 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;
+ }
+
+ 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;
+ }
+
+ surf = calloc(1, sizeof(struct _tbm_surface));
+ 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);
+ 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;
+
+ /* get size, stride and offset */
+ for (i = 0; i < info->num_planes; i++) {
+ surf->info.planes[i].offset = info->planes[i].offset;
+ surf->info.planes[i].stride = info->planes[i].stride;
+
+ if (info->planes[i].size > 0)
+ surf->info.planes[i].size = info->planes[i].size;
+ else {
+ uint32_t size = 0, offset = 0, stride = 0;
+ int32_t bo_idx = 0;
+
+ 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;
+ }
+
+ if (num == 1)
+ surf->planes_bo_idx[i] = 0;
+ else
+ surf->planes_bo_idx[i] = i;
+ }
+
+ if (info->size > 0) {
+ surf->info.size = info->size;
+ } else {
+ surf->info.size = 0;
+ for (i = 0; i < info->num_planes; i++)
+ surf->info.size += surf->info.planes[i].size;
+ }
+
+ surf->flags = TBM_BO_DEFAULT;
+
+ /* create only one bo */
+ surf->num_bos = num;
+ 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;
+ }
+
+ surf->bos[i] = tbm_bo_ref(bos[i]);
+ _tbm_bo_set_surface(bos[i], surf);
+ }
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) width(%u) height(%u) format(%s) bo_num(%d)\n", surf,
+ info->width, info->height, _tbm_surface_internal_format_to_str(info->format), num);
+
+ 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_surface_mutex_unlock();
+
+ return surf;
+
+/* 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]);
+ }
+ free(surf);
+alloc_surf_fail:
+check_valid_fail:
+ if (bufmgr_initialized && bufmgr) {
+ LIST_DELINIT(&bufmgr->surf_list);
+ _deinit_surface_bufmgr();
+ }
+ _tbm_surface_mutex_unlock();
+
+ TBM_ERR("error: width(%u) height(%u) format(%s) bo_num(%d)\n",
+ info->width, info->height,
+ _tbm_surface_internal_format_to_str(info->format), num);
+/* LCOV_EXCL_STOP */
+
+ return NULL;
+}
+
+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));
+
+ surface->refcnt--;
+
+ if (surface->refcnt > 0) {
+ TBM_TRACE_SURFACE_INTERNAL("reduce a refcnt(%d) of tbm_surface(%p)\n", surface->refcnt, surface);
+ _tbm_surface_mutex_unlock();
+ return;
+ }
+
+ TBM_TRACE_SURFACE_INTERNAL("destroy tbm_surface(%p) refcnt(%d)\n", surface, surface->refcnt);
+
+ 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();
+}
+
+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));
+
+ surface->refcnt++;
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) refcnt(%d)\n", surface, surface->refcnt);
+
+ _tbm_surface_mutex_unlock();
+}
+
+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));
+
+ surface->refcnt--;
+
+ if (surface->refcnt > 0) {
+ TBM_TRACE_SURFACE_INTERNAL("reduce a refcnt(%d) of tbm_surface(%p)\n", surface->refcnt, surface);
+ _tbm_surface_mutex_unlock();
+ return;
+ }
+
+ TBM_TRACE_SURFACE_INTERNAL("destroy tbm_surface(%p) refcnt(%d)\n", surface, surface->refcnt);
+
+ if (surface->refcnt == 0)
+ _tbm_surface_internal_destroy(surface);
+
+ _tbm_surface_mutex_unlock();
+}
+
+int
+tbm_surface_internal_get_num_bos(tbm_surface_h surface)
+{
+ struct _tbm_surface *surf;
+ 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();
+
+ return num;
+}
+
+tbm_bo
+tbm_surface_internal_get_bo(tbm_surface_h surface, int bo_idx)
+{
+ struct _tbm_surface *surf;
+ 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);
+
+ surf = (struct _tbm_surface *)surface;
+ bo = surf->bos[bo_idx];
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) bo_idx(%d) tbm_bo(%p)\n", surface, bo_idx, bo);
+
+ _tbm_surface_mutex_unlock();
+
+ return bo;
+}
+
+unsigned int
+tbm_surface_internal_get_size(tbm_surface_h surface)
+{
+ struct _tbm_surface *surf;
+ 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);
+
+ surf = (struct _tbm_surface *)surface;
+ size = surf->info.size;
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) size(%u)\n", surface, size);
+
+ _tbm_surface_mutex_unlock();
+
+ return size;
+}
+
+int
+tbm_surface_internal_get_plane_data(tbm_surface_h surface, int plane_idx,
+ uint32_t *size, uint32_t *offset, uint32_t *pitch)
+{
+ 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);
+
+ surf = (struct _tbm_surface *)surface;
+
+ 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;
+ }
+
+ if (size)
+ *size = surf->info.planes[plane_idx].size;
+
+ if (offset)
+ *offset = surf->info.planes[plane_idx].offset;
+
+ if (pitch)
+ *pitch = surf->info.planes[plane_idx].stride;
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) plane_idx(%d) size(%u) offset(%u) pitch(%u)\n", surface, plane_idx,
+ surf->info.planes[plane_idx].size, surf->info.planes[plane_idx].offset,
+ surf->info.planes[plane_idx].stride);
+
+ _tbm_surface_mutex_unlock();
+
+ return 1;
+}
+
+int
+tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
+ tbm_surface_info_s *info, int map)
+{
+ struct _tbm_surface *surf;
+ tbm_bo_handle bo_handles[4];
+ int num_bos = 0;
+ tbm_bo bos[4];
+ int planes_bo_idx[TBM_SURF_PLANE_MAX];
+ 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);
+
+ memset(bo_handles, 0, sizeof(tbm_bo_handle) * 4);
+
+ surf = (struct _tbm_surface *)surface;
+
+ memset(info, 0x00, sizeof(tbm_surface_info_s));
+ info->width = surf->info.width;
+ info->height = surf->info.height;
+ info->format = surf->info.format;
+ info->bpp = surf->info.bpp;
+ info->size = surf->info.size;
+ info->num_planes = surf->info.num_planes;
+
+ for (i = 0; i < surf->info.num_planes; i++) {
+ info->planes[i].size = surf->info.planes[i].size;
+ info->planes[i].offset = surf->info.planes[i].offset;
+ info->planes[i].stride = surf->info.planes[i].stride;
+ planes_bo_idx[i] = surf->planes_bo_idx[i];
+ }
+
+ for (i = 0; i < surf->num_bos; i++)
+ bos[i] = surf->bos[i];
+
+ num_bos = surf->num_bos;
+
+ if (map == 1) {
+ _tbm_surface_mutex_unlock();
+ for (i = 0; i < num_bos; i++) {
+ bo_handles[i] = tbm_bo_map(bos[i], TBM_DEVICE_CPU, opt);
+ if (bo_handles[i].ptr == NULL) {
+ for (j = 0; j < i; j++)
+ tbm_bo_unmap(bos[j]);
+
+ TBM_ERR("error: tbm_surface(%p) opt(%d) map(%d)\n", surface, opt, map);
+ return 0;
+ }
+ }
+ _tbm_surface_mutex_lock();
+ } else {
+ for (i = 0; i < num_bos; i++) {
+ bo_handles[i] = tbm_bo_get_handle(bos[i], TBM_DEVICE_CPU);
+ if (bo_handles[i].ptr == NULL) {
+ TBM_ERR("error: tbm_surface(%p) opt(%d) map(%d)\n", surface, opt, map);
+ _tbm_surface_mutex_unlock();
+ return 0;
+ }
+ }
+ }
+
+ for (i = 0; i < info->num_planes; i++) {
+ if (bo_handles[planes_bo_idx[i]].ptr)
+ info->planes[i].ptr = bo_handles[planes_bo_idx[i]].ptr + info->planes[i].offset;
+ }
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) opt(%d) map(%d)\n", surface, opt, map);
+
+ _tbm_surface_mutex_unlock();
+
+ return 1;
+}
+
+void
+tbm_surface_internal_unmap(tbm_surface_h surface)
+{
+ struct _tbm_surface *surf;
+ int i;
+
+ _tbm_surface_mutex_lock();
+ _tbm_set_last_result(TBM_ERROR_NONE);
+
+ TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface));
+
+ surf = (struct _tbm_surface *)surface;
+
+ for (i = 0; i < surf->num_bos; i++)
+ tbm_bo_unmap(surf->bos[i]);
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p)\n", surface);
+
+ _tbm_surface_mutex_unlock();
+}
+
+unsigned int
+tbm_surface_internal_get_width(tbm_surface_h surface)
+{
+ struct _tbm_surface *surf;
+ 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);
+
+ surf = (struct _tbm_surface *)surface;
+ width = surf->info.width;
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) width(%u)\n", surface, width);
+
+ _tbm_surface_mutex_unlock();
+
+ return width;
+}
+
+unsigned int
+tbm_surface_internal_get_height(tbm_surface_h surface)
+{
+ struct _tbm_surface *surf;
+ 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);
+
+ surf = (struct _tbm_surface *)surface;
+ height = surf->info.height;
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) height(%u)\n", surface, height);
+
+ _tbm_surface_mutex_unlock();
+
+ return height;
+
+}
+
+tbm_format
+tbm_surface_internal_get_format(tbm_surface_h surface)
+{
+ struct _tbm_surface *surf;
+ 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);
+
+ surf = (struct _tbm_surface *)surface;
+ format = surf->info.format;
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) format(%s)\n", surface, _tbm_surface_internal_format_to_str(format));
+
+ _tbm_surface_mutex_unlock();
+
+ return format;
+}
+
+int
+tbm_surface_internal_get_plane_bo_idx(tbm_surface_h surface, int plane_idx)
+{
+ struct _tbm_surface *surf;
+ 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);
+
+ surf = (struct _tbm_surface *)surface;
+ bo_idx = surf->planes_bo_idx[plane_idx];
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) plane_idx(%d) bo_idx(%d)\n", surface, plane_idx, bo_idx);
+
+ _tbm_surface_mutex_unlock();
+
+ return bo_idx;
+}
+
+int
+tbm_surface_internal_add_user_data(tbm_surface_h surface, unsigned long key,
+ tbm_data_free data_free_func)
+{
+ 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);
+
+ /* check if the data according to the key exist if so, return false. */
+ 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;
+ }
+
+ data = user_data_create(key, data_free_func);
+ if (!data) {
+ TBM_TRACE_SURFACE_INTERNAL("error: tbm_surface(%p) key(%lu)\n", surface, key);
+ _tbm_surface_mutex_unlock();
+ return 0;
+ }
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) key(%lu) data(%p)\n", surface, key, data);
+
+ LIST_ADD(&data->item_link, &surface->user_data_list);
+
+ _tbm_surface_mutex_unlock();
+
+ return 1;
+}
+
+int
+tbm_surface_internal_set_user_data(tbm_surface_h surface, unsigned long key,
+ void *data)
+{
+ 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;
+ }
+
+ if (old_data->data && old_data->free_func)
+ old_data->free_func(old_data->data);
+
+ old_data->data = data;
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) key(%lu) data(%p)\n", surface, key, old_data->data);
+
+ _tbm_surface_mutex_unlock();
+
+ return 1;
+}
+
+int
+tbm_surface_internal_get_user_data(tbm_surface_h surface, unsigned long key,
+ void **data)
+{
+ 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;
+ }
+ *data = NULL;
+
+ 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);