Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gl / gl_context.h
blob95986613e0b7e3f1392f11dc70be8b6834f59538
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/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"
20 namespace gpu {
21 class GLContextVirtual;
22 } // namespace gpu
24 namespace gfx {
26 class GLSurface;
27 class GPUTiming;
28 class GPUTimingClient;
29 class VirtualGLApi;
30 struct GLVersionInfo;
33 // Encapsulates an OpenGL context, hiding platform specific management.
34 class GL_EXPORT GLContext : public base::RefCounted<GLContext> {
35 public:
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 // Destroys the GL context.
46 virtual void Destroy() = 0;
48 // Makes the GL context and a surface current on the current thread.
49 virtual bool MakeCurrent(GLSurface* surface) = 0;
51 // Releases this GL context and surface as current on the current thread.
52 virtual void ReleaseCurrent(GLSurface* surface) = 0;
54 // Returns true if this context and surface is current. Pass a null surface
55 // if the current surface is not important.
56 virtual bool IsCurrent(GLSurface* surface) = 0;
58 // Get the underlying platform specific GL context "handle".
59 virtual void* GetHandle() = 0;
61 // Creates a GPUTimingClient class which abstracts various GPU Timing exts.
62 virtual scoped_refptr<gfx::GPUTimingClient> CreateGPUTimingClient() = 0;
64 // Gets the GLStateRestorer for the context.
65 GLStateRestorer* GetGLStateRestorer();
67 // Sets the GLStateRestorer for the context (takes ownership).
68 void SetGLStateRestorer(GLStateRestorer* state_restorer);
70 // Set swap interval. This context must be current.
71 void SetSwapInterval(int interval);
73 // Forces the swap interval to zero (no vsync) regardless of any future values
74 // passed to SetSwapInterval.
75 void ForceSwapIntervalZero(bool force);
77 // Returns space separated list of extensions. The context must be current.
78 virtual std::string GetExtensions();
80 // Returns in bytes the total amount of GPU memory for the GPU which this
81 // context is currently rendering on. Returns false if no extension exists
82 // to get the exact amount of GPU memory.
83 virtual bool GetTotalGpuMemory(size_t* bytes);
85 // Indicate that it is safe to force this context to switch GPUs, since
86 // transitioning can cause corruption and hangs (OS X only).
87 virtual void SetSafeToForceGpuSwitch();
89 // Attempt to force the context to move to the GPU of its sharegroup. Return
90 // false only in the event of an unexpected error on the context.
91 virtual bool ForceGpuSwitchIfNeeded();
93 // Indicate that the real context switches should unbind the FBO first
94 // (For an Android work-around only).
95 virtual void SetUnbindFboOnMakeCurrent();
97 // Returns whether the current context supports the named extension. The
98 // context must be current.
99 bool HasExtension(const char* name);
101 // Returns version info of the underlying GL context. The context must be
102 // current.
103 const GLVersionInfo* GetVersionInfo();
105 GLShareGroup* share_group();
107 // Create a GL context that is compatible with the given surface.
108 // |share_group|, if non-NULL, is a group of contexts which the
109 // internally created OpenGL context shares textures and other resources.
110 static scoped_refptr<GLContext> CreateGLContext(
111 GLShareGroup* share_group,
112 GLSurface* compatible_surface,
113 GpuPreference gpu_preference);
115 static bool LosesAllContextsOnContextLost();
117 // Returns the last GLContext made current, virtual or real.
118 static GLContext* GetCurrent();
120 virtual bool WasAllocatedUsingRobustnessExtension();
122 // Use this context for virtualization.
123 void SetupForVirtualization();
125 // Make this context current when used for context virtualization.
126 bool MakeVirtuallyCurrent(GLContext* virtual_context, GLSurface* surface);
128 // Notify this context that |virtual_context|, that was using us, is
129 // being released or destroyed.
130 void OnReleaseVirtuallyCurrent(GLContext* virtual_context);
132 // Returns the GL version string. The context must be current.
133 virtual std::string GetGLVersion();
135 // Returns the GL renderer string. The context must be current.
136 virtual std::string GetGLRenderer();
138 // Return a callback that, when called, indicates that the state the
139 // underlying context has been changed by code outside of the command buffer,
140 // and will need to be restored.
141 virtual base::Closure GetStateWasDirtiedExternallyCallback();
143 // Restore the context's state if it was dirtied by an external caller.
144 virtual void RestoreStateIfDirtiedExternally();
146 protected:
147 virtual ~GLContext();
149 // Will release the current context when going out of scope, unless canceled.
150 class ScopedReleaseCurrent {
151 public:
152 ScopedReleaseCurrent();
153 ~ScopedReleaseCurrent();
155 void Cancel();
157 private:
158 bool canceled_;
161 // Sets the GL api to the real hardware API (vs the VirtualAPI)
162 static void SetRealGLApi();
163 virtual void SetCurrent(GLSurface* surface);
165 // Initialize function pointers to functions where the bound version depends
166 // on GL version or supported extensions. Should be called immediately after
167 // this context is made current.
168 bool InitializeDynamicBindings();
170 // Returns the last real (non-virtual) GLContext made current.
171 static GLContext* GetRealCurrent();
173 virtual void OnSetSwapInterval(int interval) = 0;
175 bool GetStateWasDirtiedExternally() const;
176 void SetStateWasDirtiedExternally(bool dirtied_externally);
178 private:
179 friend class base::RefCounted<GLContext>;
181 // For GetRealCurrent.
182 friend class VirtualGLApi;
183 friend class gpu::GLContextVirtual;
185 scoped_refptr<GLShareGroup> share_group_;
186 scoped_ptr<VirtualGLApi> virtual_gl_api_;
187 bool state_dirtied_externally_;
188 scoped_ptr<GLStateRestorer> state_restorer_;
189 scoped_ptr<GLVersionInfo> version_info_;
191 int swap_interval_;
192 bool force_swap_interval_zero_;
194 base::CancelableCallback<void()> state_dirtied_callback_;
196 DISALLOW_COPY_AND_ASSIGN(GLContext);
199 class GL_EXPORT GLContextReal : public GLContext {
200 public:
201 explicit GLContextReal(GLShareGroup* share_group);
202 scoped_refptr<gfx::GPUTimingClient> CreateGPUTimingClient() override;
204 protected:
205 ~GLContextReal() override;
207 void SetCurrent(GLSurface* surface) override;
209 private:
210 scoped_ptr<gfx::GPUTiming> gpu_timing_;
211 DISALLOW_COPY_AND_ASSIGN(GLContextReal);
214 } // namespace gfx
216 #endif // UI_GL_GL_CONTEXT_H_