d0457fe73595ab4283fe653599d3487080fddb52
[mesa.git] / src / glx / indirect_glx.c
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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.
13 *
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.
23 *
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.
28 *
29 * Authors:
30 * Kristian Høgsberg (krh@bitplanet.net)
31 */
32
33 #include "glapi.h"
34 #include "glxclient.h"
35
36 extern struct _glapi_table *__glXNewIndirectAPI(void);
37
38 /*
39 ** All indirect rendering contexts will share the same indirect dispatch table.
40 */
41 static struct _glapi_table *IndirectAPI = NULL;
42
43 static void
44 indirect_destroy_context(struct glx_context *gc)
45 {
46 __glXFreeVertexArrayState(gc);
47
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);
55 free((char *) gc);
56 }
57
58 static Bool
59 SendMakeCurrentRequest(Display * dpy, CARD8 opcode,
60 GLXContextID gc_id, GLXContextTag gc_tag,
61 GLXDrawable draw, GLXDrawable read,
62 GLXContextTag *out_tag)
63 {
64 xGLXMakeCurrentReply reply;
65 Bool ret;
66
67 LockDisplay(dpy);
68
69 if (draw == read) {
70 xGLXMakeCurrentReq *req;
71
72 GetReq(GLXMakeCurrent, req);
73 req->reqType = opcode;
74 req->glxCode = X_GLXMakeCurrent;
75 req->drawable = draw;
76 req->context = gc_id;
77 req->oldContextTag = gc_tag;
78 }
79 else {
80 struct glx_display *priv = __glXInitialize(dpy);
81
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.
85 */
86
87 if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
88 xGLXMakeContextCurrentReq *req;
89
90 GetReq(GLXMakeContextCurrent, req);
91 req->reqType = opcode;
92 req->glxCode = X_GLXMakeContextCurrent;
93 req->drawable = draw;
94 req->readdrawable = read;
95 req->context = gc_id;
96 req->oldContextTag = gc_tag;
97 }
98 else {
99 xGLXVendorPrivateWithReplyReq *vpreq;
100 xGLXMakeCurrentReadSGIReq *req;
101
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;
113 }
114 }
115
116 ret = _XReply(dpy, (xReply *) &reply, 0, False);
117
118 if (out_tag)
119 *out_tag = reply.contextTag;
120
121 UnlockDisplay(dpy);
122 SyncHandle();
123
124 return ret;
125 }
126
127 static int
128 indirect_bind_context(struct glx_context *gc, struct glx_context *old,
129 GLXDrawable draw, GLXDrawable read)
130 {
131 GLXContextTag tag;
132 __GLXattribute *state;
133 Display *dpy = gc->psc->dpy;
134 int opcode = __glXSetupForCommand(dpy);
135
136 if (old != &dummyContext && !old->isDirect && old->psc->dpy == dpy) {
137 tag = old->currentContextTag;
138 old->currentContextTag = 0;
139 } else {
140 tag = 0;
141 }
142
143 SendMakeCurrentRequest(dpy, opcode, gc->xid, tag, draw, read,
144 &gc->currentContextTag);
145
146 if (!IndirectAPI)
147 IndirectAPI = __glXNewIndirectAPI();
148 _glapi_set_dispatch(IndirectAPI);
149
150 state = gc->client_state_private;
151 if (state->array_state == NULL) {
152 glGetString(GL_EXTENSIONS);
153 glGetString(GL_VERSION);
154 __glXInitVertexArrayState(gc);
155 }
156
157 return Success;
158 }
159
160 static void
161 indirect_unbind_context(struct glx_context *gc, struct glx_context *new)
162 {
163 Display *dpy = gc->psc->dpy;
164 int opcode = __glXSetupForCommand(dpy);
165
166 if (gc == new)
167 return;
168
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.
172 */
173 if (!new || new->isDirect || new->psc->dpy != dpy) {
174 SendMakeCurrentRequest(dpy, opcode, None,
175 gc->currentContextTag, None, None, NULL);
176 gc->currentContextTag = 0;
177 }
178 }
179
180 static void
181 indirect_wait_gl(struct glx_context *gc)
182 {
183 xGLXWaitGLReq *req;
184 Display *dpy = gc->currentDpy;
185
186 /* Flush any pending commands out */
187 __glXFlushRenderBuffer(gc, gc->pc);
188
189 /* Send the glXWaitGL request */
190 LockDisplay(dpy);
191 GetReq(GLXWaitGL, req);
192 req->reqType = gc->majorOpcode;
193 req->glxCode = X_GLXWaitGL;
194 req->contextTag = gc->currentContextTag;
195 UnlockDisplay(dpy);
196 SyncHandle();
197 }
198
199 static void
200 indirect_wait_x(struct glx_context *gc)
201 {
202 xGLXWaitXReq *req;
203 Display *dpy = gc->currentDpy;
204
205 /* Flush any pending commands out */
206 __glXFlushRenderBuffer(gc, gc->pc);
207
208 LockDisplay(dpy);
209 GetReq(GLXWaitX, req);
210 req->reqType = gc->majorOpcode;
211 req->glxCode = X_GLXWaitX;
212 req->contextTag = gc->currentContextTag;
213 UnlockDisplay(dpy);
214 SyncHandle();
215 }
216
217 static void
218 indirect_use_x_font(struct glx_context *gc,
219 Font font, int first, int count, int listBase)
220 {
221 xGLXUseXFontReq *req;
222 Display *dpy = gc->currentDpy;
223
224 /* Flush any pending commands out */
225 __glXFlushRenderBuffer(gc, gc->pc);
226
227 /* Send the glXUseFont request */
228 LockDisplay(dpy);
229 GetReq(GLXUseXFont, req);
230 req->reqType = gc->majorOpcode;
231 req->glxCode = X_GLXUseXFont;
232 req->contextTag = gc->currentContextTag;
233 req->font = font;
234 req->first = first;
235 req->count = count;
236 req->listBase = listBase;
237 UnlockDisplay(dpy);
238 SyncHandle();
239 }
240
241 static void
242 indirect_bind_tex_image(Display * dpy,
243 GLXDrawable drawable,
244 int buffer, const int *attrib_list)
245 {
246 xGLXVendorPrivateReq *req;
247 struct glx_context *gc = __glXGetCurrentContext();
248 CARD32 *drawable_ptr;
249 INT32 *buffer_ptr;
250 CARD32 *num_attrib_ptr;
251 CARD32 *attrib_ptr;
252 CARD8 opcode;
253 unsigned int i;
254
255 i = 0;
256 if (attrib_list) {
257 while (attrib_list[i * 2] != None)
258 i++;
259 }
260
261 opcode = __glXSetupForCommand(dpy);
262 if (!opcode)
263 return;
264
265 LockDisplay(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;
271
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);
276
277 *drawable_ptr = drawable;
278 *buffer_ptr = buffer;
279 *num_attrib_ptr = (CARD32) i;
280
281 i = 0;
282 if (attrib_list) {
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];
286 i++;
287 }
288 }
289
290 UnlockDisplay(dpy);
291 SyncHandle();
292 }
293
294 static void
295 indirect_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
296 {
297 xGLXVendorPrivateReq *req;
298 struct glx_context *gc = __glXGetCurrentContext();
299 CARD32 *drawable_ptr;
300 INT32 *buffer_ptr;
301 CARD8 opcode;
302
303 opcode = __glXSetupForCommand(dpy);
304 if (!opcode)
305 return;
306
307 LockDisplay(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;
313
314 drawable_ptr = (CARD32 *) (req + 1);
315 buffer_ptr = (INT32 *) (drawable_ptr + 1);
316
317 *drawable_ptr = drawable;
318 *buffer_ptr = buffer;
319
320 UnlockDisplay(dpy);
321 SyncHandle();
322 }
323
324 static const struct glx_context_vtable indirect_context_vtable = {
325 indirect_destroy_context,
326 indirect_bind_context,
327 indirect_unbind_context,
328 indirect_wait_gl,
329 indirect_wait_x,
330 indirect_use_x_font,
331 indirect_bind_tex_image,
332 indirect_release_tex_image,
333 NULL, /* get_proc_address */
334 };
335
336 /**
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).
340 *
341 * \note
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.
344 */
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)
349 {
350 struct glx_context *gc;
351 int bufSize;
352 CARD8 opcode;
353 __GLXattribute *state;
354
355 opcode = __glXSetupForCommand(psc->dpy);
356 if (!opcode) {
357 return NULL;
358 }
359
360 /* Allocate our context record */
361 gc = calloc(1, sizeof *gc);
362 if (!gc) {
363 /* Out of memory */
364 return NULL;
365 }
366
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;
372
373 if (state == NULL) {
374 /* Out of memory */
375 free(gc);
376 return NULL;
377 }
378 gc->client_state_private = state;
379 state->NoDrawArraysProtocol = (getenv("LIBGL_NO_DRAWARRAYS") != NULL);
380
381 /*
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.
386 */
387
388 bufSize = (XMaxRequestSize(psc->dpy) * 4) - sz_xGLXRenderReq;
389 gc->buf = malloc(bufSize);
390 if (!gc->buf) {
391 free(gc->client_state_private);
392 free(gc);
393 return NULL;
394 }
395 gc->bufSize = bufSize;
396
397 /* Fill in the new context */
398 gc->renderMode = GL_RENDER;
399
400 state->storePack.alignment = 4;
401 state->storeUnpack.alignment = 4;
402
403 gc->attributes.stackPointer = &gc->attributes.stack[0];
404
405 /*
406 ** PERFORMANCE NOTE: A mode dependent fill image can speed things up.
407 */
408 gc->fillImage = __glFillImage;
409 gc->pc = gc->buf;
410 gc->bufEnd = gc->buf + bufSize;
411 gc->isDirect = GL_FALSE;
412 if (__glXDebug) {
413 /*
414 ** Set limit register so that there will be one command per packet
415 */
416 gc->limit = gc->buf;
417 }
418 else {
419 gc->limit = gc->buf + bufSize - __GLX_BUFFER_LIMIT_SIZE;
420 }
421 gc->majorOpcode = opcode;
422
423 /*
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
427 ** limit.
428 */
429 if (bufSize > __GLX_RENDER_CMD_SIZE_LIMIT) {
430 bufSize = __GLX_RENDER_CMD_SIZE_LIMIT;
431 }
432 if (bufSize > __GLX_MAX_RENDER_CMD_SIZE) {
433 bufSize = __GLX_MAX_RENDER_CMD_SIZE;
434 }
435 gc->maxSmallRenderCommandSize = bufSize;
436
437
438 return gc;
439 }
440
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,
447 unsigned *error)
448 {
449 int renderType = GLX_RGBA_TYPE;
450 unsigned i;
451
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.
454 */
455 (void) error;
456
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.
460 */
461 for (i = 0; i < num_attribs; i++) {
462 if (attribs[i * 2] == GLX_RENDER_TYPE)
463 renderType = attribs[i * 2 + 1];
464 }
465
466 return indirect_create_context(base, config_base, shareList, renderType);
467 }
468
469 struct glx_screen_vtable indirect_screen_vtable = {
470 indirect_create_context,
471 indirect_create_context_attribs
472 };
473
474 _X_HIDDEN struct glx_screen *
475 indirect_create_screen(int screen, struct glx_display * priv)
476 {
477 struct glx_screen *psc;
478
479 psc = calloc(1, sizeof *psc);
480 if (psc == NULL)
481 return NULL;
482
483 glx_screen_init(psc, screen, priv);
484 psc->vtable = &indirect_screen_vtable;
485
486 return psc;
487 }