]> git.cworth.org Git - fips/blob - test/glx-link-call.c
0c5047eb7edc1ebd7789f45bbba401a8cc099824
[fips] / test / glx-link-call.c
1 /* Copyright © 2013, Intel Corporation
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 /* Perform some simple drawing via OpenGL as follows:
23  *
24  *      1. Using GLX to construct an OpenGL context
25  *      2. By directly linking with libGL.so
26  *      3. By directly calling OpenGL functions
27  */
28
29 #define GL_GLEXT_PROTOTYPES
30 #include <GL/gl.h>
31 #include <GL/glx.h>
32
33 #include "util.h"
34
35 static void
36 set_2d_projection (int width, int height)
37 {
38         glMatrixMode (GL_PROJECTION);
39         glLoadIdentity ();
40         glOrtho (0, width, height, 0, 0, 1);
41         glMatrixMode (GL_MODELVIEW);
42 }
43
44 static void
45 paint_rgb_using_clear (double r, double g, double b)
46 {
47         glClearColor(r, g, b, 1.0);
48         glClear(GL_COLOR_BUFFER_BIT);
49 }
50
51 static void
52 draw (Display *dpy, Window window, int width, int height)
53 {
54         int i;
55         int visual_attr[] = {
56                 GLX_RGBA,
57                 GLX_RED_SIZE,           8,
58                 GLX_GREEN_SIZE,         8,
59                 GLX_BLUE_SIZE,          8,
60                 GLX_ALPHA_SIZE,         8,
61                 GLX_DOUBLEBUFFER,
62                 GLX_DEPTH_SIZE,         24,
63                 GLX_STENCIL_SIZE,       8,
64                 GLX_X_VISUAL_TYPE,      GLX_DIRECT_COLOR,
65                 None
66         };
67
68         /* Window and context setup. */
69         XVisualInfo *visual_info = glXChooseVisual(dpy, 0, visual_attr);
70         GLXContext ctx = glXCreateContext(dpy, visual_info, NULL, True);
71         glXMakeCurrent(dpy, window, ctx);
72
73         glViewport(0, 0, width, height);
74
75         set_2d_projection (width, height);
76
77 /* Simply count through some colors, frame by frame. */
78 #define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
79
80         int frame = 0;
81         for (i = 0; i < 2; i++) {
82                 /* Frame: Draw a solid (magenta) frame */
83                 paint_rgb_using_clear (RGB(frame));
84                 glXSwapBuffers (dpy, window);
85                 frame++;
86
87                 /* Frame: Draw a solid (yellow) frame */
88                 paint_rgb_using_clear (RGB(frame));
89                 glXSwapBuffers (dpy, window);
90                 frame++;
91
92                 /* Frame: Draw a solid (cyan) frame */
93                 paint_rgb_using_clear (RGB(frame));
94                 glXSwapBuffers (dpy, window);
95                 frame++;
96         }
97
98         /* Cleanup */
99         glXDestroyContext (dpy, ctx);
100 }
101
102 static void
103 handle_events(Display *dpy, Window window)
104 {
105         XEvent xev;
106         int width = 0;
107         int height = 0;
108
109         XNextEvent (dpy, &xev);
110
111         while (1) {
112                 XNextEvent (dpy, &xev);
113                 switch (xev.type) {
114                 case ConfigureNotify:
115                         width = xev.xconfigure.width;
116                         height = xev.xconfigure.height;
117                         break;
118                 case Expose:
119                         if (xev.xexpose.count == 0) {
120                                 draw (dpy, window, width, height);
121                                 return;
122                         }
123                         break;
124                 }
125         }
126 }
127
128 int
129 main (void)
130 {
131         Display *dpy;
132         Window window;
133
134         util_init_display_window (&dpy, &window);
135
136         handle_events (dpy, window);
137
138         util_fini_display_window (dpy, window);
139
140         return 0;
141 }