OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "blimp/client/app/android/blimp_environment.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/android/apk_assets.h" | |
10 #include "base/command_line.h" | |
11 #include "base/files/file.h" | |
12 #include "base/files/memory_mapped_file.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/threading/thread.h" | |
15 #include "blimp/client/app/android/blimp_client_context_delegate_android.h" | |
16 #include "blimp/client/app/blimp_startup.h" | |
17 #include "blimp/client/public/blimp_client_context.h" | |
18 #include "blimp/client/support/compositor/compositor_dependencies_impl.h" | |
19 #include "components/pref_registry/pref_registry_syncable.h" | |
20 #include "components/prefs/command_line_pref_store.h" | |
21 #include "components/prefs/in_memory_pref_store.h" | |
22 #include "components/prefs/pref_service.h" | |
23 #include "components/prefs/pref_service_factory.h" | |
24 #include "jni/BlimpEnvironment_jni.h" | |
25 #include "ui/base/resource/resource_bundle.h" | |
26 | |
27 namespace blimp { | |
28 namespace client { | |
29 namespace { | |
30 | |
31 class BlimpShellCommandLinePrefStore : public CommandLinePrefStore { | |
32 public: | |
33 explicit BlimpShellCommandLinePrefStore(const base::CommandLine* command_line) | |
34 : CommandLinePrefStore(command_line) { | |
35 BlimpClientContext::ApplyBlimpSwitches(this); | |
36 } | |
37 | |
38 protected: | |
39 ~BlimpShellCommandLinePrefStore() override = default; | |
40 }; | |
41 | |
42 void InitializeResourceBundle() { | |
43 base::MemoryMappedFile::Region pak_region; | |
44 int pak_fd = | |
45 base::android::OpenApkAsset("assets/blimp_shell.pak", &pak_region); | |
46 DCHECK_GE(pak_fd, 0); | |
47 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(base::File(pak_fd), | |
48 pak_region); | |
49 } | |
50 | |
51 } // namespace | |
52 | |
53 // DelegatingCompositorDependencies delegates all calls to the public API | |
54 // to its delegate. When it is destructed it informs the BlimpEnvironment that | |
55 // it is going away to ensure that the BlimpEnvironment can know how many | |
56 // DelegatingCompositorDependencies are outstanding. | |
57 class DelegatingCompositorDependencies : public CompositorDependencies { | |
58 public: | |
59 DelegatingCompositorDependencies( | |
60 CompositorDependencies* delegate_compositor_dependencies, | |
61 BlimpEnvironment* blimp_environment) | |
62 : delegate_compositor_dependencies_(delegate_compositor_dependencies), | |
63 blimp_environment_(blimp_environment) {} | |
64 | |
65 ~DelegatingCompositorDependencies() override { | |
66 blimp_environment_->DecrementOutstandingCompositorDependencies(); | |
67 } | |
68 | |
69 gpu::GpuMemoryBufferManager* GetGpuMemoryBufferManager() override { | |
70 return delegate_compositor_dependencies_->GetGpuMemoryBufferManager(); | |
71 } | |
72 | |
73 cc::SurfaceManager* GetSurfaceManager() override { | |
74 return delegate_compositor_dependencies_->GetSurfaceManager(); | |
75 } | |
76 | |
77 cc::FrameSinkId AllocateFrameSinkId() override { | |
78 return delegate_compositor_dependencies_->AllocateFrameSinkId(); | |
79 } | |
80 | |
81 void GetContextProviders(const ContextProviderCallback& callback) override { | |
82 delegate_compositor_dependencies_->GetContextProviders(callback); | |
83 } | |
84 | |
85 private: | |
86 CompositorDependencies* delegate_compositor_dependencies_; | |
87 BlimpEnvironment* blimp_environment_; | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(DelegatingCompositorDependencies); | |
90 }; | |
91 | |
92 BlimpEnvironment::BlimpEnvironment() { | |
93 InitializeResourceBundle(); | |
94 | |
95 io_thread_ = base::MakeUnique<base::Thread>("BlimpIOThread"); | |
96 base::Thread::Options options; | |
97 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
98 io_thread_->StartWithOptions(options); | |
99 | |
100 // Create PrefRegistry and register blimp preferences with it. | |
101 scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry = | |
102 new ::user_prefs::PrefRegistrySyncable(); | |
103 BlimpClientContext::RegisterPrefs(pref_registry.get()); | |
104 | |
105 // Create command line and user preference stores. | |
106 PrefServiceFactory pref_service_factory; | |
107 pref_service_factory.set_command_line_prefs( | |
108 make_scoped_refptr(new BlimpShellCommandLinePrefStore( | |
109 base::CommandLine::ForCurrentProcess()))); | |
110 pref_service_factory.set_user_prefs(new InMemoryPrefStore()); | |
111 | |
112 // Create a PrefService binding the PrefRegistry to the pref stores. | |
113 // The PrefService owns the PrefRegistry and pref stores. | |
114 std::unique_ptr<PrefService> pref_service = | |
115 pref_service_factory.Create(pref_registry.get()); | |
116 | |
117 // Create the real CompositorDependencies. This is used for minting | |
118 // CompositorDependencies to callers of CreateCompositorDepencencies(). | |
119 compositor_dependencies_ = base::MakeUnique<CompositorDependenciesImpl>(); | |
120 | |
121 context_ = base::WrapUnique<BlimpClientContext>(BlimpClientContext::Create( | |
122 io_thread_->task_runner(), io_thread_->task_runner(), | |
123 CreateCompositorDepencencies(), pref_service.get())); | |
124 | |
125 context_delegate_ = | |
126 base::MakeUnique<BlimpClientContextDelegateAndroid>(context_.get()); | |
127 } | |
128 | |
129 BlimpEnvironment::~BlimpEnvironment() { | |
130 DCHECK_EQ(0, outstanding_compositor_dependencies_); | |
131 context_.reset(); | |
132 compositor_dependencies_.reset(); | |
133 } | |
134 | |
135 void BlimpEnvironment::Destroy(JNIEnv*, | |
136 const base::android::JavaParamRef<jobject>&) { | |
137 delete this; | |
138 } | |
139 | |
140 // static | |
141 BlimpEnvironment* BlimpEnvironment::FromJavaObject( | |
142 JNIEnv* env, | |
143 const base::android::JavaRef<jobject>& jobj) { | |
144 return reinterpret_cast<BlimpEnvironment*>( | |
145 Java_BlimpEnvironment_getNativePtr(env, jobj)); | |
146 } | |
147 | |
148 base::android::ScopedJavaLocalRef<jobject> | |
149 BlimpEnvironment::GetBlimpClientContext( | |
150 JNIEnv* env, | |
151 const base::android::JavaParamRef<jobject>& jobj) { | |
152 DCHECK(context_); | |
153 return BlimpClientContext::GetJavaObject(context_.get()); | |
154 } | |
155 | |
156 std::unique_ptr<CompositorDependencies> | |
157 BlimpEnvironment::CreateCompositorDepencencies() { | |
158 outstanding_compositor_dependencies_++; | |
159 return base::MakeUnique<DelegatingCompositorDependencies>( | |
160 compositor_dependencies_.get(), this); | |
161 } | |
162 | |
163 void BlimpEnvironment::DecrementOutstandingCompositorDependencies() { | |
164 outstanding_compositor_dependencies_--; | |
165 DCHECK_GE(0, outstanding_compositor_dependencies_); | |
166 } | |
167 | |
168 static jlong Init(JNIEnv* env, | |
169 const base::android::JavaParamRef<jobject>& obj) { | |
170 BlimpEnvironment* blimp_environment = new BlimpEnvironment(/*env, obj*/); | |
171 return reinterpret_cast<intptr_t>(blimp_environment); | |
172 } | |
173 | |
174 // static | |
175 bool BlimpEnvironment::RegisterJni(JNIEnv* env) { | |
176 return RegisterNativesImpl(env); | |
177 } | |
178 | |
179 } // namespace client | |
180 } // namespace blimp | |
OLD | NEW |