1 /* -*- mode: c; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3; coding: utf-8-unix -*- */
3 * (C) Copyright IBM Corporation 2004
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
28 * Implementation of pbuffer related functions.
30 * \author Ian Romanick <idr@us.ibm.com>
34 #include "glxclient.h"
35 #include <X11/extensions/extutil.h>
36 #include <X11/extensions/Xext.h>
40 #include "glxextensions.h"
41 #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
;
167 #ifdef GLX_DIRECT_RENDERING
168 extern __GLXDRIdrawable
*
169 GetGLXDRIDrawable(Display
*dpy
, GLXDrawable drawable
, int * const scrn_num
);
172 determineTextureTarget(const int *attribs
, int numAttribs
)
177 for (i
= 0; i
< numAttribs
; i
++) {
178 if (attribs
[2 * i
] == GLX_TEXTURE_TARGET_EXT
) {
179 switch (attribs
[2 * i
+ 1]) {
180 case GLX_TEXTURE_2D_EXT
:
181 target
= GL_TEXTURE_2D
;
183 case GLX_TEXTURE_RECTANGLE_EXT
:
184 target
= GL_TEXTURE_RECTANGLE_ARB
;
195 * Get a drawable's attribute.
197 * This function is used to implement \c glXGetSelectedEvent and
198 * \c glXGetSelectedEventSGIX.
201 * This function dynamically determines whether to use the SGIX_pbuffer
202 * version of the protocol or the GLX 1.3 version of the protocol.
205 * The number of attributes returned is likely to be small, probably less than
206 * 10. Given that, this routine should try to use an array on the stack to
207 * capture the reply rather than always calling Xmalloc.
210 * This function needs to be modified to work with direct-rendering drivers.
213 GetDrawableAttribute( Display
*dpy
, GLXDrawable drawable
,
214 int attribute
, unsigned int *value
)
216 __GLXdisplayPrivate
*priv
;
217 xGLXGetDrawableAttributesReply reply
;
222 unsigned int num_attributes
;
224 if ( (dpy
== NULL
) || (drawable
== 0) ) {
228 priv
= __glXInitialize(dpy
);
229 GLboolean use_glx_1_3
= ((priv
->majorVersion
> 1)
230 || (priv
->minorVersion
>= 3));
235 opcode
= __glXSetupForCommand(dpy
);
242 xGLXGetDrawableAttributesReq
*req
;
244 GetReqExtra( GLXGetDrawableAttributes
, 4, req
);
245 req
->reqType
= opcode
;
246 req
->glxCode
= X_GLXGetDrawableAttributes
;
247 req
->drawable
= drawable
;
250 xGLXVendorPrivateWithReplyReq
*vpreq
;
252 GetReqExtra( GLXVendorPrivateWithReply
, 4, vpreq
);
253 data
= (CARD32
*) (vpreq
+ 1);
254 data
[0] = (CARD32
) drawable
;
256 vpreq
->reqType
= opcode
;
257 vpreq
->glxCode
= X_GLXVendorPrivateWithReply
;
258 vpreq
->vendorCode
= X_GLXvop_GetDrawableAttributesSGIX
;
261 _XReply(dpy
, (xReply
*) &reply
, 0, False
);
263 if (reply
.type
== X_Error
)
270 length
= reply
.length
;
273 num_attributes
= (use_glx_1_3
) ? reply
.numAttribs
: length
/ 2;
274 data
= (CARD32
*) Xmalloc( length
* sizeof(CARD32
) );
275 if ( data
== NULL
) {
276 /* Throw data on the floor */
277 _XEatData(dpy
, length
);
279 _XRead(dpy
, (char *)data
, length
* sizeof(CARD32
) );
281 /* Search the set of returned attributes for the attribute requested by
284 for ( i
= 0 ; i
< num_attributes
; i
++ ) {
285 if ( data
[i
*2] == attribute
) {
286 *value
= data
[ (i
*2) + 1 ];
291 #ifdef GLX_DIRECT_RENDERING
293 __GLXDRIdrawable
*pdraw
= GetGLXDRIDrawable(dpy
, drawable
, NULL
);
295 if (pdraw
!= NULL
&& !pdraw
->textureTarget
)
296 pdraw
->textureTarget
= determineTextureTarget((const int *)data
,
312 * Create a non-pbuffer GLX drawable.
315 * This function needs to be modified to work with direct-rendering drivers.
318 CreateDrawable( Display
*dpy
, const __GLcontextModes
* fbconfig
,
319 Drawable drawable
, const int *attrib_list
,
322 xGLXCreateWindowReq
* req
;
329 while (attrib_list
[i
* 2] != None
)
333 opcode
= __glXSetupForCommand(dpy
);
338 GetReqExtra( GLXCreateWindow
, 8 * i
, req
);
339 data
= (CARD32
*) (req
+ 1);
341 req
->reqType
= opcode
;
342 req
->glxCode
= glxCode
;
343 req
->screen
= (CARD32
) fbconfig
->screen
;
344 req
->fbconfig
= fbconfig
->fbconfigID
;
345 req
->window
= (CARD32
) drawable
;
346 req
->glxwindow
= (GLXWindow
) XAllocID(dpy
);
347 req
->numAttribs
= (CARD32
) i
;
349 memcpy( data
, attrib_list
, 8 * i
);
354 #ifdef GLX_DIRECT_RENDERING
356 /* FIXME: Maybe delay __DRIdrawable creation until the drawable
357 * is actually bound to a context... */
359 __GLXdisplayPrivate
* const priv
= __glXInitialize(dpy
);
360 __GLXDRIdrawable
*pdraw
;
361 __GLXscreenConfigs
*psc
;
363 psc
= &priv
->screenConfigs
[fbconfig
->screen
];
364 if (psc
->driScreen
== NULL
)
366 pdraw
= psc
->driScreen
->createDrawable(psc
, drawable
,
367 req
->glxwindow
, fbconfig
);
369 fprintf(stderr
, "failed to create drawable\n");
373 if (__glxHashInsert(psc
->drawHash
, req
->glxwindow
, pdraw
)) {
374 (*pdraw
->destroyDrawable
)(pdraw
);
375 return None
; /* FIXME: Check what we're supposed to do here... */
378 pdraw
->textureTarget
= determineTextureTarget(attrib_list
, i
);
382 return (GLXDrawable
)req
->glxwindow
;
387 * Destroy a non-pbuffer GLX drawable.
390 * This function needs to be modified to work with direct-rendering drivers.
393 DestroyDrawable( Display
* dpy
, GLXDrawable drawable
, CARD32 glxCode
)
395 xGLXDestroyPbufferReq
* req
;
398 if ( (dpy
== NULL
) || (drawable
== 0) ) {
403 opcode
= __glXSetupForCommand(dpy
);
409 GetReqExtra( GLXDestroyPbuffer
, 4, req
);
410 req
->reqType
= opcode
;
411 req
->glxCode
= glxCode
;
412 req
->pbuffer
= (GLXPbuffer
) drawable
;
417 #ifdef GLX_DIRECT_RENDERING
420 __GLXdisplayPrivate
* const priv
= __glXInitialize(dpy
);
421 __GLXDRIdrawable
*pdraw
= GetGLXDRIDrawable(dpy
, drawable
, &screen
);
422 __GLXscreenConfigs
*psc
= &priv
->screenConfigs
[screen
];
425 (*pdraw
->destroyDrawable
)(pdraw
);
426 __glxHashDelete(psc
->drawHash
, drawable
);
438 * This function is used to implement \c glXCreatePbuffer and
439 * \c glXCreateGLXPbufferSGIX.
442 * This function dynamically determines whether to use the SGIX_pbuffer
443 * version of the protocol or the GLX 1.3 version of the protocol.
446 * This function needs to be modified to work with direct-rendering drivers.
449 CreatePbuffer( Display
*dpy
, const __GLcontextModes
* fbconfig
,
450 unsigned int width
, unsigned int height
,
451 const int *attrib_list
, GLboolean size_in_attribs
)
453 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
461 while (attrib_list
[i
* 2])
465 opcode
= __glXSetupForCommand(dpy
);
472 if ( (priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3) ) {
473 xGLXCreatePbufferReq
* req
;
474 unsigned int extra
= (size_in_attribs
) ? 0 : 2;
476 GetReqExtra( GLXCreatePbuffer
, (8 * (i
+ extra
)), req
);
477 data
= (CARD32
*) (req
+ 1);
479 req
->reqType
= opcode
;
480 req
->glxCode
= X_GLXCreatePbuffer
;
481 req
->screen
= (CARD32
) fbconfig
->screen
;
482 req
->fbconfig
= fbconfig
->fbconfigID
;
483 req
->pbuffer
= (GLXPbuffer
) id
;
484 req
->numAttribs
= (CARD32
) (i
+ extra
);
486 if ( ! size_in_attribs
) {
487 data
[(2 * i
) + 0] = GLX_PBUFFER_WIDTH
;
488 data
[(2 * i
) + 1] = width
;
489 data
[(2 * i
) + 2] = GLX_PBUFFER_HEIGHT
;
490 data
[(2 * i
) + 3] = height
;
495 xGLXVendorPrivateReq
*vpreq
;
497 GetReqExtra( GLXVendorPrivate
, 20 + (8 * i
), vpreq
);
498 data
= (CARD32
*) (vpreq
+ 1);
500 vpreq
->reqType
= opcode
;
501 vpreq
->glxCode
= X_GLXVendorPrivate
;
502 vpreq
->vendorCode
= X_GLXvop_CreateGLXPbufferSGIX
;
504 data
[0] = (CARD32
) fbconfig
->screen
;
505 data
[1] = (CARD32
) fbconfig
->fbconfigID
;
506 data
[2] = (CARD32
) id
;
507 data
[3] = (CARD32
) width
;
508 data
[4] = (CARD32
) height
;
512 (void) memcpy( data
, attrib_list
, sizeof(CARD32
) * 2 * i
);
522 * Create a new pbuffer.
524 PUBLIC GLXPbufferSGIX
525 glXCreateGLXPbufferSGIX(Display
*dpy
, GLXFBConfigSGIX config
,
526 unsigned int width
, unsigned int height
,
529 return (GLXPbufferSGIX
) CreatePbuffer( dpy
, (__GLcontextModes
*) config
,
531 attrib_list
, GL_FALSE
);
536 * Create a new pbuffer.
539 glXCreatePbuffer(Display
*dpy
, GLXFBConfig config
, const int *attrib_list
)
541 int i
, width
, height
;
546 for (i
= 0; attrib_list
[i
* 2]; i
++) {
547 switch (attrib_list
[i
* 2]) {
548 case GLX_PBUFFER_WIDTH
:
549 width
= attrib_list
[i
* 2 + 1];
551 case GLX_PBUFFER_HEIGHT
:
552 height
= attrib_list
[i
* 2 + 1];
557 return (GLXPbuffer
) CreatePbuffer( dpy
, (__GLcontextModes
*) config
,
559 attrib_list
, GL_TRUE
);
564 * Destroy an existing pbuffer.
567 glXDestroyPbuffer(Display
*dpy
, GLXPbuffer pbuf
)
569 DestroyPbuffer( dpy
, pbuf
);
574 * Query an attribute of a drawable.
577 glXQueryDrawable(Display
*dpy
, GLXDrawable drawable
,
578 int attribute
, unsigned int *value
)
580 GetDrawableAttribute( dpy
, drawable
, attribute
, value
);
585 * Query an attribute of a pbuffer.
588 glXQueryGLXPbufferSGIX(Display
*dpy
, GLXPbufferSGIX drawable
,
589 int attribute
, unsigned int *value
)
591 return GetDrawableAttribute( dpy
, drawable
, attribute
, value
);
596 * Select the event mask for a drawable.
599 glXSelectEvent(Display
*dpy
, GLXDrawable drawable
, unsigned long mask
)
603 attribs
[0] = (CARD32
) GLX_EVENT_MASK
;
604 attribs
[1] = (CARD32
) mask
;
606 ChangeDrawableAttribute( dpy
, drawable
, attribs
, 1 );
611 * Get the selected event mask for a drawable.
614 glXGetSelectedEvent(Display
*dpy
, GLXDrawable drawable
, unsigned long *mask
)
619 /* The non-sense with value is required because on LP64 platforms
620 * sizeof(unsigned int) != sizeof(unsigned long). On little-endian
621 * we could just type-cast the pointer, but why?
624 GetDrawableAttribute( dpy
, drawable
, GLX_EVENT_MASK_SGIX
, & value
);
630 glXCreatePixmap( Display
*dpy
, GLXFBConfig config
, Pixmap pixmap
,
631 const int *attrib_list
)
633 return CreateDrawable( dpy
, (__GLcontextModes
*) config
,
634 (Drawable
) pixmap
, attrib_list
,
640 glXCreateWindow( Display
*dpy
, GLXFBConfig config
, Window win
,
641 const int *attrib_list
)
643 return CreateDrawable( dpy
, (__GLcontextModes
*) config
,
644 (Drawable
) win
, attrib_list
,
650 glXDestroyPixmap(Display
*dpy
, GLXPixmap pixmap
)
652 DestroyDrawable( dpy
, (GLXDrawable
) pixmap
, X_GLXDestroyPixmap
);
657 glXDestroyWindow(Display
*dpy
, GLXWindow win
)
659 DestroyDrawable( dpy
, (GLXDrawable
) win
, X_GLXDestroyWindow
);
663 PUBLIC
GLX_ALIAS_VOID(glXDestroyGLXPbufferSGIX
,
664 (Display
*dpy
, GLXPbufferSGIX pbuf
),
668 PUBLIC
GLX_ALIAS_VOID(glXSelectEventSGIX
,
669 (Display
*dpy
, GLXDrawable drawable
, unsigned long mask
),
670 (dpy
, drawable
, mask
),
673 PUBLIC
GLX_ALIAS_VOID(glXGetSelectedEventSGIX
,
674 (Display
*dpy
, GLXDrawable drawable
, unsigned long *mask
),
675 (dpy
, drawable
, mask
),