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/content_export.h"
19 #include "content/common/content_param_traits.h"
20 #include "content/common/gpu/devtools_gpu_instrumentation.h"
21 #include "content/common/gpu/gpu_memory_manager.h"
22 #include "ipc/ipc_listener.h"
23 #include "ipc/ipc_sender.h"
24 #include "ui/gfx/gpu_memory_buffer.h"
25 #include "ui/gfx/native_widget_types.h"
26 #include "ui/gl/gl_surface.h"
34 struct GpuMemoryBufferHandle
;
38 class SyncPointManager
;
43 class ShaderTranslatorCache
;
53 struct GPUCreateCommandBufferConfig
;
57 class GpuMemoryBufferFactory
;
61 // A GpuChannelManager is a thread responsible for issuing rendering commands
62 // managing the lifetimes of GPU channels and forwarding IPC requests from the
63 // browser process to them based on the corresponding renderer ID.
64 class CONTENT_EXPORT GpuChannelManager
: public IPC::Listener
,
67 GpuChannelManager(MessageRouter
* router
,
68 GpuWatchdog
* watchdog
,
69 base::MessageLoopProxy
* io_message_loop
,
70 base::WaitableEvent
* shutdown_event
,
71 IPC::SyncChannel
* channel
);
72 ~GpuChannelManager() override
;
74 // Remove the channel for a particular renderer.
75 void RemoveChannel(int client_id
);
77 // Listener overrides.
78 bool OnMessageReceived(const IPC::Message
& msg
) override
;
81 bool Send(IPC::Message
* msg
) override
;
83 bool HandleMessagesScheduled();
84 uint64
MessagesProcessed();
86 void LoseAllContexts();
88 int GenerateRouteID();
89 void AddRoute(int32 routing_id
, IPC::Listener
* listener
);
90 void RemoveRoute(int32 routing_id
);
92 gpu::gles2::ProgramCache
* program_cache();
93 gpu::gles2::ShaderTranslatorCache
* shader_translator_cache();
95 GpuMemoryManager
* gpu_memory_manager() { return &gpu_memory_manager_
; }
97 GpuEventsDispatcher
* gpu_devtools_events_dispatcher() {
98 return &gpu_devtools_events_dispatcher_
;
101 GpuChannel
* LookupChannel(int32 client_id
);
103 gpu::SyncPointManager
* sync_point_manager() {
104 return sync_point_manager_
.get();
107 gfx::GLSurface
* GetDefaultOffscreenSurface();
109 GpuMemoryBufferFactory
* gpu_memory_buffer_factory() {
110 return gpu_memory_buffer_factory_
.get();
114 typedef base::ScopedPtrHashMap
<int, GpuChannel
> GpuChannelMap
;
117 void OnEstablishChannel(int client_id
,
119 bool allow_future_sync_points
);
120 void OnCloseChannel(const IPC::ChannelHandle
& channel_handle
);
121 void OnVisibilityChanged(
122 int32 render_view_id
, int32 client_id
, bool visible
);
123 void OnCreateViewCommandBuffer(
124 const gfx::GLSurfaceHandle
& window
,
125 int32 render_view_id
,
127 const GPUCreateCommandBufferConfig
& init_params
,
129 void OnLoadedShader(std::string shader
);
130 void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id
, int client_id
);
131 void DestroyGpuMemoryBufferOnIO(gfx::GpuMemoryBufferId id
, int client_id
);
132 void OnDestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id
,
136 void OnRelinquishResources();
137 void OnResourcesRelinquished();
139 void OnUpdateValueState(int client_id
,
141 const gpu::ValueState
& state
);
143 void OnLoseAllContexts();
144 void CheckRelinquishGpuResources();
146 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_
;
147 base::WaitableEvent
* shutdown_event_
;
149 // Used to send and receive IPC messages from the browser process.
150 MessageRouter
* const router_
;
152 // These objects manage channels to individual renderer processes there is
153 // one channel for each renderer process that has connected to this GPU
155 GpuChannelMap gpu_channels_
;
156 scoped_refptr
<gfx::GLShareGroup
> share_group_
;
157 scoped_refptr
<gpu::gles2::MailboxManager
> mailbox_manager_
;
158 GpuMemoryManager gpu_memory_manager_
;
159 GpuEventsDispatcher gpu_devtools_events_dispatcher_
;
160 GpuWatchdog
* watchdog_
;
161 scoped_refptr
<gpu::SyncPointManager
> sync_point_manager_
;
162 scoped_ptr
<gpu::gles2::ProgramCache
> program_cache_
;
163 scoped_refptr
<gpu::gles2::ShaderTranslatorCache
> shader_translator_cache_
;
164 scoped_refptr
<gfx::GLSurface
> default_offscreen_surface_
;
165 scoped_ptr
<GpuMemoryBufferFactory
> gpu_memory_buffer_factory_
;
166 IPC::SyncChannel
* channel_
;
167 scoped_refptr
<IPC::MessageFilter
> filter_
;
168 bool relinquish_resources_pending_
;
170 // Member variables should appear before the WeakPtrFactory, to ensure
171 // that any WeakPtrs to Controller are invalidated before its members
172 // variable's destructors are executed, rendering them invalid.
173 base::WeakPtrFactory
<GpuChannelManager
> weak_factory_
;
175 DISALLOW_COPY_AND_ASSIGN(GpuChannelManager
);
178 } // namespace content
180 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_