-
Notifications
You must be signed in to change notification settings - Fork 215
/
native-lib.cpp
291 lines (249 loc) · 8.61 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* -*- 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 <GLES2/gl2.h>
#include "BrowserWorld.h"
#include "vrb/Logger.h"
#include "vrb/GLError.h"
#include "BrowserEGLContext.h"
#include <android_native_app_glue.h>
#include <cstdlib>
#include <vrb/RunnableQueue.h>
#if defined(OCULUSVR)
#include "DeviceDelegateOculusVR.h"
#endif
#include <android/looper.h>
#include <unistd.h>
#include "VRBrowser.h"
#define JNI_METHOD(return_type, method_name) \
JNIEXPORT return_type JNICALL \
Java_org_mozilla_vrbrowser_PlatformActivity_##method_name
using namespace crow;
#if defined(OCULUSVR)
typedef DeviceDelegateOculusVR PlatformDeviceDelegate;
typedef DeviceDelegateOculusVRPtr PlatformDeviceDelegatePtr;
#endif
namespace {
jobject
GetAssetManager(JNIEnv *aEnv, jobject aActivity) {
jclass clazz = aEnv->GetObjectClass(aActivity);
jmethodID method = aEnv->GetMethodID(clazz, "getAssets", "()Landroid/content/res/AssetManager;");
jobject result = aEnv->CallObjectMethod(aActivity, method);
if (!result) {
VRB_ERROR("Failed to get AssetManager instance!");
}
return result;
}
struct AppContext {
vrb::RunnableQueuePtr mQueue;
BrowserEGLContextPtr mEgl;
PlatformDeviceDelegatePtr mDevice;
};
typedef std::shared_ptr<AppContext> AppContextPtr;
AppContextPtr sAppContext;
}
void
CommandCallback(android_app *aApp, int32_t aCmd) {
AppContext *ctx = (AppContext *) aApp->userData;
switch (aCmd) {
// A new ANativeWindow is ready for use. Upon receiving this command,
// android_app->window will contain the new window surface.
case APP_CMD_INIT_WINDOW:
VRB_LOG("APP_CMD_INIT_WINDOW %p", aApp->window);
if (!ctx->mEgl) {
ctx->mEgl = BrowserEGLContext::Create();
ctx->mEgl->Initialize(aApp->window);
ctx->mEgl->MakeCurrent();
VRB_GL_CHECK(glEnable(GL_DEPTH_TEST));
VRB_GL_CHECK(glEnable(GL_CULL_FACE));
BrowserWorld::Instance().InitializeGL();
} else {
ctx->mEgl->UpdateNativeWindow(aApp->window);
ctx->mEgl->MakeCurrent();
}
if (!BrowserWorld::Instance().IsPaused() && !ctx->mDevice->IsInVRMode()) {
ctx->mDevice->EnterVR(*ctx->mEgl);
}
break;
// The existing ANativeWindow needs to be terminated. Upon receiving this command,
// android_app->window still contains the existing window;
// after calling android_app_exec_cmd it will be set to NULL.
case APP_CMD_TERM_WINDOW:
VRB_LOG("APP_CMD_TERM_WINDOW");
if (ctx->mDevice->IsInVRMode()) {
ctx->mDevice->LeaveVR();
}
if (ctx->mEgl) {
ctx->mEgl->UpdateNativeWindow(nullptr);
}
break;
// The app's activity has been paused.
case APP_CMD_PAUSE:
VRB_LOG("APP_CMD_PAUSE");
BrowserWorld::Instance().Pause();
if (ctx->mDevice->IsInVRMode()) {
ctx->mDevice->LeaveVR();
}
break;
// The app's activity has been resumed.
case APP_CMD_RESUME:
VRB_LOG("APP_CMD_RESUME");
BrowserWorld::Instance().Resume();
if (!ctx->mDevice->IsInVRMode() && ctx->mEgl && ctx->mEgl->IsSurfaceReady() ) {
ctx->mDevice->EnterVR(*ctx->mEgl);
}
break;
// the app's activity is being destroyed,
// and waiting for the app thread to clean up and exit before proceeding.
case APP_CMD_DESTROY:
VRB_LOG("APP_CMD_DESTROY");
break;
default:
break;
}
}
#if defined(SNAPDRAGONVR)
int32_t
InputCallback(struct android_app *aApp, AInputEvent *aEvent) {
AppContext *ctx = (AppContext *) aApp->userData;
if (!ctx->mDevice) {
return 0;
}
const int type = AInputEvent_getType(aEvent);
if (type == AINPUT_EVENT_TYPE_KEY) {
const int keyCode = AKeyEvent_getKeyCode(aEvent);
const int action = AKeyEvent_getAction(aEvent);
if (action == AKEY_EVENT_ACTION_MULTIPLE) {
return 0;
}
if (action == AKEY_EVENT_ACTION_UP && (keyCode == AKEYCODE_DPAD_CENTER || keyCode == AKEYCODE_ENTER)) {
ctx->mDevice->UpdateButtonState(ControllerDelegate::BUTTON_TRIGGER, false);
return 1;
}
else if (action == AKEY_EVENT_ACTION_DOWN && (keyCode == AKEYCODE_DPAD_CENTER || keyCode == AKEYCODE_ENTER)) {
ctx->mDevice->UpdateButtonState(ControllerDelegate::BUTTON_TRIGGER, true);
return 1;
}
else if (action == AKEY_EVENT_ACTION_UP && keyCode == AKEYCODE_MENU) {
ctx->mDevice->UpdateButtonState(ControllerDelegate::BUTTON_APP, false);
return 1;
}
else if (action == AKEY_EVENT_ACTION_DOWN && keyCode == AKEYCODE_MENU) {
ctx->mDevice->UpdateButtonState(ControllerDelegate::BUTTON_APP, true);
return 1;
}
else if (keyCode == AKEYCODE_DPAD_LEFT) {
// Wheel moved: simulate scroll
ctx->mDevice->WheelScroll(-2.0);
return 1;
}
else if (keyCode == AKEYCODE_DPAD_RIGHT) {
// Wheel moved: simulate scroll
ctx->mDevice->WheelScroll(2.0);
return 1;
}
}
else if (type == AINPUT_EVENT_TYPE_MOTION) {
const int source = AInputEvent_getSource(aEvent);
// Disable trackball scroll for now because the scroll it's not very stable
/*if (source == AINPUT_SOURCE_TRACKBALL) {
const float x = AMotionEvent_getX(aEvent, 0);
const float y = AMotionEvent_getY(aEvent, 0);
ctx->mDevice->UpdateTrackpad(x, y);
}*/
}
return 0;
}
#endif // defined(SNAPDRAGONVR)
extern "C" {
void
android_main(android_app *aAppState) {
if (!ALooper_forThread()) {
ALooper_prepare(0);
}
// Attach JNI thread
JNIEnv *jniEnv;
(*aAppState->activity->vm).AttachCurrentThread(&jniEnv, nullptr);
sAppContext->mQueue->AttachToThread();
// Create Browser context
crow::VRBrowser::InitializeJava(jniEnv, aAppState->activity->clazz);
// Create device delegate
sAppContext->mDevice = PlatformDeviceDelegate::Create(BrowserWorld::Instance().GetRenderContext(),
aAppState);
BrowserWorld::Instance().RegisterDeviceDelegate(sAppContext->mDevice);
// Initialize java
auto assetManager = GetAssetManager(jniEnv, aAppState->activity->clazz);
BrowserWorld::Instance().InitializeJava(jniEnv, aAppState->activity->clazz, assetManager);
jniEnv->DeleteLocalRef(assetManager);
// Set up activity & SurfaceView life cycle callbacks
aAppState->userData = sAppContext.get();
aAppState->onAppCmd = CommandCallback;
#if defined(SNAPDRAGONVR)
aAppState->onInputEvent = InputCallback;
#endif
// Main render loop
while (true) {
int events;
android_poll_source *pSource;
// Loop until all events are read
// If the activity is paused use a blocking call to read events.
while (ALooper_pollAll(BrowserWorld::Instance().IsPaused() ? -1 : 0,
nullptr,
&events,
(void **) &pSource) >= 0) {
// Process event.
if (pSource) {
pSource->process(aAppState, pSource);
}
// Check if we are exiting.
if (aAppState->destroyRequested != 0) {
sAppContext->mEgl->MakeCurrent();
sAppContext->mQueue->ProcessRunnables();
sAppContext->mDevice->OnDestroy();
BrowserWorld::Instance().ShutdownGL();
BrowserWorld::Instance().ShutdownJava();
BrowserWorld::Destroy();
sAppContext->mEgl->Destroy();
sAppContext->mEgl.reset();
sAppContext->mDevice.reset();
aAppState->activity->vm->DetachCurrentThread();
return;
}
}
if (sAppContext->mEgl) {
sAppContext->mEgl->MakeCurrent();
}
sAppContext->mQueue->ProcessRunnables();
if (!BrowserWorld::Instance().IsPaused() && sAppContext->mDevice->IsInVRMode()) {
VRB_GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
BrowserWorld::Instance().Draw();
}
}
}
JNI_METHOD(void, queueRunnable)
(JNIEnv *aEnv, jobject, jobject aRunnable) {
if (sAppContext) {
sAppContext->mQueue->AddRunnable(aEnv, aRunnable);
} else {
VRB_ERROR("Failed to queue Runnable from UI thread. Render thread AppContext has not been initialized.")
}
}
JNI_METHOD(jboolean, platformExit)
(JNIEnv *, jobject) {
if (sAppContext && sAppContext->mDevice) {
return (jboolean) sAppContext->mDevice->ExitApp();
}
return (jboolean) false;
}
jint JNI_OnLoad(JavaVM* aVm, void*) {
sAppContext = std::make_shared<AppContext>();
sAppContext->mQueue = vrb::RunnableQueue::Create(aVm);
return JNI_VERSION_1_6;
}
void JNI_OnUnload(JavaVM* vm, void* reserved) {
sAppContext = nullptr;
}
} // extern "C"