mshtml: Keep reference in node returned from get_node.
[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 /* We route all wgl functions from opengl32.dll through gdi32.dll to
44  * the display driver. Various wgl calls have a hDC as one of their parameters.
45  * Using get_dc_ptr we get access to the functions exported by the driver.
46  * Some functions don't receive a hDC. This function creates a global hdc and
47  * if there's already a global hdc, it returns it.
48  */
49 static DC* OPENGL_GetDefaultDC(void)
50 {
51     if(!default_hdc)
52         default_hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
53
54     return get_dc_ptr(default_hdc);
55 }
56
57 /***********************************************************************
58  *              wglCreateContext (OPENGL32.@)
59  */
60 HGLRC WINAPI wglCreateContext(HDC hdc)
61 {
62     HGLRC ret = 0;
63     DC * dc = get_dc_ptr( hdc );
64
65     TRACE("(%p)\n",hdc);
66
67     if (dc)
68     {
69         PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglCreateContext );
70         update_dc( dc );
71         ret = physdev->funcs->pwglCreateContext( physdev );
72         release_dc_ptr( dc );
73     }
74     return ret;
75 }
76
77 /***********************************************************************
78  *      wglCreateContextAttribsARB
79  */
80 static HGLRC WINAPI wglCreateContextAttribsARB(HDC hdc, HGLRC hShareContext, const int *attributeList)
81 {
82     HGLRC ret = 0;
83     DC * dc = get_dc_ptr( hdc );
84
85     TRACE("(%p)\n",hdc);
86
87     if (dc)
88     {
89         PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglCreateContextAttribsARB );
90         update_dc( dc );
91         ret = physdev->funcs->pwglCreateContextAttribsARB( physdev, hShareContext, attributeList );
92         release_dc_ptr( dc );
93     }
94     return ret;
95 }
96
97 /***********************************************************************
98  *              wglMakeCurrent (OPENGL32.@)
99  */
100 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
101 {
102     BOOL ret = FALSE;
103     DC * dc = NULL;
104
105     /* When the context hglrc is NULL, the HDC is ignored and can be NULL.
106      * In that case use the global hDC to get access to the driver.  */
107     if(hglrc == NULL)
108     {
109         if (hdc == NULL && !NtCurrentTeb()->glContext)
110         {
111             WARN( "Current context is NULL\n");
112             SetLastError( ERROR_INVALID_HANDLE );
113             return FALSE;
114         }
115         dc = OPENGL_GetDefaultDC();
116     }
117     else
118         dc = get_dc_ptr( hdc );
119
120     TRACE("hdc: (%p), hglrc: (%p)\n", hdc, hglrc);
121
122     if (dc)
123     {
124         PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglMakeCurrent );
125         update_dc( dc );
126         ret = physdev->funcs->pwglMakeCurrent( physdev, hglrc );
127         release_dc_ptr( dc );
128     }
129     return ret;
130 }
131
132 /***********************************************************************
133  *              wglMakeContextCurrentARB
134  */
135 static BOOL WINAPI wglMakeContextCurrentARB(HDC hDrawDC, HDC hReadDC, HGLRC hglrc)
136 {
137     BOOL ret = FALSE;
138     PHYSDEV draw_physdev, read_physdev;
139     DC *DrawDC;
140     DC *ReadDC;
141
142     TRACE("hDrawDC: (%p), hReadDC: (%p) hglrc: (%p)\n", hDrawDC, hReadDC, hglrc);
143
144     /* Both hDrawDC and hReadDC need to be valid */
145     DrawDC = get_dc_ptr( hDrawDC );
146     if (!DrawDC) return FALSE;
147
148     ReadDC = get_dc_ptr( hReadDC );
149     if (!ReadDC) {
150         release_dc_ptr( DrawDC );
151         return FALSE;
152     }
153
154     update_dc( DrawDC );
155     update_dc( ReadDC );
156     draw_physdev = GET_DC_PHYSDEV( DrawDC, pwglMakeContextCurrentARB );
157     read_physdev = GET_DC_PHYSDEV( ReadDC, pwglMakeContextCurrentARB );
158     if (draw_physdev->funcs == read_physdev->funcs)
159         ret = draw_physdev->funcs->pwglMakeContextCurrentARB(draw_physdev, read_physdev, hglrc);
160     release_dc_ptr( DrawDC );
161     release_dc_ptr( ReadDC );
162     return ret;
163 }
164
165 /**************************************************************************************
166  *      WINE-specific wglSetPixelFormat which can set the iPixelFormat multiple times
167  *
168  */
169 static BOOL WINAPI wglSetPixelFormatWINE(HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
170 {
171     INT bRet = FALSE;
172     DC * dc = get_dc_ptr( hdc );
173
174     TRACE("(%p,%d,%p)\n", hdc, iPixelFormat, ppfd);
175
176     if (dc)
177     {
178         PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglSetPixelFormatWINE );
179         update_dc( dc );
180         bRet = physdev->funcs->pwglSetPixelFormatWINE( physdev, iPixelFormat, ppfd );
181         release_dc_ptr( dc );
182     }
183     return bRet;
184 }
185
186 /***********************************************************************
187  *              Internal wglGetProcAddress for retrieving WGL extensions
188  */
189 PROC WINAPI wglGetProcAddress(LPCSTR func)
190 {
191     PROC ret = NULL;
192     DC *dc;
193
194     if(!func)
195         return NULL;
196
197     TRACE("func: '%s'\n", func);
198
199     /* Retrieve the global hDC to get access to the driver.  */
200     dc = OPENGL_GetDefaultDC();
201     if (dc)
202     {
203         PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglGetProcAddress );
204         ret = physdev->funcs->pwglGetProcAddress(func);
205         release_dc_ptr( dc );
206     }
207
208     /* At the moment we implement one WGL extension which requires a HDC. When we
209      * are looking up this call and when the Extension is available (that is the case
210      * when a non-NULL value is returned by wglGetProcAddress), we return the address
211      * of a wrapper function which will handle the HDC->PhysDev conversion.
212      */
213     if(ret && strcmp(func, "wglCreateContextAttribsARB") == 0)
214         return (PROC)wglCreateContextAttribsARB;
215     else if(ret && strcmp(func, "wglMakeContextCurrentARB") == 0)
216         return (PROC)wglMakeContextCurrentARB;
217     else if(ret && strcmp(func, "wglSetPixelFormatWINE") == 0)
218         return (PROC)wglSetPixelFormatWINE;
219
220     return ret;
221 }