Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / ui / gl / gl_surface.h
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   virtual void SetSwapInterval(int interval);
138
139  protected:
140   virtual ~GLSurface();
141   static bool InitializeOneOffImplementation(GLImplementation impl,
142                                              bool fallback_to_osmesa,
143                                              bool gpu_service_logging,
144                                              bool disable_gl_drawing);
145   static bool InitializeOneOffInternal();
146   static void SetCurrent(GLSurface* surface);
147
148   static bool ExtensionsContain(const char* extensions, const char* name);
149
150  private:
151   friend class base::RefCounted<GLSurface>;
152   friend class GLContext;
153
154   DISALLOW_COPY_AND_ASSIGN(GLSurface);
155 };
156
157 // Implementation of GLSurface that forwards all calls through to another
158 // GLSurface.
159 class GL_EXPORT GLSurfaceAdapter : public GLSurface {
160  public:
161   explicit GLSurfaceAdapter(GLSurface* surface);
162
163   bool Initialize() override;
164   void Destroy() override;
165   bool Resize(const gfx::Size& size) override;
166   bool Recreate() override;
167   bool DeferDraws() override;
168   bool IsOffscreen() override;
169   bool SwapBuffers() override;
170   bool PostSubBuffer(int x, int y, int width, int height) override;
171   bool SupportsPostSubBuffer() override;
172   gfx::Size GetSize() override;
173   void* GetHandle() override;
174   unsigned int GetBackingFrameBufferObject() override;
175   bool OnMakeCurrent(GLContext* context) override;
176   bool SetBackbufferAllocation(bool allocated) override;
177   void SetFrontbufferAllocation(bool allocated) override;
178   void* GetShareHandle() override;
179   void* GetDisplay() override;
180   void* GetConfig() override;
181   unsigned GetFormat() override;
182   VSyncProvider* GetVSyncProvider() override;
183   bool ScheduleOverlayPlane(int z_order,
184                             OverlayTransform transform,
185                             GLImage* image,
186                             const Rect& bounds_rect,
187                             const RectF& crop_rect) override;
188   bool IsSurfaceless() const override;
189
190   GLSurface* surface() const { return surface_.get(); }
191
192  protected:
193   ~GLSurfaceAdapter() override;
194
195  private:
196   scoped_refptr<GLSurface> surface_;
197
198   DISALLOW_COPY_AND_ASSIGN(GLSurfaceAdapter);
199 };
200
201 }  // namespace gfx
202
203 #endif  // UI_GL_GL_SURFACE_H_