Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 1 | #include "benchmark/benchmark.h" |
Behdad Esfahbod | 33c990f | 2022-05-27 16:57:00 -0600 | [diff] [blame] | 2 | #include <cassert> |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 3 | #include <cstring> |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 4 | |
Qunxin Liu | 4709449 | 2022-08-31 12:01:32 -0700 | [diff] [blame] | 5 | #ifdef HAVE_CONFIG_H |
| 6 | #include "config.h" |
| 7 | #endif |
| 8 | |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 9 | #include "hb-subset.h" |
| 10 | |
Garret Rieger | 178c670 | 2022-04-20 21:19:54 +0000 | [diff] [blame] | 11 | |
Garret Rieger | bc5129d | 2022-05-04 22:16:03 +0000 | [diff] [blame] | 12 | enum operation_t |
| 13 | { |
| 14 | subset_codepoints, |
Qunxin Liu | 4709449 | 2022-08-31 12:01:32 -0700 | [diff] [blame] | 15 | subset_glyphs, |
| 16 | instance, |
Garret Rieger | bc5129d | 2022-05-04 22:16:03 +0000 | [diff] [blame] | 17 | }; |
| 18 | |
Qunxin Liu | 4709449 | 2022-08-31 12:01:32 -0700 | [diff] [blame] | 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> |
| 50 | static inline unsigned int ARRAY_LEN (const Type (&)[n]) { return n; } |
| 51 | |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 52 | #define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/" |
| 53 | |
| 54 | struct test_input_t |
| 55 | { |
| 56 | const char *font_path; |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 57 | unsigned max_subset_size; |
Qunxin Liu | 4709449 | 2022-08-31 12:01:32 -0700 | [diff] [blame] | 58 | const axis_location_t *instance_opts; |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 59 | unsigned num_instance_opts; |
| 60 | } default_tests[] = |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 61 | { |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 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}, |
Qunxin Liu | 4709449 | 2022-08-31 12:01:32 -0700 | [diff] [blame] | 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}, |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 69 | {SUBSET_FONT_BASE_PATH "AdobeVFPrototype.otf", 300, nullptr, 0}, |
Qunxin Liu | 4709449 | 2022-08-31 12:01:32 -0700 | [diff] [blame] | 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)}, |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 72 | #if 0 |
| 73 | {"perf/fonts/NotoSansCJKsc-VF.ttf", 100000}, |
| 74 | #endif |
| 75 | }; |
| 76 | |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 77 | static test_input_t *tests = default_tests; |
| 78 | static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]); |
| 79 | |
| 80 | |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 81 | void AddCodepoints(const hb_set_t* codepoints_in_font, |
| 82 | unsigned subset_size, |
| 83 | hb_subset_input_t* input) |
| 84 | { |
Behdad Esfahbod | 7edd54f | 2022-05-10 18:44:14 -0600 | [diff] [blame] | 85 | auto *unicodes = hb_subset_input_unicode_set (input); |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 86 | hb_codepoint_t cp = HB_SET_VALUE_INVALID; |
| 87 | for (unsigned i = 0; i < subset_size; i++) { |
| 88 | // TODO(garretrieger): pick randomly. |
Garret Rieger | b3ce96d | 2022-04-20 21:51:20 +0000 | [diff] [blame] | 89 | if (!hb_set_next (codepoints_in_font, &cp)) return; |
Behdad Esfahbod | 7edd54f | 2022-05-10 18:44:14 -0600 | [diff] [blame] | 90 | hb_set_add (unicodes, cp); |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Garret Rieger | 6212856 | 2022-05-04 22:16:03 +0000 | [diff] [blame] | 94 | void AddGlyphs(unsigned num_glyphs_in_font, |
| 95 | unsigned subset_size, |
| 96 | hb_subset_input_t* input) |
| 97 | { |
Behdad Esfahbod | 7edd54f | 2022-05-10 18:44:14 -0600 | [diff] [blame] | 98 | auto *glyphs = hb_subset_input_glyph_set (input); |
Garret Rieger | 6212856 | 2022-05-04 22:16:03 +0000 | [diff] [blame] | 99 | for (unsigned i = 0; i < subset_size && i < num_glyphs_in_font; i++) { |
| 100 | // TODO(garretrieger): pick randomly. |
Behdad Esfahbod | 7edd54f | 2022-05-10 18:44:14 -0600 | [diff] [blame] | 101 | hb_set_add (glyphs, i); |
Garret Rieger | 6212856 | 2022-05-04 22:16:03 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Garret Rieger | f4903de | 2022-10-13 21:38:54 +0000 | [diff] [blame] | 105 | // Preprocess face and populate the subset accelerator on it to speed up |
| 106 | // the subsetting operations. |
| 107 | static hb_face_t* preprocess_face(hb_face_t* face) |
| 108 | { |
Garret Rieger | 515863e | 2022-10-13 23:42:00 +0000 | [diff] [blame] | 109 | hb_face_t* new_face = hb_subset_preprocess(face); |
| 110 | hb_face_destroy(face); |
| 111 | return new_face; |
Garret Rieger | f4903de | 2022-10-13 21:38:54 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 114 | /* benchmark for subsetting a font */ |
Garret Rieger | bc5129d | 2022-05-04 22:16:03 +0000 | [diff] [blame] | 115 | static void BM_subset (benchmark::State &state, |
| 116 | operation_t operation, |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 117 | const test_input_t &test_input, |
| 118 | bool hinting) |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 119 | { |
Garret Rieger | b3ce96d | 2022-04-20 21:51:20 +0000 | [diff] [blame] | 120 | unsigned subset_size = state.range(0); |
| 121 | |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 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)) |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 128 | { |
Qunxin Liu | 4709449 | 2022-08-31 12:01:32 -0700 | [diff] [blame] | 129 | hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_input.font_path); |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 130 | assert (blob); |
| 131 | face = hb_face_create (blob, 0); |
| 132 | hb_blob_destroy (blob); |
Garret Rieger | f4903de | 2022-10-13 21:38:54 +0000 | [diff] [blame] | 133 | |
| 134 | face = preprocess_face (face); |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 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; |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 141 | } |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 142 | else |
| 143 | face = hb_face_reference (cached_face); |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 144 | |
| 145 | hb_subset_input_t* input = hb_subset_input_create_or_fail (); |
| 146 | assert (input); |
| 147 | |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 148 | if (!hinting) |
| 149 | hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_NO_HINTING); |
| 150 | |
Behdad Esfahbod | 7edd54f | 2022-05-10 18:44:14 -0600 | [diff] [blame] | 151 | switch (operation) |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 152 | { |
Behdad Esfahbod | 7edd54f | 2022-05-10 18:44:14 -0600 | [diff] [blame] | 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); |
Garret Rieger | bc5129d | 2022-05-04 22:16:03 +0000 | [diff] [blame] | 159 | } |
Behdad Esfahbod | 7edd54f | 2022-05-10 18:44:14 -0600 | [diff] [blame] | 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; |
Behdad Esfahbod | 238e7dd | 2022-09-01 13:24:01 -0600 | [diff] [blame] | 168 | |
Qunxin Liu | 4709449 | 2022-08-31 12:01:32 -0700 | [diff] [blame] | 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; |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 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 | |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 195 | static void test_subset (operation_t op, |
| 196 | const char *op_name, |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 197 | bool hinting, |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 198 | benchmark::TimeUnit time_unit, |
| 199 | const test_input_t &test_input) |
| 200 | { |
Qunxin Liu | 4709449 | 2022-08-31 12:01:32 -0700 | [diff] [blame] | 201 | if (op == instance && test_input.instance_opts == nullptr) |
| 202 | return; |
| 203 | |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 204 | char name[1024] = "BM_subset/"; |
| 205 | strcat (name, op_name); |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 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"); |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 211 | |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 212 | benchmark::RegisterBenchmark (name, BM_subset, op, test_input, hinting) |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 213 | ->Range(10, test_input.max_subset_size) |
| 214 | ->Unit(time_unit); |
| 215 | } |
Garret Rieger | bc5129d | 2022-05-04 22:16:03 +0000 | [diff] [blame] | 216 | |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 217 | static void test_operation (operation_t op, |
| 218 | const char *op_name, |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 219 | const test_input_t *tests, |
| 220 | unsigned num_tests, |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 221 | benchmark::TimeUnit time_unit) |
| 222 | { |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 223 | for (unsigned i = 0; i < num_tests; i++) |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 224 | { |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 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); |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 230 | |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 231 | int main(int argc, char** argv) |
| 232 | { |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 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) |
Garret Rieger | bc5129d | 2022-05-04 22:16:03 +0000 | [diff] [blame] | 247 | |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 248 | TEST_OPERATION (subset_glyphs, benchmark::kMillisecond); |
| 249 | TEST_OPERATION (subset_codepoints, benchmark::kMillisecond); |
Qunxin Liu | 4709449 | 2022-08-31 12:01:32 -0700 | [diff] [blame] | 250 | TEST_OPERATION (instance, benchmark::kMillisecond); |
Garret Rieger | fbd183d | 2022-04-20 20:05:14 +0000 | [diff] [blame] | 251 | |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 252 | #undef TEST_OPERATION |
Garret Rieger | bc5129d | 2022-05-04 22:16:03 +0000 | [diff] [blame] | 253 | |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 254 | benchmark::RunSpecifiedBenchmarks(); |
| 255 | benchmark::Shutdown(); |
Seigo Nonaka | c62d6f4 | 2023-03-01 19:52:57 +0900 | [diff] [blame] | 256 | |
| 257 | if (tests != default_tests) |
| 258 | free (tests); |
Garret Rieger | 52d59bf | 2022-05-10 19:40:37 +0000 | [diff] [blame] | 259 | } |