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 TUNGSTEN GRAPHICS 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
31/*
32void test(int pred, const char *pred_string, const char *doc_string, const char *file, unsigned int line)
33{
34	fputs(doc_string, stderr);
35	if (!pred)
36		fprintf(stderr, " FAIL!\n\t\"%s\" at %s:%u\n", pred_string, file, line);
37	else
38		fputs(" PASS!\n", stderr);
39}
40*/
41
42int GetPort
43(
44	Display *display,
45	unsigned int width,
46	unsigned int height,
47	unsigned int chroma_format,
48	const unsigned int *mc_types,
49	unsigned int num_mc_types,
50	XvPortID *port_id,
51	int *surface_type_id,
52	unsigned int *is_overlay,
53	unsigned int *intra_unsigned
54)
55{
56	unsigned int	found_port = 0;
57	XvAdaptorInfo	*adaptor_info;
58	unsigned int	num_adaptors;
59	int		num_types;
60	int		ev_base, err_base;
61	unsigned int	i, j, k, l;
62
63	if (!XvMCQueryExtension(display, &ev_base, &err_base))
64		return 0;
65	if (XvQueryAdaptors(display, XDefaultRootWindow(display), &num_adaptors, &adaptor_info) != Success)
66		return 0;
67
68	for (i = 0; i < num_adaptors && !found_port; ++i)
69	{
70		if (adaptor_info[i].type & XvImageMask)
71		{
72			XvMCSurfaceInfo *surface_info = XvMCListSurfaceTypes(display, adaptor_info[i].base_id, &num_types);
73
74			if (surface_info)
75			{
76				for (j = 0; j < num_types && !found_port; ++j)
77				{
78					if
79					(
80						surface_info[j].chroma_format == chroma_format &&
81						surface_info[j].max_width >= width &&
82						surface_info[j].max_height >= height
83					)
84					{
85						for (k = 0; k < num_mc_types && !found_port; ++k)
86						{
87							if (surface_info[j].mc_type == mc_types[k])
88							{
89								for (l = 0; l < adaptor_info[i].num_ports && !found_port; ++l)
90								{
91									if (XvGrabPort(display, adaptor_info[i].base_id + l, CurrentTime) == Success)
92									{
93										*port_id = adaptor_info[i].base_id + l;
94										*surface_type_id = surface_info[j].surface_type_id;
95										*is_overlay = surface_info[j].flags & XVMC_OVERLAID_SURFACE;
96										*intra_unsigned = surface_info[j].flags & XVMC_INTRA_UNSIGNED;
97										found_port = 1;
98									}
99								}
100							}
101						}
102					}
103				}
104
105				XFree(surface_info);
106			}
107		}
108	}
109
110	XvFreeAdaptorInfo(adaptor_info);
111
112	return found_port;
113}
114
115unsigned int align(unsigned int value, unsigned int alignment)
116{
117	return (value + alignment - 1) & ~(alignment - 1);
118}
119
120/* From the glibc manual */
121int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
122{
123	/* Perform the carry for the later subtraction by updating y. */
124	if (x->tv_usec < y->tv_usec)
125	{
126		int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
127		y->tv_usec -= 1000000 * nsec;
128		y->tv_sec += nsec;
129	}
130	if (x->tv_usec - y->tv_usec > 1000000)
131	{
132		int nsec = (x->tv_usec - y->tv_usec) / 1000000;
133		y->tv_usec += 1000000 * nsec;
134		y->tv_sec -= nsec;
135	}
136
137	/*
138	 * Compute the time remaining to wait.
139	 * tv_usec is certainly positive.
140	 */
141	result->tv_sec = x->tv_sec - y->tv_sec;
142	result->tv_usec = x->tv_usec - y->tv_usec;
143
144	/* Return 1 if result is negative. */
145	return x->tv_sec < y->tv_sec;
146}
147