apply tizen coding rule
[platform/core/uifw/libtdm.git] / src / tdm_helper.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <sc1.lim@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <png.h>
41 #include <string.h>
42 #include <tbm_surface.h>
43 #include <tbm_surface_internal.h>
44 #include <string.h>
45 #include <time.h>
46
47 #include "tdm.h"
48 #include "tdm_private.h"
49
50 #define PNG_DEPTH 8
51
52 static const char *dump_prefix[2] = {"png", "yuv"};
53
54 int tdm_dump_enable;
55
56 INTERN unsigned long
57 tdm_helper_get_time_in_millis(void)
58 {
59         struct timespec tp;
60
61         if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
62                 return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
63
64         return 0;
65 }
66
67 INTERN unsigned long
68 tdm_helper_get_time_in_micros(void)
69 {
70         struct timespec tp;
71
72         if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
73                 return (tp.tv_sec * 1000000) + (tp.tv_nsec / 1000L);
74
75         return 0;
76 }
77
78 static void
79 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
80                                          int size2, void *data3, int size3)
81 {
82         unsigned int *blocks;
83         FILE *fp = fopen(file, "w+");
84         TDM_RETURN_IF_FAIL(fp != NULL);
85
86         blocks = (unsigned int *)data1;
87         fwrite(blocks, 1, size1, fp);
88
89         if (size2 > 0) {
90                 blocks = (unsigned int *)data2;
91                 fwrite(blocks, 1, size2, fp);
92         }
93
94         if (size3 > 0) {
95                 blocks = (unsigned int *)data3;
96                 fwrite(blocks, 1, size3, fp);
97         }
98
99         fclose(fp);
100 }
101
102 static void
103 _tdm_helper_dump_png(const char *file, const void *data, int width,
104                                          int height)
105 {
106         FILE *fp = fopen(file, "wb");
107         TDM_RETURN_IF_FAIL(fp != NULL);
108
109         png_structp pPngStruct =
110                 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
111         if (!pPngStruct) {
112                 fclose(fp);
113                 return;
114         }
115
116         png_infop pPngInfo = png_create_info_struct(pPngStruct);
117         if (!pPngInfo) {
118                 png_destroy_write_struct(&pPngStruct, NULL);
119                 fclose(fp);
120                 return;
121         }
122
123         png_init_io(pPngStruct, fp);
124         png_set_IHDR(pPngStruct,
125                                  pPngInfo,
126                                  width,
127                                  height,
128                                  PNG_DEPTH,
129                                  PNG_COLOR_TYPE_RGBA,
130                                  PNG_INTERLACE_NONE,
131                                  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
132
133         png_set_bgr(pPngStruct);
134         png_write_info(pPngStruct, pPngInfo);
135
136         const int pixel_size = 4;       // RGBA
137         png_bytep *row_pointers =
138                 png_malloc(pPngStruct, height * sizeof(png_byte *));
139
140         unsigned int *blocks = (unsigned int *)data;
141         int y = 0;
142         int x = 0;
143
144         for (; y < height; ++y) {
145                 png_bytep row =
146                         png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
147                 row_pointers[y] = (png_bytep)row;
148                 for (x = 0; x < width; ++x) {
149                         unsigned int curBlock = blocks[y * width + x];
150                         row[x * pixel_size] = (curBlock & 0xFF);
151                         row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
152                         row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
153                         row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
154                 }
155         }
156
157         png_write_image(pPngStruct, row_pointers);
158         png_write_end(pPngStruct, pPngInfo);
159
160         for (y = 0; y < height; y++)
161                 png_free(pPngStruct, row_pointers[y]);
162         png_free(pPngStruct, row_pointers);
163
164         png_destroy_write_struct(&pPngStruct, &pPngInfo);
165
166         fclose(fp);
167 }
168
169 EXTERN void
170 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
171 {
172         tbm_surface_info_s info;
173         int len, ret;
174         const char *prefix;
175
176         TDM_RETURN_IF_FAIL(buffer != NULL);
177         TDM_RETURN_IF_FAIL(file != NULL);
178
179         ret = tbm_surface_map(buffer, TBM_DEVICE_CPU, &info);
180         TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
181
182         len = strnlen(file, 1024);
183         if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
184                 prefix = dump_prefix[0];
185         else
186                 prefix = dump_prefix[1];
187
188         if (strncmp(file + (len - 3), prefix, 3)) {
189                 TDM_ERR("can't dump to '%s' file", file + (len - 3));
190                 tbm_surface_unmap(buffer);
191                 return;
192         }
193
194         switch (info.format) {
195         case TBM_FORMAT_ARGB8888:
196         case TBM_FORMAT_XRGB8888:
197                 _tdm_helper_dump_png(file, info.planes[0].ptr,
198                                                          info.planes[0].stride >> 2, info.height);
199                 break;
200         case TBM_FORMAT_YVU420:
201         case TBM_FORMAT_YUV420:
202                 _tdm_helper_dump_raw(file,
203                                                          info.planes[0].ptr,
204                                                          info.planes[0].stride * info.height,
205                                                          info.planes[1].ptr,
206                                                          info.planes[1].stride * (info.height >> 1),
207                                                          info.planes[2].ptr,
208                                                          info.planes[2].stride * (info.height >> 1));
209                 break;
210         case TBM_FORMAT_NV12:
211         case TBM_FORMAT_NV21:
212                 _tdm_helper_dump_raw(file,
213                                                          info.planes[0].ptr,
214                                                          info.planes[0].stride * info.height,
215                                                          info.planes[1].ptr,
216                                                          info.planes[1].stride * (info.height >> 1), NULL,
217                                                          0);
218                 break;
219         case TBM_FORMAT_YUYV:
220         case TBM_FORMAT_UYVY:
221                 _tdm_helper_dump_raw(file,
222                                                          info.planes[0].ptr,
223                                                          info.planes[0].stride * info.height, NULL, 0,
224                                                          NULL, 0);
225                 break;
226         default:
227                 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR(info.format));
228                 tbm_surface_unmap(buffer);
229                 return;
230         }
231
232         tbm_surface_unmap(buffer);
233
234         TDM_INFO("dump %s", file);
235 }
236
237 EXTERN int
238 tdm_helper_get_fd(const char *env)
239 {
240         const char *value;
241         int fd, newfd, flags, ret;
242
243         value = (const char*)getenv(env);
244         if (!value)
245                 return -1;
246
247         ret = sscanf(value, "%d", &fd);
248         if (ret < 0) {
249                 TDM_ERR("sscanf failed: %m");
250                 return -1;
251         }
252
253         flags = fcntl(fd, F_GETFD);
254         if (flags == -1) {
255                 TDM_ERR("fcntl failed: %m");
256                 return -1;
257         }
258
259         newfd = dup(fd);
260         if (newfd < 0) {
261                 TDM_ERR("dup failed: %m");
262                 return -1;
263         }
264
265         TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd);
266
267         fcntl(newfd, F_SETFD, flags | FD_CLOEXEC);
268
269         return newfd;
270 }
271
272 EXTERN void
273 tdm_helper_set_fd(const char *env, int fd)
274 {
275         char buf[32];
276         int ret;
277
278         snprintf(buf, sizeof(buf), "%d", fd);
279
280         ret = setenv(env, (const char*)buf, 1);
281         if (ret) {
282                 TDM_ERR("setenv failed: %m");
283                 return;
284         }
285
286         if (fd >= 0)
287                 TDM_INFO("%s: fd(%d)", env, fd);
288 }
289
290 EXTERN void
291 tdm_helper_dump_start(char *dumppath, int *count)
292 {
293         if (dumppath == NULL || count == NULL) {
294                 TDM_DBG("tdm_helper_dump dumppath or count is null.");
295                 return;
296         }
297
298         tdm_dump_enable = 1;
299
300         TDM_DBG("tdm_helper_dump start.(path : %s)", dumppath);
301 }
302
303 EXTERN void
304 tdm_helper_dump_stop(void)
305 {
306         tdm_dump_enable = 0;
307
308         TDM_DBG("tdm_helper_dump stop.");
309 }
310