Merge remote branch 'origin/gallium-0.2' into gallium-0.2
[mesa.git] / src / glx / x11 / glx_pbuffer.c
1 /* -*- mode: c; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3; coding: utf-8-unix -*- */
2 /*
3 * (C) Copyright IBM Corporation 2004
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file glx_pbuffer.c
28 * Implementation of pbuffer related functions.
29 *
30 * \author Ian Romanick <idr@us.ibm.com>
31 */
32
33 #include <inttypes.h>
34 #include "glxclient.h"
35 #include <X11/extensions/extutil.h>
36 #include <X11/extensions/Xext.h>
37 #include <assert.h>
38 #include <string.h>
39 #include "glapi.h"
40 #include "glxextensions.h"
41 #include "glcontextmodes.h"
42
43
44 /**
45 * Change a drawable's attribute.
46 *
47 * This function is used to implement \c glXSelectEvent and
48 * \c glXSelectEventSGIX.
49 *
50 * \note
51 * This function dynamically determines whether to use the SGIX_pbuffer
52 * version of the protocol or the GLX 1.3 version of the protocol.
53 *
54 * \todo
55 * This function needs to be modified to work with direct-rendering drivers.
56 */
57 static void
58 ChangeDrawableAttribute(Display * dpy, GLXDrawable drawable,
59 const CARD32 * attribs, size_t num_attribs)
60 {
61 __GLXdisplayPrivate *priv = __glXInitialize(dpy);
62 CARD32 *output;
63 CARD8 opcode;
64
65 if ((dpy == NULL) || (drawable == 0)) {
66 return;
67 }
68
69 opcode = __glXSetupForCommand(dpy);
70 if (!opcode)
71 return;
72
73 LockDisplay(dpy);
74
75 if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
76 xGLXChangeDrawableAttributesReq *req;
77
78 GetReqExtra(GLXChangeDrawableAttributes, 8 + (8 * num_attribs), req);
79 output = (CARD32 *) (req + 1);
80
81 req->reqType = opcode;
82 req->glxCode = X_GLXChangeDrawableAttributes;
83 req->drawable = drawable;
84 req->numAttribs = (CARD32) num_attribs;
85 }
86 else {
87 xGLXVendorPrivateWithReplyReq *vpreq;
88
89 GetReqExtra(GLXVendorPrivateWithReply, 4 + (8 * num_attribs), vpreq);
90 output = (CARD32 *) (vpreq + 1);
91
92 vpreq->reqType = opcode;
93 vpreq->glxCode = X_GLXVendorPrivateWithReply;
94 vpreq->vendorCode = X_GLXvop_ChangeDrawableAttributesSGIX;
95
96 output[0] = (CARD32) drawable;
97 output++;
98 }
99
100 (void) memcpy(output, attribs, sizeof(CARD32) * 2 * num_attribs);
101
102 UnlockDisplay(dpy);
103 SyncHandle();
104
105 return;
106 }
107
108
109 /**
110 * Destroy a pbuffer.
111 *
112 * This function is used to implement \c glXDestroyPbuffer and
113 * \c glXDestroyGLXPbufferSGIX.
114 *
115 * \note
116 * This function dynamically determines whether to use the SGIX_pbuffer
117 * version of the protocol or the GLX 1.3 version of the protocol.
118 *
119 * \todo
120 * This function needs to be modified to work with direct-rendering drivers.
121 */
122 static void
123 DestroyPbuffer(Display * dpy, GLXDrawable drawable)
124 {
125 __GLXdisplayPrivate *priv = __glXInitialize(dpy);
126 CARD8 opcode;
127
128 if ((dpy == NULL) || (drawable == 0)) {
129 return;
130 }
131
132 opcode = __glXSetupForCommand(dpy);
133 if (!opcode)
134 return;
135
136 LockDisplay(dpy);
137
138 if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
139 xGLXDestroyPbufferReq *req;
140
141 GetReq(GLXDestroyPbuffer, req);
142 req->reqType = opcode;
143 req->glxCode = X_GLXDestroyPbuffer;
144 req->pbuffer = (GLXPbuffer) drawable;
145 }
146 else {
147 xGLXVendorPrivateWithReplyReq *vpreq;
148 CARD32 *data;
149
150 GetReqExtra(GLXVendorPrivateWithReply, 4, vpreq);
151 data = (CARD32 *) (vpreq + 1);
152
153 data[0] = (CARD32) drawable;
154
155 vpreq->reqType = opcode;
156 vpreq->glxCode = X_GLXVendorPrivateWithReply;
157 vpreq->vendorCode = X_GLXvop_DestroyGLXPbufferSGIX;
158 }
159
160 UnlockDisplay(dpy);
161 SyncHandle();
162
163 return;
164 }
165
166
167 #ifdef GLX_DIRECT_RENDERING
168 extern __GLXDRIdrawable *GetGLXDRIDrawable(Display * dpy,
169 GLXDrawable drawable,
170 int *const scrn_num);
171
172 static GLenum
173 determineTextureTarget(const int *attribs, int numAttribs)
174 {
175 GLenum target = 0;
176 int i;
177
178 for (i = 0; i < numAttribs; i++) {
179 if (attribs[2 * i] == GLX_TEXTURE_TARGET_EXT) {
180 switch (attribs[2 * i + 1]) {
181 case GLX_TEXTURE_2D_EXT:
182 target = GL_TEXTURE_2D;
183 break;
184 case GLX_TEXTURE_RECTANGLE_EXT:
185 target = GL_TEXTURE_RECTANGLE_ARB;
186 break;
187 }
188 }
189 }
190
191 return target;
192 }
193 #endif
194
195 /**
196 * Get a drawable's attribute.
197 *
198 * This function is used to implement \c glXGetSelectedEvent and
199 * \c glXGetSelectedEventSGIX.
200 *
201 * \note
202 * This function dynamically determines whether to use the SGIX_pbuffer
203 * version of the protocol or the GLX 1.3 version of the protocol.
204 *
205 * \todo
206 * The number of attributes returned is likely to be small, probably less than
207 * 10. Given that, this routine should try to use an array on the stack to
208 * capture the reply rather than always calling Xmalloc.
209 *
210 * \todo
211 * This function needs to be modified to work with direct-rendering drivers.
212 */
213 static int
214 GetDrawableAttribute(Display * dpy, GLXDrawable drawable,
215 int attribute, unsigned int *value)
216 {
217 __GLXdisplayPrivate *priv;
218 xGLXGetDrawableAttributesReply reply;
219 CARD32 *data;
220 CARD8 opcode;
221 unsigned int length;
222 unsigned int i;
223 unsigned int num_attributes;
224
225 if ((dpy == NULL) || (drawable == 0)) {
226 return 0;
227 }
228
229 priv = __glXInitialize(dpy);
230 GLboolean use_glx_1_3 = ((priv->majorVersion > 1)
231 || (priv->minorVersion >= 3));
232
233 *value = 0;
234
235
236 opcode = __glXSetupForCommand(dpy);
237 if (!opcode)
238 return 0;
239
240 LockDisplay(dpy);
241
242 if (use_glx_1_3) {
243 xGLXGetDrawableAttributesReq *req;
244
245 GetReqExtra(GLXGetDrawableAttributes, 4, req);
246 req->reqType = opcode;
247 req->glxCode = X_GLXGetDrawableAttributes;
248 req->drawable = drawable;
249 }
250 else {
251 xGLXVendorPrivateWithReplyReq *vpreq;
252
253 GetReqExtra(GLXVendorPrivateWithReply, 4, vpreq);
254 data = (CARD32 *) (vpreq + 1);
255 data[0] = (CARD32) drawable;
256
257 vpreq->reqType = opcode;
258 vpreq->glxCode = X_GLXVendorPrivateWithReply;
259 vpreq->vendorCode = X_GLXvop_GetDrawableAttributesSGIX;
260 }
261
262 _XReply(dpy, (xReply *) & reply, 0, False);
263
264 if (reply.type == X_Error) {
265 UnlockDisplay(dpy);
266 SyncHandle();
267 return 0;
268 }
269
270 length = reply.length;
271 if (length) {
272 num_attributes = (use_glx_1_3) ? reply.numAttribs : length / 2;
273 data = (CARD32 *) Xmalloc(length * sizeof(CARD32));
274 if (data == NULL) {
275 /* Throw data on the floor */
276 _XEatData(dpy, length);
277 }
278 else {
279 _XRead(dpy, (char *) data, length * sizeof(CARD32));
280
281 /* Search the set of returned attributes for the attribute requested by
282 * the caller.
283 */
284 for (i = 0; i < num_attributes; i++) {
285 if (data[i * 2] == attribute) {
286 *value = data[(i * 2) + 1];
287 break;
288 }
289 }
290
291 #ifdef GLX_DIRECT_RENDERING
292 {
293 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, NULL);
294
295 if (pdraw != NULL && !pdraw->textureTarget)
296 pdraw->textureTarget =
297 determineTextureTarget((const int *) data, num_attributes);
298 }
299 #endif
300
301 Xfree(data);
302 }
303 }
304
305 UnlockDisplay(dpy);
306 SyncHandle();
307
308 return 0;
309 }
310
311 /**
312 * Create a non-pbuffer GLX drawable.
313 *
314 * \todo
315 * This function needs to be modified to work with direct-rendering drivers.
316 */
317 static GLXDrawable
318 CreateDrawable(Display * dpy, const __GLcontextModes * fbconfig,
319 Drawable drawable, const int *attrib_list, CARD8 glxCode)
320 {
321 xGLXCreateWindowReq *req;
322 CARD32 *data;
323 unsigned int i;
324 CARD8 opcode;
325
326 i = 0;
327 if (attrib_list) {
328 while (attrib_list[i * 2] != None)
329 i++;
330 }
331
332 opcode = __glXSetupForCommand(dpy);
333 if (!opcode)
334 return None;
335
336 LockDisplay(dpy);
337 GetReqExtra(GLXCreateWindow, 8 * i, req);
338 data = (CARD32 *) (req + 1);
339
340 req->reqType = opcode;
341 req->glxCode = glxCode;
342 req->screen = (CARD32) fbconfig->screen;
343 req->fbconfig = fbconfig->fbconfigID;
344 req->window = (CARD32) drawable;
345 req->glxwindow = (GLXWindow) XAllocID(dpy);
346 req->numAttribs = (CARD32) i;
347
348 memcpy(data, attrib_list, 8 * i);
349
350 UnlockDisplay(dpy);
351 SyncHandle();
352
353 #ifdef GLX_DIRECT_RENDERING
354 do {
355 /* FIXME: Maybe delay __DRIdrawable creation until the drawable
356 * is actually bound to a context... */
357
358 __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
359 __GLXDRIdrawable *pdraw;
360 __GLXscreenConfigs *psc;
361
362 psc = &priv->screenConfigs[fbconfig->screen];
363 if (psc->driScreen == NULL)
364 break;
365 pdraw = psc->driScreen->createDrawable(psc, drawable,
366 req->glxwindow, fbconfig);
367 if (pdraw == NULL) {
368 fprintf(stderr, "failed to create drawable\n");
369 break;
370 }
371
372 if (__glxHashInsert(psc->drawHash, req->glxwindow, pdraw)) {
373 (*pdraw->destroyDrawable) (pdraw);
374 return None; /* FIXME: Check what we're supposed to do here... */
375 }
376
377 pdraw->textureTarget = determineTextureTarget(attrib_list, i);
378 } while (0);
379 #endif
380
381 return (GLXDrawable) req->glxwindow;
382 }
383
384
385 /**
386 * Destroy a non-pbuffer GLX drawable.
387 *
388 * \todo
389 * This function needs to be modified to work with direct-rendering drivers.
390 */
391 static void
392 DestroyDrawable(Display * dpy, GLXDrawable drawable, CARD32 glxCode)
393 {
394 xGLXDestroyPbufferReq *req;
395 CARD8 opcode;
396
397 if ((dpy == NULL) || (drawable == 0)) {
398 return;
399 }
400
401
402 opcode = __glXSetupForCommand(dpy);
403 if (!opcode)
404 return;
405
406 LockDisplay(dpy);
407
408 GetReqExtra(GLXDestroyPbuffer, 4, req);
409 req->reqType = opcode;
410 req->glxCode = glxCode;
411 req->pbuffer = (GLXPbuffer) drawable;
412
413 UnlockDisplay(dpy);
414 SyncHandle();
415
416 #ifdef GLX_DIRECT_RENDERING
417 {
418 int screen;
419 __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
420 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
421 __GLXscreenConfigs *psc = &priv->screenConfigs[screen];
422
423 if (pdraw != NULL) {
424 (*pdraw->destroyDrawable) (pdraw);
425 __glxHashDelete(psc->drawHash, drawable);
426 }
427 }
428 #endif
429
430 return;
431 }
432
433
434 /**
435 * Create a pbuffer.
436 *
437 * This function is used to implement \c glXCreatePbuffer and
438 * \c glXCreateGLXPbufferSGIX.
439 *
440 * \note
441 * This function dynamically determines whether to use the SGIX_pbuffer
442 * version of the protocol or the GLX 1.3 version of the protocol.
443 *
444 * \todo
445 * This function needs to be modified to work with direct-rendering drivers.
446 */
447 static GLXDrawable
448 CreatePbuffer(Display * dpy, const __GLcontextModes * fbconfig,
449 unsigned int width, unsigned int height,
450 const int *attrib_list, GLboolean size_in_attribs)
451 {
452 __GLXdisplayPrivate *priv = __glXInitialize(dpy);
453 GLXDrawable id = 0;
454 CARD32 *data;
455 CARD8 opcode;
456 unsigned int i;
457
458 i = 0;
459 if (attrib_list) {
460 while (attrib_list[i * 2])
461 i++;
462 }
463
464 opcode = __glXSetupForCommand(dpy);
465 if (!opcode)
466 return None;
467
468 LockDisplay(dpy);
469 id = XAllocID(dpy);
470
471 if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
472 xGLXCreatePbufferReq *req;
473 unsigned int extra = (size_in_attribs) ? 0 : 2;
474
475 GetReqExtra(GLXCreatePbuffer, (8 * (i + extra)), req);
476 data = (CARD32 *) (req + 1);
477
478 req->reqType = opcode;
479 req->glxCode = X_GLXCreatePbuffer;
480 req->screen = (CARD32) fbconfig->screen;
481 req->fbconfig = fbconfig->fbconfigID;
482 req->pbuffer = (GLXPbuffer) id;
483 req->numAttribs = (CARD32) (i + extra);
484
485 if (!size_in_attribs) {
486 data[(2 * i) + 0] = GLX_PBUFFER_WIDTH;
487 data[(2 * i) + 1] = width;
488 data[(2 * i) + 2] = GLX_PBUFFER_HEIGHT;
489 data[(2 * i) + 3] = height;
490 data += 4;
491 }
492 }
493 else {
494 xGLXVendorPrivateReq *vpreq;
495
496 GetReqExtra(GLXVendorPrivate, 20 + (8 * i), vpreq);
497 data = (CARD32 *) (vpreq + 1);
498
499 vpreq->reqType = opcode;
500 vpreq->glxCode = X_GLXVendorPrivate;
501 vpreq->vendorCode = X_GLXvop_CreateGLXPbufferSGIX;
502
503 data[0] = (CARD32) fbconfig->screen;
504 data[1] = (CARD32) fbconfig->fbconfigID;
505 data[2] = (CARD32) id;
506 data[3] = (CARD32) width;
507 data[4] = (CARD32) height;
508 data += 5;
509 }
510
511 (void) memcpy(data, attrib_list, sizeof(CARD32) * 2 * i);
512
513 UnlockDisplay(dpy);
514 SyncHandle();
515
516 return id;
517 }
518
519
520 /**
521 * Create a new pbuffer.
522 */
523 PUBLIC GLXPbufferSGIX
524 glXCreateGLXPbufferSGIX(Display * dpy, GLXFBConfigSGIX config,
525 unsigned int width, unsigned int height,
526 int *attrib_list)
527 {
528 return (GLXPbufferSGIX) CreatePbuffer(dpy, (__GLcontextModes *) config,
529 width, height,
530 attrib_list, GL_FALSE);
531 }
532
533
534 /**
535 * Create a new pbuffer.
536 */
537 PUBLIC GLXPbuffer
538 glXCreatePbuffer(Display * dpy, GLXFBConfig config, const int *attrib_list)
539 {
540 int i, width, height;
541
542 width = 0;
543 height = 0;
544
545 for (i = 0; attrib_list[i * 2]; i++) {
546 switch (attrib_list[i * 2]) {
547 case GLX_PBUFFER_WIDTH:
548 width = attrib_list[i * 2 + 1];
549 break;
550 case GLX_PBUFFER_HEIGHT:
551 height = attrib_list[i * 2 + 1];
552 break;
553 }
554 }
555
556 return (GLXPbuffer) CreatePbuffer(dpy, (__GLcontextModes *) config,
557 width, height, attrib_list, GL_TRUE);
558 }
559
560
561 /**
562 * Destroy an existing pbuffer.
563 */
564 PUBLIC void
565 glXDestroyPbuffer(Display * dpy, GLXPbuffer pbuf)
566 {
567 DestroyPbuffer(dpy, pbuf);
568 }
569
570
571 /**
572 * Query an attribute of a drawable.
573 */
574 PUBLIC void
575 glXQueryDrawable(Display * dpy, GLXDrawable drawable,
576 int attribute, unsigned int *value)
577 {
578 GetDrawableAttribute(dpy, drawable, attribute, value);
579 }
580
581
582 /**
583 * Query an attribute of a pbuffer.
584 */
585 PUBLIC int
586 glXQueryGLXPbufferSGIX(Display * dpy, GLXPbufferSGIX drawable,
587 int attribute, unsigned int *value)
588 {
589 return GetDrawableAttribute(dpy, drawable, attribute, value);
590 }
591
592
593 /**
594 * Select the event mask for a drawable.
595 */
596 PUBLIC void
597 glXSelectEvent(Display * dpy, GLXDrawable drawable, unsigned long mask)
598 {
599 CARD32 attribs[2];
600
601 attribs[0] = (CARD32) GLX_EVENT_MASK;
602 attribs[1] = (CARD32) mask;
603
604 ChangeDrawableAttribute(dpy, drawable, attribs, 1);
605 }
606
607
608 /**
609 * Get the selected event mask for a drawable.
610 */
611 PUBLIC void
612 glXGetSelectedEvent(Display * dpy, GLXDrawable drawable, unsigned long *mask)
613 {
614 unsigned int value;
615
616
617 /* The non-sense with value is required because on LP64 platforms
618 * sizeof(unsigned int) != sizeof(unsigned long). On little-endian
619 * we could just type-cast the pointer, but why?
620 */
621
622 GetDrawableAttribute(dpy, drawable, GLX_EVENT_MASK_SGIX, &value);
623 *mask = value;
624 }
625
626
627 PUBLIC GLXPixmap
628 glXCreatePixmap(Display * dpy, GLXFBConfig config, Pixmap pixmap,
629 const int *attrib_list)
630 {
631 return CreateDrawable(dpy, (__GLcontextModes *) config,
632 (Drawable) pixmap, attrib_list, X_GLXCreatePixmap);
633 }
634
635
636 PUBLIC GLXWindow
637 glXCreateWindow(Display * dpy, GLXFBConfig config, Window win,
638 const int *attrib_list)
639 {
640 return CreateDrawable(dpy, (__GLcontextModes *) config,
641 (Drawable) win, attrib_list, X_GLXCreateWindow);
642 }
643
644
645 PUBLIC void
646 glXDestroyPixmap(Display * dpy, GLXPixmap pixmap)
647 {
648 DestroyDrawable(dpy, (GLXDrawable) pixmap, X_GLXDestroyPixmap);
649 }
650
651
652 PUBLIC void
653 glXDestroyWindow(Display * dpy, GLXWindow win)
654 {
655 DestroyDrawable(dpy, (GLXDrawable) win, X_GLXDestroyWindow);
656 }
657
658
659 PUBLIC
660 GLX_ALIAS_VOID(glXDestroyGLXPbufferSGIX,
661 (Display * dpy, GLXPbufferSGIX pbuf),
662 (dpy, pbuf), glXDestroyPbuffer)
663
664 PUBLIC
665 GLX_ALIAS_VOID(glXSelectEventSGIX,
666 (Display * dpy, GLXDrawable drawable,
667 unsigned long mask), (dpy, drawable, mask),
668 glXSelectEvent)
669
670 PUBLIC
671 GLX_ALIAS_VOID(glXGetSelectedEventSGIX,
672 (Display * dpy, GLXDrawable drawable,
673 unsigned long *mask), (dpy, drawable, mask),
674 glXGetSelectedEvent)