Hash :
5087e53b
Author :
Date :
2025-07-26T04:02:41
[perf] Use GOption (#5422) * [perf] Don't build benchmarks if no glib Also remove unnecessary deps. * [benchmark-shape/font] Port to GOption
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
#include "hb-benchmark.hh"
#include <glib.h>
#define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
static hb_variation_t default_variations[] = {
hb_variation_t {HB_TAG ('w','g','h','t'), 500},
hb_variation_t {HB_TAG_NONE, 0}
};
static unsigned max_num_glyphs = 0;
static const char *text = " ";
static struct test_input_t
{
hb_variation_t *variations;
const char *font_path;
} default_tests[] =
{
{nullptr, SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf"},
{default_variations, SUBSET_FONT_BASE_PATH "RobotoFlex-Variable.ttf"},
{default_variations, SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf"},
{default_variations, SUBSET_FONT_BASE_PATH "AdobeVFPrototype.otf"},
{default_variations, SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf"},
{nullptr, SUBSET_FONT_BASE_PATH "Comfortaa-Regular-new.ttf"},
{nullptr, SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf"},
{nullptr, SUBSET_FONT_BASE_PATH "NotoSerifMyanmar-Regular.otf"},
};
static test_input_t *tests = default_tests;
static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
enum operation_t
{
nominal_glyphs,
glyph_h_advances,
glyph_v_advances,
glyph_v_origins,
glyph_extents,
draw_glyph,
paint_glyph,
load_face_and_shape,
};
static void
_hb_move_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, float x, float y, void *)
{
float &i = * (float *) draw_data;
i += x + y;
}
static void
_hb_line_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, float x, float y, void *)
{
float &i = * (float *) draw_data;
i += x + y;
}
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 *)
{
float &i = * (float *) draw_data;
i += cx + cy + x + y;
}
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 *)
{
float &i = * (float *) draw_data;
i += cx1 + cy1 + cx2 + cy2 + x + y;
}
static void
_hb_close_path (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, void *)
{
float &i = * (float *) draw_data;
i += 1.0;
}
static hb_draw_funcs_t *
_draw_funcs_create (void)
{
hb_draw_funcs_t *draw_funcs = hb_draw_funcs_create ();
hb_draw_funcs_set_move_to_func (draw_funcs, _hb_move_to, nullptr, nullptr);
hb_draw_funcs_set_line_to_func (draw_funcs, _hb_line_to, nullptr, nullptr);
hb_draw_funcs_set_quadratic_to_func (draw_funcs, _hb_quadratic_to, nullptr, nullptr);
hb_draw_funcs_set_cubic_to_func (draw_funcs, _hb_cubic_to, nullptr, nullptr);
hb_draw_funcs_set_close_path_func (draw_funcs, _hb_close_path, nullptr, nullptr);
return draw_funcs;
}
static void BM_Font (benchmark::State &state,
const hb_variation_t *variations, const char * backend,
operation_t operation,
const test_input_t &test_input)
{
hb_font_t *font;
unsigned num_glyphs;
{
hb_face_t *face = hb_benchmark_face_create_from_file_or_fail (test_input.font_path, 0);
assert (face);
num_glyphs = hb_face_get_glyph_count (face);
if (max_num_glyphs && num_glyphs > max_num_glyphs)
num_glyphs = max_num_glyphs;
font = hb_font_create (face);
hb_face_destroy (face);
}
if (variations)
{
unsigned count = 0;
for (const hb_variation_t *v = variations; v->tag != HB_TAG_NONE; v++)
count++;
hb_font_set_variations (font, variations, count);
}
bool ret = hb_font_set_funcs_using (font, backend);
if (!ret)
{
state.SkipWithError("Backend failed to initialize for font.");
hb_font_destroy (font);
return;
}
switch (operation)
{
case nominal_glyphs:
{
hb_set_t *set = hb_set_create ();
hb_face_collect_unicodes (hb_font_get_face (font), set);
unsigned pop = hb_set_get_population (set);
hb_codepoint_t *unicodes = (hb_codepoint_t *) calloc (pop, sizeof (hb_codepoint_t));
hb_codepoint_t *glyphs = (hb_codepoint_t *) calloc (pop, sizeof (hb_codepoint_t));
hb_codepoint_t *p = unicodes;
for (hb_codepoint_t u = HB_SET_VALUE_INVALID;
hb_set_next (set, &u);)
*p++ = u;
assert (p == unicodes + pop);
for (auto _ : state)
hb_font_get_nominal_glyphs (font,
pop,
unicodes, sizeof (*unicodes),
glyphs, sizeof (*glyphs));
free (glyphs);
free (unicodes);
hb_set_destroy (set);
break;
}
case glyph_h_advances:
{
hb_codepoint_t *glyphs = (hb_codepoint_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
hb_position_t *advances = (hb_position_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
for (unsigned g = 0; g < num_glyphs; g++)
glyphs[g] = g;
for (auto _ : state)
hb_font_get_glyph_h_advances (font,
num_glyphs,
glyphs, sizeof (*glyphs),
advances, sizeof (*advances));
free (advances);
free (glyphs);
break;
}
case glyph_v_advances:
{
hb_codepoint_t *glyphs = (hb_codepoint_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
hb_position_t *advances = (hb_position_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
for (unsigned g = 0; g < num_glyphs; g++)
glyphs[g] = g;
for (auto _ : state)
hb_font_get_glyph_v_advances (font,
num_glyphs,
glyphs, sizeof (*glyphs),
advances, sizeof (*advances));
free (advances);
free (glyphs);
break;
}
case glyph_v_origins:
{
hb_codepoint_t *glyphs = (hb_codepoint_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
hb_position_t *origins_x = (hb_position_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
hb_position_t *origins_y = (hb_position_t *) calloc (num_glyphs, sizeof (hb_codepoint_t));
for (unsigned g = 0; g < num_glyphs; g++)
glyphs[g] = g;
for (auto _ : state)
hb_font_get_glyph_v_origins (font,
num_glyphs,
glyphs, sizeof (*glyphs),
origins_x, sizeof (*origins_x),
origins_y, sizeof (*origins_y));
free (origins_y);
free (origins_x);
free (glyphs);
break;
}
case glyph_extents:
{
hb_glyph_extents_t extents;
for (auto _ : state)
for (unsigned gid = 0; gid < num_glyphs; ++gid)
hb_font_get_glyph_extents (font, gid, &extents);
break;
}
case draw_glyph:
{
hb_draw_funcs_t *draw_funcs = _draw_funcs_create ();
for (auto _ : state)
{
float i = 0;
for (unsigned gid = 0; gid < num_glyphs; ++gid)
hb_font_draw_glyph (font, gid, draw_funcs, &i);
}
hb_draw_funcs_destroy (draw_funcs);
break;
}
case paint_glyph:
{
hb_paint_funcs_t *paint_funcs = hb_paint_funcs_create ();
for (auto _ : state)
{
for (unsigned gid = 0; gid < num_glyphs; ++gid)
hb_font_paint_glyph (font, gid, paint_funcs, nullptr, 0, 0);
}
hb_paint_funcs_destroy (paint_funcs);
break;
}
case load_face_and_shape:
{
for (auto _ : state)
{
hb_face_t *face = hb_benchmark_face_create_from_file_or_fail (test_input.font_path, 0);
assert (face);
hb_font_t *font = hb_font_create (face);
hb_face_destroy (face);
bool ret = hb_font_set_funcs_using (font, backend);
if (!ret)
{
state.SkipWithError("Backend failed to initialize for font.");
hb_font_destroy (font);
return;
}
hb_buffer_t *buffer = hb_buffer_create ();
hb_buffer_add_utf8 (buffer, text, -1, 0, -1);
hb_buffer_guess_segment_properties (buffer);
hb_shape (font, buffer, nullptr, 0);
hb_buffer_destroy (buffer);
hb_font_destroy (font);
}
break;
}
}
hb_font_destroy (font);
}
static void test_backend (const char *backend,
const hb_variation_t *variations,
operation_t op,
const char *op_name,
benchmark::TimeUnit time_unit,
const test_input_t &test_input)
{
char name[1024] = "BM_Font/";
strcat (name, op_name);
strcat (name, "/");
const char *p = strrchr (test_input.font_path, '/');
strcat (name, p ? p + 1 : test_input.font_path);
strcat (name, variations ? "/var" : "");
strcat (name, "/");
strcat (name, backend);
benchmark::RegisterBenchmark (name, BM_Font, variations, backend, op, test_input)
->Unit(time_unit);
}
static void test_operation (operation_t op,
const char *op_name,
benchmark::TimeUnit time_unit)
{
const char **supported_backends = hb_font_list_funcs ();
for (unsigned i = 0; i < num_tests; i++)
{
auto& test_input = tests[i];
for (int variable = 0; variable < int (test_input.variations != nullptr) + 1; variable++)
{
const hb_variation_t *variations = variable ? test_input.variations : nullptr;
for (const char **backend = supported_backends; *backend; backend++)
test_backend (*backend, variations, op, op_name, time_unit, test_input);
}
}
}
static const char *font_file = nullptr;
static const char *variations_str = nullptr;
static GOptionEntry entries[] =
{
{"font-file", 0, 0, G_OPTION_ARG_STRING, &font_file, "Font file-path to benchmark", "FONTFILE"},
{"variations", 0, 0, G_OPTION_ARG_STRING, &variations_str, "Variations to apply", "VAR"},
{"max-glyphs", 0, 0, G_OPTION_ARG_INT, &max_num_glyphs, "Maximum number of glyphs to process", "NUM"},
{"text", 0, 0, G_OPTION_ARG_STRING, &text, "Text to use for load_face_and_shape test", "TEXT"},
{nullptr}
};
static void print_usage (const char *prgname)
{
g_print ("Usage: %s [OPTIONS] [FONTFILE]\n", prgname);
}
int main(int argc, char** argv)
{
const char *prgname = g_path_get_basename (argv[0]);
GOptionContext *context = g_option_context_new ("");
g_option_context_set_summary (context, "Benchmark font operations");
g_option_context_set_description (context, "Benchmark Options:");
g_option_context_add_main_entries (context, entries, nullptr);
bool show_help = false;
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
{
argv[i] = (char *) "--help"; // Ensure it is recognized by both google-benchmark
show_help = true;
break;
}
}
if (show_help)
{
print_usage (prgname);
gchar *help_text = g_option_context_get_help (context, false, nullptr);
help_text = strstr (help_text, "\n\n");
g_print ("%s", help_text);
benchmark::Initialize(&argc, argv); // This shows the help for google-benchmark
}
benchmark::Initialize(&argc, argv);
g_option_context_parse (context, &argc, &argv, nullptr);
g_option_context_free (context);
argc--;
argv++;
if (!font_file && argc)
{
font_file = *argv;
argc--;
argv++;
}
if (argc)
{
g_printerr ("Unexpected arguments: ");
for (int i = 0; i < argc; i++)
g_printerr ("%s ", argv[i]);
g_printerr ("\n\n");
print_usage (prgname);
return 1;
}
if (font_file)
{
unsigned num_variations = 0;
if (variations_str)
{
const char *p = variations_str;
while (p && *p)
{
num_variations++;
p = strchr (p, ',');
if (p) p++;
}
}
hb_variation_t *variations = num_variations ? (hb_variation_t *) calloc (num_variations, sizeof (hb_variation_t)) : nullptr;
if (variations_str)
{
const char *p = variations_str;
for (unsigned i = 0; i < num_variations; i++)
{
const char *end = strchr (p, ',');
if (end)
{
hb_variation_from_string (p, end - p, &variations[i]);
p = end + 1;
}
else
{
hb_variation_from_string (p, -1, &variations[i]);
p = nullptr;
}
}
}
num_tests = 1;
tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
tests[0].variations = num_variations ? variations : default_variations;
tests[0].font_path = font_file;
}
#define TEST_OPERATION(op, time_unit) test_operation (op, #op, time_unit)
TEST_OPERATION (nominal_glyphs, benchmark::kMicrosecond);
TEST_OPERATION (glyph_h_advances, benchmark::kMicrosecond);
TEST_OPERATION (glyph_v_advances, benchmark::kMicrosecond);
TEST_OPERATION (glyph_v_origins, benchmark::kMicrosecond);
TEST_OPERATION (glyph_extents, benchmark::kMicrosecond);
TEST_OPERATION (draw_glyph, benchmark::kMillisecond);
TEST_OPERATION (paint_glyph, benchmark::kMillisecond);
TEST_OPERATION (load_face_and_shape, benchmark::kMicrosecond);
#undef TEST_OPERATION
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
if (tests != default_tests)
{
if (tests[0].variations != default_variations)
free (tests[0].variations);
free (tests);
}
}