benchmark-font.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #include "hb-benchmark.hh"
  2. #define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
  3. struct test_input_t
  4. {
  5. bool is_variable;
  6. const char *font_path;
  7. } default_tests[] =
  8. {
  9. {false, SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf"},
  10. {true , SUBSET_FONT_BASE_PATH "RobotoFlex-Variable.ttf"},
  11. {false, SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf"},
  12. {true , SUBSET_FONT_BASE_PATH "AdobeVFPrototype.otf"},
  13. {true , SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf"},
  14. {false, SUBSET_FONT_BASE_PATH "Comfortaa-Regular-new.ttf"},
  15. {false, SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf"},
  16. {false, SUBSET_FONT_BASE_PATH "NotoSerifMyanmar-Regular.otf"},
  17. };
  18. static test_input_t *tests = default_tests;
  19. static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
  20. enum backend_t { HARFBUZZ, FREETYPE, CORETEXT };
  21. enum operation_t
  22. {
  23. nominal_glyphs,
  24. glyph_h_advances,
  25. glyph_extents,
  26. draw_glyph,
  27. paint_glyph,
  28. load_face_and_shape,
  29. };
  30. static void
  31. _hb_move_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, float x, float y, void *)
  32. {
  33. float &i = * (float *) draw_data;
  34. i += x + y;
  35. }
  36. static void
  37. _hb_line_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, float x, float y, void *)
  38. {
  39. float &i = * (float *) draw_data;
  40. i += x + y;
  41. }
  42. static void
  43. _hb_quadratic_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, float cx, float cy, float x, float y, void *)
  44. {
  45. float &i = * (float *) draw_data;
  46. i += cx + cy + x + y;
  47. }
  48. static void
  49. _hb_cubic_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, float cx1, float cy1, float cx2, float cy2, float x, float y, void *)
  50. {
  51. float &i = * (float *) draw_data;
  52. i += cx1 + cy1 + cx2 + cy2 + x + y;
  53. }
  54. static void
  55. _hb_close_path (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, void *)
  56. {
  57. float &i = * (float *) draw_data;
  58. i += 1.0;
  59. }
  60. static hb_draw_funcs_t *
  61. _draw_funcs_create (void)
  62. {
  63. hb_draw_funcs_t *draw_funcs = hb_draw_funcs_create ();
  64. hb_draw_funcs_set_move_to_func (draw_funcs, _hb_move_to, nullptr, nullptr);
  65. hb_draw_funcs_set_line_to_func (draw_funcs, _hb_line_to, nullptr, nullptr);
  66. hb_draw_funcs_set_quadratic_to_func (draw_funcs, _hb_quadratic_to, nullptr, nullptr);
  67. hb_draw_funcs_set_cubic_to_func (draw_funcs, _hb_cubic_to, nullptr, nullptr);
  68. hb_draw_funcs_set_close_path_func (draw_funcs, _hb_close_path, nullptr, nullptr);
  69. return draw_funcs;
  70. }
  71. static void BM_Font (benchmark::State &state,
  72. bool is_var, backend_t backend, operation_t operation,
  73. const test_input_t &test_input)
  74. {
  75. hb_font_t *font;
  76. unsigned num_glyphs;
  77. {
  78. hb_face_t *face = hb_benchmark_face_create_from_file_or_fail (test_input.font_path, 0);
  79. assert (face);
  80. num_glyphs = hb_face_get_glyph_count (face);
  81. font = hb_font_create (face);
  82. hb_face_destroy (face);
  83. }
  84. if (is_var)
  85. {
  86. hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
  87. hb_font_set_variations (font, &wght, 1);
  88. }
  89. switch (backend)
  90. {
  91. case HARFBUZZ:
  92. hb_ot_font_set_funcs (font);
  93. break;
  94. case FREETYPE:
  95. #ifdef HAVE_FREETYPE
  96. hb_ft_font_set_funcs (font);
  97. #endif
  98. break;
  99. case CORETEXT:
  100. #ifdef HAVE_CORETEXT
  101. hb_coretext_font_set_funcs (font);
  102. #endif
  103. break;
  104. }
  105. switch (operation)
  106. {
  107. case nominal_glyphs:
  108. {
  109. hb_set_t *set = hb_set_create ();
  110. hb_face_collect_unicodes (hb_font_get_face (font), set);
  111. unsigned pop = hb_set_get_population (set);
  112. hb_codepoint_t *unicodes = (hb_codepoint_t *) calloc (pop, sizeof (hb_codepoint_t));
  113. hb_codepoint_t *glyphs = (hb_codepoint_t *) calloc (pop, sizeof (hb_codepoint_t));
  114. hb_codepoint_t *p = unicodes;
  115. for (hb_codepoint_t u = HB_SET_VALUE_INVALID;
  116. hb_set_next (set, &u);)
  117. *p++ = u;
  118. assert (p == unicodes + pop);
  119. for (auto _ : state)
  120. hb_font_get_nominal_glyphs (font,
  121. pop,
  122. unicodes, sizeof (*unicodes),
  123. glyphs, sizeof (*glyphs));
  124. free (glyphs);
  125. free (unicodes);
  126. hb_set_destroy (set);
  127. break;
  128. }
  129. case glyph_h_advances:
  130. {
  131. hb_codepoint_t *glyphs = (hb_codepoint_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
  132. hb_position_t *advances = (hb_position_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
  133. for (unsigned g = 0; g < num_glyphs; g++)
  134. glyphs[g] = g;
  135. for (auto _ : state)
  136. hb_font_get_glyph_h_advances (font,
  137. num_glyphs,
  138. glyphs, sizeof (*glyphs),
  139. advances, sizeof (*advances));
  140. free (advances);
  141. free (glyphs);
  142. break;
  143. }
  144. case glyph_extents:
  145. {
  146. hb_glyph_extents_t extents;
  147. for (auto _ : state)
  148. for (unsigned gid = 0; gid < num_glyphs; ++gid)
  149. hb_font_get_glyph_extents (font, gid, &extents);
  150. break;
  151. }
  152. case draw_glyph:
  153. {
  154. hb_draw_funcs_t *draw_funcs = _draw_funcs_create ();
  155. for (auto _ : state)
  156. {
  157. float i = 0;
  158. for (unsigned gid = 0; gid < num_glyphs; ++gid)
  159. hb_font_draw_glyph (font, gid, draw_funcs, &i);
  160. }
  161. hb_draw_funcs_destroy (draw_funcs);
  162. break;
  163. }
  164. case paint_glyph:
  165. {
  166. hb_paint_funcs_t *paint_funcs = hb_paint_funcs_create ();
  167. for (auto _ : state)
  168. {
  169. for (unsigned gid = 0; gid < num_glyphs; ++gid)
  170. hb_font_paint_glyph (font, gid, paint_funcs, nullptr, 0, 0);
  171. }
  172. hb_paint_funcs_destroy (paint_funcs);
  173. break;
  174. }
  175. case load_face_and_shape:
  176. {
  177. for (auto _ : state)
  178. {
  179. hb_face_t *face = hb_benchmark_face_create_from_file_or_fail (test_input.font_path, 0);
  180. assert (face);
  181. hb_font_t *font = hb_font_create (face);
  182. hb_face_destroy (face);
  183. switch (backend)
  184. {
  185. case HARFBUZZ:
  186. hb_ot_font_set_funcs (font);
  187. break;
  188. case FREETYPE:
  189. #ifdef HAVE_FREETYPE
  190. hb_ft_font_set_funcs (font);
  191. #endif
  192. break;
  193. case CORETEXT:
  194. #ifdef HAVE_CORETEXT
  195. hb_coretext_font_set_funcs (font);
  196. #endif
  197. break;
  198. }
  199. hb_buffer_t *buffer = hb_buffer_create ();
  200. hb_buffer_add_utf8 (buffer, " ", -1, 0, -1);
  201. hb_buffer_guess_segment_properties (buffer);
  202. hb_shape (font, buffer, nullptr, 0);
  203. hb_buffer_destroy (buffer);
  204. hb_font_destroy (font);
  205. }
  206. break;
  207. }
  208. }
  209. hb_font_destroy (font);
  210. }
  211. static void test_backend (backend_t backend,
  212. const char *backend_name,
  213. bool variable,
  214. operation_t op,
  215. const char *op_name,
  216. benchmark::TimeUnit time_unit,
  217. const test_input_t &test_input)
  218. {
  219. char name[1024] = "BM_Font/";
  220. strcat (name, op_name);
  221. strcat (name, "/");
  222. const char *p = strrchr (test_input.font_path, '/');
  223. strcat (name, p ? p + 1 : test_input.font_path);
  224. strcat (name, variable ? "/var" : "");
  225. strcat (name, "/");
  226. strcat (name, backend_name);
  227. benchmark::RegisterBenchmark (name, BM_Font, variable, backend, op, test_input)
  228. ->Unit(time_unit);
  229. }
  230. static void test_operation (operation_t op,
  231. const char *op_name,
  232. benchmark::TimeUnit time_unit)
  233. {
  234. for (unsigned i = 0; i < num_tests; i++)
  235. {
  236. auto& test_input = tests[i];
  237. for (int variable = 0; variable < int (test_input.is_variable) + 1; variable++)
  238. {
  239. bool is_var = (bool) variable;
  240. test_backend (HARFBUZZ, "hb", is_var, op, op_name, time_unit, test_input);
  241. #ifdef HAVE_FREETYPE
  242. test_backend (FREETYPE, "ft", is_var, op, op_name, time_unit, test_input);
  243. #endif
  244. #ifdef HAVE_CORETEXT
  245. test_backend (CORETEXT, "coretext", is_var, op, op_name, time_unit, test_input);
  246. #endif
  247. }
  248. }
  249. }
  250. int main(int argc, char** argv)
  251. {
  252. benchmark::Initialize(&argc, argv);
  253. if (argc > 1)
  254. {
  255. num_tests = argc - 1;
  256. tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
  257. for (unsigned i = 0; i < num_tests; i++)
  258. {
  259. tests[i].is_variable = true;
  260. tests[i].font_path = argv[i + 1];
  261. }
  262. }
  263. #define TEST_OPERATION(op, time_unit) test_operation (op, #op, time_unit)
  264. TEST_OPERATION (nominal_glyphs, benchmark::kMicrosecond);
  265. TEST_OPERATION (glyph_h_advances, benchmark::kMicrosecond);
  266. TEST_OPERATION (glyph_extents, benchmark::kMicrosecond);
  267. TEST_OPERATION (draw_glyph, benchmark::kMicrosecond);
  268. TEST_OPERATION (paint_glyph, benchmark::kMillisecond);
  269. TEST_OPERATION (load_face_and_shape, benchmark::kMicrosecond);
  270. #undef TEST_OPERATION
  271. benchmark::RunSpecifiedBenchmarks();
  272. benchmark::Shutdown();
  273. if (tests != default_tests)
  274. free (tests);
  275. }