[email protected] | 3bebb1f | 2012-01-05 23:14:07 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f24a1e2b | 2011-04-08 01:48:48 | [diff] [blame] | 5 | #include "content/common/gpu/gpu_channel_manager.h" |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 6 | |
[email protected] | 35a5b75 | 2011-11-17 23:58:58 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 65dfc60 | 2012-07-23 20:39:39 | [diff] [blame] | 8 | #include "base/command_line.h" |
[email protected] | 2e7bbf2 | 2011-07-22 18:41:29 | [diff] [blame] | 9 | #include "content/common/child_thread.h" |
[email protected] | 1c98271 | 2011-06-27 23:44:31 | [diff] [blame] | 10 | #include "content/common/gpu/gpu_channel.h" |
[email protected] | 672c731 | 2012-02-03 16:28:55 | [diff] [blame] | 11 | #include "content/common/gpu/gpu_memory_manager.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 12 | #include "content/common/gpu/gpu_messages.h" |
[email protected] | c02f93e4 | 2012-06-05 23:35:16 | [diff] [blame] | 13 | #include "content/common/gpu/sync_point_manager.h" |
[email protected] | 65dfc60 | 2012-07-23 20:39:39 | [diff] [blame] | 14 | #include "gpu/command_buffer/service/feature_info.h" |
| 15 | #include "gpu/command_buffer/service/gpu_switches.h" |
[email protected] | 18fd4195 | 2012-06-06 00:09:46 | [diff] [blame] | 16 | #include "gpu/command_buffer/service/mailbox_manager.h" |
[email protected] | 65dfc60 | 2012-07-23 20:39:39 | [diff] [blame] | 17 | #include "gpu/command_buffer/service/memory_program_cache.h" |
| 18 | #include "ui/gl/gl_bindings.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 19 | #include "ui/gl/gl_share_group.h" |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 20 | |
[email protected] | 2e7bbf2 | 2011-07-22 18:41:29 | [diff] [blame] | 21 | GpuChannelManager::GpuChannelManager(ChildThread* gpu_child_thread, |
[email protected] | 2dcf702 | 2011-04-15 19:20:41 | [diff] [blame] | 22 | GpuWatchdog* watchdog, |
[email protected] | 92bf906 | 2011-05-02 18:00:49 | [diff] [blame] | 23 | base::MessageLoopProxy* io_message_loop, |
[email protected] | f24a1e2b | 2011-04-08 01:48:48 | [diff] [blame] | 24 | base::WaitableEvent* shutdown_event) |
[email protected] | 35a5b75 | 2011-11-17 23:58:58 | [diff] [blame] | 25 | : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
[email protected] | 0fc3574 | 2011-04-13 17:57:54 | [diff] [blame] | 26 | io_message_loop_(io_message_loop), |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 27 | shutdown_event_(shutdown_event), |
[email protected] | 2e7bbf2 | 2011-07-22 18:41:29 | [diff] [blame] | 28 | gpu_child_thread_(gpu_child_thread), |
[email protected] | 672c731 | 2012-02-03 16:28:55 | [diff] [blame] | 29 | ALLOW_THIS_IN_INITIALIZER_LIST(gpu_memory_manager_(this, |
| 30 | GpuMemoryManager::kDefaultMaxSurfacesWithFrontbufferSoftLimit)), |
[email protected] | c02f93e4 | 2012-06-05 23:35:16 | [diff] [blame] | 31 | watchdog_(watchdog), |
[email protected] | 65dfc60 | 2012-07-23 20:39:39 | [diff] [blame] | 32 | sync_point_manager_(new SyncPointManager), |
| 33 | program_cache_(NULL) { |
[email protected] | 2e7bbf2 | 2011-07-22 18:41:29 | [diff] [blame] | 34 | DCHECK(gpu_child_thread); |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 35 | DCHECK(io_message_loop); |
| 36 | DCHECK(shutdown_event); |
| 37 | } |
| 38 | |
[email protected] | f24a1e2b | 2011-04-08 01:48:48 | [diff] [blame] | 39 | GpuChannelManager::~GpuChannelManager() { |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 40 | gpu_channels_.clear(); |
[email protected] | b8673e6 | 2012-09-25 03:15:00 | [diff] [blame^] | 41 | if (default_offscreen_surface_.get()) { |
| 42 | default_offscreen_surface_->Destroy(); |
| 43 | default_offscreen_surface_ = NULL; |
| 44 | } |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 45 | } |
| 46 | |
[email protected] | 65dfc60 | 2012-07-23 20:39:39 | [diff] [blame] | 47 | gpu::gles2::ProgramCache* GpuChannelManager::program_cache() { |
| 48 | if (!program_cache_.get() && |
| 49 | (gfx::g_ARB_get_program_binary || gfx::g_OES_get_program_binary) && |
| 50 | !CommandLine::ForCurrentProcess()->HasSwitch( |
| 51 | switches::kDisableGpuProgramCache)) { |
| 52 | program_cache_.reset(new gpu::gles2::MemoryProgramCache()); |
| 53 | } |
| 54 | return program_cache_.get(); |
| 55 | } |
| 56 | |
[email protected] | e608ce0 | 2012-01-12 22:30:07 | [diff] [blame] | 57 | void GpuChannelManager::RemoveChannel(int client_id) { |
| 58 | gpu_channels_.erase(client_id); |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 59 | } |
| 60 | |
[email protected] | 2e7bbf2 | 2011-07-22 18:41:29 | [diff] [blame] | 61 | int GpuChannelManager::GenerateRouteID() { |
| 62 | static int last_id = 0; |
| 63 | return ++last_id; |
| 64 | } |
| 65 | |
[email protected] | d84effeb | 2012-06-25 17:03:10 | [diff] [blame] | 66 | void GpuChannelManager::AddRoute(int32 routing_id, IPC::Listener* listener) { |
[email protected] | 2e7bbf2 | 2011-07-22 18:41:29 | [diff] [blame] | 67 | gpu_child_thread_->AddRoute(routing_id, listener); |
| 68 | } |
| 69 | |
| 70 | void GpuChannelManager::RemoveRoute(int32 routing_id) { |
| 71 | gpu_child_thread_->RemoveRoute(routing_id); |
| 72 | } |
| 73 | |
[email protected] | e608ce0 | 2012-01-12 22:30:07 | [diff] [blame] | 74 | GpuChannel* GpuChannelManager::LookupChannel(int32 client_id) { |
| 75 | GpuChannelMap::const_iterator iter = gpu_channels_.find(client_id); |
[email protected] | 538195f | 2011-09-08 15:03:20 | [diff] [blame] | 76 | if (iter == gpu_channels_.end()) |
| 77 | return NULL; |
| 78 | else |
| 79 | return iter->second; |
| 80 | } |
| 81 | |
[email protected] | 672c731 | 2012-02-03 16:28:55 | [diff] [blame] | 82 | void GpuChannelManager::AppendAllCommandBufferStubs( |
| 83 | std::vector<GpuCommandBufferStubBase*>& stubs) { |
| 84 | for (GpuChannelMap::const_iterator it = gpu_channels_.begin(); |
| 85 | it != gpu_channels_.end(); ++it ) { |
| 86 | it->second->AppendAllCommandBufferStubs(stubs); |
| 87 | } |
[email protected] | 672c731 | 2012-02-03 16:28:55 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | f24a1e2b | 2011-04-08 01:48:48 | [diff] [blame] | 90 | bool GpuChannelManager::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 91 | bool msg_is_ok = true; |
| 92 | bool handled = true; |
[email protected] | f24a1e2b | 2011-04-08 01:48:48 | [diff] [blame] | 93 | IPC_BEGIN_MESSAGE_MAP_EX(GpuChannelManager, msg, msg_is_ok) |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 94 | IPC_MESSAGE_HANDLER(GpuMsg_EstablishChannel, OnEstablishChannel) |
| 95 | IPC_MESSAGE_HANDLER(GpuMsg_CloseChannel, OnCloseChannel) |
| 96 | IPC_MESSAGE_HANDLER(GpuMsg_CreateViewCommandBuffer, |
[email protected] | 8dc7fbd | 2011-04-15 00:13:12 | [diff] [blame] | 97 | OnCreateViewCommandBuffer) |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 98 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 99 | IPC_END_MESSAGE_MAP_EX() |
| 100 | return handled; |
| 101 | } |
| 102 | |
[email protected] | f24a1e2b | 2011-04-08 01:48:48 | [diff] [blame] | 103 | bool GpuChannelManager::Send(IPC::Message* msg) { |
[email protected] | 2e7bbf2 | 2011-07-22 18:41:29 | [diff] [blame] | 104 | return gpu_child_thread_->Send(msg); |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | d8d97c2 | 2012-03-08 01:31:28 | [diff] [blame] | 107 | void GpuChannelManager::OnEstablishChannel(int client_id, bool share_context) { |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 108 | IPC::ChannelHandle channel_handle; |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 109 | |
[email protected] | e608ce0 | 2012-01-12 22:30:07 | [diff] [blame] | 110 | gfx::GLShareGroup* share_group = NULL; |
[email protected] | 18fd4195 | 2012-06-06 00:09:46 | [diff] [blame] | 111 | gpu::gles2::MailboxManager* mailbox_manager = NULL; |
[email protected] | d8d97c2 | 2012-03-08 01:31:28 | [diff] [blame] | 112 | if (share_context) { |
[email protected] | 18fd4195 | 2012-06-06 00:09:46 | [diff] [blame] | 113 | if (!share_group_) { |
[email protected] | d8d97c2 | 2012-03-08 01:31:28 | [diff] [blame] | 114 | share_group_ = new gfx::GLShareGroup; |
[email protected] | 18fd4195 | 2012-06-06 00:09:46 | [diff] [blame] | 115 | DCHECK(!mailbox_manager_); |
| 116 | mailbox_manager_ = new gpu::gles2::MailboxManager; |
| 117 | } |
[email protected] | d8d97c2 | 2012-03-08 01:31:28 | [diff] [blame] | 118 | share_group = share_group_; |
[email protected] | 18fd4195 | 2012-06-06 00:09:46 | [diff] [blame] | 119 | mailbox_manager = mailbox_manager_; |
[email protected] | e608ce0 | 2012-01-12 22:30:07 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 3bebb1f | 2012-01-05 23:14:07 | [diff] [blame] | 122 | scoped_refptr<GpuChannel> channel = new GpuChannel(this, |
| 123 | watchdog_, |
[email protected] | e608ce0 | 2012-01-12 22:30:07 | [diff] [blame] | 124 | share_group, |
[email protected] | 18fd4195 | 2012-06-06 00:09:46 | [diff] [blame] | 125 | mailbox_manager, |
[email protected] | e608ce0 | 2012-01-12 22:30:07 | [diff] [blame] | 126 | client_id, |
[email protected] | 7f1a51c32 | 2012-07-19 18:52:02 | [diff] [blame] | 127 | false); |
[email protected] | 3bebb1f | 2012-01-05 23:14:07 | [diff] [blame] | 128 | if (channel->Init(io_message_loop_, shutdown_event_)) { |
[email protected] | e608ce0 | 2012-01-12 22:30:07 | [diff] [blame] | 129 | gpu_channels_[client_id] = channel; |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 130 | channel_handle.name = channel->GetChannelName(); |
[email protected] | 3bebb1f | 2012-01-05 23:14:07 | [diff] [blame] | 131 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 132 | #if defined(OS_POSIX) |
| 133 | // On POSIX, pass the renderer-side FD. Also mark it as auto-close so |
| 134 | // that it gets closed after it has been sent. |
[email protected] | 2ce26c43 | 2011-09-19 17:08:12 | [diff] [blame] | 135 | int renderer_fd = channel->TakeRendererFileDescriptor(); |
[email protected] | 8bc550e | 2012-02-07 11:05:56 | [diff] [blame] | 136 | DCHECK_NE(-1, renderer_fd); |
[email protected] | 2ce26c43 | 2011-09-19 17:08:12 | [diff] [blame] | 137 | channel_handle.socket = base::FileDescriptor(renderer_fd, true); |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 138 | #endif |
| 139 | } |
| 140 | |
| 141 | Send(new GpuHostMsg_ChannelEstablished(channel_handle)); |
| 142 | } |
| 143 | |
[email protected] | f24a1e2b | 2011-04-08 01:48:48 | [diff] [blame] | 144 | void GpuChannelManager::OnCloseChannel( |
| 145 | const IPC::ChannelHandle& channel_handle) { |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 146 | for (GpuChannelMap::iterator iter = gpu_channels_.begin(); |
| 147 | iter != gpu_channels_.end(); ++iter) { |
| 148 | if (iter->second->GetChannelName() == channel_handle.name) { |
| 149 | gpu_channels_.erase(iter); |
| 150 | return; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
[email protected] | f24a1e2b | 2011-04-08 01:48:48 | [diff] [blame] | 155 | void GpuChannelManager::OnCreateViewCommandBuffer( |
[email protected] | fc4ed09 | 2012-02-21 19:46:55 | [diff] [blame] | 156 | const gfx::GLSurfaceHandle& window, |
[email protected] | 9f4f332 | 2012-01-18 22:29:56 | [diff] [blame] | 157 | int32 surface_id, |
[email protected] | e608ce0 | 2012-01-12 22:30:07 | [diff] [blame] | 158 | int32 client_id, |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 159 | const GPUCreateCommandBufferConfig& init_params) { |
[email protected] | 9f4f332 | 2012-01-18 22:29:56 | [diff] [blame] | 160 | DCHECK(surface_id); |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 161 | int32 route_id = MSG_ROUTING_NONE; |
| 162 | |
[email protected] | e608ce0 | 2012-01-12 22:30:07 | [diff] [blame] | 163 | GpuChannelMap::const_iterator iter = gpu_channels_.find(client_id); |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 164 | if (iter != gpu_channels_.end()) { |
| 165 | iter->second->CreateViewCommandBuffer( |
[email protected] | 9f4f332 | 2012-01-18 22:29:56 | [diff] [blame] | 166 | window, surface_id, init_params, &route_id); |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | Send(new GpuHostMsg_CommandBufferCreated(route_id)); |
| 170 | } |
| 171 | |
[email protected] | 0fc3574 | 2011-04-13 17:57:54 | [diff] [blame] | 172 | void GpuChannelManager::LoseAllContexts() { |
| 173 | MessageLoop::current()->PostTask( |
[email protected] | 35a5b75 | 2011-11-17 23:58:58 | [diff] [blame] | 174 | FROM_HERE, |
| 175 | base::Bind(&GpuChannelManager::OnLoseAllContexts, |
| 176 | weak_factory_.GetWeakPtr())); |
[email protected] | 0fc3574 | 2011-04-13 17:57:54 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void GpuChannelManager::OnLoseAllContexts() { |
| 180 | gpu_channels_.clear(); |
| 181 | } |
[email protected] | b8673e6 | 2012-09-25 03:15:00 | [diff] [blame^] | 182 | |
| 183 | gfx::GLSurface* GpuChannelManager::GetDefaultOffscreenSurface() { |
| 184 | if (!default_offscreen_surface_.get()) { |
| 185 | default_offscreen_surface_ = gfx::GLSurface::CreateOffscreenGLSurface( |
| 186 | false, gfx::Size(1, 1)); |
| 187 | } |
| 188 | return default_offscreen_surface_.get(); |
| 189 | } |