Branch
Hash :
7dcd6954
Author :
Date :
2025-04-05T17:08:31
[test/shape/threads] Reduce number of tested combinations
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
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <thread>
#include <condition_variable>
#include <vector>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "hb.h"
#define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
struct test_input_t
{
const char *font_path;
const char *text_path;
bool is_variable;
} default_tests[] =
{
{"perf/fonts/NotoNastaliqUrdu-Regular.ttf",
"perf/texts/fa-thelittleprince.txt",
false},
{"perf/fonts/Roboto-Regular.ttf",
"perf/texts/en-words.txt",
false},
{SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf",
"perf/texts/en-thelittleprince.txt",
true},
};
static test_input_t *tests = default_tests;
static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
// https://en.cppreference.com/w/cpp/thread/condition_variable/wait
static std::condition_variable cv;
static std::mutex cv_m;
static bool ready = false;
static unsigned num_repetitions = 1;
static unsigned num_threads = 3;
static void shape (const test_input_t &input,
hb_font_t *font)
{
// Wait till all threads are ready.
{
std::unique_lock<std::mutex> lk (cv_m);
cv.wait(lk, [] {return ready;});
}
const char *lang_str = strrchr (input.text_path, '/');
lang_str = lang_str ? lang_str + 1 : input.text_path;
hb_language_t language = hb_language_from_string (lang_str, -1);
hb_blob_t *text_blob = hb_blob_create_from_file_or_fail (input.text_path);
assert (text_blob);
unsigned orig_text_length;
const char *orig_text = hb_blob_get_data (text_blob, &orig_text_length);
hb_buffer_t *buf = hb_buffer_create ();
hb_buffer_set_flags (buf, HB_BUFFER_FLAG_VERIFY);
for (unsigned i = 0; i < num_repetitions; i++)
{
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_buffer_set_language (buf, language);
hb_shape (font, buf, nullptr, 0);
unsigned skip = end - text + 1;
text_length -= skip;
text += skip;
}
}
hb_buffer_destroy (buf);
hb_blob_destroy (text_blob);
}
static void test_backend (const char *backend,
bool variable,
const test_input_t &test_input)
{
char name[1024] = "shape";
const char *p;
strcat (name, "/");
p = strrchr (test_input.font_path, '/');
strcat (name, p ? p + 1 : test_input.font_path);
strcat (name, "/");
p = strrchr (test_input.text_path, '/');
strcat (name, p ? p + 1 : test_input.text_path);
strcat (name, variable ? "/var" : "");
strcat (name, "/");
strcat (name, backend);
printf ("Testing %s\n", name);
hb_font_t *font;
{
hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_input.font_path);
assert (blob);
hb_face_t *face = hb_face_create (blob, 0);
hb_blob_destroy (blob);
font = hb_font_create (face);
hb_face_destroy (face);
}
if (variable)
{
hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
hb_font_set_variations (font, &wght, 1);
}
bool ret = hb_font_set_funcs_using (font, backend);
assert (ret);
std::vector<std::thread> threads;
for (unsigned i = 0; i < num_threads; i++)
threads.push_back (std::thread (shape, test_input, font));
{
std::unique_lock<std::mutex> lk (cv_m);
ready = true;
}
cv.notify_all();
for (unsigned i = 0; i < num_threads; i++)
threads[i].join ();
hb_font_destroy (font);
}
int main(int argc, char** argv)
{
if (argc > 1)
num_threads = atoi (argv[1]);
if (argc > 2)
num_repetitions = atoi (argv[2]);
/* Dummy call to alleviate _guess_segment_properties thread safety-ness
* https://github.com/harfbuzz/harfbuzz/issues/1191 */
hb_language_get_default ();
if (argc > 4)
{
num_tests = (argc - 3) / 2;
tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
for (unsigned i = 0; i < num_tests; i++)
{
tests[i].is_variable = true;
tests[i].font_path = argv[3 + i * 2];
tests[i].text_path = argv[4 + i * 2];
}
}
printf ("Num threads %u; num repetitions %u\n", num_threads, num_repetitions);
for (unsigned i = 0; i < num_tests; i++)
{
auto& test_input = tests[i];
for (int variable = 0; variable < int (test_input.is_variable) + 1; variable++)
{
bool is_var = (bool) variable;
for (const char **font_funcs = hb_font_list_funcs (); *font_funcs; font_funcs++)
test_backend (*font_funcs, is_var, test_input);
}
}
if (tests != default_tests)
free (tests);
}