dnsapi: Cast-qual warnings fix.
[wine] / dlls / winex11.drv / opengl.c
1 /*
2  * X11DRV OpenGL functions
3  *
4  * Copyright 2000 Lionel Ulmer
5  * Copyright 2006 Roderick Colenbrander
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "x11drv.h"
29 #include "wine/library.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
33 WINE_DECLARE_DEBUG_CHANNEL(opengl);
34
35 #if defined(HAVE_GL_GL_H) && defined(HAVE_GL_GLX_H)
36
37 #undef APIENTRY
38 #undef CALLBACK
39 #undef WINAPI
40
41 #ifdef HAVE_GL_GL_H
42 # include <GL/gl.h>
43 #endif
44 #ifdef HAVE_GL_GLX_H
45 # include <GL/glx.h>
46 #endif
47 #ifdef HAVE_GL_GLEXT_H
48 # include <GL/glext.h>
49 #endif
50
51 #undef APIENTRY
52 #undef CALLBACK
53 #undef WINAPI
54
55 /* Redefines the constants */
56 #define CALLBACK    __stdcall
57 #define WINAPI      __stdcall
58 #define APIENTRY    WINAPI
59
60 WINE_DECLARE_DEBUG_CHANNEL(fps);
61
62 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
63   TRACE("  - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
64   TRACE("  - dwFlags : ");
65 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
66   TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
67   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
68   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
69   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
70   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
71   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
72   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
73   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
74   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
75   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
76   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
77   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
78   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
79   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
80   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
81   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
82 #undef TEST_AND_DUMP
83   TRACE("\n");
84
85   TRACE("  - iPixelType : ");
86   switch (ppfd->iPixelType) {
87   case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
88   case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
89   }
90   TRACE("\n");
91
92   TRACE("  - Color   : %d\n", ppfd->cColorBits);
93   TRACE("  - Red     : %d\n", ppfd->cRedBits);
94   TRACE("  - Green   : %d\n", ppfd->cGreenBits);
95   TRACE("  - Blue    : %d\n", ppfd->cBlueBits);
96   TRACE("  - Alpha   : %d\n", ppfd->cAlphaBits);
97   TRACE("  - Accum   : %d\n", ppfd->cAccumBits);
98   TRACE("  - Depth   : %d\n", ppfd->cDepthBits);
99   TRACE("  - Stencil : %d\n", ppfd->cStencilBits);
100   TRACE("  - Aux     : %d\n", ppfd->cAuxBuffers);
101
102   TRACE("  - iLayerType : ");
103   switch (ppfd->iLayerType) {
104   case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
105   case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
106   case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
107   }
108   TRACE("\n");
109 }
110
111 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient and
112    include all dependencies
113 */
114 #ifndef SONAME_LIBGL
115 #define SONAME_LIBGL "libGL.so"
116 #endif
117
118 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
119 MAKE_FUNCPTR(glGetError)
120 MAKE_FUNCPTR(glXChooseVisual)
121 MAKE_FUNCPTR(glXGetConfig)
122 MAKE_FUNCPTR(glXSwapBuffers)
123 MAKE_FUNCPTR(glXQueryExtension)
124
125 MAKE_FUNCPTR(glXGetFBConfigs)
126 MAKE_FUNCPTR(glXChooseFBConfig)
127 MAKE_FUNCPTR(glXGetFBConfigAttrib)
128 MAKE_FUNCPTR(glXCreateGLXPixmap)
129 MAKE_FUNCPTR(glXDestroyGLXPixmap)
130 /* MAKE_FUNCPTR(glXQueryDrawable) */
131 #undef MAKE_FUNCPTR
132
133 static BOOL has_opengl(void)
134 {
135     static int init_done;
136     static void *opengl_handle;
137
138     int error_base, event_base;
139
140     if (init_done) return (opengl_handle != NULL);
141     init_done = 1;
142
143     opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, NULL, 0);
144     if (opengl_handle == NULL) return FALSE;
145
146 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(RTLD_DEFAULT, #f, NULL, 0)) == NULL) goto sym_not_found;
147 LOAD_FUNCPTR(glGetError)
148 LOAD_FUNCPTR(glXChooseVisual)
149 LOAD_FUNCPTR(glXGetConfig)
150 LOAD_FUNCPTR(glXSwapBuffers)
151 LOAD_FUNCPTR(glXQueryExtension)
152
153 LOAD_FUNCPTR(glXGetFBConfigs)
154 LOAD_FUNCPTR(glXChooseFBConfig)
155 LOAD_FUNCPTR(glXGetFBConfigAttrib)
156 LOAD_FUNCPTR(glXCreateGLXPixmap)
157 LOAD_FUNCPTR(glXDestroyGLXPixmap)
158 #undef LOAD_FUNCPTR
159
160     wine_tsx11_lock();
161     if (pglXQueryExtension(gdi_display, &error_base, &event_base) == True) {
162         TRACE("GLX is up and running error_base = %d\n", error_base);
163     } else {
164         wine_dlclose(opengl_handle, NULL, 0);
165         opengl_handle = NULL;
166     }
167     wine_tsx11_unlock();
168     return (opengl_handle != NULL);
169
170 sym_not_found:
171     wine_dlclose(opengl_handle, NULL, 0);
172     opengl_handle = NULL;
173     return FALSE;
174 }
175
176 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
177  * In our WGL implementation we only support a subset of these formats namely the format of
178  * Wine's main visual and offscreen formats (if they are available).
179  * This function converts a WGL format to its corresponding GLX one. It returns the index (zero-based)
180  * into the GLX FB config table and it returns the number of supported WGL formats in fmt_count.
181  */
182 static BOOL ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, int *fmt_index, int *fmt_count)
183 {
184   int res = FALSE;
185   int i = 0;
186   GLXFBConfig* cfgs = NULL;
187   int nCfgs = 0;
188   int tmp_fmt_id = 0;
189   int tmp_vis_id = 0;
190   int nFormats = 1; /* Start at 1 as we always have a main visual */
191   VisualID visualid = 0;
192
193   /* Request to look up the format of the main visual when iPixelFormat = 1 */
194   if(iPixelFormat == 1) visualid = XVisualIDFromVisual(visual);
195
196   /* As mentioned in various parts of the code only the format of the main visual can be used for onscreen rendering.
197    * Next to this format there are also so called offscreen rendering formats (used for pbuffers) which can be supported
198    * because they don't need a visual. Below we use glXGetFBConfigs instead of glXChooseFBConfig to enumerate the fb configurations
199    * bas this call lists both types of formats instead of only onscreen ones. */
200   cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
201   if (NULL == cfgs || 0 == nCfgs) {
202     ERR("glXChooseFBConfig returns NULL\n");
203     if(cfgs != NULL) XFree(cfgs);
204     return FALSE;
205   }
206
207   /* Find the requested offscreen format and count the number of offscreen formats */
208   for(i=0; i<nCfgs; i++) {
209     pglXGetFBConfigAttrib(display, cfgs[i], GLX_VISUAL_ID, &tmp_vis_id);
210     pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &tmp_fmt_id);
211
212     /* We are looking up the GLX index of our main visual and have found it :) */
213     if(iPixelFormat == 1 && visualid == tmp_vis_id) {
214       *fmt_index = i;
215       TRACE("Found FBCONFIG_ID 0x%x at index %d for VISUAL_ID 0x%x\n", tmp_fmt_id, *fmt_index, tmp_vis_id);
216       res = TRUE;
217     }
218     /* We found an offscreen rendering format :) */
219     else if(tmp_vis_id == 0) {
220       nFormats++;
221       TRACE("Checking offscreen format FBCONFIG_ID 0x%x at index %d\n", tmp_fmt_id, i);
222
223       if(iPixelFormat == nFormats) {
224         *fmt_index = i;
225         TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", tmp_fmt_id, iPixelFormat, i);
226         res = TRUE;
227       }
228     }
229   }
230   *fmt_count = nFormats;
231   TRACE("Number of offscreen formats: %d; returning index: %d\n", *fmt_count, *fmt_index);
232
233   if(cfgs != NULL) XFree(cfgs);
234
235   if(res == FALSE && iPixelFormat == 1)
236     ERR("Can't find a matching FBCONFIG_ID for VISUAL_ID 0x%lx!\n", visualid);
237
238   return res;
239 }
240
241 /**
242  * X11DRV_ChoosePixelFormat
243  *
244  * Equivalent of glXChooseVisual
245  */
246 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev, 
247                              const PIXELFORMATDESCRIPTOR *ppfd) {
248   GLXFBConfig* cfgs = NULL;
249   int ret = 0;
250   int nCfgs = 0;
251   int value = 0;
252   int fmt_index = 0;
253
254   if (!has_opengl()) {
255     ERR("No libGL on this box - disabling OpenGL support !\n");
256     return 0;
257   }
258
259   if (TRACE_ON(opengl)) {
260     TRACE("(%p,%p)\n", physDev, ppfd);
261
262     dump_PIXELFORMATDESCRIPTOR((const PIXELFORMATDESCRIPTOR *) ppfd);
263   }
264
265   wine_tsx11_lock(); 
266   if(!visual) {
267     ERR("Can't get an opengl visual!\n");
268     goto choose_exit;
269   }
270
271   /* Get a list containing all supported FB configurations */
272   cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), NULL, &nCfgs);
273   if (NULL == cfgs || 0 == nCfgs) {
274     ERR("glXChooseFBConfig returns NULL (glError: %d)\n", pglGetError());
275     goto choose_exit;
276   }
277
278   /* In case an fbconfig was found, check if it matches to the requirements of the ppfd */
279   if(!ConvertPixelFormatWGLtoGLX(gdi_display, 1 /* main visual */, &fmt_index, &value)) {
280     ERR("Can't find a matching FBCONFIG_ID for VISUAL_ID 0x%lx!\n", visual->visualid);
281   } else {
282     int dwFlags = 0;
283     int iPixelType = 0;
284     int value = 0;
285
286     /* Pixel type */
287     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_RENDER_TYPE, &value);
288     if (value & GLX_RGBA_BIT)
289       iPixelType = PFD_TYPE_RGBA;
290     else
291       iPixelType = PFD_TYPE_COLORINDEX;
292
293     if (ppfd->iPixelType != iPixelType) {
294       goto choose_exit;
295     }
296
297     /* Doublebuffer */
298     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_DOUBLEBUFFER, &value); if (value) dwFlags |= PFD_DOUBLEBUFFER;
299     if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE)) {
300       if ((ppfd->dwFlags & PFD_DOUBLEBUFFER) != (dwFlags & PFD_DOUBLEBUFFER)) {
301         goto choose_exit;
302       }
303     }
304
305     /* Stereo */
306     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_STEREO, &value); if (value) dwFlags |= PFD_STEREO;
307     if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE)) {
308       if ((ppfd->dwFlags & PFD_STEREO) != (dwFlags & PFD_STEREO)) {
309         goto choose_exit;
310       }
311     }
312
313     /* Alpha bits */
314     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_ALPHA_SIZE, &value);
315     if (ppfd->iPixelType==PFD_TYPE_RGBA && ppfd->cAlphaBits && !value) {
316       goto choose_exit;
317     }
318
319     /* Depth bits */
320     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_DEPTH_SIZE, &value);
321     if (ppfd->cDepthBits && !value) {
322       goto choose_exit;
323     }
324
325     /* Stencil bits */
326     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_STENCIL_SIZE, &value);
327     if (ppfd->cStencilBits && !value) {
328       goto choose_exit;
329     }
330
331     /* Aux buffers */
332     pglXGetFBConfigAttrib(gdi_display, cfgs[fmt_index], GLX_AUX_BUFFERS, &value);
333     if (ppfd->cAuxBuffers && !value) {
334       goto choose_exit;
335     }
336
337     /* When we pass all the checks we have found a matching format :) */
338     ret = 1;
339     TRACE("Successfully found a matching mode, returning index: %d\n", ret);
340   }
341
342 choose_exit:
343   if(!ret)
344     TRACE("No matching mode was found returning 0\n");
345
346   if (NULL != cfgs) XFree(cfgs);
347   wine_tsx11_unlock();
348   return ret;
349 }
350
351 /**
352  * X11DRV_DescribePixelFormat
353  *
354  * Get the pixel-format descriptor associated to the given id
355  */
356 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
357                                int iPixelFormat,
358                                UINT nBytes,
359                                PIXELFORMATDESCRIPTOR *ppfd) {
360   /*XVisualInfo *vis;*/
361   int value;
362   int rb,gb,bb,ab;
363
364   GLXFBConfig* cfgs = NULL;
365   GLXFBConfig cur;
366   int nCfgs = 0;
367   int ret = 0;
368   int fmt_index = 0;
369
370   if (!has_opengl()) {
371     ERR("No libGL on this box - disabling OpenGL support !\n");
372     return 0;
373   }
374   
375   TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
376
377   wine_tsx11_lock();
378   cfgs = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs);
379   wine_tsx11_unlock();
380
381   if (NULL == cfgs || 0 == nCfgs) {
382     ERR("unexpected iPixelFormat(%d), returns NULL\n", iPixelFormat);
383     return 0; /* unespected error */
384   }
385
386   /* This function always reports the total number of supported pixel formats.
387    * At the moment we only support the pixel format corresponding to the main
388    * visual which got created at x11drv initialization. More formats could be
389    * supported if there was a way to recreate x11 windows in x11drv. 
390    * Because we only support one format nCfgs needs to be set to 1.
391    */
392   nCfgs = 1;
393
394   if (ppfd == NULL) {
395     /* The application is only querying the number of visuals */
396     wine_tsx11_lock();
397     if (NULL != cfgs) XFree(cfgs);
398     wine_tsx11_unlock();
399     return nCfgs;
400   }
401
402   if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
403     ERR("Wrong structure size !\n");
404     /* Should set error */
405     return 0;
406   }
407
408   if (nCfgs < iPixelFormat || 1 > iPixelFormat) {
409     WARN("unexpected iPixelFormat(%d): not >=1 and <=nFormats(%d), returning NULL\n", iPixelFormat, nCfgs);
410     return 0;
411   }
412
413   /* Retrieve the index in the FBConfig table corresponding to the visual ID from the main visual */
414   if(!ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, &fmt_index, &value)) {
415       ERR("Can't find a valid pixel format index from the main visual, expect problems!\n");
416       return 0;
417   }
418
419   ret = nCfgs;
420   cur = cfgs[fmt_index];
421
422   memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
423   ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
424   ppfd->nVersion = 1;
425
426   /* These flags are always the same... */
427   ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
428   /* Now the flags extracted from the Visual */
429
430   wine_tsx11_lock();
431
432   pglXGetFBConfigAttrib(gdi_display, cur, GLX_CONFIG_CAVEAT, &value);
433   if(value == GLX_SLOW_CONFIG)
434       ppfd->dwFlags |= PFD_GENERIC_ACCELERATED;
435
436   pglXGetFBConfigAttrib(gdi_display, cur, GLX_DOUBLEBUFFER, &value); if (value) ppfd->dwFlags |= PFD_DOUBLEBUFFER;
437   pglXGetFBConfigAttrib(gdi_display, cur, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
438
439   /* Pixel type */
440   pglXGetFBConfigAttrib(gdi_display, cur, GLX_RENDER_TYPE, &value);
441   if (value & GLX_RGBA_BIT)
442     ppfd->iPixelType = PFD_TYPE_RGBA;
443   else
444     ppfd->iPixelType = PFD_TYPE_COLORINDEX;
445
446   /* Color bits */
447   pglXGetFBConfigAttrib(gdi_display, cur, GLX_BUFFER_SIZE, &value);
448   ppfd->cColorBits = value;
449
450   /* Red, green, blue and alpha bits / shifts */
451   if (ppfd->iPixelType == PFD_TYPE_RGBA) {
452     pglXGetFBConfigAttrib(gdi_display, cur, GLX_RED_SIZE, &rb);
453     pglXGetFBConfigAttrib(gdi_display, cur, GLX_GREEN_SIZE, &gb);
454     pglXGetFBConfigAttrib(gdi_display, cur, GLX_BLUE_SIZE, &bb);
455     pglXGetFBConfigAttrib(gdi_display, cur, GLX_ALPHA_SIZE, &ab);
456
457     ppfd->cRedBits = rb;
458     ppfd->cRedShift = gb + bb + ab;
459     ppfd->cBlueBits = bb;
460     ppfd->cBlueShift = ab;
461     ppfd->cGreenBits = gb;
462     ppfd->cGreenShift = bb + ab;
463     ppfd->cAlphaBits = ab;
464     ppfd->cAlphaShift = 0;
465   } else {
466     ppfd->cRedBits = 0;
467     ppfd->cRedShift = 0;
468     ppfd->cBlueBits = 0;
469     ppfd->cBlueShift = 0;
470     ppfd->cGreenBits = 0;
471     ppfd->cGreenShift = 0;
472     ppfd->cAlphaBits = 0;
473     ppfd->cAlphaShift = 0;
474   }
475   /* Accums : to do ... */
476
477   /* Depth bits */
478   pglXGetFBConfigAttrib(gdi_display, cur, GLX_DEPTH_SIZE, &value);
479   ppfd->cDepthBits = value;
480
481   /* stencil bits */
482   pglXGetFBConfigAttrib(gdi_display, cur, GLX_STENCIL_SIZE, &value);
483   ppfd->cStencilBits = value;
484
485   wine_tsx11_unlock();
486
487   /* Aux : to do ... */
488
489   ppfd->iLayerType = PFD_MAIN_PLANE;
490
491   if (TRACE_ON(opengl)) {
492     dump_PIXELFORMATDESCRIPTOR(ppfd);
493   }
494
495   wine_tsx11_lock();
496   if (NULL != cfgs) XFree(cfgs);
497   wine_tsx11_unlock();
498
499   return ret;
500 }
501
502 /**
503  * X11DRV_GetPixelFormat
504  *
505  * Get the pixel-format id used by this DC
506  */
507 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
508   TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
509
510   return physDev->current_pf;
511 }
512
513 /**
514  * X11DRV_SetPixelFormat
515  *
516  * Set the pixel-format id used by this DC
517  */
518 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
519                            int iPixelFormat,
520                            const PIXELFORMATDESCRIPTOR *ppfd) {
521   TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
522
523   /* At the moment we only support the pixelformat corresponding to the main
524    * x11drv visual which got created at x11drv initialization. More formats
525    * can be supported if there was a way to recreate x11 windows in x11drv
526    */
527   if(iPixelFormat != 1) {
528     TRACE("Invalid iPixelFormat: %d\n", iPixelFormat);
529     return 0;
530   }
531
532   physDev->current_pf = iPixelFormat;
533   
534   if (TRACE_ON(opengl)) {
535     int nCfgs_fmt = 0;
536     GLXFBConfig* cfgs_fmt = NULL;
537     GLXFBConfig cur_cfg;
538     int value;
539     int gl_test = 0;
540     int fmt_index = 0;
541
542     if(!ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, &fmt_index, &value)) {
543       ERR("Can't find a valid pixel format index from the main visual, expect problems!\n");
544       return TRUE; /* Return true because the SetPixelFormat stuff itself passed */
545     }
546
547     /*
548      * How to test if hdc current drawable is compatible (visual/FBConfig) ?
549      *
550      * in case of root window created HDCs we crash here :(
551      *
552     Drawable drawable =  get_drawable( physDev->hdc );
553     TRACE(" drawable (%p,%p) have :\n", drawable, root_window);
554     pglXQueryDrawable(gdi_display, drawable, GLX_FBCONFIG_ID, (unsigned int*) &value);
555     TRACE(" - FBCONFIG_ID as 0x%x\n", tmp);
556     pglXQueryDrawable(gdi_display, drawable, GLX_VISUAL_ID, (unsigned int*) &value);
557     TRACE(" - VISUAL_ID as 0x%x\n", tmp);
558     pglXQueryDrawable(gdi_display, drawable, GLX_WIDTH, (unsigned int*) &value);
559     TRACE(" - WIDTH as %d\n", tmp);
560     pglXQueryDrawable(gdi_display, drawable, GLX_HEIGHT, (unsigned int*) &value);
561     TRACE(" - HEIGHT as %d\n", tmp);
562     */
563     cfgs_fmt = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs_fmt);
564     cur_cfg = cfgs_fmt[fmt_index];
565     gl_test = pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_FBCONFIG_ID, &value);
566     if (gl_test) {
567       ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
568     } else {
569       TRACE(" FBConfig have :\n");
570       TRACE(" - FBCONFIG_ID   0x%x\n", value);
571       pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_VISUAL_ID, &value);
572       TRACE(" - VISUAL_ID     0x%x\n", value);
573       pglXGetFBConfigAttrib(gdi_display, cur_cfg, GLX_DRAWABLE_TYPE, &value);
574       TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
575     }
576     XFree(cfgs_fmt);
577   }
578   return TRUE;
579 }
580
581 static XID create_glxpixmap(X11DRV_PDEVICE *physDev)
582 {
583     GLXPixmap ret;
584     XVisualInfo *vis;
585     XVisualInfo template;
586     int num;
587
588     wine_tsx11_lock();
589
590     /* Retrieve the visualid from our main visual which is the only visual we can use */
591     template.visualid =  XVisualIDFromVisual(visual);
592     vis = XGetVisualInfo(gdi_display, VisualIDMask, &template, &num);
593
594     ret = pglXCreateGLXPixmap(gdi_display, vis, physDev->bitmap->pixmap);
595     XFree(vis);
596     wine_tsx11_unlock(); 
597     TRACE("return %lx\n", ret);
598     return ret;
599 }
600
601 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
602 {
603     Drawable ret;
604
605     if(physDev->bitmap)
606     {
607         if (physDev->bitmap->hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
608             ret = physDev->drawable; /* PBuffer */
609         else
610         {
611             if(!physDev->bitmap->glxpixmap)
612                 physDev->bitmap->glxpixmap = create_glxpixmap(physDev);
613             ret = physDev->bitmap->glxpixmap;
614         }
615     }
616     else
617         ret = physDev->drawable;
618     return ret;
619 }
620
621 BOOL destroy_glxpixmap(XID glxpixmap)
622 {
623     wine_tsx11_lock(); 
624     pglXDestroyGLXPixmap(gdi_display, glxpixmap);
625     wine_tsx11_unlock(); 
626     return TRUE;
627 }
628
629 /**
630  * X11DRV_SwapBuffers
631  *
632  * Swap the buffers of this DC
633  */
634 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev)
635 {
636   GLXDrawable drawable;
637   if (!has_opengl()) {
638     ERR("No libGL on this box - disabling OpenGL support !\n");
639     return 0;
640   }
641   
642   TRACE_(opengl)("(%p)\n", physDev);
643
644   drawable = get_glxdrawable(physDev);
645   wine_tsx11_lock();
646   pglXSwapBuffers(gdi_display, drawable);
647   wine_tsx11_unlock();
648
649   /* FPS support */
650   if (TRACE_ON(fps))
651   {
652       static long prev_time, frames;
653
654       DWORD time = GetTickCount();
655       frames++;
656       /* every 1.5 seconds */
657       if (time - prev_time > 1500) {
658           TRACE_(fps)("@ approx %.2ffps\n", 1000.0*frames/(time - prev_time));
659           prev_time = time;
660           frames = 0;
661       }
662   }
663
664   return TRUE;
665 }
666
667 /***********************************************************************
668  *              X11DRV_setup_opengl_visual
669  *
670  * Setup the default visual used for OpenGL and Direct3D, and the desktop
671  * window (if it exists).  If OpenGL isn't available, the visual is simply
672  * set to the default visual for the display
673  */
674 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
675 {
676     XVisualInfo *visual = NULL;
677     /* In order to support OpenGL or D3D, we require a double-buffered visual and stencil buffer support, */
678     int dblBuf[] = {GLX_RGBA,GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8, GLX_ALPHA_SIZE, 8, GLX_DOUBLEBUFFER, None};
679     if (!has_opengl()) return NULL;
680
681     wine_tsx11_lock();
682     visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf);
683     wine_tsx11_unlock();
684     if (visual == NULL) {
685         /* fallback to 16 bits depth, no alpha */
686         int dblBuf2[] = {GLX_RGBA,GLX_DEPTH_SIZE, 16, GLX_STENCIL_SIZE, 8, GLX_DOUBLEBUFFER, None};
687         WARN("Failed to get a visual with at least 24 bits depth\n");
688
689         wine_tsx11_lock();
690         visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf2);
691         wine_tsx11_unlock();
692         if (visual == NULL) {
693             /* fallback to no stencil */
694             int dblBuf2[] = {GLX_RGBA,GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
695             WARN("Failed to get a visual with at least 8 bits of stencil\n");
696
697             wine_tsx11_lock();
698             visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf2);
699             wine_tsx11_unlock();
700             if (visual == NULL) {
701                 /* This should only happen if we cannot find a match with a depth size 16 */
702                 FIXME("Failed to find a suitable visual\n");
703                 return visual;
704             }
705         }
706     }
707     TRACE("Visual ID %lx Chosen\n",visual->visualid);
708     return visual;
709 }
710
711 #else  /* no OpenGL includes */
712
713 void X11DRV_OpenGL_Init(Display *display)
714 {
715 }
716
717 /***********************************************************************
718  *              ChoosePixelFormat (X11DRV.@)
719  */
720 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
721                              const PIXELFORMATDESCRIPTOR *ppfd) {
722   ERR("No OpenGL support compiled in.\n");
723
724   return 0;
725 }
726
727 /***********************************************************************
728  *              DescribePixelFormat (X11DRV.@)
729  */
730 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
731                                int iPixelFormat,
732                                UINT nBytes,
733                                PIXELFORMATDESCRIPTOR *ppfd) {
734   ERR("No OpenGL support compiled in.\n");
735
736   return 0;
737 }
738
739 /***********************************************************************
740  *              GetPixelFormat (X11DRV.@)
741  */
742 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
743   ERR("No OpenGL support compiled in.\n");
744
745   return 0;
746 }
747
748 /***********************************************************************
749  *              SetPixelFormat (X11DRV.@)
750  */
751 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
752                            int iPixelFormat,
753                            const PIXELFORMATDESCRIPTOR *ppfd) {
754   ERR("No OpenGL support compiled in.\n");
755
756   return FALSE;
757 }
758
759 /***********************************************************************
760  *              SwapBuffers (X11DRV.@)
761  */
762 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
763   ERR_(opengl)("No OpenGL support compiled in.\n");
764
765   return FALSE;
766 }
767
768 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
769 {
770   return NULL;
771 }
772
773 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
774 {
775     return 0;
776 }
777
778 BOOL destroy_glxpixmap(XID glxpixmap)
779 {
780     return FALSE;
781 }
782
783 #endif /* defined(HAVE_OPENGL) */