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
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
yhhe427a28f414e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex aa8a64a08..e2f4e7bd0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -22,7 +22,8 @@ published separately; it ships inside the editor.### Fixed-- **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 a new CJK glyph took 9.7 ms (the first 63 ms), all of it the read. 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 same glyphs now cost 0.02 ms on the phone; on Windows a new CJK glyph, rasterized and turned into a distance field, costs 0.9 ms.+- **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 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.- **Voxel GI costs less on mobile GPUs.** The gather, which only reads the cache, reads it as a plain array, and the per-frame ageing reads and writes each slot plainly (each invocation owns one) and leaves empty slots alone; both had gone through atomics. On a Snapdragon 8 Elite phone at 2670x1200 a frame with one ray per point went from 134 to 83 ms, and with a 4-voxel range from 32 to 17 ms. Voxel GI is still too heavy for a phone at its defaults (about 0.8 s a frame).diff --git a/native/android/java/com/estella/host/GlyphPainter.java b/native/android/java/com/estella/host/GlyphPainter.javanew file mode 100644index 000000000..13e4e3564--- /dev/null+++ b/native/android/java/com/estella/host/GlyphPainter.java@@ -0,0 +1,116 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+package com.estella.host;++import android.graphics.Bitmap;+import android.graphics.Canvas;+import android.graphics.Paint;+import android.graphics.Path;+import android.graphics.RectF;+import android.graphics.Typeface;++import java.io.File;+import java.nio.ByteBuffer;+import java.util.HashMap;++/**+ * The platform's own rasterizer, for a face the shared one cannot read.+ *+ * Android's system CJK font (NotoSansCJK-Regular.ttc on Android 16) is a variable+ * font with CFF2 outlines, which stb_truetype does not parse, so the native glyph+ * path drew no CJK text from the system font at all. This draws the SAME face the+ * font matcher chose (the file and the collection index it answered), as Core Text+ * does on Apple, and hands back coverage in the shape Platform::GlyphCoverage+ * holds; the distance field and the atlas tile are made by the shared code. Called+ * only from the frame loop's thread, so the paint and the caches need no lock.+ */+final class GlyphPainter {+ private GlyphPainter() {}++ /** Typefaces by "path#index": built once, as the shared path parses a face once. */+ private static final HashMap<String, Typeface> FACES = new HashMap<>();+ 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();+ /** The oblique shear the shared rasterizer synthesizes italic with (FreeType's 12 degrees). */+ private static final float OBLIQUE_SHEAR = 0.2126f;++ private static Typeface face(String path, int index) {+ final String key = path + '#' + index;+ if (FACES.containsKey(key)) return FACES.get(key);+ Typeface face = null;+ try {+ face = new Typeface.Builder(new File(path)).setTtcIndex(index).build();+ } catch (RuntimeException e) {+ face = null;+ }+ FACES.put(key, face); // a failure too, so it is not retried for every glyph+ return face;+ }++ /**+ * Draws @p codepoint from face @p index of @p path at @p pixelSize times @p supersample.+ *+ * @param metrics out: width, height (supersampled px, padding included), advance, bearingX, bearingY (the tile's+ * top-left in pen space, y up, at the asked size), blank (1 for whitespace).+ * @return the coverage, width * height bytes, top row first; an empty array for a blank glyph; null when the face+ * cannot be built or has no glyph for the codepoint.+ */+ static byte[] draw(String path, int index, int codepoint, float pixelSize, int supersample, int padding,+ boolean syntheticBold, boolean syntheticItalic, float[] metrics) {+ final Typeface face = face(path, index);+ if (face == null || pixelSize <= 0 || metrics == null || metrics.length < 6) return null;+ final int ss = Math.max(supersample, 1);+ final String text = new String(Character.toChars(codepoint));++ final Paint p = PAINT;+ p.setTypeface(face);+ p.setTextSize(pixelSize * ss);+ p.setFakeBoldText(syntheticBold);+ p.setTextSkewX(syntheticItalic ? -OBLIQUE_SHEAR : 0f);+ p.setColor(0xFFFFFFFF);+ 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);+ if (BOX.isEmpty()) {+ metrics[0] = 0; metrics[1] = 0; metrics[3] = 0; metrics[4] = 0; metrics[5] = 1;+ return new byte[0];+ }+ final float grow = syntheticBold ? pixelSize * ss / 24f : 0f;+ final int x0 = (int) Math.floor(BOX.left - grow);+ final int top = (int) Math.floor(BOX.top - grow);+ final int inkW = (int) Math.ceil(BOX.right + grow) - x0;+ final int inkH = (int) Math.ceil(BOX.bottom + grow) - top;++ // Padding is in stored px, so the tile is sized there and multiplied up: the grid stays an exact multiple of+ // the supersample, as the shared path's does.+ final int w = (inkW + ss - 1) / ss + padding * 2;+ final int h = (inkH + ss - 1) / ss + padding * 2;+ final int wss = w * ss;+ final int hss = h * ss;++ final Bitmap bitmap = Bitmap.createBitmap(wss, hss, Bitmap.Config.ALPHA_8);+ // The ink's top-left `padding` stored px in from the tile's.+ new Canvas(bitmap).drawText(text, padding * ss - x0, padding * ss - top, p);+ final int stride = bitmap.getRowBytes();+ final byte[] rows = new byte[stride * hss];+ bitmap.copyPixelsToBuffer(ByteBuffer.wrap(rows));+ bitmap.recycle();+ byte[] alpha = rows;+ if (stride != wss) {+ alpha = new byte[wss * hss];+ for (int y = 0; y < hss; y++) System.arraycopy(rows, y * stride, alpha, y * wss, wss);+ }++ metrics[0] = wss;+ metrics[1] = hss;+ metrics[3] = (float) x0 / ss - padding;+ metrics[4] = (float) -top / ss + padding;+ metrics[5] = 0;+ return alpha;+ }+}diff --git a/native/host/Host.hpp b/native/host/Host.hppindex 87603782c..ff0d10fa4 100644--- a/native/host/Host.hpp+++ b/native/host/Host.hpp@@ -240,8 +240,8 @@ struct Platform {/*** Draw @p codepoint with the platform's OWN rasterizer, for a system whose- * font files the shared parser cannot read — Apple ships CFF2 outlines and- * stb_truetype has no CFF2. Reached only where the shared path could not, so+ * font files the shared parser cannot read — Apple and Android ship CFF2 outlines+ * and stb_truetype has no CFF2. Reached only where the shared path could not, so* the default false keeps stb_truetype and costs a platform nothing.*/virtual bool drawGlyph(const std::string& /*family*/, esengine::u32 /*codepoint*/,diff --git a/native/host/platform/android.cpp b/native/host/platform/android.cppindex ab1d8d519..906c6c5f5 100644--- a/native/host/platform/android.cpp+++ b/native/host/platform/android.cpp@@ -306,6 +306,58 @@ struct AndroidPlatform final : eshost::Platform {return out;}+ // -- The platform's own rasterizer: com.estella.host.GlyphPainter ----------+ //+ // For a face stb_truetype cannot read: Android 16's system CJK font has CFF2+ // outlines, so without this the native path drew no system CJK text at all.+ // The same face the matcher chose is drawn by android.graphics, as Core Text+ // draws it on Apple (Platform::drawGlyph).++ jclass glyphPainter = nullptr; // global ref, set by attachJavaShims+ jmethodID glyphPainterDraw = nullptr;++ bool drawGlyph(const std::string& family, u32 codepoint, int style, esengine::f32 pixelSize, int supersample,+ int padding, GlyphCoverage& out) override {+ if (!glyphPainter || !vm) return false;+ const eshost::FontMatch face = matchFont(family, codepoint, style);+ if (face.path.empty()) return false;+ JNIEnv* env = nullptr;+ if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {+ if (vm->AttachCurrentThread(&env, nullptr) != JNI_OK || !env) return false;+ }+ jstring path = env->NewStringUTF(face.path.c_str());+ jfloatArray metrics = env->NewFloatArray(6);+ auto alpha = (jbyteArray)env->CallStaticObjectMethod(+ glyphPainter, glyphPainterDraw, path, (jint)face.faceIndex, (jint)codepoint, (jfloat)pixelSize,+ (jint)supersample, (jint)padding, (jboolean)face.syntheticBold, (jboolean)face.syntheticItalic, metrics);+ bool ok = false;+ if (env->ExceptionCheck()) {+ env->ExceptionDescribe();+ env->ExceptionClear();+ } else if (alpha) {+ float m[6] = {};+ env->GetFloatArrayRegion(metrics, 0, 6, m);+ out.width = (int)m[0];+ out.height = (int)m[1];+ out.advance = m[2];+ out.bearingX = m[3];+ out.bearingY = m[4];+ out.blank = m[5] != 0.0f;+ const jsize n = env->GetArrayLength(alpha);+ if (out.blank) {+ ok = true;+ } else if (out.width > 0 && out.height > 0 && n == (jsize)out.width * out.height) {+ out.alpha.resize((size_t)n);+ env->GetByteArrayRegion(alpha, 0, n, reinterpret_cast<jbyte*>(out.alpha.data()));+ ok = true;+ }+ env->DeleteLocalRef(alpha);+ }+ env->DeleteLocalRef(metrics);+ env->DeleteLocalRef(path);+ return ok;+ }+// A detached JNI thread runs the request; deliverFetch is thread-safe and the// JS callback runs back on the main thread in drainFetches.void startFetch(const eshost::FetchRequest& req) override {@@ -402,36 +454,45 @@ void jniTextSubmit(JNIEnv*, jclass) { eshost::deliverTextEditorSubmit(); }void jniTextCancel(JNIEnv*, jclass) { eshost::deliverTextEditorCancel(); }/**- * Load com.estella.host.TextEditor, register its natives and construct it.- *- * Runs on the frame-loop thread, before boot binds the es_textEditor_* entry- * points — the surface has to exist by then or the SDK correctly concludes there- * is none. FindClass would fail here (off the UI thread it resolves through the- * SYSTEM class loader, which knows nothing of the APK), so the app's own loader- * is asked by name instead. Nothing touches a View: the shim posts all of that- * to the UI thread itself.+ * One of the APK's own classes (the shims in native/android/java), as a local ref;+ * null when the APK does not carry it. FindClass would fail on this thread (off+ * the UI thread it resolves through the SYSTEM class loader, which knows nothing+ * of the APK), so the app's own loader is asked by name instead.*/-void attachTextEditor(ANativeActivity* activity) {- if (g_platform.editor || !activity->vm) return;- JNIEnv* env = nullptr;- if (activity->vm->AttachCurrentThread(&env, nullptr) != JNI_OK || !env) return;-+jclass loadAppClass(JNIEnv* env, ANativeActivity* activity, const char* name) {jclass activityCls = env->GetObjectClass(activity->clazz);const jmethodID getClassLoader =env->GetMethodID(activityCls, "getClassLoader", "()Ljava/lang/ClassLoader;");env->DeleteLocalRef(activityCls);- if (!getClassLoader) { env->ExceptionClear(); return; }+ if (!getClassLoader) { env->ExceptionClear(); return nullptr; }jobject loader = env->CallObjectMethod(activity->clazz, getClassLoader);jclass loaderCls = env->FindClass("java/lang/ClassLoader");const jmethodID loadClass =env->GetMethodID(loaderCls, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");env->DeleteLocalRef(loaderCls);- jstring name = env->NewStringUTF("com.estella.host.TextEditor");- jclass cls = (jclass)env->CallObjectMethod(loader, loadClass, name);- env->DeleteLocalRef(name);+ jstring jname = env->NewStringUTF(name);+ jclass cls = (jclass)env->CallObjectMethod(loader, loadClass, jname);+ env->DeleteLocalRef(jname);env->DeleteLocalRef(loader);if (!cls || env->ExceptionCheck()) {env->ExceptionClear();+ return nullptr;+ }+ return cls;+}++/**+ * Load com.estella.host.TextEditor, register its natives and construct it.+ *+ * Runs on the frame-loop thread, before boot binds the es_textEditor_* entry+ * points — the surface has to exist by then or the SDK correctly concludes there+ * is none. Nothing touches a View: the shim posts all of that to the UI thread+ * itself.+ */+void attachTextEditor(JNIEnv* env, ANativeActivity* activity) {+ if (g_platform.editor) return;+ jclass cls = loadAppClass(env, activity, "com.estella.host.TextEditor");+ if (!cls) {__android_log_print(ANDROID_LOG_WARN, LOG_TAG,"text editor: com.estella.host.TextEditor not in the APK — typing is off");return;@@ -463,6 +524,33 @@ void attachTextEditor(ANativeActivity* activity) {__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "text editor: soft keyboard up (IME)");}+/** Load com.estella.host.GlyphPainter (see AndroidPlatform::drawGlyph). Static methods only, so no instance. */+void attachGlyphPainter(JNIEnv* env, ANativeActivity* activity) {+ if (g_platform.glyphPainter) return;+ jclass cls = loadAppClass(env, activity, "com.estella.host.GlyphPainter");+ const jmethodID draw = cls ? env->GetStaticMethodID(cls, "draw", "(Ljava/lang/String;IIFIIZZ[F)[B") : nullptr;+ if (!draw) {+ env->ExceptionClear();+ if (cls) env->DeleteLocalRef(cls);+ __android_log_print(ANDROID_LOG_WARN, LOG_TAG,+ "glyphs: com.estella.host.GlyphPainter not in the APK — a face the shared parser cannot "+ "read draws nothing");+ return;+ }+ g_platform.glyphPainter = (jclass)env->NewGlobalRef(cls);+ g_platform.glyphPainterDraw = draw;+ env->DeleteLocalRef(cls);+}++/** The APK's Java shims, loaded once on the frame-loop thread before boot binds what they answer. */+void attachJavaShims(ANativeActivity* activity) {+ if (!activity->vm) return;+ JNIEnv* env = nullptr;+ if (activity->vm->AttachCurrentThread(&env, nullptr) != JNI_OK || !env) return;+ attachTextEditor(env, activity);+ attachGlyphPainter(env, activity);+}+// The decoder's own copies of the MotionEvent constants, held against the NDK's// so a test that cannot include <android/input.h> is still testing this ABI.static_assert(eshost::input::kMotionActionMask == AMOTION_EVENT_ACTION_MASK);@@ -943,7 +1031,7 @@ void android_main(android_app* app) {g_platform.vm = app->activity->vm; // for JNI HttpURLConnection (es_fetch)// Before boot: whether there is an editing surface decides whether the// es_textEditor_* entry points are bound at all.- attachTextEditor(app->activity);+ attachJavaShims(app->activity);// files/ holds what a player keeps; cache/ is the system's to reclaim.if (app->activity->internalDataPath) g_platform.data = app->activity->internalDataPath;g_platform.cache = queryCacheDir(app->activity);diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 39e8036ef..d85da4033 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -3834,4 +3834,6 @@ export const NOTED = {{"note":"Android frames no longer drop to 30 fps when they come close to the frame budget."},"fix(native): a platform only names the font face a glyph needs, and the rasterizer maps each face's file once, so a new glyph no longer reads a whole system font":{"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."},};