2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Soft-
6 * ware"), to deal in the Software without restriction, including without
7 * limitation the rights to use, copy, modify, merge, publish, distribute,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, provided that the above copyright
10 * notice(s) and this permission notice appear in all copies of the Soft-
11 * ware and that both the above copyright notice(s) and this permission
12 * notice appear in supporting documentation.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
17 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
18 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
19 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
22 * MANCE OF THIS SOFTWARE.
24 * Except as contained in this notice, the name of a copyright holder shall
25 * not be used in advertising or otherwise to promote the sale, use or
26 * other dealings in this Software without prior written authorization of
27 * the copyright holder.
30 * Kristian Høgsberg (krh@bitplanet.net)
34 #include "glxclient.h"
36 extern struct _glapi_table
*__glXNewIndirectAPI(void);
39 ** All indirect rendering contexts will share the same indirect dispatch table.
41 static struct _glapi_table
*IndirectAPI
= NULL
;
44 indirect_destroy_context(struct glx_context
*gc
)
46 if (!gc
->imported
&& gc
->xid
)
47 glx_send_destroy_context(gc
->psc
->dpy
, gc
->xid
);
49 __glXFreeVertexArrayState(gc
);
52 XFree((char *) gc
->vendor
);
54 XFree((char *) gc
->renderer
);
56 XFree((char *) gc
->version
);
58 XFree((char *) gc
->extensions
);
59 __glFreeAttributeState(gc
);
60 XFree((char *) gc
->buf
);
61 Xfree((char *) gc
->client_state_private
);
66 SendMakeCurrentRequest(Display
* dpy
, CARD8 opcode
,
67 GLXContextID gc_id
, GLXContextTag gc_tag
,
68 GLXDrawable draw
, GLXDrawable read
,
69 xGLXMakeCurrentReply
* reply
)
76 xGLXMakeCurrentReq
*req
;
78 GetReq(GLXMakeCurrent
, req
);
79 req
->reqType
= opcode
;
80 req
->glxCode
= X_GLXMakeCurrent
;
83 req
->oldContextTag
= gc_tag
;
86 struct glx_display
*priv
= __glXInitialize(dpy
);
88 /* If the server can support the GLX 1.3 version, we should
89 * perfer that. Not only that, some servers support GLX 1.3 but
90 * not the SGI extension.
93 if ((priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3)) {
94 xGLXMakeContextCurrentReq
*req
;
96 GetReq(GLXMakeContextCurrent
, req
);
97 req
->reqType
= opcode
;
98 req
->glxCode
= X_GLXMakeContextCurrent
;
100 req
->readdrawable
= read
;
101 req
->context
= gc_id
;
102 req
->oldContextTag
= gc_tag
;
105 xGLXVendorPrivateWithReplyReq
*vpreq
;
106 xGLXMakeCurrentReadSGIReq
*req
;
108 GetReqExtra(GLXVendorPrivateWithReply
,
109 sz_xGLXMakeCurrentReadSGIReq
-
110 sz_xGLXVendorPrivateWithReplyReq
, vpreq
);
111 req
= (xGLXMakeCurrentReadSGIReq
*) vpreq
;
112 req
->reqType
= opcode
;
113 req
->glxCode
= X_GLXVendorPrivateWithReply
;
114 req
->vendorCode
= X_GLXvop_MakeCurrentReadSGI
;
115 req
->drawable
= draw
;
116 req
->readable
= read
;
117 req
->context
= gc_id
;
118 req
->oldContextTag
= gc_tag
;
122 ret
= _XReply(dpy
, (xReply
*) reply
, 0, False
);
131 indirect_bind_context(struct glx_context
*gc
, struct glx_context
*old
,
132 GLXDrawable draw
, GLXDrawable read
)
134 xGLXMakeCurrentReply reply
;
136 __GLXattribute
*state
;
137 Display
*dpy
= gc
->psc
->dpy
;
138 int opcode
= __glXSetupForCommand(dpy
);
140 if (old
!= &dummyContext
&& !old
->isDirect
&& old
->psc
->dpy
== dpy
)
141 tag
= old
->currentContextTag
;
145 SendMakeCurrentRequest(dpy
, opcode
, gc
->xid
, tag
, draw
, read
, &reply
);
148 IndirectAPI
= __glXNewIndirectAPI();
149 _glapi_set_dispatch(IndirectAPI
);
151 gc
->currentContextTag
= reply
.contextTag
;
152 state
= gc
->client_state_private
;
153 if (state
->array_state
== NULL
) {
154 glGetString(GL_EXTENSIONS
);
155 glGetString(GL_VERSION
);
156 __glXInitVertexArrayState(gc
);
163 indirect_unbind_context(struct glx_context
*gc
, struct glx_context
*new)
165 Display
*dpy
= gc
->psc
->dpy
;
166 int opcode
= __glXSetupForCommand(dpy
);
167 xGLXMakeCurrentReply reply
;
169 /* We are either switching to no context, away from a indirect
170 * context to a direct context or from one dpy to another and have
171 * to send a request to the dpy to unbind the previous context.
173 if (!new || new->isDirect
|| new->psc
->dpy
!= dpy
)
174 SendMakeCurrentRequest(dpy
, opcode
, None
,
175 gc
->currentContextTag
, None
, None
, &reply
);
176 gc
->currentContextTag
= 0;
180 indirect_wait_gl(struct glx_context
*gc
)
183 Display
*dpy
= gc
->currentDpy
;
185 /* Flush any pending commands out */
186 __glXFlushRenderBuffer(gc
, gc
->pc
);
188 /* Send the glXWaitGL request */
190 GetReq(GLXWaitGL
, req
);
191 req
->reqType
= gc
->majorOpcode
;
192 req
->glxCode
= X_GLXWaitGL
;
193 req
->contextTag
= gc
->currentContextTag
;
199 indirect_wait_x(struct glx_context
*gc
)
202 Display
*dpy
= gc
->currentDpy
;
204 /* Flush any pending commands out */
205 __glXFlushRenderBuffer(gc
, gc
->pc
);
208 GetReq(GLXWaitX
, req
);
209 req
->reqType
= gc
->majorOpcode
;
210 req
->glxCode
= X_GLXWaitX
;
211 req
->contextTag
= gc
->currentContextTag
;
217 indirect_use_x_font(struct glx_context
*gc
,
218 Font font
, int first
, int count
, int listBase
)
220 xGLXUseXFontReq
*req
;
221 Display
*dpy
= gc
->currentDpy
;
223 /* Flush any pending commands out */
224 __glXFlushRenderBuffer(gc
, gc
->pc
);
226 /* Send the glXUseFont request */
228 GetReq(GLXUseXFont
, req
);
229 req
->reqType
= gc
->majorOpcode
;
230 req
->glxCode
= X_GLXUseXFont
;
231 req
->contextTag
= gc
->currentContextTag
;
235 req
->listBase
= listBase
;
241 indirect_bind_tex_image(Display
* dpy
,
242 GLXDrawable drawable
,
243 int buffer
, const int *attrib_list
)
245 xGLXVendorPrivateReq
*req
;
246 struct glx_context
*gc
= __glXGetCurrentContext();
247 CARD32
*drawable_ptr
;
249 CARD32
*num_attrib_ptr
;
256 while (attrib_list
[i
* 2] != None
)
260 opcode
= __glXSetupForCommand(dpy
);
265 GetReqExtra(GLXVendorPrivate
, 12 + 8 * i
, req
);
266 req
->reqType
= opcode
;
267 req
->glxCode
= X_GLXVendorPrivate
;
268 req
->vendorCode
= X_GLXvop_BindTexImageEXT
;
269 req
->contextTag
= gc
->currentContextTag
;
271 drawable_ptr
= (CARD32
*) (req
+ 1);
272 buffer_ptr
= (INT32
*) (drawable_ptr
+ 1);
273 num_attrib_ptr
= (CARD32
*) (buffer_ptr
+ 1);
274 attrib_ptr
= (CARD32
*) (num_attrib_ptr
+ 1);
276 *drawable_ptr
= drawable
;
277 *buffer_ptr
= buffer
;
278 *num_attrib_ptr
= (CARD32
) i
;
282 while (attrib_list
[i
* 2] != None
) {
283 *attrib_ptr
++ = (CARD32
) attrib_list
[i
* 2 + 0];
284 *attrib_ptr
++ = (CARD32
) attrib_list
[i
* 2 + 1];
294 indirect_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
296 xGLXVendorPrivateReq
*req
;
297 struct glx_context
*gc
= __glXGetCurrentContext();
298 CARD32
*drawable_ptr
;
302 opcode
= __glXSetupForCommand(dpy
);
307 GetReqExtra(GLXVendorPrivate
, sizeof(CARD32
) + sizeof(INT32
), req
);
308 req
->reqType
= opcode
;
309 req
->glxCode
= X_GLXVendorPrivate
;
310 req
->vendorCode
= X_GLXvop_ReleaseTexImageEXT
;
311 req
->contextTag
= gc
->currentContextTag
;
313 drawable_ptr
= (CARD32
*) (req
+ 1);
314 buffer_ptr
= (INT32
*) (drawable_ptr
+ 1);
316 *drawable_ptr
= drawable
;
317 *buffer_ptr
= buffer
;
323 static const struct glx_context_vtable indirect_context_vtable
= {
324 indirect_destroy_context
,
325 indirect_bind_context
,
326 indirect_unbind_context
,
330 indirect_bind_tex_image
,
331 indirect_release_tex_image
,
335 * \todo Eliminate \c __glXInitVertexArrayState. Replace it with a new
336 * function called \c __glXAllocateClientState that allocates the memory and
337 * does all the initialization (including the pixel pack / unpack).
339 _X_HIDDEN
struct glx_context
*
340 indirect_create_context(struct glx_screen
*psc
,
341 struct glx_config
*mode
,
342 struct glx_context
*shareList
, int renderType
)
344 struct glx_context
*gc
;
347 __GLXattribute
*state
;
349 opcode
= __glXSetupForCommand(psc
->dpy
);
354 /* Allocate our context record */
355 gc
= Xmalloc(sizeof *gc
);
360 memset(gc
, 0, sizeof *gc
);
362 glx_context_init(gc
, psc
, mode
);
363 gc
->isDirect
= GL_FALSE
;
364 gc
->vtable
= &indirect_context_vtable
;
365 state
= Xmalloc(sizeof(struct __GLXattributeRec
));
371 gc
->client_state_private
= state
;
372 memset(gc
->client_state_private
, 0, sizeof(struct __GLXattributeRec
));
373 state
->NoDrawArraysProtocol
= (getenv("LIBGL_NO_DRAWARRAYS") != NULL
);
376 ** Create a temporary buffer to hold GLX rendering commands. The size
377 ** of the buffer is selected so that the maximum number of GLX rendering
378 ** commands can fit in a single X packet and still have room in the X
379 ** packet for the GLXRenderReq header.
382 bufSize
= (XMaxRequestSize(psc
->dpy
) * 4) - sz_xGLXRenderReq
;
383 gc
->buf
= (GLubyte
*) Xmalloc(bufSize
);
385 Xfree(gc
->client_state_private
);
389 gc
->bufSize
= bufSize
;
391 /* Fill in the new context */
392 gc
->renderMode
= GL_RENDER
;
394 state
->storePack
.alignment
= 4;
395 state
->storeUnpack
.alignment
= 4;
397 gc
->attributes
.stackPointer
= &gc
->attributes
.stack
[0];
400 ** PERFORMANCE NOTE: A mode dependent fill image can speed things up.
401 ** Other code uses the fastImageUnpack bit, but it is never set
404 gc
->fastImageUnpack
= GL_FALSE
;
405 gc
->fillImage
= __glFillImage
;
407 gc
->bufEnd
= gc
->buf
+ bufSize
;
408 gc
->isDirect
= GL_FALSE
;
411 ** Set limit register so that there will be one command per packet
416 gc
->limit
= gc
->buf
+ bufSize
- __GLX_BUFFER_LIMIT_SIZE
;
418 gc
->majorOpcode
= opcode
;
421 ** Constrain the maximum drawing command size allowed to be
422 ** transfered using the X_GLXRender protocol request. First
423 ** constrain by a software limit, then constrain by the protocl
426 if (bufSize
> __GLX_RENDER_CMD_SIZE_LIMIT
) {
427 bufSize
= __GLX_RENDER_CMD_SIZE_LIMIT
;
429 if (bufSize
> __GLX_MAX_RENDER_CMD_SIZE
) {
430 bufSize
= __GLX_MAX_RENDER_CMD_SIZE
;
432 gc
->maxSmallRenderCommandSize
= bufSize
;
438 struct glx_screen_vtable indirect_screen_vtable
= {
439 indirect_create_context
442 _X_HIDDEN
struct glx_screen
*
443 indirect_create_screen(int screen
, struct glx_display
* priv
)
445 struct glx_screen
*psc
;
447 psc
= Xmalloc(sizeof *psc
);
451 memset(psc
, 0, sizeof *psc
);
452 glx_screen_init(psc
, screen
, priv
);
453 psc
->vtable
= &indirect_screen_vtable
;