-
Notifications
You must be signed in to change notification settings - Fork 215
/
native-lib.cpp
160 lines (137 loc) · 4.16 KB
/
native-lib.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <jni.h>
#include <string>
#include "BrowserWorld.h"
#include "DeviceDelegatePicoVR.h"
#include "VRBrowserPico.h"
#include "vrb/GLError.h"
#include "vrb/Logger.h"
#include "vrb/RunnableQueue.h"
static vrb::RunnableQueuePtr sQueue;
static crow::DeviceDelegatePicoVRPtr sDevice;
using namespace crow;
namespace {
bool gDestroyed = false;
}
#define JNI_METHOD(return_type, method_name) \
JNIEXPORT return_type JNICALL \
Java_org_mozilla_vrbrowser_PlatformActivity_##method_name
extern "C" {
JNI_METHOD(void, nativeOnCreate)
(JNIEnv* env, jobject) {
gDestroyed = false;
}
JNI_METHOD(void, nativeInitialize)
(JNIEnv* aEnv, jobject aActivity, jint width, jint height, jobject aAssetManager, jint type, jint focusInex) {
gDestroyed = false;
sQueue->AttachToThread();
VRBrowserPico::InitializeJava(aEnv, aActivity);
if (!sDevice) {
sDevice = crow::DeviceDelegatePicoVR::Create(BrowserWorld::Instance().GetRenderContext());
}
sDevice->SetType(type);
sDevice->SetFocused(focusInex);
sDevice->SetRenderSize(width, height);
BrowserWorld::Instance().RegisterDeviceDelegate(sDevice);
BrowserWorld::Instance().InitializeJava(aEnv, aActivity, aAssetManager);
BrowserWorld::Instance().InitializeGL();
BrowserWorld::Instance().Resume();
}
JNI_METHOD(void, nativeShutdown)
(JNIEnv*, jobject) {
BrowserWorld::Instance().ShutdownGL();
}
JNI_METHOD(void, nativeDestroy)
(JNIEnv*, jobject) {
gDestroyed = true;
sQueue->Clear();
BrowserWorld::Instance().ShutdownJava();
BrowserWorld::Instance().RegisterDeviceDelegate(nullptr);
sDevice = nullptr;
BrowserWorld::Destroy();
VRBrowserPico::ShutdownJava();
}
JNI_METHOD(void, nativePause)
(JNIEnv*, jobject) {
if (gDestroyed) {
return;
}
BrowserWorld::Instance().Pause();
}
JNI_METHOD(void, nativeResume)
(JNIEnv*, jobject) {
if (gDestroyed) {
return;
}
BrowserWorld::Instance().Resume();
}
JNI_METHOD(void, nativeStartFrame)
(JNIEnv*, jobject, jfloat ipd, jfloat fov, jfloat px, jfloat py, jfloat pz, jfloat qx, jfloat qy, jfloat qz, jfloat qw) {
if (gDestroyed) {
return;
}
sQueue->ProcessRunnables();
sDevice->UpdateIpd(ipd);
sDevice->UpdateFov(fov);
sDevice->UpdatePosition(vrb::Vector(px, py, pz));
sDevice->UpdateOrientation(vrb::Quaternion(qx, qy, qz, qw));
BrowserWorld::Instance().StartFrame();
}
JNI_METHOD(void, nativeDrawEye)
(JNIEnv*, jobject, jint eye) {
if (gDestroyed) {
return;
}
VRB_GL_CHECK(glEnable(GL_BLEND));
VRB_GL_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
BrowserWorld::Instance().Draw(eye == 0 ? device::Eye::Left : device::Eye::Right);
}
JNI_METHOD(void, nativeEndFrame)
(JNIEnv*, jobject) {
if (gDestroyed) {
return;
}
BrowserWorld::Instance().EndFrame();
}
JNI_METHOD(void, nativeSetFocusedController)
(JNIEnv*, jobject, jint index) {
if (gDestroyed) {
return;
}
sDevice->SetFocused(index);
}
JNI_METHOD(void, nativeUpdateControllerPose)
(JNIEnv*, jobject, jint index, jboolean dof6, jfloat px, jfloat py, jfloat pz, jfloat qx, jfloat qy, jfloat qz, jfloat qw) {
if (gDestroyed) {
return;
}
sDevice->UpdateControllerPose(index, dof6, vrb::Vector(px, py, pz),
vrb::Quaternion(qx, qy, qz, qw));
}
JNI_METHOD(void, nativeUpdateControllerState)
(JNIEnv*, jobject, jint index, jboolean connected, jint buttons, jfloat grip, jfloat axisX, jfloat axisY, jboolean touched) {
if (gDestroyed) {
return;
}
sDevice->UpdateControllerConnected(index, connected);
sDevice->UpdateControllerButtons(index, buttons, grip, axisX, axisY, touched);
}
JNI_METHOD(void, queueRunnable)
(JNIEnv* aEnv, jobject, jobject aRunnable) {
if (gDestroyed) {
return;
}
sQueue->AddRunnable(aEnv, aRunnable);
}
jint JNI_OnLoad(JavaVM* aVM, void*) {
sQueue = vrb::RunnableQueue::Create(aVM);
return JNI_VERSION_1_6;
}
void JNI_OnUnload(JavaVM*, void*) {
sDevice = nullptr;
sQueue = nullptr;
}
} // extern "C"