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_
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"
23 // Encapsulates an OpenGL context, hiding platform specific management.
24 class GL_EXPORT GLContext : public base::RefCounted<GLContext> {
26 explicit GLContext(GLShareGroup* share_group);
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;
35 // Destroys the GL context.
36 virtual void Destroy() = 0;
38 // Makes the GL context and a surface current on the current thread.
39 virtual bool MakeCurrent(GLSurface* surface) = 0;
41 // Releases this GL context and surface as current on the current thread.
42 virtual void ReleaseCurrent(GLSurface* surface) = 0;
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;
48 // Get the underlying platform specific GL context "handle".
49 virtual void* GetHandle() = 0;
51 // Gets the GLStateRestorer for the context.
52 GLStateRestorer* GetGLStateRestorer();
54 // Sets the GLStateRestorer for the context (takes ownership).
55 void SetGLStateRestorer(GLStateRestorer* state_restorer);
57 // Set swap interval. This context must be current.
58 virtual void SetSwapInterval(int interval) = 0;
60 // Returns space separated list of extensions. The context must be current.
61 virtual std::string GetExtensions();
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);
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();
72 // Indicate that the real context switches should unbind the FBO first
73 // (For an Android work-around only).
74 virtual void SetUnbindFboOnMakeCurrent();
76 // Returns whether the current context supports the named extension. The
77 // context must be current.
78 bool HasExtension(const char* name);
80 // Returns version info of the underlying GL context. The context must be
82 const GLVersionInfo* GetVersionInfo();
84 GLShareGroup* share_group();
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);
94 static bool LosesAllContextsOnContextLost();
96 // Returns the last GLContext made current, virtual or real.
97 static GLContext* GetCurrent();
99 virtual bool WasAllocatedUsingRobustnessExtension();
101 // Use this context for virtualization.
102 void SetupForVirtualization();
104 // Make this context current when used for context virtualization.
105 bool MakeVirtuallyCurrent(GLContext* virtual_context, GLSurface* surface);
107 // Notify this context that |virtual_context|, that was using us, is
108 // being released or destroyed.
109 void OnReleaseVirtuallyCurrent(GLContext* virtual_context);
111 // Returns the GL version string. The context must be current.
112 virtual std::string GetGLVersion();
114 // Returns the GL renderer string. The context must be current.
115 virtual std::string GetGLRenderer();
118 virtual ~GLContext();
120 // Sets the GL api to the real hardware API (vs the VirtualAPI)
121 static void SetRealGLApi();
122 virtual void SetCurrent(GLSurface* surface);
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();
129 // Returns the last real (non-virtual) GLContext made current.
130 static GLContext* GetRealCurrent();
133 friend class base::RefCounted<GLContext>;
135 // For GetRealCurrent.
136 friend class VirtualGLApi;
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_;
143 DISALLOW_COPY_AND_ASSIGN(GLContext);
146 class GL_EXPORT GLContextReal : public GLContext {
148 explicit GLContextReal(GLShareGroup* share_group);
151 virtual ~GLContextReal();
153 virtual void SetCurrent(GLSurface* surface) OVERRIDE;
156 DISALLOW_COPY_AND_ASSIGN(GLContextReal);
161 #endif // UI_GL_GL_CONTEXT_H_