gfxbench.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* gfxbench: time the software drawing paths, so each rendering step can be
  2. * measured on target instead of guessed.
  3. *
  4. * The fill/copy/cell/canvas cases run against a screen-sized RAM layer, which
  5. * is what the window manager draws into (the shadow buffer), and the flush
  6. * case times the real shadow -> VRAM copy. The screen itself is never
  7. * disturbed: no drawing lands in the shadow, so the flush copies the WM's
  8. * unchanged content. */
  9. #include "blit.h"
  10. #include "cpu.h"
  11. #include "fbtext.h"
  12. #include "gfx.h"
  13. #include "io.h"
  14. #include "mm.h"
  15. #include "paging.h"
  16. #include "timer.h"
  17. #include "vga.h"
  18. #define LAYER_W 1280
  19. #define LAYER_H 1024
  20. #define CONTENT_W 514
  21. #define CONTENT_H 320
  22. #define WIN_W 514
  23. #define WIN_H 338
  24. #define CELLS_COLS 64
  25. #define CELLS_ROWS 32
  26. static void put_dec(unsigned int n)
  27. {
  28. char buf[11];
  29. unsigned int i = sizeof(buf);
  30. if (n == 0) {
  31. io_putchar('0');
  32. return;
  33. }
  34. while (n && i) {
  35. buf[--i] = (char)('0' + n % 10);
  36. n /= 10;
  37. }
  38. while (i < sizeof(buf))
  39. io_putchar(buf[i++]);
  40. }
  41. static void bench(const char *name, unsigned int n, unsigned int bytes,
  42. unsigned int ms)
  43. {
  44. io_putstr(name);
  45. io_putstr(" ");
  46. put_dec(n);
  47. io_putstr("x ");
  48. put_dec(ms);
  49. io_putstr(" ms");
  50. if (ms) {
  51. /* 32-bit friendly MB/s: bytes/1024 is small enough to scale. */
  52. unsigned int kbs = (bytes / 1024) * n;
  53. unsigned int mbs = (kbs * 1000 / ms) / 1024;
  54. io_putstr(" ");
  55. put_dec(mbs);
  56. io_putstr(" MB/s");
  57. }
  58. io_putstr("\n");
  59. }
  60. static unsigned char *alloc_pages(unsigned int bytes)
  61. {
  62. unsigned int pages = (bytes + 4095) / 4096;
  63. return (unsigned char *)mm_alloc_pages(pages);
  64. }
  65. static void free_pages(unsigned char *p, unsigned int bytes)
  66. {
  67. unsigned int pages = (bytes + 4095) / 4096;
  68. unsigned int i;
  69. for (i = 0; i < pages; i++)
  70. mm_free_page((unsigned int)p + i * 4096);
  71. }
  72. void program_gfxbench_main(void)
  73. {
  74. struct surface layer;
  75. struct gfx_canvas canvas;
  76. unsigned short *cells;
  77. unsigned char *layer_px, *cell_px;
  78. unsigned int t0, t1, i;
  79. unsigned int layer_bytes = LAYER_W * LAYER_H * 4;
  80. io_putstr("gfxbench (sse2: ");
  81. io_putstr(cpu_has_sse2() ? "yes" : "no");
  82. io_putstr(", lfb wc: ");
  83. io_putstr(paging_lfb_wc() ? "yes" : "no");
  84. io_putstr(")\n");
  85. layer_px = alloc_pages(layer_bytes);
  86. if (!layer_px) {
  87. io_putstr("gfxbench: out of memory\n");
  88. return;
  89. }
  90. layer.px = layer_px;
  91. layer.w = LAYER_W;
  92. layer.h = LAYER_H;
  93. layer.pitch = LAYER_W * 4;
  94. layer.bpp = 32;
  95. layer.alpha = 255;
  96. vga_set_target(&layer);
  97. /* Full-screen fill: what a full redraw costs. */
  98. t0 = get_ticks_ms();
  99. for (i = 0; i < 10; i++)
  100. blit->fill(0, 0, LAYER_W, LAYER_H, 0x203040);
  101. t1 = get_ticks_ms();
  102. bench("fill screen ", 10, layer_bytes, t1 - t0);
  103. /* Window content fill (the per-cell background). */
  104. t0 = get_ticks_ms();
  105. for (i = 0; i < 100; i++)
  106. blit->fill(0, 0, CONTENT_W, CONTENT_H, 0x000000);
  107. t1 = get_ticks_ms();
  108. bench("fill content", 100, CONTENT_W * CONTENT_H * 4, t1 - t0);
  109. /* Window move: an overlapping rect copy, as a drag step does. */
  110. t0 = get_ticks_ms();
  111. for (i = 0; i < 100; i++)
  112. blit->copy(1, 1, 0, 0, WIN_W, WIN_H);
  113. t1 = get_ticks_ms();
  114. bench("copy window ", 100, WIN_W * WIN_H * 4, t1 - t0);
  115. /* Cell grid: a whole terminal content repaint (glyph blitting). */
  116. cell_px = alloc_pages(CELLS_COLS * CELLS_ROWS * 2);
  117. if (cell_px) {
  118. cells = (unsigned short *)cell_px;
  119. for (i = 0; i < CELLS_COLS * CELLS_ROWS; i++)
  120. cells[i] = (unsigned short)(0x0A00 | ('A' + (i % 26)));
  121. t0 = get_ticks_ms();
  122. for (i = 0; i < 50; i++)
  123. fbtext_draw_cells_clip(cells, CELLS_COLS, CELLS_ROWS,
  124. 0, 0, 0, 0, CELLS_COLS * 8, CELLS_ROWS * 10);
  125. t1 = get_ticks_ms();
  126. bench("cells 64x32 ", 50, CELLS_COLS * CELLS_ROWS * 80 * 4, t1 - t0);
  127. free_pages(cell_px, CELLS_COLS * CELLS_ROWS * 2);
  128. }
  129. /* Canvas presentation: RGB565 -> 32 bpp expansion. */
  130. if (gfx_alloc(&canvas, CONTENT_W, CONTENT_H) == 0) {
  131. for (i = 0; i < CONTENT_W * CONTENT_H; i++)
  132. canvas.px[i] = (unsigned short)(i * 7);
  133. t0 = get_ticks_ms();
  134. for (i = 0; i < 20; i++)
  135. blit->canvas(&canvas, 0, 0, 0, 0, CONTENT_W, CONTENT_H);
  136. t1 = get_ticks_ms();
  137. bench("canvas 565 ", 20, CONTENT_W * CONTENT_H * 4, t1 - t0);
  138. gfx_free(&canvas);
  139. }
  140. vga_set_target(0);
  141. /* Flush: shadow -> VRAM (bus bound, not CPU bound). Report the bytes
  142. * the presenter really copied: a long flush can be preempted, and a
  143. * flush started while another process is inside one is skipped. */
  144. {
  145. unsigned int before = vga_present_copied;
  146. t0 = get_ticks_ms();
  147. for (i = 0; i < 2; i++) {
  148. vga_fb_mark_dirty_rect(0, vga_fb_height());
  149. vga_fb_flush();
  150. }
  151. t1 = get_ticks_ms();
  152. /* one "run" whose size is the bytes actually copied */
  153. bench("flush screen", 1, vga_present_copied - before, t1 - t0);
  154. }
  155. free_pages(layer_px, layer_bytes);
  156. }