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
,
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::GpuMemoryBufferId id
, int client_id
);
128 void DestroyGpuMemoryBufferOnIO(gfx::GpuMemoryBufferId id
, int client_id
);
129 void OnDestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id
,
133 void OnRelinquishResources();
134 void OnResourcesRelinquished();
136 void OnUpdateValueState(int client_id
,
138 const gpu::ValueState
& state
);
140 void OnLoseAllContexts();
141 void CheckRelinquishGpuResources();
143 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
144 base::WaitableEvent
* shutdown_event_
;
146 // Used to send and receive IPC messages from the browser process.
147 MessageRouter
* const router_
;
149 // These objects manage channels to individual renderer processes there is
150 // one channel for each renderer process that has connected to this GPU
152 GpuChannelMap gpu_channels_
;
153 scoped_refptr
<gfx::GLShareGroup
> share_group_
;
154 scoped_refptr
<gpu::gles2::MailboxManager
> mailbox_manager_
;
155 GpuMemoryManager gpu_memory_manager_
;
156 GpuWatchdog
* watchdog_
;
157 // SyncPointManager guaranteed to outlive running MessageLoop.
158 gpu::SyncPointManager
* sync_point_manager_
;
159 scoped_ptr
<gpu::gles2::ProgramCache
> program_cache_
;
160 scoped_refptr
<gpu::gles2::ShaderTranslatorCache
> shader_translator_cache_
;
161 scoped_refptr
<gfx::GLSurface
> default_offscreen_surface_
;
162 GpuMemoryBufferFactory
* const gpu_memory_buffer_factory_
;
163 IPC::SyncChannel
* channel_
;
164 bool relinquish_resources_pending_
;
165 // Must outlive this instance of GpuChannelManager.
166 IPC::AttachmentBroker
* attachment_broker_
;
168 // Member variables should appear before the WeakPtrFactory, to ensure
169 // that any WeakPtrs to Controller are invalidated before its members
170 // variable's destructors are executed, rendering them invalid.
171 base::WeakPtrFactory
<GpuChannelManager
> weak_factory_
;
173 DISALLOW_COPY_AND_ASSIGN(GpuChannelManager
);
176 } // namespace content
178 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_