+static struct _tbm_surface *
+_tbm_surface_internal_create_surface(tbm_bufmgr bufmgr, int width, int height, int format, int flags, tbm_error_e *error)
+{
+ struct _tbm_surface *surf = NULL;
+ uint32_t size = 0, offset = 0, stride = 0, bo_size = 0;
+ int i, j, bo_idx;
+
+ surf = calloc(1, sizeof(struct _tbm_surface));
+ if (!surf) {
+ /* LCOV_EXCL_START */
+ TBM_ERR("fail to alloc surf\n");
+ *error = TBM_ERROR_OUT_OF_MEMORY;
+ goto alloc_surf_fail;
+ /* LCOV_EXCL_STOP */
+ }
+
+ surf->magic = TBM_SURFACE_MAGIC;
+ surf->bufmgr = bufmgr;
+ surf->info.width = width;
+ surf->info.height = height;
+ surf->info.format = format;
+ surf->info.bpp = _tbm_surface_internal_get_bpp(format);
+ if (!surf->info.bpp) {
+ TBM_ERR("fail to get bpp from format(%d), error(%s)\n", format, tbm_error_str(*error));
+ *error = tbm_get_last_error();
+ goto bpp_fail;
+ }
+
+ surf->info.num_planes = _tbm_surface_internal_get_num_planes(format);
+ if (!surf->info.num_planes) {
+ TBM_ERR("fail to get num_planes from format(%d), error(%s)\n", format, tbm_error_str(*error));
+ *error = tbm_get_last_error();
+ goto num_planes_fail;
+ }
+ surf->refcnt = 1;
+
+ /* get size, stride and offset bo_idx */
+ for (i = 0; i < surf->info.num_planes; i++) {
+ if (!_tbm_surface_internal_query_plane_data(surf, i, &size, &offset, &stride, &bo_idx)) {
+ TBM_ERR("fail to query plane data\n");
+ *error = tbm_get_last_error();
+ goto query_plane_data_fail;
+ }
+
+ surf->info.planes[i].size = size;
+ surf->info.planes[i].offset = offset;
+ surf->info.planes[i].stride = stride;
+ surf->planes_bo_idx[i] = bo_idx;
+ }
+
+ surf->num_bos = 1;
+
+ for (i = 0; i < surf->info.num_planes; i++) {
+ surf->info.size += surf->info.planes[i].size;
+
+ if (surf->num_bos < surf->planes_bo_idx[i] + 1)
+ surf->num_bos = surf->planes_bo_idx[i] + 1;
+ }
+
+ surf->flags = flags;
+
+ for (i = 0; i < surf->num_bos; i++) {
+ bo_size = 0;
+ for (j = 0; j < surf->info.num_planes; j++) {
+ if (surf->planes_bo_idx[j] == i)
+ bo_size += surf->info.planes[j].size;
+ }
+
+ surf->bos[i] = tbm_bufmgr_internal_alloc_bo_with_format(bufmgr, format, i, width, height, surf->info.bpp/8, flags, error);
+ if (!surf->bos[i]) {
+ surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
+ if (!surf->bos[i]) {
+ TBM_ERR("fail to alloc bo idx:%d\n", i);
+ *error = tbm_get_last_error();
+ goto alloc_bo_fail;
+ }
+ }
+
+ _tbm_bo_set_surface(surf->bos[i], surf);
+ }
+
+ *error = TBM_ERROR_NONE;
+
+ return surf;
+
+alloc_bo_fail:
+ for (j = 0; j < i; j++) {
+ if (surf->bos[j])
+ tbm_bo_unref(surf->bos[j]);
+ }
+query_plane_data_fail:
+bpp_fail:
+num_planes_fail:
+ free(surf);
+alloc_surf_fail:
+
+ return NULL;
+}
+
+static struct _tbm_surface *
+_tbm_surface_internal_hal_tbm_create_surface(tbm_bufmgr bufmgr, int width, int height, int format, int flags, tbm_error_e *error)
+{
+ struct _tbm_surface *surf = NULL;
+ uint32_t size = 0, offset = 0, stride = 0, bo_size = 0;
+ int i, j, bo_idx;
+ hal_tbm_surface *hal_surface = NULL;
+ hal_tbm_bo **hal_bos = NULL;
+ int num_bos = 0;
+
+ surf = calloc(1, sizeof(struct _tbm_surface));
+ if (!surf) {
+ /* LCOV_EXCL_START */
+ TBM_ERR("fail to alloc surf");
+ *error = TBM_ERROR_OUT_OF_MEMORY;
+ goto alloc_surf_fail;
+ /* LCOV_EXCL_STOP */
+ }
+ surf->refcnt = 1;
+
+ surf->magic = TBM_SURFACE_MAGIC;
+ surf->bufmgr = bufmgr;
+ surf->info.width = width;
+ surf->info.height = height;
+ surf->info.format = format;
+ surf->info.bpp = _tbm_surface_internal_get_bpp(format);
+ if (!surf->info.bpp) {
+ TBM_ERR("fail to get bpp from format(%d), error(%s)", format, tbm_error_str(*error));
+ *error = tbm_get_last_error();
+ goto bpp_fail;
+ }
+ surf->flags = flags;
+
+ // get number of planes
+ surf->info.num_planes = _tbm_surface_internal_get_num_planes(format);
+ if (!surf->info.num_planes) {
+ TBM_ERR("fail to get num_planes from format(%d), error(%s)", format, tbm_error_str(*error));
+ *error = tbm_get_last_error();
+ goto num_planes_fail;
+ }
+
+ hal_surface = hal_tbm_bufmgr_alloc_surface(bufmgr->hal_bufmgr, (uint32_t)width, (uint32_t)height, (hal_tbm_format)format, (hal_tbm_bo_memory_type)flags, NULL, 0, (hal_tbm_error *)error);
+ if (hal_surface) {
+ // set hal_surface
+ surf->hal_surface = hal_surface;
+
+ // set infomation of planes
+ for (i = 0; i < surf->info.num_planes; i++) {
+ *error = (tbm_error_e)hal_tbm_surface_get_plane_data(hal_surface, i, &size, &offset, &stride, &bo_idx);
+ if (*error != TBM_ERROR_NONE) {
+ goto query_plane_data_fail;
+ }
+ surf->info.planes[i].size = size;
+ surf->info.planes[i].offset = offset;
+ surf->info.planes[i].stride = stride;
+ surf->planes_bo_idx[i] = bo_idx;
+ }
+
+ // set infomation of bos
+ hal_bos = hal_tbm_surface_get_bos(hal_surface, &num_bos, (hal_tbm_error *)error);
+ if (!hal_bos) {
+ TBM_ERR("fail to get bos, error(%s)", tbm_error_str(*error));
+ goto get_bos_fail;
+ }
+ surf->num_bos = num_bos;
+
+ for (i = 0; i < surf->info.num_planes; i++) {
+ surf->info.size += surf->info.planes[i].size;
+
+ if (surf->num_bos < surf->planes_bo_idx[i] + 1)
+ surf->num_bos = surf->planes_bo_idx[i] + 1;
+ }
+
+ for (i = 0; i < num_bos; i++) {
+ surf->bos[i] = tbm_bufmgr_internal_alloc_bo_with_bo_data(bufmgr, (tbm_backend_bo_data *)hal_bos[i], flags);
+ if (!surf->bos[i]) {
+ TBM_ERR("fail to alloc bo idx:%d", i);
+ *error = tbm_get_last_error();
+ goto get_bo_fail;
+ }
+
+ _tbm_bo_set_surface(surf->bos[i], surf);
+ }
+ } else {
+ // set infomation of planes
+ for (i = 0; i < surf->info.num_planes; i++) {
+ if (!_tbm_surface_internal_query_plane_data(surf, i, &size, &offset, &stride, &bo_idx)) {
+ TBM_ERR("fail to query plane data");
+ *error = tbm_get_last_error();
+ goto query_plane_data_fail;
+ }
+ surf->info.planes[i].size = size;
+ surf->info.planes[i].offset = offset;
+ surf->info.planes[i].stride = stride;
+ surf->planes_bo_idx[i] = bo_idx;
+ }
+
+ // count number of bos
+ surf->num_bos = 1;
+ for (i = 0; i < surf->info.num_planes; i++) {
+ surf->info.size += surf->info.planes[i].size;
+
+ if (surf->num_bos < surf->planes_bo_idx[i] + 1)
+ surf->num_bos = surf->planes_bo_idx[i] + 1;
+ }
+
+ // set infomation of bos
+ for (i = 0; i < surf->num_bos; i++) {
+ bo_size = 0;
+ for (j = 0; j < surf->info.num_planes; j++) {
+ if (surf->planes_bo_idx[j] == i)
+ bo_size += surf->info.planes[j].size;
+ }
+
+ surf->bos[i] = tbm_bufmgr_internal_alloc_bo_with_format(bufmgr, format, i, width, height, surf->info.bpp / 8, flags, error);
+ if (*error == TBM_ERROR_NOT_SUPPORTED) {
+ surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
+ if (!surf->bos[i]) {
+ TBM_ERR("fail to alloc bo idx:%d", i);
+ *error = tbm_get_last_error();
+ goto get_bo_fail;
+ }
+ }
+
+ _tbm_bo_set_surface(surf->bos[i], surf);
+ }
+ }
+
+ *error = TBM_ERROR_NONE;
+
+ return surf;
+
+get_bo_fail:
+get_bos_fail:
+query_plane_data_fail:
+ if (hal_surface) {
+ hal_tbm_surface_free(hal_surface);
+ } else {
+ for (j = 0; j < i; j++) {
+ if (surf->bos[j])
+ tbm_bo_unref(surf->bos[j]);
+ }
+ }
+num_planes_fail:
+bpp_fail:
+ if (surf)
+ free(surf);
+alloc_surf_fail:
+
+ return NULL;
+}
+
+static struct _tbm_surface *
+_tbm_surface_internal_hal_tbm_import_surface(tbm_bufmgr bufmgr, int width, int height, int format, tbm_surface_buffer_data *buffer_data, tbm_error_e *error)
+{
+ struct _tbm_surface *surf = NULL;
+ uint32_t size = 0, offset = 0, stride = 0;
+ int i, j, bo_idx;
+ hal_tbm_surface *hal_surface = NULL;
+ hal_tbm_bo **hal_bos = NULL;
+ int num_bos = 0;
+ int flags;
+
+ surf = calloc(1, sizeof(struct _tbm_surface));
+ if (!surf) {
+ /* LCOV_EXCL_START */
+ TBM_ERR("fail to alloc surf");
+ *error = TBM_ERROR_OUT_OF_MEMORY;
+ goto alloc_surf_fail;
+ /* LCOV_EXCL_STOP */
+ }
+ surf->refcnt = 1;
+
+ surf->magic = TBM_SURFACE_MAGIC;
+ surf->bufmgr = bufmgr;
+ surf->info.width = width;
+ surf->info.height = height;
+ surf->info.format = format;
+ surf->info.bpp = _tbm_surface_internal_get_bpp(format);
+ if (!surf->info.bpp) {
+ TBM_ERR("fail to get bpp from format(%d), error(%s)", format, tbm_error_str(*error));
+ *error = tbm_get_last_error();
+ goto bpp_fail;
+ }
+
+ // get number of planes
+ surf->info.num_planes = _tbm_surface_internal_get_num_planes(format);
+ if (!surf->info.num_planes) {
+ TBM_ERR("fail to get num_planes from format(%d), error(%s)", format, tbm_error_str(*error));
+ *error = tbm_get_last_error();
+ goto num_planes_fail;
+ }
+
+ // import surface
+ hal_surface = hal_tbm_bufmgr_import_surface(bufmgr->hal_bufmgr,
+ (uint32_t)width,
+ (uint32_t)height,
+ (hal_tbm_format)format,
+ (hal_tbm_surface_buffer_data *)buffer_data,
+ (hal_tbm_error *)error);
+ if (!hal_surface) {
+ TBM_ERR("hal_tbm_bufmgr_import_surface failed.(width:%d height:%d format:%d error:%s)",
+ width, height, format, tbm_error_str(*error));
+ goto import_surface_fail;
+ }
+ surf->hal_surface = hal_surface;
+
+ // set infomation of planes
+ for (i = 0; i < surf->info.num_planes; i++) {
+ *error = (tbm_error_e)hal_tbm_surface_get_plane_data(hal_surface, i, &size, &offset, &stride, &bo_idx);
+ if (*error != TBM_ERROR_NONE) {
+ goto query_plane_data_fail;
+ }
+ surf->info.planes[i].size = size;
+ surf->info.planes[i].offset = offset;
+ surf->info.planes[i].stride = stride;
+ surf->planes_bo_idx[i] = bo_idx;
+ }
+
+ // set infomation of bos
+ hal_bos = hal_tbm_surface_get_bos(hal_surface, &num_bos, (hal_tbm_error *)error);
+ if (!hal_bos) {
+ TBM_ERR("fail to get bos, error(%s)", tbm_error_str(*error));
+ goto get_bos_fail;
+ }
+ surf->num_bos = num_bos;
+
+ for (i = 0; i < surf->info.num_planes; i++) {
+ surf->info.size += surf->info.planes[i].size;
+
+ if (surf->num_bos < surf->planes_bo_idx[i] + 1)
+ surf->num_bos = surf->planes_bo_idx[i] + 1;
+ }
+
+ // get memory_types(bo flags)
+ flags = (int)hal_tbm_bo_get_memory_types(hal_bos[0], (hal_tbm_error *)error);
+ if (*error != TBM_ERROR_NONE) {
+ TBM_ERR("hal_tbm_bo_get_memory_types failed.");
+ goto get_memory_types_fail;
+ }
+ surf->flags = flags;
+
+ for (i = 0; i < num_bos; i++) {
+ surf->bos[i] = tbm_bufmgr_internal_alloc_bo_with_bo_data(bufmgr, (tbm_backend_bo_data *)hal_bos[i], flags);
+ if (!surf->bos[i]) {
+ TBM_ERR("fail to alloc bo idx:%d", i);
+ *error = tbm_get_last_error();
+ goto get_bo_fail;
+ }
+
+ _tbm_bo_set_surface(surf->bos[i], surf);
+ }
+
+ return surf;
+
+get_bo_fail:
+ for (j = 0; j < i; j++) {
+ if (surf->bos[j])
+ tbm_bo_unref(surf->bos[j]);
+ }
+get_memory_types_fail:
+get_bos_fail:
+query_plane_data_fail:
+ hal_tbm_surface_free(hal_surface);
+import_surface_fail:
+num_planes_fail:
+bpp_fail:
+ if (surf)
+ free(surf);
+alloc_surf_fail:
+
+ return NULL;
+}
+