blob: 53334a6d22be073e86e82f6a88c57889a1bffc95 [file] [log] [blame]
Pierre Ossman38a1c702016-12-20 12:32:37 +01001/* Copyright 2016 Pierre Ossman <ossman@cendio.se> for Cendio AB
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#include <sys/time.h>
20
21#include <FL/Fl.H>
22#include <FL/Fl_Window.H>
23#include <FL/fl_draw.H>
24
25#include <rdr/Exception.h>
26#include <rfb/util.h>
27
28#include "../vncviewer/PlatformPixelBuffer.h"
29#include "../vncviewer/FLTKPixelBuffer.h"
30
31#if defined(WIN32)
32#include "../vncviewer/Win32PixelBuffer.h"
33#elif defined(__APPLE__)
34#include "../vncviewer/OSXPixelBuffer.h"
35#else
36#include "../vncviewer/X11PixelBuffer.h"
37#endif
38
39#include "util.h"
40
41class TestWindow: public Fl_Window {
42public:
43 TestWindow();
44 ~TestWindow();
45
46 virtual void draw();
47
48protected:
49 virtual void flush();
50
51 void update();
52 virtual void changefb();
53
54 static void timer(void* data);
55
56public:
57 unsigned long long pixels, frames;
58 double time;
59
60protected:
61 PlatformPixelBuffer* fb;
62};
63
64class PartialTestWindow: public TestWindow {
65protected:
66 virtual void changefb();
67};
68
69TestWindow::TestWindow() :
70 Fl_Window(1024, 768, "Framebuffer Performance Test")
71{
72 rdr::U32 pixel;
73
74 pixels = 0;
75 frames = 0;
76 time = 0;
77
78 try {
79#if defined(WIN32)
80 fb = new Win32PixelBuffer(w(), h());
81#elif defined(__APPLE__)
82 fb = new OSXPixelBuffer(w(), h());
83#else
84 fb = new X11PixelBuffer(w(), h());
85#endif
86 } catch (rdr::Exception& e) {
87 fb = new FLTKPixelBuffer(w(), h());
88 }
89
90 pixel = 0;
91 fb->fillRect(fb->getRect(), &pixel);
92}
93
94TestWindow::~TestWindow()
95{
96 delete fb;
97
98 Fl::remove_idle(timer, this);
99}
100
101void TestWindow::draw()
102{
103 int X, Y, W, H;
104
105 // We cannot update the damage region from inside the draw function,
106 // so delegate this to an idle function
107 Fl::add_idle(timer, this);
108
109 // Check what actually needs updating
110 fl_clip_box(0, 0, w(), h(), X, Y, W, H);
111 if ((W == 0) || (H == 0))
112 return;
113
114 fb->draw(X, Y, X, Y, W, H);
115
116 pixels += W*H;
117 frames++;
118}
119
120void TestWindow::flush()
121{
122 startTimeCounter();
123 Fl_Window::flush();
124#if !defined(WIN32) && !defined(__APPLE__)
125 // Make sure we measure any work we queue up
126 XSync(fl_display, False);
127#endif
128 endTimeCounter();
129
130 time += getTimeCounter();
131}
132
133void TestWindow::update()
134{
135 rfb::Rect r;
136
137 startTimeCounter();
138
139 while (fb->isRendering())
140 Fl::wait();
141
142 changefb();
143
144 r = fb->getDamage();
145 damage(FL_DAMAGE_USER1, r.tl.x, r.tl.y, r.width(), r.height());
146
147#if !defined(WIN32) && !defined(__APPLE__)
148 // Make sure we measure any work we queue up
149 XSync(fl_display, False);
150#endif
151
152 endTimeCounter();
153
154 time += getTimeCounter();
155}
156
157void TestWindow::changefb()
158{
159 rdr::U32 pixel;
160
161 pixel = rand();
162 fb->fillRect(fb->getRect(), &pixel);
163}
164
165void TestWindow::timer(void* data)
166{
167 TestWindow* self;
168
169 Fl::remove_idle(timer, data);
170
171 self = (TestWindow*)data;
172 self->update();
173}
174
175void PartialTestWindow::changefb()
176{
177 rfb::Rect r;
178 rdr::U32 pixel;
179
180 r = fb->getRect();
181 r.tl.x += w() / 4;
182 r.tl.y += h() / 4;
183 r.br.x -= w() / 4;
184 r.br.y -= h() / 4;
185
186 pixel = rand();
187 fb->fillRect(r, &pixel);
188}
189
190static void dotest(TestWindow* win)
191{
192 struct timeval start;
193 char s[1024];
194
195 win->show();
196
197 gettimeofday(&start, NULL);
198 while (rfb::msSince(&start) < 10000)
199 Fl::wait();
200
201 fprintf(stderr, "Rendering time: %g ms/frame\n",
202 win->time * 1000.0 / win->frames);
203 rfb::siPrefix(win->pixels / win->time,
204 "pixels/s", s, sizeof(s));
205 fprintf(stderr, "Rendering rate: %s\n", s);
206}
207
208int main(int argc, char** argv)
209{
210 TestWindow* win;
211
212 fprintf(stderr, "Full window update:\n\n");
213 win = new TestWindow();
214 dotest(win);
215 delete win;
216 fprintf(stderr, "\n");
217
218 fprintf(stderr, "Partial window update:\n\n");
219 win = new PartialTestWindow();
220 dotest(win);
221 delete win;
222 fprintf(stderr, "\n");
223
224 return 0;
225}