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