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>
49 #include "tdm_private.h"
50 #include "tdm_helper.h"
54 static const char *dump_prefix[2] = {"png", "yuv"};
59 tdm_helper_get_time_in_millis(void)
63 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
64 return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
70 tdm_helper_get_time_in_micros(void)
74 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
75 return (tp.tv_sec * 1000000) + (tp.tv_nsec / 1000L);
81 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
82 int size2, void *data3, int size3)
85 FILE *fp = fopen(file, "w+");
86 TDM_RETURN_IF_FAIL(fp != NULL);
88 blocks = (unsigned int *)data1;
89 fwrite(blocks, 1, size1, fp);
92 blocks = (unsigned int *)data2;
93 fwrite(blocks, 1, size2, fp);
97 blocks = (unsigned int *)data3;
98 fwrite(blocks, 1, size3, fp);
105 _tdm_helper_dump_png(const char *file, const void *data, int width,
108 FILE *fp = fopen(file, "wb");
109 TDM_RETURN_IF_FAIL(fp != NULL);
111 png_structp pPngStruct =
112 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
118 png_infop pPngInfo = png_create_info_struct(pPngStruct);
120 png_destroy_write_struct(&pPngStruct, NULL);
125 png_init_io(pPngStruct, fp);
126 png_set_IHDR(pPngStruct,
133 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
135 png_set_bgr(pPngStruct);
136 png_write_info(pPngStruct, pPngInfo);
138 const int pixel_size = 4; // RGBA
139 png_bytep *row_pointers =
140 png_malloc(pPngStruct, height * sizeof(png_byte *));
142 unsigned int *blocks = (unsigned int *)data;
146 for (; y < height; ++y) {
148 png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
149 row_pointers[y] = (png_bytep)row;
150 for (x = 0; x < width; ++x) {
151 unsigned int curBlock = blocks[y * width + x];
152 row[x * pixel_size] = (curBlock & 0xFF);
153 row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
154 row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
155 row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
159 png_write_image(pPngStruct, row_pointers);
160 png_write_end(pPngStruct, pPngInfo);
162 for (y = 0; y < height; y++)
163 png_free(pPngStruct, row_pointers[y]);
164 png_free(pPngStruct, row_pointers);
166 png_destroy_write_struct(&pPngStruct, &pPngInfo);
172 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
174 tbm_surface_info_s info;
178 TDM_RETURN_IF_FAIL(buffer != NULL);
179 TDM_RETURN_IF_FAIL(file != NULL);
181 ret = tbm_surface_map(buffer, TBM_DEVICE_CPU, &info);
182 TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
184 len = strnlen(file, 1024);
185 if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
186 prefix = dump_prefix[0];
188 prefix = dump_prefix[1];
190 if (strncmp(file + (len - 3), prefix, 3)) {
191 TDM_ERR("can't dump to '%s' file", file + (len - 3));
192 tbm_surface_unmap(buffer);
196 switch (info.format) {
197 case TBM_FORMAT_ARGB8888:
198 case TBM_FORMAT_XRGB8888:
199 _tdm_helper_dump_png(file, info.planes[0].ptr,
200 info.planes[0].stride >> 2, info.height);
202 case TBM_FORMAT_YVU420:
203 case TBM_FORMAT_YUV420:
204 _tdm_helper_dump_raw(file,
206 info.planes[0].stride * info.height,
208 info.planes[1].stride * (info.height >> 1),
210 info.planes[2].stride * (info.height >> 1));
212 case TBM_FORMAT_NV12:
213 case TBM_FORMAT_NV21:
214 _tdm_helper_dump_raw(file,
216 info.planes[0].stride * info.height,
218 info.planes[1].stride * (info.height >> 1), NULL,
221 case TBM_FORMAT_YUYV:
222 case TBM_FORMAT_UYVY:
223 _tdm_helper_dump_raw(file,
225 info.planes[0].stride * info.height, NULL, 0,
229 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR(info.format));
230 tbm_surface_unmap(buffer);
234 tbm_surface_unmap(buffer);
236 TDM_INFO("dump %s", file);
240 tdm_helper_get_fd(const char *env)
243 int fd, newfd, flags, ret;
245 value = (const char*)getenv(env);
249 ret = sscanf(value, "%d", &fd);
251 TDM_ERR("sscanf failed: %m");
255 flags = fcntl(fd, F_GETFD);
257 TDM_ERR("fcntl failed: %m");
263 TDM_ERR("dup failed: %m");
267 TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd);
269 fcntl(newfd, F_SETFD, flags | FD_CLOEXEC);
275 tdm_helper_set_fd(const char *env, int fd)
280 snprintf(buf, sizeof(buf), "%d", fd);
282 ret = setenv(env, (const char*)buf, 1);
284 TDM_ERR("setenv failed: %m");
289 TDM_INFO("%s: fd(%d)", env, fd);
293 tdm_helper_dump_start(char *dumppath, int *count)
295 if (dumppath == NULL || count == NULL) {
296 TDM_DBG("tdm_helper_dump dumppath or count is null.");
302 TDM_DBG("tdm_helper_dump start.(path : %s)", dumppath);
306 tdm_helper_dump_stop(void)
310 TDM_DBG("tdm_helper_dump stop.");
313 static pixman_format_code_t
314 _tdm_helper_pixman_format_get(tbm_format format)
317 case TBM_FORMAT_ARGB8888:
318 return PIXMAN_a8r8g8b8;
319 case TBM_FORMAT_XRGB8888:
320 return PIXMAN_x8r8g8b8;
329 _tdm_helper_buffer_convert(tbm_surface_h srcbuf, tbm_surface_h dstbuf,
330 int dx, int dy, int dw, int dh, int count)
332 pixman_image_t *src_img = NULL, *dst_img = NULL;
333 pixman_format_code_t src_format, dst_format;
334 pixman_transform_t t;
335 struct pixman_f_transform ft;
337 tbm_surface_info_s src_info = {0, };
338 tbm_surface_info_s dst_info = {0, };
340 double scale_x, scale_y;
342 TDM_RETURN_VAL_IF_FAIL(srcbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
343 TDM_RETURN_VAL_IF_FAIL(dstbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
345 if (tbm_surface_map(srcbuf, TBM_SURF_OPTION_READ, &src_info)
346 != TBM_SURFACE_ERROR_NONE) {
347 TDM_ERR("cannot mmap srcbuf\n");
348 return TDM_ERROR_OPERATION_FAILED;
351 if (tbm_surface_map(dstbuf, TBM_SURF_OPTION_WRITE, &dst_info)
352 != TBM_SURFACE_ERROR_NONE) {
353 TDM_ERR("cannot mmap dstbuf\n");
354 tbm_surface_unmap(srcbuf);
355 return TDM_ERROR_OPERATION_FAILED;
357 TDM_GOTO_IF_FAIL(src_info.num_planes == 1, cant_convert);
358 TDM_GOTO_IF_FAIL(dst_info.num_planes == 1, cant_convert);
361 src_format = _tdm_helper_pixman_format_get(src_info.format);
362 TDM_GOTO_IF_FAIL(src_format > 0, cant_convert);
364 width = src_info.planes[0].stride / 4;
365 stride = src_info.planes[0].stride;
366 src_img = pixman_image_create_bits(src_format, width, src_info.height,
367 (uint32_t*)src_info.planes[0].ptr, stride);
368 TDM_GOTO_IF_FAIL(src_img != NULL, cant_convert);
371 dst_format = _tdm_helper_pixman_format_get(dst_info.format);
372 TDM_GOTO_IF_FAIL(dst_format > 0, cant_convert);
374 width = dst_info.planes[0].stride / 4;
375 stride = dst_info.planes[0].stride;
376 dst_img = pixman_image_create_bits(dst_format, width, dst_info.height,
377 (uint32_t*)dst_info.planes[0].ptr, stride);
378 TDM_GOTO_IF_FAIL(dst_img != NULL, cant_convert);
380 pixman_f_transform_init_identity(&ft);
382 scale_x = (double)src_info.width / dw;
383 scale_y = (double)src_info.height / dh;
385 pixman_f_transform_scale(&ft, NULL, scale_x, scale_y);
386 pixman_f_transform_translate(&ft, NULL, 0, 0);
387 pixman_transform_from_pixman_f_transform(&t, &ft);
388 pixman_image_set_transform(src_img, &t);
395 pixman_image_composite(op, src_img, NULL, dst_img,
396 0, 0, 0, 0, dx, dy, dw, dh);
399 pixman_image_unref(src_img);
401 pixman_image_unref(dst_img);
403 tbm_surface_unmap(srcbuf);
404 tbm_surface_unmap(dstbuf);
406 return TDM_ERROR_NONE;
410 pixman_image_unref(src_img);
412 pixman_image_unref(dst_img);
414 tbm_surface_unmap(srcbuf);
415 tbm_surface_unmap(dstbuf);
417 return TDM_ERROR_OPERATION_FAILED;
421 tdm_helper_capture_output(tdm_output *output, tbm_surface_h dst_buffer,
422 int x, int y, int w, int h,
423 tdm_helper_capture_handler func, void *data)
425 tbm_surface_h surface;
427 int i, count, first = 0;
429 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
430 TDM_RETURN_VAL_IF_FAIL(dst_buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
431 TDM_RETURN_VAL_IF_FAIL(x >= 0, TDM_ERROR_INVALID_PARAMETER);
432 TDM_RETURN_VAL_IF_FAIL(y >= 0, TDM_ERROR_INVALID_PARAMETER);
433 TDM_RETURN_VAL_IF_FAIL(w >= 0, TDM_ERROR_INVALID_PARAMETER);
434 TDM_RETURN_VAL_IF_FAIL(h >= 0, TDM_ERROR_INVALID_PARAMETER);
435 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
436 TDM_RETURN_VAL_IF_FAIL(data != NULL, TDM_ERROR_INVALID_PARAMETER);
438 err = tdm_output_get_layer_count(output, &count);
439 if (err != TDM_ERROR_NONE) {
440 TDM_ERR("tdm_output_get_layer_count fail(%d)\n", err);
441 return TDM_ERROR_OPERATION_FAILED;
444 TDM_ERR("tdm_output_get_layer_count err(%d, %d)\n", err, count);
445 return TDM_ERROR_BAD_MODULE;
448 for (i = count - 1; i >= 0; i--) {
449 tdm_layer *layer = tdm_output_get_layer(output, i, NULL);
451 surface = tdm_layer_get_displaying_buffer(layer, &err);
452 if (err != TDM_ERROR_NONE)
455 err = _tdm_helper_buffer_convert(surface, dst_buffer, x, y, w, h, first++);
456 if (err != TDM_ERROR_NONE)
457 TDM_DBG("convert fail %d-layer buffer\n", i);
459 TDM_DBG("convert success %d-layer buffer\n", i);
462 func(dst_buffer, data);
464 return TDM_ERROR_NONE;