glx-oml-sync-control-timing: Fix error message if glXWaitForMscOML fails
[piglit.git] / tests / util / piglit-wgl-util.c
bloba807d2c01ce74f08ba8ae9141dde1f043585a64e
1 /*
2 * Copyright © 2017 VMware, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
23 * WGL utility functions, based on GLX utility functions by Eric Anholt.
25 * Authors:
26 * Brian Paul
30 #include <stdio.h>
31 #include "piglit-util-gl.h"
32 #include "piglit-wgl-util.h"
35 int piglit_width = 100;
36 int piglit_height = 100;
39 static LRESULT CALLBACK
40 WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
42 switch (uMsg) {
43 case WM_CLOSE:
44 PostQuitMessage(0);
45 return 0;
46 case WM_SIZE:
47 //reshape(LOWORD(lParam), HIWORD(lParam));
48 return 0;
49 case WM_KEYDOWN:
50 if (wParam == VK_ESCAPE)
51 PostQuitMessage(0);
52 return 0;
55 return DefWindowProc(hWnd, uMsg, wParam, lParam);
59 HWND
60 piglit_get_wgl_window(void)
62 int pixelFormat;
63 WNDCLASS wc;
64 DWORD dwExStyle, dwStyle;
65 HDC hDC;
66 RECT winrect;
67 HINSTANCE hInst;
68 HWND hWnd;
69 char *name = "wgl";
71 static const PIXELFORMATDESCRIPTOR pfd = {
72 sizeof(PIXELFORMATDESCRIPTOR),
74 PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
75 PFD_TYPE_RGBA,
76 24,
77 0, 0, 0, 0, 0, 0,
81 0, 0, 0, 0,
82 16,
85 PFD_MAIN_PLANE,
87 0, 0, 0
90 winrect.left = 0;
91 winrect.right = piglit_width;
92 winrect.top = 0;
93 winrect.bottom = piglit_height;
95 hInst = GetModuleHandle(NULL);
96 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
97 wc.lpfnWndProc = (WNDPROC)WndProc;
98 wc.cbClsExtra = 0;
99 wc.cbWndExtra = 0;
100 wc.hInstance = hInst;
101 wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
102 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
103 wc.hbrBackground = NULL;
104 wc.lpszMenuName = NULL;
105 wc.lpszClassName = name;
106 if (!RegisterClass(&wc)) {
107 /* This fails for second and subsequent calls */
109 fprintf(stderr, "failed to register class\n");
110 fflush(stderr);
111 return 0;
115 dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
116 dwStyle = WS_OVERLAPPEDWINDOW;
117 AdjustWindowRectEx(&winrect, dwStyle, FALSE, dwExStyle);
119 if (!(hWnd = CreateWindowEx(dwExStyle, name, name,
120 WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle,
121 0, 0,
122 winrect.right - winrect.left,
123 winrect.bottom - winrect.top,
124 NULL, NULL, hInst, NULL))) {
125 fprintf(stderr, "failed to create window\n");
126 fflush(stderr);
127 return 0;
130 if (!(hDC = GetDC(hWnd))) {
131 fprintf(stderr, "GetDC failed\n");
132 fflush(stderr);
133 return 0;
136 if (!(pixelFormat = ChoosePixelFormat(hDC, &pfd))) {
137 fprintf(stderr, "ChoosePixelFormat failed\n");
138 fflush(stderr);
139 return 0;
142 if (!(SetPixelFormat(hDC, pixelFormat, &pfd))) {
143 fprintf(stderr, "SetPixelFormat failed\n");
144 fflush(stderr);
145 return 0;
148 ShowWindow(hWnd, SW_SHOW);
149 SetForegroundWindow(hWnd);
150 SetFocus(hWnd);
152 return hWnd;
156 HGLRC
157 piglit_get_wgl_context(HWND hWnd)
159 HDC hDC;
160 HGLRC hRC;
162 if (!(hDC = GetDC(hWnd))) {
163 fprintf(stderr, "GetDC failed\n");
164 fflush(stderr);
165 return 0;
168 if (!(hRC = wglCreateContext(hDC))) {
169 fprintf(stderr, "wglCreateContext failed\n");
170 fflush(stderr);
171 return 0;
174 return hRC;
178 void
179 piglit_wgl_event_loop(enum piglit_result (*draw)(void))
181 MSG msg;
182 enum piglit_result result = PIGLIT_SKIP;
184 while(1) {
185 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
186 if (msg.message == WM_QUIT)
187 break;
188 TranslateMessage(&msg);
189 DispatchMessage(&msg);
192 result = draw();
194 if (piglit_automatic) {
195 break;
199 piglit_report_result(result);