correct the wrong behavior of tdm-test-server
[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 buffer;
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         tdm_test_server_buffer *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         tdm_test_server_buffer *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, tdm_test_server_buffer *b);
322 static void capture_attach(tdm_test_server_capture *c, tdm_test_server_buffer *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 tdm_test_server tts_data;
752
753 static void
754 exit_test(int sig)
755 {
756         tdm_test_server *data = &tts_data;
757         tdm_test_server_output *o = NULL, *oo = NULL;
758         tdm_test_server_layer *l = NULL, *ll = NULL;
759         tdm_test_server_pp *p = NULL, *pp = NULL;
760         tdm_test_server_capture *c = NULL, *cc = NULL;
761         tdm_test_server_prop *w = NULL, *ww = NULL;
762         int i;
763
764         printf("got signal: %d\n", sig);
765
766         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &data->output_list, link) {
767                 LIST_DEL(&o->link);
768
769                 LIST_FOR_EACH_ENTRY_SAFE(l, ll, &o->layer_list, link) {
770                         LIST_DEL(&l->link);
771
772                         tdm_layer_unset_buffer(l->layer);
773
774                         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &l->prop_list, link) {
775                                 LIST_DEL(&w->link);
776                                 free(w);
777                         }
778                         for (i = 0; i < TDM_ARRAY_SIZE(l->bufs); i++) {
779                                 tbm_surface_destroy(l->bufs[i]->buffer);
780                                 free(l->bufs[i]);
781                         }
782                         free(l);
783                 }
784
785                 tdm_output_commit(o->output, 0, NULL, NULL);
786
787                 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &o->prop_list, link) {
788                         LIST_DEL(&w->link);
789                         free(w);
790                 }
791
792                 free(o);
793         }
794
795
796         LIST_FOR_EACH_ENTRY_SAFE(p, pp, &data->pp_list, link) {
797                 LIST_DEL(&p->link);
798
799                 tdm_display_lock(data->display);
800                 tdm_event_loop_source_remove(p->timer_source);
801                 tdm_display_unlock(data->display);
802
803                 tdm_pp_destroy(p->pp);
804                 for (i = 0; i < TDM_ARRAY_SIZE(p->bufs); i++) {
805                         tbm_surface_destroy(p->bufs[i]->buffer);
806                         free(p->bufs[i]);
807                 }
808                 free(p);
809         }
810
811         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &data->capture_list, link) {
812                 LIST_DEL(&c->link);
813                 tdm_capture_destroy(c->capture);
814                 free(c);
815         }
816
817         if (data->display)
818                 tdm_display_deinit(data->display);
819
820         exit(0);
821 }
822
823 int
824 main(int argc, char *argv[])
825 {
826         tdm_test_server *data = &tts_data;
827         char temp[TDM_SERVER_REPLY_MSG_LEN];
828         int len = sizeof temp;
829         tdm_error ret;
830
831         signal(SIGINT, exit_test);    /* 2 */
832         signal(SIGTERM, exit_test);   /* 15 */
833
834         memset(data, 0, sizeof * data);
835         LIST_INITHEAD(&data->output_list);
836         LIST_INITHEAD(&data->pp_list);
837         LIST_INITHEAD(&data->capture_list);
838
839         /* init value */
840         data->bflags = TBM_BO_SCANOUT;
841         data->b_fill = PATTERN_SMPTE;
842
843         data->display = tdm_display_init(&ret);
844         TDM_EXIT_IF_FAIL(data->display != NULL);
845
846         parse_args(data, argc, argv);
847         interpret_args(data);
848
849         if (data->do_query) {
850                 tdm_helper_get_display_information(data->display, temp, &len);
851                 printf("%s", temp);
852                 goto done;
853         }
854
855         run_test(data);
856
857 done:
858         tdm_display_deinit(data->display);
859
860         return 0;
861 }
862
863 static void
864 output_setup(tdm_test_server_output *o)
865 {
866         tdm_test_server *data = o->data;
867         const tdm_output_mode *modes, *found = NULL, *best = NULL, *prefer = NULL;
868         tdm_test_server_prop *w = NULL;
869         const tdm_prop *props;
870         int i, count;
871         tdm_error ret;
872
873         o->output = tdm_display_get_output(data->display, o->idx, &ret);
874         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
875
876         ret = tdm_output_get_available_modes(o->output, &modes, &count);
877         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
878
879         for (i = 0; i < count; i++) {
880                 if (!strncmp(o->mode, modes[i].name, TDM_NAME_LEN) && o->refresh == modes[i].vrefresh) {
881                         found = &modes[i];
882                         printf("found mode: %dx%d %d\n", found->hdisplay, found->vdisplay, found->vrefresh);
883                         break;
884                 }
885                 if (!best)
886                         best = &modes[i];
887                 if (modes[i].flags & TDM_OUTPUT_MODE_TYPE_PREFERRED)
888                         prefer = &modes[i];
889         }
890         if (!found && prefer) {
891                 found = prefer;
892                 printf("found prefer mode: %dx%d %d\n", found->hdisplay, found->vdisplay, found->vrefresh);
893         }
894         if (!found && best) {
895                 found = best;
896                 printf("found best mode: %dx%d %d\n", found->hdisplay, found->vdisplay, found->vrefresh);
897         }
898
899         if (!found) {
900                 printf("couldn't find any mode\n");
901                 exit(0);
902         }
903
904         ret = tdm_output_set_mode(o->output, found);
905         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
906
907         printf("output %d: %s %d\n", o->idx, found->name, found->vrefresh);
908
909         ret = tdm_output_get_available_properties(o->output, &props, &count);
910         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
911
912         LIST_FOR_EACH_ENTRY(w, &o->prop_list, link) {
913                 for (i = 0; i < count; i++) {
914                         if (strncmp(w->name, props[i].name, TDM_NAME_LEN))
915                                 continue;
916                         ret = tdm_output_set_property(o->output, props[i].id, w->value);
917                         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
918                         printf("\tprop '%s': %d\n", props[i].name, w->value.u32);
919                         break;
920                 }
921         }
922
923         /* DPMS on forcely at the first time. */
924         ret = tdm_output_set_dpms(o->output, TDM_OUTPUT_DPMS_ON);
925         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
926 }
927
928 static tdm_test_server_buffer*
929 layer_get_buffer(tdm_test_server_layer *l)
930 {
931         int i, size = TDM_ARRAY_SIZE(l->bufs);
932         if (!l->bufs[0]) {
933                 for (i = 0; i < size; i++) {
934                         int width = (l->info.src_config.size.h)?:l->info.src_config.pos.w;
935                         int height = (l->info.src_config.size.v)?:l->info.src_config.pos.h;
936                         unsigned int format = (l->info.src_config.format)?:DEFAULT_FORMAT;
937                         int flags = l->o->data->bflags;
938                         tdm_test_server_buffer *b = calloc(1, sizeof *b);
939                         TDM_EXIT_IF_FAIL(b != NULL);
940                         b->buffer = tbm_surface_internal_create_with_flags(width, height, format, flags);
941                         TDM_EXIT_IF_FAIL(b->buffer != NULL);
942                         tdm_helper_clear_buffer(b->buffer);
943                         l->bufs[i] = b;
944                 }
945         }
946         for (i = 0; i < size; i++)
947                 if (!l->bufs[i]->in_use)
948                         return l->bufs[i];
949         printf("no available layer buffer.\n");
950         exit(0);
951 }
952
953 static void
954 layer_cb_commit(tdm_output *output, unsigned int sequence,
955                                 unsigned int tv_sec, unsigned int tv_usec,
956                                 void *user_data)
957 {
958         tdm_test_server_layer *l = user_data;
959         tdm_test_server_buffer *b = layer_get_buffer(l);
960
961         if (!l->is_primary || l->o->fill_primary_layer)
962                 tdm_test_buffer_fill(b->buffer, l->data->b_fill);
963
964         TDM_EXIT_IF_FAIL(b != NULL);
965
966         if (!l->is_primary || l->o->fill_primary_layer)
967                 layer_show_buffer(l, b);
968 }
969
970 static void
971 layer_cb_buffer_release(tbm_surface_h buffer, void *user_data)
972 {
973         tdm_test_server_buffer *b = user_data;
974         b->in_use = 0;
975         tdm_buffer_remove_release_handler(b->buffer, layer_cb_buffer_release, b);
976         if (b->done)
977                 b->done(buffer, user_data);
978 }
979
980 static void
981 layer_show_buffer(tdm_test_server_layer *l, tdm_test_server_buffer *b)
982 {
983         tdm_test_server *data = l->o->data;
984         tdm_error ret;
985
986         ret = tdm_layer_set_buffer(l->layer, b->buffer);
987         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
988
989         b->in_use = 1;
990         tdm_buffer_add_release_handler(b->buffer, layer_cb_buffer_release, b);
991
992         if (data->do_vblank)
993                 ret = tdm_output_commit(l->o->output, 0, layer_cb_commit, l);
994         else
995                 ret = tdm_output_commit(l->o->output, 0, NULL, NULL);
996         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
997
998         printf("show:\tl(%p) b(%p)\n", l, b);
999 }
1000
1001 static void
1002 layer_setup(tdm_test_server_layer *l, tdm_test_server_buffer *b)
1003 {
1004         tdm_test_server_prop *w = NULL;
1005         const tdm_prop *props;
1006         int i, count;
1007         tdm_error ret;
1008         tbm_surface_info_s info;
1009
1010         l->layer = tdm_output_get_layer(l->o->output, l->idx, &ret);
1011         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1012
1013         /* The size and format information should be same with buffer's */
1014         tbm_surface_get_info(b->buffer, &info);
1015         if (IS_RGB(info.format)) {
1016                 l->info.src_config.size.h = info.planes[0].stride >> 2;
1017                 l->info.src_config.size.v = info.height;
1018         } else {
1019                 l->info.src_config.size.h = info.planes[0].stride;
1020                 l->info.src_config.size.v = info.height;
1021         }
1022         l->info.src_config.format = info.format;
1023
1024         ret = tdm_layer_set_info(l->layer, &l->info);
1025         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1026
1027         printf("layer %d: output(%d) ", l->idx, l->o->idx);
1028         print_config(&l->info.src_config);
1029         printf(" ! ");
1030         print_pos(&l->info.dst_pos);
1031         printf(" transform(%s)\n", tdm_transform_str(l->info.transform));
1032
1033         ret = tdm_layer_get_available_properties(l->layer, &props, &count);
1034         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1035
1036         LIST_FOR_EACH_ENTRY(w, &l->prop_list, link) {
1037                 for (i = 0; i < count; i++) {
1038                         if (strncmp(w->name, props[i].name, TDM_NAME_LEN))
1039                                 continue;
1040                         ret = tdm_layer_set_property(l->layer, props[i].id, w->value);
1041                         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1042                         printf("\tprop '%s': %d\n", props[i].name, w->value.u32);
1043                         break;
1044                 }
1045         }
1046 }
1047
1048 static tdm_test_server_buffer*
1049 pp_get_buffer(tdm_test_server_pp *p)
1050 {
1051         int i, size = TDM_ARRAY_SIZE(p->bufs);
1052         if (!p->bufs[0]) {
1053                 for (i = 0; i < size; i++) {
1054                         int width = (p->info.src_config.size.h)?:p->info.src_config.pos.w;
1055                         int height = (p->info.src_config.size.v)?:p->info.src_config.pos.h;
1056                         unsigned int format = (p->info.src_config.format)?:DEFAULT_FORMAT;
1057                         tdm_test_server_buffer *b = calloc(1, sizeof *b);
1058                         TDM_EXIT_IF_FAIL(b != NULL);
1059                         b->buffer = tbm_surface_create(width, height, format);
1060                         TDM_EXIT_IF_FAIL(b->buffer != NULL);
1061                         tdm_helper_clear_buffer(b->buffer);
1062                         p->bufs[i] = b;
1063                 }
1064         }
1065         for (i = 0; i < size; i++) {
1066                 if (!p->bufs[i]->in_use) {
1067                         tdm_test_buffer_fill(p->bufs[i]->buffer, p->data->b_fill);
1068                         return p->bufs[i];
1069                 }
1070         }
1071         printf("no available pp buffer.\n");
1072         exit(0);
1073 }
1074
1075 static void
1076 pp_cb_sb_release(tbm_surface_h buffer, void *user_data)
1077 {
1078         tdm_test_server_buffer *b = user_data;
1079         b->in_use = 0;
1080         tdm_buffer_remove_release_handler(b->buffer, pp_cb_sb_release, b);
1081 }
1082
1083 static void
1084 pp_cb_db_release(tbm_surface_h buffer, void *user_data)
1085 {
1086         tdm_test_server_buffer *b = user_data;
1087         b->in_use = 0;
1088         tdm_buffer_remove_release_handler(b->buffer, pp_cb_db_release, b);
1089         layer_show_buffer(b->l, b);
1090 }
1091
1092 static void
1093 pp_convert_buffer(tdm_test_server_pp *p, tdm_test_server_buffer *sb, tdm_test_server_buffer *db)
1094 {
1095         tdm_error ret;
1096
1097         ret = tdm_pp_attach(p->pp, sb->buffer, db->buffer);
1098         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1099
1100         sb->in_use = db->in_use = 1;
1101         db->l = p->l;
1102         tdm_buffer_add_release_handler(sb->buffer, pp_cb_sb_release, sb);
1103         tdm_buffer_add_release_handler(db->buffer, pp_cb_db_release, db);
1104
1105         ret = tdm_pp_commit(p->pp);
1106         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1107
1108         printf("convt:\tp(%p) sb(%p) db(%p)\n", p, sb, db);
1109 }
1110
1111 /* tdm_event_loop_xxx() function is not for the display server. It's for TDM
1112  * backend module. I use them only for simulating the video play. When we call
1113  * tdm_event_loop_xxx() outside of TDM backend module, we have to lock/unlock
1114  * the TDM display. And when the callback function of tdm_event_loop_xxx() is
1115  * called, the display has been already locked. So in this test application,
1116  * we have to unlock/lock the display for testing.
1117  */
1118 static tdm_error
1119 pp_cb_timeout(void *user_data)
1120 {
1121         tdm_test_server_pp *p = user_data;
1122         tdm_test_server *data = p->l->o->data;
1123         tdm_test_server_buffer *sb, *db;
1124         tdm_error ret;
1125
1126         tdm_display_unlock(data->display);
1127
1128         sb = pp_get_buffer(p);
1129         TDM_EXIT_IF_FAIL(sb != NULL);
1130         db = layer_get_buffer(p->l);
1131         TDM_EXIT_IF_FAIL(db != NULL);
1132
1133         pp_convert_buffer(p, sb, db);
1134
1135         tdm_display_lock(data->display);
1136         ret = tdm_event_loop_source_timer_update(p->timer_source, 1000 / p->fps);
1137         tdm_display_unlock(data->display);
1138         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1139
1140         tdm_display_lock(data->display);
1141
1142         return TDM_ERROR_NONE;
1143 }
1144
1145 static void
1146 pp_setup(tdm_test_server_pp *p, tdm_test_server_buffer *sb, tdm_test_server_buffer *db)
1147 {
1148         tdm_test_server *data = p->l->o->data;
1149         tbm_surface_info_s info;
1150         tdm_error ret;
1151
1152         p->pp = tdm_display_create_pp(data->display, &ret);
1153         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1154
1155         /* The size and format information should be same with buffer's */
1156         tbm_surface_get_info(sb->buffer, &info);
1157         if (IS_RGB(info.format)) {
1158                 p->info.src_config.size.h = info.planes[0].stride >> 2;
1159                 p->info.src_config.size.v = info.height;
1160         } else {
1161                 p->info.src_config.size.h = info.planes[0].stride;
1162                 p->info.src_config.size.v = info.height;
1163         }
1164         p->info.src_config.format = info.format;
1165
1166         /* The size and format information should be same with buffer's */
1167         tbm_surface_get_info(db->buffer, &info);
1168         if (IS_RGB(info.format)) {
1169                 p->info.dst_config.size.h = info.planes[0].stride >> 2;
1170                 p->info.dst_config.size.v = info.height;
1171         } else {
1172                 p->info.dst_config.size.h = info.planes[0].stride;
1173                 p->info.dst_config.size.v = info.height;
1174         }
1175         p->info.dst_config.format = info.format;
1176
1177         ret = tdm_pp_set_info(p->pp, &p->info);
1178         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1179
1180         printf("pp: ");
1181         print_config(&p->info.src_config);
1182         printf(" ! ");
1183         print_config(&p->info.dst_config);
1184         printf(" fps(%d) transform(%s)\n", p->fps, tdm_transform_str(p->info.transform));
1185         if (p->l)
1186                 printf("\toutput_idx(%d) layer_idx(%d)\n", p->l->o->idx, p->l->idx);
1187
1188         layer_setup(p->l, db);
1189
1190         /* tdm_event_loop_xxx() function is not for the display server. It's for TDM
1191          * backend module. I use them only for simulating the video play. When we call
1192          * tdm_event_loop_xxx() outside of TDM backend module, we have to lock/unlock
1193          * the TDM display. And when the callback function of tdm_event_loop_xxx() is
1194          * called, the display has been already locked. So in this test application,
1195          * we have to unlock/lock the display for testing.
1196          */
1197         tdm_display_lock(data->display);
1198         p->timer_source = tdm_event_loop_add_timer_handler(data->display, pp_cb_timeout, p, &ret);
1199         tdm_display_unlock(data->display);
1200         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1201
1202         tdm_display_lock(data->display);
1203         ret = tdm_event_loop_source_timer_update(p->timer_source, 1000 / p->fps);
1204         tdm_display_unlock(data->display);
1205         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1206 }
1207
1208 static void
1209 capture_cb_buffer_done(tbm_surface_h buffer, void *user_data)
1210 {
1211         tdm_test_server_buffer *b = user_data;
1212         capture_attach(b->c, b);
1213 }
1214
1215 static void
1216 capture_cb_buffer_release(tbm_surface_h buffer, void *user_data)
1217 {
1218         tdm_test_server_buffer *b = user_data;
1219         b->in_use = 0;
1220         tdm_buffer_remove_release_handler(b->buffer, capture_cb_buffer_release, b);
1221         b->done = capture_cb_buffer_done;
1222         layer_show_buffer(b->l, b);
1223 }
1224
1225 static void
1226 capture_attach(tdm_test_server_capture *c, tdm_test_server_buffer *b)
1227 {
1228         tdm_error ret;
1229
1230         ret = tdm_capture_attach(c->capture, b->buffer);
1231         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1232
1233         b->in_use = 1;
1234         b->l = c->l;
1235         b->c = c;
1236         tdm_buffer_add_release_handler(b->buffer, capture_cb_buffer_release, b);
1237         printf("capture:\tc(%p) b(%p)\n", c, b);
1238
1239         ret = tdm_capture_commit(c->capture);
1240         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1241 }
1242
1243 static void
1244 capture_setup(tdm_test_server_capture *c, tdm_test_server_buffer *b)
1245 {
1246         tdm_test_server *data = c->l->o->data;
1247         tdm_output *output;
1248         tdm_layer *layer;
1249         tbm_surface_info_s info;
1250         tdm_error ret;
1251
1252         if (c->output_idx != -1 && c->layer_idx == -1) {
1253                 output = tdm_display_get_output(data->display, c->output_idx, &ret);
1254                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1255                 c->capture = tdm_output_create_capture(output, &ret);
1256                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1257         } else if (c->output_idx != -1 && c->layer_idx != -1) {
1258                 output = tdm_display_get_output(data->display, c->output_idx, &ret);
1259                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1260                 layer = tdm_output_get_layer(output, c->layer_idx, &ret);
1261                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1262                 c->capture = tdm_layer_create_capture(layer, &ret);
1263                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1264         }
1265
1266         TDM_EXIT_IF_FAIL(c->capture != NULL);
1267
1268         /* The size and format information should be same with buffer's */
1269         tbm_surface_get_info(b->buffer, &info);
1270         if (IS_RGB(info.format)) {
1271                 c->info.dst_config.size.h = info.planes[0].stride >> 2;
1272                 c->info.dst_config.size.v = info.height;
1273         } else {
1274                 c->info.dst_config.size.h = info.planes[0].stride;
1275                 c->info.dst_config.size.v = info.height;
1276         }
1277         c->info.dst_config.format = info.format;
1278
1279         ret = tdm_capture_set_info(c->capture, &c->info);
1280         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1281
1282         printf("capture: o(%d) l(%d) ", c->output_idx, c->layer_idx);
1283         print_config(&c->info.dst_config);
1284         printf(" transform(%s)\n", tdm_transform_str(c->info.transform));
1285         if (c->l)
1286                 printf("\toutput_idx(%d) layer_idx(%d)\n", c->l->o->idx, c->l->idx);
1287
1288         layer_setup(c->l, b);
1289 }
1290
1291 static void
1292 run_test(tdm_test_server *data)
1293 {
1294         tdm_test_server_output *o = NULL;
1295         tdm_test_server_layer *l = NULL;
1296         tdm_test_server_pp *p = NULL;
1297         tdm_test_server_capture *c = NULL;
1298         tdm_display_capability caps;
1299         tdm_error ret;
1300
1301         ret = tdm_display_get_capabilities(data->display, &caps);
1302         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1303
1304         LIST_FOR_EACH_ENTRY(o, &data->output_list, link) {
1305                 LIST_FOR_EACH_ENTRY(l, &o->layer_list, link) {
1306                         if (!l->owner_p && !l->owner_c) {
1307                                 tdm_test_server_buffer *b;
1308                                 b = layer_get_buffer(l);
1309                                 if (!l->is_primary || l->o->fill_primary_layer)
1310                                         tdm_test_buffer_fill(b->buffer, data->b_fill);
1311                                 layer_setup(l, b);
1312                                 layer_show_buffer(l, b);
1313                         }
1314                 }
1315         }
1316
1317         LIST_FOR_EACH_ENTRY(p, &data->pp_list, link) {
1318                 tdm_test_server_buffer *sb, *db;
1319                 TDM_GOTO_IF_FAIL(caps & TDM_DISPLAY_CAPABILITY_PP, no_pp);
1320                 sb = pp_get_buffer(p);
1321                 TDM_EXIT_IF_FAIL(sb != NULL);
1322                 db = layer_get_buffer(p->l);
1323                 TDM_EXIT_IF_FAIL(db != NULL);
1324                 pp_setup(p, sb, db);
1325                 pp_convert_buffer(p, sb, db);
1326         }
1327
1328         LIST_FOR_EACH_ENTRY(c, &data->capture_list, link) {
1329                 TDM_GOTO_IF_FAIL(caps & TDM_DISPLAY_CAPABILITY_CAPTURE, no_capture);
1330                 tdm_test_server_buffer *b;
1331                 b = layer_get_buffer(c->l);
1332                 capture_setup(c, b);
1333                 capture_attach(c, b);
1334                 b = layer_get_buffer(c->l);
1335                 capture_attach(c, b);
1336                 b = layer_get_buffer(c->l);
1337                 capture_attach(c, b);
1338         }
1339
1340         printf("enter test loop\n");
1341
1342         while (1)
1343                 tdm_display_handle_events(data->display);
1344
1345         return;
1346 no_pp:
1347         printf("no PP capability\n");
1348         exit(0);
1349 no_capture:
1350         printf("no Capture capability\n");
1351         exit(0);
1352 }