tdm_helper: clean up _tdm_helper_dump_png()
[platform/core/uifw/libtdm.git] / src / tdm_helper.c
index 9547582..95dbe7f 100644 (file)
 static const char *file_exts[2] = {"png", "yuv"};
 
 int tdm_dump_enable;
+char *tdm_debug_dump_dir;
 
-INTERN unsigned long
-tdm_helper_get_time_in_millis(void)
+EXTERN double
+tdm_helper_get_time(void)
 {
        struct timespec tp;
 
        if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
-               return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
-
-       return 0;
-}
-
-INTERN unsigned long
-tdm_helper_get_time_in_micros(void)
-{
-       struct timespec tp;
-
-       if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
-               return (tp.tv_sec * 1000000) + (tp.tv_nsec / 1000L);
+               return (double)tp.tv_sec + ((double)tp.tv_nsec) / 1000000000.0;
 
        return 0;
 }
@@ -138,6 +128,11 @@ _tdm_helper_dump_png(const char *file, const void *data, int width,
        const int pixel_size = 4;       // RGBA
        png_bytep *row_pointers =
                png_malloc(pPngStruct, height * sizeof(png_byte *));
+       if (!row_pointers) {
+               png_destroy_write_struct(&pPngStruct, &pPngInfo);
+               fclose(fp);
+               return;
+       }
 
        unsigned int *blocks = (unsigned int *)data;
        int y = 0;
@@ -146,6 +141,15 @@ _tdm_helper_dump_png(const char *file, const void *data, int width,
        for (; y < height; ++y) {
                png_bytep row =
                        png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
+               if (!row) {
+                       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];
@@ -168,16 +172,54 @@ _tdm_helper_dump_png(const char *file, const void *data, int width,
        fclose(fp);
 }
 
+INTERN char *
+tdm_helper_dump_make_directory(const char *path, char *reply, int *len)
+{
+       char *fullpath = NULL;
+       time_t timer;
+       struct tm *t, *buf = NULL;
+
+       timer = time(NULL);
+
+       buf = calloc(1, sizeof(struct tm));
+       TDM_GOTO_IF_FAIL(buf != NULL, failed_make);
+
+       fullpath = calloc(1, TDM_PATH_LEN * sizeof(char));
+       TDM_GOTO_IF_FAIL(fullpath != NULL, failed_make);
+
+       t = localtime_r(&timer, buf);
+       TDM_GOTO_IF_FAIL(t != NULL, failed_make);
+
+       snprintf(fullpath, TDM_PATH_LEN, "%s/dump_%04d%02d%02d.%02d%02d%02d", path,
+                        t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
+
+       if ((mkdir(fullpath, 0755)) < 0) {
+               TDM_ERR("mkdir '%s' fail\n", fullpath);
+               TDM_SNPRINTF(reply, len, "mkdir '%s' fail\n", fullpath);
+               goto failed_make;
+       }
+
+       free(buf);
+
+       return fullpath;
+failed_make:
+       if (fullpath)
+               free(fullpath);
+       if (buf)
+               free(buf);
+       return NULL;
+}
+
 INTERN void
-tdm_helper_dump_buffer_str(tbm_surface_h buffer, const char *str)
+tdm_helper_dump_buffer_str(tbm_surface_h buffer, char *dir, char *str)
 {
        tbm_surface_info_s info;
-       const char *dir = "/tmp/dump-tdm";
        const char *ext;
        char file[TDM_PATH_LEN];
        int ret, bw;
 
        TDM_RETURN_IF_FAIL(buffer != NULL);
+       TDM_RETURN_IF_FAIL(dir != NULL);
        TDM_RETURN_IF_FAIL(str != NULL);
 
        ret = tbm_surface_get_info(buffer, &info);
@@ -265,8 +307,8 @@ tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
        TDM_INFO("dump %s", file);
 }
 
-EXTERN void
-tdm_helper_clear_buffer(tbm_surface_h buffer)
+void
+tdm_helper_clear_buffer_pos(tbm_surface_h buffer, tdm_pos *pos)
 {
        tbm_surface_info_s info;
        int ret;
@@ -279,7 +321,19 @@ tdm_helper_clear_buffer(tbm_surface_h buffer)
        switch (info.format) {
        case TBM_FORMAT_ARGB8888:
        case TBM_FORMAT_XRGB8888:
-               memset(info.planes[0].ptr, 0, info.planes[0].stride * info.height);
+               if (!pos) {
+                       memset(info.planes[0].ptr, 0, info.planes[0].stride * info.height);
+               } else {
+                       unsigned char *p;
+                       int x, y;
+                       for (y = pos->y; y <= (pos->y + pos->h); y++) {
+                               p = info.planes[0].ptr + info.planes[0].stride * y;
+                               for (x = pos->x; x <= (pos->x + pos->w); x++) {
+                                       int *ibuf = (int*)p;
+                                       ibuf[x] = 0x00000000;
+                               }
+                       }
+               }
                break;
        case TBM_FORMAT_YVU420:
        case TBM_FORMAT_YUV420:
@@ -316,6 +370,142 @@ tdm_helper_clear_buffer(tbm_surface_h buffer)
        tbm_surface_unmap(buffer);
 }
 
+EXTERN void
+tdm_helper_clear_buffer(tbm_surface_h buffer)
+{
+       TDM_RETURN_IF_FAIL(buffer != NULL);
+
+       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)
 {
@@ -390,21 +580,6 @@ tdm_helper_dump_stop(void)
        TDM_DBG("tdm_helper_dump stop.");
 }
 
-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;
-}
-
 static tdm_error
 _tdm_helper_buffer_convert(tbm_surface_h srcbuf, tbm_surface_h dstbuf,
                                                   int dx, int dy, int dw, int dh, int count)
@@ -585,9 +760,11 @@ tdm_helper_get_display_information(tdm_display *dpy, char *reply, int *len)
                                         tdm_status_str(private_output->caps.status),
                                         tdm_dpms_str(private_output->current_dpms_value),
                                         private_output->caps.subpixel,
-                                        private_output->caps.preferred_align,
-                                        private_output->caps.min_w, private_output->caps.min_h,
-                                        private_output->caps.max_w, private_output->caps.max_h,
+                                        TDM_FRONT_VALUE(private_output->caps.preferred_align),
+                                        TDM_FRONT_VALUE(private_output->caps.min_w),
+                                        TDM_FRONT_VALUE(private_output->caps.min_h),
+                                        TDM_FRONT_VALUE(private_output->caps.max_w),
+                                        TDM_FRONT_VALUE(private_output->caps.max_h),
                                         private_output->caps.mmWidth, private_output->caps.mmHeight);
 
                TDM_SNPRINTF(reply, len, "\t%d modes:\n", private_output->caps.mode_count);
@@ -599,13 +776,14 @@ tdm_helper_get_display_information(tdm_display *dpy, char *reply, int *len)
                        ret = func_output->output_get_mode(private_output->output_backend, &current_mode);
                        TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
 
-                       TDM_SNPRINTF(reply, len, "\t\t name refresh (Hz) hdisp hss hse htot vdisp vss vse vtot\n");
+                       TDM_SNPRINTF(reply, len, "\t\t name refresh (Hz) clk hdisp hss hse htot vdisp vss vse vtot vscan\n");
                        for (i = 0; i < private_output->caps.mode_count; i++) {
                                char *current = (current_mode == private_output->caps.modes + i) ? "*" : " ";
-                               TDM_SNPRINTF(reply, len, "\t\t%s%s %d %d %d %d %d %d %d %d %d ",
+                               TDM_SNPRINTF(reply, len, "\t\t%s%s %d %d %d %d %d %d %d %d %d %d %d ",
                                                         current,
                                                         private_output->caps.modes[i].name,
                                                         private_output->caps.modes[i].vrefresh,
+                                                        private_output->caps.modes[i].clock,
                                                         private_output->caps.modes[i].hdisplay,
                                                         private_output->caps.modes[i].hsync_start,
                                                         private_output->caps.modes[i].hsync_end,
@@ -613,7 +791,8 @@ tdm_helper_get_display_information(tdm_display *dpy, char *reply, int *len)
                                                         private_output->caps.modes[i].vdisplay,
                                                         private_output->caps.modes[i].vsync_start,
                                                         private_output->caps.modes[i].vsync_end,
-                                                        private_output->caps.modes[i].vtotal);
+                                                        private_output->caps.modes[i].vtotal,
+                                                        private_output->caps.modes[i].vscan);
                                tdm_mode_flag_str(private_output->caps.modes[i].flags, &reply, len);
                                TDM_SNPRINTF(reply, len, " ");
                                tdm_mode_type_str(private_output->caps.modes[i].type, &reply, len);
@@ -736,9 +915,11 @@ tdm_helper_get_display_information(tdm_display *dpy, char *reply, int *len)
                }
                TDM_SNPRINTF(reply, len, "\n");
                TDM_SNPRINTF(reply, len, "size\t: min(%dx%d) max(%dx%d) align_w(%d)\n",
-                                        private_display->caps_pp.min_w, private_display->caps_pp.min_h,
-                                        private_display->caps_pp.max_w, private_display->caps_pp.max_h,
-                                        private_display->caps_pp.preferred_align);
+                                        TDM_FRONT_VALUE(private_display->caps_pp.min_w),
+                                        TDM_FRONT_VALUE(private_display->caps_pp.min_h),
+                                        TDM_FRONT_VALUE(private_display->caps_pp.max_w),
+                                        TDM_FRONT_VALUE(private_display->caps_pp.max_h),
+                                        TDM_FRONT_VALUE(private_display->caps_pp.preferred_align));
                if (!LIST_IS_EMPTY(&private_display->pp_list)) {
                        TDM_SNPRINTF(reply, len, "-------------------------------------------------------------\n");
                        TDM_SNPRINTF(reply, len, "src(format size crop)  |  dst(format size crop)  |  transform\n");
@@ -778,9 +959,11 @@ tdm_helper_get_display_information(tdm_display *dpy, char *reply, int *len)
                }
                TDM_SNPRINTF(reply, len, "\n");
                TDM_SNPRINTF(reply, len, "size\t: min(%dx%d) max(%dx%d) align_w(%d)\n",
-                                        private_display->caps_capture.min_w, private_display->caps_capture.min_h,
-                                        private_display->caps_capture.max_w, private_display->caps_capture.max_h,
-                                        private_display->caps_capture.preferred_align);
+                                        TDM_FRONT_VALUE(private_display->caps_capture.min_w),
+                                        TDM_FRONT_VALUE(private_display->caps_capture.min_h),
+                                        TDM_FRONT_VALUE(private_display->caps_capture.max_w),
+                                        TDM_FRONT_VALUE(private_display->caps_capture.max_h),
+                                        TDM_FRONT_VALUE(private_display->caps_capture.preferred_align));
                if (!LIST_IS_EMPTY(&private_display->capture_list)) {
                        TDM_SNPRINTF(reply, len, "-----------------------------------\n");
                        TDM_SNPRINTF(reply, len, "dst(format size crop)  |  transform\n");