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/non_presenting_gvr_delegate.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. | |
18 class GvrNonPresentingDelegate : public device::GvrDelegate { | |
19 public: | |
20 explicit GvrNonPresentingDelegate(jlong context) { | |
21 gvr_api_ = | |
22 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(context)); | |
23 } | |
24 | |
25 virtual ~GvrNonPresentingDelegate() = default; | |
26 | |
27 // GvrDelegate implementation | |
28 void SetWebVRSecureOrigin(bool secure_origin) override {} | |
29 void SubmitWebVRFrame() override {} | |
30 void UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds, | |
31 const gvr::Rectf& right_bounds) override {} | |
32 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, | |
33 uint32_t pose_index) override {} | |
34 void SetWebVRRenderSurfaceSize(int width, int height) override {} | |
35 gvr::Sizei GetWebVRCompositorSurfaceSize() override { | |
36 return device::kInvalidRenderTargetSize; } | |
37 gvr::GvrApi* gvr_api() override { return gvr_api_.get(); } | |
38 private: | |
39 std::unique_ptr<gvr::GvrApi> gvr_api_; | |
40 }; | |
41 | |
42 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) { | 17 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) { |
43 j_vr_shell_delegate_.Reset(env, obj); | 18 j_vr_shell_delegate_.Reset(env, obj); |
44 GvrDelegateProvider::SetInstance(this); | 19 GvrDelegateProvider::SetInstance(this); |
45 } | 20 } |
46 | 21 |
47 VrShellDelegate::~VrShellDelegate() { | 22 VrShellDelegate::~VrShellDelegate() { |
48 GvrDelegateProvider::SetInstance(nullptr); | 23 GvrDelegateProvider::SetInstance(nullptr); |
49 if (device_provider_) { | 24 if (device_provider_) { |
50 device_provider_->OnNonPresentingDelegateRemoved(); | 25 device_provider_->OnNonPresentingDelegateRemoved(); |
51 } | 26 } |
52 } | 27 } |
53 | 28 |
54 VrShellDelegate* VrShellDelegate::GetNativeDelegate( | 29 VrShellDelegate* VrShellDelegate::GetNativeDelegate( |
55 JNIEnv* env, jobject jdelegate) { | 30 JNIEnv* env, jobject jdelegate) { |
56 long native_delegate = Java_VrShellDelegate_getNativePointer(env, jdelegate); | 31 long native_delegate = Java_VrShellDelegate_getNativePointer(env, jdelegate); |
57 return reinterpret_cast<VrShellDelegate*>(native_delegate); | 32 return reinterpret_cast<VrShellDelegate*>(native_delegate); |
58 } | 33 } |
59 | 34 |
60 void VrShellDelegate::SetDelegate(device::GvrDelegate* delegate) { | 35 void VrShellDelegate::SetDelegate(device::GvrDelegate* delegate) { |
61 // TODO(mthiesse): There's no reason for this delegate to be a WeakPtr | |
62 // anymore. | |
63 delegate_ = delegate; | 36 delegate_ = delegate; |
64 if (device_provider_) { | 37 if (device_provider_) { |
65 device_provider_->OnGvrDelegateReady(delegate_); | 38 device_provider_->OnGvrDelegateReady(delegate_); |
66 } | 39 } |
| 40 delegate_->UpdateVSyncInterval(timebase_nanos_, interval_seconds_); |
67 } | 41 } |
68 | 42 |
69 void VrShellDelegate::RemoveDelegate() { | 43 void VrShellDelegate::RemoveDelegate() { |
70 if (device_provider_) { | 44 if (device_provider_) { |
71 device_provider_->OnGvrDelegateRemoved(); | 45 device_provider_->OnGvrDelegateRemoved(); |
72 } | 46 } |
73 delegate_ = nullptr; | 47 delegate_ = nullptr; |
74 } | 48 } |
75 | 49 |
76 void VrShellDelegate::SetPresentResult(JNIEnv* env, jobject obj, | 50 void VrShellDelegate::SetPresentResult(JNIEnv* env, |
| 51 const JavaParamRef<jobject>& obj, |
77 jboolean result) { | 52 jboolean result) { |
78 CHECK(!present_callback_.is_null()); | 53 CHECK(!present_callback_.is_null()); |
| 54 if (result && non_presenting_delegate_) { |
| 55 non_presenting_delegate_->OnSwitchToPresentingDelegate(); |
| 56 } |
79 present_callback_.Run(result); | 57 present_callback_.Run(result); |
80 present_callback_.Reset(); | 58 present_callback_.Reset(); |
81 } | 59 } |
82 | 60 |
83 void VrShellDelegate::DisplayActivate(JNIEnv* env, jobject obj) { | 61 void VrShellDelegate::DisplayActivate(JNIEnv* env, |
| 62 const JavaParamRef<jobject>& obj) { |
84 if (device_provider_) { | 63 if (device_provider_) { |
85 device_provider_->OnDisplayActivate(); | 64 device_provider_->OnDisplayActivate(); |
86 } | 65 } |
87 } | 66 } |
88 | 67 |
| 68 void VrShellDelegate::UpdateVSyncInterval(JNIEnv* env, |
| 69 const JavaParamRef<jobject>& obj, |
| 70 jlong timebase_nanos, |
| 71 jdouble interval_seconds) { |
| 72 timebase_nanos_ = timebase_nanos; |
| 73 interval_seconds_ = interval_seconds; |
| 74 if (delegate_) { |
| 75 delegate_->UpdateVSyncInterval(timebase_nanos_, |
| 76 interval_seconds_); |
| 77 } |
| 78 if (non_presenting_delegate_) { |
| 79 non_presenting_delegate_->UpdateVSyncInterval(timebase_nanos_, |
| 80 interval_seconds_); |
| 81 } |
| 82 } |
| 83 |
| 84 void VrShellDelegate::OnPause(JNIEnv* env, |
| 85 const JavaParamRef<jobject>& obj) { |
| 86 if (non_presenting_delegate_) { |
| 87 non_presenting_delegate_->Pause(); |
| 88 } |
| 89 } |
| 90 |
| 91 void VrShellDelegate::OnResume(JNIEnv* env, |
| 92 const JavaParamRef<jobject>& obj) { |
| 93 if (non_presenting_delegate_) { |
| 94 non_presenting_delegate_->Resume(); |
| 95 } |
| 96 } |
| 97 |
89 void VrShellDelegate::SetDeviceProvider( | 98 void VrShellDelegate::SetDeviceProvider( |
90 device::GvrDeviceProvider* device_provider) { | 99 device::GvrDeviceProvider* device_provider) { |
91 device_provider_ = device_provider; | 100 device_provider_ = device_provider; |
92 if (device_provider_ && delegate_) { | 101 if (device_provider_ && delegate_) { |
93 device_provider_->OnGvrDelegateReady(delegate_); | 102 device_provider_->OnGvrDelegateReady(delegate_); |
94 } | 103 } |
95 } | 104 } |
96 | 105 |
97 void VrShellDelegate::RequestWebVRPresent( | 106 void VrShellDelegate::RequestWebVRPresent( |
98 const base::Callback<void(bool)>& callback) { | 107 const base::Callback<void(bool)>& callback) { |
(...skipping 24 matching lines...) Expand all Loading... |
123 } | 132 } |
124 | 133 |
125 device::GvrDelegate* VrShellDelegate::GetNonPresentingDelegate() { | 134 device::GvrDelegate* VrShellDelegate::GetNonPresentingDelegate() { |
126 if (!non_presenting_delegate_) { | 135 if (!non_presenting_delegate_) { |
127 JNIEnv* env = AttachCurrentThread(); | 136 JNIEnv* env = AttachCurrentThread(); |
128 jlong context = Java_VrShellDelegate_createNonPresentingNativeContext( | 137 jlong context = Java_VrShellDelegate_createNonPresentingNativeContext( |
129 env, j_vr_shell_delegate_.obj()); | 138 env, j_vr_shell_delegate_.obj()); |
130 if (!context) | 139 if (!context) |
131 return nullptr; | 140 return nullptr; |
132 | 141 |
133 non_presenting_delegate_.reset(new GvrNonPresentingDelegate(context)); | 142 non_presenting_delegate_.reset(new NonPresentingGvrDelegate(context)); |
134 } | 143 } |
135 return static_cast<GvrNonPresentingDelegate*>(non_presenting_delegate_.get()); | 144 non_presenting_delegate_->UpdateVSyncInterval(timebase_nanos_, |
| 145 interval_seconds_); |
| 146 return non_presenting_delegate_.get(); |
136 } | 147 } |
137 | 148 |
138 void VrShellDelegate::DestroyNonPresentingDelegate() { | 149 void VrShellDelegate::DestroyNonPresentingDelegate() { |
139 if (non_presenting_delegate_) { | 150 if (non_presenting_delegate_) { |
140 non_presenting_delegate_.reset(nullptr); | 151 non_presenting_delegate_.reset(nullptr); |
141 JNIEnv* env = AttachCurrentThread(); | 152 JNIEnv* env = AttachCurrentThread(); |
142 Java_VrShellDelegate_shutdownNonPresentingNativeContext( | 153 Java_VrShellDelegate_shutdownNonPresentingNativeContext( |
143 env, j_vr_shell_delegate_.obj()); | 154 env, j_vr_shell_delegate_.obj()); |
144 } | 155 } |
145 } | 156 } |
(...skipping 10 matching lines...) Expand all Loading... |
156 | 167 |
157 bool RegisterVrShellDelegate(JNIEnv* env) { | 168 bool RegisterVrShellDelegate(JNIEnv* env) { |
158 return RegisterNativesImpl(env); | 169 return RegisterNativesImpl(env); |
159 } | 170 } |
160 | 171 |
161 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 172 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
162 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); | 173 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); |
163 } | 174 } |
164 | 175 |
165 } // namespace vr_shell | 176 } // namespace vr_shell |
OLD | NEW |