| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/vr/android/gvr/gvr_device.h" | 5 #include "device/vr/android/gvr/gvr_device.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 | 9 |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 12 #include "device/vr/android/gvr/gvr_delegate.h" | 12 #include "device/vr/android/gvr/gvr_delegate.h" |
| 13 #include "device/vr/android/gvr/gvr_device_provider.h" | 13 #include "device/vr/android/gvr/gvr_device_provider.h" |
| 14 #include "device/vr/vr_device_manager.h" | 14 #include "device/vr/vr_device_manager.h" |
| 15 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr.h" | 15 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr.h" |
| 16 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr_types.h" | 16 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr_types.h" |
| 17 #include "ui/gfx/transform.h" | 17 #include "ui/gfx/transform.h" |
| 18 #include "ui/gfx/transform_util.h" | 18 #include "ui/gfx/transform_util.h" |
| 19 | 19 |
| 20 namespace device { | 20 namespace device { |
| 21 | 21 |
| 22 namespace { | |
| 23 | |
| 24 static const uint64_t kPredictionTimeWithoutVsyncNanos = 50000000; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 GvrDevice::GvrDevice(GvrDeviceProvider* provider, GvrDelegate* delegate) | 22 GvrDevice::GvrDevice(GvrDeviceProvider* provider, GvrDelegate* delegate) |
| 29 : VRDevice(), delegate_(delegate), gvr_provider_(provider) {} | 23 : VRDevice(), delegate_(delegate), gvr_provider_(provider) {} |
| 30 | 24 |
| 31 GvrDevice::~GvrDevice() {} | 25 GvrDevice::~GvrDevice() {} |
| 32 | 26 |
| 33 mojom::VRDisplayInfoPtr GvrDevice::GetVRDevice() { | 27 mojom::VRDisplayInfoPtr GvrDevice::GetVRDevice() { |
| 34 TRACE_EVENT0("input", "GvrDevice::GetVRDevice"); | 28 TRACE_EVENT0("input", "GvrDevice::GetVRDevice"); |
| 35 | 29 |
| 36 mojom::VRDisplayInfoPtr device = mojom::VRDisplayInfo::New(); | 30 mojom::VRDisplayInfoPtr device = mojom::VRDisplayInfo::New(); |
| 37 | 31 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 right_eye->offset[2] = -right_eye_mat.m[2][3]; | 137 right_eye->offset[2] = -right_eye_mat.m[2][3]; |
| 144 | 138 |
| 145 if (delegate_) { | 139 if (delegate_) { |
| 146 delegate_->SetWebVRRenderSurfaceSize(2 * left_eye->renderWidth, | 140 delegate_->SetWebVRRenderSurfaceSize(2 * left_eye->renderWidth, |
| 147 left_eye->renderHeight); | 141 left_eye->renderHeight); |
| 148 } | 142 } |
| 149 | 143 |
| 150 return device; | 144 return device; |
| 151 } | 145 } |
| 152 | 146 |
| 153 mojom::VRPosePtr GvrDevice::GetPose() { | |
| 154 TRACE_EVENT0("input", "GvrDevice::GetSensorState"); | |
| 155 | |
| 156 mojom::VRPosePtr pose = mojom::VRPose::New(); | |
| 157 | |
| 158 pose->timestamp = base::Time::Now().ToJsTime(); | |
| 159 | |
| 160 // Increment pose frame counter always, even if it's a faked pose. | |
| 161 pose->poseIndex = ++pose_index_; | |
| 162 | |
| 163 pose->orientation.emplace(4); | |
| 164 | |
| 165 gvr::GvrApi* gvr_api = GetGvrApi(); | |
| 166 if (!gvr_api) { | |
| 167 // If we don't have a GvrApi instance return a static forward orientation. | |
| 168 pose->orientation.value()[0] = 0.0; | |
| 169 pose->orientation.value()[1] = 0.0; | |
| 170 pose->orientation.value()[2] = 0.0; | |
| 171 pose->orientation.value()[3] = 1.0; | |
| 172 | |
| 173 return pose; | |
| 174 } | |
| 175 | |
| 176 if (!delegate_) | |
| 177 return nullptr; | |
| 178 | |
| 179 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); | |
| 180 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; | |
| 181 | |
| 182 gvr::Mat4f head_mat = | |
| 183 gvr_api->GetHeadSpaceFromStartSpaceRotation(target_time); | |
| 184 head_mat = gvr_api->ApplyNeckModel(head_mat, 1.0f); | |
| 185 | |
| 186 gfx::Transform inv_transform( | |
| 187 head_mat.m[0][0], head_mat.m[0][1], head_mat.m[0][2], head_mat.m[0][3], | |
| 188 head_mat.m[1][0], head_mat.m[1][1], head_mat.m[1][2], head_mat.m[1][3], | |
| 189 head_mat.m[2][0], head_mat.m[2][1], head_mat.m[2][2], head_mat.m[2][3], | |
| 190 head_mat.m[3][0], head_mat.m[3][1], head_mat.m[3][2], head_mat.m[3][3]); | |
| 191 | |
| 192 gfx::Transform transform; | |
| 193 if (inv_transform.GetInverse(&transform)) { | |
| 194 gfx::DecomposedTransform decomposed_transform; | |
| 195 gfx::DecomposeTransform(&decomposed_transform, transform); | |
| 196 | |
| 197 pose->orientation.value()[0] = decomposed_transform.quaternion[0]; | |
| 198 pose->orientation.value()[1] = decomposed_transform.quaternion[1]; | |
| 199 pose->orientation.value()[2] = decomposed_transform.quaternion[2]; | |
| 200 pose->orientation.value()[3] = decomposed_transform.quaternion[3]; | |
| 201 | |
| 202 pose->position.emplace(3); | |
| 203 pose->position.value()[0] = decomposed_transform.translate[0]; | |
| 204 pose->position.value()[1] = decomposed_transform.translate[1]; | |
| 205 pose->position.value()[2] = decomposed_transform.translate[2]; | |
| 206 } | |
| 207 | |
| 208 // Save the underlying GVR pose for use by rendering. It can't use a | |
| 209 // VRPosePtr since that's a different data type. | |
| 210 delegate_->SetGvrPoseForWebVr(head_mat, pose_index_); | |
| 211 | |
| 212 return pose; | |
| 213 } | |
| 214 | |
| 215 void GvrDevice::ResetPose() { | 147 void GvrDevice::ResetPose() { |
| 216 gvr::GvrApi* gvr_api = GetGvrApi(); | 148 gvr::GvrApi* gvr_api = GetGvrApi(); |
| 217 | 149 |
| 218 // Should never call RecenterTracking when using with Daydream viewers. On | 150 // Should never call RecenterTracking when using with Daydream viewers. On |
| 219 // those devices recentering should only be done via the controller. | 151 // those devices recentering should only be done via the controller. |
| 220 if (gvr_api && gvr_api->GetViewerType() == GVR_VIEWER_TYPE_CARDBOARD) | 152 if (gvr_api && gvr_api->GetViewerType() == GVR_VIEWER_TYPE_CARDBOARD) |
| 221 gvr_api->RecenterTracking(); | 153 gvr_api->RecenterTracking(); |
| 222 } | 154 } |
| 223 | 155 |
| 224 void GvrDevice::RequestPresent(const base::Callback<void(bool)>& callback) { | 156 void GvrDevice::RequestPresent(const base::Callback<void(bool)>& callback) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 254 | 186 |
| 255 gvr::Rectf right_gvr_bounds; | 187 gvr::Rectf right_gvr_bounds; |
| 256 right_gvr_bounds.left = right_bounds->left; | 188 right_gvr_bounds.left = right_bounds->left; |
| 257 right_gvr_bounds.top = 1.0f - right_bounds->top; | 189 right_gvr_bounds.top = 1.0f - right_bounds->top; |
| 258 right_gvr_bounds.right = right_bounds->left + right_bounds->width; | 190 right_gvr_bounds.right = right_bounds->left + right_bounds->width; |
| 259 right_gvr_bounds.bottom = 1.0f - (right_bounds->top + right_bounds->height); | 191 right_gvr_bounds.bottom = 1.0f - (right_bounds->top + right_bounds->height); |
| 260 | 192 |
| 261 delegate_->UpdateWebVRTextureBounds(left_gvr_bounds, right_gvr_bounds); | 193 delegate_->UpdateWebVRTextureBounds(left_gvr_bounds, right_gvr_bounds); |
| 262 } | 194 } |
| 263 | 195 |
| 196 void GvrDevice::GetVRVSyncProvider(mojom::VRVSyncProviderRequest request) { |
| 197 if (delegate_) |
| 198 delegate_->OnVRVsyncProviderRequest(std::move(request)); |
| 199 } |
| 200 |
| 264 void GvrDevice::SetDelegate(GvrDelegate* delegate) { | 201 void GvrDevice::SetDelegate(GvrDelegate* delegate) { |
| 265 delegate_ = delegate; | 202 delegate_ = delegate; |
| 266 | 203 |
| 267 // Notify the clients that this device has changed | 204 // Notify the clients that this device has changed |
| 268 if (delegate_) { | 205 if (delegate_) { |
| 269 delegate_->SetWebVRSecureOrigin(secure_origin_); | 206 delegate_->SetWebVRSecureOrigin(secure_origin_); |
| 270 OnChanged(); | 207 OnChanged(); |
| 271 } | 208 } |
| 272 } | 209 } |
| 273 | 210 |
| 274 gvr::GvrApi* GvrDevice::GetGvrApi() { | 211 gvr::GvrApi* GvrDevice::GetGvrApi() { |
| 275 if (!delegate_) | 212 if (!delegate_) |
| 276 return nullptr; | 213 return nullptr; |
| 277 | 214 |
| 278 return delegate_->gvr_api(); | 215 return delegate_->gvr_api(); |
| 279 } | 216 } |
| 280 | 217 |
| 281 } // namespace device | 218 } // namespace device |
| OLD | NEW |