Add more structure constructor tests.
[piglit/hramrach.git] / tests / egl / egl-util.c
blobc44b41f0a50d5b3a29ff2fe281c06608b48af8d2
1 /*
2 * Copyright © 2010 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.
23 * Author: Kristian Høgsberg <krh@bitplanet.net>
26 /** @file egl-nok-swap-region.c
28 * Test EGL_NOK_swap_region.
31 #include "piglit-util.h"
32 #include "egl-util.h"
34 static int automatic;
36 int depth;
38 static const EGLint attribs[] = {
39 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT | EGL_PBUFFER_BIT,
40 EGL_RED_SIZE, 1,
41 EGL_GREEN_SIZE, 1,
42 EGL_BLUE_SIZE, 1,
43 EGL_DEPTH_SIZE, 1,
44 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
45 EGL_NONE
48 EGLSurface
49 egl_util_create_pixmap(struct egl_state *state,
50 int width, int height, const EGLint *attribs)
52 Pixmap pixmap;
53 EGLSurface surf;
55 pixmap = XCreatePixmap(state->dpy, state->win,
56 width, height, state->depth);
58 surf = eglCreatePixmapSurface(state->egl_dpy, state->cfg,
59 pixmap, attribs);
61 return surf;
64 static void
65 create_window(struct egl_state *state)
67 XSetWindowAttributes window_attr;
68 XVisualInfo template, *vinfo;
69 EGLint id;
70 unsigned long mask;
71 int screen = DefaultScreen(state->dpy);
72 Window root_win = RootWindow(state->dpy, screen);
73 int count;
75 if (!eglGetConfigAttrib(state->egl_dpy,
76 state->cfg, EGL_NATIVE_VISUAL_ID, &id)) {
77 fprintf(stderr, "eglGetConfigAttrib() failed\n");
78 piglit_report_result(PIGLIT_FAILURE);
81 template.visualid = id;
82 vinfo = XGetVisualInfo(state->dpy, VisualIDMask, &template, &count);
83 if (count != 1) {
84 fprintf(stderr, "XGetVisualInfo() failed\n");
85 piglit_report_result(PIGLIT_FAILURE);
88 state->depth = vinfo->depth;
89 window_attr.background_pixel = 0;
90 window_attr.border_pixel = 0;
91 window_attr.colormap = XCreateColormap(state->dpy, root_win,
92 vinfo->visual, AllocNone);
93 window_attr.event_mask =
94 StructureNotifyMask | ExposureMask | KeyPressMask;
95 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
96 state->win = XCreateWindow(state->dpy, root_win, 0, 0,
97 state->width, state->height,
98 0, vinfo->depth, InputOutput,
99 vinfo->visual, mask, &window_attr);
101 XMapWindow(state->dpy, state->win);
103 XFree(vinfo);
106 static enum piglit_result
107 event_loop(struct egl_state *state, const struct egl_test *test)
109 XEvent event;
110 enum piglit_result result = PIGLIT_FAILURE;
112 while (1) {
113 XNextEvent (state->dpy, &event);
115 if (event.type == Expose) {
116 result = test->draw(state);
117 if (automatic)
118 break;
121 if (event.type == KeyPress) {
122 KeySym sym = XKeycodeToKeysym (state->dpy,
123 event.xkey.keycode, 0);
125 if (sym == XK_Escape || sym == XK_q || sym == XK_Q)
126 break;
130 return result;
133 static void
134 check_extensions(struct egl_state *state, const struct egl_test *test)
136 const char *extensions;
137 int i;
139 extensions = eglQueryString(state->egl_dpy, EGL_EXTENSIONS);
140 for (i = 0; test->extensions[i]; i++) {
141 if (!strstr(extensions, test->extensions[i])) {
142 fprintf(stderr, "missing extension %s\n",
143 test->extensions[i]);
144 piglit_report_result(PIGLIT_SKIP);
150 egl_util_run(const struct egl_test *test, int argc, char *argv[])
152 struct egl_state state;
153 EGLint count;
154 enum piglit_result result;
155 int i;
157 for (i = 1; i < argc; ++i) {
158 if (!strcmp(argv[i], "-auto"))
159 automatic = 1;
160 else
161 fprintf(stderr, "Unknown option: %s\n", argv[i]);
164 state.dpy = XOpenDisplay(NULL);
165 if (state.dpy == NULL) {
166 fprintf(stderr, "couldn't open display\n");
167 piglit_report_result(PIGLIT_FAILURE);
170 eglBindAPI(EGL_OPENGL_API);
172 state.egl_dpy = eglGetDisplay(state.dpy);
173 if (state.egl_dpy == EGL_NO_DISPLAY) {
174 fprintf(stderr, "eglGetDisplay() failed\n");
175 piglit_report_result(PIGLIT_FAILURE);
178 if (!eglInitialize(state.egl_dpy, &state.major, &state.minor)) {
179 fprintf(stderr, "eglInitialize() failed\n");
180 piglit_report_result(PIGLIT_FAILURE);
183 check_extensions(&state, test);
185 if (!eglChooseConfig(state.egl_dpy, attribs, &state.cfg, 1, &count) ||
186 count == 0) {
187 fprintf(stderr, "eglChooseConfig() failed\n");
188 piglit_report_result(PIGLIT_FAILURE);
191 state.ctx = eglCreateContext(state.egl_dpy, state.cfg,
192 EGL_NO_CONTEXT, NULL);
193 if (state.ctx == EGL_NO_CONTEXT) {
194 fprintf(stderr, "eglCreateContext() failed\n");
195 piglit_report_result(PIGLIT_FAILURE);
198 state.width = 300;
199 state.height = 300;
200 create_window(&state);
202 state.surf = eglCreateWindowSurface(state.egl_dpy,
203 state.cfg, state.win, NULL);
204 if (state.surf == EGL_NO_SURFACE) {
205 fprintf(stderr, "eglCreateWindowSurface() failed\n");
206 piglit_report_result(PIGLIT_FAILURE);
209 if (!eglMakeCurrent(state.egl_dpy,
210 state.surf, state.surf, state.ctx)) {
211 fprintf(stderr, "eglMakeCurrent() failed\n");
212 piglit_report_result(PIGLIT_FAILURE);
215 result = event_loop(&state, test);
217 eglTerminate(state.egl_dpy);
219 piglit_report_result(result);
221 return EXIT_SUCCESS;