NaCl: Update revision in DEPS, e8fbd4b -> 9e3dac8
[chromium-blink-merge.git] / ui / gl / gl_context.h
blob5eac8383f38d40c5afc67a560ea3566208735327
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 // Destroys the GL context.
38 virtual void Destroy() = 0;
40 // Makes the GL context and a surface current on the current thread.
41 virtual bool MakeCurrent(GLSurface* surface) = 0;
43 // Releases this GL context and surface as current on the current thread.
44 virtual void ReleaseCurrent(GLSurface* surface) = 0;
46 // Returns true if this context and surface is current. Pass a null surface
47 // if the current surface is not important.
48 virtual bool IsCurrent(GLSurface* surface) = 0;
50 // Get the underlying platform specific GL context "handle".
51 virtual void* GetHandle() = 0;
53 // Gets the GLStateRestorer for the context.
54 GLStateRestorer* GetGLStateRestorer();
56 // Sets the GLStateRestorer for the context (takes ownership).
57 void SetGLStateRestorer(GLStateRestorer* state_restorer);
59 // Set swap interval. This context must be current.
60 void SetSwapInterval(int interval);
62 // Forces the swap interval to zero (no vsync) regardless of any future values
63 // passed to SetSwapInterval.
64 void ForceSwapIntervalZero(bool force);
66 // Returns space separated list of extensions. The context must be current.
67 virtual std::string GetExtensions();
69 // Returns in bytes the total amount of GPU memory for the GPU which this
70 // context is currently rendering on. Returns false if no extension exists
71 // to get the exact amount of GPU memory.
72 virtual bool GetTotalGpuMemory(size_t* bytes);
74 // Indicate that it is safe to force this context to switch GPUs, since
75 // transitioning can cause corruption and hangs (OS X only).
76 virtual void SetSafeToForceGpuSwitch();
78 // Attempt to force the context to move to the GPU of its sharegroup. Return
79 // false only in the event of an unexpected error on the context.
80 virtual bool ForceGpuSwitchIfNeeded();
82 // Indicate that the real context switches should unbind the FBO first
83 // (For an Android work-around only).
84 virtual void SetUnbindFboOnMakeCurrent();
86 // Returns whether the current context supports the named extension. The
87 // context must be current.
88 bool HasExtension(const char* name);
90 // Returns version info of the underlying GL context. The context must be
91 // current.
92 const GLVersionInfo* GetVersionInfo();
94 GLShareGroup* share_group();
96 // Create a GL context that is compatible with the given surface.
97 // |share_group|, if non-NULL, is a group of contexts which the
98 // internally created OpenGL context shares textures and other resources.
99 static scoped_refptr<GLContext> CreateGLContext(
100 GLShareGroup* share_group,
101 GLSurface* compatible_surface,
102 GpuPreference gpu_preference);
104 static bool LosesAllContextsOnContextLost();
106 // Returns the last GLContext made current, virtual or real.
107 static GLContext* GetCurrent();
109 virtual bool WasAllocatedUsingRobustnessExtension();
111 // Use this context for virtualization.
112 void SetupForVirtualization();
114 // Make this context current when used for context virtualization.
115 bool MakeVirtuallyCurrent(GLContext* virtual_context, GLSurface* surface);
117 // Notify this context that |virtual_context|, that was using us, is
118 // being released or destroyed.
119 void OnReleaseVirtuallyCurrent(GLContext* virtual_context);
121 // Returns the GL version string. The context must be current.
122 virtual std::string GetGLVersion();
124 // Returns the GL renderer string. The context must be current.
125 virtual std::string GetGLRenderer();
127 protected:
128 virtual ~GLContext();
130 // Will release the current context when going out of scope, unless canceled.
131 class ScopedReleaseCurrent {
132 public:
133 ScopedReleaseCurrent();
134 ~ScopedReleaseCurrent();
136 void Cancel();
138 private:
139 bool canceled_;
142 // Sets the GL api to the real hardware API (vs the VirtualAPI)
143 static void SetRealGLApi();
144 virtual void SetCurrent(GLSurface* surface);
146 // Initialize function pointers to functions where the bound version depends
147 // on GL version or supported extensions. Should be called immediately after
148 // this context is made current.
149 bool InitializeDynamicBindings();
151 // Returns the last real (non-virtual) GLContext made current.
152 static GLContext* GetRealCurrent();
154 virtual void OnSetSwapInterval(int interval) = 0;
156 private:
157 friend class base::RefCounted<GLContext>;
159 // For GetRealCurrent.
160 friend class VirtualGLApi;
162 scoped_refptr<GLShareGroup> share_group_;
163 scoped_ptr<VirtualGLApi> virtual_gl_api_;
164 scoped_ptr<GLStateRestorer> state_restorer_;
165 scoped_ptr<GLVersionInfo> version_info_;
167 int swap_interval_;
168 bool force_swap_interval_zero_;
170 DISALLOW_COPY_AND_ASSIGN(GLContext);
173 class GL_EXPORT GLContextReal : public GLContext {
174 public:
175 explicit GLContextReal(GLShareGroup* share_group);
177 protected:
178 ~GLContextReal() override;
180 void SetCurrent(GLSurface* surface) override;
182 private:
183 DISALLOW_COPY_AND_ASSIGN(GLContextReal);
186 } // namespace gfx
188 #endif // UI_GL_GL_CONTEXT_H_