Don't dereference a NULL visual pointer.
[wine/multimedia.git] / dlls / x11drv / opengl.c
blob20a90ec630c7bdfb65ec948ca38b600fe83992ec
1 /*
2 * X11DRV OpenGL functions
4 * Copyright 2000 Lionel Ulmer
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdlib.h>
25 #include <string.h>
27 #include "x11drv.h"
28 #include "wine/library.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(opengl);
33 #if defined(HAVE_GL_GL_H) && defined(HAVE_GL_GLX_H)
35 #undef APIENTRY
36 #undef CALLBACK
37 #undef WINAPI
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
49 #undef APIENTRY
50 #undef CALLBACK
51 #undef WINAPI
53 /* Redefines the constants */
54 #define CALLBACK __stdcall
55 #define WINAPI __stdcall
56 #define APIENTRY WINAPI
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");
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;
87 DPRINTF("\n");
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);
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 (BYTE)PFD_UNDERLAY_PLANE: DPRINTF("PFD_UNDERLAY_PLANE"); break;
105 DPRINTF("\n");
108 /* No need to load any other libraries as according to the ABI, libGL should be self-sufficient and
109 include all dependencies
111 #ifndef SONAME_LIBGL
112 #define SONAME_LIBGL "libGL.so"
113 #endif
115 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
116 MAKE_FUNCPTR(glGetError)
117 MAKE_FUNCPTR(glXChooseVisual)
118 MAKE_FUNCPTR(glXGetConfig)
119 MAKE_FUNCPTR(glXSwapBuffers)
120 MAKE_FUNCPTR(glXQueryExtension)
122 MAKE_FUNCPTR(glXGetFBConfigs)
123 MAKE_FUNCPTR(glXChooseFBConfig)
124 MAKE_FUNCPTR(glXGetFBConfigAttrib)
125 #undef MAKE_FUNCPTR
127 static BOOL has_opengl(void)
129 static int init_done;
130 static void *opengl_handle;
132 int error_base, event_base;
134 if (init_done) return (opengl_handle != NULL);
135 init_done = 1;
137 opengl_handle = wine_dlopen(SONAME_LIBGL, RTLD_NOW|RTLD_GLOBAL, NULL, 0);
138 if (opengl_handle == NULL) return FALSE;
140 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(opengl_handle, #f, NULL, 0)) == NULL) goto sym_not_found;
141 LOAD_FUNCPTR(glGetError)
142 LOAD_FUNCPTR(glXChooseVisual)
143 LOAD_FUNCPTR(glXGetConfig)
144 LOAD_FUNCPTR(glXSwapBuffers)
145 LOAD_FUNCPTR(glXQueryExtension)
147 LOAD_FUNCPTR(glXGetFBConfigs)
148 LOAD_FUNCPTR(glXChooseFBConfig)
149 LOAD_FUNCPTR(glXGetFBConfigAttrib)
150 #undef LOAD_FUNCPTR
152 wine_tsx11_lock();
153 if (pglXQueryExtension(gdi_display, &event_base, &error_base) == True) {
154 TRACE("GLX is up and running error_base = %d\n", error_base);
155 } else {
156 wine_dlclose(opengl_handle, NULL, 0);
157 opengl_handle = NULL;
159 wine_tsx11_unlock();
160 return (opengl_handle != NULL);
162 sym_not_found:
163 wine_dlclose(opengl_handle, NULL, 0);
164 opengl_handle = NULL;
165 return FALSE;
168 #define TEST_AND_ADD1(t,a) if (t) att_list[att_pos++] = (a)
169 #define TEST_AND_ADD2(t,a,b) if (t) { att_list[att_pos++] = (a); att_list[att_pos++] = (b); }
170 #define NULL_TEST_AND_ADD2(tv,a,b) att_list[att_pos++] = (a); att_list[att_pos++] = ((tv) == 0 ? 0 : (b))
171 #define ADD2(a,b) att_list[att_pos++] = (a); att_list[att_pos++] = (b)
174 * X11DRV_ChoosePixelFormat
176 * Equivalent of glXChooseVisual
178 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
179 const PIXELFORMATDESCRIPTOR *ppfd) {
180 int att_list[64];
181 int att_pos = 0;
182 GLXFBConfig* cfgs = NULL;
183 int ret = 0;
185 if (!has_opengl()) {
186 ERR("No libGL on this box - disabling OpenGL support !\n");
187 return 0;
190 if (TRACE_ON(opengl)) {
191 TRACE("(%p,%p)\n", physDev, ppfd);
193 dump_PIXELFORMATDESCRIPTOR((const PIXELFORMATDESCRIPTOR *) ppfd);
196 if (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) {
197 ERR("Flag not supported !\n");
198 /* Should SetError here... */
199 return 0;
202 /* Now, build the request to GLX */
203 TEST_AND_ADD2(ppfd->dwFlags & PFD_DOUBLEBUFFER, GLX_DOUBLEBUFFER, TRUE);
204 TEST_AND_ADD1(ppfd->dwFlags & PFD_STEREO, GLX_STEREO);
206 if (ppfd->iPixelType == PFD_TYPE_COLORINDEX) {
207 ADD2(GLX_BUFFER_SIZE, ppfd->cColorBits);
209 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
210 ADD2(GLX_RENDER_TYPE, GLX_RGBA_BIT);
211 ADD2(GLX_BUFFER_SIZE, ppfd->cColorBits);
212 if (32 == ppfd->cDepthBits) {
214 * for 32 bpp depth buffers force to use 24.
215 * needed as some drivers don't support 32bpp
217 TEST_AND_ADD2(ppfd->cDepthBits, GLX_DEPTH_SIZE, 24);
218 } else {
219 TEST_AND_ADD2(ppfd->cDepthBits, GLX_DEPTH_SIZE, ppfd->cDepthBits);
221 TEST_AND_ADD2(ppfd->cAlphaBits, GLX_ALPHA_SIZE, ppfd->cAlphaBits);
223 TEST_AND_ADD2(ppfd->cStencilBits, GLX_STENCIL_SIZE, ppfd->cStencilBits);
224 TEST_AND_ADD2(ppfd->cAuxBuffers, GLX_AUX_BUFFERS, ppfd->cAuxBuffers);
226 /* These flags are not supported yet...
227 ADD2(GLX_ACCUM_SIZE, ppfd->cAccumBits);
229 att_list[att_pos] = None;
231 wine_tsx11_lock();
233 int nCfgs = 0;
234 int gl_test = 0;
235 int fmt_id;
237 GLXFBConfig* cfgs_fmt = NULL;
238 int nCfgs_fmt = 0;
239 int tmp_fmt_id;
240 UINT it_fmt;
242 cfgs = pglXChooseFBConfig(gdi_display, DefaultScreen(gdi_display), att_list, &nCfgs);
243 if (NULL == cfgs || 0 == nCfgs) {
244 ERR("glXChooseFBConfig returns NULL (glError: %d)\n", pglGetError());
245 ret = 0;
246 goto choose_exit;
249 gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs[0], GLX_FBCONFIG_ID, &fmt_id);
250 if (gl_test) {
251 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
252 ret = 0;
253 goto choose_exit;
256 cfgs_fmt = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs_fmt);
257 if (NULL == cfgs_fmt) {
258 ERR("Failed to get All FB Configs\n");
259 ret = 0;
260 goto choose_exit;
263 for (it_fmt = 0; it_fmt < nCfgs_fmt; ++it_fmt) {
264 gl_test = pglXGetFBConfigAttrib(gdi_display, cfgs_fmt[it_fmt], GLX_FBCONFIG_ID, &tmp_fmt_id);
265 if (gl_test) {
266 ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
267 XFree(cfgs_fmt);
268 ret = 0;
269 goto choose_exit;
271 if (fmt_id == tmp_fmt_id) {
272 ret = it_fmt + 1;
273 break ;
276 if (it_fmt == nCfgs_fmt) {
277 ERR("Failed to get valid fmt for FBCONFIG_ID(%d)\n", fmt_id);
278 ret = 0;
280 XFree(cfgs_fmt);
283 choose_exit:
284 if (NULL != cfgs) XFree(cfgs);
285 wine_tsx11_unlock();
286 return ret;
290 * X11DRV_DescribePixelFormat
292 * Get the pixel-format descriptor associated to the given id
294 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
295 int iPixelFormat,
296 UINT nBytes,
297 PIXELFORMATDESCRIPTOR *ppfd) {
298 /*XVisualInfo *vis;*/
299 int value;
300 int rb,gb,bb,ab;
302 GLXFBConfig* cfgs = NULL;
303 GLXFBConfig cur;
304 int nCfgs = 0;
305 int ret = 0;
307 if (!has_opengl()) {
308 ERR("No libGL on this box - disabling OpenGL support !\n");
309 return 0;
312 TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
314 wine_tsx11_lock();
315 cfgs = pglXGetFBConfigs(gdi_display, DefaultScreen(gdi_display), &nCfgs);
316 wine_tsx11_unlock();
318 if (NULL == cfgs || 0 == nCfgs) {
319 ERR("unexpected iPixelFormat(%d), returns NULL\n", iPixelFormat);
320 return 0; /* unespected error */
323 if (ppfd == NULL) {
324 /* The application is only querying the number of visuals */
325 wine_tsx11_lock();
326 if (NULL != cfgs) XFree(cfgs);
327 wine_tsx11_unlock();
328 return nCfgs;
331 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
332 ERR("Wrong structure size !\n");
333 /* Should set error */
334 return 0;
337 if (nCfgs < iPixelFormat) {
338 ERR("unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", iPixelFormat, nCfgs);
339 return 0; /* unespected error */
342 ret = nCfgs;
343 cur = cfgs[iPixelFormat - 1];
345 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
346 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
347 ppfd->nVersion = 1;
349 /* These flags are always the same... */
350 ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_GENERIC_ACCELERATED;
351 /* Now the flags extraced from the Visual */
353 wine_tsx11_lock();
355 pglXGetFBConfigAttrib(gdi_display, cur, GLX_DOUBLEBUFFER, &value); if (value) ppfd->dwFlags |= PFD_DOUBLEBUFFER;
356 pglXGetFBConfigAttrib(gdi_display, cur, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
358 /* Pixel type */
359 pglXGetFBConfigAttrib(gdi_display, cur, GLX_RENDER_TYPE, &value);
360 if (value & GLX_RGBA_BIT)
361 ppfd->iPixelType = PFD_TYPE_RGBA;
362 else
363 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
365 /* Color bits */
366 pglXGetFBConfigAttrib(gdi_display, cur, GLX_BUFFER_SIZE, &value);
367 ppfd->cColorBits = value;
369 /* Red, green, blue and alpha bits / shifts */
370 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
371 pglXGetFBConfigAttrib(gdi_display, cur, GLX_RED_SIZE, &rb);
372 pglXGetFBConfigAttrib(gdi_display, cur, GLX_GREEN_SIZE, &gb);
373 pglXGetFBConfigAttrib(gdi_display, cur, GLX_BLUE_SIZE, &bb);
374 pglXGetFBConfigAttrib(gdi_display, cur, GLX_ALPHA_SIZE, &ab);
376 ppfd->cRedBits = rb;
377 ppfd->cRedShift = gb + bb + ab;
378 ppfd->cBlueBits = bb;
379 ppfd->cBlueShift = ab;
380 ppfd->cGreenBits = gb;
381 ppfd->cGreenShift = bb + ab;
382 ppfd->cAlphaBits = ab;
383 ppfd->cAlphaShift = 0;
384 } else {
385 ppfd->cRedBits = 0;
386 ppfd->cRedShift = 0;
387 ppfd->cBlueBits = 0;
388 ppfd->cBlueShift = 0;
389 ppfd->cGreenBits = 0;
390 ppfd->cGreenShift = 0;
391 ppfd->cAlphaBits = 0;
392 ppfd->cAlphaShift = 0;
394 /* Accums : to do ... */
396 /* Depth bits */
397 pglXGetFBConfigAttrib(gdi_display, cur, GLX_DEPTH_SIZE, &value);
398 ppfd->cDepthBits = value;
400 /* stencil bits */
401 pglXGetFBConfigAttrib(gdi_display, cur, GLX_STENCIL_SIZE, &value);
402 ppfd->cStencilBits = value;
404 wine_tsx11_unlock();
406 /* Aux : to do ... */
408 ppfd->iLayerType = PFD_MAIN_PLANE;
410 if (TRACE_ON(opengl)) {
411 dump_PIXELFORMATDESCRIPTOR(ppfd);
414 wine_tsx11_lock();
415 if (NULL != cfgs) XFree(cfgs);
416 wine_tsx11_unlock();
418 return ret;
422 * X11DRV_GetPixelFormat
424 * Get the pixel-format id used by this DC
426 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
427 TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
429 return physDev->current_pf;
433 * X11DRV_SetPixelFormat
435 * Set the pixel-format id used by this DC
437 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
438 int iPixelFormat,
439 const PIXELFORMATDESCRIPTOR *ppfd) {
440 TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
442 physDev->current_pf = iPixelFormat;
444 return TRUE;
448 * X11DRV_SwapBuffers
450 * Swap the buffers of this DC
452 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
453 if (!has_opengl()) {
454 ERR("No libGL on this box - disabling OpenGL support !\n");
455 return 0;
458 TRACE("(%p)\n", physDev);
460 wine_tsx11_lock();
461 pglXSwapBuffers(gdi_display, physDev->drawable);
462 wine_tsx11_unlock();
464 return TRUE;
467 /***********************************************************************
468 * X11DRV_setup_opengl_visual
470 * Setup the default visual used for OpenGL and Direct3D, and the desktop
471 * window (if it exists). If OpenGL isn't available, the visual is simply
472 * set to the default visual for the display
474 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
476 XVisualInfo *visual = NULL;
477 /* In order to support OpenGL or D3D, we require a double-buffered visual and stencil buffer support, */
478 int dblBuf[] = {GLX_RGBA,GLX_DEPTH_SIZE, 16, GLX_STENCIL_SIZE, 8, GLX_DOUBLEBUFFER, None};
479 if (!has_opengl()) return NULL;
481 wine_tsx11_lock();
482 visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf);
483 wine_tsx11_unlock();
484 if (visual == NULL) {
485 /* fallback to no stencil */
486 int dblBuf2[] = {GLX_RGBA,GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
487 WARN("Failed to get a visual with at least 8 bits of stencil\n");
489 wine_tsx11_lock();
490 visual = pglXChooseVisual(display, DefaultScreen(display), dblBuf2);
491 wine_tsx11_unlock();
492 if (visual == NULL) {
493 /* This should only happen if we cannot find a match with a depth size 16 */
494 FIXME("Failed to find a suitable visual\n");
495 return visual;
498 TRACE("Visual ID %lx Chosen\n",visual->visualid);
499 return visual;
502 #else /* no OpenGL includes */
504 void X11DRV_OpenGL_Init(Display *display)
508 /***********************************************************************
509 * ChoosePixelFormat (X11DRV.@)
511 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
512 const PIXELFORMATDESCRIPTOR *ppfd) {
513 ERR("No OpenGL support compiled in.\n");
515 return 0;
518 /***********************************************************************
519 * DescribePixelFormat (X11DRV.@)
521 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
522 int iPixelFormat,
523 UINT nBytes,
524 PIXELFORMATDESCRIPTOR *ppfd) {
525 ERR("No OpenGL support compiled in.\n");
527 return 0;
530 /***********************************************************************
531 * GetPixelFormat (X11DRV.@)
533 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
534 ERR("No OpenGL support compiled in.\n");
536 return 0;
539 /***********************************************************************
540 * SetPixelFormat (X11DRV.@)
542 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
543 int iPixelFormat,
544 const PIXELFORMATDESCRIPTOR *ppfd) {
545 ERR("No OpenGL support compiled in.\n");
547 return FALSE;
550 /***********************************************************************
551 * SwapBuffers (X11DRV.@)
553 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
554 ERR("No OpenGL support compiled in.\n");
556 return FALSE;
559 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
561 return NULL;
564 #endif /* defined(HAVE_OPENGL) */