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 "services/ui/public/cpp/gpu/gpu_service.h" | 5 #include "services/ui/public/cpp/gpu/gpu.h" |
6 | 6 |
7 #include "base/threading/thread_task_runner_handle.h" | 7 #include "base/threading/thread_task_runner_handle.h" |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 #include "mojo/public/cpp/bindings/sync_call_restrictions.h" | 9 #include "mojo/public/cpp/bindings/sync_call_restrictions.h" |
10 #include "mojo/public/cpp/system/platform_handle.h" | 10 #include "mojo/public/cpp/system/platform_handle.h" |
11 #include "services/service_manager/public/cpp/connector.h" | 11 #include "services/service_manager/public/cpp/connector.h" |
12 #include "services/ui/public/cpp/gpu/client_gpu_memory_buffer_manager.h" | 12 #include "services/ui/public/cpp/gpu/client_gpu_memory_buffer_manager.h" |
13 #include "services/ui/public/interfaces/constants.mojom.h" | 13 #include "services/ui/public/interfaces/constants.mojom.h" |
14 #include "services/ui/public/interfaces/gpu_service.mojom.h" | 14 #include "services/ui/public/interfaces/gpu.mojom.h" |
15 | 15 |
16 namespace ui { | 16 namespace ui { |
17 | 17 |
18 GpuService::GpuService(service_manager::Connector* connector, | 18 Gpu::Gpu(service_manager::Connector* connector, |
19 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 19 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
20 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 20 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
21 io_task_runner_(std::move(task_runner)), | 21 io_task_runner_(std::move(task_runner)), |
22 connector_(connector), | 22 connector_(connector), |
23 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 23 shutdown_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
24 base::WaitableEvent::InitialState::NOT_SIGNALED) { | 24 base::WaitableEvent::InitialState::NOT_SIGNALED) { |
25 DCHECK(main_task_runner_); | 25 DCHECK(main_task_runner_); |
26 DCHECK(connector_); | 26 DCHECK(connector_); |
27 mojom::GpuServicePtr gpu_service_ptr; | 27 mojom::GpuPtr gpu_service_ptr; |
28 connector_->ConnectToInterface(ui::mojom::kServiceName, &gpu_service_ptr); | 28 connector_->ConnectToInterface(ui::mojom::kServiceName, &gpu_service_ptr); |
29 gpu_memory_buffer_manager_ = base::MakeUnique<ClientGpuMemoryBufferManager>( | 29 gpu_memory_buffer_manager_ = base::MakeUnique<ClientGpuMemoryBufferManager>( |
30 std::move(gpu_service_ptr)); | 30 std::move(gpu_service_ptr)); |
31 if (!io_task_runner_) { | 31 if (!io_task_runner_) { |
32 io_thread_.reset(new base::Thread("GPUIOThread")); | 32 io_thread_.reset(new base::Thread("GPUIOThread")); |
33 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | 33 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
34 thread_options.priority = base::ThreadPriority::NORMAL; | 34 thread_options.priority = base::ThreadPriority::NORMAL; |
35 CHECK(io_thread_->StartWithOptions(thread_options)); | 35 CHECK(io_thread_->StartWithOptions(thread_options)); |
36 io_task_runner_ = io_thread_->task_runner(); | 36 io_task_runner_ = io_thread_->task_runner(); |
37 } | 37 } |
38 } | 38 } |
39 | 39 |
40 GpuService::~GpuService() { | 40 Gpu::~Gpu() { |
41 DCHECK(IsMainThread()); | 41 DCHECK(IsMainThread()); |
42 for (const auto& callback : establish_callbacks_) | 42 for (const auto& callback : establish_callbacks_) |
43 callback.Run(nullptr); | 43 callback.Run(nullptr); |
44 shutdown_event_.Signal(); | 44 shutdown_event_.Signal(); |
45 if (gpu_channel_) | 45 if (gpu_channel_) |
46 gpu_channel_->DestroyChannel(); | 46 gpu_channel_->DestroyChannel(); |
47 } | 47 } |
48 | 48 |
49 // static | 49 // static |
50 std::unique_ptr<GpuService> GpuService::Create( | 50 std::unique_ptr<Gpu> Gpu::Create( |
51 service_manager::Connector* connector, | 51 service_manager::Connector* connector, |
52 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 52 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
53 return base::WrapUnique(new GpuService(connector, std::move(task_runner))); | 53 return base::WrapUnique(new Gpu(connector, std::move(task_runner))); |
54 } | 54 } |
55 | 55 |
56 void GpuService::EstablishGpuChannel( | 56 void Gpu::EstablishGpuChannel( |
57 const gpu::GpuChannelEstablishedCallback& callback) { | 57 const gpu::GpuChannelEstablishedCallback& callback) { |
58 DCHECK(IsMainThread()); | 58 DCHECK(IsMainThread()); |
59 scoped_refptr<gpu::GpuChannelHost> channel = GetGpuChannel(); | 59 scoped_refptr<gpu::GpuChannelHost> channel = GetGpuChannel(); |
60 if (channel) { | 60 if (channel) { |
61 main_task_runner_->PostTask(FROM_HERE, | 61 main_task_runner_->PostTask(FROM_HERE, |
62 base::Bind(callback, std::move(channel))); | 62 base::Bind(callback, std::move(channel))); |
63 return; | 63 return; |
64 } | 64 } |
65 establish_callbacks_.push_back(callback); | 65 establish_callbacks_.push_back(callback); |
66 if (gpu_service_) | 66 if (gpu_service_) |
67 return; | 67 return; |
68 | 68 |
69 connector_->ConnectToInterface(ui::mojom::kServiceName, &gpu_service_); | 69 connector_->ConnectToInterface(ui::mojom::kServiceName, &gpu_service_); |
70 gpu_service_->EstablishGpuChannel( | 70 gpu_service_->EstablishGpuChannel( |
71 base::Bind(&GpuService::OnEstablishedGpuChannel, base::Unretained(this))); | 71 base::Bind(&Gpu::OnEstablishedGpuChannel, base::Unretained(this))); |
72 } | 72 } |
73 | 73 |
74 scoped_refptr<gpu::GpuChannelHost> GpuService::EstablishGpuChannelSync() { | 74 scoped_refptr<gpu::GpuChannelHost> Gpu::EstablishGpuChannelSync() { |
75 DCHECK(IsMainThread()); | 75 DCHECK(IsMainThread()); |
76 if (GetGpuChannel()) | 76 if (GetGpuChannel()) |
77 return gpu_channel_; | 77 return gpu_channel_; |
78 | 78 |
79 int client_id = 0; | 79 int client_id = 0; |
80 mojo::ScopedMessagePipeHandle channel_handle; | 80 mojo::ScopedMessagePipeHandle channel_handle; |
81 gpu::GPUInfo gpu_info; | 81 gpu::GPUInfo gpu_info; |
82 connector_->ConnectToInterface(ui::mojom::kServiceName, &gpu_service_); | 82 connector_->ConnectToInterface(ui::mojom::kServiceName, &gpu_service_); |
83 | 83 |
84 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; | 84 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
85 if (!gpu_service_->EstablishGpuChannel(&client_id, &channel_handle, | 85 if (!gpu_service_->EstablishGpuChannel(&client_id, &channel_handle, |
86 &gpu_info)) { | 86 &gpu_info)) { |
87 DLOG(WARNING) | 87 DLOG(WARNING) |
88 << "Channel encountered error while establishing gpu channel."; | 88 << "Channel encountered error while establishing gpu channel."; |
89 return nullptr; | 89 return nullptr; |
90 } | 90 } |
91 OnEstablishedGpuChannel(client_id, std::move(channel_handle), gpu_info); | 91 OnEstablishedGpuChannel(client_id, std::move(channel_handle), gpu_info); |
92 return gpu_channel_; | 92 return gpu_channel_; |
93 } | 93 } |
94 | 94 |
95 gpu::GpuMemoryBufferManager* GpuService::GetGpuMemoryBufferManager() { | 95 gpu::GpuMemoryBufferManager* Gpu::GetGpuMemoryBufferManager() { |
96 return gpu_memory_buffer_manager_.get(); | 96 return gpu_memory_buffer_manager_.get(); |
97 } | 97 } |
98 | 98 |
99 scoped_refptr<gpu::GpuChannelHost> GpuService::GetGpuChannel() { | 99 scoped_refptr<gpu::GpuChannelHost> Gpu::GetGpuChannel() { |
100 DCHECK(IsMainThread()); | 100 DCHECK(IsMainThread()); |
101 if (gpu_channel_ && gpu_channel_->IsLost()) { | 101 if (gpu_channel_ && gpu_channel_->IsLost()) { |
102 gpu_channel_->DestroyChannel(); | 102 gpu_channel_->DestroyChannel(); |
103 gpu_channel_ = nullptr; | 103 gpu_channel_ = nullptr; |
104 } | 104 } |
105 return gpu_channel_; | 105 return gpu_channel_; |
106 } | 106 } |
107 | 107 |
108 void GpuService::OnEstablishedGpuChannel( | 108 void Gpu::OnEstablishedGpuChannel(int client_id, |
109 int client_id, | 109 mojo::ScopedMessagePipeHandle channel_handle, |
110 mojo::ScopedMessagePipeHandle channel_handle, | 110 const gpu::GPUInfo& gpu_info) { |
111 const gpu::GPUInfo& gpu_info) { | |
112 DCHECK(IsMainThread()); | 111 DCHECK(IsMainThread()); |
113 DCHECK(gpu_service_.get()); | 112 DCHECK(gpu_service_.get()); |
114 DCHECK(!gpu_channel_); | 113 DCHECK(!gpu_channel_); |
115 | 114 |
116 if (client_id && channel_handle.is_valid()) { | 115 if (client_id && channel_handle.is_valid()) { |
117 gpu_channel_ = gpu::GpuChannelHost::Create( | 116 gpu_channel_ = gpu::GpuChannelHost::Create( |
118 this, client_id, gpu_info, IPC::ChannelHandle(channel_handle.release()), | 117 this, client_id, gpu_info, IPC::ChannelHandle(channel_handle.release()), |
119 &shutdown_event_, gpu_memory_buffer_manager_.get()); | 118 &shutdown_event_, gpu_memory_buffer_manager_.get()); |
120 } | 119 } |
121 | 120 |
122 gpu_service_.reset(); | 121 gpu_service_.reset(); |
123 for (const auto& i : establish_callbacks_) | 122 for (const auto& i : establish_callbacks_) |
124 i.Run(gpu_channel_); | 123 i.Run(gpu_channel_); |
125 establish_callbacks_.clear(); | 124 establish_callbacks_.clear(); |
126 } | 125 } |
127 | 126 |
128 bool GpuService::IsMainThread() { | 127 bool Gpu::IsMainThread() { |
129 return main_task_runner_->BelongsToCurrentThread(); | 128 return main_task_runner_->BelongsToCurrentThread(); |
130 } | 129 } |
131 | 130 |
132 scoped_refptr<base::SingleThreadTaskRunner> | 131 scoped_refptr<base::SingleThreadTaskRunner> Gpu::GetIOThreadTaskRunner() { |
133 GpuService::GetIOThreadTaskRunner() { | |
134 return io_task_runner_; | 132 return io_task_runner_; |
135 } | 133 } |
136 | 134 |
137 std::unique_ptr<base::SharedMemory> GpuService::AllocateSharedMemory( | 135 std::unique_ptr<base::SharedMemory> Gpu::AllocateSharedMemory(size_t size) { |
138 size_t size) { | |
139 mojo::ScopedSharedBufferHandle handle = | 136 mojo::ScopedSharedBufferHandle handle = |
140 mojo::SharedBufferHandle::Create(size); | 137 mojo::SharedBufferHandle::Create(size); |
141 if (!handle.is_valid()) | 138 if (!handle.is_valid()) |
142 return nullptr; | 139 return nullptr; |
143 | 140 |
144 base::SharedMemoryHandle platform_handle; | 141 base::SharedMemoryHandle platform_handle; |
145 size_t shared_memory_size; | 142 size_t shared_memory_size; |
146 bool readonly; | 143 bool readonly; |
147 MojoResult result = mojo::UnwrapSharedMemoryHandle( | 144 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
148 std::move(handle), &platform_handle, &shared_memory_size, &readonly); | 145 std::move(handle), &platform_handle, &shared_memory_size, &readonly); |
149 if (result != MOJO_RESULT_OK) | 146 if (result != MOJO_RESULT_OK) |
150 return nullptr; | 147 return nullptr; |
151 DCHECK_EQ(shared_memory_size, size); | 148 DCHECK_EQ(shared_memory_size, size); |
152 | 149 |
153 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); | 150 return base::MakeUnique<base::SharedMemory>(platform_handle, readonly); |
154 } | 151 } |
155 | 152 |
156 } // namespace ui | 153 } // namespace ui |
OLD | NEW |