user32: Fixed some minor glitches in oic_winlogo.ico.
[wine] / dlls / opengl32 / tests / opengl.c
1 /*
2  * Some tests for OpenGL functions
3  *
4  * Copyright (C) 2007-2008 Roderick Colenbrander
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <windows.h>
22 #include <wingdi.h>
23 #include "wine/test.h"
24
25 const unsigned char * WINAPI glGetString(unsigned int);
26 #define GL_VENDOR 0x1F00
27 #define GL_RENDERER 0x1F01
28 #define GL_VERSION 0x1F02
29
30 #define MAX_FORMATS 256
31 typedef void* HPBUFFERARB;
32
33 /* WGL_ARB_create_context */
34 HGLRC (WINAPI *pwglCreateContextAttribsARB)(HDC hDC, HGLRC hShareContext, const int *attribList);
35 /* GetLastError */
36 #define ERROR_INVALID_VERSION_ARB 0x2095
37 #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
38 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
39 #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
40 #define WGL_CONTEXT_FLAGS_ARB 0x2094
41 /* Flags for WGL_CONTEXT_FLAGS_ARB */
42 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
43 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB  0x0002
44
45 /* WGL_ARB_extensions_string */
46 static const char* (WINAPI *pwglGetExtensionsStringARB)(HDC);
47 static int (WINAPI *pwglReleasePbufferDCARB)(HPBUFFERARB, HDC);
48
49 /* WGL_ARB_make_current_read */
50 static BOOL (WINAPI *pwglMakeContextCurrentARB)(HDC hdraw, HDC hread, HGLRC hglrc);
51 static HDC (WINAPI *pwglGetCurrentReadDCARB)(void);
52
53 /* WGL_ARB_pixel_format */
54 #define WGL_ACCELERATION_ARB 0x2003
55 #define WGL_COLOR_BITS_ARB 0x2014
56 #define WGL_RED_BITS_ARB   0x2015
57 #define WGL_GREEN_BITS_ARB 0x2017
58 #define WGL_BLUE_BITS_ARB  0x2019
59 #define WGL_ALPHA_BITS_ARB 0x201B
60 #define WGL_SUPPORT_GDI_ARB   0x200F
61 #define WGL_DOUBLE_BUFFER_ARB 0x2011
62 #define WGL_NO_ACCELERATION_ARB        0x2025
63 #define WGL_GENERIC_ACCELERATION_ARB   0x2026
64 #define WGL_FULL_ACCELERATION_ARB      0x2027
65
66 static BOOL (WINAPI *pwglChoosePixelFormatARB)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
67 static BOOL (WINAPI *pwglGetPixelFormatAttribivARB)(HDC, int, int, UINT, const int *, int *);
68
69 /* WGL_ARB_pbuffer */
70 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
71 static HPBUFFERARB* (WINAPI *pwglCreatePbufferARB)(HDC, int, int, int, const int *);
72 static HDC (WINAPI *pwglGetPbufferDCARB)(HPBUFFERARB);
73
74 static const char* wgl_extensions = NULL;
75
76 static void init_functions(void)
77 {
78 #define GET_PROC(func) \
79     p ## func = (void*)wglGetProcAddress(#func); \
80     if(!p ## func) \
81       trace("wglGetProcAddress(%s) failed\n", #func);
82
83     /* WGL_ARB_create_context */
84     GET_PROC(wglCreateContextAttribsARB);
85
86     /* WGL_ARB_extensions_string */
87     GET_PROC(wglGetExtensionsStringARB)
88
89     /* WGL_ARB_make_current_read */
90     GET_PROC(wglMakeContextCurrentARB);
91     GET_PROC(wglGetCurrentReadDCARB);
92
93     /* WGL_ARB_pixel_format */
94     GET_PROC(wglChoosePixelFormatARB)
95     GET_PROC(wglGetPixelFormatAttribivARB)
96
97     /* WGL_ARB_pbuffer */
98     GET_PROC(wglCreatePbufferARB)
99     GET_PROC(wglGetPbufferDCARB)
100     GET_PROC(wglReleasePbufferDCARB)
101
102 #undef GET_PROC
103 }
104
105 static void test_pbuffers(HDC hdc)
106 {
107     const int iAttribList[] = { WGL_DRAW_TO_PBUFFER_ARB, 1, /* Request pbuffer support */
108                                 0 };
109     int iFormats[MAX_FORMATS];
110     unsigned int nOnscreenFormats;
111     unsigned int nFormats;
112     int i, res;
113     int iPixelFormat = 0;
114
115     nOnscreenFormats = DescribePixelFormat(hdc, 0, 0, NULL);
116
117     /* When you want to render to a pbuffer you need to call wglGetPbufferDCARB which
118      * returns a 'magic' HDC which you can then pass to wglMakeCurrent to switch rendering
119      * to the pbuffer. Below some tests are performed on what happens if you use standard WGL calls
120      * on this 'magic' HDC for both a pixelformat that support onscreen and offscreen rendering
121      * and a pixelformat that's only available for offscreen rendering (this means that only
122      * wglChoosePixelFormatARB and friends know about the format.
123      *
124      * The first thing we need are pixelformats with pbuffer capabilities.
125      */
126     res = pwglChoosePixelFormatARB(hdc, iAttribList, NULL, MAX_FORMATS, iFormats, &nFormats);
127     if(res <= 0)
128     {
129         skip("No pbuffer compatible formats found while WGL_ARB_pbuffer is supported\n");
130         return;
131     }
132     trace("nOnscreenFormats: %d\n", nOnscreenFormats);
133     trace("Total number of pbuffer capable pixelformats: %d\n", nFormats);
134
135     /* Try to select an onscreen pixelformat out of the list */
136     for(i=0; i < nFormats; i++)
137     {
138         /* Check if the format is onscreen, if it is choose it */
139         if(iFormats[i] <= nOnscreenFormats)
140         {
141             iPixelFormat = iFormats[i];
142             trace("Selected iPixelFormat=%d\n", iPixelFormat);
143             break;
144         }
145     }
146
147     /* A video driver supports a large number of onscreen and offscreen pixelformats.
148      * The traditional WGL calls only see a subset of the whole pixelformat list. First
149      * of all they only see the onscreen formats (the offscreen formats are at the end of the
150      * pixelformat list) and second extended pixelformat capabilities are hidden from the
151      * standard WGL calls. Only functions that depend on WGL_ARB_pixel_format can see them.
152      *
153      * Below we check if the pixelformat is also supported onscreen.
154      */
155     if(iPixelFormat != 0)
156     {
157         HDC pbuffer_hdc;
158         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
159         if(!pbuffer)
160             skip("Pbuffer creation failed!\n");
161
162         /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
163         pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
164         res = GetPixelFormat(pbuffer_hdc);
165         ok(res == iPixelFormat, "Unexpected iPixelFormat=%d returned by GetPixelFormat for format %d\n", res, iPixelFormat);
166         trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
167         trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
168
169         pwglReleasePbufferDCARB(pbuffer, hdc);
170     }
171     else skip("Pbuffer test for onscreen pixelformat skipped as no onscreen format with pbuffer capabilities have been found\n");
172
173     /* Search for a real offscreen format */
174     for(i=0, iPixelFormat=0; i<nFormats; i++)
175     {
176         if(iFormats[i] > nOnscreenFormats)
177         {
178             iPixelFormat = iFormats[i];
179             trace("Selected iPixelFormat: %d\n", iPixelFormat);
180             break;
181         }
182     }
183
184     if(iPixelFormat != 0)
185     {
186         HDC pbuffer_hdc;
187         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
188         if(!pbuffer)
189             skip("Pbuffer creation failed!\n");
190
191         /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
192         pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
193         res = GetPixelFormat(pbuffer_hdc);
194
195         ok(res == 1, "Unexpected iPixelFormat=%d (1 expected) returned by GetPixelFormat for offscreen format %d\n", res, iPixelFormat);
196         trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
197         trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
198         pwglReleasePbufferDCARB(pbuffer, hdc);
199     }
200     else skip("Pbuffer test for offscreen pixelformat skipped as no offscreen-only format with pbuffer capabilities has been found\n");
201 }
202
203 static void test_setpixelformat(HDC winhdc)
204 {
205     int res = 0;
206     int nCfgs;
207     int pf;
208     int i;
209     HWND hwnd;
210     PIXELFORMATDESCRIPTOR pfd = {
211         sizeof(PIXELFORMATDESCRIPTOR),
212         1,                     /* version */
213         PFD_DRAW_TO_WINDOW |
214         PFD_SUPPORT_OPENGL |
215         PFD_DOUBLEBUFFER,
216         PFD_TYPE_RGBA,
217         24,                    /* 24-bit color depth */
218         0, 0, 0, 0, 0, 0,      /* color bits */
219         0,                     /* alpha buffer */
220         0,                     /* shift bit */
221         0,                     /* accumulation buffer */
222         0, 0, 0, 0,            /* accum bits */
223         32,                    /* z-buffer */
224         0,                     /* stencil buffer */
225         0,                     /* auxiliary buffer */
226         PFD_MAIN_PLANE,        /* main layer */
227         0,                     /* reserved */
228         0, 0, 0                /* layer masks */
229     };
230
231     HDC hdc = GetDC(0);
232     ok(hdc != 0, "GetDC(0) failed!\n");
233
234     /* This should pass even on the main device context */
235     pf = ChoosePixelFormat(hdc, &pfd);
236     ok(pf != 0, "ChoosePixelFormat failed on main device context\n");
237
238     /* SetPixelFormat on the main device context 'X root window' should fail,
239      * but some broken drivers allow it
240      */
241     res = SetPixelFormat(hdc, pf, &pfd);
242     trace("SetPixelFormat on main device context %s\n", res ? "succeeded" : "failed");
243
244     /* Setting the same format that was set on the HDC is allowed; other
245        formats fail */
246     nCfgs = DescribePixelFormat(winhdc, 0, 0, NULL);
247     pf = GetPixelFormat(winhdc);
248     for(i = 1;i <= nCfgs;i++)
249     {
250         int res = SetPixelFormat(winhdc, i, NULL);
251         if(i == pf) ok(res, "Failed to set the same pixel format\n");
252         else ok(!res, "Unexpectedly set an alternate pixel format\n");
253     }
254
255     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
256                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
257     ok(hwnd != NULL, "err: %d\n", GetLastError());
258     if (hwnd)
259     {
260         HDC hdc = GetDC( hwnd );
261         pf = ChoosePixelFormat( hdc, &pfd );
262         ok( pf != 0, "ChoosePixelFormat failed\n" );
263         res = SetPixelFormat( hdc, pf, &pfd );
264         ok( res != 0, "SetPixelFormat failed\n" );
265         i = GetPixelFormat( hdc );
266         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
267         ReleaseDC( hwnd, hdc );
268         hdc = GetWindowDC( hwnd );
269         i = GetPixelFormat( hdc );
270         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
271         ReleaseDC( hwnd, hdc );
272         DestroyWindow( hwnd );
273     }
274
275     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
276                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
277     ok(hwnd != NULL, "err: %d\n", GetLastError());
278     if (hwnd)
279     {
280         HDC hdc = GetWindowDC( hwnd );
281         pf = ChoosePixelFormat( hdc, &pfd );
282         ok( pf != 0, "ChoosePixelFormat failed\n" );
283         res = SetPixelFormat( hdc, pf, &pfd );
284         ok( res != 0, "SetPixelFormat failed\n" );
285         i = GetPixelFormat( hdc );
286         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
287         ReleaseDC( hwnd, hdc );
288         DestroyWindow( hwnd );
289     }
290 }
291
292 static void test_sharelists(HDC winhdc)
293 {
294     HGLRC hglrc1, hglrc2, hglrc3;
295     int res;
296
297     hglrc1 = wglCreateContext(winhdc);
298     res = wglShareLists(0, 0);
299     ok(res == FALSE, "Sharing display lists for no contexts passed!\n");
300
301     /* Test 1: Create a context and just share lists without doing anything special */
302     hglrc2 = wglCreateContext(winhdc);
303     if(hglrc2)
304     {
305         res = wglShareLists(hglrc1, hglrc2);
306         ok(res, "Sharing of display lists failed\n");
307         wglDeleteContext(hglrc2);
308     }
309
310     /* Test 2: Share display lists with a 'destination' context which has been made current */
311     hglrc2 = wglCreateContext(winhdc);
312     if(hglrc2)
313     {
314         res = wglMakeCurrent(winhdc, hglrc2);
315         ok(res, "Make current failed\n");
316         res = wglShareLists(hglrc1, hglrc2);
317         todo_wine ok(res, "Sharing display lists with a destination context which has been made current failed\n");
318         wglMakeCurrent(0, 0);
319         wglDeleteContext(hglrc2);
320     }
321
322     /* Test 3: Share display lists with a context which already shares display lists with another context.
323      * According to MSDN the second parameter cannot share any display lists but some buggy drivers might allow it */
324     hglrc3 = wglCreateContext(winhdc);
325     if(hglrc3)
326     {
327         res = wglShareLists(hglrc3, hglrc1);
328         ok(res == FALSE, "Sharing of display lists passed for a context which already shared lists before\n");
329         wglDeleteContext(hglrc3);
330     }
331
332     /* Test 4: Share display lists with a 'source' context which has been made current */
333     hglrc2 = wglCreateContext(winhdc);
334     if(hglrc2)
335     {
336         res = wglMakeCurrent(winhdc, hglrc1);
337         ok(res, "Make current failed\n");
338         res = wglShareLists(hglrc1, hglrc2);
339         ok(res, "Sharing display lists with a source context which has been made current failed\n");
340         wglMakeCurrent(0, 0);
341         wglDeleteContext(hglrc2);
342     }
343 }
344
345 static void test_makecurrent(HDC winhdc)
346 {
347     BOOL ret;
348     HGLRC hglrc;
349     DWORD error;
350
351     hglrc = wglCreateContext(winhdc);
352     ok( hglrc != 0, "wglCreateContext failed\n" );
353
354     ret = wglMakeCurrent( winhdc, hglrc );
355     ok( ret, "wglMakeCurrent failed\n" );
356
357     ok( wglGetCurrentContext() == hglrc, "wrong context\n" );
358
359     /* set the same context again */
360     ret = wglMakeCurrent( winhdc, hglrc );
361     ok( ret, "wglMakeCurrent failed\n" );
362
363     /* check wglMakeCurrent(x, y) after another call to wglMakeCurrent(x, y) */
364     ret = wglMakeCurrent( winhdc, NULL );
365     ok( ret, "wglMakeCurrent failed\n" );
366
367     ret = wglMakeCurrent( winhdc, NULL );
368     ok( ret, "wglMakeCurrent failed\n" );
369
370     SetLastError( 0xdeadbeef );
371     ret = wglMakeCurrent( NULL, NULL );
372     ok( !ret, "wglMakeCurrent succeeded\n" );
373     error = GetLastError();
374     ok( error == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got error=%x\n", error);
375
376     ret = wglMakeCurrent( winhdc, NULL );
377     ok( ret, "wglMakeCurrent failed\n" );
378
379     ret = wglMakeCurrent( winhdc, hglrc );
380     ok( ret, "wglMakeCurrent failed\n" );
381
382     ret = wglMakeCurrent( NULL, NULL );
383     ok( ret, "wglMakeCurrent failed\n" );
384
385     SetLastError( 0xdeadbeef );
386     ret = wglMakeCurrent( NULL, NULL );
387     ok( !ret, "wglMakeCurrent succeeded\n" );
388     error = GetLastError();
389     ok( error == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got error=%x\n", error);
390
391     ret = wglMakeCurrent( winhdc, hglrc );
392     ok( ret, "wglMakeCurrent failed\n" );
393 }
394
395 static void test_colorbits(HDC hdc)
396 {
397     const int iAttribList[] = { WGL_COLOR_BITS_ARB, WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB,
398                                 WGL_BLUE_BITS_ARB, WGL_ALPHA_BITS_ARB };
399     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
400     const int iAttribs[] = { WGL_ALPHA_BITS_ARB, 1, 0 };
401     unsigned int nFormats;
402     int res;
403     int iPixelFormat = 0;
404
405     if (!pwglChoosePixelFormatARB)
406     {
407         win_skip("wglChoosePixelFormatARB is not available\n");
408         return;
409     }
410
411     /* We need a pixel format with at least one bit of alpha */
412     res = pwglChoosePixelFormatARB(hdc, iAttribs, NULL, 1, &iPixelFormat, &nFormats);
413     if(res == FALSE || nFormats == 0)
414     {
415         skip("No suitable pixel formats found\n");
416         return;
417     }
418
419     res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
420               sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
421     if(res == FALSE)
422     {
423         skip("wglGetPixelFormatAttribivARB failed\n");
424         return;
425     }
426     iAttribRet[1] += iAttribRet[2]+iAttribRet[3]+iAttribRet[4];
427     ok(iAttribRet[0] == iAttribRet[1], "WGL_COLOR_BITS_ARB (%d) does not equal R+G+B+A (%d)!\n",
428                                        iAttribRet[0], iAttribRet[1]);
429 }
430
431 static void test_gdi_dbuf(HDC hdc)
432 {
433     const int iAttribList[] = { WGL_SUPPORT_GDI_ARB, WGL_DOUBLE_BUFFER_ARB };
434     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
435     unsigned int nFormats;
436     int iPixelFormat;
437     int res;
438
439     if (!pwglGetPixelFormatAttribivARB)
440     {
441         win_skip("wglGetPixelFormatAttribivARB is not available\n");
442         return;
443     }
444
445     nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
446     for(iPixelFormat = 1;iPixelFormat <= nFormats;iPixelFormat++)
447     {
448         res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
449                   sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
450                   iAttribRet);
451         ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
452         if(res == FALSE)
453             continue;
454
455         ok(!(iAttribRet[0] && iAttribRet[1]), "GDI support and double buffering on pixel format %d\n", iPixelFormat);
456     }
457 }
458
459 static void test_acceleration(HDC hdc)
460 {
461     const int iAttribList[] = { WGL_ACCELERATION_ARB };
462     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
463     unsigned int nFormats;
464     int iPixelFormat;
465     int res;
466     PIXELFORMATDESCRIPTOR pfd;
467
468     if (!pwglGetPixelFormatAttribivARB)
469     {
470         win_skip("wglGetPixelFormatAttribivARB is not available\n");
471         return;
472     }
473
474     nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
475     for(iPixelFormat = 1; iPixelFormat <= nFormats; iPixelFormat++)
476     {
477         res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
478                   sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
479                   iAttribRet);
480         ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
481         if(res == FALSE)
482             continue;
483
484         memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
485         DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
486
487         switch(iAttribRet[0])
488         {
489             case WGL_NO_ACCELERATION_ARB:
490                 ok( (pfd.dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED)) == PFD_GENERIC_FORMAT , "Expected only PFD_GENERIC_FORMAT to be set for WGL_NO_ACCELERATION_ARB!: iPixelFormat=%d, dwFlags=%x!\n", iPixelFormat, pfd.dwFlags);
491                 break;
492             case WGL_GENERIC_ACCELERATION_ARB:
493                 ok( (pfd.dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED)) == (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED), "Expected both PFD_GENERIC_FORMAT and PFD_GENERIC_ACCELERATION to be set for WGL_GENERIC_ACCELERATION_ARB: iPixelFormat=%d, dwFlags=%x!\n", iPixelFormat, pfd.dwFlags);
494                 break;
495             case WGL_FULL_ACCELERATION_ARB:
496                 ok( (pfd.dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED)) == 0, "Expected no PFD_GENERIC_FORMAT/_ACCELERATION to be set for WGL_FULL_ACCELERATION_ARB: iPixelFormat=%d, dwFlags=%x!\n", iPixelFormat, pfd.dwFlags);
497                 break;
498         }
499     }
500 }
501
502 struct wgl_thread_param
503 {
504     HANDLE test_finished;
505     HGLRC hglrc;
506     BOOL hglrc_deleted;
507 };
508
509 static DWORD WINAPI wgl_thread(void *param)
510 {
511     struct wgl_thread_param *p = param;
512
513     p->hglrc_deleted = wglDeleteContext(p->hglrc);
514     SetEvent(p->test_finished);
515
516     return 0;
517 }
518
519 static void test_deletecontext(HDC hdc)
520 {
521     struct wgl_thread_param thread_params;
522     HGLRC hglrc = wglCreateContext(hdc);
523     HANDLE thread_handle;
524     DWORD res, tid;
525
526     if(!hglrc)
527     {
528         skip("wglCreateContext failed!\n");
529         return;
530     }
531
532     res = wglMakeCurrent(hdc, hglrc);
533     if(!res)
534     {
535         skip("wglMakeCurrent failed!\n");
536         return;
537     }
538
539     /* WGL doesn't allow you to delete a context from a different thread than the one in which it is current.
540      * This differs from GLX which does allow it but it delays actual deletion until the context becomes not current.
541      */
542     thread_params.hglrc = hglrc;
543     thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
544     thread_handle = CreateThread(NULL, 0, wgl_thread, &thread_params, 0, &tid);
545     ok(!!thread_handle, "Failed to create thread, last error %#x.\n", GetLastError());
546     if(thread_handle)
547     {
548         WaitForSingleObject(thread_handle, INFINITE);
549         ok(thread_params.hglrc_deleted == FALSE, "Attempt to delete WGL context from another thread passed but should fail!\n");
550     }
551     CloseHandle(thread_params.test_finished);
552
553     res = wglDeleteContext(hglrc);
554     ok(res == TRUE, "wglDeleteContext failed\n");
555
556     /* WGL makes a context not current when deleting it. This differs from GLX behavior where
557      * deletion takes place when the thread becomes not current. */
558     hglrc = wglGetCurrentContext();
559     ok(hglrc == NULL, "A WGL context is active while none was expected\n");
560 }
561
562 static void test_make_current_read(HDC hdc)
563 {
564     int res;
565     HDC hread;
566     HGLRC hglrc = wglCreateContext(hdc);
567
568     if(!hglrc)
569     {
570         skip("wglCreateContext failed!\n");
571         return;
572     }
573
574     res = wglMakeCurrent(hdc, hglrc);
575     if(!res)
576     {
577         skip("wglMakeCurrent failed!\n");
578         return;
579     }
580
581     /* Test what wglGetCurrentReadDCARB does for wglMakeCurrent as the spec doesn't mention it */
582     hread = pwglGetCurrentReadDCARB();
583     trace("hread %p, hdc %p\n", hread, hdc);
584     ok(hread == hdc, "wglGetCurrentReadDCARB failed for standard wglMakeCurrent\n");
585
586     pwglMakeContextCurrentARB(hdc, hdc, hglrc);
587     hread = pwglGetCurrentReadDCARB();
588     ok(hread == hdc, "wglGetCurrentReadDCARB failed for wglMakeContextCurrent\n");
589 }
590
591 static void test_dc(HWND hwnd, HDC hdc)
592 {
593     int pf1, pf2;
594     HDC hdc2;
595
596     /* Get another DC and make sure it has the same pixel format */
597     hdc2 = GetDC(hwnd);
598     if(hdc != hdc2)
599     {
600         pf1 = GetPixelFormat(hdc);
601         pf2 = GetPixelFormat(hdc2);
602         ok(pf1 == pf2, "Second DC does not have the same format (%d != %d)\n", pf1, pf2);
603     }
604     else
605         skip("Could not get a different DC for the window\n");
606
607     if(hdc2)
608     {
609         ReleaseDC(hwnd, hdc2);
610         hdc2 = NULL;
611     }
612 }
613
614 /* Nvidia converts win32 error codes to (0xc007 << 16) | win32_error_code */
615 #define NVIDIA_HRESULT_FROM_WIN32(x) (HRESULT_FROM_WIN32(x) | 0x40000000)
616 static void test_opengl3(HDC hdc)
617 {
618     /* Try to create a context compatible with OpenGL 1.x; 1.0-2.1 is allowed */
619     {
620         HGLRC gl3Ctx;
621         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 1, 0};
622
623         gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
624         ok(gl3Ctx != 0, "pwglCreateContextAttribsARB for a 1.x context failed!\n");
625         wglDeleteContext(gl3Ctx);
626     }
627
628     /* Try to pass an invalid HDC */
629     {
630         HGLRC gl3Ctx;
631         DWORD error;
632         gl3Ctx = pwglCreateContextAttribsARB((HDC)0xdeadbeef, 0, 0);
633         ok(gl3Ctx == 0, "pwglCreateContextAttribsARB using an invalid HDC passed\n");
634         error = GetLastError();
635         todo_wine ok(error == ERROR_DC_NOT_FOUND ||
636                      broken(error == NVIDIA_HRESULT_FROM_WIN32(ERROR_INVALID_DATA)), /* Nvidia Vista + Win7 */
637                      "Expected ERROR_DC_NOT_FOUND, got error=%x\n", error);
638         wglDeleteContext(gl3Ctx);
639     }
640
641     /* Try to pass an invalid shareList */
642     {
643         HGLRC gl3Ctx;
644         DWORD error;
645         gl3Ctx = pwglCreateContextAttribsARB(hdc, (HGLRC)0xdeadbeef, 0);
646         todo_wine ok(gl3Ctx == 0, "pwglCreateContextAttribsARB using an invalid shareList passed\n");
647         error = GetLastError();
648         /* The Nvidia implementation seems to return hresults instead of win32 error codes */
649         todo_wine ok(error == ERROR_INVALID_OPERATION ||
650                      error == NVIDIA_HRESULT_FROM_WIN32(ERROR_INVALID_OPERATION), "Expected ERROR_INVALID_OPERATION, got error=%x\n", error);
651         wglDeleteContext(gl3Ctx);
652     }
653
654     /* Try to create an OpenGL 3.0 context */
655     {
656         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
657         HGLRC gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
658
659         if(gl3Ctx == NULL)
660         {
661             skip("Skipping the rest of the WGL_ARB_create_context test due to lack of OpenGL 3.0\n");
662             return;
663         }
664
665         wglDeleteContext(gl3Ctx);
666     }
667
668     /* Test matching an OpenGL 3.0 context with an older one, OpenGL 3.0 should allow it until the new object model is introduced in a future revision */
669     {
670         HGLRC glCtx = wglCreateContext(hdc);
671
672         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
673         int attribs_future[] = {WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
674
675         HGLRC gl3Ctx = pwglCreateContextAttribsARB(hdc, glCtx, attribs);
676         ok(gl3Ctx != NULL, "Sharing of a display list between OpenGL 3.0 and OpenGL 1.x/2.x failed!\n");
677         if(gl3Ctx)
678             wglDeleteContext(gl3Ctx);
679
680         gl3Ctx = pwglCreateContextAttribsARB(hdc, glCtx, attribs_future);
681         ok(gl3Ctx != NULL, "Sharing of a display list between a forward compatible OpenGL 3.0 context and OpenGL 1.x/2.x failed!\n");
682         if(gl3Ctx)
683             wglDeleteContext(gl3Ctx);
684
685         if(glCtx)
686             wglDeleteContext(glCtx);
687     }
688
689     /* Try to create an OpenGL 3.0 context and test windowless rendering */
690     {
691         HGLRC gl3Ctx;
692         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
693         BOOL res;
694
695         gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
696         ok(gl3Ctx != 0, "pwglCreateContextAttribsARB for a 3.0 context failed!\n");
697
698         /* OpenGL 3.0 allows offscreen rendering WITHOUT a drawable
699          * Neither AMD or Nvidia support it at this point. The WGL_ARB_create_context specs also say that
700          * it is hard because drivers use the HDC to enter the display driver and it sounds like they don't
701          * expect drivers to ever offer it.
702          */
703         res = wglMakeCurrent(0, gl3Ctx);
704         ok(res == FALSE, "Wow, OpenGL 3.0 windowless rendering passed while it was expected not to!\n");
705         if(res)
706             wglMakeCurrent(0, 0);
707
708         if(gl3Ctx)
709             wglDeleteContext(gl3Ctx);
710     }
711 }
712
713 static void test_minimized(void)
714 {
715     PIXELFORMATDESCRIPTOR pf_desc =
716     {
717         sizeof(PIXELFORMATDESCRIPTOR),
718         1,                     /* version */
719         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
720         PFD_TYPE_RGBA,
721         24,                    /* 24-bit color depth */
722         0, 0, 0, 0, 0, 0,      /* color bits */
723         0,                     /* alpha buffer */
724         0,                     /* shift bit */
725         0,                     /* accumulation buffer */
726         0, 0, 0, 0,            /* accum bits */
727         32,                    /* z-buffer */
728         0,                     /* stencil buffer */
729         0,                     /* auxiliary buffer */
730         PFD_MAIN_PLANE,        /* main layer */
731         0,                     /* reserved */
732         0, 0, 0                /* layer masks */
733     };
734     int pixel_format;
735     HWND window;
736     LONG style;
737     HGLRC ctx;
738     BOOL ret;
739     HDC dc;
740
741     window = CreateWindowA("static", "opengl32_test",
742             WS_POPUP | WS_MINIMIZE, 0, 0, 640, 480, 0, 0, 0, 0);
743     ok(!!window, "Failed to create window, last error %#x.\n", GetLastError());
744
745     dc = GetDC(window);
746     ok(!!dc, "Failed to get DC.\n");
747
748     pixel_format = ChoosePixelFormat(dc, &pf_desc);
749     if (!pixel_format)
750     {
751         win_skip("Failed to find pixel format.\n");
752         ReleaseDC(window, dc);
753         DestroyWindow(window);
754         return;
755     }
756
757     ret = SetPixelFormat(dc, pixel_format, &pf_desc);
758     ok(ret, "Failed to set pixel format, last error %#x.\n", GetLastError());
759
760     style = GetWindowLongA(window, GWL_STYLE);
761     ok(style & WS_MINIMIZE, "Window should be minimized, got style %#x.\n", style);
762
763     ctx = wglCreateContext(dc);
764     ok(!!ctx, "Failed to create GL context, last error %#x.\n", GetLastError());
765
766     ret = wglMakeCurrent(dc, ctx);
767     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
768
769     style = GetWindowLongA(window, GWL_STYLE);
770     ok(style & WS_MINIMIZE, "window should be minimized, got style %#x.\n", style);
771
772     ret = wglMakeCurrent(NULL, NULL);
773     ok(ret, "Failed to clear current context, last error %#x.\n", GetLastError());
774
775     ret = wglDeleteContext(ctx);
776     ok(ret, "Failed to delete GL context, last error %#x.\n", GetLastError());
777
778     ReleaseDC(window, dc);
779     DestroyWindow(window);
780 }
781
782 START_TEST(opengl)
783 {
784     HWND hwnd;
785     PIXELFORMATDESCRIPTOR pfd = {
786         sizeof(PIXELFORMATDESCRIPTOR),
787         1,                     /* version */
788         PFD_DRAW_TO_WINDOW |
789         PFD_SUPPORT_OPENGL |
790         PFD_DOUBLEBUFFER,
791         PFD_TYPE_RGBA,
792         24,                    /* 24-bit color depth */
793         0, 0, 0, 0, 0, 0,      /* color bits */
794         0,                     /* alpha buffer */
795         0,                     /* shift bit */
796         0,                     /* accumulation buffer */
797         0, 0, 0, 0,            /* accum bits */
798         32,                    /* z-buffer */
799         0,                     /* stencil buffer */
800         0,                     /* auxiliary buffer */
801         PFD_MAIN_PLANE,        /* main layer */
802         0,                     /* reserved */
803         0, 0, 0                /* layer masks */
804     };
805
806     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
807                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
808     ok(hwnd != NULL, "err: %d\n", GetLastError());
809     if (hwnd)
810     {
811         HDC hdc;
812         int iPixelFormat, res;
813         HGLRC hglrc;
814         DWORD error;
815         ShowWindow(hwnd, SW_SHOW);
816
817         hdc = GetDC(hwnd);
818
819         iPixelFormat = ChoosePixelFormat(hdc, &pfd);
820         if(iPixelFormat == 0)
821         {
822             /* This should never happen as ChoosePixelFormat always returns a closest match, but currently this fails in Wine if we don't have glX */
823             win_skip("Unable to find pixel format.\n");
824             goto cleanup;
825         }
826
827         /* We shouldn't be able to create a context from a hdc which doesn't have a pixel format set */
828         hglrc = wglCreateContext(hdc);
829         ok(hglrc == NULL, "wglCreateContext should fail when no pixel format has been set, but it passed\n");
830         error = GetLastError();
831         ok(error == ERROR_INVALID_PIXEL_FORMAT, "expected ERROR_INVALID_PIXEL_FORMAT for wglCreateContext without a pixelformat set, but received %#x\n", error);
832
833         res = SetPixelFormat(hdc, iPixelFormat, &pfd);
834         ok(res, "SetPixelformat failed: %x\n", GetLastError());
835
836         test_minimized();
837         test_dc(hwnd, hdc);
838
839         hglrc = wglCreateContext(hdc);
840         res = wglMakeCurrent(hdc, hglrc);
841         ok(res, "wglMakeCurrent failed!\n");
842         if(res)
843         {
844             trace("OpenGL renderer: %s\n", glGetString(GL_RENDERER));
845             trace("OpenGL driver version: %s\n", glGetString(GL_VERSION));
846             trace("OpenGL vendor: %s\n", glGetString(GL_VENDOR));
847         }
848         else
849         {
850             skip("Skipping OpenGL tests without a current context\n");
851             return;
852         }
853
854         /* Initialisation of WGL functions depends on an implicit WGL context. For this reason we can't load them before making
855          * any WGL call :( On Wine this would work but not on real Windows because there can be different implementations (software, ICD, MCD).
856          */
857         init_functions();
858         /* The lack of wglGetExtensionsStringARB in general means broken software rendering or the lack of decent OpenGL support, skip tests in such cases */
859         if (!pwglGetExtensionsStringARB)
860         {
861             win_skip("wglGetExtensionsStringARB is not available\n");
862             return;
863         }
864
865         test_deletecontext(hdc);
866         test_makecurrent(hdc);
867         test_setpixelformat(hdc);
868         test_sharelists(hdc);
869         test_colorbits(hdc);
870         test_gdi_dbuf(hdc);
871         test_acceleration(hdc);
872
873         wgl_extensions = pwglGetExtensionsStringARB(hdc);
874         if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
875
876         if(strstr(wgl_extensions, "WGL_ARB_create_context"))
877             test_opengl3(hdc);
878
879         if(strstr(wgl_extensions, "WGL_ARB_make_current_read"))
880             test_make_current_read(hdc);
881         else
882             skip("WGL_ARB_make_current_read not supported, skipping test\n");
883
884         if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
885             test_pbuffers(hdc);
886         else
887             skip("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");
888
889 cleanup:
890         ReleaseDC(hwnd, hdc);
891         DestroyWindow(hwnd);
892     }
893 }