1 /**************************************************************************
2  *
3  * Copyright 2009 Younes Manton.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #include "testlib.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31 
32 /*
33 void test(int pred, const char *pred_string, const char *doc_string, const char *file, unsigned int line)
34 {
35 	fputs(doc_string, stderr);
36 	if (!pred)
37 		fprintf(stderr, " FAIL!\n\t\"%s\" at %s:%u\n", pred_string, file, line);
38 	else
39 		fputs(" PASS!\n", stderr);
40 }
41 */
42 
GetPort(Display * display,unsigned int width,unsigned int height,unsigned int chroma_format,const unsigned int * mc_types,unsigned int num_mc_types,XvPortID * port_id,int * surface_type_id,unsigned int * is_overlay,unsigned int * intra_unsigned)43 int GetPort
44 (
45 	Display *display,
46 	unsigned int width,
47 	unsigned int height,
48 	unsigned int chroma_format,
49 	const unsigned int *mc_types,
50 	unsigned int num_mc_types,
51 	XvPortID *port_id,
52 	int *surface_type_id,
53 	unsigned int *is_overlay,
54 	unsigned int *intra_unsigned
55 )
56 {
57 	unsigned int	found_port = 0;
58 	XvAdaptorInfo	*adaptor_info;
59 	unsigned int	num_adaptors;
60 	int		num_types;
61 	int		ev_base, err_base;
62 	unsigned int	i, j, k, l;
63 
64 	if (!XvMCQueryExtension(display, &ev_base, &err_base))
65 		return 0;
66 	if (XvQueryAdaptors(display, XDefaultRootWindow(display), &num_adaptors, &adaptor_info) != Success)
67 		return 0;
68 
69 	for (i = 0; i < num_adaptors && !found_port; ++i)
70 	{
71 		if (adaptor_info[i].type & XvImageMask)
72 		{
73 			XvMCSurfaceInfo *surface_info = XvMCListSurfaceTypes(display, adaptor_info[i].base_id, &num_types);
74 
75 			if (surface_info)
76 			{
77 				for (j = 0; j < num_types && !found_port; ++j)
78 				{
79 					if
80 					(
81 						surface_info[j].chroma_format == chroma_format &&
82 						surface_info[j].max_width >= width &&
83 						surface_info[j].max_height >= height
84 					)
85 					{
86 						for (k = 0; k < num_mc_types && !found_port; ++k)
87 						{
88 							if ((surface_info[j].mc_type & mc_types[k]) == mc_types[k])
89 							{
90 								for (l = 0; l < adaptor_info[i].num_ports && !found_port; ++l)
91 								{
92 									if (XvGrabPort(display, adaptor_info[i].base_id + l, CurrentTime) == Success)
93 									{
94 										*port_id = adaptor_info[i].base_id + l;
95 										*surface_type_id = surface_info[j].surface_type_id;
96 										*is_overlay = surface_info[j].flags & XVMC_OVERLAID_SURFACE;
97 										*intra_unsigned = surface_info[j].flags & XVMC_INTRA_UNSIGNED;
98 										found_port = 1;
99 									}
100 								}
101 							}
102 						}
103 					}
104 				}
105 
106 				free(surface_info);
107 			}
108 		}
109 	}
110 
111 	XvFreeAdaptorInfo(adaptor_info);
112 
113 	return found_port;
114 }
115 
align(unsigned int value,unsigned int alignment)116 unsigned int align(unsigned int value, unsigned int alignment)
117 {
118 	return (value + alignment - 1) & ~(alignment - 1);
119 }
120 
121 /* From the glibc manual */
timeval_subtract(struct timeval * result,struct timeval * x,struct timeval * y)122 int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
123 {
124 	/* Perform the carry for the later subtraction by updating y. */
125 	if (x->tv_usec < y->tv_usec)
126 	{
127 		int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
128 		y->tv_usec -= 1000000 * nsec;
129 		y->tv_sec += nsec;
130 	}
131 	if (x->tv_usec - y->tv_usec > 1000000)
132 	{
133 		int nsec = (x->tv_usec - y->tv_usec) / 1000000;
134 		y->tv_usec += 1000000 * nsec;
135 		y->tv_sec -= nsec;
136 	}
137 
138 	/*
139 	 * Compute the time remaining to wait.
140 	 * tv_usec is certainly positive.
141 	 */
142 	result->tv_sec = x->tv_sec - y->tv_sec;
143 	result->tv_usec = x->tv_usec - y->tv_usec;
144 
145 	/* Return 1 if result is negative. */
146 	return x->tv_sec < y->tv_sec;
147 }
148