mshtml/htmltextcont: Initialize value (Coverity).
[wine] / dlls / opengl32 / tests / opengl.c
1 /*
2  * Some tests for OpenGL functions
3  *
4  * Copyright (C) 2007 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 #define MAX_FORMATS 256
26 typedef void* HPBUFFERARB;
27
28 /* WGL_ARB_extensions_string */
29 static const char* (WINAPI *pwglGetExtensionsStringARB)(HDC);
30 static int (WINAPI *pwglReleasePbufferDCARB)(HPBUFFERARB, HDC);
31
32 /* WGL_ARB_pixel_format */
33 #define WGL_COLOR_BITS_ARB 0x2014
34 #define WGL_RED_BITS_ARB   0x2015
35 #define WGL_GREEN_BITS_ARB 0x2017
36 #define WGL_BLUE_BITS_ARB  0x2019
37 #define WGL_ALPHA_BITS_ARB 0x201B
38 #define WGL_SUPPORT_GDI_ARB   0x200F
39 #define WGL_DOUBLE_BUFFER_ARB 0x2011
40
41 static BOOL (WINAPI *pwglChoosePixelFormatARB)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
42 static BOOL (WINAPI *pwglGetPixelFormatAttribivARB)(HDC, int, int, UINT, const int *, int *);
43
44 /* WGL_ARB_pbuffer */
45 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
46 static HPBUFFERARB* (WINAPI *pwglCreatePbufferARB)(HDC, int, int, int, const int *);
47 static HDC (WINAPI *pwglGetPbufferDCARB)(HPBUFFERARB);
48
49 static const char* wgl_extensions = NULL;
50
51 static void init_functions(void)
52 {
53 #define GET_PROC(func) \
54     p ## func = (void*)wglGetProcAddress(#func); \
55     if(!p ## func) \
56       trace("wglGetProcAddress(%s) failed\n", #func);
57
58     /* WGL_ARB_extensions_string */
59     GET_PROC(wglGetExtensionsStringARB)
60
61     /* WGL_ARB_pixel_format */
62     GET_PROC(wglChoosePixelFormatARB)
63     GET_PROC(wglGetPixelFormatAttribivARB)
64
65     /* WGL_ARB_pbuffer */
66     GET_PROC(wglCreatePbufferARB)
67     GET_PROC(wglGetPbufferDCARB)
68     GET_PROC(wglReleasePbufferDCARB)
69
70 #undef GET_PROC
71 }
72
73 static void test_pbuffers(HDC hdc)
74 {
75     const int iAttribList[] = { WGL_DRAW_TO_PBUFFER_ARB, 1, /* Request pbuffer support */
76                                 0 };
77     int iFormats[MAX_FORMATS];
78     unsigned int nOnscreenFormats;
79     unsigned int nFormats;
80     int i, res;
81     int iPixelFormat = 0;
82
83     nOnscreenFormats = DescribePixelFormat(hdc, 0, 0, NULL);
84
85     /* When you want to render to a pbuffer you need to call wglGetPbufferDCARB which
86      * returns a 'magic' HDC which you can then pass to wglMakeCurrent to switch rendering
87      * to the pbuffer. Below some tests are performed on what happens if you use standard WGL calls
88      * on this 'magic' HDC for both a pixelformat that support onscreen and offscreen rendering
89      * and a pixelformat that's only available for offscreen rendering (this means that only
90      * wglChoosePixelFormatARB and friends know about the format.
91      *
92      * The first thing we need are pixelformats with pbuffer capabilites.
93      */
94     res = pwglChoosePixelFormatARB(hdc, iAttribList, NULL, MAX_FORMATS, iFormats, &nFormats);
95     if(res <= 0)
96     {
97         skip("No pbuffer compatible formats found while WGL_ARB_pbuffer is supported\n");
98         return;
99     }
100     trace("nOnscreenFormats: %d\n", nOnscreenFormats);
101     trace("Total number of pbuffer capable pixelformats: %d\n", nFormats);
102
103     /* Try to select an onscreen pixelformat out of the list */
104     for(i=0; i < nFormats; i++)
105     {
106         /* Check if the format is onscreen, if it is choose it */
107         if(iFormats[i] <= nOnscreenFormats)
108         {
109             iPixelFormat = iFormats[i];
110             trace("Selected iPixelFormat=%d\n", iPixelFormat);
111             break;
112         }
113     }
114
115     /* A video driver supports a large number of onscreen and offscreen pixelformats.
116      * The traditional WGL calls only see a subset of the whole pixelformat list. First
117      * of all they only see the onscreen formats (the offscreen formats are at the end of the
118      * pixelformat list) and second extended pixelformat capabilities are hidden from the
119      * standard WGL calls. Only functions that depend on WGL_ARB_pixel_format can see them.
120      *
121      * Below we check if the pixelformat is also supported onscreen.
122      */
123     if(iPixelFormat != 0)
124     {
125         HDC pbuffer_hdc;
126         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
127         if(!pbuffer)
128             skip("Pbuffer creation failed!\n");
129
130         /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
131         pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
132         res = GetPixelFormat(pbuffer_hdc);
133         ok(res == iPixelFormat, "Unexpected iPixelFormat=%d returned by GetPixelFormat for format %d\n", res, iPixelFormat);
134         trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
135         trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
136
137         pwglReleasePbufferDCARB(pbuffer, hdc);
138     }
139     else skip("Pbuffer test for onscreen pixelformat skipped as no onscreen format with pbuffer capabilities have been found\n");
140
141     /* Search for a real offscreen format */
142     for(i=0, iPixelFormat=0; i<nFormats; i++)
143     {
144         if(iFormats[i] > nOnscreenFormats)
145         {
146             iPixelFormat = iFormats[i];
147             trace("Selected iPixelFormat: %d\n", iPixelFormat);
148             break;
149         }
150     }
151
152     if(iPixelFormat != 0)
153     {
154         HDC pbuffer_hdc;
155         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
156         if(!pbuffer)
157             skip("Pbuffer creation failed!\n");
158
159         /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
160         pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
161         res = GetPixelFormat(pbuffer_hdc);
162
163         ok(res == 1, "Unexpected iPixelFormat=%d (1 expected) returned by GetPixelFormat for offscreen format %d\n", res, iPixelFormat);
164         trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
165         trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
166         pwglReleasePbufferDCARB(pbuffer, hdc);
167     }
168     else skip("Pbuffer test for offscreen pixelformat skipped as no offscreen-only format with pbuffer capabilities has been found\n");
169 }
170
171 static void test_setpixelformat(void)
172 {
173     int res = 0;
174     int pf;
175     PIXELFORMATDESCRIPTOR pfd = {
176         sizeof(PIXELFORMATDESCRIPTOR),
177         1,                     /* version */
178         PFD_DRAW_TO_WINDOW |
179         PFD_SUPPORT_OPENGL |
180         PFD_DOUBLEBUFFER,
181         PFD_TYPE_RGBA,
182         24,                    /* 24-bit color depth */
183         0, 0, 0, 0, 0, 0,      /* color bits */
184         0,                     /* alpha buffer */
185         0,                     /* shift bit */
186         0,                     /* accumulation buffer */
187         0, 0, 0, 0,            /* accum bits */
188         32,                    /* z-buffer */
189         0,                     /* stencil buffer */
190         0,                     /* auxiliary buffer */
191         PFD_MAIN_PLANE,        /* main layer */
192         0,                     /* reserved */
193         0, 0, 0                /* layer masks */
194     };
195
196     HDC hdc = GetDC(0);
197     ok(hdc != 0, "GetDC(0) failed!\n");
198
199     /* This should pass even on the main device context */
200     pf = ChoosePixelFormat(hdc, &pfd);
201     ok(pf != 0, "ChoosePixelFormat failed on main device context\n");
202
203     /* SetPixelFormat on the main device context 'X root window' should fail */
204     res = SetPixelFormat(hdc, pf, &pfd);
205     ok(res == 0, "SetPixelFormat on main device context should fail\n");
206 }
207
208 static void test_colorbits(HDC hdc)
209 {
210     const int iAttribList[] = { WGL_COLOR_BITS_ARB, WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB,
211                                 WGL_BLUE_BITS_ARB, WGL_ALPHA_BITS_ARB };
212     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
213     const int iAttribs[] = { WGL_ALPHA_BITS_ARB, 1, 0 };
214     unsigned int nFormats;
215     int res;
216     int iPixelFormat = 0;
217
218     /* We need a pixel format with at least one bit of alpha */
219     res = pwglChoosePixelFormatARB(hdc, iAttribs, NULL, 1, &iPixelFormat, &nFormats);
220     if(res == FALSE || nFormats == 0)
221     {
222         skip("No suitable pixel formats found\n");
223         return;
224     }
225
226     res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
227               sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
228     if(res == FALSE)
229     {
230         skip("wglGetPixelFormatAttribivARB failed\n");
231         return;
232     }
233     iAttribRet[1] += iAttribRet[2]+iAttribRet[3]+iAttribRet[4];
234     ok(iAttribRet[0] == iAttribRet[1], "WGL_COLOR_BITS_ARB (%d) does not equal R+G+B+A (%d)!\n",
235                                        iAttribRet[0], iAttribRet[1]);
236 }
237
238 static void test_gdi_dbuf(HDC hdc)
239 {
240     const int iAttribList[] = { WGL_SUPPORT_GDI_ARB, WGL_DOUBLE_BUFFER_ARB };
241     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
242     unsigned int nFormats;
243     int iPixelFormat;
244     int res;
245
246     nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
247     for(iPixelFormat = 1;iPixelFormat <= nFormats;iPixelFormat++)
248     {
249         res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
250                   sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
251                   iAttribRet);
252         ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
253         if(res == FALSE)
254             continue;
255
256         ok(!(iAttribRet[0] && iAttribRet[1]), "GDI support and double buffering on pixel format %d\n", iPixelFormat);
257     }
258 }
259
260 START_TEST(opengl)
261 {
262     HWND hwnd;
263     PIXELFORMATDESCRIPTOR pfd = {
264         sizeof(PIXELFORMATDESCRIPTOR),
265         1,                     /* version */
266         PFD_DRAW_TO_WINDOW |
267         PFD_SUPPORT_OPENGL |
268         PFD_DOUBLEBUFFER,
269         PFD_TYPE_RGBA,
270         24,                    /* 24-bit color depth */
271         0, 0, 0, 0, 0, 0,      /* color bits */
272         0,                     /* alpha buffer */
273         0,                     /* shift bit */
274         0,                     /* accumulation buffer */
275         0, 0, 0, 0,            /* accum bits */
276         32,                    /* z-buffer */
277         0,                     /* stencil buffer */
278         0,                     /* auxiliary buffer */
279         PFD_MAIN_PLANE,        /* main layer */
280         0,                     /* reserved */
281         0, 0, 0                /* layer masks */
282     };
283
284     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
285                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
286     ok(hwnd != NULL, "err: %d\n", GetLastError());
287     if (hwnd)
288     {
289         HDC hdc;
290         int iPixelFormat, res;
291         HGLRC hglrc;
292         ShowWindow(hwnd, SW_SHOW);
293
294         hdc = GetDC(hwnd);
295
296         iPixelFormat = ChoosePixelFormat(hdc, &pfd);
297         ok(iPixelFormat > 0, "No pixelformat found!\n"); /* This should never happen as ChoosePixelFormat always returns a closest match */
298
299         res = SetPixelFormat(hdc, iPixelFormat, &pfd);
300         ok(res, "SetPixelformat failed: %x\n", GetLastError());
301
302         hglrc = wglCreateContext(hdc);
303         res = wglMakeCurrent(hdc, hglrc);
304         ok(res, "wglMakeCurrent failed!\n");
305         init_functions();
306
307         test_setpixelformat();
308         test_colorbits(hdc);
309         test_gdi_dbuf(hdc);
310
311         wgl_extensions = pwglGetExtensionsStringARB(hdc);
312         if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
313
314         if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
315             test_pbuffers(hdc);
316         else
317             trace("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");
318
319         DestroyWindow(hwnd);
320     }
321 }