From 121487d56ae09e0b7f50df88865874746cd6f55e Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Wed, 4 Nov 2026 20:18:32 +0100 Subject: [PATCH 1/4] feat(android): read asset: and content: URIs natively An asset: URI points into the APK and a content: URI at a provider's stream, so neither has a path tgfx can open. TgfxNative.uriBytes reads the first through the AssetManager and the second through the ContentResolver, and the image and font factories call it for both schemes before they try a file path. asset:/a.png, asset:///a.png and asset://a.png all name the same asset. Other platforms hand out no such URIs and return null, which the factories report as "could not read". --- .../src/main/cpp/PlatformAssetAndroid.cpp | 73 +++++++++++-------- .../java/com/margelo/nitro/tgfx/TgfxNative.kt | 22 ++++++ .../cpp/Image/HybridTgfxImageFactory.cpp | 7 ++ .../cpp/Image/PlatformAsset.cpp | 5 ++ .../cpp/Image/PlatformAsset.hpp | 11 ++- .../cpp/TextLayout/HybridTgfxFonts.cpp | 7 ++ 6 files changed, 94 insertions(+), 31 deletions(-) diff --git a/packages/react-native-tgfx/android/src/main/cpp/PlatformAssetAndroid.cpp b/packages/react-native-tgfx/android/src/main/cpp/PlatformAssetAndroid.cpp index 64ed46bc..6755cf52 100644 --- a/packages/react-native-tgfx/android/src/main/cpp/PlatformAssetAndroid.cpp +++ b/packages/react-native-tgfx/android/src/main/cpp/PlatformAssetAndroid.cpp @@ -1,4 +1,4 @@ -// Reads a packaged resource by name through TgfxNative.resourceBytes(). +// Reads a packaged resource by name, or an `asset:`/`content:` URI, through TgfxNative. #include "PlatformAsset.hpp" #include @@ -15,6 +15,7 @@ JavaVM* gAssetVm = nullptr; // which cannot see an app class. jclass gNativeClass = nullptr; jmethodID gResourceBytes = nullptr; +jmethodID gUriBytes = nullptr; // Daemon, so a thread this library does not own is never required to detach at exit. JNIEnv* assetEnv() { @@ -28,6 +29,37 @@ JNIEnv* assetEnv() { return gAssetVm->AttachCurrentThreadAsDaemon(&env, nullptr) == JNI_OK ? env : nullptr; } +/// Calls a static `(String) -> byte[]` on TgfxNative and copies what it returns. +std::shared_ptr callForBytes(jmethodID method, const std::string& arg) { + if (gNativeClass == nullptr || method == nullptr) { + return nullptr; + } + JNIEnv* env = assetEnv(); + if (env == nullptr) { + return nullptr; + } + jstring jarg = env->NewStringUTF(arg.c_str()); + auto bytes = static_cast(env->CallStaticObjectMethod(gNativeClass, method, jarg)); + if (env->ExceptionCheck()) { + env->ExceptionClear(); + bytes = nullptr; + } + std::shared_ptr data; + if (bytes != nullptr) { + jsize length = env->GetArrayLength(bytes); + if (length > 0) { + jbyte* raw = env->GetByteArrayElements(bytes, nullptr); + if (raw != nullptr) { + data = tgfx::Data::MakeWithCopy(raw, static_cast(length)); + env->ReleaseByteArrayElements(bytes, raw, JNI_ABORT); + } + } + env->DeleteLocalRef(bytes); + } + env->DeleteLocalRef(jarg); + return data; +} + } // namespace void setPlatformAssetVm(JavaVM* vm) { @@ -50,38 +82,19 @@ void setPlatformAssetVm(JavaVM* vm) { env->ExceptionClear(); TGFXRN_LOGW("TgfxNative.resourceBytes() not found; a bundled asset will not load"); } + gUriBytes = env->GetStaticMethodID(gNativeClass, "uriBytes", "(Ljava/lang/String;)[B"); + if (gUriBytes == nullptr) { + env->ExceptionClear(); + TGFXRN_LOGW("TgfxNative.uriBytes() not found; an asset: or content: URI will not load"); + } } std::shared_ptr platformAssetData(const std::string& name) { - if (gNativeClass == nullptr || gResourceBytes == nullptr) { - return nullptr; - } - JNIEnv* env = assetEnv(); - if (env == nullptr) { - return nullptr; - } - jclass cls = gNativeClass; - jmethodID method = gResourceBytes; - jstring jname = env->NewStringUTF(name.c_str()); - auto bytes = static_cast(env->CallStaticObjectMethod(cls, method, jname)); - if (env->ExceptionCheck()) { - env->ExceptionClear(); - bytes = nullptr; - } - std::shared_ptr data; - if (bytes != nullptr) { - jsize length = env->GetArrayLength(bytes); - if (length > 0) { - jbyte* raw = env->GetByteArrayElements(bytes, nullptr); - if (raw != nullptr) { - data = tgfx::Data::MakeWithCopy(raw, static_cast(length)); - env->ReleaseByteArrayElements(bytes, raw, JNI_ABORT); - } - } - env->DeleteLocalRef(bytes); - } - env->DeleteLocalRef(jname); - return data; + return callForBytes(gResourceBytes, name); +} + +std::shared_ptr platformUriData(const std::string& uri) { + return callForBytes(gUriBytes, uri); } } // namespace tgfxrn diff --git a/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/TgfxNative.kt b/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/TgfxNative.kt index 6d9ab7bb..e110b76e 100644 --- a/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/TgfxNative.kt +++ b/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/TgfxNative.kt @@ -2,6 +2,7 @@ package com.margelo.nitro.tgfx import android.graphics.SurfaceTexture import android.hardware.HardwareBuffer +import android.net.Uri import android.os.Build import android.util.Log import android.view.Surface @@ -74,6 +75,27 @@ internal object TgfxNative { return null } + /** Bytes behind an `asset:` or `content:` URI, which no file API can open; null if unreadable. */ + @Keep + @DoNotStrip + @JvmStatic + fun uriBytes(uri: String): ByteArray? { + val context = NitroModules.applicationContext ?: return null + val parsed = Uri.parse(uri) + return runCatching { + val stream = + if (parsed.scheme == "asset") { + // asset:/a.png, asset:///a.png and asset://a.png all name a.png. + context.assets.open((parsed.authority.orEmpty() + parsed.path.orEmpty()).trimStart('/')) + } else { + context.contentResolver.openInputStream(parsed) + } + stream?.use { it.readBytes() } + } + .onFailure { Log.w("tgfx", "could not read $uri: $it") } + .getOrNull() + } + /** Called once per [snapshot] on a native worker, never the render thread. [data] is call-scoped. */ @Keep @DoNotStrip diff --git a/packages/react-native-tgfx/cpp/Image/HybridTgfxImageFactory.cpp b/packages/react-native-tgfx/cpp/Image/HybridTgfxImageFactory.cpp index 7bd2b28a..2614aa2d 100644 --- a/packages/react-native-tgfx/cpp/Image/HybridTgfxImageFactory.cpp +++ b/packages/react-native-tgfx/cpp/Image/HybridTgfxImageFactory.cpp @@ -117,6 +117,13 @@ std::shared_ptr HybridTgfxImageFactory::fromFile(const std::string return registerData(std::move(data), file); } } + if (tgfxrn::looksLikePlatformUri(file)) { + auto data = tgfxrn::platformUriData(file); + if (data == nullptr) { + throw std::runtime_error("tgfx: could not read " + file); + } + return registerData(std::move(data), file); + } // Codec first: it keeps the bytes on disk and re-reads them after an eviction, which // reading the file up front would give up. Only a non-raster falls through to the bytes. if (auto codec = ::tgfx::ImageCodec::MakeFrom(file)) { diff --git a/packages/react-native-tgfx/cpp/Image/PlatformAsset.cpp b/packages/react-native-tgfx/cpp/Image/PlatformAsset.cpp index 9775449b..b5cbd245 100644 --- a/packages/react-native-tgfx/cpp/Image/PlatformAsset.cpp +++ b/packages/react-native-tgfx/cpp/Image/PlatformAsset.cpp @@ -10,6 +10,11 @@ std::shared_ptr platformAssetData(const std::string&) { return nullptr; } +// `asset:` and `content:` are Android's; no other platform hands them out. +std::shared_ptr platformUriData(const std::string&) { + return nullptr; +} + } // namespace tgfxrn #endif diff --git a/packages/react-native-tgfx/cpp/Image/PlatformAsset.hpp b/packages/react-native-tgfx/cpp/Image/PlatformAsset.hpp index be60f5fe..a62adb89 100644 --- a/packages/react-native-tgfx/cpp/Image/PlatformAsset.hpp +++ b/packages/react-native-tgfx/cpp/Image/PlatformAsset.hpp @@ -1,7 +1,8 @@ // Bytes for a `require()`d asset that the platform packages under a name rather // than a path. Only a release Android build does this — it compiles the asset // into a drawable/raw resource, so `resolveAssetSource` hands JS a bare -// `src_assets_sample` that no file API can open. +// `src_assets_sample` that no file API can open. Android's `asset:` and +// `content:` URIs name no file either, so their bytes come from here too. #pragma once #include @@ -21,4 +22,12 @@ inline bool looksLikeAssetName(const std::string& path) { /// The asset's bytes, or null when the name names nothing on this platform. std::shared_ptr platformAssetData(const std::string& name); +/// True for a URI only the platform can read: an APK asset or a content provider's stream. +inline bool looksLikePlatformUri(const std::string& path) { + return path.rfind("asset:", 0) == 0 || path.rfind("content:", 0) == 0; +} + +/// The bytes behind such a URI, or null when this platform cannot read it. +std::shared_ptr platformUriData(const std::string& uri); + } // namespace tgfxrn diff --git a/packages/react-native-tgfx/cpp/TextLayout/HybridTgfxFonts.cpp b/packages/react-native-tgfx/cpp/TextLayout/HybridTgfxFonts.cpp index c0be05b0..ec5126f2 100644 --- a/packages/react-native-tgfx/cpp/TextLayout/HybridTgfxFonts.cpp +++ b/packages/react-native-tgfx/cpp/TextLayout/HybridTgfxFonts.cpp @@ -75,6 +75,13 @@ std::shared_ptr HybridTgfxFonts::registerFromFile(const std::string return registerTypeface(::tgfx::Typeface::MakeFromData(std::move(data), 0), family, file); } } + if (tgfxrn::looksLikePlatformUri(file)) { + auto data = tgfxrn::platformUriData(file); + if (data == nullptr) { + throw std::runtime_error("tgfx: could not read " + file); + } + return registerTypeface(::tgfx::Typeface::MakeFromData(std::move(data), 0), family, file); + } return registerTypeface(::tgfx::Typeface::MakeFromPath(file), family, file); }); } From 2f3b5a7586f1349c0fbdf69b51f1dc78f1fdcd60 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Wed, 4 Nov 2026 20:58:17 +0100 Subject: [PATCH 2/4] fix(android): send asset: and content: sources to fromFile loadSource sent both schemes to fetch. React Native reads a non-http URI there through ContentResolver.openInputStream, which has no asset: scheme, so an asset: image or font failed to load with "Network request failed". A content: source loaded, but FileReader passed its bytes to JS as a base64 data URL for JS to decode, and the ArrayBuffer then went back to native. fromFile reads both schemes in native since the previous commit. --- .../src/__tests__/useFonts.test.tsx | 15 +++++++++++++++ .../src/__tests__/useImage.test.tsx | 15 +++++++++++++++ packages/react-native-tgfx/src/hooks/source.ts | 6 +++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/react-native-tgfx/src/__tests__/useFonts.test.tsx b/packages/react-native-tgfx/src/__tests__/useFonts.test.tsx index ef573452..e4a53efd 100644 --- a/packages/react-native-tgfx/src/__tests__/useFonts.test.tsx +++ b/packages/react-native-tgfx/src/__tests__/useFonts.test.tsx @@ -75,6 +75,21 @@ test('a Metro-served font arrives as raw bytes', async () => { expect(registered[0]!.arg).toBe('ttf') }) +test('an asset: or content: font is opened natively, never fetched', async () => { + let fetched = false + globalThis.fetch = (async () => { + fetched = true + throw new Error('should not be reached') + }) as unknown as typeof fetch + await loadFont('asset:/fonts/PT.ttf', 'Demo Serif') + await loadFont('content://com.example.fonts/PT.ttf', 'Demo Serif') + expect(fetched).toBe(false) + expect(registered.map((r) => [r.via, r.arg])).toEqual([ + ['fromFile', 'asset:/fonts/PT.ttf'], + ['fromFile', 'content://com.example.fonts/PT.ttf'], + ]) +}) + test('an empty family lets the font name itself', async () => { expect(await loadFont('/fonts/PT.ttf')).toBe('Inferred') expect(registered[0]!.family).toBe('') diff --git a/packages/react-native-tgfx/src/__tests__/useImage.test.tsx b/packages/react-native-tgfx/src/__tests__/useImage.test.tsx index 556faba3..c2b002ab 100644 --- a/packages/react-native-tgfx/src/__tests__/useImage.test.tsx +++ b/packages/react-native-tgfx/src/__tests__/useImage.test.tsx @@ -123,6 +123,21 @@ test('an absolute path, as save() writes, is opened as a file', async () => { expect(made[0]!.arg).toBe('/data/user/0/app/cache/pic.png') }) +test('an asset: or content: URI is opened natively, never fetched', async () => { + let fetched = false + globalThis.fetch = (async () => { + fetched = true + return reply('hi') + }) as unknown as typeof fetch + await mount('asset:/pics/pic.png') + await mount('content://media/external/images/media/7') + expect(fetched).toBe(false) + expect(made.map((m) => [m.via, m.arg])).toEqual([ + ['fromFile', 'asset:/pics/pic.png'], + ['fromFile', 'content://media/external/images/media/7'], + ]) +}) + // A JPEG payload starts "/9j/", so a leading slash alone cannot mean "path". test('a raw base64 source goes to the base64 path even when it starts with /', async () => { await mount('/9j/4AAQSkZJRg==') diff --git a/packages/react-native-tgfx/src/hooks/source.ts b/packages/react-native-tgfx/src/hooks/source.ts index b46c2945..fa937c95 100644 --- a/packages/react-native-tgfx/src/hooks/source.ts +++ b/packages/react-native-tgfx/src/hooks/source.ts @@ -91,7 +91,11 @@ export async function loadSource( ) { return api.fromFile(uri) } - if (/^(https?|content|asset):/.test(uri)) { + // Android's `asset:` and `content:` URIs name no file, so native reads them through the platform. + if (/^(asset|content):/.test(uri)) { + return api.fromFile(uri) + } + if (/^https?:/.test(uri)) { // Without the signal a hung source holds its socket long after the caller gave up. const res = await fetch(uri, { signal }) if (!res.ok) { From 672904cda7d7466c3d4459dc3c7e4b520054d3b8 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Wed, 4 Nov 2026 21:38:13 +0100 Subject: [PATCH 3/4] test(harness): load an asset: and a content: image on Android The example app ships tile.png as an APK asset and serves its assets through a small ContentProvider, so one 24x24 file backs both URI forms. image.harness.tsx decodes each, and checks that an asset: URI naming nothing rejects with the URI in the message. With the routing on main, both asset: tests fail with "Network request failed". --- .../android/app/src/main/AndroidManifest.xml | 4 ++ .../android/app/src/main/assets/tile.png | Bin 0 -> 191 bytes .../java/com/tgfxexample/AssetProvider.kt | 37 ++++++++++++++++++ apps/example/src/image.harness.tsx | 32 +++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 apps/example/android/app/src/main/assets/tile.png create mode 100644 apps/example/android/app/src/main/java/com/tgfxexample/AssetProvider.kt diff --git a/apps/example/android/app/src/main/AndroidManifest.xml b/apps/example/android/app/src/main/AndroidManifest.xml index a971b7ce..7b530809 100644 --- a/apps/example/android/app/src/main/AndroidManifest.xml +++ b/apps/example/android/app/src/main/AndroidManifest.xml @@ -24,5 +24,9 @@ + diff --git a/apps/example/android/app/src/main/assets/tile.png b/apps/example/android/app/src/main/assets/tile.png new file mode 100644 index 0000000000000000000000000000000000000000..fcfac37342bf6fc509f2925df5cf690eff922a77 GIT binary patch literal 191 zcmeAS@N?(olHy`uVBq!ia0vp^5+KaM1|%Pp+x`GjZJsWUAr-fhA7uUfFE4rgsEzZ% zDW{AtTq>9#!K};M9_^rg_@Ee&%fp=^_>kvF#=rVrZgsUK^BaW@CrZq(WAoy5-`?_~ z#-Z!si-WEqb_=dBHT`T5;_f)b{v^tw#7^Nj)1*&~OT`4vuuq9Fu=vENxw7-ouZEbz m?ys3&U1gtg0%Vl{8-uv)&tp5HcQpcC#Ng@b=d#Wzp$Pyd<4KtS literal 0 HcmV?d00001 diff --git a/apps/example/android/app/src/main/java/com/tgfxexample/AssetProvider.kt b/apps/example/android/app/src/main/java/com/tgfxexample/AssetProvider.kt new file mode 100644 index 00000000..ff55dec3 --- /dev/null +++ b/apps/example/android/app/src/main/java/com/tgfxexample/AssetProvider.kt @@ -0,0 +1,37 @@ +package com.tgfxexample + +import android.content.ContentProvider +import android.content.ContentValues +import android.content.res.AssetFileDescriptor +import android.database.Cursor +import android.net.Uri + +/** Serves this APK's assets as `content://com.tgfxexample.assets/`, for the content: URI tests. */ +class AssetProvider : ContentProvider() { + + override fun onCreate(): Boolean = true + + override fun openAssetFile(uri: Uri, mode: String): AssetFileDescriptor? = + context?.assets?.openFd(uri.path.orEmpty().trimStart('/')) + + override fun getType(uri: Uri): String? = null + + override fun query( + uri: Uri, + projection: Array?, + selection: String?, + selectionArgs: Array?, + sortOrder: String? + ): Cursor? = null + + override fun insert(uri: Uri, values: ContentValues?): Uri? = null + + override fun delete(uri: Uri, selection: String?, selectionArgs: Array?): Int = 0 + + override fun update( + uri: Uri, + values: ContentValues?, + selection: String?, + selectionArgs: Array? + ): Int = 0 +} diff --git a/apps/example/src/image.harness.tsx b/apps/example/src/image.harness.tsx index e01a7890..cf78fbf6 100644 --- a/apps/example/src/image.harness.tsx +++ b/apps/example/src/image.harness.tsx @@ -1,5 +1,6 @@ // , , , , the decoders, and the native handle ops. import React from 'react'; +import { Platform } from 'react-native'; import { afterAll, beforeAll, @@ -179,6 +180,37 @@ describe('tgfx image decoding', () => { } }); +// The example's AssetProvider serves its APK assets, so tile.png backs both URIs. +(Platform.OS === 'android' ? describe : describe.skip)( + 'tgfx image sources on Android', + () => { + test('decodes an asset: URI', async () => { + const image = await loadImage('asset:/tile.png'); + expect(image.width).toBe(24); + expect(image.height).toBe(24); + }); + + test('decodes a content: URI', async () => { + const image = await loadImage( + 'content://com.tgfxexample.assets/tile.png', + ); + expect(image.width).toBe(24); + expect(image.height).toBe(24); + }); + + test('rejects an asset: URI that names nothing', async () => { + let message = ''; + try { + await loadImage('asset:/missing.png'); + throw new Error('loadImage resolved, expected it to reject'); + } catch (e: unknown) { + message = e instanceof Error ? e.message : String(e); + } + expect(message).toContain('could not read asset:/missing.png'); + }); + }, +); + describe('tgfx image rendering', () => { test('draws every fit mode', async () => { await drawnSnapshot( From 98490de0ad30165d4931db658b25ceddb040cc74 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Wed, 4 Nov 2026 21:45:11 +0100 Subject: [PATCH 4/4] docs: mark native asset loading on Android done The images page says where an Android asset: and content: URI load from, and the roadmap item moves to done. --- ROADMAP.md | 9 +++++---- docs/content/docs/images.mdx | 3 +++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 09738a7f..cf12b133 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -157,10 +157,11 @@ Reducing per-op cost in tgfx's GL backend is where the remaining gap to the same matrix, in frame-pixel coordinates, with `rotation`/`mirror` folded into it. `drawCameraFrame` is gone from the spec, the renderer and both platforms. Sampling the frame *inside a runtime shader* still needs #130. -- 🟠 **Native asset loading on Android.** A `require()`d asset in a release build - resolves to a bare drawable-resource name, and `asset://` / `content://` URIs - need the JNI `AssetManager`/`ContentResolver`. Both still go through JS - `fetch`. iOS release assets and any `file:` URI already load natively. +- ✅ **Native asset loading on Android.** A release build's `require()`d asset + loads by its drawable-resource name, and since #25 `asset:` and `content:` + URIs load through `AssetManager` and `ContentResolver` over JNI, so none of + them goes through JS `fetch`. iOS release assets and any `file:` URI load + natively too. - 🟠 **``.** Skia's `ImageShader` takes a transform on top of `fit`/`rect`; here the only way to move one is a ``, which moves the shape too. diff --git a/docs/content/docs/images.mdx b/docs/content/docs/images.mdx index 07886cfc..45b788dd 100644 --- a/docs/content/docs/images.mdx +++ b/docs/content/docs/images.mdx @@ -59,6 +59,9 @@ scene.add(image({ image: photo, x: 0, y: 0, width: 300, height: 200, fit: 'cover In Android release builds, asset names can't differ only by extension (`logo.png`, `logo.webp`). +On Android, an `asset:/` URI loads from the APK's `assets` folder and a +`content://` URI from its content provider, such as a photo picker's result. + ## SVG documents A whole SVG document draws with [`ImageSVG`](/api/variables/ImageSVG):