mshtml: Added IHTMLDocument7::getElementsByClassName implementation.
[wine.git] / dlls / opengl32 / wgl.c
blobdacf0b38d4933754d77852e20a4584c6c8223735
1 /* Window-specific OpenGL functions implementation.
3 * Copyright (c) 1999 Lionel Ulmer
4 * Copyright (c) 2005 Raphael Junqueira
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "opengl_ext.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winreg.h"
33 #include "wingdi.h"
34 #include "winternl.h"
35 #include "winnt.h"
37 #define WGL_WGLEXT_PROTOTYPES
38 #include "wine/wglext.h"
39 #include "wine/gdi_driver.h"
40 #include "wine/wgl_driver.h"
41 #include "wine/glu.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
45 WINE_DECLARE_DEBUG_CHANNEL(fps);
47 extern struct opengl_funcs null_opengl_funcs;
49 /* handle management */
51 #define MAX_WGL_HANDLES 1024
53 enum wgl_handle_type
55 HANDLE_PBUFFER = 0 << 12,
56 HANDLE_CONTEXT = 1 << 12,
57 HANDLE_CONTEXT_V3 = 3 << 12,
58 HANDLE_TYPE_MASK = 15 << 12
61 struct opengl_context
63 DWORD tid; /* thread that the context is current in */
64 HDC draw_dc; /* current drawing DC */
65 HDC read_dc; /* current reading DC */
66 GLubyte *extensions; /* extension string */
67 GLuint *disabled_exts; /* indices of disabled extensions */
68 struct wgl_context *drv_ctx; /* driver context */
71 struct wgl_handle
73 UINT handle;
74 struct opengl_funcs *funcs;
75 union
77 struct opengl_context *context; /* for HANDLE_CONTEXT */
78 struct wgl_pbuffer *pbuffer; /* for HANDLE_PBUFFER */
79 struct wgl_handle *next; /* for free handles */
80 } u;
83 static struct wgl_handle wgl_handles[MAX_WGL_HANDLES];
84 static struct wgl_handle *next_free;
85 static unsigned int handle_count;
87 static CRITICAL_SECTION wgl_section;
88 static CRITICAL_SECTION_DEBUG critsect_debug =
90 0, 0, &wgl_section,
91 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
92 0, 0, { (DWORD_PTR)(__FILE__ ": wgl_section") }
94 static CRITICAL_SECTION wgl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
96 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
98 static inline struct opengl_funcs *get_dc_funcs( HDC hdc )
100 struct opengl_funcs *funcs = __wine_get_wgl_driver( hdc, WINE_WGL_DRIVER_VERSION );
101 if (!funcs) SetLastError( ERROR_INVALID_HANDLE );
102 else if (funcs == (void *)-1) funcs = &null_opengl_funcs;
103 return funcs;
106 static inline HANDLE next_handle( struct wgl_handle *ptr, enum wgl_handle_type type )
108 WORD generation = HIWORD( ptr->handle ) + 1;
109 if (!generation) generation++;
110 ptr->handle = MAKELONG( ptr - wgl_handles, generation ) | type;
111 return ULongToHandle( ptr->handle );
114 /* the current context is assumed valid and doesn't need locking */
115 static inline struct wgl_handle *get_current_context_ptr(void)
117 if (!NtCurrentTeb()->glCurrentRC) return NULL;
118 return &wgl_handles[LOWORD(NtCurrentTeb()->glCurrentRC) & ~HANDLE_TYPE_MASK];
121 static struct wgl_handle *get_handle_ptr( HANDLE handle, enum wgl_handle_type type )
123 unsigned int index = LOWORD( handle ) & ~HANDLE_TYPE_MASK;
125 EnterCriticalSection( &wgl_section );
126 if (index < handle_count && ULongToHandle(wgl_handles[index].handle) == handle)
127 return &wgl_handles[index];
129 LeaveCriticalSection( &wgl_section );
130 SetLastError( ERROR_INVALID_HANDLE );
131 return NULL;
134 static void release_handle_ptr( struct wgl_handle *ptr )
136 if (ptr) LeaveCriticalSection( &wgl_section );
139 static HANDLE alloc_handle( enum wgl_handle_type type, struct opengl_funcs *funcs, void *user_ptr )
141 HANDLE handle = 0;
142 struct wgl_handle *ptr = NULL;
144 EnterCriticalSection( &wgl_section );
145 if ((ptr = next_free))
146 next_free = next_free->u.next;
147 else if (handle_count < MAX_WGL_HANDLES)
148 ptr = &wgl_handles[handle_count++];
150 if (ptr)
152 ptr->funcs = funcs;
153 ptr->u.context = user_ptr;
154 handle = next_handle( ptr, type );
156 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
157 LeaveCriticalSection( &wgl_section );
158 return handle;
161 static void free_handle_ptr( struct wgl_handle *ptr )
163 ptr->handle |= 0xffff;
164 ptr->u.next = next_free;
165 ptr->funcs = NULL;
166 next_free = ptr;
167 LeaveCriticalSection( &wgl_section );
170 static inline enum wgl_handle_type get_current_context_type(void)
172 if (!NtCurrentTeb()->glCurrentRC) return HANDLE_CONTEXT;
173 return LOWORD(NtCurrentTeb()->glCurrentRC) & HANDLE_TYPE_MASK;
176 /***********************************************************************
177 * wglCopyContext (OPENGL32.@)
179 BOOL WINAPI wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
181 struct wgl_handle *src, *dst;
182 BOOL ret = FALSE;
184 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
185 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
187 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
188 else ret = src->funcs->wgl.p_wglCopyContext( src->u.context->drv_ctx,
189 dst->u.context->drv_ctx, mask );
191 release_handle_ptr( dst );
192 release_handle_ptr( src );
193 return ret;
196 /***********************************************************************
197 * wglDeleteContext (OPENGL32.@)
199 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
201 struct wgl_handle *ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT );
203 if (!ptr) return FALSE;
205 if (ptr->u.context->tid && ptr->u.context->tid != GetCurrentThreadId())
207 SetLastError( ERROR_BUSY );
208 release_handle_ptr( ptr );
209 return FALSE;
211 if (hglrc == NtCurrentTeb()->glCurrentRC) wglMakeCurrent( 0, 0 );
212 ptr->funcs->wgl.p_wglDeleteContext( ptr->u.context->drv_ctx );
213 HeapFree( GetProcessHeap(), 0, ptr->u.context->disabled_exts );
214 HeapFree( GetProcessHeap(), 0, ptr->u.context->extensions );
215 HeapFree( GetProcessHeap(), 0, ptr->u.context );
216 free_handle_ptr( ptr );
217 return TRUE;
220 /***********************************************************************
221 * wglMakeCurrent (OPENGL32.@)
223 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
225 BOOL ret = TRUE;
226 struct wgl_handle *ptr, *prev = get_current_context_ptr();
228 if (hglrc)
230 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
231 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
233 ret = ptr->funcs->wgl.p_wglMakeCurrent( hdc, ptr->u.context->drv_ctx );
234 if (ret)
236 if (prev) prev->u.context->tid = 0;
237 ptr->u.context->tid = GetCurrentThreadId();
238 ptr->u.context->draw_dc = hdc;
239 ptr->u.context->read_dc = hdc;
240 NtCurrentTeb()->glCurrentRC = hglrc;
241 NtCurrentTeb()->glTable = ptr->funcs;
244 else
246 SetLastError( ERROR_BUSY );
247 ret = FALSE;
249 release_handle_ptr( ptr );
251 else if (prev)
253 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
254 prev->u.context->tid = 0;
255 NtCurrentTeb()->glCurrentRC = 0;
256 NtCurrentTeb()->glTable = &null_opengl_funcs;
258 else if (!hdc)
260 SetLastError( ERROR_INVALID_HANDLE );
261 ret = FALSE;
263 return ret;
266 /***********************************************************************
267 * wglCreateContextAttribsARB
269 * Provided by the WGL_ARB_create_context extension.
271 HGLRC WINAPI wglCreateContextAttribsARB( HDC hdc, HGLRC share, const int *attribs )
273 HGLRC ret = 0;
274 struct wgl_context *drv_ctx;
275 struct wgl_handle *share_ptr = NULL;
276 struct opengl_context *context;
277 struct opengl_funcs *funcs = get_dc_funcs( hdc );
279 if (!funcs)
281 SetLastError( ERROR_DC_NOT_FOUND );
282 return 0;
284 if (!funcs->ext.p_wglCreateContextAttribsARB) return 0;
285 if (share && !(share_ptr = get_handle_ptr( share, HANDLE_CONTEXT )))
287 SetLastError( ERROR_INVALID_OPERATION );
288 return 0;
290 if ((drv_ctx = funcs->ext.p_wglCreateContextAttribsARB( hdc,
291 share_ptr ? share_ptr->u.context->drv_ctx : NULL, attribs )))
293 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
295 enum wgl_handle_type type = HANDLE_CONTEXT;
297 if (attribs)
299 while (*attribs)
301 if (attribs[0] == WGL_CONTEXT_MAJOR_VERSION_ARB)
303 if (attribs[1] >= 3)
304 type = HANDLE_CONTEXT_V3;
305 break;
307 attribs += 2;
311 context->drv_ctx = drv_ctx;
312 if (!(ret = alloc_handle( type, funcs, context )))
313 HeapFree( GetProcessHeap(), 0, context );
315 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
317 release_handle_ptr( share_ptr );
318 return ret;
322 /***********************************************************************
323 * wglMakeContextCurrentARB
325 * Provided by the WGL_ARB_make_current_read extension.
327 BOOL WINAPI wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, HGLRC hglrc )
329 BOOL ret = TRUE;
330 struct wgl_handle *ptr, *prev = get_current_context_ptr();
332 if (hglrc)
334 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
335 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
337 ret = (ptr->funcs->ext.p_wglMakeContextCurrentARB &&
338 ptr->funcs->ext.p_wglMakeContextCurrentARB( draw_hdc, read_hdc,
339 ptr->u.context->drv_ctx ));
340 if (ret)
342 if (prev) prev->u.context->tid = 0;
343 ptr->u.context->tid = GetCurrentThreadId();
344 ptr->u.context->draw_dc = draw_hdc;
345 ptr->u.context->read_dc = read_hdc;
346 NtCurrentTeb()->glCurrentRC = hglrc;
347 NtCurrentTeb()->glTable = ptr->funcs;
350 else
352 SetLastError( ERROR_BUSY );
353 ret = FALSE;
355 release_handle_ptr( ptr );
357 else if (prev)
359 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
360 prev->u.context->tid = 0;
361 NtCurrentTeb()->glCurrentRC = 0;
362 NtCurrentTeb()->glTable = &null_opengl_funcs;
364 return ret;
367 /***********************************************************************
368 * wglGetCurrentReadDCARB
370 * Provided by the WGL_ARB_make_current_read extension.
372 HDC WINAPI wglGetCurrentReadDCARB(void)
374 struct wgl_handle *ptr = get_current_context_ptr();
376 if (!ptr) return 0;
377 return ptr->u.context->read_dc;
380 /***********************************************************************
381 * wglShareLists (OPENGL32.@)
383 BOOL WINAPI wglShareLists(HGLRC hglrcSrc, HGLRC hglrcDst)
385 BOOL ret = FALSE;
386 struct wgl_handle *src, *dst;
388 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
389 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
391 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
392 else ret = src->funcs->wgl.p_wglShareLists( src->u.context->drv_ctx, dst->u.context->drv_ctx );
394 release_handle_ptr( dst );
395 release_handle_ptr( src );
396 return ret;
399 /***********************************************************************
400 * wglGetCurrentDC (OPENGL32.@)
402 HDC WINAPI wglGetCurrentDC(void)
404 struct wgl_handle *ptr = get_current_context_ptr();
406 if (!ptr) return 0;
407 return ptr->u.context->draw_dc;
410 /***********************************************************************
411 * wglCreateContext (OPENGL32.@)
413 HGLRC WINAPI wglCreateContext(HDC hdc)
415 HGLRC ret = 0;
416 struct wgl_context *drv_ctx;
417 struct opengl_context *context;
418 struct opengl_funcs *funcs = get_dc_funcs( hdc );
420 if (!funcs) return 0;
421 if (!(drv_ctx = funcs->wgl.p_wglCreateContext( hdc ))) return 0;
422 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
424 context->drv_ctx = drv_ctx;
425 if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
426 HeapFree( GetProcessHeap(), 0, context );
428 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
429 return ret;
432 /***********************************************************************
433 * wglGetCurrentContext (OPENGL32.@)
435 HGLRC WINAPI wglGetCurrentContext(void)
437 return NtCurrentTeb()->glCurrentRC;
440 /***********************************************************************
441 * wglDescribePixelFormat (OPENGL32.@)
443 INT WINAPI wglDescribePixelFormat(HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
445 struct opengl_funcs *funcs = get_dc_funcs( hdc );
446 if (!funcs) return 0;
447 return funcs->wgl.p_wglDescribePixelFormat( hdc, format, size, descr );
450 /***********************************************************************
451 * wglChoosePixelFormat (OPENGL32.@)
453 INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
455 PIXELFORMATDESCRIPTOR format, best;
456 int i, count, best_format;
457 int bestDBuffer = -1, bestStereo = -1;
459 TRACE_(wgl)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
460 "accum %u depth %u stencil %u aux %u\n",
461 hdc, ppfd, ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
462 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
463 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
465 count = wglDescribePixelFormat( hdc, 0, 0, NULL );
466 if (!count) return 0;
468 best_format = 0;
469 best.dwFlags = 0;
470 best.cAlphaBits = -1;
471 best.cColorBits = -1;
472 best.cDepthBits = -1;
473 best.cStencilBits = -1;
474 best.cAuxBuffers = -1;
476 for (i = 1; i <= count; i++)
478 if (!wglDescribePixelFormat( hdc, i, sizeof(format), &format )) continue;
480 if (ppfd->iPixelType != format.iPixelType)
482 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i );
483 continue;
486 /* only use bitmap capable for formats for bitmap rendering */
487 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (format.dwFlags & PFD_DRAW_TO_BITMAP))
489 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i );
490 continue;
493 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
494 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
495 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
496 * formats without the given flag set.
497 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
498 * has indicated that a format without stereo is returned when stereo is unavailable.
499 * So in case PFD_STEREO is set, formats that support it should have priority above formats
500 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
502 * To summarize the following is most likely the correct behavior:
503 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
504 * stereo set -> prefer stereo formats, but also accept non-stereo formats
505 * stereo don't care -> it doesn't matter whether we get stereo or not
507 * In Wine we will treat non-stereo the same way as don't care because it makes
508 * format selection even more complicated and second drivers with Stereo advertise
509 * each format twice anyway.
512 /* Doublebuffer, see the comments above */
513 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE))
515 if (((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
516 ((format.dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)))
517 goto found;
519 if (bestDBuffer != -1 && (format.dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) continue;
521 else if (!best_format)
522 goto found;
524 /* Stereo, see the comments above. */
525 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE))
527 if (((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
528 ((format.dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)))
529 goto found;
531 if (bestStereo != -1 && (format.dwFlags & PFD_STEREO) != bestStereo) continue;
533 else if (!best_format)
534 goto found;
536 /* Below we will do a number of checks to select the 'best' pixelformat.
537 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
538 * The code works by trying to match the most important options as close as possible.
539 * When a reasonable format is found, we will try to match more options.
540 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
541 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
542 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
544 if (ppfd->cColorBits)
546 if (((ppfd->cColorBits > best.cColorBits) && (format.cColorBits > best.cColorBits)) ||
547 ((format.cColorBits >= ppfd->cColorBits) && (format.cColorBits < best.cColorBits)))
548 goto found;
550 if (best.cColorBits != format.cColorBits) /* Do further checks if the format is compatible */
552 TRACE( "color mismatch for iPixelFormat=%d\n", i );
553 continue;
556 if (ppfd->cAlphaBits)
558 if (((ppfd->cAlphaBits > best.cAlphaBits) && (format.cAlphaBits > best.cAlphaBits)) ||
559 ((format.cAlphaBits >= ppfd->cAlphaBits) && (format.cAlphaBits < best.cAlphaBits)))
560 goto found;
562 if (best.cAlphaBits != format.cAlphaBits)
564 TRACE( "alpha mismatch for iPixelFormat=%d\n", i );
565 continue;
568 if (ppfd->cDepthBits)
570 if (((ppfd->cDepthBits > best.cDepthBits) && (format.cDepthBits > best.cDepthBits)) ||
571 ((format.cDepthBits >= ppfd->cDepthBits) && (format.cDepthBits < best.cDepthBits)))
572 goto found;
574 if (best.cDepthBits != format.cDepthBits)
576 TRACE( "depth mismatch for iPixelFormat=%d\n", i );
577 continue;
580 if (ppfd->cStencilBits)
582 if (((ppfd->cStencilBits > best.cStencilBits) && (format.cStencilBits > best.cStencilBits)) ||
583 ((format.cStencilBits >= ppfd->cStencilBits) && (format.cStencilBits < best.cStencilBits)))
584 goto found;
586 if (best.cStencilBits != format.cStencilBits)
588 TRACE( "stencil mismatch for iPixelFormat=%d\n", i );
589 continue;
592 if (ppfd->cAuxBuffers)
594 if (((ppfd->cAuxBuffers > best.cAuxBuffers) && (format.cAuxBuffers > best.cAuxBuffers)) ||
595 ((format.cAuxBuffers >= ppfd->cAuxBuffers) && (format.cAuxBuffers < best.cAuxBuffers)))
596 goto found;
598 if (best.cAuxBuffers != format.cAuxBuffers)
600 TRACE( "aux mismatch for iPixelFormat=%d\n", i );
601 continue;
604 continue;
606 found:
607 best_format = i;
608 best = format;
609 bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
610 bestStereo = format.dwFlags & PFD_STEREO;
613 TRACE( "returning %u\n", best_format );
614 return best_format;
617 /***********************************************************************
618 * wglGetPixelFormat (OPENGL32.@)
620 INT WINAPI wglGetPixelFormat(HDC hdc)
622 struct opengl_funcs *funcs = get_dc_funcs( hdc );
623 if (!funcs)
625 SetLastError( ERROR_INVALID_PIXEL_FORMAT );
626 return 0;
628 return funcs->wgl.p_wglGetPixelFormat( hdc );
631 /***********************************************************************
632 * wglSetPixelFormat(OPENGL32.@)
634 BOOL WINAPI wglSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
636 struct opengl_funcs *funcs = get_dc_funcs( hdc );
637 if (!funcs) return FALSE;
638 return funcs->wgl.p_wglSetPixelFormat( hdc, format, descr );
641 /***********************************************************************
642 * wglSwapBuffers (OPENGL32.@)
644 BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
646 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
648 if (!funcs || !funcs->wgl.p_wglSwapBuffers) return FALSE;
649 if (!funcs->wgl.p_wglSwapBuffers( hdc )) return FALSE;
651 if (TRACE_ON(fps))
653 static long prev_time, start_time;
654 static unsigned long frames, frames_total;
656 DWORD time = GetTickCount();
657 frames++;
658 frames_total++;
659 /* every 1.5 seconds */
660 if (time - prev_time > 1500)
662 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
663 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
664 prev_time = time;
665 frames = 0;
666 if (start_time == 0) start_time = time;
669 return TRUE;
672 /***********************************************************************
673 * wglCreateLayerContext (OPENGL32.@)
675 HGLRC WINAPI wglCreateLayerContext(HDC hdc,
676 int iLayerPlane) {
677 TRACE("(%p,%d)\n", hdc, iLayerPlane);
679 if (iLayerPlane == 0) {
680 return wglCreateContext(hdc);
682 FIXME("no handler for layer %d\n", iLayerPlane);
684 return NULL;
687 /***********************************************************************
688 * wglDescribeLayerPlane (OPENGL32.@)
690 BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
691 int iPixelFormat,
692 int iLayerPlane,
693 UINT nBytes,
694 LPLAYERPLANEDESCRIPTOR plpd) {
695 FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
697 return FALSE;
700 /***********************************************************************
701 * wglGetLayerPaletteEntries (OPENGL32.@)
703 int WINAPI wglGetLayerPaletteEntries(HDC hdc,
704 int iLayerPlane,
705 int iStart,
706 int cEntries,
707 const COLORREF *pcr) {
708 FIXME("(): stub!\n");
710 return 0;
713 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts);
715 void WINAPI glGetIntegerv(GLenum pname, GLint *data)
717 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
719 TRACE("(%d, %p)\n", pname, data);
720 if (pname == GL_NUM_EXTENSIONS)
722 struct wgl_handle *ptr = get_current_context_ptr();
724 if (ptr->u.context->disabled_exts ||
725 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
727 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
728 GLint count, disabled_count = 0;
730 funcs->gl.p_glGetIntegerv(pname, &count);
731 while (*disabled_exts++ != ~0u)
732 disabled_count++;
733 *data = count - disabled_count;
734 return;
737 funcs->gl.p_glGetIntegerv(pname, data);
740 const GLubyte * WINAPI glGetStringi(GLenum name, GLuint index)
742 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
744 TRACE("(%d, %d)\n", name, index);
745 if (!funcs->ext.p_glGetStringi)
747 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
749 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
752 if (name == GL_EXTENSIONS)
754 struct wgl_handle *ptr = get_current_context_ptr();
756 if (ptr->u.context->disabled_exts ||
757 filter_extensions(NULL, NULL, &ptr->u.context->disabled_exts))
759 const GLuint *disabled_exts = ptr->u.context->disabled_exts;
760 unsigned int disabled_count = 0;
762 while (index + disabled_count >= *disabled_exts++)
763 disabled_count++;
764 return funcs->ext.p_glGetStringi(name, index + disabled_count);
767 return funcs->ext.p_glGetStringi(name, index);
770 /* check if the extension is present in the list */
771 static BOOL has_extension( const char *list, const char *ext, size_t len )
773 if (!list)
775 const char *gl_ext;
776 unsigned int i;
777 GLint extensions_count;
779 glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
780 for (i = 0; i < extensions_count; ++i)
782 gl_ext = (const char *)glGetStringi(GL_EXTENSIONS, i);
783 if (!strncmp(gl_ext, ext, len) && !gl_ext[len])
784 return TRUE;
786 return FALSE;
789 while (list)
791 while (*list == ' ') list++;
792 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
793 list = strchr( list, ' ' );
795 return FALSE;
798 static int compar(const void *elt_a, const void *elt_b) {
799 return strcmp(((const OpenGL_extension *) elt_a)->name,
800 ((const OpenGL_extension *) elt_b)->name);
803 /* Check if a GL extension is supported */
804 static BOOL is_extension_supported(const char* extension)
806 enum wgl_handle_type type = get_current_context_type();
807 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
808 const char *gl_ext_string = NULL;
809 size_t len;
811 TRACE("Checking for extension '%s'\n", extension);
813 if (type == HANDLE_CONTEXT)
815 gl_ext_string = (const char*)glGetString(GL_EXTENSIONS);
816 if (!gl_ext_string)
818 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
819 return FALSE;
823 /* We use the GetProcAddress function from the display driver to retrieve function pointers
824 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
825 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
826 * require the function to return NULL when an extension isn't found. For this reason we check
827 * if the OpenGL extension required for the function we are looking up is supported. */
829 while ((len = strcspn(extension, " ")) != 0)
831 /* Check if the extension is part of the GL extension string to see if it is supported. */
832 if (has_extension(gl_ext_string, extension, len))
833 return TRUE;
835 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
836 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
837 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
838 * Check if we are searching for a core GL function */
839 if(strncmp(extension, "GL_VERSION_", 11) == 0)
841 const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
842 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
844 if(!gl_version) {
845 ERR("No OpenGL version found!\n");
846 return FALSE;
849 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
850 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
851 if( (gl_version[0] > version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
852 return TRUE;
854 WARN("The function requires OpenGL version '%c.%c' while your drivers only provide '%c.%c'\n", version[0], version[2], gl_version[0], gl_version[2]);
857 if (extension[len] == ' ') len++;
858 extension += len;
861 return FALSE;
864 /***********************************************************************
865 * wglGetProcAddress (OPENGL32.@)
867 PROC WINAPI wglGetProcAddress( LPCSTR name )
869 struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
870 void **func_ptr;
871 OpenGL_extension ext;
872 const OpenGL_extension *ext_ret;
874 if (!name) return NULL;
876 /* Without an active context opengl32 doesn't know to what
877 * driver it has to dispatch wglGetProcAddress.
879 if (!get_current_context_ptr())
881 WARN("No active WGL context found\n");
882 return NULL;
885 ext.name = name;
886 ext_ret = bsearch(&ext, extension_registry, extension_registry_size, sizeof(ext), compar);
887 if (!ext_ret)
889 WARN("Function %s unknown\n", name);
890 return NULL;
893 func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
894 if (!*func_ptr)
896 void *driver_func = funcs->wgl.p_wglGetProcAddress( name );
898 if (!is_extension_supported(ext_ret->extension))
900 unsigned int i;
901 static const struct { const char *name, *alt; } alternatives[] =
903 { "glCopyTexSubImage3DEXT", "glCopyTexSubImage3D" }, /* needed by RuneScape */
904 { "glVertexAttribDivisor", "glVertexAttribDivisorARB"}, /* needed by Caffeine */
907 for (i = 0; i < sizeof(alternatives)/sizeof(alternatives[0]); i++)
909 if (strcmp( name, alternatives[i].name )) continue;
910 WARN("Extension %s required for %s not supported, trying %s\n",
911 ext_ret->extension, name, alternatives[i].alt );
912 return wglGetProcAddress( alternatives[i].alt );
914 WARN("Extension %s required for %s not supported\n", ext_ret->extension, name);
915 return NULL;
918 if (driver_func == NULL)
920 WARN("Function %s not supported by driver\n", name);
921 return NULL;
923 *func_ptr = driver_func;
926 TRACE("returning %s -> %p\n", name, ext_ret->func);
927 return ext_ret->func;
930 /***********************************************************************
931 * wglRealizeLayerPalette (OPENGL32.@)
933 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
934 int iLayerPlane,
935 BOOL bRealize) {
936 FIXME("()\n");
938 return FALSE;
941 /***********************************************************************
942 * wglSetLayerPaletteEntries (OPENGL32.@)
944 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
945 int iLayerPlane,
946 int iStart,
947 int cEntries,
948 const COLORREF *pcr) {
949 FIXME("(): stub!\n");
951 return 0;
954 /***********************************************************************
955 * wglSwapLayerBuffers (OPENGL32.@)
957 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
958 UINT fuPlanes) {
959 TRACE("(%p, %08x)\n", hdc, fuPlanes);
961 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
962 if (!wglSwapBuffers( hdc )) return FALSE;
963 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
966 if (fuPlanes) {
967 WARN("Following layers unhandled: %08x\n", fuPlanes);
970 return TRUE;
973 /***********************************************************************
974 * wglAllocateMemoryNV
976 * Provided by the WGL_NV_vertex_array_range extension.
978 void * WINAPI wglAllocateMemoryNV( GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority )
980 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
982 if (!funcs->ext.p_wglAllocateMemoryNV) return NULL;
983 return funcs->ext.p_wglAllocateMemoryNV( size, readfreq, writefreq, priority );
986 /***********************************************************************
987 * wglFreeMemoryNV
989 * Provided by the WGL_NV_vertex_array_range extension.
991 void WINAPI wglFreeMemoryNV( void *pointer )
993 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
995 if (funcs->ext.p_wglFreeMemoryNV) funcs->ext.p_wglFreeMemoryNV( pointer );
998 /***********************************************************************
999 * wglBindTexImageARB
1001 * Provided by the WGL_ARB_render_texture extension.
1003 BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
1005 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1006 BOOL ret;
1008 if (!ptr) return FALSE;
1009 ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
1010 release_handle_ptr( ptr );
1011 return ret;
1014 /***********************************************************************
1015 * wglReleaseTexImageARB
1017 * Provided by the WGL_ARB_render_texture extension.
1019 BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
1021 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1022 BOOL ret;
1024 if (!ptr) return FALSE;
1025 ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
1026 release_handle_ptr( ptr );
1027 return ret;
1030 /***********************************************************************
1031 * wglSetPbufferAttribARB
1033 * Provided by the WGL_ARB_render_texture extension.
1035 BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
1037 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1038 BOOL ret;
1040 if (!ptr) return FALSE;
1041 ret = ptr->funcs->ext.p_wglSetPbufferAttribARB( ptr->u.pbuffer, attribs );
1042 release_handle_ptr( ptr );
1043 return ret;
1046 /***********************************************************************
1047 * wglChoosePixelFormatARB
1049 * Provided by the WGL_ARB_pixel_format extension.
1051 BOOL WINAPI wglChoosePixelFormatARB( HDC hdc, const int *iattribs, const FLOAT *fattribs,
1052 UINT max, int *formats, UINT *count )
1054 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1056 if (!funcs || !funcs->ext.p_wglChoosePixelFormatARB) return FALSE;
1057 return funcs->ext.p_wglChoosePixelFormatARB( hdc, iattribs, fattribs, max, formats, count );
1060 /***********************************************************************
1061 * wglGetPixelFormatAttribivARB
1063 * Provided by the WGL_ARB_pixel_format extension.
1065 BOOL WINAPI wglGetPixelFormatAttribivARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
1066 int *values )
1068 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1070 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribivARB) return FALSE;
1071 return funcs->ext.p_wglGetPixelFormatAttribivARB( hdc, format, layer, count, attribs, values );
1074 /***********************************************************************
1075 * wglGetPixelFormatAttribfvARB
1077 * Provided by the WGL_ARB_pixel_format extension.
1079 BOOL WINAPI wglGetPixelFormatAttribfvARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
1080 FLOAT *values )
1082 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1084 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribfvARB) return FALSE;
1085 return funcs->ext.p_wglGetPixelFormatAttribfvARB( hdc, format, layer, count, attribs, values );
1088 /***********************************************************************
1089 * wglCreatePbufferARB
1091 * Provided by the WGL_ARB_pbuffer extension.
1093 HPBUFFERARB WINAPI wglCreatePbufferARB( HDC hdc, int format, int width, int height, const int *attribs )
1095 HPBUFFERARB ret;
1096 struct wgl_pbuffer *pbuffer;
1097 struct opengl_funcs *funcs = get_dc_funcs( hdc );
1099 if (!funcs || !funcs->ext.p_wglCreatePbufferARB) return 0;
1100 if (!(pbuffer = funcs->ext.p_wglCreatePbufferARB( hdc, format, width, height, attribs ))) return 0;
1101 ret = alloc_handle( HANDLE_PBUFFER, funcs, pbuffer );
1102 if (!ret) funcs->ext.p_wglDestroyPbufferARB( pbuffer );
1103 return ret;
1106 /***********************************************************************
1107 * wglGetPbufferDCARB
1109 * Provided by the WGL_ARB_pbuffer extension.
1111 HDC WINAPI wglGetPbufferDCARB( HPBUFFERARB handle )
1113 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1114 HDC ret;
1116 if (!ptr) return 0;
1117 ret = ptr->funcs->ext.p_wglGetPbufferDCARB( ptr->u.pbuffer );
1118 release_handle_ptr( ptr );
1119 return ret;
1122 /***********************************************************************
1123 * wglReleasePbufferDCARB
1125 * Provided by the WGL_ARB_pbuffer extension.
1127 int WINAPI wglReleasePbufferDCARB( HPBUFFERARB handle, HDC hdc )
1129 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1130 BOOL ret;
1132 if (!ptr) return FALSE;
1133 ret = ptr->funcs->ext.p_wglReleasePbufferDCARB( ptr->u.pbuffer, hdc );
1134 release_handle_ptr( ptr );
1135 return ret;
1138 /***********************************************************************
1139 * wglDestroyPbufferARB
1141 * Provided by the WGL_ARB_pbuffer extension.
1143 BOOL WINAPI wglDestroyPbufferARB( HPBUFFERARB handle )
1145 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1147 if (!ptr) return FALSE;
1148 ptr->funcs->ext.p_wglDestroyPbufferARB( ptr->u.pbuffer );
1149 free_handle_ptr( ptr );
1150 return TRUE;
1153 /***********************************************************************
1154 * wglQueryPbufferARB
1156 * Provided by the WGL_ARB_pbuffer extension.
1158 BOOL WINAPI wglQueryPbufferARB( HPBUFFERARB handle, int attrib, int *value )
1160 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1161 BOOL ret;
1163 if (!ptr) return FALSE;
1164 ret = ptr->funcs->ext.p_wglQueryPbufferARB( ptr->u.pbuffer, attrib, value );
1165 release_handle_ptr( ptr );
1166 return ret;
1169 /***********************************************************************
1170 * wglGetExtensionsStringARB
1172 * Provided by the WGL_ARB_extensions_string extension.
1174 const char * WINAPI wglGetExtensionsStringARB( HDC hdc )
1176 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1178 if (!funcs || !funcs->ext.p_wglGetExtensionsStringARB) return NULL;
1179 return (const char *)funcs->ext.p_wglGetExtensionsStringARB( hdc );
1182 /***********************************************************************
1183 * wglGetExtensionsStringEXT
1185 * Provided by the WGL_EXT_extensions_string extension.
1187 const char * WINAPI wglGetExtensionsStringEXT(void)
1189 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1191 if (!funcs->ext.p_wglGetExtensionsStringEXT) return NULL;
1192 return (const char *)funcs->ext.p_wglGetExtensionsStringEXT();
1195 /***********************************************************************
1196 * wglSwapIntervalEXT
1198 * Provided by the WGL_EXT_swap_control extension.
1200 BOOL WINAPI wglSwapIntervalEXT( int interval )
1202 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1204 if (!funcs->ext.p_wglSwapIntervalEXT) return FALSE;
1205 return funcs->ext.p_wglSwapIntervalEXT( interval );
1208 /***********************************************************************
1209 * wglGetSwapIntervalEXT
1211 * Provided by the WGL_EXT_swap_control extension.
1213 int WINAPI wglGetSwapIntervalEXT(void)
1215 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1217 if (!funcs->ext.p_wglGetSwapIntervalEXT) return FALSE;
1218 return funcs->ext.p_wglGetSwapIntervalEXT();
1221 /***********************************************************************
1222 * wglSetPixelFormatWINE
1224 * Provided by the WGL_WINE_pixel_format_passthrough extension.
1226 BOOL WINAPI wglSetPixelFormatWINE( HDC hdc, int format )
1228 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1230 if (!funcs || !funcs->ext.p_wglSetPixelFormatWINE) return FALSE;
1231 return funcs->ext.p_wglSetPixelFormatWINE( hdc, format );
1234 /***********************************************************************
1235 * wglQueryCurrentRendererIntegerWINE
1237 * Provided by the WGL_WINE_query_renderer extension.
1239 BOOL WINAPI wglQueryCurrentRendererIntegerWINE( GLenum attribute, GLuint *value )
1241 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1243 if (!funcs->ext.p_wglQueryCurrentRendererIntegerWINE) return FALSE;
1244 return funcs->ext.p_wglQueryCurrentRendererIntegerWINE( attribute, value );
1247 /***********************************************************************
1248 * wglQueryCurrentRendererStringWINE
1250 * Provided by the WGL_WINE_query_renderer extension.
1252 const GLchar * WINAPI wglQueryCurrentRendererStringWINE( GLenum attribute )
1254 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1256 if (!funcs->ext.p_wglQueryCurrentRendererStringWINE) return NULL;
1257 return funcs->ext.p_wglQueryCurrentRendererStringWINE( attribute );
1260 /***********************************************************************
1261 * wglQueryRendererIntegerWINE
1263 * Provided by the WGL_WINE_query_renderer extension.
1265 BOOL WINAPI wglQueryRendererIntegerWINE( HDC dc, GLint renderer, GLenum attribute, GLuint *value )
1267 const struct opengl_funcs *funcs = get_dc_funcs( dc );
1269 if (!funcs || !funcs->ext.p_wglQueryRendererIntegerWINE) return FALSE;
1270 return funcs->ext.p_wglQueryRendererIntegerWINE( dc, renderer, attribute, value );
1273 /***********************************************************************
1274 * wglQueryRendererStringWINE
1276 * Provided by the WGL_WINE_query_renderer extension.
1278 const GLchar * WINAPI wglQueryRendererStringWINE( HDC dc, GLint renderer, GLenum attribute )
1280 const struct opengl_funcs *funcs = get_dc_funcs( dc );
1282 if (!funcs || !funcs->ext.p_wglQueryRendererStringWINE) return NULL;
1283 return funcs->ext.p_wglQueryRendererStringWINE( dc, renderer, attribute );
1286 /***********************************************************************
1287 * wglUseFontBitmaps_common
1289 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
1291 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1292 GLYPHMETRICS gm;
1293 unsigned int glyph, size = 0;
1294 void *bitmap = NULL, *gl_bitmap = NULL;
1295 int org_alignment;
1296 BOOL ret = TRUE;
1298 funcs->gl.p_glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1299 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1301 for (glyph = first; glyph < first + count; glyph++) {
1302 unsigned int needed_size, height, width, width_int;
1304 if (unicode)
1305 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1306 else
1307 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1309 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
1310 if (needed_size == GDI_ERROR) {
1311 ret = FALSE;
1312 break;
1315 if (needed_size > size) {
1316 size = needed_size;
1317 HeapFree(GetProcessHeap(), 0, bitmap);
1318 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1319 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1320 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1322 if (needed_size != 0) {
1323 if (unicode)
1324 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm,
1325 size, bitmap, &identity) != GDI_ERROR);
1326 else
1327 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm,
1328 size, bitmap, &identity) != GDI_ERROR);
1329 if (!ret) break;
1332 if (TRACE_ON(wgl)) {
1333 unsigned int bitmask;
1334 unsigned char *bitmap_ = bitmap;
1336 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1337 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1338 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1339 if (needed_size != 0) {
1340 TRACE(" - bitmap:\n");
1341 for (height = 0; height < gm.gmBlackBoxY; height++) {
1342 TRACE(" ");
1343 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1344 if (bitmask == 0) {
1345 bitmap_ += 1;
1346 bitmask = 0x80;
1348 if (*bitmap_ & bitmask)
1349 TRACE("*");
1350 else
1351 TRACE(" ");
1353 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1354 TRACE("\n");
1359 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1360 * glyph for it to be drawn properly.
1362 if (needed_size != 0) {
1363 width_int = (gm.gmBlackBoxX + 31) / 32;
1364 for (height = 0; height < gm.gmBlackBoxY; height++) {
1365 for (width = 0; width < width_int; width++) {
1366 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1367 ((int *) bitmap)[height * width_int + width];
1372 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1373 if (needed_size != 0) {
1374 funcs->gl.p_glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1375 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
1376 gm.gmCellIncX, gm.gmCellIncY,
1377 gl_bitmap);
1378 } else {
1379 /* This is the case of 'empty' glyphs like the space character */
1380 funcs->gl.p_glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1382 funcs->gl.p_glEndList();
1385 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1386 HeapFree(GetProcessHeap(), 0, bitmap);
1387 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1388 return ret;
1391 /***********************************************************************
1392 * wglUseFontBitmapsA (OPENGL32.@)
1394 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1396 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
1399 /***********************************************************************
1400 * wglUseFontBitmapsW (OPENGL32.@)
1402 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1404 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
1407 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
1409 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
1410 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
1411 vertex[2] = 0.0;
1414 static void WINAPI tess_callback_vertex(GLvoid *vertex)
1416 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1417 GLdouble *dbl = vertex;
1418 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
1419 funcs->gl.p_glVertex3dv(vertex);
1422 static void WINAPI tess_callback_begin(GLenum which)
1424 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1425 TRACE("%d\n", which);
1426 funcs->gl.p_glBegin(which);
1429 static void WINAPI tess_callback_end(void)
1431 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1432 TRACE("\n");
1433 funcs->gl.p_glEnd();
1436 typedef struct _bezier_vector {
1437 GLdouble x;
1438 GLdouble y;
1439 } bezier_vector;
1441 static double bezier_deviation_squared(const bezier_vector *p)
1443 bezier_vector deviation;
1444 bezier_vector vertex;
1445 bezier_vector base;
1446 double base_length;
1447 double dot;
1449 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4 - p[0].x;
1450 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4 - p[0].y;
1452 base.x = p[2].x - p[0].x;
1453 base.y = p[2].y - p[0].y;
1455 base_length = sqrt(base.x*base.x + base.y*base.y);
1456 base.x /= base_length;
1457 base.y /= base_length;
1459 dot = base.x*vertex.x + base.y*vertex.y;
1460 dot = min(max(dot, 0.0), base_length);
1461 base.x *= dot;
1462 base.y *= dot;
1464 deviation.x = vertex.x-base.x;
1465 deviation.y = vertex.y-base.y;
1467 return deviation.x*deviation.x + deviation.y*deviation.y;
1470 static int bezier_approximate(const bezier_vector *p, bezier_vector *points, FLOAT deviation)
1472 bezier_vector first_curve[3];
1473 bezier_vector second_curve[3];
1474 bezier_vector vertex;
1475 int total_vertices;
1477 if(bezier_deviation_squared(p) <= deviation*deviation)
1479 if(points)
1480 *points = p[2];
1481 return 1;
1484 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4;
1485 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4;
1487 first_curve[0] = p[0];
1488 first_curve[1].x = (p[0].x + p[1].x)/2;
1489 first_curve[1].y = (p[0].y + p[1].y)/2;
1490 first_curve[2] = vertex;
1492 second_curve[0] = vertex;
1493 second_curve[1].x = (p[2].x + p[1].x)/2;
1494 second_curve[1].y = (p[2].y + p[1].y)/2;
1495 second_curve[2] = p[2];
1497 total_vertices = bezier_approximate(first_curve, points, deviation);
1498 if(points)
1499 points += total_vertices;
1500 total_vertices += bezier_approximate(second_curve, points, deviation);
1501 return total_vertices;
1504 /***********************************************************************
1505 * wglUseFontOutlines_common
1507 static BOOL wglUseFontOutlines_common(HDC hdc,
1508 DWORD first,
1509 DWORD count,
1510 DWORD listBase,
1511 FLOAT deviation,
1512 FLOAT extrusion,
1513 int format,
1514 LPGLYPHMETRICSFLOAT lpgmf,
1515 BOOL unicode)
1517 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1518 UINT glyph;
1519 GLUtesselator *tess = NULL;
1520 LOGFONTW lf;
1521 HFONT old_font, unscaled_font;
1522 UINT em_size = 1024;
1523 RECT rc;
1525 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
1526 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
1528 if(deviation <= 0.0)
1529 deviation = 1.0/em_size;
1531 if(format == WGL_FONT_POLYGONS)
1533 tess = gluNewTess();
1534 if(!tess)
1536 ERR("glu32 is required for this function but isn't available\n");
1537 return FALSE;
1539 gluTessCallback(tess, GLU_TESS_VERTEX, (void *)tess_callback_vertex);
1540 gluTessCallback(tess, GLU_TESS_BEGIN, (void *)tess_callback_begin);
1541 gluTessCallback(tess, GLU_TESS_END, tess_callback_end);
1544 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1545 rc.left = rc.right = rc.bottom = 0;
1546 rc.top = em_size;
1547 DPtoLP(hdc, (POINT*)&rc, 2);
1548 lf.lfHeight = -abs(rc.top - rc.bottom);
1549 lf.lfOrientation = lf.lfEscapement = 0;
1550 unscaled_font = CreateFontIndirectW(&lf);
1551 old_font = SelectObject(hdc, unscaled_font);
1553 for (glyph = first; glyph < first + count; glyph++)
1555 DWORD needed;
1556 GLYPHMETRICS gm;
1557 BYTE *buf;
1558 TTPOLYGONHEADER *pph;
1559 TTPOLYCURVE *ppc;
1560 GLdouble *vertices = NULL;
1561 int vertex_total = -1;
1563 if(unicode)
1564 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1565 else
1566 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1568 if(needed == GDI_ERROR)
1569 goto error;
1571 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1573 if(unicode)
1574 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1575 else
1576 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1578 TRACE("glyph %d\n", glyph);
1580 if(lpgmf)
1582 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
1583 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
1584 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
1585 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
1586 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
1587 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
1589 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
1590 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
1591 lpgmf++;
1594 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1595 funcs->gl.p_glFrontFace(GL_CCW);
1596 if(format == WGL_FONT_POLYGONS)
1598 funcs->gl.p_glNormal3d(0.0, 0.0, 1.0);
1599 gluTessNormal(tess, 0, 0, 1);
1600 gluTessBeginPolygon(tess, NULL);
1603 while(!vertices)
1605 if(vertex_total != -1)
1606 vertices = HeapAlloc(GetProcessHeap(), 0, vertex_total * 3 * sizeof(GLdouble));
1607 vertex_total = 0;
1609 pph = (TTPOLYGONHEADER*)buf;
1610 while((BYTE*)pph < buf + needed)
1612 GLdouble previous[3];
1613 fixed_to_double(pph->pfxStart, em_size, previous);
1615 if(vertices)
1616 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
1618 if(format == WGL_FONT_POLYGONS)
1619 gluTessBeginContour(tess);
1620 else
1621 funcs->gl.p_glBegin(GL_LINE_LOOP);
1623 if(vertices)
1625 fixed_to_double(pph->pfxStart, em_size, vertices);
1626 if(format == WGL_FONT_POLYGONS)
1627 gluTessVertex(tess, vertices, vertices);
1628 else
1629 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1630 vertices += 3;
1632 vertex_total++;
1634 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
1635 while((char*)ppc < (char*)pph + pph->cb)
1637 int i, j;
1638 int num;
1640 switch(ppc->wType) {
1641 case TT_PRIM_LINE:
1642 for(i = 0; i < ppc->cpfx; i++)
1644 if(vertices)
1646 TRACE("\t\tline to %d, %d\n",
1647 ppc->apfx[i].x.value, ppc->apfx[i].y.value);
1648 fixed_to_double(ppc->apfx[i], em_size, vertices);
1649 if(format == WGL_FONT_POLYGONS)
1650 gluTessVertex(tess, vertices, vertices);
1651 else
1652 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1653 vertices += 3;
1655 fixed_to_double(ppc->apfx[i], em_size, previous);
1656 vertex_total++;
1658 break;
1660 case TT_PRIM_QSPLINE:
1661 for(i = 0; i < ppc->cpfx-1; i++)
1663 bezier_vector curve[3];
1664 bezier_vector *points;
1665 GLdouble curve_vertex[3];
1667 if(vertices)
1668 TRACE("\t\tcurve %d,%d %d,%d\n",
1669 ppc->apfx[i].x.value, ppc->apfx[i].y.value,
1670 ppc->apfx[i + 1].x.value, ppc->apfx[i + 1].y.value);
1672 curve[0].x = previous[0];
1673 curve[0].y = previous[1];
1674 fixed_to_double(ppc->apfx[i], em_size, curve_vertex);
1675 curve[1].x = curve_vertex[0];
1676 curve[1].y = curve_vertex[1];
1677 fixed_to_double(ppc->apfx[i + 1], em_size, curve_vertex);
1678 curve[2].x = curve_vertex[0];
1679 curve[2].y = curve_vertex[1];
1680 if(i < ppc->cpfx-2)
1682 curve[2].x = (curve[1].x + curve[2].x)/2;
1683 curve[2].y = (curve[1].y + curve[2].y)/2;
1685 num = bezier_approximate(curve, NULL, deviation);
1686 points = HeapAlloc(GetProcessHeap(), 0, num*sizeof(bezier_vector));
1687 num = bezier_approximate(curve, points, deviation);
1688 vertex_total += num;
1689 if(vertices)
1691 for(j=0; j<num; j++)
1693 TRACE("\t\t\tvertex at %f,%f\n", points[j].x, points[j].y);
1694 vertices[0] = points[j].x;
1695 vertices[1] = points[j].y;
1696 vertices[2] = 0.0;
1697 if(format == WGL_FONT_POLYGONS)
1698 gluTessVertex(tess, vertices, vertices);
1699 else
1700 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1701 vertices += 3;
1704 HeapFree(GetProcessHeap(), 0, points);
1705 previous[0] = curve[2].x;
1706 previous[1] = curve[2].y;
1708 break;
1709 default:
1710 ERR("\t\tcurve type = %d\n", ppc->wType);
1711 if(format == WGL_FONT_POLYGONS)
1712 gluTessEndContour(tess);
1713 else
1714 funcs->gl.p_glEnd();
1715 goto error_in_list;
1718 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
1719 (ppc->cpfx - 1) * sizeof(POINTFX));
1721 if(format == WGL_FONT_POLYGONS)
1722 gluTessEndContour(tess);
1723 else
1724 funcs->gl.p_glEnd();
1725 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
1729 error_in_list:
1730 if(format == WGL_FONT_POLYGONS)
1731 gluTessEndPolygon(tess);
1732 funcs->gl.p_glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
1733 funcs->gl.p_glEndList();
1734 HeapFree(GetProcessHeap(), 0, buf);
1735 HeapFree(GetProcessHeap(), 0, vertices);
1738 error:
1739 DeleteObject(SelectObject(hdc, old_font));
1740 if(format == WGL_FONT_POLYGONS)
1741 gluDeleteTess(tess);
1742 return TRUE;
1746 /***********************************************************************
1747 * wglUseFontOutlinesA (OPENGL32.@)
1749 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
1750 DWORD first,
1751 DWORD count,
1752 DWORD listBase,
1753 FLOAT deviation,
1754 FLOAT extrusion,
1755 int format,
1756 LPGLYPHMETRICSFLOAT lpgmf)
1758 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
1761 /***********************************************************************
1762 * wglUseFontOutlinesW (OPENGL32.@)
1764 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
1765 DWORD first,
1766 DWORD count,
1767 DWORD listBase,
1768 FLOAT deviation,
1769 FLOAT extrusion,
1770 int format,
1771 LPGLYPHMETRICSFLOAT lpgmf)
1773 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1776 /***********************************************************************
1777 * glDebugEntry (OPENGL32.@)
1779 GLint WINAPI glDebugEntry( GLint unknown1, GLint unknown2 )
1781 return 0;
1784 static GLubyte *filter_extensions_list(const char *extensions, const char *disabled)
1786 char *p, *str;
1787 const char *end;
1789 p = str = HeapAlloc(GetProcessHeap(), 0, strlen(extensions) + 2);
1790 if (!str)
1791 return NULL;
1793 TRACE( "GL_EXTENSIONS:\n" );
1795 for (;;)
1797 while (*extensions == ' ')
1798 extensions++;
1799 if (!*extensions)
1800 break;
1801 if (!(end = strchr(extensions, ' ')))
1802 end = extensions + strlen(extensions);
1803 memcpy(p, extensions, end - extensions);
1804 p[end - extensions] = 0;
1805 if (!has_extension(disabled, p, strlen(p)))
1807 TRACE("++ %s\n", p);
1808 p += end - extensions;
1809 *p++ = ' ';
1811 else
1813 TRACE("-- %s (disabled by config)\n", p);
1815 extensions = end;
1817 *p = 0;
1818 return (GLubyte *)str;
1821 static GLuint *filter_extensions_index(const char *disabled)
1823 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1824 const char *ext, *end, *gl_ext;
1825 GLuint *disabled_exts, *new_disabled_exts;
1826 unsigned int i = 0, j, disabled_size;
1827 GLint extensions_count;
1829 if (!funcs->ext.p_glGetStringi)
1831 void **func_ptr = (void **)&funcs->ext.p_glGetStringi;
1833 *func_ptr = funcs->wgl.p_wglGetProcAddress("glGetStringi");
1834 if (!funcs->ext.p_glGetStringi)
1835 return NULL;
1838 funcs->gl.p_glGetIntegerv(GL_NUM_EXTENSIONS, &extensions_count);
1839 disabled_size = 2;
1840 disabled_exts = HeapAlloc(GetProcessHeap(), 0, disabled_size * sizeof(*disabled_exts));
1841 if (!disabled_exts)
1842 return NULL;
1844 TRACE( "GL_EXTENSIONS:\n" );
1846 for (j = 0; j < extensions_count; ++j)
1848 gl_ext = (const char *)funcs->ext.p_glGetStringi(GL_EXTENSIONS, j);
1849 ext = disabled;
1850 for (;;)
1852 while (*ext == ' ')
1853 ext++;
1854 if (!*ext)
1856 TRACE("++ %s\n", gl_ext);
1857 break;
1859 if (!(end = strchr(ext, ' ')))
1860 end = ext + strlen(ext);
1862 if (!strncmp(gl_ext, ext, end - ext) && !gl_ext[end - ext])
1864 if (i + 1 == disabled_size)
1866 disabled_size *= 2;
1867 new_disabled_exts = HeapReAlloc(GetProcessHeap(), 0, disabled_exts,
1868 disabled_size * sizeof(*disabled_exts));
1869 if (!new_disabled_exts)
1871 disabled_exts[i] = ~0u;
1872 return disabled_exts;
1874 disabled_exts = new_disabled_exts;
1876 TRACE("-- %s (disabled by config)\n", gl_ext);
1877 disabled_exts[i++] = j;
1878 break;
1880 ext = end;
1883 disabled_exts[i] = ~0u;
1884 return disabled_exts;
1887 /* build the extension string by filtering out the disabled extensions */
1888 static BOOL filter_extensions(const char *extensions, GLubyte **exts_list, GLuint **disabled_exts)
1890 static const char *disabled;
1892 if (!disabled)
1894 HKEY hkey;
1895 DWORD size;
1896 char *str = NULL;
1898 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1899 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1901 if (!RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size ))
1903 str = HeapAlloc( GetProcessHeap(), 0, size );
1904 if (RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)str, &size )) *str = 0;
1906 RegCloseKey( hkey );
1908 if (str)
1910 if (InterlockedCompareExchangePointer( (void **)&disabled, str, NULL ))
1911 HeapFree( GetProcessHeap(), 0, str );
1913 else disabled = "";
1916 if (!disabled[0])
1917 return FALSE;
1919 if (extensions && !*exts_list)
1920 *exts_list = filter_extensions_list(extensions, disabled);
1922 if (!*disabled_exts)
1923 *disabled_exts = filter_extensions_index(disabled);
1925 return (exts_list && *exts_list) || *disabled_exts;
1928 /***********************************************************************
1929 * glGetString (OPENGL32.@)
1931 const GLubyte * WINAPI glGetString( GLenum name )
1933 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1934 const GLubyte *ret = funcs->gl.p_glGetString( name );
1936 if (name == GL_EXTENSIONS && ret)
1938 struct wgl_handle *ptr = get_current_context_ptr();
1939 if (ptr->u.context->extensions ||
1940 filter_extensions((const char *)ret, &ptr->u.context->extensions, &ptr->u.context->disabled_exts))
1941 ret = ptr->u.context->extensions;
1943 return ret;
1946 /***********************************************************************
1947 * OpenGL initialisation routine
1949 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1951 switch(reason)
1953 case DLL_PROCESS_ATTACH:
1954 NtCurrentTeb()->glTable = &null_opengl_funcs;
1955 break;
1956 case DLL_THREAD_ATTACH:
1957 NtCurrentTeb()->glTable = &null_opengl_funcs;
1958 break;
1960 return TRUE;