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_get_time_in_micros(void)
39 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
40 return (tp.tv_sec * 1000000) + (tp.tv_nsec / 1000L);
46 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
47 int size2, void *data3, int size3)
50 FILE *fp = fopen(file, "w+");
51 TDM_RETURN_IF_FAIL(fp != NULL);
53 blocks = (unsigned int *)data1;
54 fwrite(blocks, 1, size1, fp);
57 blocks = (unsigned int *)data2;
58 fwrite(blocks, 1, size2, fp);
62 blocks = (unsigned int *)data3;
63 fwrite(blocks, 1, size3, fp);
70 _tdm_helper_dump_png(const char *file, const void *data, int width,
73 FILE *fp = fopen(file, "wb");
74 TDM_RETURN_IF_FAIL(fp != NULL);
76 png_structp pPngStruct =
77 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
83 png_infop pPngInfo = png_create_info_struct(pPngStruct);
85 png_destroy_write_struct(&pPngStruct, NULL);
90 png_init_io(pPngStruct, fp);
91 png_set_IHDR(pPngStruct,
98 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
100 png_set_bgr(pPngStruct);
101 png_write_info(pPngStruct, pPngInfo);
103 const int pixel_size = 4; // RGBA
104 png_bytep *row_pointers =
105 png_malloc(pPngStruct, height * sizeof(png_byte *));
107 unsigned int *blocks = (unsigned int *)data;
111 for (; y < height; ++y) {
113 png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
114 row_pointers[y] = (png_bytep)row;
115 for (x = 0; x < width; ++x) {
116 unsigned int curBlock = blocks[y * width + x];
117 row[x * pixel_size] = (curBlock & 0xFF);
118 row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
119 row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
120 row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
124 png_write_image(pPngStruct, row_pointers);
125 png_write_end(pPngStruct, pPngInfo);
127 for (y = 0; y < height; y++)
128 png_free(pPngStruct, row_pointers[y]);
129 png_free(pPngStruct, row_pointers);
131 png_destroy_write_struct(&pPngStruct, &pPngInfo);
137 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
139 tbm_surface_info_s info;
143 TDM_RETURN_IF_FAIL(buffer != NULL);
144 TDM_RETURN_IF_FAIL(file != NULL);
146 ret = tbm_surface_map(buffer, TBM_DEVICE_CPU, &info);
147 TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
149 len = strnlen(file, 1024);
150 if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
151 prefix = dump_prefix[0];
153 prefix = dump_prefix[1];
155 if (strncmp(file + (len - 3), prefix, 3)) {
156 TDM_ERR("can't dump to '%s' file", file + (len - 3));
157 tbm_surface_unmap(buffer);
161 switch (info.format) {
162 case TBM_FORMAT_ARGB8888:
163 case TBM_FORMAT_XRGB8888:
164 _tdm_helper_dump_png(file, info.planes[0].ptr,
165 info.planes[0].stride >> 2, info.height);
167 case TBM_FORMAT_YVU420:
168 case TBM_FORMAT_YUV420:
169 _tdm_helper_dump_raw(file,
171 info.planes[0].stride * info.height,
173 info.planes[1].stride * (info.height >> 1),
175 info.planes[2].stride * (info.height >> 1));
177 case TBM_FORMAT_NV12:
178 case TBM_FORMAT_NV21:
179 _tdm_helper_dump_raw(file,
181 info.planes[0].stride * info.height,
183 info.planes[1].stride * (info.height >> 1), NULL,
186 case TBM_FORMAT_YUYV:
187 case TBM_FORMAT_UYVY:
188 _tdm_helper_dump_raw(file,
190 info.planes[0].stride * info.height, NULL, 0,
194 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR (info.format));
195 tbm_surface_unmap(buffer);
199 tbm_surface_unmap(buffer);
201 TDM_INFO("dump %s", file);
205 tdm_helper_get_fd(const char *env)
208 int fd, newfd, flags, ret;
210 value = (const char*)getenv(env);
214 ret = sscanf(value, "%d", &fd);
216 TDM_ERR("sscanf failed: %m");
220 flags = fcntl(fd, F_GETFD);
222 TDM_ERR("fcntl failed: %m");
228 TDM_ERR("dup failed: %m");
232 TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd);
234 fcntl(newfd, F_SETFD, flags | FD_CLOEXEC);
240 tdm_helper_set_fd(const char *env, int fd)
245 snprintf(buf, sizeof(buf), "%d", fd);
247 ret = setenv(env, (const char*)buf, 1);
249 TDM_ERR("setenv failed: %m");
254 TDM_INFO("%s: fd(%d)", env, fd);
258 tdm_helper_dump_start(char *dumppath, int *count)
260 if (tdm_helper_dump_count != NULL) {
261 TDM_DBG("tdm_helper_dump is already started.");
265 if (dumppath == NULL || count == NULL) {
266 TDM_DBG("tdm_helper_dump dumppath or count is null.");
270 tdm_helper_dump_count = count;
271 tdm_helper_dump_path = dumppath;
275 TDM_DBG("tdm_helper_dump start.(path : %s)", tdm_helper_dump_path);
279 tdm_helper_dump_stop(void)
281 tdm_helper_dump_path = NULL;
282 tdm_helper_dump_count = NULL;
286 TDM_DBG("tdm_helper_dump stop.");
290 tdm_helper_get_dump_count(void)
292 if ((tdm_helper_dump_count != NULL) && (tdm_helper_dump_path != NULL)) {
293 if (*tdm_helper_dump_count == 1000)
294 *tdm_helper_dump_count = 1;
295 return (*tdm_helper_dump_count)++;
301 tdm_helper_get_dump_path(void)
303 return tdm_helper_dump_path;