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