2020-06-09 15:07:36 +02:00
|
|
|
#include "benchmark/benchmark.h"
|
2022-04-29 23:55:19 +02:00
|
|
|
#include <cstring>
|
2020-06-09 15:07:36 +02:00
|
|
|
|
2022-05-23 22:36:06 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2020-06-09 15:07:36 +02:00
|
|
|
|
2022-05-23 22:36:06 +02:00
|
|
|
#include "hb.h"
|
|
|
|
#include "hb-ot.h"
|
|
|
|
#ifdef HAVE_FREETYPE
|
|
|
|
#include "hb-ft.h"
|
|
|
|
#endif
|
2022-05-02 22:03:15 +02:00
|
|
|
|
2022-05-21 01:21:04 +02:00
|
|
|
#define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
|
|
|
|
|
2022-04-29 23:55:19 +02:00
|
|
|
struct test_input_t
|
|
|
|
{
|
|
|
|
const char *text_path;
|
|
|
|
const char *font_path;
|
2022-05-21 01:21:04 +02:00
|
|
|
bool is_variable;
|
2022-05-21 22:42:50 +02:00
|
|
|
} default_tests[] =
|
2022-04-29 23:55:19 +02:00
|
|
|
{
|
|
|
|
{"perf/texts/fa-thelittleprince.txt",
|
2022-05-21 01:21:04 +02:00
|
|
|
"perf/fonts/Amiri-Regular.ttf",
|
|
|
|
false},
|
2022-04-29 23:55:19 +02:00
|
|
|
|
|
|
|
{"perf/texts/fa-thelittleprince.txt",
|
2022-05-21 01:21:04 +02:00
|
|
|
"perf/fonts/NotoNastaliqUrdu-Regular.ttf",
|
|
|
|
false},
|
2022-04-29 23:55:19 +02:00
|
|
|
|
|
|
|
{"perf/texts/fa-monologue.txt",
|
2022-05-21 01:21:04 +02:00
|
|
|
"perf/fonts/Amiri-Regular.ttf",
|
|
|
|
false},
|
2022-04-29 23:55:19 +02:00
|
|
|
|
|
|
|
{"perf/texts/fa-monologue.txt",
|
2022-05-21 01:21:04 +02:00
|
|
|
"perf/fonts/NotoNastaliqUrdu-Regular.ttf",
|
|
|
|
false},
|
|
|
|
|
|
|
|
{"perf/texts/en-thelittleprince.txt",
|
|
|
|
"perf/fonts/Roboto-Regular.ttf",
|
|
|
|
false},
|
2022-04-29 23:55:19 +02:00
|
|
|
|
|
|
|
{"perf/texts/en-thelittleprince.txt",
|
2022-05-21 01:21:04 +02:00
|
|
|
SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf",
|
|
|
|
true},
|
|
|
|
|
|
|
|
{"perf/texts/en-words.txt",
|
|
|
|
"perf/fonts/Roboto-Regular.ttf",
|
|
|
|
false},
|
2022-04-29 23:55:19 +02:00
|
|
|
|
|
|
|
{"perf/texts/en-words.txt",
|
2022-05-21 01:21:04 +02:00
|
|
|
SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf",
|
|
|
|
true},
|
2022-04-29 23:55:19 +02:00
|
|
|
};
|
|
|
|
|
2022-05-21 22:42:50 +02:00
|
|
|
test_input_t *tests = default_tests;
|
|
|
|
unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
|
2022-05-02 22:03:15 +02:00
|
|
|
|
2022-05-23 22:36:06 +02:00
|
|
|
enum backend_t { HARFBUZZ, FREETYPE };
|
|
|
|
|
2022-05-21 01:21:04 +02:00
|
|
|
static void BM_Shape (benchmark::State &state,
|
|
|
|
bool is_var,
|
2022-05-23 22:36:06 +02:00
|
|
|
backend_t backend,
|
2022-05-21 01:21:04 +02:00
|
|
|
const test_input_t &input)
|
2020-06-09 15:07:36 +02:00
|
|
|
{
|
|
|
|
hb_font_t *font;
|
|
|
|
{
|
2022-04-29 23:55:19 +02:00
|
|
|
hb_blob_t *blob = hb_blob_create_from_file_or_fail (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);
|
|
|
|
font = hb_font_create (face);
|
|
|
|
hb_face_destroy (face);
|
|
|
|
}
|
|
|
|
|
2022-05-21 01:21:04 +02:00
|
|
|
if (is_var)
|
|
|
|
{
|
|
|
|
hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
|
|
|
|
hb_font_set_variations (font, &wght, 1);
|
|
|
|
}
|
|
|
|
|
2022-05-23 22:36:06 +02:00
|
|
|
switch (backend)
|
|
|
|
{
|
|
|
|
case HARFBUZZ:
|
|
|
|
hb_ot_font_set_funcs (font);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FREETYPE:
|
|
|
|
#ifdef HAVE_FREETYPE
|
|
|
|
hb_ft_font_set_funcs (font);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-04-29 23:55:19 +02:00
|
|
|
hb_blob_t *text_blob = hb_blob_create_from_file_or_fail (input.text_path);
|
2021-06-14 23:46:04 +02:00
|
|
|
assert (text_blob);
|
2022-04-30 00:02:55 +02:00
|
|
|
unsigned orig_text_length;
|
|
|
|
const char *orig_text = hb_blob_get_data (text_blob, &orig_text_length);
|
2020-06-09 15:07:36 +02:00
|
|
|
|
|
|
|
hb_buffer_t *buf = hb_buffer_create ();
|
|
|
|
for (auto _ : state)
|
|
|
|
{
|
2022-04-30 00:02:55 +02:00
|
|
|
unsigned text_length = orig_text_length;
|
|
|
|
const char *text = orig_text;
|
|
|
|
|
|
|
|
const char *end;
|
|
|
|
while ((end = (const char *) memchr (text, '\n', text_length)))
|
|
|
|
{
|
|
|
|
hb_buffer_clear_contents (buf);
|
|
|
|
hb_buffer_add_utf8 (buf, text, text_length, 0, end - text);
|
|
|
|
hb_buffer_guess_segment_properties (buf);
|
|
|
|
hb_shape (font, buf, nullptr, 0);
|
|
|
|
|
|
|
|
unsigned skip = end - text + 1;
|
|
|
|
text_length -= skip;
|
|
|
|
text += skip;
|
|
|
|
}
|
2020-06-09 15:07:36 +02:00
|
|
|
}
|
|
|
|
hb_buffer_destroy (buf);
|
|
|
|
|
|
|
|
hb_blob_destroy (text_blob);
|
|
|
|
hb_font_destroy (font);
|
|
|
|
}
|
|
|
|
|
2022-05-23 22:36:06 +02:00
|
|
|
static void test_backend (backend_t backend,
|
|
|
|
const char *backend_name,
|
|
|
|
bool variable,
|
|
|
|
const test_input_t &test_input)
|
|
|
|
{
|
|
|
|
char name[1024] = "BM_Shape";
|
|
|
|
const char *p;
|
|
|
|
strcat (name, "/");
|
|
|
|
p = strrchr (test_input.text_path, '/');
|
|
|
|
strcat (name, p ? p + 1 : test_input.text_path);
|
|
|
|
strcat (name, "/");
|
|
|
|
p = strrchr (test_input.font_path, '/');
|
|
|
|
strcat (name, p ? p + 1 : test_input.font_path);
|
|
|
|
strcat (name, variable ? "/var" : "");
|
|
|
|
strcat (name, "/");
|
|
|
|
strcat (name, backend_name);
|
|
|
|
|
|
|
|
benchmark::RegisterBenchmark (name, BM_Shape, variable, backend, test_input)
|
|
|
|
->Unit(benchmark::kMillisecond);
|
|
|
|
}
|
|
|
|
|
2022-04-29 23:55:19 +02:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2022-05-21 22:42:50 +02:00
|
|
|
benchmark::Initialize(&argc, argv);
|
|
|
|
|
|
|
|
if (argc > 2)
|
|
|
|
{
|
2022-05-21 23:11:53 +02:00
|
|
|
num_tests = (argc - 1) / 2;
|
2022-05-21 22:42:50 +02:00
|
|
|
tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
|
2022-05-21 23:11:53 +02:00
|
|
|
for (unsigned i = 0; i < num_tests; i++)
|
|
|
|
{
|
|
|
|
tests[i].is_variable = true;
|
|
|
|
tests[i].text_path = argv[1 + i * 2];
|
|
|
|
tests[i].font_path = argv[2 + i * 2];
|
|
|
|
}
|
2022-05-21 22:42:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < num_tests; i++)
|
2022-04-29 23:55:19 +02:00
|
|
|
{
|
2022-05-21 22:42:50 +02:00
|
|
|
auto& test_input = tests[i];
|
2022-05-21 01:21:04 +02:00
|
|
|
for (int variable = 0; variable < int (test_input.is_variable) + 1; variable++)
|
|
|
|
{
|
2022-05-23 22:36:06 +02:00
|
|
|
bool is_var = (bool) variable;
|
|
|
|
|
|
|
|
test_backend (HARFBUZZ, "hb", is_var, test_input);
|
|
|
|
#ifdef HAVE_FREETYPE
|
|
|
|
test_backend (FREETYPE, "ft", is_var, test_input);
|
|
|
|
#endif
|
2022-05-21 01:21:04 +02:00
|
|
|
}
|
2022-04-29 23:55:19 +02:00
|
|
|
}
|
2022-04-29 23:37:11 +02:00
|
|
|
|
2022-04-29 23:55:19 +02:00
|
|
|
benchmark::RunSpecifiedBenchmarks();
|
|
|
|
benchmark::Shutdown();
|
2022-05-21 22:42:50 +02:00
|
|
|
|
|
|
|
if (tests != default_tests)
|
|
|
|
free (tests);
|
2022-04-29 23:55:19 +02:00
|
|
|
}
|