benchmark-font.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "benchmark/benchmark.h"
  2. #include <cassert>
  3. #include <cstring>
  4. #ifdef HAVE_CONFIG_H
  5. #include "config.h"
  6. #endif
  7. #include "hb.h"
  8. #include "hb-ot.h"
  9. #ifdef HAVE_FREETYPE
  10. #include "hb-ft.h"
  11. #endif
  12. #define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
  13. struct test_input_t
  14. {
  15. bool is_variable;
  16. const char *font_path;
  17. } default_tests[] =
  18. {
  19. {true , SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf"},
  20. {false, SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf"},
  21. {true , SUBSET_FONT_BASE_PATH "AdobeVFPrototype.otf"},
  22. {true , SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf"},
  23. {false, SUBSET_FONT_BASE_PATH "Comfortaa-Regular-new.ttf"},
  24. {false, SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf"},
  25. {false, SUBSET_FONT_BASE_PATH "NotoSerifMyanmar-Regular.otf"},
  26. };
  27. static test_input_t *tests = default_tests;
  28. static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
  29. enum backend_t { HARFBUZZ, FREETYPE };
  30. enum operation_t
  31. {
  32. nominal_glyphs,
  33. glyph_h_advances,
  34. glyph_extents,
  35. glyph_shape,
  36. };
  37. static void
  38. _hb_move_to (hb_draw_funcs_t *, void *, hb_draw_state_t *, float, float, void *) {}
  39. static void
  40. _hb_line_to (hb_draw_funcs_t *, void *, hb_draw_state_t *, float, float, void *) {}
  41. //static void
  42. //_hb_quadratic_to (hb_draw_funcs_t *, void *, hb_draw_state_t *, float, float, float, float, void *) {}
  43. static void
  44. _hb_cubic_to (hb_draw_funcs_t *, void *, hb_draw_state_t *, float, float, float, float, float, float, void *) {}
  45. static void
  46. _hb_close_path (hb_draw_funcs_t *, void *, hb_draw_state_t *, void *) {}
  47. static hb_draw_funcs_t *
  48. _draw_funcs_create (void)
  49. {
  50. hb_draw_funcs_t *draw_funcs = hb_draw_funcs_create ();
  51. hb_draw_funcs_set_move_to_func (draw_funcs, _hb_move_to, nullptr, nullptr);
  52. hb_draw_funcs_set_line_to_func (draw_funcs, _hb_line_to, nullptr, nullptr);
  53. //hb_draw_funcs_set_quadratic_to_func (draw_funcs, _hb_quadratic_to, nullptr, nullptr);
  54. hb_draw_funcs_set_cubic_to_func (draw_funcs, _hb_cubic_to, nullptr, nullptr);
  55. hb_draw_funcs_set_close_path_func (draw_funcs, _hb_close_path, nullptr, nullptr);
  56. return draw_funcs;
  57. }
  58. static void BM_Font (benchmark::State &state,
  59. bool is_var, backend_t backend, operation_t operation,
  60. const test_input_t &test_input)
  61. {
  62. hb_font_t *font;
  63. unsigned num_glyphs;
  64. {
  65. hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_input.font_path);
  66. assert (blob);
  67. hb_face_t *face = hb_face_create (blob, 0);
  68. hb_blob_destroy (blob);
  69. num_glyphs = hb_face_get_glyph_count (face);
  70. font = hb_font_create (face);
  71. hb_face_destroy (face);
  72. }
  73. if (is_var)
  74. {
  75. hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
  76. hb_font_set_variations (font, &wght, 1);
  77. }
  78. switch (backend)
  79. {
  80. case HARFBUZZ:
  81. hb_ot_font_set_funcs (font);
  82. break;
  83. case FREETYPE:
  84. #ifdef HAVE_FREETYPE
  85. hb_ft_font_set_funcs (font);
  86. #endif
  87. break;
  88. }
  89. switch (operation)
  90. {
  91. case nominal_glyphs:
  92. {
  93. hb_set_t *set = hb_set_create ();
  94. hb_face_collect_unicodes (hb_font_get_face (font), set);
  95. unsigned pop = hb_set_get_population (set);
  96. hb_codepoint_t *unicodes = (hb_codepoint_t *) calloc (pop, sizeof (hb_codepoint_t));
  97. hb_codepoint_t *glyphs = (hb_codepoint_t *) calloc (pop, sizeof (hb_codepoint_t));
  98. hb_codepoint_t *p = unicodes;
  99. for (hb_codepoint_t u = HB_SET_VALUE_INVALID;
  100. hb_set_next (set, &u);)
  101. *p++ = u;
  102. assert (p == unicodes + pop);
  103. for (auto _ : state)
  104. hb_font_get_nominal_glyphs (font,
  105. pop,
  106. unicodes, sizeof (*unicodes),
  107. glyphs, sizeof (*glyphs));
  108. free (glyphs);
  109. free (unicodes);
  110. hb_set_destroy (set);
  111. break;
  112. }
  113. case glyph_h_advances:
  114. {
  115. hb_codepoint_t *glyphs = (hb_codepoint_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
  116. hb_position_t *advances = (hb_position_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
  117. for (unsigned g = 0; g < num_glyphs; g++)
  118. glyphs[g] = g;
  119. for (auto _ : state)
  120. hb_font_get_glyph_h_advances (font,
  121. num_glyphs,
  122. glyphs, sizeof (*glyphs),
  123. advances, sizeof (*advances));
  124. free (advances);
  125. free (glyphs);
  126. break;
  127. }
  128. case glyph_extents:
  129. {
  130. hb_glyph_extents_t extents;
  131. for (auto _ : state)
  132. for (unsigned gid = 0; gid < num_glyphs; ++gid)
  133. hb_font_get_glyph_extents (font, gid, &extents);
  134. break;
  135. }
  136. case glyph_shape:
  137. {
  138. hb_draw_funcs_t *draw_funcs = _draw_funcs_create ();
  139. for (auto _ : state)
  140. for (unsigned gid = 0; gid < num_glyphs; ++gid)
  141. hb_font_get_glyph_shape (font, gid, draw_funcs, nullptr);
  142. break;
  143. hb_draw_funcs_destroy (draw_funcs);
  144. }
  145. }
  146. hb_font_destroy (font);
  147. }
  148. static void test_backend (backend_t backend,
  149. const char *backend_name,
  150. bool variable,
  151. operation_t op,
  152. const char *op_name,
  153. benchmark::TimeUnit time_unit,
  154. const test_input_t &test_input)
  155. {
  156. char name[1024] = "BM_Font/";
  157. strcat (name, op_name);
  158. strcat (name, "/");
  159. const char *p = strrchr (test_input.font_path, '/');
  160. strcat (name, p ? p + 1 : test_input.font_path);
  161. strcat (name, variable ? "/var" : "");
  162. strcat (name, "/");
  163. strcat (name, backend_name);
  164. benchmark::RegisterBenchmark (name, BM_Font, variable, backend, op, test_input)
  165. ->Unit(time_unit);
  166. }
  167. static void test_operation (operation_t op,
  168. const char *op_name,
  169. benchmark::TimeUnit time_unit)
  170. {
  171. for (unsigned i = 0; i < num_tests; i++)
  172. {
  173. auto& test_input = tests[i];
  174. for (int variable = 0; variable < int (test_input.is_variable) + 1; variable++)
  175. {
  176. bool is_var = (bool) variable;
  177. test_backend (HARFBUZZ, "hb", is_var, op, op_name, time_unit, test_input);
  178. #ifdef HAVE_FREETYPE
  179. test_backend (FREETYPE, "ft", is_var, op, op_name, time_unit, test_input);
  180. #endif
  181. }
  182. }
  183. }
  184. int main(int argc, char** argv)
  185. {
  186. benchmark::Initialize(&argc, argv);
  187. if (argc > 1)
  188. {
  189. num_tests = argc - 1;
  190. tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
  191. for (unsigned i = 0; i < num_tests; i++)
  192. {
  193. tests[i].is_variable = true;
  194. tests[i].font_path = argv[i + 1];
  195. }
  196. }
  197. #define TEST_OPERATION(op, time_unit) test_operation (op, #op, time_unit)
  198. TEST_OPERATION (nominal_glyphs, benchmark::kMicrosecond);
  199. TEST_OPERATION (glyph_h_advances, benchmark::kMicrosecond);
  200. TEST_OPERATION (glyph_extents, benchmark::kMicrosecond);
  201. TEST_OPERATION (glyph_shape, benchmark::kMicrosecond);
  202. #undef TEST_OPERATION
  203. benchmark::RunSpecifiedBenchmarks();
  204. benchmark::Shutdown();
  205. if (tests != default_tests)
  206. free (tests);
  207. }