forked from wcandillon/react-native-webgpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp-adapter.cpp
More file actions
112 lines (100 loc) · 4.09 KB
/
cpp-adapter.cpp
File metadata and controls
112 lines (100 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <memory>
#include <unordered_map>
#include <fbjni/fbjni.h>
#include <jni.h>
#include <jsi/jsi.h>
#include <ReactCommon/CallInvokerHolder.h>
#include <android/native_window_jni.h>
#include <webgpu/webgpu_cpp.h>
#include "AndroidPlatformContext.h"
#include "GPUCanvasContext.h"
#include "RNWebGPUManager.h"
#define LOG_TAG "WebGPUModule"
std::shared_ptr<rnwgpu::RNWebGPUManager> manager;
extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUModule_initializeNative(
JNIEnv *env, jobject /* this */, jlong jsRuntime,
jobject jsCallInvokerHolder, jobject blobModule,
jobjectArray enableToggles, jobjectArray disableToggles) {
auto runtime = reinterpret_cast<facebook::jsi::Runtime *>(jsRuntime);
jobject globalBlobModule = env->NewGlobalRef(blobModule);
auto jsCallInvoker{
facebook::jni::alias_ref<facebook::react::CallInvokerHolder::javaobject>{
reinterpret_cast<facebook::react::CallInvokerHolder::javaobject>(
jsCallInvokerHolder)} -> cthis()
->getCallInvoker()};
auto platformContext =
std::make_shared<rnwgpu::AndroidPlatformContext>(globalBlobModule);
// Convert Java string arrays to std::vector<std::string>
std::vector<std::string> enableVec;
std::vector<std::string> disableVec;
if (enableToggles != nullptr) {
jsize len = env->GetArrayLength(enableToggles);
for (jsize i = 0; i < len; i++) {
auto jstr = (jstring)env->GetObjectArrayElement(enableToggles, i);
if (jstr == nullptr) {
continue;
}
const char *cstr = env->GetStringUTFChars(jstr, nullptr);
if (cstr == nullptr) {
env->DeleteLocalRef(jstr);
continue;
}
enableVec.emplace_back(cstr);
env->ReleaseStringUTFChars(jstr, cstr);
env->DeleteLocalRef(jstr);
}
}
if (disableToggles != nullptr) {
jsize len = env->GetArrayLength(disableToggles);
for (jsize i = 0; i < len; i++) {
auto jstr = (jstring)env->GetObjectArrayElement(disableToggles, i);
if (jstr == nullptr) {
continue;
}
const char *cstr = env->GetStringUTFChars(jstr, nullptr);
if (cstr == nullptr) {
env->DeleteLocalRef(jstr);
continue;
}
disableVec.emplace_back(cstr);
env->ReleaseStringUTFChars(jstr, cstr);
env->DeleteLocalRef(jstr);
}
}
manager = std::make_shared<rnwgpu::RNWebGPUManager>(
runtime, jsCallInvoker, platformContext,
std::move(enableVec), std::move(disableVec));
}
extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUView_onSurfaceChanged(
JNIEnv *env, jobject thiz, jobject surface, jint contextId, jfloat width,
jfloat height) {
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
registry.getSurfaceInfo(contextId)->resize(static_cast<int>(width),
static_cast<int>(height));
}
extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUView_onSurfaceCreate(
JNIEnv *env, jobject thiz, jobject jSurface, jint contextId, jfloat width,
jfloat height) {
auto window = ANativeWindow_fromSurface(env, jSurface);
// ANativeWindow_acquire(window);
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
auto gpu = manager->_gpu;
auto surface = manager->_platformContext->makeSurface(
gpu, window, static_cast<int>(width), static_cast<int>(height));
registry
.getSurfaceInfoOrCreate(contextId, gpu, static_cast<int>(width),
static_cast<int>(height))
->switchToOnscreen(window, surface);
}
extern "C" JNIEXPORT void JNICALL
Java_com_webgpu_WebGPUView_switchToOffscreenSurface(JNIEnv *env, jobject thiz,
jint contextId) {
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
auto nativeSurface = registry.getSurfaceInfo(contextId)->switchToOffscreen();
// ANativeWindow_release(reinterpret_cast<ANativeWindow *>(nativeSurface));
}
extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUView_onSurfaceDestroy(
JNIEnv *env, jobject thiz, jint contextId) {
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
registry.removeSurfaceInfo(contextId);
}