glx-multithread-texture: Avoid front-buffer rendering.
[piglit.git] / tests / util / piglit-framework-gl / piglit_x11_framework.c
blob8ea7b3ac4bb79815e5c8ed9e243ee65fe489ea9d
1 /*
2 * Copyright © 2012 Intel Corporation
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.
24 #ifndef PIGLIT_HAS_X11
25 #error "Cannot build piglit_x11_framework.c without PIGLIT_HAS_X11"
26 #endif
28 #include <piglit/gl_wrap.h>
30 /* If building for a GLES API, <piglit/gl_wrap.h> may include the GLES
31 * headers, such as <GLES2/gl2.h>. Below, <waffle_glx.h> transitively includes
32 * <GL/gl.h>, which defines some of the same symbols found in <GLES2/gl2.h>.
33 * We define the include guards below in order to prevent <GL/gl.h> and
34 * related headers from being included and causing compilation failure due to
35 * symbol redefinitions.
37 #define __gl_h_
38 #define __gltypes_h_
39 #define __glext_h_
41 #include <assert.h>
42 #include <unistd.h>
44 #include <X11/Xlib.h>
45 #include <X11/Xutil.h>
46 #include <X11/keysym.h>
48 #ifdef PIGLIT_HAS_GLX
49 # include <waffle_glx.h>
50 #endif
52 #ifdef PIGLIT_HAS_EGL
53 # include <waffle_x11_egl.h>
54 #endif
56 #include "piglit-util-gl.h"
58 #include "piglit_gl_framework.h"
59 #include "piglit_x11_framework.h"
61 struct piglit_x11_framework {
62 struct piglit_winsys_framework winsys_fw;
64 Display *display;
65 Window window;
68 struct piglit_x11_framework*
69 piglit_x11_framework(struct piglit_gl_framework *gl_fw)
71 return (struct piglit_x11_framework*) gl_fw;
74 static void
75 get_window_size(struct piglit_x11_framework *x11_fw)
77 unsigned width, height;
79 Window wjunk;
80 int ijunk;
81 unsigned ujunk;
83 XGetGeometry(x11_fw->display, x11_fw->window,
84 &wjunk, &ijunk, &ijunk,
85 &width, &height, &ujunk, &ujunk);
87 piglit_width = width;
88 piglit_height = height;
91 static void
92 process_next_event(struct piglit_x11_framework *x11_fw)
94 struct piglit_winsys_framework *winsys_fw = &x11_fw->winsys_fw;
95 const struct piglit_gl_test_config *test_config = winsys_fw->wfl_fw.gl_fw.test_config;
97 Display *dpy = x11_fw->display;
98 XEvent event;
100 XNextEvent(dpy, &event);
102 switch (event.type) {
103 case Expose:
104 get_window_size(x11_fw);
105 winsys_fw->need_redisplay = true;
106 break;
107 case ConfigureNotify:
108 get_window_size(x11_fw);
109 if (winsys_fw->user_reshape_func)
110 winsys_fw->user_reshape_func(event.xconfigure.width,
111 event.xconfigure.height);
112 winsys_fw->need_redisplay = true;
113 break;
114 case KeyPress: {
115 char buffer[1];
116 KeySym sym;
117 int n;
119 n = XLookupString(&event.xkey,
120 buffer,
121 sizeof(buffer), &sym, NULL);
123 if (n > 0 && winsys_fw->user_keyboard_func)
124 winsys_fw->user_keyboard_func(buffer[0],
125 event.xkey.x,
126 event.xkey.y);
127 winsys_fw->need_redisplay = true;
128 break;
130 default:
131 break;
134 if (winsys_fw->need_redisplay) {
135 enum piglit_result result = PIGLIT_PASS;
136 if (test_config->display)
137 result = test_config->display();
138 if (piglit_automatic)
139 piglit_report_result(result);
140 winsys_fw->need_redisplay = false;
144 static void
145 enter_event_loop(struct piglit_winsys_framework *winsys_fw)
147 struct piglit_x11_framework *x11_fw = (void*) winsys_fw;
149 assert(x11_fw->display != NULL);
150 assert(x11_fw->window != 0);
152 while (true)
153 process_next_event(x11_fw);
156 static void
157 get_native(struct piglit_x11_framework *x11_fw)
159 struct piglit_wfl_framework *wfl_fw = &x11_fw->winsys_fw.wfl_fw;
160 union waffle_native_window *n_window = waffle_window_get_native(wfl_fw->window);
162 switch (wfl_fw->platform) {
163 #ifdef PIGLIT_HAS_GLX
164 case WAFFLE_PLATFORM_GLX:
165 x11_fw->display = n_window->glx->xlib_display;
166 x11_fw->window = n_window->glx->xlib_window;
167 break;
168 #endif
169 #ifdef PIGLIT_HAS_EGL
170 case WAFFLE_PLATFORM_X11_EGL:
171 x11_fw->display = n_window->x11_egl->display.xlib_display;
172 x11_fw->window = n_window->x11_egl->xlib_window;
173 break;
174 #endif
175 default:
176 assert(0);
177 break;
180 free(n_window);
183 static void
184 show_window(struct piglit_winsys_framework *winsys_fw)
186 struct piglit_x11_framework *x11_fw = piglit_x11_framework(&winsys_fw->wfl_fw.gl_fw);
187 struct piglit_wfl_framework *wfl_fw = &winsys_fw->wfl_fw;
188 XWMHints *wm_hints;
190 get_native(x11_fw);
192 if (piglit_automatic) {
193 /* Prevent the window from grabbing input. */
194 wm_hints = XAllocWMHints();
195 wm_hints->flags |= InputHint;
196 wm_hints->input = False;
197 XSetWMHints(x11_fw->display, x11_fw->window, wm_hints);
198 XFree(wm_hints);
201 waffle_window_show(wfl_fw->window);
204 static void
205 destroy(struct piglit_gl_framework *gl_fw)
207 struct piglit_x11_framework *x11_fw = piglit_x11_framework(gl_fw);
209 if (x11_fw == NULL)
210 return;
212 piglit_winsys_framework_teardown(&x11_fw->winsys_fw);
213 free(x11_fw);
216 struct piglit_gl_framework*
217 piglit_x11_framework_create(const struct piglit_gl_test_config *test_config,
218 int32_t platform)
220 struct piglit_x11_framework *x11_fw = NULL;
221 struct piglit_winsys_framework *winsys_fw = NULL;
222 struct piglit_gl_framework *gl_fw = NULL;
223 bool ok = true;
225 x11_fw = calloc(1, sizeof(*x11_fw));
226 winsys_fw = &x11_fw->winsys_fw;
227 gl_fw = &x11_fw->winsys_fw.wfl_fw.gl_fw;
229 ok = piglit_winsys_framework_init(&x11_fw->winsys_fw,
230 test_config, platform);
231 if (!ok)
232 goto fail;
234 winsys_fw->show_window = show_window;
235 winsys_fw->enter_event_loop = enter_event_loop;
236 gl_fw->destroy = destroy;
238 return gl_fw;
240 fail:
241 destroy(gl_fw);
242 return NULL;