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