| 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 "base/threading/thread_restrictions.h" |
| 10 #include "components/mus/common/gpu_type_converters.h" | 10 #include "components/mus/common/gpu_type_converters.h" |
| 11 #include "components/mus/common/mojo_gpu_memory_buffer_manager.h" | |
| 12 #include "components/mus/common/switches.h" | 11 #include "components/mus/common/switches.h" |
| 13 #include "components/mus/public/interfaces/gpu_service.mojom.h" | |
| 14 #include "mojo/public/cpp/system/platform_handle.h" | 12 #include "mojo/public/cpp/system/platform_handle.h" |
| 15 #include "services/shell/public/cpp/connector.h" | 13 #include "services/shell/public/cpp/connector.h" |
| 16 | 14 |
| 17 namespace mus { | 15 namespace mus { |
| 18 | 16 |
| 19 GpuService::GpuService() | 17 GpuService::GpuService() |
| 20 : main_message_loop_(base::MessageLoop::current()), | 18 : main_message_loop_(base::MessageLoop::current()), |
| 21 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 19 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 22 base::WaitableEvent::InitialState::NOT_SIGNALED), | 20 base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 23 io_thread_("GPUIOThread"), | 21 io_thread_("GPUIOThread"), |
| 24 gpu_memory_buffer_manager_(new MojoGpuMemoryBufferManager) { | 22 gpu_memory_buffer_manager_(new MojoGpuMemoryBufferManager), |
| 23 is_establishing_(false), |
| 24 establishing_condition_(&lock_) { |
| 25 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | 25 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
| 26 thread_options.priority = base::ThreadPriority::NORMAL; | 26 thread_options.priority = base::ThreadPriority::NORMAL; |
| 27 CHECK(io_thread_.StartWithOptions(thread_options)); | 27 CHECK(io_thread_.StartWithOptions(thread_options)); |
| 28 } | 28 } |
| 29 | 29 |
| 30 GpuService::~GpuService() {} | 30 GpuService::~GpuService() {} |
| 31 | 31 |
| 32 // static | 32 // static |
| 33 bool GpuService::UseChromeGpuCommandBuffer() { | 33 bool GpuService::UseChromeGpuCommandBuffer() { |
| 34 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 34 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 35 switches::kUseChromeGpuCommandBufferInMus); | 35 switches::kUseChromeGpuCommandBufferInMus); |
| 36 } | 36 } |
| 37 | 37 |
| 38 // static | 38 // static |
| 39 GpuService* GpuService::GetInstance() { | 39 GpuService* GpuService::GetInstance() { |
| 40 return base::Singleton<GpuService, | 40 return base::Singleton<GpuService, |
| 41 base::LeakySingletonTraits<GpuService>>::get(); | 41 base::LeakySingletonTraits<GpuService>>::get(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 scoped_refptr<gpu::GpuChannelHost> GpuService::EstablishGpuChannel( | 44 void GpuService::EstablishGpuChannel(shell::Connector* connector, |
| 45 shell::Connector* connector) { | 45 const base::Closure& callback) { |
| 46 base::AutoLock auto_lock(lock_); | 46 base::AutoLock auto_lock(lock_); |
| 47 if (gpu_channel_ && gpu_channel_->IsLost()) { | 47 auto task_runner = base::ThreadTaskRunnerHandle::Get(); |
| 48 gpu_channel_->DestroyChannel(); | 48 DCHECK(task_runner); |
| 49 gpu_channel_ = nullptr; | 49 |
| 50 if (GetGpuChannelLocked()) { |
| 51 DCHECK(establish_callbacks_.empty()); |
| 52 DCHECK(!is_establishing_); |
| 53 task_runner->PostTask(FROM_HERE, callback); |
| 54 return; |
| 50 } | 55 } |
| 51 | 56 |
| 52 if (gpu_channel_) | 57 establish_callbacks_.push_back(std::make_pair(task_runner, callback)); |
| 58 if (!is_establishing_) { |
| 59 is_establishing_ = true; |
| 60 mus::mojom::GpuServicePtr gpu_service; |
| 61 connector->ConnectToInterface("mojo:mus", &gpu_service); |
| 62 const bool locked = false; |
| 63 gpu_service->EstablishGpuChannel(base::Bind( |
| 64 &GpuService::EstablishGpuChannelDone, base::Unretained(this), locked)); |
| 65 } |
| 66 } |
| 67 |
| 68 scoped_refptr<gpu::GpuChannelHost> GpuService::EstablishGpuChannelSync( |
| 69 shell::Connector* connector) { |
| 70 fprintf(stderr, "EEE %d %s %d\n", getpid(), __PRETTY_FUNCTION__, __LINE__); |
| 71 base::AutoLock auto_lock(lock_); |
| 72 if (GetGpuChannelLocked()) |
| 53 return gpu_channel_; | 73 return gpu_channel_; |
| 54 | 74 |
| 55 mus::mojom::GpuServicePtr gpu_service; | 75 if (is_establishing_) { |
| 56 connector->ConnectToInterface("mojo:mus", &gpu_service); | 76 do { |
| 77 establishing_condition_.Wait(); |
| 78 } while (is_establishing_); |
| 79 fprintf(stderr, "EEE %d %s %d\n", getpid(), __PRETTY_FUNCTION__, __LINE__); |
| 80 return gpu_channel_; |
| 81 } |
| 82 |
| 83 is_establishing_ = true; |
| 57 | 84 |
| 58 int client_id = 0; | 85 int client_id = 0; |
| 59 mojom::ChannelHandlePtr channel_handle; | 86 mojom::ChannelHandlePtr channel_handle; |
| 60 mojom::GpuInfoPtr gpu_info; | 87 mojom::GpuInfoPtr gpu_info; |
| 88 mus::mojom::GpuServicePtr gpu_service; |
| 89 connector->ConnectToInterface("mojo:mus", &gpu_service); |
| 90 |
| 61 { | 91 { |
| 92 // Unlock the |lock_| during the sync mojo method call. |
| 93 base::AutoUnlock auto_unlock(lock_); |
| 62 // TODO(penghuang): Remove the ScopedAllowWait when HW rendering is enabled | 94 // TODO(penghuang): Remove the ScopedAllowWait when HW rendering is enabled |
| 63 // in mus chrome. | 95 // in mus chrome. |
| 64 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 96 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 65 if (!gpu_service->EstablishGpuChannel(&client_id, &channel_handle, | 97 if (!gpu_service->EstablishGpuChannel(&client_id, &channel_handle, |
| 66 &gpu_info)) { | 98 &gpu_info)) { |
| 67 DLOG(WARNING) | 99 DLOG(WARNING) |
| 68 << "Channel encountered error while establishing gpu channel."; | 100 << "Channel encountered error while establishing gpu channel."; |
| 69 return nullptr; | |
| 70 } | 101 } |
| 71 } | 102 } |
| 72 | 103 |
| 73 // TODO(penghuang): Get the real gpu info from mus. | 104 const bool locked = true; |
| 74 gpu_channel_ = gpu::GpuChannelHost::Create( | 105 EstablishGpuChannelDone(locked, client_id, std::move(channel_handle), |
| 75 this, client_id, gpu::GPUInfo(), channel_handle.To<IPC::ChannelHandle>(), | 106 std::move(gpu_info)); |
| 76 &shutdown_event_, gpu_memory_buffer_manager_.get()); | 107 fprintf(stderr, "EEE %d %s %d\n", getpid(), __PRETTY_FUNCTION__, __LINE__); |
| 77 return gpu_channel_; | 108 return gpu_channel_; |
| 78 } | 109 } |
| 79 | 110 |
| 111 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannel() { |
| 112 base::AutoLock auto_lock(lock_); |
| 113 return GetGpuChannelLocked(); |
| 114 } |
| 115 |
| 116 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannelLocked() { |
| 117 if (gpu_channel_ && gpu_channel_->IsLost()) { |
| 118 gpu_channel_->DestroyChannel(); |
| 119 gpu_channel_ = nullptr; |
| 120 } |
| 121 return gpu_channel_; |
| 122 } |
| 123 |
| 124 void GpuService::EstablishGpuChannelDone(bool locked, |
| 125 int client_id, |
| 126 mojom::ChannelHandlePtr channel_handle, |
| 127 mojom::GpuInfoPtr gpu_info) { |
| 128 scoped_refptr<gpu::GpuChannelHost> gpu_channel; |
| 129 if (client_id) { |
| 130 // TODO(penghuang): Get the real gpu info from mus. |
| 131 gpu_channel = gpu::GpuChannelHost::Create( |
| 132 this, client_id, gpu::GPUInfo(), |
| 133 channel_handle.To<IPC::ChannelHandle>(), &shutdown_event_, |
| 134 gpu_memory_buffer_manager_.get()); |
| 135 } |
| 136 |
| 137 std::unique_ptr<base::AutoLock> auto_lock; |
| 138 if (!locked) |
| 139 auto_lock = base::MakeUnique<base::AutoLock>(lock_); |
| 140 |
| 141 DCHECK(is_establishing_); |
| 142 DCHECK(!gpu_channel_); |
| 143 |
| 144 is_establishing_ = false; |
| 145 gpu_channel_ = gpu_channel; |
| 146 establishing_condition_.Broadcast(); |
| 147 |
| 148 for (const auto& i : establish_callbacks_) |
| 149 i.first->PostTask(FROM_HERE, i.second); |
| 150 establish_callbacks_.clear(); |
| 151 } |
| 152 |
| 80 bool GpuService::IsMainThread() { | 153 bool GpuService::IsMainThread() { |
| 81 return base::MessageLoop::current() == main_message_loop_; | 154 return base::MessageLoop::current() == main_message_loop_; |
| 82 } | 155 } |
| 83 | 156 |
| 84 scoped_refptr<base::SingleThreadTaskRunner> | 157 scoped_refptr<base::SingleThreadTaskRunner> |
| 85 GpuService::GetIOThreadTaskRunner() { | 158 GpuService::GetIOThreadTaskRunner() { |
| 86 return io_thread_.task_runner(); | 159 return io_thread_.task_runner(); |
| 87 } | 160 } |
| 88 | 161 |
| 89 std::unique_ptr<base::SharedMemory> GpuService::AllocateSharedMemory( | 162 std::unique_ptr<base::SharedMemory> GpuService::AllocateSharedMemory( |
| 90 size_t size) { | 163 size_t size) { |
| 91 mojo::ScopedSharedBufferHandle handle = | 164 mojo::ScopedSharedBufferHandle handle = |
| 92 mojo::SharedBufferHandle::Create(size); | 165 mojo::SharedBufferHandle::Create(size); |
| 93 if (!handle.is_valid()) | 166 if (!handle.is_valid()) |
| 94 return nullptr; | 167 return nullptr; |
| 95 | 168 |
| 96 base::SharedMemoryHandle platform_handle; | 169 base::SharedMemoryHandle platform_handle; |
| 97 size_t shared_memory_size; | 170 size_t shared_memory_size; |
| 98 bool readonly; | 171 bool readonly; |
| 99 MojoResult result = mojo::UnwrapSharedMemoryHandle( | 172 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 100 std::move(handle), &platform_handle, &shared_memory_size, &readonly); | 173 std::move(handle), &platform_handle, &shared_memory_size, &readonly); |
| 101 if (result != MOJO_RESULT_OK) | 174 if (result != MOJO_RESULT_OK) |
| 102 return nullptr; | 175 return nullptr; |
| 103 DCHECK_EQ(shared_memory_size, size); | 176 DCHECK_EQ(shared_memory_size, size); |
| 104 | 177 |
| 105 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); | 178 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); |
| 106 } | 179 } |
| 107 | 180 |
| 108 } // namespace mus | 181 } // namespace mus |
| OLD | NEW |