hb-shape-threads.cc 5.0 KB

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