1/* Copyright © 2000 Keith Packard
2 *
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation, and that the name of Keith Packard not be used in
8 * advertising or publicity pertaining to distribution of the software without
9 * specific, written prior permission.  Keith Packard makes no
10 * representations about the suitability of this software for any purpose.  It
11 * is provided "as is" without express or implied warranty.
12 *
13 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
14 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
15 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
16 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
17 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
18 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19 * PERFORMANCE OF THIS SOFTWARE.
20 */
21
22#include "xcb_renderutil.h"
23
24xcb_render_pictvisual_t *
25xcb_render_util_find_visual_format (const xcb_render_query_pict_formats_reply_t *formats,
26			       const xcb_visualid_t visual)
27{
28    xcb_render_pictscreen_iterator_t screens;
29    xcb_render_pictdepth_iterator_t depths;
30    xcb_render_pictvisual_iterator_t visuals;
31    if (!formats)
32	return 0;
33    for (screens = xcb_render_query_pict_formats_screens_iterator(formats); screens.rem; xcb_render_pictscreen_next(&screens))
34	for (depths = xcb_render_pictscreen_depths_iterator(screens.data); depths.rem; xcb_render_pictdepth_next(&depths))
35	    for (visuals = xcb_render_pictdepth_visuals_iterator(depths.data); visuals.rem; xcb_render_pictvisual_next(&visuals))
36		if (visuals.data->visual == visual)
37		    return visuals.data;
38    return 0;
39}
40
41xcb_render_pictforminfo_t *
42xcb_render_util_find_format (const xcb_render_query_pict_formats_reply_t	*formats,
43			 unsigned long				mask,
44			 const xcb_render_pictforminfo_t		*template,
45			 int					count)
46{
47    xcb_render_pictforminfo_iterator_t i;
48    if (!formats)
49	return 0;
50    for (i = xcb_render_query_pict_formats_formats_iterator(formats); i.rem; xcb_render_pictforminfo_next(&i))
51    {
52	if (mask & XCB_PICT_FORMAT_ID)
53	    if (template->id != i.data->id)
54		continue;
55	if (mask & XCB_PICT_FORMAT_TYPE)
56	    if (template->type != i.data->type)
57		continue;
58	if (mask & XCB_PICT_FORMAT_DEPTH)
59	    if (template->depth != i.data->depth)
60		continue;
61	if (mask & XCB_PICT_FORMAT_RED)
62	    if (template->direct.red_shift != i.data->direct.red_shift)
63		continue;
64	if (mask & XCB_PICT_FORMAT_RED_MASK)
65	    if (template->direct.red_mask != i.data->direct.red_mask)
66		continue;
67	if (mask & XCB_PICT_FORMAT_GREEN)
68	    if (template->direct.green_shift != i.data->direct.green_shift)
69		continue;
70	if (mask & XCB_PICT_FORMAT_GREEN_MASK)
71	    if (template->direct.green_mask != i.data->direct.green_mask)
72		continue;
73	if (mask & XCB_PICT_FORMAT_BLUE)
74	    if (template->direct.blue_shift != i.data->direct.blue_shift)
75		continue;
76	if (mask & XCB_PICT_FORMAT_BLUE_MASK)
77	    if (template->direct.blue_mask != i.data->direct.blue_mask)
78		continue;
79	if (mask & XCB_PICT_FORMAT_ALPHA)
80	    if (template->direct.alpha_shift != i.data->direct.alpha_shift)
81		continue;
82	if (mask & XCB_PICT_FORMAT_ALPHA_MASK)
83	    if (template->direct.alpha_mask != i.data->direct.alpha_mask)
84		continue;
85	if (mask & XCB_PICT_FORMAT_COLORMAP)
86	    if (template->colormap != i.data->colormap)
87		continue;
88	if (count-- == 0)
89	    return i.data;
90    }
91    return 0;
92}
93
94xcb_render_pictforminfo_t *
95xcb_render_util_find_standard_format (const xcb_render_query_pict_formats_reply_t	*formats,
96				 xcb_pict_standard_t					format)
97{
98    static const struct {
99	xcb_render_pictforminfo_t templ;
100	unsigned long	      mask;
101    } standardFormats[] = {
102	/* XCB_PICT_STANDARD_ARGB_32 */
103	{
104	    {
105		0,			    /* id */
106		XCB_RENDER_PICT_TYPE_DIRECT,    /* type */
107		32,			    /* depth */
108		{ 0 },			    /* pad */
109		{			    /* direct */
110		    16,			    /* direct.red */
111		    0xff,		    /* direct.red_mask */
112		    8,			    /* direct.green */
113		    0xff,		    /* direct.green_mask */
114		    0,			    /* direct.blue */
115		    0xff,		    /* direct.blue_mask */
116		    24,			    /* direct.alpha */
117		    0xff,		    /* direct.alpha_mask */
118		},
119		0,			    /* colormap */
120	    },
121	    XCB_PICT_FORMAT_TYPE |
122	    XCB_PICT_FORMAT_DEPTH |
123	    XCB_PICT_FORMAT_RED |
124	    XCB_PICT_FORMAT_RED_MASK |
125	    XCB_PICT_FORMAT_GREEN |
126	    XCB_PICT_FORMAT_GREEN_MASK |
127	    XCB_PICT_FORMAT_BLUE |
128	    XCB_PICT_FORMAT_BLUE_MASK |
129	    XCB_PICT_FORMAT_ALPHA |
130	    XCB_PICT_FORMAT_ALPHA_MASK,
131	},
132	/* XCB_PICT_STANDARD_RGB_24 */
133	{
134	    {
135		0,			    /* id */
136		XCB_RENDER_PICT_TYPE_DIRECT,    /* type */
137		24,			    /* depth */
138		{ 0 },			    /* pad */
139		{			    /* direct */
140		    16,			    /* direct.red */
141		    0xff,		    /* direct.red_MASK */
142		    8,			    /* direct.green */
143		    0xff,		    /* direct.green_MASK */
144		    0,			    /* direct.blue */
145		    0xff,		    /* direct.blue_MASK */
146		    0,			    /* direct.alpha */
147		    0x00,		    /* direct.alpha_MASK */
148		},
149		0,			    /* colormap */
150	    },
151	    XCB_PICT_FORMAT_TYPE |
152	    XCB_PICT_FORMAT_DEPTH |
153	    XCB_PICT_FORMAT_RED |
154	    XCB_PICT_FORMAT_RED_MASK |
155	    XCB_PICT_FORMAT_GREEN |
156	    XCB_PICT_FORMAT_GREEN_MASK |
157	    XCB_PICT_FORMAT_BLUE |
158	    XCB_PICT_FORMAT_BLUE_MASK |
159	    XCB_PICT_FORMAT_ALPHA_MASK,
160	},
161	/* XCB_PICT_STANDARD_A_8 */
162	{
163	    {
164		0,			    /* id */
165		XCB_RENDER_PICT_TYPE_DIRECT,    /* type */
166		8,			    /* depth */
167		{ 0 },			    /* pad */
168		{			    /* direct */
169		    0,			    /* direct.red */
170		    0x00,		    /* direct.red_MASK */
171		    0,			    /* direct.green */
172		    0x00,		    /* direct.green_MASK */
173		    0,			    /* direct.blue */
174		    0x00,		    /* direct.blue_MASK */
175		    0,			    /* direct.alpha */
176		    0xff,		    /* direct.alpha_MASK */
177		},
178		0,			    /* colormap */
179	    },
180	    XCB_PICT_FORMAT_TYPE |
181	    XCB_PICT_FORMAT_DEPTH |
182	    XCB_PICT_FORMAT_RED_MASK |
183	    XCB_PICT_FORMAT_GREEN_MASK |
184	    XCB_PICT_FORMAT_BLUE_MASK |
185	    XCB_PICT_FORMAT_ALPHA |
186	    XCB_PICT_FORMAT_ALPHA_MASK,
187	},
188	/* XCB_PICT_STANDARD_A_4 */
189	{
190	    {
191		0,			    /* id */
192		XCB_RENDER_PICT_TYPE_DIRECT,    /* type */
193		4,			    /* depth */
194		{ 0 },			    /* pad */
195		{			    /* direct */
196		    0,			    /* direct.red */
197		    0x00,		    /* direct.red_MASK */
198		    0,			    /* direct.green */
199		    0x00,		    /* direct.green_MASK */
200		    0,			    /* direct.blue */
201		    0x00,		    /* direct.blue_MASK */
202		    0,			    /* direct.alpha */
203		    0x0f,		    /* direct.alpha_MASK */
204		},
205		0,			    /* colormap */
206	    },
207	    XCB_PICT_FORMAT_TYPE |
208	    XCB_PICT_FORMAT_DEPTH |
209	    XCB_PICT_FORMAT_RED_MASK |
210	    XCB_PICT_FORMAT_GREEN_MASK |
211	    XCB_PICT_FORMAT_BLUE_MASK |
212	    XCB_PICT_FORMAT_ALPHA |
213	    XCB_PICT_FORMAT_ALPHA_MASK,
214	},
215	/* XCB_PICT_STANDARD_A_1 */
216	{
217	    {
218		0,			    /* id */
219		XCB_RENDER_PICT_TYPE_DIRECT,    /* type */
220		1,			    /* depth */
221		{ 0 },			    /* pad */
222		{			    /* direct */
223		    0,			    /* direct.red */
224		    0x00,		    /* direct.red_MASK */
225		    0,			    /* direct.green */
226		    0x00,		    /* direct.green_MASK */
227		    0,			    /* direct.blue */
228		    0x00,		    /* direct.blue_MASK */
229		    0,			    /* direct.alpha */
230		    0x01,		    /* direct.alpha_MASK */
231		},
232		0,			    /* colormap */
233	    },
234	    XCB_PICT_FORMAT_TYPE |
235	    XCB_PICT_FORMAT_DEPTH |
236	    XCB_PICT_FORMAT_RED_MASK |
237	    XCB_PICT_FORMAT_GREEN_MASK |
238	    XCB_PICT_FORMAT_BLUE_MASK |
239	    XCB_PICT_FORMAT_ALPHA |
240	    XCB_PICT_FORMAT_ALPHA_MASK,
241	},
242    };
243
244    if (format < 0 || format >= sizeof(standardFormats) / sizeof(*standardFormats))
245	return 0;
246
247    return xcb_render_util_find_format (formats,
248				    standardFormats[format].mask,
249				    &standardFormats[format].templ,
250				    0);
251}
252