2 * (C) Copyright IBM Corporation 2004
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:
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
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 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
27 * Implementation of pbuffer related functions.
29 * \author Ian Romanick <idr@us.ibm.com>
33 #include "glxclient.h"
34 #include <X11/extensions/extutil.h>
35 #include <X11/extensions/Xext.h>
39 #include "glxextensions.h"
40 #include "glcontextmodes.h"
44 * Change a drawable's attribute.
46 * This function is used to implement \c glXSelectEvent and
47 * \c glXSelectEventSGIX.
50 * This function dynamically determines whether to use the SGIX_pbuffer
51 * version of the protocol or the GLX 1.3 version of the protocol.
54 * This function needs to be modified to work with direct-rendering drivers.
57 ChangeDrawableAttribute( Display
* dpy
, GLXDrawable drawable
,
58 const CARD32
* attribs
, size_t num_attribs
)
60 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
64 if ( (dpy
== NULL
) || (drawable
== 0) ) {
68 opcode
= __glXSetupForCommand(dpy
);
74 if ( (priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3) ) {
75 xGLXChangeDrawableAttributesReq
*req
;
77 GetReqExtra( GLXChangeDrawableAttributes
, 8 + (8 * num_attribs
), req
);
78 output
= (CARD32
*) (req
+ 1);
80 req
->reqType
= opcode
;
81 req
->glxCode
= X_GLXChangeDrawableAttributes
;
82 req
->drawable
= drawable
;
83 req
->numAttribs
= (CARD32
) num_attribs
;
86 xGLXVendorPrivateWithReplyReq
*vpreq
;
88 GetReqExtra( GLXVendorPrivateWithReply
, 4 + (8 * num_attribs
), vpreq
);
89 output
= (CARD32
*) (vpreq
+ 1);
91 vpreq
->reqType
= opcode
;
92 vpreq
->glxCode
= X_GLXVendorPrivateWithReply
;
93 vpreq
->vendorCode
= X_GLXvop_ChangeDrawableAttributesSGIX
;
95 output
[0] = (CARD32
) drawable
;
99 (void) memcpy( output
, attribs
, sizeof( CARD32
) * 2 * num_attribs
);
111 * This function is used to implement \c glXDestroyPbuffer and
112 * \c glXDestroyGLXPbufferSGIX.
115 * This function dynamically determines whether to use the SGIX_pbuffer
116 * version of the protocol or the GLX 1.3 version of the protocol.
119 * This function needs to be modified to work with direct-rendering drivers.
122 DestroyPbuffer( Display
* dpy
, GLXDrawable drawable
)
124 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
127 if ( (dpy
== NULL
) || (drawable
== 0) ) {
131 opcode
= __glXSetupForCommand(dpy
);
137 if ( (priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3) ) {
138 xGLXDestroyPbufferReq
* req
;
140 GetReq( GLXDestroyPbuffer
, req
);
141 req
->reqType
= opcode
;
142 req
->glxCode
= X_GLXDestroyPbuffer
;
143 req
->pbuffer
= (GLXPbuffer
) drawable
;
146 xGLXVendorPrivateWithReplyReq
*vpreq
;
149 GetReqExtra( GLXVendorPrivateWithReply
, 4, vpreq
);
150 data
= (CARD32
*) (vpreq
+ 1);
152 data
[0] = (CARD32
) drawable
;
154 vpreq
->reqType
= opcode
;
155 vpreq
->glxCode
= X_GLXVendorPrivateWithReply
;
156 vpreq
->vendorCode
= X_GLXvop_DestroyGLXPbufferSGIX
;
166 #ifdef GLX_DIRECT_RENDERING
167 extern __GLXDRIdrawable
*
168 GetGLXDRIDrawable(Display
*dpy
, GLXDrawable drawable
, int * const scrn_num
);
171 determineTextureTarget(const int *attribs
, int numAttribs
)
176 for (i
= 0; i
< numAttribs
; i
++) {
177 if (attribs
[2 * i
] == GLX_TEXTURE_TARGET_EXT
) {
178 switch (attribs
[2 * i
+ 1]) {
179 case GLX_TEXTURE_2D_EXT
:
180 target
= GL_TEXTURE_2D
;
182 case GLX_TEXTURE_RECTANGLE_EXT
:
183 target
= GL_TEXTURE_RECTANGLE_ARB
;
194 * Get a drawable's attribute.
196 * This function is used to implement \c glXGetSelectedEvent and
197 * \c glXGetSelectedEventSGIX.
200 * This function dynamically determines whether to use the SGIX_pbuffer
201 * version of the protocol or the GLX 1.3 version of the protocol.
204 * The number of attributes returned is likely to be small, probably less than
205 * 10. Given that, this routine should try to use an array on the stack to
206 * capture the reply rather than always calling Xmalloc.
209 * This function needs to be modified to work with direct-rendering drivers.
212 GetDrawableAttribute( Display
*dpy
, GLXDrawable drawable
,
213 int attribute
, unsigned int *value
)
215 __GLXdisplayPrivate
*priv
;
216 xGLXGetDrawableAttributesReply reply
;
221 unsigned int num_attributes
;
223 if ( (dpy
== NULL
) || (drawable
== 0) ) {
227 priv
= __glXInitialize(dpy
);
228 GLboolean use_glx_1_3
= ((priv
->majorVersion
> 1)
229 || (priv
->minorVersion
>= 3));
234 opcode
= __glXSetupForCommand(dpy
);
241 xGLXGetDrawableAttributesReq
*req
;
243 GetReqExtra( GLXGetDrawableAttributes
, 4, req
);
244 req
->reqType
= opcode
;
245 req
->glxCode
= X_GLXGetDrawableAttributes
;
246 req
->drawable
= drawable
;
249 xGLXVendorPrivateWithReplyReq
*vpreq
;
251 GetReqExtra( GLXVendorPrivateWithReply
, 4, vpreq
);
252 data
= (CARD32
*) (vpreq
+ 1);
253 data
[0] = (CARD32
) drawable
;
255 vpreq
->reqType
= opcode
;
256 vpreq
->glxCode
= X_GLXVendorPrivateWithReply
;
257 vpreq
->vendorCode
= X_GLXvop_GetDrawableAttributesSGIX
;
260 _XReply(dpy
, (xReply
*) &reply
, 0, False
);
262 if (reply
.type
== X_Error
)
269 length
= reply
.length
;
272 num_attributes
= (use_glx_1_3
) ? reply
.numAttribs
: length
/ 2;
273 data
= (CARD32
*) Xmalloc( length
* sizeof(CARD32
) );
274 if ( data
== NULL
) {
275 /* Throw data on the floor */
276 _XEatData(dpy
, length
);
278 _XRead(dpy
, (char *)data
, length
* sizeof(CARD32
) );
280 /* Search the set of returned attributes for the attribute requested by
283 for ( i
= 0 ; i
< num_attributes
; i
++ ) {
284 if ( data
[i
*2] == attribute
) {
285 *value
= data
[ (i
*2) + 1 ];
290 #ifdef GLX_DIRECT_RENDERING
292 __GLXDRIdrawable
*pdraw
= GetGLXDRIDrawable(dpy
, drawable
, NULL
);
294 if (pdraw
!= NULL
&& !pdraw
->textureTarget
)
295 pdraw
->textureTarget
= determineTextureTarget((const int *)data
,
311 * Create a non-pbuffer GLX drawable.
314 * This function needs to be modified to work with direct-rendering drivers.
317 CreateDrawable( Display
*dpy
, const __GLcontextModes
* fbconfig
,
318 Drawable drawable
, const int *attrib_list
,
321 xGLXCreateWindowReq
* req
;
328 while (attrib_list
[i
* 2] != None
)
332 opcode
= __glXSetupForCommand(dpy
);
337 GetReqExtra( GLXCreateWindow
, 8 * i
, req
);
338 data
= (CARD32
*) (req
+ 1);
340 req
->reqType
= opcode
;
341 req
->glxCode
= glxCode
;
342 req
->screen
= (CARD32
) fbconfig
->screen
;
343 req
->fbconfig
= fbconfig
->fbconfigID
;
344 req
->window
= (CARD32
) drawable
;
345 req
->glxwindow
= (GLXWindow
) XAllocID(dpy
);
346 req
->numAttribs
= (CARD32
) i
;
348 memcpy( data
, attrib_list
, 8 * i
);
353 #ifdef GLX_DIRECT_RENDERING
355 /* FIXME: Maybe delay __DRIdrawable creation until the drawable
356 * is actually bound to a context... */
358 __GLXdisplayPrivate
* const priv
= __glXInitialize(dpy
);
359 __GLXDRIdrawable
*pdraw
;
360 __GLXscreenConfigs
*psc
;
362 psc
= &priv
->screenConfigs
[fbconfig
->screen
];
363 if (psc
->driScreen
== NULL
)
365 pdraw
= psc
->driScreen
->createDrawable(psc
, drawable
,
366 req
->glxwindow
, fbconfig
);
368 fprintf(stderr
, "failed to create drawable\n");
372 if (__glxHashInsert(psc
->drawHash
, req
->glxwindow
, pdraw
)) {
373 (*pdraw
->destroyDrawable
)(pdraw
);
374 return None
; /* FIXME: Check what we're supposed to do here... */
377 pdraw
->textureTarget
= determineTextureTarget(attrib_list
, i
);
381 return (GLXDrawable
)req
->glxwindow
;
386 * Destroy a non-pbuffer GLX drawable.
389 * This function needs to be modified to work with direct-rendering drivers.
392 DestroyDrawable( Display
* dpy
, GLXDrawable drawable
, CARD32 glxCode
)
394 xGLXDestroyPbufferReq
* req
;
397 if ( (dpy
== NULL
) || (drawable
== 0) ) {
402 opcode
= __glXSetupForCommand(dpy
);
408 GetReqExtra( GLXDestroyPbuffer
, 4, req
);
409 req
->reqType
= opcode
;
410 req
->glxCode
= glxCode
;
411 req
->pbuffer
= (GLXPbuffer
) drawable
;
416 #ifdef GLX_DIRECT_RENDERING
419 __GLXdisplayPrivate
* const priv
= __glXInitialize(dpy
);
420 __GLXDRIdrawable
*pdraw
= GetGLXDRIDrawable(dpy
, drawable
, &screen
);
421 __GLXscreenConfigs
*psc
= &priv
->screenConfigs
[screen
];
424 (*pdraw
->destroyDrawable
)(pdraw
);
425 __glxHashDelete(psc
->drawHash
, drawable
);
437 * This function is used to implement \c glXCreatePbuffer and
438 * \c glXCreateGLXPbufferSGIX.
441 * This function dynamically determines whether to use the SGIX_pbuffer
442 * version of the protocol or the GLX 1.3 version of the protocol.
445 * This function needs to be modified to work with direct-rendering drivers.
448 CreatePbuffer( Display
*dpy
, const __GLcontextModes
* fbconfig
,
449 unsigned int width
, unsigned int height
,
450 const int *attrib_list
, GLboolean size_in_attribs
)
452 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
460 while (attrib_list
[i
* 2])
464 opcode
= __glXSetupForCommand(dpy
);
471 if ( (priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3) ) {
472 xGLXCreatePbufferReq
* req
;
473 unsigned int extra
= (size_in_attribs
) ? 0 : 2;
475 GetReqExtra( GLXCreatePbuffer
, (8 * (i
+ extra
)), req
);
476 data
= (CARD32
*) (req
+ 1);
478 req
->reqType
= opcode
;
479 req
->glxCode
= X_GLXCreatePbuffer
;
480 req
->screen
= (CARD32
) fbconfig
->screen
;
481 req
->fbconfig
= fbconfig
->fbconfigID
;
482 req
->pbuffer
= (GLXPbuffer
) id
;
483 req
->numAttribs
= (CARD32
) (i
+ extra
);
485 if ( ! size_in_attribs
) {
486 data
[(2 * i
) + 0] = GLX_PBUFFER_WIDTH
;
487 data
[(2 * i
) + 1] = width
;
488 data
[(2 * i
) + 2] = GLX_PBUFFER_HEIGHT
;
489 data
[(2 * i
) + 3] = height
;
494 xGLXVendorPrivateReq
*vpreq
;
496 GetReqExtra( GLXVendorPrivate
, 20 + (8 * i
), vpreq
);
497 data
= (CARD32
*) (vpreq
+ 1);
499 vpreq
->reqType
= opcode
;
500 vpreq
->glxCode
= X_GLXVendorPrivate
;
501 vpreq
->vendorCode
= X_GLXvop_CreateGLXPbufferSGIX
;
503 data
[0] = (CARD32
) fbconfig
->screen
;
504 data
[1] = (CARD32
) fbconfig
->fbconfigID
;
505 data
[2] = (CARD32
) id
;
506 data
[3] = (CARD32
) width
;
507 data
[4] = (CARD32
) height
;
511 (void) memcpy( data
, attrib_list
, sizeof(CARD32
) * 2 * i
);
521 * Create a new pbuffer.
523 PUBLIC GLXPbufferSGIX
524 glXCreateGLXPbufferSGIX(Display
*dpy
, GLXFBConfigSGIX config
,
525 unsigned int width
, unsigned int height
,
528 return (GLXPbufferSGIX
) CreatePbuffer( dpy
, (__GLcontextModes
*) config
,
530 attrib_list
, GL_FALSE
);
535 * Create a new pbuffer.
538 glXCreatePbuffer(Display
*dpy
, GLXFBConfig config
, const int *attrib_list
)
540 int i
, width
, height
;
545 for (i
= 0; attrib_list
[i
* 2]; i
++) {
546 switch (attrib_list
[i
* 2]) {
547 case GLX_PBUFFER_WIDTH
:
548 width
= attrib_list
[i
* 2 + 1];
550 case GLX_PBUFFER_HEIGHT
:
551 height
= attrib_list
[i
* 2 + 1];
556 return (GLXPbuffer
) CreatePbuffer( dpy
, (__GLcontextModes
*) config
,
558 attrib_list
, GL_TRUE
);
563 * Destroy an existing pbuffer.
566 glXDestroyPbuffer(Display
*dpy
, GLXPbuffer pbuf
)
568 DestroyPbuffer( dpy
, pbuf
);
573 * Query an attribute of a drawable.
576 glXQueryDrawable(Display
*dpy
, GLXDrawable drawable
,
577 int attribute
, unsigned int *value
)
579 GetDrawableAttribute( dpy
, drawable
, attribute
, value
);
584 * Query an attribute of a pbuffer.
587 glXQueryGLXPbufferSGIX(Display
*dpy
, GLXPbufferSGIX drawable
,
588 int attribute
, unsigned int *value
)
590 return GetDrawableAttribute( dpy
, drawable
, attribute
, value
);
595 * Select the event mask for a drawable.
598 glXSelectEvent(Display
*dpy
, GLXDrawable drawable
, unsigned long mask
)
602 attribs
[0] = (CARD32
) GLX_EVENT_MASK
;
603 attribs
[1] = (CARD32
) mask
;
605 ChangeDrawableAttribute( dpy
, drawable
, attribs
, 1 );
610 * Get the selected event mask for a drawable.
613 glXGetSelectedEvent(Display
*dpy
, GLXDrawable drawable
, unsigned long *mask
)
618 /* The non-sense with value is required because on LP64 platforms
619 * sizeof(unsigned int) != sizeof(unsigned long). On little-endian
620 * we could just type-cast the pointer, but why?
623 GetDrawableAttribute( dpy
, drawable
, GLX_EVENT_MASK_SGIX
, & value
);
629 glXCreatePixmap( Display
*dpy
, GLXFBConfig config
, Pixmap pixmap
,
630 const int *attrib_list
)
632 return CreateDrawable( dpy
, (__GLcontextModes
*) config
,
633 (Drawable
) pixmap
, attrib_list
,
639 glXCreateWindow( Display
*dpy
, GLXFBConfig config
, Window win
,
640 const int *attrib_list
)
642 return CreateDrawable( dpy
, (__GLcontextModes
*) config
,
643 (Drawable
) win
, attrib_list
,
649 glXDestroyPixmap(Display
*dpy
, GLXPixmap pixmap
)
651 DestroyDrawable( dpy
, (GLXDrawable
) pixmap
, X_GLXDestroyPixmap
);
656 glXDestroyWindow(Display
*dpy
, GLXWindow win
)
658 DestroyDrawable( dpy
, (GLXDrawable
) win
, X_GLXDestroyWindow
);
662 PUBLIC
GLX_ALIAS_VOID(glXDestroyGLXPbufferSGIX
,
663 (Display
*dpy
, GLXPbufferSGIX pbuf
),
667 PUBLIC
GLX_ALIAS_VOID(glXSelectEventSGIX
,
668 (Display
*dpy
, GLXDrawable drawable
, unsigned long mask
),
669 (dpy
, drawable
, mask
),
672 PUBLIC
GLX_ALIAS_VOID(glXGetSelectedEventSGIX
,
673 (Display
*dpy
, GLXDrawable drawable
, unsigned long *mask
),
674 (dpy
, drawable
, mask
),