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