Home | History | Annotate | Download | only in service
      1 // Copyright 2013 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 GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
      6 #define GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/callback.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/synchronization/lock.h"
     15 #include "base/synchronization/waitable_event.h"
     16 #include "gpu/command_buffer/common/command_buffer.h"
     17 #include "gpu/gpu_export.h"
     18 #include "ui/gfx/gpu_memory_buffer.h"
     19 #include "ui/gfx/native_widget_types.h"
     20 #include "ui/gl/gl_surface.h"
     21 #include "ui/gl/gpu_preference.h"
     22 
     23 namespace base {
     24 class SequenceChecker;
     25 }
     26 
     27 namespace gfx {
     28 class GLContext;
     29 class GLImage;
     30 class GLSurface;
     31 class Size;
     32 }
     33 
     34 #if defined(OS_ANDROID)
     35 namespace gfx {
     36 class SurfaceTextureBridge;
     37 }
     38 namespace gpu {
     39 class StreamTextureManagerInProcess;
     40 }
     41 #endif
     42 
     43 namespace gpu {
     44 
     45 namespace gles2 {
     46 class GLES2Decoder;
     47 }
     48 
     49 class GpuScheduler;
     50 class TransferBufferManagerInterface;
     51 
     52 // This class provides a thread-safe interface to the global GPU service (for
     53 // example GPU thread) when being run in single process mode.
     54 // However, the behavior for accessing one context (i.e. one instance of this
     55 // class) from different client threads is undefined.
     56 class GPU_EXPORT InProcessCommandBuffer : public CommandBuffer {
     57  public:
     58   InProcessCommandBuffer();
     59   virtual ~InProcessCommandBuffer();
     60 
     61   // Used to override the GPU thread with explicit scheduling.
     62   // (By default an internal GPU thread will be spawned to handle all GL work
     63   // and the two functions are unused.)
     64   // The callback will be called from different client threads. After the
     65   // callback is issued, the client is expected to eventually call
     66   // ProcessGpuWorkOnCurrentThread(). The latter cannot be called from different
     67   // threads.
     68   // The callback needs to be set before any context is created.
     69   static void SetScheduleCallback(const base::Closure& callback);
     70   static void ProcessGpuWorkOnCurrentThread();
     71 
     72   static void EnableVirtualizedContext();
     73 
     74   // If |surface| is not NULL, use it directly; in this case, the command
     75   // buffer gpu thread must be the same as the client thread. Otherwise create
     76   // a new GLSurface.
     77   bool Initialize(scoped_refptr<gfx::GLSurface> surface,
     78                   bool is_offscreen,
     79                   bool share_resources,
     80                   gfx::AcceleratedWidget window,
     81                   const gfx::Size& size,
     82                   const char* allowed_extensions,
     83                   const std::vector<int32>& attribs,
     84                   gfx::GpuPreference gpu_preference,
     85                   const base::Closure& context_lost_callback,
     86                   unsigned int share_group_id);
     87   void Destroy();
     88   void SignalSyncPoint(unsigned sync_point,
     89                        const base::Closure& callback);
     90   unsigned int CreateImageForGpuMemoryBuffer(
     91       gfx::GpuMemoryBufferHandle buffer,
     92       gfx::Size size);
     93   void RemoveImage(unsigned int image_id);
     94 
     95   // CommandBuffer implementation:
     96   virtual bool Initialize() OVERRIDE;
     97   virtual State GetState() OVERRIDE;
     98   virtual State GetLastState() OVERRIDE;
     99   virtual int32 GetLastToken() OVERRIDE;
    100   virtual void Flush(int32 put_offset) OVERRIDE;
    101   virtual State FlushSync(int32 put_offset, int32 last_known_get) OVERRIDE;
    102   virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
    103   virtual void SetGetOffset(int32 get_offset) OVERRIDE;
    104   virtual gpu::Buffer CreateTransferBuffer(size_t size, int32* id) OVERRIDE;
    105   virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
    106   virtual gpu::Buffer GetTransferBuffer(int32 id) OVERRIDE;
    107   virtual void SetToken(int32 token) OVERRIDE;
    108   virtual void SetParseError(gpu::error::Error error) OVERRIDE;
    109   virtual void SetContextLostReason(
    110       gpu::error::ContextLostReason reason) OVERRIDE;
    111   virtual uint32 InsertSyncPoint() OVERRIDE;
    112   virtual gpu::error::Error GetLastError() OVERRIDE;
    113 
    114   // The serializer interface to the GPU service (i.e. thread).
    115   class SchedulerClient {
    116    public:
    117      virtual ~SchedulerClient() {}
    118      virtual void QueueTask(const base::Closure& task) = 0;
    119   };
    120 
    121 #if defined(OS_ANDROID)
    122   scoped_refptr<gfx::SurfaceTextureBridge> GetSurfaceTexture(
    123       uint32 stream_id);
    124 #endif
    125 
    126  private:
    127   bool InitializeOnGpuThread(bool is_offscreen,
    128                              gfx::AcceleratedWidget window,
    129                              const gfx::Size& size,
    130                              const char* allowed_extensions,
    131                              const std::vector<int32>& attribs,
    132                              gfx::GpuPreference gpu_preference);
    133   bool DestroyOnGpuThread();
    134   void FlushOnGpuThread(int32 put_offset);
    135   void CreateImageOnGpuThread(gfx::GpuMemoryBufferHandle buffer,
    136                               gfx::Size size,
    137                               unsigned int image_id);
    138   void RemoveImageOnGpuThread(unsigned int image_id);
    139   bool MakeCurrent();
    140   bool IsContextLost();
    141   base::Closure WrapCallback(const base::Closure& callback);
    142   State GetStateFast();
    143   void QueueTask(const base::Closure& task) { queue_->QueueTask(task); }
    144   void CheckSequencedThread();
    145 
    146   // Callbacks:
    147   void OnContextLost();
    148   void OnResizeView(gfx::Size size, float scale_factor);
    149   bool GetBufferChanged(int32 transfer_buffer_id);
    150   void PumpCommands();
    151 
    152   // Members accessed on the gpu thread (possibly with the exception of
    153   // creation):
    154   bool context_lost_;
    155   bool share_resources_;
    156   scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
    157   scoped_ptr<GpuScheduler> gpu_scheduler_;
    158   scoped_ptr<gles2::GLES2Decoder> decoder_;
    159   scoped_refptr<gfx::GLContext> context_;
    160   scoped_refptr<gfx::GLSurface> surface_;
    161   base::Closure context_lost_callback_;
    162   unsigned int share_group_id_;
    163 
    164   // Members accessed on the client thread:
    165   State last_state_;
    166   int32 last_put_offset_;
    167 
    168   // Accessed on both threads:
    169   scoped_ptr<CommandBuffer> command_buffer_;
    170   base::Lock command_buffer_lock_;
    171   base::WaitableEvent flush_event_;
    172   scoped_ptr<SchedulerClient> queue_;
    173   State state_after_last_flush_;
    174   base::Lock state_after_last_flush_lock_;
    175 
    176 #if defined(OS_ANDROID)
    177   scoped_refptr<StreamTextureManagerInProcess> stream_texture_manager_;
    178 #endif
    179 
    180   // Only used with explicit scheduling and the gpu thread is the same as
    181   // the client thread.
    182   scoped_ptr<base::SequenceChecker> sequence_checker_;
    183 
    184   DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
    185 };
    186 
    187 }  // namespace gpu
    188 
    189 #endif  // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
    190