1 #include "benchmark/benchmark.h"
2 #include <cassert>
3 #include <cstring>
4
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
8
9 #include "hb.h"
10 #include "hb-ot.h"
11 #ifdef HAVE_FREETYPE
12 #include "hb-ft.h"
13 #endif
14
15
16 #define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
17
18 struct test_input_t
19 {
20 bool is_variable;
21 const char *font_path;
22 } default_tests[] =
23 {
24 {false, SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf"},
25 {true , SUBSET_FONT_BASE_PATH "RobotoFlex-Variable.ttf"},
26 {false, SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf"},
27 {true , SUBSET_FONT_BASE_PATH "AdobeVFPrototype.otf"},
28 {true , SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf"},
29 {false, SUBSET_FONT_BASE_PATH "Comfortaa-Regular-new.ttf"},
30 {false, SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf"},
31 {false, SUBSET_FONT_BASE_PATH "NotoSerifMyanmar-Regular.otf"},
32 };
33
34 static test_input_t *tests = default_tests;
35 static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
36
37 enum backend_t { HARFBUZZ, FREETYPE };
38
39 enum operation_t
40 {
41 nominal_glyphs,
42 glyph_h_advances,
43 glyph_extents,
44 draw_glyph,
45 paint_glyph,
46 load_face_and_shape,
47 };
48
49 static void
_hb_move_to(hb_draw_funcs_t *,void * draw_data,hb_draw_state_t *,float x,float y,void *)50 _hb_move_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, float x, float y, void *)
51 {
52 float &i = * (float *) draw_data;
53 i += x + y;
54 }
55
56 static void
_hb_line_to(hb_draw_funcs_t *,void * draw_data,hb_draw_state_t *,float x,float y,void *)57 _hb_line_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, float x, float y, void *)
58 {
59 float &i = * (float *) draw_data;
60 i += x + y;
61 }
62
63 static void
_hb_quadratic_to(hb_draw_funcs_t *,void * draw_data,hb_draw_state_t *,float cx,float cy,float x,float y,void *)64 _hb_quadratic_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, float cx, float cy, float x, float y, void *)
65 {
66 float &i = * (float *) draw_data;
67 i += cx + cy + x + y;
68 }
69
70 static void
_hb_cubic_to(hb_draw_funcs_t *,void * draw_data,hb_draw_state_t *,float cx1,float cy1,float cx2,float cy2,float x,float y,void *)71 _hb_cubic_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, float cx1, float cy1, float cx2, float cy2, float x, float y, void *)
72 {
73 float &i = * (float *) draw_data;
74 i += cx1 + cy1 + cx2 + cy2 + x + y;
75 }
76
77 static void
_hb_close_path(hb_draw_funcs_t *,void * draw_data,hb_draw_state_t *,void *)78 _hb_close_path (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, void *)
79 {
80 float &i = * (float *) draw_data;
81 i += 1.0;
82 }
83
84 static hb_draw_funcs_t *
_draw_funcs_create(void)85 _draw_funcs_create (void)
86 {
87 hb_draw_funcs_t *draw_funcs = hb_draw_funcs_create ();
88 hb_draw_funcs_set_move_to_func (draw_funcs, _hb_move_to, nullptr, nullptr);
89 hb_draw_funcs_set_line_to_func (draw_funcs, _hb_line_to, nullptr, nullptr);
90 hb_draw_funcs_set_quadratic_to_func (draw_funcs, _hb_quadratic_to, nullptr, nullptr);
91 hb_draw_funcs_set_cubic_to_func (draw_funcs, _hb_cubic_to, nullptr, nullptr);
92 hb_draw_funcs_set_close_path_func (draw_funcs, _hb_close_path, nullptr, nullptr);
93 return draw_funcs;
94 }
95
BM_Font(benchmark::State & state,bool is_var,backend_t backend,operation_t operation,const test_input_t & test_input)96 static void BM_Font (benchmark::State &state,
97 bool is_var, backend_t backend, operation_t operation,
98 const test_input_t &test_input)
99 {
100 hb_font_t *font;
101 unsigned num_glyphs;
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 num_glyphs = hb_face_get_glyph_count (face);
108 font = hb_font_create (face);
109 hb_face_destroy (face);
110 }
111
112 if (is_var)
113 {
114 hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
115 hb_font_set_variations (font, &wght, 1);
116 }
117
118 switch (backend)
119 {
120 case HARFBUZZ:
121 hb_ot_font_set_funcs (font);
122 break;
123
124 case FREETYPE:
125 #ifdef HAVE_FREETYPE
126 hb_ft_font_set_funcs (font);
127 #endif
128 break;
129 }
130
131 switch (operation)
132 {
133 case nominal_glyphs:
134 {
135 hb_set_t *set = hb_set_create ();
136 hb_face_collect_unicodes (hb_font_get_face (font), set);
137 unsigned pop = hb_set_get_population (set);
138 hb_codepoint_t *unicodes = (hb_codepoint_t *) calloc (pop, sizeof (hb_codepoint_t));
139 hb_codepoint_t *glyphs = (hb_codepoint_t *) calloc (pop, sizeof (hb_codepoint_t));
140
141 hb_codepoint_t *p = unicodes;
142 for (hb_codepoint_t u = HB_SET_VALUE_INVALID;
143 hb_set_next (set, &u);)
144 *p++ = u;
145 assert (p == unicodes + pop);
146
147 for (auto _ : state)
148 hb_font_get_nominal_glyphs (font,
149 pop,
150 unicodes, sizeof (*unicodes),
151 glyphs, sizeof (*glyphs));
152
153 free (glyphs);
154 free (unicodes);
155 hb_set_destroy (set);
156 break;
157 }
158 case glyph_h_advances:
159 {
160 hb_codepoint_t *glyphs = (hb_codepoint_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
161 hb_position_t *advances = (hb_position_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
162
163 for (unsigned g = 0; g < num_glyphs; g++)
164 glyphs[g] = g;
165
166 for (auto _ : state)
167 hb_font_get_glyph_h_advances (font,
168 num_glyphs,
169 glyphs, sizeof (*glyphs),
170 advances, sizeof (*advances));
171
172 free (advances);
173 free (glyphs);
174 break;
175 }
176 case glyph_extents:
177 {
178 hb_glyph_extents_t extents;
179 for (auto _ : state)
180 for (unsigned gid = 0; gid < num_glyphs; ++gid)
181 hb_font_get_glyph_extents (font, gid, &extents);
182 break;
183 }
184 case draw_glyph:
185 {
186 hb_draw_funcs_t *draw_funcs = _draw_funcs_create ();
187 for (auto _ : state)
188 {
189 float i = 0;
190 for (unsigned gid = 0; gid < num_glyphs; ++gid)
191 hb_font_draw_glyph (font, gid, draw_funcs, &i);
192 }
193 hb_draw_funcs_destroy (draw_funcs);
194 break;
195 }
196 case paint_glyph:
197 {
198 hb_paint_funcs_t *paint_funcs = hb_paint_funcs_create ();
199 for (auto _ : state)
200 {
201 for (unsigned gid = 0; gid < num_glyphs; ++gid)
202 hb_font_paint_glyph (font, gid, paint_funcs, nullptr, 0, 0);
203 }
204 hb_paint_funcs_destroy (paint_funcs);
205 break;
206 }
207 case load_face_and_shape:
208 {
209 for (auto _ : state)
210 {
211 hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_input.font_path);
212 assert (blob);
213 hb_face_t *face = hb_face_create (blob, 0);
214 hb_blob_destroy (blob);
215 hb_font_t *font = hb_font_create (face);
216 hb_face_destroy (face);
217
218 switch (backend)
219 {
220 case HARFBUZZ:
221 hb_ot_font_set_funcs (font);
222 break;
223
224 case FREETYPE:
225 #ifdef HAVE_FREETYPE
226 hb_ft_font_set_funcs (font);
227 #endif
228 break;
229 }
230
231 hb_buffer_t *buffer = hb_buffer_create ();
232 hb_buffer_add_utf8 (buffer, " ", -1, 0, -1);
233 hb_buffer_guess_segment_properties (buffer);
234
235 hb_shape (font, buffer, nullptr, 0);
236
237 hb_buffer_destroy (buffer);
238 hb_font_destroy (font);
239 }
240 break;
241 }
242 }
243
244
245 hb_font_destroy (font);
246 }
247
test_backend(backend_t backend,const char * backend_name,bool variable,operation_t op,const char * op_name,benchmark::TimeUnit time_unit,const test_input_t & test_input)248 static void test_backend (backend_t backend,
249 const char *backend_name,
250 bool variable,
251 operation_t op,
252 const char *op_name,
253 benchmark::TimeUnit time_unit,
254 const test_input_t &test_input)
255 {
256 char name[1024] = "BM_Font/";
257 strcat (name, op_name);
258 strcat (name, "/");
259 const char *p = strrchr (test_input.font_path, '/');
260 strcat (name, p ? p + 1 : test_input.font_path);
261 strcat (name, variable ? "/var" : "");
262 strcat (name, "/");
263 strcat (name, backend_name);
264
265 benchmark::RegisterBenchmark (name, BM_Font, variable, backend, op, test_input)
266 ->Unit(time_unit);
267 }
268
test_operation(operation_t op,const char * op_name,benchmark::TimeUnit time_unit)269 static void test_operation (operation_t op,
270 const char *op_name,
271 benchmark::TimeUnit time_unit)
272 {
273 for (unsigned i = 0; i < num_tests; i++)
274 {
275 auto& test_input = tests[i];
276 for (int variable = 0; variable < int (test_input.is_variable) + 1; variable++)
277 {
278 bool is_var = (bool) variable;
279
280 test_backend (HARFBUZZ, "hb", is_var, op, op_name, time_unit, test_input);
281 #ifdef HAVE_FREETYPE
282 test_backend (FREETYPE, "ft", is_var, op, op_name, time_unit, test_input);
283 #endif
284 }
285 }
286 }
287
main(int argc,char ** argv)288 int main(int argc, char** argv)
289 {
290 benchmark::Initialize(&argc, argv);
291
292 if (argc > 1)
293 {
294 num_tests = argc - 1;
295 tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
296 for (unsigned i = 0; i < num_tests; i++)
297 {
298 tests[i].is_variable = true;
299 tests[i].font_path = argv[i + 1];
300 }
301 }
302
303 #define TEST_OPERATION(op, time_unit) test_operation (op, #op, time_unit)
304
305 TEST_OPERATION (nominal_glyphs, benchmark::kMicrosecond);
306 TEST_OPERATION (glyph_h_advances, benchmark::kMicrosecond);
307 TEST_OPERATION (glyph_extents, benchmark::kMicrosecond);
308 TEST_OPERATION (draw_glyph, benchmark::kMicrosecond);
309 TEST_OPERATION (paint_glyph, benchmark::kMillisecond);
310 TEST_OPERATION (load_face_and_shape, benchmark::kMicrosecond);
311
312 #undef TEST_OPERATION
313
314 benchmark::RunSpecifiedBenchmarks();
315 benchmark::Shutdown();
316
317 if (tests != default_tests)
318 free (tests);
319 }
320