**************************************************************************/
#include "config.h"
+
+#include <stdio.h>
+#include <time.h>
+#include <sys/time.h>
#include "tbm_bufmgr.h"
#include "tbm_bufmgr_int.h"
#include "tbm_surface_internal.h"
#include "list.h"
+#include <png.h>
+#include <pixman.h>
+
+#define TBM_SURFACE_MAGIC 0xBF021234
+
+static tbm_bufmgr g_surface_bufmgr;
+static pthread_mutex_t tbm_surface_lock = PTHREAD_MUTEX_INITIALIZER;
+void _tbm_surface_mutex_unlock(void);
+
+/* 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;\
+ } \
+}
+
+#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);\
+ 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)
+{
+ struct timespec tp;
+ unsigned int time;
+
+ clock_gettime(CLOCK_MONOTONIC, &tp);
+ time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
-static tbm_bufmgr g_surface_bufmgr = NULL;
-static pthread_mutex_t tbm_surface_lock;
+ return time / 1000.0;
+}
+
+static void
+_tbm_surface_internal_debug_data_delete(tbm_surface_debug_data *debug_data)
+{
+ LIST_DEL(&debug_data->item_link);
+
+ if (debug_data->key) free(debug_data->key);
+ if (debug_data->value) free(debug_data->value);
+ free(debug_data);
+}
-char *_tbm_surface_internal_format_to_str(tbm_format format)
+char *
+_tbm_surface_internal_format_to_str(tbm_format format)
{
switch (format) {
case TBM_FORMAT_C8:
}
}
-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_LOG("[libtbm] fail: tbm_surface mutex init\n");
- return false;
- }
-
- tbm_surface_mutex_init = true;
-
- return true;
-}
-
-void _tbm_surface_mutex_lock(void)
+void
+_tbm_surface_mutex_lock(void)
{
- if (!_tbm_surface_mutex_init())
- return;
-
pthread_mutex_lock(&tbm_surface_lock);
}
-void _tbm_surface_mutex_unlock(void)
+void
+_tbm_surface_mutex_unlock(void)
{
pthread_mutex_unlock(&tbm_surface_lock);
}
-static void _init_surface_bufmgr()
+static void
+_init_surface_bufmgr(void)
{
g_surface_bufmgr = tbm_bufmgr_init(-1);
}
-static void _deinit_surface_bufmgr()
+static void
+_deinit_surface_bufmgr(void)
{
if (!g_surface_bufmgr)
return;
tbm_bufmgr_deinit(g_surface_bufmgr);
g_surface_bufmgr = NULL;
}
+/* LCOV_EXCL_STOP */
-static int _tbm_surface_internal_query_size(tbm_surface_h surface)
+static int
+_tbm_surface_internal_magic_check(tbm_surface_h surface)
{
- TBM_RETURN_VAL_IF_FAIL(surface, 0);
-
- struct _tbm_surface *surf = (struct _tbm_surface *)surface;
- struct _tbm_bufmgr *mgr = surf->bufmgr;
- int size = 0;
+ if (surface->magic != TBM_SURFACE_MAGIC)
+ return 0;
- TBM_RETURN_VAL_IF_FAIL(mgr != NULL, 0);
- TBM_RETURN_VAL_IF_FAIL(surf->info.width > 0, 0);
- TBM_RETURN_VAL_IF_FAIL(surf->info.height > 0, 0);
- TBM_RETURN_VAL_IF_FAIL(surf->info.format > 0, 0);
+ return 1;
+}
- if (!mgr->backend->surface_get_size)
+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;
+ }
- size = mgr->backend->surface_get_size(surf, surf->info.width, surf->info.height, surf->info.format);
+ 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 size;
+ return 1;
}
-static int _tbm_surface_internal_query_plane_data(tbm_surface_h surface, int plane_idx, uint32_t * size, uint32_t * offset, uint32_t * pitch, int *bo_idx)
+static int
+_tbm_surface_internal_query_plane_data(tbm_surface_h surface,
+ int plane_idx, uint32_t *size, uint32_t *offset, uint32_t *pitch, int *bo_idx)
{
TBM_RETURN_VAL_IF_FAIL(surface, 0);
TBM_RETURN_VAL_IF_FAIL(plane_idx > -1, 0);
struct _tbm_surface *surf = (struct _tbm_surface *)surface;
- struct _tbm_bufmgr *mgr = surf->bufmgr;
+ struct _tbm_bufmgr *bufmgr = surf->bufmgr;
int ret = 0;
+ tbm_error_e error;
- TBM_RETURN_VAL_IF_FAIL(mgr != NULL, 0);
+ TBM_RETURN_VAL_IF_FAIL(bufmgr != NULL, 0);
TBM_RETURN_VAL_IF_FAIL(surf->info.width > 0, 0);
TBM_RETURN_VAL_IF_FAIL(surf->info.height > 0, 0);
TBM_RETURN_VAL_IF_FAIL(surf->info.format > 0, 0);
- if (!mgr->backend->surface_get_plane_data)
- return 0;
-
- ret = mgr->backend->surface_get_plane_data(surf, surf->info.width, surf->info.height, surf->info.format, plane_idx, size, offset, pitch, bo_idx);
- if (!ret)
- return 0;
-
- return 1;
-}
-
-static int _tbm_surface_internal_query_num_bos(tbm_format format)
-{
- TBM_RETURN_VAL_IF_FAIL(format > 0, 0);
- struct _tbm_bufmgr *mgr;
- int ret = 0;
-
- mgr = g_surface_bufmgr;
+ 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;
+ }
- if (!mgr->backend->surface_get_num_bos)
- 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 = mgr->backend->surface_get_num_bos(format);
- if (!ret)
- 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 */
+ }
+ }
return ret;
}
-static void _tbm_surface_internal_destroy(tbm_surface_h surface)
+static void
+_tbm_surface_internal_destroy(tbm_surface_h surface)
{
int i;
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)) {
+ LIST_FOR_EACH_ENTRY_SAFE(old_data, tmp, &surface->user_data_list, item_link) {
+ TBM_DBG("free user_data\n");
+ user_data_delete(old_data);
+ }
+ }
for (i = 0; i < surface->num_bos; i++) {
surface->bos[i]->surface = NULL;
surface->bos[i] = NULL;
}
+ if (!LIST_IS_EMPTY(&surface->debug_data_list)) {
+ LIST_FOR_EACH_ENTRY_SAFE(debug_old_data, debug_tmp, &surface->debug_data_list, item_link)
+ _tbm_surface_internal_debug_data_delete(debug_old_data);
+ }
+
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)) {
+ LIST_FOR_EACH_ENTRY_SAFE(debug_old_data, debug_tmp, &bufmgr->debug_key_list, item_link) {
+ _tbm_surface_internal_debug_data_delete(debug_old_data);
+ }
+ }
+
_deinit_surface_bufmgr();
}
}
-int tbm_surface_internal_query_supported_formats(uint32_t ** formats, uint32_t * num)
+/* LCOV_EXCL_START */
+static int
+_tbm_surface_check_file_is_symbolic_link(const char* path)
{
- struct _tbm_bufmgr *mgr;
- int ret = 0;
-
- _tbm_surface_mutex_lock();
-
- if (!g_surface_bufmgr) {
- _init_surface_bufmgr();
- LIST_INITHEAD(&g_surface_bufmgr->surf_list);
- }
+ struct stat sb;
- mgr = g_surface_bufmgr;
-
- if (!mgr->backend->surface_supported_format) {
- _tbm_surface_mutex_unlock();
+ if (!path)
return 0;
- }
- ret = mgr->backend->surface_supported_format(formats, num);
+ if (stat(path, &sb) != 0)
+ return 0;
- _tbm_surface_mutex_unlock();
+ if (S_ISLNK(sb.st_mode))
+ return 1;
- return ret;
+ return 0;
}
+/* LCOV_EXCL_STOP */
-int tbm_surface_internal_get_num_planes(tbm_format format)
+static int
+_tbm_surface_internal_get_num_planes(tbm_format format)
{
int num_planes = 0;
num_planes = 1;
break;
case TBM_FORMAT_NV12:
+ case TBM_FORMAT_NV12MT:
case TBM_FORMAT_NV21:
case TBM_FORMAT_NV16:
case TBM_FORMAT_NV61:
break;
default:
+ _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
break;
}
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;
switch (format) {
bpp = 32;
break;
case TBM_FORMAT_NV12:
+ case TBM_FORMAT_NV12MT:
case TBM_FORMAT_NV21:
bpp = 12;
break;
bpp = 24;
break;
default:
+ _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
break;
}
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 *mgr;
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_surface_mutex_lock();
-
- if (!g_surface_bufmgr) {
- _init_surface_bufmgr();
- LIST_INITHEAD(&g_surface_bufmgr->surf_list);
- }
+ uint32_t size = 0, offset = 0, stride = 0, bo_size = 0;
+ int i, j, bo_idx;
- mgr = g_surface_bufmgr;
- if (!TBM_BUFMGR_IS_VALID(mgr)) {
- _tbm_surface_mutex_unlock();
- return NULL;
- }
surf = calloc(1, sizeof(struct _tbm_surface));
if (!surf) {
- _tbm_surface_mutex_unlock();
- return NULL;
+ /* LCOV_EXCL_START */
+ TBM_ERR("fail to alloc surf\n");
+ *error = TBM_ERROR_OUT_OF_MEMORY;
+ goto alloc_surf_fail;
+ /* LCOV_EXCL_STOP */
}
- surf->bufmgr = mgr;
+ 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.size = _tbm_surface_internal_query_size(surf);
- surf->info.num_planes = tbm_surface_internal_get_num_planes(format);
- surf->num_bos = _tbm_surface_internal_query_num_bos(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++) {
- _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;
+ }
+
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++) {
if (surf->planes_bo_idx[j] == i)
bo_size += surf->info.planes[j].size;
}
- surf->bos[i] = tbm_bo_alloc(mgr, bo_size, flags);
- if (!surf->bos[i]) {
- for (j = 0; j < i; j++) {
- if (surf->bos[j])
- tbm_bo_unref(surf->bos[j]);
- }
- free(surf);
- surf = NULL;
-
- if (LIST_IS_EMPTY(&mgr->surf_list)) {
- LIST_DELINIT(&mgr->surf_list);
- _deinit_surface_bufmgr();
+ surf->bos[i] = tbm_bo_alloc_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_surface_mutex_unlock();
- return NULL;
}
- _tbm_bo_set_surface(surf->bos[i], surf);
+ _tbm_bo_set_surface(surf->bos[i], surf);
}
- LIST_ADD(&surf->item_link, &mgr->surf_list);
-
- _tbm_surface_mutex_unlock();
+ *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;
}
-tbm_surface_h tbm_surface_internal_create_with_bos(tbm_surface_info_s * info, tbm_bo * bos, int num)
+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)
{
- TBM_RETURN_VAL_IF_FAIL(bos, NULL);
- TBM_RETURN_VAL_IF_FAIL(info, NULL);
- TBM_RETURN_VAL_IF_FAIL(num == 1 || info->num_planes == num, NULL);
-
- struct _tbm_bufmgr *mgr;
struct _tbm_surface *surf = NULL;
- int i;
-
- _tbm_surface_mutex_lock();
+ 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;
- if (!g_surface_bufmgr) {
- _init_surface_bufmgr();
- LIST_INITHEAD(&g_surface_bufmgr->surf_list);
+ 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;
- mgr = g_surface_bufmgr;
- if (!TBM_BUFMGR_IS_VALID(mgr)) {
- _tbm_surface_mutex_unlock();
- return NULL;
+ 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;
- surf = calloc(1, sizeof(struct _tbm_surface));
- if (!surf) {
- _tbm_surface_mutex_unlock();
- return NULL;
+ // 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;
}
- surf->bufmgr = mgr;
- surf->info.width = info->width;
- surf->info.height = info->height;
- surf->info.format = info->format;
- surf->info.bpp = info->bpp;
- 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;
+ 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;
- if (info->planes[i].size > 0)
- surf->info.planes[i].size = info->planes[i].size;
- else
- surf->info.planes[i].size += surf->info.planes[i].stride * info->height;
+ // 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;
+ }
- if (num == 1)
- surf->planes_bo_idx[i] = 0;
- else
- surf->planes_bo_idx[i] = i;
- }
+ // 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;
- if (info->size > 0) {
- surf->info.size = info->size;
- } else {
- surf->info.size = 0;
- for (i = 0; i < info->num_planes; i++)
+ for (i = 0; i < surf->info.num_planes; i++) {
surf->info.size += surf->info.planes[i].size;
- }
- surf->flags = TBM_BO_DEFAULT;
+ if (surf->num_bos < surf->planes_bo_idx[i] + 1)
+ surf->num_bos = surf->planes_bo_idx[i] + 1;
+ }
- /* create only one bo */
- surf->num_bos = num;
- for (i = 0; i < num; i++) {
- if (bos[i] == NULL)
- goto bail1;
+ 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;
+ }
- surf->bos[i] = tbm_bo_ref(bos[i]);
- _tbm_bo_set_surface(bos[i], surf);
- }
+ _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;
+ }
- LIST_ADD(&surf->item_link, &mgr->surf_list);
+ // 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;
- _tbm_surface_mutex_unlock();
+ if (surf->num_bos < surf->planes_bo_idx[i] + 1)
+ surf->num_bos = surf->planes_bo_idx[i] + 1;
+ }
- return surf;
- bail1:
- for (i = 0; i < num; i++) {
- if (surf->bos[i]) {
- tbm_bo_unref(surf->bos[i]);
- surf->bos[i] = NULL;
+ // 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_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", i);
+ *error = tbm_get_last_error();
+ goto get_bo_fail;
+ }
+ }
+
+ _tbm_bo_set_surface(surf->bos[i], surf);
}
}
- free(surf);
- surf = NULL;
+ *error = TBM_ERROR_NONE;
- if (LIST_IS_EMPTY(&g_surface_bufmgr->surf_list)) {
- LIST_DELINIT(&g_surface_bufmgr->surf_list);
- _deinit_surface_bufmgr();
- }
+ return surf;
- _tbm_surface_mutex_unlock();
+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;
}
-void tbm_surface_internal_destroy(tbm_surface_h surface)
+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)
{
- if (!surface)
- return;
-
- _tbm_surface_mutex_lock();
+ 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;
- surface->refcnt--;
+ 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;
- if (surface->refcnt > 0) {
- _tbm_surface_mutex_unlock();
- return;
+ 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;
}
- if (surface->refcnt == 0)
- _tbm_surface_internal_destroy(surface);
+ // 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;
+ }
- _tbm_surface_mutex_unlock();
-}
+ // 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;
-void tbm_surface_internal_ref(tbm_surface_h surface)
-{
- TBM_RETURN_IF_FAIL(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;
+ }
- _tbm_surface_mutex_lock();
+ // 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;
- surface->refcnt++;
+ for (i = 0; i < surf->info.num_planes; i++) {
+ surf->info.size += surf->info.planes[i].size;
- _tbm_surface_mutex_unlock();
-}
+ if (surf->num_bos < surf->planes_bo_idx[i] + 1)
+ surf->num_bos = surf->planes_bo_idx[i] + 1;
+ }
-void tbm_surface_internal_unref(tbm_surface_h surface)
-{
- TBM_RETURN_IF_FAIL(surface);
+ // 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);
+ _tbm_surface_mutex_unlock();
+ return 0;
+ }
+
+ *data = old_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_delete_user_data(tbm_surface_h surface,
+ unsigned long key)
+{
+ 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;
+ }
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) key(%lu) data(%p)\n", surface, key, old_data->data);
+
+ user_data_delete(old_data);
+
+ _tbm_surface_mutex_unlock();
+
+ return 1;
+}
+
+/* LCOV_EXCL_START */
+unsigned int
+_tbm_surface_internal_get_debug_pid(tbm_surface_h surface)
+{
+ TBM_RETURN_VAL_IF_FAIL(surface, 0);
+
+ return surface->debug_pid;
+}
+
+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));
+
+ surface->debug_pid = pid;
+
+ _tbm_surface_mutex_unlock();
+}
+
+static tbm_surface_debug_data *
+_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) {
+ _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);
+
+ 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_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);
+
+ bufmgr = surface->bufmgr;
+
+ TBM_SURFACE_RETURN_VAL_IF_FAIL(bufmgr, 0);
+
+ 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);
+ }
+
+ /* add new debug key to list */
+ debug_data = _tbm_surface_internal_debug_data_find(&bufmgr->debug_key_list, key);
+ if (!debug_data) {
+ 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);
+
+ _tbm_surface_mutex_unlock();
+
+ return 1;
+}
+
+char *
+_tbm_surface_internal_get_debug_data(tbm_surface_h surface, char *key)
+{
+ tbm_surface_debug_data *old_data = NULL;
+
+ TBM_SURFACE_RETURN_VAL_IF_FAIL(surface, NULL);
+
+ if (!LIST_IS_EMPTY(&surface->debug_data_list)) {
+ LIST_FOR_EACH_ENTRY(old_data, &surface->debug_data_list, item_link) {
+ if (!strcmp(old_data->key, key))
+ return old_data->value;
+ }
+ }
+
+ return NULL;
+}
+
+typedef struct _tbm_surface_dump_info tbm_surface_dump_info;
+typedef struct _tbm_surface_dump_buf_info tbm_surface_dump_buf_info;
+
+struct _tbm_surface_dump_buf_info {
+ int index;
+ tbm_bo bo;
+ int size;
+ int dirty;
+ int dirty_shm;
+ int shm_stride;
+ int shm_h;
+ char name[1024];
+
+ tbm_surface_info_s info;
+
+ struct list_head link;
+};
+
+struct _tbm_surface_dump_info {
+ char *path; // copy???
+ int dump_max;
+ int count;
+ struct list_head *link;
+ struct list_head surface_list; /* link of surface */
+};
+
+static tbm_surface_dump_info *g_dump_info = NULL;
+static const char *dump_postfix[2] = {"png", "yuv"};
+static double scale_factor;
+
+static void
+_tbm_surface_internal_dump_file_raw(const char *file, void *data1, int size1,
+ void *data2, int size2, void *data3, int size3)
+{
+ FILE *fp;
+ unsigned int *blocks;
+
+ if (_tbm_surface_check_file_is_symbolic_link(file))
+ TBM_ERR("%s is symbolic link\n", file);
+
+ fp = fopen(file, "w+");
+ TBM_RETURN_IF_FAIL(fp != NULL);
+
+ blocks = (unsigned int *)data1;
+ fwrite(blocks, 1, size1, fp);
+
+ if (size2 > 0) {
+ blocks = (unsigned int *)data2;
+ fwrite(blocks, 1, size2, fp);
+ }
+
+ if (size3 > 0) {
+ blocks = (unsigned int *)data3;
+ fwrite(blocks, 1, size3, fp);
+ }
+
+ fclose(fp);
+}
+
+static void
+_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;
+ int pixel_size;
+ png_bytep *row_pointers;
+ int depth = 8, y;
+
+ if (_tbm_surface_check_file_is_symbolic_link(file))
+ TBM_ERR("%s is symbolic link\n", file);
+
+ fp = fopen(file, "wb");
+ TBM_RETURN_IF_FAIL(fp != NULL);
+
+ png_structp pPngStruct = png_create_write_struct(PNG_LIBPNG_VER_STRING,
+ NULL, NULL, NULL);
+ if (!pPngStruct) {
+ TBM_ERR("fail to create a png write structure.\n");
+ fclose(fp);
+ return;
+ }
+
+ png_infop pPngInfo = png_create_info_struct(pPngStruct);
+ if (!pPngInfo) {
+ TBM_ERR("fail to create a png info structure.\n");
+ png_destroy_write_struct(&pPngStruct, NULL);
+ fclose(fp);
+ 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;
+ png_set_IHDR(pPngStruct,
+ pPngInfo,
+ width,
+ height,
+ depth,
+ PNG_COLOR_TYPE_RGB,
+ PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+ } else {
+ pixel_size = 4;
+ png_set_IHDR(pPngStruct,
+ pPngInfo,
+ width,
+ height,
+ depth,
+ PNG_COLOR_TYPE_RGBA,
+ PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+ }
+
+ png_set_bgr(pPngStruct);
+ png_write_info(pPngStruct, pPngInfo);
+
+ row_pointers = png_malloc(pPngStruct, height * sizeof(png_byte *));
+ if (!row_pointers) {
+ TBM_ERR("fail to allocate the png row_pointers.\n");
+ png_destroy_write_struct(&pPngStruct, &pPngInfo);
+ fclose(fp);
+ return;
+ }
+
+ for (y = 0; y < height; ++y) {
+ png_bytep row;
+ int x = 0;
+
+ row = png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
+ if (!row) {
+ TBM_ERR("fail to allocate the png row.\n");
+ for (x = 0; x < y; x++)
+ png_free(pPngStruct, row_pointers[x]);
+ png_free(pPngStruct, row_pointers);
+ png_destroy_write_struct(&pPngStruct, &pPngInfo);
+ fclose(fp);
+ return;
+ }
+ row_pointers[y] = (png_bytep)row;
+
+ for (x = 0; x < width; ++x) {
+ unsigned int curBlock = blocks[(y * (stride >> 2)) + x];
+
+ if (pixel_size == 3) { // XRGB8888
+ row[x * pixel_size] = (curBlock & 0xFF);
+ row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
+ row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
+ } else { // ARGB8888
+ row[x * pixel_size] = (curBlock & 0xFF);
+ row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
+ row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
+ row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
+ }
+ }
+ }
+
+ png_write_image(pPngStruct, row_pointers);
+ png_write_end(pPngStruct, pPngInfo);
+
+ for (y = 0; y < height; y++)
+ png_free(pPngStruct, row_pointers[y]);
+ png_free(pPngStruct, row_pointers);
+
+ png_destroy_write_struct(&pPngStruct, &pPngInfo);
+
+ fclose(fp);
+}
+
+void
+tbm_surface_internal_dump_start(char *path, int w, int h, int count)
+{
+ TBM_RETURN_IF_FAIL(path != NULL);
+ TBM_RETURN_IF_FAIL(w > 0);
+ TBM_RETURN_IF_FAIL(h > 0);
+ TBM_RETURN_IF_FAIL(count > 0);
+
+ tbm_surface_dump_buf_info *buf_info = NULL;
+ tbm_surface_h tbm_surface;
+ tbm_surface_info_s info;
+ int buffer_size, i;
+
+ /* check running */
+ if (g_dump_info) {
+ TBM_WRN("waring already running the tbm_surface_internal_dump.\n");
+ return;
+ }
+
+ g_dump_info = calloc(1, sizeof(struct _tbm_surface_dump_info));
+ TBM_RETURN_IF_FAIL(g_dump_info);
+
+ LIST_INITHEAD(&g_dump_info->surface_list);
+ g_dump_info->count = 0;
+ g_dump_info->dump_max = count;
+
+ /* get buffer size */
+ tbm_surface = tbm_surface_create(w, h, TBM_FORMAT_ARGB8888);
+ if (tbm_surface == NULL) {
+ TBM_ERR("tbm_surface_create fail\n");
+ free(g_dump_info);
+ g_dump_info = NULL;
+ return;
+ }
+
+ if (TBM_SURFACE_ERROR_NONE != tbm_surface_get_info(tbm_surface, &info)) {
+ TBM_ERR("tbm_surface_get_info fail\n");
+ tbm_surface_destroy(tbm_surface);
+ free(g_dump_info);
+ g_dump_info = NULL;
+ return;
+ }
+ buffer_size = info.size;
+ tbm_surface_destroy(tbm_surface);
+
+ /* create dump lists */
+ for (i = 0; i < count; i++) {
+ tbm_bo bo = NULL;
+
+ buf_info = calloc(1, sizeof(tbm_surface_dump_buf_info));
+ TBM_GOTO_VAL_IF_FAIL(buf_info, fail);
+
+ bo = tbm_bo_alloc(g_surface_bufmgr, buffer_size, TBM_BO_DEFAULT);
+ if (bo == NULL) {
+ TBM_ERR("fail to allocate the tbm_bo[%d]\n", i);
+ free(buf_info);
+ goto fail;
+ }
+
+ buf_info->index = i;
+ buf_info->bo = bo;
+ buf_info->size = buffer_size;
+
+ LIST_ADDTAIL(&buf_info->link, &g_dump_info->surface_list);
+ }
+
+ g_dump_info->path = path;
+ g_dump_info->link = &g_dump_info->surface_list;
+
+ scale_factor = 0.0;
+
+ TBM_INFO("Dump Start.. path:%s, count:%d\n", g_dump_info->path, count);
+
+ return;
+
+fail:
+ /* free resources */
+ if (!LIST_IS_EMPTY(&g_dump_info->surface_list)) {
+ tbm_surface_dump_buf_info *tmp;
+
+ LIST_FOR_EACH_ENTRY_SAFE(buf_info, tmp, &g_dump_info->surface_list, link) {
+ tbm_bo_unref(buf_info->bo);
+ LIST_DEL(&buf_info->link);
+ free(buf_info);
+ }
+ }
+
+ TBM_ERR("Dump Start fail.. path:%s\n", g_dump_info->path);
+
+ free(g_dump_info);
+ g_dump_info = NULL;
+
+ return;
+}
+
+void
+tbm_surface_internal_dump_with_scale_start(char *path, int w, int h, int count, double scale)
+{
+ if (scale > 0.0) {
+ w *= scale;
+ h *= scale;
+ }
+
+ tbm_surface_internal_dump_start(path, w, h, count);
+ scale_factor = scale;
+}
- surface->refcnt--;
+void
+tbm_surface_internal_dump_end(void)
+{
+ tbm_surface_dump_buf_info *buf_info = NULL, *tmp = NULL;
+ tbm_bo_handle bo_handle;
- if (surface->refcnt > 0) {
- _tbm_surface_mutex_unlock();
+ if (!g_dump_info)
+ return;
+
+ if (LIST_IS_EMPTY(&g_dump_info->surface_list)) {
+ free(g_dump_info);
+ g_dump_info = NULL;
return;
}
- if (surface->refcnt == 0)
- _tbm_surface_internal_destroy(surface);
+ /* make files */
+ LIST_FOR_EACH_ENTRY_SAFE(buf_info, tmp, &g_dump_info->surface_list, link) {
+ char file[2048];
- _tbm_surface_mutex_unlock();
+ bo_handle = tbm_bo_map(buf_info->bo, TBM_DEVICE_CPU, TBM_OPTION_READ);
+ if (bo_handle.ptr == NULL) {
+ tbm_bo_unref(buf_info->bo);
+ LIST_DEL(&buf_info->link);
+ free(buf_info);
+ continue;
+ }
+
+ snprintf(file, sizeof(file), "%s/%s", g_dump_info->path, buf_info->name);
+ TBM_INFO("Dump File.. %s generated.\n", file);
+
+ if (buf_info->dirty) {
+ void *ptr1 = NULL, *ptr2 = NULL;
+
+ switch (buf_info->info.format) {
+ 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,
+ 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,
+ buf_info->info.planes[0].stride,
+ TBM_FORMAT_XRGB8888);
+ break;
+ case TBM_FORMAT_YVU420:
+ case TBM_FORMAT_YUV420:
+ ptr1 = bo_handle.ptr + buf_info->info.planes[0].stride * buf_info->info.height;
+ ptr2 = ptr1 + buf_info->info.planes[1].stride * (buf_info->info.height >> 1);
+ _tbm_surface_internal_dump_file_raw(file, bo_handle.ptr,
+ buf_info->info.planes[0].stride * buf_info->info.height,
+ ptr1,
+ buf_info->info.planes[1].stride * (buf_info->info.height >> 1),
+ ptr2,
+ buf_info->info.planes[2].stride * (buf_info->info.height >> 1));
+ break;
+ case TBM_FORMAT_NV12:
+ case TBM_FORMAT_NV21:
+ ptr1 = bo_handle.ptr + buf_info->info.planes[0].stride * buf_info->info.height;
+ _tbm_surface_internal_dump_file_raw(file, bo_handle.ptr,
+ buf_info->info.planes[0].stride * buf_info->info.height,
+ ptr1,
+ buf_info->info.planes[1].stride * (buf_info->info.height >> 1),
+ NULL, 0);
+ break;
+ case TBM_FORMAT_YUYV:
+ case TBM_FORMAT_UYVY:
+ _tbm_surface_internal_dump_file_raw(file, bo_handle.ptr,
+ buf_info->info.planes[0].stride * buf_info->info.height,
+ NULL, 0, NULL, 0);
+ break;
+ default:
+ TBM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR(buf_info->info.format));
+ break;
+ }
+ } 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,
+ buf_info->shm_stride, 0);
+
+ tbm_bo_unmap(buf_info->bo);
+ tbm_bo_unref(buf_info->bo);
+ LIST_DEL(&buf_info->link);
+ free(buf_info);
+ }
+
+ free(g_dump_info);
+ g_dump_info = NULL;
+
+ TBM_INFO("Dump End..\n");
}
-int tbm_surface_internal_get_num_bos(tbm_surface_h surface)
+static pixman_format_code_t
+_tbm_surface_internal_pixman_format_get(tbm_format format)
{
- TBM_RETURN_VAL_IF_FAIL(surface, 0);
+ switch (format) {
+ case TBM_FORMAT_ARGB8888:
+ return PIXMAN_a8r8g8b8;
+ case TBM_FORMAT_XRGB8888:
+ return PIXMAN_x8r8g8b8;
+ default:
+ return 0;
+ }
- struct _tbm_surface *surf;
- int num;
+ return 0;
+}
- _tbm_surface_mutex_lock();
+/**
+ * This function supports only if a buffer has below formats.
+ * - TBM_FORMAT_ARGB8888
+ * - TBM_FORMAT_XRGB8888
+ */
+static tbm_surface_error_e
+_tbm_surface_internal_buffer_scale(void *src_ptr, void *dst_ptr,
+ int format, int src_stride, int src_w, int src_h,
+ int dst_stride, int dst_w, int dst_h)
+{
+ pixman_image_t *src_img = NULL, *dst_img = NULL;
+ pixman_format_code_t pixman_format;
+ pixman_transform_t t;
+ struct pixman_f_transform ft;
+ double scale_x, scale_y;
- surf = (struct _tbm_surface *)surface;
- num = surf->num_bos;
+ TBM_RETURN_VAL_IF_FAIL(src_ptr != NULL, TBM_SURFACE_ERROR_INVALID_OPERATION);
+ TBM_RETURN_VAL_IF_FAIL(dst_ptr != NULL, TBM_SURFACE_ERROR_INVALID_OPERATION);
- _tbm_surface_mutex_unlock();
+ pixman_format = _tbm_surface_internal_pixman_format_get(format);
+ TBM_RETURN_VAL_IF_FAIL(pixman_format > 0, TBM_SURFACE_ERROR_INVALID_OPERATION);
- return num;
+ /* src */
+ src_img = pixman_image_create_bits(pixman_format, src_w, src_h,
+ (uint32_t*)src_ptr, src_stride);
+ TBM_GOTO_VAL_IF_FAIL(src_img != NULL, cant_convert);
+
+ /* dst */
+ dst_img = pixman_image_create_bits(pixman_format, dst_w, dst_h,
+ (uint32_t*)dst_ptr, dst_stride);
+ TBM_GOTO_VAL_IF_FAIL(dst_img != NULL, cant_convert);
+
+ pixman_f_transform_init_identity(&ft);
+
+ scale_x = (double)src_w / dst_w;
+ scale_y = (double)src_h / dst_h;
+
+ pixman_f_transform_scale(&ft, NULL, scale_x, scale_y);
+ pixman_f_transform_translate(&ft, NULL, 0, 0);
+ pixman_transform_from_pixman_f_transform(&t, &ft);
+ pixman_image_set_transform(src_img, &t);
+
+ pixman_image_composite(PIXMAN_OP_SRC, src_img, NULL, dst_img,
+ 0, 0, 0, 0, 0, 0, dst_w, dst_h);
+
+ pixman_image_unref(src_img);
+ pixman_image_unref(dst_img);
+
+ return TBM_SURFACE_ERROR_NONE;
+
+cant_convert:
+ if (src_img)
+ pixman_image_unref(src_img);
+
+ return TBM_SURFACE_ERROR_INVALID_OPERATION;
}
-tbm_bo tbm_surface_internal_get_bo(tbm_surface_h surface, int bo_idx)
-{
- TBM_RETURN_VAL_IF_FAIL(surface, NULL);
- TBM_RETURN_VAL_IF_FAIL(bo_idx > -1, NULL);
+#define MAX_BOS 4 // This value is came from bos[4] in struct _tbm_surface
+#define KEY_LEN 5 // "_XXXX"
+#define KEYS_LEN KEY_LEN * MAX_BOS
+static char *_tbm_surface_internal_get_keys(tbm_surface_h surface)
+{
+ char *keys, temp_key[KEY_LEN + 1];
struct _tbm_surface *surf;
+ int i, num_bos;
tbm_bo bo;
_tbm_surface_mutex_lock();
+ TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), NULL);
+
surf = (struct _tbm_surface *)surface;
- bo = surf->bos[bo_idx];
+
+ num_bos = surf->num_bos;
+ if (num_bos > MAX_BOS)
+ num_bos = MAX_BOS;
+
+ keys = calloc(KEYS_LEN + 1, sizeof(char));
+ if (!keys) {
+ TBM_ERR("Failed to alloc memory");
+ _tbm_surface_mutex_unlock();
+ return NULL;
+ }
+
+ for (i = 0; i < num_bos; i++) {
+ 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 + 1);
+ }
_tbm_surface_mutex_unlock();
- return bo;
+ return keys;
}
-int tbm_surface_internal_get_size(tbm_surface_h surface)
+static void _tbm_surface_internal_put_keys(char *keys)
{
- TBM_RETURN_VAL_IF_FAIL(surface, 0);
+ if (keys)
+ free(keys);
+}
- struct _tbm_surface *surf;
- unsigned int size;
+void
+tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
+{
+ TBM_RETURN_IF_FAIL(surface != NULL);
+ TBM_RETURN_IF_FAIL(type != NULL);
+
+ tbm_surface_dump_buf_info *buf_info;
+ struct list_head *next_link;
+ tbm_surface_info_s info;
+ tbm_bo_handle bo_handle;
+ const char *postfix;
+ const char *format = NULL;
+ char *keys;
+ int ret;
+
+ if (!g_dump_info)
+ return;
- _tbm_surface_mutex_lock();
+ next_link = g_dump_info->link->next;
+ TBM_RETURN_IF_FAIL(next_link != NULL);
- surf = (struct _tbm_surface *)surface;
- size = surf->info.size;
+ if (next_link == &g_dump_info->surface_list) {
+ next_link = next_link->next;
+ TBM_RETURN_IF_FAIL(next_link != NULL);
+ }
- _tbm_surface_mutex_unlock();
+ buf_info = LIST_ENTRY(tbm_surface_dump_buf_info, next_link, link);
+ TBM_RETURN_IF_FAIL(buf_info != NULL);
- return size;
-}
+ ret = tbm_surface_map(surface, TBM_SURF_OPTION_READ|TBM_SURF_OPTION_WRITE, &info);
+ TBM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
-int tbm_surface_internal_get_plane_data(tbm_surface_h surface, int plane_idx, uint32_t * size, uint32_t * offset, uint32_t * pitch)
-{
- TBM_RETURN_VAL_IF_FAIL(surface, 0);
- TBM_RETURN_VAL_IF_FAIL(plane_idx > -1, 0);
+ if (scale_factor > 0.0) {
+ const int bpp = 4;
- struct _tbm_surface *surf;
+ if (info.format != TBM_FORMAT_ARGB8888 && info.format != TBM_FORMAT_XRGB8888) {
+ TBM_WRN("Dump with scale skip. unsupported format(%s)\n",
+ _tbm_surface_internal_format_to_str(info.format));
+ tbm_surface_unmap(surface);
+ return;
+ }
- _tbm_surface_mutex_lock();
+ memset(&buf_info->info, 0, sizeof(tbm_surface_info_s));
- surf = (struct _tbm_surface *)surface;
+ 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);
+ 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;
+
+ if (buf_info->info.size > buf_info->size) {
+ TBM_WRN("Dump with scale skip. surface over created buffer size(%u, %d)\n",
+ buf_info->info.size, buf_info->size);
+ tbm_surface_unmap(surface);
+ return;
+ }
+ } else {
+ if (info.size > buf_info->size) {
+ TBM_WRN("Dump skip. surface over created buffer size(%u, %d)\n",
+ info.size, buf_info->size);
+ tbm_surface_unmap(surface);
+ return;
+ }
- if (plane_idx >= surf->info.num_planes) {
- _tbm_surface_mutex_unlock();
- return 0;
+ /* make the file information */
+ memcpy(&buf_info->info, &info, sizeof(tbm_surface_info_s));
}
- *size = surf->info.planes[plane_idx].size;
- *offset = surf->info.planes[plane_idx].offset;
- *pitch = surf->info.planes[plane_idx].stride;
+ if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888) {
+ postfix = dump_postfix[0];
+ format = _tbm_surface_internal_format_to_str(info.format);
+ } else
+ postfix = dump_postfix[1];
- _tbm_surface_mutex_unlock();
+ keys = _tbm_surface_internal_get_keys(surface);
+ if (!keys) {
+ TBM_ERR("fail to get keys");
+ tbm_surface_unmap(surface);
+ return;
+ }
- return 1;
+ /* dump */
+ bo_handle = tbm_bo_map(buf_info->bo, TBM_DEVICE_CPU, TBM_OPTION_WRITE);
+ if (!bo_handle.ptr) {
+ TBM_ERR("fail to map bo");
+ _tbm_surface_internal_put_keys(keys);
+ tbm_surface_unmap(surface);
+ return;
+ }
+ memset(bo_handle.ptr, 0x00, buf_info->size);
+
+ switch (info.format) {
+ case TBM_FORMAT_ARGB8888:
+ case TBM_FORMAT_XRGB8888:
+ snprintf(buf_info->name, sizeof(buf_info->name),
+ "%10.3f_%03d%s_%p_%s-%s.%s",
+ _tbm_surface_internal_get_time(),
+ g_dump_info->count++, keys, surface, format, type, postfix);
+
+ if (scale_factor > 0.0) {
+ ret = _tbm_surface_internal_buffer_scale(info.planes[0].ptr,
+ bo_handle.ptr,
+ buf_info->info.format,
+ info.planes[0].stride,
+ info.width, info.height,
+ buf_info->info.planes[0].stride,
+ buf_info->info.width,
+ buf_info->info.height);
+ if (ret != TBM_SURFACE_ERROR_NONE) {
+ TBM_ERR("fail to scale buffer");
+ tbm_bo_unmap(buf_info->bo);
+ _tbm_surface_internal_put_keys(keys);
+ tbm_surface_unmap(surface);
+ return;
+ }
+ } else
+ memcpy(bo_handle.ptr, info.planes[0].ptr, info.size);
+ break;
+ case TBM_FORMAT_YVU420:
+ case TBM_FORMAT_YUV420:
+ snprintf(buf_info->name, sizeof(buf_info->name),
+ "%10.3f_%03d%s-%s_%dx%d_%c%c%c%c.%s",
+ _tbm_surface_internal_get_time(),
+ g_dump_info->count++, keys, type, info.planes[0].stride,
+ info.height, FOURCC_STR(info.format), postfix);
+ memcpy(bo_handle.ptr, info.planes[0].ptr, info.planes[0].stride * info.height);
+ bo_handle.ptr += info.planes[0].stride * info.height;
+ memcpy(bo_handle.ptr, info.planes[1].ptr, info.planes[1].stride * (info.height >> 1));
+ bo_handle.ptr += info.planes[1].stride * (info.height >> 1);
+ memcpy(bo_handle.ptr, info.planes[2].ptr, info.planes[2].stride * (info.height >> 1));
+ break;
+ case TBM_FORMAT_NV12:
+ case TBM_FORMAT_NV21:
+ snprintf(buf_info->name, sizeof(buf_info->name),
+ "%10.3f_%03d%s-%s_%dx%d_%c%c%c%c.%s",
+ _tbm_surface_internal_get_time(),
+ g_dump_info->count++, keys, type, info.planes[0].stride,
+ info.height, FOURCC_STR(info.format), postfix);
+ memcpy(bo_handle.ptr, info.planes[0].ptr, info.planes[0].stride * info.height);
+ bo_handle.ptr += info.planes[0].stride * info.height;
+ memcpy(bo_handle.ptr, info.planes[1].ptr, info.planes[1].stride * (info.height >> 1));
+ break;
+ case TBM_FORMAT_YUYV:
+ case TBM_FORMAT_UYVY:
+ snprintf(buf_info->name, sizeof(buf_info->name),
+ "%10.3f_%03d%s-%s_%dx%d_%c%c%c%c.%s",
+ _tbm_surface_internal_get_time(),
+ g_dump_info->count++, keys, type, info.planes[0].stride,
+ info.height, FOURCC_STR(info.format), postfix);
+ memcpy(bo_handle.ptr, info.planes[0].ptr, info.planes[0].stride * info.height);
+ break;
+ default:
+ TBM_ERR("can't copy %c%c%c%c buffer", FOURCC_STR(info.format));
+ tbm_bo_unmap(buf_info->bo);
+ _tbm_surface_internal_put_keys(keys);
+ tbm_surface_unmap(surface);
+ return;
+ }
+
+ tbm_bo_unmap(buf_info->bo);
+
+ _tbm_surface_internal_put_keys(keys);
+
+ tbm_surface_unmap(surface);
+
+ buf_info->dirty = 1;
+ buf_info->dirty_shm = 0;
+
+ if (g_dump_info->count == 1000)
+ g_dump_info->count = 0;
+
+ g_dump_info->link = next_link;
+
+ TBM_INFO("Dump %s \n", buf_info->name);
}
-int tbm_surface_internal_get_info(tbm_surface_h surface, int opt, tbm_surface_info_s * info, int map)
+void tbm_surface_internal_dump_shm_buffer(void *ptr, int w, int h, int stride,
+ const char *type)
{
- struct _tbm_surface *surf;
- tbm_bo_handle bo_handles[4];
- int i, j;
+ TBM_RETURN_IF_FAIL(ptr != NULL);
+ TBM_RETURN_IF_FAIL(w > 0);
+ TBM_RETURN_IF_FAIL(h > 0);
+ TBM_RETURN_IF_FAIL(stride > 0);
+ TBM_RETURN_IF_FAIL(type != NULL);
+
+ tbm_surface_dump_buf_info *buf_info;
+ struct list_head *next_link;
+ tbm_bo_handle bo_handle;
+ int ret, size, dw = 0, dh = 0, dstride = 0;
+
+ if (!g_dump_info)
+ return;
- _tbm_surface_mutex_lock();
+ next_link = g_dump_info->link->next;
+ TBM_RETURN_IF_FAIL(next_link != NULL);
- memset(bo_handles, 0, sizeof(tbm_bo_handle) * 4);
+ if (next_link == &g_dump_info->surface_list) {
+ next_link = next_link->next;
+ TBM_RETURN_IF_FAIL(next_link != NULL);
+ }
- surf = (struct _tbm_surface *)surface;
+ buf_info = LIST_ENTRY(tbm_surface_dump_buf_info, next_link, link);
+ TBM_RETURN_IF_FAIL(buf_info != NULL);
- 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;
+ if (scale_factor > 0.0) {
+ const int bpp = 4;
- if (map == 1) {
- for (i = 0; i < surf->num_bos; i++) {
- bo_handles[i] = tbm_bo_map(surf->bos[i], TBM_DEVICE_CPU, opt);
- if (bo_handles[i].ptr == NULL) {
- for (j = 0; j < i; j++)
- tbm_bo_unmap(surf->bos[j]);
+ dw = w * scale_factor;
+ dh = h * scale_factor;
+ dstride = dw * bpp;
+ size = dstride * dh;
+ } else
+ size = stride * h;
- _tbm_surface_mutex_unlock();
- return 0;
- }
+ if (size > buf_info->size) {
+ TBM_WRN("Dump skip. shm buffer over created buffer size(%d, %d)\n",
+ size, buf_info->size);
+ return;
+ }
+
+ /* dump */
+ bo_handle = tbm_bo_map(buf_info->bo, TBM_DEVICE_CPU, TBM_OPTION_WRITE);
+ TBM_RETURN_IF_FAIL(bo_handle.ptr != NULL);
+
+ memset(bo_handle.ptr, 0x00, buf_info->size);
+ memset(&buf_info->info, 0x00, sizeof(tbm_surface_info_s));
+
+ snprintf(buf_info->name, sizeof(buf_info->name), "%10.3f_%03d-%s.%s",
+ _tbm_surface_internal_get_time(),
+ g_dump_info->count++, type, dump_postfix[0]);
+ if (scale_factor > 0.0) {
+ ret = _tbm_surface_internal_buffer_scale(ptr, bo_handle.ptr,
+ TBM_FORMAT_ARGB8888, stride,
+ w, h, dstride, dw, dh);
+ if (ret != TBM_SURFACE_ERROR_NONE) {
+ TBM_ERR("fail to scale buffer");
+ tbm_bo_unmap(buf_info->bo);
+ return;
}
+ buf_info->shm_stride = dstride;
+ buf_info->shm_h = dh;
} else {
- for (i = 0; i < surf->num_bos; i++) {
- bo_handles[i] = tbm_bo_get_handle(surf->bos[i], TBM_DEVICE_CPU);
- if (bo_handles[i].ptr == NULL) {
- _tbm_surface_mutex_unlock();
- return 0;
- }
- }
+ memcpy(bo_handle.ptr, ptr, size);
+ buf_info->shm_stride = stride;
+ buf_info->shm_h = h;
}
- 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;
- info->planes[i].ptr = bo_handles[surf->planes_bo_idx[i]].ptr + surf->info.planes[i].offset;
+ tbm_bo_unmap(buf_info->bo);
+
+ buf_info->dirty = 0;
+ buf_info->dirty_shm = 1;
+
+ if (g_dump_info->count == 1000)
+ g_dump_info->count = 0;
+
+ g_dump_info->link = next_link;
+
+ TBM_INFO("Dump %s \n", buf_info->name);
+}
+
+int
+tbm_surface_internal_capture_buffer(tbm_surface_h surface, const char *path, const char *name, const char *type)
+{
+ TBM_RETURN_VAL_IF_FAIL(surface != NULL, 0);
+ TBM_RETURN_VAL_IF_FAIL(path != NULL, 0);
+ TBM_RETURN_VAL_IF_FAIL(name != NULL, 0);
+
+ tbm_surface_info_s info;
+ const char *postfix;
+ int ret;
+ char file[1024];
+
+ ret = tbm_surface_map(surface, TBM_SURF_OPTION_READ|TBM_SURF_OPTION_WRITE, &info);
+ TBM_RETURN_VAL_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE, 0);
+
+ if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
+ postfix = dump_postfix[0];
+ else
+ postfix = dump_postfix[1];
+
+ if (strcmp(postfix, type)) {
+ TBM_ERR("not support type(%s) %c%c%c%c buffer", type, FOURCC_STR(info.format));
+ tbm_surface_unmap(surface);
+ return 0;
}
- _tbm_surface_mutex_unlock();
+ snprintf(file, sizeof(file), "%s/%s.%s", path , name, postfix);
+
+ if (!access(file, 0)) {
+ TBM_ERR("can't capture buffer, exist file %s", file);
+ tbm_surface_unmap(surface);
+ return 0;
+ }
+
+ switch (info.format) {
+ case TBM_FORMAT_ARGB8888:
+ _tbm_surface_internal_dump_file_png(file, info.planes[0].ptr,
+ 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.width,
+ info.height,
+ info.planes[0].stride,
+ TBM_FORMAT_XRGB8888);
+ break;
+ case TBM_FORMAT_YVU420:
+ case TBM_FORMAT_YUV420:
+ _tbm_surface_internal_dump_file_raw(file, info.planes[0].ptr,
+ info.planes[0].stride * info.height,
+ info.planes[1].ptr,
+ info.planes[1].stride * (info.height >> 1),
+ info.planes[2].ptr,
+ info.planes[2].stride * (info.height >> 1));
+ break;
+ case TBM_FORMAT_NV12:
+ case TBM_FORMAT_NV21:
+ _tbm_surface_internal_dump_file_raw(file, info.planes[0].ptr,
+ info.planes[0].stride * info.height,
+ info.planes[1].ptr,
+ info.planes[1].stride * (info.height >> 1),
+ NULL, 0);
+ break;
+ case TBM_FORMAT_YUYV:
+ case TBM_FORMAT_UYVY:
+ _tbm_surface_internal_dump_file_raw(file, info.planes[0].ptr,
+ info.planes[0].stride * info.height,
+ NULL, 0, NULL, 0);
+ break;
+ default:
+ TBM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR(info.format));
+ tbm_surface_unmap(surface);
+ return 0;
+ }
+
+ tbm_surface_unmap(surface);
+
+ TBM_TRACE_SURFACE_INTERNAL("Capture %s \n", file);
+
+ return 1;
+}
+
+int
+tbm_surface_internal_capture_shm_buffer(void *ptr, int w, int h, int stride,
+ const char *path, const char *name, const char *type)
+{
+ TBM_RETURN_VAL_IF_FAIL(ptr != NULL, 0);
+ TBM_RETURN_VAL_IF_FAIL(w > 0, 0);
+ TBM_RETURN_VAL_IF_FAIL(h > 0, 0);
+ TBM_RETURN_VAL_IF_FAIL(stride > 0, 0);
+ TBM_RETURN_VAL_IF_FAIL(path != NULL, 0);
+ TBM_RETURN_VAL_IF_FAIL(name != NULL, 0);
+
+ char file[1024];
+
+ if (strcmp(dump_postfix[0], type)) {
+ TBM_ERR("Not supported type:%s'", type);
+ return 0;
+ }
+
+ snprintf(file, sizeof(file), "%s/%s.%s", path , name, dump_postfix[0]);
+
+ if (!access(file, 0)) {
+ TBM_ERR("can't capture buffer, exist file %sTBM_FORMAT_XRGB8888", file);
+ return 0;
+ }
+
+ _tbm_surface_internal_dump_file_png(file, ptr, w, h, stride, 0);
+
+ TBM_TRACE_SURFACE_INTERNAL("Capture %s \n", file);
return 1;
}
-void tbm_surface_internal_unmap(tbm_surface_h surface)
+int
+tbm_surface_internal_set_damage(tbm_surface_h surface, int x, int y, int width, int height)
{
struct _tbm_surface *surf;
- int i;
_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;
- for (i = 0; i < surf->num_bos; i++)
- tbm_bo_unmap(surf->bos[i]);
+ 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;
}
-unsigned int tbm_surface_internal_get_width(tbm_surface_h surface)
+int
+tbm_surface_internal_get_damage(tbm_surface_h surface, int *x, int *y, int *width, int *height)
{
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;
+
+ 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 width;
+ return 1;
}
-unsigned int tbm_surface_internal_get_height(tbm_surface_h surface)
+int
+tbm_surface_internal_add_destroy_handler(tbm_surface_h surface, tbm_surface_internal_destroy_handler func, void *user_data)
{
struct _tbm_surface *surf;
- unsigned int height;
+ 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;
- height = surf->info.height;
+ 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;
+ }
+ }
- _tbm_surface_mutex_unlock();
+ 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;
+ }
- return height;
+ 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;
}
-tbm_format tbm_surface_internal_get_format(tbm_surface_h surface)
+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_format format;
+ 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;
- format = surf->info.format;
+ 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;
- _tbm_surface_mutex_unlock();
+ LIST_DEL(&func_info->item_link);
+ free(func_info);
- return format;
+ _tbm_surface_mutex_unlock();
+
+ return;
+ }
+
+ _tbm_surface_mutex_unlock();
}
-int tbm_surface_internal_get_plane_bo_idx(tbm_surface_h surface, int plane_idx)
+tbm_surface_buffer_data *
+tbm_surface_internal_export(tbm_surface_h surface, tbm_error_e *error)
{
- TBM_RETURN_VAL_IF_FAIL(surface, 0);
- TBM_RETURN_VAL_IF_FAIL(plane_idx > -1, 0);
+ tbm_surface_buffer_data *buffer_data = NULL;
struct _tbm_surface *surf;
- int bo_idx;
+ struct _tbm_bufmgr *bufmgr;
_tbm_surface_mutex_lock();
surf = (struct _tbm_surface *)surface;
- bo_idx = surf->planes_bo_idx[plane_idx];
+ 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 bo_idx;
+ return buffer_data;
}
-unsigned int _tbm_surface_internal_get_debug_pid(tbm_surface_h surface)
+tbm_surface_h
+tbm_surface_internal_import(tbm_surface_info_s *surface_info, tbm_surface_buffer_data *buffer_data, tbm_error_e *error)
{
- TBM_RETURN_VAL_IF_FAIL(surface, 0);
+ struct _tbm_surface *surf;
+ struct _tbm_bufmgr *bufmgr;
- return surface->debug_pid;
-}
+ _tbm_surface_mutex_lock();
-void tbm_surface_internal_set_debug_pid(tbm_surface_h surface, unsigned int pid)
-{
- TBM_RETURN_IF_FAIL(surface);
+ bufmgr = g_surface_bufmgr;
+ TBM_SURFACE_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr != NULL, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
- surface->debug_pid = pid;
+ // 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*/