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