Add more structure constructor tests.
[piglit/hramrach.git] / tests / glx / glx-multithread.c
blob8e9417a188807688cd5feffa93532c4f17e6842c
1 /*
2 * Copyright © 2009 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.
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
28 /** @file glx-multithread.c
30 * Test that rendering two plain colored rectangles in two different threads
31 * to the same GLX window works correctly.
34 #include "piglit-util.h"
35 #include "piglit-glx-util.h"
36 #include "pthread.h"
38 int piglit_width = 50, piglit_height = 50;
39 static Display *dpy;
40 static Window win;
41 static pthread_mutex_t mutex;
42 static XVisualInfo *visinfo;
44 static void *
45 thread_func(void *arg)
47 GLXContext ctx;
48 int *x = arg;
49 Bool ret;
51 pthread_mutex_lock(&mutex);
53 ctx = piglit_get_glx_context(dpy, visinfo);
54 ret = glXMakeCurrent(dpy, win, ctx);
55 assert(ret);
57 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
58 glColor4f(0.0, 1.0, 0.0, 0.0);
59 piglit_draw_rect(*x, 10, 10, 10);
61 glFlush();
62 glXDestroyContext(dpy, ctx);
64 pthread_mutex_unlock(&mutex);
66 return NULL;
69 enum piglit_result
70 draw(Display *dpy)
72 GLboolean pass = GL_TRUE;
73 float green[] = {0.0, 1.0, 0.0, 0.0};
74 pthread_t thread1, thread2;
75 void *retval;
76 int ret;
77 int x1 = 10, x2 = 30;
78 GLXContext ctx;
80 ctx = piglit_get_glx_context(dpy, visinfo);
81 glXMakeCurrent(dpy, win, ctx);
83 /* Clear background to gray */
84 glClearColor(0.5, 0.5, 0.5, 1.0);
85 glClear(GL_COLOR_BUFFER_BIT);
86 glFlush();
88 pthread_mutex_init(&mutex, NULL);
89 /* Now, spawn some threads that do some drawing, both with this
90 * context
92 pthread_create(&thread1, NULL, thread_func, &x1);
93 pthread_create(&thread2, NULL, thread_func, &x2);
95 ret = pthread_join(thread1, &retval);
96 assert(ret == 0);
97 ret = pthread_join(thread2, &retval);
98 assert(ret == 0);
100 pthread_mutex_destroy(&mutex);
102 pass &= piglit_probe_pixel_rgb(15, 15, green);
103 pass &= piglit_probe_pixel_rgb(35, 15, green);
105 glXSwapBuffers(dpy, win);
107 return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
111 main(int argc, char **argv)
113 int i;
115 for(i = 1; i < argc; ++i) {
116 if (!strcmp(argv[i], "-auto"))
117 piglit_automatic = 1;
118 else
119 fprintf(stderr, "Unknown option: %s\n", argv[i]);
122 dpy = XOpenDisplay(NULL);
123 if (dpy == NULL) {
124 fprintf(stderr, "couldn't open display\n");
125 piglit_report_result(PIGLIT_FAILURE);
127 visinfo = piglit_get_glx_visual(dpy);
128 win = piglit_get_glx_window(dpy, visinfo);
130 XMapWindow(dpy, win);
132 piglit_glx_event_loop(dpy, draw);
134 return 0;