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
);
136 if (old
!= &dummyContext
&& !old
->isDirect
&& old
->psc
->dpy
== dpy
) {
137 tag
= old
->currentContextTag
;
138 old
->currentContextTag
= 0;
143 SendMakeCurrentRequest(dpy
, opcode
, gc
->xid
, tag
, draw
, read
,
144 &gc
->currentContextTag
);
147 IndirectAPI
= __glXNewIndirectAPI();
148 _glapi_set_dispatch(IndirectAPI
);
150 state
= gc
->client_state_private
;
151 if (state
->array_state
== NULL
) {
152 glGetString(GL_EXTENSIONS
);
153 glGetString(GL_VERSION
);
154 __glXInitVertexArrayState(gc
);
161 indirect_unbind_context(struct glx_context
*gc
, struct glx_context
*new)
163 Display
*dpy
= gc
->psc
->dpy
;
164 int opcode
= __glXSetupForCommand(dpy
);
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
, NULL
);
176 gc
->currentContextTag
= 0;
181 indirect_wait_gl(struct glx_context
*gc
)
184 Display
*dpy
= gc
->currentDpy
;
186 /* Flush any pending commands out */
187 __glXFlushRenderBuffer(gc
, gc
->pc
);
189 /* Send the glXWaitGL request */
191 GetReq(GLXWaitGL
, req
);
192 req
->reqType
= gc
->majorOpcode
;
193 req
->glxCode
= X_GLXWaitGL
;
194 req
->contextTag
= gc
->currentContextTag
;
200 indirect_wait_x(struct glx_context
*gc
)
203 Display
*dpy
= gc
->currentDpy
;
205 /* Flush any pending commands out */
206 __glXFlushRenderBuffer(gc
, gc
->pc
);
209 GetReq(GLXWaitX
, req
);
210 req
->reqType
= gc
->majorOpcode
;
211 req
->glxCode
= X_GLXWaitX
;
212 req
->contextTag
= gc
->currentContextTag
;
218 indirect_use_x_font(struct glx_context
*gc
,
219 Font font
, int first
, int count
, int listBase
)
221 xGLXUseXFontReq
*req
;
222 Display
*dpy
= gc
->currentDpy
;
224 /* Flush any pending commands out */
225 __glXFlushRenderBuffer(gc
, gc
->pc
);
227 /* Send the glXUseFont request */
229 GetReq(GLXUseXFont
, req
);
230 req
->reqType
= gc
->majorOpcode
;
231 req
->glxCode
= X_GLXUseXFont
;
232 req
->contextTag
= gc
->currentContextTag
;
236 req
->listBase
= listBase
;
242 indirect_bind_tex_image(Display
* dpy
,
243 GLXDrawable drawable
,
244 int buffer
, const int *attrib_list
)
246 xGLXVendorPrivateReq
*req
;
247 struct glx_context
*gc
= __glXGetCurrentContext();
248 CARD32
*drawable_ptr
;
250 CARD32
*num_attrib_ptr
;
257 while (attrib_list
[i
* 2] != None
)
261 opcode
= __glXSetupForCommand(dpy
);
266 GetReqExtra(GLXVendorPrivate
, 12 + 8 * i
, req
);
267 req
->reqType
= opcode
;
268 req
->glxCode
= X_GLXVendorPrivate
;
269 req
->vendorCode
= X_GLXvop_BindTexImageEXT
;
270 req
->contextTag
= gc
->currentContextTag
;
272 drawable_ptr
= (CARD32
*) (req
+ 1);
273 buffer_ptr
= (INT32
*) (drawable_ptr
+ 1);
274 num_attrib_ptr
= (CARD32
*) (buffer_ptr
+ 1);
275 attrib_ptr
= (CARD32
*) (num_attrib_ptr
+ 1);
277 *drawable_ptr
= drawable
;
278 *buffer_ptr
= buffer
;
279 *num_attrib_ptr
= (CARD32
) i
;
283 while (attrib_list
[i
* 2] != None
) {
284 *attrib_ptr
++ = (CARD32
) attrib_list
[i
* 2 + 0];
285 *attrib_ptr
++ = (CARD32
) attrib_list
[i
* 2 + 1];
295 indirect_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
297 xGLXVendorPrivateReq
*req
;
298 struct glx_context
*gc
= __glXGetCurrentContext();
299 CARD32
*drawable_ptr
;
303 opcode
= __glXSetupForCommand(dpy
);
308 GetReqExtra(GLXVendorPrivate
, sizeof(CARD32
) + sizeof(INT32
), req
);
309 req
->reqType
= opcode
;
310 req
->glxCode
= X_GLXVendorPrivate
;
311 req
->vendorCode
= X_GLXvop_ReleaseTexImageEXT
;
312 req
->contextTag
= gc
->currentContextTag
;
314 drawable_ptr
= (CARD32
*) (req
+ 1);
315 buffer_ptr
= (INT32
*) (drawable_ptr
+ 1);
317 *drawable_ptr
= drawable
;
318 *buffer_ptr
= buffer
;
324 static const struct glx_context_vtable indirect_context_vtable
= {
325 indirect_destroy_context
,
326 indirect_bind_context
,
327 indirect_unbind_context
,
331 indirect_bind_tex_image
,
332 indirect_release_tex_image
,
333 NULL
, /* get_proc_address */
337 * \todo Eliminate \c __glXInitVertexArrayState. Replace it with a new
338 * function called \c __glXAllocateClientState that allocates the memory and
339 * does all the initialization (including the pixel pack / unpack).
342 * This function is \b not the place to validate the context creation
343 * parameters. It is just the allocator for the \c glx_context.
345 _X_HIDDEN
struct glx_context
*
346 indirect_create_context(struct glx_screen
*psc
,
347 struct glx_config
*mode
,
348 struct glx_context
*shareList
, int renderType
)
350 struct glx_context
*gc
;
353 __GLXattribute
*state
;
355 opcode
= __glXSetupForCommand(psc
->dpy
);
360 /* Allocate our context record */
361 gc
= calloc(1, sizeof *gc
);
367 glx_context_init(gc
, psc
, mode
);
368 gc
->isDirect
= GL_FALSE
;
369 gc
->vtable
= &indirect_context_vtable
;
370 state
= calloc(1, sizeof(struct __GLXattributeRec
));
371 gc
->renderType
= renderType
;
378 gc
->client_state_private
= state
;
379 state
->NoDrawArraysProtocol
= (getenv("LIBGL_NO_DRAWARRAYS") != NULL
);
382 ** Create a temporary buffer to hold GLX rendering commands. The size
383 ** of the buffer is selected so that the maximum number of GLX rendering
384 ** commands can fit in a single X packet and still have room in the X
385 ** packet for the GLXRenderReq header.
388 bufSize
= (XMaxRequestSize(psc
->dpy
) * 4) - sz_xGLXRenderReq
;
389 gc
->buf
= malloc(bufSize
);
391 free(gc
->client_state_private
);
395 gc
->bufSize
= bufSize
;
397 /* Fill in the new context */
398 gc
->renderMode
= GL_RENDER
;
400 state
->storePack
.alignment
= 4;
401 state
->storeUnpack
.alignment
= 4;
403 gc
->attributes
.stackPointer
= &gc
->attributes
.stack
[0];
406 ** PERFORMANCE NOTE: A mode dependent fill image can speed things up.
408 gc
->fillImage
= __glFillImage
;
410 gc
->bufEnd
= gc
->buf
+ bufSize
;
411 gc
->isDirect
= GL_FALSE
;
414 ** Set limit register so that there will be one command per packet
419 gc
->limit
= gc
->buf
+ bufSize
- __GLX_BUFFER_LIMIT_SIZE
;
421 gc
->majorOpcode
= opcode
;
424 ** Constrain the maximum drawing command size allowed to be
425 ** transfered using the X_GLXRender protocol request. First
426 ** constrain by a software limit, then constrain by the protocl
429 if (bufSize
> __GLX_RENDER_CMD_SIZE_LIMIT
) {
430 bufSize
= __GLX_RENDER_CMD_SIZE_LIMIT
;
432 if (bufSize
> __GLX_MAX_RENDER_CMD_SIZE
) {
433 bufSize
= __GLX_MAX_RENDER_CMD_SIZE
;
435 gc
->maxSmallRenderCommandSize
= bufSize
;
441 _X_HIDDEN
struct glx_context
*
442 indirect_create_context_attribs(struct glx_screen
*base
,
443 struct glx_config
*config_base
,
444 struct glx_context
*shareList
,
445 unsigned num_attribs
,
446 const uint32_t *attribs
,
449 int renderType
= GLX_RGBA_TYPE
;
452 /* The error parameter is only used on the server so that correct GLX
453 * protocol errors can be generated. On the client, it can be ignored.
457 /* All of the attribute validation for indirect contexts is handled on the
458 * server, so there's not much to do here. Still, we need to parse the
459 * attributes to correctly set renderType.
461 for (i
= 0; i
< num_attribs
; i
++) {
462 if (attribs
[i
* 2] == GLX_RENDER_TYPE
)
463 renderType
= attribs
[i
* 2 + 1];
466 return indirect_create_context(base
, config_base
, shareList
, renderType
);
469 struct glx_screen_vtable indirect_screen_vtable
= {
470 indirect_create_context
,
471 indirect_create_context_attribs
474 _X_HIDDEN
struct glx_screen
*
475 indirect_create_screen(int screen
, struct glx_display
* priv
)
477 struct glx_screen
*psc
;
479 psc
= calloc(1, sizeof *psc
);
483 glx_screen_init(psc
, screen
, priv
);
484 psc
->vtable
= &indirect_screen_vtable
;