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 #ifndef COMMON_EGL_PREFIX
74 #error Code including common.c with COMMON_USE_EGL must define COMMON_EGL_PREFIX
76 #define COMMON_CONTEXT EGLContext
77 #define COMMON_SWAP_DISPLAY EGLDisplay
78 #define COMMON_SWAP_WINDOW EGLSurface
79 #define COMMON_SWAP_FUNCTION _E(eglSwapBuffers)
81 #define COMMON_CONTEXT GLXContext
82 #define COMMON_SWAP_DISPLAY Display*
83 #define COMMON_SWAP_WINDOW Window
84 #define COMMON_SWAP_FUNCTION _(glXSwapBuffers)
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)
93 paint_rgb_using_clear (double r, double g, double b)
95 _(glClearColor) (r, g, b, 1.0);
96 _(glClear) (GL_COLOR_BUFFER_BIT);
101 common_create_egl_context (Display *dpy, EGLenum api, EGLDisplay *egl_dpy_ret,
102 EGLContext *ctx_ret, EGLConfig *config_ret,
103 XVisualInfo **visual_info_ret)
106 int config_attr[] = {
111 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
112 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
119 int opengl_context_attr[] = {
122 int glesv2_context_attr[] = {
123 EGL_CONTEXT_CLIENT_VERSION, 2,
128 XVisualInfo *visual_info, visual_attr;
129 int visualid, num_visuals;
131 egl_dpy = _E(eglGetDisplay) (dpy);
133 success = _E(eglInitialize) (egl_dpy, &major, &minor);
135 fprintf (stderr, "Error: Failed to initialized EGL\n");
139 success = _E(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 = _E(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");
158 _E(eglBindAPI) (api);
160 if (api == EGL_OPENGL_ES_API)
161 context_attr = glesv2_context_attr;
163 context_attr = opengl_context_attr;
164 ctx = _E(eglCreateContext) (egl_dpy, config, NULL, context_attr);
166 fprintf (stderr, "Error: Failed to create EGL context\n");
170 *egl_dpy_ret = egl_dpy;
172 *config_ret = config;
173 *visual_info_ret = visual_info;
178 common_create_glx_context (Display *dpy,
179 GLXContext *ctx_ret, XVisualInfo **visual_info_ret)
181 int visual_attr[] = {
190 GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
194 *visual_info_ret = _(glXChooseVisual) (dpy, 0, visual_attr);
195 *ctx_ret = _(glXCreateContext) (dpy, *visual_info_ret, NULL, True);
199 #ifdef COMMON_USE_EGL
201 common_make_current (EGLDisplay egl_dpy, EGLContext ctx, EGLSurface surface)
203 _E(eglMakeCurrent) (egl_dpy, surface, surface, ctx);
207 common_make_current (Display *dpy, GLXContext ctx, Window window)
209 _(glXMakeCurrent) (dpy, window, ctx);
214 draw (COMMON_SWAP_DISPLAY dpy, COMMON_SWAP_WINDOW window, int width, int height)
217 _(glViewport) (0, 0, width, height);
219 /* Simply count through some colors, frame by frame. */
220 #define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
223 for (i = 0; i < 2; i++) {
224 /* Frame: Draw a solid (magenta) frame */
225 paint_rgb_using_clear (RGB(frame));
226 COMMON_SWAP_FUNCTION (dpy, window);
229 /* Frame: Draw a solid (yellow) frame */
230 paint_rgb_using_clear (RGB(frame));
231 COMMON_SWAP_FUNCTION (dpy, window);
234 /* Frame: Draw a solid (cyan) frame */
235 paint_rgb_using_clear (RGB(frame));
236 COMMON_SWAP_FUNCTION (dpy, window);
242 common_handle_events(Display *dpy, COMMON_SWAP_DISPLAY swap_dpy, COMMON_SWAP_WINDOW window)
248 XNextEvent (dpy, &xev);
251 XNextEvent (dpy, &xev);
253 case ConfigureNotify:
254 width = xev.xconfigure.width;
255 height = xev.xconfigure.height;
258 if (xev.xexpose.count == 0) {
259 draw (swap_dpy, window, width, height);