-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
211 additions
and
10 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
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
// | ||
// 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("start", JChoreographer::start), | ||
makeNativeMethod("stop", JChoreographer::stop), | ||
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
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
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 final Choreographer.FrameCallback frameCallback; | ||
private boolean isRunning; | ||
|
||
public FilamentChoreographer() { | ||
mHybridData = initHybrid(); | ||
choreographer = Choreographer.getInstance(); | ||
frameCallback = timestamp -> { | ||
if (!isRunning) return; | ||
onFrame(timestamp); | ||
}; | ||
} | ||
|
||
private synchronized void start() { | ||
if (!isRunning) { | ||
isRunning = true; | ||
choreographer.postFrameCallback(frameCallback); | ||
} | ||
} | ||
|
||
private synchronized void stop() { | ||
if (isRunning) { | ||
isRunning = false; | ||
choreographer.removeFrameCallback(frameCallback); | ||
} | ||
} | ||
|
||
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,26 @@ | ||
// | ||
// Created by Marc Rousavy on 23.02.24. | ||
// | ||
|
||
#include "Choreographer.h" | ||
|
||
namespace margelo { | ||
|
||
void Choreographer::loadHybridMethods() { | ||
registerHybridMethod("setOnFrameCallback", &Choreographer::addOnFrameListener, 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