gdi32: Don't hold the GDI lock while calling the driver OpenGL functions.
[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 HDC default_hdc = 0;
42
43 typedef struct opengl_context
44 {
45     HDC hdc;
46 } *OPENGL_Context;
47
48 /* We route all wgl functions from opengl32.dll through gdi32.dll to
49  * the display driver. Various wgl calls have a hDC as one of their parameters.
50  * Using get_dc_ptr we get access to the functions exported by the driver.
51  * Some functions don't receive a hDC. This function creates a global hdc and
52  * if there's already a global hdc, it returns it.
53  */
54 static DC* OPENGL_GetDefaultDC(void)
55 {
56     if(!default_hdc)
57         default_hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
58
59     return get_dc_ptr(default_hdc);
60 }
61
62 /***********************************************************************
63  *              wglCreateContext (OPENGL32.@)
64  */
65 HGLRC WINAPI wglCreateContext(HDC hdc)
66 {
67     HGLRC ret = 0;
68     DC * dc = get_dc_ptr( hdc );
69
70     TRACE("(%p)\n",hdc);
71
72     if (!dc) return 0;
73
74     update_dc( dc );
75     if (!dc->funcs->pwglCreateContext) FIXME(" :stub\n");
76     else ret = dc->funcs->pwglCreateContext(dc->physDev);
77
78     release_dc_ptr( dc );
79     return ret;
80 }
81
82
83 /***********************************************************************
84  *              wglDeleteContext (OPENGL32.@)
85  */
86 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
87 {
88     DC *dc;
89     BOOL ret = FALSE;
90     OPENGL_Context ctx = (OPENGL_Context)hglrc;
91
92     TRACE("hglrc: (%p)\n", hglrc);
93     if(ctx == NULL)
94         return FALSE;
95
96     /* Retrieve the HDC associated with the context to access the display driver */
97     dc = get_dc_ptr(ctx->hdc);
98     if (!dc) return FALSE;
99
100     if (!dc->funcs->pwglDeleteContext) FIXME(" :stub\n");
101     else ret = dc->funcs->pwglDeleteContext(hglrc);
102
103     release_dc_ptr( dc );
104     return ret;
105 }
106
107 /***********************************************************************
108  *              wglGetCurrentContext (OPENGL32.@)
109  */
110 HGLRC WINAPI wglGetCurrentContext(void)
111 {
112     HGLRC ret = NtCurrentTeb()->glContext;
113     TRACE(" returning %p\n", ret);
114     return ret;
115 }
116
117 /***********************************************************************
118  *              wglGetCurrentDC (OPENGL32.@)
119  */
120 HDC WINAPI wglGetCurrentDC(void)
121 {
122     OPENGL_Context ctx = (OPENGL_Context)wglGetCurrentContext();
123
124     TRACE(" found context: %p\n", ctx);
125     if(ctx == NULL)
126         return NULL;
127
128     /* Retrieve the current DC from the active context */
129     TRACE(" returning hdc: %p\n", ctx->hdc);
130     return ctx->hdc;
131 }
132
133 /***********************************************************************
134  *              wglGetPbufferDCARB
135  */
136 static HDC WINAPI wglGetPbufferDCARB(void *pbuffer)
137 {
138     HDC ret = 0;
139
140     /* Create a device context to associate with the pbuffer */
141     HDC hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
142     DC *dc = get_dc_ptr(hdc);
143
144     TRACE("(%p)\n", pbuffer);
145
146     if (!dc) return FALSE;
147
148     /* The display driver has to do the rest of the work because
149      * we need access to lowlevel datatypes which we can't access here
150      */
151     if (!dc->funcs->pwglGetPbufferDCARB) FIXME(" :stub\n");
152     else ret = dc->funcs->pwglGetPbufferDCARB(dc->physDev, pbuffer);
153
154     TRACE("(%p), hdc=%p\n", pbuffer, ret);
155
156     release_dc_ptr( dc );
157     return ret;
158 }
159
160 /***********************************************************************
161  *              wglMakeCurrent (OPENGL32.@)
162  */
163 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
164 {
165     BOOL ret = FALSE;
166     DC * dc = NULL;
167
168     /* When the context hglrc is NULL, the HDC is ignored and can be NULL.
169      * In that case use the global hDC to get access to the driver.  */
170     if(hglrc == NULL)
171         dc = OPENGL_GetDefaultDC();
172     else
173         dc = get_dc_ptr( hdc );
174
175     TRACE("hdc: (%p), hglrc: (%p)\n", hdc, hglrc);
176
177     if (!dc) return FALSE;
178
179     update_dc( dc );
180     if (!dc->funcs->pwglMakeCurrent) FIXME(" :stub\n");
181     else ret = dc->funcs->pwglMakeCurrent(dc->physDev,hglrc);
182
183     release_dc_ptr( dc );
184     return ret;
185 }
186
187 /***********************************************************************
188  *              wglMakeContextCurrentARB
189  */
190 static BOOL WINAPI wglMakeContextCurrentARB(HDC hDrawDC, HDC hReadDC, HGLRC hglrc)
191 {
192     BOOL ret = FALSE;
193     DC *DrawDC;
194     DC *ReadDC;
195
196     TRACE("hDrawDC: (%p), hReadDC: (%p) hglrc: (%p)\n", hDrawDC, hReadDC, hglrc);
197
198     /* Both hDrawDC and hReadDC need to be valid */
199     DrawDC = get_dc_ptr( hDrawDC );
200     if (!DrawDC) return FALSE;
201
202     ReadDC = get_dc_ptr( hReadDC );
203     if (!ReadDC) {
204         release_dc_ptr( DrawDC );
205         return FALSE;
206     }
207
208     update_dc( DrawDC );
209     update_dc( ReadDC );
210     if (!DrawDC->funcs->pwglMakeContextCurrentARB) FIXME(" :stub\n");
211     else ret = DrawDC->funcs->pwglMakeContextCurrentARB(DrawDC->physDev, ReadDC->physDev, hglrc);
212
213     release_dc_ptr( DrawDC );
214     release_dc_ptr( ReadDC );
215     return ret;
216 }
217
218 /***********************************************************************
219  *              wglShareLists (OPENGL32.@)
220  */
221 BOOL WINAPI wglShareLists(HGLRC hglrc1, HGLRC hglrc2)
222 {
223     DC *dc;
224     BOOL ret = FALSE;
225     OPENGL_Context ctx = (OPENGL_Context)hglrc1;
226
227     TRACE("hglrc1: (%p); hglrc: (%p)\n", hglrc1, hglrc2);
228     if(ctx == NULL)
229         return FALSE;
230
231     /* Retrieve the HDC associated with the context to access the display driver */
232     dc = get_dc_ptr(ctx->hdc);
233     if (!dc) return FALSE;
234
235     if (!dc->funcs->pwglShareLists) FIXME(" :stub\n");
236     else ret = dc->funcs->pwglShareLists(hglrc1, hglrc2);
237
238     release_dc_ptr( dc );
239     return ret;
240 }
241
242 /***********************************************************************
243  *              wglUseFontBitmapsA (OPENGL32.@)
244  */
245 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
246 {
247     BOOL ret = FALSE;
248     DC * dc = get_dc_ptr( hdc );
249
250     TRACE("(%p, %d, %d, %d)\n", hdc, first, count, listBase);
251
252     if (!dc) return FALSE;
253
254     if (!dc->funcs->pwglUseFontBitmapsA) FIXME(" :stub\n");
255     else ret = dc->funcs->pwglUseFontBitmapsA(dc->physDev, first, count, listBase);
256
257     release_dc_ptr( dc );
258     return ret;
259 }
260
261 /***********************************************************************
262  *              wglUseFontBitmapsW (OPENGL32.@)
263  */
264 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
265 {
266     BOOL ret = FALSE;
267     DC * dc = get_dc_ptr( hdc );
268
269     TRACE("(%p, %d, %d, %d)\n", hdc, first, count, listBase);
270
271     if (!dc) return FALSE;
272
273     if (!dc->funcs->pwglUseFontBitmapsW) FIXME(" :stub\n");
274     else ret = dc->funcs->pwglUseFontBitmapsW(dc->physDev, first, count, listBase);
275
276     release_dc_ptr( dc );
277     return ret;
278 }
279
280 /***********************************************************************
281  *              Internal wglGetProcAddress for retrieving WGL extensions
282  */
283 PROC WINAPI wglGetProcAddress(LPCSTR func)
284 {
285     PROC ret = NULL;
286     DC *dc;
287
288     if(!func)
289         return NULL;
290
291     TRACE("func: '%s'\n", func);
292
293     /* Retrieve the global hDC to get access to the driver.  */
294     dc = OPENGL_GetDefaultDC();
295     if (!dc) return FALSE;
296
297     if (!dc->funcs->pwglGetProcAddress) FIXME(" :stub\n");
298     else ret = dc->funcs->pwglGetProcAddress(func);
299
300     release_dc_ptr( dc );
301
302     /* At the moment we implement one WGL extension which requires a HDC. When we
303      * are looking up this call and when the Extension is available (that is the case
304      * when a non-NULL value is returned by wglGetProcAddress), we return the address
305      * of a wrapper function which will handle the HDC->PhysDev conversion.
306      */
307     if(ret && strcmp(func, "wglMakeContextCurrentARB") == 0)
308         return (PROC)wglMakeContextCurrentARB;
309     else if(ret && strcmp(func, "wglGetPbufferDCARB") == 0)
310         return (PROC)wglGetPbufferDCARB;
311
312     return ret;
313 }