This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
/
service.rs
219 lines (187 loc) · 6.64 KB
/
service.rs
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
#![cfg(feature = "googlevr")]
use {VRService, VRDisplay, VRDisplayPtr, VREvent, VRGamepadPtr};
use super::display::{GoogleVRDisplay, GoogleVRDisplayPtr};
#[cfg(target_os="android")]
use rust_webvr_api::jni_utils::JNIScope;
#[cfg(target_os="android")]
use android_injected_glue::ffi as ndk;
use gvr_sys as gvr;
use std::mem;
use std::ptr;
#[cfg(target_os="android")]
const SERVICE_CLASS_NAME:&'static str = "com/rust/webvr/GVRService";
pub struct GoogleVRService {
ctx: *mut gvr::gvr_context,
controller_ctx: *mut gvr::gvr_controller_context,
display: Option<GoogleVRDisplayPtr>,
#[cfg(target_os="android")]
pub java_object: ndk::jobject,
#[cfg(target_os="android")]
pub java_class: ndk::jclass,
}
unsafe impl Send for GoogleVRService {}
impl VRService for GoogleVRService {
fn initialize(&mut self) -> Result<(), String> {
if self.is_initialized() {
return Ok(());
}
unsafe {
try!(self.create_context());
self.create_controller_context();
}
if self.ctx.is_null() {
return Err("GoogleVR SDK failed to initialize".into());
}
Ok(())
}
fn fetch_displays(&mut self) -> Result<Vec<VRDisplayPtr>,String> {
let display = self.init_display()?;
Ok(vec![display.clone()])
}
fn fetch_gamepads(&mut self) -> Result<Vec<VRGamepadPtr>,String> {
let display = self.init_display()?;
display.borrow_mut().fetch_gamepads()
}
fn is_available(&self) -> bool {
true
}
fn poll_events(&self) -> Vec<VREvent> {
let mut events = Vec::new();
if let Some(ref display) = self.display {
let mut d = display.borrow_mut();
d.poll_events(&mut events);
if let Some(ref gp) = d.gamepad() {
gp.borrow_mut().handle_events();
}
}
events
}
}
impl GoogleVRService {
#[cfg(target_os="android")]
pub fn new() -> GoogleVRService {
GoogleVRService {
ctx: ptr::null_mut(),
controller_ctx: ptr::null_mut(),
display: None,
java_object: ptr::null_mut(),
java_class: ptr::null_mut()
}
}
#[cfg(not(target_os="android"))]
pub fn new() -> GoogleVRService {
GoogleVRService {
ctx: ptr::null_mut(),
controller_ctx: ptr::null_mut(),
display: None,
}
}
// On Android, the gvr_context must be be obtained from
// the Java GvrLayout object via GvrLayout.getGvrApi().getNativeGvrContext()
// Java code is implemented in GVRService. It handles the life cycle of the GvrLayout.
// JNI code is used to comunicate with that Java code.
#[cfg(target_os="android")]
unsafe fn create_context(&mut self) -> Result<(), String> {
let jni_scope = try!(JNIScope::attach());
let jni = jni_scope.jni();
let env = jni_scope.env;
// Use NativeActivity's classloader to find our class
self.java_class = try!(jni_scope.find_class(SERVICE_CLASS_NAME));
if self.java_class.is_null() {
return Err("Didn't find GVRService class".into());
};
self.java_class = (jni.NewGlobalRef)(env, self.java_class);
// Create GVRService instance and own it as a globalRef.
let method = jni_scope.get_method(self.java_class, "create", "(Landroid/app/Activity;J)Ljava/lang/Object;", true);
let thiz: usize = mem::transmute(self as * mut GoogleVRService);
self.java_object = (jni.CallStaticObjectMethod)(env, self.java_class, method, jni_scope.activity, thiz as ndk::jlong);
if self.java_object.is_null() {
return Err("Failed to create GVRService instance".into());
};
self.java_object = (jni.NewGlobalRef)(env, self.java_object);
// Finally we have everything required to get the gvr_context pointer from java :)
let method = jni_scope.get_method(self.java_class, "getNativeContext", "()J", false);
let pointer = (jni.CallLongMethod)(env, self.java_object, method);
self.ctx = pointer as *mut gvr::gvr_context;
if self.ctx.is_null() {
return Err("Failed to getNativeGvrContext from java GvrLayout".into());
}
Ok(())
}
#[cfg(not(target_os="android"))]
unsafe fn create_context(&mut self) -> Result<(), String> {
self.ctx = gvr::gvr_create();
Ok(())
}
unsafe fn create_controller_context(&mut self) {
let options = gvr::gvr_controller_get_default_options();
self.controller_ctx = gvr::gvr_controller_create_and_init(options, self.ctx);
gvr::gvr_controller_resume(self.controller_ctx);
}
fn is_initialized(&self) -> bool {
return !self.ctx.is_null();
}
fn init_display(&mut self) -> Result<&GoogleVRDisplayPtr, String> {
self.initialize()?;
if let Some(ref d) = self.display {
Ok(d)
} else {
self.display = unsafe { Some(GoogleVRDisplay::new(self, self.ctx, self.controller_ctx)) };
Ok(self.display.as_ref().unwrap())
}
}
// Called from Java main thread
// Pause & resume methods are thread safe
#[cfg(target_os="android")]
fn on_pause(&mut self) {
if let Some(ref display) = self.display {
unsafe {
(*display.as_ptr()).pause();
}
}
}
// Called from Java main thread
// Pause & resume methods are thread safe
#[cfg(target_os="android")]
fn on_resume(&mut self) {
if let Some(ref display) = self.display {
unsafe {
(*display.as_ptr()).resume();
}
}
}
}
impl Drop for GoogleVRService {
fn drop(&mut self) {
if !self.controller_ctx.is_null() {
unsafe {
gvr::gvr_controller_destroy(mem::transmute(&self.ctx));
}
}
if !self.ctx.is_null() {
unsafe {
gvr::gvr_destroy(mem::transmute(&self.ctx));
}
}
}
}
#[cfg(target_os="android")]
#[no_mangle]
#[allow(non_snake_case)]
#[allow(dead_code)]
pub extern fn Java_com_rust_webvr_GVRService_nativeOnPause(_: *mut ndk::JNIEnv, service: ndk::jlong) {
unsafe {
let service: *mut GoogleVRService = mem::transmute(service as usize);
(*service).on_pause();
}
}
#[cfg(target_os="android")]
#[no_mangle]
#[allow(non_snake_case)]
#[allow(dead_code)]
pub extern fn Java_com_rust_webvr_GVRService_nativeOnResume(_: *mut ndk::JNIEnv, service: ndk::jlong) {
unsafe {
let service: *mut GoogleVRService = mem::transmute(service as usize);
(*service).on_resume();
}
}