hb-shape-threads.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <cassert>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <thread>
  6. #include <condition_variable>
  7. #include <vector>
  8. #ifdef HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif
  11. #include "hb.h"
  12. #include "hb-ot.h"
  13. #ifdef HAVE_FREETYPE
  14. #include "hb-ft.h"
  15. #endif
  16. #define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
  17. struct test_input_t
  18. {
  19. const char *font_path;
  20. const char *text_path;
  21. bool is_variable;
  22. } default_tests[] =
  23. {
  24. {"perf/fonts/NotoNastaliqUrdu-Regular.ttf",
  25. "perf/texts/fa-thelittleprince.txt",
  26. false},
  27. {"perf/fonts/Amiri-Regular.ttf",
  28. "perf/texts/fa-thelittleprince.txt",
  29. false},
  30. {"perf/fonts/Roboto-Regular.ttf",
  31. "perf/texts/en-thelittleprince.txt",
  32. false},
  33. {"perf/fonts/Roboto-Regular.ttf",
  34. "perf/texts/en-words.txt",
  35. false},
  36. {SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf",
  37. "perf/texts/en-thelittleprince.txt",
  38. true},
  39. };
  40. static test_input_t *tests = default_tests;
  41. static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
  42. enum backend_t { HARFBUZZ, FREETYPE };
  43. // https://en.cppreference.com/w/cpp/thread/condition_variable/wait
  44. static std::condition_variable cv;
  45. static std::mutex cv_m;
  46. static bool ready = false;
  47. static unsigned num_repetitions = 1;
  48. static unsigned num_threads = 3;
  49. static void shape (const test_input_t &input,
  50. hb_font_t *font)
  51. {
  52. // Wait till all threads are ready.
  53. {
  54. std::unique_lock<std::mutex> lk (cv_m);
  55. cv.wait(lk, [] {return ready;});
  56. }
  57. const char *lang_str = strrchr (input.text_path, '/');
  58. lang_str = lang_str ? lang_str + 1 : input.text_path;
  59. hb_language_t language = hb_language_from_string (lang_str, -1);
  60. hb_blob_t *text_blob = hb_blob_create_from_file_or_fail (input.text_path);
  61. assert (text_blob);
  62. unsigned orig_text_length;
  63. const char *orig_text = hb_blob_get_data (text_blob, &orig_text_length);
  64. hb_buffer_t *buf = hb_buffer_create ();
  65. hb_buffer_set_flags (buf, HB_BUFFER_FLAG_VERIFY);
  66. for (unsigned i = 0; i < num_repetitions; i++)
  67. {
  68. unsigned text_length = orig_text_length;
  69. const char *text = orig_text;
  70. const char *end;
  71. while ((end = (const char *) memchr (text, '\n', text_length)))
  72. {
  73. hb_buffer_clear_contents (buf);
  74. hb_buffer_add_utf8 (buf, text, text_length, 0, end - text);
  75. hb_buffer_guess_segment_properties (buf);
  76. hb_buffer_set_language (buf, language);
  77. hb_shape (font, buf, nullptr, 0);
  78. unsigned skip = end - text + 1;
  79. text_length -= skip;
  80. text += skip;
  81. }
  82. }
  83. hb_buffer_destroy (buf);
  84. hb_blob_destroy (text_blob);
  85. }
  86. static void test_backend (backend_t backend,
  87. const char *backend_name,
  88. bool variable,
  89. const test_input_t &test_input)
  90. {
  91. char name[1024] = "shape";
  92. const char *p;
  93. strcat (name, "/");
  94. p = strrchr (test_input.font_path, '/');
  95. strcat (name, p ? p + 1 : test_input.font_path);
  96. strcat (name, "/");
  97. p = strrchr (test_input.text_path, '/');
  98. strcat (name, p ? p + 1 : test_input.text_path);
  99. strcat (name, variable ? "/var" : "");
  100. strcat (name, "/");
  101. strcat (name, backend_name);
  102. printf ("Testing %s\n", name);
  103. hb_font_t *font;
  104. {
  105. hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_input.font_path);
  106. assert (blob);
  107. hb_face_t *face = hb_face_create (blob, 0);
  108. hb_blob_destroy (blob);
  109. font = hb_font_create (face);
  110. hb_face_destroy (face);
  111. }
  112. if (variable)
  113. {
  114. hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
  115. hb_font_set_variations (font, &wght, 1);
  116. }
  117. switch (backend)
  118. {
  119. case HARFBUZZ:
  120. hb_ot_font_set_funcs (font);
  121. break;
  122. case FREETYPE:
  123. #ifdef HAVE_FREETYPE
  124. hb_ft_font_set_funcs (font);
  125. #endif
  126. break;
  127. }
  128. std::vector<std::thread> threads;
  129. for (unsigned i = 0; i < num_threads; i++)
  130. threads.push_back (std::thread (shape, test_input, font));
  131. {
  132. std::unique_lock<std::mutex> lk (cv_m);
  133. ready = true;
  134. }
  135. cv.notify_all();
  136. for (unsigned i = 0; i < num_threads; i++)
  137. threads[i].join ();
  138. hb_font_destroy (font);
  139. }
  140. int main(int argc, char** argv)
  141. {
  142. if (argc > 1)
  143. num_threads = atoi (argv[1]);
  144. if (argc > 2)
  145. num_repetitions = atoi (argv[2]);
  146. /* Dummy call to alleviate _guess_segment_properties thread safety-ness
  147. * https://github.com/harfbuzz/harfbuzz/issues/1191 */
  148. hb_language_get_default ();
  149. if (argc > 4)
  150. {
  151. num_tests = (argc - 3) / 2;
  152. tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
  153. for (unsigned i = 0; i < num_tests; i++)
  154. {
  155. tests[i].is_variable = true;
  156. tests[i].font_path = argv[3 + i * 2];
  157. tests[i].text_path = argv[4 + i * 2];
  158. }
  159. }
  160. printf ("Num threads %u; num repetitions %u\n", num_threads, num_repetitions);
  161. for (unsigned i = 0; i < num_tests; i++)
  162. {
  163. auto& test_input = tests[i];
  164. for (int variable = 0; variable < int (test_input.is_variable) + 1; variable++)
  165. {
  166. bool is_var = (bool) variable;
  167. test_backend (HARFBUZZ, "hb", is_var, test_input);
  168. #ifdef HAVE_FREETYPE
  169. test_backend (FREETYPE, "ft", is_var, test_input);
  170. #endif
  171. }
  172. }
  173. if (tests != default_tests)
  174. free (tests);
  175. }