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