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