blob: 06c878d4da74411b77f3e1812f3346241670a1a6 [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>
Pierre Ossman8738e8a2015-02-11 13:49:04 +010041#include <rfb/UpdateTracker.h>
42
43#include <rfb/EncodeManager.h>
44#include <rfb/SConnection.h>
45#include <rfb/SMsgWriter.h>
46
47#include "util.h"
48
49static rfb::IntParameter width("width", "Frame buffer width", 0);
50static rfb::IntParameter height("height", "Frame buffer height", 0);
DRC4631a762015-02-21 11:57:27 -060051static rfb::IntParameter count("count", "Number of benchmark iterations", 9);
Pierre Ossman8738e8a2015-02-11 13:49:04 +010052
53static rfb::StringParameter format("format", "Pixel format (e.g. bgr888)", "");
54
DRC2a172c92015-02-25 14:18:07 -060055static rfb::BoolParameter translate("translate",
56 "Translate 8-bit and 16-bit datasets into 24-bit",
57 true);
58
Pierre Ossman8738e8a2015-02-11 13:49:04 +010059// The frame buffer (and output) is always this format
60static const rfb::PixelFormat fbPF(32, 24, false, true, 255, 255, 255, 0, 8, 16);
61
62// Encodings to use
63static const rdr::S32 encodings[] = {
64 rfb::encodingTight, rfb::encodingCopyRect, rfb::encodingRRE,
65 rfb::encodingHextile, rfb::encodingZRLE, rfb::pseudoEncodingLastRect,
DRC562eb712015-02-21 12:01:47 -060066 rfb::pseudoEncodingQualityLevel0 + 8,
67 rfb::pseudoEncodingCompressLevel0 + 2};
Pierre Ossman8738e8a2015-02-11 13:49:04 +010068
69class DummyOutStream : public rdr::OutStream {
70public:
71 DummyOutStream();
72
73 virtual int length();
74 virtual void flush();
75
76private:
77 virtual int overrun(int itemSize, int nItems);
78
79 int offset;
80 rdr::U8 buf[131072];
81};
82
83class CConn : public rfb::CConnection {
84public:
85 CConn(const char *filename);
86 ~CConn();
87
DRC77be9292015-02-21 11:44:26 -060088 void getStats(double& ratio, unsigned long long& bytes,
89 unsigned long long& rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +010090
91 virtual void setDesktopSize(int w, int h);
92 virtual void setCursor(int, int, const rfb::Point&, void*, void*);
93 virtual void framebufferUpdateStart();
94 virtual void framebufferUpdateEnd();
95 virtual void dataRect(const rfb::Rect&, int);
96 virtual void setColourMapEntries(int, int, rdr::U16*);
97 virtual void bell();
98 virtual void serverCutText(const char*, rdr::U32);
99
100public:
101 double decodeTime;
102 double encodeTime;
103
104protected:
105 rdr::FileInStream *in;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100106 rfb::SimpleUpdateTracker updates;
107 class SConn *sc;
108};
109
110class Manager : public rfb::EncodeManager {
111public:
112 Manager(class rfb::SConnection *conn);
113
DRC77be9292015-02-21 11:44:26 -0600114 void getStats(double&, unsigned long long&, unsigned long long&);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100115};
116
117class SConn : public rfb::SConnection {
118public:
119 SConn();
120 ~SConn();
121
122 void writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb);
123
DRC77be9292015-02-21 11:44:26 -0600124 void getStats(double&, unsigned long long&, unsigned long long&);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100125
126 virtual void setAccessRights(AccessRights ar);
127
128 virtual void setDesktopSize(int fb_width, int fb_height,
129 const rfb::ScreenSet& layout);
130
131protected:
132 DummyOutStream *out;
133 Manager *manager;
134};
135
136DummyOutStream::DummyOutStream()
137{
138 offset = 0;
139 ptr = buf;
140 end = buf + sizeof(buf);
141}
142
143int DummyOutStream::length()
144{
145 flush();
146 return offset;
147}
148
149void DummyOutStream::flush()
150{
151 offset += ptr - buf;
152 ptr = buf;
153}
154
155int DummyOutStream::overrun(int itemSize, int nItems)
156{
157 flush();
Pierre Ossmanfc331e62015-03-03 16:42:45 +0100158 if (itemSize * nItems > end - ptr)
159 nItems = (end - ptr) / itemSize;
160 return nItems;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100161}
162
163CConn::CConn(const char *filename)
164{
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100165 decodeTime = 0.0;
166 encodeTime = 0.0;
167
168 in = new rdr::FileInStream(filename);
169 setStreams(in, NULL);
170
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100171 // Need to skip the initial handshake and ServerInit
172 setState(RFBSTATE_NORMAL);
173 // That also means that the reader and writer weren't setup
174 setReader(new rfb::CMsgReader(this, in));
175 // Nor the frame buffer size and format
176 setDesktopSize(width, height);
177 rfb::PixelFormat pf;
178 pf.parse(format);
179 setPixelFormat(pf);
180
181 sc = new SConn();
Pierre Ossman9f273e92015-11-09 16:34:54 +0100182 sc->cp.setPF((bool)translate ? fbPF : pf);
DRCb4c4a382015-02-21 11:28:37 -0600183 sc->setEncodings(sizeof(encodings) / sizeof(*encodings), encodings);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100184}
185
186CConn::~CConn()
187{
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100188 delete sc;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100189 delete in;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100190}
191
DRC77be9292015-02-21 11:44:26 -0600192void CConn::getStats(double& ratio, unsigned long long& bytes,
193 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100194{
DRC77be9292015-02-21 11:44:26 -0600195 sc->getStats(ratio, bytes, rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100196}
197
198void CConn::setDesktopSize(int w, int h)
199{
200 CConnection::setDesktopSize(w, h);
201
Pierre Ossman9f273e92015-11-09 16:34:54 +0100202 setFramebuffer(new rfb::ManagedPixelBuffer(sc->cp.pf(), cp.width, cp.height));
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100203}
204
205void CConn::setCursor(int, int, const rfb::Point&, void*, void*)
206{
207}
208
209void CConn::framebufferUpdateStart()
210{
211 updates.clear();
Pierre Ossman9f273e92015-11-09 16:34:54 +0100212 startCpuCounter();
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100213}
214
215void CConn::framebufferUpdateEnd()
216{
217 rfb::UpdateInfo ui;
Pierre Ossman9f273e92015-11-09 16:34:54 +0100218 rfb::PixelBuffer* pb = getFramebuffer();
219 rfb::Region clip(pb->getRect());
220
221 endCpuCounter();
222
223 decodeTime += getCpuCounter();
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100224
225 updates.getUpdateInfo(&ui, clip);
226
227 startCpuCounter();
Pierre Ossman9f273e92015-11-09 16:34:54 +0100228 sc->writeUpdate(ui, pb);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100229 endCpuCounter();
230
231 encodeTime += getCpuCounter();
232}
233
234void CConn::dataRect(const rfb::Rect &r, int encoding)
235{
Pierre Ossman9f273e92015-11-09 16:34:54 +0100236 CConnection::dataRect(r, encoding);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100237
238 if (encoding != rfb::encodingCopyRect) // FIXME
239 updates.add_changed(rfb::Region(r));
240}
241
242void CConn::setColourMapEntries(int, int, rdr::U16*)
243{
244}
245
246void CConn::bell()
247{
248}
249
250void CConn::serverCutText(const char*, rdr::U32)
251{
252}
253
254Manager::Manager(class rfb::SConnection *conn) :
255 EncodeManager(conn)
256{
257}
258
DRC77be9292015-02-21 11:44:26 -0600259void Manager::getStats(double& ratio, unsigned long long& encodedBytes,
260 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100261{
262 StatsVector::iterator iter;
263 unsigned long long bytes, equivalent;
264
265 bytes = equivalent = 0;
DRCb4c4a382015-02-21 11:28:37 -0600266 for (iter = stats.begin(); iter != stats.end(); ++iter) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100267 StatsVector::value_type::iterator iter2;
DRCb4c4a382015-02-21 11:28:37 -0600268 for (iter2 = iter->begin(); iter2 != iter->end(); ++iter2) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100269 bytes += iter2->bytes;
270 equivalent += iter2->equivalent;
271 }
272 }
273
DRC77be9292015-02-21 11:44:26 -0600274 ratio = (double)equivalent / bytes;
275 encodedBytes = bytes;
276 rawEquivalent = equivalent;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100277}
278
279SConn::SConn()
280{
281 out = new DummyOutStream;
282 setStreams(NULL, out);
283
284 setWriter(new rfb::SMsgWriter(&cp, out));
285
286 manager = new Manager(this);
287}
288
289SConn::~SConn()
290{
291 delete manager;
292 delete out;
293}
294
295void SConn::writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb)
296{
297 manager->writeUpdate(ui, pb, NULL);
298}
299
DRC77be9292015-02-21 11:44:26 -0600300void SConn::getStats(double& ratio, unsigned long long& bytes,
301 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100302{
DRC77be9292015-02-21 11:44:26 -0600303 manager->getStats(ratio, bytes, rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100304}
305
306void SConn::setAccessRights(AccessRights ar)
307{
308}
309
310void SConn::setDesktopSize(int fb_width, int fb_height,
311 const rfb::ScreenSet& layout)
312{
313}
314
DRC77be9292015-02-21 11:44:26 -0600315static double runTest(const char *fn, double& ratio, unsigned long long& bytes,
316 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100317{
318 CConn *cc;
319 double time;
320
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100321 try {
DRC13cfb512015-02-26 12:24:03 -0600322 cc = new CConn(fn);
Pierre Ossman86475a62015-03-03 16:42:15 +0100323 } catch (rdr::Exception e) {
324 fprintf(stderr, "Failed to open rfb file: %s\n", e.str());
325 exit(1);
326 }
DRC13cfb512015-02-26 12:24:03 -0600327
Pierre Ossman86475a62015-03-03 16:42:15 +0100328 try {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100329 while (true)
330 cc->processMsg();
331 } catch (rdr::EndOfStream e) {
332 } catch (rdr::Exception e) {
333 fprintf(stderr, "Failed to run rfb file: %s\n", e.str());
334 exit(1);
335 }
336
337 time = cc->encodeTime;
DRC77be9292015-02-21 11:44:26 -0600338 cc->getStats(ratio, bytes, rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100339
340 delete cc;
341
342 return time;
343}
344
345static void sort(double *array, int count)
346{
347 bool sorted;
348 int i;
349 do {
350 sorted = true;
DRCb4c4a382015-02-21 11:28:37 -0600351 for (i = 1; i < count; i++) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100352 if (array[i-1] > array[i]) {
353 double d;
354 d = array[i];
DRCb4c4a382015-02-21 11:28:37 -0600355 array[i] = array[i - 1];
356 array[i - 1] = d;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100357 sorted = false;
358 }
359 }
360 } while (!sorted);
361}
362
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100363static void usage(const char *argv0)
364{
365 fprintf(stderr, "Syntax: %s [options] <rfb file>\n", argv0);
366 fprintf(stderr, "Options:\n");
367 rfb::Configuration::listParams(79, 14);
368 exit(1);
369}
370
371int main(int argc, char **argv)
372{
373 int i;
374
375 const char *fn;
376
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100377 fn = NULL;
378 for (i = 1; i < argc; i++) {
379 if (rfb::Configuration::setParam(argv[i]))
380 continue;
381
382 if (argv[i][0] == '-') {
DRCb4c4a382015-02-21 11:28:37 -0600383 if (i + 1 < argc) {
384 if (rfb::Configuration::setParam(&argv[i][1], argv[i + 1])) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100385 i++;
386 continue;
387 }
388 }
389 usage(argv[0]);
390 }
391
392 if (fn != NULL)
393 usage(argv[0]);
394
395 fn = argv[i];
396 }
397
DRC4631a762015-02-21 11:57:27 -0600398 int runCount = count;
399 double times[runCount], dev[runCount];
400 double median, meddev, ratio;
401 unsigned long long bytes, equivalent;
402
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100403 if (fn == NULL) {
404 fprintf(stderr, "No file specified!\n\n");
405 usage(argv[0]);
406 }
407
Pierre Ossman135906e2015-04-27 12:48:47 +0200408 if (strcmp(format, "") == 0) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100409 fprintf(stderr, "Pixel format not specified!\n\n");
410 usage(argv[0]);
411 }
412
Pierre Ossman135906e2015-04-27 12:48:47 +0200413 if (width == 0 || height == 0) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100414 fprintf(stderr, "Frame buffer size not specified!\n\n");
415 usage(argv[0]);
416 }
417
418 // Warmup
DRC77be9292015-02-21 11:44:26 -0600419 runTest(fn, ratio, bytes, equivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100420
421 // Multiple runs to get a good average
DRCb4c4a382015-02-21 11:28:37 -0600422 for (i = 0; i < runCount; i++)
DRC77be9292015-02-21 11:44:26 -0600423 times[i] = runTest(fn, ratio, bytes, equivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100424
425 // Calculate median and median deviation
426 sort(times, runCount);
DRCb4c4a382015-02-21 11:28:37 -0600427 median = times[runCount / 2];
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100428
DRCb4c4a382015-02-21 11:28:37 -0600429 for (i = 0; i < runCount; i++)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100430 dev[i] = fabs((times[i] - median) / median) * 100;
431
432 sort(dev, runCount);
DRCb4c4a382015-02-21 11:28:37 -0600433 meddev = dev[runCount / 2];
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100434
DRCe46dda62015-02-25 14:08:34 -0600435 printf("CPU time: %g s (+/- %g %%)\n", median, meddev);
Pierre Ossman7d218b02015-03-03 16:43:05 +0100436#ifdef WIN32
437 printf("Encoded bytes: %I64d\n", bytes);
438 printf("Raw equivalent bytes: %I64d\n", equivalent);
439#else
DRC77be9292015-02-21 11:44:26 -0600440 printf("Encoded bytes: %lld\n", bytes);
441 printf("Raw equivalent bytes: %lld\n", equivalent);
Pierre Ossman7d218b02015-03-03 16:43:05 +0100442#endif
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100443 printf("Ratio: %g\n", ratio);
444
445 return 0;
446}