wined3d: Pass a wined3d_gl_info structure to send_attribute().
[wine/multimedia.git] / dlls / gdi32 / opengl.c
blob9193fc536dfca2782397dfa2fe388cf96ccb02b2
1 /*
2 * OpenGL function forwarding to the display driver
4 * Copyright (c) 1999 Lionel Ulmer
5 * Copyright (c) 2005 Raphael Junqueira
6 * Copyright (c) 2006 Roderick Colenbrander
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
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"
39 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
41 static HDC default_hdc = 0;
43 typedef struct opengl_context
45 HDC hdc;
46 } *OPENGL_Context;
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.
54 static DC* OPENGL_GetDefaultDC(void)
56 if(!default_hdc)
57 default_hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
59 return get_dc_ptr(default_hdc);
62 /***********************************************************************
63 * wglCopyContext (OPENGL32.@)
65 BOOL WINAPI wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
67 DC *dc;
68 BOOL ret = FALSE;
69 OPENGL_Context ctx = (OPENGL_Context)hglrcSrc;
71 TRACE("hglrcSrc: (%p), hglrcDst: (%p), mask: %#x\n", hglrcSrc, hglrcDst, mask);
72 /* If no context is set, this call doesn't have a purpose */
73 if(!hglrcSrc || !hglrcDst)
74 return FALSE;
76 /* Retrieve the HDC associated with the context to access the display driver */
77 dc = get_dc_ptr(ctx->hdc);
78 if (!dc) return FALSE;
80 if (!dc->funcs->pwglCopyContext) FIXME(" :stub\n");
81 else ret = dc->funcs->pwglCopyContext(hglrcSrc, hglrcDst, mask);
83 release_dc_ptr( dc );
84 return ret;
87 /***********************************************************************
88 * wglCreateContext (OPENGL32.@)
90 HGLRC WINAPI wglCreateContext(HDC hdc)
92 HGLRC ret = 0;
93 DC * dc = get_dc_ptr( hdc );
95 TRACE("(%p)\n",hdc);
97 if (!dc) return 0;
99 update_dc( dc );
100 if (!dc->funcs->pwglCreateContext) FIXME(" :stub\n");
101 else ret = dc->funcs->pwglCreateContext(dc->physDev);
103 release_dc_ptr( dc );
104 return ret;
107 /***********************************************************************
108 * wglCreateContextAttribsARB
110 static HGLRC WINAPI wglCreateContextAttribsARB(HDC hdc, HGLRC hShareContext, const int *attributeList)
112 HGLRC ret = 0;
113 DC * dc = get_dc_ptr( hdc );
115 TRACE("(%p)\n",hdc);
117 if (!dc) return 0;
119 update_dc( dc );
120 if (!dc->funcs->pwglCreateContextAttribsARB) FIXME(" :stub\n");
121 else ret = dc->funcs->pwglCreateContextAttribsARB(dc->physDev, hShareContext, attributeList);
123 release_dc_ptr( dc );
124 return ret;
127 /***********************************************************************
128 * wglDeleteContext (OPENGL32.@)
130 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
132 DC *dc;
133 BOOL ret = FALSE;
134 OPENGL_Context ctx = (OPENGL_Context)hglrc;
136 TRACE("hglrc: (%p)\n", hglrc);
137 if(ctx == NULL)
138 return FALSE;
140 /* Retrieve the HDC associated with the context to access the display driver */
141 dc = get_dc_ptr(ctx->hdc);
142 if (!dc) return FALSE;
144 if (!dc->funcs->pwglDeleteContext) FIXME(" :stub\n");
145 else ret = dc->funcs->pwglDeleteContext(hglrc);
147 release_dc_ptr( dc );
148 return ret;
151 /***********************************************************************
152 * wglGetCurrentContext (OPENGL32.@)
154 HGLRC WINAPI wglGetCurrentContext(void)
156 HGLRC ret = NtCurrentTeb()->glContext;
157 TRACE(" returning %p\n", ret);
158 return ret;
161 /***********************************************************************
162 * wglGetCurrentDC (OPENGL32.@)
164 HDC WINAPI wglGetCurrentDC(void)
166 OPENGL_Context ctx = (OPENGL_Context)wglGetCurrentContext();
168 TRACE(" found context: %p\n", ctx);
169 if(ctx == NULL)
170 return NULL;
172 /* Retrieve the current DC from the active context */
173 TRACE(" returning hdc: %p\n", ctx->hdc);
174 return ctx->hdc;
177 /***********************************************************************
178 * wglGetPbufferDCARB
180 static HDC WINAPI wglGetPbufferDCARB(void *pbuffer)
182 HDC ret = 0;
184 /* Create a device context to associate with the pbuffer */
185 HDC hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
186 DC *dc = get_dc_ptr(hdc);
188 TRACE("(%p)\n", pbuffer);
190 if (!dc) return 0;
192 /* The display driver has to do the rest of the work because
193 * we need access to lowlevel datatypes which we can't access here
195 if (!dc->funcs->pwglGetPbufferDCARB) FIXME(" :stub\n");
196 else ret = dc->funcs->pwglGetPbufferDCARB(dc->physDev, pbuffer);
198 TRACE("(%p), hdc=%p\n", pbuffer, ret);
200 release_dc_ptr( dc );
201 return ret;
204 /***********************************************************************
205 * wglMakeCurrent (OPENGL32.@)
207 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
209 BOOL ret = FALSE;
210 DC * dc = NULL;
212 /* When the context hglrc is NULL, the HDC is ignored and can be NULL.
213 * In that case use the global hDC to get access to the driver. */
214 if(hglrc == NULL)
216 if( hdc == NULL && !wglGetCurrentContext() )
218 WARN( "Current context is NULL\n");
219 SetLastError( ERROR_INVALID_HANDLE );
220 return FALSE;
222 dc = OPENGL_GetDefaultDC();
224 else
225 dc = get_dc_ptr( hdc );
227 TRACE("hdc: (%p), hglrc: (%p)\n", hdc, hglrc);
229 if (!dc) return FALSE;
231 update_dc( dc );
232 if (!dc->funcs->pwglMakeCurrent) FIXME(" :stub\n");
233 else ret = dc->funcs->pwglMakeCurrent(dc->physDev,hglrc);
235 release_dc_ptr( dc );
236 return ret;
239 /***********************************************************************
240 * wglMakeContextCurrentARB
242 static BOOL WINAPI wglMakeContextCurrentARB(HDC hDrawDC, HDC hReadDC, HGLRC hglrc)
244 BOOL ret = FALSE;
245 DC *DrawDC;
246 DC *ReadDC;
248 TRACE("hDrawDC: (%p), hReadDC: (%p) hglrc: (%p)\n", hDrawDC, hReadDC, hglrc);
250 /* Both hDrawDC and hReadDC need to be valid */
251 DrawDC = get_dc_ptr( hDrawDC );
252 if (!DrawDC) return FALSE;
254 ReadDC = get_dc_ptr( hReadDC );
255 if (!ReadDC) {
256 release_dc_ptr( DrawDC );
257 return FALSE;
260 update_dc( DrawDC );
261 update_dc( ReadDC );
262 if (!DrawDC->funcs->pwglMakeContextCurrentARB) FIXME(" :stub\n");
263 else ret = DrawDC->funcs->pwglMakeContextCurrentARB(DrawDC->physDev, ReadDC->physDev, hglrc);
265 release_dc_ptr( DrawDC );
266 release_dc_ptr( ReadDC );
267 return ret;
270 /**************************************************************************************
271 * WINE-specific wglSetPixelFormat which can set the iPixelFormat multiple times
274 static BOOL WINAPI wglSetPixelFormatWINE(HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
276 INT bRet = FALSE;
277 DC * dc = get_dc_ptr( hdc );
279 TRACE("(%p,%d,%p)\n", hdc, iPixelFormat, ppfd);
281 if (!dc) return 0;
283 update_dc( dc );
284 if (!dc->funcs->pwglSetPixelFormatWINE) FIXME(" :stub\n");
285 else bRet = dc->funcs->pwglSetPixelFormatWINE(dc->physDev, iPixelFormat, ppfd);
287 release_dc_ptr( dc );
288 return bRet;
291 /***********************************************************************
292 * wglShareLists (OPENGL32.@)
294 BOOL WINAPI wglShareLists(HGLRC hglrc1, HGLRC hglrc2)
296 DC *dc;
297 BOOL ret = FALSE;
298 OPENGL_Context ctx = (OPENGL_Context)hglrc1;
300 TRACE("hglrc1: (%p); hglrc: (%p)\n", hglrc1, hglrc2);
301 if(ctx == NULL || hglrc2 == NULL)
302 return FALSE;
304 /* Retrieve the HDC associated with the context to access the display driver */
305 dc = get_dc_ptr(ctx->hdc);
306 if (!dc) return FALSE;
308 if (!dc->funcs->pwglShareLists) FIXME(" :stub\n");
309 else ret = dc->funcs->pwglShareLists(hglrc1, hglrc2);
311 release_dc_ptr( dc );
312 return ret;
315 /***********************************************************************
316 * wglUseFontBitmapsA (OPENGL32.@)
318 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
320 BOOL ret = FALSE;
321 DC * dc = get_dc_ptr( hdc );
323 TRACE("(%p, %d, %d, %d)\n", hdc, first, count, listBase);
325 if (!dc) return FALSE;
327 if (!dc->funcs->pwglUseFontBitmapsA) FIXME(" :stub\n");
328 else ret = dc->funcs->pwglUseFontBitmapsA(dc->physDev, first, count, listBase);
330 release_dc_ptr( dc );
331 return ret;
334 /***********************************************************************
335 * wglUseFontBitmapsW (OPENGL32.@)
337 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
339 BOOL ret = FALSE;
340 DC * dc = get_dc_ptr( hdc );
342 TRACE("(%p, %d, %d, %d)\n", hdc, first, count, listBase);
344 if (!dc) return FALSE;
346 if (!dc->funcs->pwglUseFontBitmapsW) FIXME(" :stub\n");
347 else ret = dc->funcs->pwglUseFontBitmapsW(dc->physDev, first, count, listBase);
349 release_dc_ptr( dc );
350 return ret;
353 /***********************************************************************
354 * Internal wglGetProcAddress for retrieving WGL extensions
356 PROC WINAPI wglGetProcAddress(LPCSTR func)
358 PROC ret = NULL;
359 DC *dc;
361 if(!func)
362 return NULL;
364 TRACE("func: '%s'\n", func);
366 /* Retrieve the global hDC to get access to the driver. */
367 dc = OPENGL_GetDefaultDC();
368 if (!dc) return NULL;
370 if (!dc->funcs->pwglGetProcAddress) FIXME(" :stub\n");
371 else ret = dc->funcs->pwglGetProcAddress(func);
373 release_dc_ptr( dc );
375 /* At the moment we implement one WGL extension which requires a HDC. When we
376 * are looking up this call and when the Extension is available (that is the case
377 * when a non-NULL value is returned by wglGetProcAddress), we return the address
378 * of a wrapper function which will handle the HDC->PhysDev conversion.
380 if(ret && strcmp(func, "wglCreateContextAttribsARB") == 0)
381 return (PROC)wglCreateContextAttribsARB;
382 else if(ret && strcmp(func, "wglMakeContextCurrentARB") == 0)
383 return (PROC)wglMakeContextCurrentARB;
384 else if(ret && strcmp(func, "wglGetPbufferDCARB") == 0)
385 return (PROC)wglGetPbufferDCARB;
386 else if(ret && strcmp(func, "wglSetPixelFormatWINE") == 0)
387 return (PROC)wglSetPixelFormatWINE;
389 return ret;