]> git.cworth.org Git - fips/blob - test/common.c
test: Add 4 tests using EGL and OpenGLESv2
[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 paint_rgb_using_clear (double r, double g, double b)
94 {
95         _(glClearColor) (r, g, b, 1.0);
96         _(glClear) (GL_COLOR_BUFFER_BIT);
97 }
98
99 #ifdef COMMON_USE_EGL
100 static void
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)
104 {
105         EGLDisplay egl_dpy;
106         int config_attr[] = {
107                 EGL_RED_SIZE,           8,
108                 EGL_GREEN_SIZE,         8,
109                 EGL_BLUE_SIZE,          8,
110                 EGL_ALPHA_SIZE,         8,
111                 EGL_SURFACE_TYPE,       EGL_WINDOW_BIT,
112                 EGL_RENDERABLE_TYPE,    EGL_OPENGL_BIT,
113                 EGL_NONE
114         };
115         EGLConfig config;
116         int num_configs;
117         EGLint major, minor;
118         EGLBoolean success;
119         int opengl_context_attr[] = {
120                 EGL_NONE
121         };
122         int glesv2_context_attr[] = {
123                 EGL_CONTEXT_CLIENT_VERSION, 2,
124                 EGL_NONE
125         };
126         int *context_attr;
127         EGLContext ctx;
128         XVisualInfo *visual_info, visual_attr;
129         int visualid, num_visuals;
130
131         egl_dpy = _E(eglGetDisplay) (dpy);
132
133         success = _E(eglInitialize) (egl_dpy, &major, &minor);
134         if (!success) {
135                 fprintf (stderr, "Error: Failed to initialized EGL\n");
136                 exit (1);
137         }
138
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");
142                 exit (1);
143         }
144
145         success = _E(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         _E(eglBindAPI) (api);
159
160         if (api == EGL_OPENGL_ES_API)
161                 context_attr = glesv2_context_attr;
162         else
163                 context_attr = opengl_context_attr;
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 /* Simply count through some colors, frame by frame. */
220 #define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
221
222         int frame = 0;
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);
227                 frame++;
228
229                 /* Frame: Draw a solid (yellow) frame */
230                 paint_rgb_using_clear (RGB(frame));
231                 COMMON_SWAP_FUNCTION (dpy, window);
232                 frame++;
233
234                 /* Frame: Draw a solid (cyan) frame */
235                 paint_rgb_using_clear (RGB(frame));
236                 COMMON_SWAP_FUNCTION (dpy, window);
237                 frame++;
238         }
239 }
240
241 static void
242 common_handle_events(Display *dpy, COMMON_SWAP_DISPLAY swap_dpy, COMMON_SWAP_WINDOW window)
243 {
244         XEvent xev;
245         int width = 0;
246         int height = 0;
247
248         XNextEvent (dpy, &xev);
249
250         while (1) {
251                 XNextEvent (dpy, &xev);
252                 switch (xev.type) {
253                 case ConfigureNotify:
254                         width = xev.xconfigure.width;
255                         height = xev.xconfigure.height;
256                         break;
257                 case Expose:
258                         if (xev.xexpose.count == 0) {
259                                 draw (swap_dpy, window, width, height);
260                                 return;
261                         }
262                         break;
263                 }
264         }
265 }