Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: content/browser/vr/cardboard/cardboard_vr_device.cc

Issue 829803003: Adding Chrome-side WebVR interface (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed the rest of sievers@ input Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/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/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "jni/CardboardVRDevice_jni.h"
17 #include "ui/base/base_window.h"
18
19 using base::android::AttachCurrentThread;
20
21 namespace content {
22
23 namespace {
24
25 void MatrixToOrientationQuat(const float m[16], blink::WebVRVector4& out) {
26 float fTrace = m[0] + m[5] + m[10];
27 float fRoot;
28 if ( fTrace > 0.0f ) {
29 fRoot = sqrtf(1.0f + fTrace) * 2.0f;
30 out.x = (m[9] - m[6]) / fRoot;
31 out.y = (m[2] - m[8]) / fRoot;
32 out.z = (m[4] - m[1]) / fRoot;
33 out.w = 0.25f * fRoot;
34 } else if ((m[0] > m[5]) && (m[0] > m[10])) {
35 fRoot = sqrtf(1.0f + m[0] - m[5] - m[10]) * 2.0f;
36 out.x = 0.25f * fRoot;
37 out.y = (m[1] + m[4]) / fRoot;
38 out.z = (m[2] + m[8]) / fRoot;
39 out.w = (m[9] - m[6]) / fRoot;
40 } else if (m[5] > m[10]) {
41 fRoot = sqrtf(1.0f + m[5] - m[0] - m[10]) * 2.0f;
42 out.x = (m[1] + m[4]) / fRoot;
43 out.y = 0.25f * fRoot;
44 out.z = (m[6] + m[9]) / fRoot;
45 out.w = (m[2] - m[8]) / fRoot;
46 } else {
47 fRoot = sqrtf(1.0f + m[10] - m[0] - m[5]) * 2.0f;
48 out.x = (m[2] + m[8]) / fRoot;
49 out.y = (m[6] + m[9]) / fRoot;
50 out.z = 0.25f * fRoot;
51 out.w = (m[4] - m[1]) / fRoot;
52 }
53 }
54
55 }
56
57 bool CardboardVRDevice::RegisterCardboardVRDevice(JNIEnv* env) {
58 return RegisterNativesImpl(env);
59 }
60
61 CardboardVRDevice::CardboardVRDevice(VRDeviceProvider* provider)
62 : VRDevice(provider)
63 , frame_index_(0)
64 {
65 j_cardboard_device_.Reset(
66 Java_CardboardVRDevice_create(
67 AttachCurrentThread(),
68 base::android::GetApplicationContext(),
69 reinterpret_cast<intptr_t>(this)));
70 }
71
72 CardboardVRDevice::~CardboardVRDevice() {
73 Java_CardboardVRDevice_stopTracking(AttachCurrentThread(),
74 j_cardboard_device_.obj());
75 }
76
77 void CardboardVRDevice::GetVRDevice(blink::WebVRDevice* device) {
78 memset(device, 0, sizeof(blink::WebVRDevice));
79
80 {
81 std::string tmp = base::StringPrintf("cardboard-%d", device->index);
82 base::TruncateUTF8ToByteSize(tmp, blink::WebVRDevice::deviceIdLengthCap-1,
no sievers 2015/03/26 19:55:14 nit: spaces around operator here and in line 85 an
83 &tmp);
84 base::string16 tmp16 = base::UTF8ToUTF16(tmp);
85 tmp16.copy(device->deviceId, blink::WebVRDevice::deviceIdLengthCap-1);
86 }
87
88 device->flags = blink::WebVRDeviceTypePosition | blink::WebVRDeviceTypeHMD;
89
90 JNIEnv* env = AttachCurrentThread();
91
92 {
93 ScopedJavaLocalRef<jstring> j_device_name;
94 j_device_name = Java_CardboardVRDevice_getDeviceName(
95 env,
96 j_cardboard_device_.obj());
97
98 base::string16 tmp16;
99 base::android::ConvertJavaStringToUTF16(env, j_device_name.obj(), &tmp16);
100 tmp16.copy(device->deviceName, blink::WebVRDevice::deviceNameLengthCap-1);
101 }
102
103 ScopedJavaLocalRef<jfloatArray> j_fov(env, env->NewFloatArray(4));
104 Java_CardboardVRDevice_getFieldOfView(env,
105 j_cardboard_device_.obj(),
106 j_fov.obj());
107
108 std::vector<float> fov;
109 base::android::JavaFloatArrayToFloatVector(env,
110 j_fov.obj(),
111 &fov);
112
113 blink::WebVRHMDInfo& hmdInfo = device->hmdInfo;
114
115 hmdInfo.leftEye.recommendedFieldOfView.upDegrees = fov[0];
116 hmdInfo.leftEye.recommendedFieldOfView.downDegrees = fov[1];
117 hmdInfo.leftEye.recommendedFieldOfView.leftDegrees = fov[2];
118 hmdInfo.leftEye.recommendedFieldOfView.rightDegrees = fov[3];
119
120 // Cardboard devices always assume a mirrored FOV, so this is just the left
121 // eye FOV with the left and right degrees swapped.
122 hmdInfo.rightEye.recommendedFieldOfView.upDegrees = fov[0];
123 hmdInfo.rightEye.recommendedFieldOfView.downDegrees = fov[1];
124 hmdInfo.rightEye.recommendedFieldOfView.leftDegrees = fov[3];
125 hmdInfo.rightEye.recommendedFieldOfView.rightDegrees = fov[2];
126
127 // Cardboard does not support configurable FOV.
128 hmdInfo.leftEye.maximumFieldOfView = hmdInfo.leftEye.recommendedFieldOfView;
129 hmdInfo.rightEye.maximumFieldOfView = hmdInfo.rightEye.recommendedFieldOfView;
130 hmdInfo.leftEye.minimumFieldOfView = hmdInfo.leftEye.recommendedFieldOfView;
131 hmdInfo.rightEye.minimumFieldOfView = hmdInfo.rightEye.recommendedFieldOfView;
132
133 float ipd = Java_CardboardVRDevice_getIPD(env,
134 j_cardboard_device_.obj());
135
136 hmdInfo.leftEye.eyeTranslation.x = ipd * -0.5f;
137 hmdInfo.leftEye.eyeTranslation.y = 0.0f;
138 hmdInfo.leftEye.eyeTranslation.z = 0.0f;
139
140 hmdInfo.rightEye.eyeTranslation.x = ipd * 0.5f;
141 hmdInfo.rightEye.eyeTranslation.y = 0.0f;
142 hmdInfo.rightEye.eyeTranslation.z = 0.0f;
143 }
144
145 void CardboardVRDevice::GetOrientationPosition(
146 blink::WebVRVector4& orientation,
147 blink::WebVRVector3& position) {
148 JNIEnv* env = AttachCurrentThread();
149 ScopedJavaLocalRef<jfloatArray> j_head_matrix(env, env->NewFloatArray(16));
150 Java_CardboardVRDevice_getSensorState(env,
151 j_cardboard_device_.obj(),
152 j_head_matrix.obj());
153
154 std::vector<float> head_matrix;
155 base::android::JavaFloatArrayToFloatVector(env,
156 j_head_matrix.obj(),
157 &head_matrix);
158
159 MatrixToOrientationQuat(&head_matrix[0], orientation);
160
161 position.x = -head_matrix[12];
162 position.y = head_matrix[13];
163 position.z = head_matrix[14];
164 }
165
166 void CardboardVRDevice::GetSensorState(blink::WebHMDSensorState* state) {
167 memset(state, 0, sizeof(blink::WebHMDSensorState));
168
169 state->timestamp = frame_index_;
170 state->frameIndex = frame_index_;
171 state->flags = blink::WebVRSensorStateOrientation |
172 blink::WebVRSensorStatePosition;
173
174 GetOrientationPosition(state->orientation, state->position);
175
176 frame_index_++;
177 }
178
179 void CardboardVRDevice::ResetSensor() {
180 Java_CardboardVRDevice_resetSensor(AttachCurrentThread(),
181 j_cardboard_device_.obj());
182 }
183
184 void CardboardVRDevice::GetRenderTargetRects(
185 blink::WebVRFieldOfView leftFov,
186 blink::WebVRFieldOfView rightFov,
187 blink::WebVRVector4* leftRect,
188 blink::WebVRVector4* rightRect) {
189 JNIEnv* env = AttachCurrentThread();
190 ScopedJavaLocalRef<jintArray> j_screen_size(env, env->NewIntArray(2));
191 Java_CardboardVRDevice_getScreenSize(env,
192 j_cardboard_device_.obj(),
193 j_screen_size.obj());
194
195 std::vector<int> screen_size;
196 base::android::JavaIntArrayToIntVector(env,
197 j_screen_size.obj(),
198 &screen_size);
199
200 leftRect->x = 0;
201 leftRect->y = 0;
202 leftRect->z = screen_size[0] / 2.0;
203 leftRect->w = screen_size[1];
204
205 rightRect->x = screen_size[0] / 2.0;
206 rightRect->y = 0;
207 rightRect->z = screen_size[0] / 2.0;
208 rightRect->w = screen_size[1];
209 }
210
211 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698