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