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/gpu_preference.h"
20 class GLStateRestorer
;
22 // Encapsulates an OpenGL context, hiding platform specific management.
23 class GL_EXPORT GLContext
: public base::RefCounted
<GLContext
> {
25 explicit GLContext(GLShareGroup
* share_group
);
27 // Initializes the GL context to be compatible with the given surface. The GL
28 // context can be made with other surface's of the same type. The compatible
29 // surface is only needed for certain platforms like WGL, OSMesa and GLX. It
30 // should be specific for all platforms though.
31 virtual bool Initialize(
32 GLSurface
* compatible_surface
, GpuPreference gpu_preference
) = 0;
34 // Destroys the GL context.
35 virtual void Destroy() = 0;
37 // Makes the GL context and a surface current on the current thread.
38 virtual bool MakeCurrent(GLSurface
* surface
) = 0;
40 // Releases this GL context and surface as current on the current thread.
41 virtual void ReleaseCurrent(GLSurface
* surface
) = 0;
43 // Returns true if this context and surface is current. Pass a null surface
44 // if the current surface is not important.
45 virtual bool IsCurrent(GLSurface
* surface
) = 0;
47 // Get the underlying platform specific GL context "handle".
48 virtual void* GetHandle() = 0;
50 // Gets the GLStateRestore for the context.
51 virtual GLStateRestorer
* GetGLStateRestorer();
53 // Set swap interval. This context must be current.
54 virtual void SetSwapInterval(int interval
) = 0;
56 // Returns space separated list of extensions. The context must be current.
57 virtual std::string
GetExtensions();
59 // Returns in bytes the total amount of GPU memory for the GPU which this
60 // context is currently rendering on. Returns false if no extension exists
61 // to get the exact amount of GPU memory.
62 virtual bool GetTotalGpuMemory(size_t* bytes
);
64 // Indicate that it is safe to force this context to switch GPUs, since
65 // transitioning can cause corruption and hangs (OS X only).
66 virtual void SetSafeToForceGpuSwitch();
68 // Returns whether the current context supports the named extension. The
69 // context must be current.
70 bool HasExtension(const char* name
);
72 GLShareGroup
* share_group();
74 // Create a GL context that is compatible with the given surface.
75 // |share_group|, if non-NULL, is a group of contexts which the
76 // internally created OpenGL context shares textures and other resources.
77 static scoped_refptr
<GLContext
> CreateGLContext(
78 GLShareGroup
* share_group
,
79 GLSurface
* compatible_surface
,
80 GpuPreference gpu_preference
);
82 static bool LosesAllContextsOnContextLost();
84 static GLContext
* GetCurrent();
86 virtual bool WasAllocatedUsingRobustnessExtension();
88 // Use this context for virtualization.
89 void SetupForVirtualization();
91 // Make this context current when used for context virtualization.
92 bool MakeVirtuallyCurrent(GLContext
* virtual_context
, GLSurface
* surface
);
94 // Notify this context that |virtual_context|, that was using us, is
96 void OnDestroyVirtualContext(GLContext
* virtual_context
);
101 // Sets the GL api to the real hardware API (vs the VirtualAPI)
102 static void SetRealGLApi();
103 static void SetCurrent(GLContext
* context
, GLSurface
* surface
);
105 // Initialize function pointers to extension functions in the GL
106 // implementation. Should be called immediately after this context is made
108 bool InitializeExtensionBindings();
111 friend class base::RefCounted
<GLContext
>;
113 scoped_refptr
<GLShareGroup
> share_group_
;
114 scoped_ptr
<VirtualGLApi
> virtual_gl_api_
;
116 DISALLOW_COPY_AND_ASSIGN(GLContext
);
121 #endif // UI_GL_GL_CONTEXT_H_