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 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
111 glClear(GL_COLOR_BUFFER_BIT);
115 void profilerTick(ProfilerState nextState)
117 assert (nextState >= 0 && nextState < NumProfilerStates);
118 double time = getTime();
119 if (curState >= 0 && curState < NumProfilerStates)
120 stateTime[curState] += time-lastProfile;
121 curState = nextState;
124 const double elapsed = time-lastDisplay;
126 printf("%.1f fps, time spent: ", framect/elapsed);
127 for (int i = 0; i < NumProfilerStates; ++i) {
128 if (i != 0) printf(", ");
129 printf("%s %.1f%%", profilerStateNames[i], stateTime[i]/elapsed*100);
134 memset(stateTime, 0, sizeof(stateTime));
140 //////////////////////////////////////////////
141 profilerTick(StatePreRender);
142 double time = getTime();
144 double passedTime = time-lastFrame;
145 boxPos += boxSpeed*passedTime*boxDirection;
146 while (boxPos < 0 || boxPos+boxWidth > 1) { // wrapover
149 boxDirection = -boxDirection;
152 boxPos = 1.0-boxWidth-(boxPos+boxWidth-1.0);
153 boxDirection = -boxDirection;
157 //////////////////////////////////////////////
158 profilerTick(StateClear);
162 glColor3f(0.0f, 0.0f, 0.0f);
163 rectQuad(0, 0, 1, 1);
167 glClear(GL_COLOR_BUFFER_BIT);
169 //////////////////////////////////////////////
170 profilerTick(StateDraw);
172 glColor3f(0.8f, 1.0f, 0.75f);
173 rectQuad(boxPos, 0, boxPos+boxWidth, 1);
175 usleep(sleep_time*1000);
176 //////////////////////////////////////////////
177 profilerTick(StatePresent);
179 glDrawBuffer(GL_FRONT);
180 glCopyPixels(0, 0, getWidth(), getHeight(), GL_COLOR);
181 glDrawBuffer(GL_BACK);
184 getBackend()->swapBuffers();
186 //////////////////////////////////////////////
187 profilerTick(StatePostRender);
190 //////////////////////////////////////////////
191 profilerTick(StateOutsideRender);
194 virtual void handleKeyPress(KeySym key)
197 case XK_Escape: close(); break;
198 case XK_F1: setFullscreen(!getFullscreen()); break;
208 GLfloat boxPos, boxDirection;
210 double lastDisplay, lastProfile;
212 ProfilerState curState;
213 double stateTime[NumProfilerStates];
216 int main(int argc, char ** argv)
218 // program options handling
219 po::options_description desc("Allowed options");
221 ("help,h", "produce help message")
222 ("swap-interval,i", po::value<int>(), "set swap interval")
223 ("copy,c", "copy to front buffer (instead of performing a buffer swap)")
224 ("overdraw,o", "overdraw previous image (instead of calling glClear)")
225 ("sleep,s", po::value<int>()->default_value(0), "Number of milliseconds to sleap in each frame (in the drawing phase)")
227 po::variables_map vm;
228 po::store(po::parse_command_line(argc, argv, desc), vm);
231 if (vm.count("help")) {
232 std::cout << desc << "\n";
237 TearTestWindow w(vm.count("overdraw"), vm.count("copy"), vm["sleep"].as<int>());
239 if (vm.count("swap-interval"))
240 w.setSwapInterval(vm["swap-interval"].as<int>());