GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / content / common / gpu / gpu_channel_manager.h
blobfc64d6c5d056dadc614dbf53fa82006d8014abdc
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_
8 #include <deque>
9 #include <string>
10 #include <vector>
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/gpu_memory_manager.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"
27 namespace base {
28 class WaitableEvent;
31 namespace gfx {
32 class GLShareGroup;
33 struct GpuMemoryBufferHandle;
36 namespace gpu {
37 class SyncPointManager;
38 union ValueState;
39 namespace gles2 {
40 class MailboxManager;
41 class ProgramCache;
42 class ShaderTranslatorCache;
46 namespace IPC {
47 struct ChannelHandle;
48 class SyncChannel;
49 class MessageFilter;
52 struct GPUCreateCommandBufferConfig;
54 namespace content {
55 class GpuChannel;
56 class GpuMemoryBufferFactory;
57 class GpuWatchdog;
58 class MessageRouter;
60 // A GpuChannelManager is a thread responsible for issuing rendering commands
61 // managing the lifetimes of GPU channels and forwarding IPC requests from the
62 // browser process to them based on the corresponding renderer ID.
63 class CONTENT_EXPORT GpuChannelManager : public IPC::Listener,
64 public IPC::Sender {
65 public:
66 GpuChannelManager(MessageRouter* router,
67 GpuWatchdog* watchdog,
68 base::MessageLoopProxy* io_message_loop,
69 base::WaitableEvent* shutdown_event,
70 IPC::SyncChannel* channel);
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;
79 // Sender overrides.
80 bool Send(IPC::Message* msg) override;
82 bool HandleMessagesScheduled();
83 uint64 MessagesProcessed();
85 void LoseAllContexts();
87 int GenerateRouteID();
88 void AddRoute(int32 routing_id, IPC::Listener* listener);
89 void RemoveRoute(int32 routing_id);
91 gpu::gles2::ProgramCache* program_cache();
92 gpu::gles2::ShaderTranslatorCache* shader_translator_cache();
94 GpuMemoryManager* gpu_memory_manager() { return &gpu_memory_manager_; }
96 GpuChannel* LookupChannel(int32 client_id);
98 gpu::SyncPointManager* sync_point_manager() {
99 return sync_point_manager_.get();
102 gfx::GLSurface* GetDefaultOffscreenSurface();
104 GpuMemoryBufferFactory* gpu_memory_buffer_factory() {
105 return gpu_memory_buffer_factory_.get();
108 private:
109 typedef base::ScopedPtrHashMap<int, GpuChannel> GpuChannelMap;
111 // Message handlers.
112 void OnEstablishChannel(int client_id,
113 bool share_context,
114 bool allow_future_sync_points);
115 void OnCloseChannel(const IPC::ChannelHandle& channel_handle);
116 void OnVisibilityChanged(
117 int32 render_view_id, int32 client_id, bool visible);
118 void OnCreateViewCommandBuffer(
119 const gfx::GLSurfaceHandle& window,
120 int32 render_view_id,
121 int32 client_id,
122 const GPUCreateCommandBufferConfig& init_params,
123 int32 route_id);
124 void OnLoadedShader(std::string shader);
125 void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, int client_id);
126 void DestroyGpuMemoryBufferOnIO(gfx::GpuMemoryBufferId id, int client_id);
127 void OnDestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id,
128 int client_id,
129 int32 sync_point);
131 void OnRelinquishResources();
132 void OnResourcesRelinquished();
134 void OnUpdateValueState(int client_id,
135 unsigned int target,
136 const gpu::ValueState& state);
138 void OnLoseAllContexts();
139 void CheckRelinquishGpuResources();
141 scoped_refptr<base::MessageLoopProxy> io_message_loop_;
142 base::WaitableEvent* shutdown_event_;
144 // Used to send and receive IPC messages from the browser process.
145 MessageRouter* const router_;
147 // These objects manage channels to individual renderer processes there is
148 // one channel for each renderer process that has connected to this GPU
149 // process.
150 GpuChannelMap gpu_channels_;
151 scoped_refptr<gfx::GLShareGroup> share_group_;
152 scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
153 GpuMemoryManager gpu_memory_manager_;
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_;
162 bool relinquish_resources_pending_;
164 // Member variables should appear before the WeakPtrFactory, to ensure
165 // that any WeakPtrs to Controller are invalidated before its members
166 // variable's destructors are executed, rendering them invalid.
167 base::WeakPtrFactory<GpuChannelManager> weak_factory_;
169 DISALLOW_COPY_AND_ASSIGN(GpuChannelManager);
172 } // namespace content
174 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_