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
24 #include "wine/port.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
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)
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)
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)
76 /* Retrieve the HDC associated with the context to access the display driver */
77 dc = get_dc_ptr(ctx->hdc);
80 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglCopyContext );
81 ret = physdev->funcs->pwglCopyContext(hglrcSrc, hglrcDst, mask);
87 /***********************************************************************
88 * wglCreateContext (OPENGL32.@)
90 HGLRC WINAPI wglCreateContext(HDC hdc)
93 DC * dc = get_dc_ptr( hdc );
99 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglCreateContext );
101 ret = physdev->funcs->pwglCreateContext( physdev );
102 release_dc_ptr( dc );
107 /***********************************************************************
108 * wglCreateContextAttribsARB
110 static HGLRC WINAPI wglCreateContextAttribsARB(HDC hdc, HGLRC hShareContext, const int *attributeList)
113 DC * dc = get_dc_ptr( hdc );
119 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglCreateContextAttribsARB );
121 ret = physdev->funcs->pwglCreateContextAttribsARB( physdev, hShareContext, attributeList );
122 release_dc_ptr( dc );
127 /***********************************************************************
128 * wglDeleteContext (OPENGL32.@)
130 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
134 OPENGL_Context ctx = (OPENGL_Context)hglrc;
136 TRACE("hglrc: (%p)\n", hglrc);
139 SetLastError(ERROR_INVALID_HANDLE);
143 /* Retrieve the HDC associated with the context to access the display driver */
144 dc = get_dc_ptr(ctx->hdc);
147 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglDeleteContext );
148 ret = physdev->funcs->pwglDeleteContext( hglrc );
149 release_dc_ptr( dc );
151 else SetLastError(ERROR_INVALID_HANDLE);
155 /***********************************************************************
156 * wglGetCurrentContext (OPENGL32.@)
158 HGLRC WINAPI wglGetCurrentContext(void)
160 HGLRC ret = NtCurrentTeb()->glContext;
161 TRACE(" returning %p\n", ret);
165 /***********************************************************************
166 * wglGetCurrentDC (OPENGL32.@)
168 HDC WINAPI wglGetCurrentDC(void)
170 OPENGL_Context ctx = (OPENGL_Context)wglGetCurrentContext();
172 TRACE(" found context: %p\n", ctx);
176 /* Retrieve the current DC from the active context */
177 TRACE(" returning hdc: %p\n", ctx->hdc);
181 /***********************************************************************
184 static HDC WINAPI wglGetPbufferDCARB(void *pbuffer)
188 /* Create a device context to associate with the pbuffer */
189 HDC hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
190 DC *dc = get_dc_ptr(hdc);
192 TRACE("(%p)\n", pbuffer);
196 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglGetPbufferDCARB );
198 /* The display driver has to do the rest of the work because
199 * we need access to lowlevel datatypes which we can't access here
201 ret = physdev->funcs->pwglGetPbufferDCARB( physdev, pbuffer );
202 release_dc_ptr( dc );
204 TRACE("(%p), hdc=%p\n", pbuffer, ret);
205 if (!ret) DeleteDC( hdc );
209 /***********************************************************************
210 * wglMakeCurrent (OPENGL32.@)
212 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
217 /* When the context hglrc is NULL, the HDC is ignored and can be NULL.
218 * In that case use the global hDC to get access to the driver. */
221 if( hdc == NULL && !wglGetCurrentContext() )
223 WARN( "Current context is NULL\n");
224 SetLastError( ERROR_INVALID_HANDLE );
227 dc = OPENGL_GetDefaultDC();
230 dc = get_dc_ptr( hdc );
232 TRACE("hdc: (%p), hglrc: (%p)\n", hdc, hglrc);
236 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglMakeCurrent );
238 ret = physdev->funcs->pwglMakeCurrent( physdev, hglrc );
239 release_dc_ptr( dc );
244 /***********************************************************************
245 * wglMakeContextCurrentARB
247 static BOOL WINAPI wglMakeContextCurrentARB(HDC hDrawDC, HDC hReadDC, HGLRC hglrc)
250 PHYSDEV draw_physdev, read_physdev;
254 TRACE("hDrawDC: (%p), hReadDC: (%p) hglrc: (%p)\n", hDrawDC, hReadDC, hglrc);
256 /* Both hDrawDC and hReadDC need to be valid */
257 DrawDC = get_dc_ptr( hDrawDC );
258 if (!DrawDC) return FALSE;
260 ReadDC = get_dc_ptr( hReadDC );
262 release_dc_ptr( DrawDC );
268 draw_physdev = GET_DC_PHYSDEV( DrawDC, pwglMakeContextCurrentARB );
269 read_physdev = GET_DC_PHYSDEV( ReadDC, pwglMakeContextCurrentARB );
270 if (draw_physdev->funcs == read_physdev->funcs)
271 ret = draw_physdev->funcs->pwglMakeContextCurrentARB(draw_physdev, read_physdev, hglrc);
272 release_dc_ptr( DrawDC );
273 release_dc_ptr( ReadDC );
277 /**************************************************************************************
278 * WINE-specific wglSetPixelFormat which can set the iPixelFormat multiple times
281 static BOOL WINAPI wglSetPixelFormatWINE(HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
284 DC * dc = get_dc_ptr( hdc );
286 TRACE("(%p,%d,%p)\n", hdc, iPixelFormat, ppfd);
290 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglSetPixelFormatWINE );
292 bRet = physdev->funcs->pwglSetPixelFormatWINE( physdev, iPixelFormat, ppfd );
293 release_dc_ptr( dc );
298 /***********************************************************************
299 * wglShareLists (OPENGL32.@)
301 BOOL WINAPI wglShareLists(HGLRC hglrc1, HGLRC hglrc2)
305 OPENGL_Context ctx = (OPENGL_Context)hglrc1;
307 TRACE("hglrc1: (%p); hglrc: (%p)\n", hglrc1, hglrc2);
308 if(ctx == NULL || hglrc2 == NULL)
311 /* Retrieve the HDC associated with the context to access the display driver */
312 dc = get_dc_ptr(ctx->hdc);
315 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglShareLists );
316 ret = physdev->funcs->pwglShareLists( hglrc1, hglrc2 );
317 release_dc_ptr( dc );
322 /***********************************************************************
323 * wglUseFontBitmapsA (OPENGL32.@)
325 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
328 DC * dc = get_dc_ptr( hdc );
330 TRACE("(%p, %d, %d, %d)\n", hdc, first, count, listBase);
334 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglUseFontBitmapsA );
335 ret = physdev->funcs->pwglUseFontBitmapsA( physdev, first, count, listBase );
336 release_dc_ptr( dc );
341 /***********************************************************************
342 * wglUseFontBitmapsW (OPENGL32.@)
344 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
347 DC * dc = get_dc_ptr( hdc );
349 TRACE("(%p, %d, %d, %d)\n", hdc, first, count, listBase);
353 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglUseFontBitmapsW );
354 ret = physdev->funcs->pwglUseFontBitmapsW( physdev, first, count, listBase );
355 release_dc_ptr( dc );
360 /***********************************************************************
361 * Internal wglGetProcAddress for retrieving WGL extensions
363 PROC WINAPI wglGetProcAddress(LPCSTR func)
371 TRACE("func: '%s'\n", func);
373 /* Retrieve the global hDC to get access to the driver. */
374 dc = OPENGL_GetDefaultDC();
377 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pwglGetProcAddress );
378 ret = physdev->funcs->pwglGetProcAddress(func);
379 release_dc_ptr( dc );
382 /* At the moment we implement one WGL extension which requires a HDC. When we
383 * are looking up this call and when the Extension is available (that is the case
384 * when a non-NULL value is returned by wglGetProcAddress), we return the address
385 * of a wrapper function which will handle the HDC->PhysDev conversion.
387 if(ret && strcmp(func, "wglCreateContextAttribsARB") == 0)
388 return (PROC)wglCreateContextAttribsARB;
389 else if(ret && strcmp(func, "wglMakeContextCurrentARB") == 0)
390 return (PROC)wglMakeContextCurrentARB;
391 else if(ret && strcmp(func, "wglGetPbufferDCARB") == 0)
392 return (PROC)wglGetPbufferDCARB;
393 else if(ret && strcmp(func, "wglSetPixelFormatWINE") == 0)
394 return (PROC)wglSetPixelFormatWINE;