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 #ifdef GLX_USE_APPLEGL
42 #include "apple_glx_drawable.h"
43 #include "glx_error.h"
46 #define WARN_ONCE_GLX_1_3(a, b) { \
47 static int warned=1; \
49 warn_GLX_1_3((a), b ); \
55 * Emit a warning when clients use GLX 1.3 functions on pre-1.3 systems.
58 warn_GLX_1_3(Display * dpy, const char *function_name)
60 struct glx_display *priv = __glXInitialize(dpy);
62 if (priv->minorVersion < 3) {
64 "WARNING: Application calling GLX 1.3 function \"%s\" "
65 "when GLX 1.3 is not supported! This is an application bug!\n",
70 #ifndef GLX_USE_APPLEGL
72 * Change a drawable's attribute.
74 * This function is used to implement \c glXSelectEvent and
75 * \c glXSelectEventSGIX.
78 * This function dynamically determines whether to use the SGIX_pbuffer
79 * version of the protocol or the GLX 1.3 version of the protocol.
82 ChangeDrawableAttribute(Display * dpy, GLXDrawable drawable,
83 const CARD32 * attribs, size_t num_attribs)
85 struct glx_display *priv = __glXInitialize(dpy);
86 #ifdef GLX_DIRECT_RENDERING
87 __GLXDRIdrawable *pdraw;
93 if ((dpy == NULL) || (drawable == 0)) {
97 opcode = __glXSetupForCommand(dpy);
103 if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
104 xGLXChangeDrawableAttributesReq *req;
106 GetReqExtra(GLXChangeDrawableAttributes, 8 * num_attribs, req);
107 output = (CARD32 *) (req + 1);
109 req->reqType = opcode;
110 req->glxCode = X_GLXChangeDrawableAttributes;
111 req->drawable = drawable;
112 req->numAttribs = (CARD32) num_attribs;
115 xGLXVendorPrivateWithReplyReq *vpreq;
117 GetReqExtra(GLXVendorPrivateWithReply, 8 + (8 * num_attribs), vpreq);
118 output = (CARD32 *) (vpreq + 1);
120 vpreq->reqType = opcode;
121 vpreq->glxCode = X_GLXVendorPrivateWithReply;
122 vpreq->vendorCode = X_GLXvop_ChangeDrawableAttributesSGIX;
124 output[0] = (CARD32) drawable;
125 output[1] = num_attribs;
129 (void) memcpy(output, attribs, sizeof(CARD32) * 2 * num_attribs);
134 #ifdef GLX_DIRECT_RENDERING
135 pdraw = GetGLXDRIDrawable(dpy, drawable);
140 for (i = 0; i < num_attribs; i++) {
141 switch(attribs[i * 2]) {
143 /* Keep a local copy for masking out DRI2 proto events as needed */
144 pdraw->eventMask = attribs[i * 2 + 1];
154 #ifdef GLX_DIRECT_RENDERING
156 determineTextureTarget(const int *attribs, int numAttribs)
161 for (i = 0; i < numAttribs; i++) {
162 if (attribs[2 * i] == GLX_TEXTURE_TARGET_EXT) {
163 switch (attribs[2 * i + 1]) {
164 case GLX_TEXTURE_2D_EXT:
165 target = GL_TEXTURE_2D;
167 case GLX_TEXTURE_RECTANGLE_EXT:
168 target = GL_TEXTURE_RECTANGLE_ARB;
178 determineTextureFormat(const int *attribs, int numAttribs)
182 for (i = 0; i < numAttribs; i++) {
183 if (attribs[2 * i] == GLX_TEXTURE_FORMAT_EXT)
184 return attribs[2 * i + 1];
191 CreateDRIDrawable(Display *dpy, struct glx_config *config,
192 XID drawable, XID glxdrawable,
193 const int *attrib_list, size_t num_attribs)
195 struct glx_display *const priv = __glXInitialize(dpy);
196 __GLXDRIdrawable *pdraw;
197 struct glx_screen *psc;
199 psc = priv->screens[config->screen];
200 if (psc->driScreen == NULL)
203 pdraw = psc->driScreen->createDrawable(psc, drawable,
204 glxdrawable, config);
206 fprintf(stderr, "failed to create drawable\n");
210 if (__glxHashInsert(priv->drawHash, glxdrawable, pdraw)) {
211 (*pdraw->destroyDrawable) (pdraw);
212 return; /* FIXME: Check what we're supposed to do here... */
215 pdraw->textureTarget = determineTextureTarget(attrib_list, num_attribs);
216 pdraw->textureFormat = determineTextureFormat(attrib_list, num_attribs);
220 DestroyDRIDrawable(Display *dpy, GLXDrawable drawable, int destroy_xdrawable)
222 struct glx_display *const priv = __glXInitialize(dpy);
223 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
227 xid = pdraw->xDrawable;
228 (*pdraw->destroyDrawable) (pdraw);
229 __glxHashDelete(priv->drawHash, drawable);
230 if (destroy_xdrawable)
231 XFreePixmap(priv->dpy, xid);
238 CreateDRIDrawable(Display *dpy, const struct glx_config * fbconfig,
239 XID drawable, XID glxdrawable,
240 const int *attrib_list, size_t num_attribs)
245 DestroyDRIDrawable(Display *dpy, GLXDrawable drawable, int destroy_xdrawable)
252 * Get a drawable's attribute.
254 * This function is used to implement \c glXGetSelectedEvent and
255 * \c glXGetSelectedEventSGIX.
258 * This function dynamically determines whether to use the SGIX_pbuffer
259 * version of the protocol or the GLX 1.3 version of the protocol.
262 * The number of attributes returned is likely to be small, probably less than
263 * 10. Given that, this routine should try to use an array on the stack to
264 * capture the reply rather than always calling Xmalloc.
267 GetDrawableAttribute(Display * dpy, GLXDrawable drawable,
268 int attribute, unsigned int *value)
270 struct glx_display *priv;
271 xGLXGetDrawableAttributesReply reply;
276 unsigned int num_attributes;
277 GLboolean use_glx_1_3;
279 if ((dpy == NULL) || (drawable == 0)) {
283 priv = __glXInitialize(dpy);
284 use_glx_1_3 = ((priv->majorVersion > 1) || (priv->minorVersion >= 3));
289 opcode = __glXSetupForCommand(dpy);
296 xGLXGetDrawableAttributesReq *req;
298 GetReq(GLXGetDrawableAttributes, req);
299 req->reqType = opcode;
300 req->glxCode = X_GLXGetDrawableAttributes;
301 req->drawable = drawable;
304 xGLXVendorPrivateWithReplyReq *vpreq;
306 GetReqExtra(GLXVendorPrivateWithReply, 4, vpreq);
307 data = (CARD32 *) (vpreq + 1);
308 data[0] = (CARD32) drawable;
310 vpreq->reqType = opcode;
311 vpreq->glxCode = X_GLXVendorPrivateWithReply;
312 vpreq->vendorCode = X_GLXvop_GetDrawableAttributesSGIX;
315 _XReply(dpy, (xReply *) & reply, 0, False);
317 if (reply.type == X_Error) {
323 length = reply.length;
325 num_attributes = (use_glx_1_3) ? reply.numAttribs : length / 2;
326 data = (CARD32 *) Xmalloc(length * sizeof(CARD32));
328 /* Throw data on the floor */
329 _XEatData(dpy, length);
332 _XRead(dpy, (char *) data, length * sizeof(CARD32));
334 /* Search the set of returned attributes for the attribute requested by
337 for (i = 0; i < num_attributes; i++) {
338 if (data[i * 2] == attribute) {
339 *value = data[(i * 2) + 1];
344 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
346 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
348 if (pdraw != NULL && !pdraw->textureTarget)
349 pdraw->textureTarget =
350 determineTextureTarget((const int *) data, num_attributes);
351 if (pdraw != NULL && !pdraw->textureFormat)
352 pdraw->textureFormat =
353 determineTextureFormat((const int *) data, num_attributes);
368 * Create a non-pbuffer GLX drawable.
371 CreateDrawable(Display *dpy, struct glx_config *config,
372 Drawable drawable, const int *attrib_list, CARD8 glxCode)
374 xGLXCreateWindowReq *req;
382 while (attrib_list[i * 2] != None)
386 opcode = __glXSetupForCommand(dpy);
391 GetReqExtra(GLXCreateWindow, 8 * i, req);
392 data = (CARD32 *) (req + 1);
394 req->reqType = opcode;
395 req->glxCode = glxCode;
396 req->screen = config->screen;
397 req->fbconfig = config->fbconfigID;
398 req->window = drawable;
399 req->glxwindow = xid = XAllocID(dpy);
403 memcpy(data, attrib_list, 8 * i);
408 CreateDRIDrawable(dpy, config, drawable, xid, attrib_list, i);
415 * Destroy a non-pbuffer GLX drawable.
418 DestroyDrawable(Display * dpy, GLXDrawable drawable, CARD32 glxCode)
420 xGLXDestroyPbufferReq *req;
423 if ((dpy == NULL) || (drawable == 0)) {
428 opcode = __glXSetupForCommand(dpy);
434 GetReq(GLXDestroyPbuffer, req);
435 req->reqType = opcode;
436 req->glxCode = glxCode;
437 req->pbuffer = (GLXPbuffer) drawable;
442 DestroyDRIDrawable(dpy, drawable, GL_FALSE);
451 * This function is used to implement \c glXCreatePbuffer and
452 * \c glXCreateGLXPbufferSGIX.
455 * This function dynamically determines whether to use the SGIX_pbuffer
456 * version of the protocol or the GLX 1.3 version of the protocol.
459 CreatePbuffer(Display * dpy, struct glx_config *config,
460 unsigned int width, unsigned int height,
461 const int *attrib_list, GLboolean size_in_attribs)
463 struct glx_display *priv = __glXInitialize(dpy);
472 while (attrib_list[i * 2])
476 opcode = __glXSetupForCommand(dpy);
483 if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
484 xGLXCreatePbufferReq *req;
485 unsigned int extra = (size_in_attribs) ? 0 : 2;
487 GetReqExtra(GLXCreatePbuffer, (8 * (i + extra)), req);
488 data = (CARD32 *) (req + 1);
490 req->reqType = opcode;
491 req->glxCode = X_GLXCreatePbuffer;
492 req->screen = config->screen;
493 req->fbconfig = config->fbconfigID;
495 req->numAttribs = i + extra;
497 if (!size_in_attribs) {
498 data[(2 * i) + 0] = GLX_PBUFFER_WIDTH;
499 data[(2 * i) + 1] = width;
500 data[(2 * i) + 2] = GLX_PBUFFER_HEIGHT;
501 data[(2 * i) + 3] = height;
506 xGLXVendorPrivateReq *vpreq;
508 GetReqExtra(GLXVendorPrivate, 20 + (8 * i), vpreq);
509 data = (CARD32 *) (vpreq + 1);
511 vpreq->reqType = opcode;
512 vpreq->glxCode = X_GLXVendorPrivate;
513 vpreq->vendorCode = X_GLXvop_CreateGLXPbufferSGIX;
515 data[0] = config->screen;
516 data[1] = config->fbconfigID;
523 (void) memcpy(data, attrib_list, sizeof(CARD32) * 2 * i);
528 pixmap = XCreatePixmap(dpy, RootWindow(dpy, config->screen),
529 width, height, config->rgbBits);
531 CreateDRIDrawable(dpy, config, pixmap, id, attrib_list, i);
539 * This function is used to implement \c glXDestroyPbuffer and
540 * \c glXDestroyGLXPbufferSGIX.
543 * This function dynamically determines whether to use the SGIX_pbuffer
544 * version of the protocol or the GLX 1.3 version of the protocol.
547 DestroyPbuffer(Display * dpy, GLXDrawable drawable)
549 struct glx_display *priv = __glXInitialize(dpy);
552 if ((dpy == NULL) || (drawable == 0)) {
556 opcode = __glXSetupForCommand(dpy);
562 if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
563 xGLXDestroyPbufferReq *req;
565 GetReq(GLXDestroyPbuffer, req);
566 req->reqType = opcode;
567 req->glxCode = X_GLXDestroyPbuffer;
568 req->pbuffer = (GLXPbuffer) drawable;
571 xGLXVendorPrivateWithReplyReq *vpreq;
574 GetReqExtra(GLXVendorPrivateWithReply, 4, vpreq);
575 data = (CARD32 *) (vpreq + 1);
577 data[0] = (CARD32) drawable;
579 vpreq->reqType = opcode;
580 vpreq->glxCode = X_GLXVendorPrivateWithReply;
581 vpreq->vendorCode = X_GLXvop_DestroyGLXPbufferSGIX;
587 DestroyDRIDrawable(dpy, drawable, GL_TRUE);
593 * Create a new pbuffer.
595 _X_EXPORT GLXPbufferSGIX
596 glXCreateGLXPbufferSGIX(Display * dpy, GLXFBConfigSGIX config,
597 unsigned int width, unsigned int height,
600 return (GLXPbufferSGIX) CreatePbuffer(dpy, (struct glx_config *) config,
602 attrib_list, GL_FALSE);
605 #endif /* GLX_USE_APPLEGL */
608 * Create a new pbuffer.
611 glXCreatePbuffer(Display * dpy, GLXFBConfig config, const int *attrib_list)
613 int i, width, height;
614 #ifdef GLX_USE_APPLEGL
622 WARN_ONCE_GLX_1_3(dpy, __func__);
624 #ifdef GLX_USE_APPLEGL
625 for (i = 0; attrib_list[i]; ++i) {
626 switch (attrib_list[i]) {
627 case GLX_PBUFFER_WIDTH:
628 width = attrib_list[i + 1];
632 case GLX_PBUFFER_HEIGHT:
633 height = attrib_list[i + 1];
637 case GLX_LARGEST_PBUFFER:
638 /* This is a hint we should probably handle, but how? */
642 case GLX_PRESERVED_CONTENTS:
643 /* The contents are always preserved with AppleSGLX with CGL. */
652 if (apple_glx_pbuffer_create(dpy, config, width, height, &errorcode,
655 * apple_glx_pbuffer_create only sets the errorcode to core X11
658 __glXSendError(dpy, errorcode, 0, X_GLXCreatePbuffer, true);
665 for (i = 0; attrib_list[i * 2]; i++) {
666 switch (attrib_list[i * 2]) {
667 case GLX_PBUFFER_WIDTH:
668 width = attrib_list[i * 2 + 1];
670 case GLX_PBUFFER_HEIGHT:
671 height = attrib_list[i * 2 + 1];
676 return (GLXPbuffer) CreatePbuffer(dpy, (struct glx_config *) config,
677 width, height, attrib_list, GL_TRUE);
683 * Destroy an existing pbuffer.
686 glXDestroyPbuffer(Display * dpy, GLXPbuffer pbuf)
688 #ifdef GLX_USE_APPLEGL
689 if (apple_glx_pbuffer_destroy(dpy, pbuf)) {
690 __glXSendError(dpy, GLXBadPbuffer, pbuf, X_GLXDestroyPbuffer, false);
693 DestroyPbuffer(dpy, pbuf);
699 * Query an attribute of a drawable.
702 glXQueryDrawable(Display * dpy, GLXDrawable drawable,
703 int attribute, unsigned int *value)
705 WARN_ONCE_GLX_1_3(dpy, __func__);
706 #ifdef GLX_USE_APPLEGL
709 unsigned int width, height, bd, depth;
711 if (apple_glx_pixmap_query(drawable, attribute, value))
714 if (apple_glx_pbuffer_query(drawable, attribute, value))
718 * The OpenGL spec states that we should report GLXBadDrawable if
719 * the drawable is invalid, however doing so would require that we
720 * use XSetErrorHandler(), which is known to not be thread safe.
721 * If we use a round-trip call to validate the drawable, there could
722 * be a race, so instead we just opt in favor of letting the
723 * XGetGeometry request fail with a GetGeometry request X error
724 * rather than GLXBadDrawable, in what is hoped to be a rare
725 * case of an invalid drawable. In practice most and possibly all
726 * X11 apps using GLX shouldn't notice a difference.
729 (dpy, drawable, &root, &x, &y, &width, &height, &bd, &depth)) {
741 GetDrawableAttribute(dpy, drawable, attribute, value);
746 #ifndef GLX_USE_APPLEGL
748 * Query an attribute of a pbuffer.
751 glXQueryGLXPbufferSGIX(Display * dpy, GLXPbufferSGIX drawable,
752 int attribute, unsigned int *value)
754 return GetDrawableAttribute(dpy, drawable, attribute, value);
759 * Select the event mask for a drawable.
762 glXSelectEvent(Display * dpy, GLXDrawable drawable, unsigned long mask)
764 #ifdef GLX_USE_APPLEGL
765 XWindowAttributes xwattr;
767 if (apple_glx_pbuffer_set_event_mask(drawable, mask))
771 * The spec allows a window, but currently there are no valid
772 * events for a window, so do nothing.
774 if (XGetWindowAttributes(dpy, drawable, &xwattr))
776 /* The drawable seems to be invalid. Report an error. */
778 __glXSendError(dpy, GLXBadDrawable, drawable,
779 X_GLXChangeDrawableAttributes, false);
783 attribs[0] = (CARD32) GLX_EVENT_MASK;
784 attribs[1] = (CARD32) mask;
786 ChangeDrawableAttribute(dpy, drawable, attribs, 1);
792 * Get the selected event mask for a drawable.
795 glXGetSelectedEvent(Display * dpy, GLXDrawable drawable, unsigned long *mask)
797 #ifdef GLX_USE_APPLEGL
798 XWindowAttributes xwattr;
800 if (apple_glx_pbuffer_get_event_mask(drawable, mask))
804 * The spec allows a window, but currently there are no valid
805 * events for a window, so do nothing, but set the mask to 0.
807 if (XGetWindowAttributes(dpy, drawable, &xwattr)) {
808 /* The window is valid, so set the mask to 0. */
812 /* The drawable seems to be invalid. Report an error. */
814 __glXSendError(dpy, GLXBadDrawable, drawable, X_GLXGetDrawableAttributes,
820 /* The non-sense with value is required because on LP64 platforms
821 * sizeof(unsigned int) != sizeof(unsigned long). On little-endian
822 * we could just type-cast the pointer, but why?
825 GetDrawableAttribute(dpy, drawable, GLX_EVENT_MASK_SGIX, &value);
832 glXCreatePixmap(Display * dpy, GLXFBConfig config, Pixmap pixmap,
833 const int *attrib_list)
835 WARN_ONCE_GLX_1_3(dpy, __func__);
837 #ifdef GLX_USE_APPLEGL
838 const struct glx_config *modes = (const struct glx_config *) config;
840 if (apple_glx_pixmap_create(dpy, modes->screen, pixmap, modes))
845 return CreateDrawable(dpy, (struct glx_config *) config,
846 (Drawable) pixmap, attrib_list, X_GLXCreatePixmap);
852 glXCreateWindow(Display * dpy, GLXFBConfig config, Window win,
853 const int *attrib_list)
855 WARN_ONCE_GLX_1_3(dpy, __func__);
856 #ifdef GLX_USE_APPLEGL
857 XWindowAttributes xwattr;
858 XVisualInfo *visinfo;
860 (void) attrib_list; /*unused according to GLX 1.4 */
862 XGetWindowAttributes(dpy, win, &xwattr);
864 visinfo = glXGetVisualFromFBConfig(dpy, config);
866 if (NULL == visinfo) {
867 __glXSendError(dpy, GLXBadFBConfig, 0, X_GLXCreateWindow, false);
871 if (visinfo->visualid != XVisualIDFromVisual(xwattr.visual)) {
872 __glXSendError(dpy, BadMatch, 0, X_GLXCreateWindow, true);
880 return CreateDrawable(dpy, (struct glx_config *) config,
881 (Drawable) win, attrib_list, X_GLXCreateWindow);
887 glXDestroyPixmap(Display * dpy, GLXPixmap pixmap)
889 WARN_ONCE_GLX_1_3(dpy, __func__);
890 #ifdef GLX_USE_APPLEGL
891 if (apple_glx_pixmap_destroy(dpy, pixmap))
892 __glXSendError(dpy, GLXBadPixmap, pixmap, X_GLXDestroyPixmap, false);
894 DestroyDrawable(dpy, (GLXDrawable) pixmap, X_GLXDestroyPixmap);
900 glXDestroyWindow(Display * dpy, GLXWindow win)
902 WARN_ONCE_GLX_1_3(dpy, __func__);
903 #ifndef GLX_USE_APPLEGL
904 DestroyDrawable(dpy, (GLXDrawable) win, X_GLXDestroyWindow);
908 #ifndef GLX_USE_APPLEGL
910 GLX_ALIAS_VOID(glXDestroyGLXPbufferSGIX,
911 (Display * dpy, GLXPbufferSGIX pbuf),
912 (dpy, pbuf), glXDestroyPbuffer)
915 GLX_ALIAS_VOID(glXSelectEventSGIX,
916 (Display * dpy, GLXDrawable drawable,
917 unsigned long mask), (dpy, drawable, mask), glXSelectEvent)
920 GLX_ALIAS_VOID(glXGetSelectedEventSGIX,
921 (Display * dpy, GLXDrawable drawable,
922 unsigned long *mask), (dpy, drawable, mask),