Update To 11.40.268.0
[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 #include <vector>
10
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/synchronization/cancellation_flag.h"
15 #include "ui/gl/gl_share_group.h"
16 #include "ui/gl/gl_state_restorer.h"
17 #include "ui/gl/gpu_preference.h"
18
19 namespace gfx {
20
21 class GLSurface;
22 class VirtualGLApi;
23 struct GLVersionInfo;
24
25 // Encapsulates an OpenGL context, hiding platform specific management.
26 class GL_EXPORT GLContext : public base::RefCounted<GLContext> {
27  public:
28   explicit GLContext(GLShareGroup* share_group);
29
30   // Initializes the GL context to be compatible with the given surface. The GL
31   // context can be made with other surface's of the same type. The compatible
32   // surface is only needed for certain platforms like WGL, OSMesa and GLX. It
33   // should be specific for all platforms though.
34   virtual bool Initialize(
35       GLSurface* compatible_surface, GpuPreference gpu_preference) = 0;
36
37   class FlushEvent : public base::RefCountedThreadSafe<FlushEvent> {
38     public:
39       bool IsSignaled();
40
41     private:
42       friend class base::RefCountedThreadSafe<FlushEvent>;
43       friend class GLContext;
44       FlushEvent();
45       virtual ~FlushEvent();
46       void Signal();
47
48       base::CancellationFlag flag_;
49   };
50
51   // Needs to be called with this context current. It will return a FlushEvent
52   // that is initially unsignaled, but will transition to signaled after the
53   // next glFlush() or glFinish() occurs in this context.
54   scoped_refptr<FlushEvent> SignalFlush();
55
56   // Destroys the GL context.
57   virtual void Destroy() = 0;
58
59   // Makes the GL context and a surface current on the current thread.
60   virtual bool MakeCurrent(GLSurface* surface) = 0;
61
62   // Releases this GL context and surface as current on the current thread.
63   virtual void ReleaseCurrent(GLSurface* surface) = 0;
64
65   // Returns true if this context and surface is current. Pass a null surface
66   // if the current surface is not important.
67   virtual bool IsCurrent(GLSurface* surface) = 0;
68
69   // Get the underlying platform specific GL context "handle".
70   virtual void* GetHandle() = 0;
71
72   // Gets the GLStateRestorer for the context.
73   GLStateRestorer* GetGLStateRestorer();
74
75   // Sets the GLStateRestorer for the context (takes ownership).
76   void SetGLStateRestorer(GLStateRestorer* state_restorer);
77
78   // Set swap interval. This context must be current.
79   virtual void SetSwapInterval(int interval) = 0;
80
81   // Returns space separated list of extensions. The context must be current.
82   virtual std::string GetExtensions();
83
84   // Returns in bytes the total amount of GPU memory for the GPU which this
85   // context is currently rendering on. Returns false if no extension exists
86   // to get the exact amount of GPU memory.
87   virtual bool GetTotalGpuMemory(size_t* bytes);
88
89   // Indicate that it is safe to force this context to switch GPUs, since
90   // transitioning can cause corruption and hangs (OS X only).
91   virtual void SetSafeToForceGpuSwitch();
92
93   // Attempt to force the context to move to the GPU of its sharegroup. Return
94   // false only in the event of an unexpected error on the context.
95   virtual bool ForceGpuSwitchIfNeeded();
96
97   // Indicate that the real context switches should unbind the FBO first
98   // (For an Android work-around only).
99   virtual void SetUnbindFboOnMakeCurrent();
100
101   // Returns whether the current context supports the named extension. The
102   // context must be current.
103   bool HasExtension(const char* name);
104
105   // Returns version info of the underlying GL context. The context must be
106   // current.
107   const GLVersionInfo* GetVersionInfo();
108
109   GLShareGroup* share_group();
110
111   // Create a GL context that is compatible with the given surface.
112   // |share_group|, if non-NULL, is a group of contexts which the
113   // internally created OpenGL context shares textures and other resources.
114   static scoped_refptr<GLContext> CreateGLContext(
115       GLShareGroup* share_group,
116       GLSurface* compatible_surface,
117       GpuPreference gpu_preference);
118
119   static bool LosesAllContextsOnContextLost();
120
121   // Returns the last GLContext made current, virtual or real.
122   static GLContext* GetCurrent();
123
124   virtual bool WasAllocatedUsingRobustnessExtension();
125
126   // Use this context for virtualization.
127   void SetupForVirtualization();
128
129   // Make this context current when used for context virtualization.
130   bool MakeVirtuallyCurrent(GLContext* virtual_context, GLSurface* surface);
131
132   // Notify this context that |virtual_context|, that was using us, is
133   // being released or destroyed.
134   void OnReleaseVirtuallyCurrent(GLContext* virtual_context);
135
136   // Returns the GL version string. The context must be current.
137   virtual std::string GetGLVersion();
138
139   // Returns the GL renderer string. The context must be current.
140   virtual std::string GetGLRenderer();
141
142   // Called when glFlush()/glFinish() is called with this context current.
143   void OnFlush();
144
145  protected:
146   virtual ~GLContext();
147
148   // Will release the current context when going out of scope, unless canceled.
149   class ScopedReleaseCurrent {
150    public:
151     ScopedReleaseCurrent();
152     ~ScopedReleaseCurrent();
153
154     void Cancel();
155
156    private:
157     bool canceled_;
158   };
159
160   // Sets the GL api to the real hardware API (vs the VirtualAPI)
161   static void SetRealGLApi();
162   virtual void SetCurrent(GLSurface* surface);
163
164   // Initialize function pointers to functions where the bound version depends
165   // on GL version or supported extensions. Should be called immediately after
166   // this context is made current.
167   bool InitializeDynamicBindings();
168
169   // Returns the last real (non-virtual) GLContext made current.
170   static GLContext* GetRealCurrent();
171
172  private:
173   friend class base::RefCounted<GLContext>;
174
175   // For GetRealCurrent.
176   friend class VirtualGLApi;
177
178   scoped_refptr<GLShareGroup> share_group_;
179   scoped_ptr<VirtualGLApi> virtual_gl_api_;
180   scoped_ptr<GLStateRestorer> state_restorer_;
181   scoped_ptr<GLVersionInfo> version_info_;
182
183   std::vector<scoped_refptr<FlushEvent> > flush_events_;
184
185   DISALLOW_COPY_AND_ASSIGN(GLContext);
186 };
187
188 class GL_EXPORT GLContextReal : public GLContext {
189  public:
190   explicit GLContextReal(GLShareGroup* share_group);
191
192  protected:
193   ~GLContextReal() override;
194
195   void SetCurrent(GLSurface* surface) override;
196
197  private:
198   DISALLOW_COPY_AND_ASSIGN(GLContextReal);
199 };
200
201 }  // namespace gfx
202
203 #endif  // UI_GL_GL_CONTEXT_H_