OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h" | 5 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "chrome/browser/android/vr_shell/non_presenting_gvr_delegate.h" | 8 #include "chrome/browser/android/vr_shell/non_presenting_gvr_delegate.h" |
| 9 #include "device/vr/android/gvr/gvr_device.h" |
9 #include "device/vr/android/gvr/gvr_device_provider.h" | 10 #include "device/vr/android/gvr/gvr_device_provider.h" |
| 11 #include "device/vr/android/gvr/gvr_gamepad_data_fetcher.h" |
10 #include "jni/VrShellDelegate_jni.h" | 12 #include "jni/VrShellDelegate_jni.h" |
11 | 13 |
12 using base::android::JavaParamRef; | 14 using base::android::JavaParamRef; |
13 using base::android::AttachCurrentThread; | 15 using base::android::AttachCurrentThread; |
14 | 16 |
15 namespace vr_shell { | 17 namespace vr_shell { |
16 | 18 |
17 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) | 19 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) |
18 : weak_ptr_factory_(this) { | 20 : weak_ptr_factory_(this) { |
19 j_vr_shell_delegate_.Reset(env, obj); | 21 j_vr_shell_delegate_.Reset(env, obj); |
20 GvrDelegateProvider::SetInstance(this); | 22 GvrDelegateProvider::SetInstance(this); |
21 } | 23 } |
22 | 24 |
23 VrShellDelegate::~VrShellDelegate() { | 25 VrShellDelegate::~VrShellDelegate() { |
24 GvrDelegateProvider::SetInstance(nullptr); | 26 GvrDelegateProvider::SetInstance(nullptr); |
25 if (device_provider_) { | 27 if (device_provider_) { |
26 device_provider_->OnNonPresentingDelegateRemoved(); | 28 device_provider_->Device()->OnDelegateChanged(); |
27 } | 29 } |
28 } | 30 } |
29 | 31 |
30 VrShellDelegate* VrShellDelegate::GetNativeDelegate( | 32 VrShellDelegate* VrShellDelegate::GetNativeVrShellDelegate( |
31 JNIEnv* env, jobject jdelegate) { | 33 JNIEnv* env, jobject jdelegate) { |
32 long native_delegate = Java_VrShellDelegate_getNativePointer(env, jdelegate); | 34 long native_delegate = Java_VrShellDelegate_getNativePointer(env, jdelegate); |
33 return reinterpret_cast<VrShellDelegate*>(native_delegate); | 35 return reinterpret_cast<VrShellDelegate*>(native_delegate); |
34 } | 36 } |
35 | 37 |
36 void VrShellDelegate::SetDelegate(device::GvrDelegate* delegate) { | 38 void VrShellDelegate::SetDelegate(device::GvrDelegate* delegate, |
| 39 gvr_context* context) { |
| 40 context_ = context; |
37 delegate_ = delegate; | 41 delegate_ = delegate; |
| 42 // Clean up the non-presenting delegate. |
38 if (non_presenting_delegate_) { | 43 if (non_presenting_delegate_) { |
39 device::mojom::VRVSyncProviderRequest request = | 44 device::mojom::VRVSyncProviderRequest request = |
40 non_presenting_delegate_->OnSwitchToPresentingDelegate(); | 45 non_presenting_delegate_->OnSwitchToPresentingDelegate(); |
41 if (request.is_pending()) | 46 if (request.is_pending()) |
42 delegate->OnVRVsyncProviderRequest(std::move(request)); | 47 delegate->OnVRVsyncProviderRequest(std::move(request)); |
| 48 non_presenting_delegate_ = nullptr; |
| 49 JNIEnv* env = AttachCurrentThread(); |
| 50 Java_VrShellDelegate_shutdownNonPresentingNativeContext( |
| 51 env, j_vr_shell_delegate_.obj()); |
43 } | 52 } |
44 if (device_provider_) { | 53 if (device_provider_) { |
45 device_provider_->OnGvrDelegateReady(delegate_); | 54 device::GvrDevice* device = device_provider_->Device(); |
| 55 device::GamepadDataFetcherManager::GetInstance()->AddFactory( |
| 56 new device::GvrGamepadDataFetcher::Factory(context, device->id())); |
| 57 device->OnDelegateChanged(); |
46 } | 58 } |
| 59 |
47 delegate_->UpdateVSyncInterval(timebase_nanos_, interval_seconds_); | 60 delegate_->UpdateVSyncInterval(timebase_nanos_, interval_seconds_); |
48 } | 61 } |
49 | 62 |
50 void VrShellDelegate::RemoveDelegate() { | 63 void VrShellDelegate::RemoveDelegate() { |
| 64 delegate_ = nullptr; |
| 65 device::GamepadDataFetcherManager::GetInstance()->RemoveSourceFactory( |
| 66 device::GAMEPAD_SOURCE_GVR); |
51 if (device_provider_) { | 67 if (device_provider_) { |
52 device_provider_->OnGvrDelegateRemoved(); | 68 CreateNonPresentingDelegate(); |
| 69 device_provider_->Device()->OnDelegateChanged(); |
53 } | 70 } |
54 delegate_ = nullptr; | |
55 } | 71 } |
56 | 72 |
57 void VrShellDelegate::SetPresentResult(JNIEnv* env, | 73 void VrShellDelegate::SetPresentResult(JNIEnv* env, |
58 const JavaParamRef<jobject>& obj, | 74 const JavaParamRef<jobject>& obj, |
59 jboolean result) { | 75 jboolean result) { |
60 CHECK(!present_callback_.is_null()); | 76 CHECK(!present_callback_.is_null()); |
61 present_callback_.Run(result); | 77 present_callback_.Run(result); |
62 present_callback_.Reset(); | 78 present_callback_.Reset(); |
63 } | 79 } |
64 | 80 |
65 void VrShellDelegate::DisplayActivate(JNIEnv* env, | 81 void VrShellDelegate::DisplayActivate(JNIEnv* env, |
66 const JavaParamRef<jobject>& obj) { | 82 const JavaParamRef<jobject>& obj) { |
67 if (device_provider_) { | 83 if (device_provider_) { |
68 device_provider_->OnDisplayActivate(); | 84 device_provider_->Device()->OnActivate( |
| 85 device::mojom::VRDisplayEventReason::MOUNTED); |
69 } | 86 } |
70 } | 87 } |
71 | 88 |
72 void VrShellDelegate::UpdateVSyncInterval(JNIEnv* env, | 89 void VrShellDelegate::UpdateVSyncInterval(JNIEnv* env, |
73 const JavaParamRef<jobject>& obj, | 90 const JavaParamRef<jobject>& obj, |
74 jlong timebase_nanos, | 91 jlong timebase_nanos, |
75 jdouble interval_seconds) { | 92 jdouble interval_seconds) { |
76 timebase_nanos_ = timebase_nanos; | 93 timebase_nanos_ = timebase_nanos; |
77 interval_seconds_ = interval_seconds; | 94 interval_seconds_ = interval_seconds; |
78 if (delegate_) { | 95 if (delegate_) { |
(...skipping 15 matching lines...) Expand all Loading... |
94 | 111 |
95 void VrShellDelegate::OnResume(JNIEnv* env, | 112 void VrShellDelegate::OnResume(JNIEnv* env, |
96 const JavaParamRef<jobject>& obj) { | 113 const JavaParamRef<jobject>& obj) { |
97 if (non_presenting_delegate_) { | 114 if (non_presenting_delegate_) { |
98 non_presenting_delegate_->Resume(); | 115 non_presenting_delegate_->Resume(); |
99 } | 116 } |
100 } | 117 } |
101 | 118 |
102 void VrShellDelegate::SetDeviceProvider( | 119 void VrShellDelegate::SetDeviceProvider( |
103 device::GvrDeviceProvider* device_provider) { | 120 device::GvrDeviceProvider* device_provider) { |
| 121 CHECK(!device_provider_); |
104 device_provider_ = device_provider; | 122 device_provider_ = device_provider; |
105 if (device_provider_ && delegate_) { | 123 if (!delegate_) |
106 device_provider_->OnGvrDelegateReady(delegate_); | 124 CreateNonPresentingDelegate(); |
107 } | 125 device_provider_->Device()->OnDelegateChanged(); |
| 126 } |
| 127 |
| 128 void VrShellDelegate::ClearDeviceProvider() { |
| 129 non_presenting_delegate_ = nullptr; |
| 130 JNIEnv* env = AttachCurrentThread(); |
| 131 Java_VrShellDelegate_shutdownNonPresentingNativeContext( |
| 132 env, j_vr_shell_delegate_.obj()); |
| 133 device_provider_->Device()->OnDelegateChanged(); |
| 134 device_provider_ = nullptr; |
108 } | 135 } |
109 | 136 |
110 void VrShellDelegate::RequestWebVRPresent( | 137 void VrShellDelegate::RequestWebVRPresent( |
111 const base::Callback<void(bool)>& callback) { | 138 const base::Callback<void(bool)>& callback) { |
112 if (!present_callback_.is_null()) { | 139 if (!present_callback_.is_null()) { |
113 // Can only handle one request at a time. This is also extremely unlikely to | 140 // Can only handle one request at a time. This is also extremely unlikely to |
114 // happen in practice. | 141 // happen in practice. |
115 callback.Run(false); | 142 callback.Run(false); |
116 return; | 143 return; |
117 } | 144 } |
(...skipping 16 matching lines...) Expand all Loading... |
134 JNIEnv* env = AttachCurrentThread(); | 161 JNIEnv* env = AttachCurrentThread(); |
135 Java_VrShellDelegate_forceExitVr(env, j_vr_shell_delegate_.obj()); | 162 Java_VrShellDelegate_forceExitVr(env, j_vr_shell_delegate_.obj()); |
136 } | 163 } |
137 | 164 |
138 base::WeakPtr<VrShellDelegate> VrShellDelegate::GetWeakPtr() { | 165 base::WeakPtr<VrShellDelegate> VrShellDelegate::GetWeakPtr() { |
139 return weak_ptr_factory_.GetWeakPtr(); | 166 return weak_ptr_factory_.GetWeakPtr(); |
140 } | 167 } |
141 | 168 |
142 void VrShellDelegate::OnVRVsyncProviderRequest( | 169 void VrShellDelegate::OnVRVsyncProviderRequest( |
143 device::mojom::VRVSyncProviderRequest request) { | 170 device::mojom::VRVSyncProviderRequest request) { |
144 GetNonPresentingDelegate()->OnVRVsyncProviderRequest(std::move(request)); | 171 GetDelegate()->OnVRVsyncProviderRequest(std::move(request)); |
145 } | 172 } |
146 | 173 |
147 device::GvrDelegate* VrShellDelegate::GetNonPresentingDelegate() { | 174 void VrShellDelegate::CreateNonPresentingDelegate() { |
148 if (!non_presenting_delegate_) { | 175 JNIEnv* env = AttachCurrentThread(); |
149 JNIEnv* env = AttachCurrentThread(); | 176 gvr_context* context = reinterpret_cast<gvr_context*>( |
150 jlong context = Java_VrShellDelegate_createNonPresentingNativeContext( | 177 Java_VrShellDelegate_createNonPresentingNativeContext( |
151 env, j_vr_shell_delegate_.obj()); | 178 env, j_vr_shell_delegate_.obj())); |
152 if (!context) | 179 if (!context) |
153 return nullptr; | 180 return; |
| 181 context_ = context; |
| 182 non_presenting_delegate_ = |
| 183 base::MakeUnique<NonPresentingGvrDelegate>(context); |
| 184 non_presenting_delegate_->UpdateVSyncInterval(timebase_nanos_, |
| 185 interval_seconds_); |
| 186 } |
154 | 187 |
155 non_presenting_delegate_.reset(new NonPresentingGvrDelegate(context)); | 188 device::GvrDelegate* VrShellDelegate::GetDelegate() { |
156 non_presenting_delegate_->UpdateVSyncInterval(timebase_nanos_, | 189 if (delegate_) |
157 interval_seconds_); | 190 return delegate_; |
158 } | |
159 return non_presenting_delegate_.get(); | 191 return non_presenting_delegate_.get(); |
160 } | 192 } |
161 | 193 |
162 void VrShellDelegate::DestroyNonPresentingDelegate() { | |
163 if (non_presenting_delegate_) { | |
164 non_presenting_delegate_.reset(nullptr); | |
165 JNIEnv* env = AttachCurrentThread(); | |
166 Java_VrShellDelegate_shutdownNonPresentingNativeContext( | |
167 env, j_vr_shell_delegate_.obj()); | |
168 } | |
169 } | |
170 | |
171 void VrShellDelegate::SetListeningForActivate(bool listening) { | 194 void VrShellDelegate::SetListeningForActivate(bool listening) { |
172 JNIEnv* env = AttachCurrentThread(); | 195 JNIEnv* env = AttachCurrentThread(); |
173 Java_VrShellDelegate_setListeningForWebVrActivate( | 196 Java_VrShellDelegate_setListeningForWebVrActivate( |
174 env, j_vr_shell_delegate_.obj(), listening); | 197 env, j_vr_shell_delegate_.obj(), listening); |
175 } | 198 } |
176 | 199 |
177 // ---------------------------------------------------------------------------- | 200 // ---------------------------------------------------------------------------- |
178 // Native JNI methods | 201 // Native JNI methods |
179 // ---------------------------------------------------------------------------- | 202 // ---------------------------------------------------------------------------- |
180 | 203 |
181 bool RegisterVrShellDelegate(JNIEnv* env) { | 204 bool RegisterVrShellDelegate(JNIEnv* env) { |
182 return RegisterNativesImpl(env); | 205 return RegisterNativesImpl(env); |
183 } | 206 } |
184 | 207 |
185 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 208 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
186 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); | 209 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); |
187 } | 210 } |
188 | 211 |
189 } // namespace vr_shell | 212 } // namespace vr_shell |
OLD | NEW |