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/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/gpu/devtools_gpu_instrumentation.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/native_widget_types.h"
23#include "ui/gl/gl_surface.h"
24
25namespace base {
26class WaitableEvent;
27}
28
29namespace gfx {
30class GLShareGroup;
31struct GpuMemoryBufferHandle;
32}
33
34namespace gpu {
35namespace gles2 {
36class MailboxManager;
37class ProgramCache;
38class ShaderTranslatorCache;
39}
40}
41
42namespace IPC {
43struct ChannelHandle;
44}
45
46struct GPUCreateCommandBufferConfig;
47
48namespace content {
49class GpuChannel;
50class GpuWatchdog;
51class MessageRouter;
52class SyncPointManager;
53
54// A GpuChannelManager is a thread responsible for issuing rendering commands
55// managing the lifetimes of GPU channels and forwarding IPC requests from the
56// browser process to them based on the corresponding renderer ID.
57class GpuChannelManager : public IPC::Listener,
58                          public IPC::Sender {
59 public:
60  GpuChannelManager(MessageRouter* router,
61                    GpuWatchdog* watchdog,
62                    base::MessageLoopProxy* io_message_loop,
63                    base::WaitableEvent* shutdown_event);
64  virtual ~GpuChannelManager();
65
66  // Remove the channel for a particular renderer.
67  void RemoveChannel(int client_id);
68
69  // Listener overrides.
70  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
71
72  // Sender overrides.
73  virtual bool Send(IPC::Message* msg) OVERRIDE;
74
75  bool HandleMessagesScheduled();
76  uint64 MessagesProcessed();
77
78  void LoseAllContexts();
79
80  base::WeakPtrFactory<GpuChannelManager> weak_factory_;
81
82  int GenerateRouteID();
83  void AddRoute(int32 routing_id, IPC::Listener* listener);
84  void RemoveRoute(int32 routing_id);
85
86  gpu::gles2::ProgramCache* program_cache();
87  gpu::gles2::ShaderTranslatorCache* shader_translator_cache();
88
89  GpuMemoryManager* gpu_memory_manager() { return &gpu_memory_manager_; }
90
91  GpuEventsDispatcher* gpu_devtools_events_dispatcher() {
92    return &gpu_devtools_events_dispatcher_;
93  }
94
95  GpuChannel* LookupChannel(int32 client_id);
96
97  SyncPointManager* sync_point_manager() { return sync_point_manager_.get(); }
98
99  gfx::GLSurface* GetDefaultOffscreenSurface();
100
101 private:
102  struct ImageOperation {
103    ImageOperation(int32 sync_point, base::Closure callback);
104    ~ImageOperation();
105
106    int32 sync_point;
107    base::Closure callback;
108  };
109  typedef base::ScopedPtrHashMap<int, GpuChannel> GpuChannelMap;
110  typedef std::deque<ImageOperation*> ImageOperationQueue;
111
112  // Message handlers.
113  void OnEstablishChannel(int client_id, bool share_context);
114  void OnCloseChannel(const IPC::ChannelHandle& channel_handle);
115  void OnVisibilityChanged(
116      int32 render_view_id, int32 client_id, bool visible);
117  void OnCreateViewCommandBuffer(
118      const gfx::GLSurfaceHandle& window,
119      int32 render_view_id,
120      int32 client_id,
121      const GPUCreateCommandBufferConfig& init_params,
122      int32 route_id);
123  void CreateImage(
124      gfx::PluginWindowHandle window, int32 client_id, int32 image_id);
125  void OnCreateImage(
126      gfx::PluginWindowHandle window, int32 client_id, int32 image_id);
127  void DeleteImage(int32 client_id, int32 image_id);
128  void OnDeleteImage(int32 client_id, int32 image_id, int32 sync_point);
129  void OnDeleteImageSyncPointRetired(ImageOperation*);
130  void OnLoadedShader(std::string shader);
131  void OnCreateGpuMemoryBuffer(const gfx::GpuMemoryBufferHandle& handle,
132                               const gfx::Size& size,
133                               unsigned internalformat,
134                               unsigned usage);
135  void OnDestroyGpuMemoryBuffer(const gfx::GpuMemoryBufferHandle& handle,
136                                int32 sync_point);
137
138  void OnLoseAllContexts();
139
140  scoped_refptr<base::MessageLoopProxy> io_message_loop_;
141  base::WaitableEvent* shutdown_event_;
142
143  // Used to send and receive IPC messages from the browser process.
144  MessageRouter* const router_;
145
146  // These objects manage channels to individual renderer processes there is
147  // one channel for each renderer process that has connected to this GPU
148  // process.
149  GpuChannelMap gpu_channels_;
150  scoped_refptr<gfx::GLShareGroup> share_group_;
151  scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
152  GpuMemoryManager gpu_memory_manager_;
153  GpuEventsDispatcher gpu_devtools_events_dispatcher_;
154  GpuWatchdog* watchdog_;
155  scoped_refptr<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  ImageOperationQueue image_operations_;
160
161  DISALLOW_COPY_AND_ASSIGN(GpuChannelManager);
162};
163
164}  // namespace content
165
166#endif  // CONTENT_COMMON_GPU_GPU_CHANNEL_MANAGER_H_
167