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> |
| 8 |
7 #include "base/android/context_utils.h" | 9 #include "base/android/context_utils.h" |
8 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
9 #include "base/android/jni_utils.h" | 11 #include "base/android/jni_utils.h" |
10 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
11 #include "device/vr/android/gvr/gvr_device.h" | 13 #include "device/vr/android/gvr/gvr_device.h" |
12 #include "jni/GvrDeviceProvider_jni.h" | 14 #include "jni/GvrDeviceProvider_jni.h" |
13 | 15 |
14 using base::android::AttachCurrentThread; | 16 using base::android::AttachCurrentThread; |
15 using base::android::GetApplicationContext; | 17 using base::android::GetApplicationContext; |
16 | 18 |
17 namespace device { | 19 namespace device { |
18 | 20 |
| 21 // A temporary delegate till the VrShell is available. |
| 22 class GvrDeviceProviderDelegate : public GvrDelegate { |
| 23 public: |
| 24 GvrDeviceProviderDelegate() { |
| 25 // TODO: This should eventually be handled by an actual GvrLayout instance |
| 26 // in the view tree. |
| 27 if (j_device_.is_null()) { |
| 28 JNIEnv* env = AttachCurrentThread(); |
| 29 |
| 30 j_device_.Reset( |
| 31 Java_GvrDeviceProvider_create(env, GetApplicationContext())); |
| 32 jlong context = |
| 33 Java_GvrDeviceProvider_getNativeContext(env, j_device_.obj()); |
| 34 |
| 35 if (!context) |
| 36 return; |
| 37 |
| 38 gvr_api_ = |
| 39 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(context)); |
| 40 |
| 41 GvrDelegateManager::GetInstance()->Initialize(this); |
| 42 } |
| 43 } |
| 44 |
| 45 virtual ~GvrDeviceProviderDelegate() { |
| 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()) { |
| 50 JNIEnv* env = AttachCurrentThread(); |
| 51 Java_GvrDeviceProvider_shutdown(env, j_device_.obj()); |
| 52 } |
| 53 } |
| 54 |
| 55 // GvrDelegate implementation |
| 56 void RequestWebVRPresent() override {} |
| 57 void ExitWebVRPresent() override {} |
| 58 |
| 59 void SubmitWebVRFrame() override {} |
| 60 void UpdateWebVRTextureBounds(int eye, |
| 61 float left, |
| 62 float top, |
| 63 float width, |
| 64 float height) override {} |
| 65 |
| 66 gvr::GvrApi* gvr_api() override { return gvr_api_.get(); } |
| 67 |
| 68 private: |
| 69 base::android::ScopedJavaGlobalRef<jobject> j_device_; |
| 70 std::unique_ptr<gvr::GvrApi> gvr_api_; |
| 71 }; |
| 72 |
19 GvrDeviceProvider::GvrDeviceProvider() : VRDeviceProvider() { | 73 GvrDeviceProvider::GvrDeviceProvider() : VRDeviceProvider() { |
20 GvrApiManager::GetInstance()->AddClient(this); | 74 GvrDelegateManager::GetInstance()->AddClient(this); |
21 } | 75 } |
22 | 76 |
23 GvrDeviceProvider::~GvrDeviceProvider() { | 77 GvrDeviceProvider::~GvrDeviceProvider() { |
24 // TODO: This should eventually be handled by an actual GvrLayout instance in | 78 GvrDelegateManager::GetInstance()->RemoveClient(this); |
25 // the view tree. | |
26 GvrApiManager::GetInstance()->Shutdown(); | |
27 if (!j_device_.is_null()) { | |
28 JNIEnv* env = AttachCurrentThread(); | |
29 Java_GvrDeviceProvider_shutdown(env, j_device_.obj()); | |
30 } | |
31 | |
32 GvrApiManager::GetInstance()->RemoveClient(this); | |
33 } | 79 } |
34 | 80 |
35 void GvrDeviceProvider::GetDevices(std::vector<VRDevice*>* devices) { | 81 void GvrDeviceProvider::GetDevices(std::vector<VRDevice*>* devices) { |
36 Initialize(); | 82 Initialize(); |
37 | 83 |
38 if (vr_device_) | 84 if (vr_device_) |
39 devices->push_back(vr_device_.get()); | 85 devices->push_back(vr_device_.get()); |
40 } | 86 } |
41 | 87 |
42 void GvrDeviceProvider::Initialize() { | 88 void GvrDeviceProvider::Initialize() { |
43 // TODO: This should eventually be handled by an actual GvrLayout instance in | 89 if (!delegate_) { |
44 // the view tree. | 90 delegate_.reset(new GvrDeviceProviderDelegate()); |
45 if (j_device_.is_null()) { | |
46 JNIEnv* env = AttachCurrentThread(); | |
47 | |
48 j_device_.Reset( | |
49 Java_GvrDeviceProvider_create(env, GetApplicationContext())); | |
50 jlong gvr_api = | |
51 Java_GvrDeviceProvider_getNativeContext(env, j_device_.obj()); | |
52 | |
53 if (!gvr_api) | |
54 return; | |
55 | |
56 GvrApiManager::GetInstance()->Initialize( | |
57 reinterpret_cast<gvr_context*>(gvr_api)); | |
58 } | 91 } |
59 } | 92 } |
60 | 93 |
61 void GvrDeviceProvider::OnGvrApiInitialized(gvr::GvrApi* gvr_api) { | 94 void GvrDeviceProvider::OnDelegateInitialized(GvrDelegate* delegate) { |
62 if (!vr_device_) | 95 if (!vr_device_) |
63 vr_device_.reset(new GvrDevice(this, gvr_api)); | 96 vr_device_.reset(new GvrDevice(this, delegate)); |
64 | 97 |
65 // Should fire a vrdisplayconnected event here. | 98 // Should fire a vrdisplayconnected event here. |
66 } | 99 } |
67 | 100 |
68 void GvrDeviceProvider::OnGvrApiShutdown() { | 101 void GvrDeviceProvider::OnDelegateShutdown() { |
69 // Nothing to do here just yet. Eventually want to shut down the VRDevice | 102 // Nothing to do here just yet. Eventually want to shut down the VRDevice and |
| 103 // fire a vrdisplaydisconnected event. |
70 } | 104 } |
71 | 105 |
72 } // namespace device | 106 } // namespace device |
OLD | NEW |