Fixes for -Wmissing-declaration and -Wwrite-string warnings.
[wine] / dlls / x11drv / opengl.c
1 /*
2  * X11DRV OpenGL functions
3  *
4  * Copyright 2000 Lionel Ulmer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "x11drv.h"
28 #include "wine/library.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(opengl);
32
33 #if defined(HAVE_GL_GL_H) && defined(HAVE_GL_GLX_H)
34
35 #undef APIENTRY
36 #undef CALLBACK
37 #undef WINAPI
38
39 #ifdef HAVE_GL_GL_H
40 # include <GL/gl.h>
41 #endif
42 #ifdef HAVE_GL_GLX_H
43 # include <GL/glx.h>
44 #endif
45 #ifdef HAVE_GL_GLEXT_H
46 # include <GL/glext.h>
47 #endif
48
49 #undef APIENTRY
50 #undef CALLBACK
51 #undef WINAPI
52
53 /* Redefines the constants */
54 #define CALLBACK    __stdcall
55 #define WINAPI      __stdcall
56 #define APIENTRY    WINAPI
57
58
59 static void dump_PIXELFORMATDESCRIPTOR(const PIXELFORMATDESCRIPTOR *ppfd) {
60   DPRINTF("  - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
61   DPRINTF("  - dwFlags : ");
62 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) DPRINTF(#tv " ")
63   TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
64   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
65   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
66   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
67   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
68   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
69   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
70   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
71   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
72   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
73   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
74   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
75   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
76   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
77   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
78   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
79 #undef TEST_AND_DUMP
80   DPRINTF("\n");
81
82   DPRINTF("  - iPixelType : ");
83   switch (ppfd->iPixelType) {
84   case PFD_TYPE_RGBA: DPRINTF("PFD_TYPE_RGBA"); break;
85   case PFD_TYPE_COLORINDEX: DPRINTF("PFD_TYPE_COLORINDEX"); break;
86   }
87   DPRINTF("\n");
88
89   DPRINTF("  - Color   : %d\n", ppfd->cColorBits);
90   DPRINTF("  - Red     : %d\n", ppfd->cRedBits);
91   DPRINTF("  - Green   : %d\n", ppfd->cGreenBits);
92   DPRINTF("  - Blue    : %d\n", ppfd->cBlueBits);
93   DPRINTF("  - Alpha   : %d\n", ppfd->cAlphaBits);
94   DPRINTF("  - Accum   : %d\n", ppfd->cAccumBits);
95   DPRINTF("  - Depth   : %d\n", ppfd->cDepthBits);
96   DPRINTF("  - Stencil : %d\n", ppfd->cStencilBits);
97   DPRINTF("  - Aux     : %d\n", ppfd->cAuxBuffers);
98
99   DPRINTF("  - iLayerType : ");
100   switch (ppfd->iLayerType) {
101   case PFD_MAIN_PLANE: DPRINTF("PFD_MAIN_PLANE"); break;
102   case PFD_OVERLAY_PLANE: DPRINTF("PFD_OVERLAY_PLANE"); break;
103   case PFD_UNDERLAY_PLANE: DPRINTF("PFD_UNDERLAY_PLANE"); break;
104   }
105   DPRINTF("\n");
106 }
107
108 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient and
109    include all dependencies
110 */
111 #ifndef SONAME_LIBGL
112 #define SONAME_LIBGL "libGL.so"
113 #endif
114
115 static void *opengl_handle;
116
117 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
118 MAKE_FUNCPTR(glGetError)
119 MAKE_FUNCPTR(glXChooseVisual)
120 MAKE_FUNCPTR(glXGetConfig)
121 MAKE_FUNCPTR(glXSwapBuffers)
122 MAKE_FUNCPTR(glXQueryExtension)
123
124 MAKE_FUNCPTR(glXGetFBConfigs)
125 MAKE_FUNCPTR(glXChooseFBConfig)
126 MAKE_FUNCPTR(glXGetFBConfigAttrib)
127 #undef MAKE_FUNCPTR
128
129 void X11DRV_OpenGL_Init(Display *display) {
130     int error_base, event_base;
131
132     opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, NULL, 0);
133     if (opengl_handle == NULL) return;
134
135 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(opengl_handle, #f, NULL, 0)) == NULL) goto sym_not_found;
136 LOAD_FUNCPTR(glGetError)
137 LOAD_FUNCPTR(glXChooseVisual)
138 LOAD_FUNCPTR(glXGetConfig)
139 LOAD_FUNCPTR(glXSwapBuffers)
140 LOAD_FUNCPTR(glXQueryExtension)
141
142 LOAD_FUNCPTR(glXGetFBConfigs)
143 LOAD_FUNCPTR(glXChooseFBConfig)
144 LOAD_FUNCPTR(glXGetFBConfigAttrib)
145 #undef LOAD_FUNCPTR
146
147     wine_tsx11_lock();
148     if (pglXQueryExtension(display, &event_base, &error_base) == True) {
149         TRACE("GLX is up and running error_base = %d\n", error_base);
150     } else {
151         wine_dlclose(opengl_handle, NULL, 0);
152         opengl_handle = NULL;
153     }
154     wine_tsx11_unlock();
155     return;
156
157 sym_not_found:
158     wine_dlclose(opengl_handle, NULL, 0);
159     opengl_handle = NULL;
160 }
161
162 #define TEST_AND_ADD1(t,a) if (t) att_list[att_pos++] = (a)
163 #define TEST_AND_ADD2(t,a,b) if (t) { att_list[att_pos++] = (a); att_list[att_pos++] = (b); }
164 #define NULL_TEST_AND_ADD2(tv,a,b) att_list[att_pos++] = (a); att_list[att_pos++] = ((tv) == 0 ? 0 : (b))
165 #define ADD2(a,b) att_list[att_pos++] = (a); att_list[att_pos++] = (b)
166
167 /**
168  * X11DRV_ChoosePixelFormat
169  *
170  * Equivalent of glXChooseVisual
171  */
172 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev, 
173                              const PIXELFORMATDESCRIPTOR *ppfd) {
174   int att_list[64];
175   int att_pos = 0;
176   GLXFBConfig* cfgs = NULL;
177   int ret = 0;
178
179   if (opengl_handle == NULL) {
180     ERR("No libGL on this box - disabling OpenGL support !\n");
181     return 0;
182   }
183   
184   if (TRACE_ON(opengl)) {
185     TRACE("(%p,%p)\n", physDev, ppfd);
186
187     dump_PIXELFORMATDESCRIPTOR((const PIXELFORMATDESCRIPTOR *) ppfd);
188   }
189
190   if (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) {
191     ERR("Flag not supported !\n");
192     /* Should SetError here... */
193     return 0;
194   }
195
196   /* Now, build the request to GLX */
197   TEST_AND_ADD2(ppfd->dwFlags & PFD_DOUBLEBUFFER, GLX_DOUBLEBUFFER, TRUE);
198   TEST_AND_ADD1(ppfd->dwFlags & PFD_STEREO, GLX_STEREO);
199   
200   if (ppfd->iPixelType == PFD_TYPE_COLORINDEX) {
201     ADD2(GLX_BUFFER_SIZE, ppfd->cColorBits);
202   }  
203   if (ppfd->iPixelType == PFD_TYPE_RGBA) {
204     ADD2(GLX_RENDER_TYPE, GLX_RGBA_BIT);
205     ADD2(GLX_BUFFER_SIZE, ppfd->cColorBits);
206     if (32 == ppfd->cDepthBits) {
207       /**
208        * for 32 bpp depth buffers force to use 24.
209        * needed as some drivers don't support 32bpp
210        */
211       TEST_AND_ADD2(ppfd->cDepthBits, GLX_DEPTH_SIZE, 24);
212     } else {
213       TEST_AND_ADD2(ppfd->cDepthBits, GLX_DEPTH_SIZE, ppfd->cDepthBits);
214     }
215     TEST_AND_ADD2(ppfd->cAlphaBits, GLX_ALPHA_SIZE, ppfd->cAlphaBits);
216   }
217   TEST_AND_ADD2(ppfd->cStencilBits, GLX_STENCIL_SIZE, ppfd->cStencilBits);
218   TEST_AND_ADD2(ppfd->cAuxBuffers,  GLX_AUX_BUFFERS,  ppfd->cAuxBuffers);
219    
220   /* These flags are not supported yet...
221   ADD2(GLX_ACCUM_SIZE, ppfd->cAccumBits);
222   */
223   att_list[att_pos] = None;
224
225   wine_tsx11_lock(); 
226   {
227     int nCfgs = 0;
228     int gl_test = 0;
229     int fmt_id;
230
231     GLXFBConfig* cfgs_fmt = NULL;
232     int nCfgs_fmt = 0;
233     int tmp_fmt_id;
234     UINT it_fmt;
235
236     cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), att_list, &nCfgs); 
237     if (NULL == cfgs || 0 == nCfgs) {
238       ERR("glXChooseFBConfig returns NULL (glError: %d)\n", pglGetError());
239       ret = 0;
240       goto choose_exit;
241     }
242     
243     gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[0], GLX_FBCONFIG_ID, &fmt_id);
244     if (gl_test) {
245       ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
246       ret = 0;
247       goto choose_exit;
248     }
249     
250     cfgs_fmt = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs_fmt);
251     if (NULL == cfgs_fmt) {
252       ERR("Failed to get All FB Configs\n");
253       ret = 0;
254       goto choose_exit;
255     }
256
257     for (it_fmt = 0; it_fmt < nCfgs_fmt; ++it_fmt) {
258       gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs_fmt[it_fmt], GLX_FBCONFIG_ID, &tmp_fmt_id);
259       if (gl_test) {
260         ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
261         XFree(cfgs_fmt);
262         ret = 0;
263         goto choose_exit;
264       }
265       if (fmt_id == tmp_fmt_id) {
266         ret = it_fmt + 1;
267         break ;
268       }
269     }
270     if (it_fmt == nCfgs_fmt) {
271       ERR("Failed to get valid fmt for FBCONFIG_ID(%d)\n", fmt_id);
272       ret = 0;
273     }
274     XFree(cfgs_fmt);
275   }
276
277 choose_exit:
278   if (NULL != cfgs) XFree(cfgs);
279   wine_tsx11_unlock();
280   return ret;
281 }
282
283 /**
284  * X11DRV_DescribePixelFormat
285  *
286  * Get the pixel-format descriptor associated to the given id
287  */
288 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
289                                int iPixelFormat,
290                                UINT nBytes,
291                                PIXELFORMATDESCRIPTOR *ppfd) {
292   /*XVisualInfo *vis;*/
293   int value;
294   int rb,gb,bb,ab;
295
296   GLXFBConfig* cfgs = NULL;
297   GLXFBConfig cur;
298   int nCfgs = 0;
299   int ret = 0;
300
301   if (opengl_handle == NULL) {
302     ERR("No libGL on this box - disabling OpenGL support !\n");
303     return 0;
304   }
305   
306   TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
307
308   wine_tsx11_lock();
309   cfgs = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs);
310   wine_tsx11_unlock();
311
312   if (NULL == cfgs || 0 == nCfgs) {
313     ERR("unexpected iPixelFormat(%d), returns NULL\n", iPixelFormat);
314     return 0; /* unespected error */
315   }
316
317   if (ppfd == NULL) {
318     /* The application is only querying the number of visuals */
319     wine_tsx11_lock();
320     if (NULL != cfgs) XFree(cfgs);
321     wine_tsx11_unlock();
322     return nCfgs;
323   }
324
325   if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
326     ERR("Wrong structure size !\n");
327     /* Should set error */
328     return 0;
329   }
330
331   if (nCfgs < iPixelFormat) {
332     ERR("unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", iPixelFormat, nCfgs);
333     return 0; /* unespected error */
334   }
335
336   ret = nCfgs;
337   cur = cfgs[iPixelFormat - 1];
338
339   memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
340   ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
341   ppfd->nVersion = 1;
342
343   /* These flags are always the same... */
344   ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_GENERIC_ACCELERATED;
345   /* Now the flags extraced from the Visual */
346
347   wine_tsx11_lock();
348
349   pglXGetFBConfigAttrib(gdi_display, cur, GLX_DOUBLEBUFFER, &value); if (value) ppfd->dwFlags |= PFD_DOUBLEBUFFER;
350   pglXGetFBConfigAttrib(gdi_display, cur, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
351
352   /* Pixel type */
353   pglXGetFBConfigAttrib(gdi_display, cur, GLX_RENDER_TYPE, &value);
354   if (value & GLX_RGBA_BIT)
355     ppfd->iPixelType = PFD_TYPE_RGBA;
356   else
357     ppfd->iPixelType = PFD_TYPE_COLORINDEX;
358
359   /* Color bits */
360   pglXGetFBConfigAttrib(gdi_display, cur, GLX_BUFFER_SIZE, &value);
361   ppfd->cColorBits = value;
362
363   /* Red, green, blue and alpha bits / shifts */
364   if (ppfd->iPixelType == PFD_TYPE_RGBA) {
365     pglXGetFBConfigAttrib(gdi_display, cur, GLX_RED_SIZE, &rb);
366     pglXGetFBConfigAttrib(gdi_display, cur, GLX_GREEN_SIZE, &gb);
367     pglXGetFBConfigAttrib(gdi_display, cur, GLX_BLUE_SIZE, &bb);
368     pglXGetFBConfigAttrib(gdi_display, cur, GLX_ALPHA_SIZE, &ab);
369
370     ppfd->cRedBits = rb;
371     ppfd->cRedShift = gb + bb + ab;
372     ppfd->cBlueBits = bb;
373     ppfd->cBlueShift = ab;
374     ppfd->cGreenBits = gb;
375     ppfd->cGreenShift = bb + ab;
376     ppfd->cAlphaBits = ab;
377     ppfd->cAlphaShift = 0;
378   } else {
379     ppfd->cRedBits = 0;
380     ppfd->cRedShift = 0;
381     ppfd->cBlueBits = 0;
382     ppfd->cBlueShift = 0;
383     ppfd->cGreenBits = 0;
384     ppfd->cGreenShift = 0;
385     ppfd->cAlphaBits = 0;
386     ppfd->cAlphaShift = 0;
387   }
388   /* Accums : to do ... */
389
390   /* Depth bits */
391   pglXGetFBConfigAttrib(gdi_display, cur, GLX_DEPTH_SIZE, &value);
392   ppfd->cDepthBits = value;
393
394   /* stencil bits */
395   pglXGetFBConfigAttrib(gdi_display, cur, GLX_STENCIL_SIZE, &value);
396   ppfd->cStencilBits = value;
397
398   wine_tsx11_unlock();
399
400   /* Aux : to do ... */
401
402   ppfd->iLayerType = PFD_MAIN_PLANE;
403
404   if (TRACE_ON(opengl)) {
405     dump_PIXELFORMATDESCRIPTOR(ppfd);
406   }
407
408   wine_tsx11_lock();
409   if (NULL != cfgs) XFree(cfgs);
410   wine_tsx11_unlock();
411
412   return ret;
413 }
414
415 /**
416  * X11DRV_GetPixelFormat
417  *
418  * Get the pixel-format id used by this DC
419  */
420 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
421   TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
422
423   return physDev->current_pf;
424 }
425
426 /**
427  * X11DRV_SetPixelFormat
428  *
429  * Set the pixel-format id used by this DC
430  */
431 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
432                            int iPixelFormat,
433                            const PIXELFORMATDESCRIPTOR *ppfd) {
434   TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
435
436   physDev->current_pf = iPixelFormat;
437
438   return TRUE;
439 }
440
441 /**
442  * X11DRV_SwapBuffers
443  *
444  * Swap the buffers of this DC
445  */
446 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
447   if (opengl_handle == NULL) {
448     ERR("No libGL on this box - disabling OpenGL support !\n");
449     return 0;
450   }
451   
452   TRACE("(%p)\n", physDev);
453
454   wine_tsx11_lock();
455   pglXSwapBuffers(gdi_display, physDev->drawable);
456   wine_tsx11_unlock();
457
458   return TRUE;
459 }
460
461 /***********************************************************************
462  *              X11DRV_setup_opengl_visual
463  *
464  * Setup the default visual used for OpenGL and Direct3D, and the desktop
465  * window (if it exists).  If OpenGL isn't available, the visual is simply
466  * set to the default visual for the display
467  */
468 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
469 {
470     XVisualInfo *visual = NULL;
471     int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
472
473     if (opengl_handle == NULL) return NULL;
474     
475     /* In order to support OpenGL or D3D, we require a double-buffered visual */
476     wine_tsx11_lock();
477     visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf);
478     wine_tsx11_unlock();
479     return visual;
480 }
481
482 #else  /* no OpenGL includes */
483
484 void X11DRV_OpenGL_Init(Display *display)
485 {
486 }
487
488 /***********************************************************************
489  *              ChoosePixelFormat (X11DRV.@)
490  */
491 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
492                              const PIXELFORMATDESCRIPTOR *ppfd) {
493   ERR("No OpenGL support compiled in.\n");
494
495   return 0;
496 }
497
498 /***********************************************************************
499  *              DescribePixelFormat (X11DRV.@)
500  */
501 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
502                                int iPixelFormat,
503                                UINT nBytes,
504                                PIXELFORMATDESCRIPTOR *ppfd) {
505   ERR("No OpenGL support compiled in.\n");
506
507   return 0;
508 }
509
510 /***********************************************************************
511  *              GetPixelFormat (X11DRV.@)
512  */
513 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
514   ERR("No OpenGL support compiled in.\n");
515
516   return 0;
517 }
518
519 /***********************************************************************
520  *              SetPixelFormat (X11DRV.@)
521  */
522 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
523                            int iPixelFormat,
524                            const PIXELFORMATDESCRIPTOR *ppfd) {
525   ERR("No OpenGL support compiled in.\n");
526
527   return FALSE;
528 }
529
530 /***********************************************************************
531  *              SwapBuffers (X11DRV.@)
532  */
533 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
534   ERR("No OpenGL support compiled in.\n");
535
536   return FALSE;
537 }
538
539 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
540 {
541   return NULL;
542 }
543
544 #endif /* defined(HAVE_OPENGL) */