Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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