A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
perf(text): a new glyph is drawn and distance-transformed at 2x rather than 4x, and the transform skips what cannot change its bytes, so a phone pays a third of what it did
yhh9ba5ebb25e34
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e2f4e7bd0..78c1555be 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -22,7 +22,8 @@ published separately; it ships inside the editor.### Fixed-- **A native Android build shows CJK text from the system font.** Android 16's system CJK font (NotoSansCJK-Regular.ttc) has CFF2 outlines, which stb_truetype cannot read, and only Apple had the platform fallback for that, so on such a phone every CJK character from the system font drew nothing. Android now draws such a face with android.graphics: the same file and face its font matcher chose, with synthetic bold and italic where the face lacks them, handed back as coverage so the distance field and the atlas tile stay the shared code's. A new CJK glyph costs 5.3 ms on a Snapdragon 8 Elite with its distance field (0.7 ms without), against 3.4 ms for a Latin one through stb_truetype. A project that bundles its own font is not affected.+- **A new character costs a phone about a third of what it did.** A glyph was drawn and distance-transformed at 4x its stored size, 16 times the pixels, and on a phone most of the time went there. It is now 2x, on the web and on every native host alike: at 12x magnification 2x and 4x look the same on the densest CJK glyph, where 1x loses its counters. The distance transform also leaves out the sites that cannot change a byte of its result, tested byte for byte against the plain transform. On a Snapdragon 8 Elite a new CJK glyph with its distance field went from 5.3 to 1.8 ms, and a Latin one from 3.4 to 0.74 ms. Text can differ from before by a few levels at its edges, up to 6 of 255 in the field.+- **A native Android build shows CJK text from the system font.** Android 16's system CJK font (NotoSansCJK-Regular.ttc) has CFF2 outlines, which stb_truetype cannot read, and only Apple had the platform fallback for that, so on such a phone every CJK character from the system font drew nothing. Android now draws such a face with android.graphics: the same file and face its font matcher chose, with synthetic bold and italic where the face lacks them, handed back as coverage so the distance field and the atlas tile stay the shared code's. A project that bundles its own font is not affected.- **A new glyph on a native build no longer stalls the frame reading a whole system font** (#106). Every native host (Android, iOS, macOS, Windows, Linux) read the matched font file whole on every call, which is every glyph rasterized, and the rasterizer then dropped those bytes for a face it had already parsed. A system CJK font is tens of MB: on a Snapdragon 8 Elite phone that read was 9.7 ms a glyph (63 ms for the first). A platform now only answers which file and which face (Platform::matchFont), and the shared rasterizer maps each face's file the first time it sees it, so the OS pages in only the tables and glyphs it touches. The read is gone: looking a face up costs 0.02 ms on the phone. On Windows a new CJK glyph, rasterized and turned into a distance field, costs 0.9 ms.- **Android frames no longer drop to 30 fps when they come close to the frame budget** (#105). The Android host drew each frame from a Choreographer callback and asked for the next one after the frame, whose present had already blocked until the display took it: two clocks, and a frame that ran past one vsync waited for a second. A 15 ms frame on a 60 fps cap ran at 30 fps on a Snapdragon 8 Elite, every interval 33 ms; a shooter in the issue saw 33/50/66 ms. The host now runs its own loop, as the desktop one does: the present paces it, input is served while it waits, and a frame is drawn when one is due even while a fast touch panel keeps the queue full. The same 15 ms frame holds 60 fps (p99 18.5 ms), and a 20 ms one runs at 46 fps instead of halving. A backgrounded app still draws nothing and uses no CPU. The cap App.setTargetFrameRate sets is now held by one pacer every native host shares (the web loop's rule: a cadence, restarted after a stall), so the desktop and iOS hosts, which ignored it, keep it too.- **A native build reports its GPU frame time.** The desktop and Android hosts never asked the GPU for timestamps, so the engine's GPU frame timer and the profiler's GPU track read -1 on every device. They ask for them now where the GPU has them.diff --git a/native/README.md b/native/README.mdindex 3de839dde..5d22e1d28 100644--- a/native/README.md+++ b/native/README.md@@ -112,7 +112,7 @@ the tree:| Render surface | `WebGPUDevice::configureSurface(NativeSurface)` | `CAMetalLayer*` / `ANativeWindow*` → `WGPUSurface` (commit `bfc495da`) || Instance / present | `WebGPUDevice(device, instance)` + `present()` | host shares its instance; flips the swapchain (native-only) || GL gate | `EstellaContext` under `ES_PLATFORM_WEB` | native drops the WebGL entry |-| Glyph source | `Platform::loadFont` + `host/media/glyph_raster.cpp` | the device has no 2D canvas: the OS names a font file, stb_truetype rasterizes, the engine's own `sdfFromAlpha` encodes |+| Glyph source | `Platform::matchFont` + `host/media/glyph_raster.cpp` | the device has no 2D canvas: the OS names a font file, stb_truetype rasterizes (the OS itself where stb cannot read the face), the engine's own `sdfFromAlpha` encodes || Bindings | `python -m eht --native-output` | `es_set_<C>` / `es_<C>_buffer` from the same reflection as the web embind bindings || Entry points | `python -m eht --native-functions` | `es_renderer_*`, `es_uiLayout_*`, `es_rm_*` — native wrappers (written against `js/esjs.hpp`) over the SAME `bindings/*.hpp` declarations embind registers || Data marshalling | `sdk/src/ecs/ptrAccessors.generated.ts` | POD components are wasm32/arm64 layout-identical → the generated accessors write native component memory unchanged (via a zero-copy `ArrayBuffer`) |diff --git a/native/android/java/com/estella/host/GlyphPainter.java b/native/android/java/com/estella/host/GlyphPainter.javaindex 13e4e3564..73c90b6da 100644--- a/native/android/java/com/estella/host/GlyphPainter.java+++ b/native/android/java/com/estella/host/GlyphPainter.java@@ -6,6 +6,7 @@ import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Path;+import android.graphics.Rect;import android.graphics.RectF;import android.graphics.Typeface;@@ -32,6 +33,7 @@ final class GlyphPainter {private static final Paint PAINT = new Paint(Paint.ANTI_ALIAS_FLAG);private static final Path PATH = new Path();private static final RectF BOX = new RectF();+ private static final Rect BOUNDS = new Rect();/** The oblique shear the shared rasterizer synthesizes italic with (FreeType's 12 degrees). */private static final float OBLIQUE_SHEAR = 0.2126f;@@ -72,10 +74,15 @@ final class GlyphPainter {if (!p.hasGlyph(text)) return null;metrics[2] = p.measureText(text) / ss;- // The ink box from the outline itself, y down from the baseline: it follows the shear, where a text-bounds query- // reports the upright glyph. Fake bold strokes past the outline, so it is widened by that stroke.- p.getTextPath(text, 0, text.length(), 0f, 0f, PATH);- PATH.computeBounds(BOX, true);+ // The ink box, y down from the baseline. Sheared, it has to come from the outline, since a text-bounds query reports+ // the upright glyph; upright, that query is the cheap way. Fake bold strokes past both, so it is widened by that.+ if (syntheticItalic) {+ p.getTextPath(text, 0, text.length(), 0f, 0f, PATH);+ PATH.computeBounds(BOX, true);+ } else {+ p.getTextBounds(text, 0, text.length(), BOUNDS);+ BOX.set(BOUNDS);+ }if (BOX.isEmpty()) {metrics[0] = 0; metrics[1] = 0; metrics[3] = 0; metrics[4] = 0; metrics[5] = 1;return new byte[0];diff --git a/native/host/media/glyph_raster.cpp b/native/host/media/glyph_raster.cppindex 37551ef61..6b19cb113 100644--- a/native/host/media/glyph_raster.cpp+++ b/native/host/media/glyph_raster.cpp@@ -26,8 +26,9 @@ namespace {// The SDF is rasterized and distance-transformed at this multiple of the stored// resolution, then box-downsampled — magnified glyphs stop showing the source-// grid as edge wobble. The SDK's canvas rasterizer supersamples by the same 4.-constexpr int SDF_SUPERSAMPLE = 4;+// grid as edge wobble. The SDK's canvas rasterizer uses the same number, and says+// why it is 2 (sdk/src/ui/text/glyph-rasterizer.ts).+constexpr int SDF_SUPERSAMPLE = 2;/** A parsed font file, kept for the life of the host: fonts are few, glyphs many. Mapped, not read (mapped_file.hpp): the* parser touches the tables and the glyphs drawn, which is a small part of a CJK font. */@@ -96,12 +97,12 @@ std::vector<uint8_t> downsample(const std::vector<uint8_t>& src, int width, intconst int area = factor * factor;for (int y = 0; y < h; y++) {for (int x = 0; x < w; x++) {- int sum = 0;+ int sum = area / 2; // rounds to nearestfor (int sy = 0; sy < factor; sy++) {const uint8_t* row = src.data() + (size_t)(y * factor + sy) * width + (size_t)x * factor;for (int sx = 0; sx < factor; sx++) sum += row[sx];}- out[(size_t)y * w + x] = (uint8_t)std::lround((double)sum / area);+ out[(size_t)y * w + x] = (uint8_t)(sum / area);}}return out;diff --git a/sdk/src/ui/text/glyph-rasterizer.ts b/sdk/src/ui/text/glyph-rasterizer.tsindex 0eaa3eafd..a69b188a4 100644--- a/sdk/src/ui/text/glyph-rasterizer.ts+++ b/sdk/src/ui/text/glyph-rasterizer.ts@@ -80,10 +80,14 @@ export interface CanvasGlyphRasterizerOptions {type Canvas2D = PlatformCanvas;type Ctx2D = PlatformCanvas2DContext;-// SDF supersampling: rasterize + distance-transform at 4× the stored+// SDF supersampling: rasterize + distance-transform at 2× the stored// resolution, then box-downsample — magnified glyphs stop showing the source-// grid as edge wobble. One-time per glyph; atlas memory is unchanged.-const SDF_SUPERSAMPLE = 4;+// grid as edge wobble. One-time per glyph; atlas memory is unchanged. 2 and not+// more: at 12× magnification 2× and 4× look the same on the densest CJK glyph+// (1× loses its counters), and 4× costs 4 times the pixels to draw and transform,+// which a phone pays in the frame a new character appears. The native rasterizer+// uses the same number (native/host/media/glyph_raster.cpp).+const SDF_SUPERSAMPLE = 2;export class CanvasGlyphRasterizer implements GlyphRasterizer {readonly renderSize: number;diff --git a/src/esengine/text/SdfGenerator.cpp b/src/esengine/text/SdfGenerator.cppindex d2ee346ab..c5eca54db 100644--- a/src/esengine/text/SdfGenerator.cpp+++ b/src/esengine/text/SdfGenerator.cpp@@ -8,7 +8,8 @@* coverage-derived fractional seeds (tiny-sdf lineage): a partially covered* texel seeds the transform with (0.5 − α)², placing the edge at sub-texel* accuracy so magnified glyphs stay smooth. Interior and exterior fields are- * subtracted for the signed distance; the 1D transform is exact and O(n).+ * subtracted for the signed distance; the 1D transform is exact and O(n), and+ * skips the sites that cannot change a byte of the result (see edt1d).*/#include "SdfGenerator.hpp"@@ -20,24 +21,47 @@ namespace esengine::text {namespace {constexpr f32 kInf = 1e20f;+/** Texels of scratch kept between calls: a glyph at a time, a few hundred of them, but not a one-off giant's worth for good. */+constexpr usize kKeptScratch = 1u << 20;++struct Scratch {+ std::vector<f32> outer, inner, d, z;+ std::vector<i32> v;+};/*** 1D squared-distance transform (Felzenszwalb & Huttenlocher): d[q] =- * min_p((q-p)² + f[p]). The additive seed cost f is what lets partially- * covered texels contribute fractional distances. v/z are the parabola- * lower-envelope scratch (v: site indices, z: envelope boundaries).+ * min_p((q-p)² + f[p]), f read every `stride` floats. The additive seed cost f is+ * what lets partially covered texels contribute fractional distances. v/z are the+ * parabola lower-envelope scratch (v: site indices, z: envelope boundaries).+ *+ * Only the sites that can matter enter the envelope, which is where a glyph's time+ * goes. One at or past `limit` is farther than anything the field encodes (a texel+ * that only it could answer has clamped either way). A zero strictly inside a run+ * of zeros answers for itself alone: past the run, an end of it is nearer. Returns+ * false when no site is left, with d untouched: the line stays as it was.*/-void edt1d(f32* f, f32* d, i32* v, f32* z, i32 n) {- i32 k = 0;- v[0] = 0;- z[0] = -kInf;- z[1] = kInf;- for (i32 q = 1; q < n; ++q) {+bool edt1d(const f32* f, usize stride, f32* d, i32* v, f32* z, i32 n, f32 limit) {+ i32 k = -1;+ for (i32 q = 0; q < n; ++q) {+ const f32 fq = f[static_cast<usize>(q) * stride];+ if (fq >= limit) continue;+ if (fq == 0.0f && q > 0 && q + 1 < n+ && f[static_cast<usize>(q - 1) * stride] == 0.0f && f[static_cast<usize>(q + 1) * stride] == 0.0f) {+ continue;+ }+ if (k < 0) {+ k = 0;+ v[0] = q;+ z[0] = -kInf;+ z[1] = kInf;+ continue;+ }+ const f32 sq = fq + static_cast<f32>(q) * q;f32 s;while (true) {const i32 p = v[k];- s = (f[q] + static_cast<f32>(q) * q - (f[p] + static_cast<f32>(p) * p))- / (2.0f * (q - p));+ s = (sq - (f[static_cast<usize>(p) * stride] + static_cast<f32>(p) * p)) / (2.0f * (q - p));if (s > z[k]) break;--k;}@@ -46,27 +70,30 @@ void edt1d(f32* f, f32* d, i32* v, f32* z, i32 n) {z[k] = s;z[k + 1] = kInf;}+ if (k < 0) return false;k = 0;for (i32 q = 0; q < n; ++q) {+ if (f[static_cast<usize>(q) * stride] == 0.0f) {+ d[q] = 0.0f;+ continue;+ }while (z[k + 1] < static_cast<f32>(q)) ++k;const f32 dx = static_cast<f32>(q - v[k]);- d[q] = dx * dx + f[v[k]];+ d[q] = dx * dx + f[static_cast<usize>(v[k]) * stride];}+ return true;}/** 2D squared EDT in place: columns then rows, sharing the 1D scratch. */-void edt2d(std::vector<f32>& grid, i32 w, i32 h,- std::vector<f32>& f, std::vector<f32>& d,- std::vector<i32>& v, std::vector<f32>& z) {+void edt2d(f32* grid, i32 w, i32 h, Scratch& s, f32 limit) {for (i32 x = 0; x < w; ++x) {- for (i32 y = 0; y < h; ++y) f[static_cast<usize>(y)] = grid[static_cast<usize>(y) * w + x];- edt1d(f.data(), d.data(), v.data(), z.data(), h);- for (i32 y = 0; y < h; ++y) grid[static_cast<usize>(y) * w + x] = d[static_cast<usize>(y)];+ if (!edt1d(grid + x, static_cast<usize>(w), s.d.data(), s.v.data(), s.z.data(), h, limit)) continue;+ for (i32 y = 0; y < h; ++y) grid[static_cast<usize>(y) * w + x] = s.d[static_cast<usize>(y)];}for (i32 y = 0; y < h; ++y) {- f32* row = grid.data() + static_cast<usize>(y) * w;- edt1d(row, d.data(), v.data(), z.data(), w);- std::copy_n(d.data(), w, row);+ f32* row = grid + static_cast<usize>(y) * w;+ if (!edt1d(row, 1, s.d.data(), s.v.data(), s.z.data(), w, limit)) continue;+ std::copy_n(s.d.data(), w, row);}}@@ -78,23 +105,36 @@ void sdfFromAlpha(const u8* alpha, u8* out, u32 width, u32 height, f32 spread) {const i32 w = static_cast<i32>(width);const i32 h = static_cast<i32>(height);const usize n = static_cast<usize>(w) * static_cast<usize>(h);- const i32 m = std::max(w, h);+ const usize m = static_cast<usize>(std::max(w, h));++ static thread_local Scratch scratch;+ Scratch& s = scratch;+ if (s.outer.size() < n) {+ s.outer.resize(n);+ s.inner.resize(n);+ }+ if (s.d.size() < m) {+ s.d.resize(m);+ s.v.resize(m);+ s.z.resize(m + 1);+ }+ f32* outer = s.outer.data();+ f32* inner = s.inner.data();// Squared distance to the glyph interior (outer) and exterior (inner).// Coverage seeds: a fully covered texel is interior (outer seed 0); a// fully empty one is exterior (inner seed 0); a partial texel sits within// half a texel of the edge, on the side its coverage says.- std::vector<f32> outer(n);- std::vector<f32> inner(n);for (usize i = 0; i < n; ++i) {- const f32 a = static_cast<f32>(alpha[i]) / 255.0f;- if (a >= 1.0f) {+ const u8 c = alpha[i];+ if (c == 255) {outer[i] = 0.0f;inner[i] = kInf;- } else if (a <= 0.0f) {+ } else if (c == 0) {outer[i] = kInf;inner[i] = 0.0f;} else {+ const f32 a = static_cast<f32>(c) / 255.0f;const f32 dOut = std::max(0.0f, 0.5f - a); // toward the interior sideconst f32 dIn = std::max(0.0f, a - 0.5f); // toward the exterior sideouter[i] = dOut * dOut;@@ -102,19 +142,26 @@ void sdfFromAlpha(const u8* alpha, u8* out, u32 width, u32 height, f32 spread) {}}- std::vector<f32> f(static_cast<usize>(m));- std::vector<f32> d(static_cast<usize>(m));- std::vector<i32> v(static_cast<usize>(m));- std::vector<f32> z(static_cast<usize>(m) + 1);- edt2d(outer, w, h, f, d, v, z);- edt2d(inner, w, h, f, d, v, z);+ // Past spread + 1 the byte has clamped: wherever one field is that far, the texel is fully on one side and the other is 0.+ const f32 limit = (spread > 0.0f) ? (spread + 1.0f) * (spread + 1.0f) : kInf;+ edt2d(outer, w, h, s, limit);+ edt2d(inner, w, h, s, limit);const f32 scale = (spread > 0.0f) ? (127.0f / spread) : 127.0f;for (usize i = 0; i < n; ++i) {- // + inside, − outside, ~0 at the (sub-texel) edge.- const f32 signedDist = std::sqrt(inner[i]) - std::sqrt(outer[i]);- const i32 b = static_cast<i32>(std::lround(128.0f + signedDist * scale));- out[i] = static_cast<u8>(std::clamp(b, 0, 255));+ // + inside, − outside, ~0 at the (sub-texel) edge. Most texels have one side at exactly 0.+ const f32 toOut = inner[i] == 0.0f ? 0.0f : std::sqrt(inner[i]);+ const f32 toIn = outer[i] == 0.0f ? 0.0f : std::sqrt(outer[i]);+ const f32 v = 128.5f + (toOut - toIn) * scale;+ out[i] = v <= 0.0f ? u8(0) : v >= 255.0f ? u8(255) : static_cast<u8>(v);+ }++ if (n > kKeptScratch) {+ s.outer = {};+ s.inner = {};+ s.d = {};+ s.v = {};+ s.z = {};}}diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txtindex 6cf3041b5..a42c07e2d 100644--- a/tests/CMakeLists.txt+++ b/tests/CMakeLists.txt@@ -122,6 +122,13 @@ add_executable(test_mapped_filetarget_include_directories(test_mapped_file PRIVATE ${CMAKE_SOURCE_DIR}/native/host)add_test(NAME test_mapped_file COMMAND test_mapped_file)+# The glyph SDF generator, byte for byte against the transform with no shortcuts.+add_executable(test_sdf_generator+ text/test_sdf_generator.cpp+)+target_link_libraries(test_sdf_generator PRIVATE esengine)+add_test(NAME test_sdf_generator COMMAND test_sdf_generator)+# Tilemap system testsadd_executable(test_tilemaptilemap/test_tilemap.cppdiff --git a/tests/text/test_sdf_generator.cpp b/tests/text/test_sdf_generator.cppnew file mode 100644index 000000000..a313a7400--- /dev/null+++ b/tests/text/test_sdf_generator.cpp@@ -0,0 +1,177 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file The glyph SDF generator skips the sites of its distance transform that+ * cannot change a byte of the result. These hold it, byte for byte, against+ * the transform with no shortcuts, on shapes that exercise each skip.+ */+#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN+#include "doctest.h"++#include "esengine/text/SdfGenerator.hpp"++#include <algorithm>+#include <cmath>+#include <cstdlib>+#include <vector>++using esengine::u8;+using esengine::text::sdfFromAlpha;++namespace {++constexpr float kInf = 1e20f;++/** The plain Felzenszwalb–Huttenlocher transform: every site enters the envelope. */+void plain1d(const float* f, float* d, int* v, float* z, int n) {+ int k = 0;+ v[0] = 0;+ z[0] = -kInf;+ z[1] = kInf;+ for (int q = 1; q < n; ++q) {+ float s;+ while (true) {+ const int p = v[k];+ s = (f[q] + float(q) * q - (f[p] + float(p) * p)) / (2.0f * (q - p));+ if (s > z[k]) break;+ --k;+ }+ ++k;+ v[k] = q;+ z[k] = s;+ z[k + 1] = kInf;+ }+ k = 0;+ for (int q = 0; q < n; ++q) {+ while (z[k + 1] < float(q)) ++k;+ const float dx = float(q - v[k]);+ d[q] = dx * dx + f[v[k]];+ }+}++void plain2d(std::vector<float>& g, int w, int h) {+ const int m = std::max(w, h);+ std::vector<float> f(m), d(m), z(m + 1);+ std::vector<int> v(m);+ for (int x = 0; x < w; ++x) {+ for (int y = 0; y < h; ++y) f[y] = g[size_t(y) * w + x];+ plain1d(f.data(), d.data(), v.data(), z.data(), h);+ for (int y = 0; y < h; ++y) g[size_t(y) * w + x] = d[y];+ }+ for (int y = 0; y < h; ++y) {+ plain1d(&g[size_t(y) * w], d.data(), v.data(), z.data(), w);+ std::copy_n(d.data(), w, &g[size_t(y) * w]);+ }+}++std::vector<u8> plainSdf(const std::vector<u8>& alpha, int w, int h, float spread) {+ const size_t n = size_t(w) * h;+ std::vector<float> outer(n), inner(n);+ for (size_t i = 0; i < n; ++i) {+ const float a = alpha[i] / 255.0f;+ if (a >= 1.0f) { outer[i] = 0; inner[i] = kInf; }+ else if (a <= 0.0f) { outer[i] = kInf; inner[i] = 0; }+ else {+ const float dOut = std::max(0.0f, 0.5f - a), dIn = std::max(0.0f, a - 0.5f);+ outer[i] = dOut * dOut;+ inner[i] = dIn * dIn;+ }+ }+ plain2d(outer, w, h);+ plain2d(inner, w, h);+ const float scale = spread > 0 ? 127.0f / spread : 127.0f;+ std::vector<u8> out(n);+ for (size_t i = 0; i < n; ++i) {+ // Clamped before it is rounded: a grid with no exterior reads about 1e10 here, past a 32-bit long.+ const long b = std::lround(std::clamp(128.0f + (std::sqrt(inner[i]) - std::sqrt(outer[i])) * scale, -1.0f, 256.0f));+ out[i] = (u8)std::clamp(b, 0L, 255L);+ }+ return out;+}++void holdAgainstPlain(const std::vector<u8>& alpha, int w, int h, float spread) {+ std::vector<u8> out(alpha.size(), 77);+ sdfFromAlpha(alpha.data(), out.data(), (esengine::u32)w, (esengine::u32)h, spread);+ const std::vector<u8> want = plainSdf(alpha, w, h, spread);+ int worst = 0;+ for (size_t i = 0; i < out.size(); ++i) worst = std::max(worst, std::abs(int(out[i]) - int(want[i])));+ CHECK(worst == 0);+}++/** Antialiased disc: every edge direction, partial texels all round. */+void disc(std::vector<u8>& a, int w, int h, float cx, float cy, float r) {+ for (int y = 0; y < h; ++y) for (int x = 0; x < w; ++x) {+ const float d = std::hypot(x + 0.5f - cx, y + 0.5f - cy) - r;+ const float c = std::clamp(0.5f - d, 0.0f, 1.0f);+ a[size_t(y) * w + x] = std::max(a[size_t(y) * w + x], (u8)std::lround(c * 255));+ }+}++} // namespace++TEST_CASE("every skip gives the plain transform's bytes") {+ const int w = 120, h = 96;+ const size_t n = size_t(w) * h;++ SUBCASE("nothing: every line has no site in one field") {+ holdAgainstPlain(std::vector<u8>(n, 0), w, h, 12.0f);+ }+ SUBCASE("everything covered") {+ holdAgainstPlain(std::vector<u8>(n, 255), w, h, 12.0f);+ }+ SUBCASE("a single partial texel") {+ std::vector<u8> a(n, 0);+ a[size_t(40) * w + 60] = 90;+ holdAgainstPlain(a, w, h, 12.0f);+ }+ SUBCASE("hard-edged bars: long runs of zeros in both fields") {+ std::vector<u8> a(n, 0);+ for (int y = 10; y < 22; ++y) for (int x = 8; x < 110; ++x) a[size_t(y) * w + x] = 255;+ for (int y = 40; y < 90; ++y) for (int x = 50; x < 54; ++x) a[size_t(y) * w + x] = 255;+ holdAgainstPlain(a, w, h, 12.0f);+ }+ SUBCASE("ink touching every border") {+ std::vector<u8> a(n, 0);+ for (int x = 0; x < w; ++x) { a[x] = 255; a[size_t(h - 1) * w + x] = 255; }+ for (int y = 0; y < h; ++y) { a[size_t(y) * w] = 200; a[size_t(y) * w + w - 1] = 40; }+ holdAgainstPlain(a, w, h, 12.0f);+ }+ SUBCASE("antialiased discs, near and far apart past the spread") {+ std::vector<u8> a(n, 0);+ disc(a, w, h, 20, 20, 12);+ disc(a, w, h, 95, 70, 18);+ disc(a, w, h, 30, 80, 3);+ holdAgainstPlain(a, w, h, 12.0f);+ holdAgainstPlain(a, w, h, 3.0f);+ holdAgainstPlain(a, w, h, 40.0f);+ }+ SUBCASE("noise: partial texels everywhere") {+ std::vector<u8> a(n);+ unsigned seed = 12345;+ for (size_t i = 0; i < n; ++i) {+ seed = seed * 1103515245u + 12345u;+ a[i] = (seed >> 16) & 7 ? 0 : (u8)((seed >> 8) & 255);+ }+ holdAgainstPlain(a, w, h, 12.0f);+ }+ SUBCASE("no spread") {+ std::vector<u8> a(n, 0);+ disc(a, w, h, 60, 48, 20);+ holdAgainstPlain(a, w, h, 0.0f);+ }+}++TEST_CASE("sizes in turn share the kept scratch without leaking between them") {+ std::vector<u8> big(size_t(200) * 200, 0);+ disc(big, 200, 200, 100, 100, 60);+ holdAgainstPlain(big, 200, 200, 12.0f);+ std::vector<u8> small(size_t(16) * 24, 0);+ disc(small, 16, 24, 8, 12, 5);+ holdAgainstPlain(small, 16, 24, 4.0f);+ holdAgainstPlain(big, 200, 200, 12.0f);+ // Past what is kept: the scratch is dropped, and the next call has to come back with its own.+ std::vector<u8> huge(size_t(1100) * 1000, 0);+ disc(huge, 1100, 1000, 500, 500, 300);+ holdAgainstPlain(huge, 1100, 1000, 8.0f);+ holdAgainstPlain(small, 16, 24, 4.0f);+}diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex d85da4033..c7a6c1001 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -3836,4 +3836,6 @@ export const NOTED = {{"note":"A new glyph on a native build no longer stalls the frame reading a whole system font."},"fix(native): Android draws a face the shared rasterizer cannot read with its own, as Apple does, so a native build shows the system's CJK text":{"note":"A native Android build shows CJK text from the system font."},+ "perf(text): a new glyph is drawn and distance-transformed at 2x rather than 4x, and the transform skips what cannot change its bytes, so a phone pays a third of what it did":+ {"note":"A new character costs a phone about a third of what it did."},};