gallium: fix refcount bug introduced in eb20e2984
[mesa.git] / src / glx / x11 / glx_texture_compression.c
1 /*
2 * (C) Copyright IBM Corporation 2004
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDERS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file glx_texture_compression.c
27 * Contains the routines required to implement GLX protocol for
28 * ARB_texture_compression and related extensions.
29 *
30 * \sa http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_compression.txt
31 *
32 * \author Ian Romanick <idr@us.ibm.com>
33 */
34
35 #include "packrender.h"
36 #include "packsingle.h"
37 #include "indirect.h"
38
39 #include <assert.h>
40
41
42 void
43 __indirect_glGetCompressedTexImageARB( GLenum target, GLint level,
44 GLvoid * img )
45 {
46 __GLX_SINGLE_DECLARE_VARIABLES();
47 xGLXGetTexImageReply reply;
48 size_t image_bytes;
49
50 __GLX_SINGLE_LOAD_VARIABLES();
51 __GLX_SINGLE_BEGIN( X_GLsop_GetCompressedTexImage, 8 );
52 __GLX_SINGLE_PUT_LONG( 0, target );
53 __GLX_SINGLE_PUT_LONG( 4, level );
54 __GLX_SINGLE_READ_XREPLY();
55
56 image_bytes = reply.width;
57 assert( image_bytes <= ((4 * reply.length) - 0) );
58 assert( image_bytes >= ((4 * reply.length) - 3) );
59
60 if ( image_bytes != 0 ) {
61 _XRead( dpy, (char *) img, image_bytes );
62 if ( image_bytes < (4 * reply.length) ) {
63 _XEatData( dpy, (4 * reply.length) - image_bytes );
64 }
65 }
66
67 __GLX_SINGLE_END();
68 }
69
70
71 /**
72 * Internal function used for \c glCompressedTexImage1D and
73 * \c glCompressedTexImage2D.
74 */
75 static void
76 CompressedTexImage1D2D( GLenum target, GLint level,
77 GLenum internal_format,
78 GLsizei width, GLsizei height,
79 GLint border, GLsizei image_size,
80 const GLvoid *data, CARD32 rop )
81 {
82 __GLX_DECLARE_VARIABLES();
83
84 __GLX_LOAD_VARIABLES();
85 if ( gc->currentDpy == NULL ) {
86 return;
87 }
88
89 if ( (target == GL_PROXY_TEXTURE_1D)
90 || (target == GL_PROXY_TEXTURE_2D)
91 || (target == GL_PROXY_TEXTURE_CUBE_MAP) ) {
92 compsize = 0;
93 }
94 else {
95 compsize = image_size;
96 }
97
98 cmdlen = __GLX_PAD( __GLX_COMPRESSED_TEXIMAGE_CMD_HDR_SIZE
99 + compsize );
100 if ( cmdlen <= gc->maxSmallRenderCommandSize ) {
101 __GLX_BEGIN_VARIABLE( rop, cmdlen );
102 __GLX_PUT_LONG( 4, target );
103 __GLX_PUT_LONG( 8, level );
104 __GLX_PUT_LONG( 12, internal_format );
105 __GLX_PUT_LONG( 16, width );
106 __GLX_PUT_LONG( 20, height );
107 __GLX_PUT_LONG( 24, border );
108 __GLX_PUT_LONG( 28, image_size );
109 if ( compsize != 0 ) {
110 __GLX_PUT_CHAR_ARRAY( __GLX_COMPRESSED_TEXIMAGE_CMD_HDR_SIZE,
111 data, image_size );
112 }
113 __GLX_END( cmdlen );
114 }
115 else {
116 assert( compsize != 0 );
117
118 __GLX_BEGIN_VARIABLE_LARGE( rop, cmdlen + 4 );
119 __GLX_PUT_LONG( 8, target );
120 __GLX_PUT_LONG( 12, level );
121 __GLX_PUT_LONG( 16, internal_format );
122 __GLX_PUT_LONG( 20, width );
123 __GLX_PUT_LONG( 24, height );
124 __GLX_PUT_LONG( 28, border );
125 __GLX_PUT_LONG( 32, image_size );
126 __glXSendLargeCommand( gc, gc->pc,
127 __GLX_COMPRESSED_TEXIMAGE_CMD_HDR_SIZE + 4,
128 data, image_size );
129 }
130 }
131
132
133 /**
134 * Internal function used for \c glCompressedTexSubImage1D and
135 * \c glCompressedTexSubImage2D.
136 */
137 static void
138 CompressedTexSubImage1D2D( GLenum target, GLint level,
139 GLsizei xoffset, GLsizei yoffset,
140 GLsizei width, GLsizei height,
141 GLenum format, GLsizei image_size,
142 const GLvoid *data, CARD32 rop )
143 {
144 __GLX_DECLARE_VARIABLES();
145
146 __GLX_LOAD_VARIABLES();
147 if ( gc->currentDpy == NULL ) {
148 return;
149 }
150
151 if ( target == GL_PROXY_TEXTURE_3D ) {
152 compsize = 0;
153 }
154 else {
155 compsize = image_size;
156 }
157
158 cmdlen = __GLX_PAD( __GLX_COMPRESSED_TEXSUBIMAGE_CMD_HDR_SIZE
159 + compsize );
160 if ( cmdlen <= gc->maxSmallRenderCommandSize ) {
161 __GLX_BEGIN_VARIABLE( rop, cmdlen );
162 __GLX_PUT_LONG( 4, target );
163 __GLX_PUT_LONG( 8, level );
164 __GLX_PUT_LONG( 12, xoffset );
165 __GLX_PUT_LONG( 16, yoffset );
166 __GLX_PUT_LONG( 20, width );
167 __GLX_PUT_LONG( 24, height );
168 __GLX_PUT_LONG( 28, format );
169 __GLX_PUT_LONG( 32, image_size );
170 if ( compsize != 0 ) {
171 __GLX_PUT_CHAR_ARRAY( __GLX_COMPRESSED_TEXSUBIMAGE_CMD_HDR_SIZE,
172 data, image_size );
173 }
174 __GLX_END( cmdlen );
175 }
176 else {
177 assert( compsize != 0 );
178
179 __GLX_BEGIN_VARIABLE_LARGE( rop, cmdlen + 4 );
180 __GLX_PUT_LONG( 8, target );
181 __GLX_PUT_LONG( 12, level );
182 __GLX_PUT_LONG( 16, xoffset );
183 __GLX_PUT_LONG( 20, yoffset );
184 __GLX_PUT_LONG( 24, width );
185 __GLX_PUT_LONG( 28, height );
186 __GLX_PUT_LONG( 32, format );
187 __GLX_PUT_LONG( 36, image_size );
188 __glXSendLargeCommand( gc, gc->pc,
189 __GLX_COMPRESSED_TEXSUBIMAGE_CMD_HDR_SIZE + 4,
190 data, image_size );
191 }
192 }
193
194
195 void
196 __indirect_glCompressedTexImage1DARB( GLenum target, GLint level,
197 GLenum internal_format, GLsizei width,
198 GLint border, GLsizei image_size,
199 const GLvoid *data )
200 {
201 CompressedTexImage1D2D( target, level, internal_format, width, 0,
202 border, image_size, data,
203 X_GLrop_CompressedTexImage1D );
204 }
205
206
207 void
208 __indirect_glCompressedTexImage2DARB( GLenum target, GLint level,
209 GLenum internal_format,
210 GLsizei width, GLsizei height,
211 GLint border, GLsizei image_size,
212 const GLvoid *data )
213 {
214 CompressedTexImage1D2D( target, level, internal_format, width, height,
215 border, image_size, data,
216 X_GLrop_CompressedTexImage2D );
217 }
218
219
220 void
221 __indirect_glCompressedTexImage3DARB( GLenum target, GLint level,
222 GLenum internal_format,
223 GLsizei width, GLsizei height, GLsizei depth,
224 GLint border, GLsizei image_size,
225 const GLvoid *data )
226 {
227 __GLX_DECLARE_VARIABLES();
228
229 __GLX_LOAD_VARIABLES();
230 if ( gc->currentDpy == NULL ) {
231 return;
232 }
233
234 cmdlen = __GLX_PAD( __GLX_COMPRESSED_TEXIMAGE_3D_CMD_HDR_SIZE
235 + image_size );
236 if ( cmdlen <= gc->maxSmallRenderCommandSize ) {
237 __GLX_BEGIN_VARIABLE( X_GLrop_CompressedTexImage3D, cmdlen );
238 __GLX_PUT_LONG( 4, target );
239 __GLX_PUT_LONG( 8, level );
240 __GLX_PUT_LONG( 12, internal_format );
241 __GLX_PUT_LONG( 16, width );
242 __GLX_PUT_LONG( 20, height );
243 __GLX_PUT_LONG( 24, depth );
244 __GLX_PUT_LONG( 28, border );
245 __GLX_PUT_LONG( 32, image_size );
246 if ( image_size != 0 ) {
247 __GLX_PUT_CHAR_ARRAY( __GLX_COMPRESSED_TEXIMAGE_3D_CMD_HDR_SIZE,
248 data, image_size );
249 }
250 __GLX_END( cmdlen );
251 }
252 else {
253 __GLX_BEGIN_VARIABLE_LARGE( X_GLrop_CompressedTexImage3D,
254 cmdlen + 4 );
255 __GLX_PUT_LONG( 8, target );
256 __GLX_PUT_LONG( 12, level );
257 __GLX_PUT_LONG( 16, internal_format );
258 __GLX_PUT_LONG( 20, width );
259 __GLX_PUT_LONG( 24, height );
260 __GLX_PUT_LONG( 28, depth );
261 __GLX_PUT_LONG( 32, border );
262 __GLX_PUT_LONG( 36, image_size );
263 __glXSendLargeCommand( gc, gc->pc,
264 __GLX_COMPRESSED_TEXIMAGE_3D_CMD_HDR_SIZE + 4,
265 data, image_size );
266 }
267 }
268
269
270 void
271 __indirect_glCompressedTexSubImage1DARB( GLenum target, GLint level,
272 GLint xoffset,
273 GLsizei width,
274 GLenum format, GLsizei image_size,
275 const GLvoid *data )
276 {
277 CompressedTexSubImage1D2D( target, level, xoffset, 0, width, 0,
278 format, image_size, data,
279 X_GLrop_CompressedTexSubImage1D );
280 }
281
282
283 void
284 __indirect_glCompressedTexSubImage2DARB( GLenum target, GLint level,
285 GLint xoffset, GLint yoffset,
286 GLsizei width, GLsizei height,
287 GLenum format, GLsizei image_size,
288 const GLvoid *data )
289 {
290 CompressedTexSubImage1D2D( target, level, xoffset, yoffset, width, height,
291 format, image_size, data,
292 X_GLrop_CompressedTexSubImage2D );
293 }
294
295
296 void
297 __indirect_glCompressedTexSubImage3DARB( GLenum target, GLint level,
298 GLint xoffset, GLint yoffset, GLint zoffset,
299 GLsizei width, GLsizei height, GLsizei depth,
300 GLenum format, GLsizei image_size,
301 const GLvoid *data )
302 {
303 __GLX_DECLARE_VARIABLES();
304
305 __GLX_LOAD_VARIABLES();
306 if ( gc->currentDpy == NULL ) {
307 return;
308 }
309
310 cmdlen = __GLX_PAD( __GLX_COMPRESSED_TEXSUBIMAGE_3D_CMD_HDR_SIZE
311 + image_size );
312 if ( cmdlen <= gc->maxSmallRenderCommandSize ) {
313 __GLX_BEGIN_VARIABLE( X_GLrop_CompressedTexSubImage3D, cmdlen );
314 __GLX_PUT_LONG( 4, target );
315 __GLX_PUT_LONG( 8, level );
316 __GLX_PUT_LONG( 12, xoffset );
317 __GLX_PUT_LONG( 16, yoffset );
318 __GLX_PUT_LONG( 20, zoffset );
319 __GLX_PUT_LONG( 24, width );
320 __GLX_PUT_LONG( 28, height );
321 __GLX_PUT_LONG( 32, depth );
322 __GLX_PUT_LONG( 36, format );
323 __GLX_PUT_LONG( 40, image_size );
324 if ( image_size != 0 ) {
325 __GLX_PUT_CHAR_ARRAY( __GLX_COMPRESSED_TEXSUBIMAGE_3D_CMD_HDR_SIZE,
326 data, image_size );
327 }
328 __GLX_END( cmdlen );
329 }
330 else {
331 __GLX_BEGIN_VARIABLE_LARGE( X_GLrop_CompressedTexSubImage3D,
332 cmdlen + 4 );
333 __GLX_PUT_LONG( 8, target );
334 __GLX_PUT_LONG( 12, level );
335 __GLX_PUT_LONG( 16, xoffset );
336 __GLX_PUT_LONG( 20, yoffset );
337 __GLX_PUT_LONG( 24, zoffset );
338 __GLX_PUT_LONG( 28, width );
339 __GLX_PUT_LONG( 32, height );
340 __GLX_PUT_LONG( 36, depth );
341 __GLX_PUT_LONG( 40, format );
342 __GLX_PUT_LONG( 44, image_size );
343 __glXSendLargeCommand( gc, gc->pc,
344 __GLX_COMPRESSED_TEXSUBIMAGE_3D_CMD_HDR_SIZE + 4,
345 data, image_size );
346 }
347 }