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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_ 6 #define CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_ 7 8 #include <deque> 9 #include <string> 10 #include <vector> 11 12 #include "base/containers/scoped_ptr_hash_map.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/weak_ptr.h" 16 #include "base/message_loop/message_loop_proxy.h" 17 #include "build/build_config.h" 18 #include "content/common/gpu/devtools_gpu_instrumentation.h" 19 #include "content/common/gpu/gpu_memory_manager.h" 20 #include "ipc/ipc_listener.h" 21 #include "ipc/ipc_sender.h" 22 #include "ui/gfx/native_widget_types.h" 23 #include "ui/gl/gl_surface.h" 24 25 namespace base { 26 class WaitableEvent; 27 } 28 29 namespace gfx { 30 class GLShareGroup; 31 struct GpuMemoryBufferHandle; 32 } 33 34 namespace gpu { 35 namespace gles2 { 36 class MailboxManager; 37 class ProgramCache; 38 class ShaderTranslatorCache; 39 } 40 } 41 42 namespace IPC { 43 struct ChannelHandle; 44 class SyncChannel; 45 class MessageFilter; 46 } 47 48 struct GPUCreateCommandBufferConfig; 49 50 namespace content { 51 class GpuChannel; 52 class GpuMemoryBufferFactory; 53 class GpuWatchdog; 54 class MessageRouter; 55 class SyncPointManager; 56 57 // A GpuChannelManager is a thread responsible for issuing rendering commands 58 // managing the lifetimes of GPU channels and forwarding IPC requests from the 59 // browser process to them based on the corresponding renderer ID. 60 class GpuChannelManager : public IPC::Listener, 61 public IPC::Sender { 62 public: 63 GpuChannelManager(MessageRouter* router, 64 GpuWatchdog* watchdog, 65 base::MessageLoopProxy* io_message_loop, 66 base::WaitableEvent* shutdown_event, 67 IPC::SyncChannel* channel); 68 virtual ~GpuChannelManager(); 69 70 // Remove the channel for a particular renderer. 71 void RemoveChannel(int client_id); 72 73 // Listener overrides. 74 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; 75 76 // Sender overrides. 77 virtual bool Send(IPC::Message* msg) OVERRIDE; 78 79 bool HandleMessagesScheduled(); 80 uint64 MessagesProcessed(); 81 82 void LoseAllContexts(); 83 84 int GenerateRouteID(); 85 void AddRoute(int32 routing_id, IPC::Listener* listener); 86 void RemoveRoute(int32 routing_id); 87 88 gpu::gles2::ProgramCache* program_cache(); 89 gpu::gles2::ShaderTranslatorCache* shader_translator_cache(); 90 91 GpuMemoryManager* gpu_memory_manager() { return &gpu_memory_manager_; } 92 93 GpuEventsDispatcher* gpu_devtools_events_dispatcher() { 94 return &gpu_devtools_events_dispatcher_; 95 } 96 97 GpuChannel* LookupChannel(int32 client_id); 98 99 SyncPointManager* sync_point_manager() { return sync_point_manager_.get(); } 100 101 gfx::GLSurface* GetDefaultOffscreenSurface(); 102 103 GpuMemoryBufferFactory* gpu_memory_buffer_factory() { 104 return gpu_memory_buffer_factory_.get(); 105 } 106 107 private: 108 typedef base::ScopedPtrHashMap<int, GpuChannel> GpuChannelMap; 109 110 // Message handlers. 111 void OnEstablishChannel(int client_id, 112 bool share_context, 113 bool allow_future_sync_points); 114 void OnCloseChannel(const IPC::ChannelHandle& channel_handle); 115 void OnVisibilityChanged( 116 int32 render_view_id, int32 client_id, bool visible); 117 void OnCreateViewCommandBuffer( 118 const gfx::GLSurfaceHandle& window, 119 int32 render_view_id, 120 int32 client_id, 121 const GPUCreateCommandBufferConfig& init_params, 122 int32 route_id); 123 void OnLoadedShader(std::string shader); 124 void DestroyGpuMemoryBuffer(const gfx::GpuMemoryBufferHandle& handle); 125 void DestroyGpuMemoryBufferOnIO(const gfx::GpuMemoryBufferHandle& handle); 126 void OnDestroyGpuMemoryBuffer(const gfx::GpuMemoryBufferHandle& handle, 127 int32 sync_point); 128 129 void OnLoseAllContexts(); 130 131 scoped_refptr<base::MessageLoopProxy> io_message_loop_; 132 base::WaitableEvent* shutdown_event_; 133 134 // Used to send and receive IPC messages from the browser process. 135 MessageRouter* const router_; 136 137 // These objects manage channels to individual renderer processes there is 138 // one channel for each renderer process that has connected to this GPU 139 // process. 140 GpuChannelMap gpu_channels_; 141 scoped_refptr<gfx::GLShareGroup> share_group_; 142 scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_; 143 GpuMemoryManager gpu_memory_manager_; 144 GpuEventsDispatcher gpu_devtools_events_dispatcher_; 145 GpuWatchdog* watchdog_; 146 scoped_refptr<SyncPointManager> sync_point_manager_; 147 scoped_ptr<gpu::gles2::ProgramCache> program_cache_; 148 scoped_refptr<gpu::gles2::ShaderTranslatorCache> shader_translator_cache_; 149 scoped_refptr<gfx::GLSurface> default_offscreen_surface_; 150 scoped_ptr<GpuMemoryBufferFactory> gpu_memory_buffer_factory_; 151 IPC::SyncChannel* channel_; 152 scoped_refptr<IPC::MessageFilter> filter_; 153 154 // Member variables should appear before the WeakPtrFactory, to ensure 155 // that any WeakPtrs to Controller are invalidated before its members 156 // variable's destructors are executed, rendering them invalid. 157 base::WeakPtrFactory<GpuChannelManager> weak_factory_; 158 159 DISALLOW_COPY_AND_ASSIGN(GpuChannelManager); 160 }; 161 162 } // namespace content 163 164 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_ 165