From 971e607319fe4cef48e64f2bd734ca353f062c32 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Mar 2026 19:54:21 -0600 Subject: [PATCH] [gpu] Add API test and google-benchmark encode benchmark test-gpu: 7 subtests covering create/destroy, empty encode, glyph encode, reset, draw funcs singleton, shader sources, and recycle_blob. benchmark-gpu: benchmarks draw, encode, and draw+encode per-glyph across several fonts using google-benchmark. Co-Authored-By: Claude Opus 4.6 (1M context) --- perf/benchmark-gpu.cc | 130 ++++++++++++++++++++++++++ perf/meson.build | 11 +++ test/api/meson.build | 12 +++ test/api/test-gpu.cc | 209 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 362 insertions(+) create mode 100644 perf/benchmark-gpu.cc create mode 100644 test/api/test-gpu.cc diff --git a/perf/benchmark-gpu.cc b/perf/benchmark-gpu.cc new file mode 100644 index 000000000..78d90d3cf --- /dev/null +++ b/perf/benchmark-gpu.cc @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb-benchmark.hh" + +#include + +#include + +#define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/" + +static struct test_input_t +{ + const char *font_path; +} default_tests[] = +{ + {SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf"}, + {SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf"}, + {SUBSET_FONT_BASE_PATH "AdobeVFPrototype.otf"}, + {SUBSET_FONT_BASE_PATH "Comfortaa-Regular-new.ttf"}, + {SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf"}, + {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]); + +static void BM_GpuEncode (benchmark::State &state, + const test_input_t &test_input) +{ + hb_face_t *face = hb_benchmark_face_create_from_file_or_fail (test_input.font_path, 0); + if (!face) + { + state.SkipWithError ("Failed to open font file"); + return; + } + + hb_font_t *font = hb_font_create (face); + unsigned num_glyphs = hb_face_get_glyph_count (face); + hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail (); + assert (draw); + + for (auto _ : state) + for (unsigned gid = 0; gid < num_glyphs; ++gid) + { + hb_gpu_draw_reset (draw); + hb_gpu_draw_glyph (draw, font, gid); + hb_blob_t *blob = hb_gpu_draw_encode (draw); + hb_blob_destroy (blob); + } + + hb_gpu_draw_destroy (draw); + hb_font_destroy (font); + hb_face_destroy (face); +} + +static const char *font_file = nullptr; + +static GOptionEntry entries[] = +{ + {"font-file", 0, 0, G_OPTION_ARG_STRING, &font_file, "Font file-path to benchmark", "FONTFILE"}, + {nullptr} +}; + +int main (int argc, char **argv) +{ + benchmark::Initialize (&argc, argv); + + GOptionContext *context = g_option_context_new (""); + g_option_context_set_summary (context, "Benchmark GPU glyph encoding"); + g_option_context_add_main_entries (context, entries, nullptr); + 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 (font_file) + { + num_tests = 1; + tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t)); + tests[0].font_path = font_file; + } + + for (unsigned i = 0; i < num_tests; i++) + { + auto &test_input = tests[i]; + char name[1024] = "BM_GpuEncode/"; + const char *p = strrchr (test_input.font_path, '/'); + strcat (name, p ? p + 1 : test_input.font_path); + + benchmark::RegisterBenchmark (name, BM_GpuEncode, test_input) + ->Unit (benchmark::kMillisecond); + } + + benchmark::RunSpecifiedBenchmarks (); + benchmark::Shutdown (); + + if (tests != default_tests) + free (tests); +} diff --git a/perf/meson.build b/perf/meson.build index e649b080b..261612c9f 100644 --- a/perf/meson.build +++ b/perf/meson.build @@ -26,6 +26,17 @@ foreach source : benchmarks ), workdir: meson.current_source_dir() / '..', timeout: 100) endforeach +if not get_option('gpu').disabled() + benchmark('benchmark-gpu', executable('benchmark-gpu', 'benchmark-gpu.cc', + dependencies: [ + google_benchmark_dep, libharfbuzz_dep, libharfbuzz_gpu_dep + ], + cpp_args: [], + include_directories: [incconfig, incsrc], + install: false, + ), workdir: meson.current_source_dir() / '..', timeout: 100) +endif + if not get_option('subset').disabled() benchmarks_subset = [ 'benchmark-subset.cc', diff --git a/test/api/meson.build b/test/api/meson.build index e867fe23f..6d3a74e09 100644 --- a/test/api/meson.build +++ b/test/api/meson.build @@ -141,3 +141,15 @@ if not get_option('raster').disabled() env: env, suite: ['api', 'raster']) endif + +if not get_option('gpu').disabled() + test('test-gpu', + executable('test-gpu', 'test-gpu.cc', + include_directories: [incconfig], + dependencies: [libharfbuzz_gpu_dep], + install: false, + ), + protocol: 'tap', + env: env, + suite: ['api', 'gpu']) +endif diff --git a/test/api/test-gpu.cc b/test/api/test-gpu.cc new file mode 100644 index 000000000..ad4276a3a --- /dev/null +++ b/test/api/test-gpu.cc @@ -0,0 +1,209 @@ +/* + * Copyright (C) 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb-test.h" +#include "hb-gpu.h" +#include + +#define FONT_FILE "fonts/Roboto-Regular.abc.ttf" + + +static void +test_create_destroy (void) +{ + hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail (); + g_assert_nonnull (draw); + + hb_gpu_draw_t *ref = hb_gpu_draw_reference (draw); + g_assert_true (ref == draw); + hb_gpu_draw_destroy (ref); + + hb_gpu_draw_destroy (draw); +} + +static void +test_empty_encode (void) +{ + hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail (); + g_assert_nonnull (draw); + + /* Encode with no curves should return empty blob. */ + hb_blob_t *blob = hb_gpu_draw_encode (draw); + g_assert_nonnull (blob); + g_assert_cmpuint (hb_blob_get_length (blob), ==, 0); + hb_blob_destroy (blob); + + /* Extents should be zero. */ + hb_glyph_extents_t ext; + hb_gpu_draw_get_extents (draw, &ext); + g_assert_cmpint (ext.x_bearing, ==, 0); + g_assert_cmpint (ext.y_bearing, ==, 0); + g_assert_cmpint (ext.width, ==, 0); + g_assert_cmpint (ext.height, ==, 0); + + hb_gpu_draw_destroy (draw); +} + +static void +test_encode_glyph (void) +{ + hb_face_t *face = hb_test_open_font_file (FONT_FILE); + g_assert_nonnull (face); + hb_font_t *font = hb_font_create (face); + + hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail (); + g_assert_nonnull (draw); + + /* Draw glyph for 'a' (glyph index from cmap). */ + hb_codepoint_t gid; + hb_bool_t found = hb_font_get_nominal_glyph (font, 'a', &gid); + g_assert_true (found); + + hb_gpu_draw_glyph (draw, font, gid); + + /* Encode should produce non-empty blob. */ + hb_blob_t *blob = hb_gpu_draw_encode (draw); + g_assert_nonnull (blob); + g_assert_cmpuint (hb_blob_get_length (blob), >, 0); + + /* Blob size should be a multiple of 8 (RGBA16I texels). */ + g_assert_cmpuint (hb_blob_get_length (blob) % 8, ==, 0); + + /* Extents should be non-zero. */ + hb_glyph_extents_t ext; + hb_gpu_draw_get_extents (draw, &ext); + g_assert_cmpint (ext.width, >, 0); + g_assert_cmpint (ext.height, <, 0); /* height is negative (y-down) */ + + hb_blob_destroy (blob); + hb_gpu_draw_destroy (draw); + hb_font_destroy (font); + hb_face_destroy (face); +} + +static void +test_reset (void) +{ + hb_face_t *face = hb_test_open_font_file (FONT_FILE); + hb_font_t *font = hb_font_create (face); + + hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail (); + g_assert_nonnull (draw); + + hb_codepoint_t gid; + hb_font_get_nominal_glyph (font, 'a', &gid); + + /* Draw, encode, reset, encode again — should work. */ + hb_gpu_draw_glyph (draw, font, gid); + hb_blob_t *blob1 = hb_gpu_draw_encode (draw); + g_assert_cmpuint (hb_blob_get_length (blob1), >, 0); + hb_blob_destroy (blob1); + + hb_gpu_draw_reset (draw); + + /* After reset, encode should be empty. */ + hb_blob_t *blob2 = hb_gpu_draw_encode (draw); + g_assert_cmpuint (hb_blob_get_length (blob2), ==, 0); + hb_blob_destroy (blob2); + + /* Draw again after reset. */ + hb_gpu_draw_glyph (draw, font, gid); + hb_blob_t *blob3 = hb_gpu_draw_encode (draw); + g_assert_cmpuint (hb_blob_get_length (blob3), >, 0); + hb_blob_destroy (blob3); + + hb_gpu_draw_destroy (draw); + hb_font_destroy (font); + hb_face_destroy (face); +} + +static void +test_draw_funcs (void) +{ + hb_draw_funcs_t *funcs = hb_gpu_draw_get_funcs (); + g_assert_nonnull (funcs); + + /* Should be the same singleton each time. */ + g_assert_true (funcs == hb_gpu_draw_get_funcs ()); +} + +static void +test_shader_sources (void) +{ + unsigned count = 0; + const char * const *sources; + + sources = hb_gpu_shader_fragment_sources (HB_GPU_SHADER_GLSL_330, &count); + g_assert_nonnull (sources); + g_assert_cmpuint (count, >, 0); + for (unsigned i = 0; i < count; i++) + g_assert_nonnull (sources[i]); + + sources = hb_gpu_shader_vertex_sources (HB_GPU_SHADER_GLSL_330, &count); + g_assert_nonnull (sources); + g_assert_cmpuint (count, >, 0); + for (unsigned i = 0; i < count; i++) + g_assert_nonnull (sources[i]); +} + +static void +test_recycle_blob (void) +{ + hb_face_t *face = hb_test_open_font_file (FONT_FILE); + hb_font_t *font = hb_font_create (face); + + hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail (); + + hb_codepoint_t gid; + hb_font_get_nominal_glyph (font, 'b', &gid); + hb_gpu_draw_glyph (draw, font, gid); + + hb_blob_t *blob = hb_gpu_draw_encode (draw); + g_assert_nonnull (blob); + + /* Recycle should not crash. */ + hb_gpu_draw_recycle_blob (draw, blob); + + hb_gpu_draw_destroy (draw); + hb_font_destroy (font); + hb_face_destroy (face); +} + +int +main (int argc, char **argv) +{ + hb_test_init (&argc, &argv); + + hb_test_add (test_create_destroy); + hb_test_add (test_empty_encode); + hb_test_add (test_encode_glyph); + hb_test_add (test_reset); + hb_test_add (test_draw_funcs); + hb_test_add (test_shader_sources); + hb_test_add (test_recycle_blob); + + return hb_test_run (); +}