You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fbperf.cxx 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* 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. #include <math.h>
  19. #include <sys/time.h>
  20. #include <FL/Fl.H>
  21. #include <FL/Fl_Window.H>
  22. #include <FL/fl_draw.H>
  23. #include <rdr/Exception.h>
  24. #include <rfb/util.h>
  25. #include "../vncviewer/PlatformPixelBuffer.h"
  26. #include "util.h"
  27. class TestWindow: public Fl_Window {
  28. public:
  29. TestWindow();
  30. ~TestWindow();
  31. virtual void start(int width, int height);
  32. virtual void stop();
  33. virtual void draw();
  34. protected:
  35. virtual void flush();
  36. void update();
  37. virtual void changefb();
  38. static void timer(void* data);
  39. public:
  40. unsigned long long pixels, frames;
  41. double time;
  42. protected:
  43. PlatformPixelBuffer* fb;
  44. };
  45. class PartialTestWindow: public TestWindow {
  46. protected:
  47. virtual void changefb();
  48. };
  49. class OverlayTestWindow: public PartialTestWindow {
  50. public:
  51. OverlayTestWindow();
  52. virtual void start(int width, int height);
  53. virtual void stop();
  54. virtual void draw();
  55. protected:
  56. Surface* overlay;
  57. };
  58. TestWindow::TestWindow() :
  59. Fl_Window(0, 0, "Framebuffer Performance Test"),
  60. fb(NULL)
  61. {
  62. }
  63. TestWindow::~TestWindow()
  64. {
  65. stop();
  66. }
  67. void TestWindow::start(int width, int height)
  68. {
  69. rdr::U32 pixel;
  70. stop();
  71. resize(x(), y(), width, height);
  72. pixels = 0;
  73. frames = 0;
  74. time = 0;
  75. fb = new PlatformPixelBuffer(w(), h());
  76. pixel = 0;
  77. fb->fillRect(fb->getRect(), &pixel);
  78. show();
  79. }
  80. void TestWindow::stop()
  81. {
  82. hide();
  83. delete fb;
  84. fb = NULL;
  85. Fl::remove_idle(timer, this);
  86. }
  87. void TestWindow::draw()
  88. {
  89. int X, Y, W, H;
  90. // We cannot update the damage region from inside the draw function,
  91. // so delegate this to an idle function
  92. Fl::add_idle(timer, this);
  93. // Check what actually needs updating
  94. fl_clip_box(0, 0, w(), h(), X, Y, W, H);
  95. if ((W == 0) || (H == 0))
  96. return;
  97. fb->draw(X, Y, X, Y, W, H);
  98. pixels += W*H;
  99. frames++;
  100. }
  101. void TestWindow::flush()
  102. {
  103. startTimeCounter();
  104. Fl_Window::flush();
  105. #if !defined(WIN32) && !defined(__APPLE__)
  106. // Make sure we measure any work we queue up
  107. XSync(fl_display, False);
  108. #endif
  109. endTimeCounter();
  110. time += getTimeCounter();
  111. }
  112. void TestWindow::update()
  113. {
  114. rfb::Rect r;
  115. startTimeCounter();
  116. changefb();
  117. r = fb->getDamage();
  118. damage(FL_DAMAGE_USER1, r.tl.x, r.tl.y, r.width(), r.height());
  119. #if !defined(WIN32) && !defined(__APPLE__)
  120. // Make sure we measure any work we queue up
  121. XSync(fl_display, False);
  122. #endif
  123. endTimeCounter();
  124. time += getTimeCounter();
  125. }
  126. void TestWindow::changefb()
  127. {
  128. rdr::U32 pixel;
  129. pixel = rand();
  130. fb->fillRect(fb->getRect(), &pixel);
  131. }
  132. void TestWindow::timer(void* data)
  133. {
  134. TestWindow* self;
  135. Fl::remove_idle(timer, data);
  136. self = (TestWindow*)data;
  137. self->update();
  138. }
  139. void PartialTestWindow::changefb()
  140. {
  141. rfb::Rect r;
  142. rdr::U32 pixel;
  143. r = fb->getRect();
  144. r.tl.x += w() / 4;
  145. r.tl.y += h() / 4;
  146. r.br.x -= w() / 4;
  147. r.br.y -= h() / 4;
  148. pixel = rand();
  149. fb->fillRect(r, &pixel);
  150. }
  151. OverlayTestWindow::OverlayTestWindow() :
  152. overlay(NULL)
  153. {
  154. }
  155. void OverlayTestWindow::start(int width, int height)
  156. {
  157. PartialTestWindow::start(width, height);
  158. overlay = new Surface(400, 200);
  159. overlay->clear(0xff, 0x80, 0x00, 0xcc);
  160. }
  161. void OverlayTestWindow::stop()
  162. {
  163. PartialTestWindow::stop();
  164. delete overlay;
  165. overlay = NULL;
  166. }
  167. void OverlayTestWindow::draw()
  168. {
  169. int ox, oy, ow, oh;
  170. int X, Y, W, H;
  171. // Check what actually needs updating
  172. fl_clip_box(0, 0, w(), h(), X, Y, W, H);
  173. if ((W == 0) || (H == 0))
  174. return;
  175. PartialTestWindow::draw();
  176. // We might get a redraw before we are fully ready
  177. if (!overlay)
  178. return;
  179. // Simplify the clip region to a simple rectangle in order to
  180. // properly draw all the layers even if they only partially overlap
  181. fl_push_no_clip();
  182. fl_push_clip(X, Y, W, H);
  183. ox = (w() - overlay->width()) / 2;
  184. oy = h() / 4 - overlay->height() / 2;
  185. ow = overlay->width();
  186. oh = overlay->height();
  187. fl_clip_box(ox, oy, ow, oh, X, Y, W, H);
  188. if ((W != 0) && (H != 0))
  189. overlay->draw(X - ox, Y - oy, X, Y, W, H);
  190. fl_pop_clip();
  191. fl_pop_clip();
  192. }
  193. static void dosubtest(TestWindow* win, int width, int height,
  194. unsigned long long* pixels,
  195. unsigned long long* frames,
  196. double* time)
  197. {
  198. struct timeval start;
  199. win->start(width, height);
  200. gettimeofday(&start, NULL);
  201. while (rfb::msSince(&start) < 3000)
  202. Fl::wait();
  203. win->stop();
  204. *pixels = win->pixels;
  205. *frames = win->frames;
  206. *time = win->time;
  207. }
  208. static bool is_constant(double a, double b)
  209. {
  210. return (fabs(a - b) / a) < 0.1;
  211. }
  212. static void dotest(TestWindow* win)
  213. {
  214. unsigned long long pixels[3];
  215. unsigned long long frames[3];
  216. double time[3];
  217. double delay, rate;
  218. char s[1024];
  219. // Run the test several times at different resolutions...
  220. dosubtest(win, 800, 600, &pixels[0], &frames[0], &time[0]);
  221. dosubtest(win, 1024, 768, &pixels[1], &frames[1], &time[1]);
  222. dosubtest(win, 1280, 960, &pixels[2], &frames[2], &time[2]);
  223. // ...in order to compute how much of the rendering time is static,
  224. // and how much depends on the number of pixels
  225. // (i.e. solve: time = delay * frames + rate * pixels)
  226. delay = (((time[0] - (double)pixels[0] / pixels[1] * time[1]) /
  227. (frames[0] - (double)pixels[0] / pixels[1] * frames[1])) +
  228. ((time[1] - (double)pixels[1] / pixels[2] * time[2]) /
  229. (frames[1] - (double)pixels[1] / pixels[2] * frames[2]))) / 2.0;
  230. rate = (((time[0] - (double)frames[0] / frames[1] * time[1]) /
  231. (pixels[0] - (double)frames[0] / frames[1] * pixels[1])) +
  232. ((time[1] - (double)frames[1] / frames[2] * time[2]) /
  233. (pixels[1] - (double)frames[1] / frames[2] * pixels[2]))) / 2.0;
  234. // However, we have some corner cases:
  235. // We are restricted by some delay, e.g. refresh rate
  236. if (is_constant(frames[0]/time[0], frames[2]/time[2])) {
  237. fprintf(stderr, "WARNING: Fixed delay dominating updates.\n\n");
  238. delay = time[2]/frames[2];
  239. rate = 0.0;
  240. }
  241. // There isn't any fixed delay, we are only restricted by pixel
  242. // throughput
  243. if (fabs(delay) < 0.001) {
  244. delay = 0.0;
  245. rate = time[2]/pixels[2];
  246. }
  247. // We can hit cache limits that causes performance to drop
  248. // with increasing update size, screwing up our calculations
  249. if ((pixels[2] / time[2]) < (pixels[0] / time[0] * 0.9)) {
  250. fprintf(stderr, "WARNING: Unexpected behaviour. Measurement unreliable.\n\n");
  251. // We can't determine the proportions between these, so divide the
  252. // time spent evenly
  253. delay = time[2] / 2.0 / frames[2];
  254. rate = time[2] / 2.0 / pixels[2];
  255. }
  256. fprintf(stderr, "Rendering delay: %g ms/frame\n", delay * 1000.0);
  257. if (rate == 0.0)
  258. strcpy(s, "N/A pixels/s");
  259. else
  260. rfb::siPrefix(1.0 / rate, "pixels/s", s, sizeof(s));
  261. fprintf(stderr, "Rendering rate: %s\n", s);
  262. fprintf(stderr, "Maximum FPS: %g fps @ 1920x1080\n",
  263. 1.0 / (delay + rate * 1920 * 1080));
  264. }
  265. int main(int argc, char** argv)
  266. {
  267. TestWindow* win;
  268. fprintf(stderr, "Full window update:\n\n");
  269. win = new TestWindow();
  270. dotest(win);
  271. delete win;
  272. fprintf(stderr, "\n");
  273. fprintf(stderr, "Partial window update:\n\n");
  274. win = new PartialTestWindow();
  275. dotest(win);
  276. delete win;
  277. fprintf(stderr, "\n");
  278. fprintf(stderr, "Partial window update with overlay:\n\n");
  279. win = new OverlayTestWindow();
  280. dotest(win);
  281. delete win;
  282. fprintf(stderr, "\n");
  283. return 0;
  284. }