add dump function for debugging
[platform/core/uifw/libtdm.git] / src / tdm_helper.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <png.h>
6 #include <string.h>
7 #include <tbm_surface.h>
8 #include <tbm_surface_internal.h>
9 #include <string.h>
10
11 #include "tdm.h"
12 #include "tdm_private.h"
13
14 #define PNG_DEPTH 8
15
16 int tdm_helper_drm_fd = -1;
17
18 static const char *dump_prefix[2] = {"png", "yuv"};
19
20 static void
21 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
22                      int size2, void *data3, int size3)
23 {
24     unsigned int *blocks;
25     FILE *fp = fopen(file, "w+");
26     TDM_RETURN_IF_FAIL(fp != NULL);
27
28     blocks = (unsigned int*)data1;
29     fwrite(blocks, 1, size1, fp);
30
31     if (size2 > 0)
32     {
33         blocks = (unsigned int*)data2;
34         fwrite(blocks, 1, size2, fp);
35     }
36
37     if (size3 > 0)
38     {
39         blocks = (unsigned int*)data3;
40         fwrite(blocks, 1, size3, fp);
41     }
42
43     fclose(fp);
44 }
45
46 static void
47 _tdm_helper_dump_png(const char *file, const void *data, int width,
48                      int height)
49 {
50     FILE *fp = fopen(file, "wb");
51     TDM_RETURN_IF_FAIL(fp != NULL);
52
53     png_structp pPngStruct =
54         png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
55     if (!pPngStruct)
56     {
57         fclose(fp);
58         return;
59     }
60
61     png_infop pPngInfo = png_create_info_struct(pPngStruct);
62     if (!pPngInfo)
63     {
64         png_destroy_write_struct(&pPngStruct, NULL);
65         fclose(fp);
66         return;
67     }
68
69     png_init_io(pPngStruct, fp);
70     png_set_IHDR(pPngStruct,
71                  pPngInfo,
72                  width,
73                  height,
74                  PNG_DEPTH,
75                  PNG_COLOR_TYPE_RGBA,
76                  PNG_INTERLACE_NONE,
77                  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
78
79     png_set_bgr(pPngStruct);
80     png_write_info(pPngStruct, pPngInfo);
81
82     const int pixel_size = 4;   // RGBA
83     png_bytep *row_pointers =
84         png_malloc(pPngStruct, height * sizeof(png_byte*));
85
86     unsigned int *blocks = (unsigned int*)data;
87     int y = 0;
88     int x = 0;
89
90     for (; y < height; ++y)
91     {
92         png_bytep row =
93             png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
94         row_pointers[y] = (png_bytep)row;
95         for (x = 0; x < width; ++x)
96         {
97             unsigned int curBlock = blocks[y * width + x];
98             row[x * pixel_size] = (curBlock & 0xFF);
99             row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
100             row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
101             row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
102         }
103     }
104
105     png_write_image(pPngStruct, row_pointers);
106     png_write_end(pPngStruct, pPngInfo);
107
108     for (y = 0; y < height; y++)
109         png_free(pPngStruct, row_pointers[y]);
110     png_free(pPngStruct, row_pointers);
111
112     png_destroy_write_struct(&pPngStruct, &pPngInfo);
113
114     fclose(fp);
115 }
116
117 EXTERN void
118 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
119 {
120     tbm_surface_info_s info;
121     int len;
122     const char *prefix;
123
124     TDM_RETURN_IF_FAIL(buffer != NULL);
125     TDM_RETURN_IF_FAIL(file != NULL);
126
127     tbm_surface_get_info(buffer, &info);
128
129     len = strnlen(file, 1024);
130     if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
131         prefix = dump_prefix[0];
132     else
133         prefix = dump_prefix[1];
134
135     if (strncmp(file + (len - 3), prefix, 3))
136     {
137         TDM_ERR("can't dump to '%s' file", file + (len - 3));
138         return;
139     }
140
141     switch(info.format)
142     {
143     case TBM_FORMAT_ARGB8888:
144     case TBM_FORMAT_XRGB8888:
145         _tdm_helper_dump_png(file, info.planes[0].ptr,
146                              info.planes[0].stride >> 2, info.height);
147         break;
148     case TBM_FORMAT_YVU420:
149     case TBM_FORMAT_YUV420:
150         _tdm_helper_dump_raw(file,
151                              info.planes[0].ptr + info.planes[0].offset,
152                              info.planes[0].stride * info.height,
153                              info.planes[1].ptr + info.planes[1].offset,
154                              info.planes[1].stride * (info.height >> 1),
155                              info.planes[2].ptr + info.planes[2].offset,
156                              info.planes[2].stride * (info.height >> 1));
157         break;
158     case TBM_FORMAT_NV12:
159     case TBM_FORMAT_NV21:
160         _tdm_helper_dump_raw(file,
161                              info.planes[0].ptr + info.planes[0].offset,
162                              info.planes[0].stride * info.height,
163                              info.planes[1].ptr + info.planes[1].offset,
164                              info.planes[1].stride * (info.height >> 1), NULL,
165                              0);
166         break;
167     case TBM_FORMAT_YUYV:
168     case TBM_FORMAT_UYVY:
169         _tdm_helper_dump_raw(file,
170                              info.planes[0].ptr + info.planes[0].offset,
171                              info.planes[0].stride * info.height, NULL, 0,
172                              NULL, 0);
173         break;
174     default:
175         TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR (info.format));
176         return;
177     }
178
179     TDM_INFO("dump %s", file);
180 }