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 "device/vr/android/cardboard/cardboard_vr_device.h" | |
6 | |
7 #include <math.h> | |
8 #include <algorithm> | |
9 | |
10 #include "base/android/context_utils.h" | |
11 #include "base/android/jni_android.h" | |
12 #include "base/android/jni_array.h" | |
13 #include "base/android/jni_string.h" | |
14 #include "base/strings/string_util.h" | |
15 #include "base/strings/utf_string_conversions.h" | |
16 #include "base/time/time.h" | |
17 #include "base/trace_event/trace_event.h" | |
18 #include "jni/CardboardVRDevice_jni.h" | |
19 #include "ui/gfx/transform.h" | |
20 #include "ui/gfx/transform_util.h" | |
21 | |
22 using base::android::AttachCurrentThread; | |
23 using base::android::ScopedJavaLocalRef; | |
24 | |
25 namespace device { | |
26 | |
27 CardboardVRDevice::CardboardVRDevice(VRDeviceProvider* provider) | |
28 : VRDevice(provider) { | |
29 JNIEnv* env = AttachCurrentThread(); | |
30 j_cardboard_device_.Reset(Java_CardboardVRDevice_create( | |
31 env, base::android::GetApplicationContext())); | |
32 j_head_matrix_.Reset(env, env->NewFloatArray(16)); | |
33 } | |
34 | |
35 CardboardVRDevice::~CardboardVRDevice() { | |
36 Java_CardboardVRDevice_stopTracking(AttachCurrentThread(), | |
37 j_cardboard_device_.obj()); | |
38 } | |
39 | |
40 VRDisplayPtr CardboardVRDevice::GetVRDevice() { | |
41 TRACE_EVENT0("input", "CardboardVRDevice::GetVRDevice"); | |
42 VRDisplayPtr device = VRDisplay::New(); | |
43 | |
44 JNIEnv* env = AttachCurrentThread(); | |
45 | |
46 ScopedJavaLocalRef<jstring> j_device_name = | |
47 Java_CardboardVRDevice_getDeviceName(env, j_cardboard_device_.obj()); | |
48 device->displayName = | |
49 base::android::ConvertJavaStringToUTF8(env, j_device_name.obj()); | |
50 | |
51 ScopedJavaLocalRef<jfloatArray> j_fov(env, env->NewFloatArray(4)); | |
52 Java_CardboardVRDevice_getFieldOfView(env, j_cardboard_device_.obj(), | |
53 j_fov.obj()); | |
54 | |
55 std::vector<float> fov; | |
56 base::android::JavaFloatArrayToFloatVector(env, j_fov.obj(), &fov); | |
57 | |
58 device->capabilities = VRDisplayCapabilities::New(); | |
59 device->capabilities->hasOrientation = true; | |
60 device->capabilities->hasPosition = false; | |
61 device->capabilities->hasExternalDisplay = false; | |
62 device->capabilities->canPresent = false; | |
63 | |
64 device->leftEye = VREyeParameters::New(); | |
65 device->rightEye = VREyeParameters::New(); | |
66 VREyeParametersPtr& left_eye = device->leftEye; | |
67 VREyeParametersPtr& right_eye = device->rightEye; | |
68 | |
69 left_eye->fieldOfView = VRFieldOfView::New(); | |
70 left_eye->fieldOfView->upDegrees = fov[0]; | |
71 left_eye->fieldOfView->downDegrees = fov[1]; | |
72 left_eye->fieldOfView->leftDegrees = fov[2]; | |
73 left_eye->fieldOfView->rightDegrees = fov[3]; | |
74 | |
75 // Cardboard devices always assume a mirrored FOV, so this is just the left | |
76 // eye FOV with the left and right degrees swapped. | |
77 right_eye->fieldOfView = VRFieldOfView::New(); | |
78 right_eye->fieldOfView->upDegrees = fov[0]; | |
79 right_eye->fieldOfView->downDegrees = fov[1]; | |
80 right_eye->fieldOfView->leftDegrees = fov[3]; | |
81 right_eye->fieldOfView->rightDegrees = fov[2]; | |
82 | |
83 float ipd = Java_CardboardVRDevice_getIpd(env, j_cardboard_device_.obj()); | |
84 | |
85 left_eye->offset = mojo::Array<float>::New(3); | |
86 left_eye->offset[0] = ipd * -0.5f; | |
87 left_eye->offset[1] = 0.0f; | |
88 left_eye->offset[2] = 0.0f; | |
89 | |
90 right_eye->offset = mojo::Array<float>::New(3); | |
91 right_eye->offset[0] = ipd * 0.5f; | |
92 right_eye->offset[1] = 0.0f; | |
93 right_eye->offset[2] = 0.0f; | |
94 | |
95 ScopedJavaLocalRef<jintArray> j_screen_size(env, env->NewIntArray(2)); | |
96 Java_CardboardVRDevice_getScreenSize(env, j_cardboard_device_.obj(), | |
97 j_screen_size.obj()); | |
98 | |
99 std::vector<int> screen_size; | |
100 base::android::JavaIntArrayToIntVector(env, j_screen_size.obj(), | |
101 &screen_size); | |
102 | |
103 left_eye->renderWidth = screen_size[0] / 2.0; | |
104 left_eye->renderHeight = screen_size[1]; | |
105 | |
106 right_eye->renderWidth = screen_size[0] / 2.0; | |
107 right_eye->renderHeight = screen_size[1]; | |
108 | |
109 return device; | |
110 } | |
111 | |
112 VRPosePtr CardboardVRDevice::GetPose() { | |
113 TRACE_EVENT0("input", "CardboardVRDevice::GetSensorState"); | |
114 VRPosePtr pose = VRPose::New(); | |
115 | |
116 pose->timestamp = base::Time::Now().ToJsTime(); | |
117 | |
118 JNIEnv* env = AttachCurrentThread(); | |
119 Java_CardboardVRDevice_getSensorState(env, j_cardboard_device_.obj(), | |
120 j_head_matrix_.obj()); | |
121 | |
122 std::vector<float> head_matrix; | |
123 base::android::JavaFloatArrayToFloatVector(env, j_head_matrix_.obj(), | |
124 &head_matrix); | |
125 | |
126 gfx::Transform transform( | |
127 head_matrix[0], head_matrix[1], head_matrix[2], head_matrix[3], | |
128 head_matrix[4], head_matrix[5], head_matrix[6], head_matrix[7], | |
129 head_matrix[8], head_matrix[9], head_matrix[10], head_matrix[11], | |
130 head_matrix[12], head_matrix[13], head_matrix[14], head_matrix[15]); | |
131 | |
132 gfx::DecomposedTransform decomposed_transform; | |
133 gfx::DecomposeTransform(&decomposed_transform, transform); | |
134 | |
135 pose->orientation = mojo::Array<float>::New(4); | |
136 pose->orientation[0] = decomposed_transform.quaternion[0]; | |
137 pose->orientation[1] = decomposed_transform.quaternion[1]; | |
138 pose->orientation[2] = decomposed_transform.quaternion[2]; | |
139 pose->orientation[3] = decomposed_transform.quaternion[3]; | |
140 | |
141 pose->position = mojo::Array<float>::New(3); | |
142 pose->position[0] = decomposed_transform.translate[0]; | |
143 pose->position[1] = decomposed_transform.translate[1]; | |
144 pose->position[2] = decomposed_transform.translate[2]; | |
145 | |
146 return pose; | |
147 } | |
148 | |
149 void CardboardVRDevice::ResetPose() { | |
150 Java_CardboardVRDevice_resetSensor(AttachCurrentThread(), | |
151 j_cardboard_device_.obj()); | |
152 } | |
153 | |
154 } // namespace device | |
OLD | NEW |