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