- add sources.
[platform/framework/web/crosswalk.git] / src / content / common / gpu / gpu_channel_manager.h
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.
4
5 #ifndef CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_
6 #define CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_
7
8 #include <deque>
9 #include <string>
10 #include <vector>
11
12 #include "base/containers/hash_tables.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/gpu/gpu_memory_manager.h"
19 #include "ipc/ipc_listener.h"
20 #include "ipc/ipc_sender.h"
21 #include "ui/gfx/native_widget_types.h"
22 #include "ui/gl/gl_surface.h"
23
24 namespace base {
25 class WaitableEvent;
26 }
27
28 namespace gfx {
29 class GLShareGroup;
30 }
31
32 namespace gpu {
33 namespace gles2 {
34 class MailboxManager;
35 class ProgramCache;
36 }
37 }
38
39 namespace IPC {
40 struct ChannelHandle;
41 }
42
43 struct GPUCreateCommandBufferConfig;
44
45 namespace content {
46 class ChildThread;
47 class GpuChannel;
48 class GpuWatchdog;
49 class SyncPointManager;
50
51 // A GpuChannelManager is a thread responsible for issuing rendering commands
52 // managing the lifetimes of GPU channels and forwarding IPC requests from the
53 // browser process to them based on the corresponding renderer ID.
54 //
55 // A GpuChannelManager can also be hosted in the browser process in single
56 // process or in-process GPU modes. In this case there is no corresponding
57 // GpuChildThread and this is the reason the GpuChildThread is referenced via
58 // a pointer to IPC::Sender, which can be implemented by other hosts to send
59 // IPC messages to the browser process IO thread on the GpuChannelManager's
60 // behalf.
61 class GpuChannelManager : public IPC::Listener,
62                           public IPC::Sender {
63  public:
64   GpuChannelManager(ChildThread* gpu_child_thread,
65                     GpuWatchdog* watchdog,
66                     base::MessageLoopProxy* io_message_loop,
67                     base::WaitableEvent* shutdown_event);
68   virtual ~GpuChannelManager();
69
70   // Remove the channel for a particular renderer.
71   void RemoveChannel(int client_id);
72
73   // Listener overrides.
74   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
75
76   // Sender overrides.
77   virtual bool Send(IPC::Message* msg) OVERRIDE;
78
79   bool HandleMessagesScheduled();
80   uint64 MessagesProcessed();
81
82   void LoseAllContexts();
83
84   base::WeakPtrFactory<GpuChannelManager> weak_factory_;
85
86   int GenerateRouteID();
87   void AddRoute(int32 routing_id, IPC::Listener* listener);
88   void RemoveRoute(int32 routing_id);
89
90   gpu::gles2::ProgramCache* program_cache();
91
92   GpuMemoryManager* gpu_memory_manager() { return &gpu_memory_manager_; }
93
94   GpuChannel* LookupChannel(int32 client_id);
95
96   SyncPointManager* sync_point_manager() { return sync_point_manager_.get(); }
97
98   gfx::GLSurface* GetDefaultOffscreenSurface();
99
100  private:
101   struct ImageOperation {
102     ImageOperation(int32 sync_point, base::Closure callback);
103     ~ImageOperation();
104
105     int32 sync_point;
106     base::Closure callback;
107   };
108   typedef base::hash_map<int, scoped_refptr<GpuChannel> > GpuChannelMap;
109   typedef std::deque<ImageOperation*> ImageOperationQueue;
110
111   // Message handlers.
112   void OnEstablishChannel(int client_id, bool share_context);
113   void OnCloseChannel(const IPC::ChannelHandle& channel_handle);
114   void OnVisibilityChanged(
115       int32 render_view_id, int32 client_id, bool visible);
116   void OnCreateViewCommandBuffer(
117       const gfx::GLSurfaceHandle& window,
118       int32 render_view_id,
119       int32 client_id,
120       const GPUCreateCommandBufferConfig& init_params);
121   void CreateImage(
122       gfx::PluginWindowHandle window, int32 client_id, int32 image_id);
123   void OnCreateImage(
124       gfx::PluginWindowHandle window, int32 client_id, int32 image_id);
125   void DeleteImage(int32 client_id, int32 image_id);
126   void OnDeleteImage(int32 client_id, int32 image_id, int32 sync_point);
127   void OnDeleteImageSyncPointRetired(ImageOperation*);
128   void OnLoadedShader(std::string shader);
129
130   void OnLoseAllContexts();
131
132   scoped_refptr<base::MessageLoopProxy> io_message_loop_;
133   base::WaitableEvent* shutdown_event_;
134
135   // Used to send and receive IPC messages from the browser process.
136   ChildThread* gpu_child_thread_;
137
138   // These objects manage channels to individual renderer processes there is
139   // one channel for each renderer process that has connected to this GPU
140   // process.
141   GpuChannelMap gpu_channels_;
142   scoped_refptr<gfx::GLShareGroup> share_group_;
143   scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
144   GpuMemoryManager gpu_memory_manager_;
145   GpuWatchdog* watchdog_;
146   scoped_refptr<SyncPointManager> sync_point_manager_;
147   scoped_ptr<gpu::gles2::ProgramCache> program_cache_;
148   scoped_refptr<gfx::GLSurface> default_offscreen_surface_;
149   ImageOperationQueue image_operations_;
150
151   DISALLOW_COPY_AND_ASSIGN(GpuChannelManager);
152 };
153
154 }  // namespace content
155
156 #endif  // CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_