]> git.cworth.org Git - fips/blob - test/common.c
1eecc18dda88b3bf968505a441acb8c674ab5e5f
[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 #ifndef COMMON_EGL_PREFIX
74 #error Code including common.c with COMMON_USE_EGL must define COMMON_EGL_PREFIX
75 #endif
76 #define COMMON_CONTEXT EGLContext
77 #define COMMON_SWAP_DISPLAY EGLDisplay
78 #define COMMON_SWAP_WINDOW EGLSurface
79 #define COMMON_SWAP_FUNCTION _E(eglSwapBuffers)
80 #else
81 #define COMMON_CONTEXT GLXContext
82 #define COMMON_SWAP_DISPLAY Display*
83 #define COMMON_SWAP_WINDOW Window
84 #define COMMON_SWAP_FUNCTION _(glXSwapBuffers)
85 #endif
86
87 #define concat_(a,b) a ## b
88 #define concat(a,b) concat_(a,b)
89 #define _(func) concat(COMMON_GL_PREFIX, func)
90 #define _E(func) concat(COMMON_EGL_PREFIX, func)
91
92 static void
93 set_2d_projection (int width, int height)
94 {
95         _(glMatrixMode) (GL_PROJECTION);
96         _(glLoadIdentity) ();
97         _(glOrtho) (0, width, height, 0, 0, 1);
98         _(glMatrixMode) (GL_MODELVIEW);
99 }
100
101 static void
102 paint_rgb_using_clear (double r, double g, double b)
103 {
104         _(glClearColor) (r, g, b, 1.0);
105         _(glClear) (GL_COLOR_BUFFER_BIT);
106 }
107
108 #ifdef COMMON_USE_EGL
109 static void
110 common_create_egl_context (Display *dpy, EGLenum api, EGLDisplay *egl_dpy_ret,
111                            EGLContext *ctx_ret, EGLConfig *config_ret,
112                            XVisualInfo **visual_info_ret)
113 {
114         EGLDisplay egl_dpy;
115         int config_attr[] = {
116                 EGL_RED_SIZE,           8,
117                 EGL_GREEN_SIZE,         8,
118                 EGL_BLUE_SIZE,          8,
119                 EGL_ALPHA_SIZE,         8,
120                 EGL_SURFACE_TYPE,       EGL_WINDOW_BIT,
121                 EGL_RENDERABLE_TYPE,    EGL_OPENGL_BIT,
122                 EGL_NONE
123         };
124         EGLConfig config;
125         int num_configs;
126         EGLint major, minor;
127         EGLBoolean success;
128         int context_attr[] = {
129                 EGL_NONE
130         };
131         EGLContext ctx;
132         XVisualInfo *visual_info, visual_attr;
133         int visualid, num_visuals;
134
135         egl_dpy = _E(eglGetDisplay) (dpy);
136
137         success = _E(eglInitialize) (egl_dpy, &major, &minor);
138         if (!success) {
139                 fprintf (stderr, "Error: Failed to initialized EGL\n");
140                 exit (1);
141         }
142
143         success = _E(eglChooseConfig) (egl_dpy, config_attr, &config, 1, &num_configs);
144         if (!success || num_configs == 0) {
145                 fprintf (stderr, "Error: Failed to find EGL config\n");
146                 exit (1);
147         }
148
149         success = _E(eglGetConfigAttrib) (egl_dpy, config, EGL_NATIVE_VISUAL_ID, &visualid);
150         if (!success) {
151                 fprintf (stderr, "Error: Failed to find native Visual ID\n");
152                 exit (1);
153         }
154
155         visual_attr.visualid = visualid;
156         visual_info = XGetVisualInfo (dpy, VisualIDMask, &visual_attr, &num_visuals);
157         if (!visual_info) {
158                 fprintf (stderr, "Error: Failed to get XVisualInfo\n");
159                 exit (1);
160         }
161
162         _E(eglBindAPI) (api);
163
164         ctx = _E(eglCreateContext) (egl_dpy, config, NULL, context_attr);
165         if (!ctx) {
166                 fprintf (stderr, "Error: Failed to create EGL context\n");
167                 exit (1);
168         }
169
170         *egl_dpy_ret = egl_dpy;
171         *ctx_ret = ctx;
172         *config_ret = config;
173         *visual_info_ret = visual_info;
174         
175 }
176 #else
177 static void
178 common_create_glx_context (Display *dpy,
179                            GLXContext *ctx_ret, XVisualInfo **visual_info_ret)
180 {
181         int visual_attr[] = {
182                 GLX_RGBA,
183                 GLX_RED_SIZE,           8,
184                 GLX_GREEN_SIZE,         8,
185                 GLX_BLUE_SIZE,          8,
186                 GLX_ALPHA_SIZE,         8,
187                 GLX_DOUBLEBUFFER,
188                 GLX_DEPTH_SIZE,         24,
189                 GLX_STENCIL_SIZE,       8,
190                 GLX_X_VISUAL_TYPE,      GLX_DIRECT_COLOR,
191                 None
192         };
193
194         *visual_info_ret = _(glXChooseVisual) (dpy, 0, visual_attr);
195         *ctx_ret = _(glXCreateContext) (dpy, *visual_info_ret, NULL, True);
196 }
197 #endif
198
199 #ifdef COMMON_USE_EGL
200 static void
201 common_make_current (EGLDisplay egl_dpy, EGLContext ctx, EGLSurface surface)
202 {
203         _E(eglMakeCurrent) (egl_dpy, surface, surface, ctx);
204 }
205 #else
206 static void
207 common_make_current (Display *dpy, GLXContext ctx, Window window)
208 {
209         _(glXMakeCurrent) (dpy, window, ctx);
210 }
211 #endif
212
213 static void
214 draw (COMMON_SWAP_DISPLAY dpy, COMMON_SWAP_WINDOW window, int width, int height)
215 {
216         int i;
217         _(glViewport) (0, 0, width, height);
218
219         set_2d_projection (width, height);
220
221 /* Simply count through some colors, frame by frame. */
222 #define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
223
224         int frame = 0;
225         for (i = 0; i < 2; i++) {
226                 /* Frame: Draw a solid (magenta) frame */
227                 paint_rgb_using_clear (RGB(frame));
228                 COMMON_SWAP_FUNCTION (dpy, window);
229                 frame++;
230
231                 /* Frame: Draw a solid (yellow) frame */
232                 paint_rgb_using_clear (RGB(frame));
233                 COMMON_SWAP_FUNCTION (dpy, window);
234                 frame++;
235
236                 /* Frame: Draw a solid (cyan) frame */
237                 paint_rgb_using_clear (RGB(frame));
238                 COMMON_SWAP_FUNCTION (dpy, window);
239                 frame++;
240         }
241 }
242
243 static void
244 common_handle_events(Display *dpy, COMMON_SWAP_DISPLAY swap_dpy, COMMON_SWAP_WINDOW window)
245 {
246         XEvent xev;
247         int width = 0;
248         int height = 0;
249
250         XNextEvent (dpy, &xev);
251
252         while (1) {
253                 XNextEvent (dpy, &xev);
254                 switch (xev.type) {
255                 case ConfigureNotify:
256                         width = xev.xconfigure.width;
257                         height = xev.xconfigure.height;
258                         break;
259                 case Expose:
260                         if (xev.xexpose.count == 0) {
261                                 draw (swap_dpy, window, width, height);
262                                 return;
263                         }
264                         break;
265                 }
266         }
267 }