remove unused valiable
[platform/core/uifw/libtbm.git] / src / tbm_surface_internal.c
index e5f3243..df77fe7 100644 (file)
@@ -39,6 +39,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "tbm_surface_internal.h"
 #include "list.h"
 #include <png.h>
+#include <pixman.h>
 
 static tbm_bufmgr g_surface_bufmgr;
 static pthread_mutex_t tbm_surface_lock;
@@ -68,7 +69,6 @@ void _tbm_surface_mutex_unlock(void);
 }
 
 /* LCOV_EXCL_START */
-
 static double
 _tbm_surface_internal_get_time(void)
 {
@@ -217,7 +217,6 @@ _tbm_surface_internal_format_to_str(tbm_format format)
                return "unknwon";
        }
 }
-/* LCOV_EXCL_STOP */
 
 static bool
 _tbm_surface_mutex_init(void)
@@ -228,7 +227,7 @@ _tbm_surface_mutex_init(void)
                return true;
 
        if (pthread_mutex_init(&tbm_surface_lock, NULL)) {
-               TBM_LOG_E("fail: tbm_surface mutex init\n");
+               TBM_LOG_E("fail: pthread_mutex_init for tbm_surface_lock.\n");
                return false;
        }
 
@@ -240,8 +239,10 @@ _tbm_surface_mutex_init(void)
 void
 _tbm_surface_mutex_lock(void)
 {
-       if (!_tbm_surface_mutex_init())
+       if (!_tbm_surface_mutex_init()) {
+               TBM_LOG_E("fail: _tbm_surface_mutex_init.\n");
                return;
+       }
 
        pthread_mutex_lock(&tbm_surface_lock);
 }
@@ -267,6 +268,29 @@ _deinit_surface_bufmgr(void)
        tbm_bufmgr_deinit(g_surface_bufmgr);
        g_surface_bufmgr = NULL;
 }
+/* LCOV_EXCL_STOP */
+
+static int
+_tbm_surface_internal_is_valid(tbm_surface_h surface)
+{
+       tbm_surface_h old_data = NULL;
+
+       TBM_RETURN_VAL_IF_FAIL(g_surface_bufmgr, 0);
+       TBM_RETURN_VAL_IF_FAIL(surface, 0);
+
+       if (!LIST_IS_EMPTY(&g_surface_bufmgr->surf_list)) {
+               LIST_FOR_EACH_ENTRY(old_data, &g_surface_bufmgr->surf_list, item_link) {
+                       if (old_data == surface) {
+                               TBM_TRACE("tbm_surface(%p)\n", surface);
+                               return 1;
+                       }
+               }
+       }
+
+       TBM_LOG_E("error: No valid tbm_surface(%p)\n", surface);
+
+       return 0;
+}
 
 static int
 _tbm_surface_internal_query_plane_data(tbm_surface_h surface,
@@ -289,8 +313,12 @@ _tbm_surface_internal_query_plane_data(tbm_surface_h surface,
 
        ret = mgr->backend->surface_get_plane_data(surf->info.width,
                        surf->info.height, surf->info.format, plane_idx, size, offset, pitch, bo_idx);
-       if (!ret)
+       if (!ret) {
+               /* LCOV_EXCL_START */
+               TBM_LOG_E("Fail to surface_get_plane_data. surface(%p)\n", surface);
                return 0;
+               /* LCOV_EXCL_STOP */
+       }
 
        return 1;
 }
@@ -344,23 +372,21 @@ _tbm_surface_internal_destroy(tbm_surface_h surface)
 int
 tbm_surface_internal_is_valid(tbm_surface_h surface)
 {
-       tbm_surface_h old_data = NULL;
+       int ret = 0;
 
-       if (surface == NULL || g_surface_bufmgr == NULL) {
-               TBM_TRACE("error: tbm_surface(%p)\n", surface);
+       _tbm_surface_mutex_lock();
+
+       /* Return silently if surface is null. */
+       if (!surface) {
+               _tbm_surface_mutex_unlock();
                return 0;
        }
 
-       if (!LIST_IS_EMPTY(&g_surface_bufmgr->surf_list)) {
-               LIST_FOR_EACH_ENTRY(old_data, &g_surface_bufmgr->surf_list, item_link) {
-                       if (old_data == surface) {
-                               TBM_TRACE("tbm_surface(%p)\n", surface);
-                               return 1;
-                       }
-               }
-       }
-       TBM_TRACE("error: tbm_surface(%p)\n", surface);
-       return 0;
+       ret = _tbm_surface_internal_is_valid(surface);
+
+       _tbm_surface_mutex_unlock();
+
+       return ret;
 }
 
 int
@@ -385,8 +411,12 @@ tbm_surface_internal_query_supported_formats(uint32_t **formats,
                goto fail;
 
        ret = mgr->backend->surface_supported_format(formats, num);
-       if (!ret)
+       if (!ret)  {
+               /* LCOV_EXCL_START */
+               TBM_LOG_E("Fail to surface_supported_format.\n");
                goto fail;
+               /* LCOV_EXCL_START */
+       }
 
        TBM_TRACE("tbm_bufmgr(%p) format num(%u)\n", g_surface_bufmgr, *num);
 
@@ -394,14 +424,18 @@ tbm_surface_internal_query_supported_formats(uint32_t **formats,
 
        return ret;
 
+/* LCOV_EXCL_START */
 fail:
        if (bufmgr_initialized) {
                LIST_DELINIT(&g_surface_bufmgr->surf_list);
                _deinit_surface_bufmgr();
        }
        _tbm_surface_mutex_unlock();
-       TBM_TRACE("error: tbm_bufmgr(%p)\n", g_surface_bufmgr);
+
+       TBM_LOG_E("error: tbm_bufmgr(%p)\n", g_surface_bufmgr);
+
        return 0;
+/* LCOV_EXCL_STOP */
 }
 
 int
@@ -613,8 +647,10 @@ tbm_surface_internal_create_with_flags(int width, int height,
 
        surf = calloc(1, sizeof(struct _tbm_surface));
        if (!surf) {
+               /* LCOV_EXCL_START */
                TBM_LOG_E("fail to alloc surf\n");
                goto alloc_surf_fail;
+               /* LCOV_EXCL_STOP */
        }
 
        surf->bufmgr = mgr;
@@ -715,6 +751,7 @@ tbm_surface_internal_create_with_flags(int width, int height,
 
        return surf;
 
+/* LCOV_EXCL_START */
 alloc_bo_fail:
        for (j = 0; j < i; j++) {
                if (surf->bos[j])
@@ -724,14 +761,17 @@ query_plane_data_fail:
        free(surf);
 alloc_surf_fail:
 check_valid_fail:
-       if (bufmgr_initialized) {
+       if (bufmgr_initialized && mgr) {
                LIST_DELINIT(&mgr->surf_list);
                _deinit_surface_bufmgr();
        }
        _tbm_surface_mutex_unlock();
-       TBM_TRACE("error: width(%d) height(%d) format(%s) flags(%d)\n",
+
+       TBM_LOG_E("error: width(%d) height(%d) format(%s) flags(%d)\n",
                        width, height,
                        _tbm_surface_internal_format_to_str(format), flags);
+/* LCOV_EXCL_STOP */
+
        return NULL;
 }
 
@@ -757,18 +797,27 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info,
        }
 
        mgr = g_surface_bufmgr;
-       if (!TBM_BUFMGR_IS_VALID(mgr))
+       if (!TBM_BUFMGR_IS_VALID(mgr)) {
+               TBM_LOG_E("fail to validate the Bufmgr.\n");
                goto check_valid_fail;
+       }
 
        surf = calloc(1, sizeof(struct _tbm_surface));
-       if (!surf)
+       if (!surf) {
+               /* LCOV_EXCL_START */
+               TBM_LOG_E("fail to allocate struct _tbm_surface.\n");
                goto alloc_surf_fail;
+               /* LCOV_EXCL_STOP */
+       }
 
        surf->bufmgr = mgr;
        surf->info.width = info->width;
        surf->info.height = info->height;
        surf->info.format = info->format;
-       surf->info.bpp = info->bpp;
+       if (info->bpp > 0)
+               surf->info.bpp = info->bpp;
+       else
+               surf->info.bpp = tbm_surface_internal_get_bpp(info->format);
        surf->info.num_planes = info->num_planes;
        surf->refcnt = 1;
 
@@ -779,8 +828,13 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info,
 
                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;
+               else {
+                       uint32_t size = 0, offset = 0, stride = 0;
+                       int32_t bo_idx = 0;
+
+                       _tbm_surface_internal_query_plane_data(surf, i, &size, &offset, &stride, &bo_idx);
+                       surf->info.planes[i].size = size;
+               }
 
                if (num == 1)
                        surf->planes_bo_idx[i] = 0;
@@ -801,8 +855,10 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info,
        /* create only one bo */
        surf->num_bos = num;
        for (i = 0; i < num; i++) {
-               if (bos[i] == NULL)
+               if (bos[i] == NULL) {
+                       TBM_LOG_E("bos[%d] is null.\n", i);
                        goto check_bo_fail;
+               }
 
                surf->bos[i] = tbm_bo_ref(bos[i]);
                _tbm_bo_set_surface(bos[i], surf);
@@ -820,6 +876,7 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info,
 
        return surf;
 
+/* LCOV_EXCL_START */
 check_bo_fail:
        for (i = 0; i < num; i++) {
                if (surf->bos[i])
@@ -828,14 +885,17 @@ check_bo_fail:
        free(surf);
 alloc_surf_fail:
 check_valid_fail:
-       if (bufmgr_initialized) {
-               LIST_DELINIT(&g_surface_bufmgr->surf_list);
+       if (bufmgr_initialized && mgr) {
+               LIST_DELINIT(&mgr->surf_list);
                _deinit_surface_bufmgr();
        }
        _tbm_surface_mutex_unlock();
-       TBM_TRACE("error: width(%u) height(%u) format(%s) bo_num(%d)\n",
+
+       TBM_LOG_E("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;
 }
 
@@ -844,7 +904,7 @@ tbm_surface_internal_destroy(tbm_surface_h surface)
 {
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_IF_FAIL(tbm_surface_internal_is_valid(surface));
+       TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface));
 
        surface->refcnt--;
 
@@ -867,7 +927,7 @@ tbm_surface_internal_ref(tbm_surface_h surface)
 {
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_IF_FAIL(tbm_surface_internal_is_valid(surface));
+       TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface));
 
        surface->refcnt++;
 
@@ -881,7 +941,7 @@ tbm_surface_internal_unref(tbm_surface_h surface)
 {
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_IF_FAIL(tbm_surface_internal_is_valid(surface));
+       TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface));
 
        surface->refcnt--;
 
@@ -907,7 +967,7 @@ tbm_surface_internal_get_num_bos(tbm_surface_h surface)
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), 0);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        surf = (struct _tbm_surface *)surface;
        num = surf->num_bos;
@@ -927,7 +987,7 @@ tbm_surface_internal_get_bo(tbm_surface_h surface, int bo_idx)
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), NULL);
+       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;
@@ -940,7 +1000,7 @@ tbm_surface_internal_get_bo(tbm_surface_h surface, int bo_idx)
        return bo;
 }
 
-int
+unsigned int
 tbm_surface_internal_get_size(tbm_surface_h surface)
 {
        struct _tbm_surface *surf;
@@ -948,12 +1008,12 @@ tbm_surface_internal_get_size(tbm_surface_h surface)
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), 0);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        surf = (struct _tbm_surface *)surface;
        size = surf->info.size;
 
-       TBM_TRACE("tbm_surface(%p) size(%d)\n", surface, size);
+       TBM_TRACE("tbm_surface(%p) size(%u)\n", surface, size);
 
        _tbm_surface_mutex_unlock();
 
@@ -968,7 +1028,7 @@ tbm_surface_internal_get_plane_data(tbm_surface_h surface, int plane_idx,
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), 0);
+       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;
@@ -988,7 +1048,7 @@ tbm_surface_internal_get_plane_data(tbm_surface_h surface, int plane_idx,
        if (pitch)
                *pitch = surf->info.planes[plane_idx].stride;
 
-       TBM_TRACE("tbm_surface(%p) plane_idx(%d) size(%d) offset(%d) pitch(%d)\n", surface, plane_idx,
+       TBM_TRACE("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);
 
@@ -1007,7 +1067,7 @@ tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), 0);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        memset(bo_handles, 0, sizeof(tbm_bo_handle) * 4);
 
@@ -1030,14 +1090,20 @@ tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
                                for (j = 0; j < i; j++)
                                        tbm_bo_unmap(surf->bos[j]);
 
-                               TBM_TRACE("error: tbm_surface(%p) opt(%d) map(%d)\n", surface, opt, map);
+                               TBM_LOG_E("error: tbm_surface(%p) opt(%d) map(%d)\n", surface, opt, map);
                                _tbm_surface_mutex_unlock();
                                return 0;
                        }
                }
        } else {
-               for (i = 0; i < surf->num_bos; i++)
+               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_LOG_E("error: tbm_surface(%p) opt(%d) map(%d)\n", surface, opt, map);
+                               _tbm_surface_mutex_unlock();
+                               return 0;
+                       }
+               }
        }
 
        for (i = 0; i < surf->info.num_planes; i++) {
@@ -1065,7 +1131,7 @@ tbm_surface_internal_unmap(tbm_surface_h surface)
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_IF_FAIL(tbm_surface_internal_is_valid(surface));
+       TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface));
 
        surf = (struct _tbm_surface *)surface;
 
@@ -1085,12 +1151,12 @@ tbm_surface_internal_get_width(tbm_surface_h surface)
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), 0);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        surf = (struct _tbm_surface *)surface;
        width = surf->info.width;
 
-       TBM_TRACE("tbm_surface(%p) width(%d)\n", surface, width);
+       TBM_TRACE("tbm_surface(%p) width(%u)\n", surface, width);
 
        _tbm_surface_mutex_unlock();
 
@@ -1105,12 +1171,12 @@ tbm_surface_internal_get_height(tbm_surface_h surface)
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), 0);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        surf = (struct _tbm_surface *)surface;
        height = surf->info.height;
 
-       TBM_TRACE("tbm_surface(%p) height(%d)\n", surface, height);
+       TBM_TRACE("tbm_surface(%p) height(%u)\n", surface, height);
 
        _tbm_surface_mutex_unlock();
 
@@ -1126,7 +1192,7 @@ tbm_surface_internal_get_format(tbm_surface_h surface)
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), 0);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        surf = (struct _tbm_surface *)surface;
        format = surf->info.format;
@@ -1146,7 +1212,7 @@ tbm_surface_internal_get_plane_bo_idx(tbm_surface_h surface, int plane_idx)
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), 0);
+       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;
@@ -1167,7 +1233,7 @@ tbm_surface_internal_add_user_data(tbm_surface_h surface, unsigned long key,
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), 0);
+       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);
@@ -1201,13 +1267,7 @@ tbm_surface_internal_set_user_data(tbm_surface_h surface, unsigned long key,
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), 0);
-
-       if (LIST_IS_EMPTY(&surface->user_data_list)) {
-               TBM_TRACE("error: tbm_surface(%p) key(%lu)\n", surface, key);
-               _tbm_surface_mutex_unlock();
-               return 0;
-       }
+       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) {
@@ -1236,18 +1296,18 @@ tbm_surface_internal_get_user_data(tbm_surface_h surface, unsigned long key,
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), 0);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
-       if (!data || LIST_IS_EMPTY(&surface->user_data_list)) {
-               TBM_TRACE("error: tbm_surface(%p) key(%lu)\n", surface, key);
+       if (!data) {
+               TBM_LOG_E("error: tbm_surface(%p) key(%lu)\n", surface, key);
                _tbm_surface_mutex_unlock();
                return 0;
        }
+       *data = NULL;
 
        old_data = user_data_lookup(&surface->user_data_list, key);
        if (!old_data) {
                TBM_TRACE("error: tbm_surface(%p) key(%lu)\n", surface, key);
-               *data = NULL;
                _tbm_surface_mutex_unlock();
                return 0;
        }
@@ -1269,13 +1329,7 @@ tbm_surface_internal_delete_user_data(tbm_surface_h surface,
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), 0);
-
-       if (LIST_IS_EMPTY(&surface->user_data_list)) {
-               TBM_TRACE("error: tbm_surface(%p) key(%lu)\n", surface, key);
-               _tbm_surface_mutex_unlock();
-               return 0;
-       }
+       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) {
@@ -1307,7 +1361,7 @@ tbm_surface_internal_set_debug_pid(tbm_surface_h surface, unsigned int pid)
 {
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_IF_FAIL(tbm_surface_internal_is_valid(surface));
+       TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface));
 
        surface->debug_pid = pid;
 
@@ -1338,7 +1392,7 @@ tbm_surface_internal_set_debug_data(tbm_surface_h surface, char *key, char *valu
 
        _tbm_surface_mutex_lock();
 
-       TBM_SURFACE_RETURN_VAL_IF_FAIL(tbm_surface_internal_is_valid(surface), 0);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
        TBM_SURFACE_RETURN_VAL_IF_FAIL(key, 0);
 
        bufmgr = surface->bufmgr;
@@ -1346,19 +1400,31 @@ tbm_surface_internal_set_debug_data(tbm_surface_h surface, char *key, char *valu
        TBM_SURFACE_RETURN_VAL_IF_FAIL(bufmgr, 0);
 
        if (!LIST_IS_EMPTY(&surface->debug_data_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(old_data, tmp, &surface->debug_data_list, item_link) {
-                       if (!strcmp(old_data->key, key)) {
-                               if (value)
-                                       old_data->value = strdup(value);
-                               else
-                                       old_data->value = NULL;
+               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("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_TRACE("error: tbm_surface(%p) key(%s) value(%s)\n", surface, key, value);
+               TBM_LOG_E("error: tbm_surface(%p) key(%s) value(%s)\n", surface, key, value);
                _tbm_surface_mutex_unlock();
                return 0;
        }
@@ -1367,6 +1433,7 @@ tbm_surface_internal_set_debug_data(tbm_surface_h surface, char *key, char *valu
 
        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)) {
@@ -1387,23 +1454,17 @@ tbm_surface_internal_set_debug_data(tbm_surface_h surface, char *key, char *valu
 char *
 _tbm_surface_internal_get_debug_data(tbm_surface_h surface, char *key)
 {
-       tbm_surface_debug_data *old_data = NULL, *tmp = NULL;
-
-       _tbm_surface_mutex_lock();
+       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_SAFE(old_data, tmp, &surface->debug_data_list, item_link) {
-                       if (!strcmp(old_data->key, key)) {
-                               _tbm_surface_mutex_unlock();
+               LIST_FOR_EACH_ENTRY(old_data, &surface->debug_data_list, item_link) {
+                       if (!strcmp(old_data->key, key))
                                return old_data->value;
-                       }
                }
        }
 
-       _tbm_surface_mutex_unlock();
-
        return NULL;
 }
 
@@ -1435,14 +1496,15 @@ struct _tbm_surface_dump_info {
 
 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)
+_tbm_surface_internal_dump_file_raw(const char *file, void *data1, int size1,
+                               void *data2, int size2, void *data3, int size3)
 {
-       unsigned int *blocks;
        FILE *fp = fopen(file, "w+");
        TBM_RETURN_IF_FAIL(fp != NULL);
+       unsigned int *blocks;
 
        blocks = (unsigned int *)data1;
        fwrite(blocks, 1, size1, fp);
@@ -1461,22 +1523,26 @@ _tbm_surface_internal_dump_file_raw(const char *file, void *data1, int size1, vo
 }
 
 static void
-_tbm_surface_internal_dump_file_png(const char *file, const void *data, int width,
-               int height)
+_tbm_surface_internal_dump_file_png(const char *file, const void *data, int width, int height)
 {
+       unsigned int *blocks = (unsigned int *)data;
        FILE *fp = fopen(file, "wb");
        TBM_RETURN_IF_FAIL(fp != NULL);
-       int depth = 8;
+       const int pixel_size = 4;       // RGBA
+       png_bytep *row_pointers;
+       int depth = 8, y;
 
-       png_structp pPngStruct =
-               png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+       png_structp pPngStruct = png_create_write_struct(PNG_LIBPNG_VER_STRING,
+                                                       NULL, NULL, NULL);
        if (!pPngStruct) {
+               TBM_LOG_E("fail to create a png write structure.\n");
                fclose(fp);
                return;
        }
 
        png_infop pPngInfo = png_create_info_struct(pPngStruct);
        if (!pPngInfo) {
+               TBM_LOG_E("fail to create a png info structure.\n");
                png_destroy_write_struct(&pPngStruct, NULL);
                fclose(fp);
                return;
@@ -1495,20 +1561,33 @@ _tbm_surface_internal_dump_file_png(const char *file, const void *data, int widt
        png_set_bgr(pPngStruct);
        png_write_info(pPngStruct, pPngInfo);
 
-       const int pixel_size = 4;       // RGBA
-       png_bytep *row_pointers =
-                       png_malloc(pPngStruct, height * sizeof(png_byte *));
-
-       unsigned int *blocks = (unsigned int *)data;
-       int y = 0;
-       int x = 0;
+       row_pointers = png_malloc(pPngStruct, height * sizeof(png_byte *));
+       if (!row_pointers) {
+               TBM_LOG_E("fail to allocate the png row_pointers.\n");
+               png_destroy_write_struct(&pPngStruct, &pPngInfo);
+               fclose(fp);
+               return;
+       }
 
-       for (; y < height; ++y) {
-               png_bytep row =
-                       png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
+       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_LOG_E("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];
+
                        row[x * pixel_size] = (curBlock & 0xFF);
                        row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
                        row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
@@ -1537,13 +1616,9 @@ tbm_surface_internal_dump_start(char *path, int w, int h, int count)
        TBM_RETURN_IF_FAIL(count > 0);
 
        tbm_surface_dump_buf_info *buf_info = NULL;
-       tbm_surface_dump_buf_info *tmp;
-       tbm_bo bo = NULL;
-       int i;
-       int buffer_size;
        tbm_surface_h tbm_surface;
        tbm_surface_info_s info;
-       tbm_surface_error_e err;
+       int buffer_size, i;
 
        /* check running */
        if (g_dump_info) {
@@ -1566,8 +1641,9 @@ tbm_surface_internal_dump_start(char *path, int w, int h, int count)
                g_dump_info = NULL;
                return;
        }
-       err = tbm_surface_map(tbm_surface, TBM_SURF_OPTION_READ, &info);
-       if (err != TBM_SURFACE_ERROR_NONE) {
+
+       if (TBM_SURFACE_ERROR_NONE != tbm_surface_map(tbm_surface,
+                                               TBM_SURF_OPTION_READ, &info)) {
                TBM_LOG_E("tbm_surface_map fail\n");
                tbm_surface_destroy(tbm_surface);
                free(g_dump_info);
@@ -1575,15 +1651,20 @@ tbm_surface_internal_dump_start(char *path, int w, int h, int count)
                return;
        }
        buffer_size = info.planes[0].stride * h;
+
        tbm_surface_unmap(tbm_surface);
        tbm_surface_destroy(tbm_surface);
 
        /* create dump lists */
-       for (i = 0; i < count; i++)     {
+       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_LOG_E("fail to allocate the tbm_bo[%d]\n", i);
                        free(buf_info);
                        goto fail;
                }
@@ -1598,14 +1679,20 @@ tbm_surface_internal_dump_start(char *path, int w, int h, int count)
        g_dump_info->path = path;
        g_dump_info->link = &g_dump_info->surface_list;
 
+       scale_factor = 0.0;
+
        TBM_LOG_I("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);
                }
        }
@@ -1619,6 +1706,18 @@ fail:
 }
 
 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;
@@ -1627,83 +1726,76 @@ tbm_surface_internal_dump_end(void)
        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 */
-       if (!LIST_IS_EMPTY(&g_dump_info->surface_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(buf_info, tmp, &g_dump_info->surface_list, link) {
-                       char file[2048];
-
-                       if (buf_info->dirty) {
-                               void *ptr1 = NULL;
-                               void *ptr2 = NULL;
-
-                               bo_handle = tbm_bo_map(buf_info->bo, TBM_DEVICE_CPU, TBM_OPTION_READ);
-                               if (bo_handle.ptr == NULL)
-                                       continue;
-
-                               snprintf(file, sizeof(file), "%s/%s", g_dump_info->path, buf_info->name);
-                               TBM_LOG_I("Dump File.. %s generated.\n", file);
-
-                               switch (buf_info->info.format) {
-                               case TBM_FORMAT_ARGB8888:
-                               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);
-                                       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_LOG_E("can't dump %c%c%c%c buffer", FOURCC_STR(buf_info->info.format));
-                                       tbm_bo_unmap(buf_info->bo);
-                                       return;
-                               }
+       LIST_FOR_EACH_ENTRY_SAFE(buf_info, tmp, &g_dump_info->surface_list, link) {
+               char file[2048];
 
-                               tbm_bo_unmap(buf_info->bo);
-                       } else if (buf_info->dirty_shm) {
-                               bo_handle = tbm_bo_map(buf_info->bo, TBM_DEVICE_CPU, TBM_OPTION_READ);
-                               if (bo_handle.ptr == NULL)
-                                       continue;
+               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_LOG_I("Dump File.. %s generated.\n", file);
+               snprintf(file, sizeof(file), "%s/%s", g_dump_info->path, buf_info->name);
+               TBM_LOG_I("Dump File.. %s generated.\n", file);
 
-                               _tbm_surface_internal_dump_file_png(file, bo_handle.ptr,
-                                                               buf_info->shm_stride >> 2, buf_info->shm_h);
+               if (buf_info->dirty) {
+                       void *ptr1 = NULL, *ptr2 = NULL;
 
-                               tbm_bo_unmap(buf_info->bo);
+                       switch (buf_info->info.format) {
+                       case TBM_FORMAT_ARGB8888:
+                       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);
+                               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_LOG_E("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);
 
-       /* free resources */
-       if (!LIST_IS_EMPTY(&g_dump_info->surface_list)) {
-               LIST_FOR_EACH_ENTRY_SAFE(buf_info, tmp, &g_dump_info->surface_list, link) {
-                       tbm_bo_unref(buf_info->bo);
-                       free(buf_info);
-               }
+               tbm_bo_unmap(buf_info->bo);
+               tbm_bo_unref(buf_info->bo);
+               LIST_DEL(&buf_info->link);
+               free(buf_info);
        }
 
        free(g_dump_info);
@@ -1712,6 +1804,78 @@ tbm_surface_internal_dump_end(void)
        TBM_LOG_I("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;
+}
+
 void
 tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
 {
@@ -1719,11 +1883,11 @@ tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
        TBM_RETURN_IF_FAIL(type != NULL);
 
        tbm_surface_dump_buf_info *buf_info;
-       tbm_surface_info_s info;
        struct list_head *next_link;
+       tbm_surface_info_s info;
        tbm_bo_handle bo_handle;
-       int ret;
        const char *postfix;
+       int ret;
 
        if (!g_dump_info)
                return;
@@ -1742,10 +1906,42 @@ tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
        ret = tbm_surface_map(surface, TBM_SURF_OPTION_READ|TBM_SURF_OPTION_WRITE, &info);
        TBM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
 
-       if (info.size > buf_info->size) {
-               TBM_LOG_W("Dump skip. surface over created buffer size(%d, %d)\n", info.size, buf_info->size);
-               tbm_surface_unmap(surface);
-               return;
+       if (scale_factor > 0.0) {
+               const int bpp = 4;
+
+               if (info.format != TBM_FORMAT_ARGB8888 && info.format != TBM_FORMAT_XRGB8888) {
+                       TBM_LOG_W("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);
+               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_LOG_W("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_LOG_W("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)
@@ -1753,27 +1949,48 @@ tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
        else
                postfix = dump_postfix[1];
 
-       /* make the file information */
-       memcpy(&buf_info->info, &info, sizeof(tbm_surface_info_s));
-
        /* dump */
        bo_handle = tbm_bo_map(buf_info->bo, TBM_DEVICE_CPU, TBM_OPTION_WRITE);
-       TBM_RETURN_IF_FAIL(bo_handle.ptr != NULL);
+       if (!bo_handle.ptr) {
+               TBM_LOG_E("fail to map bo");
+               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_%p-%s.%s",
+               snprintf(buf_info->name, sizeof(buf_info->name),
+                               "%10.3f_%03d_%p-%s.%s",
                                 _tbm_surface_internal_get_time(),
                                 g_dump_info->count++, surface, type, postfix);
-               memcpy(bo_handle.ptr, info.planes[0].ptr, info.size);
+
+               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_LOG_E("fail to scale buffer");
+                               tbm_bo_unmap(buf_info->bo);
+                               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_%dx%d_%c%c%c%c.%s",
+               snprintf(buf_info->name, sizeof(buf_info->name),
+                               "%10.3f_%03d-%s_%dx%d_%c%c%c%c.%s",
                                 _tbm_surface_internal_get_time(),
-                                g_dump_info->count++, type, info.planes[0].stride, info.height, FOURCC_STR(info.format), postfix);
+                                g_dump_info->count++, 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));
@@ -1782,23 +1999,28 @@ tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
                break;
        case TBM_FORMAT_NV12:
        case TBM_FORMAT_NV21:
-               snprintf(buf_info->name, sizeof(buf_info->name), "%10.3f_%03d-%s_%dx%d_%c%c%c%c.%s",
+               snprintf(buf_info->name, sizeof(buf_info->name),
+                               "%10.3f_%03d-%s_%dx%d_%c%c%c%c.%s",
                                 _tbm_surface_internal_get_time(),
-                                g_dump_info->count++, type, info.planes[0].stride, info.height, FOURCC_STR(info.format), postfix);
+                                g_dump_info->count++, 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_%dx%d_%c%c%c%c.%s",
+               snprintf(buf_info->name, sizeof(buf_info->name),
+                               "%10.3f_%03d-%s_%dx%d_%c%c%c%c.%s",
                                 _tbm_surface_internal_get_time(),
-                                g_dump_info->count++, type, info.planes[0].stride, info.height, FOURCC_STR(info.format), postfix);
+                                g_dump_info->count++, 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_LOG_E("can't copy %c%c%c%c buffer", FOURCC_STR(info.format));
                tbm_bo_unmap(buf_info->bo);
+               tbm_surface_unmap(surface);
                return;
        }
 
@@ -1817,7 +2039,8 @@ tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
        TBM_LOG_I("Dump %s \n", buf_info->name);
 }
 
-void tbm_surface_internal_dump_shm_buffer(void *ptr, int w, int h, int  stride, const char *type)
+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);
@@ -1828,6 +2051,7 @@ void tbm_surface_internal_dump_shm_buffer(void *ptr, int w, int h, int  stride,
        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;
@@ -1843,28 +2067,53 @@ void tbm_surface_internal_dump_shm_buffer(void *ptr, int w, int h, int  stride,
        buf_info = LIST_ENTRY(tbm_surface_dump_buf_info, next_link, link);
        TBM_RETURN_IF_FAIL(buf_info != NULL);
 
-       if (stride * h > buf_info->size) {
-               TBM_LOG_W("Dump skip. shm buffer over created buffer size(%d, %d)\n", stride * h, buf_info->size);
+       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_LOG_W("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]);
-       memcpy(bo_handle.ptr, ptr, stride * h);
+       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_LOG_E("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;
-       buf_info->shm_stride = stride;
-       buf_info->shm_h = h;
 
        if (g_dump_info->count == 1000)
                g_dump_info->count = 0;
@@ -1873,4 +2122,113 @@ void tbm_surface_internal_dump_shm_buffer(void *ptr, int w, int h, int  stride,
 
        TBM_LOG_I("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_LOG_E("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_LOG_E("can't capture  buffer, exist file %s", file);
+               tbm_surface_unmap(surface);
+               return 0;
+       }
+
+       switch (info.format) {
+       case TBM_FORMAT_ARGB8888:
+       case TBM_FORMAT_XRGB8888:
+               _tbm_surface_internal_dump_file_png(file, info.planes[0].ptr,
+                                                       info.planes[0].stride >> 2,
+                                                       info.height);
+               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_LOG_E("can't dump %c%c%c%c buffer", FOURCC_STR(info.format));
+               tbm_surface_unmap(surface);
+               return 0;
+       }
+
+       tbm_surface_unmap(surface);
+
+       TBM_TRACE("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_LOG_E("Not supported type:%s'", type);
+               return 0;
+       }
+
+       if (!access(file, 0)) {
+               TBM_LOG_E("can't capture buffer, exist file %s", file);
+               return 0;
+       }
+
+       snprintf(file, sizeof(file), "%s/%s.%s", path , name, dump_postfix[0]);
+
+       _tbm_surface_internal_dump_file_png(file, ptr, stride, h);
+
+       TBM_TRACE("Capture %s \n", file);
+
+       return 1;
+}
 /*LCOV_EXCL_STOP*/