| 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. | 17 VrShellDelegate::VrShellDelegate(JNIEnv* env, jobject obj) |
| 18 class GvrNonPresentingDelegate : public device::GvrDelegate { | 18 : weak_ptr_factory_(this) { |
| 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) { | |
| 43 j_vr_shell_delegate_.Reset(env, obj); | 19 j_vr_shell_delegate_.Reset(env, obj); |
| 44 GvrDelegateProvider::SetInstance(this); | 20 GvrDelegateProvider::SetInstance(this); |
| 45 } | 21 } |
| 46 | 22 |
| 47 VrShellDelegate::~VrShellDelegate() { | 23 VrShellDelegate::~VrShellDelegate() { |
| 48 GvrDelegateProvider::SetInstance(nullptr); | 24 GvrDelegateProvider::SetInstance(nullptr); |
| 49 if (device_provider_) { | 25 if (device_provider_) { |
| 50 device_provider_->OnNonPresentingDelegateRemoved(); | 26 device_provider_->OnNonPresentingDelegateRemoved(); |
| 51 } | 27 } |
| 52 } | 28 } |
| 53 | 29 |
| 54 VrShellDelegate* VrShellDelegate::GetNativeDelegate( | 30 VrShellDelegate* VrShellDelegate::GetNativeDelegate( |
| 55 JNIEnv* env, jobject jdelegate) { | 31 JNIEnv* env, jobject jdelegate) { |
| 56 long native_delegate = Java_VrShellDelegate_getNativePointer(env, jdelegate); | 32 long native_delegate = Java_VrShellDelegate_getNativePointer(env, jdelegate); |
| 57 return reinterpret_cast<VrShellDelegate*>(native_delegate); | 33 return reinterpret_cast<VrShellDelegate*>(native_delegate); |
| 58 } | 34 } |
| 59 | 35 |
| 60 void VrShellDelegate::SetDelegate(device::GvrDelegate* delegate) { | 36 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; | 37 delegate_ = delegate; |
| 38 if (non_presenting_delegate_) { |
| 39 device::mojom::VRVSyncProviderRequest request = |
| 40 non_presenting_delegate_->OnSwitchToPresentingDelegate(); |
| 41 if (request.is_pending()) |
| 42 delegate->OnVRVsyncProviderRequest(std::move(request)); |
| 43 } |
| 64 if (device_provider_) { | 44 if (device_provider_) { |
| 65 device_provider_->OnGvrDelegateReady(delegate_); | 45 device_provider_->OnGvrDelegateReady(delegate_); |
| 66 } | 46 } |
| 47 delegate_->UpdateVSyncInterval(timebase_nanos_, interval_seconds_); |
| 67 } | 48 } |
| 68 | 49 |
| 69 void VrShellDelegate::RemoveDelegate() { | 50 void VrShellDelegate::RemoveDelegate() { |
| 70 if (device_provider_) { | 51 if (device_provider_) { |
| 71 device_provider_->OnGvrDelegateRemoved(); | 52 device_provider_->OnGvrDelegateRemoved(); |
| 72 } | 53 } |
| 73 delegate_ = nullptr; | 54 delegate_ = nullptr; |
| 74 } | 55 } |
| 75 | 56 |
| 76 void VrShellDelegate::SetPresentResult(JNIEnv* env, jobject obj, | 57 void VrShellDelegate::SetPresentResult(JNIEnv* env, |
| 58 const JavaParamRef<jobject>& obj, |
| 77 jboolean result) { | 59 jboolean result) { |
| 78 CHECK(!present_callback_.is_null()); | 60 CHECK(!present_callback_.is_null()); |
| 79 present_callback_.Run(result); | 61 present_callback_.Run(result); |
| 80 present_callback_.Reset(); | 62 present_callback_.Reset(); |
| 81 } | 63 } |
| 82 | 64 |
| 83 void VrShellDelegate::DisplayActivate(JNIEnv* env, jobject obj) { | 65 void VrShellDelegate::DisplayActivate(JNIEnv* env, |
| 66 const JavaParamRef<jobject>& obj) { |
| 84 if (device_provider_) { | 67 if (device_provider_) { |
| 85 device_provider_->OnDisplayActivate(); | 68 device_provider_->OnDisplayActivate(); |
| 86 } | 69 } |
| 87 } | 70 } |
| 88 | 71 |
| 72 void VrShellDelegate::UpdateVSyncInterval(JNIEnv* env, |
| 73 const JavaParamRef<jobject>& obj, |
| 74 jlong timebase_nanos, |
| 75 jdouble interval_seconds) { |
| 76 timebase_nanos_ = timebase_nanos; |
| 77 interval_seconds_ = interval_seconds; |
| 78 if (delegate_) { |
| 79 delegate_->UpdateVSyncInterval(timebase_nanos_, |
| 80 interval_seconds_); |
| 81 } |
| 82 if (non_presenting_delegate_) { |
| 83 non_presenting_delegate_->UpdateVSyncInterval(timebase_nanos_, |
| 84 interval_seconds_); |
| 85 } |
| 86 } |
| 87 |
| 88 void VrShellDelegate::OnPause(JNIEnv* env, |
| 89 const JavaParamRef<jobject>& obj) { |
| 90 if (non_presenting_delegate_) { |
| 91 non_presenting_delegate_->Pause(); |
| 92 } |
| 93 } |
| 94 |
| 95 void VrShellDelegate::OnResume(JNIEnv* env, |
| 96 const JavaParamRef<jobject>& obj) { |
| 97 if (non_presenting_delegate_) { |
| 98 non_presenting_delegate_->Resume(); |
| 99 } |
| 100 } |
| 101 |
| 89 void VrShellDelegate::SetDeviceProvider( | 102 void VrShellDelegate::SetDeviceProvider( |
| 90 device::GvrDeviceProvider* device_provider) { | 103 device::GvrDeviceProvider* device_provider) { |
| 91 device_provider_ = device_provider; | 104 device_provider_ = device_provider; |
| 92 if (device_provider_ && delegate_) { | 105 if (device_provider_ && delegate_) { |
| 93 device_provider_->OnGvrDelegateReady(delegate_); | 106 device_provider_->OnGvrDelegateReady(delegate_); |
| 94 } | 107 } |
| 95 } | 108 } |
| 96 | 109 |
| 97 void VrShellDelegate::RequestWebVRPresent( | 110 void VrShellDelegate::RequestWebVRPresent( |
| 98 const base::Callback<void(bool)>& callback) { | 111 const base::Callback<void(bool)>& callback) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 115 // being used elsewhere. | 128 // being used elsewhere. |
| 116 JNIEnv* env = AttachCurrentThread(); | 129 JNIEnv* env = AttachCurrentThread(); |
| 117 Java_VrShellDelegate_exitWebVR(env, j_vr_shell_delegate_.obj()); | 130 Java_VrShellDelegate_exitWebVR(env, j_vr_shell_delegate_.obj()); |
| 118 } | 131 } |
| 119 | 132 |
| 120 void VrShellDelegate::ForceExitVr() { | 133 void VrShellDelegate::ForceExitVr() { |
| 121 JNIEnv* env = AttachCurrentThread(); | 134 JNIEnv* env = AttachCurrentThread(); |
| 122 Java_VrShellDelegate_forceExitVr(env, j_vr_shell_delegate_.obj()); | 135 Java_VrShellDelegate_forceExitVr(env, j_vr_shell_delegate_.obj()); |
| 123 } | 136 } |
| 124 | 137 |
| 138 base::WeakPtr<VrShellDelegate> VrShellDelegate::GetWeakPtr() { |
| 139 return weak_ptr_factory_.GetWeakPtr(); |
| 140 } |
| 141 |
| 142 void VrShellDelegate::OnVRVsyncProviderRequest( |
| 143 device::mojom::VRVSyncProviderRequest request) { |
| 144 GetNonPresentingDelegate()->OnVRVsyncProviderRequest(std::move(request)); |
| 145 } |
| 146 |
| 125 device::GvrDelegate* VrShellDelegate::GetNonPresentingDelegate() { | 147 device::GvrDelegate* VrShellDelegate::GetNonPresentingDelegate() { |
| 126 if (!non_presenting_delegate_) { | 148 if (!non_presenting_delegate_) { |
| 127 JNIEnv* env = AttachCurrentThread(); | 149 JNIEnv* env = AttachCurrentThread(); |
| 128 jlong context = Java_VrShellDelegate_createNonPresentingNativeContext( | 150 jlong context = Java_VrShellDelegate_createNonPresentingNativeContext( |
| 129 env, j_vr_shell_delegate_.obj()); | 151 env, j_vr_shell_delegate_.obj()); |
| 130 if (!context) | 152 if (!context) |
| 131 return nullptr; | 153 return nullptr; |
| 132 | 154 |
| 133 non_presenting_delegate_.reset(new GvrNonPresentingDelegate(context)); | 155 non_presenting_delegate_.reset(new NonPresentingGvrDelegate(context)); |
| 156 non_presenting_delegate_->UpdateVSyncInterval(timebase_nanos_, |
| 157 interval_seconds_); |
| 134 } | 158 } |
| 135 return static_cast<GvrNonPresentingDelegate*>(non_presenting_delegate_.get()); | 159 return non_presenting_delegate_.get(); |
| 136 } | 160 } |
| 137 | 161 |
| 138 void VrShellDelegate::DestroyNonPresentingDelegate() { | 162 void VrShellDelegate::DestroyNonPresentingDelegate() { |
| 139 if (non_presenting_delegate_) { | 163 if (non_presenting_delegate_) { |
| 140 non_presenting_delegate_.reset(nullptr); | 164 non_presenting_delegate_.reset(nullptr); |
| 141 JNIEnv* env = AttachCurrentThread(); | 165 JNIEnv* env = AttachCurrentThread(); |
| 142 Java_VrShellDelegate_shutdownNonPresentingNativeContext( | 166 Java_VrShellDelegate_shutdownNonPresentingNativeContext( |
| 143 env, j_vr_shell_delegate_.obj()); | 167 env, j_vr_shell_delegate_.obj()); |
| 144 } | 168 } |
| 145 } | 169 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 156 | 180 |
| 157 bool RegisterVrShellDelegate(JNIEnv* env) { | 181 bool RegisterVrShellDelegate(JNIEnv* env) { |
| 158 return RegisterNativesImpl(env); | 182 return RegisterNativesImpl(env); |
| 159 } | 183 } |
| 160 | 184 |
| 161 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 185 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 162 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); | 186 return reinterpret_cast<intptr_t>(new VrShellDelegate(env, obj)); |
| 163 } | 187 } |
| 164 | 188 |
| 165 } // namespace vr_shell | 189 } // namespace vr_shell |
| OLD | NEW |