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_
11 #include "base/basictypes.h"
12 #include "base/cancelable_callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/synchronization/cancellation_flag.h"
16 #include "ui/gl/gl_share_group.h"
17 #include "ui/gl/gl_state_restorer.h"
18 #include "ui/gl/gpu_preference.h"
21 class GLContextVirtual
;
28 class GPUTimingClient
;
33 // Encapsulates an OpenGL context, hiding platform specific management.
34 class GL_EXPORT GLContext
: public base::RefCounted
<GLContext
> {
36 explicit GLContext(GLShareGroup
* share_group
);
38 // Initializes the GL context to be compatible with the given surface. The GL
39 // context can be made with other surface's of the same type. The compatible
40 // surface is only needed for certain platforms like WGL, OSMesa and GLX. It
41 // should be specific for all platforms though.
42 virtual bool Initialize(
43 GLSurface
* compatible_surface
, GpuPreference gpu_preference
) = 0;
45 // Makes the GL context and a surface current on the current thread.
46 virtual bool MakeCurrent(GLSurface
* surface
) = 0;
48 // Releases this GL context and surface as current on the current thread.
49 virtual void ReleaseCurrent(GLSurface
* surface
) = 0;
51 // Returns true if this context and surface is current. Pass a null surface
52 // if the current surface is not important.
53 virtual bool IsCurrent(GLSurface
* surface
) = 0;
55 // Get the underlying platform specific GL context "handle".
56 virtual void* GetHandle() = 0;
58 // Creates a GPUTimingClient class which abstracts various GPU Timing exts.
59 virtual scoped_refptr
<gfx::GPUTimingClient
> CreateGPUTimingClient() = 0;
61 // Gets the GLStateRestorer for the context.
62 GLStateRestorer
* GetGLStateRestorer();
64 // Sets the GLStateRestorer for the context (takes ownership).
65 void SetGLStateRestorer(GLStateRestorer
* state_restorer
);
67 // Set swap interval. This context must be current.
68 void SetSwapInterval(int interval
);
70 // Forces the swap interval to zero (no vsync) regardless of any future values
71 // passed to SetSwapInterval.
72 void ForceSwapIntervalZero(bool force
);
74 // Returns space separated list of extensions. The context must be current.
75 virtual std::string
GetExtensions();
77 // Returns in bytes the total amount of GPU memory for the GPU which this
78 // context is currently rendering on. Returns false if no extension exists
79 // to get the exact amount of GPU memory.
80 virtual bool GetTotalGpuMemory(size_t* bytes
);
82 // Indicate that it is safe to force this context to switch GPUs, since
83 // transitioning can cause corruption and hangs (OS X only).
84 virtual void SetSafeToForceGpuSwitch();
86 // Attempt to force the context to move to the GPU of its sharegroup. Return
87 // false only in the event of an unexpected error on the context.
88 virtual bool ForceGpuSwitchIfNeeded();
90 // Indicate that the real context switches should unbind the FBO first
91 // (For an Android work-around only).
92 virtual void SetUnbindFboOnMakeCurrent();
94 // Returns whether the current context supports the named extension. The
95 // context must be current.
96 bool HasExtension(const char* name
);
98 // Returns version info of the underlying GL context. The context must be
100 const GLVersionInfo
* GetVersionInfo();
102 GLShareGroup
* share_group();
104 // Create a GL context that is compatible with the given surface.
105 // |share_group|, if non-NULL, is a group of contexts which the
106 // internally created OpenGL context shares textures and other resources.
107 static scoped_refptr
<GLContext
> CreateGLContext(
108 GLShareGroup
* share_group
,
109 GLSurface
* compatible_surface
,
110 GpuPreference gpu_preference
);
112 static bool LosesAllContextsOnContextLost();
114 // Returns the last GLContext made current, virtual or real.
115 static GLContext
* GetCurrent();
117 virtual bool WasAllocatedUsingRobustnessExtension();
119 // Use this context for virtualization.
120 void SetupForVirtualization();
122 // Make this context current when used for context virtualization.
123 bool MakeVirtuallyCurrent(GLContext
* virtual_context
, GLSurface
* surface
);
125 // Notify this context that |virtual_context|, that was using us, is
126 // being released or destroyed.
127 void OnReleaseVirtuallyCurrent(GLContext
* virtual_context
);
129 // Returns the GL version string. The context must be current.
130 virtual std::string
GetGLVersion();
132 // Returns the GL renderer string. The context must be current.
133 virtual std::string
GetGLRenderer();
135 // Return a callback that, when called, indicates that the state the
136 // underlying context has been changed by code outside of the command buffer,
137 // and will need to be restored.
138 virtual base::Closure
GetStateWasDirtiedExternallyCallback();
140 // Restore the context's state if it was dirtied by an external caller.
141 virtual void RestoreStateIfDirtiedExternally();
144 virtual ~GLContext();
146 // Will release the current context when going out of scope, unless canceled.
147 class ScopedReleaseCurrent
{
149 ScopedReleaseCurrent();
150 ~ScopedReleaseCurrent();
158 // Sets the GL api to the real hardware API (vs the VirtualAPI)
159 static void SetRealGLApi();
160 virtual void SetCurrent(GLSurface
* surface
);
162 // Initialize function pointers to functions where the bound version depends
163 // on GL version or supported extensions. Should be called immediately after
164 // this context is made current.
165 bool InitializeDynamicBindings();
167 // Returns the last real (non-virtual) GLContext made current.
168 static GLContext
* GetRealCurrent();
170 virtual void OnSetSwapInterval(int interval
) = 0;
172 bool GetStateWasDirtiedExternally() const;
173 void SetStateWasDirtiedExternally(bool dirtied_externally
);
176 friend class base::RefCounted
<GLContext
>;
178 // For GetRealCurrent.
179 friend class VirtualGLApi
;
180 friend class gpu::GLContextVirtual
;
182 scoped_refptr
<GLShareGroup
> share_group_
;
183 scoped_ptr
<VirtualGLApi
> virtual_gl_api_
;
184 bool state_dirtied_externally_
;
185 scoped_ptr
<GLStateRestorer
> state_restorer_
;
186 scoped_ptr
<GLVersionInfo
> version_info_
;
189 bool force_swap_interval_zero_
;
191 base::CancelableCallback
<void()> state_dirtied_callback_
;
193 DISALLOW_COPY_AND_ASSIGN(GLContext
);
196 class GL_EXPORT GLContextReal
: public GLContext
{
198 explicit GLContextReal(GLShareGroup
* share_group
);
199 scoped_refptr
<gfx::GPUTimingClient
> CreateGPUTimingClient() override
;
202 ~GLContextReal() override
;
204 void SetCurrent(GLSurface
* surface
) override
;
207 scoped_ptr
<gfx::GPUTiming
> gpu_timing_
;
208 DISALLOW_COPY_AND_ASSIGN(GLContextReal
);
213 #endif // UI_GL_GL_CONTEXT_H_