Index: components/mus/common/gpu_service.cc |
diff --git a/components/mus/common/gpu_service.cc b/components/mus/common/gpu_service.cc |
index 833e62b339ad2cd8911f0d1d7c01166f84f9add8..0d9ac6c20c68e368a378b793309bea8b580b6d43 100644 |
--- a/components/mus/common/gpu_service.cc |
+++ b/components/mus/common/gpu_service.cc |
@@ -6,22 +6,37 @@ |
#include "base/command_line.h" |
#include "base/memory/singleton.h" |
+#include "base/threading/thread_restrictions.h" |
#include "components/mus/common/gpu_type_converters.h" |
-#include "components/mus/common/mojo_gpu_memory_buffer_manager.h" |
#include "components/mus/common/switches.h" |
#include "components/mus/public/interfaces/gpu_service.mojom.h" |
-#include "mojo/public/cpp/bindings/sync_call_restrictions.h" |
#include "mojo/public/cpp/system/platform_handle.h" |
#include "services/shell/public/cpp/connector.h" |
namespace mus { |
-GpuService::GpuService() |
+namespace { |
+ |
+void PostTask(scoped_refptr<base::SingleThreadTaskRunner> runner, |
+ const tracked_objects::Location& from_here, |
+ const base::Closure& callback) { |
+ runner->PostTask(from_here, callback); |
+} |
+ |
+GpuService* g_gpu_service = nullptr; |
+} |
+ |
+GpuService::GpuService(shell::Connector* connector) |
: main_message_loop_(base::MessageLoop::current()), |
+ connector_(connector), |
shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
base::WaitableEvent::InitialState::NOT_SIGNALED), |
io_thread_("GPUIOThread"), |
- gpu_memory_buffer_manager_(new MojoGpuMemoryBufferManager) { |
+ gpu_memory_buffer_manager_(new MojoGpuMemoryBufferManager), |
+ is_establishing_(false), |
+ establishing_condition_(&lock_) { |
+ DCHECK(main_message_loop_); |
+ DCHECK(connector_); |
base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
thread_options.priority = base::ThreadPriority::NORMAL; |
CHECK(io_thread_.StartWithOptions(thread_options)); |
@@ -36,47 +51,136 @@ bool GpuService::UseChromeGpuCommandBuffer() { |
} |
// static |
+void GpuService::Initialize(shell::Connector* connector) { |
+ DCHECK(!g_gpu_service); |
+ g_gpu_service = new GpuService(connector); |
+} |
+ |
+// static |
GpuService* GpuService::GetInstance() { |
- return base::Singleton<GpuService, |
- base::LeakySingletonTraits<GpuService>>::get(); |
+ DCHECK(g_gpu_service); |
+ return g_gpu_service; |
} |
-scoped_refptr<gpu::GpuChannelHost> GpuService::EstablishGpuChannel( |
- shell::Connector* connector) { |
+void GpuService::EstablishGpuChannel(const base::Closure& callback) { |
base::AutoLock auto_lock(lock_); |
- if (gpu_channel_ && gpu_channel_->IsLost()) { |
- gpu_channel_->DestroyChannel(); |
- gpu_channel_ = nullptr; |
+ auto runner = base::ThreadTaskRunnerHandle::Get(); |
+ if (GetGpuChannelLocked()) { |
+ runner->PostTask(FROM_HERE, callback); |
+ return; |
} |
+ base::Closure wrapper_callback = |
+ IsMainThread() ? callback |
+ : base::Bind(PostTask, runner, FROM_HERE, callback); |
+ establish_callbacks_.push_back(wrapper_callback); |
+ |
+ if (!is_establishing_) { |
+ is_establishing_ = true; |
+ main_message_loop_->task_runner()->PostTask( |
+ FROM_HERE, base::Bind(&GpuService::EstablishGpuChannelOnMainThread, |
+ base::Unretained(this))); |
+ } |
+} |
- if (gpu_channel_) |
+scoped_refptr<gpu::GpuChannelHost> GpuService::EstablishGpuChannelSync() { |
+ base::AutoLock auto_lock(lock_); |
+ if (GetGpuChannelLocked()) |
return gpu_channel_; |
- mus::mojom::GpuServicePtr gpu_service; |
- connector->ConnectToInterface("mojo:mus", &gpu_service); |
- |
- int client_id = 0; |
- mojom::ChannelHandlePtr channel_handle; |
- mojom::GpuInfoPtr gpu_info; |
- { |
- // TODO(penghuang): Remove the ScopedAllowSyncCall when HW rendering is |
- // enabled in mus chrome. |
- mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
- if (!gpu_service->EstablishGpuChannel(&client_id, &channel_handle, |
- &gpu_info)) { |
- DLOG(WARNING) |
- << "Channel encountered error while establishing gpu channel."; |
- return nullptr; |
+ if (!IsMainThread()) { |
+ if (!is_establishing_) { |
+ // Create an establishing gpu channel task, if there isn't one. |
+ is_establishing_ = true; |
+ main_message_loop_->task_runner()->PostTask( |
+ FROM_HERE, base::Bind(&GpuService::EstablishGpuChannelOnMainThread, |
+ base::Unretained(this))); |
} |
+ // Wait until the pending establishing task is finished. |
+ do { |
+ establishing_condition_.Wait(); |
+ } while (is_establishing_); |
+ return gpu_channel_; |
+ } |
+ |
+ if (!gpu_service_) { |
+ // gpu_service_ is null, it means there is no unfinished async |
+ // EstablishGpuChannel call, so we should issue one. |
+ is_establishing_ = true; |
+ EstablishGpuChannelOnMainThreadLocked(); |
} |
- // TODO(penghuang): Get the real gpu info from mus. |
- gpu_channel_ = gpu::GpuChannelHost::Create( |
- this, client_id, gpu::GPUInfo(), channel_handle.To<IPC::ChannelHandle>(), |
- &shutdown_event_, gpu_memory_buffer_manager_.get()); |
+ base::AutoUnlock auto_unlock(lock_); |
+ base::ThreadRestrictions::ScopedAllowWait allow_wait; |
+ 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
|
+ DLOG(WARNING) |
+ << "Channel encountered error while establishing gpu channel."; |
+ return nullptr; |
+ } |
return gpu_channel_; |
} |
+scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannel() { |
+ base::AutoLock auto_lock(lock_); |
+ return GetGpuChannelLocked(); |
+} |
+ |
+scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannelLocked() { |
+ if (gpu_channel_ && gpu_channel_->IsLost()) { |
+ main_message_loop_->task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&gpu::GpuChannelHost::DestroyChannel, gpu_channel_)); |
+ gpu_channel_ = nullptr; |
+ } |
+ return gpu_channel_; |
+} |
+ |
+void GpuService::EstablishGpuChannelOnMainThread() { |
+ base::AutoLock auto_lock(lock_); |
+ DCHECK(IsMainThread()); |
+ EstablishGpuChannelOnMainThreadLocked(); |
+} |
+ |
+void GpuService::EstablishGpuChannelOnMainThreadLocked() { |
+ DCHECK(IsMainThread()); |
+ // is_establishing_ is false, it means GpuService::EstablishGpuChannelSync() |
+ // has been used, and we don't need try to establish a new GPU channel |
+ // anymore. |
+ if (!is_establishing_) |
+ return; |
+ connector_->ConnectToInterface("mojo:mus", &gpu_service_); |
+ gpu_service_->EstablishGpuChannel( |
+ base::Bind(&GpuService::EstablishGpuChannelOnMainThreadDone, |
+ base::Unretained(this))); |
+} |
+ |
+void GpuService::EstablishGpuChannelOnMainThreadDone( |
+ int client_id, |
+ mojom::ChannelHandlePtr channel_handle, |
+ mojom::GpuInfoPtr gpu_info) { |
+ DCHECK(IsMainThread()); |
+ scoped_refptr<gpu::GpuChannelHost> gpu_channel; |
+ if (client_id) { |
+ // TODO(penghuang): Get the real gpu info from mus. |
+ gpu_channel = gpu::GpuChannelHost::Create( |
+ this, client_id, gpu::GPUInfo(), |
+ channel_handle.To<IPC::ChannelHandle>(), &shutdown_event_, |
+ gpu_memory_buffer_manager_.get()); |
+ } |
+ |
+ base::AutoLock auto_lock(lock_); |
+ DCHECK(is_establishing_); |
+ DCHECK(!gpu_channel_); |
+ |
+ is_establishing_ = false; |
+ gpu_channel_ = gpu_channel; |
+ establishing_condition_.Broadcast(); |
+ |
+ for (const auto& i : establish_callbacks_) |
+ i.Run(); |
+ establish_callbacks_.clear(); |
+ gpu_service_.reset(); |
+} |
+ |
bool GpuService::IsMainThread() { |
return base::MessageLoop::current() == main_message_loop_; |
} |