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_H_
      6 #define CONTENT_COMMON_GPU_GPU_CHANNEL_H_
      7 
      8 #include <deque>
      9 #include <string>
     10 
     11 #include "base/id_map.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "base/process/process.h"
     16 #include "build/build_config.h"
     17 #include "content/common/gpu/gpu_command_buffer_stub.h"
     18 #include "content/common/gpu/gpu_memory_manager.h"
     19 #include "content/common/message_router.h"
     20 #include "ipc/ipc_sync_channel.h"
     21 #include "ui/gfx/native_widget_types.h"
     22 #include "ui/gfx/size.h"
     23 #include "ui/gl/gl_share_group.h"
     24 #include "ui/gl/gpu_preference.h"
     25 
     26 #if defined(OS_ANDROID)
     27 #include "content/common/android/surface_texture_peer.h"
     28 #endif
     29 
     30 struct GPUCreateCommandBufferConfig;
     31 
     32 namespace base {
     33 class MessageLoopProxy;
     34 class WaitableEvent;
     35 }
     36 
     37 namespace gpu {
     38 class PreemptionFlag;
     39 namespace gles2 {
     40 class ImageManager;
     41 }
     42 }
     43 
     44 #if defined(OS_ANDROID)
     45 namespace content {
     46 class StreamTextureManagerAndroid;
     47 }
     48 #endif
     49 
     50 namespace content {
     51 class GpuChannelManager;
     52 struct GpuRenderingStats;
     53 class GpuWatchdog;
     54 class GpuChannelMessageFilter;
     55 
     56 // Encapsulates an IPC channel between the GPU process and one renderer
     57 // process. On the renderer side there's a corresponding GpuChannelHost.
     58 class GpuChannel : public IPC::Listener,
     59                    public IPC::Sender,
     60                    public base::RefCountedThreadSafe<GpuChannel> {
     61  public:
     62   // Takes ownership of the renderer process handle.
     63   GpuChannel(GpuChannelManager* gpu_channel_manager,
     64              GpuWatchdog* watchdog,
     65              gfx::GLShareGroup* share_group,
     66              gpu::gles2::MailboxManager* mailbox_manager,
     67              int client_id,
     68              bool software);
     69 
     70   bool Init(base::MessageLoopProxy* io_message_loop,
     71             base::WaitableEvent* shutdown_event);
     72 
     73   // Get the GpuChannelManager that owns this channel.
     74   GpuChannelManager* gpu_channel_manager() const {
     75     return gpu_channel_manager_;
     76   }
     77 
     78   // Returns the name of the associated IPC channel.
     79   std::string GetChannelName();
     80 
     81 #if defined(OS_POSIX)
     82   int TakeRendererFileDescriptor();
     83 #endif  // defined(OS_POSIX)
     84 
     85   base::ProcessId renderer_pid() const { return channel_->peer_pid(); }
     86 
     87   // IPC::Listener implementation:
     88   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
     89   virtual void OnChannelError() OVERRIDE;
     90 
     91   // IPC::Sender implementation:
     92   virtual bool Send(IPC::Message* msg) OVERRIDE;
     93 
     94   // Requeue the message that is currently being processed to the beginning of
     95   // the queue. Used when the processing of a message gets aborted because of
     96   // unscheduling conditions.
     97   void RequeueMessage();
     98 
     99   // This is called when a command buffer transitions from the unscheduled
    100   // state to the scheduled state, which potentially means the channel
    101   // transitions from the unscheduled to the scheduled state. When this occurs
    102   // deferred IPC messaged are handled.
    103   void OnScheduled();
    104 
    105   // This is called when a command buffer transitions between scheduled and
    106   // descheduled states. When any stub is descheduled, we stop preempting
    107   // other channels.
    108   void StubSchedulingChanged(bool scheduled);
    109 
    110   void CreateViewCommandBuffer(
    111       const gfx::GLSurfaceHandle& window,
    112       int32 surface_id,
    113       const GPUCreateCommandBufferConfig& init_params,
    114       int32* route_id);
    115 
    116   void CreateImage(
    117       gfx::PluginWindowHandle window,
    118       int32 image_id,
    119       gfx::Size* size);
    120   void DeleteImage(int32 image_id);
    121 
    122   gfx::GLShareGroup* share_group() const { return share_group_.get(); }
    123 
    124   GpuCommandBufferStub* LookupCommandBuffer(int32 route_id);
    125 
    126   void LoseAllContexts();
    127   void MarkAllContextsLost();
    128 
    129   // Destroy channel and all contained contexts.
    130   void DestroySoon();
    131 
    132   // Generate a route ID guaranteed to be unique for this channel.
    133   int GenerateRouteID();
    134 
    135   // Called to add/remove a listener for a particular message routing ID.
    136   void AddRoute(int32 route_id, IPC::Listener* listener);
    137   void RemoveRoute(int32 route_id);
    138 
    139   gpu::PreemptionFlag* GetPreemptionFlag();
    140 
    141   bool handle_messages_scheduled() const { return handle_messages_scheduled_; }
    142   uint64 messages_processed() const { return messages_processed_; }
    143 
    144   // If |preemption_flag->IsSet()|, any stub on this channel
    145   // should stop issuing GL commands. Setting this to NULL stops deferral.
    146   void SetPreemptByFlag(
    147       scoped_refptr<gpu::PreemptionFlag> preemption_flag);
    148 
    149 #if defined(OS_ANDROID)
    150   StreamTextureManagerAndroid* stream_texture_manager() {
    151     return stream_texture_manager_.get();
    152   }
    153 #endif
    154 
    155   void CacheShader(const std::string& key, const std::string& shader);
    156 
    157  protected:
    158   virtual ~GpuChannel();
    159 
    160  private:
    161   friend class base::RefCountedThreadSafe<GpuChannel>;
    162   friend class GpuChannelMessageFilter;
    163 
    164   void OnDestroy();
    165 
    166   bool OnControlMessageReceived(const IPC::Message& msg);
    167 
    168   void HandleMessage();
    169 
    170   // Message handlers.
    171   void OnCreateOffscreenCommandBuffer(
    172       const gfx::Size& size,
    173       const GPUCreateCommandBufferConfig& init_params,
    174       int32* route_id);
    175   void OnDestroyCommandBuffer(int32 route_id);
    176 
    177 #if defined(OS_ANDROID)
    178   // Register the StreamTextureProxy class with the gpu process so that all
    179   // the callbacks will be correctly forwarded to the renderer.
    180   void OnRegisterStreamTextureProxy(int32 stream_id, int32* route_id);
    181 
    182   // Create a java surface texture object and send it to the renderer process
    183   // through binder thread.
    184   void OnEstablishStreamTexture(
    185       int32 stream_id, int32 primary_id, int32 secondary_id);
    186 
    187   // Set the size of StreamTexture.
    188   void OnSetStreamTextureSize(int32 stream_id, const gfx::Size& size);
    189 #endif
    190 
    191   // Collect rendering stats.
    192   void OnCollectRenderingStatsForSurface(
    193       int32 surface_id, GpuRenderingStats* stats);
    194 
    195   // Decrement the count of unhandled IPC messages and defer preemption.
    196   void MessageProcessed();
    197 
    198   // The lifetime of objects of this class is managed by a GpuChannelManager.
    199   // The GpuChannelManager destroy all the GpuChannels that they own when they
    200   // are destroyed. So a raw pointer is safe.
    201   GpuChannelManager* gpu_channel_manager_;
    202 
    203   scoped_ptr<IPC::SyncChannel> channel_;
    204 
    205   uint64 messages_processed_;
    206 
    207   // Whether the processing of IPCs on this channel is stalled and we should
    208   // preempt other GpuChannels.
    209   scoped_refptr<gpu::PreemptionFlag> preempting_flag_;
    210 
    211   // If non-NULL, all stubs on this channel should stop processing GL
    212   // commands (via their GpuScheduler) when preempted_flag_->IsSet()
    213   scoped_refptr<gpu::PreemptionFlag> preempted_flag_;
    214 
    215   std::deque<IPC::Message*> deferred_messages_;
    216 
    217   // The id of the client who is on the other side of the channel.
    218   int client_id_;
    219 
    220   // Uniquely identifies the channel within this GPU process.
    221   std::string channel_id_;
    222 
    223   // Used to implement message routing functionality to CommandBuffer objects
    224   MessageRouter router_;
    225 
    226   // The share group that all contexts associated with a particular renderer
    227   // process use.
    228   scoped_refptr<gfx::GLShareGroup> share_group_;
    229 
    230   scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
    231   scoped_refptr<gpu::gles2::ImageManager> image_manager_;
    232 #if defined(OS_ANDROID)
    233   scoped_ptr<StreamTextureManagerAndroid> stream_texture_manager_;
    234 #endif
    235 
    236 #if defined(ENABLE_GPU)
    237   typedef IDMap<GpuCommandBufferStub, IDMapOwnPointer> StubMap;
    238   StubMap stubs_;
    239 #endif  // defined (ENABLE_GPU)
    240 
    241   bool log_messages_;  // True if we should log sent and received messages.
    242   gpu::gles2::DisallowedFeatures disallowed_features_;
    243   GpuWatchdog* watchdog_;
    244   bool software_;
    245   bool handle_messages_scheduled_;
    246   bool processed_get_state_fast_;
    247   IPC::Message* currently_processing_message_;
    248 
    249   base::WeakPtrFactory<GpuChannel> weak_factory_;
    250 
    251   scoped_refptr<GpuChannelMessageFilter> filter_;
    252   scoped_refptr<base::MessageLoopProxy> io_message_loop_;
    253 
    254   size_t num_stubs_descheduled_;
    255 
    256   DISALLOW_COPY_AND_ASSIGN(GpuChannel);
    257 };
    258 
    259 }  // namespace content
    260 
    261 #endif  // CONTENT_COMMON_GPU_GPU_CHANNEL_H_
    262