1 /* Copyright © 2013, Intel Corporation
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 /* This is C code intended to be included (yes, included) by test
23 * programs in the fips test suite.
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
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).
35 * If COMMON_USE_EGL is not defined, this file provides three
36 * functions for use by the test program:
39 * common_create_glx_context (Display *dpy,
40 * GLXContext *ctx, XVisualInfo **vi);
43 * common_make_current (Display *dpy, GLXContext ctx, Window window)
46 * common_handle_events (Display *dpy, GLXContext ctx, Window window);
48 * If COMMON_USE_EGL is defined, this file instead provides three
52 * common_create_egl_context (Display *dpy, EGLenum api,
53 * EGLDisplay *egl_dpy, EGLContext *ctx,
54 * EGLConfig *config, XVisualInfo **vi);
57 * common_make_current (EGLDisplay egl_dpy, EGLContext ctx,
61 * common_handle_event (Display *dpy, EGLDisplay egl_dpy,
62 * EGLSurface surface);
68 #ifndef COMMON_GL_PREFIX
69 #error Code including common.c must define COMMON_GL_PREFIX
73 #define COMMON_CONTEXT EGLContext
74 #define COMMON_SWAP_DISPLAY EGLDisplay
75 #define COMMON_SWAP_WINDOW EGLSurface
76 #define COMMON_SWAP_FUNCTION eglSwapBuffers
78 #define COMMON_CONTEXT GLXContext
79 #define COMMON_SWAP_DISPLAY Display*
80 #define COMMON_SWAP_WINDOW Window
81 #define COMMON_SWAP_FUNCTION glXSwapBuffers
84 #define concat_(a,b) a ## b
85 #define concat(a,b) concat_(a,b)
86 #define _(func) concat(COMMON_GL_PREFIX, func)
89 set_2d_projection (int width, int height)
91 _(glMatrixMode) (GL_PROJECTION);
93 _(glOrtho) (0, width, height, 0, 0, 1);
94 _(glMatrixMode) (GL_MODELVIEW);
98 paint_rgb_using_clear (double r, double g, double b)
100 _(glClearColor) (r, g, b, 1.0);
101 _(glClear) (GL_COLOR_BUFFER_BIT);
104 #ifdef COMMON_USE_EGL
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)
111 int config_attr[] = {
116 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
117 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
124 int context_attr[] = {
128 XVisualInfo *visual_info, visual_attr;
129 int visualid, num_visuals;
131 egl_dpy = eglGetDisplay (dpy);
133 success = eglInitialize (egl_dpy, &major, &minor);
135 fprintf (stderr, "Error: Failed to initialized EGL\n");
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");
145 success = eglGetConfigAttrib (egl_dpy, config, EGL_NATIVE_VISUAL_ID, &visualid);
147 fprintf (stderr, "Error: Failed to find native Visual ID\n");
151 visual_attr.visualid = visualid;
152 visual_info = XGetVisualInfo (dpy, VisualIDMask, &visual_attr, &num_visuals);
154 fprintf (stderr, "Error: Failed to get XVisualInfo\n");
160 ctx = eglCreateContext (egl_dpy, config, NULL, context_attr);
162 fprintf (stderr, "Error: Failed to create EGL context\n");
166 *egl_dpy_ret = egl_dpy;
168 *config_ret = config;
169 *visual_info_ret = visual_info;
174 common_create_glx_context (Display *dpy,
175 GLXContext *ctx_ret, XVisualInfo **visual_info_ret)
177 int visual_attr[] = {
186 GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
190 *visual_info_ret = _(glXChooseVisual) (dpy, 0, visual_attr);
191 *ctx_ret = _(glXCreateContext) (dpy, *visual_info_ret, NULL, True);
195 #ifdef COMMON_USE_EGL
197 common_make_current (EGLDisplay egl_dpy, EGLContext ctx, EGLSurface surface)
199 _(eglMakeCurrent) (egl_dpy, surface, surface, ctx);
203 common_make_current (Display *dpy, GLXContext ctx, Window window)
205 _(glXMakeCurrent) (dpy, window, ctx);
210 draw (COMMON_SWAP_DISPLAY dpy, COMMON_SWAP_WINDOW window, int width, int height)
213 _(glViewport) (0, 0, width, height);
215 set_2d_projection (width, height);
217 /* Simply count through some colors, frame by frame. */
218 #define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
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);
227 /* Frame: Draw a solid (yellow) frame */
228 paint_rgb_using_clear (RGB(frame));
229 _(COMMON_SWAP_FUNCTION) (dpy, window);
232 /* Frame: Draw a solid (cyan) frame */
233 paint_rgb_using_clear (RGB(frame));
234 _(COMMON_SWAP_FUNCTION) (dpy, window);
240 common_handle_events(Display *dpy, COMMON_SWAP_DISPLAY swap_dpy, COMMON_SWAP_WINDOW window)
246 XNextEvent (dpy, &xev);
249 XNextEvent (dpy, &xev);
251 case ConfigureNotify:
252 width = xev.xconfigure.width;
253 height = xev.xconfigure.height;
256 if (xev.xexpose.count == 0) {
257 draw (swap_dpy, window, width, height);