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 "device/vr/android/gvr/gvr_device_provider.h" |
10 #include "jni/VrShellDelegate_jni.h" | 10 #include "jni/VrShellDelegate_jni.h" |
11 | 11 |
12 using base::android::JavaParamRef; | 12 using base::android::JavaParamRef; |
13 using base::android::AttachCurrentThread; | 13 using base::android::AttachCurrentThread; |
14 | 14 |
15 namespace vr_shell { | 15 namespace vr_shell { |
16 | 16 |
17 // A non presenting delegate for magic window mode. | 17 // A non presenting delegate for magic window mode. |
18 class GvrNonPresentingDelegate : public device::GvrDelegate { | 18 class GvrNonPresentingDelegate : public device::GvrDelegate { |
19 public: | 19 public: |
20 explicit GvrNonPresentingDelegate(jlong context) : weak_ptr_factory_(this) { | 20 explicit GvrNonPresentingDelegate(jlong context) { |
21 gvr_api_ = | 21 gvr_api_ = |
22 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(context)); | 22 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(context)); |
23 } | 23 } |
24 | 24 |
25 virtual ~GvrNonPresentingDelegate() = default; | 25 virtual ~GvrNonPresentingDelegate() = default; |
26 | 26 |
27 // GvrDelegate implementation | 27 // GvrDelegate implementation |
28 void SetWebVRSecureOrigin(bool secure_origin) override {} | 28 void SetWebVRSecureOrigin(bool secure_origin) override {} |
29 void SubmitWebVRFrame() override {} | 29 void SubmitWebVRFrame() override {} |
30 void UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds, | 30 void UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds, |
31 const gvr::Rectf& right_bounds) override {} | 31 const gvr::Rectf& right_bounds) override {} |
32 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, | 32 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, |
33 uint32_t pose_index) override {} | 33 uint32_t pose_index) override {} |
34 void SetWebVRRenderSurfaceSize(int width, int height) override {} | 34 void SetWebVRRenderSurfaceSize(int width, int height) override {} |
35 gvr::Sizei GetWebVRCompositorSurfaceSize() override { | 35 gvr::Sizei GetWebVRCompositorSurfaceSize() override { |
36 return device::kInvalidRenderTargetSize; } | 36 return device::kInvalidRenderTargetSize; } |
37 gvr::GvrApi* gvr_api() override { return gvr_api_.get(); } | 37 gvr::GvrApi* gvr_api() override { return gvr_api_.get(); } |
38 base::WeakPtr<GvrNonPresentingDelegate> GetWeakPtr() { | |
39 return weak_ptr_factory_.GetWeakPtr(); | |
40 } | |
41 private: | 38 private: |
42 std::unique_ptr<gvr::GvrApi> gvr_api_; | 39 std::unique_ptr<gvr::GvrApi> gvr_api_; |
43 base::WeakPtrFactory<GvrNonPresentingDelegate> weak_ptr_factory_; | |
44 }; | 40 }; |
45 | 41 |
46 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) | 42 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) { |
47 : device_provider_(nullptr) { | |
48 j_vr_shell_delegate_.Reset(env, obj); | 43 j_vr_shell_delegate_.Reset(env, obj); |
49 GvrDelegateProvider::SetInstance(this); | 44 GvrDelegateProvider::SetInstance(this); |
50 } | 45 } |
51 | 46 |
52 VrShellDelegate::~VrShellDelegate() { | 47 VrShellDelegate::~VrShellDelegate() { |
53 GvrDelegateProvider::SetInstance(nullptr); | 48 GvrDelegateProvider::SetInstance(nullptr); |
| 49 if (device_provider_) { |
| 50 device_provider_->OnNonPresentingDelegateRemoved(); |
| 51 } |
54 } | 52 } |
55 | 53 |
56 VrShellDelegate* VrShellDelegate::GetNativeDelegate( | 54 VrShellDelegate* VrShellDelegate::GetNativeDelegate( |
57 JNIEnv* env, jobject jdelegate) { | 55 JNIEnv* env, jobject jdelegate) { |
58 long native_delegate = Java_VrShellDelegate_getNativePointer(env, jdelegate); | 56 long native_delegate = Java_VrShellDelegate_getNativePointer(env, jdelegate); |
59 return reinterpret_cast<VrShellDelegate*>(native_delegate); | 57 return reinterpret_cast<VrShellDelegate*>(native_delegate); |
60 } | 58 } |
61 | 59 |
62 void VrShellDelegate::SetDelegate( | 60 void VrShellDelegate::SetDelegate(device::GvrDelegate* delegate) { |
63 const base::WeakPtr<device::GvrDelegate>& delegate) { | |
64 // TODO(mthiesse): There's no reason for this delegate to be a WeakPtr | 61 // TODO(mthiesse): There's no reason for this delegate to be a WeakPtr |
65 // anymore. | 62 // anymore. |
66 delegate_ = delegate; | 63 delegate_ = delegate; |
67 if (device_provider_.get()) { | 64 if (device_provider_) { |
68 device_provider_->OnGvrDelegateReady(delegate_); | 65 device_provider_->OnGvrDelegateReady(delegate_); |
69 } | 66 } |
70 } | 67 } |
71 | 68 |
72 void VrShellDelegate::RemoveDelegate() { | 69 void VrShellDelegate::RemoveDelegate() { |
73 delegate_.reset(); | 70 if (device_provider_) { |
74 if (device_provider_.get()) { | |
75 device_provider_->OnGvrDelegateRemoved(); | 71 device_provider_->OnGvrDelegateRemoved(); |
76 } | 72 } |
| 73 delegate_ = nullptr; |
77 } | 74 } |
78 | 75 |
79 void VrShellDelegate::SetPresentResult(JNIEnv* env, jobject obj, | 76 void VrShellDelegate::SetPresentResult(JNIEnv* env, jobject obj, |
80 jboolean result) { | 77 jboolean result) { |
81 CHECK(!present_callback_.is_null()); | 78 CHECK(!present_callback_.is_null()); |
82 present_callback_.Run(result); | 79 present_callback_.Run(result); |
83 present_callback_.Reset(); | 80 present_callback_.Reset(); |
84 } | 81 } |
85 | 82 |
86 void VrShellDelegate::DisplayActivate(JNIEnv* env, jobject obj) { | 83 void VrShellDelegate::DisplayActivate(JNIEnv* env, jobject obj) { |
87 if (device_provider_) { | 84 if (device_provider_) { |
88 device_provider_->OnDisplayActivate(); | 85 device_provider_->OnDisplayActivate(); |
89 } | 86 } |
90 } | 87 } |
91 | 88 |
92 void VrShellDelegate::SetDeviceProvider( | 89 void VrShellDelegate::SetDeviceProvider( |
93 base::WeakPtr<device::GvrDeviceProvider> device_provider) { | 90 device::GvrDeviceProvider* device_provider) { |
94 device_provider_ = device_provider; | 91 device_provider_ = device_provider; |
95 if (delegate_.get()) { | 92 if (device_provider_ && delegate_) { |
96 device_provider_->OnGvrDelegateReady(delegate_); | 93 device_provider_->OnGvrDelegateReady(delegate_); |
97 } | 94 } |
98 } | 95 } |
99 | 96 |
100 void VrShellDelegate::RequestWebVRPresent( | 97 void VrShellDelegate::RequestWebVRPresent( |
101 const base::Callback<void(bool)>& callback) { | 98 const base::Callback<void(bool)>& callback) { |
102 if (!present_callback_.is_null()) { | 99 if (!present_callback_.is_null()) { |
103 // Can only handle one request at a time. This is also extremely unlikely to | 100 // Can only handle one request at a time. This is also extremely unlikely to |
104 // happen in practice. | 101 // happen in practice. |
105 callback.Run(false); | 102 callback.Run(false); |
(...skipping 12 matching lines...) Expand all Loading... |
118 // being used elsewhere. | 115 // being used elsewhere. |
119 JNIEnv* env = AttachCurrentThread(); | 116 JNIEnv* env = AttachCurrentThread(); |
120 Java_VrShellDelegate_exitWebVR(env, j_vr_shell_delegate_.obj()); | 117 Java_VrShellDelegate_exitWebVR(env, j_vr_shell_delegate_.obj()); |
121 } | 118 } |
122 | 119 |
123 void VrShellDelegate::ForceExitVr() { | 120 void VrShellDelegate::ForceExitVr() { |
124 JNIEnv* env = AttachCurrentThread(); | 121 JNIEnv* env = AttachCurrentThread(); |
125 Java_VrShellDelegate_forceExitVr(env, j_vr_shell_delegate_.obj()); | 122 Java_VrShellDelegate_forceExitVr(env, j_vr_shell_delegate_.obj()); |
126 } | 123 } |
127 | 124 |
128 base::WeakPtr<device::GvrDelegate> VrShellDelegate::GetNonPresentingDelegate() { | 125 device::GvrDelegate* VrShellDelegate::GetNonPresentingDelegate() { |
129 if (!non_presenting_delegate_) { | 126 if (!non_presenting_delegate_) { |
130 JNIEnv* env = AttachCurrentThread(); | 127 JNIEnv* env = AttachCurrentThread(); |
131 jlong context = Java_VrShellDelegate_createNonPresentingNativeContext( | 128 jlong context = Java_VrShellDelegate_createNonPresentingNativeContext( |
132 env, j_vr_shell_delegate_.obj()); | 129 env, j_vr_shell_delegate_.obj()); |
133 if (!context) | 130 if (!context) |
134 return nullptr; | 131 return nullptr; |
135 | 132 |
136 non_presenting_delegate_.reset(new GvrNonPresentingDelegate(context)); | 133 non_presenting_delegate_.reset(new GvrNonPresentingDelegate(context)); |
137 } | 134 } |
138 return static_cast<GvrNonPresentingDelegate*>(non_presenting_delegate_.get()) | 135 return static_cast<GvrNonPresentingDelegate*>(non_presenting_delegate_.get()); |
139 ->GetWeakPtr(); | |
140 } | 136 } |
141 | 137 |
142 void VrShellDelegate::DestroyNonPresentingDelegate() { | 138 void VrShellDelegate::DestroyNonPresentingDelegate() { |
143 if (non_presenting_delegate_) { | 139 if (non_presenting_delegate_) { |
144 non_presenting_delegate_.reset(nullptr); | 140 non_presenting_delegate_.reset(nullptr); |
145 JNIEnv* env = AttachCurrentThread(); | 141 JNIEnv* env = AttachCurrentThread(); |
146 Java_VrShellDelegate_shutdownNonPresentingNativeContext( | 142 Java_VrShellDelegate_shutdownNonPresentingNativeContext( |
147 env, j_vr_shell_delegate_.obj()); | 143 env, j_vr_shell_delegate_.obj()); |
148 } | 144 } |
149 } | 145 } |
(...skipping 10 matching lines...) Expand all Loading... |
160 | 156 |
161 bool RegisterVrShellDelegate(JNIEnv* env) { | 157 bool RegisterVrShellDelegate(JNIEnv* env) { |
162 return RegisterNativesImpl(env); | 158 return RegisterNativesImpl(env); |
163 } | 159 } |
164 | 160 |
165 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 161 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
166 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); | 162 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); |
167 } | 163 } |
168 | 164 |
169 } // namespace vr_shell | 165 } // namespace vr_shell |
OLD | NEW |