]> git.cworth.org Git - fips/blob - test/common.c
e4d81f81ba5afc6599d6502159bf458ff955337f
[fips] / test / common.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 /* This is C code intended to be included (yes, included) by test
23  * programs in the fips test suite.
24  *
25  * Before including this file, the test program must define a macro
26  * COMMON_GL_PREFIX to some prefix. This macro can be empty (to
27  * directly call functions in an OpenGL-library directly linked) or
28  * can be some custom prefix to call through symbols defined in the
29  * test program.
30  *
31  * Optionally, the test program may define the macro COMMON_USE_EGL to
32  * instruct the common code to use EGL (rather than GLX which it will
33  * use by default if COMMON_USE_EGL is not defined).
34  *
35  * If COMMON_USE_EGL is not defined, this file provides three
36  * functions for use by the test program:
37  *
38  *      static void
39  *      common_create_glx_context (Display *dpy,
40  *                                 GLXContext *ctx, XVisualInfo **vi);
41  *
42  *      static void
43  *      common_make_current (Display *dpy, GLXContext ctx, Window window)
44  *
45  *      static void
46  *      common_handle_events (Display *dpy, GLXContext ctx, Window window);
47  *
48  * If COMMON_USE_EGL is defined, this file instead provides three
49  * similar functions:
50  *
51  *      static void
52  *      common_create_egl_context (Display *dpy, EGLenum api,
53  *                                 EGLDisplay *egl_dpy, EGLContext *ctx,
54  *                                 EGLConfig *config, XVisualInfo **vi);
55  *
56  *      static void
57  *      common_make_current (EGLDisplay egl_dpy, EGLContext ctx,
58  *                           EGLSurface surface)
59  *
60  *      static void
61  *      common_handle_event (Display *dpy, EGLDisplay egl_dpy,
62  *                           EGLSurface surface);
63  */
64
65 #include <stdio.h>
66 #include <stdlib.h>
67
68 #ifndef COMMON_GL_PREFIX
69 #error Code including common.c must define COMMON_GL_PREFIX
70 #endif
71
72 #ifdef COMMON_USE_EGL
73 #define COMMON_CONTEXT EGLContext
74 #define COMMON_SWAP_DISPLAY EGLDisplay
75 #define COMMON_SWAP_WINDOW EGLSurface
76 #define COMMON_SWAP_FUNCTION eglSwapBuffers
77 #else
78 #define COMMON_CONTEXT GLXContext
79 #define COMMON_SWAP_DISPLAY Display*
80 #define COMMON_SWAP_WINDOW Window
81 #define COMMON_SWAP_FUNCTION glXSwapBuffers
82 #endif
83
84 #define concat_(a,b) a ## b
85 #define concat(a,b) concat_(a,b)
86 #define _(func) concat(COMMON_GL_PREFIX, func)
87
88 static void
89 set_2d_projection (int width, int height)
90 {
91         _(glMatrixMode) (GL_PROJECTION);
92         _(glLoadIdentity) ();
93         _(glOrtho) (0, width, height, 0, 0, 1);
94         _(glMatrixMode) (GL_MODELVIEW);
95 }
96
97 static void
98 paint_rgb_using_clear (double r, double g, double b)
99 {
100         _(glClearColor) (r, g, b, 1.0);
101         _(glClear) (GL_COLOR_BUFFER_BIT);
102 }
103
104 #ifdef COMMON_USE_EGL
105 static void
106 common_create_egl_context (Display *dpy, EGLenum api, EGLDisplay *egl_dpy_ret,
107                            EGLContext *ctx_ret, EGLConfig *config_ret,
108                            XVisualInfo **visual_info_ret)
109 {
110         EGLDisplay egl_dpy;
111         int config_attr[] = {
112                 EGL_RED_SIZE,           8,
113                 EGL_GREEN_SIZE,         8,
114                 EGL_BLUE_SIZE,          8,
115                 EGL_ALPHA_SIZE,         8,
116                 EGL_SURFACE_TYPE,       EGL_WINDOW_BIT,
117                 EGL_RENDERABLE_TYPE,    EGL_OPENGL_BIT,
118                 EGL_NONE
119         };
120         EGLConfig config;
121         int num_configs;
122         EGLint major, minor;
123         EGLBoolean success;
124         int context_attr[] = {
125                 EGL_NONE
126         };
127         EGLContext ctx;
128         XVisualInfo *visual_info, visual_attr;
129         int visualid, num_visuals;
130
131         egl_dpy = eglGetDisplay (dpy);
132
133         success = eglInitialize (egl_dpy, &major, &minor);
134         if (!success) {
135                 fprintf (stderr, "Error: Failed to initialized EGL\n");
136                 exit (1);
137         }
138
139         success = eglChooseConfig (egl_dpy, config_attr, &config, 1, &num_configs);
140         if (!success || num_configs == 0) {
141                 fprintf (stderr, "Error: Failed to find EGL config\n");
142                 exit (1);
143         }
144
145         success = eglGetConfigAttrib (egl_dpy, config, EGL_NATIVE_VISUAL_ID, &visualid);
146         if (!success) {
147                 fprintf (stderr, "Error: Failed to find native Visual ID\n");
148                 exit (1);
149         }
150
151         visual_attr.visualid = visualid;
152         visual_info = XGetVisualInfo (dpy, VisualIDMask, &visual_attr, &num_visuals);
153         if (!visual_info) {
154                 fprintf (stderr, "Error: Failed to get XVisualInfo\n");
155                 exit (1);
156         }
157
158         eglBindAPI (api);
159
160         ctx = eglCreateContext (egl_dpy, config, NULL, context_attr);
161         if (!ctx) {
162                 fprintf (stderr, "Error: Failed to create EGL context\n");
163                 exit (1);
164         }
165
166         *egl_dpy_ret = egl_dpy;
167         *ctx_ret = ctx;
168         *config_ret = config;
169         *visual_info_ret = visual_info;
170         
171 }
172 #else
173 static void
174 common_create_glx_context (Display *dpy,
175                            GLXContext *ctx_ret, XVisualInfo **visual_info_ret)
176 {
177         int visual_attr[] = {
178                 GLX_RGBA,
179                 GLX_RED_SIZE,           8,
180                 GLX_GREEN_SIZE,         8,
181                 GLX_BLUE_SIZE,          8,
182                 GLX_ALPHA_SIZE,         8,
183                 GLX_DOUBLEBUFFER,
184                 GLX_DEPTH_SIZE,         24,
185                 GLX_STENCIL_SIZE,       8,
186                 GLX_X_VISUAL_TYPE,      GLX_DIRECT_COLOR,
187                 None
188         };
189
190         *visual_info_ret = _(glXChooseVisual) (dpy, 0, visual_attr);
191         *ctx_ret = _(glXCreateContext) (dpy, *visual_info_ret, NULL, True);
192 }
193 #endif
194
195 #ifdef COMMON_USE_EGL
196 static void
197 common_make_current (EGLDisplay egl_dpy, EGLContext ctx, EGLSurface surface)
198 {
199         _(eglMakeCurrent) (egl_dpy, surface, surface, ctx);
200 }
201 #else
202 static void
203 common_make_current (Display *dpy, GLXContext ctx, Window window)
204 {
205         _(glXMakeCurrent) (dpy, window, ctx);
206 }
207 #endif
208
209 static void
210 draw (COMMON_SWAP_DISPLAY dpy, COMMON_SWAP_WINDOW window, int width, int height)
211 {
212         int i;
213         _(glViewport) (0, 0, width, height);
214
215         set_2d_projection (width, height);
216
217 /* Simply count through some colors, frame by frame. */
218 #define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
219
220         int frame = 0;
221         for (i = 0; i < 2; i++) {
222                 /* Frame: Draw a solid (magenta) frame */
223                 paint_rgb_using_clear (RGB(frame));
224                 _(COMMON_SWAP_FUNCTION) (dpy, window);
225                 frame++;
226
227                 /* Frame: Draw a solid (yellow) frame */
228                 paint_rgb_using_clear (RGB(frame));
229                 _(COMMON_SWAP_FUNCTION) (dpy, window);
230                 frame++;
231
232                 /* Frame: Draw a solid (cyan) frame */
233                 paint_rgb_using_clear (RGB(frame));
234                 _(COMMON_SWAP_FUNCTION) (dpy, window);
235                 frame++;
236         }
237 }
238
239 static void
240 common_handle_events(Display *dpy, COMMON_SWAP_DISPLAY swap_dpy, COMMON_SWAP_WINDOW window)
241 {
242         XEvent xev;
243         int width = 0;
244         int height = 0;
245
246         XNextEvent (dpy, &xev);
247
248         while (1) {
249                 XNextEvent (dpy, &xev);
250                 switch (xev.type) {
251                 case ConfigureNotify:
252                         width = xev.xconfigure.width;
253                         height = xev.xconfigure.height;
254                         break;
255                 case Expose:
256                         if (xev.xexpose.count == 0) {
257                                 draw (swap_dpy, window, width, height);
258                                 return;
259                         }
260                         break;
261                 }
262         }
263 }