make {glx,egl}test GPLv2+
[gltest.git] / gltest.cpp
index 2b6105c3d9b3b3da56600cb7be960de0a819eafb..b9824a85395e3d5076cc9a531706a28c4006ee45 100644 (file)
@@ -1,3 +1,21 @@
+/* gltest - small OpenGL tearing test program
+ * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
 // stdlib includes
 #include <time.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <assert.h>
 #include <GL/gl.h>
+#include <boost/program_options.hpp>
+
+namespace po = boost::program_options;
 
 // include proper GL connector
 #include "glwindow.h"
 #if defined(USE_GLX)
 #include "glxbackend.h"
-GLBackend *createGLBackend()
+static GLBackend *createGLBackend()
 {
        return new GLXBackend();
 }
 #elif defined(USE_EGL)
 #include "eglbackend.h"
-GLBackend *createGLBackend()
+static GLBackend *createGLBackend()
 {
        return new EGLBackend();
 }
@@ -27,12 +48,12 @@ GLBackend *createGLBackend()
 #endif
 
 // configuration
-const GLfloat boxWidth = 0.045f;
-const GLfloat boxSpeed = 1.25f; // per second
+static const GLfloat boxWidth = 0.045f;
+static const GLfloat boxSpeed = 1.25f; // per second
 
 // profiler
-const int numProfilerStates = 5;
-const char *profilerStateNames[numProfilerStates] = { "Pre-Render", "Drawing", "Swapping", "Post-Render", "Outside renderer"};
+enum ProfilerState { statePreRender, stateClear, stateDraw, statePresent, statePostRender, stateOutsideRender, numProfilerStates };
+static const char *profilerStateNames[numProfilerStates] = { "Pre-Render", "Clearing", "Drawing", "Presenting", "Post-Render", "Outside renderer"};
 
 // utility functions
 static double getTime()
@@ -42,21 +63,28 @@ static double getTime()
        return tp.tv_sec + 1e-9 * tp.tv_nsec;
 }
 
-static void drawRect(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
+static void rectVertices(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
 {
-       glVertex2f(x1, y1); glVertex2f(x2, y1); glVertex2f(x2, y2); glVertex2f(x1, y2);
+       glVertex2f(x1, y1);
+       glVertex2f(x2, y1);
+       glVertex2f(x2, y2);
+       glVertex2f(x1, y2);
 }
 
 // the window
 class TearTestWindow : public GLWindow {
 public:
-       TearTestWindow() : GLWindow(XOpenDisplay(0), createGLBackend()), boxPos(0), boxDirection(1)
+       TearTestWindow(bool overdraw, bool copy, int sleep_time) : GLWindow(XOpenDisplay(0), createGLBackend()),
+               overdraw(overdraw), copy(copy), sleep_time(sleep_time), boxPos(0), boxDirection(1)
        {}
 
+       void setSwapInterval(int i) {
+               getBackend()->setSwapInterval(i);
+       }
+
 protected:
        virtual void initGL()
        {
-               getBackend()->setSwapInterval(1);
                // initialize GL proper
                glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
                glDisable(GL_DEPTH_TEST);
@@ -83,7 +111,7 @@ protected:
                glFlush();
        }
        
-       void profilerTick(int nextState)
+       void profilerTick(ProfilerState nextState)
        {
                assert (nextState >= 0 && nextState < numProfilerStates);
                double time = getTime();
@@ -107,8 +135,9 @@ protected:
        }
        
        void renderGL()
-       {                            
-               profilerTick(0);
+       {              
+               //////////////////////////////////////////////
+               profilerTick(statePreRender);
                double time = getTime();
                // anim
                double passedTime = time-lastFrame;
@@ -124,37 +153,41 @@ protected:
                        }
                }
                lastFrame = time;
-               // draw
-               //glFlush();
-               profilerTick(1);
-               //glClear(GL_COLOR_BUFFER_BIT);
+               //////////////////////////////////////////////
+               profilerTick(stateClear);
+               if (overdraw) {
+                       // clear manually
+                       glBegin(GL_QUADS);
+                       glColor3f(0.0f, 0.0f, 0.0f);
+                       rectVertices(0, 0, 1, 1);
+                       glEnd();
+               }
+               else {
+                       glClear(GL_COLOR_BUFFER_BIT);
+               }
+               //////////////////////////////////////////////
+               profilerTick(stateDraw);
                glBegin(GL_QUADS);
-               // clear manually
-               glColor3f(0.0f, 0.0f, 0.0f);
-               drawRect(0, 0, 1, 1);
                glColor3f(0.8f, 1.0f, 0.75f);
-               drawRect(boxPos, 0, boxPos+boxWidth, 1);
+               rectVertices(boxPos, 0, boxPos+boxWidth, 1);
                glEnd();
-               usleep(20*1000);
-               profilerTick(2);
-               getBackend()->swapBuffers();
-//             glDrawBuffer(GL_FRONT);
-//             int xpos = 0;
-//             int ypos = 0;
-               //foreach (const QRect &r, region.rects()) {
-                       // convert to OpenGL coordinates
-                       //int y = displayHeight() - 0 - r.height();
-//                     glBitmap(0, 0, 0, 0, 0 - xpos, 0 - ypos, NULL); // not glRasterPos2f, see glxbackend.cpp
-//                     xpos = 0;
-//                     ypos = 0;
-//                     glCopyPixels(0, 0, getWidth(), getHeight(), GL_COLOR);
-               //}
-//             glBitmap(0, 0, 0, 0, -xpos, -ypos, NULL); // move position back to 0,0
-//             glDrawBuffer(GL_BACK);
-               profilerTick(3);
+               usleep(sleep_time*1000);
+               //////////////////////////////////////////////
+               profilerTick(statePresent);
+               if (copy) {
+                       glDrawBuffer(GL_FRONT);
+                       glCopyPixels(0, 0, getWidth(), getHeight(), GL_COLOR);
+                       glDrawBuffer(GL_BACK);
+               }
+               else {
+                       getBackend()->swapBuffers();
+               }
+               //////////////////////////////////////////////
+               profilerTick(statePostRender);
                glFlush();
                ++framect;
-               profilerTick(4);
+               //////////////////////////////////////////////
+               profilerTick(stateOutsideRender);
        }
        
        virtual void handleKeyPress(KeySym key)
@@ -167,19 +200,41 @@ protected:
        }
 
 private:
+       bool overdraw, copy;
+       int sleep_time;
+       // animation control
        double lastFrame;
        GLfloat boxPos, boxDirection;
-       // FPS
+       // FPS, profiler
        double lastDisplay, lastProfile;                 
        int framect, curState;
        double stateTime[numProfilerStates];
 };
 
-
-
 int main(int argc, char ** argv)
 {
-       TearTestWindow w;
+       // program options handling
+       po::options_description desc("Allowed options");
+       desc.add_options()
+               ("help,h", "produce help message")
+               ("swap-interval,i", po::value<int>(), "set swap interval")
+               ("copy,c", "copy to front buffer (instead of performing a buffer swap)")
+               ("overdraw,o", "overdraw previous image (instead of calling glClear)")
+               ("sleep,s", po::value<int>()->default_value(0), "Number of milliseconds to sleap in each frame (in the drawing phase)")
+       ;
+       po::variables_map vm;
+       po::store(po::parse_command_line(argc, argv, desc), vm);
+       po::notify(vm);
+
+       if (vm.count("help")) {
+               std::cout << desc << "\n";
+               return 1;
+       }
+
+       // actual program
+       TearTestWindow w(vm.count("overdraw"), vm.count("copy"), vm["sleep"].as<int>());
        w.open(800, 600);
+       if (vm.count("swap-interval"))
+               w.setSwapInterval(vm["swap-interval"].as<int>());
        w.exec();
 }