benchmark-shape.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "benchmark/benchmark.h"
  2. #include <cstring>
  3. #ifdef HAVE_CONFIG_H
  4. #include "config.h"
  5. #endif
  6. #include <cassert>
  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. const char *font_path;
  16. const char *text_path;
  17. bool is_variable;
  18. } default_tests[] =
  19. {
  20. {"perf/fonts/NotoNastaliqUrdu-Regular.ttf",
  21. "perf/texts/fa-thelittleprince.txt",
  22. false},
  23. {"perf/fonts/NotoNastaliqUrdu-Regular.ttf",
  24. "perf/texts/fa-words.txt",
  25. false},
  26. {"perf/fonts/Amiri-Regular.ttf",
  27. "perf/texts/fa-thelittleprince.txt",
  28. false},
  29. {SUBSET_FONT_BASE_PATH "NotoSansDevanagari-Regular.ttf",
  30. "perf/texts/hi-words.txt",
  31. false},
  32. {"perf/fonts/Roboto-Regular.ttf",
  33. "perf/texts/en-thelittleprince.txt",
  34. false},
  35. {"perf/fonts/Roboto-Regular.ttf",
  36. "perf/texts/en-words.txt",
  37. false},
  38. {SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf",
  39. "perf/texts/en-thelittleprince.txt",
  40. true},
  41. };
  42. static test_input_t *tests = default_tests;
  43. static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
  44. enum backend_t { HARFBUZZ, FREETYPE };
  45. static void BM_Shape (benchmark::State &state,
  46. bool is_var,
  47. backend_t backend,
  48. const test_input_t &input)
  49. {
  50. hb_font_t *font;
  51. {
  52. hb_blob_t *blob = hb_blob_create_from_file_or_fail (input.font_path);
  53. assert (blob);
  54. hb_face_t *face = hb_face_create (blob, 0);
  55. hb_blob_destroy (blob);
  56. font = hb_font_create (face);
  57. hb_face_destroy (face);
  58. }
  59. if (is_var)
  60. {
  61. hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
  62. hb_font_set_variations (font, &wght, 1);
  63. }
  64. switch (backend)
  65. {
  66. case HARFBUZZ:
  67. hb_ot_font_set_funcs (font);
  68. break;
  69. case FREETYPE:
  70. #ifdef HAVE_FREETYPE
  71. hb_ft_font_set_funcs (font);
  72. #endif
  73. break;
  74. }
  75. hb_blob_t *text_blob = hb_blob_create_from_file_or_fail (input.text_path);
  76. assert (text_blob);
  77. unsigned orig_text_length;
  78. const char *orig_text = hb_blob_get_data (text_blob, &orig_text_length);
  79. hb_buffer_t *buf = hb_buffer_create ();
  80. for (auto _ : state)
  81. {
  82. unsigned text_length = orig_text_length;
  83. const char *text = orig_text;
  84. const char *end;
  85. while ((end = (const char *) memchr (text, '\n', text_length)))
  86. {
  87. hb_buffer_clear_contents (buf);
  88. hb_buffer_add_utf8 (buf, text, text_length, 0, end - text);
  89. hb_buffer_guess_segment_properties (buf);
  90. hb_shape (font, buf, nullptr, 0);
  91. unsigned skip = end - text + 1;
  92. text_length -= skip;
  93. text += skip;
  94. }
  95. }
  96. hb_buffer_destroy (buf);
  97. hb_blob_destroy (text_blob);
  98. hb_font_destroy (font);
  99. }
  100. static void test_backend (backend_t backend,
  101. const char *backend_name,
  102. bool variable,
  103. const test_input_t &test_input)
  104. {
  105. char name[1024] = "BM_Shape";
  106. const char *p;
  107. strcat (name, "/");
  108. p = strrchr (test_input.font_path, '/');
  109. strcat (name, p ? p + 1 : test_input.font_path);
  110. strcat (name, "/");
  111. p = strrchr (test_input.text_path, '/');
  112. strcat (name, p ? p + 1 : test_input.text_path);
  113. strcat (name, variable ? "/var" : "");
  114. strcat (name, "/");
  115. strcat (name, backend_name);
  116. benchmark::RegisterBenchmark (name, BM_Shape, variable, backend, test_input)
  117. ->Unit(benchmark::kMillisecond);
  118. }
  119. int main(int argc, char** argv)
  120. {
  121. benchmark::Initialize(&argc, argv);
  122. if (argc > 2)
  123. {
  124. num_tests = (argc - 1) / 2;
  125. tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
  126. for (unsigned i = 0; i < num_tests; i++)
  127. {
  128. tests[i].is_variable = true;
  129. tests[i].font_path = argv[1 + i * 2];
  130. tests[i].text_path = argv[2 + i * 2];
  131. }
  132. }
  133. for (unsigned i = 0; i < num_tests; i++)
  134. {
  135. auto& test_input = tests[i];
  136. for (int variable = 0; variable < int (test_input.is_variable) + 1; variable++)
  137. {
  138. bool is_var = (bool) variable;
  139. test_backend (HARFBUZZ, "hb", is_var, test_input);
  140. #ifdef HAVE_FREETYPE
  141. test_backend (FREETYPE, "ft", is_var, test_input);
  142. #endif
  143. }
  144. }
  145. benchmark::RunSpecifiedBenchmarks();
  146. benchmark::Shutdown();
  147. if (tests != default_tests)
  148. free (tests);
  149. }