tbm_bufmgr: arrange tbm_bufmgr debug info
[platform/core/uifw/libtbm.git] / src / tbm_surface_internal.c
index a18ed01..cdafc92 100644 (file)
@@ -42,7 +42,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include <pixman.h>
 
 static tbm_bufmgr g_surface_bufmgr;
-static pthread_mutex_t tbm_surface_lock;
+static pthread_mutex_t tbm_surface_lock = PTHREAD_MUTEX_INITIALIZER;
 void _tbm_surface_mutex_unlock(void);
 
 #define C(b, m)              (((b) >> (m)) & 0xFF)
@@ -54,7 +54,8 @@ void _tbm_surface_mutex_unlock(void);
 /* check condition */
 #define TBM_SURFACE_RETURN_IF_FAIL(cond) {\
        if (!(cond)) {\
-               TBM_LOG_E("'%s' failed.\n", #cond);\
+               TBM_ERR("'%s' failed.\n", #cond);\
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);\
                _tbm_surface_mutex_unlock();\
                return;\
        } \
@@ -62,7 +63,8 @@ void _tbm_surface_mutex_unlock(void);
 
 #define TBM_SURFACE_RETURN_VAL_IF_FAIL(cond, val) {\
        if (!(cond)) {\
-               TBM_LOG_E("'%s' failed.\n", #cond);\
+               TBM_ERR("'%s' failed.\n", #cond);\
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);\
                _tbm_surface_mutex_unlock();\
                return val;\
        } \
@@ -218,32 +220,9 @@ _tbm_surface_internal_format_to_str(tbm_format format)
        }
 }
 
-static bool
-_tbm_surface_mutex_init(void)
-{
-       static bool tbm_surface_mutex_init = false;
-
-       if (tbm_surface_mutex_init)
-               return true;
-
-       if (pthread_mutex_init(&tbm_surface_lock, NULL)) {
-               TBM_LOG_E("fail: pthread_mutex_init for tbm_surface_lock.\n");
-               return false;
-       }
-
-       tbm_surface_mutex_init = true;
-
-       return true;
-}
-
 void
 _tbm_surface_mutex_lock(void)
 {
-       if (!_tbm_surface_mutex_init()) {
-               TBM_LOG_E("fail: _tbm_surface_mutex_init.\n");
-               return;
-       }
-
        pthread_mutex_lock(&tbm_surface_lock);
 }
 
@@ -280,14 +259,14 @@ _tbm_surface_internal_is_valid(tbm_surface_h surface)
 
        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);
+                       if (old_data == surface)
                                return 1;
-                       }
                }
        }
 
-       TBM_LOG_E("error: No valid tbm_surface(%p)\n", surface);
+       _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
+
+       TBM_ERR("error: No valid tbm_surface(%p)\n", surface);
 
        return 0;
 }
@@ -300,27 +279,49 @@ _tbm_surface_internal_query_plane_data(tbm_surface_h surface,
        TBM_RETURN_VAL_IF_FAIL(plane_idx > -1, 0);
 
        struct _tbm_surface *surf = (struct _tbm_surface *)surface;
-       struct _tbm_bufmgr *mgr = surf->bufmgr;
+       struct _tbm_bufmgr *bufmgr = surf->bufmgr;
        int ret = 0;
+       tbm_error_e error;
 
-       TBM_RETURN_VAL_IF_FAIL(mgr != NULL, 0);
+       TBM_RETURN_VAL_IF_FAIL(bufmgr != NULL, 0);
        TBM_RETURN_VAL_IF_FAIL(surf->info.width > 0, 0);
        TBM_RETURN_VAL_IF_FAIL(surf->info.height > 0, 0);
        TBM_RETURN_VAL_IF_FAIL(surf->info.format > 0, 0);
 
-       if (!mgr->backend->surface_get_plane_data)
-               return 0;
+       if (bufmgr->backend_module_data) {
+               if (!bufmgr->bufmgr_func->bufmgr_get_plane_data) {
+                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
+                       return 0;
+               }
 
-       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) {
-               /* LCOV_EXCL_START */
-               TBM_LOG_E("Fail to surface_get_plane_data. surface(%p)\n", surface);
-               return 0;
-               /* LCOV_EXCL_STOP */
+               error = bufmgr->bufmgr_func->bufmgr_get_plane_data(bufmgr->bufmgr_data, surf->info.format, plane_idx,
+                                               surf->info.width, surf->info.height, size, offset, pitch, bo_idx);
+               if (error != TBM_ERROR_NONE) {
+                       /* LCOV_EXCL_START */
+                       TBM_ERR("Fail to surface_get_plane_data. surface(%p) error(%d)\n", surface, error);
+                       _tbm_set_last_result(error);
+                       return 0;
+                       /* LCOV_EXCL_STOP */
+               }
+               ret = 1;
+       } else {
+               if (!bufmgr->backend->surface_get_plane_data) {
+                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
+                       return 0;
+               }
+
+               ret = bufmgr->backend->surface_get_plane_data(surf->info.width,
+                               surf->info.height, surf->info.format, plane_idx, size, offset, pitch, bo_idx);
+               if (!ret) {
+                       /* LCOV_EXCL_START */
+                       TBM_ERR("Fail to surface_get_plane_data. surface(%p)\n", surface);
+                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
+                       return 0;
+                       /* LCOV_EXCL_STOP */
+               }
        }
 
-       return 1;
+       return ret;
 }
 
 static void
@@ -356,7 +357,7 @@ _tbm_surface_internal_destroy(tbm_surface_h surface)
        free(surface);
        surface = NULL;
 
-       if (LIST_IS_EMPTY(&bufmgr->surf_list)) {
+       if (bufmgr && LIST_IS_EMPTY(&bufmgr->surf_list)) {
                LIST_DELINIT(&bufmgr->surf_list);
 
                if (!LIST_IS_EMPTY(&bufmgr->debug_key_list)) {
@@ -371,103 +372,25 @@ _tbm_surface_internal_destroy(tbm_surface_h surface)
 
 /* LCOV_EXCL_START */
 static int
-_tbm_surface_check_file_is_valid(const char* path, int del_link)
+_tbm_surface_check_file_is_symbolic_link(const char* path)
 {
-       char *real_path;
+       struct stat sb;
 
        if (!path)
                return 0;
 
-       real_path = realpath(path, NULL);
-       if (real_path && strncmp(path, real_path, strlen(path))) {
-               if (del_link)
-                       unlink(path);
-               free(real_path);
-
-               return 0;
-       }
-
-       if (real_path)
-               free(real_path);
-
-       return 1;
-}
-/* LCOV_EXCL_STOP */
-
-int
-tbm_surface_internal_is_valid(tbm_surface_h surface)
-{
-       int ret = 0;
-
-       _tbm_surface_mutex_lock();
-
-       /* Return silently if surface is null. */
-       if (!surface) {
-               _tbm_surface_mutex_unlock();
+       if (stat(path, &sb) != 0)
                return 0;
-       }
 
-       ret = _tbm_surface_internal_is_valid(surface);
-
-       _tbm_surface_mutex_unlock();
-
-       return ret;
-}
-
-int
-tbm_surface_internal_query_supported_formats(uint32_t **formats,
-               uint32_t *num)
-{
-       TBM_RETURN_VAL_IF_FAIL(formats, 0);
-       TBM_RETURN_VAL_IF_FAIL(num, 0);
-
-       struct _tbm_bufmgr *mgr;
-       int ret = 0;
-       bool bufmgr_initialized = false;
-
-       _tbm_surface_mutex_lock();
-
-       if (!g_surface_bufmgr) {
-               _init_surface_bufmgr();
-               LIST_INITHEAD(&g_surface_bufmgr->surf_list);
-               bufmgr_initialized = true;
-       }
-
-       mgr = g_surface_bufmgr;
-
-       if (!mgr->backend->surface_supported_format)
-               goto fail;
-
-       ret = mgr->backend->surface_supported_format(formats, num);
-       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);
-
-       _tbm_surface_mutex_unlock();
-
-       return ret;
-
-/* LCOV_EXCL_START */
-fail:
-       if (bufmgr_initialized) {
-               LIST_DELINIT(&g_surface_bufmgr->surf_list);
-               _deinit_surface_bufmgr();
-       }
-       _tbm_surface_mutex_unlock();
-
-       TBM_LOG_E("error: tbm_bufmgr(%p)\n", g_surface_bufmgr);
+       if (S_ISLNK(sb.st_mode))
+               return 1;
 
        return 0;
-/* LCOV_EXCL_STOP */
 }
+/* LCOV_EXCL_STOP */
 
-int
-tbm_surface_internal_get_num_planes(tbm_format format)
+static int
+_tbm_surface_internal_get_num_planes(tbm_format format)
 {
        int num_planes = 0;
 
@@ -539,16 +462,15 @@ tbm_surface_internal_get_num_planes(tbm_format format)
                break;
 
        default:
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
                break;
        }
 
-       TBM_TRACE("tbm_format(%s) num_planes(%d)\n", _tbm_surface_internal_format_to_str(format), num_planes);
-
        return num_planes;
 }
 
-int
-tbm_surface_internal_get_bpp(tbm_format format)
+static int
+_tbm_surface_internal_get_bpp(tbm_format format)
 {
 
        int bpp = 0;
@@ -634,10 +556,153 @@ tbm_surface_internal_get_bpp(tbm_format format)
                bpp = 24;
                break;
        default:
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
                break;
        }
 
-       TBM_TRACE("tbm_format(%s) bpp(%d)\n", _tbm_surface_internal_format_to_str(format), bpp);
+       return bpp;
+}
+
+int
+tbm_surface_internal_is_valid(tbm_surface_h surface)
+{
+       int ret = 0;
+
+       _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       /* Return silently if surface is null. */
+       if (!surface) {
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
+               _tbm_surface_mutex_unlock();
+               return 0;
+       }
+
+       ret = _tbm_surface_internal_is_valid(surface);
+
+       _tbm_surface_mutex_unlock();
+
+       return ret;
+}
+
+int
+tbm_surface_internal_query_supported_formats(uint32_t **formats,
+               uint32_t *num)
+{
+       struct _tbm_bufmgr *bufmgr;
+       int ret = 0;
+       bool bufmgr_initialized = false;
+       tbm_error_e error;
+
+       _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(formats, 0);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(num, 0);
+
+       if (!g_surface_bufmgr) {
+               _init_surface_bufmgr();
+               LIST_INITHEAD(&g_surface_bufmgr->surf_list);
+               bufmgr_initialized = true;
+       }
+
+       bufmgr = g_surface_bufmgr;
+
+       if (bufmgr->backend_module_data) {
+               if (!bufmgr->bufmgr_func->bufmgr_get_supported_formats) {
+                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
+                       goto fail;
+               }
+
+               error = bufmgr->bufmgr_func->bufmgr_get_supported_formats(bufmgr->bufmgr_data, formats, num);
+               if (error != TBM_ERROR_NONE) {
+                       /* LCOV_EXCL_START */
+                       TBM_ERR("Fail to surface_supported_format. error(%d)\n", error);
+                       goto fail;
+                       /* LCOV_EXCL_START */
+               }
+               ret = 1;
+       } else {
+               if (!bufmgr->backend->surface_supported_format) {
+                       _tbm_set_last_result(TBM_ERROR_NOT_SUPPORTED);
+                       goto fail;
+               }
+
+               ret = bufmgr->backend->surface_supported_format(formats, num);
+               if (!ret)  {
+                       /* LCOV_EXCL_START */
+                       TBM_ERR("Fail to surface_supported_format.\n");
+                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
+                       goto fail;
+                       /* LCOV_EXCL_START */
+               }
+       }
+
+       TBM_TRACE_SURFACE_INTERNAL("tbm_bufmgr(%p) format num(%u)\n", g_surface_bufmgr, *num);
+
+       if (bufmgr_initialized) {
+               LIST_DELINIT(&g_surface_bufmgr->surf_list);
+               _deinit_surface_bufmgr();
+       }
+
+       _tbm_surface_mutex_unlock();
+
+       return ret;
+
+/* LCOV_EXCL_START */
+fail:
+       if (bufmgr_initialized) {
+               LIST_DELINIT(&g_surface_bufmgr->surf_list);
+               _deinit_surface_bufmgr();
+       }
+       _tbm_surface_mutex_unlock();
+
+       TBM_ERR("error: tbm_bufmgr(%p)\n", g_surface_bufmgr);
+
+       return 0;
+/* LCOV_EXCL_STOP */
+}
+
+int
+tbm_surface_internal_get_num_planes(tbm_format format)
+{
+       int num_planes = 0;
+
+       _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       num_planes = _tbm_surface_internal_get_num_planes(format);
+       if (!num_planes) {
+               TBM_ERR("error: tbm_error(%s)\n", tbm_error_str(tbm_get_last_error()));
+               _tbm_surface_mutex_unlock();
+               return 0;
+       }
+
+       TBM_TRACE_SURFACE_INTERNAL("tbm_format(%s) num_planes(%d)\n", _tbm_surface_internal_format_to_str(format), num_planes);
+
+       _tbm_surface_mutex_unlock();
+
+       return num_planes;
+}
+
+int
+tbm_surface_internal_get_bpp(tbm_format format)
+{
+       int bpp = 0;
+
+       _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       bpp = _tbm_surface_internal_get_bpp(format);
+       if (!bpp) {
+               TBM_ERR("error: tbm_error(%s)\n", tbm_error_str(tbm_get_last_error()));
+               _tbm_surface_mutex_unlock();
+               return 0;
+       }
+
+       _tbm_surface_mutex_unlock();
+
+       TBM_TRACE_SURFACE_INTERNAL("tbm_format(%s) bpp(%d)\n", _tbm_surface_internal_format_to_str(format), bpp);
 
        return bpp;
 }
@@ -646,10 +711,7 @@ tbm_surface_h
 tbm_surface_internal_create_with_flags(int width, int height,
                                       int format, int flags)
 {
-       TBM_RETURN_VAL_IF_FAIL(width > 0, NULL);
-       TBM_RETURN_VAL_IF_FAIL(height > 0, NULL);
-
-       struct _tbm_bufmgr *mgr;
+       struct _tbm_bufmgr *bufmgr;
        struct _tbm_surface *surf = NULL;
        uint32_t size = 0;
        uint32_t offset = 0;
@@ -658,8 +720,16 @@ tbm_surface_internal_create_with_flags(int width, int height,
        int bo_idx;
        int i, j;
        bool bufmgr_initialized = false;
+       tbm_bo bo = NULL;
+       void *bo_priv = NULL;
+       tbm_backend_bo_data *bo_data = NULL;
+       tbm_error_e error;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(width > 0, NULL);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(height > 0, NULL);
 
        if (!g_surface_bufmgr) {
                _init_surface_bufmgr();
@@ -667,33 +737,42 @@ tbm_surface_internal_create_with_flags(int width, int height,
                bufmgr_initialized = true;
        }
 
-       mgr = g_surface_bufmgr;
-       if (!TBM_BUFMGR_IS_VALID(mgr)) {
-               TBM_LOG_E("The bufmgr is invalid\n");
+       bufmgr = g_surface_bufmgr;
+       if (!TBM_BUFMGR_IS_VALID(bufmgr)) {
+               TBM_ERR("The bufmgr is invalid\n");
                goto check_valid_fail;
        }
 
        surf = calloc(1, sizeof(struct _tbm_surface));
        if (!surf) {
                /* LCOV_EXCL_START */
-               TBM_LOG_E("fail to alloc surf\n");
+               TBM_ERR("fail to alloc surf\n");
+               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
                goto alloc_surf_fail;
                /* LCOV_EXCL_STOP */
        }
 
-       surf->bufmgr = mgr;
+       surf->bufmgr = bufmgr;
        surf->info.width = width;
        surf->info.height = height;
        surf->info.format = format;
-       surf->info.bpp = tbm_surface_internal_get_bpp(format);
-       surf->info.num_planes = tbm_surface_internal_get_num_planes(format);
+       surf->info.bpp = _tbm_surface_internal_get_bpp(format);
+       if (!surf->info.bpp) {
+               TBM_ERR("fail to get bpp. error(%s)\n", tbm_error_str(tbm_get_last_error()));
+               goto bpp_fail;
+       }
+       surf->info.num_planes = _tbm_surface_internal_get_num_planes(format);
+       if (!surf->info.num_planes) {
+               TBM_ERR("fail to get num_planes. error(%s)\n", tbm_error_str(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_LOG_E("fail to query plane data\n");
+                       TBM_ERR("fail to query plane data\n");
                        goto query_plane_data_fail;
                }
 
@@ -721,59 +800,133 @@ tbm_surface_internal_create_with_flags(int width, int height,
                                bo_size += surf->info.planes[j].size;
                }
 
-               if (mgr->backend->surface_bo_alloc) {
-                       /* LCOV_EXCL_START */
-                       tbm_bo bo = NULL;
-                       void *bo_priv = NULL;
+               if (bufmgr->backend_module_data) {
+                       if (bufmgr->bufmgr_func->bufmgr_alloc_bo_with_format) {
+                               /* LCOV_EXCL_START */
+                               bo = calloc(1, sizeof(struct _tbm_bo));
+                               if (!bo) {
+                                       TBM_ERR("fail to alloc bo struct\n");
+                                       _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+                                       goto alloc_bo_fail;
+                               }
 
-                       bo = calloc(1, sizeof(struct _tbm_bo));
-                       if (!bo) {
-                               TBM_LOG_E("fail to alloc bo struct\n");
-                               goto alloc_bo_fail;
-                       }
+                               bo->bufmgr = surf->bufmgr;
+
+                               _tbm_bufmgr_mutex_lock();
 
-                       bo->bufmgr = surf->bufmgr;
+                               bo_data = bufmgr->bufmgr_func->bufmgr_alloc_bo_with_format(bufmgr->bufmgr_data, format, i,
+                                                                                                                               width, height, flags, &error);
+                               if (!bo_data) {
+                                       TBM_ERR("fail to alloc bo priv. error(%d)\n", error);
+                                       _tbm_set_last_result(error);
+                                       free(bo);
+                                       _tbm_bufmgr_mutex_unlock();
+                                       goto alloc_bo_fail;
+                               }
+                               bo->bo_data = bo_data;
 
-                       pthread_mutex_lock(&surf->bufmgr->lock);
+                               bo->ref_cnt = 1;
+                               bo->flags = flags;
+                               LIST_INITHEAD(&bo->user_data_list);
 
-                       bo_priv = mgr->backend->surface_bo_alloc(bo, width, height, format, flags, i);
-                       if (!bo_priv) {
-                               TBM_LOG_E("fail to alloc bo priv\n");
-                               free(bo);
-                               pthread_mutex_unlock(&surf->bufmgr->lock);
-                               goto alloc_bo_fail;
-                       }
+                               LIST_ADD(&bo->item_link, &surf->bufmgr->bo_list);
 
-                       bo->ref_cnt = 1;
-                       bo->flags = flags;
-                       bo->priv = bo_priv;
+                               _tbm_bufmgr_mutex_unlock();
 
-                       LIST_INITHEAD(&bo->user_data_list);
+                               surf->bos[i] = bo;
+                               /* LCOV_EXCL_STOP */
+                       } else if (bufmgr->bufmgr_func->bufmgr_alloc_bo_with_tiled_format && (flags & TBM_BO_TILED)) {
+                               bo = calloc(1, sizeof(struct _tbm_bo));
+                               if (!bo) {
+                                       TBM_ERR("fail to alloc bo struct\n");
+                                       _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+                                       goto alloc_bo_fail;
+                               }
 
-                       LIST_ADD(&bo->item_link, &surf->bufmgr->bo_list);
+                               bo->bufmgr = surf->bufmgr;
 
-                       pthread_mutex_unlock(&surf->bufmgr->lock);
+                               _tbm_bufmgr_mutex_lock();
 
-                       surf->bos[i] = bo;
-                       /* LCOV_EXCL_STOP */
+                               bo_data = bufmgr->bufmgr_func->bufmgr_alloc_bo_with_tiled_format(bufmgr->bufmgr_data, width, height, surf->info.bpp/8, format, flags, i, &error);
+                               if (!bo_data) {
+                                       TBM_ERR("fail to alloc bo priv. error(%d)\n", error);
+                                       _tbm_set_last_result(error);
+                                       free(bo);
+                                       _tbm_bufmgr_mutex_unlock();
+                                       goto alloc_bo_fail;
+                               }
+                               bo->bo_data = bo_data;
+
+                               bo->ref_cnt = 1;
+                               bo->flags = flags;
+                               LIST_INITHEAD(&bo->user_data_list);
+
+                               LIST_ADD(&bo->item_link, &surf->bufmgr->bo_list);
+
+                               _tbm_bufmgr_mutex_unlock();
+
+                               surf->bos[i] = bo;
+
+                       } else {
+                               surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to alloc bo idx:%d\n", i);
+                                       goto alloc_bo_fail;
+                               }
+                       }
                } else {
-                       surf->bos[i] = tbm_bo_alloc(mgr, bo_size, flags);
-                       if (!surf->bos[i]) {
-                               TBM_LOG_E("fail to alloc bo idx:%d\n", i);
-                               goto alloc_bo_fail;
+                       if (bufmgr->backend->surface_bo_alloc) {
+                               /* LCOV_EXCL_START */
+                               bo = calloc(1, sizeof(struct _tbm_bo));
+                               if (!bo) {
+                                       TBM_ERR("fail to alloc bo struct\n");
+                                       _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
+                                       goto alloc_bo_fail;
+                               }
+
+                               bo->bufmgr = surf->bufmgr;
+
+                               _tbm_bufmgr_mutex_lock();
+
+                               bo_priv = bufmgr->backend->surface_bo_alloc(bo, width, height, format, flags, i);
+                               if (!bo_priv) {
+                                       TBM_ERR("fail to alloc bo priv\n");
+                                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
+                                       free(bo);
+                                       _tbm_bufmgr_mutex_unlock();
+                                       goto alloc_bo_fail;
+                               }
+                               bo->priv = bo_priv;
+
+                               bo->ref_cnt = 1;
+                               bo->flags = flags;
+                               LIST_INITHEAD(&bo->user_data_list);
+
+                               LIST_ADD(&bo->item_link, &surf->bufmgr->bo_list);
+
+                               _tbm_bufmgr_mutex_unlock();
+
+                               surf->bos[i] = bo;
+                               /* LCOV_EXCL_STOP */
+                       } else {
+                               surf->bos[i] = tbm_bo_alloc(bufmgr, bo_size, flags);
+                               if (!surf->bos[i]) {
+                                       TBM_ERR("fail to alloc bo idx:%d\n", i);
+                                       goto alloc_bo_fail;
+                               }
                        }
                }
 
                _tbm_bo_set_surface(surf->bos[i], surf);
        }
 
-       TBM_TRACE("width(%d) height(%d) format(%s) flags(%d) tbm_surface(%p)\n", width, height,
+       TBM_TRACE_SURFACE_INTERNAL("width(%d) height(%d) format(%s) flags(%d) tbm_surface(%p)\n", width, height,
                        _tbm_surface_internal_format_to_str(format), flags, surf);
 
        LIST_INITHEAD(&surf->user_data_list);
        LIST_INITHEAD(&surf->debug_data_list);
 
-       LIST_ADD(&surf->item_link, &mgr->surf_list);
+       LIST_ADD(&surf->item_link, &bufmgr->surf_list);
 
        _tbm_surface_mutex_unlock();
 
@@ -786,16 +939,18 @@ alloc_bo_fail:
                        tbm_bo_unref(surf->bos[j]);
        }
 query_plane_data_fail:
+bpp_fail:
+num_planes_fail:
        free(surf);
 alloc_surf_fail:
 check_valid_fail:
-       if (bufmgr_initialized && mgr) {
-               LIST_DELINIT(&mgr->surf_list);
+       if (bufmgr_initialized && bufmgr) {
+               LIST_DELINIT(&bufmgr->surf_list);
                _deinit_surface_bufmgr();
        }
        _tbm_surface_mutex_unlock();
 
-       TBM_LOG_E("error: width(%d) height(%d) format(%s) flags(%d)\n",
+       TBM_ERR("error: width(%d) height(%d) format(%s) flags(%d)\n",
                        width, height,
                        _tbm_surface_internal_format_to_str(format), flags);
 /* LCOV_EXCL_STOP */
@@ -807,18 +962,19 @@ tbm_surface_h
 tbm_surface_internal_create_with_bos(tbm_surface_info_s *info,
                                     tbm_bo *bos, int num)
 {
-       TBM_RETURN_VAL_IF_FAIL(bos, NULL);
-       TBM_RETURN_VAL_IF_FAIL(info, NULL);
-       TBM_RETURN_VAL_IF_FAIL(info->num_planes > 0, NULL);
-       TBM_RETURN_VAL_IF_FAIL(num > 0, NULL);
-       TBM_RETURN_VAL_IF_FAIL(num == 1 || info->num_planes == num, NULL);
-
-       struct _tbm_bufmgr *mgr;
+       struct _tbm_bufmgr *bufmgr;
        struct _tbm_surface *surf = NULL;
        int i;
        bool bufmgr_initialized = false;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(bos, NULL);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(info, NULL);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(info->num_planes > 0, NULL);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(num > 0, NULL);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(num == 1 || info->num_planes == num, NULL);
 
        if (!g_surface_bufmgr) {
                _init_surface_bufmgr();
@@ -826,28 +982,35 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info,
                bufmgr_initialized = true;
        }
 
-       mgr = g_surface_bufmgr;
-       if (!TBM_BUFMGR_IS_VALID(mgr)) {
-               TBM_LOG_E("fail to validate the Bufmgr.\n");
+       bufmgr = g_surface_bufmgr;
+       if (!TBM_BUFMGR_IS_VALID(bufmgr)) {
+               TBM_ERR("fail to validate the Bufmgr.\n");
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
                goto check_valid_fail;
        }
 
        surf = calloc(1, sizeof(struct _tbm_surface));
        if (!surf) {
                /* LCOV_EXCL_START */
-               TBM_LOG_E("fail to allocate struct _tbm_surface.\n");
+               TBM_ERR("fail to allocate struct _tbm_surface.\n");
+               _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
                goto alloc_surf_fail;
                /* LCOV_EXCL_STOP */
        }
 
-       surf->bufmgr = mgr;
+       surf->bufmgr = bufmgr;
        surf->info.width = info->width;
        surf->info.height = info->height;
        surf->info.format = info->format;
        if (info->bpp > 0)
                surf->info.bpp = info->bpp;
-       else
-               surf->info.bpp = tbm_surface_internal_get_bpp(info->format);
+       else {
+               surf->info.bpp = _tbm_surface_internal_get_bpp(info->format);
+               if (!surf->info.bpp) {
+                       TBM_ERR("fail to get bpp. error(%s)\n", tbm_error_str(tbm_get_last_error()));
+                       goto bpp_fail;
+               }
+       }
        surf->info.num_planes = info->num_planes;
        surf->refcnt = 1;
 
@@ -862,7 +1025,10 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info,
                        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);
+                       if (!_tbm_surface_internal_query_plane_data(surf, i, &size, &offset, &stride, &bo_idx)) {
+                               TBM_ERR("fail to get plane_data. error(%s)\n", tbm_error_str(tbm_get_last_error()));
+                               goto plane_data_fail;
+                       }
                        surf->info.planes[i].size = size;
                }
 
@@ -886,7 +1052,8 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info,
        surf->num_bos = num;
        for (i = 0; i < num; i++) {
                if (bos[i] == NULL) {
-                       TBM_LOG_E("bos[%d] is null.\n", i);
+                       TBM_ERR("bos[%d] is null.\n", i);
+                       _tbm_set_last_result(TBM_ERROR_INVALID_OPERATION);
                        goto check_bo_fail;
                }
 
@@ -894,13 +1061,13 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info,
                _tbm_bo_set_surface(bos[i], surf);
        }
 
-       TBM_TRACE("tbm_surface(%p) width(%u) height(%u) format(%s) bo_num(%d)\n", surf,
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) width(%u) height(%u) format(%s) bo_num(%d)\n", surf,
                        info->width, info->height, _tbm_surface_internal_format_to_str(info->format), num);
 
        LIST_INITHEAD(&surf->user_data_list);
        LIST_INITHEAD(&surf->debug_data_list);
 
-       LIST_ADD(&surf->item_link, &mgr->surf_list);
+       LIST_ADD(&surf->item_link, &bufmgr->surf_list);
 
        _tbm_surface_mutex_unlock();
 
@@ -908,6 +1075,8 @@ tbm_surface_internal_create_with_bos(tbm_surface_info_s *info,
 
 /* LCOV_EXCL_START */
 check_bo_fail:
+plane_data_fail:
+bpp_fail:
        for (i = 0; i < num; i++) {
                if (surf->bos[i])
                        tbm_bo_unref(surf->bos[i]);
@@ -915,13 +1084,13 @@ check_bo_fail:
        free(surf);
 alloc_surf_fail:
 check_valid_fail:
-       if (bufmgr_initialized && mgr) {
-               LIST_DELINIT(&mgr->surf_list);
+       if (bufmgr_initialized && bufmgr) {
+               LIST_DELINIT(&bufmgr->surf_list);
                _deinit_surface_bufmgr();
        }
        _tbm_surface_mutex_unlock();
 
-       TBM_LOG_E("error: width(%u) height(%u) format(%s) bo_num(%d)\n",
+       TBM_ERR("error: width(%u) height(%u) format(%s) bo_num(%d)\n",
                        info->width, info->height,
                        _tbm_surface_internal_format_to_str(info->format), num);
 /* LCOV_EXCL_STOP */
@@ -933,18 +1102,19 @@ void
 tbm_surface_internal_destroy(tbm_surface_h surface)
 {
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface));
 
        surface->refcnt--;
 
        if (surface->refcnt > 0) {
-               TBM_TRACE("reduce a refcnt(%d) of tbm_surface(%p)\n", surface->refcnt, surface);
+               TBM_TRACE_SURFACE_INTERNAL("reduce a refcnt(%d) of tbm_surface(%p)\n", surface->refcnt, surface);
                _tbm_surface_mutex_unlock();
                return;
        }
 
-       TBM_TRACE("destroy tbm_surface(%p) refcnt(%d)\n", surface, surface->refcnt);
+       TBM_TRACE_SURFACE_INTERNAL("destroy tbm_surface(%p) refcnt(%d)\n", surface, surface->refcnt);
 
        if (surface->refcnt == 0)
                _tbm_surface_internal_destroy(surface);
@@ -956,12 +1126,13 @@ void
 tbm_surface_internal_ref(tbm_surface_h surface)
 {
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface));
 
        surface->refcnt++;
 
-       TBM_TRACE("tbm_surface(%p) refcnt(%d)\n", surface, surface->refcnt);
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) refcnt(%d)\n", surface, surface->refcnt);
 
        _tbm_surface_mutex_unlock();
 }
@@ -970,18 +1141,19 @@ void
 tbm_surface_internal_unref(tbm_surface_h surface)
 {
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface));
 
        surface->refcnt--;
 
        if (surface->refcnt > 0) {
-               TBM_TRACE("reduce a refcnt(%d) of tbm_surface(%p)\n", surface->refcnt, surface);
+               TBM_TRACE_SURFACE_INTERNAL("reduce a refcnt(%d) of tbm_surface(%p)\n", surface->refcnt, surface);
                _tbm_surface_mutex_unlock();
                return;
        }
 
-       TBM_TRACE("destroy tbm_surface(%p) refcnt(%d)\n", surface, surface->refcnt);
+       TBM_TRACE_SURFACE_INTERNAL("destroy tbm_surface(%p) refcnt(%d)\n", surface, surface->refcnt);
 
        if (surface->refcnt == 0)
                _tbm_surface_internal_destroy(surface);
@@ -996,13 +1168,17 @@ tbm_surface_internal_get_num_bos(tbm_surface_h surface)
        int num;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        surf = (struct _tbm_surface *)surface;
        num = surf->num_bos;
 
-       TBM_TRACE("tbm_surface(%p) num_bos(%d)\n", surface, num);
+       if (!num)
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
+
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) num_bos(%d)\n", surface, num);
 
        _tbm_surface_mutex_unlock();
 
@@ -1016,6 +1192,7 @@ tbm_surface_internal_get_bo(tbm_surface_h surface, int bo_idx)
        tbm_bo bo;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), NULL);
        TBM_SURFACE_RETURN_VAL_IF_FAIL(bo_idx > -1, NULL);
@@ -1023,7 +1200,7 @@ tbm_surface_internal_get_bo(tbm_surface_h surface, int bo_idx)
        surf = (struct _tbm_surface *)surface;
        bo = surf->bos[bo_idx];
 
-       TBM_TRACE("tbm_surface(%p) bo_idx(%d) tbm_bo(%p)\n", surface, bo_idx, bo);
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) bo_idx(%d) tbm_bo(%p)\n", surface, bo_idx, bo);
 
        _tbm_surface_mutex_unlock();
 
@@ -1037,13 +1214,14 @@ tbm_surface_internal_get_size(tbm_surface_h surface)
        unsigned int size;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        surf = (struct _tbm_surface *)surface;
        size = surf->info.size;
 
-       TBM_TRACE("tbm_surface(%p) size(%u)\n", surface, size);
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) size(%u)\n", surface, size);
 
        _tbm_surface_mutex_unlock();
 
@@ -1057,6 +1235,7 @@ tbm_surface_internal_get_plane_data(tbm_surface_h surface, int plane_idx,
        struct _tbm_surface *surf;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
        TBM_SURFACE_RETURN_VAL_IF_FAIL(plane_idx > -1, 0);
@@ -1064,7 +1243,8 @@ tbm_surface_internal_get_plane_data(tbm_surface_h surface, int plane_idx,
        surf = (struct _tbm_surface *)surface;
 
        if (plane_idx >= surf->info.num_planes) {
-               TBM_TRACE("error: tbm_surface(%p) plane_idx(%d)\n", surface, plane_idx);
+               TBM_TRACE_SURFACE_INTERNAL("error: tbm_surface(%p) plane_idx(%d)\n", surface, plane_idx);
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
                _tbm_surface_mutex_unlock();
                return 0;
        }
@@ -1078,7 +1258,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(%u) offset(%u) pitch(%u)\n", surface, plane_idx,
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) plane_idx(%d) size(%u) offset(%u) pitch(%u)\n", surface, plane_idx,
                                surf->info.planes[plane_idx].size, surf->info.planes[plane_idx].offset,
                                surf->info.planes[plane_idx].stride);
 
@@ -1099,6 +1279,7 @@ tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
        int i, j;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
@@ -1134,7 +1315,7 @@ tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
                                for (j = 0; j < i; j++)
                                        tbm_bo_unmap(bos[j]);
 
-                               TBM_LOG_E("error: tbm_surface(%p) opt(%d) map(%d)\n", surface, opt, map);
+                               TBM_ERR("error: tbm_surface(%p) opt(%d) map(%d)\n", surface, opt, map);
                                return 0;
                        }
                }
@@ -1143,7 +1324,7 @@ tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
                for (i = 0; i < num_bos; i++) {
                        bo_handles[i] = tbm_bo_get_handle(bos[i], TBM_DEVICE_CPU);
                        if (bo_handles[i].ptr == NULL) {
-                               TBM_LOG_E("error: tbm_surface(%p) opt(%d) map(%d)\n", surface, opt, map);
+                               TBM_ERR("error: tbm_surface(%p) opt(%d) map(%d)\n", surface, opt, map);
                                _tbm_surface_mutex_unlock();
                                return 0;
                        }
@@ -1155,7 +1336,7 @@ tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
                        info->planes[i].ptr = bo_handles[planes_bo_idx[i]].ptr + info->planes[i].offset;
        }
 
-       TBM_TRACE("tbm_surface(%p) opt(%d) map(%d)\n", surface, opt, map);
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) opt(%d) map(%d)\n", surface, opt, map);
 
        _tbm_surface_mutex_unlock();
 
@@ -1169,6 +1350,7 @@ tbm_surface_internal_unmap(tbm_surface_h surface)
        int i;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_IF_FAIL(_tbm_surface_internal_is_valid(surface));
 
@@ -1177,7 +1359,7 @@ tbm_surface_internal_unmap(tbm_surface_h surface)
        for (i = 0; i < surf->num_bos; i++)
                tbm_bo_unmap(surf->bos[i]);
 
-       TBM_TRACE("tbm_surface(%p)\n", surface);
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p)\n", surface);
 
        _tbm_surface_mutex_unlock();
 }
@@ -1189,13 +1371,14 @@ tbm_surface_internal_get_width(tbm_surface_h surface)
        unsigned int width;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        surf = (struct _tbm_surface *)surface;
        width = surf->info.width;
 
-       TBM_TRACE("tbm_surface(%p) width(%u)\n", surface, width);
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) width(%u)\n", surface, width);
 
        _tbm_surface_mutex_unlock();
 
@@ -1209,13 +1392,14 @@ tbm_surface_internal_get_height(tbm_surface_h surface)
        unsigned int height;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        surf = (struct _tbm_surface *)surface;
        height = surf->info.height;
 
-       TBM_TRACE("tbm_surface(%p) height(%u)\n", surface, height);
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) height(%u)\n", surface, height);
 
        _tbm_surface_mutex_unlock();
 
@@ -1230,13 +1414,14 @@ tbm_surface_internal_get_format(tbm_surface_h surface)
        tbm_format format;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        surf = (struct _tbm_surface *)surface;
        format = surf->info.format;
 
-       TBM_TRACE("tbm_surface(%p) format(%s)\n", surface, _tbm_surface_internal_format_to_str(format));
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) format(%s)\n", surface, _tbm_surface_internal_format_to_str(format));
 
        _tbm_surface_mutex_unlock();
 
@@ -1250,6 +1435,7 @@ tbm_surface_internal_get_plane_bo_idx(tbm_surface_h surface, int plane_idx)
        int bo_idx;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
        TBM_SURFACE_RETURN_VAL_IF_FAIL(plane_idx > -1, 0);
@@ -1257,7 +1443,7 @@ tbm_surface_internal_get_plane_bo_idx(tbm_surface_h surface, int plane_idx)
        surf = (struct _tbm_surface *)surface;
        bo_idx = surf->planes_bo_idx[plane_idx];
 
-       TBM_TRACE("tbm_surface(%p) plane_idx(%d) bo_idx(%d)\n", surface, plane_idx, bo_idx);
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) plane_idx(%d) bo_idx(%d)\n", surface, plane_idx, bo_idx);
 
        _tbm_surface_mutex_unlock();
 
@@ -1271,25 +1457,27 @@ tbm_surface_internal_add_user_data(tbm_surface_h surface, unsigned long key,
        tbm_user_data *data;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        /* check if the data according to the key exist if so, return false. */
        data = user_data_lookup(&surface->user_data_list, key);
        if (data) {
-               TBM_TRACE("warning: user data already exist tbm_surface(%p) key(%lu)\n", surface, key);
+               TBM_TRACE_SURFACE_INTERNAL("warning: user data already exist tbm_surface(%p) key(%lu)\n", surface, key);
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
                _tbm_surface_mutex_unlock();
                return 0;
        }
 
        data = user_data_create(key, data_free_func);
        if (!data) {
-               TBM_TRACE("error: tbm_surface(%p) key(%lu)\n", surface, key);
+               TBM_TRACE_SURFACE_INTERNAL("error: tbm_surface(%p) key(%lu)\n", surface, key);
                _tbm_surface_mutex_unlock();
                return 0;
        }
 
-       TBM_TRACE("tbm_surface(%p) key(%lu) data(%p)\n", surface, key, data);
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) key(%lu) data(%p)\n", surface, key, data);
 
        LIST_ADD(&data->item_link, &surface->user_data_list);
 
@@ -1305,12 +1493,14 @@ tbm_surface_internal_set_user_data(tbm_surface_h surface, unsigned long key,
        tbm_user_data *old_data;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        old_data = user_data_lookup(&surface->user_data_list, key);
        if (!old_data) {
-               TBM_TRACE("error: tbm_surface(%p) key(%lu)\n", surface, key);
+               TBM_TRACE_SURFACE_INTERNAL("error: tbm_surface(%p) key(%lu)\n", surface, key);
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
                _tbm_surface_mutex_unlock();
                return 0;
        }
@@ -1320,7 +1510,7 @@ tbm_surface_internal_set_user_data(tbm_surface_h surface, unsigned long key,
 
        old_data->data = data;
 
-       TBM_TRACE("tbm_surface(%p) key(%lu) data(%p)\n", surface, key, old_data->data);
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) key(%lu) data(%p)\n", surface, key, old_data->data);
 
        _tbm_surface_mutex_unlock();
 
@@ -1334,11 +1524,13 @@ tbm_surface_internal_get_user_data(tbm_surface_h surface, unsigned long key,
        tbm_user_data *old_data;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        if (!data) {
-               TBM_LOG_E("error: tbm_surface(%p) key(%lu)\n", surface, key);
+               TBM_ERR("error: tbm_surface(%p) key(%lu)\n", surface, key);
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
                _tbm_surface_mutex_unlock();
                return 0;
        }
@@ -1346,14 +1538,15 @@ tbm_surface_internal_get_user_data(tbm_surface_h surface, unsigned long key,
 
        old_data = user_data_lookup(&surface->user_data_list, key);
        if (!old_data) {
-               TBM_TRACE("error: tbm_surface(%p) key(%lu)\n", surface, key);
+               TBM_TRACE_SURFACE_INTERNAL("error: tbm_surface(%p) key(%lu)\n", surface, key);
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
                _tbm_surface_mutex_unlock();
                return 0;
        }
 
        *data = old_data->data;
 
-       TBM_TRACE("tbm_surface(%p) key(%lu) data(%p)\n", surface, key, old_data->data);
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) key(%lu) data(%p)\n", surface, key, old_data->data);
 
        _tbm_surface_mutex_unlock();
 
@@ -1367,17 +1560,19 @@ tbm_surface_internal_delete_user_data(tbm_surface_h surface,
        tbm_user_data *old_data = (void *)0;
 
        _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
 
        TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
 
        old_data = user_data_lookup(&surface->user_data_list, key);
        if (!old_data) {
-               TBM_TRACE("error: tbm_surface(%p) key(%lu)\n", surface, key);
+               TBM_TRACE_SURFACE_INTERNAL("error: tbm_surface(%p) key(%lu)\n", surface, key);
+               _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
                _tbm_surface_mutex_unlock();
                return 0;
        }
 
-       TBM_TRACE("tbm_surface(%p) key(%lu) data(%p)\n", surface, key, old_data->data);
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) key(%lu) data(%p)\n", surface, key, old_data->data);
 
        user_data_delete(old_data);
 
@@ -1399,6 +1594,7 @@ 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));
 
@@ -1413,8 +1609,11 @@ _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)
+       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);
@@ -1430,6 +1629,7 @@ tbm_surface_internal_set_debug_data(tbm_surface_h surface, char *key, char *valu
        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);
@@ -1443,7 +1643,7 @@ tbm_surface_internal_set_debug_data(tbm_surface_h surface, char *key, char *valu
                        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);
+                                               TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) Already exist key(%s) and value(%s)!\n", surface, key, value);
                                                goto add_debug_key_list;
                                        }
 
@@ -1463,12 +1663,12 @@ tbm_surface_internal_set_debug_data(tbm_surface_h surface, char *key, char *valu
 
        debug_data = _tbm_surface_internal_debug_data_create(key, value);
        if (!debug_data) {
-               TBM_LOG_E("error: tbm_surface(%p) key(%s) value(%s)\n", surface, key, value);
+               TBM_ERR("error: tbm_surface(%p) key(%s) value(%s)\n", surface, key, value);
                _tbm_surface_mutex_unlock();
                return 0;
        }
 
-       TBM_TRACE("tbm_surface(%p) key(%s) value(%s)\n", surface, key, value);
+       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);
 
@@ -1483,7 +1683,7 @@ add_debug_key_list:
        }
 
        debug_data = _tbm_surface_internal_debug_data_create(key, NULL);
-       LIST_ADD(&debug_data->item_link, &bufmgr->debug_key_list);
+       LIST_ADDTAIL(&debug_data->item_link, &bufmgr->debug_key_list);
 
        _tbm_surface_mutex_unlock();
 
@@ -1544,8 +1744,8 @@ _tbm_surface_internal_dump_file_raw(const char *file, void *data1, int size1,
        FILE *fp;
        unsigned int *blocks;
 
-       if (!_tbm_surface_check_file_is_valid(file, 1))
-               TBM_LOG_E("%s is symbolic link\n", file);
+       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);
@@ -1567,7 +1767,7 @@ _tbm_surface_internal_dump_file_raw(const char *file, void *data1, int size1,
 }
 
 static void
-_tbm_surface_internal_dump_file_png(const char *file, const void *data, int width, int height, int format)
+_tbm_surface_internal_dump_file_png(const char *file, const void *data, int width, int height, int stride, int format)
 {
        unsigned int *blocks = (unsigned int *)data;
        FILE *fp;
@@ -1575,8 +1775,8 @@ _tbm_surface_internal_dump_file_png(const char *file, const void *data, int widt
        png_bytep *row_pointers;
        int depth = 8, y;
 
-       if (!_tbm_surface_check_file_is_valid(file, 1))
-               TBM_LOG_E("%s is symbolic link\n", file);
+       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);
@@ -1584,19 +1784,27 @@ _tbm_surface_internal_dump_file_png(const char *file, const void *data, int widt
        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");
+               TBM_ERR("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");
+               TBM_ERR("fail to create a png info structure.\n");
                png_destroy_write_struct(&pPngStruct, NULL);
                fclose(fp);
                return;
        }
 
+       if (setjmp(png_jmpbuf(pPngStruct))) {
+               /* if png has problem of writing the file, we get here */
+               TBM_ERR("fail to write png file.\n");
+               png_destroy_write_struct(&pPngStruct, &pPngInfo);
+               fclose(fp);
+               return;
+       }
+
        png_init_io(pPngStruct, fp);
        if (format == TBM_FORMAT_XRGB8888) {
                pixel_size = 3;
@@ -1625,7 +1833,7 @@ _tbm_surface_internal_dump_file_png(const char *file, const void *data, int widt
 
        row_pointers = png_malloc(pPngStruct, height * sizeof(png_byte *));
        if (!row_pointers) {
-               TBM_LOG_E("fail to allocate the png row_pointers.\n");
+               TBM_ERR("fail to allocate the png row_pointers.\n");
                png_destroy_write_struct(&pPngStruct, &pPngInfo);
                fclose(fp);
                return;
@@ -1637,7 +1845,7 @@ _tbm_surface_internal_dump_file_png(const char *file, const void *data, int widt
 
                row = png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
                if (!row) {
-                       TBM_LOG_E("fail to allocate the png row.\n");
+                       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);
@@ -1648,7 +1856,7 @@ _tbm_surface_internal_dump_file_png(const char *file, const void *data, int widt
                row_pointers[y] = (png_bytep)row;
 
                for (x = 0; x < width; ++x) {
-                       unsigned int curBlock = blocks[y * width + x];
+                       unsigned int curBlock = blocks[(y * (stride >> 2)) + x];
 
                        if (pixel_size == 3) { // XRGB8888
                                row[x * pixel_size] = (curBlock & 0xFF);
@@ -1690,7 +1898,7 @@ tbm_surface_internal_dump_start(char *path, int w, int h, int count)
 
        /* check running */
        if (g_dump_info) {
-               TBM_LOG_W("waring already running the tbm_surface_internal_dump.\n");
+               TBM_WRN("waring already running the tbm_surface_internal_dump.\n");
                return;
        }
 
@@ -1704,14 +1912,14 @@ tbm_surface_internal_dump_start(char *path, int w, int h, int count)
        /* get buffer size */
        tbm_surface = tbm_surface_create(w, h, TBM_FORMAT_ARGB8888);
        if (tbm_surface == NULL) {
-               TBM_LOG_E("tbm_surface_create fail\n");
+               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_LOG_E("tbm_surface_get_info fail\n");
+               TBM_ERR("tbm_surface_get_info fail\n");
                tbm_surface_destroy(tbm_surface);
                free(g_dump_info);
                g_dump_info = NULL;
@@ -1729,7 +1937,7 @@ tbm_surface_internal_dump_start(char *path, int w, int h, int count)
 
                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);
+                       TBM_ERR("fail to allocate the tbm_bo[%d]\n", i);
                        free(buf_info);
                        goto fail;
                }
@@ -1746,7 +1954,7 @@ tbm_surface_internal_dump_start(char *path, int w, int h, int count)
 
        scale_factor = 0.0;
 
-       TBM_LOG_I("Dump Start.. path:%s, count:%d\n", g_dump_info->path, count);
+       TBM_INFO("Dump Start.. path:%s, count:%d\n", g_dump_info->path, count);
 
        return;
 
@@ -1762,7 +1970,7 @@ fail:
                }
        }
 
-       TBM_LOG_E("Dump Start fail.. path:%s\n", g_dump_info->path);
+       TBM_ERR("Dump Start fail.. path:%s\n", g_dump_info->path);
 
        free(g_dump_info);
        g_dump_info = NULL;
@@ -1810,7 +2018,7 @@ tbm_surface_internal_dump_end(void)
                }
 
                snprintf(file, sizeof(file), "%s/%s", g_dump_info->path, buf_info->name);
-               TBM_LOG_I("Dump File.. %s generated.\n", file);
+               TBM_INFO("Dump File.. %s generated.\n", file);
 
                if (buf_info->dirty) {
                        void *ptr1 = NULL, *ptr2 = NULL;
@@ -1819,12 +2027,16 @@ tbm_surface_internal_dump_end(void)
                        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);
+                                                       buf_info->info.height,
+                                                       buf_info->info.planes[0].stride,
+                                                       TBM_FORMAT_ARGB8888);
                                break;
                        case TBM_FORMAT_XRGB8888:
                                _tbm_surface_internal_dump_file_png(file, bo_handle.ptr,
                                                        buf_info->info.planes[0].stride >> 2,
-                                                       buf_info->info.height, TBM_FORMAT_XRGB8888);
+                                                       buf_info->info.height,
+                                                       buf_info->info.planes[0].stride,
+                                                       TBM_FORMAT_XRGB8888);
                                break;
                        case TBM_FORMAT_YVU420:
                        case TBM_FORMAT_YUV420:
@@ -1853,13 +2065,14 @@ tbm_surface_internal_dump_end(void)
                                                        NULL, 0, NULL, 0);
                                break;
                        default:
-                               TBM_LOG_E("can't dump %c%c%c%c buffer", FOURCC_STR(buf_info->info.format));
+                               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);
+                                                       buf_info->shm_h,
+                                                       buf_info->shm_stride, 0);
 
                tbm_bo_unmap(buf_info->bo);
                tbm_bo_unref(buf_info->bo);
@@ -1870,7 +2083,7 @@ tbm_surface_internal_dump_end(void)
        free(g_dump_info);
        g_dump_info = NULL;
 
-       TBM_LOG_I("Dump End..\n");
+       TBM_INFO("Dump End..\n");
 }
 
 static pixman_format_code_t
@@ -1968,7 +2181,7 @@ static char *_tbm_surface_internal_get_keys(tbm_surface_h surface)
 
        keys = calloc(KEYS_LEN + 1, sizeof(char));
        if (!keys) {
-               TBM_LOG_E("Failed to alloc memory");
+               TBM_ERR("Failed to alloc memory");
                _tbm_surface_mutex_unlock();
                return NULL;
        }
@@ -1977,7 +2190,7 @@ static char *_tbm_surface_internal_get_keys(tbm_surface_h surface)
                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);
+               strncat(keys, temp_key, KEY_LEN + 1);
        }
 
        _tbm_surface_mutex_unlock();
@@ -2027,7 +2240,7 @@ tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
                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_WRN("Dump with scale skip. unsupported format(%s)\n",
                                          _tbm_surface_internal_format_to_str(info.format));
                        tbm_surface_unmap(surface);
                        return;
@@ -2038,20 +2251,25 @@ tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
                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.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_LOG_W("Dump with scale skip. surface over created buffer size(%u, %d)\n",
+                       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_LOG_W("Dump skip. surface over created buffer size(%u, %d)\n",
+                       TBM_WRN("Dump skip. surface over created buffer size(%u, %d)\n",
                                        info.size, buf_info->size);
                        tbm_surface_unmap(surface);
                        return;
@@ -2069,7 +2287,7 @@ tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
 
        keys = _tbm_surface_internal_get_keys(surface);
        if (!keys) {
-               TBM_LOG_E("fail to get keys");
+               TBM_ERR("fail to get keys");
                tbm_surface_unmap(surface);
                return;
        }
@@ -2077,7 +2295,7 @@ tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
        /* dump */
        bo_handle = tbm_bo_map(buf_info->bo, TBM_DEVICE_CPU, TBM_OPTION_WRITE);
        if (!bo_handle.ptr) {
-               TBM_LOG_E("fail to map bo");
+               TBM_ERR("fail to map bo");
                _tbm_surface_internal_put_keys(keys);
                tbm_surface_unmap(surface);
                return;
@@ -2102,7 +2320,7 @@ tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
                                                                                                         buf_info->info.width,
                                                                                                         buf_info->info.height);
                        if (ret != TBM_SURFACE_ERROR_NONE) {
-                               TBM_LOG_E("fail to scale buffer");
+                               TBM_ERR("fail to scale buffer");
                                tbm_bo_unmap(buf_info->bo);
                                _tbm_surface_internal_put_keys(keys);
                                tbm_surface_unmap(surface);
@@ -2145,7 +2363,7 @@ tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
                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_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);
@@ -2166,7 +2384,7 @@ tbm_surface_internal_dump_buffer(tbm_surface_h surface, const char *type)
 
        g_dump_info->link = next_link;
 
-       TBM_LOG_I("Dump %s \n", buf_info->name);
+       TBM_INFO("Dump %s \n", buf_info->name);
 }
 
 void tbm_surface_internal_dump_shm_buffer(void *ptr, int w, int h, int stride,
@@ -2208,7 +2426,7 @@ void tbm_surface_internal_dump_shm_buffer(void *ptr, int w, int h, int stride,
                size = stride * h;
 
        if (size > buf_info->size) {
-               TBM_LOG_W("Dump skip. shm buffer over created buffer size(%d, %d)\n",
+               TBM_WRN("Dump skip. shm buffer over created buffer size(%d, %d)\n",
                                size, buf_info->size);
                return;
        }
@@ -2228,7 +2446,7 @@ void tbm_surface_internal_dump_shm_buffer(void *ptr, int w, int h, int stride,
                                                                                                 TBM_FORMAT_ARGB8888, stride,
                                                                                                 w, h, dstride, dw, dh);
                if (ret != TBM_SURFACE_ERROR_NONE) {
-                       TBM_LOG_E("fail to scale buffer");
+                       TBM_ERR("fail to scale buffer");
                        tbm_bo_unmap(buf_info->bo);
                        return;
                }
@@ -2250,7 +2468,7 @@ void tbm_surface_internal_dump_shm_buffer(void *ptr, int w, int h, int stride,
 
        g_dump_info->link = next_link;
 
-       TBM_LOG_I("Dump %s \n", buf_info->name);
+       TBM_INFO("Dump %s \n", buf_info->name);
 }
 
 int
@@ -2274,7 +2492,7 @@ tbm_surface_internal_capture_buffer(tbm_surface_h surface, const char *path, con
                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_ERR("not support type(%s) %c%c%c%c buffer", type, FOURCC_STR(info.format));
                tbm_surface_unmap(surface);
                return 0;
        }
@@ -2282,7 +2500,7 @@ tbm_surface_internal_capture_buffer(tbm_surface_h surface, const char *path, con
        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_ERR("can't capture  buffer, exist file %s", file);
                tbm_surface_unmap(surface);
                return 0;
        }
@@ -2290,13 +2508,17 @@ tbm_surface_internal_capture_buffer(tbm_surface_h surface, const char *path, con
        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);
+                                                       info.width,
+                                                       info.height,
+                                                       info.planes[0].stride,
+                                                       TBM_FORMAT_ARGB8888);
                break;
        case TBM_FORMAT_XRGB8888:
                _tbm_surface_internal_dump_file_png(file, info.planes[0].ptr,
-                                                       info.planes[0].stride >> 2,
-                                                       info.height, TBM_FORMAT_XRGB8888);
+                                                       info.width,
+                                                       info.height,
+                                                       info.planes[0].stride,
+                                                       TBM_FORMAT_XRGB8888);
                break;
        case TBM_FORMAT_YVU420:
        case TBM_FORMAT_YUV420:
@@ -2322,14 +2544,14 @@ tbm_surface_internal_capture_buffer(tbm_surface_h surface, const char *path, con
                                        NULL, 0, NULL, 0);
                break;
        default:
-               TBM_LOG_E("can't dump %c%c%c%c buffer", FOURCC_STR(info.format));
+               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("Capture %s \n", file);
+       TBM_TRACE_SURFACE_INTERNAL("Capture %s \n", file);
 
        return 1;
 }
@@ -2348,20 +2570,72 @@ tbm_surface_internal_capture_shm_buffer(void *ptr, int w, int h, int stride,
        char file[1024];
 
        if (strcmp(dump_postfix[0], type)) {
-               TBM_LOG_E("Not supported type:%s'", type);
+               TBM_ERR("Not supported type:%s'", type);
                return 0;
        }
 
        snprintf(file, sizeof(file), "%s/%s.%s", path , name, dump_postfix[0]);
 
        if (!access(file, 0)) {
-               TBM_LOG_E("can't capture buffer, exist file %sTBM_FORMAT_XRGB8888", file);
+               TBM_ERR("can't capture buffer, exist file %sTBM_FORMAT_XRGB8888", file);
                return 0;
        }
 
-       _tbm_surface_internal_dump_file_png(file, ptr, w, h, 0);
+       _tbm_surface_internal_dump_file_png(file, ptr, w, h, stride, 0);
+
+       TBM_TRACE_SURFACE_INTERNAL("Capture %s \n", file);
+
+       return 1;
+}
+
+int
+tbm_surface_internal_set_damage(tbm_surface_h surface, int x, int y, int width, int height)
+{
+       struct _tbm_surface *surf;
+
+       _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(width > 0, 0);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(height > 0, 0);
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
+
+       surf = (struct _tbm_surface *)surface;
+
+       surf->damage.x = x;
+       surf->damage.y = y;
+       surf->damage.width = width;
+       surf->damage.height = height;
+
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) x(%d) y(%d) width(%d) height(%d)\n",
+                                                       surface, x, y, width, height);
 
-       TBM_TRACE("Capture %s \n", file);
+       _tbm_surface_mutex_unlock();
+
+       return 1;
+}
+
+int
+tbm_surface_internal_get_damage(tbm_surface_h surface, int *x, int *y, int *width, int *height)
+{
+       struct _tbm_surface *surf;
+
+       _tbm_surface_mutex_lock();
+       _tbm_set_last_result(TBM_ERROR_NONE);
+
+       TBM_SURFACE_RETURN_VAL_IF_FAIL(_tbm_surface_internal_is_valid(surface), 0);
+
+       surf = (struct _tbm_surface *)surface;
+
+       if (x) *x = surf->damage.x;
+       if (y) *y = surf->damage.y;
+       if (width) *width = surf->damage.width;
+       if (height) *height = surf->damage.height;
+
+       TBM_TRACE_SURFACE_INTERNAL("tbm_surface(%p) x(%d) y(%d) width(%d) height(%d)\n",
+                                                       surface, surf->damage.x, surf->damage.y, surf->damage.width, surf->damage.height);
+
+       _tbm_surface_mutex_unlock();
 
        return 1;
 }