Home | History | Annotate | Download | only in gl
      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 UI_GL_GL_SURFACE_H_
      6 #define UI_GL_GL_SURFACE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "build/build_config.h"
     12 #include "ui/gfx/native_widget_types.h"
     13 #include "ui/gfx/size.h"
     14 #include "ui/gl/gl_export.h"
     15 #include "ui/gl/gl_implementation.h"
     16 
     17 namespace gfx {
     18 
     19 class GLContext;
     20 class VSyncProvider;
     21 
     22 // Encapsulates a surface that can be rendered to with GL, hiding platform
     23 // specific management.
     24 class GL_EXPORT GLSurface : public base::RefCounted<GLSurface> {
     25  public:
     26   GLSurface();
     27 
     28   // (Re)create the surface. TODO(apatrick): This is an ugly hack to allow the
     29   // EGL surface associated to be recreated without destroying the associated
     30   // context. The implementation of this function for other GLSurface derived
     31   // classes is in a pending changelist.
     32   virtual bool Initialize();
     33 
     34   // Destroys the surface.
     35   virtual void Destroy() = 0;
     36 
     37   virtual bool Resize(const gfx::Size& size);
     38 
     39   // Recreate the surface without changing the size.
     40   virtual bool Recreate();
     41 
     42   // Unschedule the GpuScheduler and return true to abort the processing of
     43   // a GL draw call to this surface and defer it until the GpuScheduler is
     44   // rescheduled.
     45   virtual bool DeferDraws();
     46 
     47   // Returns true if this surface is offscreen.
     48   virtual bool IsOffscreen() = 0;
     49 
     50   // Swaps front and back buffers. This has no effect for off-screen
     51   // contexts.
     52   virtual bool SwapBuffers() = 0;
     53 
     54   // Get the size of the surface.
     55   virtual gfx::Size GetSize() = 0;
     56 
     57   // Get the underlying platform specific surface "handle".
     58   virtual void* GetHandle() = 0;
     59 
     60   // Returns whether or not the surface supports PostSubBuffer.
     61   virtual bool SupportsPostSubBuffer();
     62 
     63   // Returns the internal frame buffer object name if the surface is backed by
     64   // FBO. Otherwise returns 0.
     65   virtual unsigned int GetBackingFrameBufferObject();
     66 
     67   // Copy part of the backbuffer to the frontbuffer.
     68   virtual bool PostSubBuffer(int x, int y, int width, int height);
     69 
     70   // Initialize GL bindings.
     71   static bool InitializeOneOff();
     72 
     73   // Unit tests should call these instead of InitializeOneOff() to set up
     74   // GL bindings appropriate for tests.
     75   static void InitializeOneOffForTests();
     76   static void InitializeOneOffWithMockBindingsForTests();
     77   static void InitializeDynamicMockBindingsForTests(GLContext* context);
     78 
     79   // Called after a context is made current with this surface. Returns false
     80   // on error.
     81   virtual bool OnMakeCurrent(GLContext* context);
     82 
     83   // Used for explicit buffer management.
     84   virtual bool SetBackbufferAllocation(bool allocated);
     85   virtual void SetFrontbufferAllocation(bool allocated);
     86 
     87   // Get a handle used to share the surface with another process. Returns null
     88   // if this is not possible.
     89   virtual void* GetShareHandle();
     90 
     91   // Get the platform specific display on which this surface resides, if
     92   // available.
     93   virtual void* GetDisplay();
     94 
     95   // Get the platfrom specific configuration for this surface, if available.
     96   virtual void* GetConfig();
     97 
     98   // Get the GL pixel format of the surface, if available.
     99   virtual unsigned GetFormat();
    100 
    101   // Get access to a helper providing time of recent refresh and period
    102   // of screen refresh. If unavailable, returns NULL.
    103   virtual VSyncProvider* GetVSyncProvider();
    104 
    105   // Create a GL surface that renders directly to a view.
    106   static scoped_refptr<GLSurface> CreateViewGLSurface(
    107       gfx::AcceleratedWidget window);
    108 
    109   // Create a GL surface used for offscreen rendering.
    110   static scoped_refptr<GLSurface> CreateOffscreenGLSurface(
    111       const gfx::Size& size);
    112 
    113   static GLSurface* GetCurrent();
    114 
    115  protected:
    116   virtual ~GLSurface();
    117   static bool InitializeOneOffImplementation(GLImplementation impl,
    118                                              bool fallback_to_osmesa,
    119                                              bool gpu_service_logging,
    120                                              bool disable_gl_drawing);
    121   static bool InitializeOneOffInternal();
    122   static void SetCurrent(GLSurface* surface);
    123 
    124   static bool ExtensionsContain(const char* extensions, const char* name);
    125 
    126  private:
    127   friend class base::RefCounted<GLSurface>;
    128   friend class GLContext;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(GLSurface);
    131 };
    132 
    133 // Implementation of GLSurface that forwards all calls through to another
    134 // GLSurface.
    135 class GL_EXPORT GLSurfaceAdapter : public GLSurface {
    136  public:
    137   explicit GLSurfaceAdapter(GLSurface* surface);
    138 
    139   virtual bool Initialize() OVERRIDE;
    140   virtual void Destroy() OVERRIDE;
    141   virtual bool Resize(const gfx::Size& size) OVERRIDE;
    142   virtual bool Recreate() OVERRIDE;
    143   virtual bool DeferDraws() OVERRIDE;
    144   virtual bool IsOffscreen() OVERRIDE;
    145   virtual bool SwapBuffers() OVERRIDE;
    146   virtual bool PostSubBuffer(int x, int y, int width, int height) OVERRIDE;
    147   virtual bool SupportsPostSubBuffer() OVERRIDE;
    148   virtual gfx::Size GetSize() OVERRIDE;
    149   virtual void* GetHandle() OVERRIDE;
    150   virtual unsigned int GetBackingFrameBufferObject() OVERRIDE;
    151   virtual bool OnMakeCurrent(GLContext* context) OVERRIDE;
    152   virtual bool SetBackbufferAllocation(bool allocated) OVERRIDE;
    153   virtual void SetFrontbufferAllocation(bool allocated) OVERRIDE;
    154   virtual void* GetShareHandle() OVERRIDE;
    155   virtual void* GetDisplay() OVERRIDE;
    156   virtual void* GetConfig() OVERRIDE;
    157   virtual unsigned GetFormat() OVERRIDE;
    158   virtual VSyncProvider* GetVSyncProvider() OVERRIDE;
    159 
    160   GLSurface* surface() const { return surface_.get(); }
    161 
    162  protected:
    163   virtual ~GLSurfaceAdapter();
    164 
    165  private:
    166   scoped_refptr<GLSurface> surface_;
    167 
    168   DISALLOW_COPY_AND_ASSIGN(GLSurfaceAdapter);
    169 };
    170 
    171 }  // namespace gfx
    172 
    173 #endif  // UI_GL_GL_SURFACE_H_
    174