59a03ca813f2a47f13a2c5d1eb2e0344719d3b18
[profile/ivi/mesa.git] / src / gallium / state_trackers / xorg / xvmc / tests / testlib.c
1 #include "testlib.h"
2 #include <stdio.h>
3
4 /*
5 void test(int pred, const char *pred_string, const char *doc_string, const char *file, unsigned int line)
6 {
7         fputs(doc_string, stderr);
8         if (!pred)
9                 fprintf(stderr, " FAIL!\n\t\"%s\" at %s:%u\n", pred_string, file, line);
10         else
11                 fputs(" PASS!\n", stderr);
12 }
13 */
14
15 int GetPort
16 (
17         Display *display,
18         unsigned int width,
19         unsigned int height,
20         unsigned int chroma_format,
21         const unsigned int *mc_types,
22         unsigned int num_mc_types,
23         XvPortID *port_id,
24         int *surface_type_id,
25         unsigned int *is_overlay,
26         unsigned int *intra_unsigned
27 )
28 {
29         unsigned int    found_port = 0;
30         XvAdaptorInfo   *adaptor_info;
31         unsigned int    num_adaptors;
32         int             num_types;
33         int             ev_base, err_base;
34         unsigned int    i, j, k, l;
35
36         if (!XvMCQueryExtension(display, &ev_base, &err_base))
37                 return 0;
38         if (XvQueryAdaptors(display, XDefaultRootWindow(display), &num_adaptors, &adaptor_info) != Success)
39                 return 0;
40
41         for (i = 0; i < num_adaptors && !found_port; ++i)
42         {
43                 if (adaptor_info[i].type & XvImageMask)
44                 {
45                         XvMCSurfaceInfo *surface_info = XvMCListSurfaceTypes(display, adaptor_info[i].base_id, &num_types);
46
47                         if (surface_info)
48                         {
49                                 for (j = 0; j < num_types && !found_port; ++j)
50                                 {
51                                         if
52                                         (
53                                                 surface_info[j].chroma_format == chroma_format &&
54                                                 surface_info[j].max_width >= width &&
55                                                 surface_info[j].max_height >= height
56                                         )
57                                         {
58                                                 for (k = 0; k < num_mc_types && !found_port; ++k)
59                                                 {
60                                                         if (surface_info[j].mc_type == mc_types[k])
61                                                         {
62                                                                 for (l = 0; l < adaptor_info[i].num_ports && !found_port; ++l)
63                                                                 {
64                                                                         if (XvGrabPort(display, adaptor_info[i].base_id + l, CurrentTime) == Success)
65                                                                         {
66                                                                                 *port_id = adaptor_info[i].base_id + l;
67                                                                                 *surface_type_id = surface_info[j].surface_type_id;
68                                                                                 *is_overlay = surface_info[j].flags & XVMC_OVERLAID_SURFACE;
69                                                                                 *intra_unsigned = surface_info[j].flags & XVMC_INTRA_UNSIGNED;
70                                                                                 found_port = 1;
71                                                                         }
72                                                                 }
73                                                         }
74                                                 }
75                                         }
76                                 }
77
78                                 XFree(surface_info);
79                         }
80                 }
81         }
82
83         XvFreeAdaptorInfo(adaptor_info);
84
85         return found_port;
86 }
87
88 unsigned int align(unsigned int value, unsigned int alignment)
89 {
90         return (value + alignment - 1) & ~(alignment - 1);
91 }
92
93 /* From the glibc manual */
94 int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
95 {
96         /* Perform the carry for the later subtraction by updating y. */
97         if (x->tv_usec < y->tv_usec)
98         {
99                 int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
100                 y->tv_usec -= 1000000 * nsec;
101                 y->tv_sec += nsec;
102         }
103         if (x->tv_usec - y->tv_usec > 1000000)
104         {
105                 int nsec = (x->tv_usec - y->tv_usec) / 1000000;
106                 y->tv_usec += 1000000 * nsec;
107                 y->tv_sec -= nsec;
108         }
109
110         /*
111          * Compute the time remaining to wait.
112          * tv_usec is certainly positive.
113          */
114         result->tv_sec = x->tv_sec - y->tv_sec;
115         result->tv_usec = x->tv_usec - y->tv_usec;
116
117         /* Return 1 if result is negative. */
118         return x->tv_sec < y->tv_sec;
119 }