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