implementation for SCREEN_PREROTATION_HINT
[platform/core/uifw/libtdm.git] / tools / tdm_test_server.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 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <signal.h>
40 #include <poll.h>
41 #include <errno.h>
42 #include <time.h>
43 #include <stdint.h>
44
45 #include <tbm_surface.h>
46 #include <tbm_surface_internal.h>
47
48 #include <tdm.h>
49 #include <tdm_log.h>
50 #include <tdm_list.h>
51 #include <tdm_helper.h>
52 #include <tdm_backend.h>
53
54 #include "tdm_macro.h"
55 #include "tdm_private.h"
56 #include "buffers.h"
57
58 ////////////////////////////////////////////////////////////////////////////////
59 struct typestrings {
60         int type;
61         const char *string;
62         const char *desc;
63 };
64
65 struct optstrings {
66         int  type;
67         const char *opt;
68         const char *desc;
69         const char *arg;
70         const char *ex;
71 };
72
73 enum {
74         OPT_QRY,
75         OPT_TST,
76         OPT_GEN,
77 };
78
79 static struct typestrings typestrs[] = {
80         {OPT_QRY, "Query",          NULL},
81         {OPT_TST, "Test",           NULL},
82         {OPT_GEN, "General",        NULL},
83 };
84
85 static struct optstrings optstrs[] = {
86         {
87                 OPT_QRY, "q", "show tdm output, layer information",
88                 NULL, NULL
89         },
90         {
91                 OPT_TST, "a", "set all layer objects for all connected outputs",
92                 NULL, NULL
93         },
94         {
95                 OPT_TST, "o", "set a mode for a output object",
96                 "<output_idx>@<mode>[&<refresh>]", "0@1920x1080"
97         },
98         {
99                 OPT_TST, "l", "set a layer object",
100                 "<layer_idx>[:<w>x<h>[+<x>+<y>][,<h>x<v>][@<format>]]~<w>x<h>[+<x>+<y>][*<transform>]", NULL
101         },
102         {
103                 OPT_TST, "p", "set a PP object.\n\t\t'-l' is used to show the result on screen.",
104                 "<w>x<h>[+<x>+<y>][,<h>x<v>][@<format>]~<w>x<h>[+<x>+<y>][,<h>x<v>][@<format>][*<transform>][&<fps>]", NULL
105         },
106         {
107                 OPT_TST, "c", "catpure a output object or a layer object.\n\t\t'-l' is used to show the result on screen.",
108                 "<output_idx>[,<layer_idx>]~<w>x<h>[+<x>+<y>][,<h>x<v>][@<format>][*<transform>]", NULL
109         },
110         {
111                 OPT_GEN, "w", "set the property of a object",
112                 "<prop_name>:<value>", NULL
113         },
114         {
115                 OPT_GEN, "b", "set the fill(smtpe,tiles,plain) and framebuffer type(scanout,noncachable,wc)",
116                 "<fill>[:<buf_flag>[,<buf_flag2>[,...]]]", NULL
117         },
118         {
119                 OPT_GEN, "v", "update layers every vblank",
120                 NULL, NULL
121         },
122 };
123
124 struct usagestring {
125         const char *string;
126         const char *desc;
127 };
128
129 static struct usagestring usages[] = {
130         {
131                 "-q",
132                 NULL
133         },
134         {
135                 "-a -b plain",
136                 "test all outputs, layers with plain buffers"
137         },
138         {
139                 "-o 0@1920x1080",
140                 "Set the \"1920x1080\" mode to the output 0. And show a buffer via a primary layer of the output 0"
141         },
142         {
143                 "-o 0@1920x1080 -l 1~640x480+50+100",
144                 "Create the 640x480 buffer and show it in the (50,100) pos of screen via the layer 1"
145         },
146         {
147                 "-p 320x240@NV12~480x360+80+40,640x480@AR24 -l 1~640x480+50+100",
148                 "Convert the 320x240@NV12 buffer to the 640x480@AR24 buffer(480x360+80+40) and show the result via the layer 1"
149         },
150 };
151
152 static void
153 usage(char *app_name)
154 {
155         int type_size = sizeof(typestrs) / sizeof(struct typestrings);
156         int opt_size = sizeof(optstrs) / sizeof(struct optstrings);
157         int usages_size = sizeof(usages) / sizeof(struct usagestring);
158         int t;
159
160         printf("usage: %s \n\n", app_name);
161
162         for (t = 0; t < type_size; t++) {
163                 int o, f = 1;
164
165                 for (o = 0; o < opt_size; o++)
166                         if (optstrs[o].type == typestrs[t].type) {
167                                 if (f == 1)
168                                         printf(" %s options: %s\n\n", typestrs[t].string, (typestrs[t].desc) ? : "");
169                                 printf("\t-%s\t%s\n", optstrs[o].opt, optstrs[o].desc);
170                                 if (optstrs[o].arg)
171                                         printf("\t\t  %s\n", optstrs[o].arg);
172                                 if (optstrs[o].ex)
173                                         printf("\t\t  ex) %s\n", optstrs[o].ex);
174                                 f = 0;
175                         }
176                 printf("\n");
177         }
178
179         printf(" For example)\n\n");
180
181         for (t = 0; t < usages_size; t++) {
182                 printf("    $ %s %s\n", app_name, usages[t].string);
183                 printf("\t%s\n", usages[t].desc);
184         }
185         printf("\n");
186         exit(0);
187 }
188
189 ////////////////////////////////////////////////////////////////////////////////
190
191 static const char *tdm_buf_flag_names[] = {
192         "scanout",
193         "noncachable",
194         "wc",
195 };
196 TDM_BIT_NAME_FB(buf_flag)
197
198 #define DEFAULT_FORMAT  TBM_FORMAT_ARGB8888
199
200 #define print_size(s) \
201         printf("%dx%d", (s)->h, (s)->v)
202 #define print_pos(p) \
203         printf("%dx%d+%d+%d", (p)->w, (p)->h, (p)->x, (p)->y)
204 #define print_format(f) \
205         if (f) printf("%c%c%c%c", FOURCC_STR(f)); \
206         else printf("NONE")
207 #define print_config(c) \
208         do { \
209                 print_size(&(c)->size); \
210                 printf(" "); \
211                 print_pos(&(c)->pos); \
212                 printf(" "); \
213                 print_format((c)->format); \
214         } while (0)
215 #define print_prop(w) \
216         printf("%s(%d)", (w)->name, ((w)->value).u32)
217
218 typedef struct _tdm_test_server tdm_test_server;
219 typedef struct _tdm_test_server_layer tdm_test_server_layer;
220 typedef struct _tdm_test_server_capture tdm_test_server_capture;
221
222 typedef struct _tdm_test_server_prop {
223         /* args */
224         char name[TDM_NAME_LEN];
225         tdm_value value;
226
227         /* variables for test */
228         struct list_head link;
229 } tdm_test_server_prop;
230
231 typedef struct _tdm_test_server_buffer {
232         /* variables for test */
233         tbm_surface_h b;
234         int in_use;
235         tdm_test_server_layer *l;
236
237         tdm_test_server_capture *c;
238         tdm_buffer_release_handler done;
239 } tdm_test_server_buffer;
240
241 typedef struct _tdm_test_server_output {
242         /* args */
243         int idx;
244         char mode[TDM_NAME_LEN];
245         int refresh;
246
247         /* variables for test */
248         struct list_head link;
249         struct list_head prop_list;
250         struct list_head layer_list;
251         tdm_test_server *data;
252         tdm_output *output;
253
254         int fill_primary_layer;
255 } tdm_test_server_output;
256
257 typedef struct _tdm_test_server_pp {
258         /* args */
259         tdm_info_pp info;
260         int fps;
261
262         /* variables for test */
263         struct list_head link;
264         tdm_test_server *data;
265         tdm_test_server_layer *l;
266         tdm_pp *pp;
267         tbm_surface_h bufs[6];
268         int buf_idx;
269
270         tdm_event_loop_source *timer_source;
271 } tdm_test_server_pp;
272
273 struct _tdm_test_server_capture {
274         /* args */
275         int output_idx;
276         int layer_idx;
277         tdm_info_capture info;
278
279         /* variables for test */
280         struct list_head link;
281         tdm_test_server *data;
282         tdm_test_server_layer *l;
283         tdm_capture *capture;
284 };
285
286 struct _tdm_test_server_layer {
287         /* args */
288         int idx;
289         tdm_info_layer info;
290
291         /* variables for test */
292         struct list_head link;
293         struct list_head prop_list;
294         tdm_test_server *data;
295         tdm_test_server_output *o;
296         tdm_layer *layer;
297         int is_primary;
298         tdm_test_server_pp *owner_p;
299         tdm_test_server_capture *owner_c;
300         tbm_surface_h bufs[3];
301         int buf_idx;
302 };
303
304 struct _tdm_test_server {
305         /* args */
306         int do_query;
307         int do_all;
308         int do_vblank;
309         int bflags;
310         int b_fill;
311
312         /* variables for test */
313         struct list_head output_list;
314         struct list_head pp_list;
315         struct list_head capture_list;
316         tdm_display *display;
317 };
318
319 static void run_test(tdm_test_server *data);
320 static void output_setup(tdm_test_server_output *o);
321 static void layer_show_buffer(tdm_test_server_layer *l, tbm_surface_h b);
322 static void capture_attach(tdm_test_server_capture *c, tbm_surface_h b);
323
324 static char*
325 parse_size(tdm_size *size, char *arg)
326 {
327         char *end;
328         size->h = strtol(arg, &end, 10);
329         TDM_EXIT_IF_FAIL(*end == 'x');
330         arg = end + 1;
331         size->v = strtol(arg, &end, 10);
332         return end;
333 }
334
335 static char*
336 parse_pos(tdm_pos *pos, char *arg)
337 {
338         char *end;
339         pos->w = strtol(arg, &end, 10);
340         TDM_EXIT_IF_FAIL(*end == 'x');
341         arg = end + 1;
342         pos->h = strtol(arg, &end, 10);
343         if (*end == '+') {
344                 arg = end + 1;
345                 pos->x = strtol(arg, &end, 10);
346                 TDM_EXIT_IF_FAIL(*end == '+');
347                 arg = end + 1;
348                 pos->y = strtol(arg, &end, 10);
349         }
350         return end;
351 }
352
353 static char*
354 parse_config(tdm_info_config *config, char *arg)
355 {
356         char *end;
357         end = parse_pos(&config->pos, arg);
358         if (*end == ',') {
359                 arg = end + 1;
360                 end = parse_size(&config->size, arg);
361         }
362         if (*end == '@') {
363                 char temp[32];
364                 arg = end + 1;
365                 end = strtostr(temp, 32, arg, TDM_DELIM);
366                 config->format = FOURCC_ID(temp);
367         }
368         return end;
369 }
370
371 static void
372 parse_arg_o(tdm_test_server_output *o, char *arg)
373 {
374         char *end;
375         TDM_EXIT_IF_FAIL(arg != NULL);
376         o->idx = strtol(arg, &end, 10);
377         TDM_EXIT_IF_FAIL(*end == '@');
378         arg = end + 1;
379         end = strtostr(o->mode, TDM_NAME_LEN, arg, TDM_DELIM);
380         if (*end == ',') {
381                 arg = end + 1;
382                 o->refresh = strtol(arg, &end, 10);
383         }
384 }
385
386 static void
387 parse_arg_p(tdm_test_server_pp *p, char *arg)
388 {
389         tdm_info_pp *pp_info = &p->info;
390         char *end;
391         TDM_EXIT_IF_FAIL(arg != NULL);
392         end = parse_config(&pp_info->src_config, arg);
393         TDM_EXIT_IF_FAIL(*end == '~');
394         arg = end + 1;
395         end = parse_config(&pp_info->dst_config, arg);
396         if (*end == '*') {
397                 arg = end + 1;
398                 pp_info->transform = strtol(arg, &end, 10);
399         }
400         if (*end == '&') {
401                 arg = end + 1;
402                 p->fps = strtol(arg, &end, 10);
403         }
404 }
405
406 static void
407 parse_arg_c(tdm_test_server_capture *c, char *arg)
408 {
409         tdm_info_capture *capture_info = &c->info;
410         char *end;
411         TDM_EXIT_IF_FAIL(arg != NULL);
412         c->output_idx = strtol(arg, &end, 10);
413         if (*end == ',') {
414                 arg = end + 1;
415                 c->layer_idx = strtol(arg, &end, 10);
416         }
417         TDM_EXIT_IF_FAIL(*end == '~');
418         arg = end + 1;
419         end = parse_config(&capture_info->dst_config, arg);
420         if (*end == '*') {
421                 arg = end + 1;
422                 capture_info->transform = strtol(arg, &end, 10);
423         }
424 }
425
426 static void
427 parse_arg_l(tdm_test_server_layer *l, char *arg)
428 {
429         tdm_info_layer *layer_info = &l->info;
430         char *end;
431         TDM_EXIT_IF_FAIL(arg != NULL);
432         l->idx = strtol(arg, &end, 10);
433         if (*end == ':') {
434                 arg = end + 1;
435                 end = parse_config(&layer_info->src_config, arg);
436         }
437         TDM_EXIT_IF_FAIL(*end == '~');
438         arg = end + 1;
439         end = parse_pos(&layer_info->dst_pos, arg);
440         if (*end == '*') {
441                 arg = end + 1;
442                 layer_info->transform = strtol(arg, &end, 10);
443         }
444 }
445
446 static void
447 parse_arg_w(tdm_test_server_prop *w, char *arg)
448 {
449         char *end;
450         TDM_EXIT_IF_FAIL(arg != NULL);
451         end = strtostr(w->name, TDM_PATH_LEN, arg, TDM_DELIM);
452         TDM_EXIT_IF_FAIL(*end == ':');
453         arg = end + 1;
454         w->value.u32 = strtol(arg, &end, 10);
455 }
456
457 static void
458 parse_arg_b(tdm_test_server *data, char *arg)
459 {
460         char *end = arg;
461         char temp[TDM_NAME_LEN] = {0,};
462         TDM_EXIT_IF_FAIL(arg != NULL);
463
464         end = strtostr(temp, 32, arg, TDM_DELIM);
465         if (!strncmp(temp, "smpte", 5))
466                 data->b_fill = PATTERN_SMPTE;
467         else if (!strncmp(temp, "tiles", 5))
468                 data->b_fill = PATTERN_TILES;
469         else if (!strncmp(temp, "plain", 5))
470                 data->b_fill = PATTERN_PLAIN;
471         else {
472                 printf("'%s': unknown flag\n", temp);
473                 exit(0);
474         }
475
476         if (*arg == ':') {
477                 data->bflags = 0;
478                 arg = end + 1;
479                 snprintf(temp, TDM_NAME_LEN, "%s", arg);
480                 arg = strtok_r(temp, ",", &end);
481                 while (arg) {
482                         if (!strncmp(arg, "default", 7))
483                                 printf("Ignore '%s' flag\n", arg);
484                         else if (!strncmp(arg, "scanout", 7))
485                                 data->bflags |= TBM_BO_SCANOUT;
486                         else if (!strncmp(arg, "noncachable", 11))
487                                 data->bflags |= TBM_BO_NONCACHABLE;
488                         else if (!strncmp(arg, "wc", 2))
489                                 data->bflags |= TBM_BO_WC;
490                         else {
491                                 printf("'%s': unknown flag\n", arg);
492                                 exit(0);
493                         }
494                         arg = strtok_r(NULL, ",", &end);
495                 }
496         }
497 }
498
499 static void
500 parse_args(tdm_test_server *data, int argc, char *argv[])
501 {
502         tdm_test_server_output *o = NULL;
503         tdm_test_server_layer *l = NULL;
504         tdm_test_server_pp *p = NULL;
505         tdm_test_server_capture *c = NULL;
506         tdm_test_server_prop *w = NULL;
507         void *last_option = NULL;
508         void *last_object = NULL;
509         int i;
510
511         if (argc < 2) {
512                 usage(argv[0]);
513                 exit(0);
514         }
515
516         for (i = 1; i < argc; i++) {
517                 if (!strncmp(argv[i] + 1, "q", 1)) {
518                         data->do_query = 1;
519                         return;
520                 } else if (!strncmp(argv[i] + 1, "a", 1)) {
521                         data->do_all = 1;
522                 } else if (!strncmp(argv[i] + 1, "o", 1)) {
523                         TDM_GOTO_IF_FAIL(data->do_all == 0, all);
524                         o = calloc(1, sizeof * o);
525                         TDM_EXIT_IF_FAIL(o != NULL);
526                         o->data = data;
527                         LIST_INITHEAD(&o->layer_list);
528                         LIST_INITHEAD(&o->prop_list);
529                         LIST_ADDTAIL(&o->link, &data->output_list);
530                         parse_arg_o(o, argv[++i]);
531                         last_object = o;
532                 } else if (!strncmp(argv[i] + 1, "p", 1)) {
533                         TDM_GOTO_IF_FAIL(data->do_all == 0, all);
534                         p = calloc(1, sizeof * p);
535                         TDM_EXIT_IF_FAIL(p != NULL);
536                         p->data = data;
537                         p->fps = 30;   /* default 30 fps */
538                         LIST_ADDTAIL(&p->link, &data->pp_list);
539                         parse_arg_p(p, argv[++i]);
540                         last_option = p;
541                 } else if (!strncmp(argv[i] + 1, "c", 1)) {
542                         TDM_GOTO_IF_FAIL(data->do_all == 0, all);
543                         c = calloc(1, sizeof * c);
544                         TDM_EXIT_IF_FAIL(c != NULL);
545                         c->data = data;
546                         c->output_idx = -1;
547                         c->layer_idx = -1;
548                         LIST_ADDTAIL(&c->link, &data->capture_list);
549                         parse_arg_c(c, argv[++i]);
550                         last_option = c;
551                 } else if (!strncmp(argv[i] + 1, "l", 1)) {
552                         TDM_GOTO_IF_FAIL(data->do_all == 0, all);
553                         if (!o) {
554                                 o = calloc(1, sizeof * o);
555                                 TDM_EXIT_IF_FAIL(o != NULL);
556                                 o->data = data;
557                                 LIST_INITHEAD(&o->layer_list);
558                                 LIST_INITHEAD(&o->prop_list);
559                                 LIST_ADDTAIL(&o->link, &data->output_list);
560                         }
561                         l = calloc(1, sizeof * l);
562                         TDM_EXIT_IF_FAIL(l != NULL);
563                         LIST_INITHEAD(&l->prop_list);
564                         LIST_ADDTAIL(&l->link, &o->layer_list);
565                         l->data = data;
566                         l->o = o;
567                         parse_arg_l(l, argv[++i]);
568                         if (p && last_option == p) {
569                                 p->l = l;
570                                 l->owner_p = p;
571                         } else if (c && last_option == c) {
572                                 c->l = l;
573                                 l->owner_c = c;
574                         }
575                         last_object = l;
576                 } else if (!strncmp(argv[i] + 1, "w", 1)) {
577                         TDM_GOTO_IF_FAIL(data->do_all == 0, all);
578                         if (!last_object)
579                                 goto no_object;
580                         w = calloc(1, sizeof * w);
581                         TDM_EXIT_IF_FAIL(w != NULL);
582                         if (o && last_object == o)
583                                 LIST_ADDTAIL(&w->link, &o->prop_list);
584                         else if (l && last_object == l)
585                                 LIST_ADDTAIL(&w->link, &l->prop_list);
586                         parse_arg_w(w, argv[++i]);
587                 } else if (!strncmp(argv[i] + 1, "b", 1)) {
588                         parse_arg_b(data, argv[++i]);
589                 } else if (!strncmp(argv[i] + 1, "v", 1)) {
590                         data->do_vblank = 1;
591                 } else {
592                         usage(argv[0]);
593                         exit(0);
594                 }
595         }
596
597         LIST_FOR_EACH_ENTRY(p, &data->pp_list, link) {
598                 if (!p->l)
599                         goto no_layer;
600         }
601         LIST_FOR_EACH_ENTRY(c, &data->capture_list, link) {
602                 if (!c->l)
603                         goto no_layer;
604         }
605
606         return;
607 no_layer:
608         printf("Use '-l' to set a layer for '-p' or '-c'.\n");
609         exit(0);
610 no_object:
611         printf("Use '-o' or '-l' to set a object first.\n");
612         exit(0);
613 all:
614         printf("Can't use '-%s' with '-a'.\n", argv[i] + 1);
615         exit(0);
616 }
617
618 static void
619 interpret_args(tdm_test_server *data)
620 {
621         tdm_test_server_output *o = NULL;
622         tdm_test_server_layer *l = NULL;
623         tdm_error ret;
624
625         /* create the objects of outputs */
626         if (data->do_all) {
627                 int i, output_count;
628
629                 ret = tdm_display_get_output_count(data->display, &output_count);
630                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
631
632                 for (i = 0; i < output_count; i++) {
633                         o = calloc(1, sizeof * o);
634                         TDM_EXIT_IF_FAIL(o != NULL);
635                         o->data = data;
636                         LIST_INITHEAD(&o->layer_list);
637                         LIST_INITHEAD(&o->prop_list);
638                         LIST_ADDTAIL(&o->link, &data->output_list);
639                         o->idx = i;
640                         o->fill_primary_layer = 1;
641                 }
642         }
643
644         /* check if the primary layer object exists */
645         LIST_FOR_EACH_ENTRY(o, &data->output_list, link) {
646                 tdm_output *output;
647                 const tdm_output_mode *mode;
648                 int j, layer_count, primary_index = 0;
649                 tdm_test_server_layer *primary_l = NULL;
650
651                 output_setup(o);
652
653                 output = tdm_display_get_output(data->display, o->idx, &ret);
654                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
655                 ret = tdm_output_get_mode(output, &mode);
656                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
657                 ret = tdm_output_get_layer_count(output, &layer_count);
658                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
659                 ret = tdm_output_get_primary_index(output, &primary_index);
660                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
661
662                 if (LIST_IS_EMPTY(&o->layer_list))
663                         o->fill_primary_layer = 1;
664                 else {
665                         LIST_FOR_EACH_ENTRY(l, &o->layer_list, link) {
666                                 if (l->idx == primary_index) {
667                                         l->is_primary = 1;
668                                         o->fill_primary_layer = 1;
669                                         primary_l = l;
670                                         break;
671                                 }
672                         }
673                 }
674
675                 if (!primary_l || data->do_all) {
676                         for (j = 0; j < layer_count; j++) {
677                                 if (j != primary_index && !data->do_all)
678                                         continue;
679                                 l = calloc(1, sizeof * l);
680                                 TDM_EXIT_IF_FAIL(l != NULL);
681                                 LIST_INITHEAD(&l->prop_list);
682                                 LIST_ADDTAIL(&l->link, &o->layer_list);
683                                 l->data = data;
684                                 l->o = o;
685                                 l->idx = j;
686                                 if (j == primary_index) {
687                                         l->is_primary = 1;
688                                         l->info.dst_pos.w = mode->hdisplay;
689                                         l->info.dst_pos.h = mode->vdisplay;
690                                         primary_l = l;
691                                 } else {
692                                         l->info.dst_pos.w = TDM_ALIGN(mode->hdisplay / 3, 2);
693                                         l->info.dst_pos.h = TDM_ALIGN(mode->vdisplay / 3, 2);
694                                         l->info.dst_pos.x = TDM_ALIGN(((mode->hdisplay / 3) / layer_count) * j, 2);
695                                         l->info.dst_pos.y = TDM_ALIGN(((mode->vdisplay / 3) / layer_count) * j, 2);
696                                 }
697                         }
698                 }
699
700                 TDM_EXIT_IF_FAIL(primary_l != NULL);
701                 LIST_DEL(&primary_l->link);
702                 LIST_ADD(&primary_l->link, &o->layer_list);
703         }
704
705         /* fill the empty information of layers */
706         LIST_FOR_EACH_ENTRY(o, &data->output_list, link) {
707                 tdm_output *output;
708                 int minw, minh, maxw, maxh;
709
710                 output = tdm_display_get_output(data->display, o->idx, &ret);
711                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
712                 ret = tdm_output_get_available_size(output, &minw, &minh, &maxw, &maxh, NULL);
713                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
714
715                 /* l->info.src_config.size will be decided when a buffer shows really */
716                 LIST_FOR_EACH_ENTRY(l, &o->layer_list, link) {
717                         if (minw > 0 && minh > 0) {
718                                 TDM_EXIT_IF_FAIL(l->info.dst_pos.w >= minw);
719                                 TDM_EXIT_IF_FAIL(l->info.dst_pos.h >= minh);
720                         }
721                         if (maxw > 0 && maxh > 0) {
722                                 TDM_EXIT_IF_FAIL(l->info.dst_pos.w <= maxw);
723                                 TDM_EXIT_IF_FAIL(l->info.dst_pos.h <= maxh);
724                         }
725
726                         if (l->owner_p) {
727                                 l->info.src_config.format = l->owner_p->info.dst_config.format;
728                                 if (l->info.src_config.pos.w == 0) {
729                                         TDM_EXIT_IF_FAIL(l->owner_p->info.dst_config.size.h > 0);
730                                         l->info.src_config.pos.w = l->owner_p->info.dst_config.size.h;
731                                         l->info.src_config.pos.h = l->owner_p->info.dst_config.size.v;
732                                 }
733                         } else if (l->owner_c) {
734                                 l->info.src_config.format = l->owner_c->info.dst_config.format;
735                                 if (l->info.src_config.pos.w == 0) {
736                                         TDM_EXIT_IF_FAIL(l->owner_c->info.dst_config.size.h > 0);
737                                         l->info.src_config.pos.w = l->owner_c->info.dst_config.size.h;
738                                         l->info.src_config.pos.h = l->owner_c->info.dst_config.size.v;
739                                 }
740                         } else {
741                                 if (l->info.src_config.pos.w == 0) {
742                                         TDM_EXIT_IF_FAIL(l->info.dst_pos.w > 0);
743                                         l->info.src_config.pos.w = l->info.dst_pos.w;
744                                         l->info.src_config.pos.h = l->info.dst_pos.h;
745                                 }
746                         }
747                 }
748         }
749 }
750
751 static unsigned int tts_screen_prerotation_hint;
752 static tdm_test_server tts_data;
753 static int tts_buffer_key;
754 #define TTS_BUFFER_KEY ((unsigned long)&tts_buffer_key)
755
756 static tbm_surface_h
757 buffer_allocate(int width, int height, int format, int flags)
758 {
759         tdm_test_server_buffer *tb = calloc(1, sizeof *tb);
760         TDM_EXIT_IF_FAIL(tb != NULL);
761         if (tts_screen_prerotation_hint % 180)
762                 TDM_SWAP(width, height);
763         tb->b = tbm_surface_internal_create_with_flags(width, height, format, flags);
764         TDM_EXIT_IF_FAIL(tb->b != NULL);
765         tdm_helper_clear_buffer(tb->b);
766         tbm_surface_internal_add_user_data(tb->b, TTS_BUFFER_KEY, free);
767         tbm_surface_internal_set_user_data(tb->b, TTS_BUFFER_KEY, tb);
768         return tb->b;
769 }
770
771 static tdm_test_server_buffer*
772 get_tts_buffer(tbm_surface_h b)
773 {
774         tdm_test_server_buffer *tb = NULL;
775         tbm_surface_internal_get_user_data(b, TTS_BUFFER_KEY, (void **)&tb);
776         return tb;
777 }
778
779 static void
780 exit_test(int sig)
781 {
782         tdm_test_server *data = &tts_data;
783         tdm_test_server_output *o = NULL, *oo = NULL;
784         tdm_test_server_layer *l = NULL, *ll = NULL;
785         tdm_test_server_pp *p = NULL, *pp = NULL;
786         tdm_test_server_capture *c = NULL, *cc = NULL;
787         tdm_test_server_prop *w = NULL, *ww = NULL;
788         int i;
789
790         printf("got signal: %d\n", sig);
791
792         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &data->output_list, link) {
793                 LIST_DEL(&o->link);
794
795                 LIST_FOR_EACH_ENTRY_SAFE(l, ll, &o->layer_list, link) {
796                         LIST_DEL(&l->link);
797
798                         tdm_layer_unset_buffer(l->layer);
799
800                         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &l->prop_list, link) {
801                                 LIST_DEL(&w->link);
802                                 free(w);
803                         }
804                         for (i = 0; i < TDM_ARRAY_SIZE(l->bufs); i++)
805                                 tbm_surface_destroy(l->bufs[i]);
806                         free(l);
807                 }
808
809                 tdm_output_commit(o->output, 0, NULL, NULL);
810
811                 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &o->prop_list, link) {
812                         LIST_DEL(&w->link);
813                         free(w);
814                 }
815
816                 free(o);
817         }
818
819
820         LIST_FOR_EACH_ENTRY_SAFE(p, pp, &data->pp_list, link) {
821                 LIST_DEL(&p->link);
822
823                 tdm_display_lock(data->display);
824                 tdm_event_loop_source_remove(p->timer_source);
825                 tdm_display_unlock(data->display);
826
827                 tdm_pp_destroy(p->pp);
828                 for (i = 0; i < TDM_ARRAY_SIZE(p->bufs); i++)
829                         tbm_surface_destroy(p->bufs[i]);
830                 free(p);
831         }
832
833         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &data->capture_list, link) {
834                 LIST_DEL(&c->link);
835                 tdm_capture_destroy(c->capture);
836                 free(c);
837         }
838
839         if (data->display)
840                 tdm_display_deinit(data->display);
841
842         exit(0);
843 }
844
845 int
846 main(int argc, char *argv[])
847 {
848         tdm_test_server *data = &tts_data;
849         char temp[TDM_SERVER_REPLY_MSG_LEN];
850         int len = sizeof temp;
851         tdm_error ret;
852         const char *value;
853
854         value = (const char*)getenv("SCREEN_PREROTATION_HINT");
855         if (value) {
856                 char *end;
857                 tts_screen_prerotation_hint = strtol(value, &end, 10);
858                 printf("SCREEN_PREROTATION_HINT = %d", tts_screen_prerotation_hint);
859         }
860
861         signal(SIGINT, exit_test);    /* 2 */
862         signal(SIGTERM, exit_test);   /* 15 */
863
864         memset(data, 0, sizeof * data);
865         LIST_INITHEAD(&data->output_list);
866         LIST_INITHEAD(&data->pp_list);
867         LIST_INITHEAD(&data->capture_list);
868
869         /* init value */
870         data->bflags = TBM_BO_SCANOUT;
871         data->b_fill = PATTERN_SMPTE;
872
873         data->display = tdm_display_init(&ret);
874         TDM_EXIT_IF_FAIL(data->display != NULL);
875
876         parse_args(data, argc, argv);
877         interpret_args(data);
878
879         if (data->do_query) {
880                 tdm_helper_get_display_information(data->display, temp, &len);
881                 printf("%s", temp);
882                 goto done;
883         }
884
885         run_test(data);
886
887 done:
888         tdm_display_deinit(data->display);
889
890         return 0;
891 }
892
893 static void
894 output_setup(tdm_test_server_output *o)
895 {
896         tdm_test_server *data = o->data;
897         const tdm_output_mode *modes, *found = NULL, *best = NULL, *prefer = NULL;
898         tdm_test_server_prop *w = NULL;
899         const tdm_prop *props;
900         int i, count;
901         tdm_error ret;
902
903         o->output = tdm_display_get_output(data->display, o->idx, &ret);
904         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
905
906         ret = tdm_output_get_available_modes(o->output, &modes, &count);
907         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
908
909         for (i = 0; i < count; i++) {
910                 if (!strncmp(o->mode, modes[i].name, TDM_NAME_LEN) && o->refresh == modes[i].vrefresh) {
911                         found = &modes[i];
912                         printf("found mode: %dx%d %d\n", found->hdisplay, found->vdisplay, found->vrefresh);
913                         break;
914                 }
915                 if (!best)
916                         best = &modes[i];
917                 if (modes[i].flags & TDM_OUTPUT_MODE_TYPE_PREFERRED)
918                         prefer = &modes[i];
919         }
920         if (!found && prefer) {
921                 found = prefer;
922                 printf("found prefer mode: %dx%d %d\n", found->hdisplay, found->vdisplay, found->vrefresh);
923         }
924         if (!found && best) {
925                 found = best;
926                 printf("found best mode: %dx%d %d\n", found->hdisplay, found->vdisplay, found->vrefresh);
927         }
928
929         if (!found) {
930                 printf("couldn't find any mode\n");
931                 exit(0);
932         }
933
934         ret = tdm_output_set_mode(o->output, found);
935         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
936
937         printf("output %d: %s %d\n", o->idx, found->name, found->vrefresh);
938
939         ret = tdm_output_get_available_properties(o->output, &props, &count);
940         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
941
942         LIST_FOR_EACH_ENTRY(w, &o->prop_list, link) {
943                 for (i = 0; i < count; i++) {
944                         if (strncmp(w->name, props[i].name, TDM_NAME_LEN))
945                                 continue;
946                         ret = tdm_output_set_property(o->output, props[i].id, w->value);
947                         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
948                         printf("\tprop '%s': %d\n", props[i].name, w->value.u32);
949                         break;
950                 }
951         }
952
953         /* DPMS on forcely at the first time. */
954         ret = tdm_output_set_dpms(o->output, TDM_OUTPUT_DPMS_ON);
955         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
956 }
957
958 static tbm_surface_h
959 layer_get_buffer(tdm_test_server_layer *l)
960 {
961         int i, size = TDM_ARRAY_SIZE(l->bufs);
962         if (!l->bufs[0]) {
963                 for (i = 0; i < size; i++) {
964                         int width = (l->info.src_config.size.h)?:l->info.src_config.pos.w;
965                         int height = (l->info.src_config.size.v)?:l->info.src_config.pos.h;
966                         unsigned int format = (l->info.src_config.format)?:DEFAULT_FORMAT;
967                         int flags = l->o->data->bflags;
968                         tbm_surface_h b = buffer_allocate(width, height, format, flags);
969                         TDM_EXIT_IF_FAIL(b != NULL);
970                         l->bufs[i] = b;
971                 }
972         }
973         for (i = 0; i < size; i++) {
974                 tdm_test_server_buffer *tb = get_tts_buffer(l->bufs[i]);
975                 TDM_EXIT_IF_FAIL(tb != NULL);
976                 if (!tb->in_use)
977                         return l->bufs[i];
978         }
979         printf("no available layer buffer.\n");
980         exit(0);
981 }
982
983 static void
984 layer_cb_commit(tdm_output *output, unsigned int sequence,
985                                 unsigned int tv_sec, unsigned int tv_usec,
986                                 void *user_data)
987 {
988         tdm_test_server_layer *l = user_data;
989         tbm_surface_h b = layer_get_buffer(l);
990         TDM_EXIT_IF_FAIL(b != NULL);
991
992         if (!l->is_primary || l->o->fill_primary_layer)
993                 tdm_test_buffer_fill(b, l->data->b_fill);
994
995         if (!l->is_primary || l->o->fill_primary_layer)
996                 layer_show_buffer(l, b);
997 }
998
999 static void
1000 layer_cb_buffer_release(tbm_surface_h b, void *user_data)
1001 {
1002         tdm_test_server_buffer *tb = get_tts_buffer(b);
1003         TDM_EXIT_IF_FAIL(tb != NULL);
1004
1005         tb->in_use = 0;
1006         tdm_buffer_remove_release_handler(b, layer_cb_buffer_release, NULL);
1007         if (tb->done)
1008                 tb->done(b, user_data);
1009 }
1010
1011 static void
1012 layer_show_buffer(tdm_test_server_layer *l, tbm_surface_h b)
1013 {
1014         tdm_test_server *data = l->o->data;
1015         tdm_test_server_buffer *tb;
1016         tdm_error ret;
1017
1018         ret = tdm_layer_set_buffer(l->layer, b);
1019         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1020
1021         tb = get_tts_buffer(b);
1022         TDM_EXIT_IF_FAIL(tb != NULL);
1023
1024         tb->in_use = 1;
1025         tdm_buffer_add_release_handler(b, layer_cb_buffer_release, NULL);
1026
1027         if (data->do_vblank)
1028                 ret = tdm_output_commit(l->o->output, 0, layer_cb_commit, l);
1029         else
1030                 ret = tdm_output_commit(l->o->output, 0, NULL, NULL);
1031         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1032
1033         printf("show:\tl(%p) b(%p)\n", l, b);
1034 }
1035
1036 static void
1037 layer_setup(tdm_test_server_layer *l, tbm_surface_h b)
1038 {
1039         tdm_test_server_prop *w = NULL;
1040         const tdm_prop *props;
1041         int i, count;
1042         tdm_error ret;
1043         tbm_surface_info_s info;
1044
1045         l->layer = tdm_output_get_layer(l->o->output, l->idx, &ret);
1046         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1047
1048         /* The size and format information should be same with buffer's */
1049         tbm_surface_get_info(b, &info);
1050         if (tts_screen_prerotation_hint % 180) {
1051                 if (IS_RGB(info.format)) {
1052                         l->info.src_config.size.h = info.height;
1053                         l->info.src_config.size.v = info.planes[0].stride >> 2;
1054                 } else {
1055                         l->info.src_config.size.h = info.height;
1056                         l->info.src_config.size.v = info.planes[0].stride;
1057                 }
1058         } else {
1059                 if (IS_RGB(info.format)) {
1060                         l->info.src_config.size.h = info.planes[0].stride >> 2;
1061                         l->info.src_config.size.v = info.height;
1062                 } else {
1063                         l->info.src_config.size.h = info.planes[0].stride;
1064                         l->info.src_config.size.v = info.height;
1065                 }
1066         }
1067         l->info.src_config.format = info.format;
1068
1069         ret = tdm_layer_set_info(l->layer, &l->info);
1070         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1071
1072         printf("layer %d: output(%d) ", l->idx, l->o->idx);
1073         print_config(&l->info.src_config);
1074         printf(" ! ");
1075         print_pos(&l->info.dst_pos);
1076         printf(" transform(%s)\n", tdm_transform_str(l->info.transform));
1077
1078         ret = tdm_layer_get_available_properties(l->layer, &props, &count);
1079         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1080
1081         LIST_FOR_EACH_ENTRY(w, &l->prop_list, link) {
1082                 for (i = 0; i < count; i++) {
1083                         if (strncmp(w->name, props[i].name, TDM_NAME_LEN))
1084                                 continue;
1085                         ret = tdm_layer_set_property(l->layer, props[i].id, w->value);
1086                         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1087                         printf("\tprop '%s': %d\n", props[i].name, w->value.u32);
1088                         break;
1089                 }
1090         }
1091 }
1092
1093 static tbm_surface_h
1094 pp_get_buffer(tdm_test_server_pp *p)
1095 {
1096         int i, size = TDM_ARRAY_SIZE(p->bufs);
1097         if (!p->bufs[0]) {
1098                 for (i = 0; i < size; i++) {
1099                         int width = (p->info.src_config.size.h)?:p->info.src_config.pos.w;
1100                         int height = (p->info.src_config.size.v)?:p->info.src_config.pos.h;
1101                         unsigned int format = (p->info.src_config.format)?:DEFAULT_FORMAT;
1102                         tbm_surface_h b = buffer_allocate(width, height, format, 0);
1103                         TDM_EXIT_IF_FAIL(b != NULL);
1104                         p->bufs[i] = b;
1105                 }
1106         }
1107         for (i = 0; i < size; i++) {
1108                 tdm_test_server_buffer *tb = get_tts_buffer(p->bufs[i]);
1109
1110                 if (tb == NULL)
1111                         continue;
1112
1113                 if (!tb->in_use) {
1114                         tdm_test_buffer_fill(p->bufs[i], p->data->b_fill);
1115                         return p->bufs[i];
1116                 }
1117         }
1118         printf("no available pp buffer.\n");
1119         exit(0);
1120 }
1121
1122 static void
1123 pp_cb_done(tdm_pp *pp, tbm_surface_h sb, tbm_surface_h db, void *user_data)
1124 {
1125         tdm_test_server_buffer *stb, *dtb;
1126
1127         stb = get_tts_buffer(sb);
1128         TDM_EXIT_IF_FAIL(stb != NULL);
1129
1130         dtb = get_tts_buffer(db);
1131         TDM_EXIT_IF_FAIL(dtb != NULL);
1132
1133         stb->in_use = dtb->in_use = 0;
1134
1135         layer_show_buffer(dtb->l, db);
1136 }
1137
1138 static void
1139 pp_convert_buffer(tdm_test_server_pp *p, tbm_surface_h sb, tbm_surface_h db)
1140 {
1141         tdm_test_server_buffer *stb, *dtb;
1142         tdm_error ret;
1143
1144         stb = get_tts_buffer(sb);
1145         TDM_EXIT_IF_FAIL(stb != NULL);
1146
1147         dtb = get_tts_buffer(db);
1148         TDM_EXIT_IF_FAIL(dtb != NULL);
1149
1150         stb->in_use = dtb->in_use = 1;
1151         dtb->l = p->l;
1152
1153         ret = tdm_pp_attach(p->pp, sb, db);
1154         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1155
1156         ret = tdm_pp_commit(p->pp);
1157         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1158
1159         printf("convt:\tp(%p) sb(%p) db(%p)\n", p, sb, db);
1160 }
1161
1162 /* tdm_event_loop_xxx() function is not for the display server. It's for TDM
1163  * backend module. I use them only for simulating the video play. When we call
1164  * tdm_event_loop_xxx() outside of TDM backend module, we have to lock/unlock
1165  * the TDM display. And when the callback function of tdm_event_loop_xxx() is
1166  * called, the display has been already locked. So in this test application,
1167  * we have to unlock/lock the display for testing.
1168  */
1169 static tdm_error
1170 pp_cb_timeout(void *user_data)
1171 {
1172         tdm_test_server_pp *p = user_data;
1173         tdm_test_server *data = p->l->o->data;
1174         tbm_surface_h sb, db;
1175         tdm_error ret;
1176
1177         tdm_display_unlock(data->display);
1178
1179         sb = pp_get_buffer(p);
1180         TDM_EXIT_IF_FAIL(sb != NULL);
1181         db = layer_get_buffer(p->l);
1182         TDM_EXIT_IF_FAIL(db != NULL);
1183
1184         pp_convert_buffer(p, sb, db);
1185
1186         tdm_display_lock(data->display);
1187         ret = tdm_event_loop_source_timer_update(p->timer_source, 1000 / p->fps);
1188         tdm_display_unlock(data->display);
1189         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1190
1191         tdm_display_lock(data->display);
1192
1193         return TDM_ERROR_NONE;
1194 }
1195
1196 static void
1197 pp_setup(tdm_test_server_pp *p, tbm_surface_h sb, tbm_surface_h db)
1198 {
1199         tdm_test_server *data;
1200         tbm_surface_info_s info;
1201         tdm_error ret;
1202
1203         if (!p || !p->l || !p->l->o || !p->l->o->data) {
1204                 TDM_ERR("invalid parameter failed");
1205                 exit(0);
1206         }
1207         data = p->l->o->data;
1208
1209         p->pp = tdm_display_create_pp(data->display, &ret);
1210         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1211
1212         /* The size and format information should be same with buffer's */
1213         tbm_surface_get_info(sb, &info);
1214         if (IS_RGB(info.format)) {
1215                 p->info.src_config.size.h = info.planes[0].stride >> 2;
1216                 p->info.src_config.size.v = info.height;
1217         } else {
1218                 p->info.src_config.size.h = info.planes[0].stride;
1219                 p->info.src_config.size.v = info.height;
1220         }
1221         p->info.src_config.format = info.format;
1222
1223         /* The size and format information should be same with buffer's */
1224         tbm_surface_get_info(db, &info);
1225         if (IS_RGB(info.format)) {
1226                 p->info.dst_config.size.h = info.planes[0].stride >> 2;
1227                 p->info.dst_config.size.v = info.height;
1228         } else {
1229                 p->info.dst_config.size.h = info.planes[0].stride;
1230                 p->info.dst_config.size.v = info.height;
1231         }
1232         p->info.dst_config.format = info.format;
1233
1234         ret = tdm_pp_set_info(p->pp, &p->info);
1235         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1236
1237         ret = tdm_pp_set_done_handler(p->pp, pp_cb_done, NULL);
1238         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1239
1240         printf("pp: ");
1241         print_config(&p->info.src_config);
1242         printf(" ! ");
1243         print_config(&p->info.dst_config);
1244         printf(" fps(%d) transform(%s)\n", p->fps, tdm_transform_str(p->info.transform));
1245         printf("\toutput_idx(%d) layer_idx(%d)\n", p->l->o->idx, p->l->idx);
1246
1247         layer_setup(p->l, db);
1248
1249         /* tdm_event_loop_xxx() function is not for the display server. It's for TDM
1250          * backend module. I use them only for simulating the video play. When we call
1251          * tdm_event_loop_xxx() outside of TDM backend module, we have to lock/unlock
1252          * the TDM display. And when the callback function of tdm_event_loop_xxx() is
1253          * called, the display has been already locked. So in this test application,
1254          * we have to unlock/lock the display for testing.
1255          */
1256         tdm_display_lock(data->display);
1257         p->timer_source = tdm_event_loop_add_timer_handler(data->display, pp_cb_timeout, p, &ret);
1258         tdm_display_unlock(data->display);
1259         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1260
1261         tdm_display_lock(data->display);
1262         ret = tdm_event_loop_source_timer_update(p->timer_source, 1000 / p->fps);
1263         tdm_display_unlock(data->display);
1264         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1265 }
1266
1267 static void
1268 capture_cb_buffer_done(tbm_surface_h b, void *user_data)
1269 {
1270         tdm_test_server_buffer *tb;
1271
1272         tb = get_tts_buffer(b);
1273         TDM_EXIT_IF_FAIL(tb != NULL);
1274
1275         capture_attach(tb->c, b);
1276 }
1277
1278 static void
1279 capture_cb_done(tdm_capture *capture, tbm_surface_h b, void *user_data)
1280 {
1281         tdm_test_server_buffer *tb;
1282
1283         tb = get_tts_buffer(b);
1284         TDM_EXIT_IF_FAIL(tb != NULL);
1285
1286         tb->in_use = 0;
1287
1288         tb->done = capture_cb_buffer_done;
1289         layer_show_buffer(tb->l, b);
1290 }
1291
1292 static void
1293 capture_attach(tdm_test_server_capture *c, tbm_surface_h b)
1294 {
1295         tdm_error ret;
1296         tdm_test_server_buffer *tb;
1297
1298         tb = get_tts_buffer(b);
1299         TDM_EXIT_IF_FAIL(tb != NULL);
1300
1301         tb->in_use = 1;
1302         tb->l = c->l;
1303         tb->c = c;
1304
1305         ret = tdm_capture_attach(c->capture, b);
1306         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1307
1308         printf("capture:\tc(%p) b(%p)\n", c, b);
1309
1310         ret = tdm_capture_commit(c->capture);
1311         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1312 }
1313
1314 static void
1315 capture_setup(tdm_test_server_capture *c, tbm_surface_h b)
1316 {
1317         tdm_test_server *data;
1318         tdm_output *output;
1319         tdm_layer *layer;
1320         tbm_surface_info_s info;
1321         tdm_error ret;
1322
1323         if (!c || !c->l || !c->l->o || !c->l->o->data) {
1324                 TDM_ERR("invalid parameter failed");
1325                 exit(0);
1326         }
1327         data = c->l->o->data;
1328
1329         if (c->output_idx != -1 && c->layer_idx == -1) {
1330                 output = tdm_display_get_output(data->display, c->output_idx, &ret);
1331                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1332                 c->capture = tdm_output_create_capture(output, &ret);
1333                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1334         } else if (c->output_idx != -1 && c->layer_idx != -1) {
1335                 output = tdm_display_get_output(data->display, c->output_idx, &ret);
1336                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1337                 layer = tdm_output_get_layer(output, c->layer_idx, &ret);
1338                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1339                 c->capture = tdm_layer_create_capture(layer, &ret);
1340                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1341         }
1342
1343         TDM_EXIT_IF_FAIL(c->capture != NULL);
1344
1345         /* The size and format information should be same with buffer's */
1346         tbm_surface_get_info(b, &info);
1347         if (IS_RGB(info.format)) {
1348                 c->info.dst_config.size.h = info.planes[0].stride >> 2;
1349                 c->info.dst_config.size.v = info.height;
1350         } else {
1351                 c->info.dst_config.size.h = info.planes[0].stride;
1352                 c->info.dst_config.size.v = info.height;
1353         }
1354         c->info.dst_config.format = info.format;
1355
1356         ret = tdm_capture_set_info(c->capture, &c->info);
1357         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1358
1359         ret = tdm_capture_set_done_handler(c->capture, capture_cb_done, NULL);
1360         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1361
1362         printf("capture: o(%d) l(%d) ", c->output_idx, c->layer_idx);
1363         print_config(&c->info.dst_config);
1364         printf(" transform(%s)\n", tdm_transform_str(c->info.transform));
1365         printf("\toutput_idx(%d) layer_idx(%d)\n", c->l->o->idx, c->l->idx);
1366
1367         layer_setup(c->l, b);
1368 }
1369
1370 static void
1371 run_test(tdm_test_server *data)
1372 {
1373         tdm_test_server_output *o = NULL;
1374         tdm_test_server_layer *l = NULL;
1375         tdm_test_server_pp *p = NULL;
1376         tdm_test_server_capture *c = NULL;
1377         tdm_display_capability caps;
1378         tdm_error ret;
1379
1380         ret = tdm_display_get_capabilities(data->display, &caps);
1381         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1382
1383         LIST_FOR_EACH_ENTRY(o, &data->output_list, link) {
1384                 LIST_FOR_EACH_ENTRY(l, &o->layer_list, link) {
1385                         if (!l->owner_p && !l->owner_c) {
1386                                 tbm_surface_h b;
1387                                 b = layer_get_buffer(l);
1388                                 if (!l->is_primary || l->o->fill_primary_layer)
1389                                         tdm_test_buffer_fill(b, data->b_fill);
1390                                 layer_setup(l, b);
1391                                 layer_show_buffer(l, b);
1392                         }
1393                 }
1394         }
1395
1396         LIST_FOR_EACH_ENTRY(p, &data->pp_list, link) {
1397                 tbm_surface_h sb, db;
1398                 TDM_GOTO_IF_FAIL(caps & TDM_DISPLAY_CAPABILITY_PP, no_pp);
1399                 sb = pp_get_buffer(p);
1400                 TDM_EXIT_IF_FAIL(sb != NULL);
1401                 db = layer_get_buffer(p->l);
1402                 TDM_EXIT_IF_FAIL(db != NULL);
1403                 pp_setup(p, sb, db);
1404                 pp_convert_buffer(p, sb, db);
1405         }
1406
1407         LIST_FOR_EACH_ENTRY(c, &data->capture_list, link) {
1408                 TDM_GOTO_IF_FAIL(caps & TDM_DISPLAY_CAPABILITY_CAPTURE, no_capture);
1409                 tbm_surface_h b;
1410                 b = layer_get_buffer(c->l);
1411                 capture_setup(c, b);
1412                 capture_attach(c, b);
1413                 b = layer_get_buffer(c->l);
1414                 capture_attach(c, b);
1415                 b = layer_get_buffer(c->l);
1416                 capture_attach(c, b);
1417         }
1418
1419         printf("enter test loop\n");
1420
1421         while (1)
1422                 tdm_display_handle_events(data->display);
1423
1424         return;
1425 no_pp:
1426         printf("no PP capability\n");
1427         exit(0);
1428 no_capture:
1429         printf("no Capture capability\n");
1430         exit(0);
1431 }