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