Skip to content

Commit ab3034e

Browse files
committed
Rework pixel format selection on Windows
1 parent c3b9601 commit ab3034e

File tree

1 file changed

+61
-23
lines changed

1 file changed

+61
-23
lines changed

test/wgl_common.c

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,79 @@
2727

2828
static int (*test_callback)(HDC hdc);
2929

30+
static int
31+
find_pixel_format(HDC hdc)
32+
{
33+
DWORD required_flags = PFD_DRAW_TO_WINDOW |
34+
PFD_SUPPORT_OPENGL;
35+
DWORD unwanted_flags = PFD_NEED_PALETTE |
36+
PFD_NEED_SYSTEM_PALETTE;
37+
PIXELFORMATDESCRIPTOR pfd;
38+
int best_index = 0;
39+
int best_distance = 1;
40+
41+
for (int index = 1;
42+
DescribePixelFormat(hdc, index, sizeof (pfd), &pfd) && best_distance != 0;
43+
index++)
44+
{
45+
int distance;
46+
47+
if (pfd.nSize < sizeof (pfd) || pfd.nVersion != 1)
48+
continue;
49+
50+
if (pfd.iPixelType != PFD_TYPE_RGBA)
51+
continue;
52+
53+
if (pfd.iLayerType != PFD_MAIN_PLANE)
54+
continue;
55+
56+
if ((pfd.dwFlags & required_flags) != required_flags)
57+
continue;
58+
59+
if ((pfd.dwFlags & unwanted_flags) != 0)
60+
continue;
61+
62+
distance = 1000 * !!(pfd.dwFlags & PFD_GENERIC_FORMAT) +
63+
100 * !!(pfd.dwFlags & PFD_GENERIC_ACCELERATED) +
64+
10 * !!(pfd.cColorBits != 24 && pfd.cColorBits != 32) +
65+
10 * !!(pfd.cRedBits != 8 || pfd.cGreenBits != 8 || pfd.cBlueBits != 8) +
66+
!(pfd.dwFlags & PFD_DOUBLEBUFFER);
67+
if (best_index == 0 || distance < best_distance) {
68+
best_index = index;
69+
best_distance = distance;
70+
}
71+
}
72+
73+
return best_index;
74+
}
75+
3076
static void
3177
setup_pixel_format(HDC hdc)
3278
{
33-
PIXELFORMATDESCRIPTOR pfd = {
34-
sizeof(PIXELFORMATDESCRIPTOR),
35-
1,
36-
PFD_SUPPORT_OPENGL |
37-
PFD_DRAW_TO_WINDOW |
38-
PFD_DOUBLEBUFFER,
39-
PFD_TYPE_RGBA,
40-
32,
41-
0, 0, 0, 0, 0, 0,
42-
0,
43-
0,
44-
0,
45-
0, 0, 0, 0,
46-
16,
47-
0,
48-
0,
49-
PFD_MAIN_PLANE,
50-
0,
51-
0, 0, 0,
52-
};
79+
PIXELFORMATDESCRIPTOR pfd;
5380
int pixel_format;
81+
int ret;
5482

55-
pixel_format = ChoosePixelFormat(hdc, &pfd);
83+
pixel_format = find_pixel_format(hdc);
5684
if (!pixel_format) {
57-
fputs("ChoosePixelFormat failed.\n", stderr);
58-
exit(1);
85+
fputs("Could not find suitable pixel format.\n", stderr);
86+
exit(77); /* skip */
5987
}
6088

89+
DescribePixelFormat(hdc, pixel_format, sizeof (pfd), &pfd);
90+
6191
if (SetPixelFormat(hdc, pixel_format, &pfd) != TRUE) {
6292
fputs("SetPixelFormat() failed.\n", stderr);
6393
exit(1);
6494
}
95+
96+
SetLastError(0);
97+
ret = GetPixelFormat(hdc);
98+
if (ret == 0) {
99+
unsigned int code = GetLastError();
100+
fprintf(stderr, "GetPixelFormat failed with error code %u\n", code);
101+
exit(1);
102+
}
65103
}
66104

67105
static LRESULT CALLBACK

0 commit comments

Comments
 (0)