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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
11 #include "chrome/browser/android/vr_shell/non_presenting_gvr_delegate.h" | 11 #include "chrome/browser/android/vr_shell/non_presenting_gvr_delegate.h" |
12 #include "chrome/browser/android/vr_shell/vr_usage_monitor.h" | |
12 #include "device/vr/android/gvr/gvr_delegate.h" | 13 #include "device/vr/android/gvr/gvr_delegate.h" |
13 #include "device/vr/android/gvr/gvr_device.h" | 14 #include "device/vr/android/gvr/gvr_device.h" |
14 #include "device/vr/android/gvr/gvr_device_provider.h" | 15 #include "device/vr/android/gvr/gvr_device_provider.h" |
15 #include "jni/VrShellDelegate_jni.h" | 16 #include "jni/VrShellDelegate_jni.h" |
16 | 17 |
17 using base::android::JavaParamRef; | 18 using base::android::JavaParamRef; |
18 using base::android::AttachCurrentThread; | 19 using base::android::AttachCurrentThread; |
20 using base::android::ScopedJavaLocalRef; | |
19 | 21 |
20 namespace vr_shell { | 22 namespace vr_shell { |
21 | 23 |
22 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) | 24 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) |
23 : weak_ptr_factory_(this) { | 25 : weak_ptr_factory_(this) { |
24 DVLOG(1) << __FUNCTION__ << "=" << this; | 26 DVLOG(1) << __FUNCTION__ << "=" << this; |
25 j_vr_shell_delegate_.Reset(env, obj); | 27 j_vr_shell_delegate_.Reset(env, obj); |
26 } | 28 } |
27 | 29 |
28 VrShellDelegate::~VrShellDelegate() { | 30 VrShellDelegate::~VrShellDelegate() { |
29 DVLOG(1) << __FUNCTION__ << "=" << this; | 31 DVLOG(1) << __FUNCTION__ << "=" << this; |
30 if (device_provider_) { | 32 if (device_provider_) { |
31 device_provider_->Device()->OnExitPresent(); | 33 device_provider_->Device()->OnExitPresent(); |
32 device_provider_->Device()->OnDelegateChanged(); | 34 device_provider_->Device()->OnDelegateChanged(); |
33 } | 35 } |
34 if (!present_callback_.is_null()) { | 36 if (!present_callback_.is_null()) { |
35 base::ResetAndReturn(&present_callback_).Run(false); | 37 base::ResetAndReturn(&present_callback_).Run(false); |
36 } | 38 } |
37 } | 39 } |
38 | 40 |
39 device::GvrDelegateProvider* VrShellDelegate::CreateVrShellDelegate() { | 41 device::GvrDelegateProvider* VrShellDelegate::CreateVrShellDelegate() { |
40 JNIEnv* env = AttachCurrentThread(); | 42 JNIEnv* env = AttachCurrentThread(); |
41 base::android::ScopedJavaLocalRef<jobject> jdelegate = | 43 ScopedJavaLocalRef<jobject> jdelegate = Java_VrShellDelegate_getInstance(env); |
42 Java_VrShellDelegate_getInstance(env); | |
43 if (!jdelegate.is_null()) | 44 if (!jdelegate.is_null()) |
44 return GetNativeVrShellDelegate(env, jdelegate.obj()); | 45 return GetNativeVrShellDelegate(env, jdelegate.obj()); |
45 return nullptr; | 46 return nullptr; |
46 } | 47 } |
47 | 48 |
48 VrShellDelegate* VrShellDelegate::GetNativeVrShellDelegate(JNIEnv* env, | 49 VrShellDelegate* VrShellDelegate::GetNativeVrShellDelegate(JNIEnv* env, |
49 jobject jdelegate) { | 50 jobject jdelegate) { |
50 return reinterpret_cast<VrShellDelegate*>( | 51 return reinterpret_cast<VrShellDelegate*>( |
51 Java_VrShellDelegate_getNativePointer(env, jdelegate)); | 52 Java_VrShellDelegate_getNativePointer(env, jdelegate)); |
52 } | 53 } |
53 | 54 |
54 void VrShellDelegate::SetPresentingDelegate( | 55 void VrShellDelegate::SetPresentingDelegate( |
55 device::PresentingGvrDelegate* delegate, | 56 device::PresentingGvrDelegate* delegate, |
56 gvr_context* context) { | 57 gvr_context* context) { |
57 presenting_delegate_ = delegate; | 58 presenting_delegate_ = delegate; |
58 // Clean up the non-presenting delegate. | 59 // Clean up the non-presenting delegate. |
60 JNIEnv* env = AttachCurrentThread(); | |
59 if (presenting_delegate_ && non_presenting_delegate_) { | 61 if (presenting_delegate_ && non_presenting_delegate_) { |
60 non_presenting_delegate_ = nullptr; | 62 non_presenting_delegate_ = nullptr; |
61 JNIEnv* env = AttachCurrentThread(); | |
62 Java_VrShellDelegate_shutdownNonPresentingNativeContext( | 63 Java_VrShellDelegate_shutdownNonPresentingNativeContext( |
63 env, j_vr_shell_delegate_.obj()); | 64 env, j_vr_shell_delegate_.obj()); |
64 } | 65 } |
65 if (device_provider_) { | 66 if (device_provider_) { |
66 device::GvrDevice* device = device_provider_->Device(); | 67 device::GvrDevice* device = device_provider_->Device(); |
67 device->OnDelegateChanged(); | 68 device->OnDelegateChanged(); |
68 } | 69 } |
69 | 70 |
70 presenting_delegate_->UpdateVSyncInterval(timebase_nanos_, interval_seconds_); | 71 presenting_delegate_->UpdateVSyncInterval(timebase_nanos_, interval_seconds_); |
71 | 72 |
72 if (pending_successful_present_request_) { | 73 if (pending_successful_present_request_) { |
73 presenting_delegate_->SetSubmitClient(std::move(submit_client_)); | 74 presenting_delegate_->SetSubmitClient(std::move(submit_client_)); |
74 base::ResetAndReturn(&present_callback_).Run(true); | 75 base::ResetAndReturn(&present_callback_).Run(true); |
75 pending_successful_present_request_ = false; | 76 pending_successful_present_request_ = false; |
76 } | 77 } |
78 | |
79 VrMetricsHelper::LogGvrVersionAndVrViewerType(context, GetGvrVersion(env)); | |
77 } | 80 } |
78 | 81 |
79 void VrShellDelegate::RemoveDelegate() { | 82 void VrShellDelegate::RemoveDelegate() { |
80 presenting_delegate_ = nullptr; | 83 presenting_delegate_ = nullptr; |
81 if (device_provider_) { | 84 if (device_provider_) { |
82 CreateNonPresentingDelegate(); | 85 CreateNonPresentingDelegate(); |
83 device_provider_->Device()->OnExitPresent(); | 86 device_provider_->Device()->OnExitPresent(); |
84 device_provider_->Device()->OnDelegateChanged(); | 87 device_provider_->Device()->OnDelegateChanged(); |
85 } | 88 } |
86 } | 89 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 | 191 |
189 void VrShellDelegate::ExitWebVRPresent() { | 192 void VrShellDelegate::ExitWebVRPresent() { |
190 // VRShell is no longer needed by WebVR, allow it to shut down if it's not | 193 // VRShell is no longer needed by WebVR, allow it to shut down if it's not |
191 // being used elsewhere. | 194 // being used elsewhere. |
192 JNIEnv* env = AttachCurrentThread(); | 195 JNIEnv* env = AttachCurrentThread(); |
193 if (Java_VrShellDelegate_exitWebVRPresent(env, j_vr_shell_delegate_.obj())) { | 196 if (Java_VrShellDelegate_exitWebVRPresent(env, j_vr_shell_delegate_.obj())) { |
194 device_provider_->Device()->OnExitPresent(); | 197 device_provider_->Device()->OnExitPresent(); |
195 } | 198 } |
196 } | 199 } |
197 | 200 |
201 gvr_version VrShellDelegate::GetGvrVersion(JNIEnv* env) { | |
202 ScopedJavaLocalRef<jobject> j_vr_core_info = | |
203 Java_VrShellDelegate_getVrCoreInfo(env, j_vr_shell_delegate_.obj()); | |
204 jclass j_vr_core_info_class = env->GetObjectClass(j_vr_core_info.obj()); | |
205 jfieldID j_field_id = env->GetFieldID(j_vr_core_info_class, "version", | |
206 "Lcom/google/vr/ndk/base/Version;"); | |
207 jobject j_version_object = | |
208 env->GetObjectField(j_vr_core_info.obj(), j_field_id); | |
209 gvr_version gvr_version; | |
210 if (j_version_object) { | |
211 j_vr_core_info_class = env->GetObjectClass(j_version_object); | |
212 j_field_id = env->GetFieldID(j_vr_core_info_class, "majorVersion", "I"); | |
ddorwin
2017/05/05 20:29:59
Aren't these members on a member of this class?
tiborg
2017/05/10 20:39:51
Yep, pretty much. Are you aware of an easier way t
| |
213 gvr_version.major = env->GetIntField(j_version_object, j_field_id); | |
214 j_field_id = env->GetFieldID(j_vr_core_info_class, "minorVersion", "I"); | |
215 gvr_version.minor = env->GetIntField(j_version_object, j_field_id); | |
216 j_field_id = env->GetFieldID(j_vr_core_info_class, "patchVersion", "I"); | |
217 gvr_version.patch = env->GetIntField(j_version_object, j_field_id); | |
218 } else { | |
219 j_field_id = env->GetFieldID(j_vr_core_info_class, "compatibility", "I"); | |
220 jint compatibility = env->GetIntField(j_vr_core_info.obj(), j_field_id); | |
221 | |
222 if (compatibility == 2) { | |
ddorwin
2017/05/05 20:29:59
s/2/constant?
tiborg
2017/05/10 20:39:52
Done.
| |
223 gvr_version.major = 0; | |
224 gvr_version.minor = 0; | |
225 gvr_version.patch = 0; | |
226 } else { | |
227 gvr_version.major = -1; | |
228 gvr_version.minor = -1; | |
229 gvr_version.patch = -1; | |
230 } | |
231 } | |
232 return gvr_version; | |
233 } | |
234 | |
198 void VrShellDelegate::CreateNonPresentingDelegate() { | 235 void VrShellDelegate::CreateNonPresentingDelegate() { |
199 JNIEnv* env = AttachCurrentThread(); | 236 JNIEnv* env = AttachCurrentThread(); |
200 gvr_context* context = reinterpret_cast<gvr_context*>( | 237 gvr_context* context = reinterpret_cast<gvr_context*>( |
201 Java_VrShellDelegate_createNonPresentingNativeContext( | 238 Java_VrShellDelegate_createNonPresentingNativeContext( |
202 env, j_vr_shell_delegate_.obj())); | 239 env, j_vr_shell_delegate_.obj())); |
203 non_presenting_delegate_ = | 240 non_presenting_delegate_ = |
204 base::MakeUnique<NonPresentingGvrDelegate>(context); | 241 base::MakeUnique<NonPresentingGvrDelegate>(context); |
205 non_presenting_delegate_->UpdateVSyncInterval(timebase_nanos_, | 242 non_presenting_delegate_->UpdateVSyncInterval(timebase_nanos_, |
206 interval_seconds_); | 243 interval_seconds_); |
244 VrMetricsHelper::LogGvrVersionAndVrViewerType(context, GetGvrVersion(env)); | |
207 } | 245 } |
208 | 246 |
209 void VrShellDelegate::OnActivateDisplayHandled(bool present_requested) { | 247 void VrShellDelegate::OnActivateDisplayHandled(bool present_requested) { |
210 if (!present_requested) { | 248 if (!present_requested) { |
211 // WebVR page didn't request presentation in the vrdisplayactivate handler. | 249 // WebVR page didn't request presentation in the vrdisplayactivate handler. |
212 // Tell VrShell that we are in VR Browsing Mode. | 250 // Tell VrShell that we are in VR Browsing Mode. |
213 ExitWebVRPresent(); | 251 ExitWebVRPresent(); |
214 } | 252 } |
215 } | 253 } |
216 | 254 |
(...skipping 20 matching lines...) Expand all Loading... | |
237 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 275 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
238 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); | 276 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); |
239 } | 277 } |
240 | 278 |
241 static void OnLibraryAvailable(JNIEnv* env, const JavaParamRef<jclass>& clazz) { | 279 static void OnLibraryAvailable(JNIEnv* env, const JavaParamRef<jclass>& clazz) { |
242 device::GvrDelegateProvider::SetInstance( | 280 device::GvrDelegateProvider::SetInstance( |
243 base::Bind(&VrShellDelegate::CreateVrShellDelegate)); | 281 base::Bind(&VrShellDelegate::CreateVrShellDelegate)); |
244 } | 282 } |
245 | 283 |
246 } // namespace vr_shell | 284 } // namespace vr_shell |
OLD | NEW |