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