README: recommend Ninja by default and switch to cmake --build
[piglit.git] / tests / glx / glx-multi-context-single-window.c
blob3c23ad0f6490b0dbf0edcfadc4f65858b78fc6a0
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.
24 /** @file glx-multi-context-single-window.c
26 * Exercise rendering to a single window with multiple contexts.
29 #include <unistd.h>
30 #include "piglit-util-gl.h"
31 #include "piglit-glx-util.h"
33 #define MAX_CONTEXTS 8
35 static GLXContext ctx[MAX_CONTEXTS];
36 static int num_contexts = MAX_CONTEXTS;
37 static Window win;
38 static Display *dpy;
40 static const float colors[MAX_CONTEXTS][4] = {
41 {1, 0, 0, 1},
42 {0, 1, 0, 1},
43 {0, 0, 1, 1},
44 {0, 1, 1, 1},
45 {1, 0, 1, 1},
46 {1, 1, 0, 1},
47 {1, 1, 1, 1},
48 {.5, .5, .5, 1},
51 int piglit_width = 500, piglit_height = 500;
53 static int rect_size = 40;
56 static int
57 rect_pos(int i)
59 return i * rect_size / 2;
63 enum piglit_result
64 draw(Display *dpy)
66 int i;
67 bool pass = true;
69 /* draw a series of colored quads, one per context, at increasing
70 * Z distance.
72 for (i = 0; i < num_contexts; i++) {
73 glXMakeCurrent(dpy, win, ctx[i]);
75 if (i == 0) {
76 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
79 glEnable(GL_DEPTH_TEST);
80 glMatrixMode(GL_PROJECTION);
81 glLoadIdentity();
82 glOrtho(0, piglit_width, 0, piglit_height, 0, 1);
84 glMatrixMode(GL_MODELVIEW);
85 glLoadIdentity();
87 glPushMatrix();
88 float p = rect_pos(i);
89 float z = -i / 10.0;
90 glTranslatef(p, p, z);
92 glColor4fv(colors[i]);
93 piglit_draw_rect(0, 0, rect_size, rect_size);
95 glPopMatrix();
98 /* probe rendering */
99 glXMakeCurrent(dpy, win, ctx[0]);
100 for (i = 0; i < num_contexts; i++) {
101 int x = rect_pos(i) + rect_size * 3 / 4;
102 int p = piglit_probe_pixel_rgb(x, x, colors[i]);
104 if (!p) {
105 printf("Failed probe for rect/context %d\n", i);
106 pass = false;
110 glXSwapBuffers(dpy, win);
112 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
117 main(int argc, char **argv)
119 XVisualInfo *visinfo;
120 int i;
122 for (i = 1; i < argc; ++i) {
123 if (!strcmp(argv[i], "-auto"))
124 piglit_automatic = 1;
125 else
126 fprintf(stderr, "Unknown option: %s\n", argv[i]);
129 dpy = XOpenDisplay(NULL);
130 if (dpy == NULL) {
131 fprintf(stderr, "couldn't open display\n");
132 piglit_report_result(PIGLIT_FAIL);
134 visinfo = piglit_get_glx_visual(dpy);
136 win = piglit_get_glx_window(dpy, visinfo);
138 for (i = 0; i < num_contexts; i++) {
139 ctx[i] = piglit_get_glx_context(dpy, visinfo);
142 glXMakeCurrent(dpy, win, ctx[0]);
143 piglit_dispatch_default_init(PIGLIT_DISPATCH_GL);
145 piglit_glx_event_loop(dpy, draw);
147 XFree(visinfo);
148 glXDestroyWindow(dpy, win);
149 for (i = 0; i < num_contexts; i++) {
150 glXDestroyContext(dpy, ctx[i]);
153 return 0;