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