support thread
[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 #include <time.h>
11
12 #include "tdm.h"
13 #include "tdm_private.h"
14
15 #define PNG_DEPTH 8
16
17 static const char *dump_prefix[2] = {"png", "yuv"};
18
19 INTERN unsigned long
20 tdm_helper_get_time_in_millis(void)
21 {
22         struct timespec tp;
23
24         if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
25                 return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
26
27         return 0;
28 }
29
30 static void
31 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
32                      int size2, void *data3, int size3)
33 {
34         unsigned int *blocks;
35         FILE *fp = fopen(file, "w+");
36         TDM_RETURN_IF_FAIL(fp != NULL);
37
38         blocks = (unsigned int *)data1;
39         fwrite(blocks, 1, size1, fp);
40
41         if (size2 > 0) {
42                 blocks = (unsigned int *)data2;
43                 fwrite(blocks, 1, size2, fp);
44         }
45
46         if (size3 > 0) {
47                 blocks = (unsigned int *)data3;
48                 fwrite(blocks, 1, size3, fp);
49         }
50
51         fclose(fp);
52 }
53
54 static void
55 _tdm_helper_dump_png(const char *file, const void *data, int width,
56                      int height)
57 {
58         FILE *fp = fopen(file, "wb");
59         TDM_RETURN_IF_FAIL(fp != NULL);
60
61         png_structp pPngStruct =
62                 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
63         if (!pPngStruct) {
64                 fclose(fp);
65                 return;
66         }
67
68         png_infop pPngInfo = png_create_info_struct(pPngStruct);
69         if (!pPngInfo) {
70                 png_destroy_write_struct(&pPngStruct, NULL);
71                 fclose(fp);
72                 return;
73         }
74
75         png_init_io(pPngStruct, fp);
76         png_set_IHDR(pPngStruct,
77                      pPngInfo,
78                      width,
79                      height,
80                      PNG_DEPTH,
81                      PNG_COLOR_TYPE_RGBA,
82                      PNG_INTERLACE_NONE,
83                      PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
84
85         png_set_bgr(pPngStruct);
86         png_write_info(pPngStruct, pPngInfo);
87
88         const int pixel_size = 4;       // RGBA
89         png_bytep *row_pointers =
90                 png_malloc(pPngStruct, height * sizeof(png_byte *));
91
92         unsigned int *blocks = (unsigned int *)data;
93         int y = 0;
94         int x = 0;
95
96         for (; y < height; ++y) {
97                 png_bytep row =
98                         png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
99                 row_pointers[y] = (png_bytep)row;
100                 for (x = 0; x < width; ++x) {
101                         unsigned int curBlock = blocks[y * width + x];
102                         row[x * pixel_size] = (curBlock & 0xFF);
103                         row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
104                         row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
105                         row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
106                 }
107         }
108
109         png_write_image(pPngStruct, row_pointers);
110         png_write_end(pPngStruct, pPngInfo);
111
112         for (y = 0; y < height; y++)
113                 png_free(pPngStruct, row_pointers[y]);
114         png_free(pPngStruct, row_pointers);
115
116         png_destroy_write_struct(&pPngStruct, &pPngInfo);
117
118         fclose(fp);
119 }
120
121 EXTERN void
122 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
123 {
124         tbm_surface_info_s info;
125         int len, ret;
126         const char *prefix;
127
128         TDM_RETURN_IF_FAIL(buffer != NULL);
129         TDM_RETURN_IF_FAIL(file != NULL);
130
131         ret = tbm_surface_map(buffer, TBM_DEVICE_CPU, &info);
132         TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
133
134         len = strnlen(file, 1024);
135         if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
136                 prefix = dump_prefix[0];
137         else
138                 prefix = dump_prefix[1];
139
140         if (strncmp(file + (len - 3), prefix, 3)) {
141                 TDM_ERR("can't dump to '%s' file", file + (len - 3));
142                 tbm_surface_unmap(buffer);
143                 return;
144         }
145
146         switch (info.format) {
147         case TBM_FORMAT_ARGB8888:
148         case TBM_FORMAT_XRGB8888:
149                 _tdm_helper_dump_png(file, info.planes[0].ptr,
150                                      info.planes[0].stride >> 2, info.height);
151                 break;
152         case TBM_FORMAT_YVU420:
153         case TBM_FORMAT_YUV420:
154                 _tdm_helper_dump_raw(file,
155                                      info.planes[0].ptr,
156                                      info.planes[0].stride * info.height,
157                                      info.planes[1].ptr,
158                                      info.planes[1].stride * (info.height >> 1),
159                                      info.planes[2].ptr,
160                                      info.planes[2].stride * (info.height >> 1));
161                 break;
162         case TBM_FORMAT_NV12:
163         case TBM_FORMAT_NV21:
164                 _tdm_helper_dump_raw(file,
165                                      info.planes[0].ptr,
166                                      info.planes[0].stride * info.height,
167                                      info.planes[1].ptr,
168                                      info.planes[1].stride * (info.height >> 1), NULL,
169                                      0);
170                 break;
171         case TBM_FORMAT_YUYV:
172         case TBM_FORMAT_UYVY:
173                 _tdm_helper_dump_raw(file,
174                                      info.planes[0].ptr,
175                                      info.planes[0].stride * info.height, NULL, 0,
176                                      NULL, 0);
177                 break;
178         default:
179                 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR (info.format));
180                 tbm_surface_unmap(buffer);
181                 return;
182         }
183
184         tbm_surface_unmap(buffer);
185
186         TDM_INFO("dump %s", file);
187 }
188
189 EXTERN int
190 tdm_helper_get_fd(const char *env)
191 {
192         const char *value;
193         int fd, newfd, flags, ret;
194
195         value = (const char*)getenv(env);
196         if (!value)
197                 return -1;
198
199         ret = sscanf(value, "%d", &fd);
200         if (ret < 0) {
201                 TDM_ERR("sscanf failed: %m");
202                 return -1;
203         }
204
205         flags = fcntl(fd, F_GETFD);
206         if (flags == -1) {
207                 TDM_ERR("fcntl failed: %m");
208                 return -1;
209         }
210
211         newfd = dup(fd);
212         if (newfd < 0) {
213                 TDM_ERR("dup failed: %m");
214                 return -1;
215         }
216
217         TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd);
218
219         fcntl(newfd, F_SETFD, flags | FD_CLOEXEC);
220
221         return newfd;
222 }
223
224 EXTERN void
225 tdm_helper_set_fd(const char *env, int fd)
226 {
227         char buf[32];
228         int ret;
229
230         snprintf(buf, sizeof(buf), "%d", fd);
231
232         ret = setenv(env, (const char*)buf, 1);
233         if (ret) {
234                 TDM_ERR("setenv failed: %m");
235                 return;
236         }
237
238         if (fd >= 0)
239                 TDM_INFO("%s: fd(%d)", env, fd);
240 }