• 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-subset.h"
10 
11 
12 enum operation_t
13 {
14   subset_codepoints,
15   subset_glyphs,
16   instance,
17 };
18 
19 struct axis_location_t
20 {
21   hb_tag_t axis_tag;
22   float axis_value;
23 };
24 
25 static const axis_location_t
26 _roboto_flex_instance_opts[] =
27 {
28   {HB_TAG ('w', 'g', 'h', 't'), 600.f},
29   {HB_TAG ('w', 'd', 't', 'h'), 75.f},
30   {HB_TAG ('o', 'p', 's', 'z'), 90.f},
31   {HB_TAG ('G', 'R', 'A', 'D'), -100.f},
32   {HB_TAG ('s', 'l', 'n', 't'), -3.f},
33   {HB_TAG ('X', 'T', 'R', 'A'), 500.f},
34   {HB_TAG ('X', 'O', 'P', 'Q'), 150.f},
35   {HB_TAG ('Y', 'O', 'P', 'Q'), 100.f},
36   {HB_TAG ('Y', 'T', 'L', 'C'), 480.f},
37   {HB_TAG ('Y', 'T', 'U', 'C'), 600.f},
38   {HB_TAG ('Y', 'T', 'A', 'S'), 800.f},
39   {HB_TAG ('Y', 'T', 'D', 'E'), -50.f},
40   {HB_TAG ('Y', 'T', 'F', 'I'), 600.f},
41 };
42 
43 static const axis_location_t
44 _mplus_instance_opts[] =
45 {
46   {HB_TAG ('w', 'g', 'h', 't'), 800.f},
47 };
48 
49 template <typename Type, unsigned int n>
ARRAY_LEN(const Type (&)[n])50 static inline unsigned int ARRAY_LEN (const Type (&)[n]) { return n; }
51 
52 #define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
53 
54 struct test_input_t
55 {
56   const char *font_path;
57   unsigned max_subset_size;
58   const axis_location_t *instance_opts;
59   unsigned num_instance_opts;
60 } default_tests[] =
61 {
62   {SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf", 1000, nullptr, 0},
63   {SUBSET_FONT_BASE_PATH "Amiri-Regular.ttf", 4096, nullptr, 0},
64   {SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf", 1400, nullptr, 0},
65   {SUBSET_FONT_BASE_PATH "NotoSansDevanagari-Regular.ttf", 1000, nullptr, 0},
66   {SUBSET_FONT_BASE_PATH "Mplus1p-Regular.ttf", 10000, nullptr, 0},
67   {SUBSET_FONT_BASE_PATH "SourceHanSans-Regular_subset.otf", 10000, nullptr, 0},
68   {SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf", 2000, nullptr, 0},
69   {SUBSET_FONT_BASE_PATH "AdobeVFPrototype.otf", 300, nullptr, 0},
70   {SUBSET_FONT_BASE_PATH "MPLUS1-Variable.ttf", 6000, _mplus_instance_opts, ARRAY_LEN (_mplus_instance_opts)},
71   {SUBSET_FONT_BASE_PATH "RobotoFlex-Variable.ttf", 900, _roboto_flex_instance_opts, ARRAY_LEN (_roboto_flex_instance_opts)},
72 #if 0
73   {"perf/fonts/NotoSansCJKsc-VF.ttf", 100000},
74 #endif
75 };
76 
77 static test_input_t *tests = default_tests;
78 static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
79 
80 
AddCodepoints(const hb_set_t * codepoints_in_font,unsigned subset_size,hb_subset_input_t * input)81 void AddCodepoints(const hb_set_t* codepoints_in_font,
82                    unsigned subset_size,
83                    hb_subset_input_t* input)
84 {
85   auto *unicodes = hb_subset_input_unicode_set (input);
86   hb_codepoint_t cp = HB_SET_VALUE_INVALID;
87   for (unsigned i = 0; i < subset_size; i++) {
88     // TODO(garretrieger): pick randomly.
89     if (!hb_set_next (codepoints_in_font, &cp)) return;
90     hb_set_add (unicodes, cp);
91   }
92 }
93 
AddGlyphs(unsigned num_glyphs_in_font,unsigned subset_size,hb_subset_input_t * input)94 void AddGlyphs(unsigned num_glyphs_in_font,
95                unsigned subset_size,
96                hb_subset_input_t* input)
97 {
98   auto *glyphs = hb_subset_input_glyph_set (input);
99   for (unsigned i = 0; i < subset_size && i < num_glyphs_in_font; i++) {
100     // TODO(garretrieger): pick randomly.
101     hb_set_add (glyphs, i);
102   }
103 }
104 
105 // Preprocess face and populate the subset accelerator on it to speed up
106 // the subsetting operations.
preprocess_face(hb_face_t * face)107 static hb_face_t* preprocess_face(hb_face_t* face)
108 {
109   hb_face_t* new_face = hb_subset_preprocess(face);
110   hb_face_destroy(face);
111   return new_face;
112 }
113 
114 /* benchmark for subsetting a font */
BM_subset(benchmark::State & state,operation_t operation,const test_input_t & test_input,bool hinting)115 static void BM_subset (benchmark::State &state,
116                        operation_t operation,
117                        const test_input_t &test_input,
118                        bool hinting)
119 {
120   unsigned subset_size = state.range(0);
121 
122   hb_face_t *face = nullptr;
123 
124   static hb_face_t *cached_face;
125   static const char *cached_font_path;
126 
127   if (!cached_font_path || strcmp (cached_font_path, test_input.font_path))
128   {
129     hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_input.font_path);
130     assert (blob);
131     face = hb_face_create (blob, 0);
132     hb_blob_destroy (blob);
133 
134     face = preprocess_face (face);
135 
136     if (cached_face)
137       hb_face_destroy (cached_face);
138 
139     cached_face = hb_face_reference (face);
140     cached_font_path = test_input.font_path;
141   }
142   else
143     face = hb_face_reference (cached_face);
144 
145   hb_subset_input_t* input = hb_subset_input_create_or_fail ();
146   assert (input);
147 
148   if (!hinting)
149     hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_NO_HINTING);
150 
151   switch (operation)
152   {
153     case subset_codepoints:
154     {
155       hb_set_t* all_codepoints = hb_set_create ();
156       hb_face_collect_unicodes (face, all_codepoints);
157       AddCodepoints(all_codepoints, subset_size, input);
158       hb_set_destroy (all_codepoints);
159     }
160     break;
161 
162     case subset_glyphs:
163     {
164       unsigned num_glyphs = hb_face_get_glyph_count (face);
165       AddGlyphs(num_glyphs, subset_size, input);
166     }
167     break;
168 
169     case instance:
170     {
171       hb_set_t* all_codepoints = hb_set_create ();
172       hb_face_collect_unicodes (face, all_codepoints);
173       AddCodepoints(all_codepoints, subset_size, input);
174       hb_set_destroy (all_codepoints);
175 
176       for (unsigned i = 0; i < test_input.num_instance_opts; i++)
177         hb_subset_input_pin_axis_location (input, face,
178                                            test_input.instance_opts[i].axis_tag,
179                                            test_input.instance_opts[i].axis_value);
180     }
181     break;
182   }
183 
184   for (auto _ : state)
185   {
186     hb_face_t* subset = hb_subset_or_fail (face, input);
187     assert (subset);
188     hb_face_destroy (subset);
189   }
190 
191   hb_subset_input_destroy (input);
192   hb_face_destroy (face);
193 }
194 
test_subset(operation_t op,const char * op_name,bool hinting,benchmark::TimeUnit time_unit,const test_input_t & test_input)195 static void test_subset (operation_t op,
196                          const char *op_name,
197                          bool hinting,
198                          benchmark::TimeUnit time_unit,
199                          const test_input_t &test_input)
200 {
201   if (op == instance && test_input.instance_opts == nullptr)
202     return;
203 
204   char name[1024] = "BM_subset/";
205   strcat (name, op_name);
206   strcat (name, "/");
207   const char *p = strrchr (test_input.font_path, '/');
208   strcat (name, p ? p + 1 : test_input.font_path);
209   if (!hinting)
210     strcat (name, "/nohinting");
211 
212   benchmark::RegisterBenchmark (name, BM_subset, op, test_input, hinting)
213       ->Range(10, test_input.max_subset_size)
214       ->Unit(time_unit);
215 }
216 
test_operation(operation_t op,const char * op_name,const test_input_t * tests,unsigned num_tests,benchmark::TimeUnit time_unit)217 static void test_operation (operation_t op,
218                             const char *op_name,
219                             const test_input_t *tests,
220                             unsigned num_tests,
221                             benchmark::TimeUnit time_unit)
222 {
223   for (unsigned i = 0; i < num_tests; i++)
224   {
225     auto& test_input = tests[i];
226     test_subset (op, op_name, true, time_unit, test_input);
227     test_subset (op, op_name, false, time_unit, test_input);
228   }
229 }
230 
main(int argc,char ** argv)231 int main(int argc, char** argv)
232 {
233   benchmark::Initialize(&argc, argv);
234 
235   if (argc > 1)
236   {
237     num_tests = (argc - 1) / 2;
238     tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
239     for (unsigned i = 0; i < num_tests; i++)
240     {
241       tests[i].font_path = argv[1 + i * 2];
242       tests[i].max_subset_size = atoi (argv[2 + i * 2]);
243     }
244   }
245 
246 #define TEST_OPERATION(op, time_unit) test_operation (op, #op, tests, num_tests, time_unit)
247 
248   TEST_OPERATION (subset_glyphs, benchmark::kMillisecond);
249   TEST_OPERATION (subset_codepoints, benchmark::kMillisecond);
250   TEST_OPERATION (instance, benchmark::kMillisecond);
251 
252 #undef TEST_OPERATION
253 
254   benchmark::RunSpecifiedBenchmarks();
255   benchmark::Shutdown();
256 
257   if (tests != default_tests)
258     free (tests);
259 }
260