riched20: Fix one more memory leak.
[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.h"
37 #include "gdi_private.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
41
42 static HDC default_hdc = 0;
43
44 typedef struct opengl_context
45 {
46     HDC hdc;
47 } *OPENGL_Context;
48
49 /* We route all wgl functions from opengl32.dll through gdi32.dll to
50  * the display driver. Various wgl calls have a hDC as one of their parameters.
51  * Using DC_GetDCPtr we get access to the functions exported by the driver.
52  * Some functions don't receive a hDC. This function creates a global hdc and
53  * if there's already a global hdc, it returns it.
54  */
55 static DC* OPENGL_GetDefaultDC()
56 {
57     if(!default_hdc)
58         default_hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
59         
60     return DC_GetDCPtr(default_hdc);
61 }
62
63 /***********************************************************************
64  *              wglCreateContext (OPENGL32.@)
65  */
66 HGLRC WINAPI wglCreateContext(HDC hdc)
67 {
68     HGLRC ret = 0;
69     DC * dc = DC_GetDCPtr( hdc );
70
71     TRACE("(%p)\n",hdc);
72
73     if (!dc) return 0;
74
75     if (!dc->funcs->pwglCreateContext) FIXME(" :stub\n");
76     else ret = dc->funcs->pwglCreateContext(dc->physDev);
77
78     GDI_ReleaseObj( hdc );
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 = DC_GetDCPtr(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     GDI_ReleaseObj(ctx->hdc);
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 = DC_GetDCPtr(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     GDI_ReleaseObj(hdc);
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 = DC_GetDCPtr( hdc );
174
175     TRACE("hdc: (%p), hglrc: (%p)\n", hdc, hglrc);
176
177     if (!dc) return FALSE;
178
179     if (!dc->funcs->pwglMakeCurrent) FIXME(" :stub\n");
180     else ret = dc->funcs->pwglMakeCurrent(dc->physDev,hglrc);
181
182     if(hglrc == NULL)
183         GDI_ReleaseObj(default_hdc);
184     else
185         GDI_ReleaseObj(hdc);
186
187     return ret;
188 }
189
190 /***********************************************************************
191  *              wglMakeContextCurrentARB
192  */
193 static BOOL WINAPI wglMakeContextCurrentARB(HDC hDrawDC, HDC hReadDC, HGLRC hglrc)
194 {
195     BOOL ret = FALSE;
196     DC *DrawDC;
197     DC *ReadDC;
198
199     TRACE("hDrawDC: (%p), hReadDC: (%p) hglrc: (%p)\n", hDrawDC, hReadDC, hglrc);
200
201     /* Both hDrawDC and hReadDC need to be valid */
202     DrawDC = DC_GetDCPtr( hDrawDC);
203     if (!DrawDC) return FALSE;
204
205     ReadDC = DC_GetDCPtr( hReadDC);
206     if (!ReadDC) {
207         GDI_ReleaseObj(hDrawDC);
208         return FALSE;
209     }
210
211     if (!DrawDC->funcs->pwglMakeContextCurrentARB) FIXME(" :stub\n");
212     else ret = DrawDC->funcs->pwglMakeContextCurrentARB(DrawDC->physDev, ReadDC->physDev, hglrc);
213
214     GDI_ReleaseObj(hDrawDC);
215     GDI_ReleaseObj(hReadDC);
216
217     return ret;
218 }
219
220 /***********************************************************************
221  *              wglShareLists (OPENGL32.@)
222  */
223 BOOL WINAPI wglShareLists(HGLRC hglrc1, HGLRC hglrc2)
224 {
225     DC *dc;
226     BOOL ret = FALSE;
227     OPENGL_Context ctx = (OPENGL_Context)hglrc1;
228
229     TRACE("hglrc1: (%p); hglrc: (%p)\n", hglrc1, hglrc2);
230     if(ctx == NULL)
231         return FALSE;
232     
233     /* Retrieve the HDC associated with the context to access the display driver */
234     dc = DC_GetDCPtr(ctx->hdc);
235     if (!dc) return FALSE;
236
237     if (!dc->funcs->pwglShareLists) FIXME(" :stub\n");
238     else ret = dc->funcs->pwglShareLists(hglrc1, hglrc2);
239
240     GDI_ReleaseObj(ctx->hdc);
241     return ret;
242 }
243
244 /***********************************************************************
245  *              wglUseFontBitmapsA (OPENGL32.@)
246  */
247 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
248 {
249     BOOL ret = FALSE;
250     DC * dc = DC_GetDCPtr( hdc );
251
252     TRACE("(%p, %d, %d, %d)\n", hdc, first, count, listBase);
253
254     if (!dc) return FALSE;
255
256     if (!dc->funcs->pwglUseFontBitmapsA) FIXME(" :stub\n");
257     else ret = dc->funcs->pwglUseFontBitmapsA(dc->physDev, first, count, listBase);
258
259     GDI_ReleaseObj( hdc);
260     return ret;
261 }
262
263 /***********************************************************************
264  *              wglUseFontBitmapsW (OPENGL32.@)
265  */
266 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
267 {
268     BOOL ret = FALSE;
269     DC * dc = DC_GetDCPtr( hdc );
270
271     TRACE("(%p, %d, %d, %d)\n", hdc, first, count, listBase);
272
273     if (!dc) return FALSE;
274
275     if (!dc->funcs->pwglUseFontBitmapsW) FIXME(" :stub\n");
276     else ret = dc->funcs->pwglUseFontBitmapsW(dc->physDev, first, count, listBase);
277
278     GDI_ReleaseObj( hdc);
279     return ret;
280 }
281
282 /***********************************************************************
283  *              Internal wglGetProcAddress for retrieving WGL extensions
284  */
285 PROC WINAPI wglGetProcAddress(LPCSTR func)
286 {
287     PROC ret = NULL;
288     DC *dc;
289
290     if(!func)
291         return NULL;
292
293     TRACE("func: '%p'\n", func);
294
295     /* Retrieve the global hDC to get access to the driver.  */
296     dc = OPENGL_GetDefaultDC();
297     if (!dc) return FALSE;
298
299     if (!dc->funcs->pwglGetProcAddress) FIXME(" :stub\n");
300     else ret = dc->funcs->pwglGetProcAddress(func);
301
302     GDI_ReleaseObj(default_hdc);
303
304     /* At the moment we implement one WGL extension which requires a HDC. When we
305      * are looking up this call and when the Extension is available (that is the case
306      * when a non-NULL value is returned by wglGetProcAddress), we return the address
307      * of a wrapper function which will handle the HDC->PhysDev conversion.
308      */
309     if(ret && strcmp(func, "wglMakeContextCurrentARB") == 0)
310         return wglMakeContextCurrentARB;
311     else if(ret && strcmp(func, "wglGetPbufferDCARB") == 0)
312         return (PROC)wglGetPbufferDCARB;
313
314     return ret;
315 }