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 "device/vr/android/gvr/gvr_device_provider.h" | 5 #include "device/vr/android/gvr/gvr_device_provider.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
8 | 8 |
9 #include "base/android/context_utils.h" | 9 #include "base/android/context_utils.h" |
10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
11 #include "base/android/jni_utils.h" | 11 #include "base/android/jni_utils.h" |
12 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.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" |
| 15 #include "device/vr/vr_device_manager.h" |
14 #include "jni/GvrDeviceProvider_jni.h" | 16 #include "jni/GvrDeviceProvider_jni.h" |
| 17 #include "third_party/gvr-android-sdk/src/ndk-beta/include/vr/gvr/capi/include/g
vr.h" |
15 | 18 |
16 using base::android::AttachCurrentThread; | 19 using base::android::AttachCurrentThread; |
17 using base::android::GetApplicationContext; | 20 using base::android::GetApplicationContext; |
18 | 21 |
19 namespace device { | 22 namespace device { |
20 | 23 |
21 // A temporary delegate till the VrShell is available. | 24 // A temporary delegate till a VrShell instance becomes available. |
22 class GvrDeviceProviderDelegate : public GvrDelegate { | 25 class GvrNonPresentingDelegate : public GvrDelegate { |
23 public: | 26 public: |
24 GvrDeviceProviderDelegate() { | 27 GvrNonPresentingDelegate() { Initialize(); } |
25 // TODO: This should eventually be handled by an actual GvrLayout instance | 28 |
26 // in the view tree. | 29 virtual ~GvrNonPresentingDelegate() { Shutdown(); } |
| 30 |
| 31 void Initialize() { |
27 if (j_device_.is_null()) { | 32 if (j_device_.is_null()) { |
28 JNIEnv* env = AttachCurrentThread(); | 33 JNIEnv* env = AttachCurrentThread(); |
29 | 34 |
30 j_device_.Reset( | 35 j_device_.Reset( |
31 Java_GvrDeviceProvider_create(env, GetApplicationContext())); | 36 Java_GvrDeviceProvider_create(env, GetApplicationContext())); |
32 jlong context = | 37 jlong context = |
33 Java_GvrDeviceProvider_getNativeContext(env, j_device_.obj()); | 38 Java_GvrDeviceProvider_getNativeContext(env, j_device_.obj()); |
34 | 39 |
35 if (!context) | 40 if (!context) |
36 return; | 41 return; |
37 | 42 |
38 gvr_api_ = | 43 gvr_api_ = |
39 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(context)); | 44 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(context)); |
40 | |
41 GvrDelegateManager::GetInstance()->Initialize(this); | |
42 } | 45 } |
43 } | 46 } |
44 | 47 |
45 virtual ~GvrDeviceProviderDelegate() { | 48 void Shutdown() { |
46 // TODO: This should eventually be handled by an actual GvrLayout instance | |
47 // in the view tree. | |
48 GvrDelegateManager::GetInstance()->Shutdown(); | |
49 if (!j_device_.is_null()) { | 49 if (!j_device_.is_null()) { |
| 50 gvr_api_ = nullptr; |
50 JNIEnv* env = AttachCurrentThread(); | 51 JNIEnv* env = AttachCurrentThread(); |
51 Java_GvrDeviceProvider_shutdown(env, j_device_.obj()); | 52 Java_GvrDeviceProvider_shutdown(env, j_device_.obj()); |
| 53 j_device_ = nullptr; |
52 } | 54 } |
53 } | 55 } |
54 | 56 |
55 // GvrDelegate implementation | 57 // GvrDelegate implementation |
56 void RequestWebVRPresent() override {} | |
57 void ExitWebVRPresent() override {} | |
58 | |
59 void SubmitWebVRFrame() override {} | 58 void SubmitWebVRFrame() override {} |
60 void UpdateWebVRTextureBounds(int eye, | 59 void UpdateWebVRTextureBounds(int eye, |
61 float left, | 60 float left, |
62 float top, | 61 float top, |
63 float width, | 62 float width, |
64 float height) override {} | 63 float height) override {} |
65 | 64 |
66 gvr::GvrApi* gvr_api() override { return gvr_api_.get(); } | 65 gvr::GvrApi* gvr_api() override { return gvr_api_.get(); } |
67 | 66 |
68 private: | 67 private: |
69 base::android::ScopedJavaGlobalRef<jobject> j_device_; | 68 base::android::ScopedJavaGlobalRef<jobject> j_device_; |
70 std::unique_ptr<gvr::GvrApi> gvr_api_; | 69 std::unique_ptr<gvr::GvrApi> gvr_api_; |
71 }; | 70 }; |
72 | 71 |
73 GvrDeviceProvider::GvrDeviceProvider() : VRDeviceProvider() { | 72 GvrDeviceProvider::GvrDeviceProvider() |
74 GvrDelegateManager::GetInstance()->AddClient(this); | 73 : VRDeviceProvider(), |
75 } | 74 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} |
76 | 75 |
77 GvrDeviceProvider::~GvrDeviceProvider() { | 76 GvrDeviceProvider::~GvrDeviceProvider() { |
78 GvrDelegateManager::GetInstance()->RemoveClient(this); | 77 ExitPresent(); |
79 } | 78 } |
80 | 79 |
81 void GvrDeviceProvider::GetDevices(std::vector<VRDevice*>* devices) { | 80 void GvrDeviceProvider::GetDevices(std::vector<VRDevice*>* devices) { |
82 Initialize(); | 81 Initialize(); |
83 | 82 |
84 if (vr_device_) | 83 if (vr_device_) |
85 devices->push_back(vr_device_.get()); | 84 devices->push_back(vr_device_.get()); |
86 } | 85 } |
87 | 86 |
88 void GvrDeviceProvider::SetClient(VRClientDispatcher* client) { | 87 void GvrDeviceProvider::SetClient(VRClientDispatcher* client) { |
89 if (!client_) | 88 if (!client_) |
90 client_.reset(client); | 89 client_.reset(client); |
91 } | 90 } |
92 | 91 |
93 void GvrDeviceProvider::Initialize() { | 92 void GvrDeviceProvider::Initialize() { |
94 if (!delegate_) | 93 if (!non_presenting_delegate_) { |
95 delegate_.reset(new GvrDeviceProviderDelegate()); | 94 non_presenting_delegate_.reset(new GvrNonPresentingDelegate()); |
| 95 if (non_presenting_delegate_->gvr_api()) { |
| 96 vr_device_.reset(new GvrDevice(this, non_presenting_delegate_.get())); |
| 97 client_->OnDeviceConnectionStatusChanged(vr_device_.get(), true); |
| 98 } |
| 99 } |
96 } | 100 } |
97 | 101 |
98 void GvrDeviceProvider::OnDelegateInitialized(GvrDelegate* delegate) { | 102 bool GvrDeviceProvider::RequestPresent() { |
99 if (!vr_device_) | 103 GvrDelegateProvider* delegate_provider = GvrDelegateProvider::GetInstance(); |
100 vr_device_.reset(new GvrDevice(this, delegate)); | 104 if (!delegate_provider) |
| 105 return false; |
101 | 106 |
102 client_->OnDeviceConnectionStatusChanged(vr_device_.get(), true); | 107 return delegate_provider->RequestWebVRPresent(this); |
103 } | 108 } |
104 | 109 |
105 void GvrDeviceProvider::OnDelegateShutdown() { | 110 void GvrDeviceProvider::ExitPresent() { |
106 if (client_ && vr_device_) | 111 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
107 client_->OnDeviceConnectionStatusChanged(vr_device_.get(), false); | 112 |
| 113 if (!vr_device_) |
| 114 return; |
| 115 |
| 116 vr_device_->SetDelegate(non_presenting_delegate_.get()); |
| 117 |
| 118 GvrDelegateProvider* delegate_provider = GvrDelegateProvider::GetInstance(); |
| 119 if (delegate_provider) |
| 120 delegate_provider->ExitWebVRPresent(); |
| 121 |
| 122 if (client_) |
| 123 client_->OnPresentEnded(vr_device_.get()); |
| 124 } |
| 125 |
| 126 void GvrDeviceProvider::OnGvrDelegateReady(GvrDelegate* delegate) { |
| 127 main_thread_task_runner_->PostTask( |
| 128 FROM_HERE, |
| 129 base::Bind(&GvrDeviceProvider::GvrDelegateReady, base::Unretained(this), |
| 130 base::Unretained(delegate))); |
| 131 } |
| 132 |
| 133 void GvrDeviceProvider::OnGvrDelegateRemoved() { |
| 134 if (!vr_device_) |
| 135 return; |
| 136 |
| 137 main_thread_task_runner_->PostTask( |
| 138 FROM_HERE, |
| 139 base::Bind(&GvrDeviceProvider::ExitPresent, base::Unretained(this))); |
| 140 } |
| 141 |
| 142 void GvrDeviceProvider::GvrDelegateReady(GvrDelegate* delegate) { |
| 143 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 144 |
| 145 vr_device_->SetDelegate(delegate); |
108 } | 146 } |
109 | 147 |
110 } // namespace device | 148 } // namespace device |
OLD | NEW |