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