• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   {true , SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf"},
25   {false, SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf"},
26   {true , SUBSET_FONT_BASE_PATH "AdobeVFPrototype.otf"},
27   {true , SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf"},
28   {false, SUBSET_FONT_BASE_PATH "Comfortaa-Regular-new.ttf"},
29   {false, SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf"},
30   {false, SUBSET_FONT_BASE_PATH "NotoSerifMyanmar-Regular.otf"},
31 };
32 
33 static test_input_t *tests = default_tests;
34 static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
35 
36 enum backend_t { HARFBUZZ, FREETYPE };
37 
38 enum operation_t
39 {
40   nominal_glyphs,
41   glyph_h_advances,
42   glyph_extents,
43   glyph_shape,
44 };
45 
46 static void
_hb_move_to(hb_draw_funcs_t *,void *,hb_draw_state_t *,float,float,void *)47 _hb_move_to (hb_draw_funcs_t *, void *, hb_draw_state_t *, float, float, void *) {}
48 
49 static void
_hb_line_to(hb_draw_funcs_t *,void *,hb_draw_state_t *,float,float,void *)50 _hb_line_to (hb_draw_funcs_t *, void *, hb_draw_state_t *, float, float, void *) {}
51 
52 //static void
53 //_hb_quadratic_to (hb_draw_funcs_t *, void *, hb_draw_state_t *, float, float, float, float, void *) {}
54 
55 static void
_hb_cubic_to(hb_draw_funcs_t *,void *,hb_draw_state_t *,float,float,float,float,float,float,void *)56 _hb_cubic_to (hb_draw_funcs_t *, void *, hb_draw_state_t *, float, float, float, float, float, float, void *) {}
57 
58 static void
_hb_close_path(hb_draw_funcs_t *,void *,hb_draw_state_t *,void *)59 _hb_close_path (hb_draw_funcs_t *, void *, hb_draw_state_t *, void *) {}
60 
61 static hb_draw_funcs_t *
_draw_funcs_create(void)62 _draw_funcs_create (void)
63 {
64   hb_draw_funcs_t *draw_funcs = hb_draw_funcs_create ();
65   hb_draw_funcs_set_move_to_func (draw_funcs, _hb_move_to, nullptr, nullptr);
66   hb_draw_funcs_set_line_to_func (draw_funcs, _hb_line_to, nullptr, nullptr);
67   //hb_draw_funcs_set_quadratic_to_func (draw_funcs, _hb_quadratic_to, nullptr, nullptr);
68   hb_draw_funcs_set_cubic_to_func (draw_funcs, _hb_cubic_to, nullptr, nullptr);
69   hb_draw_funcs_set_close_path_func (draw_funcs, _hb_close_path, nullptr, nullptr);
70   return draw_funcs;
71 }
72 
BM_Font(benchmark::State & state,bool is_var,backend_t backend,operation_t operation,const test_input_t & test_input)73 static void BM_Font (benchmark::State &state,
74 		     bool is_var, backend_t backend, operation_t operation,
75 		     const test_input_t &test_input)
76 {
77   hb_font_t *font;
78   unsigned num_glyphs;
79   {
80     hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_input.font_path);
81     assert (blob);
82     hb_face_t *face = hb_face_create (blob, 0);
83     hb_blob_destroy (blob);
84     num_glyphs = hb_face_get_glyph_count (face);
85     font = hb_font_create (face);
86     hb_face_destroy (face);
87   }
88 
89   if (is_var)
90   {
91     hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
92     hb_font_set_variations (font, &wght, 1);
93   }
94 
95   switch (backend)
96   {
97     case HARFBUZZ:
98       hb_ot_font_set_funcs (font);
99       break;
100 
101     case FREETYPE:
102 #ifdef HAVE_FREETYPE
103       hb_ft_font_set_funcs (font);
104 #endif
105       break;
106   }
107 
108   switch (operation)
109   {
110     case nominal_glyphs:
111     {
112       hb_set_t *set = hb_set_create ();
113       hb_face_collect_unicodes (hb_font_get_face (font), set);
114       unsigned pop = hb_set_get_population (set);
115       hb_codepoint_t *unicodes = (hb_codepoint_t *) calloc (pop, sizeof (hb_codepoint_t));
116       hb_codepoint_t *glyphs = (hb_codepoint_t *) calloc (pop, sizeof (hb_codepoint_t));
117 
118       hb_codepoint_t *p = unicodes;
119       for (hb_codepoint_t u = HB_SET_VALUE_INVALID;
120 	   hb_set_next (set, &u);)
121         *p++ = u;
122       assert (p == unicodes + pop);
123 
124       for (auto _ : state)
125 	hb_font_get_nominal_glyphs (font,
126 				    pop,
127 				    unicodes, sizeof (*unicodes),
128 				    glyphs, sizeof (*glyphs));
129 
130       free (glyphs);
131       free (unicodes);
132       hb_set_destroy (set);
133       break;
134     }
135     case glyph_h_advances:
136     {
137       hb_codepoint_t *glyphs = (hb_codepoint_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
138       hb_position_t *advances = (hb_position_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
139 
140       for (unsigned g = 0; g < num_glyphs; g++)
141         glyphs[g] = g;
142 
143       for (auto _ : state)
144 	hb_font_get_glyph_h_advances (font,
145 				      num_glyphs,
146 				      glyphs, sizeof (*glyphs),
147 				      advances, sizeof (*advances));
148 
149       free (advances);
150       free (glyphs);
151       break;
152     }
153     case glyph_extents:
154     {
155       hb_glyph_extents_t extents;
156       for (auto _ : state)
157 	for (unsigned gid = 0; gid < num_glyphs; ++gid)
158 	  hb_font_get_glyph_extents (font, gid, &extents);
159       break;
160     }
161     case glyph_shape:
162     {
163       hb_draw_funcs_t *draw_funcs = _draw_funcs_create ();
164       for (auto _ : state)
165 	for (unsigned gid = 0; gid < num_glyphs; ++gid)
166 	  hb_font_get_glyph_shape (font, gid, draw_funcs, nullptr);
167       break;
168       hb_draw_funcs_destroy (draw_funcs);
169     }
170   }
171 
172 
173   hb_font_destroy (font);
174 }
175 
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)176 static void test_backend (backend_t backend,
177 			  const char *backend_name,
178 			  bool variable,
179 			  operation_t op,
180 			  const char *op_name,
181 			  benchmark::TimeUnit time_unit,
182 			  const test_input_t &test_input)
183 {
184   char name[1024] = "BM_Font/";
185   strcat (name, op_name);
186   strcat (name, "/");
187   const char *p = strrchr (test_input.font_path, '/');
188   strcat (name, p ? p + 1 : test_input.font_path);
189   strcat (name, variable ? "/var" : "");
190   strcat (name, "/");
191   strcat (name, backend_name);
192 
193   benchmark::RegisterBenchmark (name, BM_Font, variable, backend, op, test_input)
194    ->Unit(time_unit);
195 }
196 
test_operation(operation_t op,const char * op_name,benchmark::TimeUnit time_unit)197 static void test_operation (operation_t op,
198 			    const char *op_name,
199 			    benchmark::TimeUnit time_unit)
200 {
201   for (unsigned i = 0; i < num_tests; i++)
202   {
203     auto& test_input = tests[i];
204     for (int variable = 0; variable < int (test_input.is_variable) + 1; variable++)
205     {
206       bool is_var = (bool) variable;
207 
208       test_backend (HARFBUZZ, "hb", is_var, op, op_name, time_unit, test_input);
209 #ifdef HAVE_FREETYPE
210       test_backend (FREETYPE, "ft", is_var, op, op_name, time_unit, test_input);
211 #endif
212     }
213   }
214 }
215 
main(int argc,char ** argv)216 int main(int argc, char** argv)
217 {
218   benchmark::Initialize(&argc, argv);
219 
220   if (argc > 1)
221   {
222     num_tests = argc - 1;
223     tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
224     for (unsigned i = 0; i < num_tests; i++)
225     {
226       tests[i].is_variable = true;
227       tests[i].font_path = argv[i + 1];
228     }
229   }
230 
231 #define TEST_OPERATION(op, time_unit) test_operation (op, #op, time_unit)
232 
233   TEST_OPERATION (nominal_glyphs, benchmark::kMicrosecond);
234   TEST_OPERATION (glyph_h_advances, benchmark::kMicrosecond);
235   TEST_OPERATION (glyph_extents, benchmark::kMicrosecond);
236   TEST_OPERATION (glyph_shape, benchmark::kMicrosecond);
237 
238 #undef TEST_OPERATION
239 
240   benchmark::RunSpecifiedBenchmarks();
241   benchmark::Shutdown();
242 
243   if (tests != default_tests)
244     free (tests);
245 }
246