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_IMAGE_TRANSPORT_SURFACE_H_
      6 #define CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_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/memory/weak_ptr.h"
     15 #include "content/common/content_export.h"
     16 #include "ipc/ipc_listener.h"
     17 #include "ipc/ipc_message.h"
     18 #include "ui/events/latency_info.h"
     19 #include "ui/gfx/native_widget_types.h"
     20 #include "ui/gfx/rect.h"
     21 #include "ui/gfx/size.h"
     22 #include "ui/gl/gl_surface.h"
     23 
     24 struct AcceleratedSurfaceMsg_BufferPresented_Params;
     25 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
     26 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
     27 
     28 namespace gfx {
     29 class GLSurface;
     30 }
     31 
     32 namespace gpu {
     33 class GpuScheduler;
     34 class PreemptionFlag;
     35 namespace gles2 {
     36 class GLES2Decoder;
     37 }
     38 }
     39 
     40 namespace content {
     41 class GpuChannelManager;
     42 class GpuCommandBufferStub;
     43 
     44 // The GPU process is agnostic as to how it displays results. On some platforms
     45 // it renders directly to window. On others it renders offscreen and transports
     46 // the results to the browser process to display. This file provides a simple
     47 // framework for making the offscreen path seem more like the onscreen path.
     48 //
     49 // The ImageTransportSurface class defines an simple interface for events that
     50 // should be responded to. The factory returns an offscreen surface that looks
     51 // a lot like an onscreen surface to the GPU process.
     52 //
     53 // The ImageTransportSurfaceHelper provides some glue to the outside world:
     54 // making sure outside events reach the ImageTransportSurface and
     55 // allowing the ImageTransportSurface to send events to the outside world.
     56 
     57 class ImageTransportSurface {
     58  public:
     59   ImageTransportSurface();
     60 
     61   virtual void OnBufferPresented(
     62       const AcceleratedSurfaceMsg_BufferPresented_Params& params) = 0;
     63   virtual void OnResizeViewACK() = 0;
     64   virtual void OnResize(gfx::Size size, float scale_factor) = 0;
     65   virtual void SetLatencyInfo(
     66       const ui::LatencyInfo& latency_info) = 0;
     67   virtual void WakeUpGpu() = 0;
     68 
     69   // Creates a surface with the given attributes.
     70   static scoped_refptr<gfx::GLSurface> CreateSurface(
     71       GpuChannelManager* manager,
     72       GpuCommandBufferStub* stub,
     73       const gfx::GLSurfaceHandle& handle);
     74 
     75 #if defined(OS_MACOSX)
     76   CONTENT_EXPORT static void SetAllowOSMesaForTesting(bool allow);
     77 #endif
     78 
     79   virtual gfx::Size GetSize() = 0;
     80 
     81  protected:
     82   virtual ~ImageTransportSurface();
     83 
     84  private:
     85   // Creates the appropriate native surface depending on the GL implementation.
     86   // This will be implemented separately by each platform.
     87   //
     88   // This will not be called for texture transport surfaces which are
     89   // cross-platform. The platform implementation should only create the
     90   // surface and should not initialize it. On failure, a null scoped_refptr
     91   // should be returned.
     92   static scoped_refptr<gfx::GLSurface> CreateNativeSurface(
     93       GpuChannelManager* manager,
     94       GpuCommandBufferStub* stub,
     95       const gfx::GLSurfaceHandle& handle);
     96 
     97   DISALLOW_COPY_AND_ASSIGN(ImageTransportSurface);
     98 };
     99 
    100 class ImageTransportHelper
    101     : public IPC::Listener,
    102       public base::SupportsWeakPtr<ImageTransportHelper> {
    103  public:
    104   // Takes weak pointers to objects that outlive the helper.
    105   ImageTransportHelper(ImageTransportSurface* surface,
    106                        GpuChannelManager* manager,
    107                        GpuCommandBufferStub* stub,
    108                        gfx::PluginWindowHandle handle);
    109   virtual ~ImageTransportHelper();
    110 
    111   bool Initialize();
    112   void Destroy();
    113 
    114   // IPC::Listener implementation:
    115   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
    116 
    117   // Helper send functions. Caller fills in the surface specific params
    118   // like size and surface id. The helper fills in the rest.
    119   void SendAcceleratedSurfaceBuffersSwapped(
    120       GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params);
    121   void SendAcceleratedSurfacePostSubBuffer(
    122       GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params params);
    123   void SendAcceleratedSurfaceRelease();
    124   void SendResizeView(const gfx::Size& size);
    125   void SendUpdateVSyncParameters(
    126       base::TimeTicks timebase, base::TimeDelta interval);
    127 
    128   void SendLatencyInfo(const ui::LatencyInfo& latency_info);
    129 
    130   // Whether or not we should execute more commands.
    131   void SetScheduled(bool is_scheduled);
    132 
    133   void DeferToFence(base::Closure task);
    134 
    135   void SetPreemptByFlag(
    136       scoped_refptr<gpu::PreemptionFlag> preemption_flag);
    137 
    138   // Make the surface's context current.
    139   bool MakeCurrent();
    140 
    141   // Set the default swap interval on the surface.
    142   static void SetSwapInterval(gfx::GLContext* context);
    143 
    144   void Suspend();
    145 
    146   GpuChannelManager* manager() const { return manager_; }
    147   GpuCommandBufferStub* stub() const { return stub_.get(); }
    148 
    149  private:
    150   gpu::GpuScheduler* Scheduler();
    151   gpu::gles2::GLES2Decoder* Decoder();
    152 
    153   // IPC::Message handlers.
    154   void OnBufferPresented(
    155       const AcceleratedSurfaceMsg_BufferPresented_Params& params);
    156   void OnResizeViewACK();
    157   void OnWakeUpGpu();
    158 
    159   // Backbuffer resize callback.
    160   void Resize(gfx::Size size, float scale_factor);
    161 
    162   void SetLatencyInfo(const ui::LatencyInfo& latency_info);
    163 
    164   // Weak pointers that point to objects that outlive this helper.
    165   ImageTransportSurface* surface_;
    166   GpuChannelManager* manager_;
    167 
    168   base::WeakPtr<GpuCommandBufferStub> stub_;
    169   int32 route_id_;
    170   gfx::PluginWindowHandle handle_;
    171 
    172   DISALLOW_COPY_AND_ASSIGN(ImageTransportHelper);
    173 };
    174 
    175 // An implementation of ImageTransportSurface that implements GLSurface through
    176 // GLSurfaceAdapter, thereby forwarding GLSurface methods through to it.
    177 class PassThroughImageTransportSurface
    178     : public gfx::GLSurfaceAdapter,
    179       public ImageTransportSurface {
    180  public:
    181   PassThroughImageTransportSurface(GpuChannelManager* manager,
    182                                    GpuCommandBufferStub* stub,
    183                                    gfx::GLSurface* surface,
    184                                    bool transport);
    185 
    186   // GLSurface implementation.
    187   virtual bool Initialize() OVERRIDE;
    188   virtual void Destroy() OVERRIDE;
    189   virtual bool DeferDraws() OVERRIDE;
    190   virtual bool SwapBuffers() OVERRIDE;
    191   virtual bool PostSubBuffer(int x, int y, int width, int height) OVERRIDE;
    192   virtual bool OnMakeCurrent(gfx::GLContext* context) OVERRIDE;
    193 
    194   // ImageTransportSurface implementation.
    195   virtual void OnBufferPresented(
    196       const AcceleratedSurfaceMsg_BufferPresented_Params& params) OVERRIDE;
    197   virtual void OnResizeViewACK() OVERRIDE;
    198   virtual void OnResize(gfx::Size size, float scale_factor) OVERRIDE;
    199   virtual gfx::Size GetSize() OVERRIDE;
    200   virtual void SetLatencyInfo(
    201       const ui::LatencyInfo& latency_info) OVERRIDE;
    202   virtual void WakeUpGpu() OVERRIDE;
    203 
    204  protected:
    205   virtual ~PassThroughImageTransportSurface();
    206 
    207   // If updated vsync parameters can be determined, send this information to
    208   // the browser.
    209   virtual void SendVSyncUpdateIfAvailable();
    210 
    211   ImageTransportHelper* GetHelper() { return helper_.get(); }
    212 
    213  private:
    214   scoped_ptr<ImageTransportHelper> helper_;
    215   gfx::Size new_size_;
    216   bool transport_;
    217   bool did_set_swap_interval_;
    218   bool did_unschedule_;
    219   bool is_swap_buffers_pending_;
    220   ui::LatencyInfo latency_info_;
    221 
    222   DISALLOW_COPY_AND_ASSIGN(PassThroughImageTransportSurface);
    223 };
    224 
    225 }  // namespace content
    226 
    227 #endif  // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_
    228