-
Notifications
You must be signed in to change notification settings - Fork 596
Expand file tree
/
Copy pathJniWebGPUView.cpp
More file actions
95 lines (85 loc) · 3.12 KB
/
JniWebGPUView.cpp
File metadata and controls
95 lines (85 loc) · 3.12 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
#include <jni.h>
#include <string.h>
#include <android/native_window_jni.h>
#include <android/api-level.h>
#ifdef SK_GRAPHITE
#include "webgpu/webgpu_cpp.h"
#include "rnwgpu/SurfaceRegistry.h"
#include "rnskia/RNDawnContext.h"
#endif
#if __ANDROID_API__ >= 28
#include <android/data_space.h>
static void applyColorSpace(JNIEnv *env, ANativeWindow *window,
jstring jColorSpace) {
int32_t dataSpace = ADATASPACE_SRGB;
if (jColorSpace != nullptr) {
const char *cs = env->GetStringUTFChars(jColorSpace, nullptr);
if (strcmp(cs, "display-p3") == 0) {
dataSpace = ADATASPACE_DISPLAY_P3;
} else if (strcmp(cs, "bt2020-hlg") == 0) {
dataSpace = ADATASPACE_BT2020_HLG;
} else if (strcmp(cs, "bt2020-pq") == 0) {
dataSpace = ADATASPACE_BT2020_PQ;
}
env->ReleaseStringUTFChars(jColorSpace, cs);
}
ANativeWindow_setBuffersDataSpace(window, dataSpace);
}
#endif
extern "C" JNIEXPORT void JNICALL
Java_com_shopify_reactnative_skia_WebGPUView_onSurfaceCreate(
JNIEnv *env, jobject thiz, jobject jSurface, jint contextId, jfloat width,
jfloat height, jstring jColorSpace) {
#ifdef SK_GRAPHITE
auto window = ANativeWindow_fromSurface(env, jSurface);
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
auto &dawnContext = RNSkia::DawnContext::getInstance();
auto gpu = dawnContext.getWGPUInstance();
#if __ANDROID_API__ >= 28
// Set color space on the native window
applyColorSpace(env, window, jColorSpace);
#endif
// Create surface from ANativeWindow
wgpu::SurfaceSourceAndroidNativeWindow androidSurfaceDesc;
androidSurfaceDesc.window = window;
wgpu::SurfaceDescriptor surfaceDescriptor;
surfaceDescriptor.nextInChain = &androidSurfaceDesc;
auto surface = gpu.CreateSurface(&surfaceDescriptor);
registry
.getSurfaceInfoOrCreate(contextId, gpu, static_cast<int>(width),
static_cast<int>(height))
->switchToOnscreen(window, surface);
#endif
}
extern "C" JNIEXPORT void JNICALL
Java_com_shopify_reactnative_skia_WebGPUView_onSurfaceChanged(
JNIEnv *env, jobject thiz, jobject surface, jint contextId, jfloat width,
jfloat height) {
#ifdef SK_GRAPHITE
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
auto surfaceInfo = registry.getSurfaceInfo(contextId);
if (surfaceInfo) {
surfaceInfo->resize(static_cast<int>(width), static_cast<int>(height));
}
#endif
}
extern "C" JNIEXPORT void JNICALL
Java_com_shopify_reactnative_skia_WebGPUView_switchToOffscreenSurface(
JNIEnv *env, jobject thiz, jint contextId) {
#ifdef SK_GRAPHITE
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
auto surfaceInfo = registry.getSurfaceInfo(contextId);
if (surfaceInfo) {
surfaceInfo->switchToOffscreen();
}
#endif
}
extern "C" JNIEXPORT void JNICALL
Java_com_shopify_reactnative_skia_WebGPUView_onSurfaceDestroy(JNIEnv *env,
jobject thiz,
jint contextId) {
#ifdef SK_GRAPHITE
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
registry.removeSurfaceInfo(contextId);
#endif
}