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"
45 * Change a drawable's attribute.
47 * This function is used to implement \c glXSelectEvent and
48 * \c glXSelectEventSGIX.
51 * This function dynamically determines whether to use the SGIX_pbuffer
52 * version of the protocol or the GLX 1.3 version of the protocol.
55 * This function needs to be modified to work with direct-rendering drivers.
58 ChangeDrawableAttribute( Display
* dpy
, GLXDrawable drawable
,
59 const CARD32
* attribs
, size_t num_attribs
)
61 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
65 if ( (dpy
== NULL
) || (drawable
== 0) ) {
69 opcode
= __glXSetupForCommand(dpy
);
75 if ( (priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3) ) {
76 xGLXChangeDrawableAttributesReq
*req
;
78 GetReqExtra( GLXChangeDrawableAttributes
, 8 + (8 * num_attribs
), req
);
79 output
= (CARD32
*) (req
+ 1);
81 req
->reqType
= opcode
;
82 req
->glxCode
= X_GLXChangeDrawableAttributes
;
83 req
->drawable
= drawable
;
84 req
->numAttribs
= (CARD32
) num_attribs
;
87 xGLXVendorPrivateWithReplyReq
*vpreq
;
89 GetReqExtra( GLXVendorPrivateWithReply
, 4 + (8 * num_attribs
), vpreq
);
90 output
= (CARD32
*) (vpreq
+ 1);
92 vpreq
->reqType
= opcode
;
93 vpreq
->glxCode
= X_GLXVendorPrivateWithReply
;
94 vpreq
->vendorCode
= X_GLXvop_ChangeDrawableAttributesSGIX
;
96 output
[0] = (CARD32
) drawable
;
100 (void) memcpy( output
, attribs
, sizeof( CARD32
) * 2 * num_attribs
);
112 * This function is used to implement \c glXDestroyPbuffer and
113 * \c glXDestroyGLXPbufferSGIX.
116 * This function dynamically determines whether to use the SGIX_pbuffer
117 * version of the protocol or the GLX 1.3 version of the protocol.
120 * This function needs to be modified to work with direct-rendering drivers.
123 DestroyPbuffer( Display
* dpy
, GLXDrawable drawable
)
125 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
128 if ( (dpy
== NULL
) || (drawable
== 0) ) {
132 opcode
= __glXSetupForCommand(dpy
);
138 if ( (priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3) ) {
139 xGLXDestroyPbufferReq
* req
;
141 GetReq( GLXDestroyPbuffer
, req
);
142 req
->reqType
= opcode
;
143 req
->glxCode
= X_GLXDestroyPbuffer
;
144 req
->pbuffer
= (GLXPbuffer
) drawable
;
147 xGLXVendorPrivateWithReplyReq
*vpreq
;
150 GetReqExtra( GLXVendorPrivateWithReply
, 4, vpreq
);
151 data
= (CARD32
*) (vpreq
+ 1);
153 data
[0] = (CARD32
) drawable
;
155 vpreq
->reqType
= opcode
;
156 vpreq
->glxCode
= X_GLXVendorPrivateWithReply
;
157 vpreq
->vendorCode
= X_GLXvop_DestroyGLXPbufferSGIX
;
168 * Get a drawable's attribute.
170 * This function is used to implement \c glXGetSelectedEvent and
171 * \c glXGetSelectedEventSGIX.
174 * This function dynamically determines whether to use the SGIX_pbuffer
175 * version of the protocol or the GLX 1.3 version of the protocol.
178 * The number of attributes returned is likely to be small, probably less than
179 * 10. Given that, this routine should try to use an array on the stack to
180 * capture the reply rather than always calling Xmalloc.
183 * This function needs to be modified to work with direct-rendering drivers.
186 GetDrawableAttribute( Display
*dpy
, GLXDrawable drawable
,
187 int attribute
, unsigned int *value
)
189 __GLXdisplayPrivate
*priv
;
190 xGLXGetDrawableAttributesReply reply
;
195 unsigned int num_attributes
;
197 if ( (dpy
== NULL
) || (drawable
== 0) ) {
201 priv
= __glXInitialize(dpy
);
202 GLboolean use_glx_1_3
= ((priv
->majorVersion
> 1)
203 || (priv
->minorVersion
>= 3));
208 opcode
= __glXSetupForCommand(dpy
);
215 xGLXGetDrawableAttributesReq
*req
;
217 GetReqExtra( GLXGetDrawableAttributes
, 4, req
);
218 req
->reqType
= opcode
;
219 req
->glxCode
= X_GLXGetDrawableAttributes
;
220 req
->drawable
= drawable
;
223 xGLXVendorPrivateWithReplyReq
*vpreq
;
225 GetReqExtra( GLXVendorPrivateWithReply
, 4, vpreq
);
226 data
= (CARD32
*) (vpreq
+ 1);
227 data
[0] = (CARD32
) drawable
;
229 vpreq
->reqType
= opcode
;
230 vpreq
->glxCode
= X_GLXVendorPrivateWithReply
;
231 vpreq
->vendorCode
= X_GLXvop_GetDrawableAttributesSGIX
;
234 _XReply(dpy
, (xReply
*) &reply
, 0, False
);
236 if (reply
.type
== X_Error
)
243 length
= reply
.length
;
246 num_attributes
= (use_glx_1_3
) ? reply
.numAttribs
: length
/ 2;
247 data
= (CARD32
*) Xmalloc( length
* sizeof(CARD32
) );
248 if ( data
== NULL
) {
249 /* Throw data on the floor */
250 _XEatData(dpy
, length
);
252 _XRead(dpy
, (char *)data
, length
* sizeof(CARD32
) );
254 /* Search the set of returned attributes for the attribute requested by
257 for ( i
= 0 ; i
< num_attributes
; i
++ ) {
258 if ( data
[i
*2] == attribute
) {
259 *value
= data
[ (i
*2) + 1 ];
274 #ifdef GLX_DIRECT_RENDERING
275 extern __GLXDRIdrawable
*
276 GetGLXDRIDrawable(Display
*dpy
, GLXDrawable drawable
, int * const scrn_num
);
279 determineTextureTarget(const int *attribs
, int numAttribs
)
284 for (i
= 0; i
< numAttribs
; i
++) {
285 if (attribs
[2 * i
] == GLX_TEXTURE_TARGET_EXT
) {
286 switch (attribs
[2 * i
+ 1]) {
287 case GLX_TEXTURE_2D_EXT
:
288 target
= GL_TEXTURE_2D
;
290 case GLX_TEXTURE_RECTANGLE_EXT
:
291 target
= GL_TEXTURE_RECTANGLE_ARB
;
302 * Create a non-pbuffer GLX drawable.
305 * This function needs to be modified to work with direct-rendering drivers.
308 CreateDrawable( Display
*dpy
, const __GLcontextModes
* fbconfig
,
309 Drawable drawable
, const int *attrib_list
,
312 xGLXCreateWindowReq
* req
;
319 while (attrib_list
[i
* 2] != None
)
323 opcode
= __glXSetupForCommand(dpy
);
328 GetReqExtra( GLXCreateWindow
, 8 * i
, req
);
329 data
= (CARD32
*) (req
+ 1);
331 req
->reqType
= opcode
;
332 req
->glxCode
= glxCode
;
333 req
->screen
= (CARD32
) fbconfig
->screen
;
334 req
->fbconfig
= fbconfig
->fbconfigID
;
335 req
->window
= (CARD32
) drawable
;
336 req
->glxwindow
= (GLXWindow
) XAllocID(dpy
);
337 req
->numAttribs
= (CARD32
) i
;
339 memcpy( data
, attrib_list
, 8 * i
);
344 #ifdef GLX_DIRECT_RENDERING
346 /* FIXME: Maybe delay __DRIdrawable creation until the drawable
347 * is actually bound to a context... */
349 __GLXdisplayPrivate
* const priv
= __glXInitialize(dpy
);
350 __GLXDRIdrawable
*pdraw
;
351 __GLXscreenConfigs
*psc
;
353 psc
= &priv
->screenConfigs
[fbconfig
->screen
];
354 if (psc
->driScreen
== NULL
)
356 pdraw
= psc
->driScreen
->createDrawable(psc
, drawable
,
357 req
->glxwindow
, fbconfig
);
359 fprintf(stderr
, "failed to create drawable\n");
363 if (__glxHashInsert(psc
->drawHash
, req
->glxwindow
, pdraw
)) {
364 (*pdraw
->destroyDrawable
)(pdraw
);
365 return None
; /* FIXME: Check what we're supposed to do here... */
368 pdraw
->textureTarget
= determineTextureTarget(attrib_list
, i
);
372 return (GLXDrawable
)req
->glxwindow
;
377 * Destroy a non-pbuffer GLX drawable.
380 * This function needs to be modified to work with direct-rendering drivers.
383 DestroyDrawable( Display
* dpy
, GLXDrawable drawable
, CARD32 glxCode
)
385 xGLXDestroyPbufferReq
* req
;
388 if ( (dpy
== NULL
) || (drawable
== 0) ) {
393 opcode
= __glXSetupForCommand(dpy
);
399 GetReqExtra( GLXDestroyPbuffer
, 4, req
);
400 req
->reqType
= opcode
;
401 req
->glxCode
= glxCode
;
402 req
->pbuffer
= (GLXPbuffer
) drawable
;
407 #ifdef GLX_DIRECT_RENDERING
410 __GLXdisplayPrivate
* const priv
= __glXInitialize(dpy
);
411 __GLXDRIdrawable
*pdraw
= GetGLXDRIDrawable(dpy
, drawable
, &screen
);
412 __GLXscreenConfigs
*psc
= &priv
->screenConfigs
[screen
];
415 (*pdraw
->destroyDrawable
)(pdraw
);
416 __glxHashDelete(psc
->drawHash
, drawable
);
428 * This function is used to implement \c glXCreatePbuffer and
429 * \c glXCreateGLXPbufferSGIX.
432 * This function dynamically determines whether to use the SGIX_pbuffer
433 * version of the protocol or the GLX 1.3 version of the protocol.
436 * This function needs to be modified to work with direct-rendering drivers.
439 CreatePbuffer( Display
*dpy
, const __GLcontextModes
* fbconfig
,
440 unsigned int width
, unsigned int height
,
441 const int *attrib_list
, GLboolean size_in_attribs
)
443 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
451 while (attrib_list
[i
* 2])
455 opcode
= __glXSetupForCommand(dpy
);
462 if ( (priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3) ) {
463 xGLXCreatePbufferReq
* req
;
464 unsigned int extra
= (size_in_attribs
) ? 0 : 2;
466 GetReqExtra( GLXCreatePbuffer
, (8 * (i
+ extra
)), req
);
467 data
= (CARD32
*) (req
+ 1);
469 req
->reqType
= opcode
;
470 req
->glxCode
= X_GLXCreatePbuffer
;
471 req
->screen
= (CARD32
) fbconfig
->screen
;
472 req
->fbconfig
= fbconfig
->fbconfigID
;
473 req
->pbuffer
= (GLXPbuffer
) id
;
474 req
->numAttribs
= (CARD32
) (i
+ extra
);
476 if ( ! size_in_attribs
) {
477 data
[(2 * i
) + 0] = GLX_PBUFFER_WIDTH
;
478 data
[(2 * i
) + 1] = width
;
479 data
[(2 * i
) + 2] = GLX_PBUFFER_HEIGHT
;
480 data
[(2 * i
) + 3] = height
;
485 xGLXVendorPrivateReq
*vpreq
;
487 GetReqExtra( GLXVendorPrivate
, 20 + (8 * i
), vpreq
);
488 data
= (CARD32
*) (vpreq
+ 1);
490 vpreq
->reqType
= opcode
;
491 vpreq
->glxCode
= X_GLXVendorPrivate
;
492 vpreq
->vendorCode
= X_GLXvop_CreateGLXPbufferSGIX
;
494 data
[0] = (CARD32
) fbconfig
->screen
;
495 data
[1] = (CARD32
) fbconfig
->fbconfigID
;
496 data
[2] = (CARD32
) id
;
497 data
[3] = (CARD32
) width
;
498 data
[4] = (CARD32
) height
;
502 (void) memcpy( data
, attrib_list
, sizeof(CARD32
) * 2 * i
);
512 * Create a new pbuffer.
514 PUBLIC GLXPbufferSGIX
515 glXCreateGLXPbufferSGIX(Display
*dpy
, GLXFBConfigSGIX config
,
516 unsigned int width
, unsigned int height
,
519 return (GLXPbufferSGIX
) CreatePbuffer( dpy
, (__GLcontextModes
*) config
,
521 attrib_list
, GL_FALSE
);
526 * Create a new pbuffer.
529 glXCreatePbuffer(Display
*dpy
, GLXFBConfig config
, const int *attrib_list
)
531 int i
, width
, height
;
536 for (i
= 0; attrib_list
[i
* 2]; i
++) {
537 switch (attrib_list
[i
* 2]) {
538 case GLX_PBUFFER_WIDTH
:
539 width
= attrib_list
[i
* 2 + 1];
541 case GLX_PBUFFER_HEIGHT
:
542 height
= attrib_list
[i
* 2 + 1];
547 return (GLXPbuffer
) CreatePbuffer( dpy
, (__GLcontextModes
*) config
,
549 attrib_list
, GL_TRUE
);
554 * Destroy an existing pbuffer.
557 glXDestroyPbuffer(Display
*dpy
, GLXPbuffer pbuf
)
559 DestroyPbuffer( dpy
, pbuf
);
564 * Query an attribute of a drawable.
567 glXQueryDrawable(Display
*dpy
, GLXDrawable drawable
,
568 int attribute
, unsigned int *value
)
570 GetDrawableAttribute( dpy
, drawable
, attribute
, value
);
575 * Query an attribute of a pbuffer.
578 glXQueryGLXPbufferSGIX(Display
*dpy
, GLXPbufferSGIX drawable
,
579 int attribute
, unsigned int *value
)
581 return GetDrawableAttribute( dpy
, drawable
, attribute
, value
);
586 * Select the event mask for a drawable.
589 glXSelectEvent(Display
*dpy
, GLXDrawable drawable
, unsigned long mask
)
593 attribs
[0] = (CARD32
) GLX_EVENT_MASK
;
594 attribs
[1] = (CARD32
) mask
;
596 ChangeDrawableAttribute( dpy
, drawable
, attribs
, 1 );
601 * Get the selected event mask for a drawable.
604 glXGetSelectedEvent(Display
*dpy
, GLXDrawable drawable
, unsigned long *mask
)
609 /* The non-sense with value is required because on LP64 platforms
610 * sizeof(unsigned int) != sizeof(unsigned long). On little-endian
611 * we could just type-cast the pointer, but why?
614 GetDrawableAttribute( dpy
, drawable
, GLX_EVENT_MASK_SGIX
, & value
);
620 glXCreatePixmap( Display
*dpy
, GLXFBConfig config
, Pixmap pixmap
,
621 const int *attrib_list
)
623 return CreateDrawable( dpy
, (__GLcontextModes
*) config
,
624 (Drawable
) pixmap
, attrib_list
,
630 glXCreateWindow( Display
*dpy
, GLXFBConfig config
, Window win
,
631 const int *attrib_list
)
633 return CreateDrawable( dpy
, (__GLcontextModes
*) config
,
634 (Drawable
) win
, attrib_list
,
640 glXDestroyPixmap(Display
*dpy
, GLXPixmap pixmap
)
642 DestroyDrawable( dpy
, (GLXDrawable
) pixmap
, X_GLXDestroyPixmap
);
647 glXDestroyWindow(Display
*dpy
, GLXWindow win
)
649 DestroyDrawable( dpy
, (GLXDrawable
) win
, X_GLXDestroyWindow
);
653 PUBLIC
GLX_ALIAS_VOID(glXDestroyGLXPbufferSGIX
,
654 (Display
*dpy
, GLXPbufferSGIX pbuf
),
658 PUBLIC
GLX_ALIAS_VOID(glXSelectEventSGIX
,
659 (Display
*dpy
, GLXDrawable drawable
, unsigned long mask
),
660 (dpy
, drawable
, mask
),
663 PUBLIC
GLX_ALIAS_VOID(glXGetSelectedEventSGIX
,
664 (Display
*dpy
, GLXDrawable drawable
, unsigned long *mask
),
665 (dpy
, drawable
, mask
),