Chromium Code Reviews| 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 "components/mus/common/gpu_service.h" | 5 #include "components/mus/common/gpu_service.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
| 9 #include "base/threading/thread_restrictions.h" | |
| 9 #include "components/mus/common/gpu_type_converters.h" | 10 #include "components/mus/common/gpu_type_converters.h" |
| 10 #include "components/mus/common/mojo_gpu_memory_buffer_manager.h" | |
| 11 #include "components/mus/common/switches.h" | 11 #include "components/mus/common/switches.h" |
| 12 #include "components/mus/public/interfaces/gpu_service.mojom.h" | 12 #include "components/mus/public/interfaces/gpu_service.mojom.h" |
| 13 #include "mojo/public/cpp/bindings/sync_call_restrictions.h" | |
| 14 #include "mojo/public/cpp/system/platform_handle.h" | 13 #include "mojo/public/cpp/system/platform_handle.h" |
| 15 #include "services/shell/public/cpp/connector.h" | 14 #include "services/shell/public/cpp/connector.h" |
| 16 | 15 |
| 17 namespace mus { | 16 namespace mus { |
| 18 | 17 |
| 19 GpuService::GpuService() | 18 namespace { |
| 19 | |
| 20 void PostTask(scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 21 const tracked_objects::Location& from_here, | |
| 22 const base::Closure& callback) { | |
| 23 runner->PostTask(from_here, callback); | |
| 24 } | |
| 25 | |
| 26 GpuService* g_gpu_service = nullptr; | |
| 27 } | |
| 28 | |
| 29 GpuService::GpuService(shell::Connector* connector) | |
| 20 : main_message_loop_(base::MessageLoop::current()), | 30 : main_message_loop_(base::MessageLoop::current()), |
| 31 connector_(connector), | |
| 21 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 32 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 22 base::WaitableEvent::InitialState::NOT_SIGNALED), | 33 base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 23 io_thread_("GPUIOThread"), | 34 io_thread_("GPUIOThread"), |
| 24 gpu_memory_buffer_manager_(new MojoGpuMemoryBufferManager) { | 35 gpu_memory_buffer_manager_(new MojoGpuMemoryBufferManager), |
| 36 is_establishing_(false), | |
| 37 establishing_condition_(&lock_) { | |
| 38 DCHECK(main_message_loop_); | |
| 39 DCHECK(connector_); | |
| 25 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | 40 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
| 26 thread_options.priority = base::ThreadPriority::NORMAL; | 41 thread_options.priority = base::ThreadPriority::NORMAL; |
| 27 CHECK(io_thread_.StartWithOptions(thread_options)); | 42 CHECK(io_thread_.StartWithOptions(thread_options)); |
| 28 } | 43 } |
| 29 | 44 |
| 30 GpuService::~GpuService() {} | 45 GpuService::~GpuService() {} |
| 31 | 46 |
| 32 // static | 47 // static |
| 33 bool GpuService::UseChromeGpuCommandBuffer() { | 48 bool GpuService::UseChromeGpuCommandBuffer() { |
| 34 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 49 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 35 switches::kUseChromeGpuCommandBufferInMus); | 50 switches::kUseChromeGpuCommandBufferInMus); |
| 36 } | 51 } |
| 37 | 52 |
| 38 // static | 53 // static |
| 54 void GpuService::Initialize(shell::Connector* connector) { | |
| 55 DCHECK(!g_gpu_service); | |
| 56 g_gpu_service = new GpuService(connector); | |
| 57 } | |
| 58 | |
| 59 // static | |
| 39 GpuService* GpuService::GetInstance() { | 60 GpuService* GpuService::GetInstance() { |
| 40 return base::Singleton<GpuService, | 61 DCHECK(g_gpu_service); |
| 41 base::LeakySingletonTraits<GpuService>>::get(); | 62 return g_gpu_service; |
| 42 } | 63 } |
| 43 | 64 |
| 44 scoped_refptr<gpu::GpuChannelHost> GpuService::EstablishGpuChannel( | 65 void GpuService::EstablishGpuChannel(const base::Closure& callback) { |
| 45 shell::Connector* connector) { | |
| 46 base::AutoLock auto_lock(lock_); | 66 base::AutoLock auto_lock(lock_); |
| 67 auto runner = base::ThreadTaskRunnerHandle::Get(); | |
| 68 if (GetGpuChannelLocked()) { | |
| 69 runner->PostTask(FROM_HERE, callback); | |
| 70 return; | |
| 71 } | |
| 72 base::Closure wrapper_callback = | |
| 73 IsMainThread() ? callback | |
| 74 : base::Bind(PostTask, runner, FROM_HERE, callback); | |
| 75 establish_callbacks_.push_back(wrapper_callback); | |
| 76 | |
| 77 if (!is_establishing_) { | |
| 78 is_establishing_ = true; | |
| 79 main_message_loop_->task_runner()->PostTask( | |
| 80 FROM_HERE, base::Bind(&GpuService::EstablishGpuChannelOnMainThread, | |
| 81 base::Unretained(this))); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 scoped_refptr<gpu::GpuChannelHost> GpuService::EstablishGpuChannelSync() { | |
| 86 base::AutoLock auto_lock(lock_); | |
| 87 if (GetGpuChannelLocked()) | |
| 88 return gpu_channel_; | |
| 89 | |
| 90 if (!IsMainThread()) { | |
| 91 if (!is_establishing_) { | |
| 92 // Create an establishing gpu channel task, if there isn't one. | |
| 93 is_establishing_ = true; | |
| 94 main_message_loop_->task_runner()->PostTask( | |
| 95 FROM_HERE, base::Bind(&GpuService::EstablishGpuChannelOnMainThread, | |
| 96 base::Unretained(this))); | |
| 97 } | |
| 98 // Wait until the pending establishing task is finished. | |
| 99 do { | |
| 100 establishing_condition_.Wait(); | |
| 101 } while (is_establishing_); | |
| 102 return gpu_channel_; | |
| 103 } | |
| 104 | |
| 105 if (!gpu_service_) { | |
| 106 // gpu_service_ is null, it means there is no unfinished async | |
| 107 // EstablishGpuChannel call, so we should issue one. | |
| 108 is_establishing_ = true; | |
| 109 EstablishGpuChannelOnMainThreadLocked(); | |
| 110 } | |
| 111 | |
| 112 base::AutoUnlock auto_unlock(lock_); | |
| 113 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 114 if (!gpu_service_.WaitForIncomingResponse()) { | |
|
jam
2016/06/22 23:57:25
i'm not sure I follow: why is this needed since th
Peng
2016/06/23 14:15:57
GpuService supports EstablishGpuChannel in both sy
jam
2016/06/23 15:27:23
Why do we need both async and sync modes? The cl d
| |
| 115 DLOG(WARNING) | |
| 116 << "Channel encountered error while establishing gpu channel."; | |
| 117 return nullptr; | |
| 118 } | |
| 119 return gpu_channel_; | |
| 120 } | |
| 121 | |
| 122 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannel() { | |
| 123 base::AutoLock auto_lock(lock_); | |
| 124 return GetGpuChannelLocked(); | |
| 125 } | |
| 126 | |
| 127 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannelLocked() { | |
| 47 if (gpu_channel_ && gpu_channel_->IsLost()) { | 128 if (gpu_channel_ && gpu_channel_->IsLost()) { |
| 48 gpu_channel_->DestroyChannel(); | 129 main_message_loop_->task_runner()->PostTask( |
| 130 FROM_HERE, | |
| 131 base::Bind(&gpu::GpuChannelHost::DestroyChannel, gpu_channel_)); | |
| 49 gpu_channel_ = nullptr; | 132 gpu_channel_ = nullptr; |
| 50 } | 133 } |
| 134 return gpu_channel_; | |
| 135 } | |
| 51 | 136 |
| 52 if (gpu_channel_) | 137 void GpuService::EstablishGpuChannelOnMainThread() { |
| 53 return gpu_channel_; | 138 base::AutoLock auto_lock(lock_); |
| 139 DCHECK(IsMainThread()); | |
| 140 EstablishGpuChannelOnMainThreadLocked(); | |
| 141 } | |
| 54 | 142 |
| 55 mus::mojom::GpuServicePtr gpu_service; | 143 void GpuService::EstablishGpuChannelOnMainThreadLocked() { |
| 56 connector->ConnectToInterface("mojo:mus", &gpu_service); | 144 DCHECK(IsMainThread()); |
| 145 // is_establishing_ is false, it means GpuService::EstablishGpuChannelSync() | |
| 146 // has been used, and we don't need try to establish a new GPU channel | |
| 147 // anymore. | |
| 148 if (!is_establishing_) | |
| 149 return; | |
| 150 connector_->ConnectToInterface("mojo:mus", &gpu_service_); | |
| 151 gpu_service_->EstablishGpuChannel( | |
| 152 base::Bind(&GpuService::EstablishGpuChannelOnMainThreadDone, | |
| 153 base::Unretained(this))); | |
| 154 } | |
| 57 | 155 |
| 58 int client_id = 0; | 156 void GpuService::EstablishGpuChannelOnMainThreadDone( |
| 59 mojom::ChannelHandlePtr channel_handle; | 157 int client_id, |
| 60 mojom::GpuInfoPtr gpu_info; | 158 mojom::ChannelHandlePtr channel_handle, |
| 61 { | 159 mojom::GpuInfoPtr gpu_info) { |
| 62 // TODO(penghuang): Remove the ScopedAllowSyncCall when HW rendering is | 160 DCHECK(IsMainThread()); |
| 63 // enabled in mus chrome. | 161 scoped_refptr<gpu::GpuChannelHost> gpu_channel; |
| 64 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; | 162 if (client_id) { |
| 65 if (!gpu_service->EstablishGpuChannel(&client_id, &channel_handle, | 163 // TODO(penghuang): Get the real gpu info from mus. |
| 66 &gpu_info)) { | 164 gpu_channel = gpu::GpuChannelHost::Create( |
| 67 DLOG(WARNING) | 165 this, client_id, gpu::GPUInfo(), |
| 68 << "Channel encountered error while establishing gpu channel."; | 166 channel_handle.To<IPC::ChannelHandle>(), &shutdown_event_, |
| 69 return nullptr; | 167 gpu_memory_buffer_manager_.get()); |
| 70 } | |
| 71 } | 168 } |
| 72 | 169 |
| 73 // TODO(penghuang): Get the real gpu info from mus. | 170 base::AutoLock auto_lock(lock_); |
| 74 gpu_channel_ = gpu::GpuChannelHost::Create( | 171 DCHECK(is_establishing_); |
| 75 this, client_id, gpu::GPUInfo(), channel_handle.To<IPC::ChannelHandle>(), | 172 DCHECK(!gpu_channel_); |
| 76 &shutdown_event_, gpu_memory_buffer_manager_.get()); | 173 |
| 77 return gpu_channel_; | 174 is_establishing_ = false; |
| 175 gpu_channel_ = gpu_channel; | |
| 176 establishing_condition_.Broadcast(); | |
| 177 | |
| 178 for (const auto& i : establish_callbacks_) | |
| 179 i.Run(); | |
| 180 establish_callbacks_.clear(); | |
| 181 gpu_service_.reset(); | |
| 78 } | 182 } |
| 79 | 183 |
| 80 bool GpuService::IsMainThread() { | 184 bool GpuService::IsMainThread() { |
| 81 return base::MessageLoop::current() == main_message_loop_; | 185 return base::MessageLoop::current() == main_message_loop_; |
| 82 } | 186 } |
| 83 | 187 |
| 84 scoped_refptr<base::SingleThreadTaskRunner> | 188 scoped_refptr<base::SingleThreadTaskRunner> |
| 85 GpuService::GetIOThreadTaskRunner() { | 189 GpuService::GetIOThreadTaskRunner() { |
| 86 return io_thread_.task_runner(); | 190 return io_thread_.task_runner(); |
| 87 } | 191 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 99 MojoResult result = mojo::UnwrapSharedMemoryHandle( | 203 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 100 std::move(handle), &platform_handle, &shared_memory_size, &readonly); | 204 std::move(handle), &platform_handle, &shared_memory_size, &readonly); |
| 101 if (result != MOJO_RESULT_OK) | 205 if (result != MOJO_RESULT_OK) |
| 102 return nullptr; | 206 return nullptr; |
| 103 DCHECK_EQ(shared_memory_size, size); | 207 DCHECK_EQ(shared_memory_size, size); |
| 104 | 208 |
| 105 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); | 209 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); |
| 106 } | 210 } |
| 107 | 211 |
| 108 } // namespace mus | 212 } // namespace mus |
| OLD | NEW |