11 // include proper GL connector
14 #include "glxbackend.h"
15 GLBackend *createGLBackend()
17 return new GLXBackend();
19 #elif defined(USE_EGL)
20 #include "eglbackend.h"
21 GLBackend *createGLBackend()
23 return new EGLBackend();
26 #error "No GL window type selected"
30 const GLfloat boxWidth = 0.045f;
31 const GLfloat boxSpeed = 1.25f; // per second
34 const int numProfilerStates = 5;
35 const char *profilerStateNames[numProfilerStates] = { "Pre-Render", "Drawing", "Swapping", "Post-Render", "Outside renderer"};
38 static double getTime()
41 clock_gettime(CLOCK_MONOTONIC, &tp);
42 return tp.tv_sec + 1e-9 * tp.tv_nsec;
45 static void drawRect(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
47 glVertex2f(x1, y1); glVertex2f(x2, y1); glVertex2f(x2, y2); glVertex2f(x1, y2);
51 class TearTestWindow : public GLWindow {
53 TearTestWindow() : GLWindow(XOpenDisplay(0), createGLBackend()), boxPos(0), boxDirection(1)
59 getBackend()->setSwapInterval(1);
60 // initialize GL proper
61 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
62 glDisable(GL_DEPTH_TEST);
64 lastFrame = getTime();
65 // initailize profiler
67 memset(stateTime, 0, sizeof(stateTime));
69 lastDisplay = lastProfile = getTime();
72 virtual void resizeGL(unsigned int width, unsigned int height)
74 /* prevent divide-by-zero */
77 glViewport(0, 0, width, height);
78 glMatrixMode(GL_PROJECTION);
80 glOrtho (0, 1, 1, 0, 0, 1);
81 glMatrixMode(GL_MODELVIEW);
82 glClear(GL_COLOR_BUFFER_BIT);
86 void profilerTick(int nextState)
88 assert (nextState >= 0 && nextState < numProfilerStates);
89 double time = getTime();
91 stateTime[curState] += time-lastProfile;
95 const double elapsed = time-lastDisplay;
97 printf("%.1f fps, time spent: ", framect/elapsed);
98 for (int i = 0; i < numProfilerStates; ++i) {
99 if (i != 0) printf(", ");
100 printf("%s %.1f%%", profilerStateNames[i], stateTime[i]/elapsed*100);
105 memset(stateTime, 0, sizeof(stateTime));
112 double time = getTime();
114 double passedTime = time-lastFrame;
115 boxPos += boxSpeed*passedTime*boxDirection;
116 while (boxPos < 0 || boxPos+boxWidth > 1) { // wrapover
119 boxDirection = -boxDirection;
122 boxPos = 1.0-boxWidth-(boxPos+boxWidth-1.0);
123 boxDirection = -boxDirection;
130 //glClear(GL_COLOR_BUFFER_BIT);
133 glColor3f(0.0f, 0.0f, 0.0f);
134 drawRect(0, 0, 1, 1);
135 glColor3f(0.8f, 1.0f, 0.75f);
136 drawRect(boxPos, 0, boxPos+boxWidth, 1);
140 getBackend()->swapBuffers();
141 // glDrawBuffer(GL_FRONT);
144 //foreach (const QRect &r, region.rects()) {
145 // convert to OpenGL coordinates
146 //int y = displayHeight() - 0 - r.height();
147 // glBitmap(0, 0, 0, 0, 0 - xpos, 0 - ypos, NULL); // not glRasterPos2f, see glxbackend.cpp
150 // glCopyPixels(0, 0, getWidth(), getHeight(), GL_COLOR);
152 // glBitmap(0, 0, 0, 0, -xpos, -ypos, NULL); // move position back to 0,0
153 // glDrawBuffer(GL_BACK);
160 virtual void handleKeyPress(KeySym key)
163 case XK_Escape: close(); break;
164 case XK_F1: setFullscreen(!getFullscreen()); break;
171 GLfloat boxPos, boxDirection;
173 double lastDisplay, lastProfile;
174 int framect, curState;
175 double stateTime[numProfilerStates];
180 int main(int argc, char ** argv)