From f032b38e3ebd9fa0c621f8d4063326f46fd8fbcf Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Tue, 3 Nov 2026 18:22:19 +0100 Subject: [PATCH 1/5] perf(native): copy slot values from a bound block on push setValues takes a number[], so every animated frame builds an array that Nitro converts into a std::vector before the renderer swaps it in. A view can now bind the Float64 block behind a TgfxSlots values buffer once (bindValues) and copy it natively on each pushValues(), so only the call crosses JSI per frame. The renderer copies under the same lock as setValues, and clearScene drops the binding. Each platform view keeps the bound ArrayBuffer until the next bind, and the slots object now shares its value storage with the buffers it hands out, so a bound canvas can outlive it. --- .../android/src/main/cpp/TgfxAndroid.cpp | 21 ++++++++++++++++++ .../com/margelo/nitro/tgfx/HybridTgfxView.kt | 8 +++++++ .../java/com/margelo/nitro/tgfx/TgfxNative.kt | 6 +++++ .../margelo/nitro/tgfx/TgfxRenderTarget.kt | 20 +++++++++++++++++ .../cpp/Core/HybridTgfxSlotsFactory.hpp | 13 +++++++---- .../cpp/Render/TgfxRenderer.cpp | 22 +++++++++++++++++++ .../cpp/Render/TgfxRenderer.hpp | 9 ++++++++ .../ios/HybridTgfxView.swift | 12 ++++++++++ .../react-native-tgfx/ios/TgfxMetalView.h | 3 +++ .../react-native-tgfx/ios/TgfxMetalView.mm | 12 ++++++++++ .../android/c++/JHybridTgfxViewSpec.cpp | 8 +++++++ .../android/c++/JHybridTgfxViewSpec.hpp | 2 ++ .../margelo/nitro/tgfx/HybridTgfxViewSpec.kt | 8 +++++++ .../ios/c++/HybridTgfxViewSpecSwift.hpp | 12 ++++++++++ .../ios/swift/HybridTgfxViewSpec.swift | 2 ++ .../ios/swift/HybridTgfxViewSpec_cxx.swift | 22 +++++++++++++++++++ .../shared/c++/HybridTgfxViewSpec.cpp | 2 ++ .../shared/c++/HybridTgfxViewSpec.hpp | 2 ++ .../src/specs/TgfxView.nitro.ts | 14 ++++++++++++ 19 files changed, 194 insertions(+), 4 deletions(-) diff --git a/packages/react-native-tgfx/android/src/main/cpp/TgfxAndroid.cpp b/packages/react-native-tgfx/android/src/main/cpp/TgfxAndroid.cpp index 3effef5d..53469e15 100644 --- a/packages/react-native-tgfx/android/src/main/cpp/TgfxAndroid.cpp +++ b/packages/react-native-tgfx/android/src/main/cpp/TgfxAndroid.cpp @@ -311,6 +311,27 @@ Java_com_margelo_nitro_tgfx_TgfxNative_setValues(JNIEnv* env, jobject, jlong ptr view->renderer.setValues(std::move(values)); } +JNIEXPORT void JNICALL +Java_com_margelo_nitro_tgfx_TgfxNative_bindValues(JNIEnv* env, jobject, jlong ptr, + jobject valuesBuffer) { + auto* view = asView(ptr); + if (view == nullptr) { + return; + } + // Read on every push, not copied here: TgfxRenderTarget holds the ArrayBuffer behind it. + auto* values = valuesBuffer != nullptr + ? static_cast(env->GetDirectBufferAddress(valuesBuffer)) + : nullptr; + auto bytes = values != nullptr ? env->GetDirectBufferCapacity(valuesBuffer) : 0; + view->renderer.bindValues(values, bytes > 0 ? static_cast(bytes) / sizeof(double) : 0); +} + +JNIEXPORT jboolean JNICALL +Java_com_margelo_nitro_tgfx_TgfxNative_pushValues(JNIEnv*, jobject, jlong ptr) { + auto* view = asView(ptr); + return view != nullptr && view->renderer.pushValues() ? JNI_TRUE : JNI_FALSE; +} + JNIEXPORT void JNICALL Java_com_margelo_nitro_tgfx_TgfxNative_setStringValues(JNIEnv* env, jobject, jlong ptr, jdoubleArray indexArr, diff --git a/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/HybridTgfxView.kt b/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/HybridTgfxView.kt index 6818de4c..98b6cd58 100644 --- a/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/HybridTgfxView.kt +++ b/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/HybridTgfxView.kt @@ -119,6 +119,14 @@ class HybridTgfxView(private val context: ThemedReactContext) : target.setValues(values) } + override fun bindValues(values: ArrayBuffer) { + target.bindValues(values) + } + + override fun pushValues() { + target.pushValues() + } + override fun setStringValues(indices: DoubleArray, values: Array) { target.setStringValues(indices, values) } 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 3daaab85..6d9ab7bb 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 @@ -36,6 +36,12 @@ internal object TgfxNative { external fun clearScene(ptr: Long) external fun setOpaque(ptr: Long, opaque: Boolean) external fun setValues(ptr: Long, values: DoubleArray) + + /** [values] is a direct buffer the renderer reads on every [pushValues]; keep it alive until the next bind. */ + external fun bindValues(ptr: Long, values: ByteBuffer) + + /** False when nothing is bound, so there is nothing to draw. */ + external fun pushValues(ptr: Long): Boolean external fun setStringValues(ptr: Long, indices: DoubleArray, values: Array) /** True when the viewport moved; an unchanged write must not wake the render thread. */ diff --git a/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/TgfxRenderTarget.kt b/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/TgfxRenderTarget.kt index ee182ff6..ed56a136 100644 --- a/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/TgfxRenderTarget.kt +++ b/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/TgfxRenderTarget.kt @@ -7,6 +7,7 @@ import android.os.Process import android.view.Surface import android.view.View import android.view.ViewTreeObserver +import com.margelo.nitro.core.ArrayBuffer import java.nio.ByteBuffer import java.util.concurrent.CountDownLatch @@ -126,6 +127,25 @@ internal class TgfxRenderTarget { requestRender() } + // Guarded by [lock]: the renderer reads these bytes on every push, so the old block goes only after the new bind. + private var boundValues: ArrayBuffer? = null + + fun bindValues(values: ArrayBuffer) { + val bytes = values.getBuffer(false) + withPtr { + TgfxNative.bindValues(it, bytes) + boundValues = values + } + } + + fun pushValues() { + var pushed = false + withPtr { pushed = TgfxNative.pushValues(it) } + if (pushed) { + requestRender() + } + } + fun setStringValues(indices: DoubleArray, values: Array) { withPtr { TgfxNative.setStringValues(it, indices, values) } requestRender() diff --git a/packages/react-native-tgfx/cpp/Core/HybridTgfxSlotsFactory.hpp b/packages/react-native-tgfx/cpp/Core/HybridTgfxSlotsFactory.hpp index 07565d6c..1a422f52 100644 --- a/packages/react-native-tgfx/cpp/Core/HybridTgfxSlotsFactory.hpp +++ b/packages/react-native-tgfx/cpp/Core/HybridTgfxSlotsFactory.hpp @@ -17,7 +17,10 @@ namespace margelo::nitro::tgfx { class HybridTgfxSlots : public HybridTgfxSlotsSpec { public: explicit HybridTgfxSlots(size_t size, size_t valueCount, size_t stringCount) - : HybridObject(TAG), bytes_(size, 0), values_(valueCount, 0.0), strings_(stringCount) {} + : HybridObject(TAG), + bytes_(size, 0), + values_(std::make_shared>(valueCount, 0.0)), + strings_(stringCount) {} std::shared_ptr getFlags() override { // Borrowed, not owned: this object outlives every view, so a view must not free them. @@ -25,8 +28,10 @@ class HybridTgfxSlots : public HybridTgfxSlotsSpec { } std::shared_ptr getValues() override { - return ArrayBuffer::wrap(reinterpret_cast(values_.data()), values_.size() * sizeof(double), - []() {}); + // Shared, not borrowed: a canvas binds these for its per-frame push and can outlive this object. + auto values = values_; + return ArrayBuffer::wrap(reinterpret_cast(values->data()), values->size() * sizeof(double), + [values]() {}); } std::string getString(double at) override { @@ -45,7 +50,7 @@ class HybridTgfxSlots : public HybridTgfxSlotsSpec { private: std::vector bytes_; - std::vector values_; + std::shared_ptr> values_; std::vector strings_; // Doubles need no lock (an aligned 8-byte store is indivisible); strings do. std::mutex stringsMutex_; diff --git a/packages/react-native-tgfx/cpp/Render/TgfxRenderer.cpp b/packages/react-native-tgfx/cpp/Render/TgfxRenderer.cpp index 1b470a78..c8ee4386 100644 --- a/packages/react-native-tgfx/cpp/Render/TgfxRenderer.cpp +++ b/packages/react-native-tgfx/cpp/Render/TgfxRenderer.cpp @@ -160,6 +160,8 @@ void TgfxRenderer::clearScene() { viewportZoom_ = 1.0f; viewportX_ = 0.0f; viewportY_ = 0.0f; + boundValues_ = nullptr; + boundCount_ = 0; } version_.fetch_add(1, std::memory_order_release); } @@ -178,6 +180,26 @@ void TgfxRenderer::setValues(std::vector values) { version_.fetch_add(1, std::memory_order_release); } +void TgfxRenderer::bindValues(const double* values, size_t count) { + std::lock_guard lock(mutex_); + boundValues_ = values; + boundCount_ = values != nullptr ? count : 0; +} + +bool TgfxRenderer::pushValues() { + { + std::lock_guard lock(mutex_); + if (boundValues_ == nullptr) { + return false; + } + // assign reuses values_'s storage, so a steady push allocates nothing. + values_.assign(boundValues_, boundValues_ + boundCount_); + valuesGen_++; + } + version_.fetch_add(1, std::memory_order_release); + return true; +} + void TgfxRenderer::setStringValues(const std::vector& indices, const std::vector& values) { { diff --git a/packages/react-native-tgfx/cpp/Render/TgfxRenderer.hpp b/packages/react-native-tgfx/cpp/Render/TgfxRenderer.hpp index 277d0bdd..29f35fc6 100644 --- a/packages/react-native-tgfx/cpp/Render/TgfxRenderer.hpp +++ b/packages/react-native-tgfx/cpp/Render/TgfxRenderer.hpp @@ -63,6 +63,12 @@ class TgfxRenderer { void setValues(std::vector values); + /// Point pushValues at `count` doubles the caller keeps alive until the next bind or clearScene. + void bindValues(const double* values, size_t count); + + /// setValues from the bound block, copied now; false when nothing is bound. + bool pushValues(); + /// Sparse writes into the string-slot array (live text and path data). void setStringValues(const std::vector& indices, const std::vector& values); @@ -151,6 +157,9 @@ class TgfxRenderer { /// The FontRegistry epoch this command list was compiled against; a later face forces a recompile. uint64_t compiledFontEpoch_ = 0; std::vector values_; + /// The block pushValues copies into values_, under mutex_; the platform view keeps it alive. + const double* boundValues_ = nullptr; + size_t boundCount_ = 0; std::vector drawValues_; std::vector stringValues_; std::vector drawStringValues_; diff --git a/packages/react-native-tgfx/ios/HybridTgfxView.swift b/packages/react-native-tgfx/ios/HybridTgfxView.swift index 39803235..91056281 100644 --- a/packages/react-native-tgfx/ios/HybridTgfxView.swift +++ b/packages/react-native-tgfx/ios/HybridTgfxView.swift @@ -4,6 +4,8 @@ import UIKit /** iOS `TgfxView`: a thin Swift shell over the Objective-C++ `TgfxMetalView`. */ final class HybridTgfxView: HybridTgfxViewSpec, RecyclableView { private let metalView = TgfxMetalView() + // The renderer reads these bytes on every push, so they are released only by the next bind. + private var boundValues: ArrayBuffer? var view: UIView { metalView } @@ -46,6 +48,16 @@ final class HybridTgfxView: HybridTgfxViewSpec, RecyclableView { } } + func bindValues(values: ArrayBuffer) throws { + let slots = UnsafeRawPointer(values.data).assumingMemoryBound(to: Double.self) + metalView.bindValues(slots, count: Int32(values.size / MemoryLayout.size)) + boundValues = values + } + + func pushValues() throws { + metalView.pushValues() + } + func setStringValues(indices: [Double], values: [String]) throws { indices.withUnsafeBufferPointer { buffer in guard let base = buffer.baseAddress else { return } diff --git a/packages/react-native-tgfx/ios/TgfxMetalView.h b/packages/react-native-tgfx/ios/TgfxMetalView.h index f6074666..dd3f0e35 100644 --- a/packages/react-native-tgfx/ios/TgfxMetalView.h +++ b/packages/react-native-tgfx/ios/TgfxMetalView.h @@ -25,6 +25,9 @@ FOUNDATION_EXPORT void TgfxSnapshotRelease(void* _Nullable token); - (void)setOpaqueSurface:(BOOL)opaque; - (void)setValues:(const double*)values count:(int32_t)count; +/// The block `pushValues` copies from; the caller keeps it alive until the next bind. +- (void)bindValues:(const double* _Nullable)values count:(int32_t)count; +- (void)pushValues; - (void)setStringValues:(const double*)indices count:(int32_t)count values:(NSArray*)values; diff --git a/packages/react-native-tgfx/ios/TgfxMetalView.mm b/packages/react-native-tgfx/ios/TgfxMetalView.mm index eacb8f2d..4d7e53df 100644 --- a/packages/react-native-tgfx/ios/TgfxMetalView.mm +++ b/packages/react-native-tgfx/ios/TgfxMetalView.mm @@ -266,6 +266,18 @@ - (void)setValues:(const double*)values count:(int32_t)count { [self wakeUp]; } +- (void)bindValues:(const double*)values count:(int32_t)count { + if (_renderer != nullptr) { + _renderer->bindValues(values, count > 0 ? static_cast(count) : 0); + } +} + +- (void)pushValues { + if (_renderer != nullptr && _renderer->pushValues()) { + [self wakeUp]; + } +} + - (void)setStringValues:(const double*)indices count:(int32_t)count values:(NSArray*)values { diff --git a/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.cpp b/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.cpp index 1bc39ed3..3637bc19 100644 --- a/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.cpp +++ b/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.cpp @@ -101,6 +101,14 @@ namespace margelo::nitro::tgfx { return __array; }()); } + void JHybridTgfxViewSpec::bindValues(const std::shared_ptr& values) { + static const auto method = _javaPart->javaClassStatic()->getMethod /* values */)>("bindValues"); + method(_javaPart, JArrayBuffer::wrap(values)); + } + void JHybridTgfxViewSpec::pushValues() { + static const auto method = _javaPart->javaClassStatic()->getMethod("pushValues"); + method(_javaPart); + } void JHybridTgfxViewSpec::setStringValues(const std::vector& indices, const std::vector& values) { static const auto method = _javaPart->javaClassStatic()->getMethod /* indices */, jni::alias_ref> /* values */)>("setStringValues"); method(_javaPart, [&]() { diff --git a/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.hpp b/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.hpp index 82932d36..4448f884 100644 --- a/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.hpp +++ b/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.hpp @@ -60,6 +60,8 @@ namespace margelo::nitro::tgfx { void setScene(const std::shared_ptr& scene, const std::vector& strings, std::optional awaitValues) override; void invalidate() override; void setValues(const std::vector& values) override; + void bindValues(const std::shared_ptr& values) override; + void pushValues() override; void setStringValues(const std::vector& indices, const std::vector& values) override; void setViewport(const std::vector& viewport) override; void setColorSpace(const std::string& colorSpace) override; diff --git a/packages/react-native-tgfx/nitrogen/generated/android/kotlin/com/margelo/nitro/tgfx/HybridTgfxViewSpec.kt b/packages/react-native-tgfx/nitrogen/generated/android/kotlin/com/margelo/nitro/tgfx/HybridTgfxViewSpec.kt index cf7ff8fc..10d0da77 100644 --- a/packages/react-native-tgfx/nitrogen/generated/android/kotlin/com/margelo/nitro/tgfx/HybridTgfxViewSpec.kt +++ b/packages/react-native-tgfx/nitrogen/generated/android/kotlin/com/margelo/nitro/tgfx/HybridTgfxViewSpec.kt @@ -53,6 +53,14 @@ abstract class HybridTgfxViewSpec: HybridView() { @Keep abstract fun setValues(values: DoubleArray): Unit + @DoNotStrip + @Keep + abstract fun bindValues(values: ArrayBuffer): Unit + + @DoNotStrip + @Keep + abstract fun pushValues(): Unit + @DoNotStrip @Keep abstract fun setStringValues(indices: DoubleArray, values: Array): Unit diff --git a/packages/react-native-tgfx/nitrogen/generated/ios/c++/HybridTgfxViewSpecSwift.hpp b/packages/react-native-tgfx/nitrogen/generated/ios/c++/HybridTgfxViewSpecSwift.hpp index 83e5d901..617ba7ba 100644 --- a/packages/react-native-tgfx/nitrogen/generated/ios/c++/HybridTgfxViewSpecSwift.hpp +++ b/packages/react-native-tgfx/nitrogen/generated/ios/c++/HybridTgfxViewSpecSwift.hpp @@ -107,6 +107,18 @@ namespace margelo::nitro::tgfx { std::rethrow_exception(__result.error()); } } + inline void bindValues(const std::shared_ptr& values) override { + auto __result = _swiftPart.bindValues(ArrayBufferHolder(values)); + if (__result.hasError()) [[unlikely]] { + std::rethrow_exception(__result.error()); + } + } + inline void pushValues() override { + auto __result = _swiftPart.pushValues(); + if (__result.hasError()) [[unlikely]] { + std::rethrow_exception(__result.error()); + } + } inline void setStringValues(const std::vector& indices, const std::vector& values) override { auto __result = _swiftPart.setStringValues(indices, values); if (__result.hasError()) [[unlikely]] { diff --git a/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec.swift b/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec.swift index 8cf972b6..4cd7acee 100644 --- a/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec.swift +++ b/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec.swift @@ -17,6 +17,8 @@ public protocol HybridTgfxViewSpec_protocol: HybridObject, HybridView { func setScene(scene: ArrayBuffer, strings: [String], awaitValues: Bool?) throws -> Void func invalidate() throws -> Void func setValues(values: [Double]) throws -> Void + func bindValues(values: ArrayBuffer) throws -> Void + func pushValues() throws -> Void func setStringValues(indices: [Double], values: [String]) throws -> Void func setViewport(viewport: [Double]) throws -> Void func setColorSpace(colorSpace: String) throws -> Void diff --git a/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec_cxx.swift b/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec_cxx.swift index 315c0de1..13790279 100644 --- a/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec_cxx.swift +++ b/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec_cxx.swift @@ -184,6 +184,28 @@ open class HybridTgfxViewSpec_cxx { } } + @inline(__always) + public final func bindValues(values: ArrayBuffer) -> bridge.Result_void_ { + do { + try self.__implementation.bindValues(values: values) + return bridge.create_Result_void_() + } catch (let __error) { + let __exceptionPtr = __error.toCpp() + return bridge.create_Result_void_(__exceptionPtr) + } + } + + @inline(__always) + public final func pushValues() -> bridge.Result_void_ { + do { + try self.__implementation.pushValues() + return bridge.create_Result_void_() + } catch (let __error) { + let __exceptionPtr = __error.toCpp() + return bridge.create_Result_void_(__exceptionPtr) + } + } + @inline(__always) public final func setStringValues(indices: bridge.std__vector_double_, values: bridge.std__vector_std__string_) -> bridge.Result_void_ { do { diff --git a/packages/react-native-tgfx/nitrogen/generated/shared/c++/HybridTgfxViewSpec.cpp b/packages/react-native-tgfx/nitrogen/generated/shared/c++/HybridTgfxViewSpec.cpp index 63f4dfbc..8d4b63a8 100644 --- a/packages/react-native-tgfx/nitrogen/generated/shared/c++/HybridTgfxViewSpec.cpp +++ b/packages/react-native-tgfx/nitrogen/generated/shared/c++/HybridTgfxViewSpec.cpp @@ -21,6 +21,8 @@ namespace margelo::nitro::tgfx { prototype.registerHybridMethod("setScene", &HybridTgfxViewSpec::setScene); prototype.registerHybridMethod("invalidate", &HybridTgfxViewSpec::invalidate); prototype.registerHybridMethod("setValues", &HybridTgfxViewSpec::setValues); + prototype.registerHybridMethod("bindValues", &HybridTgfxViewSpec::bindValues); + prototype.registerHybridMethod("pushValues", &HybridTgfxViewSpec::pushValues); prototype.registerHybridMethod("setStringValues", &HybridTgfxViewSpec::setStringValues); prototype.registerHybridMethod("setViewport", &HybridTgfxViewSpec::setViewport); prototype.registerHybridMethod("setColorSpace", &HybridTgfxViewSpec::setColorSpace); diff --git a/packages/react-native-tgfx/nitrogen/generated/shared/c++/HybridTgfxViewSpec.hpp b/packages/react-native-tgfx/nitrogen/generated/shared/c++/HybridTgfxViewSpec.hpp index c7ac0134..e74bc123 100644 --- a/packages/react-native-tgfx/nitrogen/generated/shared/c++/HybridTgfxViewSpec.hpp +++ b/packages/react-native-tgfx/nitrogen/generated/shared/c++/HybridTgfxViewSpec.hpp @@ -63,6 +63,8 @@ namespace margelo::nitro::tgfx { virtual void setScene(const std::shared_ptr& scene, const std::vector& strings, std::optional awaitValues) = 0; virtual void invalidate() = 0; virtual void setValues(const std::vector& values) = 0; + virtual void bindValues(const std::shared_ptr& values) = 0; + virtual void pushValues() = 0; virtual void setStringValues(const std::vector& indices, const std::vector& values) = 0; virtual void setViewport(const std::vector& viewport) = 0; virtual void setColorSpace(const std::string& colorSpace) = 0; diff --git a/packages/react-native-tgfx/src/specs/TgfxView.nitro.ts b/packages/react-native-tgfx/src/specs/TgfxView.nitro.ts index 902abed5..0f0dbdbc 100644 --- a/packages/react-native-tgfx/src/specs/TgfxView.nitro.ts +++ b/packages/react-native-tgfx/src/specs/TgfxView.nitro.ts @@ -91,6 +91,20 @@ export interface TgfxViewMethods extends HybridViewMethods { */ setValues(values: number[]): void + /** + * Point {@link pushValues} at a block of `Float64` value slots: the buffer + * behind a `TgfxSlots` `values` view, whose memory every runtime shares. The + * view holds the buffer until the next bind, so the caller can drop it. + */ + bindValues(values: ArrayBuffer): void + + /** + * {@link setValues} with the bound block's current contents, copied + * natively. The animation driver writes the block and calls this every + * frame, so no array is marshalled. Does nothing until a block is bound. + */ + pushValues(): void + /** * Update the scene's string slots - live `text` and `path` content. Sparse: * `indices[i]` names the slot `values[i]` replaces, and only changed slots are From 8d3fcb5b4d08ada4e8e9e7cb642ab288bd1a96e4 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Tue, 3 Nov 2026 19:09:20 +0100 Subject: [PATCH 2/5] perf(reanimated): push driver values from the mirror block The driver already writes every animated value into the mirror block for the next commit's snapshot. It now binds that block to the view once per mapper and calls pushValues() each frame instead of building a slot array for setValues. A view without pushValues, or a canvas without a mirror, still gets the array. On a Pixel 9a emulator (debug build) a push of 256 values drops from 44 us to 7 us, and 4096 values from about 665 us to 87 us. A harness case covers a value set that changes while the canvas animates. --- apps/example/src/imperative.harness.tsx | 32 +++++ .../src/__tests__/AnimationDriver.test.ts | 122 ++++++++++++++++++ .../src/reanimated/AnimationDriver.tsx | 18 ++- .../src/reanimated/slotFlags.ts | 6 +- 4 files changed, 173 insertions(+), 5 deletions(-) diff --git a/apps/example/src/imperative.harness.tsx b/apps/example/src/imperative.harness.tsx index c28cd2e7..9ecd373e 100644 --- a/apps/example/src/imperative.harness.tsx +++ b/apps/example/src/imperative.harness.tsx @@ -248,6 +248,38 @@ describe('tgfx shared values', () => { expect(sameFrame(unwritten, await stageBytes(0))).toBe(false); await snapshot('animated-shared-value-written'); }); + + test('a new shared value set keeps redrawing after the driver rebinds', async () => { + let grow: (() => void) | null = null; + let width: { value: number } | null = null; + + function Scene() { + const r = useSharedValue(30); + const w = useSharedValue(40); + const [both, setBoth] = React.useState(false); + grow = () => setBoth(true); + width = w; + return ( + + + + {both ? ( + + ) : null} + + ); + } + + await render(); + // A second shared value changes the set, so the driver starts on a new mirror. + grow!(); + await sleep(200); + const grown = await stageBytes(0); + width!.value = 160; + await sleep(200); + // Only a push from the new block carries the width slot. + expect(sameFrame(grown, await stageBytes(0))).toBe(false); + }); }); describe('tgfx canvas handle', () => { diff --git a/packages/react-native-tgfx/src/__tests__/AnimationDriver.test.ts b/packages/react-native-tgfx/src/__tests__/AnimationDriver.test.ts index 30686fd7..8814205d 100644 --- a/packages/react-native-tgfx/src/__tests__/AnimationDriver.test.ts +++ b/packages/react-native-tgfx/src/__tests__/AnimationDriver.test.ts @@ -1,4 +1,9 @@ import { expect, mock, test } from 'bun:test' +import { createElement } from 'react' +import TestRenderer, { act } from 'react-test-renderer' +;( + globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean } +).IS_REACT_ACT_ENVIRONMENT = true // A renamed export must not look like an absent package, and it throws where the driver renders. test('Reanimated present but missing an export throws on render', async () => { @@ -10,3 +15,120 @@ test('Reanimated present but missing an export throws on render', async () => { /does not export startMapper/ ) }) + +/** A driver whose mapper runs when the test calls `frame()`. */ +async function driverWithFrames() { + const mappers: (() => void)[] = [] + mock.module('react-native-reanimated', () => ({ + startMapper: (worklet: () => void) => mappers.push(worklet), + stopMapper: () => {}, + })) + const { AnimationDriver } = + (await import('../reanimated/AnimationDriver?frames')) as typeof import('../reanimated/AnimationDriver') + return { Driver: AnimationDriver!, frame: () => mappers.at(-1)!() } +} + +const shared = (value: number) => ({ + value, + _isReanimatedSharedValue: true as const, +}) + +/** A mirror block as the driver sees it: boxed, unboxing to the slots' buffers. */ +function block(count: number) { + const values = new ArrayBuffer(count * 8) + const flags = new ArrayBuffer(1) + return { values, boxed: { unbox: () => ({ flags, values }) } } +} + +function driverProps( + view: object, + values: object[], + mirror: { boxed: object } | null +) { + return { + view, + values, + strings: [], + gens: [], + mirror: mirror?.boxed ?? null, + } as never +} + +test('the driver binds its mirror once and has the view push it each frame', async () => { + const { Driver, frame } = await driverWithFrames() + const mirror = block(2) + const calls: string[] = [] + const view = { + setValues: () => calls.push('setValues'), + bindValues: (values: ArrayBuffer) => + calls.push(values === mirror.values ? 'bind' : 'bind another'), + pushValues: () => calls.push('push'), + } + const x = shared(3) + const y = shared(4) + act(() => { + TestRenderer.create( + createElement(Driver, driverProps(view, [x, y], mirror)) + ) + }) + frame() + x.value = 5 + frame() + expect(calls).toEqual(['bind', 'push', 'push']) + expect(Array.from(new Float64Array(mirror.values))).toEqual([5, 4]) +}) + +test('a new value set binds its own mirror before its first push', async () => { + const { Driver, frame } = await driverWithFrames() + const first = block(1) + const second = block(2) + const calls: string[] = [] + const view = { + setValues: () => calls.push('setValues'), + bindValues: (values: ArrayBuffer) => + calls.push( + values === first.values + ? 'bind first' + : values === second.values + ? 'bind second' + : 'bind another' + ), + pushValues: () => calls.push('push'), + } + const x = shared(1) + let tree: TestRenderer.ReactTestRenderer | null = null + act(() => { + tree = TestRenderer.create( + createElement(Driver, driverProps(view, [x], first)) + ) + }) + frame() + act(() => { + tree!.update( + createElement(Driver, driverProps(view, [x, shared(2)], second)) + ) + }) + frame() + frame() + expect(calls).toEqual(['bind first', 'push', 'bind second', 'push', 'push']) +}) + +test('a native view without pushValues, or no mirror, still gets the slot array', async () => { + const { Driver, frame } = await driverWithFrames() + const pushed: number[][] = [] + const older = { setValues: (values: number[]) => pushed.push(values.slice()) } + act(() => { + TestRenderer.create( + createElement(Driver, driverProps(older, [shared(3)], block(1))) + ) + }) + frame() + const unmirrored = { ...older, bindValues: () => {}, pushValues: () => {} } + act(() => { + TestRenderer.create( + createElement(Driver, driverProps(unmirrored, [shared(7)], null)) + ) + }) + frame() + expect(pushed).toEqual([[3], [7]]) +}) diff --git a/packages/react-native-tgfx/src/reanimated/AnimationDriver.tsx b/packages/react-native-tgfx/src/reanimated/AnimationDriver.tsx index 5179d8dd..4ef5f797 100644 --- a/packages/react-native-tgfx/src/reanimated/AnimationDriver.tsx +++ b/packages/react-native-tgfx/src/reanimated/AnimationDriver.tsx @@ -26,6 +26,8 @@ function Driver({ view, values, strings, gens, mirror }: DriverProps): null { const slots = new Array(values.length) // Last pushed per string slot: only changes cross, so an idle frame is free. const sent = new Array(strings.length).fill(null) + // Set once this mapper has bound its mirror, from the thread that writes it. + const bound = [false] // A block slot cannot be a mapper input; the handle's gen stands in for it. const listenable = [...values, ...strings].filter((v) => !isBlockSlot(v)) for (const gen of gens) { @@ -37,9 +39,13 @@ function Driver({ view, values, strings, gens, mirror }: DriverProps): null { const mapperId = start(() => { 'worklet' const mirrored = mirror != null ? valuesOf(mirror) : null + // The view copies the mirror natively, so no array is marshalled. Optional: an older binary lacks it. + const direct = mirrored != null && view.pushValues != null for (let i = 0; i < values.length; i++) { const v = readLiveNumber(values[i]!) - slots[i] = v + if (!direct) { + slots[i] = v + } if (mirrored != null) { mirrored[i] = v } @@ -47,7 +53,15 @@ function Driver({ view, values, strings, gens, mirror }: DriverProps): null { if (mirror != null) { flagsOf(mirror)[0] = 1 } - view.setValues(slots) + if (direct) { + if (!bound[0]) { + view.bindValues(mirrored.buffer) + bound[0] = true + } + view.pushValues() + } else { + view.setValues(slots) + } if (strings.length > 0) { let indices: number[] | null = null let out: string[] | null = null diff --git a/packages/react-native-tgfx/src/reanimated/slotFlags.ts b/packages/react-native-tgfx/src/reanimated/slotFlags.ts index c0acc089..91fd7936 100644 --- a/packages/react-native-tgfx/src/reanimated/slotFlags.ts +++ b/packages/react-native-tgfx/src/reanimated/slotFlags.ts @@ -84,14 +84,14 @@ export function flagsOf(boxed: BoxedSlots): Uint8Array { } /** This runtime's `Float64` view of the block's value slots, cached like {@link flagsOf}. */ -export function valuesOf(boxed: BoxedSlots): Float64Array { +export function valuesOf(boxed: BoxedSlots): Float64Array { 'worklet' const scope = globalThis as { - __tgfxValueViews?: WeakMap + __tgfxValueViews?: WeakMap> } let views = scope.__tgfxValueViews if (views === undefined) { - views = new WeakMap() + views = new WeakMap>() scope.__tgfxValueViews = views } let view = views.get(boxed) From a8ef6b283a205fb95bbbf78ce59d44024360e620 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Tue, 3 Nov 2026 19:53:48 +0100 Subject: [PATCH 3/5] perf(scene): push a Scene's values from the mirror block too A Scene attached to a canvas runs the same mapper as the driver but still sent setValues(slots) to every target each frame. A target that has pushValues now binds the mirror once and copies it natively, while an offscreen surface or an older binary keeps getting the array, which the mapper now fills only when some target reads it. With 1024 slots on a Pixel 9a emulator (debug build) a frame for one canvas drops from about 166 us to 33 us, and a second canvas adds about 1.5 us instead of another 124 us marshal. --- .../src/__tests__/scene.test.ts | 46 +++++++++++++++++++ packages/react-native-tgfx/src/scene.ts | 26 +++++++++-- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/packages/react-native-tgfx/src/__tests__/scene.test.ts b/packages/react-native-tgfx/src/__tests__/scene.test.ts index cd6c9f2b..00b6e79b 100644 --- a/packages/react-native-tgfx/src/__tests__/scene.test.ts +++ b/packages/react-native-tgfx/src/__tests__/scene.test.ts @@ -177,6 +177,52 @@ test('a scene runs its own mapper so handle writes land without a driver', () => expect(mappers.size).toBe(0) // no targets left, nothing to push to }) +test('a mirrored mapper binds each pushing target once; the rest get the array', () => { + const scene = Scene.create() + const box = createHandle() + scene.add(rect({ ref: box, x: 1, y: 2, width: 3, height: 4 })) + const { t: surface, calls: surfaceCalls } = target() + scene.attach(surface) + scene.flush() + // The test runner has no Nitro, so stand in for the mirror the container would make. + const values = new ArrayBuffer(4 * 8) + const flags = new ArrayBuffer(1) + const internals = scene as unknown as { + container: { mirror: unknown } + lastValues: unknown[] + } + internals.container.mirror = { + values: internals.lastValues.slice(), + block: { + bytes: new Uint8Array(flags), + values: new Float64Array(values), + slots: {}, + boxed: { unbox: () => ({ flags, values }) }, + }, + } + const pushes: string[] = [] + const canvas: SceneTarget = { + setScene: () => {}, + setValues: () => pushes.push('setValues'), + bindValues: (buffer) => + pushes.push(buffer === values ? 'bind' : 'bind another'), + pushValues: () => pushes.push('push'), + } + // Attaching recompiles with the same slots, so the mirror stays and the mapper restarts. + scene.attach(canvas) + expect(mappers.size).toBe(1) + const run = [...mappers.values()][0]! + const before = surfaceCalls.values.length + run() + run() + expect(pushes).toEqual(['bind', 'push', 'push']) + expect(surfaceCalls.values.slice(before)).toEqual([ + [1, 2, 3, 4], + [1, 2, 3, 4], + ]) + expect(Array.from(new Float64Array(values))).toEqual([1, 2, 3, 4]) +}) + test('dispose stops the mapper', () => { const { t } = target() const scene = Scene.create() diff --git a/packages/react-native-tgfx/src/scene.ts b/packages/react-native-tgfx/src/scene.ts index 42c6118c..fe17ba54 100644 --- a/packages/react-native-tgfx/src/scene.ts +++ b/packages/react-native-tgfx/src/scene.ts @@ -49,6 +49,10 @@ export interface SceneTarget { setScene(scene: ArrayBuffer, strings: string[]): void setValues(values: number[]): void setStringValues?(indices: number[], values: string[]): void + /** @internal bound once by the mapper, which then has the target push the mirror block. */ + bindValues?(values: ArrayBuffer): void + /** @internal */ + pushValues?(): void } interface Attachment { @@ -392,6 +396,10 @@ export class Scene { const mirror = this.container.mirror?.block.boxed ?? null const slots = new Array(values.length) const sent = new Array(stringValues.length).fill(null) + // Per target: set once it has bound the mirror, from the thread that writes it. + const bound = views.map(() => false) + // Only a target that cannot push, or a scene without a mirror, still reads the array. + const fillSlots = mirror == null || views.some((v) => v.pushValues == null) // Block slots have no listeners, so they cannot be mapper inputs; the // handles' gen values - bumped once per worklet-side set() - stand in. const listenable = [...values, ...stringValues].filter( @@ -408,7 +416,9 @@ export class Scene { const mirrored = mirror != null ? valuesOf(mirror) : null for (let i = 0; i < values.length; i++) { const v = readLiveNumber(values[i]!) - slots[i] = v + if (fillSlots) { + slots[i] = v + } if (mirrored != null) { mirrored[i] = v } @@ -416,8 +426,18 @@ export class Scene { if (mirror != null) { flagsOf(mirror)[0] = 1 } - for (const view of views) { - view.setValues(slots) + for (let t = 0; t < views.length; t++) { + const view = views[t]! + // An offscreen surface or an older binary has no push, so it takes the array. + if (mirrored != null && view.pushValues != null) { + if (!bound[t]) { + view.bindValues?.(mirrored.buffer) + bound[t] = true + } + view.pushValues() + } else { + view.setValues(slots) + } } if (stringValues.length > 0) { let indices: number[] | null = null From 53875563f1318eef339654a61b865f12349c71b4 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Tue, 3 Nov 2026 20:38:02 +0100 Subject: [PATCH 4/5] fix: bind the value block again after a recycle drops it On iOS a canvas hidden with display: none is unmounted and its view is recycled, which clears the binding. When the canvas shows again it keeps the same view, so the mapper still counts it as bound and every push is dropped: the animation freezes until the value set changes. setValues had no such state to lose. pushValues now returns false while nothing is bound, and the driver and the Scene mapper bind again and retry. bindValues also rejects a JS-owned buffer, which would leave the renderer reading memory its runtime frees. --- apps/example/src/imperative.harness.tsx | 32 +++++ .../com/margelo/nitro/tgfx/HybridTgfxView.kt | 4 +- .../margelo/nitro/tgfx/TgfxRenderTarget.kt | 5 +- .../ios/HybridTgfxView.swift | 8 +- .../react-native-tgfx/ios/TgfxMetalView.h | 2 +- .../react-native-tgfx/ios/TgfxMetalView.mm | 8 +- .../android/c++/JHybridTgfxViewSpec.cpp | 7 +- .../android/c++/JHybridTgfxViewSpec.hpp | 2 +- .../margelo/nitro/tgfx/HybridTgfxViewSpec.kt | 2 +- .../ios/NitroTgfx-Swift-Cxx-Bridge.hpp | 9 ++ .../ios/c++/HybridTgfxViewSpecSwift.hpp | 4 +- .../ios/swift/HybridTgfxViewSpec.swift | 2 +- .../ios/swift/HybridTgfxViewSpec_cxx.swift | 9 +- .../shared/c++/HybridTgfxViewSpec.hpp | 2 +- .../src/__tests__/AnimationDriver.test.ts | 114 ++++++++++++------ .../src/__tests__/scene.test.ts | 79 +++++++++--- .../src/reanimated/AnimationDriver.tsx | 6 +- packages/react-native-tgfx/src/scene.ts | 10 +- .../src/specs/TgfxView.nitro.ts | 5 +- 19 files changed, 225 insertions(+), 85 deletions(-) diff --git a/apps/example/src/imperative.harness.tsx b/apps/example/src/imperative.harness.tsx index 9ecd373e..80354eca 100644 --- a/apps/example/src/imperative.harness.tsx +++ b/apps/example/src/imperative.harness.tsx @@ -1,5 +1,6 @@ // Node handles and shared values: the two ways a scene changes without a React commit. import React, { useRef } from 'react'; +import { View } from 'react-native'; import { describe, expect, render, test } from 'react-native-harness'; import { useSharedValue } from 'react-native-reanimated'; import { @@ -280,6 +281,37 @@ describe('tgfx shared values', () => { // Only a push from the new block carries the width slot. expect(sameFrame(grown, await stageBytes(0))).toBe(false); }); + + test('a canvas hidden and shown again keeps animating', async () => { + let hide: ((hidden: boolean) => void) | null = null; + let radius: { value: number } | null = null; + + function Scene() { + const r = useSharedValue(30); + const [hidden, setHidden] = React.useState(false); + radius = r; + hide = setHidden; + return ( + + ); + } + + await render(); + // iOS unmounts a hidden view and recycles it, which drops its value binding. + hide!(true); + await sleep(200); + hide!(false); + await sleep(200); + const shown = await stageBytes(0); + radius!.value = 85; + await sleep(200); + expect(sameFrame(shown, await stageBytes(0))).toBe(false); + }); }); describe('tgfx canvas handle', () => { diff --git a/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/HybridTgfxView.kt b/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/HybridTgfxView.kt index 98b6cd58..5dc1b493 100644 --- a/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/HybridTgfxView.kt +++ b/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/HybridTgfxView.kt @@ -123,8 +123,8 @@ class HybridTgfxView(private val context: ThemedReactContext) : target.bindValues(values) } - override fun pushValues() { - target.pushValues() + override fun pushValues(): Boolean { + return target.pushValues() } override fun setStringValues(indices: DoubleArray, values: Array) { diff --git a/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/TgfxRenderTarget.kt b/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/TgfxRenderTarget.kt index ed56a136..380416b7 100644 --- a/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/TgfxRenderTarget.kt +++ b/packages/react-native-tgfx/android/src/main/java/com/margelo/nitro/tgfx/TgfxRenderTarget.kt @@ -131,6 +131,8 @@ internal class TgfxRenderTarget { private var boundValues: ArrayBuffer? = null fun bindValues(values: ArrayBuffer) { + // A JS-owned buffer dies with its runtime while the renderer keeps reading it. + require(values.isOwner) { "[tgfx] bindValues needs a native-owned buffer (a TgfxSlots values block)" } val bytes = values.getBuffer(false) withPtr { TgfxNative.bindValues(it, bytes) @@ -138,12 +140,13 @@ internal class TgfxRenderTarget { } } - fun pushValues() { + fun pushValues(): Boolean { var pushed = false withPtr { pushed = TgfxNative.pushValues(it) } if (pushed) { requestRender() } + return pushed } fun setStringValues(indices: DoubleArray, values: Array) { diff --git a/packages/react-native-tgfx/ios/HybridTgfxView.swift b/packages/react-native-tgfx/ios/HybridTgfxView.swift index 91056281..53e43ce3 100644 --- a/packages/react-native-tgfx/ios/HybridTgfxView.swift +++ b/packages/react-native-tgfx/ios/HybridTgfxView.swift @@ -49,13 +49,17 @@ final class HybridTgfxView: HybridTgfxViewSpec, RecyclableView { } func bindValues(values: ArrayBuffer) throws { + // A JS-owned buffer dies with its runtime while the renderer keeps reading it. + guard values.isOwner else { + throw RuntimeError.error(withMessage: "[tgfx] bindValues needs a native-owned buffer (a TgfxSlots values block)") + } let slots = UnsafeRawPointer(values.data).assumingMemoryBound(to: Double.self) metalView.bindValues(slots, count: Int32(values.size / MemoryLayout.size)) boundValues = values } - func pushValues() throws { - metalView.pushValues() + func pushValues() throws -> Bool { + return metalView.pushValues() } func setStringValues(indices: [Double], values: [String]) throws { diff --git a/packages/react-native-tgfx/ios/TgfxMetalView.h b/packages/react-native-tgfx/ios/TgfxMetalView.h index dd3f0e35..f2a67eb1 100644 --- a/packages/react-native-tgfx/ios/TgfxMetalView.h +++ b/packages/react-native-tgfx/ios/TgfxMetalView.h @@ -27,7 +27,7 @@ FOUNDATION_EXPORT void TgfxSnapshotRelease(void* _Nullable token); - (void)setValues:(const double*)values count:(int32_t)count; /// The block `pushValues` copies from; the caller keeps it alive until the next bind. - (void)bindValues:(const double* _Nullable)values count:(int32_t)count; -- (void)pushValues; +- (BOOL)pushValues; - (void)setStringValues:(const double*)indices count:(int32_t)count values:(NSArray*)values; diff --git a/packages/react-native-tgfx/ios/TgfxMetalView.mm b/packages/react-native-tgfx/ios/TgfxMetalView.mm index 4d7e53df..7d9799f9 100644 --- a/packages/react-native-tgfx/ios/TgfxMetalView.mm +++ b/packages/react-native-tgfx/ios/TgfxMetalView.mm @@ -272,10 +272,12 @@ - (void)bindValues:(const double*)values count:(int32_t)count { } } -- (void)pushValues { - if (_renderer != nullptr && _renderer->pushValues()) { - [self wakeUp]; +- (BOOL)pushValues { + if (_renderer == nullptr || !_renderer->pushValues()) { + return NO; } + [self wakeUp]; + return YES; } - (void)setStringValues:(const double*)indices diff --git a/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.cpp b/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.cpp index 3637bc19..d6b8a5dd 100644 --- a/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.cpp +++ b/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.cpp @@ -105,9 +105,10 @@ namespace margelo::nitro::tgfx { static const auto method = _javaPart->javaClassStatic()->getMethod /* values */)>("bindValues"); method(_javaPart, JArrayBuffer::wrap(values)); } - void JHybridTgfxViewSpec::pushValues() { - static const auto method = _javaPart->javaClassStatic()->getMethod("pushValues"); - method(_javaPart); + bool JHybridTgfxViewSpec::pushValues() { + static const auto method = _javaPart->javaClassStatic()->getMethod("pushValues"); + auto __result = method(_javaPart); + return static_cast(__result); } void JHybridTgfxViewSpec::setStringValues(const std::vector& indices, const std::vector& values) { static const auto method = _javaPart->javaClassStatic()->getMethod /* indices */, jni::alias_ref> /* values */)>("setStringValues"); diff --git a/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.hpp b/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.hpp index 4448f884..966f84f9 100644 --- a/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.hpp +++ b/packages/react-native-tgfx/nitrogen/generated/android/c++/JHybridTgfxViewSpec.hpp @@ -61,7 +61,7 @@ namespace margelo::nitro::tgfx { void invalidate() override; void setValues(const std::vector& values) override; void bindValues(const std::shared_ptr& values) override; - void pushValues() override; + bool pushValues() override; void setStringValues(const std::vector& indices, const std::vector& values) override; void setViewport(const std::vector& viewport) override; void setColorSpace(const std::string& colorSpace) override; diff --git a/packages/react-native-tgfx/nitrogen/generated/android/kotlin/com/margelo/nitro/tgfx/HybridTgfxViewSpec.kt b/packages/react-native-tgfx/nitrogen/generated/android/kotlin/com/margelo/nitro/tgfx/HybridTgfxViewSpec.kt index 10d0da77..eddef428 100644 --- a/packages/react-native-tgfx/nitrogen/generated/android/kotlin/com/margelo/nitro/tgfx/HybridTgfxViewSpec.kt +++ b/packages/react-native-tgfx/nitrogen/generated/android/kotlin/com/margelo/nitro/tgfx/HybridTgfxViewSpec.kt @@ -59,7 +59,7 @@ abstract class HybridTgfxViewSpec: HybridView() { @DoNotStrip @Keep - abstract fun pushValues(): Unit + abstract fun pushValues(): Boolean @DoNotStrip @Keep diff --git a/packages/react-native-tgfx/nitrogen/generated/ios/NitroTgfx-Swift-Cxx-Bridge.hpp b/packages/react-native-tgfx/nitrogen/generated/ios/NitroTgfx-Swift-Cxx-Bridge.hpp index fc35db56..d22fc0e2 100644 --- a/packages/react-native-tgfx/nitrogen/generated/ios/NitroTgfx-Swift-Cxx-Bridge.hpp +++ b/packages/react-native-tgfx/nitrogen/generated/ios/NitroTgfx-Swift-Cxx-Bridge.hpp @@ -274,6 +274,15 @@ namespace margelo::nitro::tgfx::bridge::swift { using std__weak_ptr_HybridTgfxViewSpec_ = std::weak_ptr; inline std__weak_ptr_HybridTgfxViewSpec_ weakify_std__shared_ptr_HybridTgfxViewSpec_(const std::shared_ptr& strong) noexcept { return strong; } + // pragma MARK: Result + using Result_bool_ = Result; + inline Result_bool_ create_Result_bool_(bool value) noexcept { + return Result::withValue(std::move(value)); + } + inline Result_bool_ create_Result_bool_(const std::exception_ptr& error) noexcept { + return Result::withError(error); + } + // pragma MARK: Result> using Result_std__vector_double__ = Result>; inline Result_std__vector_double__ create_Result_std__vector_double__(const std::vector& value) noexcept { diff --git a/packages/react-native-tgfx/nitrogen/generated/ios/c++/HybridTgfxViewSpecSwift.hpp b/packages/react-native-tgfx/nitrogen/generated/ios/c++/HybridTgfxViewSpecSwift.hpp index 617ba7ba..1f3c519b 100644 --- a/packages/react-native-tgfx/nitrogen/generated/ios/c++/HybridTgfxViewSpecSwift.hpp +++ b/packages/react-native-tgfx/nitrogen/generated/ios/c++/HybridTgfxViewSpecSwift.hpp @@ -113,11 +113,13 @@ namespace margelo::nitro::tgfx { std::rethrow_exception(__result.error()); } } - inline void pushValues() override { + inline bool pushValues() override { auto __result = _swiftPart.pushValues(); if (__result.hasError()) [[unlikely]] { std::rethrow_exception(__result.error()); } + auto __value = std::move(__result.value()); + return __value; } inline void setStringValues(const std::vector& indices, const std::vector& values) override { auto __result = _swiftPart.setStringValues(indices, values); diff --git a/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec.swift b/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec.swift index 4cd7acee..2f1d78d9 100644 --- a/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec.swift +++ b/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec.swift @@ -18,7 +18,7 @@ public protocol HybridTgfxViewSpec_protocol: HybridObject, HybridView { func invalidate() throws -> Void func setValues(values: [Double]) throws -> Void func bindValues(values: ArrayBuffer) throws -> Void - func pushValues() throws -> Void + func pushValues() throws -> Bool func setStringValues(indices: [Double], values: [String]) throws -> Void func setViewport(viewport: [Double]) throws -> Void func setColorSpace(colorSpace: String) throws -> Void diff --git a/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec_cxx.swift b/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec_cxx.swift index 13790279..ea5f37a5 100644 --- a/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec_cxx.swift +++ b/packages/react-native-tgfx/nitrogen/generated/ios/swift/HybridTgfxViewSpec_cxx.swift @@ -196,13 +196,14 @@ open class HybridTgfxViewSpec_cxx { } @inline(__always) - public final func pushValues() -> bridge.Result_void_ { + public final func pushValues() -> bridge.Result_bool_ { do { - try self.__implementation.pushValues() - return bridge.create_Result_void_() + let __result = try self.__implementation.pushValues() + let __resultCpp = __result + return bridge.create_Result_bool_(__resultCpp) } catch (let __error) { let __exceptionPtr = __error.toCpp() - return bridge.create_Result_void_(__exceptionPtr) + return bridge.create_Result_bool_(__exceptionPtr) } } diff --git a/packages/react-native-tgfx/nitrogen/generated/shared/c++/HybridTgfxViewSpec.hpp b/packages/react-native-tgfx/nitrogen/generated/shared/c++/HybridTgfxViewSpec.hpp index e74bc123..45a09686 100644 --- a/packages/react-native-tgfx/nitrogen/generated/shared/c++/HybridTgfxViewSpec.hpp +++ b/packages/react-native-tgfx/nitrogen/generated/shared/c++/HybridTgfxViewSpec.hpp @@ -64,7 +64,7 @@ namespace margelo::nitro::tgfx { virtual void invalidate() = 0; virtual void setValues(const std::vector& values) = 0; virtual void bindValues(const std::shared_ptr& values) = 0; - virtual void pushValues() = 0; + virtual bool pushValues() = 0; virtual void setStringValues(const std::vector& indices, const std::vector& values) = 0; virtual void setViewport(const std::vector& viewport) = 0; virtual void setColorSpace(const std::string& colorSpace) = 0; diff --git a/packages/react-native-tgfx/src/__tests__/AnimationDriver.test.ts b/packages/react-native-tgfx/src/__tests__/AnimationDriver.test.ts index 8814205d..c4f9659b 100644 --- a/packages/react-native-tgfx/src/__tests__/AnimationDriver.test.ts +++ b/packages/react-native-tgfx/src/__tests__/AnimationDriver.test.ts @@ -16,16 +16,54 @@ test('Reanimated present but missing an export throws on render', async () => { ) }) -/** A driver whose mapper runs when the test calls `frame()`. */ +/** A driver whose live mappers all run when the test calls `frame()`, as Reanimated's would. */ async function driverWithFrames() { - const mappers: (() => void)[] = [] + const mappers = new Map void>() + let next = 1 mock.module('react-native-reanimated', () => ({ - startMapper: (worklet: () => void) => mappers.push(worklet), - stopMapper: () => {}, + startMapper: (worklet: () => void) => { + mappers.set(next, worklet) + return next++ + }, + stopMapper: (id: number) => mappers.delete(id), })) const { AnimationDriver } = (await import('../reanimated/AnimationDriver?frames')) as typeof import('../reanimated/AnimationDriver') - return { Driver: AnimationDriver!, frame: () => mappers.at(-1)!() } + return { + Driver: AnimationDriver!, + frame: () => { + for (const run of mappers.values()) { + run() + } + }, + } +} + +/** The native contract: a bind keeps the buffer, a push copies it now, and a recycle unbinds. */ +function nativeView() { + let bound: ArrayBuffer | null = null + const received: number[][] = [] + const arrays: number[][] = [] + return { + received, + arrays, + recycle: () => { + bound = null + }, + setValues: (values: number[]) => { + arrays.push(values.slice()) + }, + bindValues: (values: ArrayBuffer) => { + bound = values + }, + pushValues: () => { + if (bound == null) { + return false + } + received.push(Array.from(new Float64Array(bound))) + return true + }, + } } const shared = (value: number) => ({ @@ -37,7 +75,7 @@ const shared = (value: number) => ({ function block(count: number) { const values = new ArrayBuffer(count * 8) const flags = new ArrayBuffer(1) - return { values, boxed: { unbox: () => ({ flags, values }) } } + return { values, flags, boxed: { unbox: () => ({ flags, values }) } } } function driverProps( @@ -54,76 +92,76 @@ function driverProps( } as never } -test('the driver binds its mirror once and has the view push it each frame', async () => { +test('each frame the view copies that frame values from the mirror', async () => { const { Driver, frame } = await driverWithFrames() const mirror = block(2) - const calls: string[] = [] - const view = { - setValues: () => calls.push('setValues'), - bindValues: (values: ArrayBuffer) => - calls.push(values === mirror.values ? 'bind' : 'bind another'), - pushValues: () => calls.push('push'), - } + const view = nativeView() const x = shared(3) - const y = shared(4) act(() => { TestRenderer.create( - createElement(Driver, driverProps(view, [x, y], mirror)) + createElement(Driver, driverProps(view, [x, shared(4)], mirror)) ) }) frame() x.value = 5 frame() - expect(calls).toEqual(['bind', 'push', 'push']) - expect(Array.from(new Float64Array(mirror.values))).toEqual([5, 4]) + expect(view.received).toEqual([ + [3, 4], + [5, 4], + ]) + expect(view.arrays).toEqual([]) + expect(new Uint8Array(mirror.flags)[0]).toBe(1) }) test('a new value set binds its own mirror before its first push', async () => { const { Driver, frame } = await driverWithFrames() - const first = block(1) - const second = block(2) - const calls: string[] = [] - const view = { - setValues: () => calls.push('setValues'), - bindValues: (values: ArrayBuffer) => - calls.push( - values === first.values - ? 'bind first' - : values === second.values - ? 'bind second' - : 'bind another' - ), - pushValues: () => calls.push('push'), - } + const view = nativeView() const x = shared(1) let tree: TestRenderer.ReactTestRenderer | null = null act(() => { tree = TestRenderer.create( - createElement(Driver, driverProps(view, [x], first)) + createElement(Driver, driverProps(view, [x], block(1))) ) }) frame() act(() => { tree!.update( - createElement(Driver, driverProps(view, [x, shared(2)], second)) + createElement(Driver, driverProps(view, [x, shared(20)], block(2))) ) }) frame() + expect(view.received).toEqual([[1], [1, 20]]) +}) + +test('a view that lost its binding to a recycle is bound again on the next frame', async () => { + const { Driver, frame } = await driverWithFrames() + const view = nativeView() + const x = shared(1) + act(() => { + TestRenderer.create(createElement(Driver, driverProps(view, [x], block(1)))) + }) + frame() + view.recycle() + x.value = 2 frame() - expect(calls).toEqual(['bind first', 'push', 'bind second', 'push', 'push']) + expect(view.received).toEqual([[1], [2]]) }) test('a native view without pushValues, or no mirror, still gets the slot array', async () => { const { Driver, frame } = await driverWithFrames() const pushed: number[][] = [] const older = { setValues: (values: number[]) => pushed.push(values.slice()) } + let first: TestRenderer.ReactTestRenderer | null = null act(() => { - TestRenderer.create( + first = TestRenderer.create( createElement(Driver, driverProps(older, [shared(3)], block(1))) ) }) frame() - const unmirrored = { ...older, bindValues: () => {}, pushValues: () => {} } + act(() => { + first!.unmount() + }) + const unmirrored = { ...nativeView(), setValues: older.setValues } act(() => { TestRenderer.create( createElement(Driver, driverProps(unmirrored, [shared(7)], null)) diff --git a/packages/react-native-tgfx/src/__tests__/scene.test.ts b/packages/react-native-tgfx/src/__tests__/scene.test.ts index 00b6e79b..3145c0f8 100644 --- a/packages/react-native-tgfx/src/__tests__/scene.test.ts +++ b/packages/react-native-tgfx/src/__tests__/scene.test.ts @@ -177,15 +177,9 @@ test('a scene runs its own mapper so handle writes land without a driver', () => expect(mappers.size).toBe(0) // no targets left, nothing to push to }) -test('a mirrored mapper binds each pushing target once; the rest get the array', () => { - const scene = Scene.create() - const box = createHandle() - scene.add(rect({ ref: box, x: 1, y: 2, width: 3, height: 4 })) - const { t: surface, calls: surfaceCalls } = target() - scene.attach(surface) - scene.flush() - // The test runner has no Nitro, so stand in for the mirror the container would make. - const values = new ArrayBuffer(4 * 8) +/** The test runner has no Nitro, so stand in for the mirror block the container would make. */ +function mirrorScene(scene: Scene, count: number): ArrayBuffer { + const values = new ArrayBuffer(count * 8) const flags = new ArrayBuffer(1) const internals = scene as unknown as { container: { mirror: unknown } @@ -200,29 +194,74 @@ test('a mirrored mapper binds each pushing target once; the rest get the array', boxed: { unbox: () => ({ flags, values }) }, }, } - const pushes: string[] = [] - const canvas: SceneTarget = { + return values +} + +/** A canvas as native behaves: a bind keeps the buffer, a push copies it now, and a recycle unbinds. */ +function canvasTarget() { + let bound: ArrayBuffer | null = null + const received: number[][] = [] + const t: SceneTarget = { setScene: () => {}, - setValues: () => pushes.push('setValues'), - bindValues: (buffer) => - pushes.push(buffer === values ? 'bind' : 'bind another'), - pushValues: () => pushes.push('push'), + setValues: () => {}, + bindValues: (buffer) => { + bound = buffer + }, + pushValues: () => { + if (bound == null) { + return false + } + received.push(Array.from(new Float64Array(bound))) + return true + }, } + return { t, received, recycle: () => (bound = null) } +} + +/** A handle scene on a surface (no push) and a canvas, its mapper running on the mirror. */ +function mirroredHandleScene() { + const scene = Scene.create() + const box = createHandle() + scene.add(rect({ ref: box, x: 1, y: 2, width: 3, height: 4 })) + const surface = target() + scene.attach(surface.t) + scene.flush() + const values = mirrorScene(scene, 4) + const canvas = canvasTarget() // Attaching recompiles with the same slots, so the mirror stays and the mapper restarts. - scene.attach(canvas) + scene.attach(canvas.t) expect(mappers.size).toBe(1) - const run = [...mappers.values()][0]! - const before = surfaceCalls.values.length + return { box, surface, canvas, values, run: [...mappers.values()][0]! } +} + +test('a mirrored mapper has a pushing target copy the block; the rest get the array', () => { + const { surface, canvas, values, run } = mirroredHandleScene() + const before = surface.calls.values.length run() run() - expect(pushes).toEqual(['bind', 'push', 'push']) - expect(surfaceCalls.values.slice(before)).toEqual([ + expect(canvas.received).toEqual([ + [1, 2, 3, 4], + [1, 2, 3, 4], + ]) + expect(surface.calls.values.slice(before)).toEqual([ [1, 2, 3, 4], [1, 2, 3, 4], ]) expect(Array.from(new Float64Array(values))).toEqual([1, 2, 3, 4]) }) +test('a target that lost its binding to a recycle is bound again on the next frame', () => { + const { box, canvas, run } = mirroredHandleScene() + run() + canvas.recycle() + box.set({ x: 9 }) + run() + expect(canvas.received).toEqual([ + [1, 2, 3, 4], + [9, 2, 3, 4], + ]) +}) + test('dispose stops the mapper', () => { const { t } = target() const scene = Scene.create() diff --git a/packages/react-native-tgfx/src/reanimated/AnimationDriver.tsx b/packages/react-native-tgfx/src/reanimated/AnimationDriver.tsx index 4ef5f797..8b9bfcb0 100644 --- a/packages/react-native-tgfx/src/reanimated/AnimationDriver.tsx +++ b/packages/react-native-tgfx/src/reanimated/AnimationDriver.tsx @@ -58,7 +58,11 @@ function Driver({ view, values, strings, gens, mirror }: DriverProps): null { view.bindValues(mirrored.buffer) bound[0] = true } - view.pushValues() + // A recycled view comes back unbound (iOS unmounts a hidden one), so bind again. + if (view.pushValues() === false) { + view.bindValues(mirrored.buffer) + view.pushValues() + } } else { view.setValues(slots) } diff --git a/packages/react-native-tgfx/src/scene.ts b/packages/react-native-tgfx/src/scene.ts index fe17ba54..ee3998c2 100644 --- a/packages/react-native-tgfx/src/scene.ts +++ b/packages/react-native-tgfx/src/scene.ts @@ -51,8 +51,8 @@ export interface SceneTarget { setStringValues?(indices: number[], values: string[]): void /** @internal bound once by the mapper, which then has the target push the mirror block. */ bindValues?(values: ArrayBuffer): void - /** @internal */ - pushValues?(): void + /** @internal false while nothing is bound, as after a recycle. */ + pushValues?(): boolean } interface Attachment { @@ -434,7 +434,11 @@ export class Scene { view.bindValues?.(mirrored.buffer) bound[t] = true } - view.pushValues() + // A recycled view comes back unbound, so bind again. + if (view.pushValues() === false) { + view.bindValues?.(mirrored.buffer) + view.pushValues() + } } else { view.setValues(slots) } diff --git a/packages/react-native-tgfx/src/specs/TgfxView.nitro.ts b/packages/react-native-tgfx/src/specs/TgfxView.nitro.ts index 0f0dbdbc..0f3db357 100644 --- a/packages/react-native-tgfx/src/specs/TgfxView.nitro.ts +++ b/packages/react-native-tgfx/src/specs/TgfxView.nitro.ts @@ -101,9 +101,10 @@ export interface TgfxViewMethods extends HybridViewMethods { /** * {@link setValues} with the bound block's current contents, copied * natively. The animation driver writes the block and calls this every - * frame, so no array is marshalled. Does nothing until a block is bound. + * frame, so no array is marshalled. False, and nothing pushed, while no + * block is bound: before the first bind and after a recycle. */ - pushValues(): void + pushValues(): boolean /** * Update the scene's string slots - live `text` and `path` content. Sparse: From 3e5157fd0256785a1dda6145297b8802f05a4d13 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Tue, 3 Nov 2026 21:17:40 +0100 Subject: [PATCH 5/5] docs: mark the native value push internal and fix stale comments bindValues and pushValues serve the animation driver and the Scene mapper and need a native-owned slot block that callers cannot create, so they are @internal and drop out of the API reference. Comments that named setValues as the per-frame push now say value push, and the roadmap entry for sending values through a shared ArrayBuffer is marked done. --- ROADMAP.md | 6 ++++-- .../cpp/Core/HybridTgfxSlotsFactory.hpp | 2 +- .../cpp/Render/TgfxRenderer.hpp | 2 +- packages/react-native-tgfx/src/imperative.ts | 4 ++-- .../src/reanimated/liveValue.ts | 2 +- .../src/specs/TgfxView.nitro.ts | 20 ++++++------------- 6 files changed, 15 insertions(+), 21 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 2a26ef82..09738a7f 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -184,8 +184,10 @@ Reducing per-op cost in tgfx's GL backend is where the remaining gap to - 🟠 Re-subscribe the `useAnimatedReaction` mapper only when the value *set* changes (currently re-subscribes on every commit due to new array identity). -- 🟠 Send values via a shared ArrayBuffer instead of a fresh `number[]` per frame - (the scene already travels this way; `setValues` still takes `number[]`). +- ✅ Values travel through a shared ArrayBuffer (#20): the `` driver and a + `Scene`'s mapper bind their mirror block once and copy it natively each frame, + so no `number[]` crosses JSI per frame. Still open: JS-thread `handle.set()` + writes and offscreen surfaces send `setValues(number[])`. - 🟡 Examples + tests for gesture-driven (gesture-handler is installed but unused), `withDecay`, `useDerivedValue`, `useClock`. - 🟡 Document that animated `rotate`/`skew` expect radians (`'45deg'` strings are diff --git a/packages/react-native-tgfx/cpp/Core/HybridTgfxSlotsFactory.hpp b/packages/react-native-tgfx/cpp/Core/HybridTgfxSlotsFactory.hpp index 1a422f52..bfa692b5 100644 --- a/packages/react-native-tgfx/cpp/Core/HybridTgfxSlotsFactory.hpp +++ b/packages/react-native-tgfx/cpp/Core/HybridTgfxSlotsFactory.hpp @@ -23,7 +23,7 @@ class HybridTgfxSlots : public HybridTgfxSlotsSpec { strings_(stringCount) {} std::shared_ptr getFlags() override { - // Borrowed, not owned: this object outlives every view, so a view must not free them. + // Borrowed, not owned: only JS typed-array views read the flags, and they hold this object. return ArrayBuffer::wrap(bytes_.data(), bytes_.size(), []() {}); } diff --git a/packages/react-native-tgfx/cpp/Render/TgfxRenderer.hpp b/packages/react-native-tgfx/cpp/Render/TgfxRenderer.hpp index 29f35fc6..0b647bcd 100644 --- a/packages/react-native-tgfx/cpp/Render/TgfxRenderer.hpp +++ b/packages/react-native-tgfx/cpp/Render/TgfxRenderer.hpp @@ -51,7 +51,7 @@ class TgfxRenderer { void setWindow(std::shared_ptr window); /// Stage a draw program. `words` is copied; `packedStrings` is a delta, see SceneStrings.hpp. - /// With `awaitValues` the program is not drawn until a setValues lands after it: its slot + /// With `awaitValues` the program is not drawn until a value push lands after it: its slot /// literals are placeholders that the driver's first push completes. void setScene(std::span words, const std::vector& packedStrings, diff --git a/packages/react-native-tgfx/src/imperative.ts b/packages/react-native-tgfx/src/imperative.ts index 9f6a9c5b..44497841 100644 --- a/packages/react-native-tgfx/src/imperative.ts +++ b/packages/react-native-tgfx/src/imperative.ts @@ -166,8 +166,8 @@ export interface NodeOwner { * box.set({ x: 120 }) * ``` * - * A prop the program keeps a value slot for is patched in place: one - * `setValues`, no re-encode, and it works **inside a worklet**, so a gesture can + * A prop the program keeps a value slot for is patched in place: one value + * push, no re-encode, and it works **inside a worklet**, so a gesture can * drive it without ever waking the JS thread. That covers the geometry props * (`x`, `y`, `width`, `height`, `cx`, `cy`, `r`, `rx`, `ry`, `x1`, `y1`, `x2`, * `y2`), `strokeWidth`, `strokeMiter`, `opacity`, `fontSize`, `visible`, diff --git a/packages/react-native-tgfx/src/reanimated/liveValue.ts b/packages/react-native-tgfx/src/reanimated/liveValue.ts index 02a61ccc..13b05d6e 100644 --- a/packages/react-native-tgfx/src/reanimated/liveValue.ts +++ b/packages/react-native-tgfx/src/reanimated/liveValue.ts @@ -154,7 +154,7 @@ export function isBlockSlot(v: unknown): boolean { /** * A value the compiled program can patch in place: it is written as a value slot - * exactly like an animated prop, so writing it again costs one `setValues` + * exactly like an animated prop, so writing it again costs one value push * instead of a re-encode. * * A real SharedValue whenever Reanimated is installed, because it has to be diff --git a/packages/react-native-tgfx/src/specs/TgfxView.nitro.ts b/packages/react-native-tgfx/src/specs/TgfxView.nitro.ts index 0f3db357..b4843882 100644 --- a/packages/react-native-tgfx/src/specs/TgfxView.nitro.ts +++ b/packages/react-native-tgfx/src/specs/TgfxView.nitro.ts @@ -77,7 +77,7 @@ export interface TgfxViewMethods extends HybridViewMethods { * The buffer is only valid for the duration of the call; the engine copies it. * * `awaitValues`: the program's slot literals are placeholders, so hold it - * until the next `setValues` lands and draw the two together. The driver + * until the next value push lands and draw the two together. The driver * pushes one frame later; the frames in between keep the previous program. */ setScene(scene: ArrayBuffer, strings: string[], awaitValues?: boolean): void @@ -86,24 +86,16 @@ export interface TgfxViewMethods extends HybridViewMethods { invalidate(): void /** - * Update the scene's `{ "__s": index }` value slots. Called every frame from a - * Reanimated worklet, so it runs natively on the UI thread. + * Replace the scene's value slots, in slot order. The animation driver + * copies its shared slot block natively instead; JS-thread handle writes + * and an older native binary use this. */ setValues(values: number[]): void - /** - * Point {@link pushValues} at a block of `Float64` value slots: the buffer - * behind a `TgfxSlots` `values` view, whose memory every runtime shares. The - * view holds the buffer until the next bind, so the caller can drop it. - */ + /** @internal points pushValues at a native-owned Float64 slot block (a TgfxSlots values buffer), held until the next bind. */ bindValues(values: ArrayBuffer): void - /** - * {@link setValues} with the bound block's current contents, copied - * natively. The animation driver writes the block and calls this every - * frame, so no array is marshalled. False, and nothing pushed, while no - * block is bound: before the first bind and after a recycle. - */ + /** @internal setValues from the bound block, copied natively; false while nothing is bound, as after a recycle. */ pushValues(): boolean /**