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