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"};
20 static int *tdm_helper_dump_count;
21 static char *tdm_helper_dump_path;
24 tdm_helper_get_time_in_millis(void)
28 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
29 return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
35 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
36 int size2, void *data3, int size3)
39 FILE *fp = fopen(file, "w+");
40 TDM_RETURN_IF_FAIL(fp != NULL);
42 blocks = (unsigned int *)data1;
43 fwrite(blocks, 1, size1, fp);
46 blocks = (unsigned int *)data2;
47 fwrite(blocks, 1, size2, fp);
51 blocks = (unsigned int *)data3;
52 fwrite(blocks, 1, size3, fp);
59 _tdm_helper_dump_png(const char *file, const void *data, int width,
62 FILE *fp = fopen(file, "wb");
63 TDM_RETURN_IF_FAIL(fp != NULL);
65 png_structp pPngStruct =
66 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
72 png_infop pPngInfo = png_create_info_struct(pPngStruct);
74 png_destroy_write_struct(&pPngStruct, NULL);
79 png_init_io(pPngStruct, fp);
80 png_set_IHDR(pPngStruct,
87 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
89 png_set_bgr(pPngStruct);
90 png_write_info(pPngStruct, pPngInfo);
92 const int pixel_size = 4; // RGBA
93 png_bytep *row_pointers =
94 png_malloc(pPngStruct, height * sizeof(png_byte *));
96 unsigned int *blocks = (unsigned int *)data;
100 for (; y < height; ++y) {
102 png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
103 row_pointers[y] = (png_bytep)row;
104 for (x = 0; x < width; ++x) {
105 unsigned int curBlock = blocks[y * width + x];
106 row[x * pixel_size] = (curBlock & 0xFF);
107 row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
108 row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
109 row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
113 png_write_image(pPngStruct, row_pointers);
114 png_write_end(pPngStruct, pPngInfo);
116 for (y = 0; y < height; y++)
117 png_free(pPngStruct, row_pointers[y]);
118 png_free(pPngStruct, row_pointers);
120 png_destroy_write_struct(&pPngStruct, &pPngInfo);
126 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
128 tbm_surface_info_s info;
132 TDM_RETURN_IF_FAIL(buffer != NULL);
133 TDM_RETURN_IF_FAIL(file != NULL);
135 ret = tbm_surface_map(buffer, TBM_DEVICE_CPU, &info);
136 TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
138 len = strnlen(file, 1024);
139 if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
140 prefix = dump_prefix[0];
142 prefix = dump_prefix[1];
144 if (strncmp(file + (len - 3), prefix, 3)) {
145 TDM_ERR("can't dump to '%s' file", file + (len - 3));
146 tbm_surface_unmap(buffer);
150 switch (info.format) {
151 case TBM_FORMAT_ARGB8888:
152 case TBM_FORMAT_XRGB8888:
153 _tdm_helper_dump_png(file, info.planes[0].ptr,
154 info.planes[0].stride >> 2, info.height);
156 case TBM_FORMAT_YVU420:
157 case TBM_FORMAT_YUV420:
158 _tdm_helper_dump_raw(file,
160 info.planes[0].stride * info.height,
162 info.planes[1].stride * (info.height >> 1),
164 info.planes[2].stride * (info.height >> 1));
166 case TBM_FORMAT_NV12:
167 case TBM_FORMAT_NV21:
168 _tdm_helper_dump_raw(file,
170 info.planes[0].stride * info.height,
172 info.planes[1].stride * (info.height >> 1), NULL,
175 case TBM_FORMAT_YUYV:
176 case TBM_FORMAT_UYVY:
177 _tdm_helper_dump_raw(file,
179 info.planes[0].stride * info.height, NULL, 0,
183 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR (info.format));
184 tbm_surface_unmap(buffer);
188 tbm_surface_unmap(buffer);
190 TDM_INFO("dump %s", file);
194 tdm_helper_get_fd(const char *env)
197 int fd, newfd, flags, ret;
199 value = (const char*)getenv(env);
203 ret = sscanf(value, "%d", &fd);
205 TDM_ERR("sscanf failed: %m");
209 flags = fcntl(fd, F_GETFD);
211 TDM_ERR("fcntl failed: %m");
217 TDM_ERR("dup failed: %m");
221 TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd);
223 fcntl(newfd, F_SETFD, flags | FD_CLOEXEC);
229 tdm_helper_set_fd(const char *env, int fd)
234 snprintf(buf, sizeof(buf), "%d", fd);
236 ret = setenv(env, (const char*)buf, 1);
238 TDM_ERR("setenv failed: %m");
243 TDM_INFO("%s: fd(%d)", env, fd);
247 tdm_helper_dump_start(char *dumppath, int *count)
249 if (tdm_helper_dump_count != NULL) {
250 TDM_DBG("tdm_helper_dump is already started.");
254 if (dumppath == NULL || count == NULL) {
255 TDM_DBG("tdm_helper_dump dumppath or count is null.");
259 tdm_helper_dump_count = count;
260 tdm_helper_dump_path = dumppath;
264 TDM_DBG("tdm_helper_dump start.(path : %s)", tdm_helper_dump_path);
268 tdm_helper_dump_stop(void)
270 tdm_helper_dump_path = NULL;
271 tdm_helper_dump_count = NULL;
275 TDM_DBG("tdm_helper_dump stop.");
279 tdm_helper_get_dump_count(void)
281 if ((tdm_helper_dump_count != NULL) && (tdm_helper_dump_path != NULL)) {
282 if (*tdm_helper_dump_count == 1000)
283 *tdm_helper_dump_count = 1;
284 return (*tdm_helper_dump_count)++;
290 tdm_helper_get_dump_path(void)
292 return tdm_helper_dump_path;