glsl-1.10: move algebraic tests to glsl-1.10 dir
[piglit.git] / tests / glx / glx-multithread-makecurrent-4.c
blobf8f4da03e1301ef13048f9d358fabe971e79ee6a
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-4.c
26 * Test of issue #3 of GLX_MESA_multithread_makecurrent: Bind one
27 * context into multiple threads, do rendering from both, then destroy
28 * the context in one and keep using it to render from the other.
31 #include <unistd.h>
32 #include "piglit-util-gl.h"
33 #include "piglit-glx-util.h"
34 #include "pthread.h"
36 int piglit_width = 70, piglit_height = 30;
37 static Display *dpy;
38 static Window win;
39 static GLXContext ctx;
40 static pthread_mutex_t mutex;
41 static XVisualInfo *visinfo;
42 static int current_step = 1;
44 static void
45 get_lock_for_step(int step)
47 while (true) {
48 pthread_mutex_lock(&mutex);
49 if (step == current_step) {
50 current_step++;
51 return;
53 pthread_mutex_unlock(&mutex);
54 usleep(1);
58 static void *
59 thread1_func(void *arg)
61 get_lock_for_step(1);
62 glXMakeCurrent(dpy, win, ctx);
63 glColor4f(0.0, 1.0, 0.0, 1.0);
64 piglit_draw_rect(10, 10, 10, 10);
65 pthread_mutex_unlock(&mutex);
67 get_lock_for_step(3);
68 glXMakeCurrent(dpy, None, None);
69 glXDestroyContext(dpy, ctx);
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);
145 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
149 main(int argc, char **argv)
151 int i;
153 for(i = 1; i < argc; ++i) {
154 if (!strcmp(argv[i], "-auto"))
155 piglit_automatic = 1;
156 else
157 fprintf(stderr, "Unknown option: %s\n", argv[i]);
160 dpy = XOpenDisplay(NULL);
161 if (dpy == NULL) {
162 fprintf(stderr, "couldn't open display\n");
163 piglit_report_result(PIGLIT_FAIL);
165 visinfo = piglit_get_glx_visual(dpy);
166 win = piglit_get_glx_window(dpy, visinfo);
168 XMapWindow(dpy, win);
170 piglit_glx_event_loop(dpy, draw);
172 XFree(visinfo);
174 return 0;