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