ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / glx_ext_import_context / common.c
blob8b0be4fccdfd6b437ced2b198ad7f64a13b0bfd4
1 /* Copyright © 2011 Intel Corporation
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation
6 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 * and/or sell copies of the Software, and to permit persons to whom the
8 * Software is furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice (including the next
11 * paragraph) shall be included in all copies or substantial portions of the
12 * Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
23 #include <assert.h>
24 #include "piglit-util-gl.h"
25 #include "piglit-glx-util.h"
26 #include "common.h"
28 PFNGLXGETCURRENTDISPLAYEXTPROC __piglit_glXGetCurrentDisplayEXT = NULL;
29 PFNGLXQUERYCONTEXTINFOEXTPROC __piglit_glXQueryContextInfoEXT = NULL;
30 PFNGLXGETCONTEXTIDEXTPROC __piglit_glXGetContextIDEXT = NULL;
31 PFNGLXIMPORTCONTEXTEXTPROC __piglit_glXImportContextEXT = NULL;
32 PFNGLXFREECONTEXTEXTPROC __piglit_glXFreeContextEXT = NULL;
34 #define ADD_FUNC(name) PIGLIT_GLX_PROC(__piglit_##name, name)
35 static const struct piglit_glx_proc_reference procedures[] = {
36 ADD_FUNC(glXGetCurrentDisplayEXT),
37 ADD_FUNC(glXQueryContextInfoEXT),
38 ADD_FUNC(glXGetContextIDEXT),
39 ADD_FUNC(glXImportContextEXT),
40 ADD_FUNC(glXFreeContextEXT),
43 Display *dpy = NULL;
44 XVisualInfo *visinfo = NULL;
45 GLXContext directCtx = NULL;
46 GLXContextID directID = None;
47 GLXContext indirectCtx = NULL;
48 GLXContextID indirectID = None;
50 int glx_error_code = -1;
51 int x_error_code = Success;
53 int piglit_height = 50;
54 int piglit_width = 50;
56 static int (*old_handler)(Display *, XErrorEvent *);
58 static int x_error_handler(Display *dpy, XErrorEvent *e)
61 x_error_code = e->error_code;
62 glx_error_code = piglit_glx_get_error(dpy, e);
64 return 0;
67 void GLX_EXT_import_context_setup_for_child(void)
69 dpy = piglit_get_glx_display();
70 old_handler = XSetErrorHandler(x_error_handler);
73 void GLX_EXT_import_context_setup(void)
75 const char *vendor;
77 dpy = piglit_get_glx_display();
79 /* NVIDIA incorrectly only list the extension in the client
80 * extensions list. If the extension is available for applications
81 * to use, it is supposed to be included in the list returned by
82 * glXQueryExtensionsString.
84 * The glXImportContextEXT manual page is somewhat clear on this
85 * topic:
87 * "If _glxextstring(EXT_import_context) is included in the string
88 * returned by glXQueryExtensionsString, when called with argument
89 * GLX_EXTENSIONS, extension EXT_import_context is supported."
91 * The text is a little weird because the only parameters to
92 * glXQueryExtensionsString are the display and the screen.
94 vendor = glXGetClientString(dpy, GLX_VENDOR);
95 if (strcmp("NVIDIA Corporation", vendor) == 0) {
96 const char *const client_extensions =
97 glXGetClientString(dpy, GLX_EXTENSIONS);
99 if (!piglit_is_extension_in_string(client_extensions,
100 "GLX_EXT_import_context")) {
101 fprintf(stderr,
102 "Test requires GLX_EXT_import_context.\n");
103 piglit_report_result(PIGLIT_SKIP);
105 } else {
106 piglit_require_glx_extension(dpy, "GLX_EXT_import_context");
109 piglit_glx_get_all_proc_addresses(procedures, ARRAY_SIZE(procedures));
111 visinfo = piglit_get_glx_visual(dpy);
113 directCtx = glXCreateContext(dpy, visinfo, NULL, True);
114 if (directCtx == NULL) {
115 fprintf(stderr,
116 "Could not create initial direct-rendering context.\n");
117 piglit_report_result(PIGLIT_FAIL);
120 if (!glXIsDirect(dpy, directCtx)) {
121 glXDestroyContext(dpy, directCtx);
122 directCtx = NULL;
125 indirectCtx = glXCreateContext(dpy, visinfo, NULL, False);
126 if (indirectCtx == NULL) {
127 fprintf(stderr,
128 "Could not create initial indirect-rendering "
129 "context.\n");
130 piglit_report_result(PIGLIT_FAIL);
133 piglit_glx_get_error(dpy, NULL);
134 old_handler = XSetErrorHandler(x_error_handler);
137 void GLX_EXT_import_context_teardown(void)
139 if (directCtx != NULL) {
140 glXDestroyContext(dpy, directCtx);
141 directCtx = NULL;
144 if (indirectCtx != NULL) {
145 glXDestroyContext(dpy, indirectCtx);
146 indirectCtx = NULL;
149 XFree(visinfo);
150 visinfo = NULL;
152 XSetErrorHandler(old_handler);
155 void get_context_IDs(void)
157 directID = None;
158 if (directCtx != NULL) {
159 directID = glXGetContextIDEXT(directCtx);
162 indirectID = None;
163 if (indirectCtx != NULL) {
164 indirectID = glXGetContextIDEXT(indirectCtx);
168 const char *context_mode_name(enum context_mode mode)
170 switch (mode) {
171 case direct_rendering:
172 return "direct-rendering";
173 case indirect_rendering:
174 return "indirect-rendering";
175 case invalid:
176 return "invalid";
177 default:
178 assert(!"Should not get here.");
179 piglit_report_result(PIGLIT_FAIL);
182 return "";
185 bool try_import_context(GLXContextID id, enum context_mode mode)
187 const int expected_glx_error = (mode == invalid) ? GLXBadContext : -1;
188 bool pass = true;
189 GLXContext ctx = glXImportContextEXT(dpy, id);
191 XSync(dpy, 0);
193 switch (mode) {
194 case direct_rendering:
195 if (ctx != NULL) {
196 fprintf(stderr,
197 "Could import direct-rendering context, "
198 "but should have failed.\n");
199 pass = false;
201 break;
203 case indirect_rendering:
204 if (ctx == NULL) {
205 fprintf(stderr,
206 "Could not import indirect-rendering context, "
207 "but should have succeeded.\n");
208 pass = false;
210 break;
212 case invalid:
213 if (ctx != NULL) {
214 fprintf(stderr,
215 "Could import invalid context (0x%08x), "
216 "but should have failed.\n",
217 (int) id);
218 pass = false;
220 break;
223 assert(mode != invalid || expected_glx_error != -1);
224 pass = validate_glx_error_code(Success, expected_glx_error) && pass;
226 if (!pass)
227 fprintf(stderr, "Context ID = 0x%08x.\n", (int) id);
229 if (ctx != NULL)
230 glXFreeContextEXT(dpy, ctx);
232 return pass;
235 bool validate_glx_error_code(int expected_x_error, int expected_glx_error)
237 bool pass = true;
239 if (expected_glx_error == -1
240 && expected_x_error == Success
241 && (glx_error_code != -1 || x_error_code != Success)) {
242 fprintf(stderr,
243 "X error %d (%s (%d)) was generated, but "
244 "no error was expected.\n",
245 x_error_code,
246 piglit_glx_error_string(glx_error_code),
247 glx_error_code);
248 pass = false;
251 if (expected_glx_error != -1
252 && glx_error_code != expected_glx_error) {
253 fprintf(stderr,
254 "X error %d (%s (%d)) was generated, but "
255 "%s (%d) was expected.\n",
256 x_error_code,
257 piglit_glx_error_string(glx_error_code),
258 glx_error_code,
259 piglit_glx_error_string(expected_glx_error),
260 expected_glx_error);
261 pass = false;
262 } else if (expected_x_error != Success
263 && x_error_code != expected_x_error) {
264 fprintf(stderr,
265 "X error %d (%s (%d)) was generated, but "
266 "X error %d was expected.\n",
267 x_error_code,
268 piglit_glx_error_string(glx_error_code),
269 glx_error_code,
270 expected_x_error);
271 pass = false;
274 x_error_code = Success;
275 glx_error_code = -1;
276 return pass;