f40b1bba78647ab9ef7090ffa420e4c5ba1b739c
[platform/framework/web/crosswalk.git] / src / ui / gl / gl_context.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_CONTEXT_H_
6 #define UI_GL_GL_CONTEXT_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "ui/gl/gl_share_group.h"
14 #include "ui/gl/gl_state_restorer.h"
15 #include "ui/gl/gpu_preference.h"
16
17 namespace gfx {
18
19 class GLSurface;
20 class VirtualGLApi;
21 struct GLVersionInfo;
22
23 // Encapsulates an OpenGL context, hiding platform specific management.
24 class GL_EXPORT GLContext : public base::RefCounted<GLContext> {
25  public:
26   explicit GLContext(GLShareGroup* share_group);
27
28   // Initializes the GL context to be compatible with the given surface. The GL
29   // context can be made with other surface's of the same type. The compatible
30   // surface is only needed for certain platforms like WGL, OSMesa and GLX. It
31   // should be specific for all platforms though.
32   virtual bool Initialize(
33       GLSurface* compatible_surface, GpuPreference gpu_preference) = 0;
34
35   // Destroys the GL context.
36   virtual void Destroy() = 0;
37
38   // Makes the GL context and a surface current on the current thread.
39   virtual bool MakeCurrent(GLSurface* surface) = 0;
40
41   // Releases this GL context and surface as current on the current thread.
42   virtual void ReleaseCurrent(GLSurface* surface) = 0;
43
44   // Returns true if this context and surface is current. Pass a null surface
45   // if the current surface is not important.
46   virtual bool IsCurrent(GLSurface* surface) = 0;
47
48   // Get the underlying platform specific GL context "handle".
49   virtual void* GetHandle() = 0;
50
51   // Gets the GLStateRestorer for the context.
52   GLStateRestorer* GetGLStateRestorer();
53
54   // Sets the GLStateRestorer for the context (takes ownership).
55   void SetGLStateRestorer(GLStateRestorer* state_restorer);
56
57   // Set swap interval. This context must be current.
58   virtual void SetSwapInterval(int interval) = 0;
59
60   // Returns space separated list of extensions. The context must be current.
61   virtual std::string GetExtensions();
62
63   // Returns in bytes the total amount of GPU memory for the GPU which this
64   // context is currently rendering on. Returns false if no extension exists
65   // to get the exact amount of GPU memory.
66   virtual bool GetTotalGpuMemory(size_t* bytes);
67
68   // Indicate that it is safe to force this context to switch GPUs, since
69   // transitioning can cause corruption and hangs (OS X only).
70   virtual void SetSafeToForceGpuSwitch();
71
72   // Indicate that the real context switches should unbind the FBO first
73   // (For an Android work-around only).
74   virtual void SetUnbindFboOnMakeCurrent();
75
76   // Returns whether the current context supports the named extension. The
77   // context must be current.
78   bool HasExtension(const char* name);
79
80   // Returns version info of the underlying GL context. The context must be
81   // current.
82   const GLVersionInfo* GetVersionInfo();
83
84   GLShareGroup* share_group();
85
86   // Create a GL context that is compatible with the given surface.
87   // |share_group|, if non-NULL, is a group of contexts which the
88   // internally created OpenGL context shares textures and other resources.
89   static scoped_refptr<GLContext> CreateGLContext(
90       GLShareGroup* share_group,
91       GLSurface* compatible_surface,
92       GpuPreference gpu_preference);
93
94   static bool LosesAllContextsOnContextLost();
95
96   // Returns the last GLContext made current, virtual or real.
97   static GLContext* GetCurrent();
98
99   virtual bool WasAllocatedUsingRobustnessExtension();
100
101   // Use this context for virtualization.
102   void SetupForVirtualization();
103
104   // Make this context current when used for context virtualization.
105   bool MakeVirtuallyCurrent(GLContext* virtual_context, GLSurface* surface);
106
107   // Notify this context that |virtual_context|, that was using us, is
108   // being released or destroyed.
109   void OnReleaseVirtuallyCurrent(GLContext* virtual_context);
110
111   // Returns the GL version string. The context must be current.
112   virtual std::string GetGLVersion();
113
114   // Returns the GL renderer string. The context must be current.
115   virtual std::string GetGLRenderer();
116
117  protected:
118   virtual ~GLContext();
119
120   // Sets the GL api to the real hardware API (vs the VirtualAPI)
121   static void SetRealGLApi();
122   virtual void SetCurrent(GLSurface* surface);
123
124   // Initialize function pointers to functions where the bound version depends
125   // on GL version or supported extensions. Should be called immediately after
126   // this context is made current.
127   bool InitializeDynamicBindings();
128
129   // Returns the last real (non-virtual) GLContext made current.
130   static GLContext* GetRealCurrent();
131
132  private:
133   friend class base::RefCounted<GLContext>;
134
135   // For GetRealCurrent.
136   friend class VirtualGLApi;
137
138   scoped_refptr<GLShareGroup> share_group_;
139   scoped_ptr<VirtualGLApi> virtual_gl_api_;
140   scoped_ptr<GLStateRestorer> state_restorer_;
141   scoped_ptr<GLVersionInfo> version_info_;
142
143   DISALLOW_COPY_AND_ASSIGN(GLContext);
144 };
145
146 class GL_EXPORT GLContextReal : public GLContext {
147  public:
148   explicit GLContextReal(GLShareGroup* share_group);
149
150  protected:
151   virtual ~GLContextReal();
152
153   virtual void SetCurrent(GLSurface* surface) OVERRIDE;
154
155  private:
156   DISALLOW_COPY_AND_ASSIGN(GLContextReal);
157 };
158
159 }  // namespace gfx
160
161 #endif  // UI_GL_GL_CONTEXT_H_