1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/* Copyright 2016 Pierre Ossman <ossman@cendio.se> for Cendio AB
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <math.h>
#include <sys/time.h>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
#include <FL/x.H>
#include <rdr/Exception.h>
#include <rfb/util.h>
#include "../vncviewer/PlatformPixelBuffer.h"
#include "util.h"
class TestWindow: public Fl_Window {
public:
TestWindow();
~TestWindow();
virtual void start(int width, int height);
virtual void stop();
virtual void draw();
protected:
virtual void flush();
void update();
virtual void changefb();
static void timer(void* data);
public:
unsigned long long pixels, frames;
double time;
protected:
PlatformPixelBuffer* fb;
};
class PartialTestWindow: public TestWindow {
protected:
virtual void changefb();
};
class OverlayTestWindow: public PartialTestWindow {
public:
OverlayTestWindow();
virtual void start(int width, int height);
virtual void stop();
virtual void draw();
protected:
Surface* overlay;
Surface* offscreen;
};
TestWindow::TestWindow() :
Fl_Window(0, 0, "Framebuffer Performance Test"),
fb(nullptr)
{
}
TestWindow::~TestWindow()
{
stop();
}
void TestWindow::start(int width, int height)
{
uint32_t pixel;
stop();
resize(x(), y(), width, height);
pixels = 0;
frames = 0;
time = 0;
fb = new PlatformPixelBuffer(w(), h());
pixel = 0;
fb->fillRect(fb->getRect(), &pixel);
show();
}
void TestWindow::stop()
{
hide();
delete fb;
fb = nullptr;
Fl::remove_idle(timer, this);
}
void TestWindow::draw()
{
int X, Y, W, H;
// We cannot update the damage region from inside the draw function,
// so delegate this to an idle function
Fl::add_idle(timer, this);
// Check what actually needs updating
fl_clip_box(0, 0, w(), h(), X, Y, W, H);
if ((W == 0) || (H == 0))
return;
fb->draw(X, Y, X, Y, W, H);
pixels += W*H;
frames++;
}
void TestWindow::flush()
{
startTimeCounter();
Fl_Window::flush();
#if !defined(WIN32) && !defined(__APPLE__)
// Make sure we measure any work we queue up
XSync(fl_display, False);
#endif
endTimeCounter();
time += getTimeCounter();
}
void TestWindow::update()
{
rfb::Rect r;
startTimeCounter();
changefb();
r = fb->getDamage();
damage(FL_DAMAGE_USER1, r.tl.x, r.tl.y, r.width(), r.height());
#if !defined(WIN32) && !defined(__APPLE__)
// Make sure we measure any work we queue up
XSync(fl_display, False);
#endif
endTimeCounter();
time += getTimeCounter();
}
void TestWindow::changefb()
{
uint32_t pixel;
pixel = rand();
fb->fillRect(fb->getRect(), &pixel);
}
void TestWindow::timer(void* data)
{
TestWindow* self;
Fl::remove_idle(timer, data);
self = (TestWindow*)data;
self->update();
}
void PartialTestWindow::changefb()
{
rfb::Rect r;
uint32_t pixel;
r = fb->getRect();
r.tl.x += w() / 4;
r.tl.y += h() / 4;
r.br.x -= w() / 4;
r.br.y -= h() / 4;
pixel = rand();
fb->fillRect(r, &pixel);
}
OverlayTestWindow::OverlayTestWindow() :
overlay(nullptr), offscreen(nullptr)
{
}
void OverlayTestWindow::start(int width, int height)
{
PartialTestWindow::start(width, height);
overlay = new Surface(400, 200);
overlay->clear(0xff, 0x80, 0x00, 0xcc);
// X11 needs an off screen buffer for compositing to avoid flicker,
// and alpha blending doesn't work for windows on Win32
#if !defined(__APPLE__)
offscreen = new Surface(w(), h());
#else
offscreen = nullptr;
#endif
}
void OverlayTestWindow::stop()
{
PartialTestWindow::stop();
delete offscreen;
offscreen = nullptr;
delete overlay;
overlay = nullptr;
}
void OverlayTestWindow::draw()
{
int ox, oy, ow, oh;
int X, Y, W, H;
// We cannot update the damage region from inside the draw function,
// so delegate this to an idle function
Fl::add_idle(timer, this);
// Check what actually needs updating
fl_clip_box(0, 0, w(), h(), X, Y, W, H);
if ((W == 0) || (H == 0))
return;
// We might get a redraw before we are fully ready
if (!overlay)
return;
// Simplify the clip region to a simple rectangle in order to
// properly draw all the layers even if they only partially overlap
fl_push_no_clip();
fl_push_clip(X, Y, W, H);
if (offscreen)
fb->draw(offscreen, X, Y, X, Y, W, H);
else
fb->draw(X, Y, X, Y, W, H);
pixels += W*H;
frames++;
ox = (w() - overlay->width()) / 2;
oy = h() / 4 - overlay->height() / 2;
ow = overlay->width();
oh = overlay->height();
fl_clip_box(ox, oy, ow, oh, X, Y, W, H);
if ((W != 0) && (H != 0)) {
if (offscreen)
overlay->blend(offscreen, X - ox, Y - oy, X, Y, W, H);
else
overlay->blend(X - ox, Y - oy, X, Y, W, H);
}
fl_pop_clip();
fl_pop_clip();
if (offscreen) {
fl_clip_box(0, 0, w(), h(), X, Y, W, H);
offscreen->draw(X, Y, X, Y, W, H);
}
}
static void dosubtest(TestWindow* win, int width, int height,
unsigned long long* pixels,
unsigned long long* frames,
double* time)
{
struct timeval start;
win->start(width, height);
gettimeofday(&start, nullptr);
while (rfb::msSince(&start) < 3000)
Fl::wait();
win->stop();
*pixels = win->pixels;
*frames = win->frames;
*time = win->time;
}
static bool is_constant(double a, double b)
{
return (fabs(a - b) / a) < 0.1;
}
static void dotest(TestWindow* win)
{
unsigned long long pixels[3];
unsigned long long frames[3];
double time[3];
double delay, rate;
// Run the test several times at different resolutions...
dosubtest(win, 800, 600, &pixels[0], &frames[0], &time[0]);
dosubtest(win, 1024, 768, &pixels[1], &frames[1], &time[1]);
dosubtest(win, 1280, 960, &pixels[2], &frames[2], &time[2]);
// ...in order to compute how much of the rendering time is static,
// and how much depends on the number of pixels
// (i.e. solve: time = delay * frames + rate * pixels)
delay = (((time[0] - (double)pixels[0] / pixels[1] * time[1]) /
(frames[0] - (double)pixels[0] / pixels[1] * frames[1])) +
((time[1] - (double)pixels[1] / pixels[2] * time[2]) /
(frames[1] - (double)pixels[1] / pixels[2] * frames[2]))) / 2.0;
rate = (((time[0] - (double)frames[0] / frames[1] * time[1]) /
(pixels[0] - (double)frames[0] / frames[1] * pixels[1])) +
((time[1] - (double)frames[1] / frames[2] * time[2]) /
(pixels[1] - (double)frames[1] / frames[2] * pixels[2]))) / 2.0;
// However, we have some corner cases:
// We are restricted by some delay, e.g. refresh rate
if (is_constant(frames[0]/time[0], frames[2]/time[2])) {
fprintf(stderr, "WARNING: Fixed delay dominating updates.\n\n");
delay = time[2]/frames[2];
rate = 0.0;
}
// There isn't any fixed delay, we are only restricted by pixel
// throughput
if (fabs(delay) < 0.001) {
delay = 0.0;
rate = time[2]/pixels[2];
}
// We can hit cache limits that causes performance to drop
// with increasing update size, screwing up our calculations
if ((pixels[2] / time[2]) < (pixels[0] / time[0] * 0.9)) {
fprintf(stderr, "WARNING: Unexpected behaviour. Measurement unreliable.\n\n");
// We can't determine the proportions between these, so divide the
// time spent evenly
delay = time[2] / 2.0 / frames[2];
rate = time[2] / 2.0 / pixels[2];
}
fprintf(stderr, "Rendering delay: %g ms/frame\n", delay * 1000.0);
fprintf(stderr, "Rendering rate: %s\n",
(rate == 0.0) ? "N/A pixels/s" :
rfb::siPrefix(1.0 / rate, "pixels/s").c_str());
fprintf(stderr, "Maximum FPS: %g fps @ 1920x1080\n",
1.0 / (delay + rate * 1920 * 1080));
}
int main(int /*argc*/, char** /*argv*/)
{
TestWindow* win;
fprintf(stderr, "Full window update:\n\n");
win = new TestWindow();
dotest(win);
delete win;
fprintf(stderr, "\n");
fprintf(stderr, "Partial window update:\n\n");
win = new PartialTestWindow();
dotest(win);
delete win;
fprintf(stderr, "\n");
fprintf(stderr, "Partial window update with overlay:\n\n");
win = new OverlayTestWindow();
dotest(win);
delete win;
fprintf(stderr, "\n");
return 0;
}
|