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 "components/mus/public/cpp/lib/gpu_service.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/memory/singleton.h" |
| 9 #include "components/mus/common/gpu_type_converters.h" |
| 10 #include "components/mus/common/switches.h" |
| 11 #include "components/mus/public/cpp/lib/gpu_memory_buffer_manager_mus.h" |
| 12 #include "components/mus/public/interfaces/gpu_service.mojom.h" |
| 13 #include "services/shell/public/cpp/connector.h" |
| 14 |
| 15 namespace mus { |
| 16 |
| 17 namespace { |
| 18 |
| 19 void GpuChannelEstablishCallback(int* client_id_out, |
| 20 IPC::ChannelHandle* channel_handle_out, |
| 21 gpu::GPUInfo* gpu_info_out, |
| 22 int client_id, |
| 23 mus::mojom::ChannelHandlePtr channel_handle, |
| 24 mus::mojom::GpuInfoPtr gpu_info) { |
| 25 *client_id_out = client_id; |
| 26 *channel_handle_out = channel_handle.To<IPC::ChannelHandle>(); |
| 27 // TODO(penghuang): Get the gpu info. |
| 28 } |
| 29 } |
| 30 |
| 31 GpuService::GpuService() |
| 32 : main_message_loop_(base::MessageLoop::current()), |
| 33 shutdown_event_(false, false), |
| 34 io_thread_("GPUIOThread"), |
| 35 gpu_memory_buffer_manager_(new mus::GpuMemoryBufferManagerMus) { |
| 36 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
| 37 thread_options.priority = base::ThreadPriority::NORMAL; |
| 38 CHECK(io_thread_.StartWithOptions(thread_options)); |
| 39 } |
| 40 |
| 41 GpuService::~GpuService() {} |
| 42 |
| 43 // static |
| 44 bool GpuService::UseChromeGpuCommandBuffer() { |
| 45 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 46 switches::kUseChromeGpuCommandBufferInMus); |
| 47 } |
| 48 |
| 49 // static |
| 50 GpuService* GpuService::GetInstance() { |
| 51 return base::Singleton<GpuService, |
| 52 base::LeakySingletonTraits<GpuService>>::get(); |
| 53 } |
| 54 |
| 55 scoped_refptr<gpu::GpuChannelHost> GpuService::EstablishGpuChannel( |
| 56 shell::Connector* connector) { |
| 57 if (gpu_channel_ && gpu_channel_->IsLost()) { |
| 58 gpu_channel_->DestroyChannel(); |
| 59 gpu_channel_ = nullptr; |
| 60 } |
| 61 |
| 62 if (gpu_channel_) |
| 63 return gpu_channel_; |
| 64 |
| 65 mus::mojom::GpuServicePtr gpu_service; |
| 66 connector->ConnectToInterface("mojo:mus", &gpu_service); |
| 67 |
| 68 int client_id = 0; |
| 69 IPC::ChannelHandle channel_handle; |
| 70 gpu::GPUInfo gpu_info; |
| 71 gpu_service->EstablishGpuChannel(base::Bind( |
| 72 &GpuChannelEstablishCallback, &client_id, &channel_handle, &gpu_info)); |
| 73 if (!gpu_service.WaitForIncomingResponse()) { |
| 74 DLOG(WARNING) |
| 75 << "Channel encountered error while establishing gpu channel."; |
| 76 return nullptr; |
| 77 } |
| 78 gpu_channel_ = gpu::GpuChannelHost::Create(this, client_id, gpu_info, |
| 79 channel_handle, &shutdown_event_, |
| 80 gpu_memory_buffer_manager_.get()); |
| 81 return gpu_channel_; |
| 82 } |
| 83 |
| 84 bool GpuService::IsMainThread() { |
| 85 return base::MessageLoop::current() == main_message_loop_; |
| 86 } |
| 87 |
| 88 scoped_refptr<base::SingleThreadTaskRunner> |
| 89 GpuService::GetIOThreadTaskRunner() { |
| 90 return io_thread_.task_runner(); |
| 91 } |
| 92 |
| 93 std::unique_ptr<base::SharedMemory> GpuService::AllocateSharedMemory( |
| 94 size_t size) { |
| 95 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); |
| 96 if (!shm->CreateAnonymous(size)) |
| 97 return std::unique_ptr<base::SharedMemory>(); |
| 98 return shm; |
| 99 } |
| 100 |
| 101 } // namespace mus |
OLD | NEW |