| 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/vr_shell.h" | 8 #include "chrome/browser/android/vr_shell/vr_shell.h" |
| 9 #include "device/vr/android/gvr/gvr_device_provider.h" |
| 9 #include "jni/VrShellDelegate_jni.h" | 10 #include "jni/VrShellDelegate_jni.h" |
| 10 | 11 |
| 11 using base::android::JavaParamRef; | 12 using base::android::JavaParamRef; |
| 12 using base::android::AttachCurrentThread; | 13 using base::android::AttachCurrentThread; |
| 13 | 14 |
| 14 namespace vr_shell { | 15 namespace vr_shell { |
| 15 | 16 |
| 16 // A non presenting delegate for magic window mode. | 17 // A non presenting delegate for magic window mode. |
| 17 class GvrNonPresentingDelegate : public device::GvrDelegate { | 18 class GvrNonPresentingDelegate : public device::GvrDelegate { |
| 18 public: | 19 public: |
| 19 explicit GvrNonPresentingDelegate(jlong context) { | 20 explicit GvrNonPresentingDelegate(jlong context) : weak_ptr_factory_(this) { |
| 20 gvr_api_ = | 21 gvr_api_ = |
| 21 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(context)); | 22 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(context)); |
| 22 } | 23 } |
| 23 | 24 |
| 24 virtual ~GvrNonPresentingDelegate() = default; | 25 virtual ~GvrNonPresentingDelegate() = default; |
| 25 | 26 |
| 26 // GvrDelegate implementation | 27 // GvrDelegate implementation |
| 27 void SetWebVRSecureOrigin(bool secure_origin) override {} | 28 void SetWebVRSecureOrigin(bool secure_origin) override {} |
| 28 void SubmitWebVRFrame() override {} | 29 void SubmitWebVRFrame() override {} |
| 29 void UpdateWebVRTextureBounds(int eye, | 30 void UpdateWebVRTextureBounds(int eye, |
| 30 float left, | 31 float left, |
| 31 float top, | 32 float top, |
| 32 float width, | 33 float width, |
| 33 float height) override {} | 34 float height) override {} |
| 34 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, | 35 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, |
| 35 uint32_t pose_index) override {} | 36 uint32_t pose_index) override {} |
| 36 gvr::GvrApi* gvr_api() override { return gvr_api_.get(); } | 37 gvr::GvrApi* gvr_api() override { return gvr_api_.get(); } |
| 37 | 38 base::WeakPtr<GvrNonPresentingDelegate> GetWeakPtr() { |
| 39 return weak_ptr_factory_.GetWeakPtr(); |
| 40 } |
| 38 private: | 41 private: |
| 39 std::unique_ptr<gvr::GvrApi> gvr_api_; | 42 std::unique_ptr<gvr::GvrApi> gvr_api_; |
| 43 base::WeakPtrFactory<GvrNonPresentingDelegate> weak_ptr_factory_; |
| 40 }; | 44 }; |
| 41 | 45 |
| 42 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) | 46 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) |
| 43 : device_provider_(nullptr) { | 47 : device_provider_(nullptr) { |
| 44 j_vr_shell_delegate_.Reset(env, obj); | 48 j_vr_shell_delegate_.Reset(env, obj); |
| 45 GvrDelegateProvider::SetInstance(this); | 49 GvrDelegateProvider::SetInstance(this); |
| 46 } | 50 } |
| 47 | 51 |
| 48 VrShellDelegate::~VrShellDelegate() { | 52 VrShellDelegate::~VrShellDelegate() { |
| 49 GvrDelegateProvider::SetInstance(nullptr); | 53 GvrDelegateProvider::SetInstance(nullptr); |
| 50 } | 54 } |
| 51 | 55 |
| 52 VrShellDelegate* VrShellDelegate::getNativeDelegate( | 56 VrShellDelegate* VrShellDelegate::GetNativeDelegate( |
| 53 JNIEnv* env, jobject jdelegate) { | 57 JNIEnv* env, jobject jdelegate) { |
| 54 long native_delegate = Java_VrShellDelegate_getNativePointer(env, jdelegate); | 58 long native_delegate = Java_VrShellDelegate_getNativePointer(env, jdelegate); |
| 55 return reinterpret_cast<VrShellDelegate*>(native_delegate); | 59 return reinterpret_cast<VrShellDelegate*>(native_delegate); |
| 56 } | 60 } |
| 57 | 61 |
| 62 base::WeakPtr<device::GvrDeviceProvider> VrShellDelegate::GetDeviceProvider() { |
| 63 return device_provider_; |
| 64 } |
| 65 |
| 58 void VrShellDelegate::ExitWebVRIfNecessary(JNIEnv* env, jobject obj) { | 66 void VrShellDelegate::ExitWebVRIfNecessary(JNIEnv* env, jobject obj) { |
| 59 if (!device_provider_) | 67 if (!device_provider_) |
| 60 return; | 68 return; |
| 61 | 69 |
| 62 device_provider_->OnGvrDelegateRemoved(); | 70 device_provider_->OnGvrDelegateRemoved(); |
| 63 } | 71 } |
| 64 | 72 |
| 65 bool VrShellDelegate::RequestWebVRPresent( | 73 bool VrShellDelegate::RequestWebVRPresent( |
| 66 device::GvrDeviceProvider* device_provider) { | 74 base::WeakPtr<device::GvrDeviceProvider> device_provider) { |
| 67 // Only set one device provider at a time | 75 // Only set one device provider at a time |
| 68 DCHECK(!device_provider_); | 76 DCHECK(!device_provider_); |
| 69 device_provider_ = device_provider; | 77 device_provider_ = device_provider; |
| 70 | 78 |
| 71 // If/When VRShell is ready for use it will call OnVrShellReady. | 79 // If/When VRShell is ready for use it will call OnVrShellReady. |
| 72 JNIEnv* env = AttachCurrentThread(); | 80 JNIEnv* env = AttachCurrentThread(); |
| 73 return Java_VrShellDelegate_enterVRIfNecessary(env, | 81 return Java_VrShellDelegate_enterVRIfNecessary(env, |
| 74 j_vr_shell_delegate_.obj(), | 82 j_vr_shell_delegate_.obj(), |
| 75 true); | 83 true); |
| 76 } | 84 } |
| 77 | 85 |
| 78 void VrShellDelegate::ExitWebVRPresent() { | 86 void VrShellDelegate::ExitWebVRPresent() { |
| 79 if (!device_provider_) | 87 device_provider_.reset(); |
| 80 return; | |
| 81 | |
| 82 device_provider_ = nullptr; | |
| 83 | 88 |
| 84 // VRShell is no longer needed by WebVR, allow it to shut down if it's not | 89 // VRShell is no longer needed by WebVR, allow it to shut down if it's not |
| 85 // being used elsewhere. | 90 // being used elsewhere. |
| 86 JNIEnv* env = AttachCurrentThread(); | 91 JNIEnv* env = AttachCurrentThread(); |
| 87 Java_VrShellDelegate_exitWebVR(env, j_vr_shell_delegate_.obj()); | 92 Java_VrShellDelegate_exitWebVR(env, j_vr_shell_delegate_.obj()); |
| 88 } | 93 } |
| 89 | 94 |
| 90 device::GvrDelegate* VrShellDelegate::GetNonPresentingDelegate() { | 95 base::WeakPtr<device::GvrDelegate> VrShellDelegate::GetNonPresentingDelegate() { |
| 91 if (!non_presenting_delegate_) { | 96 if (!non_presenting_delegate_) { |
| 92 JNIEnv* env = AttachCurrentThread(); | 97 JNIEnv* env = AttachCurrentThread(); |
| 93 jlong context = Java_VrShellDelegate_createNonPresentingNativeContext( | 98 jlong context = Java_VrShellDelegate_createNonPresentingNativeContext( |
| 94 env, j_vr_shell_delegate_.obj()); | 99 env, j_vr_shell_delegate_.obj()); |
| 95 if (!context) | 100 if (!context) |
| 96 return nullptr; | 101 return nullptr; |
| 97 | 102 |
| 98 non_presenting_delegate_.reset(new GvrNonPresentingDelegate(context)); | 103 non_presenting_delegate_.reset(new GvrNonPresentingDelegate(context)); |
| 99 } | 104 } |
| 100 return non_presenting_delegate_.get(); | 105 return static_cast<GvrNonPresentingDelegate*>(non_presenting_delegate_.get()) |
| 106 ->GetWeakPtr(); |
| 101 } | 107 } |
| 102 | 108 |
| 103 void VrShellDelegate::DestroyNonPresentingDelegate() { | 109 void VrShellDelegate::DestroyNonPresentingDelegate() { |
| 104 non_presenting_delegate_.reset(nullptr); | 110 non_presenting_delegate_.reset(nullptr); |
| 105 JNIEnv* env = AttachCurrentThread(); | 111 JNIEnv* env = AttachCurrentThread(); |
| 106 Java_VrShellDelegate_shutdownNonPresentingNativeContext( | 112 Java_VrShellDelegate_shutdownNonPresentingNativeContext( |
| 107 env, j_vr_shell_delegate_.obj()); | 113 env, j_vr_shell_delegate_.obj()); |
| 108 } | 114 } |
| 109 | 115 |
| 110 void VrShellDelegate::OnVrShellReady(VrShell* vr_shell) { | |
| 111 if (device_provider_) | |
| 112 device_provider_->OnGvrDelegateReady(vr_shell); | |
| 113 } | |
| 114 | |
| 115 // ---------------------------------------------------------------------------- | 116 // ---------------------------------------------------------------------------- |
| 116 // Native JNI methods | 117 // Native JNI methods |
| 117 // ---------------------------------------------------------------------------- | 118 // ---------------------------------------------------------------------------- |
| 118 | 119 |
| 119 bool RegisterVrShellDelegate(JNIEnv* env) { | 120 bool RegisterVrShellDelegate(JNIEnv* env) { |
| 120 return RegisterNativesImpl(env); | 121 return RegisterNativesImpl(env); |
| 121 } | 122 } |
| 122 | 123 |
| 123 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 124 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 124 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); | 125 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); |
| 125 } | 126 } |
| 126 | 127 |
| 127 } // namespace vr_shell | 128 } // namespace vr_shell |
| OLD | NEW |