tdm_helper: clean up _tdm_helper_dump_png()
[platform/core/uifw/libtdm.git] / src / tdm_helper.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
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>
13  *
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:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
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.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <png.h>
41 #include <string.h>
42 #include <tbm_surface.h>
43 #include <tbm_surface_internal.h>
44 #include <string.h>
45 #include <time.h>
46 #include <pixman.h>
47
48 #include "tdm.h"
49 #include "tdm_private.h"
50 #include "tdm_helper.h"
51
52 #define PNG_DEPTH 8
53
54 static const char *file_exts[2] = {"png", "yuv"};
55
56 int tdm_dump_enable;
57 char *tdm_debug_dump_dir;
58
59 EXTERN double
60 tdm_helper_get_time(void)
61 {
62         struct timespec tp;
63
64         if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
65                 return (double)tp.tv_sec + ((double)tp.tv_nsec) / 1000000000.0;
66
67         return 0;
68 }
69
70 static void
71 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
72                                          int size2, void *data3, int size3)
73 {
74         unsigned int *blocks;
75         FILE *fp = fopen(file, "w+");
76         TDM_RETURN_IF_FAIL(fp != NULL);
77
78         blocks = (unsigned int *)data1;
79         fwrite(blocks, 1, size1, fp);
80
81         if (size2 > 0) {
82                 blocks = (unsigned int *)data2;
83                 fwrite(blocks, 1, size2, fp);
84         }
85
86         if (size3 > 0) {
87                 blocks = (unsigned int *)data3;
88                 fwrite(blocks, 1, size3, fp);
89         }
90
91         fclose(fp);
92 }
93
94 static void
95 _tdm_helper_dump_png(const char *file, const void *data, int width,
96                                          int height)
97 {
98         FILE *fp = fopen(file, "wb");
99         TDM_RETURN_IF_FAIL(fp != NULL);
100
101         png_structp pPngStruct =
102                 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
103         if (!pPngStruct) {
104                 fclose(fp);
105                 return;
106         }
107
108         png_infop pPngInfo = png_create_info_struct(pPngStruct);
109         if (!pPngInfo) {
110                 png_destroy_write_struct(&pPngStruct, NULL);
111                 fclose(fp);
112                 return;
113         }
114
115         png_init_io(pPngStruct, fp);
116         png_set_IHDR(pPngStruct,
117                                  pPngInfo,
118                                  width,
119                                  height,
120                                  PNG_DEPTH,
121                                  PNG_COLOR_TYPE_RGBA,
122                                  PNG_INTERLACE_NONE,
123                                  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
124
125         png_set_bgr(pPngStruct);
126         png_write_info(pPngStruct, pPngInfo);
127
128         const int pixel_size = 4;       // RGBA
129         png_bytep *row_pointers =
130                 png_malloc(pPngStruct, height * sizeof(png_byte *));
131         if (!row_pointers) {
132                 png_destroy_write_struct(&pPngStruct, &pPngInfo);
133                 fclose(fp);
134                 return;
135         }
136
137         unsigned int *blocks = (unsigned int *)data;
138         int y = 0;
139         int x = 0;
140
141         for (; y < height; ++y) {
142                 png_bytep row =
143                         png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
144                 if (!row) {
145                         for (x = 0; x < y; x++)
146                                 png_free(pPngStruct, row_pointers[x]);
147                         png_free(pPngStruct, row_pointers);
148                         png_destroy_write_struct(&pPngStruct, &pPngInfo);
149                         fclose(fp);
150                         return;
151                 }
152
153                 row_pointers[y] = (png_bytep)row;
154                 for (x = 0; x < width; ++x) {
155                         unsigned int curBlock = blocks[y * width + x];
156                         row[x * pixel_size] = (curBlock & 0xFF);
157                         row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
158                         row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
159                         row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
160                 }
161         }
162
163         png_write_image(pPngStruct, row_pointers);
164         png_write_end(pPngStruct, pPngInfo);
165
166         for (y = 0; y < height; y++)
167                 png_free(pPngStruct, row_pointers[y]);
168         png_free(pPngStruct, row_pointers);
169
170         png_destroy_write_struct(&pPngStruct, &pPngInfo);
171
172         fclose(fp);
173 }
174
175 INTERN char *
176 tdm_helper_dump_make_directory(const char *path, char *reply, int *len)
177 {
178         char *fullpath = NULL;
179         time_t timer;
180         struct tm *t, *buf = NULL;
181
182         timer = time(NULL);
183
184         buf = calloc(1, sizeof(struct tm));
185         TDM_GOTO_IF_FAIL(buf != NULL, failed_make);
186
187         fullpath = calloc(1, TDM_PATH_LEN * sizeof(char));
188         TDM_GOTO_IF_FAIL(fullpath != NULL, failed_make);
189
190         t = localtime_r(&timer, buf);
191         TDM_GOTO_IF_FAIL(t != NULL, failed_make);
192
193         snprintf(fullpath, TDM_PATH_LEN, "%s/dump_%04d%02d%02d.%02d%02d%02d", path,
194                          t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
195
196         if ((mkdir(fullpath, 0755)) < 0) {
197                 TDM_ERR("mkdir '%s' fail\n", fullpath);
198                 TDM_SNPRINTF(reply, len, "mkdir '%s' fail\n", fullpath);
199                 goto failed_make;
200         }
201
202         free(buf);
203
204         return fullpath;
205 failed_make:
206         if (fullpath)
207                 free(fullpath);
208         if (buf)
209                 free(buf);
210         return NULL;
211 }
212
213 INTERN void
214 tdm_helper_dump_buffer_str(tbm_surface_h buffer, char *dir, char *str)
215 {
216         tbm_surface_info_s info;
217         const char *ext;
218         char file[TDM_PATH_LEN];
219         int ret, bw;
220
221         TDM_RETURN_IF_FAIL(buffer != NULL);
222         TDM_RETURN_IF_FAIL(dir != NULL);
223         TDM_RETURN_IF_FAIL(str != NULL);
224
225         ret = tbm_surface_get_info(buffer, &info);
226         TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
227
228         if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888) {
229                 ext = file_exts[0];
230                 bw = info.planes[0].stride >> 2;
231         } else {
232                 ext = file_exts[1];
233                 bw = info.planes[0].stride;
234         }
235
236         snprintf(file, TDM_PATH_LEN, "%s/%c%c%c%c_%dx%d_%s.%s",
237                          dir, FOURCC_STR(info.format), bw, info.height, str, ext);
238
239         tdm_helper_dump_buffer(buffer, file);
240 }
241
242 EXTERN void
243 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
244 {
245         tbm_surface_info_s info;
246         int len, ret;
247         const char *ext;
248
249         TDM_RETURN_IF_FAIL(buffer != NULL);
250         TDM_RETURN_IF_FAIL(file != NULL);
251
252         ret = tbm_surface_map(buffer, TBM_OPTION_READ, &info);
253         TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
254
255         len = strnlen(file, 1024);
256         if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
257                 ext = file_exts[0];
258         else
259                 ext = file_exts[1];
260
261         if (strncmp(file + (len - 3), ext, 3)) {
262                 TDM_ERR("can't dump to '%s' file", file + (len - 3));
263                 tbm_surface_unmap(buffer);
264                 return;
265         }
266
267         switch (info.format) {
268         case TBM_FORMAT_ARGB8888:
269         case TBM_FORMAT_XRGB8888:
270                 _tdm_helper_dump_png(file, info.planes[0].ptr,
271                                                          info.planes[0].stride >> 2, info.height);
272                 break;
273         case TBM_FORMAT_YVU420:
274         case TBM_FORMAT_YUV420:
275                 _tdm_helper_dump_raw(file,
276                                                          info.planes[0].ptr,
277                                                          info.planes[0].stride * info.height,
278                                                          info.planes[1].ptr,
279                                                          info.planes[1].stride * (info.height >> 1),
280                                                          info.planes[2].ptr,
281                                                          info.planes[2].stride * (info.height >> 1));
282                 break;
283         case TBM_FORMAT_NV12:
284         case TBM_FORMAT_NV21:
285                 _tdm_helper_dump_raw(file,
286                                                          info.planes[0].ptr,
287                                                          info.planes[0].stride * info.height,
288                                                          info.planes[1].ptr,
289                                                          info.planes[1].stride * (info.height >> 1), NULL,
290                                                          0);
291                 break;
292         case TBM_FORMAT_YUYV:
293         case TBM_FORMAT_UYVY:
294                 _tdm_helper_dump_raw(file,
295                                                          info.planes[0].ptr,
296                                                          info.planes[0].stride * info.height, NULL, 0,
297                                                          NULL, 0);
298                 break;
299         default:
300                 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR(info.format));
301                 tbm_surface_unmap(buffer);
302                 return;
303         }
304
305         tbm_surface_unmap(buffer);
306
307         TDM_INFO("dump %s", file);
308 }
309
310 void
311 tdm_helper_clear_buffer_pos(tbm_surface_h buffer, tdm_pos *pos)
312 {
313         tbm_surface_info_s info;
314         int ret;
315
316         TDM_RETURN_IF_FAIL(buffer != NULL);
317
318         ret = tbm_surface_map(buffer, TBM_OPTION_READ, &info);
319         TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
320
321         switch (info.format) {
322         case TBM_FORMAT_ARGB8888:
323         case TBM_FORMAT_XRGB8888:
324                 if (!pos) {
325                         memset(info.planes[0].ptr, 0, info.planes[0].stride * info.height);
326                 } else {
327                         unsigned char *p;
328                         int x, y;
329                         for (y = pos->y; y <= (pos->y + pos->h); y++) {
330                                 p = info.planes[0].ptr + info.planes[0].stride * y;
331                                 for (x = pos->x; x <= (pos->x + pos->w); x++) {
332                                         int *ibuf = (int*)p;
333                                         ibuf[x] = 0x00000000;
334                                 }
335                         }
336                 }
337                 break;
338         case TBM_FORMAT_YVU420:
339         case TBM_FORMAT_YUV420:
340                 memset((char*)info.planes[0].ptr, 0x10, info.planes[0].stride * info.height);
341                 memset((char*)info.planes[1].ptr, 0x80, info.planes[1].stride * (info.height >> 1));
342                 memset((char*)info.planes[2].ptr, 0x80, info.planes[2].stride * (info.height >> 1));
343                 break;
344         case TBM_FORMAT_NV12:
345         case TBM_FORMAT_NV21:
346                 memset((char*)info.planes[0].ptr, 0x10, info.planes[0].stride * info.height);
347                 memset((char*)info.planes[1].ptr, 0x80, info.planes[1].stride * (info.height >> 1));
348                 break;
349         case TBM_FORMAT_YUYV: {
350                 int *ibuf = (int*)info.planes[0].ptr;
351                 int i, size = info.planes[0].stride * info.height / 4;
352
353                 for (i = 0 ; i < size ; i++)
354                         ibuf[i] = 0x10801080;
355         }
356         break;
357         case TBM_FORMAT_UYVY: {
358                 int *ibuf = (int*)info.planes[0].ptr;
359                 int i, size = info.planes[0].stride * info.height / 4;
360
361                 for (i = 0 ; i < size ; i++)
362                         ibuf[i] = 0x80108010; /* YUYV -> 0xVYUY */
363         }
364         break;
365         default:
366                 TDM_ERR("can't clear %c%c%c%c buffer", FOURCC_STR(info.format));
367                 break;
368         }
369
370         tbm_surface_unmap(buffer);
371 }
372
373 EXTERN void
374 tdm_helper_clear_buffer(tbm_surface_h buffer)
375 {
376         TDM_RETURN_IF_FAIL(buffer != NULL);
377
378         tdm_helper_clear_buffer_pos(buffer, NULL);
379 }
380
381 static pixman_format_code_t
382 _tdm_helper_pixman_format_get(tbm_format format)
383 {
384         switch (format) {
385         case TBM_FORMAT_ARGB8888:
386                 return PIXMAN_a8r8g8b8;
387         case TBM_FORMAT_XRGB8888:
388                 return PIXMAN_x8r8g8b8;
389         default:
390                 return 0;
391         }
392
393         return 0;
394 }
395
396 EXTERN tdm_error
397 tdm_helper_convert_buffer(tbm_surface_h srcbuf, tbm_surface_h dstbuf,
398                                                   tdm_pos *srcpos, tdm_pos *dstpos,
399                                                   tdm_transform transform, int over)
400 {
401         tbm_surface_info_s src_info, dst_info;
402         pixman_image_t *src_img = NULL, *dst_img = NULL;
403         pixman_format_code_t src_format, dst_format;
404         double scale_x, scale_y;
405         int rotate_step, bos;
406         pixman_transform_t t;
407         struct pixman_f_transform ft;
408         pixman_op_t op;
409         int src_stride, dst_stride;
410         int buf_width, err;
411         tdm_error ret = TDM_ERROR_OPERATION_FAILED;
412
413         TDM_RETURN_VAL_IF_FAIL(srcbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
414         TDM_RETURN_VAL_IF_FAIL(dstbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
415
416         bos = tbm_surface_internal_get_num_bos(srcbuf);
417         TDM_RETURN_VAL_IF_FAIL(bos == 1, TDM_ERROR_OPERATION_FAILED);
418
419         bos = tbm_surface_internal_get_num_bos(dstbuf);
420         TDM_RETURN_VAL_IF_FAIL(bos == 1, TDM_ERROR_OPERATION_FAILED);
421
422         err = tbm_surface_map(srcbuf, TBM_OPTION_READ, &src_info);
423         TDM_RETURN_VAL_IF_FAIL(err == TBM_SURFACE_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
424
425         err = tbm_surface_map(dstbuf, TBM_OPTION_WRITE, &dst_info);
426         TDM_GOTO_IF_FAIL(err == TBM_SURFACE_ERROR_NONE, unmap_srcbuf);
427
428         /* not handle buffers which have 2 more gem handles */
429         TDM_GOTO_IF_FAIL(src_info.planes[0].ptr != NULL, unmap_dstbuf);
430         TDM_GOTO_IF_FAIL(dst_info.planes[0].ptr != NULL, unmap_dstbuf);
431
432         src_format = _tdm_helper_pixman_format_get(src_info.format);
433         TDM_GOTO_IF_FAIL(src_format > 0, unmap_dstbuf);
434         dst_format = _tdm_helper_pixman_format_get(dst_info.format);
435         TDM_GOTO_IF_FAIL(dst_format > 0, unmap_dstbuf);
436
437         buf_width = src_info.planes[0].stride >> 2;
438         src_stride = src_info.planes[0].stride;
439         src_img = pixman_image_create_bits(src_format, buf_width, src_info.height,
440                                                                            (uint32_t*)src_info.planes[0].ptr, src_stride);
441         TDM_GOTO_IF_FAIL(src_img, unref_img);
442
443         buf_width = dst_info.planes[0].stride >> 2;
444         dst_stride = dst_info.planes[0].stride;
445         dst_img = pixman_image_create_bits(dst_format, buf_width, dst_info.height,
446                                                                            (uint32_t*)dst_info.planes[0].ptr, dst_stride);
447         TDM_GOTO_IF_FAIL(dst_img, unref_img);
448
449         pixman_f_transform_init_identity(&ft);
450
451         if (transform & TDM_TRANSFORM_FLIPPED) {
452                 pixman_f_transform_scale(&ft, NULL, -1, 1);
453                 pixman_f_transform_translate(&ft, NULL, dstpos->w, 0);
454         }
455
456         rotate_step = transform & 0x3;
457         if (rotate_step > 0) {
458                 int c, s, tx = 0, ty = 0;
459                 switch (rotate_step) {
460                         case 1:
461                                 c = 0, s = -1, tx = -dstpos->w;
462                                 break;
463                         case 2:
464                                 c = -1, s = 0, tx = -dstpos->w, ty = -dstpos->h;
465                                 break;
466                         case 3:
467                                 c = 0, s = 1, ty = -dstpos->h;
468                                 break;
469                         default:
470                                 break;
471                 }
472                 pixman_f_transform_translate(&ft, NULL, tx, ty);
473                 pixman_f_transform_rotate(&ft, NULL, c, s);
474         }
475
476         if (rotate_step % 2 == 0) {
477                 scale_x = (double)srcpos->w / dstpos->w;
478                 scale_y = (double)srcpos->h / dstpos->h;
479         } else {
480                 scale_x = (double)srcpos->w / dstpos->h;
481                 scale_y = (double)srcpos->h / dstpos->w;
482         }
483
484         pixman_f_transform_scale(&ft, NULL, scale_x, scale_y);
485         pixman_f_transform_translate(&ft, NULL, srcpos->x, srcpos->y);
486         pixman_transform_from_pixman_f_transform(&t, &ft);
487         pixman_image_set_transform(src_img, &t);
488
489         op = (!over) ? PIXMAN_OP_SRC : PIXMAN_OP_OVER;
490
491         pixman_image_composite(op, src_img, NULL, dst_img, 0, 0, 0, 0,
492                                                    dstpos->x, dstpos->y, dstpos->w, dstpos->h);
493
494         ret = TDM_ERROR_NONE;
495
496 unref_img:
497         if (src_img)
498                 pixman_image_unref(src_img);
499         if (dst_img)
500                 pixman_image_unref(dst_img);
501 unmap_dstbuf:
502         tbm_surface_unmap(dstbuf);
503 unmap_srcbuf:
504         tbm_surface_unmap(srcbuf);
505
506         return ret;
507 }
508
509 EXTERN int
510 tdm_helper_get_fd(const char *env)
511 {
512         const char *value;
513         int fd, newfd, flags, ret;
514
515         value = (const char*)getenv(env);
516         if (!value)
517                 return -1;
518
519         ret = sscanf(value, "%d", &fd);
520         if (ret < 0) {
521                 TDM_ERR("sscanf failed: %m");
522                 return -1;
523         }
524
525         flags = fcntl(fd, F_GETFD);
526         if (flags == -1) {
527                 TDM_ERR("fcntl failed: %m");
528                 return -1;
529         }
530
531         newfd = dup(fd);
532         if (newfd < 0) {
533                 TDM_ERR("dup failed: %m");
534                 return -1;
535         }
536
537         TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd);
538
539         fcntl(newfd, F_SETFD, flags | FD_CLOEXEC);
540
541         return newfd;
542 }
543
544 EXTERN void
545 tdm_helper_set_fd(const char *env, int fd)
546 {
547         char buf[32];
548         int ret;
549
550         snprintf(buf, sizeof(buf), "%d", fd);
551
552         ret = setenv(env, (const char*)buf, 1);
553         if (ret) {
554                 TDM_ERR("setenv failed: %m");
555                 return;
556         }
557
558         if (fd >= 0)
559                 TDM_INFO("%s: fd(%d)", env, fd);
560 }
561
562 EXTERN void
563 tdm_helper_dump_start(char *dumppath, int *count)
564 {
565         if (dumppath == NULL || count == NULL) {
566                 TDM_DBG("tdm_helper_dump dumppath or count is null.");
567                 return;
568         }
569
570         tdm_dump_enable = 1;
571
572         TDM_DBG("tdm_helper_dump start.(path : %s)", dumppath);
573 }
574
575 EXTERN void
576 tdm_helper_dump_stop(void)
577 {
578         tdm_dump_enable = 0;
579
580         TDM_DBG("tdm_helper_dump stop.");
581 }
582
583 static tdm_error
584 _tdm_helper_buffer_convert(tbm_surface_h srcbuf, tbm_surface_h dstbuf,
585                                                    int dx, int dy, int dw, int dh, int count)
586 {
587         pixman_image_t *src_img = NULL, *dst_img = NULL;
588         pixman_format_code_t src_format, dst_format;
589         pixman_transform_t t;
590         struct pixman_f_transform ft;
591         pixman_op_t op;
592         tbm_surface_info_s src_info = {0, };
593         tbm_surface_info_s dst_info = {0, };
594         int stride, width;
595         double scale_x, scale_y;
596
597         TDM_RETURN_VAL_IF_FAIL(srcbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
598         TDM_RETURN_VAL_IF_FAIL(dstbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
599
600         if (tbm_surface_map(srcbuf, TBM_SURF_OPTION_READ, &src_info)
601                         != TBM_SURFACE_ERROR_NONE) {
602                 TDM_ERR("cannot mmap srcbuf\n");
603                 return TDM_ERROR_OPERATION_FAILED;
604         }
605
606         if (tbm_surface_map(dstbuf, TBM_SURF_OPTION_WRITE, &dst_info)
607                         != TBM_SURFACE_ERROR_NONE) {
608                 TDM_ERR("cannot mmap dstbuf\n");
609                 tbm_surface_unmap(srcbuf);
610                 return TDM_ERROR_OPERATION_FAILED;
611         }
612         TDM_GOTO_IF_FAIL(src_info.num_planes == 1, cant_convert);
613         TDM_GOTO_IF_FAIL(dst_info.num_planes == 1, cant_convert);
614
615         /* src */
616         src_format = _tdm_helper_pixman_format_get(src_info.format);
617         TDM_GOTO_IF_FAIL(src_format > 0, cant_convert);
618
619         width = src_info.planes[0].stride / 4;
620         stride = src_info.planes[0].stride;
621         src_img = pixman_image_create_bits(src_format, width, src_info.height,
622                                                                            (uint32_t*)src_info.planes[0].ptr, stride);
623         TDM_GOTO_IF_FAIL(src_img != NULL, cant_convert);
624
625         /* dst */
626         dst_format = _tdm_helper_pixman_format_get(dst_info.format);
627         TDM_GOTO_IF_FAIL(dst_format > 0, cant_convert);
628
629         width = dst_info.planes[0].stride / 4;
630         stride = dst_info.planes[0].stride;
631         dst_img = pixman_image_create_bits(dst_format, width, dst_info.height,
632                                                                            (uint32_t*)dst_info.planes[0].ptr, stride);
633         TDM_GOTO_IF_FAIL(dst_img != NULL, cant_convert);
634
635         pixman_f_transform_init_identity(&ft);
636
637         scale_x = (double)src_info.width / dw;
638         scale_y = (double)src_info.height / dh;
639
640         pixman_f_transform_scale(&ft, NULL, scale_x, scale_y);
641         pixman_f_transform_translate(&ft, NULL, 0, 0);
642         pixman_transform_from_pixman_f_transform(&t, &ft);
643         pixman_image_set_transform(src_img, &t);
644
645         if (count == 0)
646                 op = PIXMAN_OP_SRC;
647         else
648                 op = PIXMAN_OP_OVER;
649
650         pixman_image_composite(op, src_img, NULL, dst_img,
651                                                    0, 0, 0, 0, dx, dy, dw, dh);
652
653         if (src_img)
654                 pixman_image_unref(src_img);
655         if (dst_img)
656                 pixman_image_unref(dst_img);
657
658         tbm_surface_unmap(srcbuf);
659         tbm_surface_unmap(dstbuf);
660
661         return TDM_ERROR_NONE;
662
663 cant_convert:
664         if (src_img)
665                 pixman_image_unref(src_img);
666
667         tbm_surface_unmap(srcbuf);
668         tbm_surface_unmap(dstbuf);
669
670         return TDM_ERROR_OPERATION_FAILED;
671 }
672
673 EXTERN tdm_error
674 tdm_helper_capture_output(tdm_output *output, tbm_surface_h dst_buffer,
675                                                   int x, int y, int w, int h,
676                                                   tdm_helper_capture_handler func, void *data)
677 {
678         tbm_surface_h surface;
679         tdm_error err;
680         int i, count, first = 0;
681
682         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
683         TDM_RETURN_VAL_IF_FAIL(dst_buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
684         TDM_RETURN_VAL_IF_FAIL(x >= 0, TDM_ERROR_INVALID_PARAMETER);
685         TDM_RETURN_VAL_IF_FAIL(y >= 0, TDM_ERROR_INVALID_PARAMETER);
686         TDM_RETURN_VAL_IF_FAIL(w >= 0, TDM_ERROR_INVALID_PARAMETER);
687         TDM_RETURN_VAL_IF_FAIL(h >= 0, TDM_ERROR_INVALID_PARAMETER);
688         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
689         TDM_RETURN_VAL_IF_FAIL(data != NULL, TDM_ERROR_INVALID_PARAMETER);
690
691         err = tdm_output_get_layer_count(output, &count);
692         if (err != TDM_ERROR_NONE) {
693                 TDM_ERR("tdm_output_get_layer_count fail(%d)\n", err);
694                 return TDM_ERROR_OPERATION_FAILED;
695         }
696         if (count <= 0) {
697                 TDM_ERR("tdm_output_get_layer_count err(%d, %d)\n", err, count);
698                 return TDM_ERROR_BAD_MODULE;
699         }
700
701         for (i = count - 1; i >= 0; i--) {
702                 tdm_layer *layer = tdm_output_get_layer(output, i, NULL);
703
704                 surface = tdm_layer_get_displaying_buffer(layer, &err);
705                 if (err != TDM_ERROR_NONE)
706                         continue;
707
708                 err = _tdm_helper_buffer_convert(surface, dst_buffer, x, y, w, h, first++);
709                 if (err != TDM_ERROR_NONE)
710                         TDM_DBG("convert fail %d-layer buffer\n", i);
711                 else
712                         TDM_DBG("convert success %d-layer buffer\n", i);
713         }
714
715         func(dst_buffer, data);
716
717         return TDM_ERROR_NONE;
718 }
719
720 EXTERN void
721 tdm_helper_get_display_information(tdm_display *dpy, char *reply, int *len)
722 {
723         tdm_private_display *private_display;
724         tdm_backend_module *module_data;
725         tdm_func_output *func_output;
726         tdm_func_layer *func_layer;
727         tdm_private_output *private_output = NULL;
728         tdm_private_layer *private_layer = NULL;
729         tdm_private_pp *private_pp = NULL;
730         tdm_private_capture *private_capture = NULL;
731         tdm_error ret;
732         int i;
733
734         TDM_DBG_RETURN_IF_FAIL(dpy != NULL);
735
736         private_display = dpy;
737         func_output = &private_display->func_output;
738         func_layer = &private_display->func_layer;
739         _pthread_mutex_lock(&private_display->lock);
740
741         /* module information */
742         module_data = private_display->module_data;
743         TDM_SNPRINTF(reply, len, "[TDM backend information]\n");
744         TDM_SNPRINTF(reply, len, "name: %s\n", module_data->name);
745         TDM_SNPRINTF(reply, len, "vendor: %s\n", module_data->vendor);
746         TDM_SNPRINTF(reply, len, "version: %d.%d\n\n",
747                                  (int)TDM_BACKEND_GET_ABI_MAJOR(module_data->abi_version),
748                                  (int)TDM_BACKEND_GET_ABI_MINOR(module_data->abi_version));
749
750         /* output information */
751         TDM_SNPRINTF(reply, len, "[Output information]\n");
752         TDM_SNPRINTF(reply, len, "--------------------------------------------------------------------------------------------\n");
753         TDM_SNPRINTF(reply, len, "idx   maker   model   name   type   status   dpms   subpixel   align_w   min   max   phy(mm)\n");
754         TDM_SNPRINTF(reply, len, "--------------------------------------------------------------------------------------------\n");
755         LIST_FOR_EACH_ENTRY(private_output, &private_display->output_list, link) {
756                 TDM_SNPRINTF(reply, len, "%d   %s   %s   %s   %s   %s   %s   %d   %d   %dx%d   %dx%d   %dx%d\n",
757                                          private_output->index, private_output->caps.maker,
758                                          private_output->caps.model, private_output->caps.name,
759                                          tdm_conn_str(private_output->caps.type),
760                                          tdm_status_str(private_output->caps.status),
761                                          tdm_dpms_str(private_output->current_dpms_value),
762                                          private_output->caps.subpixel,
763                                          TDM_FRONT_VALUE(private_output->caps.preferred_align),
764                                          TDM_FRONT_VALUE(private_output->caps.min_w),
765                                          TDM_FRONT_VALUE(private_output->caps.min_h),
766                                          TDM_FRONT_VALUE(private_output->caps.max_w),
767                                          TDM_FRONT_VALUE(private_output->caps.max_h),
768                                          private_output->caps.mmWidth, private_output->caps.mmHeight);
769
770                 TDM_SNPRINTF(reply, len, "\t%d modes:\n", private_output->caps.mode_count);
771
772                 if (private_output->caps.mode_count > 0) {
773                         const tdm_output_mode *current_mode = NULL;
774
775                         TDM_DBG_GOTO_IF_FAIL(func_output->output_get_mode, unlock);
776                         ret = func_output->output_get_mode(private_output->output_backend, &current_mode);
777                         TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
778
779                         TDM_SNPRINTF(reply, len, "\t\t name refresh (Hz) clk hdisp hss hse htot vdisp vss vse vtot vscan\n");
780                         for (i = 0; i < private_output->caps.mode_count; i++) {
781                                 char *current = (current_mode == private_output->caps.modes + i) ? "*" : " ";
782                                 TDM_SNPRINTF(reply, len, "\t\t%s%s %d %d %d %d %d %d %d %d %d %d %d ",
783                                                          current,
784                                                          private_output->caps.modes[i].name,
785                                                          private_output->caps.modes[i].vrefresh,
786                                                          private_output->caps.modes[i].clock,
787                                                          private_output->caps.modes[i].hdisplay,
788                                                          private_output->caps.modes[i].hsync_start,
789                                                          private_output->caps.modes[i].hsync_end,
790                                                          private_output->caps.modes[i].htotal,
791                                                          private_output->caps.modes[i].vdisplay,
792                                                          private_output->caps.modes[i].vsync_start,
793                                                          private_output->caps.modes[i].vsync_end,
794                                                          private_output->caps.modes[i].vtotal,
795                                                          private_output->caps.modes[i].vscan);
796                                 tdm_mode_flag_str(private_output->caps.modes[i].flags, &reply, len);
797                                 TDM_SNPRINTF(reply, len, " ");
798                                 tdm_mode_type_str(private_output->caps.modes[i].type, &reply, len);
799                                 TDM_SNPRINTF(reply, len, "\n");
800                         }
801                 }
802
803                 TDM_SNPRINTF(reply, len, "\t%d properties:\n", private_output->caps.prop_count);
804                 if (private_output->caps.prop_count > 0) {
805                         TDM_SNPRINTF(reply, len, "\t\tname\tidx\tvalue\n");
806                         for (i = 0; i < private_output->caps.prop_count; i++) {
807                                 tdm_value value;
808                                 TDM_DBG_GOTO_IF_FAIL(func_output->output_get_property, unlock);
809                                 ret = func_output->output_get_property(private_output->output_backend,
810                                                                                                            private_output->caps.props[i].id,
811                                                                                                            &value);
812                                 TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
813                                 TDM_SNPRINTF(reply, len, "\t\t%s\t%d\t%d\n",
814                                                          private_output->caps.props[i].name,
815                                                          private_output->caps.props[i].id,
816                                                          value.u32);
817                         }
818                 }
819         }
820         TDM_SNPRINTF(reply, len, "\n");
821
822         /* layer information */
823         TDM_SNPRINTF(reply, len, "[Layer information]\n");
824         TDM_SNPRINTF(reply, len, "-----------------------------------------------------------------------\n");
825         TDM_SNPRINTF(reply, len, "idx   output   zpos   buf   format   size   crop   geometry   transform\n");
826         TDM_SNPRINTF(reply, len, "-----------------------------------------------------------------------\n");
827         LIST_FOR_EACH_ENTRY(private_output, &private_display->output_list, link) {
828                 LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
829                         if (!private_layer->usable) {
830                                 tdm_info_layer info;
831                                 unsigned int format;
832                                 tdm_size size;
833                                 tbm_surface_info_s buf_info;
834
835                                 TDM_DBG_GOTO_IF_FAIL(func_layer->layer_get_info, unlock);
836                                 memset(&info, 0, sizeof info);
837                                 ret = func_layer->layer_get_info(private_layer->layer_backend, &info);
838                                 TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
839
840                                 format = tbm_surface_get_format(private_layer->showing_buffer);
841                                 tbm_surface_get_info(private_layer->showing_buffer, &buf_info);
842
843                                 if (IS_RGB(format))
844                                         size.h = buf_info.planes[0].stride >> 2;
845                                 else
846                                         size.h = buf_info.planes[0].stride;
847                                 size.v = tbm_surface_get_height(private_layer->showing_buffer);
848
849                                 if (info.src_config.format)
850                                         format = (info.src_config.format)?:format;
851
852                                 TDM_SNPRINTF(reply, len, "%d   %d   %d   %p   %c%c%c%c   %dx%d   %dx%d+%d+%d   %dx%d+%d+%d   %s\n",
853                                                          private_layer->index,
854                                                          private_output->index,
855                                                          private_layer->caps.zpos,
856                                                          private_layer->showing_buffer, FOURCC_STR(format), size.h, size.v,
857                                                          info.src_config.pos.w, info.src_config.pos.h, info.src_config.pos.x, info.src_config.pos.y,
858                                                          info.dst_pos.w, info.dst_pos.h, info.dst_pos.x, info.dst_pos.y,
859                                                          tdm_transform_str(info.transform));
860                         } else {
861                                 TDM_SNPRINTF(reply, len, "%d   %d   %d   -\n",
862                                                          private_layer->index,
863                                                          private_output->index,
864                                                          private_layer->caps.zpos);
865                         }
866
867                         TDM_SNPRINTF(reply, len, "\tcaps\t: ");
868                         tdm_layer_caps_str(private_layer->caps.capabilities, &reply, len);
869                         TDM_SNPRINTF(reply, len, "\n");
870
871                         TDM_SNPRINTF(reply, len, "\tformats\t: ");
872                         if (private_layer->caps.format_count > 0) {
873                                 const char *sep = "";
874                                 for (i = 0; i < private_layer->caps.format_count; i++) {
875                                         if (private_layer->caps.formats[i] == 0)
876                                                 continue;
877                                         TDM_SNPRINTF(reply, len, "%s%c%c%c%c", sep, FOURCC_STR(private_layer->caps.formats[i]));
878                                         sep = ",";
879                                 }
880                                 TDM_SNPRINTF(reply, len, "\n");
881                         }
882
883                         TDM_SNPRINTF(reply, len, "\t%d properties:\n", private_layer->caps.prop_count);
884                         if (private_layer->caps.prop_count > 0) {
885                                 TDM_SNPRINTF(reply, len, "\t\tname\tidx\tvalue\n");
886                                 for (i = 0; i < private_layer->caps.prop_count; i++) {
887                                         tdm_value value;
888                                         TDM_DBG_GOTO_IF_FAIL(func_layer->layer_get_property, unlock);
889                                         ret = func_layer->layer_get_property(private_layer->layer_backend,
890                                                                                                                  private_layer->caps.props[i].id,
891                                                                                                                  &value);
892                                         TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
893                                         TDM_SNPRINTF(reply, len, "\t\t%s\t%d\t%d\n",
894                                                                  private_layer->caps.props[i].name,
895                                                                  private_layer->caps.props[i].id,
896                                                                  value.u32);
897                                 }
898                         }
899                 }
900         }
901         TDM_SNPRINTF(reply, len, "\n");
902
903         if (private_display->capabilities & TDM_DISPLAY_CAPABILITY_PP) {
904                 const char *sep = "";
905                 TDM_SNPRINTF(reply, len, "[PP information]\n");
906                 TDM_SNPRINTF(reply, len, "caps\t: ");
907                 tdm_pp_caps_str(private_display->caps_pp.capabilities, &reply, len);
908                 TDM_SNPRINTF(reply, len, "\n");
909                 TDM_SNPRINTF(reply, len, "formats\t: ");
910                 for (i = 0; i < private_display->caps_pp.format_count; i++) {
911                         if (private_display->caps_pp.formats[i] == 0)
912                                 continue;
913                         TDM_SNPRINTF(reply, len, "%s%c%c%c%c", sep, FOURCC_STR(private_display->caps_pp.formats[i]));
914                         sep = ",";
915                 }
916                 TDM_SNPRINTF(reply, len, "\n");
917                 TDM_SNPRINTF(reply, len, "size\t: min(%dx%d) max(%dx%d) align_w(%d)\n",
918                                          TDM_FRONT_VALUE(private_display->caps_pp.min_w),
919                                          TDM_FRONT_VALUE(private_display->caps_pp.min_h),
920                                          TDM_FRONT_VALUE(private_display->caps_pp.max_w),
921                                          TDM_FRONT_VALUE(private_display->caps_pp.max_h),
922                                          TDM_FRONT_VALUE(private_display->caps_pp.preferred_align));
923                 if (!LIST_IS_EMPTY(&private_display->pp_list)) {
924                         TDM_SNPRINTF(reply, len, "-------------------------------------------------------------\n");
925                         TDM_SNPRINTF(reply, len, "src(format size crop)  |  dst(format size crop)  |  transform\n");
926                         TDM_SNPRINTF(reply, len, "-------------------------------------------------------------\n");
927                         LIST_FOR_EACH_ENTRY(private_pp, &private_display->pp_list, link) {
928                                 TDM_SNPRINTF(reply, len, "%c%c%c%c %dx%d %dx%d+%d+%d | %c%c%c%c %dx%d %dx%d+%d+%d | %s\n",
929                                                          FOURCC_STR(private_pp->info.src_config.format),
930                                                          private_pp->info.src_config.size.h,
931                                                          private_pp->info.src_config.size.v,
932                                                          private_pp->info.src_config.pos.x, private_pp->info.src_config.pos.y,
933                                                          private_pp->info.src_config.pos.w, private_pp->info.src_config.pos.h,
934                                                          FOURCC_STR(private_pp->info.dst_config.format),
935                                                          private_pp->info.dst_config.size.h,
936                                                          private_pp->info.dst_config.size.v,
937                                                          private_pp->info.dst_config.pos.x, private_pp->info.dst_config.pos.y,
938                                                          private_pp->info.dst_config.pos.w, private_pp->info.dst_config.pos.h,
939                                                          tdm_transform_str(private_pp->info.transform));
940                         }
941                 }
942         } else {
943                 TDM_SNPRINTF(reply, len, "[No PP capability]\n");
944         }
945         TDM_SNPRINTF(reply, len, "\n");
946
947         if (private_display->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE) {
948                 const char *sep = "";
949                 TDM_SNPRINTF(reply, len, "[Capture information]\n");
950                 TDM_SNPRINTF(reply, len, "caps\t: ");
951                 tdm_capture_caps_str(private_display->caps_capture.capabilities, &reply, len);
952                 TDM_SNPRINTF(reply, len, "\n");
953                 TDM_SNPRINTF(reply, len, "formats\t: ");
954                 for (i = 0; i < private_display->caps_capture.format_count; i++) {
955                         if (private_display->caps_capture.formats[i] == 0)
956                                 continue;
957                         TDM_SNPRINTF(reply, len, "%s%c%c%c%c", sep, FOURCC_STR(private_display->caps_capture.formats[i]));
958                         sep = ",";
959                 }
960                 TDM_SNPRINTF(reply, len, "\n");
961                 TDM_SNPRINTF(reply, len, "size\t: min(%dx%d) max(%dx%d) align_w(%d)\n",
962                                          TDM_FRONT_VALUE(private_display->caps_capture.min_w),
963                                          TDM_FRONT_VALUE(private_display->caps_capture.min_h),
964                                          TDM_FRONT_VALUE(private_display->caps_capture.max_w),
965                                          TDM_FRONT_VALUE(private_display->caps_capture.max_h),
966                                          TDM_FRONT_VALUE(private_display->caps_capture.preferred_align));
967                 if (!LIST_IS_EMPTY(&private_display->capture_list)) {
968                         TDM_SNPRINTF(reply, len, "-----------------------------------\n");
969                         TDM_SNPRINTF(reply, len, "dst(format size crop)  |  transform\n");
970                         TDM_SNPRINTF(reply, len, "-----------------------------------\n");
971                         LIST_FOR_EACH_ENTRY(private_capture, &private_display->capture_list, link) {
972                                 TDM_SNPRINTF(reply, len, "%c%c%c%c %dx%d %dx%d+%d+%d | %s\n",
973                                                          FOURCC_STR(private_capture->info.dst_config.format),
974                                                          private_capture->info.dst_config.size.h,
975                                                          private_capture->info.dst_config.size.v,
976                                                          private_capture->info.dst_config.pos.x, private_capture->info.dst_config.pos.y,
977                                                          private_capture->info.dst_config.pos.w, private_capture->info.dst_config.pos.h,
978                                                          tdm_transform_str(private_capture->info.transform));
979                         }
980                 }
981         } else {
982                 TDM_SNPRINTF(reply, len, "[No Capture capability]\n");
983         }
984         TDM_SNPRINTF(reply, len, "\n");
985
986 unlock:
987         _pthread_mutex_unlock(&private_display->lock);
988 }