1 /* gltest - small OpenGL tearing test program
2 * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <boost/program_options.hpp>
30 namespace po = boost::program_options;
32 // include proper GL connector
35 #include "glxbackend.h"
36 static GLBackend *createGLBackend()
38 return new GLXBackend();
40 #elif defined(USE_EGL)
41 #include "eglbackend.h"
42 static GLBackend *createGLBackend()
44 return new EGLBackend();
47 #error "No GL window type selected"
51 static const GLfloat boxWidth = 0.045f;
52 static const GLfloat boxSpeed = 1.25f; // per second
55 enum ProfilerState { StatePreRender, StateClear, StateDraw, StatePresent, StatePostRender, StateOutsideRender, NumProfilerStates };
56 static const char *profilerStateNames[NumProfilerStates] = { "Pre-Render", "Clearing", "Drawing", "Presenting", "Post-Render", "Outside renderer"};
59 static double getTime()
62 clock_gettime(CLOCK_MONOTONIC, &tp);
63 return tp.tv_sec + 1e-9 * tp.tv_nsec;
66 static void rectQuad(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
75 class TearTestWindow : public GLWindow {
77 TearTestWindow(bool overdraw, bool copy, int sleep_time) : GLWindow(XOpenDisplay(0), createGLBackend()),
78 overdraw(overdraw), copy(copy), sleep_time(sleep_time), boxPos(0), boxDirection(1)
81 void setSwapInterval(int i) {
82 getBackend()->setSwapInterval(i);
88 // initialize GL proper
89 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
90 glDisable(GL_DEPTH_TEST);
92 lastFrame = getTime();
93 // initailize profiler
95 memset(stateTime, 0, sizeof(stateTime));
96 curState = NumProfilerStates;
97 lastDisplay = lastProfile = getTime();
100 virtual void resizeGL(unsigned int width, unsigned int height)
102 /* prevent divide-by-zero */
105 glViewport(0, 0, width, height);
106 glMatrixMode(GL_PROJECTION);
108 glOrtho (0, 1, 1, 0, 0, 1);
109 glMatrixMode(GL_MODELVIEW);
110 glClear(GL_COLOR_BUFFER_BIT);
114 void profilerTick(ProfilerState nextState)
116 assert (nextState >= 0 && nextState < NumProfilerStates);
117 double time = getTime();
118 if (curState >= 0 && curState < NumProfilerStates)
119 stateTime[curState] += time-lastProfile;
120 curState = nextState;
123 const double elapsed = time-lastDisplay;
125 printf("%.1f fps, time spent: ", framect/elapsed);
126 for (int i = 0; i < NumProfilerStates; ++i) {
127 if (i != 0) printf(", ");
128 printf("%s %.1f%%", profilerStateNames[i], stateTime[i]/elapsed*100);
133 memset(stateTime, 0, sizeof(stateTime));
139 //////////////////////////////////////////////
140 profilerTick(StatePreRender);
141 double time = getTime();
143 double passedTime = time-lastFrame;
144 boxPos += boxSpeed*passedTime*boxDirection;
145 while (boxPos < 0 || boxPos+boxWidth > 1) { // wrapover
148 boxDirection = -boxDirection;
151 boxPos = 1.0-boxWidth-(boxPos+boxWidth-1.0);
152 boxDirection = -boxDirection;
156 //////////////////////////////////////////////
157 profilerTick(StateClear);
161 glColor3f(0.0f, 0.0f, 0.0f);
162 rectQuad(0, 0, 1, 1);
166 glClear(GL_COLOR_BUFFER_BIT);
168 //////////////////////////////////////////////
169 profilerTick(StateDraw);
171 glColor3f(0.8f, 1.0f, 0.75f);
172 rectQuad(boxPos, 0, boxPos+boxWidth, 1);
174 usleep(sleep_time*1000);
175 //////////////////////////////////////////////
176 profilerTick(StatePresent);
178 glDrawBuffer(GL_FRONT);
179 glCopyPixels(0, 0, getWidth(), getHeight(), GL_COLOR);
180 glDrawBuffer(GL_BACK);
183 getBackend()->swapBuffers();
185 //////////////////////////////////////////////
186 profilerTick(StatePostRender);
189 //////////////////////////////////////////////
190 profilerTick(StateOutsideRender);
193 virtual void handleKeyPress(KeySym key)
196 case XK_Escape: close(); break;
197 case XK_F1: setFullscreen(!getFullscreen()); break;
207 GLfloat boxPos, boxDirection;
209 double lastDisplay, lastProfile;
211 ProfilerState curState;
212 double stateTime[NumProfilerStates];
215 int main(int argc, char ** argv)
217 // program options handling
218 po::options_description desc("Allowed options");
220 ("help,h", "produce help message")
221 ("swap-interval,i", po::value<int>(), "set swap interval")
222 ("copy,c", "copy to front buffer (instead of performing a buffer swap)")
223 ("overdraw,o", "overdraw previous image (instead of calling glClear)")
224 ("sleep,s", po::value<int>()->default_value(0), "Number of milliseconds to sleap in each frame (in the drawing phase)")
226 po::variables_map vm;
227 po::store(po::parse_command_line(argc, argv, desc), vm);
230 if (vm.count("help")) {
231 std::cout << desc << "\n";
236 TearTestWindow w(vm.count("overdraw"), vm.count("copy"), vm["sleep"].as<int>());
238 if (vm.count("swap-interval"))
239 w.setSwapInterval(vm["swap-interval"].as<int>());