Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(900)

Side by Side Diff: chrome/browser/android/vr_shell/non_presenting_gvr_delegate.cc

Issue 2624633002: Remove Sync GetPose VRService call, implement VRVSyncProvider (Closed)
Patch Set: nits Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 if (binding_.is_bound())
25 binding_.Close();
dcheng 2017/01/18 23:46:06 Nit: Close() already checks this, so just uncondit
mthiesse 2017/01/19 00:19:05 haha it probably does, I was erring a little too f
26 }
27
28 void NonPresentingGvrDelegate::OnVRVsyncProviderRequest(
29 device::mojom::VRVSyncProviderRequest request) {
30 if (binding_.is_bound())
31 binding_.Close();
32 binding_.Bind(std::move(request));
33 binding_.set_connection_error_handler(
34 base::Bind(&NonPresentingGvrDelegate::StopVSyncLoop,
35 weak_ptr_factory_.GetWeakPtr()));
36 StartVSyncLoop();
37 }
38
39 void NonPresentingGvrDelegate::Pause() {
40 vsync_task_.Cancel();
41 paused_ = true;
42 gvr_api_->PauseTracking();
43 }
44
45 void NonPresentingGvrDelegate::Resume() {
46 if (!paused_)
47 return;
48 paused_ = false;
49 StartVSyncLoop();
50 }
51
52 device::mojom::VRVSyncProviderRequest
53 NonPresentingGvrDelegate::OnSwitchToPresentingDelegate() {
54 StopVSyncLoop();
55 if (binding_.is_bound())
56 return binding_.Unbind();
57 return nullptr;
58 }
59
60 void NonPresentingGvrDelegate::StopVSyncLoop() {
61 vsync_task_.Cancel();
62 if (!callback_.is_null())
63 callback_.Run(nullptr, base::TimeDelta());
64 callback_.Reset();
65 gvr_api_->PauseTracking();
66 paused_ = false;
dcheng 2017/01/18 23:46:06 Nit: minor comment about why paused_ = false is co
mthiesse 2017/01/19 00:19:05 Renamed and added clarifying comment.
67 }
68
69 void NonPresentingGvrDelegate::StartVSyncLoop() {
70 vsync_task_.Reset(
71 base::Bind(&NonPresentingGvrDelegate::OnVSync, base::Unretained(this)));
72 gvr_api_->RefreshViewerProfile();
73 gvr_api_->ResumeTracking();
74 OnVSync();
75 }
76
77 void NonPresentingGvrDelegate::OnVSync() {
78 base::TimeTicks now = base::TimeTicks::Now();
79 base::TimeTicks target;
80
81 // Don't run the VSync loop if we're not bound.
82 if (!binding_.is_bound()) {
83 return;
84 }
85
86 // Don't send VSyncs until we have a timebase/interval.
87 if (vsync_interval_.is_zero())
88 return;
89 target = now + vsync_interval_;
90 int64_t intervals = (target - vsync_timebase_) / vsync_interval_;
91 target = vsync_timebase_ + intervals * vsync_interval_;
92 if (!vsync_task_.IsCancelled()) {
93 task_runner_->PostDelayedTask(FROM_HERE, vsync_task_.callback(),
94 target - now);
95 }
96
97 base::TimeDelta time = intervals * vsync_interval_;
98 if (!callback_.is_null()) {
99 callback_.Run(GetPose(), time);
100 callback_.Reset();
101 } else {
102 pending_vsync_ = true;
103 pending_time_ = time;
104 }
105 }
106
107 void NonPresentingGvrDelegate::GetVSync(const GetVSyncCallback& callback) {
108 if (!pending_vsync_) {
109 if (!callback_.is_null()) {
110 mojo::ReportBadMessage("Requested VSync before waiting for response to "
111 "previous request.");
112 return;
113 }
114 callback_ = std::move(callback);
dcheng 2017/01/18 23:46:06 Nit: |callback| is a const ref, so std::move() doe
mthiesse 2017/01/19 00:19:05 Done.
115 return;
116 }
117 pending_vsync_ = false;
118 callback.Run(GetPose(), pending_time_);
119 }
120
121 void NonPresentingGvrDelegate::UpdateVSyncInterval(long timebase_nanos,
122 double interval_seconds) {
123 vsync_timebase_ = base::TimeTicks();
124 vsync_timebase_ += base::TimeDelta::FromMicroseconds(timebase_nanos / 1000);
125 vsync_interval_ = base::TimeDelta::FromSecondsD(interval_seconds);
126 StartVSyncLoop();
127 }
128
129 device::mojom::VRPosePtr NonPresentingGvrDelegate::GetPose() {
130 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow();
131 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos;
132
133 gvr::Mat4f head_mat =
134 gvr_api_->GetHeadSpaceFromStartSpaceRotation(target_time);
135 head_mat = gvr_api_->ApplyNeckModel(head_mat, 1.0f);
136
137 uint32_t pose_index = pose_index_++;
138
139 return VrShell::VRPosePtrFromGvrPose(head_mat, pose_index);
140 }
141
142 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698