unpack.h revision 9ace9065
1#ifdef HAVE_DIX_CONFIG_H
2#include <dix-config.h>
3#endif
4
5#ifndef __GLX_unpack_h__
6#define __GLX_unpack_h__
7
8/*
9 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
10 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice including the dates of first publication and
20 * either this permission notice or a reference to
21 * http://oss.sgi.com/projects/FreeB/
22 * shall be included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
29 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 * Except as contained in this notice, the name of Silicon Graphics, Inc.
33 * shall not be used in advertising or otherwise to promote the sale, use or
34 * other dealings in this Software without prior written authorization from
35 * Silicon Graphics, Inc.
36 */
37
38#define __GLX_PAD(s) (((s)+3) & (GLuint)~3)
39
40/*
41** Fetch the context-id out of a SingleReq request pointed to by pc.
42*/
43#define __GLX_GET_SINGLE_CONTEXT_TAG(pc) (((xGLXSingleReq*)pc)->contextTag)
44#define __GLX_GET_VENDPRIV_CONTEXT_TAG(pc) (((xGLXVendorPrivateReq*)pc)->contextTag)
45
46/*
47** Fetch a double from potentially unaligned memory.
48*/
49#ifdef __GLX_ALIGN64
50#define __GLX_MEM_COPY(dst,src,n)	memmove(dst,src,n)
51#define __GLX_GET_DOUBLE(dst,src)	__GLX_MEM_COPY(&dst,src,8)
52#else
53#define __GLX_GET_DOUBLE(dst,src)	(dst) = *((GLdouble*)(src))
54#endif
55
56extern void __glXMemInit(void);
57
58extern xGLXSingleReply __glXReply;
59
60#define __GLX_BEGIN_REPLY(size) \
61  	__glXReply.length = __GLX_PAD(size) >> 2;	\
62  	__glXReply.type = X_Reply; 			\
63  	__glXReply.sequenceNumber = client->sequence;
64
65#define __GLX_SEND_HEADER() \
66	WriteToClient( client, sz_xGLXSingleReply, (char *)&__glXReply);
67
68#define __GLX_PUT_RETVAL(a) \
69  	__glXReply.retval = (a);
70
71#define __GLX_PUT_SIZE(a) \
72  	__glXReply.size = (a);
73
74#define __GLX_PUT_RENDERMODE(m) \
75        __glXReply.pad3 = (m)
76
77/*
78** Get a buffer to hold returned data, with the given alignment.  If we have
79** to realloc, allocate size+align, in case the pointer has to be bumped for
80** alignment.  The answerBuffer should already be aligned.
81**
82** NOTE: the cast (long)res below assumes a long is large enough to hold a
83** pointer.
84*/
85#define __GLX_GET_ANSWER_BUFFER(res,cl,size,align)			 \
86    if ((size) > sizeof(answerBuffer)) {				 \
87	int bump;							 \
88	if ((cl)->returnBufSize < (size)+(align)) {			 \
89	    (cl)->returnBuf = (GLbyte*)realloc((cl)->returnBuf,	 	 \
90						(size)+(align));         \
91	    if (!(cl)->returnBuf) {					 \
92		return BadAlloc;					 \
93	    }								 \
94	    (cl)->returnBufSize = (size)+(align);			 \
95	}								 \
96	res = (char*)cl->returnBuf;					 \
97	bump = (long)(res) % (align);					 \
98	if (bump) res += (align) - (bump);				 \
99    } else {								 \
100	res = (char *)answerBuffer;					 \
101    }
102
103#define __GLX_PUT_BYTE() \
104  	*(GLbyte *)&__glXReply.pad3 = *(GLbyte *)answer
105
106#define __GLX_PUT_SHORT() \
107  	*(GLshort *)&__glXReply.pad3 = *(GLshort *)answer
108
109#define __GLX_PUT_INT() \
110  	*(GLint *)&__glXReply.pad3 = *(GLint *)answer
111
112#define __GLX_PUT_FLOAT() \
113  	*(GLfloat *)&__glXReply.pad3 = *(GLfloat *)answer
114
115#define __GLX_PUT_DOUBLE() \
116  	*(GLdouble *)&__glXReply.pad3 = *(GLdouble *)answer
117
118#define __GLX_SEND_BYTE_ARRAY(len) \
119	WriteToClient(client, __GLX_PAD((len)*__GLX_SIZE_INT8), (char *)answer)
120
121#define __GLX_SEND_SHORT_ARRAY(len) \
122	WriteToClient(client, __GLX_PAD((len)*__GLX_SIZE_INT16), (char *)answer)
123
124#define __GLX_SEND_INT_ARRAY(len) \
125	WriteToClient(client, (len)*__GLX_SIZE_INT32, (char *)answer)
126
127#define __GLX_SEND_FLOAT_ARRAY(len) \
128	WriteToClient(client, (len)*__GLX_SIZE_FLOAT32, (char *)answer)
129
130#define __GLX_SEND_DOUBLE_ARRAY(len) \
131	WriteToClient(client, (len)*__GLX_SIZE_FLOAT64, (char *)answer)
132
133
134#define __GLX_SEND_VOID_ARRAY(len)  __GLX_SEND_BYTE_ARRAY(len)
135#define __GLX_SEND_UBYTE_ARRAY(len)  __GLX_SEND_BYTE_ARRAY(len)
136#define __GLX_SEND_USHORT_ARRAY(len) __GLX_SEND_SHORT_ARRAY(len)
137#define __GLX_SEND_UINT_ARRAY(len)  __GLX_SEND_INT_ARRAY(len)
138
139/*
140** PERFORMANCE NOTE:
141** Machine dependent optimizations abound here; these swapping macros can
142** conceivably be replaced with routines that do the job faster.
143*/
144#define __GLX_DECLARE_SWAP_VARIABLES \
145	GLbyte sw
146
147#define __GLX_DECLARE_SWAP_ARRAY_VARIABLES \
148  	GLbyte *swapPC;		\
149  	GLbyte *swapEnd
150
151
152#define __GLX_SWAP_INT(pc) 			\
153  	sw = ((GLbyte *)(pc))[0]; 		\
154  	((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[3]; 	\
155  	((GLbyte *)(pc))[3] = sw; 		\
156  	sw = ((GLbyte *)(pc))[1]; 		\
157  	((GLbyte *)(pc))[1] = ((GLbyte *)(pc))[2]; 	\
158  	((GLbyte *)(pc))[2] = sw;
159
160#define __GLX_SWAP_SHORT(pc) \
161  	sw = ((GLbyte *)(pc))[0]; 		\
162  	((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[1]; 	\
163  	((GLbyte *)(pc))[1] = sw;
164
165#define __GLX_SWAP_DOUBLE(pc) \
166  	sw = ((GLbyte *)(pc))[0]; 		\
167  	((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[7]; 	\
168  	((GLbyte *)(pc))[7] = sw; 		\
169  	sw = ((GLbyte *)(pc))[1]; 		\
170  	((GLbyte *)(pc))[1] = ((GLbyte *)(pc))[6]; 	\
171  	((GLbyte *)(pc))[6] = sw;			\
172  	sw = ((GLbyte *)(pc))[2]; 		\
173  	((GLbyte *)(pc))[2] = ((GLbyte *)(pc))[5]; 	\
174  	((GLbyte *)(pc))[5] = sw;			\
175  	sw = ((GLbyte *)(pc))[3]; 		\
176  	((GLbyte *)(pc))[3] = ((GLbyte *)(pc))[4]; 	\
177  	((GLbyte *)(pc))[4] = sw;
178
179#define __GLX_SWAP_FLOAT(pc) \
180  	sw = ((GLbyte *)(pc))[0]; 		\
181  	((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[3]; 	\
182  	((GLbyte *)(pc))[3] = sw; 		\
183  	sw = ((GLbyte *)(pc))[1]; 		\
184  	((GLbyte *)(pc))[1] = ((GLbyte *)(pc))[2]; 	\
185  	((GLbyte *)(pc))[2] = sw;
186
187#define __GLX_SWAP_INT_ARRAY(pc, count) \
188  	swapPC = ((GLbyte *)(pc));		\
189  	swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_INT32;\
190  	while (swapPC < swapEnd) {		\
191	    __GLX_SWAP_INT(swapPC);		\
192	    swapPC += __GLX_SIZE_INT32;		\
193	}
194
195#define __GLX_SWAP_SHORT_ARRAY(pc, count) \
196  	swapPC = ((GLbyte *)(pc));		\
197  	swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_INT16;\
198  	while (swapPC < swapEnd) {		\
199	    __GLX_SWAP_SHORT(swapPC);		\
200	    swapPC += __GLX_SIZE_INT16;		\
201	}
202
203#define __GLX_SWAP_DOUBLE_ARRAY(pc, count) \
204  	swapPC = ((GLbyte *)(pc));		\
205  	swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_FLOAT64;\
206  	while (swapPC < swapEnd) {		\
207	    __GLX_SWAP_DOUBLE(swapPC);		\
208	    swapPC += __GLX_SIZE_FLOAT64;	\
209	}
210
211#define __GLX_SWAP_FLOAT_ARRAY(pc, count) \
212  	swapPC = ((GLbyte *)(pc));		\
213  	swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_FLOAT32;\
214  	while (swapPC < swapEnd) {		\
215	    __GLX_SWAP_FLOAT(swapPC);		\
216	    swapPC += __GLX_SIZE_FLOAT32;	\
217	}
218
219#define __GLX_SWAP_REPLY_HEADER() \
220  	__GLX_SWAP_SHORT(&__glXReply.sequenceNumber); \
221  	__GLX_SWAP_INT(&__glXReply.length);
222
223#define __GLX_SWAP_REPLY_RETVAL() \
224  	__GLX_SWAP_INT(&__glXReply.retval)
225
226#define __GLX_SWAP_REPLY_SIZE() \
227  	__GLX_SWAP_INT(&__glXReply.size)
228
229#endif /* !__GLX_unpack_h__ */
230
231
232
233
234
235