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_COMMAND_BUFFER_STUB_H_
      6 #define CONTENT_COMMON_GPU_GPU_COMMAND_BUFFER_STUB_H_
      7 
      8 #include <deque>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/memory/scoped_vector.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/observer_list.h"
     15 #include "content/common/content_export.h"
     16 #include "content/common/gpu/gpu_memory_allocation.h"
     17 #include "content/common/gpu/gpu_memory_manager.h"
     18 #include "content/common/gpu/gpu_memory_manager_client.h"
     19 #include "gpu/command_buffer/common/constants.h"
     20 #include "gpu/command_buffer/service/command_buffer_service.h"
     21 #include "gpu/command_buffer/service/context_group.h"
     22 #include "gpu/command_buffer/service/gpu_scheduler.h"
     23 #include "ipc/ipc_listener.h"
     24 #include "ipc/ipc_sender.h"
     25 #include "media/base/video_decoder_config.h"
     26 #include "ui/base/latency_info.h"
     27 #include "ui/gfx/native_widget_types.h"
     28 #include "ui/gfx/size.h"
     29 #include "ui/gl/gl_surface.h"
     30 #include "ui/gl/gpu_preference.h"
     31 #include "url/gurl.h"
     32 
     33 namespace gpu {
     34 struct Mailbox;
     35 namespace gles2 {
     36 class ImageManager;
     37 class MailboxManager;
     38 }
     39 }
     40 
     41 namespace content {
     42 
     43 class GpuChannel;
     44 class GpuVideoDecodeAccelerator;
     45 class GpuWatchdog;
     46 
     47 class GpuCommandBufferStub
     48     : public GpuMemoryManagerClient,
     49       public IPC::Listener,
     50       public IPC::Sender,
     51       public base::SupportsWeakPtr<GpuCommandBufferStub> {
     52  public:
     53   class DestructionObserver {
     54    public:
     55     // Called in Destroy(), before the context/surface are released.
     56     virtual void OnWillDestroyStub() = 0;
     57 
     58    protected:
     59     virtual ~DestructionObserver() {}
     60   };
     61 
     62   typedef base::Callback<void(const ui::LatencyInfo&)>
     63       LatencyInfoCallback;
     64 
     65   GpuCommandBufferStub(
     66       GpuChannel* channel,
     67       GpuCommandBufferStub* share_group,
     68       const gfx::GLSurfaceHandle& handle,
     69       gpu::gles2::MailboxManager* mailbox_manager,
     70       gpu::gles2::ImageManager* image_manager,
     71       const gfx::Size& size,
     72       const gpu::gles2::DisallowedFeatures& disallowed_features,
     73       const std::string& allowed_extensions,
     74       const std::vector<int32>& attribs,
     75       gfx::GpuPreference gpu_preference,
     76       bool use_virtualized_gl_context,
     77       int32 route_id,
     78       int32 surface_id,
     79       GpuWatchdog* watchdog,
     80       bool software,
     81       const GURL& active_url);
     82 
     83   virtual ~GpuCommandBufferStub();
     84 
     85   // IPC::Listener implementation:
     86   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     87 
     88   // IPC::Sender implementation:
     89   virtual bool Send(IPC::Message* msg) OVERRIDE;
     90 
     91   // GpuMemoryManagerClient implementation:
     92   virtual gfx::Size GetSurfaceSize() const OVERRIDE;
     93   virtual gpu::gles2::MemoryTracker* GetMemoryTracker() const OVERRIDE;
     94   virtual void SetMemoryAllocation(
     95       const GpuMemoryAllocation& allocation) OVERRIDE;
     96   virtual bool GetTotalGpuMemory(uint64* bytes) OVERRIDE;
     97 
     98   // Whether this command buffer can currently handle IPC messages.
     99   bool IsScheduled();
    100 
    101   // If the command buffer is pre-empted and cannot process commands.
    102   bool IsPreempted() const {
    103     return scheduler_.get() && scheduler_->IsPreempted();
    104   }
    105 
    106   // Whether there are commands in the buffer that haven't been processed.
    107   bool HasUnprocessedCommands();
    108 
    109   gpu::gles2::GLES2Decoder* decoder() const { return decoder_.get(); }
    110   gpu::GpuScheduler* scheduler() const { return scheduler_.get(); }
    111   GpuChannel* channel() const { return channel_; }
    112 
    113   // Identifies the target surface.
    114   int32 surface_id() const { return surface_id_; }
    115 
    116   // Identifies the various GpuCommandBufferStubs in the GPU process belonging
    117   // to the same renderer process.
    118   int32 route_id() const { return route_id_; }
    119 
    120   gfx::GpuPreference gpu_preference() { return gpu_preference_; }
    121 
    122   // Sends a message to the console.
    123   void SendConsoleMessage(int32 id, const std::string& message);
    124 
    125   void SendCachedShader(const std::string& key, const std::string& shader);
    126 
    127   gfx::GLSurface* surface() const { return surface_.get(); }
    128 
    129   void AddDestructionObserver(DestructionObserver* observer);
    130   void RemoveDestructionObserver(DestructionObserver* observer);
    131 
    132   // Associates a sync point to this stub. When the stub is destroyed, it will
    133   // retire all sync points that haven't been previously retired.
    134   void AddSyncPoint(uint32 sync_point);
    135 
    136   void SetPreemptByFlag(scoped_refptr<gpu::PreemptionFlag> flag);
    137 
    138   void SetLatencyInfoCallback(const LatencyInfoCallback& callback);
    139 
    140   void MarkContextLost();
    141 
    142  private:
    143   GpuMemoryManager* GetMemoryManager();
    144   bool MakeCurrent();
    145   void Destroy();
    146 
    147   // Cleans up and sends reply if OnInitialize failed.
    148   void OnInitializeFailed(IPC::Message* reply_message);
    149 
    150   // Message handlers:
    151   void OnInitialize(base::SharedMemoryHandle shared_state_shm,
    152                     IPC::Message* reply_message);
    153   void OnSetGetBuffer(int32 shm_id, IPC::Message* reply_message);
    154   void OnProduceFrontBuffer(const gpu::Mailbox& mailbox);
    155   void OnGetState(IPC::Message* reply_message);
    156   void OnGetStateFast(IPC::Message* reply_message);
    157   void OnAsyncFlush(int32 put_offset, uint32 flush_count);
    158   void OnEcho(const IPC::Message& message);
    159   void OnRescheduled();
    160   void OnRegisterTransferBuffer(int32 id,
    161                                 base::SharedMemoryHandle transfer_buffer,
    162                                 uint32 size);
    163   void OnDestroyTransferBuffer(int32 id);
    164   void OnGetTransferBuffer(int32 id, IPC::Message* reply_message);
    165 
    166   void OnCreateVideoDecoder(
    167       media::VideoCodecProfile profile,
    168       IPC::Message* reply_message);
    169 
    170   void OnSetSurfaceVisible(bool visible);
    171 
    172   void OnDiscardBackbuffer();
    173   void OnEnsureBackbuffer();
    174 
    175   void OnRetireSyncPoint(uint32 sync_point);
    176   bool OnWaitSyncPoint(uint32 sync_point);
    177   void OnSyncPointRetired();
    178   void OnSignalSyncPoint(uint32 sync_point, uint32 id);
    179   void OnSignalSyncPointAck(uint32 id);
    180   void OnSignalQuery(uint32 query, uint32 id);
    181 
    182   void OnReceivedClientManagedMemoryStats(const GpuManagedMemoryStats& stats);
    183   void OnSetClientHasMemoryAllocationChangedCallback(bool has_callback);
    184 
    185   void OnCommandProcessed();
    186   void OnParseError();
    187   void OnSetLatencyInfo(const ui::LatencyInfo& latency_info);
    188 
    189   void ReportState();
    190 
    191   // Wrapper for GpuScheduler::PutChanged that sets the crash report URL.
    192   void PutChanged();
    193 
    194   // Poll the command buffer to execute work.
    195   void PollWork();
    196 
    197   // Whether this command buffer needs to be polled again in the future.
    198   bool HasMoreWork();
    199 
    200   void ScheduleDelayedWork(int64 delay);
    201 
    202   bool CheckContextLost();
    203 
    204   // The lifetime of objects of this class is managed by a GpuChannel. The
    205   // GpuChannels destroy all the GpuCommandBufferStubs that they own when they
    206   // are destroyed. So a raw pointer is safe.
    207   GpuChannel* channel_;
    208 
    209   // The group of contexts that share namespaces with this context.
    210   scoped_refptr<gpu::gles2::ContextGroup> context_group_;
    211 
    212   gfx::GLSurfaceHandle handle_;
    213   gfx::Size initial_size_;
    214   gpu::gles2::DisallowedFeatures disallowed_features_;
    215   std::string allowed_extensions_;
    216   std::vector<int32> requested_attribs_;
    217   gfx::GpuPreference gpu_preference_;
    218   bool use_virtualized_gl_context_;
    219   int32 route_id_;
    220   int32 surface_id_;
    221   bool software_;
    222   uint32 last_flush_count_;
    223 
    224   scoped_ptr<gpu::CommandBufferService> command_buffer_;
    225   scoped_ptr<gpu::gles2::GLES2Decoder> decoder_;
    226   scoped_ptr<gpu::GpuScheduler> scheduler_;
    227   scoped_refptr<gfx::GLSurface> surface_;
    228 
    229   scoped_ptr<GpuMemoryManagerClientState> memory_manager_client_state_;
    230   // The last memory allocation received from the GpuMemoryManager (used to
    231   // elide redundant work).
    232   bool last_memory_allocation_valid_;
    233   GpuMemoryAllocation last_memory_allocation_;
    234 
    235   GpuWatchdog* watchdog_;
    236 
    237   ObserverList<DestructionObserver> destruction_observers_;
    238 
    239   // A queue of sync points associated with this stub.
    240   std::deque<uint32> sync_points_;
    241   int sync_point_wait_count_;
    242 
    243   bool delayed_work_scheduled_;
    244   uint64 previous_messages_processed_;
    245   base::TimeTicks last_idle_time_;
    246 
    247   scoped_refptr<gpu::PreemptionFlag> preemption_flag_;
    248 
    249   LatencyInfoCallback latency_info_callback_;
    250 
    251   GURL active_url_;
    252   size_t active_url_hash_;
    253 
    254   size_t total_gpu_memory_;
    255 
    256   DISALLOW_COPY_AND_ASSIGN(GpuCommandBufferStub);
    257 };
    258 
    259 }  // namespace content
    260 
    261 #endif  // CONTENT_COMMON_GPU_GPU_COMMAND_BUFFER_STUB_H_
    262