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
*GetGLXDRIDrawable(Display
* dpy
,
168 GLXDrawable drawable
,
169 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
) {
269 length
= reply
.length
;
271 num_attributes
= (use_glx_1_3
) ? reply
.numAttribs
: length
/ 2;
272 data
= (CARD32
*) Xmalloc(length
* sizeof(CARD32
));
274 /* Throw data on the floor */
275 _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
=
296 determineTextureTarget((const int *) data
, num_attributes
);
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
, CARD8 glxCode
)
320 xGLXCreateWindowReq
*req
;
327 while (attrib_list
[i
* 2] != None
)
331 opcode
= __glXSetupForCommand(dpy
);
336 GetReqExtra(GLXCreateWindow
, 8 * i
, req
);
337 data
= (CARD32
*) (req
+ 1);
339 req
->reqType
= opcode
;
340 req
->glxCode
= glxCode
;
341 req
->screen
= (CARD32
) fbconfig
->screen
;
342 req
->fbconfig
= fbconfig
->fbconfigID
;
343 req
->window
= (CARD32
) drawable
;
344 req
->glxwindow
= (GLXWindow
) XAllocID(dpy
);
345 req
->numAttribs
= (CARD32
) i
;
347 memcpy(data
, attrib_list
, 8 * i
);
352 #ifdef GLX_DIRECT_RENDERING
354 /* FIXME: Maybe delay __DRIdrawable creation until the drawable
355 * is actually bound to a context... */
357 __GLXdisplayPrivate
*const priv
= __glXInitialize(dpy
);
358 __GLXDRIdrawable
*pdraw
;
359 __GLXscreenConfigs
*psc
;
361 psc
= &priv
->screenConfigs
[fbconfig
->screen
];
362 if (psc
->driScreen
== NULL
)
364 pdraw
= psc
->driScreen
->createDrawable(psc
, drawable
,
365 req
->glxwindow
, fbconfig
);
367 fprintf(stderr
, "failed to create drawable\n");
371 if (__glxHashInsert(psc
->drawHash
, req
->glxwindow
, pdraw
)) {
372 (*pdraw
->destroyDrawable
) (pdraw
);
373 return None
; /* FIXME: Check what we're supposed to do here... */
376 pdraw
->textureTarget
= determineTextureTarget(attrib_list
, i
);
380 return (GLXDrawable
) req
->glxwindow
;
385 * Destroy a non-pbuffer GLX drawable.
388 * This function needs to be modified to work with direct-rendering drivers.
391 DestroyDrawable(Display
* dpy
, GLXDrawable drawable
, CARD32 glxCode
)
393 xGLXDestroyPbufferReq
*req
;
396 if ((dpy
== NULL
) || (drawable
== 0)) {
401 opcode
= __glXSetupForCommand(dpy
);
407 GetReqExtra(GLXDestroyPbuffer
, 4, req
);
408 req
->reqType
= opcode
;
409 req
->glxCode
= glxCode
;
410 req
->pbuffer
= (GLXPbuffer
) drawable
;
415 #ifdef GLX_DIRECT_RENDERING
418 __GLXdisplayPrivate
*const priv
= __glXInitialize(dpy
);
419 __GLXDRIdrawable
*pdraw
= GetGLXDRIDrawable(dpy
, drawable
, &screen
);
420 __GLXscreenConfigs
*psc
= &priv
->screenConfigs
[screen
];
423 (*pdraw
->destroyDrawable
) (pdraw
);
424 __glxHashDelete(psc
->drawHash
, drawable
);
436 * This function is used to implement \c glXCreatePbuffer and
437 * \c glXCreateGLXPbufferSGIX.
440 * This function dynamically determines whether to use the SGIX_pbuffer
441 * version of the protocol or the GLX 1.3 version of the protocol.
444 * This function needs to be modified to work with direct-rendering drivers.
447 CreatePbuffer(Display
* dpy
, const __GLcontextModes
* fbconfig
,
448 unsigned int width
, unsigned int height
,
449 const int *attrib_list
, GLboolean size_in_attribs
)
451 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
459 while (attrib_list
[i
* 2])
463 opcode
= __glXSetupForCommand(dpy
);
470 if ((priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3)) {
471 xGLXCreatePbufferReq
*req
;
472 unsigned int extra
= (size_in_attribs
) ? 0 : 2;
474 GetReqExtra(GLXCreatePbuffer
, (8 * (i
+ extra
)), req
);
475 data
= (CARD32
*) (req
+ 1);
477 req
->reqType
= opcode
;
478 req
->glxCode
= X_GLXCreatePbuffer
;
479 req
->screen
= (CARD32
) fbconfig
->screen
;
480 req
->fbconfig
= fbconfig
->fbconfigID
;
481 req
->pbuffer
= (GLXPbuffer
) id
;
482 req
->numAttribs
= (CARD32
) (i
+ extra
);
484 if (!size_in_attribs
) {
485 data
[(2 * i
) + 0] = GLX_PBUFFER_WIDTH
;
486 data
[(2 * i
) + 1] = width
;
487 data
[(2 * i
) + 2] = GLX_PBUFFER_HEIGHT
;
488 data
[(2 * i
) + 3] = height
;
493 xGLXVendorPrivateReq
*vpreq
;
495 GetReqExtra(GLXVendorPrivate
, 20 + (8 * i
), vpreq
);
496 data
= (CARD32
*) (vpreq
+ 1);
498 vpreq
->reqType
= opcode
;
499 vpreq
->glxCode
= X_GLXVendorPrivate
;
500 vpreq
->vendorCode
= X_GLXvop_CreateGLXPbufferSGIX
;
502 data
[0] = (CARD32
) fbconfig
->screen
;
503 data
[1] = (CARD32
) fbconfig
->fbconfigID
;
504 data
[2] = (CARD32
) id
;
505 data
[3] = (CARD32
) width
;
506 data
[4] = (CARD32
) height
;
510 (void) memcpy(data
, attrib_list
, sizeof(CARD32
) * 2 * i
);
520 * Create a new pbuffer.
522 PUBLIC GLXPbufferSGIX
523 glXCreateGLXPbufferSGIX(Display
* dpy
, GLXFBConfigSGIX config
,
524 unsigned int width
, unsigned int height
,
527 return (GLXPbufferSGIX
) CreatePbuffer(dpy
, (__GLcontextModes
*) config
,
529 attrib_list
, GL_FALSE
);
534 * Create a new pbuffer.
537 glXCreatePbuffer(Display
* dpy
, GLXFBConfig config
, const int *attrib_list
)
539 int i
, width
, height
;
544 for (i
= 0; attrib_list
[i
* 2]; i
++) {
545 switch (attrib_list
[i
* 2]) {
546 case GLX_PBUFFER_WIDTH
:
547 width
= attrib_list
[i
* 2 + 1];
549 case GLX_PBUFFER_HEIGHT
:
550 height
= attrib_list
[i
* 2 + 1];
555 return (GLXPbuffer
) CreatePbuffer(dpy
, (__GLcontextModes
*) config
,
556 width
, height
, attrib_list
, GL_TRUE
);
561 * Destroy an existing pbuffer.
564 glXDestroyPbuffer(Display
* dpy
, GLXPbuffer pbuf
)
566 DestroyPbuffer(dpy
, pbuf
);
571 * Query an attribute of a drawable.
574 glXQueryDrawable(Display
* dpy
, GLXDrawable drawable
,
575 int attribute
, unsigned int *value
)
577 GetDrawableAttribute(dpy
, drawable
, attribute
, value
);
582 * Query an attribute of a pbuffer.
585 glXQueryGLXPbufferSGIX(Display
* dpy
, GLXPbufferSGIX drawable
,
586 int attribute
, unsigned int *value
)
588 return GetDrawableAttribute(dpy
, drawable
, attribute
, value
);
593 * Select the event mask for a drawable.
596 glXSelectEvent(Display
* dpy
, GLXDrawable drawable
, unsigned long mask
)
600 attribs
[0] = (CARD32
) GLX_EVENT_MASK
;
601 attribs
[1] = (CARD32
) mask
;
603 ChangeDrawableAttribute(dpy
, drawable
, attribs
, 1);
608 * Get the selected event mask for a drawable.
611 glXGetSelectedEvent(Display
* dpy
, GLXDrawable drawable
, unsigned long *mask
)
616 /* The non-sense with value is required because on LP64 platforms
617 * sizeof(unsigned int) != sizeof(unsigned long). On little-endian
618 * we could just type-cast the pointer, but why?
621 GetDrawableAttribute(dpy
, drawable
, GLX_EVENT_MASK_SGIX
, &value
);
627 glXCreatePixmap(Display
* dpy
, GLXFBConfig config
, Pixmap pixmap
,
628 const int *attrib_list
)
630 return CreateDrawable(dpy
, (__GLcontextModes
*) config
,
631 (Drawable
) pixmap
, attrib_list
, X_GLXCreatePixmap
);
636 glXCreateWindow(Display
* dpy
, GLXFBConfig config
, Window win
,
637 const int *attrib_list
)
639 return CreateDrawable(dpy
, (__GLcontextModes
*) config
,
640 (Drawable
) win
, attrib_list
, X_GLXCreateWindow
);
645 glXDestroyPixmap(Display
* dpy
, GLXPixmap pixmap
)
647 DestroyDrawable(dpy
, (GLXDrawable
) pixmap
, X_GLXDestroyPixmap
);
652 glXDestroyWindow(Display
* dpy
, GLXWindow win
)
654 DestroyDrawable(dpy
, (GLXDrawable
) win
, X_GLXDestroyWindow
);
659 GLX_ALIAS_VOID(glXDestroyGLXPbufferSGIX
,
660 (Display
* dpy
, GLXPbufferSGIX pbuf
),
661 (dpy
, pbuf
), glXDestroyPbuffer
)
664 GLX_ALIAS_VOID(glXSelectEventSGIX
,
665 (Display
* dpy
, GLXDrawable drawable
,
666 unsigned long mask
), (dpy
, drawable
, mask
),
670 GLX_ALIAS_VOID(glXGetSelectedEventSGIX
,
671 (Display
* dpy
, GLXDrawable drawable
,
672 unsigned long *mask
), (dpy
, drawable
, mask
),