rsaenh/tests: Fix memory leaks.
[wine] / dlls / winex11.drv / opengl.c
1 /*
2  * X11DRV OpenGL functions
3  *
4  * Copyright 2000 Lionel Ulmer
5  * Copyright 2005 Alex Woods
6  * Copyright 2005 Raphael Junqueira
7  * Copyright 2006-2009 Roderick Colenbrander
8  * Copyright 2006 Tomas Carnecky
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
34 #endif
35 #ifdef HAVE_SYS_UN_H
36 #include <sys/un.h>
37 #endif
38
39 #include "x11drv.h"
40 #include "winternl.h"
41 #include "wine/library.h"
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
45 WINE_DECLARE_DEBUG_CHANNEL(winediag);
46
47 #ifdef SONAME_LIBGL
48
49 #undef APIENTRY
50 #undef CALLBACK
51 #undef WINAPI
52
53 #ifdef HAVE_GL_GL_H
54 # include <GL/gl.h>
55 #endif
56 #ifdef HAVE_GL_GLX_H
57 # include <GL/glx.h>
58 #endif
59
60 #include "wine/wgl.h"
61
62 #undef APIENTRY
63 #undef CALLBACK
64 #undef WINAPI
65
66 /* Redefines the constants */
67 #define CALLBACK    __stdcall
68 #define WINAPI      __stdcall
69 #define APIENTRY    WINAPI
70
71
72 WINE_DECLARE_DEBUG_CHANNEL(fps);
73
74 typedef struct wine_glextension {
75     const char *extName;
76     struct {
77         const char *funcName;
78         void *funcAddress;
79     } extEntryPoints[9];
80 } WineGLExtension;
81
82 struct WineGLInfo {
83     const char *glVersion;
84     char *glExtensions;
85
86     int glxVersion[2];
87
88     const char *glxServerVersion;
89     const char *glxServerVendor;
90     const char *glxServerExtensions;
91
92     const char *glxClientVersion;
93     const char *glxClientVendor;
94     const char *glxClientExtensions;
95
96     const char *glxExtensions;
97
98     BOOL glxDirect;
99     char wglExtensions[4096];
100 };
101
102 typedef struct wine_glpixelformat {
103     int         iPixelFormat;
104     GLXFBConfig fbconfig;
105     int         fmt_id;
106     int         render_type;
107     BOOL        offscreenOnly;
108     DWORD       dwFlags; /* We store some PFD_* flags in here for emulated bitmap formats */
109 } WineGLPixelFormat;
110
111 typedef struct wine_glcontext {
112     HDC hdc;
113     BOOL do_escape;
114     BOOL has_been_current;
115     BOOL sharing;
116     BOOL gl3_context;
117     XVisualInfo *vis;
118     WineGLPixelFormat *fmt;
119     int numAttribs; /* This is needed for delaying wglCreateContextAttribsARB */
120     int attribList[16]; /* This is needed for delaying wglCreateContextAttribsARB */
121     GLXContext ctx;
122     HDC read_hdc;
123     Drawable drawables[2];
124     BOOL refresh_drawables;
125     struct wine_glcontext *next;
126     struct wine_glcontext *prev;
127 } Wine_GLContext;
128
129 typedef struct wine_glpbuffer {
130     Drawable   drawable;
131     Display*   display;
132     WineGLPixelFormat* fmt;
133     int        width;
134     int        height;
135     int*       attribList;
136     HDC        hdc;
137
138     int        use_render_texture; /* This is also the internal texture format */
139     int        texture_bind_target;
140     int        texture_bpp;
141     GLint      texture_format;
142     GLuint     texture_target;
143     GLenum     texture_type;
144     GLuint     texture;
145     int        texture_level;
146 } Wine_GLPBuffer;
147
148 static Wine_GLContext *context_list;
149 static struct WineGLInfo WineGLInfo = { 0 };
150 static int use_render_texture_emulation = 1;
151 static int use_render_texture_ati = 0;
152 static int swap_interval = 1;
153
154 #define MAX_EXTENSIONS 16
155 static const WineGLExtension *WineGLExtensionList[MAX_EXTENSIONS];
156 static int WineGLExtensionListSize;
157
158 static void X11DRV_WineGL_LoadExtensions(void);
159 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count);
160 static BOOL glxRequireVersion(int requiredVersion);
161 static BOOL glxRequireExtension(const char *requiredExtension);
162
163 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
164   TRACE("  - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
165   TRACE("  - dwFlags : ");
166 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) TRACE(#tv " ")
167   TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
168   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
169   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
170   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
171   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
172   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
173   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
174   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
175   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
176   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
177   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
178   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
179   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
180   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
181   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
182   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
183   /* PFD_SUPPORT_COMPOSITION is new in Vista, it is similar to composition
184    * under X e.g. COMPOSITE + GLX_EXT_TEXTURE_FROM_PIXMAP. */
185   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_COMPOSITION);
186 #undef TEST_AND_DUMP
187   TRACE("\n");
188
189   TRACE("  - iPixelType : ");
190   switch (ppfd->iPixelType) {
191   case PFD_TYPE_RGBA: TRACE("PFD_TYPE_RGBA"); break;
192   case PFD_TYPE_COLORINDEX: TRACE("PFD_TYPE_COLORINDEX"); break;
193   }
194   TRACE("\n");
195
196   TRACE("  - Color   : %d\n", ppfd->cColorBits);
197   TRACE("  - Red     : %d\n", ppfd->cRedBits);
198   TRACE("  - Green   : %d\n", ppfd->cGreenBits);
199   TRACE("  - Blue    : %d\n", ppfd->cBlueBits);
200   TRACE("  - Alpha   : %d\n", ppfd->cAlphaBits);
201   TRACE("  - Accum   : %d\n", ppfd->cAccumBits);
202   TRACE("  - Depth   : %d\n", ppfd->cDepthBits);
203   TRACE("  - Stencil : %d\n", ppfd->cStencilBits);
204   TRACE("  - Aux     : %d\n", ppfd->cAuxBuffers);
205
206   TRACE("  - iLayerType : ");
207   switch (ppfd->iLayerType) {
208   case PFD_MAIN_PLANE: TRACE("PFD_MAIN_PLANE"); break;
209   case PFD_OVERLAY_PLANE: TRACE("PFD_OVERLAY_PLANE"); break;
210   case (BYTE)PFD_UNDERLAY_PLANE: TRACE("PFD_UNDERLAY_PLANE"); break;
211   }
212   TRACE("\n");
213 }
214
215 #define PUSH1(attribs,att)        do { attribs[nAttribs++] = (att); } while (0)
216 #define PUSH2(attribs,att,value)  do { attribs[nAttribs++] = (att); attribs[nAttribs++] = (value); } while(0)
217
218 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
219 /* GLX 1.0 */
220 MAKE_FUNCPTR(glXChooseVisual)
221 MAKE_FUNCPTR(glXCopyContext)
222 MAKE_FUNCPTR(glXCreateContext)
223 MAKE_FUNCPTR(glXCreateGLXPixmap)
224 MAKE_FUNCPTR(glXGetCurrentContext)
225 MAKE_FUNCPTR(glXGetCurrentDrawable)
226 MAKE_FUNCPTR(glXDestroyContext)
227 MAKE_FUNCPTR(glXDestroyGLXPixmap)
228 MAKE_FUNCPTR(glXGetConfig)
229 MAKE_FUNCPTR(glXIsDirect)
230 MAKE_FUNCPTR(glXMakeCurrent)
231 MAKE_FUNCPTR(glXSwapBuffers)
232 MAKE_FUNCPTR(glXQueryExtension)
233 MAKE_FUNCPTR(glXQueryVersion)
234 MAKE_FUNCPTR(glXUseXFont)
235
236 /* GLX 1.1 */
237 MAKE_FUNCPTR(glXGetClientString)
238 MAKE_FUNCPTR(glXQueryExtensionsString)
239 MAKE_FUNCPTR(glXQueryServerString)
240
241 /* GLX 1.3 */
242 MAKE_FUNCPTR(glXGetFBConfigs)
243 MAKE_FUNCPTR(glXChooseFBConfig)
244 MAKE_FUNCPTR(glXCreatePbuffer)
245 MAKE_FUNCPTR(glXCreateNewContext)
246 MAKE_FUNCPTR(glXDestroyPbuffer)
247 MAKE_FUNCPTR(glXGetFBConfigAttrib)
248 MAKE_FUNCPTR(glXGetVisualFromFBConfig)
249 MAKE_FUNCPTR(glXMakeContextCurrent)
250 MAKE_FUNCPTR(glXQueryDrawable)
251 MAKE_FUNCPTR(glXGetCurrentReadDrawable)
252
253 /* GLX Extensions */
254 static GLXContext (*pglXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
255 static void* (*pglXGetProcAddressARB)(const GLubyte *);
256 static int   (*pglXSwapIntervalSGI)(int);
257
258 /* ATI GLX Extensions */
259 static BOOL  (*pglXBindTexImageATI)(Display *dpy, GLXPbuffer pbuffer, int buffer);
260 static BOOL  (*pglXReleaseTexImageATI)(Display *dpy, GLXPbuffer pbuffer, int buffer);
261 static BOOL  (*pglXDrawableAttribATI)(Display *dpy, GLXDrawable draw, const int *attribList);
262
263 /* NV GLX Extension */
264 static void* (*pglXAllocateMemoryNV)(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority);
265 static void  (*pglXFreeMemoryNV)(GLvoid *pointer);
266
267 /* MESA GLX Extensions */
268 static void (*pglXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height);
269
270 /* Standard OpenGL */
271 MAKE_FUNCPTR(glBindTexture)
272 MAKE_FUNCPTR(glBitmap)
273 MAKE_FUNCPTR(glCopyTexSubImage1D)
274 MAKE_FUNCPTR(glCopyTexImage2D)
275 MAKE_FUNCPTR(glCopyTexSubImage2D)
276 MAKE_FUNCPTR(glDrawBuffer)
277 MAKE_FUNCPTR(glEndList)
278 MAKE_FUNCPTR(glGetError)
279 MAKE_FUNCPTR(glGetIntegerv)
280 MAKE_FUNCPTR(glGetString)
281 MAKE_FUNCPTR(glNewList)
282 MAKE_FUNCPTR(glPixelStorei)
283 MAKE_FUNCPTR(glReadPixels)
284 MAKE_FUNCPTR(glTexImage2D)
285 MAKE_FUNCPTR(glFinish)
286 MAKE_FUNCPTR(glFlush)
287 #undef MAKE_FUNCPTR
288
289 static int GLXErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
290 {
291     /* In the future we might want to find the exact X or GLX error to report back to the app */
292     return 1;
293 }
294
295 static BOOL infoInitialized = FALSE;
296 static BOOL X11DRV_WineGL_InitOpenglInfo(void)
297 {
298     int screen = DefaultScreen(gdi_display);
299     Window win = RootWindow(gdi_display, screen);
300     const char* str;
301     XVisualInfo *vis;
302     GLXContext ctx = NULL;
303     int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};
304
305     if (infoInitialized)
306         return TRUE;
307     infoInitialized = TRUE;
308
309     wine_tsx11_lock();
310
311     vis = pglXChooseVisual(gdi_display, screen, attribList);
312     if (vis) {
313         WORD old_fs = wine_get_fs();
314         /* Create a GLX Context. Without one we can't query GL information */
315         ctx = pglXCreateContext(gdi_display, vis, None, GL_TRUE);
316         if (wine_get_fs() != old_fs)
317         {
318             wine_set_fs( old_fs );
319             wine_tsx11_unlock();
320             ERR( "%%fs register corrupted, probably broken ATI driver, disabling OpenGL.\n" );
321             ERR( "You need to set the \"UseFastTls\" option to \"2\" in your X config file.\n" );
322             return FALSE;
323         }
324     }
325
326     if (ctx) {
327         pglXMakeCurrent(gdi_display, win, ctx);
328     } else {
329         ERR(" couldn't initialize OpenGL, expect problems\n");
330         wine_tsx11_unlock();
331         return FALSE;
332     }
333
334     WineGLInfo.glVersion = (const char *) pglGetString(GL_VERSION);
335     str = (const char *) pglGetString(GL_EXTENSIONS);
336     WineGLInfo.glExtensions = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
337     strcpy(WineGLInfo.glExtensions, str);
338
339     /* Get the common GLX version supported by GLX client and server ( major/minor) */
340     pglXQueryVersion(gdi_display, &WineGLInfo.glxVersion[0], &WineGLInfo.glxVersion[1]);
341
342     WineGLInfo.glxServerVersion = pglXQueryServerString(gdi_display, screen, GLX_VERSION);
343     WineGLInfo.glxServerVendor = pglXQueryServerString(gdi_display, screen, GLX_VENDOR);
344     WineGLInfo.glxServerExtensions = pglXQueryServerString(gdi_display, screen, GLX_EXTENSIONS);
345
346     WineGLInfo.glxClientVersion = pglXGetClientString(gdi_display, GLX_VERSION);
347     WineGLInfo.glxClientVendor = pglXGetClientString(gdi_display, GLX_VENDOR);
348     WineGLInfo.glxClientExtensions = pglXGetClientString(gdi_display, GLX_EXTENSIONS);
349
350     WineGLInfo.glxExtensions = pglXQueryExtensionsString(gdi_display, screen);
351     WineGLInfo.glxDirect = pglXIsDirect(gdi_display, ctx);
352
353     TRACE("GL version             : %s.\n", WineGLInfo.glVersion);
354     TRACE("GL renderer            : %s.\n", pglGetString(GL_RENDERER));
355     TRACE("GLX version            : %d.%d.\n", WineGLInfo.glxVersion[0], WineGLInfo.glxVersion[1]);
356     TRACE("Server GLX version     : %s.\n", WineGLInfo.glxServerVersion);
357     TRACE("Server GLX vendor:     : %s.\n", WineGLInfo.glxServerVendor);
358     TRACE("Client GLX version     : %s.\n", WineGLInfo.glxClientVersion);
359     TRACE("Client GLX vendor:     : %s.\n", WineGLInfo.glxClientVendor);
360     TRACE("Direct rendering enabled: %s\n", WineGLInfo.glxDirect ? "True" : "False");
361
362     if(!WineGLInfo.glxDirect)
363     {
364         int fd = ConnectionNumber(gdi_display);
365         struct sockaddr_un uaddr;
366         unsigned int uaddrlen = sizeof(struct sockaddr_un);
367
368         /* In general indirect rendering on a local X11 server indicates a driver problem.
369          * Detect a local X11 server by checking whether the X11 socket is a Unix socket.
370          */
371         if(!getsockname(fd, &uaddr, &uaddrlen) && uaddr.sun_family == AF_UNIX)
372             ERR_(winediag)("Direct rendering is disabled, most likely your OpenGL drivers haven't been installed correctly\n");
373     }
374
375     if(vis) XFree(vis);
376     if(ctx) {
377         pglXMakeCurrent(gdi_display, None, NULL);    
378         pglXDestroyContext(gdi_display, ctx);
379     }
380     wine_tsx11_unlock();
381     return TRUE;
382 }
383
384 void X11DRV_OpenGL_Cleanup(void)
385 {
386     HeapFree(GetProcessHeap(), 0, WineGLInfo.glExtensions);
387     infoInitialized = FALSE;
388 }
389
390 static BOOL has_opengl(void)
391 {
392     static int init_done;
393     static void *opengl_handle;
394
395     char buffer[200];
396     int error_base, event_base;
397
398     if (init_done) return (opengl_handle != NULL);
399     init_done = 1;
400
401     /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient
402        and include all dependencies */
403     opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, buffer, sizeof(buffer));
404     if (opengl_handle == NULL)
405     {
406         ERR( "Failed to load libGL: %s\n", buffer );
407         ERR( "OpenGL support is disabled.\n");
408         return FALSE;
409     }
410
411     pglXGetProcAddressARB = wine_dlsym(opengl_handle, "glXGetProcAddressARB", NULL, 0);
412     if (pglXGetProcAddressARB == NULL) {
413         ERR("Could not find glXGetProcAddressARB in libGL, disabling OpenGL.\n");
414         goto failed;
415     }
416
417 #define LOAD_FUNCPTR(f) do if((p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)) == NULL) \
418     { \
419         ERR( "%s not found in libGL, disabling OpenGL.\n", #f ); \
420         goto failed; \
421     } while(0)
422
423     /* GLX 1.0 */
424     LOAD_FUNCPTR(glXChooseVisual);
425     LOAD_FUNCPTR(glXCopyContext);
426     LOAD_FUNCPTR(glXCreateContext);
427     LOAD_FUNCPTR(glXCreateGLXPixmap);
428     LOAD_FUNCPTR(glXGetCurrentContext);
429     LOAD_FUNCPTR(glXGetCurrentDrawable);
430     LOAD_FUNCPTR(glXDestroyContext);
431     LOAD_FUNCPTR(glXDestroyGLXPixmap);
432     LOAD_FUNCPTR(glXGetConfig);
433     LOAD_FUNCPTR(glXIsDirect);
434     LOAD_FUNCPTR(glXMakeCurrent);
435     LOAD_FUNCPTR(glXSwapBuffers);
436     LOAD_FUNCPTR(glXQueryExtension);
437     LOAD_FUNCPTR(glXQueryVersion);
438     LOAD_FUNCPTR(glXUseXFont);
439
440     /* GLX 1.1 */
441     LOAD_FUNCPTR(glXGetClientString);
442     LOAD_FUNCPTR(glXQueryExtensionsString);
443     LOAD_FUNCPTR(glXQueryServerString);
444
445     /* GLX 1.3 */
446     LOAD_FUNCPTR(glXCreatePbuffer);
447     LOAD_FUNCPTR(glXCreateNewContext);
448     LOAD_FUNCPTR(glXDestroyPbuffer);
449     LOAD_FUNCPTR(glXMakeContextCurrent);
450     LOAD_FUNCPTR(glXGetCurrentReadDrawable);
451     LOAD_FUNCPTR(glXGetFBConfigs);
452
453     /* Standard OpenGL calls */
454     LOAD_FUNCPTR(glBindTexture);
455     LOAD_FUNCPTR(glBitmap);
456     LOAD_FUNCPTR(glCopyTexSubImage1D);
457     LOAD_FUNCPTR(glCopyTexImage2D);
458     LOAD_FUNCPTR(glCopyTexSubImage2D);
459     LOAD_FUNCPTR(glDrawBuffer);
460     LOAD_FUNCPTR(glEndList);
461     LOAD_FUNCPTR(glGetError);
462     LOAD_FUNCPTR(glGetIntegerv);
463     LOAD_FUNCPTR(glGetString);
464     LOAD_FUNCPTR(glNewList);
465     LOAD_FUNCPTR(glPixelStorei);
466     LOAD_FUNCPTR(glReadPixels);
467     LOAD_FUNCPTR(glTexImage2D);
468     LOAD_FUNCPTR(glFinish);
469     LOAD_FUNCPTR(glFlush);
470 #undef LOAD_FUNCPTR
471
472 /* It doesn't matter if these fail. They'll only be used if the driver reports
473    the associated extension is available (and if a driver reports the extension
474    is available but fails to provide the functions, it's quite broken) */
475 #define LOAD_FUNCPTR(f) p##f = (void*)pglXGetProcAddressARB((const unsigned char*)#f)
476     /* ARB GLX Extension */
477     LOAD_FUNCPTR(glXCreateContextAttribsARB);
478     /* NV GLX Extension */
479     LOAD_FUNCPTR(glXAllocateMemoryNV);
480     LOAD_FUNCPTR(glXFreeMemoryNV);
481 #undef LOAD_FUNCPTR
482
483     if(!X11DRV_WineGL_InitOpenglInfo()) goto failed;
484
485     wine_tsx11_lock();
486     if (pglXQueryExtension(gdi_display, &error_base, &event_base)) {
487         TRACE("GLX is up and running error_base = %d\n", error_base);
488     } else {
489         wine_tsx11_unlock();
490         ERR( "GLX extension is missing, disabling OpenGL.\n" );
491         goto failed;
492     }
493
494     /* In case of GLX you have direct and indirect rendering. Most of the time direct rendering is used
495      * as in general only that is hardware accelerated. In some cases like in case of remote X indirect
496      * rendering is used.
497      *
498      * The main problem for our OpenGL code is that we need certain GLX calls but their presence
499      * depends on the reported GLX client / server version and on the client / server extension list.
500      * Those don't have to be the same.
501      *
502      * In general the server GLX information lists the capabilities in case of indirect rendering.
503      * When direct rendering is used, the OpenGL client library is responsible for which GLX calls are
504      * available and in that case the client GLX informat can be used.
505      * OpenGL programs should use the 'intersection' of both sets of information which is advertised
506      * in the GLX version/extension list. When a program does this it works for certain for both
507      * direct and indirect rendering.
508      *
509      * The problem we are having in this area is that ATI's Linux drivers are broken. For some reason
510      * they haven't added some very important GLX extensions like GLX_SGIX_fbconfig to their client
511      * extension list which causes this extension not to be listed. (Wine requires this extension).
512      * ATI advertises a GLX client version of 1.3 which implies that this fbconfig extension among
513      * pbuffers is around.
514      *
515      * In order to provide users of Ati's proprietary drivers with OpenGL support, we need to detect
516      * the ATI drivers and from then on use GLX client information for them.
517      */
518
519     if(glxRequireVersion(3)) {
520         pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
521         pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
522         pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
523         pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
524     } else if(glxRequireExtension("GLX_SGIX_fbconfig")) {
525         pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfigSGIX");
526         pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttribSGIX");
527         pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfigSGIX");
528
529         /* The mesa libGL client library seems to forward glXQueryDrawable to the Xserver, so only
530          * enable this function when the Xserver understand GLX 1.3 or newer
531          */
532         pglXQueryDrawable = NULL;
533      } else if(strcmp("ATI", WineGLInfo.glxClientVendor) == 0) {
534         TRACE("Overriding ATI GLX capabilities!\n");
535         pglXChooseFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXChooseFBConfig");
536         pglXGetFBConfigAttrib = pglXGetProcAddressARB((const GLubyte *) "glXGetFBConfigAttrib");
537         pglXGetVisualFromFBConfig = pglXGetProcAddressARB((const GLubyte *) "glXGetVisualFromFBConfig");
538         pglXQueryDrawable = pglXGetProcAddressARB((const GLubyte *) "glXQueryDrawable");
539
540         /* Use client GLX information in case of the ATI drivers. We override the
541          * capabilities over here and not somewhere else as ATI might better their
542          * life in the future. In case they release proper drivers this block of
543          * code won't be called. */
544         WineGLInfo.glxExtensions = WineGLInfo.glxClientExtensions;
545     } else {
546          ERR(" glx_version is %s and GLX_SGIX_fbconfig extension is unsupported. Expect problems.\n", WineGLInfo.glxServerVersion);
547     }
548
549     if(glxRequireExtension("GLX_ATI_render_texture")) {
550         use_render_texture_ati = 1;
551         pglXBindTexImageATI = pglXGetProcAddressARB((const GLubyte *) "glXBindTexImageATI");
552         pglXReleaseTexImageATI = pglXGetProcAddressARB((const GLubyte *) "glXReleaseTexImageATI");
553         pglXDrawableAttribATI = pglXGetProcAddressARB((const GLubyte *) "glXDrawableAttribATI");
554     }
555
556     if(glxRequireExtension("GLX_MESA_copy_sub_buffer")) {
557         pglXCopySubBufferMESA = pglXGetProcAddressARB((const GLubyte *) "glXCopySubBufferMESA");
558     }
559
560     X11DRV_WineGL_LoadExtensions();
561
562     wine_tsx11_unlock();
563     return TRUE;
564
565 failed:
566     wine_dlclose(opengl_handle, NULL, 0);
567     opengl_handle = NULL;
568     return FALSE;
569 }
570
571 static inline Wine_GLContext *alloc_context(void)
572 {
573     Wine_GLContext *ret;
574
575     if ((ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLContext))))
576     {
577         ret->next = context_list;
578         if (context_list) context_list->prev = ret;
579         context_list = ret;
580     }
581     return ret;
582 }
583
584 static inline void free_context(Wine_GLContext *context)
585 {
586     if (context->next != NULL) context->next->prev = context->prev;
587     if (context->prev != NULL) context->prev->next = context->next;
588     else context_list = context->next;
589
590     if (context->vis) XFree(context->vis);
591     HeapFree(GetProcessHeap(), 0, context);
592 }
593
594 static inline BOOL is_valid_context( Wine_GLContext *ctx )
595 {
596     Wine_GLContext *ptr;
597     for (ptr = context_list; ptr; ptr = ptr->next) if (ptr == ctx) break;
598     return (ptr != NULL);
599 }
600
601 static int describeContext(Wine_GLContext* ctx) {
602     int tmp;
603     int ctx_vis_id;
604     TRACE(" Context %p have (vis:%p):\n", ctx, ctx->vis);
605     pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_FBCONFIG_ID, &tmp);
606     TRACE(" - FBCONFIG_ID 0x%x\n", tmp);
607     pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_VISUAL_ID, &tmp);
608     TRACE(" - VISUAL_ID 0x%x\n", tmp);
609     ctx_vis_id = tmp;
610     return ctx_vis_id;
611 }
612
613 static BOOL describeDrawable(X11DRV_PDEVICE *physDev) {
614     int tmp;
615     WineGLPixelFormat *fmt;
616     int fmt_count = 0;
617
618     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physDev->current_pf, TRUE /* Offscreen */, &fmt_count);
619     if(!fmt) return FALSE;
620
621     TRACE(" HDC %p has:\n", physDev->hdc);
622     TRACE(" - iPixelFormat %d\n", fmt->iPixelFormat);
623     TRACE(" - Drawable %p\n", (void*) get_glxdrawable(physDev));
624     TRACE(" - FBCONFIG_ID 0x%x\n", fmt->fmt_id);
625
626     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &tmp);
627     TRACE(" - VISUAL_ID 0x%x\n", tmp);
628
629     return TRUE;
630 }
631
632 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, Wine_GLPBuffer* pbuf) {
633   int nAttribs = 0;
634   unsigned cur = 0; 
635   int pop;
636   int drawattrib = 0;
637   int nvfloatattrib = GLX_DONT_CARE;
638   int pixelattrib = ~0;
639
640   /* The list of WGL attributes is allowed to be NULL. We don't return here for NULL
641    * because we need to do fixups for GLX_DRAWABLE_TYPE/GLX_RENDER_TYPE/GLX_FLOAT_COMPONENTS_NV. */
642   while (iWGLAttr && 0 != iWGLAttr[cur]) {
643     TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
644
645     switch (iWGLAttr[cur]) {
646     case WGL_AUX_BUFFERS_ARB:
647       pop = iWGLAttr[++cur];
648       PUSH2(oGLXAttr, GLX_AUX_BUFFERS, pop);
649       TRACE("pAttr[%d] = GLX_AUX_BUFFERS: %d\n", cur, pop);
650       break;
651     case WGL_COLOR_BITS_ARB:
652       pop = iWGLAttr[++cur];
653       PUSH2(oGLXAttr, GLX_BUFFER_SIZE, pop);
654       TRACE("pAttr[%d] = GLX_BUFFER_SIZE: %d\n", cur, pop);
655       break;
656     case WGL_BLUE_BITS_ARB:
657       pop = iWGLAttr[++cur];
658       PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
659       TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
660       break;
661     case WGL_RED_BITS_ARB:
662       pop = iWGLAttr[++cur];
663       PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
664       TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
665       break;
666     case WGL_GREEN_BITS_ARB:
667       pop = iWGLAttr[++cur];
668       PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
669       TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
670       break;
671     case WGL_ALPHA_BITS_ARB:
672       pop = iWGLAttr[++cur];
673       PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
674       TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
675       break;
676     case WGL_DEPTH_BITS_ARB:
677       pop = iWGLAttr[++cur];
678       PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
679       TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
680       break;
681     case WGL_STENCIL_BITS_ARB:
682       pop = iWGLAttr[++cur];
683       PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
684       TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
685       break;
686     case WGL_DOUBLE_BUFFER_ARB:
687       pop = iWGLAttr[++cur];
688       PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
689       TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
690       break;
691     case WGL_STEREO_ARB:
692       pop = iWGLAttr[++cur];
693       PUSH2(oGLXAttr, GLX_STEREO, pop);
694       TRACE("pAttr[%d] = GLX_STEREO: %d\n", cur, pop);
695       break;
696
697     case WGL_PIXEL_TYPE_ARB:
698       pop = iWGLAttr[++cur];
699       TRACE("pAttr[%d] = WGL_PIXEL_TYPE_ARB: %d\n", cur, pop);
700       switch (pop) {
701       case WGL_TYPE_COLORINDEX_ARB: pixelattrib = GLX_COLOR_INDEX_BIT; break ;
702       case WGL_TYPE_RGBA_ARB: pixelattrib = GLX_RGBA_BIT; break ;
703       /* This is the same as WGL_TYPE_RGBA_FLOAT_ATI but the GLX constants differ, only the ARB GLX one is widely supported so use that */
704       case WGL_TYPE_RGBA_FLOAT_ATI: pixelattrib = GLX_RGBA_FLOAT_BIT; break ;
705       case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT: pixelattrib = GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT; break ;
706       default:
707         ERR("unexpected PixelType(%x)\n", pop); 
708         pop = 0;
709       }
710       break;
711
712     case WGL_SUPPORT_GDI_ARB:
713       /* This flag is set in a WineGLPixelFormat */
714       pop = iWGLAttr[++cur];
715       TRACE("pAttr[%d] = WGL_SUPPORT_GDI_ARB: %d\n", cur, pop);
716       break;
717
718     case WGL_DRAW_TO_BITMAP_ARB:
719       /* This flag is set in a WineGLPixelFormat */
720       pop = iWGLAttr[++cur];
721       TRACE("pAttr[%d] = WGL_DRAW_TO_BITMAP_ARB: %d\n", cur, pop);
722       break;
723
724     case WGL_DRAW_TO_WINDOW_ARB:
725       pop = iWGLAttr[++cur];
726       TRACE("pAttr[%d] = WGL_DRAW_TO_WINDOW_ARB: %d\n", cur, pop);
727       /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
728       if (pop) {
729         drawattrib |= GLX_WINDOW_BIT;
730       }
731       break;
732
733     case WGL_DRAW_TO_PBUFFER_ARB:
734       pop = iWGLAttr[++cur];
735       TRACE("pAttr[%d] = WGL_DRAW_TO_PBUFFER_ARB: %d\n", cur, pop);
736       /* GLX_DRAWABLE_TYPE flags need to be OR'd together. See below. */
737       if (pop) {
738         drawattrib |= GLX_PBUFFER_BIT;
739       }
740       break;
741
742     case WGL_ACCELERATION_ARB:
743       /* This flag is set in a WineGLPixelFormat */
744       pop = iWGLAttr[++cur];
745       TRACE("pAttr[%d] = WGL_ACCELERATION_ARB: %d\n", cur, pop);
746       break;
747
748     case WGL_SUPPORT_OPENGL_ARB:
749       pop = iWGLAttr[++cur];
750       /** nothing to do, if we are here, supposing support Accelerated OpenGL */
751       TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
752       break;
753
754     case WGL_SWAP_METHOD_ARB:
755       pop = iWGLAttr[++cur];
756       /* For now we ignore this and just return SWAP_EXCHANGE */
757       TRACE("pAttr[%d] = WGL_SWAP_METHOD_ARB: %#x\n", cur, pop);
758       break;
759
760     case WGL_PBUFFER_LARGEST_ARB:
761       pop = iWGLAttr[++cur];
762       PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
763       TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
764       break;
765
766     case WGL_SAMPLE_BUFFERS_ARB:
767       pop = iWGLAttr[++cur];
768       PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
769       TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
770       break;
771
772     case WGL_SAMPLES_ARB:
773       pop = iWGLAttr[++cur];
774       PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
775       TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
776       break;
777
778     case WGL_TEXTURE_FORMAT_ARB:
779     case WGL_TEXTURE_TARGET_ARB:
780     case WGL_MIPMAP_TEXTURE_ARB:
781       TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
782       pop = iWGLAttr[++cur];
783       if (NULL == pbuf) {
784         ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
785       }
786       if (use_render_texture_ati) {
787         /** nothing to do here */
788       }
789       else if (!use_render_texture_emulation) {
790         if (WGL_NO_TEXTURE_ARB != pop) {
791           ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
792           return -1; /** error: don't support it */
793         } else {
794           drawattrib |= GLX_PBUFFER_BIT;
795         }
796       }
797       break ;
798     case WGL_FLOAT_COMPONENTS_NV:
799       nvfloatattrib = iWGLAttr[++cur];
800       TRACE("pAttr[%d] = WGL_FLOAT_COMPONENTS_NV: %x\n", cur, nvfloatattrib);
801       break ;
802     case WGL_BIND_TO_TEXTURE_DEPTH_NV:
803     case WGL_BIND_TO_TEXTURE_RGB_ARB:
804     case WGL_BIND_TO_TEXTURE_RGBA_ARB:
805     case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV:
806     case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV:
807     case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV:
808     case WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV:
809       pop = iWGLAttr[++cur];
810       /** cannot be converted, see direct handling on 
811        *   - wglGetPixelFormatAttribivARB
812        *  TODO: wglChoosePixelFormat
813        */
814       break ;
815     case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
816       pop = iWGLAttr[++cur];
817       PUSH2(oGLXAttr, GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, pop);
818       TRACE("pAttr[%d] = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT: %x\n", cur, pop);
819       break ;
820
821     case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
822       pop = iWGLAttr[++cur];
823       PUSH2(oGLXAttr, GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT, pop);
824       TRACE("pAttr[%d] = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT: %x\n", cur, pop);
825       break ;
826     default:
827       FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
828       break;
829     }
830     ++cur;
831   }
832
833   /* By default glXChooseFBConfig defaults to GLX_WINDOW_BIT. wglChoosePixelFormatARB searches through
834    * all formats. Unless drawattrib is set to a non-zero value override it with ~0, so that pixmap and pbuffer
835    * formats appear as well. */
836   if(!drawattrib) drawattrib = ~0;
837   PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, drawattrib);
838   TRACE("pAttr[?] = GLX_DRAWABLE_TYPE: %#x\n", drawattrib);
839
840   /* By default glXChooseFBConfig uses GLX_RGBA_BIT as the default value. Since wglChoosePixelFormatARB
841    * searches in all formats we have to do the same. For this reason we set GLX_RENDER_TYPE to ~0 unless
842    * it is overridden. */
843   PUSH2(oGLXAttr, GLX_RENDER_TYPE, pixelattrib);
844   TRACE("pAttr[?] = GLX_RENDER_TYPE: %#x\n", pixelattrib);
845
846   /* Set GLX_FLOAT_COMPONENTS_NV all the time */
847   if(strstr(WineGLInfo.glxExtensions, "GLX_NV_float_buffer")) {
848     PUSH2(oGLXAttr, GLX_FLOAT_COMPONENTS_NV, nvfloatattrib);
849     TRACE("pAttr[?] = GLX_FLOAT_COMPONENTS_NV: %#x\n", nvfloatattrib);
850   }
851
852   return nAttribs;
853 }
854
855 static int get_render_type_from_fbconfig(Display *display, GLXFBConfig fbconfig)
856 {
857     int render_type=0, render_type_bit;
858     pglXGetFBConfigAttrib(display, fbconfig, GLX_RENDER_TYPE, &render_type_bit);
859     switch(render_type_bit)
860     {
861         case GLX_RGBA_BIT:
862             render_type = GLX_RGBA_TYPE;
863             break;
864         case GLX_COLOR_INDEX_BIT:
865             render_type = GLX_COLOR_INDEX_TYPE;
866             break;
867         case GLX_RGBA_FLOAT_BIT:
868             render_type = GLX_RGBA_FLOAT_TYPE;
869             break;
870         case GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT:
871             render_type = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
872             break;
873         default:
874             ERR("Unknown render_type: %x\n", render_type);
875     }
876     return render_type;
877 }
878
879 /* Check whether a fbconfig is suitable for Windows-style bitmap rendering */
880 static BOOL check_fbconfig_bitmap_capability(Display *display, GLXFBConfig fbconfig)
881 {
882     int dbuf, value;
883     pglXGetFBConfigAttrib(display, fbconfig, GLX_DOUBLEBUFFER, &dbuf);
884     pglXGetFBConfigAttrib(gdi_display, fbconfig, GLX_DRAWABLE_TYPE, &value);
885
886     /* Windows only supports bitmap rendering on single buffered formats, further the fbconfig needs to have
887      * the GLX_PIXMAP_BIT set. */
888     return !dbuf && (value & GLX_PIXMAP_BIT);
889 }
890
891 static WineGLPixelFormat *get_formats(Display *display, int *size_ret, int *onscreen_size_ret)
892 {
893     static WineGLPixelFormat *list;
894     static int size, onscreen_size;
895
896     int fmt_id, nCfgs, i, run, bmp_formats;
897     GLXFBConfig* cfgs;
898     XVisualInfo *visinfo;
899
900     wine_tsx11_lock();
901     if (list) goto done;
902
903     cfgs = pglXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
904     if (NULL == cfgs || 0 == nCfgs) {
905         if(cfgs != NULL) XFree(cfgs);
906         wine_tsx11_unlock();
907         ERR("glXChooseFBConfig returns NULL\n");
908         return NULL;
909     }
910
911     /* Bitmap rendering on Windows implies the use of the Microsoft GDI software renderer.
912      * Further most GLX drivers only offer pixmap rendering using indirect rendering (except for modern drivers which support 'AIGLX' / composite).
913      * Indirect rendering can indicate software rendering (on Nvidia it is hw accelerated)
914      * Since bitmap rendering implies the use of software rendering we can safely use indirect rendering for bitmaps.
915      *
916      * Below we count the number of formats which are suitable for bitmap rendering. Windows restricts bitmap rendering to single buffered formats.
917      */
918     for(i=0, bmp_formats=0; i<nCfgs; i++)
919     {
920         if(check_fbconfig_bitmap_capability(display, cfgs[i]))
921             bmp_formats++;
922     }
923     TRACE("Found %d bitmap capable fbconfigs\n", bmp_formats);
924
925     list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nCfgs + bmp_formats)*sizeof(WineGLPixelFormat));
926
927     /* Fill the pixel format list. Put onscreen formats at the top and offscreen ones at the bottom.
928      * Do this as GLX doesn't guarantee that the list is sorted */
929     for(run=0; run < 2; run++)
930     {
931         for(i=0; i<nCfgs; i++) {
932             pglXGetFBConfigAttrib(display, cfgs[i], GLX_FBCONFIG_ID, &fmt_id);
933             visinfo = pglXGetVisualFromFBConfig(display, cfgs[i]);
934
935             /* The first run we only add onscreen formats (ones which have an associated X Visual).
936              * The second run we only set offscreen formats. */
937             if(!run && visinfo)
938             {
939                 /* We implement child window rendering using offscreen buffers (using composite or an XPixmap).
940                  * The contents is copied to the destination using XCopyArea. For the copying to work
941                  * the depth of the source and destination window should be the same. In general this should
942                  * not be a problem for OpenGL as drivers only advertise formats with a similar depth (or no depth).
943                  * As of the introduction of composition managers at least Nvidia now also offers ARGB visuals
944                  * with a depth of 32 in addition to the default 24 bit. In order to prevent BadMatch errors we only
945                  * list formats with the same depth. */
946                 if(visinfo->depth != screen_depth)
947                 {
948                     XFree(visinfo);
949                     continue;
950                 }
951
952                 TRACE("Found onscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
953                 list[size].iPixelFormat = size+1; /* The index starts at 1 */
954                 list[size].fbconfig = cfgs[i];
955                 list[size].fmt_id = fmt_id;
956                 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
957                 list[size].offscreenOnly = FALSE;
958                 list[size].dwFlags = 0;
959                 size++;
960                 onscreen_size++;
961
962                 /* Clone a format if it is bitmap capable for indirect rendering to bitmaps */
963                 if(check_fbconfig_bitmap_capability(display, cfgs[i]))
964                 {
965                     TRACE("Found bitmap capable format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
966                     list[size].iPixelFormat = size+1; /* The index starts at 1 */
967                     list[size].fbconfig = cfgs[i];
968                     list[size].fmt_id = fmt_id;
969                     list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
970                     list[size].offscreenOnly = FALSE;
971                     list[size].dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI | PFD_GENERIC_FORMAT;
972                     size++;
973                     onscreen_size++;
974                 }
975             } else if(run && !visinfo) {
976                 TRACE("Found offscreen format FBCONFIG_ID 0x%x corresponding to iPixelFormat %d at GLX index %d\n", fmt_id, size+1, i);
977                 list[size].iPixelFormat = size+1; /* The index starts at 1 */
978                 list[size].fbconfig = cfgs[i];
979                 list[size].fmt_id = fmt_id;
980                 list[size].render_type = get_render_type_from_fbconfig(display, cfgs[i]);
981                 list[size].offscreenOnly = TRUE;
982                 list[size].dwFlags = 0;
983                 size++;
984             }
985
986             if (visinfo) XFree(visinfo);
987         }
988     }
989
990     if(cfgs != NULL) XFree(cfgs);
991
992 done:
993     if (size_ret) *size_ret = size;
994     if (onscreen_size_ret) *onscreen_size_ret = onscreen_size;
995     wine_tsx11_unlock();
996     return list;
997 }
998
999 /* GLX can advertise dozens of different pixelformats including offscreen and onscreen ones.
1000  * In our WGL implementation we only support a subset of these formats namely the format of
1001  * Wine's main visual and offscreen formats (if they are available).
1002  * This function converts a WGL format to its corresponding GLX one. It returns a WineGLPixelFormat
1003  * and it returns the number of supported WGL formats in fmt_count.
1004  */
1005 static WineGLPixelFormat* ConvertPixelFormatWGLtoGLX(Display *display, int iPixelFormat, BOOL AllowOffscreen, int *fmt_count)
1006 {
1007     WineGLPixelFormat *list, *res = NULL;
1008     int size, onscreen_size;
1009
1010     if (!(list = get_formats(display, &size, &onscreen_size ))) return NULL;
1011
1012     /* Check if the pixelformat is valid. Note that it is legal to pass an invalid
1013      * iPixelFormat in case of probing the number of pixelformats.
1014      */
1015     if((iPixelFormat > 0) && (iPixelFormat <= size) &&
1016        (!list[iPixelFormat-1].offscreenOnly || AllowOffscreen)) {
1017         res = &list[iPixelFormat-1];
1018         TRACE("Returning FBConfig=%p for iPixelFormat=%d\n", res->fbconfig, iPixelFormat);
1019     }
1020
1021     if(AllowOffscreen)
1022         *fmt_count = size;
1023     else
1024         *fmt_count = onscreen_size;
1025
1026     TRACE("Number of returned pixelformats=%d\n", *fmt_count);
1027
1028     return res;
1029 }
1030
1031 /* Search our internal pixelformat list for the WGL format corresponding to the given fbconfig */
1032 static WineGLPixelFormat* ConvertPixelFormatGLXtoWGL(Display *display, int fmt_id, DWORD dwFlags)
1033 {
1034     WineGLPixelFormat *list;
1035     int i, size;
1036
1037     if (!(list = get_formats(display, &size, NULL ))) return NULL;
1038
1039     for(i=0; i<size; i++) {
1040         /* A GLX format can appear multiple times in the pixel format list due to fake formats for bitmap rendering.
1041          * Fake formats might get selected when the user passes the proper flags using the dwFlags parameter. */
1042         if( (list[i].fmt_id == fmt_id) && ((list[i].dwFlags & dwFlags) == dwFlags) ) {
1043             TRACE("Returning iPixelFormat %d for fmt_id 0x%x\n", list[i].iPixelFormat, fmt_id);
1044             return &list[i];
1045         }
1046     }
1047     TRACE("No compatible format found for fmt_id 0x%x\n", fmt_id);
1048     return NULL;
1049 }
1050
1051 int pixelformat_from_fbconfig_id(XID fbconfig_id)
1052 {
1053     WineGLPixelFormat *fmt;
1054
1055     if (!fbconfig_id) return 0;
1056
1057     fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
1058     if(fmt)
1059         return fmt->iPixelFormat;
1060     /* This will happen on hwnds without a pixel format set; it's ok */
1061     return 0;
1062 }
1063
1064
1065 /* Mark any allocated context using the glx drawable 'old' to use 'new' */
1066 void mark_drawable_dirty(Drawable old, Drawable new)
1067 {
1068     Wine_GLContext *ctx;
1069     for (ctx = context_list; ctx; ctx = ctx->next) {
1070         if (old == ctx->drawables[0]) {
1071             ctx->drawables[0] = new;
1072             ctx->refresh_drawables = TRUE;
1073         }
1074         if (old == ctx->drawables[1]) {
1075             ctx->drawables[1] = new;
1076             ctx->refresh_drawables = TRUE;
1077         }
1078     }
1079 }
1080
1081 /* Given the current context, make sure its drawable is sync'd */
1082 static inline void sync_context(Wine_GLContext *context)
1083 {
1084     if(context && context->refresh_drawables) {
1085         if (glxRequireVersion(3))
1086             pglXMakeContextCurrent(gdi_display, context->drawables[0],
1087                                    context->drawables[1], context->ctx);
1088         else
1089             pglXMakeCurrent(gdi_display, context->drawables[0], context->ctx);
1090         context->refresh_drawables = FALSE;
1091     }
1092 }
1093
1094
1095 static GLXContext create_glxcontext(Display *display, Wine_GLContext *context, GLXContext shareList)
1096 {
1097     GLXContext ctx;
1098
1099     /* We use indirect rendering for rendering to bitmaps. See get_formats for a comment about this. */
1100     BOOL indirect = (context->fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? FALSE : TRUE;
1101
1102     if(context->gl3_context)
1103     {
1104         if(context->numAttribs)
1105             ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, context->attribList);
1106         else
1107             ctx = pglXCreateContextAttribsARB(gdi_display, context->fmt->fbconfig, shareList, indirect, NULL);
1108     }
1109     else if(context->vis)
1110         ctx = pglXCreateContext(gdi_display, context->vis, shareList, indirect);
1111     else /* Create a GLX Context for a pbuffer */
1112         ctx = pglXCreateNewContext(gdi_display, context->fmt->fbconfig, context->fmt->render_type, shareList, TRUE);
1113
1114     return ctx;
1115 }
1116
1117
1118 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
1119 {
1120     return pglXCreateGLXPixmap(display, vis, parent);
1121 }
1122
1123
1124 static XID create_bitmap_glxpixmap(X11DRV_PDEVICE *physDev, WineGLPixelFormat *fmt)
1125 {
1126     GLXPixmap ret = 0;
1127     XVisualInfo *vis;
1128
1129     wine_tsx11_lock();
1130
1131     vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1132     if(vis) {
1133         if(vis->depth == physDev->bitmap->pixmap_depth)
1134             ret = pglXCreateGLXPixmap(gdi_display, vis, physDev->bitmap->pixmap);
1135         XFree(vis);
1136     }
1137     wine_tsx11_unlock();
1138     TRACE("return %lx\n", ret);
1139     return ret;
1140 }
1141
1142 /**
1143  * X11DRV_ChoosePixelFormat
1144  *
1145  * Equivalent to glXChooseVisual.
1146  */
1147 int CDECL X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
1148                              const PIXELFORMATDESCRIPTOR *ppfd) {
1149     WineGLPixelFormat *list;
1150     int onscreen_size;
1151     int ret = 0;
1152     int value = 0;
1153     int i = 0;
1154     int bestFormat = -1;
1155     int bestDBuffer = -1;
1156     int bestStereo = -1;
1157     int bestColor = -1;
1158     int bestAlpha = -1;
1159     int bestDepth = -1;
1160     int bestStencil = -1;
1161     int bestAux = -1;
1162
1163     if (!has_opengl()) return 0;
1164
1165     if (TRACE_ON(wgl)) {
1166         TRACE("(%p,%p)\n", physDev, ppfd);
1167
1168         dump_PIXELFORMATDESCRIPTOR(ppfd);
1169     }
1170
1171     if (!(list = get_formats(gdi_display, NULL, &onscreen_size ))) return 0;
1172
1173     wine_tsx11_lock();
1174     for(i=0; i<onscreen_size; i++)
1175     {
1176         int dwFlags = 0;
1177         int iPixelType = 0;
1178         int alpha=0, color=0, depth=0, stencil=0, aux=0;
1179         WineGLPixelFormat *fmt = &list[i];
1180
1181         /* Pixel type */
1182         pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1183         if (value & GLX_RGBA_BIT)
1184             iPixelType = PFD_TYPE_RGBA;
1185         else
1186             iPixelType = PFD_TYPE_COLORINDEX;
1187
1188         if (ppfd->iPixelType != iPixelType)
1189         {
1190             TRACE("pixel type mismatch for iPixelFormat=%d\n", i+1);
1191             continue;
1192         }
1193
1194         /* Only use bitmap capable for formats for bitmap rendering.
1195          * See get_formats for more info. */
1196         if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (fmt->dwFlags & PFD_DRAW_TO_BITMAP))
1197         {
1198             TRACE("PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i+1);
1199             continue;
1200         }
1201
1202         pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1203         if (value) dwFlags |= PFD_DOUBLEBUFFER;
1204         pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value);
1205         if (value) dwFlags |= PFD_STEREO;
1206         pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &color); /* cColorBits */
1207         pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &alpha); /* cAlphaBits */
1208         pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &depth); /* cDepthBits */
1209         pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &stencil); /* cStencilBits */
1210         pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &aux); /* cAuxBuffers */
1211
1212         /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
1213          * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
1214          * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
1215          * formats without the given flag set.
1216          * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
1217          * has indicated that a format without stereo is returned when stereo is unavailable.
1218          * So in case PFD_STEREO is set, formats that support it should have priority above formats
1219          * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
1220          *
1221          * To summarize the following is most likely the correct behavior:
1222          * stereo not set -> prefer no-stereo formats, else also accept stereo formats
1223          * stereo set -> prefer stereo formats, else also accept no-stereo formats
1224          * stereo don't care -> it doesn't matter whether we get stereo or not
1225          *
1226          * In Wine we will treat no-stereo the same way as don't care because it makes
1227          * format selection even more complicated and second drivers with Stereo advertise
1228          * each format twice anyway.
1229          */
1230
1231         /* Doublebuffer, see the comments above */
1232         if( !(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) ) {
1233             if( ((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
1234                 ((dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)) )
1235             {
1236                 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1237                 bestStereo = dwFlags & PFD_STEREO;
1238                 bestAlpha = alpha;
1239                 bestColor = color;
1240                 bestDepth = depth;
1241                 bestStencil = stencil;
1242                 bestAux = aux;
1243                 bestFormat = i;
1244                 continue;
1245             }
1246             if(bestDBuffer != -1 && (dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer)
1247                 continue;
1248         }
1249
1250         /* Stereo, see the comments above. */
1251         if( !(ppfd->dwFlags & PFD_STEREO_DONTCARE) ) {
1252             if( ((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
1253                 ((dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)) )
1254             {
1255                 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1256                 bestStereo = dwFlags & PFD_STEREO;
1257                 bestAlpha = alpha;
1258                 bestColor = color;
1259                 bestDepth = depth;
1260                 bestStencil = stencil;
1261                 bestAux = aux;
1262                 bestFormat = i;
1263                 continue;
1264             }
1265             if(bestStereo != -1 && (dwFlags & PFD_STEREO) != bestStereo)
1266                 continue;
1267         }
1268
1269         /* Below we will do a number of checks to select the 'best' pixelformat.
1270          * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
1271          * The code works by trying to match the most important options as close as possible.
1272          * When a reasonable format is found, we will try to match more options.
1273          * It appears (see the opengl32 test) that Windows opengl drivers ignore options
1274          * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
1275          * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
1276
1277         /* Color bits */
1278         if(ppfd->cColorBits) {
1279             if( ((ppfd->cColorBits > bestColor) && (color > bestColor)) ||
1280                 ((color >= ppfd->cColorBits) && (color < bestColor)) )
1281             {
1282                 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1283                 bestStereo = dwFlags & PFD_STEREO;
1284                 bestAlpha = alpha;
1285                 bestColor = color;
1286                 bestDepth = depth;
1287                 bestStencil = stencil;
1288                 bestAux = aux;
1289                 bestFormat = i;
1290                 continue;
1291             } else if(bestColor != color) {  /* Do further checks if the format is compatible */
1292                 TRACE("color mismatch for iPixelFormat=%d\n", i+1);
1293                 continue;
1294             }
1295         }
1296
1297         /* Alpha bits */
1298         if(ppfd->cAlphaBits) {
1299             if( ((ppfd->cAlphaBits > bestAlpha) && (alpha > bestAlpha)) ||
1300                 ((alpha >= ppfd->cAlphaBits) && (alpha < bestAlpha)) )
1301             {
1302                 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1303                 bestStereo = dwFlags & PFD_STEREO;
1304                 bestAlpha = alpha;
1305                 bestColor = color;
1306                 bestDepth = depth;
1307                 bestStencil = stencil;
1308                 bestAux = aux;
1309                 bestFormat = i;
1310                 continue;
1311             } else if(bestAlpha != alpha) {
1312                 TRACE("alpha mismatch for iPixelFormat=%d\n", i+1);
1313                 continue;
1314             }
1315         }
1316
1317         /* Depth bits */
1318         if(ppfd->cDepthBits) {
1319             if( ((ppfd->cDepthBits > bestDepth) && (depth > bestDepth)) ||
1320                 ((depth >= ppfd->cDepthBits) && (depth < bestDepth)) )
1321             {
1322                 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1323                 bestStereo = dwFlags & PFD_STEREO;
1324                 bestAlpha = alpha;
1325                 bestColor = color;
1326                 bestDepth = depth;
1327                 bestStencil = stencil;
1328                 bestAux = aux;
1329                 bestFormat = i;
1330                 continue;
1331             } else if(bestDepth != depth) {
1332                 TRACE("depth mismatch for iPixelFormat=%d\n", i+1);
1333                 continue;
1334             }
1335         }
1336
1337         /* Stencil bits */
1338         if(ppfd->cStencilBits) {
1339             if( ((ppfd->cStencilBits > bestStencil) && (stencil > bestStencil)) ||
1340                 ((stencil >= ppfd->cStencilBits) && (stencil < bestStencil)) )
1341             {
1342                 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1343                 bestStereo = dwFlags & PFD_STEREO;
1344                 bestAlpha = alpha;
1345                 bestColor = color;
1346                 bestDepth = depth;
1347                 bestStencil = stencil;
1348                 bestAux = aux;
1349                 bestFormat = i;
1350                 continue;
1351             } else if(bestStencil != stencil) {
1352                 TRACE("stencil mismatch for iPixelFormat=%d\n", i+1);
1353                 continue;
1354             }
1355         }
1356
1357         /* Aux buffers */
1358         if(ppfd->cAuxBuffers) {
1359             if( ((ppfd->cAuxBuffers > bestAux) && (aux > bestAux)) ||
1360                 ((aux >= ppfd->cAuxBuffers) && (aux < bestAux)) )
1361             {
1362                 bestDBuffer = dwFlags & PFD_DOUBLEBUFFER;
1363                 bestStereo = dwFlags & PFD_STEREO;
1364                 bestAlpha = alpha;
1365                 bestColor = color;
1366                 bestDepth = depth;
1367                 bestStencil = stencil;
1368                 bestAux = aux;
1369                 bestFormat = i;
1370                 continue;
1371             } else if(bestAux != aux) {
1372                 TRACE("aux mismatch for iPixelFormat=%d\n", i+1);
1373                 continue;
1374             }
1375         }
1376     }
1377
1378     if(bestFormat == -1) {
1379         TRACE("No matching mode was found returning 0\n");
1380         ret = 0;
1381     }
1382     else {
1383         ret = bestFormat+1; /* the return value should be a 1-based index */
1384         TRACE("Successfully found a matching mode, returning index: %d %x\n", ret, list[bestFormat].fmt_id);
1385     }
1386
1387     wine_tsx11_unlock();
1388
1389     return ret;
1390 }
1391 /**
1392  * X11DRV_DescribePixelFormat
1393  *
1394  * Get the pixel-format descriptor associated to the given id
1395  */
1396 int CDECL X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
1397                                int iPixelFormat,
1398                                UINT nBytes,
1399                                PIXELFORMATDESCRIPTOR *ppfd) {
1400   /*XVisualInfo *vis;*/
1401   int value;
1402   int rb,gb,bb,ab;
1403   WineGLPixelFormat *fmt;
1404   int ret = 0;
1405   int fmt_count = 0;
1406
1407   if (!has_opengl()) return 0;
1408
1409   TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
1410
1411   /* Look for the iPixelFormat in our list of supported formats. If it is supported we get the index in the FBConfig table and the number of supported formats back */
1412   fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &fmt_count);
1413   if (ppfd == NULL) {
1414       /* The application is only querying the number of pixelformats */
1415       return fmt_count;
1416   } else if(fmt == NULL) {
1417       WARN("unexpected iPixelFormat(%d): not >=1 and <=nFormats(%d), returning NULL!\n", iPixelFormat, fmt_count);
1418       return 0;
1419   }
1420
1421   if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
1422     ERR("Wrong structure size !\n");
1423     /* Should set error */
1424     return 0;
1425   }
1426
1427   ret = fmt_count;
1428
1429   memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
1430   ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
1431   ppfd->nVersion = 1;
1432
1433   /* These flags are always the same... */
1434   ppfd->dwFlags = PFD_SUPPORT_OPENGL;
1435   /* Now the flags extracted from the Visual */
1436
1437   wine_tsx11_lock();
1438
1439   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1440   if(value & GLX_WINDOW_BIT)
1441       ppfd->dwFlags |= PFD_DRAW_TO_WINDOW;
1442
1443   /* On Windows bitmap rendering is only offered using the GDI Software renderer. We reserve some formats (see get_formats for more info)
1444    * for bitmap rendering since we require indirect rendering for this. Further pixel format logs of a GeforceFX, Geforce8800GT, Radeon HD3400 and a
1445    * Radeon 9000 indicated that all bitmap formats have PFD_SUPPORT_GDI. Except for 2 formats on the Radeon 9000 none of the hw accelerated formats
1446    * offered the GDI bit either. */
1447   ppfd->dwFlags |= fmt->dwFlags & (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI);
1448
1449   /* PFD_GENERIC_FORMAT - gdi software rendering
1450    * PFD_GENERIC_ACCELERATED - some parts are accelerated by a display driver (MCD e.g. 3dfx minigl)
1451    * none set - full hardware accelerated by a ICD
1452    *
1453    * We only set PFD_GENERIC_FORMAT on bitmap formats (see get_formats) as that's what ATI and Nvidia Windows drivers do  */
1454   ppfd->dwFlags |= fmt->dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED);
1455
1456   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DOUBLEBUFFER, &value);
1457   if (value) {
1458       ppfd->dwFlags |= PFD_DOUBLEBUFFER;
1459       ppfd->dwFlags &= ~PFD_SUPPORT_GDI;
1460   }
1461   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
1462
1463   /* Pixel type */
1464   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RENDER_TYPE, &value);
1465   if (value & GLX_RGBA_BIT)
1466     ppfd->iPixelType = PFD_TYPE_RGBA;
1467   else
1468     ppfd->iPixelType = PFD_TYPE_COLORINDEX;
1469
1470   /* Color bits */
1471   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BUFFER_SIZE, &value);
1472   ppfd->cColorBits = value;
1473
1474   /* Red, green, blue and alpha bits / shifts */
1475   if (ppfd->iPixelType == PFD_TYPE_RGBA) {
1476     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_RED_SIZE, &rb);
1477     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_GREEN_SIZE, &gb);
1478     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_BLUE_SIZE, &bb);
1479     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ALPHA_SIZE, &ab);
1480
1481     ppfd->cRedBits = rb;
1482     ppfd->cRedShift = gb + bb + ab;
1483     ppfd->cBlueBits = bb;
1484     ppfd->cBlueShift = ab;
1485     ppfd->cGreenBits = gb;
1486     ppfd->cGreenShift = bb + ab;
1487     ppfd->cAlphaBits = ab;
1488     ppfd->cAlphaShift = 0;
1489   } else {
1490     ppfd->cRedBits = 0;
1491     ppfd->cRedShift = 0;
1492     ppfd->cBlueBits = 0;
1493     ppfd->cBlueShift = 0;
1494     ppfd->cGreenBits = 0;
1495     ppfd->cGreenShift = 0;
1496     ppfd->cAlphaBits = 0;
1497     ppfd->cAlphaShift = 0;
1498   }
1499
1500   /* Accum RGBA bits */
1501   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &rb);
1502   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &gb);
1503   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &bb);
1504   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &ab);
1505
1506   ppfd->cAccumBits = rb+gb+bb+ab;
1507   ppfd->cAccumRedBits = rb;
1508   ppfd->cAccumGreenBits = gb;
1509   ppfd->cAccumBlueBits = bb;
1510   ppfd->cAccumAlphaBits = ab;
1511
1512   /* Aux bits */
1513   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_AUX_BUFFERS, &value);
1514   ppfd->cAuxBuffers = value;
1515
1516   /* Depth bits */
1517   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DEPTH_SIZE, &value);
1518   ppfd->cDepthBits = value;
1519
1520   /* stencil bits */
1521   pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_STENCIL_SIZE, &value);
1522   ppfd->cStencilBits = value;
1523
1524   wine_tsx11_unlock();
1525
1526   ppfd->iLayerType = PFD_MAIN_PLANE;
1527
1528   if (TRACE_ON(wgl)) {
1529     dump_PIXELFORMATDESCRIPTOR(ppfd);
1530   }
1531
1532   return ret;
1533 }
1534
1535 /**
1536  * X11DRV_GetPixelFormat
1537  *
1538  * Get the pixel-format id used by this DC
1539  */
1540 int CDECL X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
1541   WineGLPixelFormat *fmt;
1542   int tmp;
1543   TRACE("(%p)\n", physDev);
1544
1545   if (!physDev->current_pf) return 0;  /* not set yet */
1546
1547   fmt = ConvertPixelFormatWGLtoGLX(gdi_display, physDev->current_pf, TRUE, &tmp);
1548   if(!fmt)
1549   {
1550     ERR("Unable to find a WineGLPixelFormat for iPixelFormat=%d\n", physDev->current_pf);
1551     return 0;
1552   }
1553   else if(fmt->offscreenOnly)
1554   {
1555     /* Offscreen formats can't be used with traditional WGL calls.
1556      * As has been verified on Windows GetPixelFormat doesn't fail but returns iPixelFormat=1. */
1557      TRACE("Returning iPixelFormat=1 for offscreen format: %d\n", fmt->iPixelFormat);
1558     return 1;
1559   }
1560
1561   TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
1562   return physDev->current_pf;
1563 }
1564
1565 /* This function is the core of X11DRV_SetPixelFormat and X11DRV_SetPixelFormatWINE.
1566  * Both functions are the same except that X11DRV_SetPixelFormatWINE allows you to
1567  * set the pixel format multiple times. */
1568 static BOOL internal_SetPixelFormat(X11DRV_PDEVICE *physDev,
1569                            int iPixelFormat,
1570                            const PIXELFORMATDESCRIPTOR *ppfd) {
1571     WineGLPixelFormat *fmt;
1572     int value;
1573     HWND hwnd;
1574
1575     /* SetPixelFormat is not allowed on the X root_window e.g. GetDC(0) */
1576     if(get_glxdrawable(physDev) == root_window)
1577     {
1578         ERR("Invalid operation on root_window\n");
1579         return FALSE;
1580     }
1581
1582     /* Check if iPixelFormat is in our list of supported formats to see if it is supported. */
1583     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, FALSE /* Offscreen */, &value);
1584     if(!fmt) {
1585         ERR("Invalid iPixelFormat: %d\n", iPixelFormat);
1586         return FALSE;
1587     }
1588
1589     wine_tsx11_lock();
1590     pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1591     wine_tsx11_unlock();
1592
1593     hwnd = WindowFromDC(physDev->hdc);
1594     if(hwnd) {
1595         if(!(value&GLX_WINDOW_BIT)) {
1596             WARN("Pixel format %d is not compatible for window rendering\n", iPixelFormat);
1597             return FALSE;
1598         }
1599
1600         if(!SendMessageW(hwnd, WM_X11DRV_SET_WIN_FORMAT, (WPARAM)fmt->fmt_id, 0)) {
1601             ERR("Couldn't set format of the window, returning failure\n");
1602             return FALSE;
1603         }
1604     }
1605     else if(physDev->bitmap) {
1606         if(!(value&GLX_PIXMAP_BIT)) {
1607             WARN("Pixel format %d is not compatible for bitmap rendering\n", iPixelFormat);
1608             return FALSE;
1609         }
1610
1611         physDev->bitmap->glxpixmap = create_bitmap_glxpixmap(physDev, fmt);
1612         if(!physDev->bitmap->glxpixmap) {
1613             WARN("Couldn't create glxpixmap for pixel format %d\n", iPixelFormat);
1614             return FALSE;
1615         }
1616     }
1617     else {
1618         FIXME("called on a non-window, non-bitmap object?\n");
1619     }
1620
1621     physDev->current_pf = iPixelFormat;
1622
1623     if (TRACE_ON(wgl)) {
1624         int gl_test = 0;
1625
1626         wine_tsx11_lock();
1627         gl_test = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_FBCONFIG_ID, &value);
1628         if (gl_test) {
1629            ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
1630         } else {
1631             TRACE(" FBConfig have :\n");
1632             TRACE(" - FBCONFIG_ID   0x%x\n", value);
1633             pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_VISUAL_ID, &value);
1634             TRACE(" - VISUAL_ID     0x%x\n", value);
1635             pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &value);
1636             TRACE(" - DRAWABLE_TYPE 0x%x\n", value);
1637         }
1638         wine_tsx11_unlock();
1639     }
1640     return TRUE;
1641 }
1642
1643
1644 /**
1645  * X11DRV_SetPixelFormat
1646  *
1647  * Set the pixel-format id used by this DC
1648  */
1649 BOOL CDECL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
1650                            int iPixelFormat,
1651                            const PIXELFORMATDESCRIPTOR *ppfd) {
1652     TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
1653
1654     if (!has_opengl()) return FALSE;
1655
1656     if(physDev->current_pf)  /* cannot change it if already set */
1657         return (physDev->current_pf == iPixelFormat);
1658
1659     return internal_SetPixelFormat(physDev, iPixelFormat, ppfd);
1660 }
1661
1662 /**
1663  * X11DRV_wglCopyContext
1664  *
1665  * For OpenGL32 wglCopyContext.
1666  */
1667 BOOL CDECL X11DRV_wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask) {
1668     Wine_GLContext *src = (Wine_GLContext*)hglrcSrc;
1669     Wine_GLContext *dst = (Wine_GLContext*)hglrcDst;
1670
1671     TRACE("hglrcSrc: (%p), hglrcDst: (%p), mask: %#x\n", hglrcSrc, hglrcDst, mask);
1672
1673     wine_tsx11_lock();
1674     pglXCopyContext(gdi_display, src->ctx, dst->ctx, mask);
1675     wine_tsx11_unlock();
1676
1677     /* As opposed to wglCopyContext, glXCopyContext doesn't return anything, so hopefully we passed */
1678     return TRUE;
1679 }
1680
1681 /**
1682  * X11DRV_wglCreateContext
1683  *
1684  * For OpenGL32 wglCreateContext.
1685  */
1686 HGLRC CDECL X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev)
1687 {
1688     Wine_GLContext *ret;
1689     WineGLPixelFormat *fmt;
1690     int hdcPF = physDev->current_pf;
1691     int fmt_count = 0;
1692     HDC hdc = physDev->hdc;
1693
1694     TRACE("(%p)->(PF:%d)\n", hdc, hdcPF);
1695
1696     if (!has_opengl()) return 0;
1697
1698     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, hdcPF, TRUE /* Offscreen */, &fmt_count);
1699     /* We can render using the iPixelFormat (1) of Wine's Main visual AND using some offscreen formats.
1700      * Note that standard WGL-calls don't recognize offscreen-only formats. For that reason pbuffers
1701      * use a sort of 'proxy' HDC (wglGetPbufferDCARB).
1702      * If this fails something is very wrong on the system. */
1703     if(!fmt) {
1704         ERR("Cannot get FB Config for iPixelFormat %d, expect problems!\n", hdcPF);
1705         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
1706         return NULL;
1707     }
1708
1709     wine_tsx11_lock();
1710     ret = alloc_context();
1711     ret->hdc = hdc;
1712     ret->fmt = fmt;
1713     ret->has_been_current = FALSE;
1714     ret->sharing = FALSE;
1715
1716     ret->vis = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
1717     ret->ctx = create_glxcontext(gdi_display, ret, NULL);
1718     wine_tsx11_unlock();
1719
1720     TRACE(" creating context %p (GL context creation delayed)\n", ret);
1721     return (HGLRC) ret;
1722 }
1723
1724 /**
1725  * X11DRV_wglDeleteContext
1726  *
1727  * For OpenGL32 wglDeleteContext.
1728  */
1729 BOOL CDECL X11DRV_wglDeleteContext(HGLRC hglrc)
1730 {
1731     Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1732     BOOL ret = TRUE;
1733
1734     TRACE("(%p)\n", hglrc);
1735
1736     if (!has_opengl()) return 0;
1737
1738     wine_tsx11_lock();
1739     /* A game (Half Life not to name it) deletes twice the same context,
1740     * so make sure it is valid first */
1741     if (is_valid_context( ctx ))
1742     {
1743         if (ctx->ctx) pglXDestroyContext(gdi_display, ctx->ctx);
1744         free_context(ctx);
1745     }
1746     else
1747     {
1748         WARN("Error deleting context !\n");
1749         SetLastError(ERROR_INVALID_HANDLE);
1750         ret = FALSE;
1751     }
1752     wine_tsx11_unlock();
1753
1754     return ret;
1755 }
1756
1757 /**
1758  * X11DRV_wglGetCurrentReadDCARB
1759  *
1760  * For OpenGL32 wglGetCurrentReadDCARB.
1761  */
1762 static HDC WINAPI X11DRV_wglGetCurrentReadDCARB(void) 
1763 {
1764     HDC ret = 0;
1765     Wine_GLContext *ctx = NtCurrentTeb()->glContext;
1766
1767     if (ctx) ret = ctx->read_hdc;
1768
1769     TRACE(" returning %p (GL drawable %lu)\n", ret, ctx ? ctx->drawables[1] : 0);
1770     return ret;
1771 }
1772
1773 /**
1774  * X11DRV_wglGetProcAddress
1775  *
1776  * For OpenGL32 wglGetProcAddress.
1777  */
1778 PROC CDECL X11DRV_wglGetProcAddress(LPCSTR lpszProc)
1779 {
1780     int i, j;
1781     const WineGLExtension *ext;
1782
1783     int padding = 32 - strlen(lpszProc);
1784     if (padding < 0)
1785         padding = 0;
1786
1787     if (!has_opengl()) return NULL;
1788
1789     /* Check the table of WGL extensions to see if we need to return a WGL extension
1790      * or a function pointer to a native OpenGL function. */
1791     if(strncmp(lpszProc, "wgl", 3) != 0) {
1792         return pglXGetProcAddressARB((const GLubyte*)lpszProc);
1793     } else {
1794         TRACE("('%s'):%*s", lpszProc, padding, " ");
1795         for (i = 0; i < WineGLExtensionListSize; ++i) {
1796             ext = WineGLExtensionList[i];
1797             for (j = 0; ext->extEntryPoints[j].funcName; ++j) {
1798                 if (strcmp(ext->extEntryPoints[j].funcName, lpszProc) == 0) {
1799                     TRACE("(%p) - WineGL\n", ext->extEntryPoints[j].funcAddress);
1800                     return ext->extEntryPoints[j].funcAddress;
1801                 }
1802             }
1803         }
1804     }
1805
1806     WARN("(%s) - not found\n", lpszProc);
1807     return NULL;
1808 }
1809
1810 /**
1811  * X11DRV_wglMakeCurrent
1812  *
1813  * For OpenGL32 wglMakeCurrent.
1814  */
1815 BOOL CDECL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
1816     BOOL ret;
1817     HDC hdc = physDev->hdc;
1818     DWORD type = GetObjectType(hdc);
1819     Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1820
1821     TRACE("(%p,%p)\n", hdc, hglrc);
1822
1823     if (!has_opengl()) return FALSE;
1824
1825     wine_tsx11_lock();
1826     if (hglrc == NULL) {
1827         ret = pglXMakeCurrent(gdi_display, None, NULL);
1828         NtCurrentTeb()->glContext = NULL;
1829     } else if (ctx->fmt->iPixelFormat != physDev->current_pf) {
1830         WARN( "mismatched pixel format hdc %p %u ctx %p %u\n",
1831               hdc, physDev->current_pf, ctx, ctx->fmt->iPixelFormat );
1832         SetLastError( ERROR_INVALID_PIXEL_FORMAT );
1833         ret = FALSE;
1834     } else {
1835         Drawable drawable = get_glxdrawable(physDev);
1836
1837         /* The describe lines below are for debugging purposes only */
1838         if (TRACE_ON(wgl)) {
1839             describeDrawable(physDev);
1840             describeContext(ctx);
1841         }
1842
1843         TRACE(" make current for dis %p, drawable %p, ctx %p\n", gdi_display, (void*) drawable, ctx->ctx);
1844         ret = pglXMakeCurrent(gdi_display, drawable, ctx->ctx);
1845         NtCurrentTeb()->glContext = ctx;
1846
1847         if(ret)
1848         {
1849             ctx->has_been_current = TRUE;
1850             ctx->hdc = hdc;
1851             ctx->read_hdc = hdc;
1852             ctx->drawables[0] = drawable;
1853             ctx->drawables[1] = drawable;
1854             ctx->refresh_drawables = FALSE;
1855
1856             if (type == OBJ_MEMDC)
1857             {
1858                 ctx->do_escape = TRUE;
1859                 pglDrawBuffer(GL_FRONT_LEFT);
1860             }
1861         }
1862     }
1863     wine_tsx11_unlock();
1864     TRACE(" returning %s\n", (ret ? "True" : "False"));
1865     return ret;
1866 }
1867
1868 /**
1869  * X11DRV_wglMakeContextCurrentARB
1870  *
1871  * For OpenGL32 wglMakeContextCurrentARB
1872  */
1873 BOOL CDECL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* pDrawDev, X11DRV_PDEVICE* pReadDev, HGLRC hglrc)
1874 {
1875     BOOL ret;
1876
1877     TRACE("(%p,%p,%p)\n", pDrawDev, pReadDev, hglrc);
1878
1879     if (!has_opengl()) return 0;
1880
1881     wine_tsx11_lock();
1882     if (hglrc == NULL) {
1883         ret = pglXMakeCurrent(gdi_display, None, NULL);
1884         NtCurrentTeb()->glContext = NULL;
1885     } else {
1886         if (NULL == pglXMakeContextCurrent) {
1887             ret = FALSE;
1888         } else {
1889             Wine_GLContext *ctx = (Wine_GLContext *) hglrc;
1890             Drawable d_draw = get_glxdrawable(pDrawDev);
1891             Drawable d_read = get_glxdrawable(pReadDev);
1892
1893             ctx->has_been_current = TRUE;
1894             ctx->hdc = pDrawDev->hdc;
1895             ctx->read_hdc = pReadDev->hdc;
1896             ctx->drawables[0] = d_draw;
1897             ctx->drawables[1] = d_read;
1898             ctx->refresh_drawables = FALSE;
1899             ret = pglXMakeContextCurrent(gdi_display, d_draw, d_read, ctx->ctx);
1900             NtCurrentTeb()->glContext = ctx;
1901         }
1902     }
1903     wine_tsx11_unlock();
1904
1905     TRACE(" returning %s\n", (ret ? "True" : "False"));
1906     return ret;
1907 }
1908
1909 /**
1910  * X11DRV_wglShareLists
1911  *
1912  * For OpenGL32 wglShareLists.
1913  */
1914 BOOL CDECL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
1915     Wine_GLContext *org  = (Wine_GLContext *) hglrc1;
1916     Wine_GLContext *dest = (Wine_GLContext *) hglrc2;
1917
1918     TRACE("(%p, %p)\n", org, dest);
1919
1920     if (!has_opengl()) return FALSE;
1921
1922     /* Sharing of display lists works differently in GLX and WGL. In case of GLX it is done
1923      * at context creation time but in case of WGL it is done using wglShareLists.
1924      * In the past we tried to emulate wglShareLists by delaying GLX context creation until
1925      * either a wglMakeCurrent or wglShareLists. This worked fine for most apps but it causes
1926      * issues for OpenGL 3 because there wglCreateContextAttribsARB can fail in a lot of cases,
1927      * so there delaying context creation doesn't work.
1928      *
1929      * The new approach is to create a GLX context in wglCreateContext / wglCreateContextAttribsARB
1930      * and when a program requests sharing we recreate the destination context if it hasn't been made
1931      * current or when it hasn't shared display lists before.
1932      */
1933
1934     if((org->has_been_current && dest->has_been_current) || dest->has_been_current)
1935     {
1936         ERR("Could not share display lists, one of the contexts has been current already !\n");
1937         return FALSE;
1938     }
1939     else if(dest->sharing)
1940     {
1941         ERR("Could not share display lists because hglrc2 has already shared lists before\n");
1942         return FALSE;
1943     }
1944     else
1945     {
1946         if((GetObjectType(org->hdc) == OBJ_MEMDC) ^ (GetObjectType(dest->hdc) == OBJ_MEMDC))
1947         {
1948             WARN("Attempting to share a context between a direct and indirect rendering context, expect issues!\n");
1949         }
1950
1951         wine_tsx11_lock();
1952         describeContext(org);
1953         describeContext(dest);
1954
1955         /* Re-create the GLX context and share display lists */
1956         pglXDestroyContext(gdi_display, dest->ctx);
1957         dest->ctx = create_glxcontext(gdi_display, dest, org->ctx);
1958         wine_tsx11_unlock();
1959         TRACE(" re-created an OpenGL context (%p) for Wine context %p sharing lists with OpenGL ctx %p\n", dest->ctx, dest, org->ctx);
1960
1961         org->sharing = TRUE;
1962         dest->sharing = TRUE;
1963         return TRUE;
1964     }
1965     return FALSE;
1966 }
1967
1968 static BOOL internal_wglUseFontBitmaps(HDC hdc, DWORD first, DWORD count, DWORD listBase, DWORD (WINAPI *GetGlyphOutline_ptr)(HDC,UINT,UINT,LPGLYPHMETRICS,DWORD,LPVOID,const MAT2*))
1969 {
1970      /* We are running using client-side rendering fonts... */
1971      GLYPHMETRICS gm;
1972      unsigned int glyph, size = 0;
1973      void *bitmap = NULL, *gl_bitmap = NULL;
1974      int org_alignment;
1975
1976      wine_tsx11_lock();
1977      pglGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1978      pglPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1979      wine_tsx11_unlock();
1980
1981      for (glyph = first; glyph < first + count; glyph++) {
1982          static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1983          unsigned int needed_size = GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1984          unsigned int height, width_int;
1985
1986          TRACE("Glyph : %3d / List : %d\n", glyph, listBase);
1987          if (needed_size == GDI_ERROR) {
1988              TRACE("  - needed size : %d (GDI_ERROR)\n", needed_size);
1989              goto error;
1990          } else {
1991              TRACE("  - needed size : %d\n", needed_size);
1992          }
1993
1994          if (needed_size > size) {
1995              size = needed_size;
1996              HeapFree(GetProcessHeap(), 0, bitmap);
1997              HeapFree(GetProcessHeap(), 0, gl_bitmap);
1998              bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1999              gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
2000          }
2001          if (GetGlyphOutline_ptr(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, &identity) == GDI_ERROR)
2002              goto error;
2003          if (TRACE_ON(wgl)) {
2004              unsigned int height, width, bitmask;
2005              unsigned char *bitmap_ = bitmap;
2006
2007              TRACE("  - bbox : %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
2008              TRACE("  - origin : (%d , %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
2009              TRACE("  - increment : %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
2010              if (needed_size != 0) {
2011                  TRACE("  - bitmap :\n");
2012                  for (height = 0; height < gm.gmBlackBoxY; height++) {
2013                      TRACE("      ");
2014                      for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
2015                          if (bitmask == 0) {
2016                              bitmap_ += 1;
2017                              bitmask = 0x80;
2018                          }
2019                          if (*bitmap_ & bitmask)
2020                              TRACE("*");
2021                          else
2022                              TRACE(" ");
2023                      }
2024                      bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
2025                      TRACE("\n");
2026                  }
2027              }
2028          }
2029
2030          /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
2031          * glyph for it to be drawn properly.
2032          */
2033          if (needed_size != 0) {
2034              width_int = (gm.gmBlackBoxX + 31) / 32;
2035              for (height = 0; height < gm.gmBlackBoxY; height++) {
2036                  unsigned int width;
2037                  for (width = 0; width < width_int; width++) {
2038                      ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
2039                      ((int *) bitmap)[height * width_int + width];
2040                  }
2041              }
2042          }
2043
2044          wine_tsx11_lock();
2045          pglNewList(listBase++, GL_COMPILE);
2046          if (needed_size != 0) {
2047              pglBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
2048                      0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
2049                      gm.gmCellIncX, gm.gmCellIncY,
2050                      gl_bitmap);
2051          } else {
2052              /* This is the case of 'empty' glyphs like the space character */
2053              pglBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
2054          }
2055          pglEndList();
2056          wine_tsx11_unlock();
2057      }
2058
2059      wine_tsx11_lock();
2060      pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
2061      wine_tsx11_unlock();
2062
2063      HeapFree(GetProcessHeap(), 0, bitmap);
2064      HeapFree(GetProcessHeap(), 0, gl_bitmap);
2065      return TRUE;
2066
2067   error:
2068      wine_tsx11_lock();
2069      pglPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
2070      wine_tsx11_unlock();
2071
2072      HeapFree(GetProcessHeap(), 0, bitmap);
2073      HeapFree(GetProcessHeap(), 0, gl_bitmap);
2074      return FALSE;
2075 }
2076
2077 /**
2078  * X11DRV_wglUseFontBitmapsA
2079  *
2080  * For OpenGL32 wglUseFontBitmapsA.
2081  */
2082 BOOL CDECL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
2083 {
2084      Font fid = physDev->font;
2085
2086      TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
2087
2088      if (!has_opengl()) return FALSE;
2089
2090      if (fid == 0) {
2091          return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineA);
2092      }
2093
2094      wine_tsx11_lock();
2095      /* I assume that the glyphs are at the same position for X and for Windows */
2096      pglXUseXFont(fid, first, count, listBase);
2097      wine_tsx11_unlock();
2098      return TRUE;
2099 }
2100
2101 /**
2102  * X11DRV_wglUseFontBitmapsW
2103  *
2104  * For OpenGL32 wglUseFontBitmapsW.
2105  */
2106 BOOL CDECL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
2107 {
2108      Font fid = physDev->font;
2109
2110      TRACE("(%p, %d, %d, %d) using font %ld\n", physDev->hdc, first, count, listBase, fid);
2111
2112      if (!has_opengl()) return FALSE;
2113
2114      if (fid == 0) {
2115          return internal_wglUseFontBitmaps(physDev->hdc, first, count, listBase, GetGlyphOutlineW);
2116      }
2117
2118      WARN("Using the glX API for the WCHAR variant - some characters may come out incorrectly !\n");
2119
2120      wine_tsx11_lock();
2121      /* I assume that the glyphs are at the same position for X and for Windows */
2122      pglXUseXFont(fid, first, count, listBase);
2123      wine_tsx11_unlock();
2124      return TRUE;
2125 }
2126
2127 /* WGL helper function which handles differences in glGetIntegerv from WGL and GLX */
2128 static void WINAPI X11DRV_wglGetIntegerv(GLenum pname, GLint* params)
2129 {
2130     wine_tsx11_lock();
2131     switch(pname)
2132     {
2133     case GL_DEPTH_BITS:
2134         {
2135             Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2136
2137             pglGetIntegerv(pname, params);
2138             /**
2139              * if we cannot find a Wine Context
2140              * we only have the default wine desktop context,
2141              * so if we have only a 24 depth say we have 32
2142              */
2143             if (!ctx && *params == 24) {
2144                 *params = 32;
2145             }
2146             TRACE("returns GL_DEPTH_BITS as '%d'\n", *params);
2147             break;
2148         }
2149     case GL_ALPHA_BITS:
2150         {
2151             Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2152
2153             pglXGetFBConfigAttrib(gdi_display, ctx->fmt->fbconfig, GLX_ALPHA_SIZE, params);
2154             TRACE("returns GL_ALPHA_BITS as '%d'\n", *params);
2155             break;
2156         }
2157     default:
2158         pglGetIntegerv(pname, params);
2159         break;
2160     }
2161     wine_tsx11_unlock();
2162 }
2163
2164 void flush_gl_drawable(X11DRV_PDEVICE *physDev)
2165 {
2166     int w, h;
2167
2168     if (!physDev->gl_copy)
2169         return;
2170
2171     w = physDev->dc_rect.right - physDev->dc_rect.left;
2172     h = physDev->dc_rect.bottom - physDev->dc_rect.top;
2173
2174     if(w > 0 && h > 0) {
2175         Drawable src = physDev->pixmap;
2176         if(!src) src = physDev->gl_drawable;
2177
2178         /* The GL drawable may be lagged behind if we don't flush first, so
2179          * flush the display make sure we copy up-to-date data */
2180         wine_tsx11_lock();
2181         XFlush(gdi_display);
2182         XSetFunction(gdi_display, physDev->gc, GXcopy);
2183         XCopyArea(gdi_display, src, physDev->drawable, physDev->gc, 0, 0, w, h,
2184                   physDev->dc_rect.left, physDev->dc_rect.top);
2185         wine_tsx11_unlock();
2186     }
2187 }
2188
2189
2190 static void WINAPI X11DRV_wglFinish(void)
2191 {
2192     Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2193     enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
2194
2195     wine_tsx11_lock();
2196     sync_context(ctx);
2197     pglFinish();
2198     wine_tsx11_unlock();
2199     if (ctx) ExtEscape(ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2200 }
2201
2202 static void WINAPI X11DRV_wglFlush(void)
2203 {
2204     Wine_GLContext *ctx = NtCurrentTeb()->glContext;
2205     enum x11drv_escape_codes code = X11DRV_FLUSH_GL_DRAWABLE;
2206
2207     wine_tsx11_lock();
2208     sync_context(ctx);
2209     pglFlush();
2210     wine_tsx11_unlock();
2211     if (ctx) ExtEscape(ctx->hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2212 }
2213
2214 /**
2215  * X11DRV_wglCreateContextAttribsARB
2216  *
2217  * WGL_ARB_create_context: wglCreateContextAttribsARB
2218  */
2219 HGLRC X11DRV_wglCreateContextAttribsARB(X11DRV_PDEVICE *physDev, HGLRC hShareContext, const int* attribList)
2220 {
2221     Wine_GLContext *ret;
2222     WineGLPixelFormat *fmt;
2223     int hdcPF = physDev->current_pf;
2224     int fmt_count = 0;
2225
2226     TRACE("(%p %p %p)\n", physDev, hShareContext, attribList);
2227
2228     if (!has_opengl()) return 0;
2229
2230     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, hdcPF, TRUE /* Offscreen */, &fmt_count);
2231     /* wglCreateContextAttribsARB supports ALL pixel formats, so also offscreen ones.
2232      * If this fails something is very wrong on the system. */
2233     if(!fmt)
2234     {
2235         ERR("Cannot get FB Config for iPixelFormat %d, expect problems!\n", hdcPF);
2236         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2237         return NULL;
2238     }
2239
2240     wine_tsx11_lock();
2241     ret = alloc_context();
2242     wine_tsx11_unlock();
2243     ret->hdc = physDev->hdc;
2244     ret->fmt = fmt;
2245     ret->vis = NULL; /* glXCreateContextAttribsARB requires a fbconfig instead of a visual */
2246     ret->gl3_context = TRUE;
2247
2248     ret->numAttribs = 0;
2249     if(attribList)
2250     {
2251         int *pAttribList = (int*)attribList;
2252         int *pContextAttribList = &ret->attribList[0];
2253         /* attribList consists of pairs {token, value] terminated with 0 */
2254         while(pAttribList[0] != 0)
2255         {
2256             TRACE("%#x %#x\n", pAttribList[0], pAttribList[1]);
2257             switch(pAttribList[0])
2258             {
2259                 case WGL_CONTEXT_MAJOR_VERSION_ARB:
2260                     pContextAttribList[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
2261                     pContextAttribList[1] = pAttribList[1];
2262                     break;
2263                 case WGL_CONTEXT_MINOR_VERSION_ARB:
2264                     pContextAttribList[0] = GLX_CONTEXT_MINOR_VERSION_ARB;
2265                     pContextAttribList[1] = pAttribList[1];
2266                     break;
2267                 case WGL_CONTEXT_LAYER_PLANE_ARB:
2268                     break;
2269                 case WGL_CONTEXT_FLAGS_ARB:
2270                     pContextAttribList[0] = GLX_CONTEXT_FLAGS_ARB;
2271                     pContextAttribList[1] = pAttribList[1];
2272                     break;
2273                 case WGL_CONTEXT_PROFILE_MASK_ARB:
2274                     pContextAttribList[0] = GLX_CONTEXT_PROFILE_MASK_ARB;
2275                     pContextAttribList[1] = pAttribList[1];
2276                     break;
2277                 default:
2278                     ERR("Unhandled attribList pair: %#x %#x\n", pAttribList[0], pAttribList[1]);
2279             }
2280
2281             ret->numAttribs++;
2282             pAttribList += 2;
2283             pContextAttribList += 2;
2284         }
2285     }
2286
2287     wine_tsx11_lock();
2288     X11DRV_expect_error(gdi_display, GLXErrorHandler, NULL);
2289     ret->ctx = create_glxcontext(gdi_display, ret, NULL);
2290
2291     XSync(gdi_display, False);
2292     if(X11DRV_check_error() || !ret->ctx)
2293     {
2294         /* In the future we should convert the GLX error to a win32 one here if needed */
2295         ERR("Context creation failed\n");
2296         free_context(ret);
2297         wine_tsx11_unlock();
2298         return NULL;
2299     }
2300
2301     wine_tsx11_unlock();
2302     TRACE(" creating context %p\n", ret);
2303     return (HGLRC) ret;
2304 }
2305
2306 /**
2307  * X11DRV_wglGetExtensionsStringARB
2308  *
2309  * WGL_ARB_extensions_string: wglGetExtensionsStringARB
2310  */
2311 static const char * WINAPI X11DRV_wglGetExtensionsStringARB(HDC hdc) {
2312     TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
2313     return WineGLInfo.wglExtensions;
2314 }
2315
2316 /**
2317  * X11DRV_wglCreatePbufferARB
2318  *
2319  * WGL_ARB_pbuffer: wglCreatePbufferARB
2320  */
2321 static HPBUFFERARB WINAPI X11DRV_wglCreatePbufferARB(HDC hdc, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList)
2322 {
2323     Wine_GLPBuffer* object = NULL;
2324     WineGLPixelFormat *fmt = NULL;
2325     int nCfgs = 0;
2326     int attribs[256];
2327     int nAttribs = 0;
2328
2329     TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
2330
2331     if (0 >= iPixelFormat) {
2332         ERR("(%p): unexpected iPixelFormat(%d) <= 0, returns NULL\n", hdc, iPixelFormat);
2333         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2334         return NULL; /* unexpected error */
2335     }
2336
2337     /* Convert the WGL pixelformat to a GLX format, if it fails then the format is invalid */
2338     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nCfgs);
2339     if(!fmt) {
2340         ERR("(%p): unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, iPixelFormat, nCfgs);
2341         SetLastError(ERROR_INVALID_PIXEL_FORMAT);
2342         goto create_failed; /* unexpected error */
2343     }
2344
2345     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLPBuffer));
2346     if (NULL == object) {
2347         SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2348         goto create_failed; /* unexpected error */
2349     }
2350     object->hdc = hdc;
2351     object->display = gdi_display;
2352     object->width = iWidth;
2353     object->height = iHeight;
2354     object->fmt = fmt;
2355
2356     PUSH2(attribs, GLX_PBUFFER_WIDTH,  iWidth);
2357     PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight); 
2358     while (piAttribList && 0 != *piAttribList) {
2359         int attr_v;
2360         switch (*piAttribList) {
2361             case WGL_PBUFFER_LARGEST_ARB: {
2362                 ++piAttribList;
2363                 attr_v = *piAttribList;
2364                 TRACE("WGL_LARGEST_PBUFFER_ARB = %d\n", attr_v);
2365                 PUSH2(attribs, GLX_LARGEST_PBUFFER, attr_v);
2366                 break;
2367             }
2368
2369             case WGL_TEXTURE_FORMAT_ARB: {
2370                 ++piAttribList;
2371                 attr_v = *piAttribList;
2372                 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
2373                 if (use_render_texture_ati) {
2374                     int type = 0;
2375                     switch (attr_v) {
2376                         case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2377                         case WGL_TEXTURE_RGB_ARB: type = GLX_TEXTURE_RGB_ATI; break ;
2378                         case WGL_TEXTURE_RGBA_ARB: type = GLX_TEXTURE_RGBA_ATI; break ;
2379                         default:
2380                             SetLastError(ERROR_INVALID_DATA);
2381                             goto create_failed;
2382                     }
2383                     object->use_render_texture = 1;
2384                     PUSH2(attribs, GLX_TEXTURE_FORMAT_ATI, type);
2385                 } else {
2386                     if (WGL_NO_TEXTURE_ARB == attr_v) {
2387                         object->use_render_texture = 0;
2388                     } else {
2389                         if (!use_render_texture_emulation) {
2390                             SetLastError(ERROR_INVALID_DATA);
2391                             goto create_failed;
2392                         }
2393                         switch (attr_v) {
2394                             case WGL_TEXTURE_RGB_ARB:
2395                                 object->use_render_texture = GL_RGB;
2396                                 object->texture_bpp = 3;
2397                                 object->texture_format = GL_RGB;
2398                                 object->texture_type = GL_UNSIGNED_BYTE;
2399                                 break;
2400                             case WGL_TEXTURE_RGBA_ARB:
2401                                 object->use_render_texture = GL_RGBA;
2402                                 object->texture_bpp = 4;
2403                                 object->texture_format = GL_RGBA;
2404                                 object->texture_type = GL_UNSIGNED_BYTE;
2405                                 break;
2406
2407                             /* WGL_FLOAT_COMPONENTS_NV */
2408                             case WGL_TEXTURE_FLOAT_R_NV:
2409                                 object->use_render_texture = GL_FLOAT_R_NV;
2410                                 object->texture_bpp = 4;
2411                                 object->texture_format = GL_RED;
2412                                 object->texture_type = GL_FLOAT;
2413                                 break;
2414                             case WGL_TEXTURE_FLOAT_RG_NV:
2415                                 object->use_render_texture = GL_FLOAT_RG_NV;
2416                                 object->texture_bpp = 8;
2417                                 object->texture_format = GL_LUMINANCE_ALPHA;
2418                                 object->texture_type = GL_FLOAT;
2419                                 break;
2420                             case WGL_TEXTURE_FLOAT_RGB_NV:
2421                                 object->use_render_texture = GL_FLOAT_RGB_NV;
2422                                 object->texture_bpp = 12;
2423                                 object->texture_format = GL_RGB;
2424                                 object->texture_type = GL_FLOAT;
2425                                 break;
2426                             case WGL_TEXTURE_FLOAT_RGBA_NV:
2427                                 object->use_render_texture = GL_FLOAT_RGBA_NV;
2428                                 object->texture_bpp = 16;
2429                                 object->texture_format = GL_RGBA;
2430                                 object->texture_type = GL_FLOAT;
2431                                 break;
2432                             default:
2433                                 ERR("Unknown texture format: %x\n", attr_v);
2434                                 SetLastError(ERROR_INVALID_DATA);
2435                                 goto create_failed;
2436                         }
2437                     }
2438                 }
2439                 break;
2440             }
2441
2442             case WGL_TEXTURE_TARGET_ARB: {
2443                 ++piAttribList;
2444                 attr_v = *piAttribList;
2445                 TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
2446                 if (use_render_texture_ati) {
2447                     int type = 0;
2448                     switch (attr_v) {
2449                         case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
2450                         case WGL_TEXTURE_CUBE_MAP_ARB: type = GLX_TEXTURE_CUBE_MAP_ATI; break ;
2451                         case WGL_TEXTURE_1D_ARB: type = GLX_TEXTURE_1D_ATI; break ;
2452                         case WGL_TEXTURE_2D_ARB: type = GLX_TEXTURE_2D_ATI; break ;
2453                         default:
2454                             SetLastError(ERROR_INVALID_DATA);
2455                             goto create_failed;
2456                     }
2457                     PUSH2(attribs, GLX_TEXTURE_TARGET_ATI, type);
2458                 } else {
2459                     if (WGL_NO_TEXTURE_ARB == attr_v) {
2460                         object->texture_target = 0;
2461                     } else {
2462                         if (!use_render_texture_emulation) {
2463                             SetLastError(ERROR_INVALID_DATA);
2464                             goto create_failed;
2465                         }
2466                         switch (attr_v) {
2467                             case WGL_TEXTURE_CUBE_MAP_ARB: {
2468                                 if (iWidth != iHeight) {
2469                                     SetLastError(ERROR_INVALID_DATA);
2470                                     goto create_failed;
2471                                 }
2472                                 object->texture_target = GL_TEXTURE_CUBE_MAP;
2473                                 object->texture_bind_target = GL_TEXTURE_BINDING_CUBE_MAP;
2474                                break;
2475                             }
2476                             case WGL_TEXTURE_1D_ARB: {
2477                                 if (1 != iHeight) {
2478                                     SetLastError(ERROR_INVALID_DATA);
2479                                     goto create_failed;
2480                                 }
2481                                 object->texture_target = GL_TEXTURE_1D;
2482                                 object->texture_bind_target = GL_TEXTURE_BINDING_1D;
2483                                 break;
2484                             }
2485                             case WGL_TEXTURE_2D_ARB: {
2486                                 object->texture_target = GL_TEXTURE_2D;
2487                                 object->texture_bind_target = GL_TEXTURE_BINDING_2D;
2488                                 break;
2489                             }
2490                             case WGL_TEXTURE_RECTANGLE_NV: {
2491                                 object->texture_target = GL_TEXTURE_RECTANGLE_NV;
2492                                 object->texture_bind_target = GL_TEXTURE_BINDING_RECTANGLE_NV;
2493                                 break;
2494                             }
2495                             default:
2496                                 ERR("Unknown texture target: %x\n", attr_v);
2497                                 SetLastError(ERROR_INVALID_DATA);
2498                                 goto create_failed;
2499                         }
2500                     }
2501                 }
2502                 break;
2503             }
2504
2505             case WGL_MIPMAP_TEXTURE_ARB: {
2506                 ++piAttribList;
2507                 attr_v = *piAttribList;
2508                 TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
2509                 if (use_render_texture_ati) {
2510                     PUSH2(attribs, GLX_MIPMAP_TEXTURE_ATI, attr_v);
2511                 } else {
2512                     if (!use_render_texture_emulation) {
2513                         SetLastError(ERROR_INVALID_DATA);
2514                         goto create_failed;
2515                     }
2516                 }
2517                 break;
2518             }
2519         }
2520         ++piAttribList;
2521     }
2522
2523     PUSH1(attribs, None);
2524     wine_tsx11_lock();
2525     object->drawable = pglXCreatePbuffer(gdi_display, fmt->fbconfig, attribs);
2526     wine_tsx11_unlock();
2527     TRACE("new Pbuffer drawable as %p\n", (void*) object->drawable);
2528     if (!object->drawable) {
2529         SetLastError(ERROR_NO_SYSTEM_RESOURCES);
2530         goto create_failed; /* unexpected error */
2531     }
2532     TRACE("->(%p)\n", object);
2533     return object;
2534
2535 create_failed:
2536     HeapFree(GetProcessHeap(), 0, object);
2537     TRACE("->(FAILED)\n");
2538     return NULL;
2539 }
2540
2541 /**
2542  * X11DRV_wglDestroyPbufferARB
2543  *
2544  * WGL_ARB_pbuffer: wglDestroyPbufferARB
2545  */
2546 static GLboolean WINAPI X11DRV_wglDestroyPbufferARB(HPBUFFERARB hPbuffer)
2547 {
2548     Wine_GLPBuffer* object = hPbuffer;
2549     TRACE("(%p)\n", hPbuffer);
2550     if (NULL == object) {
2551         SetLastError(ERROR_INVALID_HANDLE);
2552         return GL_FALSE;
2553     }
2554     wine_tsx11_lock();
2555     pglXDestroyPbuffer(object->display, object->drawable);
2556     wine_tsx11_unlock();
2557     HeapFree(GetProcessHeap(), 0, object);
2558     return GL_TRUE;
2559 }
2560
2561 /**
2562  * X11DRV_wglGetPbufferDCARB
2563  *
2564  * WGL_ARB_pbuffer: wglGetPbufferDCARB
2565  * The function wglGetPbufferDCARB returns a device context for a pbuffer.
2566  * Gdi32 implements the part of this function which creates a device context.
2567  * This part associates the physDev with the X drawable of the pbuffer.
2568  */
2569 HDC CDECL X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *physDev, HPBUFFERARB hPbuffer)
2570 {
2571     Wine_GLPBuffer* object = hPbuffer;
2572     if (NULL == object) {
2573         SetLastError(ERROR_INVALID_HANDLE);
2574         return NULL;
2575     }
2576
2577     /* The function wglGetPbufferDCARB returns a DC to which the pbuffer can be connected.
2578      * All formats in our pixelformat list are compatible with each other and the main drawable. */
2579     physDev->current_pf = object->fmt->iPixelFormat;
2580     physDev->drawable = object->drawable;
2581     SetRect( &physDev->drawable_rect, 0, 0, object->width, object->height );
2582     physDev->dc_rect = physDev->drawable_rect;
2583
2584     TRACE("(%p)->(%p)\n", hPbuffer, physDev->hdc);
2585     return physDev->hdc;
2586 }
2587
2588 /**
2589  * X11DRV_wglQueryPbufferARB
2590  *
2591  * WGL_ARB_pbuffer: wglQueryPbufferARB
2592  */
2593 static GLboolean WINAPI X11DRV_wglQueryPbufferARB(HPBUFFERARB hPbuffer, int iAttribute, int *piValue)
2594 {
2595     Wine_GLPBuffer* object = hPbuffer;
2596     TRACE("(%p, 0x%x, %p)\n", hPbuffer, iAttribute, piValue);
2597     if (NULL == object) {
2598         SetLastError(ERROR_INVALID_HANDLE);
2599         return GL_FALSE;
2600     }
2601     switch (iAttribute) {
2602         case WGL_PBUFFER_WIDTH_ARB:
2603             wine_tsx11_lock();
2604             pglXQueryDrawable(object->display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
2605             wine_tsx11_unlock();
2606             break;
2607         case WGL_PBUFFER_HEIGHT_ARB:
2608             wine_tsx11_lock();
2609             pglXQueryDrawable(object->display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
2610             wine_tsx11_unlock();
2611             break;
2612
2613         case WGL_PBUFFER_LOST_ARB:
2614             /* GLX Pbuffers cannot be lost by default. We can support this by
2615              * setting GLX_PRESERVED_CONTENTS to False and using glXSelectEvent
2616              * to receive pixel buffer clobber events, however that may or may
2617              * not give any benefit */
2618             *piValue = GL_FALSE;
2619             break;
2620
2621         case WGL_TEXTURE_FORMAT_ARB:
2622             if (use_render_texture_ati) {
2623                 unsigned int tmp;
2624                 int type = WGL_NO_TEXTURE_ARB;
2625                 wine_tsx11_lock();
2626                 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_FORMAT_ATI, &tmp);
2627                 wine_tsx11_unlock();
2628                 switch (tmp) {
2629                     case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2630                     case GLX_TEXTURE_RGB_ATI: type = WGL_TEXTURE_RGB_ARB; break ;
2631                     case GLX_TEXTURE_RGBA_ATI: type = WGL_TEXTURE_RGBA_ARB; break ;
2632                 }
2633                 *piValue = type;
2634             } else {
2635                 if (!object->use_render_texture) {
2636                     *piValue = WGL_NO_TEXTURE_ARB;
2637                 } else {
2638                     if (!use_render_texture_emulation) {
2639                         SetLastError(ERROR_INVALID_HANDLE);
2640                         return GL_FALSE;
2641                     }
2642                     switch(object->use_render_texture) {
2643                         case GL_RGB:
2644                             *piValue = WGL_TEXTURE_RGB_ARB;
2645                             break;
2646                         case GL_RGBA:
2647                             *piValue = WGL_TEXTURE_RGBA_ARB;
2648                             break;
2649                         /* WGL_FLOAT_COMPONENTS_NV */
2650                         case GL_FLOAT_R_NV:
2651                             *piValue = WGL_TEXTURE_FLOAT_R_NV;
2652                             break;
2653                         case GL_FLOAT_RG_NV:
2654                             *piValue = WGL_TEXTURE_FLOAT_RG_NV;
2655                             break;
2656                         case GL_FLOAT_RGB_NV:
2657                             *piValue = WGL_TEXTURE_FLOAT_RGB_NV;
2658                             break;
2659                         case GL_FLOAT_RGBA_NV:
2660                             *piValue = WGL_TEXTURE_FLOAT_RGBA_NV;
2661                             break;
2662                         default:
2663                             ERR("Unknown texture format: %x\n", object->use_render_texture);
2664                     }
2665                 }
2666             }
2667             break;
2668
2669         case WGL_TEXTURE_TARGET_ARB:
2670             if (use_render_texture_ati) {
2671                 unsigned int tmp;
2672                 int type = WGL_NO_TEXTURE_ARB;
2673                 wine_tsx11_lock();
2674                 pglXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_TARGET_ATI, &tmp);
2675                 wine_tsx11_unlock();
2676                 switch (tmp) {
2677                     case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
2678                     case GLX_TEXTURE_CUBE_MAP_ATI: type = WGL_TEXTURE_CUBE_MAP_ARB; break ;
2679                     case GLX_TEXTURE_1D_ATI: type = WGL_TEXTURE_1D_ARB; break ;
2680                     case GLX_TEXTURE_2D_ATI: type = WGL_TEXTURE_2D_ARB; break ;
2681                 }
2682                 *piValue = type;
2683             } else {
2684             if (!object->texture_target) {
2685                 *piValue = WGL_NO_TEXTURE_ARB;
2686             } else {
2687                 if (!use_render_texture_emulation) {
2688                     SetLastError(ERROR_INVALID_DATA);      
2689                     return GL_FALSE;
2690                 }
2691                 switch (object->texture_target) {
2692                     case GL_TEXTURE_1D:       *piValue = WGL_TEXTURE_1D_ARB; break;
2693                     case GL_TEXTURE_2D:       *piValue = WGL_TEXTURE_2D_ARB; break;
2694                     case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
2695                     case GL_TEXTURE_RECTANGLE_NV: *piValue = WGL_TEXTURE_RECTANGLE_NV; break;
2696                 }
2697             }
2698         }
2699         break;
2700
2701     case WGL_MIPMAP_TEXTURE_ARB:
2702         if (use_render_texture_ati) {
2703             wine_tsx11_lock();
2704             pglXQueryDrawable(object->display, object->drawable, GLX_MIPMAP_TEXTURE_ATI, (unsigned int*) piValue);
2705             wine_tsx11_unlock();
2706         } else {
2707             *piValue = GL_FALSE; /** don't support that */
2708             FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
2709         }
2710         break;
2711
2712     default:
2713         FIXME("unexpected attribute %x\n", iAttribute);
2714         break;
2715     }
2716
2717     return GL_TRUE;
2718 }
2719
2720 /**
2721  * X11DRV_wglReleasePbufferDCARB
2722  *
2723  * WGL_ARB_pbuffer: wglReleasePbufferDCARB
2724  */
2725 static int WINAPI X11DRV_wglReleasePbufferDCARB(HPBUFFERARB hPbuffer, HDC hdc)
2726 {
2727     TRACE("(%p, %p)\n", hPbuffer, hdc);
2728     return DeleteDC(hdc);
2729 }
2730
2731 /**
2732  * X11DRV_wglSetPbufferAttribARB
2733  *
2734  * WGL_ARB_pbuffer: wglSetPbufferAttribARB
2735  */
2736 static GLboolean WINAPI X11DRV_wglSetPbufferAttribARB(HPBUFFERARB hPbuffer, const int *piAttribList)
2737 {
2738     Wine_GLPBuffer* object = hPbuffer;
2739     GLboolean ret = GL_FALSE;
2740
2741     WARN("(%p, %p): alpha-testing, report any problem\n", hPbuffer, piAttribList);
2742     if (NULL == object) {
2743         SetLastError(ERROR_INVALID_HANDLE);
2744         return GL_FALSE;
2745     }
2746     if (!object->use_render_texture) {
2747         SetLastError(ERROR_INVALID_HANDLE);
2748         return GL_FALSE;
2749     }
2750     if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
2751         return GL_TRUE;
2752     }
2753     if (NULL != pglXDrawableAttribATI) {
2754         if (use_render_texture_ati) {
2755             FIXME("Need conversion for GLX_ATI_render_texture\n");
2756         }
2757         wine_tsx11_lock();
2758         ret = pglXDrawableAttribATI(object->display, object->drawable, piAttribList);
2759         wine_tsx11_unlock();
2760     }
2761     return ret;
2762 }
2763
2764 /**
2765  * X11DRV_wglChoosePixelFormatARB
2766  *
2767  * WGL_ARB_pixel_format: wglChoosePixelFormatARB
2768  */
2769 static GLboolean WINAPI X11DRV_wglChoosePixelFormatARB(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats)
2770 {
2771     int gl_test = 0;
2772     int attribs[256];
2773     int nAttribs = 0;
2774     GLXFBConfig* cfgs = NULL;
2775     int nCfgs = 0;
2776     int it;
2777     int fmt_id;
2778     WineGLPixelFormat *fmt;
2779     UINT pfmt_it = 0;
2780     int run;
2781     int i;
2782     DWORD dwFlags = 0;
2783
2784     TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
2785     if (NULL != pfAttribFList) {
2786         FIXME("unused pfAttribFList\n");
2787     }
2788
2789     nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
2790     if (-1 == nAttribs) {
2791         WARN("Cannot convert WGL to GLX attributes\n");
2792         return GL_FALSE;
2793     }
2794     PUSH1(attribs, None);
2795
2796     /* There is no 1:1 mapping between GLX and WGL formats because we duplicate some GLX formats for bitmap rendering (see get_formats).
2797      * Flags like PFD_SUPPORT_GDI, PFD_DRAW_TO_BITMAP and others are a property of the WineGLPixelFormat. We don't query these attributes
2798      * using glXChooseFBConfig but we filter the result of glXChooseFBConfig later on by passing a dwFlags to 'ConvertPixelFormatGLXtoWGL'. */
2799     for(i=0; piAttribIList[i] != 0; i+=2)
2800     {
2801         switch(piAttribIList[i])
2802         {
2803             case WGL_DRAW_TO_BITMAP_ARB:
2804                 if(piAttribIList[i+1])
2805                     dwFlags |= PFD_DRAW_TO_BITMAP;
2806                 break;
2807             case WGL_ACCELERATION_ARB:
2808                 switch(piAttribIList[i+1])
2809                 {
2810                     case WGL_NO_ACCELERATION_ARB:
2811                         dwFlags |= PFD_GENERIC_FORMAT;
2812                         break;
2813                     case WGL_GENERIC_ACCELERATION_ARB:
2814                         dwFlags |= PFD_GENERIC_ACCELERATED;
2815                         break;
2816                     case WGL_FULL_ACCELERATION_ARB:
2817                         /* Nothing to do */
2818                         break;
2819                 }
2820                 break;
2821             case WGL_SUPPORT_GDI_ARB:
2822                 if(piAttribIList[i+1])
2823                     dwFlags |= PFD_SUPPORT_GDI;
2824                 break;
2825         }
2826     }
2827
2828     /* Search for FB configurations matching the requirements in attribs */
2829     wine_tsx11_lock();
2830     cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), attribs, &nCfgs);
2831     if (NULL == cfgs) {
2832         wine_tsx11_unlock();
2833         WARN("Compatible Pixel Format not found\n");
2834         return GL_FALSE;
2835     }
2836
2837     /* Loop through all matching formats and check if they are suitable.
2838     * Note that this function should at max return nMaxFormats different formats */
2839     for(run=0; run < 2; run++)
2840     {
2841         for (it = 0; it < nCfgs; ++it) {
2842             gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id);
2843             if (gl_test) {
2844                 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
2845                 continue;
2846             }
2847
2848             /* Search for the format in our list of compatible formats */
2849             fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fmt_id, dwFlags);
2850             if(!fmt)
2851                 continue;
2852
2853             /* During the first run we only want onscreen formats and during the second only offscreen 'XOR' */
2854             if( ((run == 0) && fmt->offscreenOnly) || ((run == 1) && !fmt->offscreenOnly) )
2855                 continue;
2856
2857             if(pfmt_it < nMaxFormats) {
2858                 piFormats[pfmt_it] = fmt->iPixelFormat;
2859                 TRACE("at %d/%d found FBCONFIG_ID 0x%x (%d)\n", it + 1, nCfgs, fmt_id, piFormats[pfmt_it]);
2860             }
2861             pfmt_it++;
2862         }
2863     }
2864
2865     *nNumFormats = pfmt_it;
2866     /** free list */
2867     XFree(cfgs);
2868     wine_tsx11_unlock();
2869     return GL_TRUE;
2870 }
2871
2872 /**
2873  * X11DRV_wglGetPixelFormatAttribivARB
2874  *
2875  * WGL_ARB_pixel_format: wglGetPixelFormatAttribivARB
2876  */
2877 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribivARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues)
2878 {
2879     UINT i;
2880     WineGLPixelFormat *fmt = NULL;
2881     int hTest;
2882     int tmp;
2883     int curGLXAttr = 0;
2884     int nWGLFormats = 0;
2885
2886     TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
2887
2888     if (0 < iLayerPlane) {
2889         FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
2890         return GL_FALSE;
2891     }
2892
2893     /* Convert the WGL pixelformat to a GLX one, if this fails then most likely the iPixelFormat isn't supported.
2894     * We don't have to fail yet as a program can specify an invalid iPixelFormat (lets say 0) if it wants to query
2895     * the number of supported WGL formats. Whether the iPixelFormat is valid is handled in the for-loop below. */
2896     fmt = ConvertPixelFormatWGLtoGLX(gdi_display, iPixelFormat, TRUE /* Offscreen */, &nWGLFormats);
2897     if(!fmt) {
2898         WARN("Unable to convert iPixelFormat %d to a GLX one!\n", iPixelFormat);
2899     }
2900
2901     wine_tsx11_lock();
2902     for (i = 0; i < nAttributes; ++i) {
2903         const int curWGLAttr = piAttributes[i];
2904         TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
2905
2906         switch (curWGLAttr) {
2907             case WGL_NUMBER_PIXEL_FORMATS_ARB:
2908                 piValues[i] = nWGLFormats; 
2909                 continue;
2910
2911             case WGL_SUPPORT_OPENGL_ARB:
2912                 piValues[i] = GL_TRUE; 
2913                 continue;
2914
2915             case WGL_ACCELERATION_ARB:
2916                 curGLXAttr = GLX_CONFIG_CAVEAT;
2917                 if (!fmt) goto pix_error;
2918                 if(fmt->dwFlags & PFD_GENERIC_FORMAT)
2919                     piValues[i] = WGL_NO_ACCELERATION_ARB;
2920                 else if(fmt->dwFlags & PFD_GENERIC_ACCELERATED)
2921                     piValues[i] = WGL_GENERIC_ACCELERATION_ARB;
2922                 else
2923                     piValues[i] = WGL_FULL_ACCELERATION_ARB;
2924                 continue;
2925
2926             case WGL_TRANSPARENT_ARB:
2927                 curGLXAttr = GLX_TRANSPARENT_TYPE;
2928                 if (!fmt) goto pix_error;
2929                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2930                 if (hTest) goto get_error;
2931                     piValues[i] = GL_FALSE;
2932                 if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
2933                     continue;
2934
2935             case WGL_PIXEL_TYPE_ARB:
2936                 curGLXAttr = GLX_RENDER_TYPE;
2937                 if (!fmt) goto pix_error;
2938                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2939                 if (hTest) goto get_error;
2940                 TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
2941                 if      (tmp & GLX_RGBA_BIT)           { piValues[i] = WGL_TYPE_RGBA_ARB; }
2942                 else if (tmp & GLX_COLOR_INDEX_BIT)    { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
2943                 else if (tmp & GLX_RGBA_FLOAT_BIT)     { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2944                 else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
2945                 else if (tmp & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) { piValues[i] = WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT; }
2946                 else {
2947                     ERR("unexpected RenderType(%x)\n", tmp);
2948                     piValues[i] = WGL_TYPE_RGBA_ARB;
2949                 }
2950                 continue;
2951
2952             case WGL_COLOR_BITS_ARB:
2953                 curGLXAttr = GLX_BUFFER_SIZE;
2954                 break;
2955
2956             case WGL_BIND_TO_TEXTURE_RGB_ARB:
2957                 if (use_render_texture_ati) {
2958                     curGLXAttr = GLX_BIND_TO_TEXTURE_RGB_ATI;
2959                     break;
2960                 }
2961             case WGL_BIND_TO_TEXTURE_RGBA_ARB:
2962                 if (use_render_texture_ati) {
2963                     curGLXAttr = GLX_BIND_TO_TEXTURE_RGBA_ATI;
2964                     break;
2965                 }
2966                 if (!use_render_texture_emulation) {
2967                     piValues[i] = GL_FALSE;
2968                     continue;   
2969                 }
2970                 curGLXAttr = GLX_RENDER_TYPE;
2971                 if (!fmt) goto pix_error;
2972                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, &tmp);
2973                 if (hTest) goto get_error;
2974                 if (GLX_COLOR_INDEX_BIT == tmp) {
2975                     piValues[i] = GL_FALSE;  
2976                     continue;
2977                 }
2978                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
2979                 if (hTest) goto get_error;
2980                 piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
2981                 continue;
2982
2983             case WGL_BLUE_BITS_ARB:
2984                 curGLXAttr = GLX_BLUE_SIZE;
2985                 break;
2986             case WGL_RED_BITS_ARB:
2987                 curGLXAttr = GLX_RED_SIZE;
2988                 break;
2989             case WGL_GREEN_BITS_ARB:
2990                 curGLXAttr = GLX_GREEN_SIZE;
2991                 break;
2992             case WGL_ALPHA_BITS_ARB:
2993                 curGLXAttr = GLX_ALPHA_SIZE;
2994                 break;
2995             case WGL_DEPTH_BITS_ARB:
2996                 curGLXAttr = GLX_DEPTH_SIZE;
2997                 break;
2998             case WGL_STENCIL_BITS_ARB:
2999                 curGLXAttr = GLX_STENCIL_SIZE;
3000                 break;
3001             case WGL_DOUBLE_BUFFER_ARB:
3002                 curGLXAttr = GLX_DOUBLEBUFFER;
3003                 break;
3004             case WGL_STEREO_ARB:
3005                 curGLXAttr = GLX_STEREO;
3006                 break;
3007             case WGL_AUX_BUFFERS_ARB:
3008                 curGLXAttr = GLX_AUX_BUFFERS;
3009                 break;
3010
3011             case WGL_SUPPORT_GDI_ARB:
3012                 if (!fmt) goto pix_error;
3013                 piValues[i] = (fmt->dwFlags & PFD_SUPPORT_GDI) ? TRUE : FALSE;
3014                 continue;
3015
3016             case WGL_DRAW_TO_BITMAP_ARB:
3017                 if (!fmt) goto pix_error;
3018                 piValues[i] = (fmt->dwFlags & PFD_DRAW_TO_BITMAP) ? TRUE : FALSE;
3019                 continue;
3020
3021             case WGL_DRAW_TO_WINDOW_ARB:
3022             case WGL_DRAW_TO_PBUFFER_ARB:
3023                 if (!fmt) goto pix_error;
3024                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_DRAWABLE_TYPE, &tmp);
3025                 if (hTest) goto get_error;
3026                 if((curWGLAttr == WGL_DRAW_TO_WINDOW_ARB && (tmp&GLX_WINDOW_BIT)) ||
3027                    (curWGLAttr == WGL_DRAW_TO_PBUFFER_ARB && (tmp&GLX_PBUFFER_BIT)))
3028                     piValues[i] = GL_TRUE;
3029                 else
3030                     piValues[i] = GL_FALSE;
3031                 continue;
3032
3033             case WGL_SWAP_METHOD_ARB:
3034                 /* For now return SWAP_EXCHANGE_ARB which is the best type of buffer switch available.
3035                  * Later on we can also use GLX_OML_swap_method on drivers which support this. At this
3036                  * point only ATI offers this.
3037                  */
3038                 piValues[i] = WGL_SWAP_EXCHANGE_ARB;
3039                 break;
3040
3041             case WGL_PBUFFER_LARGEST_ARB:
3042                 curGLXAttr = GLX_LARGEST_PBUFFER;
3043                 break;
3044
3045             case WGL_SAMPLE_BUFFERS_ARB:
3046                 curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
3047                 break;
3048
3049             case WGL_SAMPLES_ARB:
3050                 curGLXAttr = GLX_SAMPLES_ARB;
3051                 break;
3052
3053             case WGL_FLOAT_COMPONENTS_NV:
3054                 curGLXAttr = GLX_FLOAT_COMPONENTS_NV;
3055                 break;
3056
3057             case WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT:
3058                 curGLXAttr = GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT;
3059                 break;
3060
3061             case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
3062                 curGLXAttr = GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT;
3063                 break;
3064
3065             case WGL_ACCUM_RED_BITS_ARB:
3066                 curGLXAttr = GLX_ACCUM_RED_SIZE;
3067                 break;
3068             case WGL_ACCUM_GREEN_BITS_ARB:
3069                 curGLXAttr = GLX_ACCUM_GREEN_SIZE;
3070                 break;
3071             case WGL_ACCUM_BLUE_BITS_ARB:
3072                 curGLXAttr = GLX_ACCUM_BLUE_SIZE;
3073                 break;
3074             case WGL_ACCUM_ALPHA_BITS_ARB:
3075                 curGLXAttr = GLX_ACCUM_ALPHA_SIZE;
3076                 break;
3077             case WGL_ACCUM_BITS_ARB:
3078                 if (!fmt) goto pix_error;
3079                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_RED_SIZE, &tmp);
3080                 if (hTest) goto get_error;
3081                 piValues[i] = tmp;
3082                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_GREEN_SIZE, &tmp);
3083                 if (hTest) goto get_error;
3084                 piValues[i] += tmp;
3085                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_BLUE_SIZE, &tmp);
3086                 if (hTest) goto get_error;
3087                 piValues[i] += tmp;
3088                 hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, GLX_ACCUM_ALPHA_SIZE, &tmp);
3089                 if (hTest) goto get_error;
3090                 piValues[i] += tmp;
3091                 continue;
3092
3093             default:
3094                 FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
3095         }
3096
3097         /* Retrieve a GLX FBConfigAttrib when the attribute to query is valid and
3098          * iPixelFormat != 0. When iPixelFormat is 0 the only value which makes
3099          * sense to query is WGL_NUMBER_PIXEL_FORMATS_ARB.
3100          *
3101          * TODO: properly test the behavior of wglGetPixelFormatAttrib*v on Windows
3102          *       and check which options can work using iPixelFormat=0 and which not.
3103          *       A problem would be that this function is an extension. This would
3104          *       mean that the behavior could differ between different vendors (ATI, Nvidia, ..).
3105          */
3106         if (0 != curGLXAttr && iPixelFormat != 0) {
3107             if (!fmt) goto pix_error;
3108             hTest = pglXGetFBConfigAttrib(gdi_display, fmt->fbconfig, curGLXAttr, piValues + i);
3109             if (hTest) goto get_error;
3110             curGLXAttr = 0;
3111         } else { 
3112             piValues[i] = GL_FALSE; 
3113         }
3114     }
3115     wine_tsx11_unlock();
3116     return GL_TRUE;
3117
3118 get_error:
3119     wine_tsx11_unlock();
3120     ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
3121     return GL_FALSE;
3122
3123 pix_error:
3124     wine_tsx11_unlock();
3125     ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nWGLFormats);
3126     return GL_FALSE;
3127 }
3128
3129 /**
3130  * X11DRV_wglGetPixelFormatAttribfvARB
3131  *
3132  * WGL_ARB_pixel_format: wglGetPixelFormatAttribfvARB
3133  */
3134 static GLboolean WINAPI X11DRV_wglGetPixelFormatAttribfvARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues)
3135 {
3136     int *attr;
3137     int ret;
3138     UINT i;
3139
3140     TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
3141
3142     /* Allocate a temporary array to store integer values */
3143     attr = HeapAlloc(GetProcessHeap(), 0, nAttributes * sizeof(int));
3144     if (!attr) {
3145         ERR("couldn't allocate %d array\n", nAttributes);
3146         return GL_FALSE;
3147     }
3148
3149     /* Piggy-back on wglGetPixelFormatAttribivARB */
3150     ret = X11DRV_wglGetPixelFormatAttribivARB(hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, attr);
3151     if (ret) {
3152         /* Convert integer values to float. Should also check for attributes
3153            that can give decimal values here */
3154         for (i=0; i<nAttributes;i++) {
3155             pfValues[i] = attr[i];
3156         }
3157     }
3158
3159     HeapFree(GetProcessHeap(), 0, attr);
3160     return ret;
3161 }
3162
3163 /**
3164  * X11DRV_wglBindTexImageARB
3165  *
3166  * WGL_ARB_render_texture: wglBindTexImageARB
3167  */
3168 static GLboolean WINAPI X11DRV_wglBindTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
3169 {
3170     Wine_GLPBuffer* object = hPbuffer;
3171     GLboolean ret = GL_FALSE;
3172
3173     TRACE("(%p, %d)\n", hPbuffer, iBuffer);
3174     if (NULL == object) {
3175         SetLastError(ERROR_INVALID_HANDLE);
3176         return GL_FALSE;
3177     }
3178     if (!object->use_render_texture) {
3179         SetLastError(ERROR_INVALID_HANDLE);
3180         return GL_FALSE;
3181     }
3182
3183     if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
3184         static int init = 0;
3185         int prev_binded_texture = 0;
3186         GLXContext prev_context;
3187         Drawable prev_drawable;
3188         GLXContext tmp_context;
3189
3190         wine_tsx11_lock();
3191         prev_context = pglXGetCurrentContext();
3192         prev_drawable = pglXGetCurrentDrawable();
3193
3194         /* Our render_texture emulation is basic and lacks some features (1D/Cube support).
3195            This is mostly due to lack of demos/games using them. Further the use of glReadPixels
3196            isn't ideal performance wise but I wasn't able to get other ways working.
3197         */
3198         if(!init) {
3199             init = 1; /* Only show the FIXME once for performance reasons */
3200             FIXME("partial stub!\n");
3201         }
3202
3203         TRACE("drawable=%p, context=%p\n", (void*)object->drawable, prev_context);
3204         tmp_context = pglXCreateNewContext(gdi_display, object->fmt->fbconfig, object->fmt->render_type, prev_context, True);
3205
3206         pglGetIntegerv(object->texture_bind_target, &prev_binded_texture);
3207
3208         /* Switch to our pbuffer */
3209         pglXMakeCurrent(gdi_display, object->drawable, tmp_context);
3210
3211         /* Make sure that the prev_binded_texture is set as the current texture state isn't shared between contexts.
3212          * After that upload the pbuffer texture data. */
3213         pglBindTexture(object->texture_target, prev_binded_texture);
3214         pglCopyTexImage2D(object->texture_target, 0, object->use_render_texture, 0, 0, object->width, object->height, 0);
3215
3216         /* Switch back to the original drawable and upload the pbuffer-texture */
3217         pglXMakeCurrent(object->display, prev_drawable, prev_context);
3218         pglXDestroyContext(gdi_display, tmp_context);
3219         wine_tsx11_unlock();
3220         return GL_TRUE;
3221     }
3222
3223     if (NULL != pglXBindTexImageATI) {
3224         int buffer;
3225
3226         switch(iBuffer)
3227         {
3228             case WGL_FRONT_LEFT_ARB:
3229                 buffer = GLX_FRONT_LEFT_ATI;
3230                 break;
3231             case WGL_FRONT_RIGHT_ARB:
3232                 buffer = GLX_FRONT_RIGHT_ATI;
3233                 break;
3234             case WGL_BACK_LEFT_ARB:
3235                 buffer = GLX_BACK_LEFT_ATI;
3236                 break;
3237             case WGL_BACK_RIGHT_ARB:
3238                 buffer = GLX_BACK_RIGHT_ATI;
3239                 break;
3240             default:
3241                 ERR("Unknown iBuffer=%#x\n", iBuffer);
3242                 return FALSE;
3243         }
3244
3245         /* In the sample 'ogl_offscreen_rendering_3' from codesampler.net I get garbage on the screen.
3246          * I'm not sure if that's a bug in the ATI extension or in the program. I think that the program
3247          * expected a single buffering format since it didn't ask for double buffering. A buffer swap
3248          * fixed the program. I don't know what the correct behavior is. On the other hand that demo
3249          * works fine using our pbuffer emulation path.
3250          */
3251         wine_tsx11_lock();
3252         ret = pglXBindTexImageATI(object->display, object->drawable, buffer);
3253         wine_tsx11_unlock();
3254     }
3255     return ret;
3256 }
3257
3258 /**
3259  * X11DRV_wglReleaseTexImageARB
3260  *
3261  * WGL_ARB_render_texture: wglReleaseTexImageARB
3262  */
3263 static GLboolean WINAPI X11DRV_wglReleaseTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
3264 {
3265     Wine_GLPBuffer* object = hPbuffer;
3266     GLboolean ret = GL_FALSE;
3267
3268     TRACE("(%p, %d)\n", hPbuffer, iBuffer);
3269     if (NULL == object) {
3270         SetLastError(ERROR_INVALID_HANDLE);
3271         return GL_FALSE;
3272     }
3273     if (!object->use_render_texture) {
3274         SetLastError(ERROR_INVALID_HANDLE);
3275         return GL_FALSE;
3276     }
3277     if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
3278         return GL_TRUE;
3279     }
3280     if (NULL != pglXReleaseTexImageATI) {
3281         int buffer;
3282
3283         switch(iBuffer)
3284         {
3285             case WGL_FRONT_LEFT_ARB:
3286                 buffer = GLX_FRONT_LEFT_ATI;
3287                 break;
3288             case WGL_FRONT_RIGHT_ARB:
3289                 buffer = GLX_FRONT_RIGHT_ATI;
3290                 break;
3291             case WGL_BACK_LEFT_ARB:
3292                 buffer = GLX_BACK_LEFT_ATI;
3293                 break;
3294             case WGL_BACK_RIGHT_ARB:
3295                 buffer = GLX_BACK_RIGHT_ATI;
3296                 break;
3297             default:
3298                 ERR("Unknown iBuffer=%#x\n", iBuffer);
3299                 return FALSE;
3300         }
3301         wine_tsx11_lock();
3302         ret = pglXReleaseTexImageATI(object->display, object->drawable, buffer);
3303         wine_tsx11_unlock();
3304     }
3305     return ret;
3306 }
3307
3308 /**
3309  * X11DRV_wglGetExtensionsStringEXT
3310  *
3311  * WGL_EXT_extensions_string: wglGetExtensionsStringEXT
3312  */
3313 static const char * WINAPI X11DRV_wglGetExtensionsStringEXT(void) {
3314     TRACE("() returning \"%s\"\n", WineGLInfo.wglExtensions);
3315     return WineGLInfo.wglExtensions;
3316 }
3317
3318 /**
3319  * X11DRV_wglGetSwapIntervalEXT
3320  *
3321  * WGL_EXT_swap_control: wglGetSwapIntervalEXT
3322  */
3323 static int WINAPI X11DRV_wglGetSwapIntervalEXT(VOID) {
3324     FIXME("(),stub!\n");
3325     return swap_interval;
3326 }
3327
3328 /**
3329  * X11DRV_wglSwapIntervalEXT
3330  *
3331  * WGL_EXT_swap_control: wglSwapIntervalEXT
3332  */
3333 static BOOL WINAPI X11DRV_wglSwapIntervalEXT(int interval) {
3334     BOOL ret = TRUE;
3335
3336     TRACE("(%d)\n", interval);
3337     swap_interval = interval;
3338     if (NULL != pglXSwapIntervalSGI) {
3339         wine_tsx11_lock();
3340         ret = !pglXSwapIntervalSGI(interval);
3341         wine_tsx11_unlock();
3342     }
3343     else WARN("(): GLX_SGI_swap_control extension seems not supported\n");
3344     return ret;
3345 }
3346
3347 /**
3348  * X11DRV_wglAllocateMemoryNV
3349  *
3350  * WGL_NV_vertex_array_range: wglAllocateMemoryNV
3351  */
3352 static void* WINAPI X11DRV_wglAllocateMemoryNV(GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority) {
3353     void *ret = NULL;
3354     TRACE("(%d, %f, %f, %f)\n", size, readfreq, writefreq, priority );
3355
3356     if (pglXAllocateMemoryNV)
3357     {
3358         wine_tsx11_lock();
3359         ret = pglXAllocateMemoryNV(size, readfreq, writefreq, priority);
3360         wine_tsx11_unlock();
3361     }
3362     return ret;
3363 }
3364
3365 /**
3366  * X11DRV_wglFreeMemoryNV
3367  *
3368  * WGL_NV_vertex_array_range: wglFreeMemoryNV
3369  */
3370 static void WINAPI X11DRV_wglFreeMemoryNV(GLvoid* pointer) {
3371     TRACE("(%p)\n", pointer);
3372     if (pglXFreeMemoryNV == NULL)
3373         return;
3374
3375     wine_tsx11_lock();
3376     pglXFreeMemoryNV(pointer);
3377     wine_tsx11_unlock();
3378 }
3379
3380 /**
3381  * X11DRV_wglSetPixelFormatWINE
3382  *
3383  * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3384  * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3385  */
3386 BOOL CDECL X11DRV_wglSetPixelFormatWINE(X11DRV_PDEVICE *physDev, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
3387 {
3388     TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
3389
3390     if (!has_opengl()) return FALSE;
3391
3392     if (physDev->current_pf == iPixelFormat) return TRUE;
3393
3394     /* Relay to the core SetPixelFormat */
3395     TRACE("Changing iPixelFormat from %d to %d\n", physDev->current_pf, iPixelFormat);
3396     return internal_SetPixelFormat(physDev, iPixelFormat, ppfd);
3397 }
3398
3399 /**
3400  * glxRequireVersion (internal)
3401  *
3402  * Check if the supported GLX version matches requiredVersion.
3403  */
3404 static BOOL glxRequireVersion(int requiredVersion)
3405 {
3406     /* Both requiredVersion and glXVersion[1] contains the minor GLX version */
3407     if(requiredVersion <= WineGLInfo.glxVersion[1])
3408         return TRUE;
3409
3410     return FALSE;
3411 }
3412
3413 static BOOL glxRequireExtension(const char *requiredExtension)
3414 {
3415     if (strstr(WineGLInfo.glxExtensions, requiredExtension) == NULL) {
3416         return FALSE;
3417     }
3418
3419     return TRUE;
3420 }
3421
3422 static void register_extension_string(const char *ext)
3423 {
3424     if (WineGLInfo.wglExtensions[0])
3425         strcat(WineGLInfo.wglExtensions, " ");
3426     strcat(WineGLInfo.wglExtensions, ext);
3427
3428     TRACE("'%s'\n", ext);
3429 }
3430
3431 static BOOL register_extension(const WineGLExtension * ext)
3432 {
3433     int i;
3434
3435     assert( WineGLExtensionListSize < MAX_EXTENSIONS );
3436     WineGLExtensionList[WineGLExtensionListSize++] = ext;
3437
3438     register_extension_string(ext->extName);
3439
3440     for (i = 0; ext->extEntryPoints[i].funcName; ++i)
3441         TRACE("    - '%s'\n", ext->extEntryPoints[i].funcName);
3442
3443     return TRUE;
3444 }
3445
3446 static const WineGLExtension WGL_internal_functions =
3447 {
3448   "",
3449   {
3450     { "wglGetIntegerv", X11DRV_wglGetIntegerv },
3451     { "wglFinish", X11DRV_wglFinish },
3452     { "wglFlush", X11DRV_wglFlush },
3453   }
3454 };
3455
3456
3457 static const WineGLExtension WGL_ARB_create_context =
3458 {
3459   "WGL_ARB_create_context",
3460   {
3461     { "wglCreateContextAttribsARB", X11DRV_wglCreateContextAttribsARB },
3462   }
3463 };
3464
3465 static const WineGLExtension WGL_ARB_extensions_string =
3466 {
3467   "WGL_ARB_extensions_string",
3468   {
3469     { "wglGetExtensionsStringARB", X11DRV_wglGetExtensionsStringARB },
3470   }
3471 };
3472
3473 static const WineGLExtension WGL_ARB_make_current_read =
3474 {
3475   "WGL_ARB_make_current_read",
3476   {
3477     { "wglGetCurrentReadDCARB", X11DRV_wglGetCurrentReadDCARB },
3478     { "wglMakeContextCurrentARB", X11DRV_wglMakeContextCurrentARB },
3479   }
3480 };
3481
3482 static const WineGLExtension WGL_ARB_multisample =
3483 {
3484   "WGL_ARB_multisample",
3485 };
3486
3487 static const WineGLExtension WGL_ARB_pbuffer =
3488 {
3489   "WGL_ARB_pbuffer",
3490   {
3491     { "wglCreatePbufferARB", X11DRV_wglCreatePbufferARB },
3492     { "wglDestroyPbufferARB", X11DRV_wglDestroyPbufferARB },
3493     { "wglGetPbufferDCARB", X11DRV_wglGetPbufferDCARB },
3494     { "wglQueryPbufferARB", X11DRV_wglQueryPbufferARB },
3495     { "wglReleasePbufferDCARB", X11DRV_wglReleasePbufferDCARB },
3496     { "wglSetPbufferAttribARB", X11DRV_wglSetPbufferAttribARB },
3497   }
3498 };
3499
3500 static const WineGLExtension WGL_ARB_pixel_format =
3501 {
3502   "WGL_ARB_pixel_format",
3503   {
3504     { "wglChoosePixelFormatARB", X11DRV_wglChoosePixelFormatARB },
3505     { "wglGetPixelFormatAttribfvARB", X11DRV_wglGetPixelFormatAttribfvARB },
3506     { "wglGetPixelFormatAttribivARB", X11DRV_wglGetPixelFormatAttribivARB },
3507   }
3508 };
3509
3510 static const WineGLExtension WGL_ARB_render_texture =
3511 {
3512   "WGL_ARB_render_texture",
3513   {
3514     { "wglBindTexImageARB", X11DRV_wglBindTexImageARB },
3515     { "wglReleaseTexImageARB", X11DRV_wglReleaseTexImageARB },
3516   }
3517 };
3518
3519 static const WineGLExtension WGL_EXT_extensions_string =
3520 {
3521   "WGL_EXT_extensions_string",
3522   {
3523     { "wglGetExtensionsStringEXT", X11DRV_wglGetExtensionsStringEXT },
3524   }
3525 };
3526
3527 static const WineGLExtension WGL_EXT_swap_control =
3528 {
3529   "WGL_EXT_swap_control",
3530   {
3531     { "wglSwapIntervalEXT", X11DRV_wglSwapIntervalEXT },
3532     { "wglGetSwapIntervalEXT", X11DRV_wglGetSwapIntervalEXT },
3533   }
3534 };
3535
3536 static const WineGLExtension WGL_NV_vertex_array_range =
3537 {
3538   "WGL_NV_vertex_array_range",
3539   {
3540     { "wglAllocateMemoryNV", X11DRV_wglAllocateMemoryNV },
3541     { "wglFreeMemoryNV", X11DRV_wglFreeMemoryNV },
3542   }
3543 };
3544
3545 static const WineGLExtension WGL_WINE_pixel_format_passthrough =
3546 {
3547   "WGL_WINE_pixel_format_passthrough",
3548   {
3549     { "wglSetPixelFormatWINE", X11DRV_wglSetPixelFormatWINE },
3550   }
3551 };
3552
3553 /**
3554  * X11DRV_WineGL_LoadExtensions
3555  */
3556 static void X11DRV_WineGL_LoadExtensions(void)
3557 {
3558     WineGLInfo.wglExtensions[0] = 0;
3559
3560     /* Load Wine internal functions */
3561     register_extension(&WGL_internal_functions);
3562
3563     /* ARB Extensions */
3564
3565     if(glxRequireExtension("GLX_ARB_create_context"))
3566     {
3567         register_extension(&WGL_ARB_create_context);
3568
3569         if(glxRequireExtension("GLX_ARB_create_context_profile"))
3570             register_extension_string("WGL_ARB_create_context_profile");
3571     }
3572
3573     if(glxRequireExtension("GLX_ARB_fbconfig_float"))
3574     {
3575         register_extension_string("WGL_ARB_pixel_format_float");
3576         register_extension_string("WGL_ATI_pixel_format_float");
3577     }
3578
3579     register_extension(&WGL_ARB_extensions_string);
3580
3581     if (glxRequireVersion(3))
3582         register_extension(&WGL_ARB_make_current_read);
3583
3584     if (glxRequireExtension("GLX_ARB_multisample"))
3585         register_extension(&WGL_ARB_multisample);
3586
3587     /* In general pbuffer functionality requires support in the X-server. The functionality is
3588      * available either when the GLX_SGIX_pbuffer is present or when the GLX server version is 1.3.
3589      * All display drivers except for Nvidia's use the GLX module from Xfree86/Xorg which only
3590      * supports GLX 1.2. The endresult is that only Nvidia's drivers support pbuffers.
3591      *
3592      * The only other drive which has pbuffer support is Ati's FGLRX driver. They provide clientside GLX 1.3 support
3593      * without support in the X-server (which other Mesa based drivers require).
3594      *
3595      * Support pbuffers when the GLX version is 1.3 and GLX_SGIX_pbuffer is available. Further pbuffers can
3596      * also be supported when GLX_ATI_render_texture is available. This extension depends on pbuffers, so when it
3597      * is available pbuffers must be available too. */
3598     if ( (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer")) || glxRequireExtension("GLX_ATI_render_texture"))
3599         register_extension(&WGL_ARB_pbuffer);
3600
3601     register_extension(&WGL_ARB_pixel_format);
3602
3603     /* Support WGL_ARB_render_texture when there's support or pbuffer based emulation */
3604     if (glxRequireExtension("GLX_ATI_render_texture") ||
3605         glxRequireExtension("GLX_ARB_render_texture") ||
3606         (glxRequireVersion(3) && glxRequireExtension("GLX_SGIX_pbuffer") && use_render_texture_emulation))
3607     {
3608         register_extension(&WGL_ARB_render_texture);
3609
3610         /* The WGL version of GLX_NV_float_buffer requires render_texture */
3611         if(glxRequireExtension("GLX_NV_float_buffer"))
3612             register_extension_string("WGL_NV_float_buffer");
3613
3614         /* Again there's no GLX equivalent for this extension, so depend on the required GL extension */
3615         if(strstr(WineGLInfo.glExtensions, "GL_NV_texture_rectangle") != NULL)
3616             register_extension_string("WGL_NV_texture_rectangle");
3617     }
3618
3619     /* EXT Extensions */
3620
3621     register_extension(&WGL_EXT_extensions_string);
3622
3623     /* Load this extension even when it isn't backed by a GLX extension because it is has been around for ages.
3624      * Games like Call of Duty and K.O.T.O.R. rely on it. Further our emulation is good enough. */
3625     register_extension(&WGL_EXT_swap_control);
3626
3627     if(glxRequireExtension("GLX_EXT_framebuffer_sRGB"))
3628         register_extension_string("WGL_EXT_framebuffer_sRGB");
3629
3630     if(glxRequireExtension("GLX_EXT_fbconfig_packed_float"))
3631         register_extension_string("WGL_EXT_pixel_format_packed_float");
3632
3633     /* The OpenGL extension GL_NV_vertex_array_range adds wgl/glX functions which aren't exported as 'real' wgl/glX extensions. */
3634     if(strstr(WineGLInfo.glExtensions, "GL_NV_vertex_array_range") != NULL)
3635         register_extension(&WGL_NV_vertex_array_range);
3636
3637     /* WINE-specific WGL Extensions */
3638
3639     /* In WineD3D we need the ability to set the pixel format more than once (e.g. after a device reset).
3640      * The default wglSetPixelFormat doesn't allow this, so add our own which allows it.
3641      */
3642     register_extension(&WGL_WINE_pixel_format_passthrough);
3643 }
3644
3645
3646 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3647 {
3648     Drawable ret;
3649
3650     if(physDev->bitmap)
3651     {
3652         if (physDev->bitmap->hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
3653             ret = physDev->drawable; /* PBuffer */
3654         else
3655             ret = physDev->bitmap->glxpixmap;
3656     }
3657     else if(physDev->gl_drawable)
3658         ret = physDev->gl_drawable;
3659     else
3660         ret = physDev->drawable;
3661     return ret;
3662 }
3663
3664 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
3665 {
3666     wine_tsx11_lock(); 
3667     pglXDestroyGLXPixmap(display, glxpixmap);
3668     wine_tsx11_unlock(); 
3669     return TRUE;
3670 }
3671
3672 /**
3673  * X11DRV_SwapBuffers
3674  *
3675  * Swap the buffers of this DC
3676  */
3677 BOOL CDECL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev)
3678 {
3679   GLXDrawable drawable;
3680   Wine_GLContext *ctx = NtCurrentTeb()->glContext;
3681
3682   if (!has_opengl()) return FALSE;
3683
3684   TRACE("(%p)\n", physDev);
3685
3686   drawable = get_glxdrawable(physDev);
3687
3688   wine_tsx11_lock();
3689   sync_context(ctx);
3690   if(physDev->pixmap) {
3691       if(pglXCopySubBufferMESA) {
3692           int w = physDev->dc_rect.right - physDev->dc_rect.left;
3693           int h = physDev->dc_rect.bottom - physDev->dc_rect.top;
3694
3695           /* (glX)SwapBuffers has an implicit glFlush effect, however
3696            * GLX_MESA_copy_sub_buffer doesn't. Make sure GL is flushed before
3697            * copying */
3698           pglFlush();
3699           if(w > 0 && h > 0)
3700               pglXCopySubBufferMESA(gdi_display, drawable, 0, 0, w, h);
3701       }
3702       else
3703           pglXSwapBuffers(gdi_display, drawable);
3704   }
3705   else
3706       pglXSwapBuffers(gdi_display, drawable);
3707
3708   flush_gl_drawable(physDev);
3709   wine_tsx11_unlock();
3710
3711   /* FPS support */
3712   if (TRACE_ON(fps))
3713   {
3714       static long prev_time, start_time;
3715       static unsigned long frames, frames_total;
3716
3717       DWORD time = GetTickCount();
3718       frames++;
3719       frames_total++;
3720       /* every 1.5 seconds */
3721       if (time - prev_time > 1500) {
3722           TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
3723                       1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
3724           prev_time = time;
3725           frames = 0;
3726           if(start_time == 0) start_time = time;
3727       }
3728   }
3729
3730   return TRUE;
3731 }
3732
3733 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
3734 {
3735     WineGLPixelFormat *fmt;
3736     XVisualInfo *ret;
3737
3738     fmt = ConvertPixelFormatGLXtoWGL(gdi_display, fbconfig_id, 0 /* no flags */);
3739     if(fmt == NULL)
3740         return NULL;
3741
3742     wine_tsx11_lock();
3743     ret = pglXGetVisualFromFBConfig(gdi_display, fmt->fbconfig);
3744     wine_tsx11_unlock();
3745     return ret;
3746 }
3747
3748 #else  /* no OpenGL includes */
3749
3750 void X11DRV_OpenGL_Cleanup(void)
3751 {
3752 }
3753
3754 static inline void opengl_error(void)
3755 {
3756     static int warned;
3757     if (!warned++) ERR("No OpenGL support compiled in.\n");
3758 }
3759
3760 int pixelformat_from_fbconfig_id(XID fbconfig_id)
3761 {
3762     return 0;
3763 }
3764
3765 void mark_drawable_dirty(Drawable old, Drawable new)
3766 {
3767 }
3768
3769 void flush_gl_drawable(X11DRV_PDEVICE *physDev)
3770 {
3771 }
3772
3773 Drawable create_glxpixmap(Display *display, XVisualInfo *vis, Pixmap parent)
3774 {
3775     return 0;
3776 }
3777
3778 /***********************************************************************
3779  *              ChoosePixelFormat (X11DRV.@)
3780  */
3781 int CDECL X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
3782                                      const PIXELFORMATDESCRIPTOR *ppfd) {
3783   opengl_error();
3784   return 0;
3785 }
3786
3787 /***********************************************************************
3788  *              DescribePixelFormat (X11DRV.@)
3789  */
3790 int CDECL X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
3791                                      int iPixelFormat,
3792                                      UINT nBytes,
3793                                      PIXELFORMATDESCRIPTOR *ppfd) {
3794   opengl_error();
3795   return 0;
3796 }
3797
3798 /***********************************************************************
3799  *              GetPixelFormat (X11DRV.@)
3800  */
3801 int CDECL X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
3802   opengl_error();
3803   return 0;
3804 }
3805
3806 /***********************************************************************
3807  *              SetPixelFormat (X11DRV.@)
3808  */
3809 BOOL CDECL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
3810                                    int iPixelFormat,
3811                                    const PIXELFORMATDESCRIPTOR *ppfd) {
3812   opengl_error();
3813   return FALSE;
3814 }
3815
3816 /***********************************************************************
3817  *              SwapBuffers (X11DRV.@)
3818  */
3819 BOOL CDECL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
3820   opengl_error();
3821   return FALSE;
3822 }
3823
3824 /**
3825  * X11DRV_wglCopyContext
3826  *
3827  * For OpenGL32 wglCopyContext.
3828  */
3829 BOOL CDECL X11DRV_wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask) {
3830     opengl_error();
3831     return FALSE;
3832 }
3833
3834 /**
3835  * X11DRV_wglCreateContext
3836  *
3837  * For OpenGL32 wglCreateContext.
3838  */
3839 HGLRC CDECL X11DRV_wglCreateContext(X11DRV_PDEVICE *physDev) {
3840     opengl_error();
3841     return NULL;
3842 }
3843
3844 /**
3845  * X11DRV_wglCreateContextAttribsARB
3846  *
3847  * WGL_ARB_create_context: wglCreateContextAttribsARB
3848  */
3849 HGLRC X11DRV_wglCreateContextAttribsARB(X11DRV_PDEVICE *physDev, HGLRC hShareContext, const int* attribList)
3850 {
3851     opengl_error();
3852     return NULL;
3853 }
3854
3855 /**
3856  * X11DRV_wglDeleteContext
3857  *
3858  * For OpenGL32 wglDeleteContext.
3859  */
3860 BOOL CDECL X11DRV_wglDeleteContext(HGLRC hglrc) {
3861     opengl_error();
3862     return FALSE;
3863 }
3864
3865 /**
3866  * X11DRV_wglGetProcAddress
3867  *
3868  * For OpenGL32 wglGetProcAddress.
3869  */
3870 PROC CDECL X11DRV_wglGetProcAddress(LPCSTR lpszProc) {
3871     opengl_error();
3872     return NULL;
3873 }
3874
3875 HDC CDECL X11DRV_wglGetPbufferDCARB(X11DRV_PDEVICE *hDevice, void *hPbuffer)
3876 {
3877     opengl_error();
3878     return NULL;
3879 }
3880
3881 BOOL CDECL X11DRV_wglMakeContextCurrentARB(X11DRV_PDEVICE* hDrawDev, X11DRV_PDEVICE* hReadDev, HGLRC hglrc) {
3882     opengl_error();
3883     return FALSE;
3884 }
3885
3886 /**
3887  * X11DRV_wglMakeCurrent
3888  *
3889  * For OpenGL32 wglMakeCurrent.
3890  */
3891 BOOL CDECL X11DRV_wglMakeCurrent(X11DRV_PDEVICE *physDev, HGLRC hglrc) {
3892     opengl_error();
3893     return FALSE;
3894 }
3895
3896 /**
3897  * X11DRV_wglShareLists
3898  *
3899  * For OpenGL32 wglShareLists.
3900  */
3901 BOOL CDECL X11DRV_wglShareLists(HGLRC hglrc1, HGLRC hglrc2) {
3902     opengl_error();
3903     return FALSE;
3904 }
3905
3906 /**
3907  * X11DRV_wglUseFontBitmapsA
3908  *
3909  * For OpenGL32 wglUseFontBitmapsA.
3910  */
3911 BOOL CDECL X11DRV_wglUseFontBitmapsA(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3912 {
3913     opengl_error();
3914     return FALSE;
3915 }
3916
3917 /**
3918  * X11DRV_wglUseFontBitmapsW
3919  *
3920  * For OpenGL32 wglUseFontBitmapsW.
3921  */
3922 BOOL CDECL X11DRV_wglUseFontBitmapsW(X11DRV_PDEVICE *physDev, DWORD first, DWORD count, DWORD listBase)
3923 {
3924     opengl_error();
3925     return FALSE;
3926 }
3927
3928 /**
3929  * X11DRV_wglSetPixelFormatWINE
3930  *
3931  * WGL_WINE_pixel_format_passthrough: wglSetPixelFormatWINE
3932  * This is a WINE-specific wglSetPixelFormat which can set the pixel format multiple times.
3933  */
3934 BOOL CDECL X11DRV_wglSetPixelFormatWINE(X11DRV_PDEVICE *physDev, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
3935 {
3936     opengl_error();
3937     return FALSE;
3938 }
3939
3940 Drawable get_glxdrawable(X11DRV_PDEVICE *physDev)
3941 {
3942     return 0;
3943 }
3944
3945 BOOL destroy_glxpixmap(Display *display, XID glxpixmap)
3946 {
3947     return FALSE;
3948 }
3949
3950 XVisualInfo *visual_from_fbconfig_id( XID fbconfig_id )
3951 {
3952     return NULL;
3953 }
3954
3955 #endif /* defined(HAVE_OPENGL) */