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.
5 #ifndef CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_
6 #define CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_
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/gpu_memory_buffer.h"
23 #include "ui/gfx/native_widget_types.h"
24 #include "ui/gl/gl_surface.h"
32 struct GpuMemoryBufferHandle
;
36 class SyncPointManager
;
40 class ShaderTranslatorCache
;
50 struct GPUCreateCommandBufferConfig
;
54 class GpuMemoryBufferFactory
;
58 // A GpuChannelManager is a thread responsible for issuing rendering commands
59 // managing the lifetimes of GPU channels and forwarding IPC requests from the
60 // browser process to them based on the corresponding renderer ID.
61 class GpuChannelManager
: public IPC::Listener
,
64 GpuChannelManager(MessageRouter
* router
,
65 GpuWatchdog
* watchdog
,
66 base::MessageLoopProxy
* io_message_loop
,
67 base::WaitableEvent
* shutdown_event
,
68 IPC::SyncChannel
* channel
);
69 ~GpuChannelManager() override
;
71 // Remove the channel for a particular renderer.
72 void RemoveChannel(int client_id
);
74 // Listener overrides.
75 bool OnMessageReceived(const IPC::Message
& msg
) override
;
78 bool Send(IPC::Message
* msg
) override
;
80 bool HandleMessagesScheduled();
81 uint64
MessagesProcessed();
83 void LoseAllContexts();
85 int GenerateRouteID();
86 void AddRoute(int32 routing_id
, IPC::Listener
* listener
);
87 void RemoveRoute(int32 routing_id
);
89 gpu::gles2::ProgramCache
* program_cache();
90 gpu::gles2::ShaderTranslatorCache
* shader_translator_cache();
92 GpuMemoryManager
* gpu_memory_manager() { return &gpu_memory_manager_
; }
94 GpuEventsDispatcher
* gpu_devtools_events_dispatcher() {
95 return &gpu_devtools_events_dispatcher_
;
98 GpuChannel
* LookupChannel(int32 client_id
);
100 gpu::SyncPointManager
* sync_point_manager() {
101 return sync_point_manager_
.get();
104 gfx::GLSurface
* GetDefaultOffscreenSurface();
106 GpuMemoryBufferFactory
* gpu_memory_buffer_factory() {
107 return gpu_memory_buffer_factory_
.get();
111 typedef base::ScopedPtrHashMap
<int, GpuChannel
> GpuChannelMap
;
114 void OnEstablishChannel(int client_id
,
116 bool allow_future_sync_points
);
117 void OnCloseChannel(const IPC::ChannelHandle
& channel_handle
);
118 void OnVisibilityChanged(
119 int32 render_view_id
, int32 client_id
, bool visible
);
120 void OnCreateViewCommandBuffer(
121 const gfx::GLSurfaceHandle
& window
,
122 int32 render_view_id
,
124 const GPUCreateCommandBufferConfig
& init_params
,
126 void OnLoadedShader(std::string shader
);
127 void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferType type
,
128 gfx::GpuMemoryBufferId id
,
130 void DestroyGpuMemoryBufferOnIO(gfx::GpuMemoryBufferType type
,
131 gfx::GpuMemoryBufferId id
,
133 void OnDestroyGpuMemoryBuffer(gfx::GpuMemoryBufferType type
,
134 gfx::GpuMemoryBufferId id
,
138 void OnLoseAllContexts();
140 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_
;
141 base::WaitableEvent
* shutdown_event_
;
143 // Used to send and receive IPC messages from the browser process.
144 MessageRouter
* const router_
;
146 // These objects manage channels to individual renderer processes there is
147 // one channel for each renderer process that has connected to this GPU
149 GpuChannelMap gpu_channels_
;
150 scoped_refptr
<gfx::GLShareGroup
> share_group_
;
151 scoped_refptr
<gpu::gles2::MailboxManager
> mailbox_manager_
;
152 GpuMemoryManager gpu_memory_manager_
;
153 GpuEventsDispatcher gpu_devtools_events_dispatcher_
;
154 GpuWatchdog
* watchdog_
;
155 scoped_refptr
<gpu::SyncPointManager
> sync_point_manager_
;
156 scoped_ptr
<gpu::gles2::ProgramCache
> program_cache_
;
157 scoped_refptr
<gpu::gles2::ShaderTranslatorCache
> shader_translator_cache_
;
158 scoped_refptr
<gfx::GLSurface
> default_offscreen_surface_
;
159 scoped_ptr
<GpuMemoryBufferFactory
> gpu_memory_buffer_factory_
;
160 IPC::SyncChannel
* channel_
;
161 scoped_refptr
<IPC::MessageFilter
> filter_
;
163 // Member variables should appear before the WeakPtrFactory, to ensure
164 // that any WeakPtrs to Controller are invalidated before its members
165 // variable's destructors are executed, rendering them invalid.
166 base::WeakPtrFactory
<GpuChannelManager
> weak_factory_
;
168 DISALLOW_COPY_AND_ASSIGN(GpuChannelManager
);
171 } // namespace content
173 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_