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 paused_ = true; |
| 39 gvr_api_->PauseTracking(); |
| 40 } |
| 41 |
| 42 void NonPresentingGvrDelegate::Resume() { |
| 43 if (!paused_) |
| 44 return; |
| 45 paused_ = false; |
| 46 StartVSyncLoop(); |
| 47 } |
| 48 |
| 49 void NonPresentingGvrDelegate::OnSwitchToPresentingDelegate() { |
| 50 StopVSyncLoop(); |
| 51 } |
| 52 |
| 53 void NonPresentingGvrDelegate::StopVSyncLoop() { |
| 54 vsync_task_.Cancel(); |
| 55 if (!callback_.is_null()) |
| 56 callback_.Run(nullptr, 0); |
| 57 callback_.Reset(); |
| 58 if (binding_.is_bound()) |
| 59 binding_.Close(); |
| 60 gvr_api_->PauseTracking(); |
| 61 paused_ = false; |
| 62 } |
| 63 |
| 64 void NonPresentingGvrDelegate::StartVSyncLoop() { |
| 65 vsync_task_.Reset( |
| 66 base::Bind(&NonPresentingGvrDelegate::OnVSync, base::Unretained(this))); |
| 67 gvr_api_->RefreshViewerProfile(); |
| 68 gvr_api_->ResumeTracking(); |
| 69 OnVSync(); |
| 70 } |
| 71 |
| 72 void NonPresentingGvrDelegate::OnVSync() { |
| 73 base::TimeTicks now = base::TimeTicks::Now(); |
| 74 base::TimeTicks target; |
| 75 |
| 76 // Don't run the VSync loop if we're not bound. |
| 77 if (!binding_.is_bound()) |
| 78 return; |
| 79 |
| 80 // Don't send VSyncs until we have a timebase/interval. |
| 81 if (vsync_interval_.is_zero()) |
| 82 return; |
| 83 target = now + vsync_interval_; |
| 84 int64_t intervals = (target - vsync_timebase_) / vsync_interval_; |
| 85 target = vsync_timebase_ + intervals * vsync_interval_; |
| 86 if (!vsync_task_.IsCancelled()) { |
| 87 task_runner_->PostDelayedTask(FROM_HERE, vsync_task_.callback(), |
| 88 target - now); |
| 89 } |
| 90 |
| 91 double time = (intervals * vsync_interval_).InSecondsF(); |
| 92 if (!callback_.is_null()) { |
| 93 callback_.Run(GetPose(), time); |
| 94 callback_.Reset(); |
| 95 } else { |
| 96 pending_vsync_ = true; |
| 97 pending_time_ = time; |
| 98 } |
| 99 } |
| 100 |
| 101 void NonPresentingGvrDelegate::GetVSync(const GetVSyncCallback& callback) { |
| 102 if (!pending_vsync_) { |
| 103 callback_ = std::move(callback); |
| 104 return; |
| 105 } |
| 106 pending_vsync_ = false; |
| 107 callback.Run(GetPose(), pending_time_); |
| 108 } |
| 109 |
| 110 void NonPresentingGvrDelegate::UpdateVSyncInterval(long timebase_nanos, |
| 111 double interval_seconds) { |
| 112 vsync_timebase_ = base::TimeTicks(); |
| 113 vsync_timebase_ += base::TimeDelta::FromMicroseconds(timebase_nanos / 1000); |
| 114 vsync_interval_ = base::TimeDelta::FromSecondsD(interval_seconds); |
| 115 StartVSyncLoop(); |
| 116 } |
| 117 |
| 118 device::mojom::VRPosePtr NonPresentingGvrDelegate::GetPose() { |
| 119 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); |
| 120 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; |
| 121 |
| 122 gvr::Mat4f head_mat = |
| 123 gvr_api_->GetHeadSpaceFromStartSpaceRotation(target_time); |
| 124 head_mat = gvr_api_->ApplyNeckModel(head_mat, 1.0f); |
| 125 |
| 126 uint32_t pose_index = pose_index_++; |
| 127 |
| 128 return VrShell::VRPosePtrFromGvrPose(head_mat, pose_index); |
| 129 } |
| 130 |
| 131 } // namespace vr_shell |
OLD | NEW |