]> git.cworth.org Git - fips/commitdiff
test: Reduce code duplication in test-suite programs
authorCarl Worth <cworth@cworth.org>
Thu, 27 Jun 2013 22:20:45 +0000 (15:20 -0700)
committerCarl Worth <cworth@cworth.org>
Thu, 27 Jun 2013 22:20:45 +0000 (15:20 -0700)
All of the test suite programs previously had their own copies of
common drawing code. Now, this code is put into a shared
handle-events.c. Each test program includes handle-events and can
provide its own prefix for called OpenGL functions by first defining
HANDLE_EVENTS_GL_PREFIX.

test/glx-dlopen-dlsym.c
test/glx-dlopen-gpa.c
test/glx-dlopen-gpaa.c
test/glx-link-call.c
test/handle-events.c [new file with mode: 0644]

index b4f8c3e7f9ac7afc9c2901bc2a1d8cee28b724e7..a2a6f5dcfd5eb2ac030646e0d42238faf8b2b4f8 100644 (file)
@@ -48,98 +48,8 @@ void (*my_glXDestroyContext) (Display *, GLXContext);
 Bool (*my_glXMakeCurrent) (Display *, GLXDrawable, GLXContext);
 void (*my_glXSwapBuffers) (Display *, GLXDrawable);
 
-static void
-set_2d_projection (int width, int height)
-{
-       my_glMatrixMode (GL_PROJECTION);
-       my_glLoadIdentity ();
-       my_glOrtho (0, width, height, 0, 0, 1);
-       my_glMatrixMode (GL_MODELVIEW);
-}
-
-static void
-paint_rgb_using_clear (double r, double g, double b)
-{
-        my_glClearColor(r, g, b, 1.0);
-        my_glClear(GL_COLOR_BUFFER_BIT);
-}
-
-static void
-draw (Display *dpy, Window window, int width, int height)
-{
-       int i;
-        int visual_attr[] = {
-                GLX_RGBA,
-                GLX_RED_SIZE,          8,
-                GLX_GREEN_SIZE,        8,
-                GLX_BLUE_SIZE,         8,
-                GLX_ALPHA_SIZE,        8,
-                GLX_DOUBLEBUFFER,
-                GLX_DEPTH_SIZE,                24,
-                GLX_STENCIL_SIZE,      8,
-                GLX_X_VISUAL_TYPE,     GLX_DIRECT_COLOR,
-                None
-        };
-
-       /* Window and context setup. */
-        XVisualInfo *visual_info = my_glXChooseVisual(dpy, 0, visual_attr);
-        GLXContext ctx = my_glXCreateContext(dpy, visual_info, NULL, True);
-        my_glXMakeCurrent(dpy, window, ctx);
-
-        my_glViewport(0, 0, width, height);
-
-       set_2d_projection (width, height);
-
-/* Simply count through some colors, frame by frame. */
-#define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
-
-       int frame = 0;
-       for (i = 0; i < 2; i++) {
-               /* Frame: Draw a solid (magenta) frame */
-               paint_rgb_using_clear (RGB(frame));
-               my_glXSwapBuffers (dpy, window);
-               frame++;
-
-               /* Frame: Draw a solid (yellow) frame */
-               paint_rgb_using_clear (RGB(frame));
-               my_glXSwapBuffers (dpy, window);
-               frame++;
-
-               /* Frame: Draw a solid (cyan) frame */
-               paint_rgb_using_clear (RGB(frame));
-               my_glXSwapBuffers (dpy, window);
-               frame++;
-       }
-
-       /* Cleanup */
-        my_glXDestroyContext (dpy, ctx);
-}
-
-static void
-handle_events(Display *dpy, Window window)
-{
-        XEvent xev;
-       int width = 0;
-       int height = 0;
-
-        XNextEvent (dpy, &xev);
-
-        while (1) {
-                XNextEvent (dpy, &xev);
-                switch (xev.type) {
-                case ConfigureNotify:
-                        width = xev.xconfigure.width;
-                        height = xev.xconfigure.height;
-                        break;
-                case Expose:
-                        if (xev.xexpose.count == 0) {
-                                draw (dpy, window, width, height);
-                                return;
-                        }
-                        break;
-                }
-        }
-}
+#define HANDLE_EVENTS_GL_PREFIX my_
+#include "handle-events.c"
 
 static void
 resolve_symbols (void)
index 716d3e2ffc7e31351140a4ecc45b61fb10057448..3c2c325e874919069b67773d834b262315943cc4 100644 (file)
@@ -23,7 +23,7 @@
  *
  *     1. Using GLX to construct an OpenGL context
  *     2. By using dlopen to dynamically load libGL.so
- *     3. By using dlsym to lookup OpenGL functions
+ *     3. By using glXGetProcAddress to lookup OpenGL functions
  */
 
 #define GL_GLEXT_PROTOTYPES
@@ -49,98 +49,8 @@ void (*my_glXDestroyContext) (Display *, GLXContext);
 Bool (*my_glXMakeCurrent) (Display *, GLXDrawable, GLXContext);
 void (*my_glXSwapBuffers) (Display *, GLXDrawable);
 
-static void
-set_2d_projection (int width, int height)
-{
-       my_glMatrixMode (GL_PROJECTION);
-       my_glLoadIdentity ();
-       my_glOrtho (0, width, height, 0, 0, 1);
-       my_glMatrixMode (GL_MODELVIEW);
-}
-
-static void
-paint_rgb_using_clear (double r, double g, double b)
-{
-        my_glClearColor(r, g, b, 1.0);
-        my_glClear(GL_COLOR_BUFFER_BIT);
-}
-
-static void
-draw (Display *dpy, Window window, int width, int height)
-{
-       int i;
-        int visual_attr[] = {
-                GLX_RGBA,
-                GLX_RED_SIZE,          8,
-                GLX_GREEN_SIZE,        8,
-                GLX_BLUE_SIZE,         8,
-                GLX_ALPHA_SIZE,        8,
-                GLX_DOUBLEBUFFER,
-                GLX_DEPTH_SIZE,                24,
-                GLX_STENCIL_SIZE,      8,
-                GLX_X_VISUAL_TYPE,     GLX_DIRECT_COLOR,
-                None
-        };
-
-       /* Window and context setup. */
-        XVisualInfo *visual_info = my_glXChooseVisual(dpy, 0, visual_attr);
-        GLXContext ctx = my_glXCreateContext(dpy, visual_info, NULL, True);
-        my_glXMakeCurrent(dpy, window, ctx);
-
-        my_glViewport(0, 0, width, height);
-
-       set_2d_projection (width, height);
-
-/* Simply count through some colors, frame by frame. */
-#define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
-
-       int frame = 0;
-       for (i = 0; i < 2; i++) {
-               /* Frame: Draw a solid (magenta) frame */
-               paint_rgb_using_clear (RGB(frame));
-               my_glXSwapBuffers (dpy, window);
-               frame++;
-
-               /* Frame: Draw a solid (yellow) frame */
-               paint_rgb_using_clear (RGB(frame));
-               my_glXSwapBuffers (dpy, window);
-               frame++;
-
-               /* Frame: Draw a solid (cyan) frame */
-               paint_rgb_using_clear (RGB(frame));
-               my_glXSwapBuffers (dpy, window);
-               frame++;
-       }
-
-       /* Cleanup */
-        my_glXDestroyContext (dpy, ctx);
-}
-
-static void
-handle_events(Display *dpy, Window window)
-{
-        XEvent xev;
-       int width = 0;
-       int height = 0;
-
-        XNextEvent (dpy, &xev);
-
-        while (1) {
-                XNextEvent (dpy, &xev);
-                switch (xev.type) {
-                case ConfigureNotify:
-                        width = xev.xconfigure.width;
-                        height = xev.xconfigure.height;
-                        break;
-                case Expose:
-                        if (xev.xexpose.count == 0) {
-                                draw (dpy, window, width, height);
-                                return;
-                        }
-                        break;
-                }
-        }
-}
+#define HANDLE_EVENTS_GL_PREFIX my_
+#include "handle-events.c"
 
 static void
 resolve_symbols (void)
index b81a57b4a975b2525923c98a3087f8473f34dab5..b4aea47a0a7be1531857cb1d7a83f3a17fb4cdca 100644 (file)
@@ -23,7 +23,7 @@
  *
  *     1. Using GLX to construct an OpenGL context
  *     2. By using dlopen to dynamically load libGL.so
- *     3. By using dlsym to lookup OpenGL functions
+ *     3. By using glXGetProcAddressARB to lookup OpenGL functions
  */
 
 #define GL_GLEXT_PROTOTYPES
@@ -49,98 +49,8 @@ void (*my_glXDestroyContext) (Display *, GLXContext);
 Bool (*my_glXMakeCurrent) (Display *, GLXDrawable, GLXContext);
 void (*my_glXSwapBuffers) (Display *, GLXDrawable);
 
-static void
-set_2d_projection (int width, int height)
-{
-       my_glMatrixMode (GL_PROJECTION);
-       my_glLoadIdentity ();
-       my_glOrtho (0, width, height, 0, 0, 1);
-       my_glMatrixMode (GL_MODELVIEW);
-}
-
-static void
-paint_rgb_using_clear (double r, double g, double b)
-{
-        my_glClearColor(r, g, b, 1.0);
-        my_glClear(GL_COLOR_BUFFER_BIT);
-}
-
-static void
-draw (Display *dpy, Window window, int width, int height)
-{
-       int i;
-        int visual_attr[] = {
-                GLX_RGBA,
-                GLX_RED_SIZE,          8,
-                GLX_GREEN_SIZE,        8,
-                GLX_BLUE_SIZE,         8,
-                GLX_ALPHA_SIZE,        8,
-                GLX_DOUBLEBUFFER,
-                GLX_DEPTH_SIZE,                24,
-                GLX_STENCIL_SIZE,      8,
-                GLX_X_VISUAL_TYPE,     GLX_DIRECT_COLOR,
-                None
-        };
-
-       /* Window and context setup. */
-        XVisualInfo *visual_info = my_glXChooseVisual(dpy, 0, visual_attr);
-        GLXContext ctx = my_glXCreateContext(dpy, visual_info, NULL, True);
-        my_glXMakeCurrent(dpy, window, ctx);
-
-        my_glViewport(0, 0, width, height);
-
-       set_2d_projection (width, height);
-
-/* Simply count through some colors, frame by frame. */
-#define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
-
-       int frame = 0;
-       for (i = 0; i < 2; i++) {
-               /* Frame: Draw a solid (magenta) frame */
-               paint_rgb_using_clear (RGB(frame));
-               my_glXSwapBuffers (dpy, window);
-               frame++;
-
-               /* Frame: Draw a solid (yellow) frame */
-               paint_rgb_using_clear (RGB(frame));
-               my_glXSwapBuffers (dpy, window);
-               frame++;
-
-               /* Frame: Draw a solid (cyan) frame */
-               paint_rgb_using_clear (RGB(frame));
-               my_glXSwapBuffers (dpy, window);
-               frame++;
-       }
-
-       /* Cleanup */
-        my_glXDestroyContext (dpy, ctx);
-}
-
-static void
-handle_events(Display *dpy, Window window)
-{
-        XEvent xev;
-       int width = 0;
-       int height = 0;
-
-        XNextEvent (dpy, &xev);
-
-        while (1) {
-                XNextEvent (dpy, &xev);
-                switch (xev.type) {
-                case ConfigureNotify:
-                        width = xev.xconfigure.width;
-                        height = xev.xconfigure.height;
-                        break;
-                case Expose:
-                        if (xev.xexpose.count == 0) {
-                                draw (dpy, window, width, height);
-                                return;
-                        }
-                        break;
-                }
-        }
-}
+#define HANDLE_EVENTS_GL_PREFIX my_
+#include "handle-events.c"
 
 static void
 resolve_symbols (void)
index 0c5047eb7edc1ebd7789f45bbba401a8cc099824..c5f8eb46d25fba4d106b88172b885994f0d8283c 100644 (file)
 
 #include "util.h"
 
-static void
-set_2d_projection (int width, int height)
-{
-       glMatrixMode (GL_PROJECTION);
-       glLoadIdentity ();
-       glOrtho (0, width, height, 0, 0, 1);
-       glMatrixMode (GL_MODELVIEW);
-}
-
-static void
-paint_rgb_using_clear (double r, double g, double b)
-{
-        glClearColor(r, g, b, 1.0);
-        glClear(GL_COLOR_BUFFER_BIT);
-}
-
-static void
-draw (Display *dpy, Window window, int width, int height)
-{
-       int i;
-        int visual_attr[] = {
-                GLX_RGBA,
-                GLX_RED_SIZE,          8,
-                GLX_GREEN_SIZE,        8,
-                GLX_BLUE_SIZE,         8,
-                GLX_ALPHA_SIZE,        8,
-                GLX_DOUBLEBUFFER,
-                GLX_DEPTH_SIZE,                24,
-                GLX_STENCIL_SIZE,      8,
-                GLX_X_VISUAL_TYPE,     GLX_DIRECT_COLOR,
-                None
-        };
-
-       /* Window and context setup. */
-        XVisualInfo *visual_info = glXChooseVisual(dpy, 0, visual_attr);
-        GLXContext ctx = glXCreateContext(dpy, visual_info, NULL, True);
-        glXMakeCurrent(dpy, window, ctx);
-
-        glViewport(0, 0, width, height);
-
-       set_2d_projection (width, height);
-
-/* Simply count through some colors, frame by frame. */
-#define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
-
-       int frame = 0;
-       for (i = 0; i < 2; i++) {
-               /* Frame: Draw a solid (magenta) frame */
-               paint_rgb_using_clear (RGB(frame));
-               glXSwapBuffers (dpy, window);
-               frame++;
-
-               /* Frame: Draw a solid (yellow) frame */
-               paint_rgb_using_clear (RGB(frame));
-               glXSwapBuffers (dpy, window);
-               frame++;
-
-               /* Frame: Draw a solid (cyan) frame */
-               paint_rgb_using_clear (RGB(frame));
-               glXSwapBuffers (dpy, window);
-               frame++;
-       }
-
-       /* Cleanup */
-        glXDestroyContext (dpy, ctx);
-}
-
-static void
-handle_events(Display *dpy, Window window)
-{
-        XEvent xev;
-       int width = 0;
-       int height = 0;
-
-        XNextEvent (dpy, &xev);
-
-        while (1) {
-                XNextEvent (dpy, &xev);
-                switch (xev.type) {
-                case ConfigureNotify:
-                        width = xev.xconfigure.width;
-                        height = xev.xconfigure.height;
-                        break;
-                case Expose:
-                        if (xev.xexpose.count == 0) {
-                                draw (dpy, window, width, height);
-                                return;
-                        }
-                        break;
-                }
-        }
-}
+#define HANDLE_EVENTS_GL_PREFIX
+#include "handle-events.c"
 
 int
 main (void)
diff --git a/test/handle-events.c b/test/handle-events.c
new file mode 100644 (file)
index 0000000..3939c04
--- /dev/null
@@ -0,0 +1,139 @@
+/* Copyright © 2013, Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/* This is C code intended to be included (yes, included) by test
+ * programs in the fips test suite.
+ *
+ * It defines a function:
+ *
+ *     static void
+ *     handle_events(Display *dpy, Window window);
+ *
+ * which handles X events, responding by drawing a few frames using
+ * OpenGL.
+ *
+ * Before including this file, the test program must define a macro
+ * HANDLE_EVENTS_GL_PREFIX to some prefix. This macro can be empty (to
+ * directly call functions in an OpenGL-library directly linked) or
+ * can be some custom prefix to call through symbols defined in the
+ * test program.
+ */
+
+#ifndef HANDLE_EVENTS_GL_PREFIX
+#error Code including handle-events.c must define HANDLE_EVENTS_GL_PREFIX
+#endif
+
+#define concat_(a,b) a ## b
+#define concat(a,b) concat_(a,b)
+#define _(func) concat(HANDLE_EVENTS_GL_PREFIX, func)
+
+static void
+set_2d_projection (int width, int height)
+{
+       _(glMatrixMode) (GL_PROJECTION);
+       _(glLoadIdentity) ();
+       _(glOrtho) (0, width, height, 0, 0, 1);
+       _(glMatrixMode) (GL_MODELVIEW);
+}
+
+static void
+paint_rgb_using_clear (double r, double g, double b)
+{
+        _(glClearColor) (r, g, b, 1.0);
+        _(glClear) (GL_COLOR_BUFFER_BIT);
+}
+
+static void
+draw (Display *dpy, Window window, int width, int height)
+{
+       int i;
+        int visual_attr[] = {
+                GLX_RGBA,
+                GLX_RED_SIZE,          8,
+                GLX_GREEN_SIZE,        8,
+                GLX_BLUE_SIZE,         8,
+                GLX_ALPHA_SIZE,        8,
+                GLX_DOUBLEBUFFER,
+                GLX_DEPTH_SIZE,                24,
+                GLX_STENCIL_SIZE,      8,
+                GLX_X_VISUAL_TYPE,     GLX_DIRECT_COLOR,
+                None
+        };
+
+       /* Window and context setup. */
+        XVisualInfo *visual_info = _(glXChooseVisual) (dpy, 0, visual_attr);
+        GLXContext ctx = _(glXCreateContext) (dpy, visual_info, NULL, True);
+        _(glXMakeCurrent) (dpy, window, ctx);
+
+        _(glViewport) (0, 0, width, height);
+
+       set_2d_projection (width, height);
+
+/* Simply count through some colors, frame by frame. */
+#define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
+
+       int frame = 0;
+       for (i = 0; i < 2; i++) {
+               /* Frame: Draw a solid (magenta) frame */
+               paint_rgb_using_clear (RGB(frame));
+               _(glXSwapBuffers) (dpy, window);
+               frame++;
+
+               /* Frame: Draw a solid (yellow) frame */
+               paint_rgb_using_clear (RGB(frame));
+               _(glXSwapBuffers) (dpy, window);
+               frame++;
+
+               /* Frame: Draw a solid (cyan) frame */
+               paint_rgb_using_clear (RGB(frame));
+               _(glXSwapBuffers) (dpy, window);
+               frame++;
+       }
+
+       /* Cleanup */
+        _(glXDestroyContext) (dpy, ctx);
+}
+
+static void
+handle_events(Display *dpy, Window window)
+{
+        XEvent xev;
+       int width = 0;
+       int height = 0;
+
+        XNextEvent (dpy, &xev);
+
+        while (1) {
+                XNextEvent (dpy, &xev);
+                switch (xev.type) {
+                case ConfigureNotify:
+                        width = xev.xconfigure.width;
+                        height = xev.xconfigure.height;
+                        break;
+                case Expose:
+                        if (xev.xexpose.count == 0) {
+                                draw (dpy, window, width, height);
+                                return;
+                        }
+                        break;
+                }
+        }
+}