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 "chrome/browser/android/vr_shell/non_presenting_gvr_delegate.h" | 5 #include "chrome/browser/android/vr_shell/non_presenting_gvr_delegate.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
10 #include "chrome/browser/android/vr_shell/vr_shell.h" | 10 #include "chrome/browser/android/vr_shell/vr_shell.h" |
11 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr.h" | 11 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr.h" |
12 | 12 |
13 namespace vr_shell { | 13 namespace vr_shell { |
14 | 14 |
15 namespace { | 15 namespace { |
16 static constexpr int64_t kPredictionTimeWithoutVsyncNanos = 50000000; | 16 static constexpr int64_t kPredictionTimeWithoutVsyncNanos = 50000000; |
17 } // namespace | 17 } // namespace |
18 | 18 |
19 NonPresentingGvrDelegate::NonPresentingGvrDelegate(gvr_context* context) | 19 NonPresentingGvrDelegate::NonPresentingGvrDelegate(gvr_context* context) |
20 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | 20 : task_runner_(base::ThreadTaskRunnerHandle::Get()), |
21 binding_(this), | 21 binding_(this), |
22 weak_ptr_factory_(this) { | 22 weak_ptr_factory_(this) { |
23 // Context may be null, see VrShellDelegate#createNonPresentingNativeContext | 23 gvr_api_ = gvr::GvrApi::WrapNonOwned(context); |
24 // for possible reasons a context could fail to be created. For example, | |
25 // the user might uninstall apps or clear data after VR support was detected. | |
26 if (context) | |
27 gvr_api_ = gvr::GvrApi::WrapNonOwned(context); | |
28 } | 24 } |
29 | 25 |
30 NonPresentingGvrDelegate::~NonPresentingGvrDelegate() { | 26 NonPresentingGvrDelegate::~NonPresentingGvrDelegate() { |
31 StopVSyncLoop(); | 27 StopVSyncLoop(); |
32 } | 28 } |
33 | 29 |
34 void NonPresentingGvrDelegate::OnVRVsyncProviderRequest( | 30 void NonPresentingGvrDelegate::OnVRVsyncProviderRequest( |
35 device::mojom::VRVSyncProviderRequest request) { | 31 device::mojom::VRVSyncProviderRequest request) { |
36 binding_.Close(); | 32 binding_.Close(); |
37 binding_.Bind(std::move(request)); | 33 binding_.Bind(std::move(request)); |
38 binding_.set_connection_error_handler( | 34 binding_.set_connection_error_handler( |
39 base::Bind(&NonPresentingGvrDelegate::StopVSyncLoop, | 35 base::Bind(&NonPresentingGvrDelegate::StopVSyncLoop, |
40 weak_ptr_factory_.GetWeakPtr())); | 36 weak_ptr_factory_.GetWeakPtr())); |
41 StartVSyncLoop(); | 37 StartVSyncLoop(); |
42 } | 38 } |
43 | 39 |
44 void NonPresentingGvrDelegate::Pause() { | 40 void NonPresentingGvrDelegate::Pause() { |
45 vsync_task_.Cancel(); | 41 vsync_task_.Cancel(); |
46 vsync_paused_ = true; | 42 vsync_paused_ = true; |
47 if (gvr_api_) | 43 gvr_api_->PauseTracking(); |
48 gvr_api_->PauseTracking(); | |
49 } | 44 } |
50 | 45 |
51 void NonPresentingGvrDelegate::Resume() { | 46 void NonPresentingGvrDelegate::Resume() { |
52 if (!vsync_paused_) | 47 if (!vsync_paused_) |
53 return; | 48 return; |
54 vsync_paused_ = false; | 49 vsync_paused_ = false; |
55 StartVSyncLoop(); | 50 StartVSyncLoop(); |
56 } | 51 } |
57 | 52 |
58 device::mojom::VRVSyncProviderRequest | 53 device::mojom::VRVSyncProviderRequest |
59 NonPresentingGvrDelegate::OnSwitchToPresentingDelegate() { | 54 NonPresentingGvrDelegate::OnSwitchToPresentingDelegate() { |
60 StopVSyncLoop(); | 55 StopVSyncLoop(); |
61 if (binding_.is_bound()) | 56 if (binding_.is_bound()) |
62 return binding_.Unbind(); | 57 return binding_.Unbind(); |
63 return nullptr; | 58 return nullptr; |
64 } | 59 } |
65 | 60 |
66 void NonPresentingGvrDelegate::StopVSyncLoop() { | 61 void NonPresentingGvrDelegate::StopVSyncLoop() { |
67 vsync_task_.Cancel(); | 62 vsync_task_.Cancel(); |
68 binding_.Close(); | |
69 if (!callback_.is_null()) { | 63 if (!callback_.is_null()) { |
70 base::ResetAndReturn(&callback_) | 64 base::ResetAndReturn(&callback_) |
71 .Run(nullptr, base::TimeDelta(), -1, | 65 .Run(nullptr, base::TimeDelta(), -1, |
72 device::mojom::VRVSyncProvider::Status::CLOSING); | 66 device::mojom::VRVSyncProvider::Status::RETRY); |
73 } | 67 } |
74 if (gvr_api_) | 68 gvr_api_->PauseTracking(); |
75 gvr_api_->PauseTracking(); | |
76 // If the loop is stopped, it's not considered to be paused. | 69 // If the loop is stopped, it's not considered to be paused. |
77 vsync_paused_ = false; | 70 vsync_paused_ = false; |
78 } | 71 } |
79 | 72 |
80 void NonPresentingGvrDelegate::StartVSyncLoop() { | 73 void NonPresentingGvrDelegate::StartVSyncLoop() { |
81 vsync_task_.Reset( | 74 vsync_task_.Reset( |
82 base::Bind(&NonPresentingGvrDelegate::OnVSync, base::Unretained(this))); | 75 base::Bind(&NonPresentingGvrDelegate::OnVSync, base::Unretained(this))); |
83 if (gvr_api_) { | 76 gvr_api_->RefreshViewerProfile(); |
84 gvr_api_->RefreshViewerProfile(); | 77 gvr_api_->ResumeTracking(); |
85 gvr_api_->ResumeTracking(); | |
86 } | |
87 OnVSync(); | 78 OnVSync(); |
88 } | 79 } |
89 | 80 |
90 void NonPresentingGvrDelegate::OnVSync() { | 81 void NonPresentingGvrDelegate::OnVSync() { |
91 base::TimeTicks now = base::TimeTicks::Now(); | 82 base::TimeTicks now = base::TimeTicks::Now(); |
92 base::TimeTicks target; | 83 base::TimeTicks target; |
93 | 84 |
94 // Don't run the VSync loop if we're not bound. | 85 // Don't run the VSync loop if we're not bound. |
95 if (!binding_.is_bound()) { | 86 if (!binding_.is_bound()) { |
96 return; | 87 return; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 vsync_timebase_ += base::TimeDelta::FromMicroseconds(timebase_nanos / 1000); | 129 vsync_timebase_ += base::TimeDelta::FromMicroseconds(timebase_nanos / 1000); |
139 vsync_interval_ = base::TimeDelta::FromSecondsD(interval_seconds); | 130 vsync_interval_ = base::TimeDelta::FromSecondsD(interval_seconds); |
140 StartVSyncLoop(); | 131 StartVSyncLoop(); |
141 } | 132 } |
142 | 133 |
143 void NonPresentingGvrDelegate::SendVSync(base::TimeDelta time, | 134 void NonPresentingGvrDelegate::SendVSync(base::TimeDelta time, |
144 const GetVSyncCallback& callback) { | 135 const GetVSyncCallback& callback) { |
145 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); | 136 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); |
146 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; | 137 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; |
147 | 138 |
148 if (!gvr_api_) { | |
149 callback.Run(device::mojom::VRPosePtr(nullptr), time, -1, | |
150 device::mojom::VRVSyncProvider::Status::SUCCESS); | |
151 return; | |
152 } | |
153 | |
154 gvr::Mat4f head_mat = gvr_api_->ApplyNeckModel( | 139 gvr::Mat4f head_mat = gvr_api_->ApplyNeckModel( |
155 gvr_api_->GetHeadSpaceFromStartSpaceRotation(target_time), 1.0f); | 140 gvr_api_->GetHeadSpaceFromStartSpaceRotation(target_time), 1.0f); |
156 callback.Run(VrShell::VRPosePtrFromGvrPose(head_mat), time, -1, | 141 callback.Run(VrShell::VRPosePtrFromGvrPose(head_mat), time, -1, |
157 device::mojom::VRVSyncProvider::Status::SUCCESS); | 142 device::mojom::VRVSyncProvider::Status::SUCCESS); |
158 } | 143 } |
159 | 144 |
160 bool NonPresentingGvrDelegate::SupportsPresentation() { | 145 bool NonPresentingGvrDelegate::SupportsPresentation() { |
161 return false; | 146 return false; |
162 } | 147 } |
163 | 148 |
164 void NonPresentingGvrDelegate::ResetPose() { | 149 void NonPresentingGvrDelegate::ResetPose() { |
165 // Should never call RecenterTracking when using with Daydream viewers. On | 150 // Should never call RecenterTracking when using with Daydream viewers. On |
166 // those devices recentering should only be done via the controller. | 151 // those devices recentering should only be done via the controller. |
167 if (gvr_api_ && gvr_api_->GetViewerType() == GVR_VIEWER_TYPE_CARDBOARD) | 152 if (gvr_api_ && gvr_api_->GetViewerType() == GVR_VIEWER_TYPE_CARDBOARD) |
168 gvr_api_->RecenterTracking(); | 153 gvr_api_->RecenterTracking(); |
169 } | 154 } |
170 | 155 |
171 void NonPresentingGvrDelegate::CreateVRDisplayInfo( | 156 void NonPresentingGvrDelegate::CreateVRDisplayInfo( |
172 const base::Callback<void(device::mojom::VRDisplayInfoPtr)>& callback, | 157 const base::Callback<void(device::mojom::VRDisplayInfoPtr)>& callback, |
173 uint32_t device_id) { | 158 uint32_t device_id) { |
174 if (!gvr_api_) { | |
175 callback.Run(device::mojom::VRDisplayInfoPtr(nullptr)); | |
176 return; | |
177 } | |
178 | |
179 callback.Run(VrShell::CreateVRDisplayInfo( | 159 callback.Run(VrShell::CreateVRDisplayInfo( |
180 gvr_api_.get(), device::kInvalidRenderTargetSize, device_id)); | 160 gvr_api_.get(), device::kInvalidRenderTargetSize, device_id)); |
181 } | 161 } |
182 | 162 |
183 } // namespace vr_shell | 163 } // namespace vr_shell |
OLD | NEW |