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/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 00000000..fcfac373 Binary files /dev/null and b/apps/example/android/app/src/main/assets/tile.png differ 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( 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): 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); }); } 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) {