blob: 4759a2f36b927e513858d1bf4e7bcc768a5395f2 [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
Pierre Ossman38a1c702016-12-20 12:32:37 +0100139 changefb();
140
141 r = fb->getDamage();
142 damage(FL_DAMAGE_USER1, r.tl.x, r.tl.y, r.width(), r.height());
143
144#if !defined(WIN32) && !defined(__APPLE__)
145 // Make sure we measure any work we queue up
146 XSync(fl_display, False);
147#endif
148
149 endTimeCounter();
150
151 time += getTimeCounter();
152}
153
154void TestWindow::changefb()
155{
156 rdr::U32 pixel;
157
158 pixel = rand();
159 fb->fillRect(fb->getRect(), &pixel);
160}
161
162void TestWindow::timer(void* data)
163{
164 TestWindow* self;
165
166 Fl::remove_idle(timer, data);
167
168 self = (TestWindow*)data;
169 self->update();
170}
171
172void PartialTestWindow::changefb()
173{
174 rfb::Rect r;
175 rdr::U32 pixel;
176
177 r = fb->getRect();
178 r.tl.x += w() / 4;
179 r.tl.y += h() / 4;
180 r.br.x -= w() / 4;
181 r.br.y -= h() / 4;
182
183 pixel = rand();
184 fb->fillRect(r, &pixel);
185}
186
187static void dotest(TestWindow* win)
188{
189 struct timeval start;
190 char s[1024];
191
192 win->show();
193
194 gettimeofday(&start, NULL);
195 while (rfb::msSince(&start) < 10000)
196 Fl::wait();
197
198 fprintf(stderr, "Rendering time: %g ms/frame\n",
199 win->time * 1000.0 / win->frames);
200 rfb::siPrefix(win->pixels / win->time,
201 "pixels/s", s, sizeof(s));
202 fprintf(stderr, "Rendering rate: %s\n", s);
203}
204
205int main(int argc, char** argv)
206{
207 TestWindow* win;
208
209 fprintf(stderr, "Full window update:\n\n");
210 win = new TestWindow();
211 dotest(win);
212 delete win;
213 fprintf(stderr, "\n");
214
215 fprintf(stderr, "Partial window update:\n\n");
216 win = new PartialTestWindow();
217 dotest(win);
218 delete win;
219 fprintf(stderr, "\n");
220
221 return 0;
222}