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