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