+ _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;
+}
+
+int
+tbm_surface_internal_set_debug_data(tbm_surface_h surface, char *key, char *value)
+{
+ tbm_surface_debug_data *debug_data = NULL;
+ tbm_surface_debug_data *old_data = NULL, *tmp = NULL;
+ tbm_bufmgr bufmgr = NULL;
+
+ _tbm_surface_mutex_lock();
+ _tbm_set_last_result(TBM_ERROR_NONE);
+
+ TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
+ TBM_SURFACE_RETURN_VAL_IF_FAIL(key, 0);
+
+ bufmgr = surface->bufmgr;
+
+ TBM_SURFACE_RETURN_VAL_IF_FAIL(bufmgr, 0);
+
+ if (!LIST_IS_EMPTY(&surface->debug_data_list)) {
+ LIST_FOR_EACH_ENTRY(old_data, &surface->debug_data_list, item_link) {
+ if (old_data) {
+ if (!strcmp(old_data->key, key)) {
+ if (old_data->value && value && !strncmp(old_data->value, value, strlen(old_data->value))) {
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) Already exist key(%s) and value(%s)!\n", surface, key, value);
+ goto add_debug_key_list;
+ }
+
+ if (old_data->value)
+ free(old_data->value);
+
+ if (value)
+ old_data->value = strdup(value);
+ else
+ old_data->value = NULL;
+
+ goto add_debug_key_list;
+ }
+ }
+ }
+ }
+
+ debug_data = _tbm_surface_internal_debug_data_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;
+ }
+
+ TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) key(%s) value(%s)\n", surface, key, value);
+
+ LIST_ADD(&debug_data->item_link, &surface->debug_data_list);
+
+add_debug_key_list:
+ if (!LIST_IS_EMPTY(&bufmgr->debug_key_list)) {
+ LIST_FOR_EACH_ENTRY_SAFE(old_data, tmp, &bufmgr->debug_key_list, item_link) {
+ if (!strcmp(old_data->key, key)) {
+ _tbm_surface_mutex_unlock();
+ return 1;
+ }
+ }
+ }
+
+ debug_data = _tbm_surface_internal_debug_data_create(key, NULL);
+ LIST_ADD(&debug_data->item_link, &bufmgr->debug_key_list);
+
+ _tbm_surface_mutex_unlock();
+
+ return 1;
+}
+
+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 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;
+ }
+
+ 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 * width + 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;
+}
+
+void
+tbm_surface_internal_dump_end(void)
+{
+ tbm_surface_dump_buf_info *buf_info = NULL, *tmp = NULL;
+ tbm_bo_handle bo_handle;
+
+ if (!g_dump_info)
+ return;
+
+ if (LIST_IS_EMPTY(&g_dump_info->surface_list)) {
+ free(g_dump_info);
+ g_dump_info = NULL;
+ return;
+ }
+
+ /* make files */
+ LIST_FOR_EACH_ENTRY_SAFE(buf_info, tmp, &g_dump_info->surface_list, link) {
+ char file[2048];
+
+ 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, TBM_FORMAT_ARGB8888);
+ break;
+ case TBM_FORMAT_XRGB8888:
+ _tbm_surface_internal_dump_file_png(file, bo_handle.ptr,
+ buf_info->info.planes[0].stride >> 2,
+ buf_info->info.height, TBM_FORMAT_XRGB8888);
+ 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, 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");
+}
+
+static pixman_format_code_t
+_tbm_surface_internal_pixman_format_get(tbm_format format)
+{
+ switch (format) {
+ case TBM_FORMAT_ARGB8888:
+ return PIXMAN_a8r8g8b8;
+ case TBM_FORMAT_XRGB8888:
+ return PIXMAN_x8r8g8b8;
+ default:
+ return 0;
+ }
+
+ return 0;
+}
+
+/**
+ * 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;
+
+ 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);
+
+ pixman_format = _tbm_surface_internal_pixman_format_get(format);
+ TBM_RETURN_VAL_IF_FAIL(pixman_format > 0, TBM_SURFACE_ERROR_INVALID_OPERATION);
+
+ /* 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;
+}
+
+#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;
+
+ 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);
+ }
+
+ _tbm_surface_mutex_unlock();
+
+ return keys;
+}
+
+static void _tbm_surface_internal_put_keys(char *keys)
+{
+ if (keys)
+ free(keys);
+}
+
+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;
+
+ next_link = g_dump_info->link->next;
+ TBM_RETURN_IF_FAIL(next_link != NULL);
+
+ if (next_link == &g_dump_info->surface_list) {
+ next_link = next_link->next;
+ TBM_RETURN_IF_FAIL(next_link != NULL);
+ }
+
+ buf_info = LIST_ENTRY(tbm_surface_dump_buf_info, next_link, link);
+ TBM_RETURN_IF_FAIL(buf_info != NULL);
+
+ ret = tbm_surface_map(surface, TBM_SURF_OPTION_READ|TBM_SURF_OPTION_WRITE, &info);
+ TBM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
+
+ if (scale_factor > 0.0) {
+ const int bpp = 4;
+
+ 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;
+ }
+
+ memset(&buf_info->info, 0, sizeof(tbm_surface_info_s));
+
+ 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;
+ }
+
+ /* make the file information */
+ memcpy(&buf_info->info, &info, sizeof(tbm_surface_info_s));
+ }
+
+ 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];
+
+ keys = _tbm_surface_internal_get_keys(surface);
+ if (!keys) {
+ TBM_ERR("fail to get keys");
+ tbm_surface_unmap(surface);
+ return;
+ }
+
+ /* 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);
+}
+
+void tbm_surface_internal_dump_shm_buffer(void *ptr, int w, int h, int stride,
+ const char *type)
+{
+ 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;
+
+ next_link = g_dump_info->link->next;
+ TBM_RETURN_IF_FAIL(next_link != NULL);
+
+ if (next_link == &g_dump_info->surface_list) {
+ next_link = next_link->next;
+ TBM_RETURN_IF_FAIL(next_link != NULL);
+ }
+
+ buf_info = LIST_ENTRY(tbm_surface_dump_buf_info, next_link, link);
+ TBM_RETURN_IF_FAIL(buf_info != NULL);
+
+ if (scale_factor > 0.0) {
+ const int bpp = 4;
+
+ dw = w * scale_factor;
+ dh = h * scale_factor;
+ dstride = dw * bpp;
+ size = dstride * dh;
+ } else
+ size = stride * h;
+
+ 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 {
+ memcpy(bo_handle.ptr, ptr, size);
+ buf_info->shm_stride = stride;
+ buf_info->shm_h = h;
+ }
+
+ 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;
+ }
+
+ 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.planes[0].stride >> 2,
+ info.height, TBM_FORMAT_ARGB8888);
+ break;
+ case TBM_FORMAT_XRGB8888:
+ _tbm_surface_internal_dump_file_png(file, info.planes[0].ptr,
+ info.planes[0].stride >> 2,
+ info.height, TBM_FORMAT_XRGB8888);
+ 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);
+