OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/vr/android/cardboard/cardboard_vr_device.h" | |
6 | |
7 #include <math.h> | |
8 #include <algorithm> | |
9 | |
10 #include "base/android/jni_android.h" | |
11 #include "base/android/jni_array.h" | |
12 #include "base/android/jni_string.h" | |
13 #include "base/strings/string_util.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "base/time/time.h" | |
16 #include "base/trace_event/trace_event.h" | |
17 #include "content/browser/vr/vr_transform_util.h" | |
18 #include "jni/CardboardVRDevice_jni.h" | |
19 | |
20 using base::android::AttachCurrentThread; | |
21 | |
22 namespace content { | |
23 | |
24 bool CardboardVRDevice::RegisterCardboardVRDevice(JNIEnv* env) { | |
25 return RegisterNativesImpl(env); | |
26 } | |
27 | |
28 CardboardVRDevice::CardboardVRDevice(VRDeviceProvider* provider) | |
29 : VRDevice(provider), frame_index_(0) { | |
30 JNIEnv* env = AttachCurrentThread(); | |
31 j_cardboard_device_.Reset(Java_CardboardVRDevice_create( | |
32 env, base::android::GetApplicationContext())); | |
33 j_head_matrix_.Reset(env, env->NewFloatArray(16)); | |
34 } | |
35 | |
36 CardboardVRDevice::~CardboardVRDevice() { | |
37 Java_CardboardVRDevice_stopTracking(AttachCurrentThread(), | |
38 j_cardboard_device_.obj()); | |
39 } | |
40 | |
41 VRDeviceInfoPtr CardboardVRDevice::GetVRDevice() { | |
42 TRACE_EVENT0("input", "CardboardVRDevice::GetVRDevice"); | |
43 VRDeviceInfoPtr device = VRDeviceInfo::New(); | |
44 | |
45 JNIEnv* env = AttachCurrentThread(); | |
46 | |
47 ScopedJavaLocalRef<jstring> j_device_name = | |
48 Java_CardboardVRDevice_getDeviceName(env, j_cardboard_device_.obj()); | |
49 device->deviceName = | |
50 base::android::ConvertJavaStringToUTF8(env, j_device_name.obj()); | |
51 | |
52 ScopedJavaLocalRef<jfloatArray> j_fov(env, env->NewFloatArray(4)); | |
53 Java_CardboardVRDevice_getFieldOfView(env, j_cardboard_device_.obj(), | |
54 j_fov.obj()); | |
55 | |
56 std::vector<float> fov; | |
57 base::android::JavaFloatArrayToFloatVector(env, j_fov.obj(), &fov); | |
58 | |
59 device->hmdInfo = VRHMDInfo::New(); | |
60 VRHMDInfoPtr& hmdInfo = device->hmdInfo; | |
61 | |
62 hmdInfo->leftEye = VREyeParameters::New(); | |
63 hmdInfo->rightEye = VREyeParameters::New(); | |
64 VREyeParametersPtr& left_eye = hmdInfo->leftEye; | |
65 VREyeParametersPtr& right_eye = hmdInfo->rightEye; | |
66 | |
67 left_eye->recommendedFieldOfView = VRFieldOfView::New(); | |
68 left_eye->recommendedFieldOfView->upDegrees = fov[0]; | |
69 left_eye->recommendedFieldOfView->downDegrees = fov[1]; | |
70 left_eye->recommendedFieldOfView->leftDegrees = fov[2]; | |
71 left_eye->recommendedFieldOfView->rightDegrees = fov[3]; | |
72 | |
73 // Cardboard devices always assume a mirrored FOV, so this is just the left | |
74 // eye FOV with the left and right degrees swapped. | |
75 right_eye->recommendedFieldOfView = VRFieldOfView::New(); | |
76 right_eye->recommendedFieldOfView->upDegrees = fov[0]; | |
77 right_eye->recommendedFieldOfView->downDegrees = fov[1]; | |
78 right_eye->recommendedFieldOfView->leftDegrees = fov[3]; | |
79 right_eye->recommendedFieldOfView->rightDegrees = fov[2]; | |
80 | |
81 // Cardboard does not support configurable FOV. | |
82 left_eye->maximumFieldOfView = left_eye->recommendedFieldOfView.Clone(); | |
83 right_eye->maximumFieldOfView = right_eye->recommendedFieldOfView.Clone(); | |
84 left_eye->minimumFieldOfView = left_eye->recommendedFieldOfView.Clone(); | |
85 right_eye->minimumFieldOfView = right_eye->recommendedFieldOfView.Clone(); | |
86 | |
87 float ipd = Java_CardboardVRDevice_getIpd(env, j_cardboard_device_.obj()); | |
88 | |
89 left_eye->eyeTranslation = VRVector3::New(); | |
90 left_eye->eyeTranslation->x = ipd * -0.5f; | |
91 left_eye->eyeTranslation->y = 0.0f; | |
92 left_eye->eyeTranslation->z = 0.0f; | |
93 | |
94 right_eye->eyeTranslation = VRVector3::New(); | |
95 right_eye->eyeTranslation->x = ipd * 0.5f; | |
96 right_eye->eyeTranslation->y = 0.0f; | |
97 right_eye->eyeTranslation->z = 0.0f; | |
98 | |
99 ScopedJavaLocalRef<jintArray> j_screen_size(env, env->NewIntArray(2)); | |
100 Java_CardboardVRDevice_getScreenSize(env, j_cardboard_device_.obj(), | |
101 j_screen_size.obj()); | |
102 | |
103 std::vector<int> screen_size; | |
104 base::android::JavaIntArrayToIntVector(env, j_screen_size.obj(), | |
105 &screen_size); | |
106 | |
107 left_eye->renderRect = VRRect::New(); | |
108 left_eye->renderRect->x = 0; | |
109 left_eye->renderRect->y = 0; | |
110 left_eye->renderRect->width = screen_size[0] / 2.0; | |
111 left_eye->renderRect->height = screen_size[1]; | |
112 | |
113 right_eye->renderRect = VRRect::New(); | |
114 right_eye->renderRect->x = screen_size[0] / 2.0; | |
115 right_eye->renderRect->y = 0; | |
116 right_eye->renderRect->width = screen_size[0] / 2.0; | |
117 right_eye->renderRect->height = screen_size[1]; | |
118 | |
119 return device.Pass(); | |
120 } | |
121 | |
122 VRSensorStatePtr CardboardVRDevice::GetSensorState() { | |
123 TRACE_EVENT0("input", "CardboardVRDevice::GetSensorState"); | |
124 VRSensorStatePtr state = VRSensorState::New(); | |
125 | |
126 state->timestamp = base::Time::Now().ToJsTime(); | |
127 state->frameIndex = frame_index_++; | |
128 | |
129 JNIEnv* env = AttachCurrentThread(); | |
130 Java_CardboardVRDevice_getSensorState(env, j_cardboard_device_.obj(), | |
131 j_head_matrix_.obj()); | |
132 | |
133 std::vector<float> head_matrix; | |
134 base::android::JavaFloatArrayToFloatVector(env, j_head_matrix_.obj(), | |
135 &head_matrix); | |
136 | |
137 state->orientation = MatrixToOrientationQuaternion( | |
138 &head_matrix[0], &head_matrix[4], &head_matrix[8]); | |
139 | |
140 state->position = VRVector3::New(); | |
141 state->position->x = -head_matrix[12]; | |
142 state->position->y = head_matrix[13]; | |
143 state->position->z = head_matrix[14]; | |
144 | |
145 return state.Pass(); | |
146 } | |
147 | |
148 void CardboardVRDevice::ResetSensor() { | |
149 Java_CardboardVRDevice_resetSensor(AttachCurrentThread(), | |
150 j_cardboard_device_.obj()); | |
151 } | |
152 | |
153 } // namespace content | |
OLD | NEW |