7 #include <tbm_surface.h>
8 #include <tbm_surface_internal.h>
13 #include "tdm_private.h"
17 static const char *dump_prefix[2] = {"png", "yuv"};
19 static int *tdm_helper_dump_count;
20 static char *tdm_helper_dump_path;
23 tdm_helper_get_time_in_millis(void)
27 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
28 return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
34 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
35 int size2, void *data3, int size3)
38 FILE *fp = fopen(file, "w+");
39 TDM_RETURN_IF_FAIL(fp != NULL);
41 blocks = (unsigned int *)data1;
42 fwrite(blocks, 1, size1, fp);
45 blocks = (unsigned int *)data2;
46 fwrite(blocks, 1, size2, fp);
50 blocks = (unsigned int *)data3;
51 fwrite(blocks, 1, size3, fp);
58 _tdm_helper_dump_png(const char *file, const void *data, int width,
61 FILE *fp = fopen(file, "wb");
62 TDM_RETURN_IF_FAIL(fp != NULL);
64 png_structp pPngStruct =
65 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
71 png_infop pPngInfo = png_create_info_struct(pPngStruct);
73 png_destroy_write_struct(&pPngStruct, NULL);
78 png_init_io(pPngStruct, fp);
79 png_set_IHDR(pPngStruct,
86 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
88 png_set_bgr(pPngStruct);
89 png_write_info(pPngStruct, pPngInfo);
91 const int pixel_size = 4; // RGBA
92 png_bytep *row_pointers =
93 png_malloc(pPngStruct, height * sizeof(png_byte *));
95 unsigned int *blocks = (unsigned int *)data;
99 for (; y < height; ++y) {
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;
112 png_write_image(pPngStruct, row_pointers);
113 png_write_end(pPngStruct, pPngInfo);
115 for (y = 0; y < height; y++)
116 png_free(pPngStruct, row_pointers[y]);
117 png_free(pPngStruct, row_pointers);
119 png_destroy_write_struct(&pPngStruct, &pPngInfo);
125 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
127 tbm_surface_info_s info;
131 TDM_RETURN_IF_FAIL(buffer != NULL);
132 TDM_RETURN_IF_FAIL(file != NULL);
134 ret = tbm_surface_map(buffer, TBM_DEVICE_CPU, &info);
135 TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
137 len = strnlen(file, 1024);
138 if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
139 prefix = dump_prefix[0];
141 prefix = dump_prefix[1];
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);
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);
155 case TBM_FORMAT_YVU420:
156 case TBM_FORMAT_YUV420:
157 _tdm_helper_dump_raw(file,
159 info.planes[0].stride * info.height,
161 info.planes[1].stride * (info.height >> 1),
163 info.planes[2].stride * (info.height >> 1));
165 case TBM_FORMAT_NV12:
166 case TBM_FORMAT_NV21:
167 _tdm_helper_dump_raw(file,
169 info.planes[0].stride * info.height,
171 info.planes[1].stride * (info.height >> 1), NULL,
174 case TBM_FORMAT_YUYV:
175 case TBM_FORMAT_UYVY:
176 _tdm_helper_dump_raw(file,
178 info.planes[0].stride * info.height, NULL, 0,
182 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR (info.format));
183 tbm_surface_unmap(buffer);
187 tbm_surface_unmap(buffer);
189 TDM_INFO("dump %s", file);
193 tdm_helper_get_fd(const char *env)
196 int fd, newfd, flags, ret;
198 value = (const char*)getenv(env);
202 ret = sscanf(value, "%d", &fd);
204 TDM_ERR("sscanf failed: %m");
208 flags = fcntl(fd, F_GETFD);
210 TDM_ERR("fcntl failed: %m");
216 TDM_ERR("dup failed: %m");
220 TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd);
222 fcntl(newfd, F_SETFD, flags | FD_CLOEXEC);
228 tdm_helper_set_fd(const char *env, int fd)
233 snprintf(buf, sizeof(buf), "%d", fd);
235 ret = setenv(env, (const char*)buf, 1);
237 TDM_ERR("setenv failed: %m");
242 TDM_INFO("%s: fd(%d)", env, fd);
246 tdm_helper_dump_start(char *dumppath, int *count)
248 if (tdm_helper_dump_count != NULL) {
249 TDM_DBG("tdm_helper_dump is already started.");
253 if (dumppath == NULL) {
254 TDM_DBG("tdm_helper_dump dumppath is null.");
258 tdm_helper_dump_count = count;
259 tdm_helper_dump_path = dumppath;
261 TDM_DBG("tdm_helper_dump start.(path : %s)", tdm_helper_dump_path);
265 tdm_helper_dump_stop(void)
267 tdm_helper_dump_path = NULL;
268 tdm_helper_dump_count = NULL;
270 TDM_DBG("tdm_helper_dump stop.");
274 tdm_helper_get_dump_count(void)
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)++;
285 tdm_helper_get_dump_path(void)
287 return tdm_helper_dump_path;