add tdm_helper_convert_buffer function 30/99930/2
authorBoram Park <boram1288.park@samsung.com>
Thu, 24 Nov 2016 03:19:30 +0000 (12:19 +0900)
committerSooChan Lim <sc1.lim@samsung.com>
Fri, 25 Nov 2016 07:52:08 +0000 (23:52 -0800)
Change-Id: I4c63d1e3546194229daf19f4a59444b96a33f46c

include/tdm_helper.h
src/tdm_helper.c

index 3de7da3..a6791aa 100644 (file)
@@ -110,6 +110,20 @@ void
 tdm_helper_clear_buffer(tbm_surface_h buffer);
 
 /**
+ * @brief convert the source buffer to the destination buffer with given rectangles
+ * trannsform
+ * @details
+ * This function supports only if buffers have below formats.
+ * - TBM_FORMAT_ARGB8888
+ * - TBM_FORMAT_XRGB8888
+ * @param[in] buffer A TDM buffer
+ */
+tdm_error
+tdm_helper_convert_buffer(tbm_surface_h srcbuf, tbm_surface_h dstbuf,
+                                                 tdm_pos *srcpos, tdm_pos *dstpos,
+                                                 tdm_transform transform, int over);
+
+/**
  * @brief Get a fd from the given enviroment variable.
  * @details
  * This function will dup the fd of the given enviroment variable. The Caller
index 3e4af9d..e91033e 100644 (file)
@@ -364,6 +364,134 @@ tdm_helper_clear_buffer(tbm_surface_h buffer)
        tdm_helper_clear_buffer_pos(buffer, NULL);
 }
 
+static pixman_format_code_t
+_tdm_helper_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;
+}
+
+EXTERN tdm_error
+tdm_helper_convert_buffer(tbm_surface_h srcbuf, tbm_surface_h dstbuf,
+                                                 tdm_pos *srcpos, tdm_pos *dstpos,
+                                                 tdm_transform transform, int over)
+{
+       tbm_surface_info_s src_info, dst_info;
+       pixman_image_t *src_img = NULL, *dst_img = NULL;
+       pixman_format_code_t src_format, dst_format;
+       double scale_x, scale_y;
+       int rotate_step, bos;
+       pixman_transform_t t;
+       struct pixman_f_transform ft;
+       pixman_op_t op;
+       int src_stride, dst_stride;
+       int buf_width, err;
+       tdm_error ret = TDM_ERROR_OPERATION_FAILED;
+
+       TDM_RETURN_VAL_IF_FAIL(srcbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(dstbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       bos = tbm_surface_internal_get_num_bos(srcbuf);
+       TDM_RETURN_VAL_IF_FAIL(bos == 1, TDM_ERROR_OPERATION_FAILED);
+
+       bos = tbm_surface_internal_get_num_bos(dstbuf);
+       TDM_RETURN_VAL_IF_FAIL(bos == 1, TDM_ERROR_OPERATION_FAILED);
+
+       err = tbm_surface_map(srcbuf, TBM_OPTION_READ, &src_info);
+       TDM_RETURN_VAL_IF_FAIL(err == TBM_SURFACE_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
+
+       err = tbm_surface_map(dstbuf, TBM_OPTION_WRITE, &dst_info);
+       TDM_GOTO_IF_FAIL(err == TBM_SURFACE_ERROR_NONE, unmap_srcbuf);
+
+       /* not handle buffers which have 2 more gem handles */
+       TDM_GOTO_IF_FAIL(src_info.planes[0].ptr != NULL, unmap_dstbuf);
+       TDM_GOTO_IF_FAIL(dst_info.planes[0].ptr != NULL, unmap_dstbuf);
+
+       src_format = _tdm_helper_pixman_format_get(src_info.format);
+       TDM_GOTO_IF_FAIL(src_format > 0, unmap_dstbuf);
+       dst_format = _tdm_helper_pixman_format_get(dst_info.format);
+       TDM_GOTO_IF_FAIL(dst_format > 0, unmap_dstbuf);
+
+       buf_width = src_info.planes[0].stride >> 2;
+       src_stride = src_info.planes[0].stride;
+       src_img = pixman_image_create_bits(src_format, buf_width, src_info.height,
+                                                                          (uint32_t*)src_info.planes[0].ptr, src_stride);
+       TDM_GOTO_IF_FAIL(src_img, unref_img);
+
+       buf_width = dst_info.planes[0].stride >> 2;
+       dst_stride = dst_info.planes[0].stride;
+       dst_img = pixman_image_create_bits(dst_format, buf_width, dst_info.height,
+                                                                          (uint32_t*)dst_info.planes[0].ptr, dst_stride);
+       TDM_GOTO_IF_FAIL(dst_img, unref_img);
+
+       pixman_f_transform_init_identity(&ft);
+
+       if (transform & TDM_TRANSFORM_FLIPPED) {
+               pixman_f_transform_scale(&ft, NULL, -1, 1);
+               pixman_f_transform_translate(&ft, NULL, dstpos->w, 0);
+       }
+
+       rotate_step = transform & 0x3;
+       if (rotate_step > 0) {
+               int c, s, tx = 0, ty = 0;
+               switch (rotate_step) {
+                       case 1:
+                               c = 0, s = -1, tx = -dstpos->w;
+                               break;
+                       case 2:
+                               c = -1, s = 0, tx = -dstpos->w, ty = -dstpos->h;
+                               break;
+                       case 3:
+                               c = 0, s = 1, ty = -dstpos->h;
+                               break;
+                       default:
+                               break;
+               }
+               pixman_f_transform_translate(&ft, NULL, tx, ty);
+               pixman_f_transform_rotate(&ft, NULL, c, s);
+       }
+
+       if (rotate_step % 2 == 0) {
+               scale_x = (double)srcpos->w / dstpos->w;
+               scale_y = (double)srcpos->h / dstpos->h;
+       } else {
+               scale_x = (double)srcpos->w / dstpos->h;
+               scale_y = (double)srcpos->h / dstpos->w;
+       }
+
+       pixman_f_transform_scale(&ft, NULL, scale_x, scale_y);
+       pixman_f_transform_translate(&ft, NULL, srcpos->x, srcpos->y);
+       pixman_transform_from_pixman_f_transform(&t, &ft);
+       pixman_image_set_transform(src_img, &t);
+
+       op = (!over) ? PIXMAN_OP_SRC : PIXMAN_OP_OVER;
+
+       pixman_image_composite(op, src_img, NULL, dst_img, 0, 0, 0, 0,
+                                                  dstpos->x, dstpos->y, dstpos->w, dstpos->h);
+
+       ret = TDM_ERROR_NONE;
+
+unref_img:
+       if (src_img)
+               pixman_image_unref(src_img);
+       if (dst_img)
+               pixman_image_unref(dst_img);
+unmap_dstbuf:
+       tbm_surface_unmap(dstbuf);
+unmap_srcbuf:
+       tbm_surface_unmap(srcbuf);
+
+       return ret;
+}
+
 EXTERN int
 tdm_helper_get_fd(const char *env)
 {