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