OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/vr_shell/non_presenting_gvr_delegate.h" |
| 6 |
| 7 #include "chrome/browser/android/vr_shell/vr_shell.h" |
| 8 |
| 9 namespace vr_shell { |
| 10 |
| 11 namespace { |
| 12 static constexpr long kPredictionTimeWithoutVsyncNanos = 50000000; |
| 13 } // namespace |
| 14 |
| 15 NonPresentingGvrDelegate::NonPresentingGvrDelegate(long context) |
| 16 : task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 17 binding_(this), |
| 18 weak_ptr_factory_(this) { |
| 19 gvr_api_ = gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(context)); |
| 20 } |
| 21 |
| 22 NonPresentingGvrDelegate::~NonPresentingGvrDelegate() { |
| 23 StopVSyncLoop(); |
| 24 } |
| 25 |
| 26 void NonPresentingGvrDelegate::OnVRVsyncProviderRequest( |
| 27 device::mojom::VRVSyncProviderRequest request) { |
| 28 binding_.Close(); |
| 29 binding_.Bind(std::move(request)); |
| 30 binding_.set_connection_error_handler( |
| 31 base::Bind(&NonPresentingGvrDelegate::StopVSyncLoop, |
| 32 weak_ptr_factory_.GetWeakPtr())); |
| 33 StartVSyncLoop(); |
| 34 } |
| 35 |
| 36 void NonPresentingGvrDelegate::Pause() { |
| 37 vsync_task_.Cancel(); |
| 38 vsync_paused_ = true; |
| 39 gvr_api_->PauseTracking(); |
| 40 } |
| 41 |
| 42 void NonPresentingGvrDelegate::Resume() { |
| 43 if (!vsync_paused_) |
| 44 return; |
| 45 vsync_paused_ = false; |
| 46 StartVSyncLoop(); |
| 47 } |
| 48 |
| 49 device::mojom::VRVSyncProviderRequest |
| 50 NonPresentingGvrDelegate::OnSwitchToPresentingDelegate() { |
| 51 StopVSyncLoop(); |
| 52 if (binding_.is_bound()) |
| 53 return binding_.Unbind(); |
| 54 return nullptr; |
| 55 } |
| 56 |
| 57 void NonPresentingGvrDelegate::StopVSyncLoop() { |
| 58 vsync_task_.Cancel(); |
| 59 if (!callback_.is_null()) |
| 60 callback_.Run(nullptr, base::TimeDelta()); |
| 61 callback_.Reset(); |
| 62 gvr_api_->PauseTracking(); |
| 63 // If the loop is stopped, it's not considered to be paused. |
| 64 vsync_paused_ = false; |
| 65 } |
| 66 |
| 67 void NonPresentingGvrDelegate::StartVSyncLoop() { |
| 68 vsync_task_.Reset( |
| 69 base::Bind(&NonPresentingGvrDelegate::OnVSync, base::Unretained(this))); |
| 70 gvr_api_->RefreshViewerProfile(); |
| 71 gvr_api_->ResumeTracking(); |
| 72 OnVSync(); |
| 73 } |
| 74 |
| 75 void NonPresentingGvrDelegate::OnVSync() { |
| 76 base::TimeTicks now = base::TimeTicks::Now(); |
| 77 base::TimeTicks target; |
| 78 |
| 79 // Don't run the VSync loop if we're not bound. |
| 80 if (!binding_.is_bound()) { |
| 81 return; |
| 82 } |
| 83 |
| 84 // Don't send VSyncs until we have a timebase/interval. |
| 85 if (vsync_interval_.is_zero()) |
| 86 return; |
| 87 target = now + vsync_interval_; |
| 88 int64_t intervals = (target - vsync_timebase_) / vsync_interval_; |
| 89 target = vsync_timebase_ + intervals * vsync_interval_; |
| 90 if (!vsync_task_.IsCancelled()) { |
| 91 task_runner_->PostDelayedTask(FROM_HERE, vsync_task_.callback(), |
| 92 target - now); |
| 93 } |
| 94 |
| 95 base::TimeDelta time = intervals * vsync_interval_; |
| 96 if (!callback_.is_null()) { |
| 97 callback_.Run(GetPose(), time); |
| 98 callback_.Reset(); |
| 99 } else { |
| 100 pending_vsync_ = true; |
| 101 pending_time_ = time; |
| 102 } |
| 103 } |
| 104 |
| 105 void NonPresentingGvrDelegate::GetVSync(const GetVSyncCallback& callback) { |
| 106 if (!pending_vsync_) { |
| 107 if (!callback_.is_null()) { |
| 108 mojo::ReportBadMessage("Requested VSync before waiting for response to " |
| 109 "previous request."); |
| 110 return; |
| 111 } |
| 112 callback_ = callback; |
| 113 return; |
| 114 } |
| 115 pending_vsync_ = false; |
| 116 callback.Run(GetPose(), pending_time_); |
| 117 } |
| 118 |
| 119 void NonPresentingGvrDelegate::UpdateVSyncInterval(long timebase_nanos, |
| 120 double interval_seconds) { |
| 121 vsync_timebase_ = base::TimeTicks(); |
| 122 vsync_timebase_ += base::TimeDelta::FromMicroseconds(timebase_nanos / 1000); |
| 123 vsync_interval_ = base::TimeDelta::FromSecondsD(interval_seconds); |
| 124 StartVSyncLoop(); |
| 125 } |
| 126 |
| 127 device::mojom::VRPosePtr NonPresentingGvrDelegate::GetPose() { |
| 128 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); |
| 129 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; |
| 130 |
| 131 gvr::Mat4f head_mat = |
| 132 gvr_api_->GetHeadSpaceFromStartSpaceRotation(target_time); |
| 133 head_mat = gvr_api_->ApplyNeckModel(head_mat, 1.0f); |
| 134 |
| 135 uint32_t pose_index = pose_index_++; |
| 136 |
| 137 return VrShell::VRPosePtrFromGvrPose(head_mat, pose_index); |
| 138 } |
| 139 |
| 140 } // namespace vr_shell |
OLD | NEW |