ext_transform_feedback: document missing mode in usage
[piglit.git] / tests / glx / glx-multithread-makecurrent-3.c
blob680ee4c06904243122cb3357942550ac058eac88
1 /*
2 * Copyright © 2009-2011 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 /** @file glx-multithread-makecurrent-3.c
26 * Test of issue #2 of GLX_MESA_multithread_makecurrent: Bind one
27 * context into multiple threads, do rendering from both, then make
28 * sure that once we unbind from one that rendering from the second
29 * can continue.
32 #include <unistd.h>
33 #include "piglit-util-gl.h"
34 #include "piglit-glx-util.h"
35 #include "pthread.h"
37 int piglit_width = 70, piglit_height = 30;
38 static Display *dpy;
39 static Window win;
40 static GLXContext ctx;
41 static pthread_mutex_t mutex;
42 static XVisualInfo *visinfo;
43 static int current_step = 1;
45 static void
46 get_lock_for_step(int step)
48 while (true) {
49 pthread_mutex_lock(&mutex);
50 if (step == current_step) {
51 current_step++;
52 return;
54 pthread_mutex_unlock(&mutex);
55 usleep(1);
59 static void *
60 thread1_func(void *arg)
62 get_lock_for_step(1);
63 glXMakeCurrent(dpy, win, ctx);
64 glColor4f(0.0, 1.0, 0.0, 1.0);
65 piglit_draw_rect(10, 10, 10, 10);
66 pthread_mutex_unlock(&mutex);
68 get_lock_for_step(3);
69 glXMakeCurrent(dpy, None, None);
70 pthread_mutex_unlock(&mutex);
72 return NULL;
75 static void *
76 thread2_func(void *arg)
78 get_lock_for_step(2);
79 glXMakeCurrent(dpy, win, ctx);
80 glColor4f(0.0, 1.0, 0.0, 1.0);
81 piglit_draw_rect(30, 10, 10, 10);
82 pthread_mutex_unlock(&mutex);
84 get_lock_for_step(4);
85 piglit_draw_rect(50, 10, 10, 10);
86 pthread_mutex_unlock(&mutex);
88 return NULL;
91 enum piglit_result
92 draw(Display *dpy)
94 GLboolean pass = GL_TRUE;
95 float green[] = {0.0, 1.0, 0.0, 1.0};
96 float gray[] = {0.5, 0.5, 0.5, 1.0};
97 pthread_t thread1, thread2;
98 void *retval;
99 int ret;
101 ctx = piglit_get_glx_context(dpy, visinfo);
102 glXMakeCurrent(dpy, win, ctx);
103 piglit_dispatch_default_init(PIGLIT_DISPATCH_GL);
105 piglit_require_glx_extension(dpy, "GLX_MESA_multithread_makecurrent");
107 /* Clear background to gray */
108 glClearColor(0.5, 0.5, 0.5, 1.0);
109 glClear(GL_COLOR_BUFFER_BIT);
111 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
113 pthread_mutex_init(&mutex, NULL);
115 glFlush();
116 /* Now, spawn some threads that do some drawing, both with this
117 * context
119 pthread_create(&thread1, NULL, thread1_func, NULL);
120 pthread_create(&thread2, NULL, thread2_func, NULL);
122 ret = pthread_join(thread1, &retval);
123 assert(ret == 0);
124 ret = pthread_join(thread2, &retval);
125 assert(ret == 0);
127 pthread_mutex_destroy(&mutex);
128 glFlush();
130 pass &= piglit_probe_rect_rgba( 0, 10, 10, 10, gray);
131 pass &= piglit_probe_rect_rgba(10, 10, 10, 10, green);
132 pass &= piglit_probe_rect_rgba(20, 10, 10, 10, gray);
133 pass &= piglit_probe_rect_rgba(30, 10, 10, 10, green);
134 pass &= piglit_probe_rect_rgba(40, 10, 10, 10, gray);
135 pass &= piglit_probe_rect_rgba(50, 10, 10, 10, green);
136 pass &= piglit_probe_rect_rgba(60, 10, 10, 10, gray);
138 pass &= piglit_probe_rect_rgba(0, 0, piglit_width, 10, gray);
139 pass &= piglit_probe_rect_rgba(0, 20, piglit_width, 10, gray);
141 glXSwapBuffers(dpy, win);
143 glXMakeCurrent(dpy, None, None);
144 glXDestroyContext(dpy, ctx);
146 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
150 main(int argc, char **argv)
152 int i;
154 for(i = 1; i < argc; ++i) {
155 if (!strcmp(argv[i], "-auto"))
156 piglit_automatic = 1;
157 else
158 fprintf(stderr, "Unknown option: %s\n", argv[i]);
161 dpy = XOpenDisplay(NULL);
162 if (dpy == NULL) {
163 fprintf(stderr, "couldn't open display\n");
164 piglit_report_result(PIGLIT_FAIL);
166 visinfo = piglit_get_glx_visual(dpy);
167 win = piglit_get_glx_window(dpy, visinfo);
169 piglit_glx_event_loop(dpy, draw);
171 XFree(visinfo);
173 return 0;