1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
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>
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:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
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.
34 **************************************************************************/
42 #include <tbm_surface.h>
43 #include <tbm_surface_internal.h>
48 #include "tdm_private.h"
52 static const char *dump_prefix[2] = {"png", "yuv"};
57 tdm_helper_get_time_in_millis(void)
61 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
62 return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
68 tdm_helper_get_time_in_micros(void)
72 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
73 return (tp.tv_sec * 1000000) + (tp.tv_nsec / 1000L);
79 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
80 int size2, void *data3, int size3)
83 FILE *fp = fopen(file, "w+");
84 TDM_RETURN_IF_FAIL(fp != NULL);
86 blocks = (unsigned int *)data1;
87 fwrite(blocks, 1, size1, fp);
90 blocks = (unsigned int *)data2;
91 fwrite(blocks, 1, size2, fp);
95 blocks = (unsigned int *)data3;
96 fwrite(blocks, 1, size3, fp);
103 _tdm_helper_dump_png(const char *file, const void *data, int width,
106 FILE *fp = fopen(file, "wb");
107 TDM_RETURN_IF_FAIL(fp != NULL);
109 png_structp pPngStruct =
110 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
116 png_infop pPngInfo = png_create_info_struct(pPngStruct);
118 png_destroy_write_struct(&pPngStruct, NULL);
123 png_init_io(pPngStruct, fp);
124 png_set_IHDR(pPngStruct,
131 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
133 png_set_bgr(pPngStruct);
134 png_write_info(pPngStruct, pPngInfo);
136 const int pixel_size = 4; // RGBA
137 png_bytep *row_pointers =
138 png_malloc(pPngStruct, height * sizeof(png_byte *));
140 unsigned int *blocks = (unsigned int *)data;
144 for (; y < height; ++y) {
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;
157 png_write_image(pPngStruct, row_pointers);
158 png_write_end(pPngStruct, pPngInfo);
160 for (y = 0; y < height; y++)
161 png_free(pPngStruct, row_pointers[y]);
162 png_free(pPngStruct, row_pointers);
164 png_destroy_write_struct(&pPngStruct, &pPngInfo);
170 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
172 tbm_surface_info_s info;
176 TDM_RETURN_IF_FAIL(buffer != NULL);
177 TDM_RETURN_IF_FAIL(file != NULL);
179 ret = tbm_surface_map(buffer, TBM_DEVICE_CPU, &info);
180 TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
182 len = strnlen(file, 1024);
183 if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
184 prefix = dump_prefix[0];
186 prefix = dump_prefix[1];
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);
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);
200 case TBM_FORMAT_YVU420:
201 case TBM_FORMAT_YUV420:
202 _tdm_helper_dump_raw(file,
204 info.planes[0].stride * info.height,
206 info.planes[1].stride * (info.height >> 1),
208 info.planes[2].stride * (info.height >> 1));
210 case TBM_FORMAT_NV12:
211 case TBM_FORMAT_NV21:
212 _tdm_helper_dump_raw(file,
214 info.planes[0].stride * info.height,
216 info.planes[1].stride * (info.height >> 1), NULL,
219 case TBM_FORMAT_YUYV:
220 case TBM_FORMAT_UYVY:
221 _tdm_helper_dump_raw(file,
223 info.planes[0].stride * info.height, NULL, 0,
227 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR(info.format));
228 tbm_surface_unmap(buffer);
232 tbm_surface_unmap(buffer);
234 TDM_INFO("dump %s", file);
238 tdm_helper_get_fd(const char *env)
241 int fd, newfd, flags, ret;
243 value = (const char*)getenv(env);
247 ret = sscanf(value, "%d", &fd);
249 TDM_ERR("sscanf failed: %m");
253 flags = fcntl(fd, F_GETFD);
255 TDM_ERR("fcntl failed: %m");
261 TDM_ERR("dup failed: %m");
265 TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd);
267 fcntl(newfd, F_SETFD, flags | FD_CLOEXEC);
273 tdm_helper_set_fd(const char *env, int fd)
278 snprintf(buf, sizeof(buf), "%d", fd);
280 ret = setenv(env, (const char*)buf, 1);
282 TDM_ERR("setenv failed: %m");
287 TDM_INFO("%s: fd(%d)", env, fd);
291 tdm_helper_dump_start(char *dumppath, int *count)
293 if (dumppath == NULL || count == NULL) {
294 TDM_DBG("tdm_helper_dump dumppath or count is null.");
300 TDM_DBG("tdm_helper_dump start.(path : %s)", dumppath);
304 tdm_helper_dump_stop(void)
308 TDM_DBG("tdm_helper_dump stop.");