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 __glXFreeVertexArrayState(gc
);
48 free((char *) gc
->vendor
);
49 free((char *) gc
->renderer
);
50 free((char *) gc
->version
);
51 free((char *) gc
->extensions
);
52 __glFreeAttributeState(gc
);
53 free((char *) gc
->buf
);
54 free((char *) gc
->client_state_private
);
59 SendMakeCurrentRequest(Display
* dpy
, CARD8 opcode
,
60 GLXContextID gc_id
, GLXContextTag gc_tag
,
61 GLXDrawable draw
, GLXDrawable read
,
62 GLXContextTag
*out_tag
)
64 xGLXMakeCurrentReply reply
;
70 xGLXMakeCurrentReq
*req
;
72 GetReq(GLXMakeCurrent
, req
);
73 req
->reqType
= opcode
;
74 req
->glxCode
= X_GLXMakeCurrent
;
77 req
->oldContextTag
= gc_tag
;
80 struct glx_display
*priv
= __glXInitialize(dpy
);
82 /* If the server can support the GLX 1.3 version, we should
83 * perfer that. Not only that, some servers support GLX 1.3 but
84 * not the SGI extension.
87 if ((priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3)) {
88 xGLXMakeContextCurrentReq
*req
;
90 GetReq(GLXMakeContextCurrent
, req
);
91 req
->reqType
= opcode
;
92 req
->glxCode
= X_GLXMakeContextCurrent
;
94 req
->readdrawable
= read
;
96 req
->oldContextTag
= gc_tag
;
99 xGLXVendorPrivateWithReplyReq
*vpreq
;
100 xGLXMakeCurrentReadSGIReq
*req
;
102 GetReqExtra(GLXVendorPrivateWithReply
,
103 sz_xGLXMakeCurrentReadSGIReq
-
104 sz_xGLXVendorPrivateWithReplyReq
, vpreq
);
105 req
= (xGLXMakeCurrentReadSGIReq
*) vpreq
;
106 req
->reqType
= opcode
;
107 req
->glxCode
= X_GLXVendorPrivateWithReply
;
108 req
->vendorCode
= X_GLXvop_MakeCurrentReadSGI
;
109 req
->drawable
= draw
;
110 req
->readable
= read
;
111 req
->context
= gc_id
;
112 req
->oldContextTag
= gc_tag
;
116 ret
= _XReply(dpy
, (xReply
*) &reply
, 0, False
);
119 *out_tag
= reply
.contextTag
;
128 indirect_bind_context(struct glx_context
*gc
, struct glx_context
*old
,
129 GLXDrawable draw
, GLXDrawable read
)
132 __GLXattribute
*state
;
133 Display
*dpy
= gc
->psc
->dpy
;
134 int opcode
= __glXSetupForCommand(dpy
);
137 if (old
!= &dummyContext
&& !old
->isDirect
&& old
->psc
->dpy
== dpy
) {
138 tag
= old
->currentContextTag
;
139 old
->currentContextTag
= 0;
144 sent
= SendMakeCurrentRequest(dpy
, opcode
, gc
->xid
, tag
, draw
, read
,
145 &gc
->currentContextTag
);
148 IndirectAPI
= __glXNewIndirectAPI();
149 _glapi_set_dispatch(IndirectAPI
);
151 state
= gc
->client_state_private
;
152 if (state
->array_state
== NULL
) {
153 glGetString(GL_EXTENSIONS
);
154 glGetString(GL_VERSION
);
155 __glXInitVertexArrayState(gc
);
162 indirect_unbind_context(struct glx_context
*gc
, struct glx_context
*new)
164 Display
*dpy
= gc
->psc
->dpy
;
165 int opcode
= __glXSetupForCommand(dpy
);
170 /* We are either switching to no context, away from a indirect
171 * context to a direct context or from one dpy to another and have
172 * to send a request to the dpy to unbind the previous context.
174 if (!new || new->isDirect
|| new->psc
->dpy
!= dpy
) {
175 SendMakeCurrentRequest(dpy
, opcode
, None
,
176 gc
->currentContextTag
, None
, None
, NULL
);
177 gc
->currentContextTag
= 0;
182 indirect_wait_gl(struct glx_context
*gc
)
185 Display
*dpy
= gc
->currentDpy
;
187 /* Flush any pending commands out */
188 __glXFlushRenderBuffer(gc
, gc
->pc
);
190 /* Send the glXWaitGL request */
192 GetReq(GLXWaitGL
, req
);
193 req
->reqType
= gc
->majorOpcode
;
194 req
->glxCode
= X_GLXWaitGL
;
195 req
->contextTag
= gc
->currentContextTag
;
201 indirect_wait_x(struct glx_context
*gc
)
204 Display
*dpy
= gc
->currentDpy
;
206 /* Flush any pending commands out */
207 __glXFlushRenderBuffer(gc
, gc
->pc
);
210 GetReq(GLXWaitX
, req
);
211 req
->reqType
= gc
->majorOpcode
;
212 req
->glxCode
= X_GLXWaitX
;
213 req
->contextTag
= gc
->currentContextTag
;
219 indirect_use_x_font(struct glx_context
*gc
,
220 Font font
, int first
, int count
, int listBase
)
222 xGLXUseXFontReq
*req
;
223 Display
*dpy
= gc
->currentDpy
;
225 /* Flush any pending commands out */
226 __glXFlushRenderBuffer(gc
, gc
->pc
);
228 /* Send the glXUseFont request */
230 GetReq(GLXUseXFont
, req
);
231 req
->reqType
= gc
->majorOpcode
;
232 req
->glxCode
= X_GLXUseXFont
;
233 req
->contextTag
= gc
->currentContextTag
;
237 req
->listBase
= listBase
;
243 indirect_bind_tex_image(Display
* dpy
,
244 GLXDrawable drawable
,
245 int buffer
, const int *attrib_list
)
247 xGLXVendorPrivateReq
*req
;
248 struct glx_context
*gc
= __glXGetCurrentContext();
249 CARD32
*drawable_ptr
;
251 CARD32
*num_attrib_ptr
;
258 while (attrib_list
[i
* 2] != None
)
262 opcode
= __glXSetupForCommand(dpy
);
267 GetReqExtra(GLXVendorPrivate
, 12 + 8 * i
, req
);
268 req
->reqType
= opcode
;
269 req
->glxCode
= X_GLXVendorPrivate
;
270 req
->vendorCode
= X_GLXvop_BindTexImageEXT
;
271 req
->contextTag
= gc
->currentContextTag
;
273 drawable_ptr
= (CARD32
*) (req
+ 1);
274 buffer_ptr
= (INT32
*) (drawable_ptr
+ 1);
275 num_attrib_ptr
= (CARD32
*) (buffer_ptr
+ 1);
276 attrib_ptr
= (CARD32
*) (num_attrib_ptr
+ 1);
278 *drawable_ptr
= drawable
;
279 *buffer_ptr
= buffer
;
280 *num_attrib_ptr
= (CARD32
) i
;
284 while (attrib_list
[i
* 2] != None
) {
285 *attrib_ptr
++ = (CARD32
) attrib_list
[i
* 2 + 0];
286 *attrib_ptr
++ = (CARD32
) attrib_list
[i
* 2 + 1];
296 indirect_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
298 xGLXVendorPrivateReq
*req
;
299 struct glx_context
*gc
= __glXGetCurrentContext();
300 CARD32
*drawable_ptr
;
304 opcode
= __glXSetupForCommand(dpy
);
309 GetReqExtra(GLXVendorPrivate
, sizeof(CARD32
) + sizeof(INT32
), req
);
310 req
->reqType
= opcode
;
311 req
->glxCode
= X_GLXVendorPrivate
;
312 req
->vendorCode
= X_GLXvop_ReleaseTexImageEXT
;
313 req
->contextTag
= gc
->currentContextTag
;
315 drawable_ptr
= (CARD32
*) (req
+ 1);
316 buffer_ptr
= (INT32
*) (drawable_ptr
+ 1);
318 *drawable_ptr
= drawable
;
319 *buffer_ptr
= buffer
;
325 static const struct glx_context_vtable indirect_context_vtable
= {
326 indirect_destroy_context
,
327 indirect_bind_context
,
328 indirect_unbind_context
,
332 indirect_bind_tex_image
,
333 indirect_release_tex_image
,
334 NULL
, /* get_proc_address */
338 * \todo Eliminate \c __glXInitVertexArrayState. Replace it with a new
339 * function called \c __glXAllocateClientState that allocates the memory and
340 * does all the initialization (including the pixel pack / unpack).
343 * This function is \b not the place to validate the context creation
344 * parameters. It is just the allocator for the \c glx_context.
346 _X_HIDDEN
struct glx_context
*
347 indirect_create_context(struct glx_screen
*psc
,
348 struct glx_config
*mode
,
349 struct glx_context
*shareList
, int renderType
)
351 struct glx_context
*gc
;
354 __GLXattribute
*state
;
356 opcode
= __glXSetupForCommand(psc
->dpy
);
361 /* Allocate our context record */
362 gc
= calloc(1, sizeof *gc
);
368 glx_context_init(gc
, psc
, mode
);
369 gc
->isDirect
= GL_FALSE
;
370 gc
->vtable
= &indirect_context_vtable
;
371 state
= calloc(1, sizeof(struct __GLXattributeRec
));
372 gc
->renderType
= renderType
;
379 gc
->client_state_private
= state
;
380 state
->NoDrawArraysProtocol
= (getenv("LIBGL_NO_DRAWARRAYS") != NULL
);
383 ** Create a temporary buffer to hold GLX rendering commands. The size
384 ** of the buffer is selected so that the maximum number of GLX rendering
385 ** commands can fit in a single X packet and still have room in the X
386 ** packet for the GLXRenderReq header.
389 bufSize
= (XMaxRequestSize(psc
->dpy
) * 4) - sz_xGLXRenderReq
;
390 gc
->buf
= malloc(bufSize
);
392 free(gc
->client_state_private
);
396 gc
->bufSize
= bufSize
;
398 /* Fill in the new context */
399 gc
->renderMode
= GL_RENDER
;
401 state
->storePack
.alignment
= 4;
402 state
->storeUnpack
.alignment
= 4;
404 gc
->attributes
.stackPointer
= &gc
->attributes
.stack
[0];
407 ** PERFORMANCE NOTE: A mode dependent fill image can speed things up.
409 gc
->fillImage
= __glFillImage
;
411 gc
->bufEnd
= gc
->buf
+ bufSize
;
412 gc
->isDirect
= GL_FALSE
;
415 ** Set limit register so that there will be one command per packet
420 gc
->limit
= gc
->buf
+ bufSize
- __GLX_BUFFER_LIMIT_SIZE
;
422 gc
->majorOpcode
= opcode
;
425 ** Constrain the maximum drawing command size allowed to be
426 ** transfered using the X_GLXRender protocol request. First
427 ** constrain by a software limit, then constrain by the protocl
430 if (bufSize
> __GLX_RENDER_CMD_SIZE_LIMIT
) {
431 bufSize
= __GLX_RENDER_CMD_SIZE_LIMIT
;
433 if (bufSize
> __GLX_MAX_RENDER_CMD_SIZE
) {
434 bufSize
= __GLX_MAX_RENDER_CMD_SIZE
;
436 gc
->maxSmallRenderCommandSize
= bufSize
;
442 _X_HIDDEN
struct glx_context
*
443 indirect_create_context_attribs(struct glx_screen
*base
,
444 struct glx_config
*config_base
,
445 struct glx_context
*shareList
,
446 unsigned num_attribs
,
447 const uint32_t *attribs
,
450 int renderType
= GLX_RGBA_TYPE
;
453 /* The error parameter is only used on the server so that correct GLX
454 * protocol errors can be generated. On the client, it can be ignored.
458 /* All of the attribute validation for indirect contexts is handled on the
459 * server, so there's not much to do here. Still, we need to parse the
460 * attributes to correctly set renderType.
462 for (i
= 0; i
< num_attribs
; i
++) {
463 if (attribs
[i
* 2] == GLX_RENDER_TYPE
)
464 renderType
= attribs
[i
* 2 + 1];
467 return indirect_create_context(base
, config_base
, shareList
, renderType
);
470 struct glx_screen_vtable indirect_screen_vtable
= {
471 indirect_create_context
,
472 indirect_create_context_attribs
475 _X_HIDDEN
struct glx_screen
*
476 indirect_create_screen(int screen
, struct glx_display
* priv
)
478 struct glx_screen
*psc
;
480 psc
= calloc(1, sizeof *psc
);
484 glx_screen_init(psc
, screen
, priv
);
485 psc
->vtable
= &indirect_screen_vtable
;