ovr_multiview: add some basic glsl tests
[piglit.git] / tests / spec / glx_ext_import_context / common.c
blob35fe955512225f66cfee9bb582b2b0f106cdb042
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 printf("Test requires GLX_EXT_import_context.\n");
102 piglit_report_result(PIGLIT_SKIP);
104 } else {
105 piglit_require_glx_extension(dpy, "GLX_EXT_import_context");
108 piglit_glx_get_all_proc_addresses(procedures, ARRAY_SIZE(procedures));
110 visinfo = piglit_get_glx_visual(dpy);
112 directCtx = glXCreateContext(dpy, visinfo, NULL, True);
113 if (directCtx == NULL) {
114 fprintf(stderr,
115 "Could not create initial direct-rendering context.\n");
116 piglit_report_result(PIGLIT_FAIL);
119 if (!glXIsDirect(dpy, directCtx)) {
120 glXDestroyContext(dpy, directCtx);
121 directCtx = NULL;
124 indirectCtx = glXCreateContext(dpy, visinfo, NULL, False);
125 if (indirectCtx == NULL) {
126 fprintf(stderr,
127 "Could not create initial indirect-rendering "
128 "context.\n");
129 piglit_report_result(PIGLIT_FAIL);
132 piglit_glx_get_error(dpy, NULL);
133 old_handler = XSetErrorHandler(x_error_handler);
136 void GLX_EXT_import_context_teardown(void)
138 if (directCtx != NULL) {
139 glXDestroyContext(dpy, directCtx);
140 directCtx = NULL;
143 if (indirectCtx != NULL) {
144 glXDestroyContext(dpy, indirectCtx);
145 indirectCtx = NULL;
148 XFree(visinfo);
149 visinfo = NULL;
151 XSetErrorHandler(old_handler);
154 void get_context_IDs(void)
156 directID = None;
157 if (directCtx != NULL) {
158 directID = glXGetContextIDEXT(directCtx);
161 indirectID = None;
162 if (indirectCtx != NULL) {
163 indirectID = glXGetContextIDEXT(indirectCtx);
167 const char *context_mode_name(enum context_mode mode)
169 switch (mode) {
170 case direct_rendering:
171 return "direct-rendering";
172 case indirect_rendering:
173 return "indirect-rendering";
174 case invalid:
175 return "invalid";
176 default:
177 assert(!"Should not get here.");
178 piglit_report_result(PIGLIT_FAIL);
181 return "";
184 bool try_import_context(GLXContextID id, enum context_mode mode)
186 const int expected_glx_error = (mode == invalid) ? GLXBadContext : -1;
187 bool pass = true;
188 GLXContext ctx = glXImportContextEXT(dpy, id);
190 XSync(dpy, 0);
192 switch (mode) {
193 case direct_rendering:
194 if (ctx != NULL) {
195 fprintf(stderr,
196 "Could import direct-rendering context, "
197 "but should have failed.\n");
198 pass = false;
200 break;
202 case indirect_rendering:
203 if (ctx == NULL) {
204 fprintf(stderr,
205 "Could not import indirect-rendering context, "
206 "but should have succeeded.\n");
207 pass = false;
209 break;
211 case invalid:
212 if (ctx != NULL) {
213 fprintf(stderr,
214 "Could import invalid context (0x%08x), "
215 "but should have failed.\n",
216 (int) id);
217 pass = false;
219 break;
222 assert(mode != invalid || expected_glx_error != -1);
223 pass = validate_glx_error_code(Success, expected_glx_error) && pass;
225 if (!pass)
226 fprintf(stderr, "Context ID = 0x%08x.\n", (int) id);
228 if (ctx != NULL)
229 glXFreeContextEXT(dpy, ctx);
231 return pass;
234 bool validate_glx_error_code(int expected_x_error, int expected_glx_error)
236 bool pass = true;
238 if (expected_glx_error == -1
239 && expected_x_error == Success
240 && (glx_error_code != -1 || x_error_code != Success)) {
241 fprintf(stderr,
242 "X error %d (%s (%d)) was generated, but "
243 "no error was expected.\n",
244 x_error_code,
245 piglit_glx_error_string(glx_error_code),
246 glx_error_code);
247 pass = false;
250 if (expected_glx_error != -1
251 && glx_error_code != expected_glx_error) {
252 fprintf(stderr,
253 "X error %d (%s (%d)) was generated, but "
254 "%s (%d) was expected.\n",
255 x_error_code,
256 piglit_glx_error_string(glx_error_code),
257 glx_error_code,
258 piglit_glx_error_string(expected_glx_error),
259 expected_glx_error);
260 pass = false;
261 } else if (expected_x_error != Success
262 && x_error_code != expected_x_error) {
263 fprintf(stderr,
264 "X error %d (%s (%d)) was generated, but "
265 "X error %d was expected.\n",
266 x_error_code,
267 piglit_glx_error_string(glx_error_code),
268 glx_error_code,
269 expected_x_error);
270 pass = false;
273 x_error_code = Success;
274 glx_error_code = -1;
275 return pass;