OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/common/gpu/gpu_channel_manager.h" | 5 #include "content/common/gpu/gpu_channel_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
16 #include "content/common/gpu/establish_channel_params.h" | |
17 #include "content/common/gpu/gpu_channel.h" | 16 #include "content/common/gpu/gpu_channel.h" |
18 #include "content/common/gpu/gpu_channel_manager_delegate.h" | |
19 #include "content/common/gpu/gpu_memory_buffer_factory.h" | 17 #include "content/common/gpu/gpu_memory_buffer_factory.h" |
20 #include "content/common/gpu/gpu_memory_manager.h" | 18 #include "content/common/gpu/gpu_memory_manager.h" |
21 #include "content/common/gpu/gpu_messages.h" | 19 #include "content/common/gpu/gpu_messages.h" |
22 #include "content/common/gpu/image_transport_surface.h" | |
23 #include "content/public/common/content_switches.h" | 20 #include "content/public/common/content_switches.h" |
24 #include "gpu/command_buffer/common/sync_token.h" | |
25 #include "gpu/command_buffer/common/value_state.h" | 21 #include "gpu/command_buffer/common/value_state.h" |
26 #include "gpu/command_buffer/service/feature_info.h" | 22 #include "gpu/command_buffer/service/feature_info.h" |
27 #include "gpu/command_buffer/service/gpu_switches.h" | 23 #include "gpu/command_buffer/service/gpu_switches.h" |
28 #include "gpu/command_buffer/service/mailbox_manager.h" | 24 #include "gpu/command_buffer/service/mailbox_manager.h" |
29 #include "gpu/command_buffer/service/memory_program_cache.h" | 25 #include "gpu/command_buffer/service/memory_program_cache.h" |
30 #include "gpu/command_buffer/service/shader_translator_cache.h" | 26 #include "gpu/command_buffer/service/shader_translator_cache.h" |
31 #include "gpu/command_buffer/service/sync_point_manager.h" | 27 #include "gpu/command_buffer/service/sync_point_manager.h" |
32 #include "ipc/message_filter.h" | 28 #include "ipc/message_filter.h" |
33 #include "ipc/message_router.h" | 29 #include "ipc/message_router.h" |
34 #include "ui/gl/gl_bindings.h" | 30 #include "ui/gl/gl_bindings.h" |
35 #include "ui/gl/gl_share_group.h" | 31 #include "ui/gl/gl_share_group.h" |
36 | 32 |
37 #if defined(OS_MACOSX) | |
38 #include "content/common/gpu/buffer_presented_params_mac.h" | |
39 #endif | |
40 | |
41 namespace content { | 33 namespace content { |
42 | 34 |
43 namespace { | 35 namespace { |
44 #if defined(OS_ANDROID) | 36 #if defined(OS_ANDROID) |
45 // Amount of time we expect the GPU to stay powered up without being used. | 37 // Amount of time we expect the GPU to stay powered up without being used. |
46 const int kMaxGpuIdleTimeMs = 40; | 38 const int kMaxGpuIdleTimeMs = 40; |
47 // Maximum amount of time we keep pinging the GPU waiting for the client to | 39 // Maximum amount of time we keep pinging the GPU waiting for the client to |
48 // draw. | 40 // draw. |
49 const int kMaxKeepAliveTimeMs = 200; | 41 const int kMaxKeepAliveTimeMs = 200; |
50 #endif | 42 #endif |
51 | 43 |
52 } | 44 } |
53 | 45 |
54 GpuChannelManager::GpuChannelManager( | 46 GpuChannelManager::GpuChannelManager( |
55 GpuChannelManagerDelegate* delegate, | 47 IPC::SyncChannel* channel, |
56 GpuWatchdog* watchdog, | 48 GpuWatchdog* watchdog, |
57 base::SingleThreadTaskRunner* task_runner, | 49 base::SingleThreadTaskRunner* task_runner, |
58 base::SingleThreadTaskRunner* io_task_runner, | 50 base::SingleThreadTaskRunner* io_task_runner, |
59 base::WaitableEvent* shutdown_event, | 51 base::WaitableEvent* shutdown_event, |
60 gpu::SyncPointManager* sync_point_manager, | 52 gpu::SyncPointManager* sync_point_manager, |
61 GpuMemoryBufferFactory* gpu_memory_buffer_factory) | 53 GpuMemoryBufferFactory* gpu_memory_buffer_factory) |
62 : task_runner_(task_runner), | 54 : task_runner_(task_runner), |
63 io_task_runner_(io_task_runner), | 55 io_task_runner_(io_task_runner), |
64 delegate_(delegate), | 56 channel_(channel), |
65 watchdog_(watchdog), | 57 watchdog_(watchdog), |
66 shutdown_event_(shutdown_event), | 58 shutdown_event_(shutdown_event), |
67 share_group_(new gfx::GLShareGroup), | 59 share_group_(new gfx::GLShareGroup), |
68 mailbox_manager_(gpu::gles2::MailboxManager::Create()), | 60 mailbox_manager_(gpu::gles2::MailboxManager::Create()), |
69 gpu_memory_manager_(this), | 61 gpu_memory_manager_(this), |
70 sync_point_manager_(sync_point_manager), | 62 sync_point_manager_(sync_point_manager), |
71 sync_point_client_waiter_( | 63 sync_point_client_waiter_( |
72 sync_point_manager->CreateSyncPointClientWaiter()), | 64 sync_point_manager->CreateSyncPointClientWaiter()), |
73 gpu_memory_buffer_factory_(gpu_memory_buffer_factory), | 65 gpu_memory_buffer_factory_(gpu_memory_buffer_factory), |
74 weak_factory_(this) { | 66 weak_factory_(this) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 | 101 |
110 gpu::gles2::FramebufferCompletenessCache* | 102 gpu::gles2::FramebufferCompletenessCache* |
111 GpuChannelManager::framebuffer_completeness_cache() { | 103 GpuChannelManager::framebuffer_completeness_cache() { |
112 if (!framebuffer_completeness_cache_.get()) | 104 if (!framebuffer_completeness_cache_.get()) |
113 framebuffer_completeness_cache_ = | 105 framebuffer_completeness_cache_ = |
114 new gpu::gles2::FramebufferCompletenessCache; | 106 new gpu::gles2::FramebufferCompletenessCache; |
115 return framebuffer_completeness_cache_.get(); | 107 return framebuffer_completeness_cache_.get(); |
116 } | 108 } |
117 | 109 |
118 void GpuChannelManager::RemoveChannel(int client_id) { | 110 void GpuChannelManager::RemoveChannel(int client_id) { |
119 delegate_->DidDestroyChannel(client_id); | 111 Send(new GpuHostMsg_DestroyChannel(client_id)); |
120 gpu_channels_.erase(client_id); | 112 gpu_channels_.erase(client_id); |
121 } | 113 } |
122 | 114 |
123 #if defined(OS_MACOSX) | 115 int GpuChannelManager::GenerateRouteID() { |
124 void GpuChannelManager::AddImageTransportSurface( | 116 static int last_id = 0; |
125 int32_t surface_id, | 117 return ++last_id; |
126 ImageTransportHelper* image_transport_helper) { | |
127 image_transport_map_.AddWithID(image_transport_helper, surface_id); | |
128 } | 118 } |
129 | 119 |
130 void GpuChannelManager::RemoveImageTransportSurface(int32_t surface_id) { | 120 void GpuChannelManager::AddRoute(int32_t routing_id, IPC::Listener* listener) { |
131 image_transport_map_.Remove(surface_id); | 121 router_.AddRoute(routing_id, listener); |
132 } | 122 } |
133 | 123 |
134 void GpuChannelManager::BufferPresented(const BufferPresentedParams& params) { | 124 void GpuChannelManager::RemoveRoute(int32_t routing_id) { |
135 ImageTransportHelper* helper = image_transport_map_.Lookup(params.surface_id); | 125 router_.RemoveRoute(routing_id); |
136 if (helper) | |
137 helper->BufferPresented(params); | |
138 } | 126 } |
139 #endif | |
140 | 127 |
141 GpuChannel* GpuChannelManager::LookupChannel(int32_t client_id) const { | 128 GpuChannel* GpuChannelManager::LookupChannel(int32_t client_id) const { |
142 const auto& it = gpu_channels_.find(client_id); | 129 const auto& it = gpu_channels_.find(client_id); |
143 return it != gpu_channels_.end() ? it->second : nullptr; | 130 return it != gpu_channels_.end() ? it->second : nullptr; |
144 } | 131 } |
145 | 132 |
| 133 bool GpuChannelManager::OnControlMessageReceived(const IPC::Message& msg) { |
| 134 bool handled = true; |
| 135 IPC_BEGIN_MESSAGE_MAP(GpuChannelManager, msg) |
| 136 IPC_MESSAGE_HANDLER(GpuMsg_EstablishChannel, OnEstablishChannel) |
| 137 IPC_MESSAGE_HANDLER(GpuMsg_CloseChannel, OnCloseChannel) |
| 138 IPC_MESSAGE_HANDLER(GpuMsg_DestroyGpuMemoryBuffer, OnDestroyGpuMemoryBuffer) |
| 139 IPC_MESSAGE_HANDLER(GpuMsg_LoadedShader, OnLoadedShader) |
| 140 IPC_MESSAGE_HANDLER(GpuMsg_UpdateValueState, OnUpdateValueState) |
| 141 #if defined(OS_ANDROID) |
| 142 IPC_MESSAGE_HANDLER(GpuMsg_WakeUpGpu, OnWakeUpGpu); |
| 143 #endif |
| 144 IPC_MESSAGE_UNHANDLED(handled = false) |
| 145 IPC_END_MESSAGE_MAP() |
| 146 return handled; |
| 147 } |
| 148 |
| 149 bool GpuChannelManager::OnMessageReceived(const IPC::Message& msg) { |
| 150 if (msg.routing_id() == MSG_ROUTING_CONTROL) |
| 151 return OnControlMessageReceived(msg); |
| 152 |
| 153 return router_.RouteMessage(msg); |
| 154 } |
| 155 |
| 156 bool GpuChannelManager::Send(IPC::Message* msg) { |
| 157 return channel_->Send(msg); |
| 158 } |
| 159 |
146 scoped_ptr<GpuChannel> GpuChannelManager::CreateGpuChannel( | 160 scoped_ptr<GpuChannel> GpuChannelManager::CreateGpuChannel( |
147 int client_id, | 161 int client_id, |
148 uint64_t client_tracing_id, | 162 uint64_t client_tracing_id, |
149 bool preempts, | 163 bool preempts, |
150 bool allow_view_command_buffers, | 164 bool allow_view_command_buffers, |
151 bool allow_real_time_streams) { | 165 bool allow_real_time_streams) { |
152 return make_scoped_ptr( | 166 return make_scoped_ptr( |
153 new GpuChannel(this, sync_point_manager(), watchdog_, share_group(), | 167 new GpuChannel(this, sync_point_manager(), watchdog_, share_group(), |
154 mailbox_manager(), preempts ? preemption_flag() : nullptr, | 168 mailbox_manager(), preempts ? preemption_flag() : nullptr, |
155 preempts ? nullptr : preemption_flag(), task_runner_.get(), | 169 preempts ? nullptr : preemption_flag(), task_runner_.get(), |
156 io_task_runner_.get(), client_id, client_tracing_id, | 170 io_task_runner_.get(), client_id, client_tracing_id, |
157 allow_view_command_buffers, allow_real_time_streams)); | 171 allow_view_command_buffers, allow_real_time_streams)); |
158 } | 172 } |
159 | 173 |
160 void GpuChannelManager::EstablishChannel(const EstablishChannelParams& params) { | 174 void GpuChannelManager::OnEstablishChannel( |
| 175 const GpuMsg_EstablishChannel_Params& params) { |
161 scoped_ptr<GpuChannel> channel(CreateGpuChannel( | 176 scoped_ptr<GpuChannel> channel(CreateGpuChannel( |
162 params.client_id, params.client_tracing_id, params.preempts, | 177 params.client_id, params.client_tracing_id, params.preempts, |
163 params.allow_view_command_buffers, params.allow_real_time_streams)); | 178 params.allow_view_command_buffers, params.allow_real_time_streams)); |
164 IPC::ChannelHandle channel_handle = channel->Init(shutdown_event_); | 179 IPC::ChannelHandle channel_handle = channel->Init(shutdown_event_); |
165 | 180 |
166 gpu_channels_.set(params.client_id, std::move(channel)); | 181 gpu_channels_.set(params.client_id, std::move(channel)); |
167 | 182 |
168 delegate_->ChannelEstablished(channel_handle); | 183 Send(new GpuHostMsg_ChannelEstablished(channel_handle)); |
169 } | 184 } |
170 | 185 |
171 void GpuChannelManager::CloseChannel(const IPC::ChannelHandle& channel_handle) { | 186 void GpuChannelManager::OnCloseChannel( |
| 187 const IPC::ChannelHandle& channel_handle) { |
172 for (auto it = gpu_channels_.begin(); it != gpu_channels_.end(); ++it) { | 188 for (auto it = gpu_channels_.begin(); it != gpu_channels_.end(); ++it) { |
173 if (it->second->channel_id() == channel_handle.name) { | 189 if (it->second->channel_id() == channel_handle.name) { |
174 gpu_channels_.erase(it); | 190 gpu_channels_.erase(it); |
175 return; | 191 return; |
176 } | 192 } |
177 } | 193 } |
178 } | 194 } |
179 | 195 |
180 void GpuChannelManager::InternalDestroyGpuMemoryBuffer( | 196 void GpuChannelManager::DestroyGpuMemoryBuffer( |
181 gfx::GpuMemoryBufferId id, | 197 gfx::GpuMemoryBufferId id, |
182 int client_id) { | 198 int client_id) { |
183 io_task_runner_->PostTask( | 199 io_task_runner_->PostTask( |
184 FROM_HERE, | 200 FROM_HERE, base::Bind(&GpuChannelManager::DestroyGpuMemoryBufferOnIO, |
185 base::Bind(&GpuChannelManager::InternalDestroyGpuMemoryBufferOnIO, | 201 base::Unretained(this), id, client_id)); |
186 base::Unretained(this), id, client_id)); | |
187 } | 202 } |
188 | 203 |
189 void GpuChannelManager::InternalDestroyGpuMemoryBufferOnIO( | 204 void GpuChannelManager::DestroyGpuMemoryBufferOnIO( |
190 gfx::GpuMemoryBufferId id, | 205 gfx::GpuMemoryBufferId id, |
191 int client_id) { | 206 int client_id) { |
192 gpu_memory_buffer_factory_->DestroyGpuMemoryBuffer(id, client_id); | 207 gpu_memory_buffer_factory_->DestroyGpuMemoryBuffer(id, client_id); |
193 } | 208 } |
194 | 209 |
195 void GpuChannelManager::DestroyGpuMemoryBuffer( | 210 void GpuChannelManager::OnDestroyGpuMemoryBuffer( |
196 gfx::GpuMemoryBufferId id, | 211 gfx::GpuMemoryBufferId id, |
197 int client_id, | 212 int client_id, |
198 const gpu::SyncToken& sync_token) { | 213 const gpu::SyncToken& sync_token) { |
199 if (sync_token.HasData()) { | 214 if (sync_token.HasData()) { |
200 scoped_refptr<gpu::SyncPointClientState> release_state = | 215 scoped_refptr<gpu::SyncPointClientState> release_state = |
201 sync_point_manager()->GetSyncPointClientState( | 216 sync_point_manager()->GetSyncPointClientState( |
202 sync_token.namespace_id(), sync_token.command_buffer_id()); | 217 sync_token.namespace_id(), sync_token.command_buffer_id()); |
203 if (release_state) { | 218 if (release_state) { |
204 sync_point_client_waiter_->WaitOutOfOrder( | 219 sync_point_client_waiter_->WaitOutOfOrder( |
205 release_state.get(), sync_token.release_count(), | 220 release_state.get(), sync_token.release_count(), |
206 base::Bind(&GpuChannelManager::InternalDestroyGpuMemoryBuffer, | 221 base::Bind(&GpuChannelManager::DestroyGpuMemoryBuffer, |
207 base::Unretained(this), id, client_id)); | 222 base::Unretained(this), id, client_id)); |
208 return; | 223 return; |
209 } | 224 } |
210 } | 225 } |
211 | 226 |
212 // No sync token or invalid sync token, destroy immediately. | 227 // No sync token or invalid sync token, destroy immediately. |
213 InternalDestroyGpuMemoryBuffer(id, client_id); | 228 DestroyGpuMemoryBuffer(id, client_id); |
214 } | 229 } |
215 | 230 |
216 void GpuChannelManager::UpdateValueState(int client_id, | 231 void GpuChannelManager::OnUpdateValueState( |
217 unsigned int target, | 232 int client_id, unsigned int target, const gpu::ValueState& state) { |
218 const gpu::ValueState& state) { | |
219 // Only pass updated state to the channel corresponding to the | 233 // Only pass updated state to the channel corresponding to the |
220 // render_widget_host where the event originated. | 234 // render_widget_host where the event originated. |
221 auto it = gpu_channels_.find(client_id); | 235 auto it = gpu_channels_.find(client_id); |
222 if (it != gpu_channels_.end()) | 236 if (it != gpu_channels_.end()) |
223 it->second->HandleUpdateValueState(target, state); | 237 it->second->HandleUpdateValueState(target, state); |
224 } | 238 } |
225 | 239 |
226 void GpuChannelManager::PopulateShaderCache(const std::string& program_proto) { | 240 void GpuChannelManager::OnLoadedShader(const std::string& program_proto) { |
227 if (program_cache()) | 241 if (program_cache()) |
228 program_cache()->LoadProgram(program_proto); | 242 program_cache()->LoadProgram(program_proto); |
229 } | 243 } |
230 | 244 |
231 uint32_t GpuChannelManager::GetUnprocessedOrderNum() const { | 245 uint32_t GpuChannelManager::GetUnprocessedOrderNum() const { |
232 uint32_t unprocessed_order_num = 0; | 246 uint32_t unprocessed_order_num = 0; |
233 for (auto& kv : gpu_channels_) { | 247 for (auto& kv : gpu_channels_) { |
234 unprocessed_order_num = | 248 unprocessed_order_num = |
235 std::max(unprocessed_order_num, kv.second->GetUnprocessedOrderNum()); | 249 std::max(unprocessed_order_num, kv.second->GetUnprocessedOrderNum()); |
236 } | 250 } |
237 return unprocessed_order_num; | 251 return unprocessed_order_num; |
238 } | 252 } |
239 | 253 |
240 uint32_t GpuChannelManager::GetProcessedOrderNum() const { | 254 uint32_t GpuChannelManager::GetProcessedOrderNum() const { |
241 uint32_t processed_order_num = 0; | 255 uint32_t processed_order_num = 0; |
242 for (auto& kv : gpu_channels_) { | 256 for (auto& kv : gpu_channels_) { |
243 processed_order_num = | 257 processed_order_num = |
244 std::max(processed_order_num, kv.second->GetProcessedOrderNum()); | 258 std::max(processed_order_num, kv.second->GetProcessedOrderNum()); |
245 } | 259 } |
246 return processed_order_num; | 260 return processed_order_num; |
247 } | 261 } |
248 | 262 |
249 void GpuChannelManager::LoseAllContexts() { | 263 void GpuChannelManager::LoseAllContexts() { |
250 for (auto& kv : gpu_channels_) { | 264 for (auto& kv : gpu_channels_) { |
251 kv.second->MarkAllContextsLost(); | 265 kv.second->MarkAllContextsLost(); |
252 } | 266 } |
253 task_runner_->PostTask(FROM_HERE, | 267 task_runner_->PostTask(FROM_HERE, |
254 base::Bind(&GpuChannelManager::DestroyAllChannels, | 268 base::Bind(&GpuChannelManager::OnLoseAllContexts, |
255 weak_factory_.GetWeakPtr())); | 269 weak_factory_.GetWeakPtr())); |
256 } | 270 } |
257 | 271 |
258 void GpuChannelManager::DestroyAllChannels() { | 272 void GpuChannelManager::OnLoseAllContexts() { |
259 gpu_channels_.clear(); | 273 gpu_channels_.clear(); |
260 } | 274 } |
261 | 275 |
262 gfx::GLSurface* GpuChannelManager::GetDefaultOffscreenSurface() { | 276 gfx::GLSurface* GpuChannelManager::GetDefaultOffscreenSurface() { |
263 if (!default_offscreen_surface_.get()) { | 277 if (!default_offscreen_surface_.get()) { |
264 default_offscreen_surface_ = | 278 default_offscreen_surface_ = |
265 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size()); | 279 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size()); |
266 } | 280 } |
267 return default_offscreen_surface_.get(); | 281 return default_offscreen_surface_.get(); |
268 } | 282 } |
269 | 283 |
270 #if defined(OS_ANDROID) | 284 #if defined(OS_ANDROID) |
271 void GpuChannelManager::DidAccessGpu() { | 285 void GpuChannelManager::DidAccessGpu() { |
272 last_gpu_access_time_ = base::TimeTicks::Now(); | 286 last_gpu_access_time_ = base::TimeTicks::Now(); |
273 } | 287 } |
274 | 288 |
275 void GpuChannelManager::WakeUpGpu() { | 289 void GpuChannelManager::OnWakeUpGpu() { |
276 begin_wake_up_time_ = base::TimeTicks::Now(); | 290 begin_wake_up_time_ = base::TimeTicks::Now(); |
277 ScheduleWakeUpGpu(); | 291 ScheduleWakeUpGpu(); |
278 } | 292 } |
279 | 293 |
280 void GpuChannelManager::ScheduleWakeUpGpu() { | 294 void GpuChannelManager::ScheduleWakeUpGpu() { |
281 base::TimeTicks now = base::TimeTicks::Now(); | 295 base::TimeTicks now = base::TimeTicks::Now(); |
282 TRACE_EVENT2("gpu", "GpuChannelManager::ScheduleWakeUp", | 296 TRACE_EVENT2("gpu", "GpuChannelManager::ScheduleWakeUp", |
283 "idle_time", (now - last_gpu_access_time_).InMilliseconds(), | 297 "idle_time", (now - last_gpu_access_time_).InMilliseconds(), |
284 "keep_awake_time", (now - begin_wake_up_time_).InMilliseconds()); | 298 "keep_awake_time", (now - begin_wake_up_time_).InMilliseconds()); |
285 if (now - last_gpu_access_time_ < | 299 if (now - last_gpu_access_time_ < |
(...skipping 22 matching lines...) Expand all Loading... |
308 } | 322 } |
309 } | 323 } |
310 if (!stub || !stub->decoder()->MakeCurrent()) | 324 if (!stub || !stub->decoder()->MakeCurrent()) |
311 return; | 325 return; |
312 glFinish(); | 326 glFinish(); |
313 DidAccessGpu(); | 327 DidAccessGpu(); |
314 } | 328 } |
315 #endif | 329 #endif |
316 | 330 |
317 } // namespace content | 331 } // namespace content |
OLD | NEW |