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