Import wine_tsx11_lock/unlock directly from x11drv in opengl32 and
[wine] / graphics / 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
23 #include "ts_xlib.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "gdi.h"
29 #include "x11drv.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(opengl);
33
34 #ifdef HAVE_OPENGL
35
36 #undef APIENTRY
37 #undef CALLBACK
38 #undef WINAPI
39
40 #define XMD_H /* This is to prevent the Xmd.h inclusion bug :-/ */
41 #include <GL/gl.h>
42 #include <GL/glx.h>
43 #ifdef HAVE_GL_GLEXT_H
44 # include <GL/glext.h>
45 #endif
46 #undef  XMD_H
47
48 #undef APIENTRY
49 #undef CALLBACK
50 #undef WINAPI
51
52 /* Redefines the constants */
53 #define CALLBACK    __stdcall
54 #define WINAPI      __stdcall
55 #define APIENTRY    WINAPI
56
57
58 static void dump_PIXELFORMATDESCRIPTOR(PIXELFORMATDESCRIPTOR *ppfd) {
59   DPRINTF("  - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
60   DPRINTF("  - dwFlags : ");
61 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) DPRINTF(#tv " ")
62   TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
63   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
64   TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
65   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
66   TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
67   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
68   TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
69   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
70   TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
71   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
72   TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
73   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
74   TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
75   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
76   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
77   TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
78 #undef TEST_AND_DUMP
79   DPRINTF("\n");
80
81   DPRINTF("  - iPixelType : ");
82   switch (ppfd->iPixelType) {
83   case PFD_TYPE_RGBA: DPRINTF("PFD_TYPE_RGBA"); break;
84   case PFD_TYPE_COLORINDEX: DPRINTF("PFD_TYPE_COLORINDEX"); break;
85   }
86   DPRINTF("\n");
87
88   DPRINTF("  - Color   : %d\n", ppfd->cColorBits);
89   DPRINTF("  - Alpha   : %d\n", ppfd->cAlphaBits);
90   DPRINTF("  - Accum   : %d\n", ppfd->cAccumBits);
91   DPRINTF("  - Depth   : %d\n", ppfd->cDepthBits);
92   DPRINTF("  - Stencil : %d\n", ppfd->cStencilBits);
93   DPRINTF("  - Aux     : %d\n", ppfd->cAuxBuffers);
94
95   DPRINTF("  - iLayerType : ");
96   switch (ppfd->iLayerType) {
97   case PFD_MAIN_PLANE: DPRINTF("PFD_MAIN_PLANE"); break;
98   case PFD_OVERLAY_PLANE: DPRINTF("PFD_OVERLAY_PLANE"); break;
99   case PFD_UNDERLAY_PLANE: DPRINTF("PFD_UNDERLAY_PLANE"); break;
100   }
101   DPRINTF("\n");
102 }
103
104 /* X11DRV_ChoosePixelFormat
105
106      Equivalent of glXChooseVisual
107 */
108 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
109                              const PIXELFORMATDESCRIPTOR *ppfd) {
110 #define TEST_AND_ADD1(t,a) if (t) att_list[att_pos++] = a
111 #define TEST_AND_ADD2(t,a,b) if (t) { att_list[att_pos++] = a; att_list[att_pos++] = b; }
112 #define NULL_TEST_AND_ADD2(tv,a,b) att_list[att_pos++] = a; att_list[att_pos++] = ((tv) == 0 ? 0 : b)
113 #define ADD2(a,b) att_list[att_pos++] = a; att_list[att_pos++] = b
114
115   int att_list[64];
116   int att_pos = 0;
117   XVisualInfo *vis;
118   int i;
119
120   if (TRACE_ON(opengl)) {
121     TRACE("(%p,%p)\n", physDev, ppfd);
122
123     dump_PIXELFORMATDESCRIPTOR((PIXELFORMATDESCRIPTOR *) ppfd);
124   }
125
126   if (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) {
127     ERR("Flag not supported !\n");
128     /* Should SetError here... */
129     return 0;
130   }
131
132   /* Now, build the request to GLX */
133   TEST_AND_ADD1(ppfd->dwFlags & PFD_DOUBLEBUFFER, GLX_DOUBLEBUFFER);
134   TEST_AND_ADD1(ppfd->dwFlags & PFD_STEREO, GLX_STEREO);
135   TEST_AND_ADD1(ppfd->iPixelType == PFD_TYPE_RGBA, GLX_RGBA);
136   TEST_AND_ADD2(ppfd->iPixelType == PFD_TYPE_COLORINDEX, GLX_BUFFER_SIZE, ppfd->cColorBits);
137
138   NULL_TEST_AND_ADD2(ppfd->cDepthBits, GLX_DEPTH_SIZE, 8);
139   /* These flags are not supported yet...
140
141      NULL_TEST_AND_ADD2(ppfd->cAlphaBits, GLX_ALPHA_SIZE, 8);
142      ADD2(GLX_ACCUM_SIZE, ppfd->cAccumBits); */
143   ADD2(GLX_STENCIL_SIZE, ppfd->cStencilBits); /* now suported */
144   /*   ADD2(GLX_AUX_BUFFERS, ppfd->cAuxBuffers); */
145   att_list[att_pos] = None;
146
147   wine_tsx11_lock(); {
148     /*
149        This command cannot be used as we need to use the default visual...
150        Let's hope it at least contains some OpenGL functionnalities
151
152        vis = glXChooseVisual(gdi_display, DefaultScreen(gdi_display), att_list);
153     */
154     int num;
155     XVisualInfo template;
156
157     template.visualid = XVisualIDFromVisual(visual);
158     vis = XGetVisualInfo(gdi_display, VisualIDMask, &template, &num);
159
160     TRACE("Found visual : %p - returns %d\n", vis, physDev->used_visuals + 1);
161   }
162   wine_tsx11_unlock();
163
164   if (vis == NULL) {
165     ERR("No visual found !\n");
166     /* Should SetError here... */
167     return 0;
168   }
169   /* try to find the visualid in the already created visuals */
170   for( i=0; i<physDev->used_visuals; i++ ) {
171     if ( vis->visualid == physDev->visuals[i]->visualid ) {
172       XFree(vis);
173       return i+1;
174     }
175   }
176   /* now give up, if the maximum is reached */
177   if (physDev->used_visuals == MAX_PIXELFORMATS) {
178     ERR("Maximum number of visuals reached !\n");
179     /* Should SetError here... */
180     return 0;
181   }
182   physDev->visuals[physDev->used_visuals++] = vis;
183
184   return physDev->used_visuals;
185 }
186
187 /* X11DRV_DescribePixelFormat
188
189      Get the pixel-format descriptor associated to the given id
190 */
191 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
192                                int iPixelFormat,
193                                UINT nBytes,
194                                PIXELFORMATDESCRIPTOR *ppfd) {
195   XVisualInfo *vis;
196   int value;
197   int rb,gb,bb,ab;
198
199   TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
200
201   if (ppfd == NULL) {
202     /* The application is only querying the number of visuals */
203     return MAX_PIXELFORMATS;
204   }
205
206   if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
207     ERR("Wrong structure size !\n");
208     /* Should set error */
209     return 0;
210   }
211   if ((iPixelFormat > MAX_PIXELFORMATS) ||
212       (iPixelFormat > physDev->used_visuals + 1) ||
213       (iPixelFormat <= 0)) {
214     ERR("Wrong pixel format !\n");
215     /* Should set error */
216     return 0;
217   }
218
219   if (iPixelFormat == physDev->used_visuals + 1) {
220     int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
221
222     /* Create a 'standard' X Visual */
223     wine_tsx11_lock();
224     vis = glXChooseVisual(gdi_display, DefaultScreen(gdi_display), dblBuf);
225     wine_tsx11_unlock();
226
227     WARN("Uninitialized Visual. Creating standard (%p) !\n", vis);
228
229     if (vis == NULL) {
230       ERR("Could not create standard visual !\n");
231       /* Should set error */
232       return 0;
233     }
234
235     physDev->visuals[physDev->used_visuals++] = vis;
236   }
237   vis = physDev->visuals[iPixelFormat - 1];
238
239   memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
240   ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
241   ppfd->nVersion = 1;
242
243   /* These flags are always the same... */
244   ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_GENERIC_ACCELERATED;
245   /* Now the flags extraced from the Visual */
246   wine_tsx11_lock();
247   glXGetConfig(gdi_display, vis, GLX_DOUBLEBUFFER, &value); if (value) ppfd->dwFlags |= PFD_DOUBLEBUFFER;
248   glXGetConfig(gdi_display, vis, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
249
250   /* Pixel type */
251   glXGetConfig(gdi_display, vis, GLX_RGBA, &value);
252   if (value)
253     ppfd->iPixelType = PFD_TYPE_RGBA;
254   else
255     ppfd->iPixelType = PFD_TYPE_COLORINDEX;
256
257   /* Color bits */
258   glXGetConfig(gdi_display, vis, GLX_BUFFER_SIZE, &value);
259   ppfd->cColorBits = value;
260
261   /* Red, green, blue and alpha bits / shifts */
262   if (ppfd->iPixelType == PFD_TYPE_RGBA) {
263     glXGetConfig(gdi_display, vis, GLX_RED_SIZE, &rb);
264     glXGetConfig(gdi_display, vis, GLX_GREEN_SIZE, &gb);
265     glXGetConfig(gdi_display, vis, GLX_BLUE_SIZE, &bb);
266     glXGetConfig(gdi_display, vis, GLX_ALPHA_SIZE, &ab);
267
268     ppfd->cRedBits = rb;
269     ppfd->cRedShift = gb + bb + ab;
270     ppfd->cBlueBits = bb;
271     ppfd->cBlueShift = ab;
272     ppfd->cGreenBits = gb;
273     ppfd->cGreenShift = bb + ab;
274     ppfd->cAlphaBits = ab;
275     ppfd->cAlphaShift = 0;
276   } else {
277     ppfd->cRedBits = 0;
278     ppfd->cRedShift = 0;
279     ppfd->cBlueBits = 0;
280     ppfd->cBlueShift = 0;
281     ppfd->cGreenBits = 0;
282     ppfd->cGreenShift = 0;
283     ppfd->cAlphaBits = 0;
284     ppfd->cAlphaShift = 0;
285   }
286   /* Accums : to do ... */
287
288   /* Depth bits */
289   glXGetConfig(gdi_display, vis, GLX_DEPTH_SIZE, &value);
290   ppfd->cDepthBits = value;
291
292   /* stencil bits */
293   glXGetConfig( gdi_display, vis, GLX_STENCIL_SIZE, &value );
294   ppfd->cStencilBits = value;
295
296   wine_tsx11_unlock();
297
298   /* Aux : to do ... */
299
300   ppfd->iLayerType = PFD_MAIN_PLANE;
301
302   if (TRACE_ON(opengl)) {
303     dump_PIXELFORMATDESCRIPTOR(ppfd);
304   }
305
306   return MAX_PIXELFORMATS;
307 }
308
309 /* X11DRV_GetPixelFormat
310
311      Get the pixel-format id used by this DC
312 */
313 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
314   TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
315
316   return physDev->current_pf;
317 }
318
319 /* X11DRV_SetPixelFormat
320
321      Set the pixel-format id used by this DC
322 */
323 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
324                            int iPixelFormat,
325                            const PIXELFORMATDESCRIPTOR *ppfd) {
326   TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
327
328   physDev->current_pf = iPixelFormat;
329
330   return TRUE;
331 }
332
333 /* X11DRV_SwapBuffers
334
335      Swap the buffers of this DC
336 */
337 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
338   TRACE("(%p)\n", physDev);
339
340   wine_tsx11_lock();
341   glXSwapBuffers(gdi_display, physDev->drawable);
342   wine_tsx11_unlock();
343
344   return TRUE;
345 }
346
347 /***********************************************************************
348  *              X11DRV_setup_opengl_visual
349  *
350  * Setup the default visual used for OpenGL and Direct3D, and the desktop
351  * window (if it exists).  If OpenGL isn't available, the visual is simply
352  * set to the default visual for the display
353  */
354 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
355 {
356     int err_base, evt_base;
357     XVisualInfo *visual = NULL;
358
359     /* In order to support OpenGL or D3D, we require a double-buffered
360      * visual */
361     wine_tsx11_lock();
362     if (glXQueryExtension(display, &err_base, &evt_base) == True)
363     {
364         int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
365         visual = glXChooseVisual(display, DefaultScreen(display), dblBuf);
366     }
367     wine_tsx11_unlock();
368     return visual;
369 }
370
371 #else  /* defined(HAVE_OPENGL) */
372
373 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
374                              const PIXELFORMATDESCRIPTOR *ppfd) {
375   ERR("No OpenGL support compiled in.\n");
376
377   return 0;
378 }
379
380 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
381                                int iPixelFormat,
382                                UINT nBytes,
383                                PIXELFORMATDESCRIPTOR *ppfd) {
384   ERR("No OpenGL support compiled in.\n");
385
386   return 0;
387 }
388
389 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
390   ERR("No OpenGL support compiled in.\n");
391
392   return 0;
393 }
394
395 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
396                            int iPixelFormat,
397                            const PIXELFORMATDESCRIPTOR *ppfd) {
398   ERR("No OpenGL support compiled in.\n");
399
400   return FALSE;
401 }
402
403 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
404   ERR("No OpenGL support compiled in.\n");
405
406   return FALSE;
407 }
408
409 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
410 {
411   return NULL;
412 }
413
414 #endif /* defined(HAVE_OPENGL) */