]> git.cworth.org Git - apitrace-tests/blob - cli/src/few-side-effects.c
Add --exact, --no-deps, or --no-prune to trim tests as needed
[apitrace-tests] / cli / src / few-side-effects.c
1 /**************************************************************************
2  * Copyright 2012 Intel corporation
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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 THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE.
22  *
23  **************************************************************************/
24
25 #include <stdio.h>
26
27 #include <X11/Xlib.h>
28 #include <GL/glew.h>
29 #include <GL/gl.h>
30 #include <GL/glx.h>
31
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 int width = 64;
38 int height = 64;
39
40 #ifdef __GNUC__
41 #define UNUSED __attribute__((unused))
42 #else
43 #define UNUSED
44 #endif
45
46 static void
47 set_2d_projection (void)
48 {
49         glMatrixMode (GL_PROJECTION);
50         glLoadIdentity ();
51         glOrtho (0, width, height, 0, 0, 1);
52         glMatrixMode (GL_MODELVIEW);
53 }
54
55 static void
56 draw (Display *dpy, Window window, int width, int height)
57 {
58         GLenum glew_err;
59         int visual_attr[] = {
60                 GLX_RGBA,
61                 GLX_RED_SIZE,           8,
62                 GLX_GREEN_SIZE,         8,
63                 GLX_BLUE_SIZE,          8,
64                 GLX_ALPHA_SIZE,         8,
65                 GLX_DOUBLEBUFFER,
66                 GLX_DEPTH_SIZE,         24,
67                 GLX_STENCIL_SIZE,       8,
68                 GLX_X_VISUAL_TYPE,      GLX_DIRECT_COLOR,
69                 None
70         };
71
72         /* Window and context setup. */
73         XVisualInfo *visual_info = glXChooseVisual(dpy, 0, visual_attr);
74         GLXContext ctx = glXCreateContext(dpy, visual_info, NULL, True);
75         glXMakeCurrent(dpy, window, ctx);
76
77         glew_err = glewInit();
78         if (glew_err != GLEW_OK)
79         {
80                 fprintf (stderr, "glewInit failed: %s\n",
81                          glewGetErrorString(glew_err));
82                 exit (1);
83         }
84
85         glViewport(0, 0, width, height);
86
87         set_2d_projection ();
88
89         /* Frame 1. Perform a few operations, most without side effects. */
90         {
91                 GLint line_width UNUSED, logic_op UNUSED;
92                 GLboolean blend_enabled UNUSED;
93
94                 glGetIntegerv(GL_LINE_WIDTH, &line_width);
95                 glGetIntegerv(GL_LOGIC_OP_MODE, &logic_op);
96
97                 blend_enabled = glIsEnabled(GL_BLEND);
98
99                 glEnable(GL_BLEND);
100
101                 blend_enabled = glIsEnabled(GL_BLEND);
102
103                 glDisable(GL_BLEND);
104         }
105         
106         glXSwapBuffers (dpy, window);
107         /* Frame 2. Again, more operations, most with side effects. */
108         {
109                 GLint line_width UNUSED, logic_op UNUSED;
110                 GLboolean blend_enabled UNUSED;
111
112                 glGetIntegerv(GL_LINE_WIDTH, &line_width);
113                 glGetIntegerv(GL_LOGIC_OP_MODE, &logic_op);
114
115                 blend_enabled = glIsEnabled(GL_BLEND);
116
117                 glEnable(GL_BLEND);
118
119                 blend_enabled = glIsEnabled(GL_BLEND);
120
121                 glDisable(GL_BLEND);
122         }
123
124         /* Cleanup */
125         glXDestroyContext (dpy, ctx);
126 }
127
128 static void
129 handle_events(Display *dpy, Window window, int width, int height)
130 {
131         XEvent xev;
132         KeyCode quit_code = XKeysymToKeycode (dpy, XStringToKeysym("Q"));
133
134         XNextEvent (dpy, &xev);
135
136         while (1) {
137                 XNextEvent (dpy, &xev);
138                 switch (xev.type) {
139                 case KeyPress:
140                         if (xev.xkey.keycode == quit_code) {
141                                 return;
142                         }
143                         break;
144                 case ConfigureNotify:
145                         width = xev.xconfigure.width;
146                         height = xev.xconfigure.height;
147                         break;
148                 case Expose:
149                         if (xev.xexpose.count == 0) {
150                                 draw (dpy, window, width, height);
151                                 return;
152                         }
153                         break;
154                 }
155         }
156 }
157
158 int
159 main (void)
160 {
161         Display *dpy;
162         Window window;
163
164         dpy = XOpenDisplay (NULL);
165
166         if (dpy == NULL) {
167                 fprintf(stderr, "Failed to open display %s\n",
168                         XDisplayName(NULL));
169                 return 1;
170         }
171
172         window = XCreateSimpleWindow(dpy, DefaultRootWindow (dpy),
173                                      0, 0, width, height, 0,
174                                      BlackPixel (dpy, DefaultScreen (dpy)),
175                                      BlackPixel (dpy, DefaultScreen (dpy)));
176
177         XSelectInput(dpy, window,
178                      KeyPressMask | StructureNotifyMask | ExposureMask);
179
180         XMapWindow (dpy, window);
181
182         handle_events (dpy, window, width, height);
183
184         XDestroyWindow (dpy, window);
185         XCloseDisplay (dpy);
186
187         return 0;
188 }