glx: remove #include "glheader.h" lines
[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 *
168 GetGLXDRIDrawable(Display *dpy, GLXDrawable drawable, int * const scrn_num);
169
170 static GLenum
171 determineTextureTarget(const int *attribs, int numAttribs)
172 {
173 GLenum target = 0;
174 int i;
175
176 for (i = 0; i < numAttribs; i++) {
177 if (attribs[2 * i] == GLX_TEXTURE_TARGET_EXT) {
178 switch (attribs[2 * i + 1]) {
179 case GLX_TEXTURE_2D_EXT:
180 target = GL_TEXTURE_2D;
181 break;
182 case GLX_TEXTURE_RECTANGLE_EXT:
183 target = GL_TEXTURE_RECTANGLE_ARB;
184 break;
185 }
186 }
187 }
188
189 return target;
190 }
191 #endif
192
193 /**
194 * Get a drawable's attribute.
195 *
196 * This function is used to implement \c glXGetSelectedEvent and
197 * \c glXGetSelectedEventSGIX.
198 *
199 * \note
200 * This function dynamically determines whether to use the SGIX_pbuffer
201 * version of the protocol or the GLX 1.3 version of the protocol.
202 *
203 * \todo
204 * The number of attributes returned is likely to be small, probably less than
205 * 10. Given that, this routine should try to use an array on the stack to
206 * capture the reply rather than always calling Xmalloc.
207 *
208 * \todo
209 * This function needs to be modified to work with direct-rendering drivers.
210 */
211 static int
212 GetDrawableAttribute( Display *dpy, GLXDrawable drawable,
213 int attribute, unsigned int *value )
214 {
215 __GLXdisplayPrivate *priv;
216 xGLXGetDrawableAttributesReply reply;
217 CARD32 * data;
218 CARD8 opcode;
219 unsigned int length;
220 unsigned int i;
221 unsigned int num_attributes;
222
223 if ( (dpy == NULL) || (drawable == 0) ) {
224 return 0;
225 }
226
227 priv = __glXInitialize(dpy);
228 GLboolean use_glx_1_3 = ((priv->majorVersion > 1)
229 || (priv->minorVersion >= 3));
230
231 *value = 0;
232
233
234 opcode = __glXSetupForCommand(dpy);
235 if (!opcode)
236 return 0;
237
238 LockDisplay(dpy);
239
240 if ( use_glx_1_3 ) {
241 xGLXGetDrawableAttributesReq *req;
242
243 GetReqExtra( GLXGetDrawableAttributes, 4, req );
244 req->reqType = opcode;
245 req->glxCode = X_GLXGetDrawableAttributes;
246 req->drawable = drawable;
247 }
248 else {
249 xGLXVendorPrivateWithReplyReq *vpreq;
250
251 GetReqExtra( GLXVendorPrivateWithReply, 4, vpreq );
252 data = (CARD32 *) (vpreq + 1);
253 data[0] = (CARD32) drawable;
254
255 vpreq->reqType = opcode;
256 vpreq->glxCode = X_GLXVendorPrivateWithReply;
257 vpreq->vendorCode = X_GLXvop_GetDrawableAttributesSGIX;
258 }
259
260 _XReply(dpy, (xReply*) &reply, 0, False);
261
262 if (reply.type == X_Error)
263 {
264 UnlockDisplay(dpy);
265 SyncHandle();
266 return 0;
267 }
268
269 length = reply.length;
270 if (length)
271 {
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 } 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 = determineTextureTarget((const int *)data,
296 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,
319 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,
558 attrib_list, GL_TRUE );
559 }
560
561
562 /**
563 * Destroy an existing pbuffer.
564 */
565 PUBLIC void
566 glXDestroyPbuffer(Display *dpy, GLXPbuffer pbuf)
567 {
568 DestroyPbuffer( dpy, pbuf );
569 }
570
571
572 /**
573 * Query an attribute of a drawable.
574 */
575 PUBLIC void
576 glXQueryDrawable(Display *dpy, GLXDrawable drawable,
577 int attribute, unsigned int *value)
578 {
579 GetDrawableAttribute( dpy, drawable, attribute, value );
580 }
581
582
583 /**
584 * Query an attribute of a pbuffer.
585 */
586 PUBLIC int
587 glXQueryGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX drawable,
588 int attribute, unsigned int *value)
589 {
590 return GetDrawableAttribute( dpy, drawable, attribute, value );
591 }
592
593
594 /**
595 * Select the event mask for a drawable.
596 */
597 PUBLIC void
598 glXSelectEvent(Display *dpy, GLXDrawable drawable, unsigned long mask)
599 {
600 CARD32 attribs[2];
601
602 attribs[0] = (CARD32) GLX_EVENT_MASK;
603 attribs[1] = (CARD32) mask;
604
605 ChangeDrawableAttribute( dpy, drawable, attribs, 1 );
606 }
607
608
609 /**
610 * Get the selected event mask for a drawable.
611 */
612 PUBLIC void
613 glXGetSelectedEvent(Display *dpy, GLXDrawable drawable, unsigned long *mask)
614 {
615 unsigned int value;
616
617
618 /* The non-sense with value is required because on LP64 platforms
619 * sizeof(unsigned int) != sizeof(unsigned long). On little-endian
620 * we could just type-cast the pointer, but why?
621 */
622
623 GetDrawableAttribute( dpy, drawable, GLX_EVENT_MASK_SGIX, & value );
624 *mask = value;
625 }
626
627
628 PUBLIC GLXPixmap
629 glXCreatePixmap( Display *dpy, GLXFBConfig config, Pixmap pixmap,
630 const int *attrib_list )
631 {
632 return CreateDrawable( dpy, (__GLcontextModes *) config,
633 (Drawable) pixmap, attrib_list,
634 X_GLXCreatePixmap );
635 }
636
637
638 PUBLIC GLXWindow
639 glXCreateWindow( Display *dpy, GLXFBConfig config, Window win,
640 const int *attrib_list )
641 {
642 return CreateDrawable( dpy, (__GLcontextModes *) config,
643 (Drawable) win, attrib_list,
644 X_GLXCreateWindow );
645 }
646
647
648 PUBLIC void
649 glXDestroyPixmap(Display *dpy, GLXPixmap pixmap)
650 {
651 DestroyDrawable( dpy, (GLXDrawable) pixmap, X_GLXDestroyPixmap );
652 }
653
654
655 PUBLIC void
656 glXDestroyWindow(Display *dpy, GLXWindow win)
657 {
658 DestroyDrawable( dpy, (GLXDrawable) win, X_GLXDestroyWindow );
659 }
660
661
662 PUBLIC GLX_ALIAS_VOID(glXDestroyGLXPbufferSGIX,
663 (Display *dpy, GLXPbufferSGIX pbuf),
664 (dpy, pbuf),
665 glXDestroyPbuffer)
666
667 PUBLIC GLX_ALIAS_VOID(glXSelectEventSGIX,
668 (Display *dpy, GLXDrawable drawable, unsigned long mask),
669 (dpy, drawable, mask),
670 glXSelectEvent)
671
672 PUBLIC GLX_ALIAS_VOID(glXGetSelectedEventSGIX,
673 (Display *dpy, GLXDrawable drawable, unsigned long *mask),
674 (dpy, drawable, mask),
675 glXGetSelectedEvent)