1 #include "hb-benchmark.hh"
2
3 #define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
4
5 struct test_input_t
6 {
7 const char *font_path;
8 const char *text_path;
9 bool is_variable;
10 } default_tests[] =
11 {
12
13 {"perf/fonts/NotoNastaliqUrdu-Regular.ttf",
14 "perf/texts/fa-thelittleprince.txt",
15 false},
16
17 {"perf/fonts/NotoNastaliqUrdu-Regular.ttf",
18 "perf/texts/fa-words.txt",
19 false},
20
21 {"perf/fonts/Amiri-Regular.ttf",
22 "perf/texts/fa-thelittleprince.txt",
23 false},
24
25 {SUBSET_FONT_BASE_PATH "NotoSansDevanagari-Regular.ttf",
26 "perf/texts/hi-words.txt",
27 false},
28
29 {"perf/fonts/Roboto-Regular.ttf",
30 "perf/texts/en-thelittleprince.txt",
31 false},
32
33 {"perf/fonts/Roboto-Regular.ttf",
34 "perf/texts/en-words.txt",
35 false},
36
37 {SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf",
38 "perf/texts/en-thelittleprince.txt",
39 true},
40 };
41
42 static test_input_t *tests = default_tests;
43 static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
44
45 enum backend_t { HARFBUZZ, FREETYPE };
46
BM_Shape(benchmark::State & state,bool is_var,backend_t backend,const test_input_t & input)47 static void BM_Shape (benchmark::State &state,
48 bool is_var,
49 backend_t backend,
50 const test_input_t &input)
51 {
52 hb_font_t *font;
53 {
54 hb_face_t *face = hb_benchmark_face_create_from_file_or_fail (input.font_path, 0);
55 assert (face);
56 font = hb_font_create (face);
57 hb_face_destroy (face);
58 }
59
60 if (is_var)
61 {
62 hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
63 hb_font_set_variations (font, &wght, 1);
64 }
65
66 switch (backend)
67 {
68 case HARFBUZZ:
69 hb_ot_font_set_funcs (font);
70 break;
71
72 case FREETYPE:
73 #ifdef HAVE_FREETYPE
74 hb_ft_font_set_funcs (font);
75 #endif
76 break;
77 }
78
79 hb_blob_t *text_blob = hb_blob_create_from_file_or_fail (input.text_path);
80 assert (text_blob);
81 unsigned orig_text_length;
82 const char *orig_text = hb_blob_get_data (text_blob, &orig_text_length);
83
84 hb_buffer_t *buf = hb_buffer_create ();
85 for (auto _ : state)
86 {
87 unsigned text_length = orig_text_length;
88 const char *text = orig_text;
89
90 const char *end;
91 while ((end = (const char *) memchr (text, '\n', text_length)))
92 {
93 hb_buffer_clear_contents (buf);
94 hb_buffer_add_utf8 (buf, text, text_length, 0, end - text);
95 hb_buffer_guess_segment_properties (buf);
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 hb_font_destroy (font);
107 }
108
test_backend(backend_t backend,const char * backend_name,bool variable,const test_input_t & test_input)109 static void test_backend (backend_t backend,
110 const char *backend_name,
111 bool variable,
112 const test_input_t &test_input)
113 {
114 char name[1024] = "BM_Shape";
115 const char *p;
116 strcat (name, "/");
117 p = strrchr (test_input.font_path, '/');
118 strcat (name, p ? p + 1 : test_input.font_path);
119 strcat (name, "/");
120 p = strrchr (test_input.text_path, '/');
121 strcat (name, p ? p + 1 : test_input.text_path);
122 strcat (name, variable ? "/var" : "");
123 strcat (name, "/");
124 strcat (name, backend_name);
125
126 benchmark::RegisterBenchmark (name, BM_Shape, variable, backend, test_input)
127 ->Unit(benchmark::kMillisecond);
128 }
129
main(int argc,char ** argv)130 int main(int argc, char** argv)
131 {
132 benchmark::Initialize(&argc, argv);
133
134 if (argc > 2)
135 {
136 num_tests = (argc - 1) / 2;
137 tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
138 for (unsigned i = 0; i < num_tests; i++)
139 {
140 tests[i].is_variable = true;
141 tests[i].font_path = argv[1 + i * 2];
142 tests[i].text_path = argv[2 + i * 2];
143 }
144 }
145
146 for (unsigned i = 0; i < num_tests; i++)
147 {
148 auto& test_input = tests[i];
149 for (int variable = 0; variable < int (test_input.is_variable) + 1; variable++)
150 {
151 bool is_var = (bool) variable;
152
153 test_backend (HARFBUZZ, "hb", is_var, test_input);
154 #ifdef HAVE_FREETYPE
155 test_backend (FREETYPE, "ft", is_var, test_input);
156 #endif
157 }
158 }
159
160 benchmark::RunSpecifiedBenchmarks();
161 benchmark::Shutdown();
162
163 if (tests != default_tests)
164 free (tests);
165 }
166