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 "build/build_config.h"
17 #include "content/common/content_export.h"
18 #include "content/common/content_param_traits.h"
19 #include "content/common/gpu/gpu_memory_manager.h"
20 #include "content/common/message_router.h"
21 #include "ipc/ipc_listener.h"
22 #include "ipc/ipc_sender.h"
23 #include "ui/gfx/gpu_memory_buffer.h"
24 #include "ui/gfx/native_widget_types.h"
25 #include "ui/gl/gl_surface.h"
36 class SyncPointManager
;
39 class FramebufferCompletenessCache
;
42 class ShaderTranslatorCache
;
51 struct GPUCreateCommandBufferConfig
;
55 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 CONTENT_EXPORT GpuChannelManager
: public IPC::Listener
,
64 GpuChannelManager(IPC::SyncChannel
* channel
,
65 GpuWatchdog
* watchdog
,
66 base::SingleThreadTaskRunner
* task_runner
,
67 base::SingleThreadTaskRunner
* io_task_runner
,
68 base::WaitableEvent
* shutdown_event
,
69 gpu::SyncPointManager
* sync_point_manager
,
70 GpuMemoryBufferFactory
* gpu_memory_buffer_factory
);
71 ~GpuChannelManager() override
;
73 // Remove the channel for a particular renderer.
74 void RemoveChannel(int client_id
);
76 // Listener overrides.
77 bool OnMessageReceived(const IPC::Message
& msg
) override
;
80 bool Send(IPC::Message
* msg
) override
;
82 void LoseAllContexts();
84 int GenerateRouteID();
85 void AddRoute(int32 routing_id
, IPC::Listener
* listener
);
86 void RemoveRoute(int32 routing_id
);
88 gpu::gles2::ProgramCache
* program_cache();
89 gpu::gles2::ShaderTranslatorCache
* shader_translator_cache();
90 gpu::gles2::FramebufferCompletenessCache
* framebuffer_completeness_cache();
92 GpuMemoryManager
* gpu_memory_manager() { return &gpu_memory_manager_
; }
94 GpuChannel
* LookupChannel(int32 client_id
) const;
96 gpu::SyncPointManager
* sync_point_manager() {
97 return sync_point_manager_
;
100 gfx::GLSurface
* GetDefaultOffscreenSurface();
102 GpuMemoryBufferFactory
* gpu_memory_buffer_factory() {
103 return gpu_memory_buffer_factory_
;
106 // Returns the maximum order number for unprocessed IPC messages across all
108 uint32_t GetUnprocessedOrderNum() const;
110 // Returns the maximum order number for processed IPC messages across all
112 uint32_t GetProcessedOrderNum() const;
115 virtual scoped_ptr
<GpuChannel
> CreateGpuChannel(
116 gfx::GLShareGroup
* share_group
,
117 gpu::gles2::MailboxManager
* mailbox_manager
,
119 uint64_t client_tracing_id
,
120 bool allow_future_sync_points
,
121 bool allow_real_time_streams
);
123 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
124 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
126 // These objects manage channels to individual renderer processes there is
127 // one channel for each renderer process that has connected to this GPU
129 base::ScopedPtrHashMap
<int32
, scoped_ptr
<GpuChannel
>> gpu_channels_
;
133 bool OnControlMessageReceived(const IPC::Message
& msg
);
134 void OnEstablishChannel(int client_id
,
135 uint64_t client_tracing_id
,
137 bool allow_future_sync_points
,
138 bool allow_real_time_streams
);
139 void OnCloseChannel(const IPC::ChannelHandle
& channel_handle
);
140 void OnVisibilityChanged(
141 int32 render_view_id
, int32 client_id
, bool visible
);
142 void OnCreateViewCommandBuffer(
143 const gfx::GLSurfaceHandle
& window
,
144 int32 render_view_id
,
146 const GPUCreateCommandBufferConfig
& init_params
,
148 void OnLoadedShader(std::string shader
);
149 void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id
, int client_id
);
150 void DestroyGpuMemoryBufferOnIO(gfx::GpuMemoryBufferId id
, int client_id
);
151 void OnDestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id
,
155 void OnUpdateValueState(int client_id
,
157 const gpu::ValueState
& state
);
158 void OnLoseAllContexts();
160 // Used to send and receive IPC messages from the browser process.
161 IPC::SyncChannel
* const channel_
;
162 MessageRouter router_
;
164 GpuWatchdog
* watchdog_
;
166 base::WaitableEvent
* shutdown_event_
;
168 scoped_refptr
<gfx::GLShareGroup
> share_group_
;
169 scoped_refptr
<gpu::gles2::MailboxManager
> mailbox_manager_
;
170 GpuMemoryManager gpu_memory_manager_
;
171 // SyncPointManager guaranteed to outlive running MessageLoop.
172 gpu::SyncPointManager
* sync_point_manager_
;
173 scoped_ptr
<gpu::gles2::ProgramCache
> program_cache_
;
174 scoped_refptr
<gpu::gles2::ShaderTranslatorCache
> shader_translator_cache_
;
175 scoped_refptr
<gpu::gles2::FramebufferCompletenessCache
>
176 framebuffer_completeness_cache_
;
177 scoped_refptr
<gfx::GLSurface
> default_offscreen_surface_
;
178 GpuMemoryBufferFactory
* const gpu_memory_buffer_factory_
;
180 // Member variables should appear before the WeakPtrFactory, to ensure
181 // that any WeakPtrs to Controller are invalidated before its members
182 // variable's destructors are executed, rendering them invalid.
183 base::WeakPtrFactory
<GpuChannelManager
> weak_factory_
;
185 DISALLOW_COPY_AND_ASSIGN(GpuChannelManager
);
188 } // namespace content
190 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_