Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- 🟠 **`<ImageShader transform>`.** Skia's `ImageShader` takes a transform on top
of `fit`/`rect`; here the only way to move one is a `<Group>`, which moves the
shape too.
Expand Down
4 changes: 4 additions & 0 deletions apps/example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name=".AssetProvider"
android:authorities="${applicationId}.assets"
android:exported="false" />
</application>
</manifest>
Binary file added apps/example/android/app/src/main/assets/tile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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/<path>`, 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<String>?,
selection: String?,
selectionArgs: Array<String>?,
sortOrder: String?
): Cursor? = null

override fun insert(uri: Uri, values: ContentValues?): Uri? = null

override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int = 0

override fun update(
uri: Uri,
values: ContentValues?,
selection: String?,
selectionArgs: Array<String>?
): Int = 0
}
32 changes: 32 additions & 0 deletions apps/example/src/image.harness.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// <TgfxImage>, <Atlas>, <Vertices>, <ImageShader>, the decoders, and the native handle ops.
import React from 'react';
import { Platform } from 'react-native';
import {
afterAll,
beforeAll,
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions docs/content/docs/images.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <jni.h>
Expand All @@ -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() {
Expand All @@ -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<tgfx::Data> 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<jbyteArray>(env->CallStaticObjectMethod(gNativeClass, method, jarg));
if (env->ExceptionCheck()) {
env->ExceptionClear();
bytes = nullptr;
}
std::shared_ptr<tgfx::Data> 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<size_t>(length));
env->ReleaseByteArrayElements(bytes, raw, JNI_ABORT);
}
}
env->DeleteLocalRef(bytes);
}
env->DeleteLocalRef(jarg);
return data;
}

} // namespace

void setPlatformAssetVm(JavaVM* vm) {
Expand All @@ -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<tgfx::Data> 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<jbyteArray>(env->CallStaticObjectMethod(cls, method, jname));
if (env->ExceptionCheck()) {
env->ExceptionClear();
bytes = nullptr;
}
std::shared_ptr<tgfx::Data> 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<size_t>(length));
env->ReleaseByteArrayElements(bytes, raw, JNI_ABORT);
}
}
env->DeleteLocalRef(bytes);
}
env->DeleteLocalRef(jname);
return data;
return callForBytes(gResourceBytes, name);
}

std::shared_ptr<tgfx::Data> platformUriData(const std::string& uri) {
return callForBytes(gUriBytes, uri);
}

} // namespace tgfxrn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ std::shared_ptr<ImagePromise> 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)) {
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native-tgfx/cpp/Image/PlatformAsset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ std::shared_ptr<tgfx::Data> platformAssetData(const std::string&) {
return nullptr;
}

// `asset:` and `content:` are Android's; no other platform hands them out.
std::shared_ptr<tgfx::Data> platformUriData(const std::string&) {
return nullptr;
}

} // namespace tgfxrn

#endif
11 changes: 10 additions & 1 deletion packages/react-native-tgfx/cpp/Image/PlatformAsset.hpp
Original file line number Diff line number Diff line change
@@ -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 <memory>
Expand All @@ -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<tgfx::Data> 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<tgfx::Data> platformUriData(const std::string& uri);

} // namespace tgfxrn
7 changes: 7 additions & 0 deletions packages/react-native-tgfx/cpp/TextLayout/HybridTgfxFonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ std::shared_ptr<NamePromise> 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);
});
}
Expand Down
15 changes: 15 additions & 0 deletions packages/react-native-tgfx/src/__tests__/useFonts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
Expand Down
15 changes: 15 additions & 0 deletions packages/react-native-tgfx/src/__tests__/useImage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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==')
Expand Down
6 changes: 5 additions & 1 deletion packages/react-native-tgfx/src/hooks/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ export async function loadSource<T>(
) {
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) {
Expand Down