2020-06-09 15:07:36 +02:00
|
|
|
#include "benchmark/benchmark.h"
|
2022-05-03 00:01:22 +02:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2020-06-09 15:07:36 +02:00
|
|
|
|
|
|
|
#include "hb.h"
|
|
|
|
#include "hb-ot.h"
|
2022-05-03 00:01:22 +02:00
|
|
|
#ifdef HAVE_FREETYPE
|
2022-05-02 21:39:54 +02:00
|
|
|
#include "hb-ft.h"
|
2022-05-03 00:01:22 +02:00
|
|
|
#endif
|
2022-05-02 21:39:54 +02:00
|
|
|
|
2022-05-02 22:03:15 +02:00
|
|
|
|
|
|
|
#define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
|
|
|
|
|
|
|
|
struct test_input_t
|
|
|
|
{
|
|
|
|
const char *font_path;
|
|
|
|
} tests[] =
|
|
|
|
{
|
2022-05-03 00:50:54 +02:00
|
|
|
{SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf"},
|
2022-05-02 22:03:15 +02:00
|
|
|
{SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf"},
|
|
|
|
{SUBSET_FONT_BASE_PATH "AdobeVFPrototype.otf"},
|
|
|
|
{SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf"},
|
|
|
|
{SUBSET_FONT_BASE_PATH "Comfortaa-Regular-new.ttf"},
|
2022-05-03 00:50:54 +02:00
|
|
|
{SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf"},
|
|
|
|
{SUBSET_FONT_BASE_PATH "NotoSerifMyanmar-Regular.otf"},
|
2022-05-02 22:03:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-05-02 21:39:54 +02:00
|
|
|
enum backend_t { HARFBUZZ, FREETYPE };
|
|
|
|
|
|
|
|
enum operation_t
|
|
|
|
{
|
2022-05-03 00:59:48 +02:00
|
|
|
nominal_glyphs,
|
|
|
|
glyph_h_advances,
|
2022-05-02 22:03:15 +02:00
|
|
|
glyph_extents,
|
|
|
|
glyph_shape,
|
2022-05-02 21:39:54 +02:00
|
|
|
};
|
2020-06-09 15:07:36 +02:00
|
|
|
|
|
|
|
static void
|
2022-04-20 23:53:37 +02:00
|
|
|
_hb_move_to (hb_draw_funcs_t *, void *, hb_draw_state_t *, float, float, void *) {}
|
2020-06-09 15:07:36 +02:00
|
|
|
|
|
|
|
static void
|
2022-04-20 23:53:37 +02:00
|
|
|
_hb_line_to (hb_draw_funcs_t *, void *, hb_draw_state_t *, float, float, void *) {}
|
2020-06-09 15:07:36 +02:00
|
|
|
|
|
|
|
static void
|
2022-04-20 23:53:37 +02:00
|
|
|
_hb_quadratic_to (hb_draw_funcs_t *, void *, hb_draw_state_t *, float, float, float, float, void *) {}
|
2020-06-09 15:07:36 +02:00
|
|
|
|
|
|
|
static void
|
2022-04-20 23:53:37 +02:00
|
|
|
_hb_cubic_to (hb_draw_funcs_t *, void *, hb_draw_state_t *, float, float, float, float, float, float, void *) {}
|
2020-06-09 15:07:36 +02:00
|
|
|
|
|
|
|
static void
|
2022-04-20 23:53:37 +02:00
|
|
|
_hb_close_path (hb_draw_funcs_t *, void *, hb_draw_state_t *, void *) {}
|
2020-06-09 15:07:36 +02:00
|
|
|
|
2022-05-02 21:39:54 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-02 22:03:15 +02:00
|
|
|
static void BM_Font (benchmark::State &state,
|
|
|
|
bool is_var, backend_t backend, operation_t operation,
|
|
|
|
const test_input_t &test_input)
|
2020-06-09 15:07:36 +02:00
|
|
|
{
|
|
|
|
hb_font_t *font;
|
|
|
|
unsigned num_glyphs;
|
|
|
|
{
|
2022-05-02 22:03:15 +02:00
|
|
|
hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_input.font_path);
|
2021-06-14 23:46:04 +02:00
|
|
|
assert (blob);
|
2020-06-09 15:07:36 +02:00
|
|
|
hb_face_t *face = hb_face_create (blob, 0);
|
|
|
|
hb_blob_destroy (blob);
|
|
|
|
num_glyphs = hb_face_get_glyph_count (face);
|
|
|
|
font = hb_font_create (face);
|
|
|
|
hb_face_destroy (face);
|
|
|
|
}
|
|
|
|
|
2022-05-02 21:26:41 +02:00
|
|
|
if (is_var)
|
2020-06-09 15:07:36 +02:00
|
|
|
{
|
2022-05-02 21:26:41 +02:00
|
|
|
hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
|
|
|
|
hb_font_set_variations (font, &wght, 1);
|
2020-06-09 15:07:36 +02:00
|
|
|
}
|
2022-05-02 21:26:41 +02:00
|
|
|
|
2022-05-02 21:39:54 +02:00
|
|
|
switch (backend)
|
2020-06-09 15:07:36 +02:00
|
|
|
{
|
2022-05-02 21:39:54 +02:00
|
|
|
case HARFBUZZ:
|
|
|
|
hb_ot_font_set_funcs (font);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FREETYPE:
|
2022-05-03 00:01:22 +02:00
|
|
|
#ifdef HAVE_FREETYPE
|
2022-05-02 21:39:54 +02:00
|
|
|
hb_ft_font_set_funcs (font);
|
2022-05-03 00:01:22 +02:00
|
|
|
#endif
|
2022-05-02 21:39:54 +02:00
|
|
|
break;
|
2020-06-09 15:07:36 +02:00
|
|
|
}
|
2022-05-02 21:26:41 +02:00
|
|
|
|
2022-05-02 21:39:54 +02:00
|
|
|
switch (operation)
|
|
|
|
{
|
2022-05-03 00:59:48 +02:00
|
|
|
case nominal_glyphs:
|
2022-05-02 23:57:19 +02:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
|
2022-05-03 00:18:53 +02:00
|
|
|
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);
|
|
|
|
|
2022-05-02 23:57:19 +02:00
|
|
|
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;
|
|
|
|
}
|
2022-05-03 00:59:48 +02:00
|
|
|
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;
|
|
|
|
}
|
2022-05-02 22:03:15 +02:00
|
|
|
case glyph_extents:
|
2022-05-02 21:39:54 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2022-05-02 22:03:15 +02:00
|
|
|
case glyph_shape:
|
2022-05-02 21:39:54 +02:00
|
|
|
{
|
|
|
|
hb_draw_funcs_t *draw_funcs = _draw_funcs_create ();
|
|
|
|
for (auto _ : state)
|
|
|
|
for (unsigned gid = 0; gid < num_glyphs; ++gid)
|
|
|
|
hb_font_get_glyph_shape (font, gid, draw_funcs, nullptr);
|
|
|
|
break;
|
|
|
|
hb_draw_funcs_destroy (draw_funcs);
|
|
|
|
}
|
|
|
|
}
|
2022-05-02 21:26:41 +02:00
|
|
|
|
2020-06-09 15:07:36 +02:00
|
|
|
|
|
|
|
hb_font_destroy (font);
|
|
|
|
}
|
|
|
|
|
2022-05-02 22:03:15 +02:00
|
|
|
static void test_backend (backend_t backend,
|
|
|
|
const char *backend_name,
|
2022-05-03 00:07:32 +02:00
|
|
|
bool variable,
|
2022-05-02 22:03:15 +02:00
|
|
|
operation_t op,
|
|
|
|
const char *op_name,
|
2022-05-02 23:57:19 +02:00
|
|
|
benchmark::TimeUnit time_unit,
|
2022-05-02 22:03:15 +02:00
|
|
|
const test_input_t &test_input)
|
|
|
|
{
|
|
|
|
char name[1024] = "BM_Font/";
|
|
|
|
strcat (name, op_name);
|
2022-05-03 00:07:32 +02:00
|
|
|
strcat (name, strrchr (test_input.font_path, '/'));
|
2022-05-03 00:31:59 +02:00
|
|
|
strcat (name, variable ? "/var" : "");
|
2022-05-02 22:03:15 +02:00
|
|
|
strcat (name, "/");
|
|
|
|
strcat (name, backend_name);
|
|
|
|
|
2022-05-03 00:07:32 +02:00
|
|
|
benchmark::RegisterBenchmark (name, BM_Font, variable, backend, op, test_input)
|
2022-05-02 23:57:19 +02:00
|
|
|
->Unit(time_unit);
|
2022-05-02 22:03:15 +02:00
|
|
|
}
|
2020-06-09 15:07:36 +02:00
|
|
|
|
2022-05-02 22:03:15 +02:00
|
|
|
static void test_operation (operation_t op,
|
2022-05-02 23:57:19 +02:00
|
|
|
const char *op_name,
|
|
|
|
benchmark::TimeUnit time_unit)
|
2022-05-02 22:03:15 +02:00
|
|
|
{
|
|
|
|
for (auto& test_input : tests)
|
|
|
|
{
|
2022-05-03 00:07:32 +02:00
|
|
|
for (int variable = 0; variable < 2; variable++)
|
|
|
|
{
|
|
|
|
bool is_var = (bool) variable;
|
|
|
|
|
|
|
|
test_backend (HARFBUZZ, "hb", is_var, op, op_name, time_unit, test_input);
|
2022-05-03 00:01:22 +02:00
|
|
|
#ifdef HAVE_FREETYPE
|
2022-05-03 00:07:32 +02:00
|
|
|
test_backend (FREETYPE, "ft", is_var, op, op_name, time_unit, test_input);
|
2022-05-03 00:01:22 +02:00
|
|
|
#endif
|
2022-05-03 00:07:32 +02:00
|
|
|
}
|
2022-05-02 22:03:15 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-09 15:07:36 +02:00
|
|
|
|
2022-05-02 22:03:15 +02:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2022-05-02 23:57:19 +02:00
|
|
|
#define TEST_OPERATION(op, time_unit) test_operation (op, #op, time_unit)
|
2020-06-09 15:07:36 +02:00
|
|
|
|
2022-05-03 00:59:48 +02:00
|
|
|
TEST_OPERATION (nominal_glyphs, benchmark::kMicrosecond);
|
|
|
|
TEST_OPERATION (glyph_h_advances, benchmark::kMicrosecond);
|
2022-05-02 23:57:19 +02:00
|
|
|
TEST_OPERATION (glyph_extents, benchmark::kMicrosecond);
|
|
|
|
TEST_OPERATION (glyph_shape, benchmark::kMicrosecond);
|
2020-06-09 15:07:36 +02:00
|
|
|
|
2022-05-02 22:03:15 +02:00
|
|
|
#undef TEST_OPERATION
|
2020-06-09 15:07:36 +02:00
|
|
|
|
2022-05-02 22:03:15 +02:00
|
|
|
benchmark::Initialize(&argc, argv);
|
|
|
|
benchmark::RunSpecifiedBenchmarks();
|
|
|
|
benchmark::Shutdown();
|
|
|
|
}
|