d3drm: Implement IDirect3DRMMesh_AddGroup.
[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 void WINAPI glClearColor(float red, float green, float blue, float alpha);
26 void WINAPI glClear(unsigned int mask);
27 #define GL_COLOR 0x1800
28 typedef unsigned int GLenum;
29 typedef int GLint;
30 void WINAPI glCopyPixels(int x, int y, int width, int height, GLenum type);
31 void WINAPI glFinish(void);
32 #define GL_NO_ERROR 0x0
33 #define GL_INVALID_OPERATION 0x502
34 GLenum WINAPI glGetError(void);
35 #define GL_COLOR_BUFFER_BIT 0x00004000
36 const unsigned char * WINAPI glGetString(unsigned int);
37 #define GL_VENDOR 0x1F00
38 #define GL_RENDERER 0x1F01
39 #define GL_VERSION 0x1F02
40
41 #define GL_VIEWPORT 0x0ba2
42 void WINAPI glGetIntegerv(GLenum pname, GLint *params);
43
44 #define MAX_FORMATS 256
45 typedef void* HPBUFFERARB;
46
47 /* WGL_ARB_create_context */
48 static HGLRC (WINAPI *pwglCreateContextAttribsARB)(HDC hDC, HGLRC hShareContext, const int *attribList);
49 /* GetLastError */
50 #define ERROR_INVALID_VERSION_ARB 0x2095
51 #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
52 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
53 #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
54 #define WGL_CONTEXT_FLAGS_ARB 0x2094
55 /* Flags for WGL_CONTEXT_FLAGS_ARB */
56 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
57 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB  0x0002
58
59 /* WGL_ARB_extensions_string */
60 static const char* (WINAPI *pwglGetExtensionsStringARB)(HDC);
61 static int (WINAPI *pwglReleasePbufferDCARB)(HPBUFFERARB, HDC);
62
63 /* WGL_ARB_make_current_read */
64 static BOOL (WINAPI *pwglMakeContextCurrentARB)(HDC hdraw, HDC hread, HGLRC hglrc);
65 static HDC (WINAPI *pwglGetCurrentReadDCARB)(void);
66
67 /* WGL_ARB_pixel_format */
68 #define WGL_ACCELERATION_ARB 0x2003
69 #define WGL_COLOR_BITS_ARB 0x2014
70 #define WGL_RED_BITS_ARB   0x2015
71 #define WGL_GREEN_BITS_ARB 0x2017
72 #define WGL_BLUE_BITS_ARB  0x2019
73 #define WGL_ALPHA_BITS_ARB 0x201B
74 #define WGL_SUPPORT_GDI_ARB   0x200F
75 #define WGL_DOUBLE_BUFFER_ARB 0x2011
76 #define WGL_NO_ACCELERATION_ARB        0x2025
77 #define WGL_GENERIC_ACCELERATION_ARB   0x2026
78 #define WGL_FULL_ACCELERATION_ARB      0x2027
79
80 static BOOL (WINAPI *pwglChoosePixelFormatARB)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
81 static BOOL (WINAPI *pwglGetPixelFormatAttribivARB)(HDC, int, int, UINT, const int *, int *);
82
83 /* WGL_ARB_pbuffer */
84 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
85 static HPBUFFERARB* (WINAPI *pwglCreatePbufferARB)(HDC, int, int, int, const int *);
86 static HDC (WINAPI *pwglGetPbufferDCARB)(HPBUFFERARB);
87
88 static const char* wgl_extensions = NULL;
89
90 static void init_functions(void)
91 {
92 #define GET_PROC(func) \
93     p ## func = (void*)wglGetProcAddress(#func); \
94     if(!p ## func) \
95       trace("wglGetProcAddress(%s) failed\n", #func);
96
97     /* WGL_ARB_create_context */
98     GET_PROC(wglCreateContextAttribsARB);
99
100     /* WGL_ARB_extensions_string */
101     GET_PROC(wglGetExtensionsStringARB)
102
103     /* WGL_ARB_make_current_read */
104     GET_PROC(wglMakeContextCurrentARB);
105     GET_PROC(wglGetCurrentReadDCARB);
106
107     /* WGL_ARB_pixel_format */
108     GET_PROC(wglChoosePixelFormatARB)
109     GET_PROC(wglGetPixelFormatAttribivARB)
110
111     /* WGL_ARB_pbuffer */
112     GET_PROC(wglCreatePbufferARB)
113     GET_PROC(wglGetPbufferDCARB)
114     GET_PROC(wglReleasePbufferDCARB)
115
116 #undef GET_PROC
117 }
118
119 static void test_pbuffers(HDC hdc)
120 {
121     const int iAttribList[] = { WGL_DRAW_TO_PBUFFER_ARB, 1, /* Request pbuffer support */
122                                 0 };
123     int iFormats[MAX_FORMATS];
124     unsigned int nOnscreenFormats;
125     unsigned int nFormats;
126     int i, res;
127     int iPixelFormat = 0;
128
129     nOnscreenFormats = DescribePixelFormat(hdc, 0, 0, NULL);
130
131     /* When you want to render to a pbuffer you need to call wglGetPbufferDCARB which
132      * returns a 'magic' HDC which you can then pass to wglMakeCurrent to switch rendering
133      * to the pbuffer. Below some tests are performed on what happens if you use standard WGL calls
134      * on this 'magic' HDC for both a pixelformat that support onscreen and offscreen rendering
135      * and a pixelformat that's only available for offscreen rendering (this means that only
136      * wglChoosePixelFormatARB and friends know about the format.
137      *
138      * The first thing we need are pixelformats with pbuffer capabilities.
139      */
140     res = pwglChoosePixelFormatARB(hdc, iAttribList, NULL, MAX_FORMATS, iFormats, &nFormats);
141     if(res <= 0)
142     {
143         skip("No pbuffer compatible formats found while WGL_ARB_pbuffer is supported\n");
144         return;
145     }
146     trace("nOnscreenFormats: %d\n", nOnscreenFormats);
147     trace("Total number of pbuffer capable pixelformats: %d\n", nFormats);
148
149     /* Try to select an onscreen pixelformat out of the list */
150     for(i=0; i < nFormats; i++)
151     {
152         /* Check if the format is onscreen, if it is choose it */
153         if(iFormats[i] <= nOnscreenFormats)
154         {
155             iPixelFormat = iFormats[i];
156             trace("Selected iPixelFormat=%d\n", iPixelFormat);
157             break;
158         }
159     }
160
161     /* A video driver supports a large number of onscreen and offscreen pixelformats.
162      * The traditional WGL calls only see a subset of the whole pixelformat list. First
163      * of all they only see the onscreen formats (the offscreen formats are at the end of the
164      * pixelformat list) and second extended pixelformat capabilities are hidden from the
165      * standard WGL calls. Only functions that depend on WGL_ARB_pixel_format can see them.
166      *
167      * Below we check if the pixelformat is also supported onscreen.
168      */
169     if(iPixelFormat != 0)
170     {
171         HDC pbuffer_hdc;
172         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
173         if(!pbuffer)
174             skip("Pbuffer creation failed!\n");
175
176         /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
177         pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
178         res = GetPixelFormat(pbuffer_hdc);
179         ok(res == iPixelFormat, "Unexpected iPixelFormat=%d returned by GetPixelFormat for format %d\n", res, iPixelFormat);
180         trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
181         trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
182
183         pwglReleasePbufferDCARB(pbuffer, hdc);
184     }
185     else skip("Pbuffer test for onscreen pixelformat skipped as no onscreen format with pbuffer capabilities have been found\n");
186
187     /* Search for a real offscreen format */
188     for(i=0, iPixelFormat=0; i<nFormats; i++)
189     {
190         if(iFormats[i] > nOnscreenFormats)
191         {
192             iPixelFormat = iFormats[i];
193             trace("Selected iPixelFormat: %d\n", iPixelFormat);
194             break;
195         }
196     }
197
198     if(iPixelFormat != 0)
199     {
200         HDC pbuffer_hdc;
201         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
202         if(!pbuffer)
203             skip("Pbuffer creation failed!\n");
204
205         /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
206         pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
207         res = GetPixelFormat(pbuffer_hdc);
208
209         ok(res == 1, "Unexpected iPixelFormat=%d (1 expected) returned by GetPixelFormat for offscreen format %d\n", res, iPixelFormat);
210         trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
211         trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
212         pwglReleasePbufferDCARB(pbuffer, hdc);
213     }
214     else skip("Pbuffer test for offscreen pixelformat skipped as no offscreen-only format with pbuffer capabilities has been found\n");
215 }
216
217 static void test_setpixelformat(HDC winhdc)
218 {
219     int res = 0;
220     int nCfgs;
221     int pf;
222     int i;
223     HWND hwnd;
224     PIXELFORMATDESCRIPTOR pfd = {
225         sizeof(PIXELFORMATDESCRIPTOR),
226         1,                     /* version */
227         PFD_DRAW_TO_WINDOW |
228         PFD_SUPPORT_OPENGL |
229         PFD_DOUBLEBUFFER,
230         PFD_TYPE_RGBA,
231         24,                    /* 24-bit color depth */
232         0, 0, 0, 0, 0, 0,      /* color bits */
233         0,                     /* alpha buffer */
234         0,                     /* shift bit */
235         0,                     /* accumulation buffer */
236         0, 0, 0, 0,            /* accum bits */
237         32,                    /* z-buffer */
238         0,                     /* stencil buffer */
239         0,                     /* auxiliary buffer */
240         PFD_MAIN_PLANE,        /* main layer */
241         0,                     /* reserved */
242         0, 0, 0                /* layer masks */
243     };
244
245     HDC hdc = GetDC(0);
246     ok(hdc != 0, "GetDC(0) failed!\n");
247
248     /* This should pass even on the main device context */
249     pf = ChoosePixelFormat(hdc, &pfd);
250     ok(pf != 0, "ChoosePixelFormat failed on main device context\n");
251
252     /* SetPixelFormat on the main device context 'X root window' should fail,
253      * but some broken drivers allow it
254      */
255     res = SetPixelFormat(hdc, pf, &pfd);
256     trace("SetPixelFormat on main device context %s\n", res ? "succeeded" : "failed");
257
258     /* Setting the same format that was set on the HDC is allowed; other
259        formats fail */
260     nCfgs = DescribePixelFormat(winhdc, 0, 0, NULL);
261     pf = GetPixelFormat(winhdc);
262     for(i = 1;i <= nCfgs;i++)
263     {
264         int res = SetPixelFormat(winhdc, i, NULL);
265         if(i == pf) ok(res, "Failed to set the same pixel format\n");
266         else ok(!res, "Unexpectedly set an alternate pixel format\n");
267     }
268
269     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
270                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
271     ok(hwnd != NULL, "err: %d\n", GetLastError());
272     if (hwnd)
273     {
274         HDC hdc = GetDC( hwnd );
275         pf = ChoosePixelFormat( hdc, &pfd );
276         ok( pf != 0, "ChoosePixelFormat failed\n" );
277         res = SetPixelFormat( hdc, pf, &pfd );
278         ok( res != 0, "SetPixelFormat failed\n" );
279         i = GetPixelFormat( hdc );
280         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
281         ReleaseDC( hwnd, hdc );
282         hdc = GetWindowDC( hwnd );
283         i = GetPixelFormat( hdc );
284         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
285         ReleaseDC( hwnd, hdc );
286         DestroyWindow( hwnd );
287     }
288
289     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
290                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
291     ok(hwnd != NULL, "err: %d\n", GetLastError());
292     if (hwnd)
293     {
294         HDC hdc = GetWindowDC( hwnd );
295         pf = ChoosePixelFormat( hdc, &pfd );
296         ok( pf != 0, "ChoosePixelFormat failed\n" );
297         res = SetPixelFormat( hdc, pf, &pfd );
298         ok( res != 0, "SetPixelFormat failed\n" );
299         i = GetPixelFormat( hdc );
300         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
301         ReleaseDC( hwnd, hdc );
302         DestroyWindow( hwnd );
303     }
304 }
305
306 static void test_sharelists(HDC winhdc)
307 {
308     HGLRC hglrc1, hglrc2, hglrc3;
309     int res;
310
311     hglrc1 = wglCreateContext(winhdc);
312     res = wglShareLists(0, 0);
313     ok(res == FALSE, "Sharing display lists for no contexts passed!\n");
314
315     /* Test 1: Create a context and just share lists without doing anything special */
316     hglrc2 = wglCreateContext(winhdc);
317     if(hglrc2)
318     {
319         res = wglShareLists(hglrc1, hglrc2);
320         ok(res, "Sharing of display lists failed\n");
321         wglDeleteContext(hglrc2);
322     }
323
324     /* Test 2: Share display lists with a 'destination' context which has been made current */
325     hglrc2 = wglCreateContext(winhdc);
326     if(hglrc2)
327     {
328         res = wglMakeCurrent(winhdc, hglrc2);
329         ok(res, "Make current failed\n");
330         res = wglShareLists(hglrc1, hglrc2);
331         todo_wine ok(res, "Sharing display lists with a destination context which has been made current failed\n");
332         wglMakeCurrent(0, 0);
333         wglDeleteContext(hglrc2);
334     }
335
336     /* Test 3: Share display lists with a context which already shares display lists with another context.
337      * According to MSDN the second parameter cannot share any display lists but some buggy drivers might allow it */
338     hglrc3 = wglCreateContext(winhdc);
339     if(hglrc3)
340     {
341         res = wglShareLists(hglrc3, hglrc1);
342         ok(res == FALSE, "Sharing of display lists passed for a context which already shared lists before\n");
343         wglDeleteContext(hglrc3);
344     }
345
346     /* Test 4: Share display lists with a 'source' context which has been made current */
347     hglrc2 = wglCreateContext(winhdc);
348     if(hglrc2)
349     {
350         res = wglMakeCurrent(winhdc, hglrc1);
351         ok(res, "Make current failed\n");
352         res = wglShareLists(hglrc1, hglrc2);
353         ok(res, "Sharing display lists with a source context which has been made current failed\n");
354         wglMakeCurrent(0, 0);
355         wglDeleteContext(hglrc2);
356     }
357 }
358
359 static void test_makecurrent(HDC winhdc)
360 {
361     BOOL ret;
362     HGLRC hglrc;
363     DWORD error;
364
365     hglrc = wglCreateContext(winhdc);
366     ok( hglrc != 0, "wglCreateContext failed\n" );
367
368     ret = wglMakeCurrent( winhdc, hglrc );
369     ok( ret, "wglMakeCurrent failed\n" );
370
371     ok( wglGetCurrentContext() == hglrc, "wrong context\n" );
372
373     /* set the same context again */
374     ret = wglMakeCurrent( winhdc, hglrc );
375     ok( ret, "wglMakeCurrent failed\n" );
376
377     /* check wglMakeCurrent(x, y) after another call to wglMakeCurrent(x, y) */
378     ret = wglMakeCurrent( winhdc, NULL );
379     ok( ret, "wglMakeCurrent failed\n" );
380
381     ret = wglMakeCurrent( winhdc, NULL );
382     ok( ret, "wglMakeCurrent failed\n" );
383
384     SetLastError( 0xdeadbeef );
385     ret = wglMakeCurrent( NULL, NULL );
386     ok( !ret, "wglMakeCurrent succeeded\n" );
387     error = GetLastError();
388     ok( error == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got error=%x\n", error);
389
390     ret = wglMakeCurrent( winhdc, NULL );
391     ok( ret, "wglMakeCurrent failed\n" );
392
393     ret = wglMakeCurrent( winhdc, hglrc );
394     ok( ret, "wglMakeCurrent failed\n" );
395
396     ret = wglMakeCurrent( NULL, NULL );
397     ok( ret, "wglMakeCurrent failed\n" );
398
399     ok( wglGetCurrentContext() == NULL, "wrong context\n" );
400
401     SetLastError( 0xdeadbeef );
402     ret = wglMakeCurrent( NULL, NULL );
403     ok( !ret, "wglMakeCurrent succeeded\n" );
404     error = GetLastError();
405     ok( error == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got error=%x\n", error);
406
407     ret = wglMakeCurrent( winhdc, hglrc );
408     ok( ret, "wglMakeCurrent failed\n" );
409 }
410
411 static void test_colorbits(HDC hdc)
412 {
413     const int iAttribList[] = { WGL_COLOR_BITS_ARB, WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB,
414                                 WGL_BLUE_BITS_ARB, WGL_ALPHA_BITS_ARB };
415     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
416     const int iAttribs[] = { WGL_ALPHA_BITS_ARB, 1, 0 };
417     unsigned int nFormats;
418     int res;
419     int iPixelFormat = 0;
420
421     if (!pwglChoosePixelFormatARB)
422     {
423         win_skip("wglChoosePixelFormatARB is not available\n");
424         return;
425     }
426
427     /* We need a pixel format with at least one bit of alpha */
428     res = pwglChoosePixelFormatARB(hdc, iAttribs, NULL, 1, &iPixelFormat, &nFormats);
429     if(res == FALSE || nFormats == 0)
430     {
431         skip("No suitable pixel formats found\n");
432         return;
433     }
434
435     res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
436               sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
437     if(res == FALSE)
438     {
439         skip("wglGetPixelFormatAttribivARB failed\n");
440         return;
441     }
442     iAttribRet[1] += iAttribRet[2]+iAttribRet[3]+iAttribRet[4];
443     ok(iAttribRet[0] == iAttribRet[1], "WGL_COLOR_BITS_ARB (%d) does not equal R+G+B+A (%d)!\n",
444                                        iAttribRet[0], iAttribRet[1]);
445 }
446
447 static void test_gdi_dbuf(HDC hdc)
448 {
449     const int iAttribList[] = { WGL_SUPPORT_GDI_ARB, WGL_DOUBLE_BUFFER_ARB };
450     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
451     unsigned int nFormats;
452     int iPixelFormat;
453     int res;
454
455     if (!pwglGetPixelFormatAttribivARB)
456     {
457         win_skip("wglGetPixelFormatAttribivARB is not available\n");
458         return;
459     }
460
461     nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
462     for(iPixelFormat = 1;iPixelFormat <= nFormats;iPixelFormat++)
463     {
464         res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
465                   sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
466                   iAttribRet);
467         ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
468         if(res == FALSE)
469             continue;
470
471         ok(!(iAttribRet[0] && iAttribRet[1]), "GDI support and double buffering on pixel format %d\n", iPixelFormat);
472     }
473 }
474
475 static void test_acceleration(HDC hdc)
476 {
477     const int iAttribList[] = { WGL_ACCELERATION_ARB };
478     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
479     unsigned int nFormats;
480     int iPixelFormat;
481     int res;
482     PIXELFORMATDESCRIPTOR pfd;
483
484     if (!pwglGetPixelFormatAttribivARB)
485     {
486         win_skip("wglGetPixelFormatAttribivARB is not available\n");
487         return;
488     }
489
490     nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
491     for(iPixelFormat = 1; iPixelFormat <= nFormats; iPixelFormat++)
492     {
493         res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
494                   sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
495                   iAttribRet);
496         ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
497         if(res == FALSE)
498             continue;
499
500         memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
501         DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
502
503         switch(iAttribRet[0])
504         {
505             case WGL_NO_ACCELERATION_ARB:
506                 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);
507                 break;
508             case WGL_GENERIC_ACCELERATION_ARB:
509                 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);
510                 break;
511             case WGL_FULL_ACCELERATION_ARB:
512                 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);
513                 break;
514         }
515     }
516 }
517
518 static void test_bitmap_rendering( BOOL use_dib )
519 {
520     PIXELFORMATDESCRIPTOR pfd;
521     int i, ret, bpp, iPixelFormat=0;
522     unsigned int nFormats;
523     HGLRC hglrc, hglrc2;
524     BITMAPINFO biDst;
525     HBITMAP bmpDst, oldDst, bmp2;
526     HDC hdcDst, hdcScreen;
527     UINT *dstBuffer = NULL;
528
529     hdcScreen = CreateCompatibleDC(0);
530     hdcDst = CreateCompatibleDC(0);
531
532     if (use_dib)
533     {
534         bpp = 32;
535         memset(&biDst, 0, sizeof(BITMAPINFO));
536         biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
537         biDst.bmiHeader.biWidth = 4;
538         biDst.bmiHeader.biHeight = -4;
539         biDst.bmiHeader.biPlanes = 1;
540         biDst.bmiHeader.biBitCount = 32;
541         biDst.bmiHeader.biCompression = BI_RGB;
542
543         bmpDst = CreateDIBSection(0, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer, NULL, 0);
544
545         biDst.bmiHeader.biWidth = 12;
546         biDst.bmiHeader.biHeight = -12;
547         biDst.bmiHeader.biBitCount = 16;
548         bmp2 = CreateDIBSection(0, &biDst, DIB_RGB_COLORS, NULL, NULL, 0);
549     }
550     else
551     {
552         bpp = GetDeviceCaps( hdcScreen, BITSPIXEL );
553         bmpDst = CreateBitmap( 4, 4, 1, bpp, NULL );
554         bmp2 = CreateBitmap( 12, 12, 1, bpp, NULL );
555     }
556
557     oldDst = SelectObject(hdcDst, bmpDst);
558
559     trace( "testing on %s\n", use_dib ? "DIB" : "DDB" );
560
561     /* Pick a pixel format by hand because ChoosePixelFormat is unreliable */
562     nFormats = DescribePixelFormat(hdcDst, 0, 0, NULL);
563     for(i=1; i<=nFormats; i++)
564     {
565         memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
566         DescribePixelFormat(hdcDst, i, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
567
568         if((pfd.dwFlags & PFD_DRAW_TO_BITMAP) &&
569            (pfd.dwFlags & PFD_SUPPORT_OPENGL) &&
570            (pfd.cColorBits == bpp) &&
571            (pfd.cAlphaBits == 8) )
572         {
573             iPixelFormat = i;
574             break;
575         }
576     }
577
578     if(!iPixelFormat)
579     {
580         skip("Unable to find a suitable pixel format\n");
581     }
582     else
583     {
584         ret = SetPixelFormat(hdcDst, iPixelFormat, &pfd);
585         ok( ret, "SetPixelFormat failed\n" );
586         ret = GetPixelFormat( hdcDst );
587         ok( ret == iPixelFormat, "GetPixelFormat returned %d/%d\n", ret, iPixelFormat );
588         ret = SetPixelFormat(hdcDst, iPixelFormat + 1, &pfd);
589         ok( !ret, "SetPixelFormat succeeded\n" );
590         hglrc = wglCreateContext(hdcDst);
591         ok(hglrc != NULL, "Unable to create a context\n");
592
593         if(hglrc)
594         {
595             GLint viewport[4];
596             wglMakeCurrent(hdcDst, hglrc);
597             hglrc2 = wglCreateContext(hdcDst);
598             ok(hglrc2 != NULL, "Unable to create a context\n");
599
600             /* Note this is RGBA but we read ARGB back */
601             glClearColor((float)0x22/0xff, (float)0x33/0xff, (float)0x44/0xff, (float)0x11/0xff);
602             glClear(GL_COLOR_BUFFER_BIT);
603             glGetIntegerv( GL_VIEWPORT, viewport );
604             glFinish();
605
606             ok( viewport[0] == 0 && viewport[1] == 0 && viewport[2] == 4 && viewport[3] == 4,
607                 "wrong viewport %d,%d,%d,%d\n", viewport[0], viewport[1], viewport[2], viewport[3] );
608             /* Note apparently the alpha channel is not supported by the software renderer (bitmap only works using software) */
609             if (dstBuffer)
610                 for (i = 0; i < 16; i++)
611                     ok(dstBuffer[i] == 0x223344 || dstBuffer[i] == 0x11223344, "Received color=%x at %u\n",
612                        dstBuffer[i], i);
613
614             SelectObject(hdcDst, bmp2);
615             ret = GetPixelFormat( hdcDst );
616             ok( ret == iPixelFormat, "GetPixelFormat returned %d/%d\n", ret, iPixelFormat );
617             ret = SetPixelFormat(hdcDst, iPixelFormat + 1, &pfd);
618             ok( !ret, "SetPixelFormat succeeded\n" );
619
620             /* context still uses the old pixel format and viewport */
621             glClearColor((float)0x44/0xff, (float)0x33/0xff, (float)0x22/0xff, (float)0x11/0xff);
622             glClear(GL_COLOR_BUFFER_BIT);
623             glFinish();
624             glGetIntegerv( GL_VIEWPORT, viewport );
625             ok( viewport[0] == 0 && viewport[1] == 0 && viewport[2] == 4 && viewport[3] == 4,
626                 "wrong viewport %d,%d,%d,%d\n", viewport[0], viewport[1], viewport[2], viewport[3] );
627
628             wglMakeCurrent(NULL, NULL);
629             wglMakeCurrent(hdcDst, hglrc);
630             glClearColor((float)0x44/0xff, (float)0x55/0xff, (float)0x66/0xff, (float)0x11/0xff);
631             glClear(GL_COLOR_BUFFER_BIT);
632             glFinish();
633             glGetIntegerv( GL_VIEWPORT, viewport );
634             ok( viewport[0] == 0 && viewport[1] == 0 && viewport[2] == 4 && viewport[3] == 4,
635                 "wrong viewport %d,%d,%d,%d\n", viewport[0], viewport[1], viewport[2], viewport[3] );
636
637             wglMakeCurrent(hdcDst, hglrc2);
638             glGetIntegerv( GL_VIEWPORT, viewport );
639             ok( viewport[0] == 0 && viewport[1] == 0 && viewport[2] == 12 && viewport[3] == 12,
640                 "wrong viewport %d,%d,%d,%d\n", viewport[0], viewport[1], viewport[2], viewport[3] );
641
642             wglMakeCurrent(hdcDst, hglrc);
643             glGetIntegerv( GL_VIEWPORT, viewport );
644             ok( viewport[0] == 0 && viewport[1] == 0 && viewport[2] == 4 && viewport[3] == 4,
645                 "wrong viewport %d,%d,%d,%d\n", viewport[0], viewport[1], viewport[2], viewport[3] );
646
647             SelectObject(hdcDst, bmpDst);
648             ret = GetPixelFormat( hdcDst );
649             ok( ret == iPixelFormat, "GetPixelFormat returned %d/%d\n", ret, iPixelFormat );
650             ret = SetPixelFormat(hdcDst, iPixelFormat + 1, &pfd);
651             ok( !ret, "SetPixelFormat succeeded\n" );
652             wglMakeCurrent(hdcDst, hglrc2);
653             glGetIntegerv( GL_VIEWPORT, viewport );
654             ok( viewport[0] == 0 && viewport[1] == 0 && viewport[2] == 12 && viewport[3] == 12,
655                 "wrong viewport %d,%d,%d,%d\n", viewport[0], viewport[1], viewport[2], viewport[3] );
656         }
657     }
658
659     SelectObject(hdcDst, oldDst);
660     DeleteObject(bmp2);
661     DeleteObject(bmpDst);
662     DeleteDC(hdcDst);
663     DeleteDC(hdcScreen);
664 }
665
666 struct wgl_thread_param
667 {
668     HANDLE test_finished;
669     HGLRC hglrc;
670     BOOL hglrc_deleted;
671     DWORD last_error;
672 };
673
674 static DWORD WINAPI wgl_thread(void *param)
675 {
676     struct wgl_thread_param *p = param;
677
678     SetLastError(0xdeadbeef);
679     p->hglrc_deleted = wglDeleteContext(p->hglrc);
680     p->last_error = GetLastError();
681     SetEvent(p->test_finished);
682
683     return 0;
684 }
685
686 static void test_deletecontext(HDC hdc)
687 {
688     struct wgl_thread_param thread_params;
689     HGLRC hglrc = wglCreateContext(hdc);
690     HANDLE thread_handle;
691     DWORD res, tid;
692
693     SetLastError(0xdeadbeef);
694     res = wglDeleteContext(NULL);
695     ok(res == FALSE, "wglDeleteContext succeeded\n");
696     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected last error to be ERROR_INVALID_HANDLE, got %u\n", GetLastError());
697
698     if(!hglrc)
699     {
700         skip("wglCreateContext failed!\n");
701         return;
702     }
703
704     res = wglMakeCurrent(hdc, hglrc);
705     if(!res)
706     {
707         skip("wglMakeCurrent failed!\n");
708         return;
709     }
710
711     /* WGL doesn't allow you to delete a context from a different thread than the one in which it is current.
712      * This differs from GLX which does allow it but it delays actual deletion until the context becomes not current.
713      */
714     thread_params.hglrc = hglrc;
715     thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
716     thread_handle = CreateThread(NULL, 0, wgl_thread, &thread_params, 0, &tid);
717     ok(!!thread_handle, "Failed to create thread, last error %#x.\n", GetLastError());
718     if(thread_handle)
719     {
720         WaitForSingleObject(thread_handle, INFINITE);
721         ok(thread_params.hglrc_deleted == FALSE, "Attempt to delete WGL context from another thread passed but should fail!\n");
722         ok(thread_params.last_error == ERROR_BUSY, "Expected last error to be ERROR_BUSY, got %u\n", thread_params.last_error);
723     }
724     CloseHandle(thread_params.test_finished);
725
726     res = wglDeleteContext(hglrc);
727     ok(res == TRUE, "wglDeleteContext failed\n");
728
729     /* Attempting to delete the same context twice should fail. */
730     SetLastError(0xdeadbeef);
731     res = wglDeleteContext(hglrc);
732     ok(res == FALSE, "wglDeleteContext succeeded\n");
733     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected last error to be ERROR_INVALID_HANDLE, got %u\n", GetLastError());
734
735     /* WGL makes a context not current when deleting it. This differs from GLX behavior where
736      * deletion takes place when the thread becomes not current. */
737     hglrc = wglGetCurrentContext();
738     ok(hglrc == NULL, "A WGL context is active while none was expected\n");
739 }
740
741 static void test_make_current_read(HDC hdc)
742 {
743     int res;
744     HDC hread;
745     HGLRC hglrc = wglCreateContext(hdc);
746
747     if(!hglrc)
748     {
749         skip("wglCreateContext failed!\n");
750         return;
751     }
752
753     res = wglMakeCurrent(hdc, hglrc);
754     if(!res)
755     {
756         skip("wglMakeCurrent failed!\n");
757         return;
758     }
759
760     /* Test what wglGetCurrentReadDCARB does for wglMakeCurrent as the spec doesn't mention it */
761     hread = pwglGetCurrentReadDCARB();
762     trace("hread %p, hdc %p\n", hread, hdc);
763     ok(hread == hdc, "wglGetCurrentReadDCARB failed for standard wglMakeCurrent\n");
764
765     pwglMakeContextCurrentARB(hdc, hdc, hglrc);
766     hread = pwglGetCurrentReadDCARB();
767     ok(hread == hdc, "wglGetCurrentReadDCARB failed for wglMakeContextCurrent\n");
768 }
769
770 static void test_dc(HWND hwnd, HDC hdc)
771 {
772     int pf1, pf2;
773     HDC hdc2;
774
775     /* Get another DC and make sure it has the same pixel format */
776     hdc2 = GetDC(hwnd);
777     if(hdc != hdc2)
778     {
779         pf1 = GetPixelFormat(hdc);
780         pf2 = GetPixelFormat(hdc2);
781         ok(pf1 == pf2, "Second DC does not have the same format (%d != %d)\n", pf1, pf2);
782     }
783     else
784         skip("Could not get a different DC for the window\n");
785
786     if(hdc2)
787     {
788         ReleaseDC(hwnd, hdc2);
789         hdc2 = NULL;
790     }
791 }
792
793 /* Nvidia converts win32 error codes to (0xc007 << 16) | win32_error_code */
794 #define NVIDIA_HRESULT_FROM_WIN32(x) (HRESULT_FROM_WIN32(x) | 0x40000000)
795 static void test_opengl3(HDC hdc)
796 {
797     /* Try to create a context compatible with OpenGL 1.x; 1.0-2.1 is allowed */
798     {
799         HGLRC gl3Ctx;
800         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 1, 0};
801
802         gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
803         ok(gl3Ctx != 0, "pwglCreateContextAttribsARB for a 1.x context failed!\n");
804         wglDeleteContext(gl3Ctx);
805     }
806
807     /* Try to pass an invalid HDC */
808     {
809         HGLRC gl3Ctx;
810         DWORD error;
811         gl3Ctx = pwglCreateContextAttribsARB((HDC)0xdeadbeef, 0, 0);
812         ok(gl3Ctx == 0, "pwglCreateContextAttribsARB using an invalid HDC passed\n");
813         error = GetLastError();
814         todo_wine ok(error == ERROR_DC_NOT_FOUND ||
815                      broken(error == NVIDIA_HRESULT_FROM_WIN32(ERROR_INVALID_DATA)), /* Nvidia Vista + Win7 */
816                      "Expected ERROR_DC_NOT_FOUND, got error=%x\n", error);
817         wglDeleteContext(gl3Ctx);
818     }
819
820     /* Try to pass an invalid shareList */
821     {
822         HGLRC gl3Ctx;
823         DWORD error;
824         gl3Ctx = pwglCreateContextAttribsARB(hdc, (HGLRC)0xdeadbeef, 0);
825         todo_wine ok(gl3Ctx == 0, "pwglCreateContextAttribsARB using an invalid shareList passed\n");
826         error = GetLastError();
827         /* The Nvidia implementation seems to return hresults instead of win32 error codes */
828         todo_wine ok(error == ERROR_INVALID_OPERATION ||
829                      error == NVIDIA_HRESULT_FROM_WIN32(ERROR_INVALID_OPERATION), "Expected ERROR_INVALID_OPERATION, got error=%x\n", error);
830         wglDeleteContext(gl3Ctx);
831     }
832
833     /* Try to create an OpenGL 3.0 context */
834     {
835         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
836         HGLRC gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
837
838         if(gl3Ctx == NULL)
839         {
840             skip("Skipping the rest of the WGL_ARB_create_context test due to lack of OpenGL 3.0\n");
841             return;
842         }
843
844         wglDeleteContext(gl3Ctx);
845     }
846
847     /* 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 */
848     {
849         HGLRC glCtx = wglCreateContext(hdc);
850
851         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
852         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};
853
854         HGLRC gl3Ctx = pwglCreateContextAttribsARB(hdc, glCtx, attribs);
855         ok(gl3Ctx != NULL, "Sharing of a display list between OpenGL 3.0 and OpenGL 1.x/2.x failed!\n");
856         if(gl3Ctx)
857             wglDeleteContext(gl3Ctx);
858
859         gl3Ctx = pwglCreateContextAttribsARB(hdc, glCtx, attribs_future);
860         ok(gl3Ctx != NULL, "Sharing of a display list between a forward compatible OpenGL 3.0 context and OpenGL 1.x/2.x failed!\n");
861         if(gl3Ctx)
862             wglDeleteContext(gl3Ctx);
863
864         if(glCtx)
865             wglDeleteContext(glCtx);
866     }
867
868     /* Try to create an OpenGL 3.0 context and test windowless rendering */
869     {
870         HGLRC gl3Ctx;
871         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
872         BOOL res;
873
874         gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
875         ok(gl3Ctx != 0, "pwglCreateContextAttribsARB for a 3.0 context failed!\n");
876
877         /* OpenGL 3.0 allows offscreen rendering WITHOUT a drawable
878          * Neither AMD or Nvidia support it at this point. The WGL_ARB_create_context specs also say that
879          * it is hard because drivers use the HDC to enter the display driver and it sounds like they don't
880          * expect drivers to ever offer it.
881          */
882         res = wglMakeCurrent(0, gl3Ctx);
883         ok(res == FALSE, "Wow, OpenGL 3.0 windowless rendering passed while it was expected not to!\n");
884         if(res)
885             wglMakeCurrent(0, 0);
886
887         if(gl3Ctx)
888             wglDeleteContext(gl3Ctx);
889     }
890 }
891
892 static void test_minimized(void)
893 {
894     PIXELFORMATDESCRIPTOR pf_desc =
895     {
896         sizeof(PIXELFORMATDESCRIPTOR),
897         1,                     /* version */
898         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
899         PFD_TYPE_RGBA,
900         24,                    /* 24-bit color depth */
901         0, 0, 0, 0, 0, 0,      /* color bits */
902         0,                     /* alpha buffer */
903         0,                     /* shift bit */
904         0,                     /* accumulation buffer */
905         0, 0, 0, 0,            /* accum bits */
906         32,                    /* z-buffer */
907         0,                     /* stencil buffer */
908         0,                     /* auxiliary buffer */
909         PFD_MAIN_PLANE,        /* main layer */
910         0,                     /* reserved */
911         0, 0, 0                /* layer masks */
912     };
913     int pixel_format;
914     HWND window;
915     LONG style;
916     HGLRC ctx;
917     BOOL ret;
918     HDC dc;
919
920     window = CreateWindowA("static", "opengl32_test",
921             WS_POPUP | WS_MINIMIZE, 0, 0, 640, 480, 0, 0, 0, 0);
922     ok(!!window, "Failed to create window, last error %#x.\n", GetLastError());
923
924     dc = GetDC(window);
925     ok(!!dc, "Failed to get DC.\n");
926
927     pixel_format = ChoosePixelFormat(dc, &pf_desc);
928     if (!pixel_format)
929     {
930         win_skip("Failed to find pixel format.\n");
931         ReleaseDC(window, dc);
932         DestroyWindow(window);
933         return;
934     }
935
936     ret = SetPixelFormat(dc, pixel_format, &pf_desc);
937     ok(ret, "Failed to set pixel format, last error %#x.\n", GetLastError());
938
939     style = GetWindowLongA(window, GWL_STYLE);
940     ok(style & WS_MINIMIZE, "Window should be minimized, got style %#x.\n", style);
941
942     ctx = wglCreateContext(dc);
943     ok(!!ctx, "Failed to create GL context, last error %#x.\n", GetLastError());
944
945     ret = wglMakeCurrent(dc, ctx);
946     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
947
948     style = GetWindowLongA(window, GWL_STYLE);
949     ok(style & WS_MINIMIZE, "window should be minimized, got style %#x.\n", style);
950
951     ret = wglMakeCurrent(NULL, NULL);
952     ok(ret, "Failed to clear current context, last error %#x.\n", GetLastError());
953
954     ret = wglDeleteContext(ctx);
955     ok(ret, "Failed to delete GL context, last error %#x.\n", GetLastError());
956
957     ReleaseDC(window, dc);
958     DestroyWindow(window);
959 }
960
961 static void test_window_dc(void)
962 {
963     PIXELFORMATDESCRIPTOR pf_desc =
964     {
965         sizeof(PIXELFORMATDESCRIPTOR),
966         1,                     /* version */
967         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
968         PFD_TYPE_RGBA,
969         24,                    /* 24-bit color depth */
970         0, 0, 0, 0, 0, 0,      /* color bits */
971         0,                     /* alpha buffer */
972         0,                     /* shift bit */
973         0,                     /* accumulation buffer */
974         0, 0, 0, 0,            /* accum bits */
975         32,                    /* z-buffer */
976         0,                     /* stencil buffer */
977         0,                     /* auxiliary buffer */
978         PFD_MAIN_PLANE,        /* main layer */
979         0,                     /* reserved */
980         0, 0, 0                /* layer masks */
981     };
982     int pixel_format;
983     HWND window;
984     RECT vp, r;
985     HGLRC ctx;
986     BOOL ret;
987     HDC dc;
988
989     window = CreateWindowA("static", "opengl32_test",
990             WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, 0, 0, 0, 0);
991     ok(!!window, "Failed to create window, last error %#x.\n", GetLastError());
992
993     ShowWindow(window, SW_SHOW);
994
995     dc = GetWindowDC(window);
996     ok(!!dc, "Failed to get DC.\n");
997
998     pixel_format = ChoosePixelFormat(dc, &pf_desc);
999     if (!pixel_format)
1000     {
1001         win_skip("Failed to find pixel format.\n");
1002         ReleaseDC(window, dc);
1003         DestroyWindow(window);
1004         return;
1005     }
1006
1007     ret = SetPixelFormat(dc, pixel_format, &pf_desc);
1008     ok(ret, "Failed to set pixel format, last error %#x.\n", GetLastError());
1009
1010     ctx = wglCreateContext(dc);
1011     ok(!!ctx, "Failed to create GL context, last error %#x.\n", GetLastError());
1012
1013     ret = wglMakeCurrent(dc, ctx);
1014     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
1015
1016     GetClientRect(window, &r);
1017     glGetIntegerv(GL_VIEWPORT, (GLint *)&vp);
1018     ok(EqualRect(&r, &vp), "Viewport not equal to client rect.\n");
1019
1020     ret = wglMakeCurrent(NULL, NULL);
1021     ok(ret, "Failed to clear current context, last error %#x.\n", GetLastError());
1022
1023     ret = wglDeleteContext(ctx);
1024     ok(ret, "Failed to delete GL context, last error %#x.\n", GetLastError());
1025
1026     ReleaseDC(window, dc);
1027     DestroyWindow(window);
1028 }
1029
1030 static void test_destroy(HDC oldhdc)
1031 {
1032     PIXELFORMATDESCRIPTOR pf_desc =
1033     {
1034         sizeof(PIXELFORMATDESCRIPTOR),
1035         1,                     /* version */
1036         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
1037         PFD_TYPE_RGBA,
1038         24,                    /* 24-bit color depth */
1039         0, 0, 0, 0, 0, 0,      /* color bits */
1040         0,                     /* alpha buffer */
1041         0,                     /* shift bit */
1042         0,                     /* accumulation buffer */
1043         0, 0, 0, 0,            /* accum bits */
1044         32,                    /* z-buffer */
1045         0,                     /* stencil buffer */
1046         0,                     /* auxiliary buffer */
1047         PFD_MAIN_PLANE,        /* main layer */
1048         0,                     /* reserved */
1049         0, 0, 0                /* layer masks */
1050     };
1051     int pixel_format;
1052     HWND window;
1053     HGLRC ctx;
1054     BOOL ret;
1055     HDC dc;
1056     GLenum glerr;
1057     DWORD err;
1058     HGLRC oldctx = wglGetCurrentContext();
1059
1060     ok(!!oldctx, "Expected to find a valid current context.\n");
1061
1062     window = CreateWindowA("static", "opengl32_test",
1063             WS_POPUP, 0, 0, 640, 480, 0, 0, 0, 0);
1064     ok(!!window, "Failed to create window, last error %#x.\n", GetLastError());
1065
1066     dc = GetDC(window);
1067     ok(!!dc, "Failed to get DC.\n");
1068
1069     pixel_format = ChoosePixelFormat(dc, &pf_desc);
1070     if (!pixel_format)
1071     {
1072         win_skip("Failed to find pixel format.\n");
1073         ReleaseDC(window, dc);
1074         DestroyWindow(window);
1075         return;
1076     }
1077
1078     ret = SetPixelFormat(dc, pixel_format, &pf_desc);
1079     ok(ret, "Failed to set pixel format, last error %#x.\n", GetLastError());
1080
1081     ctx = wglCreateContext(dc);
1082     ok(!!ctx, "Failed to create GL context, last error %#x.\n", GetLastError());
1083
1084     ret = wglMakeCurrent(dc, ctx);
1085     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
1086
1087     glClear(GL_COLOR_BUFFER_BIT);
1088     glFinish();
1089     glerr = glGetError();
1090     ok(glerr == GL_NO_ERROR, "Failed glClear, error %#x.\n", glerr);
1091     ret = SwapBuffers(dc);
1092     ok(ret, "Failed SwapBuffers, error %#x.\n", GetLastError());
1093
1094     ret = DestroyWindow(window);
1095     ok(ret, "Failed to destroy window, last error %#x.\n", GetLastError());
1096
1097     ok(wglGetCurrentContext() == ctx, "Wrong current context.\n");
1098
1099     SetLastError(0xdeadbeef);
1100     ret = wglMakeCurrent(dc, ctx);
1101     err = GetLastError();
1102     ok(!ret && err == ERROR_INVALID_HANDLE,
1103             "Unexpected behavior when making context current, ret %d, last error %#x.\n", ret, err);
1104     SetLastError(0xdeadbeef);
1105     ret = SwapBuffers(dc);
1106     err = GetLastError();
1107     ok(!ret && err == ERROR_INVALID_HANDLE, "Unexpected behavior with SwapBuffer, last error %#x.\n", err);
1108
1109     ok(wglGetCurrentContext() == ctx, "Wrong current context.\n");
1110
1111     glClear(GL_COLOR_BUFFER_BIT);
1112     glFinish();
1113     glerr = glGetError();
1114     ok(glerr == GL_NO_ERROR, "Failed glClear, error %#x.\n", glerr);
1115     SetLastError(0xdeadbeef);
1116     ret = SwapBuffers(dc);
1117     err = GetLastError();
1118     ok(!ret && err == ERROR_INVALID_HANDLE, "Unexpected behavior with SwapBuffer, last error %#x.\n", err);
1119
1120     ret = wglMakeCurrent(NULL, NULL);
1121     ok(ret, "Failed to clear current context, last error %#x.\n", GetLastError());
1122
1123     glClear(GL_COLOR_BUFFER_BIT);
1124     glFinish();
1125     glerr = glGetError();
1126     todo_wine ok(glerr == GL_INVALID_OPERATION, "Failed glClear, error %#x.\n", glerr);
1127     SetLastError(0xdeadbeef);
1128     ret = SwapBuffers(dc);
1129     err = GetLastError();
1130     ok(!ret && err == ERROR_INVALID_HANDLE, "Unexpected behavior with SwapBuffer, last error %#x.\n", err);
1131
1132     SetLastError(0xdeadbeef);
1133     ret = wglMakeCurrent(dc, ctx);
1134     err = GetLastError();
1135     ok(!ret && err == ERROR_INVALID_HANDLE,
1136             "Unexpected behavior when making context current, ret %d, last error %#x.\n", ret, err);
1137
1138     ok(wglGetCurrentContext() == NULL, "Wrong current context.\n");
1139
1140     ret = wglMakeCurrent(oldhdc, oldctx);
1141     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
1142     ok(wglGetCurrentContext() == oldctx, "Wrong current context.\n");
1143
1144     SetLastError(0xdeadbeef);
1145     ret = wglMakeCurrent(dc, ctx);
1146     err = GetLastError();
1147     ok(!ret && err == ERROR_INVALID_HANDLE,
1148             "Unexpected behavior when making context current, ret %d, last error %#x.\n", ret, err);
1149
1150     ok(wglGetCurrentContext() == oldctx, "Wrong current context.\n");
1151
1152     ret = wglDeleteContext(ctx);
1153     ok(ret, "Failed to delete GL context, last error %#x.\n", GetLastError());
1154
1155     ReleaseDC(window, dc);
1156
1157     ret = wglMakeCurrent(oldhdc, oldctx);
1158     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
1159 }
1160
1161 static void test_destroy_read(HDC oldhdc)
1162 {
1163     PIXELFORMATDESCRIPTOR pf_desc =
1164     {
1165         sizeof(PIXELFORMATDESCRIPTOR),
1166         1,                     /* version */
1167         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
1168         PFD_TYPE_RGBA,
1169         24,                    /* 24-bit color depth */
1170         0, 0, 0, 0, 0, 0,      /* color bits */
1171         0,                     /* alpha buffer */
1172         0,                     /* shift bit */
1173         0,                     /* accumulation buffer */
1174         0, 0, 0, 0,            /* accum bits */
1175         32,                    /* z-buffer */
1176         0,                     /* stencil buffer */
1177         0,                     /* auxiliary buffer */
1178         PFD_MAIN_PLANE,        /* main layer */
1179         0,                     /* reserved */
1180         0, 0, 0                /* layer masks */
1181     };
1182     int pixel_format;
1183     HWND draw_window, read_window;
1184     HGLRC ctx;
1185     BOOL ret;
1186     HDC read_dc, draw_dc;
1187     GLenum glerr;
1188     DWORD err;
1189     HGLRC oldctx = wglGetCurrentContext();
1190
1191     ok(!!oldctx, "Expected to find a valid current context\n");
1192
1193     draw_window = CreateWindowA("static", "opengl32_test",
1194             WS_POPUP, 0, 0, 640, 480, 0, 0, 0, 0);
1195     ok(!!draw_window, "Failed to create window, last error %#x.\n", GetLastError());
1196
1197     draw_dc = GetDC(draw_window);
1198     ok(!!draw_dc, "Failed to get DC.\n");
1199
1200     pixel_format = ChoosePixelFormat(draw_dc, &pf_desc);
1201     if (!pixel_format)
1202     {
1203         win_skip("Failed to find pixel format.\n");
1204         ReleaseDC(draw_window, draw_dc);
1205         DestroyWindow(draw_window);
1206         return;
1207     }
1208
1209     ret = SetPixelFormat(draw_dc, pixel_format, &pf_desc);
1210     ok(ret, "Failed to set pixel format, last error %#x.\n", GetLastError());
1211
1212     read_window = CreateWindowA("static", "opengl32_test",
1213             WS_POPUP, 0, 0, 640, 480, 0, 0, 0, 0);
1214     ok(!!read_window, "Failed to create window, last error %#x.\n", GetLastError());
1215
1216     read_dc = GetDC(read_window);
1217     ok(!!draw_dc, "Failed to get DC.\n");
1218
1219     pixel_format = ChoosePixelFormat(read_dc, &pf_desc);
1220     if (!pixel_format)
1221     {
1222         win_skip("Failed to find pixel format.\n");
1223         ReleaseDC(read_window, read_dc);
1224         DestroyWindow(read_window);
1225         ReleaseDC(draw_window, draw_dc);
1226         DestroyWindow(draw_window);
1227         return;
1228     }
1229
1230     ret = SetPixelFormat(read_dc, pixel_format, &pf_desc);
1231     ok(ret, "Failed to set pixel format, last error %#x.\n", GetLastError());
1232
1233     ctx = wglCreateContext(draw_dc);
1234     ok(!!ctx, "Failed to create GL context, last error %#x.\n", GetLastError());
1235
1236     ret = pwglMakeContextCurrentARB(draw_dc, read_dc, ctx);
1237     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
1238
1239     glCopyPixels(0, 0, 640, 480, GL_COLOR);
1240     glFinish();
1241     glerr = glGetError();
1242     ok(glerr == GL_NO_ERROR, "Failed glCopyPixel, error %#x.\n", glerr);
1243     ret = SwapBuffers(draw_dc);
1244     ok(ret, "Failed SwapBuffers, error %#x.\n", GetLastError());
1245
1246     ret = DestroyWindow(read_window);
1247     ok(ret, "Failed to destroy window, last error %#x.\n", GetLastError());
1248
1249     ok(wglGetCurrentContext() == ctx, "Wrong current context.\n");
1250
1251     if (0) /* Crashes on AMD on Windows */
1252     {
1253         glCopyPixels(0, 0, 640, 480, GL_COLOR);
1254         glFinish();
1255         glerr = glGetError();
1256         ok(glerr == GL_NO_ERROR, "Failed glCopyPixel, error %#x.\n", glerr);
1257     }
1258
1259     glClear(GL_COLOR_BUFFER_BIT);
1260     glFinish();
1261     glerr = glGetError();
1262     ok(glerr == GL_NO_ERROR, "Failed glClear, error %#x.\n", glerr);
1263     ret = SwapBuffers(draw_dc);
1264     ok(ret, "Failed SwapBuffers, error %#x.\n", GetLastError());
1265
1266     ret = wglMakeCurrent(NULL, NULL);
1267     ok(ret, "Failed to clear current context, last error %#x.\n", GetLastError());
1268
1269     if (0) /* This crashes with Nvidia drivers on Windows. */
1270     {
1271         SetLastError(0xdeadbeef);
1272         ret = pwglMakeContextCurrentARB(draw_dc, read_dc, ctx);
1273         err = GetLastError();
1274         ok(!ret && err == ERROR_INVALID_HANDLE,
1275                 "Unexpected behavior when making context current, ret %d, last error %#x.\n", ret, err);
1276     }
1277
1278     ret = DestroyWindow(draw_window);
1279     ok(ret, "Failed to destroy window, last error %#x.\n", GetLastError());
1280
1281     glClear(GL_COLOR_BUFFER_BIT);
1282     glFinish();
1283     glerr = glGetError();
1284     todo_wine ok(glerr == GL_INVALID_OPERATION, "Failed glClear, error %#x.\n", glerr);
1285     SetLastError(0xdeadbeef);
1286     ret = SwapBuffers(draw_dc);
1287     err = GetLastError();
1288     ok(!ret && err == ERROR_INVALID_HANDLE, "Unexpected behavior with SwapBuffer, last error %#x.\n", err);
1289
1290     SetLastError(0xdeadbeef);
1291     ret = pwglMakeContextCurrentARB(draw_dc, read_dc, ctx);
1292     err = GetLastError();
1293     ok(!ret && (err == ERROR_INVALID_HANDLE || err == 0xc0070006),
1294             "Unexpected behavior when making context current, ret %d, last error %#x.\n", ret, err);
1295
1296     ok(wglGetCurrentContext() == NULL, "Wrong current context.\n");
1297
1298     wglMakeCurrent(NULL, NULL);
1299
1300     wglMakeCurrent(oldhdc, oldctx);
1301     ok(wglGetCurrentContext() == oldctx, "Wrong current context.\n");
1302
1303     SetLastError(0xdeadbeef);
1304     ret = pwglMakeContextCurrentARB(draw_dc, read_dc, ctx);
1305     err = GetLastError();
1306     ok(!ret && (err == ERROR_INVALID_HANDLE || err == 0xc0070006),
1307             "Unexpected behavior when making context current, last error %#x.\n", err);
1308
1309     ok(wglGetCurrentContext() == oldctx, "Wrong current context.\n");
1310
1311     ret = wglDeleteContext(ctx);
1312     ok(ret, "Failed to delete GL context, last error %#x.\n", GetLastError());
1313
1314     ReleaseDC(read_window, read_dc);
1315     ReleaseDC(draw_window, draw_dc);
1316
1317     wglMakeCurrent(oldhdc, oldctx);
1318 }
1319
1320 START_TEST(opengl)
1321 {
1322     HWND hwnd;
1323     PIXELFORMATDESCRIPTOR pfd = {
1324         sizeof(PIXELFORMATDESCRIPTOR),
1325         1,                     /* version */
1326         PFD_DRAW_TO_WINDOW |
1327         PFD_SUPPORT_OPENGL |
1328         PFD_DOUBLEBUFFER,
1329         PFD_TYPE_RGBA,
1330         24,                    /* 24-bit color depth */
1331         0, 0, 0, 0, 0, 0,      /* color bits */
1332         0,                     /* alpha buffer */
1333         0,                     /* shift bit */
1334         0,                     /* accumulation buffer */
1335         0, 0, 0, 0,            /* accum bits */
1336         32,                    /* z-buffer */
1337         0,                     /* stencil buffer */
1338         0,                     /* auxiliary buffer */
1339         PFD_MAIN_PLANE,        /* main layer */
1340         0,                     /* reserved */
1341         0, 0, 0                /* layer masks */
1342     };
1343
1344     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
1345                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
1346     ok(hwnd != NULL, "err: %d\n", GetLastError());
1347     if (hwnd)
1348     {
1349         HDC hdc;
1350         int iPixelFormat, res;
1351         HGLRC hglrc;
1352         DWORD error;
1353         ShowWindow(hwnd, SW_SHOW);
1354
1355         hdc = GetDC(hwnd);
1356
1357         iPixelFormat = ChoosePixelFormat(hdc, &pfd);
1358         if(iPixelFormat == 0)
1359         {
1360             /* This should never happen as ChoosePixelFormat always returns a closest match, but currently this fails in Wine if we don't have glX */
1361             win_skip("Unable to find pixel format.\n");
1362             goto cleanup;
1363         }
1364
1365         /* We shouldn't be able to create a context from a hdc which doesn't have a pixel format set */
1366         hglrc = wglCreateContext(hdc);
1367         ok(hglrc == NULL, "wglCreateContext should fail when no pixel format has been set, but it passed\n");
1368         error = GetLastError();
1369         ok(error == ERROR_INVALID_PIXEL_FORMAT, "expected ERROR_INVALID_PIXEL_FORMAT for wglCreateContext without a pixelformat set, but received %#x\n", error);
1370
1371         res = SetPixelFormat(hdc, iPixelFormat, &pfd);
1372         ok(res, "SetPixelformat failed: %x\n", GetLastError());
1373
1374         test_bitmap_rendering( TRUE );
1375         test_bitmap_rendering( FALSE );
1376         test_minimized();
1377         test_window_dc();
1378         test_dc(hwnd, hdc);
1379
1380         hglrc = wglCreateContext(hdc);
1381         res = wglMakeCurrent(hdc, hglrc);
1382         ok(res, "wglMakeCurrent failed!\n");
1383         if(res)
1384         {
1385             trace("OpenGL renderer: %s\n", glGetString(GL_RENDERER));
1386             trace("OpenGL driver version: %s\n", glGetString(GL_VERSION));
1387             trace("OpenGL vendor: %s\n", glGetString(GL_VENDOR));
1388         }
1389         else
1390         {
1391             skip("Skipping OpenGL tests without a current context\n");
1392             return;
1393         }
1394
1395         /* Initialisation of WGL functions depends on an implicit WGL context. For this reason we can't load them before making
1396          * any WGL call :( On Wine this would work but not on real Windows because there can be different implementations (software, ICD, MCD).
1397          */
1398         init_functions();
1399         /* The lack of wglGetExtensionsStringARB in general means broken software rendering or the lack of decent OpenGL support, skip tests in such cases */
1400         if (!pwglGetExtensionsStringARB)
1401         {
1402             win_skip("wglGetExtensionsStringARB is not available\n");
1403             return;
1404         }
1405
1406         test_deletecontext(hdc);
1407         test_makecurrent(hdc);
1408         test_setpixelformat(hdc);
1409         test_destroy(hdc);
1410         test_sharelists(hdc);
1411         test_colorbits(hdc);
1412         test_gdi_dbuf(hdc);
1413         test_acceleration(hdc);
1414
1415         wgl_extensions = pwglGetExtensionsStringARB(hdc);
1416         if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
1417
1418         if(strstr(wgl_extensions, "WGL_ARB_create_context"))
1419             test_opengl3(hdc);
1420
1421         if(strstr(wgl_extensions, "WGL_ARB_make_current_read"))
1422         {
1423             test_make_current_read(hdc);
1424             test_destroy_read(hdc);
1425         }
1426         else
1427             skip("WGL_ARB_make_current_read not supported, skipping test\n");
1428
1429         if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
1430             test_pbuffers(hdc);
1431         else
1432             skip("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");
1433
1434 cleanup:
1435         ReleaseDC(hwnd, hdc);
1436         DestroyWindow(hwnd);
1437     }
1438 }