-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creates `Choreographer`, a platform specific instance that fires `onFrame` callbacks at the screen's refresh rate. The `Choreographer` instance runs on the Thread that created it. We could create such instances on the UI Thread or any other arbitrary Thread using either react-native-reanimated or react-native-worklets-core. Currently you can create a `Choreographer` using the `FilamentProxy`: ```ts const choreographer = FilamentProxy.createChoreographer() choreographer.addOnFrameCallback((timestamp: number) => { console.log(`onFrame at ${timestamp}`) }) choreographer.start() setTimeout(() => { choreographer.stop() }, 1500) ``` Create on the UI Thread: ```ts runOnUI(() => { 'worklet' const choreographer = FilamentProxy.createChoreographer() choreographer.addOnFrameCallback((timestamp: number) => { 'worklet' // TODO: This is currently not possible since we don't have a Worklets integration. This func cannot be called probably. // Reanimated did work on something pretty interesting where they abstract all of this on the JS side, meaning there's // no change required from our end - we just receive a jsi::Function as normal - but it's not yet public. // So to make that work, we need to integrate react-native-reanimated or react-native-worklets-core in RNF for now. }) })() ``` Note: You need to keep a strong reference on the `Choreographer`, otherwise it might get deleted by GC.
- Loading branch information
Showing
31 changed files
with
418 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
package/android/src/main/cpp/java-bindings/JChoreographer.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// Created by Marc Rousavy on 23.02.24. | ||
// | ||
|
||
#include "JChoreographer.h" | ||
#include <android/choreographer.h> | ||
|
||
namespace margelo { | ||
|
||
void JChoreographer::registerNatives() { | ||
registerHybrid({ | ||
makeNativeMethod("initHybrid", JChoreographer::initHybrid), | ||
makeNativeMethod("onFrame", JChoreographer::onFrameLong), | ||
}); | ||
} | ||
|
||
JChoreographer::JChoreographer(const jni::alias_ref<jhybridobject>& javaThis) : _javaPart(jni::make_global(javaThis)) {} | ||
|
||
jni::local_ref<JChoreographer::jhybriddata> JChoreographer::initHybrid(jni::alias_ref<jhybridobject> javaThis) { | ||
return makeCxxInstance(javaThis); | ||
} | ||
|
||
void JChoreographer::onFrameLong(jlong timestamp) { | ||
onFrame(static_cast<double>(timestamp)); | ||
} | ||
|
||
void JChoreographer::start() { | ||
static const auto method = javaClassLocal()->getMethod<void()>("start"); | ||
method(_javaPart); | ||
} | ||
|
||
void JChoreographer::stop() { | ||
static const auto method = javaClassLocal()->getMethod<void()>("stop"); | ||
method(_javaPart); | ||
} | ||
|
||
} // namespace margelo |
33 changes: 33 additions & 0 deletions
33
package/android/src/main/cpp/java-bindings/JChoreographer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// Created by Marc Rousavy on 23.02.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "Choreographer.h" | ||
#include <fbjni/fbjni.h> | ||
|
||
namespace margelo { | ||
|
||
using namespace facebook; | ||
|
||
class JChoreographer : public jni::HybridClass<JChoreographer>, public Choreographer { | ||
public: | ||
static void registerNatives(); | ||
|
||
void start() override; | ||
void stop() override; | ||
void onFrameLong(jlong timestamp); | ||
|
||
private: | ||
friend HybridBase; | ||
jni::global_ref<JChoreographer::javaobject> _javaPart; | ||
static auto constexpr TAG = "JChoreographer"; | ||
static auto constexpr kJavaDescriptor = "Lcom/margelo/filament/FilamentChoreographer;"; | ||
|
||
private: | ||
explicit JChoreographer(const jni::alias_ref<jhybridobject>& javaThis); | ||
static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject> javaThis); | ||
}; | ||
|
||
} // namespace margelo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
package/android/src/main/java/com/margelo/filament/FilamentChoreographer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.margelo.filament; | ||
|
||
import android.view.Choreographer; | ||
|
||
import androidx.annotation.Keep; | ||
|
||
import com.facebook.jni.HybridData; | ||
import com.facebook.proguard.annotations.DoNotStrip; | ||
|
||
/** @noinspection JavaJniMissingFunction*/ | ||
public class FilamentChoreographer { | ||
/** @noinspection unused, FieldCanBeLocal */ | ||
@DoNotStrip | ||
@Keep | ||
private final HybridData mHybridData; | ||
private final Choreographer choreographer; | ||
private boolean isRunning; | ||
|
||
public FilamentChoreographer() { | ||
mHybridData = initHybrid(); | ||
choreographer = Choreographer.getInstance(); | ||
} | ||
|
||
private void onFrameCallback(long timestamp) { | ||
if (!isRunning) return; | ||
onFrame(timestamp); | ||
choreographer.postFrameCallback(this::onFrameCallback); | ||
} | ||
|
||
/** @noinspection unused */ | ||
@DoNotStrip | ||
@Keep | ||
private synchronized void start() { | ||
if (!isRunning) { | ||
isRunning = true; | ||
choreographer.postFrameCallback(this::onFrameCallback); | ||
} | ||
} | ||
|
||
/** @noinspection unused */ | ||
@DoNotStrip | ||
@Keep | ||
private synchronized void stop() { | ||
if (isRunning) { | ||
isRunning = false; | ||
choreographer.removeFrameCallback(this::onFrameCallback); | ||
} | ||
} | ||
|
||
private native HybridData initHybrid(); | ||
private native void onFrame(long timestamp); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// Created by Marc Rousavy on 23.02.24. | ||
// | ||
|
||
#include "Choreographer.h" | ||
|
||
namespace margelo { | ||
|
||
void Choreographer::loadHybridMethods() { | ||
registerHybridMethod("addOnFrameListener", &Choreographer::addOnFrameListener, this); | ||
registerHybridMethod("start", &Choreographer::start, this); | ||
registerHybridMethod("stop", &Choreographer::stop, this); | ||
} | ||
|
||
std::shared_ptr<Listener> Choreographer::addOnFrameListener(Choreographer::OnFrameCallback onFrameCallback) { | ||
_callbacks.push_back(std::move(onFrameCallback)); | ||
return std::make_shared<Listener>([]() { | ||
// TODO: Find a safe way to remove this listener from the vector. | ||
}); | ||
} | ||
|
||
void Choreographer::onFrame(double timestamp) { | ||
for (const auto& callback : _callbacks) { | ||
callback(timestamp); | ||
} | ||
} | ||
|
||
} // namespace margelo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// Created by Marc Rousavy on 23.02.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "Listener.h" | ||
#include "jsi/HybridObject.h" | ||
#include <functional> | ||
|
||
namespace margelo { | ||
|
||
class Choreographer : public HybridObject { | ||
public: | ||
using OnFrameCallback = std::function<void(double timestamp)>; | ||
|
||
std::shared_ptr<Listener> addOnFrameListener(OnFrameCallback onFrameCallback); | ||
|
||
virtual void start() = 0; | ||
virtual void stop() = 0; | ||
|
||
protected: | ||
void onFrame(double timestamp); | ||
|
||
void loadHybridMethods() override; | ||
|
||
private: | ||
std::vector<OnFrameCallback> _callbacks; | ||
}; | ||
|
||
} // namespace margelo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.