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 "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"
35 class SyncPointManager
;
40 class ShaderTranslatorCache
;
45 class AttachmentBroker
;
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 CONTENT_EXPORT GpuChannelManager
: public IPC::Listener
,
64 // |broker| must outlive GpuChannelManager and any channels it creates.
65 GpuChannelManager(MessageRouter
* router
,
66 GpuWatchdog
* watchdog
,
67 base::SingleThreadTaskRunner
* io_task_runner
,
68 base::WaitableEvent
* shutdown_event
,
69 IPC::SyncChannel
* channel
,
70 IPC::AttachmentBroker
* broker
,
71 gpu::SyncPointManager
* sync_point_manager
,
72 GpuMemoryBufferFactory
* gpu_memory_buffer_factory
);
73 ~GpuChannelManager() override
;
75 // Remove the channel for a particular renderer.
76 void RemoveChannel(int client_id
);
78 // Listener overrides.
79 bool OnMessageReceived(const IPC::Message
& msg
) override
;
82 bool Send(IPC::Message
* msg
) override
;
84 bool HandleMessagesScheduled();
85 uint64
MessagesProcessed();
87 void LoseAllContexts();
89 int GenerateRouteID();
90 void AddRoute(int32 routing_id
, IPC::Listener
* listener
);
91 void RemoveRoute(int32 routing_id
);
93 gpu::gles2::ProgramCache
* program_cache();
94 gpu::gles2::ShaderTranslatorCache
* shader_translator_cache();
96 GpuMemoryManager
* gpu_memory_manager() { return &gpu_memory_manager_
; }
98 GpuChannel
* LookupChannel(int32 client_id
);
100 gpu::SyncPointManager
* sync_point_manager() {
101 return sync_point_manager_
;
104 gfx::GLSurface
* GetDefaultOffscreenSurface();
106 GpuMemoryBufferFactory
* gpu_memory_buffer_factory() {
107 return gpu_memory_buffer_factory_
;
111 typedef base::ScopedPtrHashMap
<int, scoped_ptr
<GpuChannel
>> GpuChannelMap
;
114 void OnEstablishChannel(int client_id
,
115 uint64_t client_tracing_id
,
117 bool allow_future_sync_points
);
118 void OnCloseChannel(const IPC::ChannelHandle
& channel_handle
);
119 void OnVisibilityChanged(
120 int32 render_view_id
, int32 client_id
, bool visible
);
121 void OnCreateViewCommandBuffer(
122 const gfx::GLSurfaceHandle
& window
,
123 int32 render_view_id
,
125 const GPUCreateCommandBufferConfig
& init_params
,
127 void OnLoadedShader(std::string shader
);
128 void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id
, int client_id
);
129 void DestroyGpuMemoryBufferOnIO(gfx::GpuMemoryBufferId id
, int client_id
);
130 void OnDestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id
,
136 void OnUpdateValueState(int client_id
,
138 const gpu::ValueState
& state
);
140 void OnLoseAllContexts();
142 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
143 base::WaitableEvent
* shutdown_event_
;
145 // Used to send and receive IPC messages from the browser process.
146 MessageRouter
* const router_
;
148 // These objects manage channels to individual renderer processes there is
149 // one channel for each renderer process that has connected to this GPU
151 GpuChannelMap gpu_channels_
;
152 scoped_refptr
<gfx::GLShareGroup
> share_group_
;
153 scoped_refptr
<gpu::gles2::MailboxManager
> mailbox_manager_
;
154 GpuMemoryManager gpu_memory_manager_
;
155 GpuWatchdog
* watchdog_
;
156 // SyncPointManager guaranteed to outlive running MessageLoop.
157 gpu::SyncPointManager
* sync_point_manager_
;
158 scoped_ptr
<gpu::gles2::ProgramCache
> program_cache_
;
159 scoped_refptr
<gpu::gles2::ShaderTranslatorCache
> shader_translator_cache_
;
160 scoped_refptr
<gfx::GLSurface
> default_offscreen_surface_
;
161 GpuMemoryBufferFactory
* const gpu_memory_buffer_factory_
;
162 IPC::SyncChannel
* channel_
;
163 // Must outlive this instance of GpuChannelManager.
164 IPC::AttachmentBroker
* attachment_broker_
;
166 // Member variables should appear before the WeakPtrFactory, to ensure
167 // that any WeakPtrs to Controller are invalidated before its members
168 // variable's destructors are executed, rendering them invalid.
169 base::WeakPtrFactory
<GpuChannelManager
> weak_factory_
;
171 DISALLOW_COPY_AND_ASSIGN(GpuChannelManager
);
174 } // namespace content
176 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_