wbemprox: Implement IEnumWbemClassObject::Clone.
[wine] / dlls / gdi32 / opengl.c
1 /*
2  * OpenGL function forwarding to the display driver
3  *
4  * Copyright (c) 1999 Lionel Ulmer
5  * Copyright (c) 2005 Raphael Junqueira
6  * Copyright (c) 2006 Roderick Colenbrander
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winerror.h"
34 #include "winternl.h"
35 #include "winnt.h"
36 #include "gdi_private.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
40
41 static const WCHAR opengl32W[] = {'o','p','e','n','g','l','3','2','.','d','l','l',0};
42 static HMODULE opengl32;
43 static INT (WINAPI *wglChoosePixelFormat)(HDC,const PIXELFORMATDESCRIPTOR *);
44 static INT (WINAPI *wglDescribePixelFormat)(HDC,INT,UINT,PIXELFORMATDESCRIPTOR*);
45 static BOOL (WINAPI *wglSetPixelFormat)(HDC,INT,const PIXELFORMATDESCRIPTOR*);
46 static BOOL (WINAPI *wglSwapBuffers)(HDC);
47
48 static HDC default_hdc = 0;
49
50 /* We route all wgl functions from opengl32.dll through gdi32.dll to
51  * the display driver. Various wgl calls have a hDC as one of their parameters.
52  * Using get_dc_ptr we get access to the functions exported by the driver.
53  * Some functions don't receive a hDC. This function creates a global hdc and
54  * if there's already a global hdc, it returns it.
55  */
56 static DC* OPENGL_GetDefaultDC(void)
57 {
58     if(!default_hdc)
59         default_hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
60
61     return get_dc_ptr(default_hdc);
62 }
63
64 /***********************************************************************
65  *              wglCreateContext (OPENGL32.@)
66  */
67 HGLRC WINAPI wglCreateContext(HDC hdc)
68 {
69     HGLRC ret = 0;
70     DC * dc = get_dc_ptr( hdc );
71
72     TRACE("(%p)\n",hdc);
73
74     if (dc)
75     {
76         PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglCreateContext );
77         update_dc( dc );
78         ret = physdev->funcs->pwglCreateContext( physdev );
79         release_dc_ptr( dc );
80     }
81     return ret;
82 }
83
84 /***********************************************************************
85  *      wglCreateContextAttribsARB
86  */
87 static HGLRC WINAPI wglCreateContextAttribsARB(HDC hdc, HGLRC hShareContext, const int *attributeList)
88 {
89     HGLRC ret = 0;
90     DC * dc = get_dc_ptr( hdc );
91
92     TRACE("(%p)\n",hdc);
93
94     if (dc)
95     {
96         PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglCreateContextAttribsARB );
97         update_dc( dc );
98         ret = physdev->funcs->pwglCreateContextAttribsARB( physdev, hShareContext, attributeList );
99         release_dc_ptr( dc );
100     }
101     return ret;
102 }
103
104 /**************************************************************************************
105  *      WINE-specific wglSetPixelFormat which can set the iPixelFormat multiple times
106  *
107  */
108 static BOOL WINAPI wglSetPixelFormatWINE(HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
109 {
110     INT bRet = FALSE;
111     DC * dc = get_dc_ptr( hdc );
112
113     TRACE("(%p,%d,%p)\n", hdc, iPixelFormat, ppfd);
114
115     if (dc)
116     {
117         PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglSetPixelFormatWINE );
118         update_dc( dc );
119         bRet = physdev->funcs->pwglSetPixelFormatWINE( physdev, iPixelFormat, ppfd );
120         release_dc_ptr( dc );
121     }
122     return bRet;
123 }
124
125 /***********************************************************************
126  *              Internal wglGetProcAddress for retrieving WGL extensions
127  */
128 PROC WINAPI wglGetProcAddress(LPCSTR func)
129 {
130     PROC ret = NULL;
131     DC *dc;
132
133     if(!func)
134         return NULL;
135
136     TRACE("func: '%s'\n", func);
137
138     /* Retrieve the global hDC to get access to the driver.  */
139     dc = OPENGL_GetDefaultDC();
140     if (dc)
141     {
142         PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglGetProcAddress );
143         ret = physdev->funcs->pwglGetProcAddress(func);
144         release_dc_ptr( dc );
145     }
146
147     /* At the moment we implement one WGL extension which requires a HDC. When we
148      * are looking up this call and when the Extension is available (that is the case
149      * when a non-NULL value is returned by wglGetProcAddress), we return the address
150      * of a wrapper function which will handle the HDC->PhysDev conversion.
151      */
152     if(ret && strcmp(func, "wglCreateContextAttribsARB") == 0)
153         return (PROC)wglCreateContextAttribsARB;
154     else if(ret && strcmp(func, "wglSetPixelFormatWINE") == 0)
155         return (PROC)wglSetPixelFormatWINE;
156
157     return ret;
158 }
159
160 /******************************************************************************
161  *              ChoosePixelFormat (GDI32.@)
162  */
163 INT WINAPI ChoosePixelFormat( HDC hdc, const PIXELFORMATDESCRIPTOR *pfd )
164 {
165     if (!wglChoosePixelFormat)
166     {
167         if (!opengl32) opengl32 = LoadLibraryW( opengl32W );
168         if (!(wglChoosePixelFormat = (void *)GetProcAddress( opengl32, "wglChoosePixelFormat" )))
169             return 0;
170     }
171     return wglChoosePixelFormat( hdc, pfd );
172 }
173
174 /******************************************************************************
175  *              DescribePixelFormat (GDI32.@)
176  */
177 INT WINAPI DescribePixelFormat( HDC hdc, INT fmt, UINT size, PIXELFORMATDESCRIPTOR *pfd )
178 {
179     if (!wglDescribePixelFormat)
180     {
181         if (!opengl32) opengl32 = LoadLibraryW( opengl32W );
182         if (!(wglDescribePixelFormat = (void *)GetProcAddress( opengl32, "wglDescribePixelFormat" )))
183             return 0;
184     }
185     return wglDescribePixelFormat( hdc, fmt, size, pfd );
186 }
187
188 /******************************************************************************
189  *              SetPixelFormat (GDI32.@)
190  */
191 BOOL WINAPI SetPixelFormat( HDC hdc, INT fmt, const PIXELFORMATDESCRIPTOR *pfd )
192 {
193     if (!wglSetPixelFormat)
194     {
195         if (!opengl32) opengl32 = LoadLibraryW( opengl32W );
196         if (!(wglSetPixelFormat = (void *)GetProcAddress( opengl32, "wglSetPixelFormat" )))
197             return 0;
198     }
199     return wglSetPixelFormat( hdc, fmt, pfd );
200 }
201
202 /******************************************************************************
203  *              SwapBuffers (GDI32.@)
204  */
205 BOOL WINAPI SwapBuffers( HDC hdc )
206 {
207     if (!wglSwapBuffers)
208     {
209         if (!opengl32) opengl32 = LoadLibraryW( opengl32W );
210         if (!(wglSwapBuffers = (void *)GetProcAddress( opengl32, "wglSwapBuffers" )))
211             return 0;
212     }
213     return wglSwapBuffers( hdc );
214 }