Disable PPAPINaClPNaClTest.MediaStreamAudioTrack
[chromium-blink-merge.git] / ui / gl / gl_context.h
blobcdebd686b33eea7f994c4de880d57490cd14298d
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.
5 #ifndef UI_GL_GL_CONTEXT_H_
6 #define UI_GL_GL_CONTEXT_H_
8 #include <string>
9 #include <vector>
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"
19 namespace gfx {
21 class GLSurface;
22 class VirtualGLApi;
23 struct GLVersionInfo;
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);
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;
37 class FlushEvent : public base::RefCountedThreadSafe<FlushEvent> {
38 public:
39 bool IsSignaled();
41 private:
42 friend class base::RefCountedThreadSafe<FlushEvent>;
43 friend class GLContext;
44 FlushEvent();
45 virtual ~FlushEvent();
46 void Signal();
48 base::CancellationFlag flag_;
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();
56 // Destroys the GL context.
57 virtual void Destroy() = 0;
59 // Makes the GL context and a surface current on the current thread.
60 virtual bool MakeCurrent(GLSurface* surface) = 0;
62 // Releases this GL context and surface as current on the current thread.
63 virtual void ReleaseCurrent(GLSurface* surface) = 0;
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;
69 // Get the underlying platform specific GL context "handle".
70 virtual void* GetHandle() = 0;
72 // Gets the GLStateRestorer for the context.
73 GLStateRestorer* GetGLStateRestorer();
75 // Sets the GLStateRestorer for the context (takes ownership).
76 void SetGLStateRestorer(GLStateRestorer* state_restorer);
78 // Set swap interval. This context must be current.
79 virtual void SetSwapInterval(int interval) = 0;
81 // Returns space separated list of extensions. The context must be current.
82 virtual std::string GetExtensions();
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);
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();
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();
97 // Indicate that the real context switches should unbind the FBO first
98 // (For an Android work-around only).
99 virtual void SetUnbindFboOnMakeCurrent();
101 // Returns whether the current context supports the named extension. The
102 // context must be current.
103 bool HasExtension(const char* name);
105 // Returns version info of the underlying GL context. The context must be
106 // current.
107 const GLVersionInfo* GetVersionInfo();
109 GLShareGroup* share_group();
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);
119 static bool LosesAllContextsOnContextLost();
121 // Returns the last GLContext made current, virtual or real.
122 static GLContext* GetCurrent();
124 virtual bool WasAllocatedUsingRobustnessExtension();
126 // Use this context for virtualization.
127 void SetupForVirtualization();
129 // Make this context current when used for context virtualization.
130 bool MakeVirtuallyCurrent(GLContext* virtual_context, GLSurface* surface);
132 // Notify this context that |virtual_context|, that was using us, is
133 // being released or destroyed.
134 void OnReleaseVirtuallyCurrent(GLContext* virtual_context);
136 // Returns the GL version string. The context must be current.
137 virtual std::string GetGLVersion();
139 // Returns the GL renderer string. The context must be current.
140 virtual std::string GetGLRenderer();
142 // Called when glFlush()/glFinish() is called with this context current.
143 void OnFlush();
145 protected:
146 virtual ~GLContext();
148 // Will release the current context when going out of scope, unless canceled.
149 class ScopedReleaseCurrent {
150 public:
151 ScopedReleaseCurrent();
152 ~ScopedReleaseCurrent();
154 void Cancel();
156 private:
157 bool canceled_;
160 // Sets the GL api to the real hardware API (vs the VirtualAPI)
161 static void SetRealGLApi();
162 virtual void SetCurrent(GLSurface* surface);
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();
169 // Returns the last real (non-virtual) GLContext made current.
170 static GLContext* GetRealCurrent();
172 private:
173 friend class base::RefCounted<GLContext>;
175 // For GetRealCurrent.
176 friend class VirtualGLApi;
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_;
183 std::vector<scoped_refptr<FlushEvent> > flush_events_;
185 DISALLOW_COPY_AND_ASSIGN(GLContext);
188 class GL_EXPORT GLContextReal : public GLContext {
189 public:
190 explicit GLContextReal(GLShareGroup* share_group);
192 protected:
193 ~GLContextReal() override;
195 void SetCurrent(GLSurface* surface) override;
197 private:
198 DISALLOW_COPY_AND_ASSIGN(GLContextReal);
201 } // namespace gfx
203 #endif // UI_GL_GL_CONTEXT_H_