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/base/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 struct GpuHostMsg_AcceleratedSurfaceRelease_Params;
     28 
     29 namespace gfx {
     30 class GLSurface;
     31 }
     32 
     33 namespace gpu {
     34 class GpuScheduler;
     35 class PreemptionFlag;
     36 namespace gles2 {
     37 class GLES2Decoder;
     38 }
     39 }
     40 
     41 namespace content {
     42 class GpuChannelManager;
     43 class GpuCommandBufferStub;
     44 
     45 // The GPU process is agnostic as to how it displays results. On some platforms
     46 // it renders directly to window. On others it renders offscreen and transports
     47 // the results to the browser process to display. This file provides a simple
     48 // framework for making the offscreen path seem more like the onscreen path.
     49 //
     50 // The ImageTransportSurface class defines an simple interface for events that
     51 // should be responded to. The factory returns an offscreen surface that looks
     52 // a lot like an onscreen surface to the GPU process.
     53 //
     54 // The ImageTransportSurfaceHelper provides some glue to the outside world:
     55 // making sure outside events reach the ImageTransportSurface and
     56 // allowing the ImageTransportSurface to send events to the outside world.
     57 
     58 class ImageTransportSurface {
     59  public:
     60   ImageTransportSurface();
     61 
     62   virtual void OnBufferPresented(
     63       const AcceleratedSurfaceMsg_BufferPresented_Params& params) = 0;
     64   virtual void OnResizeViewACK() = 0;
     65   virtual void OnResize(gfx::Size size, float scale_factor) = 0;
     66   virtual void SetLatencyInfo(
     67       const ui::LatencyInfo& latency_info) = 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       GpuHostMsg_AcceleratedSurfaceRelease_Params params);
    125   void SendResizeView(const gfx::Size& size);
    126   void SendUpdateVSyncParameters(
    127       base::TimeTicks timebase, base::TimeDelta interval);
    128 
    129   void SendLatencyInfo(const ui::LatencyInfo& latency_info);
    130 
    131   // Whether or not we should execute more commands.
    132   void SetScheduled(bool is_scheduled);
    133 
    134   void DeferToFence(base::Closure task);
    135 
    136   void SetPreemptByFlag(
    137       scoped_refptr<gpu::PreemptionFlag> preemption_flag);
    138 
    139   // Make the surface's context current.
    140   bool MakeCurrent();
    141 
    142   // Set the default swap interval on the surface.
    143   static void SetSwapInterval(gfx::GLContext* context);
    144 
    145   void Suspend();
    146 
    147   GpuChannelManager* manager() const { return manager_; }
    148   GpuCommandBufferStub* stub() const { return stub_.get(); }
    149 
    150  private:
    151   gpu::GpuScheduler* Scheduler();
    152   gpu::gles2::GLES2Decoder* Decoder();
    153 
    154   // IPC::Message handlers.
    155   void OnBufferPresented(
    156       const AcceleratedSurfaceMsg_BufferPresented_Params& params);
    157   void OnResizeViewACK();
    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 
    203  protected:
    204   virtual ~PassThroughImageTransportSurface();
    205 
    206   // If updated vsync parameters can be determined, send this information to
    207   // the browser.
    208   virtual void SendVSyncUpdateIfAvailable();
    209 
    210  private:
    211   scoped_ptr<ImageTransportHelper> helper_;
    212   gfx::Size new_size_;
    213   bool transport_;
    214   bool did_set_swap_interval_;
    215   bool did_unschedule_;
    216   bool is_swap_buffers_pending_;
    217   ui::LatencyInfo latency_info_;
    218 
    219   DISALLOW_COPY_AND_ASSIGN(PassThroughImageTransportSurface);
    220 };
    221 
    222 }  // namespace content
    223 
    224 #endif  // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_
    225