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