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>
38 #include "glxextensions.h"
40 #define WARN_ONCE_GLX_1_3(a, b) { \
41 static int warned=1; \
43 warn_GLX_1_3((a), b ); \
49 * Emit a warning when clients use GLX 1.3 functions on pre-1.3 systems.
52 warn_GLX_1_3(Display
*dpy
, const char *function_name
)
54 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
56 if (priv
->minorVersion
< 3) {
58 "WARNING: Application calling GLX 1.3 function \"%s\" "
59 "when GLX 1.3 is not supported! This is an application bug!\n",
66 * Change a drawable's attribute.
68 * This function is used to implement \c glXSelectEvent and
69 * \c glXSelectEventSGIX.
72 * This function dynamically determines whether to use the SGIX_pbuffer
73 * version of the protocol or the GLX 1.3 version of the protocol.
76 * This function needs to be modified to work with direct-rendering drivers.
79 ChangeDrawableAttribute(Display
* dpy
, GLXDrawable drawable
,
80 const CARD32
* attribs
, size_t num_attribs
)
82 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
86 if ((dpy
== NULL
) || (drawable
== 0)) {
90 opcode
= __glXSetupForCommand(dpy
);
96 if ((priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3)) {
97 xGLXChangeDrawableAttributesReq
*req
;
99 GetReqExtra(GLXChangeDrawableAttributes
, 8 + (8 * num_attribs
), req
);
100 output
= (CARD32
*) (req
+ 1);
102 req
->reqType
= opcode
;
103 req
->glxCode
= X_GLXChangeDrawableAttributes
;
104 req
->drawable
= drawable
;
105 req
->numAttribs
= (CARD32
) num_attribs
;
108 xGLXVendorPrivateWithReplyReq
*vpreq
;
110 GetReqExtra(GLXVendorPrivateWithReply
, 4 + (8 * num_attribs
), vpreq
);
111 output
= (CARD32
*) (vpreq
+ 1);
113 vpreq
->reqType
= opcode
;
114 vpreq
->glxCode
= X_GLXVendorPrivateWithReply
;
115 vpreq
->vendorCode
= X_GLXvop_ChangeDrawableAttributesSGIX
;
117 output
[0] = (CARD32
) drawable
;
121 (void) memcpy(output
, attribs
, sizeof(CARD32
) * 2 * num_attribs
);
133 * This function is used to implement \c glXDestroyPbuffer and
134 * \c glXDestroyGLXPbufferSGIX.
137 * This function dynamically determines whether to use the SGIX_pbuffer
138 * version of the protocol or the GLX 1.3 version of the protocol.
141 * This function needs to be modified to work with direct-rendering drivers.
144 DestroyPbuffer(Display
* dpy
, GLXDrawable drawable
)
146 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
149 if ((dpy
== NULL
) || (drawable
== 0)) {
153 opcode
= __glXSetupForCommand(dpy
);
159 if ((priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3)) {
160 xGLXDestroyPbufferReq
*req
;
162 GetReq(GLXDestroyPbuffer
, req
);
163 req
->reqType
= opcode
;
164 req
->glxCode
= X_GLXDestroyPbuffer
;
165 req
->pbuffer
= (GLXPbuffer
) drawable
;
168 xGLXVendorPrivateWithReplyReq
*vpreq
;
171 GetReqExtra(GLXVendorPrivateWithReply
, 4, vpreq
);
172 data
= (CARD32
*) (vpreq
+ 1);
174 data
[0] = (CARD32
) drawable
;
176 vpreq
->reqType
= opcode
;
177 vpreq
->glxCode
= X_GLXVendorPrivateWithReply
;
178 vpreq
->vendorCode
= X_GLXvop_DestroyGLXPbufferSGIX
;
188 #ifdef GLX_DIRECT_RENDERING
189 extern __GLXDRIdrawable
*GetGLXDRIDrawable(Display
* dpy
,
190 GLXDrawable drawable
,
191 int *const scrn_num
);
194 determineTextureTarget(const int *attribs
, int numAttribs
)
199 for (i
= 0; i
< numAttribs
; i
++) {
200 if (attribs
[2 * i
] == GLX_TEXTURE_TARGET_EXT
) {
201 switch (attribs
[2 * i
+ 1]) {
202 case GLX_TEXTURE_2D_EXT
:
203 target
= GL_TEXTURE_2D
;
205 case GLX_TEXTURE_RECTANGLE_EXT
:
206 target
= GL_TEXTURE_RECTANGLE_ARB
;
217 determineTextureFormat(const int *attribs
, int numAttribs
)
221 for (i
= 0; i
< numAttribs
; i
++) {
222 if (attribs
[2 * i
] == GLX_TEXTURE_FORMAT_EXT
)
223 return attribs
[2 * i
+ 1];
231 * Get a drawable's attribute.
233 * This function is used to implement \c glXGetSelectedEvent and
234 * \c glXGetSelectedEventSGIX.
237 * This function dynamically determines whether to use the SGIX_pbuffer
238 * version of the protocol or the GLX 1.3 version of the protocol.
241 * The number of attributes returned is likely to be small, probably less than
242 * 10. Given that, this routine should try to use an array on the stack to
243 * capture the reply rather than always calling Xmalloc.
246 * This function needs to be modified to work with direct-rendering drivers.
249 GetDrawableAttribute(Display
* dpy
, GLXDrawable drawable
,
250 int attribute
, unsigned int *value
)
252 __GLXdisplayPrivate
*priv
;
253 xGLXGetDrawableAttributesReply reply
;
258 unsigned int num_attributes
;
259 GLboolean use_glx_1_3
;
261 if ((dpy
== NULL
) || (drawable
== 0)) {
265 priv
= __glXInitialize(dpy
);
266 use_glx_1_3
= ((priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3));
271 opcode
= __glXSetupForCommand(dpy
);
278 xGLXGetDrawableAttributesReq
*req
;
280 GetReqExtra(GLXGetDrawableAttributes
, 4, req
);
281 req
->reqType
= opcode
;
282 req
->glxCode
= X_GLXGetDrawableAttributes
;
283 req
->drawable
= drawable
;
286 xGLXVendorPrivateWithReplyReq
*vpreq
;
288 GetReqExtra(GLXVendorPrivateWithReply
, 4, vpreq
);
289 data
= (CARD32
*) (vpreq
+ 1);
290 data
[0] = (CARD32
) drawable
;
292 vpreq
->reqType
= opcode
;
293 vpreq
->glxCode
= X_GLXVendorPrivateWithReply
;
294 vpreq
->vendorCode
= X_GLXvop_GetDrawableAttributesSGIX
;
297 _XReply(dpy
, (xReply
*) & reply
, 0, False
);
299 if (reply
.type
== X_Error
) {
305 length
= reply
.length
;
307 num_attributes
= (use_glx_1_3
) ? reply
.numAttribs
: length
/ 2;
308 data
= (CARD32
*) Xmalloc(length
* sizeof(CARD32
));
310 /* Throw data on the floor */
311 _XEatData(dpy
, length
);
314 _XRead(dpy
, (char *) data
, length
* sizeof(CARD32
));
316 /* Search the set of returned attributes for the attribute requested by
319 for (i
= 0; i
< num_attributes
; i
++) {
320 if (data
[i
* 2] == attribute
) {
321 *value
= data
[(i
* 2) + 1];
326 #ifdef GLX_DIRECT_RENDERING
328 __GLXDRIdrawable
*pdraw
= GetGLXDRIDrawable(dpy
, drawable
, NULL
);
330 if (pdraw
!= NULL
&& !pdraw
->textureTarget
)
331 pdraw
->textureTarget
=
332 determineTextureTarget((const int *) data
, num_attributes
);
333 if (pdraw
!= NULL
&& !pdraw
->textureFormat
)
334 pdraw
->textureFormat
=
335 determineTextureFormat((const int *) data
, num_attributes
);
350 * Create a non-pbuffer GLX drawable.
353 * This function needs to be modified to work with direct-rendering drivers.
356 CreateDrawable(Display
* dpy
, const __GLcontextModes
* fbconfig
,
357 Drawable drawable
, const int *attrib_list
, CARD8 glxCode
)
359 xGLXCreateWindowReq
*req
;
366 while (attrib_list
[i
* 2] != None
)
370 opcode
= __glXSetupForCommand(dpy
);
375 GetReqExtra(GLXCreateWindow
, 8 * i
, req
);
376 data
= (CARD32
*) (req
+ 1);
378 req
->reqType
= opcode
;
379 req
->glxCode
= glxCode
;
380 req
->screen
= (CARD32
) fbconfig
->screen
;
381 req
->fbconfig
= fbconfig
->fbconfigID
;
382 req
->window
= (CARD32
) drawable
;
383 req
->glxwindow
= (GLXWindow
) XAllocID(dpy
);
384 req
->numAttribs
= (CARD32
) i
;
386 memcpy(data
, attrib_list
, 8 * i
);
391 #ifdef GLX_DIRECT_RENDERING
393 /* FIXME: Maybe delay __DRIdrawable creation until the drawable
394 * is actually bound to a context... */
396 __GLXdisplayPrivate
*const priv
= __glXInitialize(dpy
);
397 __GLXDRIdrawable
*pdraw
;
398 __GLXscreenConfigs
*psc
;
400 psc
= &priv
->screenConfigs
[fbconfig
->screen
];
401 if (psc
->driScreen
== NULL
)
403 pdraw
= psc
->driScreen
->createDrawable(psc
, drawable
,
404 req
->glxwindow
, fbconfig
);
406 fprintf(stderr
, "failed to create drawable\n");
410 if (__glxHashInsert(psc
->drawHash
, req
->glxwindow
, pdraw
)) {
411 (*pdraw
->destroyDrawable
) (pdraw
);
412 return None
; /* FIXME: Check what we're supposed to do here... */
415 pdraw
->textureTarget
= determineTextureTarget(attrib_list
, i
);
416 pdraw
->textureFormat
= determineTextureFormat(attrib_list
, i
);
420 return (GLXDrawable
) req
->glxwindow
;
425 * Destroy a non-pbuffer GLX drawable.
428 * This function needs to be modified to work with direct-rendering drivers.
431 DestroyDrawable(Display
* dpy
, GLXDrawable drawable
, CARD32 glxCode
)
433 xGLXDestroyPbufferReq
*req
;
436 if ((dpy
== NULL
) || (drawable
== 0)) {
441 opcode
= __glXSetupForCommand(dpy
);
447 GetReqExtra(GLXDestroyPbuffer
, 4, req
);
448 req
->reqType
= opcode
;
449 req
->glxCode
= glxCode
;
450 req
->pbuffer
= (GLXPbuffer
) drawable
;
455 #ifdef GLX_DIRECT_RENDERING
458 __GLXdisplayPrivate
*const priv
= __glXInitialize(dpy
);
459 __GLXDRIdrawable
*pdraw
= GetGLXDRIDrawable(dpy
, drawable
, &screen
);
460 __GLXscreenConfigs
*psc
= &priv
->screenConfigs
[screen
];
463 (*pdraw
->destroyDrawable
) (pdraw
);
464 __glxHashDelete(psc
->drawHash
, drawable
);
476 * This function is used to implement \c glXCreatePbuffer and
477 * \c glXCreateGLXPbufferSGIX.
480 * This function dynamically determines whether to use the SGIX_pbuffer
481 * version of the protocol or the GLX 1.3 version of the protocol.
484 * This function needs to be modified to work with direct-rendering drivers.
487 CreatePbuffer(Display
* dpy
, const __GLcontextModes
* fbconfig
,
488 unsigned int width
, unsigned int height
,
489 const int *attrib_list
, GLboolean size_in_attribs
)
491 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
499 while (attrib_list
[i
* 2])
503 opcode
= __glXSetupForCommand(dpy
);
510 if ((priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3)) {
511 xGLXCreatePbufferReq
*req
;
512 unsigned int extra
= (size_in_attribs
) ? 0 : 2;
514 GetReqExtra(GLXCreatePbuffer
, (8 * (i
+ extra
)), req
);
515 data
= (CARD32
*) (req
+ 1);
517 req
->reqType
= opcode
;
518 req
->glxCode
= X_GLXCreatePbuffer
;
519 req
->screen
= (CARD32
) fbconfig
->screen
;
520 req
->fbconfig
= fbconfig
->fbconfigID
;
521 req
->pbuffer
= (GLXPbuffer
) id
;
522 req
->numAttribs
= (CARD32
) (i
+ extra
);
524 if (!size_in_attribs
) {
525 data
[(2 * i
) + 0] = GLX_PBUFFER_WIDTH
;
526 data
[(2 * i
) + 1] = width
;
527 data
[(2 * i
) + 2] = GLX_PBUFFER_HEIGHT
;
528 data
[(2 * i
) + 3] = height
;
533 xGLXVendorPrivateReq
*vpreq
;
535 GetReqExtra(GLXVendorPrivate
, 20 + (8 * i
), vpreq
);
536 data
= (CARD32
*) (vpreq
+ 1);
538 vpreq
->reqType
= opcode
;
539 vpreq
->glxCode
= X_GLXVendorPrivate
;
540 vpreq
->vendorCode
= X_GLXvop_CreateGLXPbufferSGIX
;
542 data
[0] = (CARD32
) fbconfig
->screen
;
543 data
[1] = (CARD32
) fbconfig
->fbconfigID
;
544 data
[2] = (CARD32
) id
;
545 data
[3] = (CARD32
) width
;
546 data
[4] = (CARD32
) height
;
550 (void) memcpy(data
, attrib_list
, sizeof(CARD32
) * 2 * i
);
560 * Create a new pbuffer.
562 PUBLIC GLXPbufferSGIX
563 glXCreateGLXPbufferSGIX(Display
* dpy
, GLXFBConfigSGIX config
,
564 unsigned int width
, unsigned int height
,
567 return (GLXPbufferSGIX
) CreatePbuffer(dpy
, (__GLcontextModes
*) config
,
569 attrib_list
, GL_FALSE
);
574 * Create a new pbuffer.
577 glXCreatePbuffer(Display
* dpy
, GLXFBConfig config
, const int *attrib_list
)
579 int i
, width
, height
;
584 WARN_ONCE_GLX_1_3(dpy
, __func__
);
586 for (i
= 0; attrib_list
[i
* 2]; i
++) {
587 switch (attrib_list
[i
* 2]) {
588 case GLX_PBUFFER_WIDTH
:
589 width
= attrib_list
[i
* 2 + 1];
591 case GLX_PBUFFER_HEIGHT
:
592 height
= attrib_list
[i
* 2 + 1];
597 return (GLXPbuffer
) CreatePbuffer(dpy
, (__GLcontextModes
*) config
,
598 width
, height
, attrib_list
, GL_TRUE
);
603 * Destroy an existing pbuffer.
606 glXDestroyPbuffer(Display
* dpy
, GLXPbuffer pbuf
)
608 DestroyPbuffer(dpy
, pbuf
);
613 * Query an attribute of a drawable.
616 glXQueryDrawable(Display
* dpy
, GLXDrawable drawable
,
617 int attribute
, unsigned int *value
)
619 WARN_ONCE_GLX_1_3(dpy
, __func__
);
620 GetDrawableAttribute(dpy
, drawable
, attribute
, value
);
625 * Query an attribute of a pbuffer.
628 glXQueryGLXPbufferSGIX(Display
* dpy
, GLXPbufferSGIX drawable
,
629 int attribute
, unsigned int *value
)
631 return GetDrawableAttribute(dpy
, drawable
, attribute
, value
);
636 * Select the event mask for a drawable.
639 glXSelectEvent(Display
* dpy
, GLXDrawable drawable
, unsigned long mask
)
643 attribs
[0] = (CARD32
) GLX_EVENT_MASK
;
644 attribs
[1] = (CARD32
) mask
;
646 ChangeDrawableAttribute(dpy
, drawable
, attribs
, 1);
651 * Get the selected event mask for a drawable.
654 glXGetSelectedEvent(Display
* dpy
, GLXDrawable drawable
, unsigned long *mask
)
659 /* The non-sense with value is required because on LP64 platforms
660 * sizeof(unsigned int) != sizeof(unsigned long). On little-endian
661 * we could just type-cast the pointer, but why?
664 GetDrawableAttribute(dpy
, drawable
, GLX_EVENT_MASK_SGIX
, &value
);
670 glXCreatePixmap(Display
* dpy
, GLXFBConfig config
, Pixmap pixmap
,
671 const int *attrib_list
)
673 WARN_ONCE_GLX_1_3(dpy
, __func__
);
675 return CreateDrawable(dpy
, (__GLcontextModes
*) config
,
676 (Drawable
) pixmap
, attrib_list
, X_GLXCreatePixmap
);
681 glXCreateWindow(Display
* dpy
, GLXFBConfig config
, Window win
,
682 const int *attrib_list
)
684 WARN_ONCE_GLX_1_3(dpy
, __func__
);
686 return CreateDrawable(dpy
, (__GLcontextModes
*) config
,
687 (Drawable
) win
, attrib_list
, X_GLXCreateWindow
);
692 glXDestroyPixmap(Display
* dpy
, GLXPixmap pixmap
)
694 WARN_ONCE_GLX_1_3(dpy
, __func__
);
696 DestroyDrawable(dpy
, (GLXDrawable
) pixmap
, X_GLXDestroyPixmap
);
701 glXDestroyWindow(Display
* dpy
, GLXWindow win
)
703 WARN_ONCE_GLX_1_3(dpy
, __func__
);
705 DestroyDrawable(dpy
, (GLXDrawable
) win
, X_GLXDestroyWindow
);
710 GLX_ALIAS_VOID(glXDestroyGLXPbufferSGIX
,
711 (Display
* dpy
, GLXPbufferSGIX pbuf
),
712 (dpy
, pbuf
), glXDestroyPbuffer
)
715 GLX_ALIAS_VOID(glXSelectEventSGIX
,
716 (Display
* dpy
, GLXDrawable drawable
,
717 unsigned long mask
), (dpy
, drawable
, mask
), glXSelectEvent
)
720 GLX_ALIAS_VOID(glXGetSelectedEventSGIX
,
721 (Display
* dpy
, GLXDrawable drawable
,
722 unsigned long *mask
), (dpy
, drawable
, mask
),