blob: 2628b46a6724597179bd44d660becf6e8173bab9 [file] [log] [blame]
Pierre Ossman8738e8a2015-02-11 13:49:04 +01001/* Copyright 2015 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRC77be9292015-02-21 11:44:26 -06002 * Copyright (C) 2015 D. R. Commander. All Rights Reserved.
DRCb4c4a382015-02-21 11:28:37 -06003 *
Pierre Ossman8738e8a2015-02-11 13:49:04 +01004 * This 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.
DRCb4c4a382015-02-21 11:28:37 -06008 *
Pierre Ossman8738e8a2015-02-11 13:49:04 +01009 * This software 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.
DRCb4c4a382015-02-21 11:28:37 -060013 *
Pierre Ossman8738e8a2015-02-11 13:49:04 +010014 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20/*
21 * This program reads files produced by TightVNC's/TurboVNC's
22 * fbs-dump, which in turn takes files from rfbproxy. It is
23 * basically a dump of the RFB protocol from the server side after
24 * the ServerInit message. Mostly this consists of FramebufferUpdate
25 * message using the HexTile encoding. Screen size and pixel format
26 * are not encoded in the file and must be specified by the user.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <math.h>
32
33#include <rdr/Exception.h>
34#include <rdr/OutStream.h>
35#include <rdr/FileInStream.h>
36
37#include <rfb/PixelFormat.h>
38
39#include <rfb/CConnection.h>
40#include <rfb/CMsgReader.h>
41#include <rfb/Decoder.h>
42#include <rfb/UpdateTracker.h>
43
44#include <rfb/EncodeManager.h>
45#include <rfb/SConnection.h>
46#include <rfb/SMsgWriter.h>
47
48#include "util.h"
49
50static rfb::IntParameter width("width", "Frame buffer width", 0);
51static rfb::IntParameter height("height", "Frame buffer height", 0);
DRC4631a762015-02-21 11:57:27 -060052static rfb::IntParameter count("count", "Number of benchmark iterations", 9);
Pierre Ossman8738e8a2015-02-11 13:49:04 +010053
54static rfb::StringParameter format("format", "Pixel format (e.g. bgr888)", "");
55
DRC2a172c92015-02-25 14:18:07 -060056static rfb::BoolParameter translate("translate",
57 "Translate 8-bit and 16-bit datasets into 24-bit",
58 true);
59
Pierre Ossman8738e8a2015-02-11 13:49:04 +010060// The frame buffer (and output) is always this format
61static const rfb::PixelFormat fbPF(32, 24, false, true, 255, 255, 255, 0, 8, 16);
62
63// Encodings to use
64static const rdr::S32 encodings[] = {
65 rfb::encodingTight, rfb::encodingCopyRect, rfb::encodingRRE,
66 rfb::encodingHextile, rfb::encodingZRLE, rfb::pseudoEncodingLastRect,
DRC562eb712015-02-21 12:01:47 -060067 rfb::pseudoEncodingQualityLevel0 + 8,
68 rfb::pseudoEncodingCompressLevel0 + 2};
Pierre Ossman8738e8a2015-02-11 13:49:04 +010069
70class DummyOutStream : public rdr::OutStream {
71public:
72 DummyOutStream();
73
74 virtual int length();
75 virtual void flush();
76
77private:
78 virtual int overrun(int itemSize, int nItems);
79
80 int offset;
81 rdr::U8 buf[131072];
82};
83
84class CConn : public rfb::CConnection {
85public:
86 CConn(const char *filename);
87 ~CConn();
88
DRC77be9292015-02-21 11:44:26 -060089 void getStats(double& ratio, unsigned long long& bytes,
90 unsigned long long& rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +010091
92 virtual void setDesktopSize(int w, int h);
93 virtual void setCursor(int, int, const rfb::Point&, void*, void*);
94 virtual void framebufferUpdateStart();
95 virtual void framebufferUpdateEnd();
96 virtual void dataRect(const rfb::Rect&, int);
97 virtual void setColourMapEntries(int, int, rdr::U16*);
98 virtual void bell();
99 virtual void serverCutText(const char*, rdr::U32);
100
101public:
102 double decodeTime;
103 double encodeTime;
104
105protected:
106 rdr::FileInStream *in;
DRCb4c4a382015-02-21 11:28:37 -0600107 rfb::Decoder *decoders[rfb::encodingMax + 1];
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100108 rfb::ManagedPixelBuffer pb;
109 rfb::SimpleUpdateTracker updates;
110 class SConn *sc;
111};
112
113class Manager : public rfb::EncodeManager {
114public:
115 Manager(class rfb::SConnection *conn);
116
DRC77be9292015-02-21 11:44:26 -0600117 void getStats(double&, unsigned long long&, unsigned long long&);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100118};
119
120class SConn : public rfb::SConnection {
121public:
122 SConn();
123 ~SConn();
124
125 void writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb);
126
DRC77be9292015-02-21 11:44:26 -0600127 void getStats(double&, unsigned long long&, unsigned long long&);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100128
129 virtual void setAccessRights(AccessRights ar);
130
131 virtual void setDesktopSize(int fb_width, int fb_height,
132 const rfb::ScreenSet& layout);
133
134protected:
135 DummyOutStream *out;
136 Manager *manager;
137};
138
139DummyOutStream::DummyOutStream()
140{
141 offset = 0;
142 ptr = buf;
143 end = buf + sizeof(buf);
144}
145
146int DummyOutStream::length()
147{
148 flush();
149 return offset;
150}
151
152void DummyOutStream::flush()
153{
154 offset += ptr - buf;
155 ptr = buf;
156}
157
158int DummyOutStream::overrun(int itemSize, int nItems)
159{
160 flush();
161}
162
163CConn::CConn(const char *filename)
164{
165 int i;
166
167 decodeTime = 0.0;
168 encodeTime = 0.0;
169
170 in = new rdr::FileInStream(filename);
171 setStreams(in, NULL);
172
173 memset(decoders, 0, sizeof(decoders));
DRCb4c4a382015-02-21 11:28:37 -0600174 for (i = 0; i < rfb::encodingMax; i++) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100175 if (!rfb::Decoder::supported(i))
176 continue;
177
178 decoders[i] = rfb::Decoder::createDecoder(i, this);
179 }
180
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100181 // Need to skip the initial handshake and ServerInit
182 setState(RFBSTATE_NORMAL);
183 // That also means that the reader and writer weren't setup
184 setReader(new rfb::CMsgReader(this, in));
185 // Nor the frame buffer size and format
186 setDesktopSize(width, height);
187 rfb::PixelFormat pf;
188 pf.parse(format);
189 setPixelFormat(pf);
190
DRC2a172c92015-02-25 14:18:07 -0600191 pb.setPF((bool)translate ? fbPF : pf);
192
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100193 sc = new SConn();
194 sc->cp.setPF(pb.getPF());
DRCb4c4a382015-02-21 11:28:37 -0600195 sc->setEncodings(sizeof(encodings) / sizeof(*encodings), encodings);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100196}
197
198CConn::~CConn()
199{
200 int i;
201
202 delete sc;
203
204 delete in;
205
DRCb4c4a382015-02-21 11:28:37 -0600206 for (i = 0; i < rfb::encodingMax; i++)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100207 delete decoders[i];
208}
209
DRC77be9292015-02-21 11:44:26 -0600210void CConn::getStats(double& ratio, unsigned long long& bytes,
211 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100212{
DRC77be9292015-02-21 11:44:26 -0600213 sc->getStats(ratio, bytes, rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100214}
215
216void CConn::setDesktopSize(int w, int h)
217{
218 CConnection::setDesktopSize(w, h);
219
220 pb.setSize(cp.width, cp.height);
221}
222
223void CConn::setCursor(int, int, const rfb::Point&, void*, void*)
224{
225}
226
227void CConn::framebufferUpdateStart()
228{
229 updates.clear();
230}
231
232void CConn::framebufferUpdateEnd()
233{
234 rfb::UpdateInfo ui;
235 rfb::Region clip(pb.getRect());
236
237 updates.getUpdateInfo(&ui, clip);
238
239 startCpuCounter();
240 sc->writeUpdate(ui, &pb);
241 endCpuCounter();
242
243 encodeTime += getCpuCounter();
244}
245
246void CConn::dataRect(const rfb::Rect &r, int encoding)
247{
248 if (!decoders[encoding])
249 throw rdr::Exception("Unknown encoding");
250
251 startCpuCounter();
252 decoders[encoding]->readRect(r, &pb);
253 endCpuCounter();
254
255 decodeTime += getCpuCounter();
256
257 if (encoding != rfb::encodingCopyRect) // FIXME
258 updates.add_changed(rfb::Region(r));
259}
260
261void CConn::setColourMapEntries(int, int, rdr::U16*)
262{
263}
264
265void CConn::bell()
266{
267}
268
269void CConn::serverCutText(const char*, rdr::U32)
270{
271}
272
273Manager::Manager(class rfb::SConnection *conn) :
274 EncodeManager(conn)
275{
276}
277
DRC77be9292015-02-21 11:44:26 -0600278void Manager::getStats(double& ratio, unsigned long long& encodedBytes,
279 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100280{
281 StatsVector::iterator iter;
282 unsigned long long bytes, equivalent;
283
284 bytes = equivalent = 0;
DRCb4c4a382015-02-21 11:28:37 -0600285 for (iter = stats.begin(); iter != stats.end(); ++iter) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100286 StatsVector::value_type::iterator iter2;
DRCb4c4a382015-02-21 11:28:37 -0600287 for (iter2 = iter->begin(); iter2 != iter->end(); ++iter2) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100288 bytes += iter2->bytes;
289 equivalent += iter2->equivalent;
290 }
291 }
292
DRC77be9292015-02-21 11:44:26 -0600293 ratio = (double)equivalent / bytes;
294 encodedBytes = bytes;
295 rawEquivalent = equivalent;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100296}
297
298SConn::SConn()
299{
300 out = new DummyOutStream;
301 setStreams(NULL, out);
302
303 setWriter(new rfb::SMsgWriter(&cp, out));
304
305 manager = new Manager(this);
306}
307
308SConn::~SConn()
309{
310 delete manager;
311 delete out;
312}
313
314void SConn::writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb)
315{
316 manager->writeUpdate(ui, pb, NULL);
317}
318
DRC77be9292015-02-21 11:44:26 -0600319void SConn::getStats(double& ratio, unsigned long long& bytes,
320 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100321{
DRC77be9292015-02-21 11:44:26 -0600322 manager->getStats(ratio, bytes, rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100323}
324
325void SConn::setAccessRights(AccessRights ar)
326{
327}
328
329void SConn::setDesktopSize(int fb_width, int fb_height,
330 const rfb::ScreenSet& layout)
331{
332}
333
DRC77be9292015-02-21 11:44:26 -0600334static double runTest(const char *fn, double& ratio, unsigned long long& bytes,
335 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100336{
337 CConn *cc;
338 double time;
339
340 cc = new CConn(fn);
341
342 try {
343 while (true)
344 cc->processMsg();
345 } catch (rdr::EndOfStream e) {
346 } catch (rdr::Exception e) {
347 fprintf(stderr, "Failed to run rfb file: %s\n", e.str());
348 exit(1);
349 }
350
351 time = cc->encodeTime;
DRC77be9292015-02-21 11:44:26 -0600352 cc->getStats(ratio, bytes, rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100353
354 delete cc;
355
356 return time;
357}
358
359static void sort(double *array, int count)
360{
361 bool sorted;
362 int i;
363 do {
364 sorted = true;
DRCb4c4a382015-02-21 11:28:37 -0600365 for (i = 1; i < count; i++) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100366 if (array[i-1] > array[i]) {
367 double d;
368 d = array[i];
DRCb4c4a382015-02-21 11:28:37 -0600369 array[i] = array[i - 1];
370 array[i - 1] = d;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100371 sorted = false;
372 }
373 }
374 } while (!sorted);
375}
376
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100377static void usage(const char *argv0)
378{
379 fprintf(stderr, "Syntax: %s [options] <rfb file>\n", argv0);
380 fprintf(stderr, "Options:\n");
381 rfb::Configuration::listParams(79, 14);
382 exit(1);
383}
384
385int main(int argc, char **argv)
386{
387 int i;
388
389 const char *fn;
390
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100391 fn = NULL;
392 for (i = 1; i < argc; i++) {
393 if (rfb::Configuration::setParam(argv[i]))
394 continue;
395
396 if (argv[i][0] == '-') {
DRCb4c4a382015-02-21 11:28:37 -0600397 if (i + 1 < argc) {
398 if (rfb::Configuration::setParam(&argv[i][1], argv[i + 1])) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100399 i++;
400 continue;
401 }
402 }
403 usage(argv[0]);
404 }
405
406 if (fn != NULL)
407 usage(argv[0]);
408
409 fn = argv[i];
410 }
411
DRC4631a762015-02-21 11:57:27 -0600412 int runCount = count;
413 double times[runCount], dev[runCount];
414 double median, meddev, ratio;
415 unsigned long long bytes, equivalent;
416
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100417 if (fn == NULL) {
418 fprintf(stderr, "No file specified!\n\n");
419 usage(argv[0]);
420 }
421
422 if (!format.hasBeenSet()) {
423 fprintf(stderr, "Pixel format not specified!\n\n");
424 usage(argv[0]);
425 }
426
427 if (!width.hasBeenSet() || !height.hasBeenSet()) {
428 fprintf(stderr, "Frame buffer size not specified!\n\n");
429 usage(argv[0]);
430 }
431
432 // Warmup
DRC77be9292015-02-21 11:44:26 -0600433 runTest(fn, ratio, bytes, equivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100434
435 // Multiple runs to get a good average
DRCb4c4a382015-02-21 11:28:37 -0600436 for (i = 0; i < runCount; i++)
DRC77be9292015-02-21 11:44:26 -0600437 times[i] = runTest(fn, ratio, bytes, equivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100438
439 // Calculate median and median deviation
440 sort(times, runCount);
DRCb4c4a382015-02-21 11:28:37 -0600441 median = times[runCount / 2];
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100442
DRCb4c4a382015-02-21 11:28:37 -0600443 for (i = 0; i < runCount; i++)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100444 dev[i] = fabs((times[i] - median) / median) * 100;
445
446 sort(dev, runCount);
DRCb4c4a382015-02-21 11:28:37 -0600447 meddev = dev[runCount / 2];
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100448
DRCe46dda62015-02-25 14:08:34 -0600449 printf("CPU time: %g s (+/- %g %%)\n", median, meddev);
DRC77be9292015-02-21 11:44:26 -0600450 printf("Encoded bytes: %lld\n", bytes);
451 printf("Raw equivalent bytes: %lld\n", equivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100452 printf("Ratio: %g\n", ratio);
453
454 return 0;
455}