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