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/gpu_service.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "components/mus/common/gpu_type_converters.h" |
| 9 #include "components/mus/common/switches.h" |
| 10 #include "components/mus/public/cpp/lib/gpu_memory_buffer_manager_mus.h" |
| 11 #include "components/mus/public/interfaces/gpu_service.mojom.h" |
| 12 #include "gpu/ipc/client/gpu_channel_host.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 base::LazyInstance<GpuService> g_gpu_service = LAZY_INSTANCE_INITIALIZER; |
| 31 } |
| 32 |
| 33 GpuService::GpuService() |
| 34 : initialized_(false), |
| 35 main_message_loop_(base::MessageLoop::current()), |
| 36 shutdown_event_(false, false), |
| 37 io_thread_("GPUIOThread") {} |
| 38 |
| 39 GpuService::~GpuService() {} |
| 40 |
| 41 // static |
| 42 bool GpuService::UseChromeGpuCommandBuffer() { |
| 43 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 44 switches::kUseChromeGpuCommandBufferInMus); |
| 45 } |
| 46 |
| 47 // static |
| 48 void GpuService::Initialize(shell::Connector* connector) { |
| 49 g_gpu_service.Get().Init(connector); |
| 50 } |
| 51 |
| 52 // static |
| 53 GpuService* GpuService::GetInstance() { |
| 54 DCHECK(!(g_gpu_service == nullptr)); |
| 55 return g_gpu_service.Pointer(); |
| 56 } |
| 57 |
| 58 void GpuService::Init(shell::Connector* connector) { |
| 59 if (initialized_) |
| 60 return; |
| 61 initialized_ = true; |
| 62 |
| 63 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
| 64 thread_options.priority = base::ThreadPriority::NORMAL; |
| 65 CHECK(io_thread_.StartWithOptions(thread_options)); |
| 66 |
| 67 mus::mojom::GpuServicePtr gpu_service; |
| 68 connector->ConnectToInterface("mojo:mus", &gpu_service); |
| 69 |
| 70 int client_id = 0; |
| 71 IPC::ChannelHandle channel_handle; |
| 72 gpu::GPUInfo gpu_info; |
| 73 gpu_service->EstablishGpuChannel(base::Bind( |
| 74 &GpuChannelEstablishCallback, &client_id, &channel_handle, &gpu_info)); |
| 75 CHECK(gpu_service.WaitForIncomingResponse()); |
| 76 gpu_memory_buffer_manager_.reset(new mus::GpuMemoryBufferManagerMus()); |
| 77 gpu_channel_ = gpu::GpuChannelHost::Create(this, client_id, gpu_info, |
| 78 channel_handle, &shutdown_event_, |
| 79 gpu_memory_buffer_manager_.get()); |
| 80 } |
| 81 |
| 82 bool GpuService::IsMainThread() { |
| 83 return base::MessageLoop::current() == main_message_loop_; |
| 84 } |
| 85 |
| 86 scoped_refptr<base::SingleThreadTaskRunner> |
| 87 GpuService::GetIOThreadTaskRunner() { |
| 88 return io_thread_.task_runner(); |
| 89 } |
| 90 |
| 91 std::unique_ptr<base::SharedMemory> GpuService::AllocateSharedMemory( |
| 92 size_t size) { |
| 93 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); |
| 94 if (!shm->CreateAnonymous(size)) |
| 95 return std::unique_ptr<base::SharedMemory>(); |
| 96 return shm; |
| 97 } |
| 98 |
| 99 } // namespace mus |
OLD | NEW |