Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / ui / gl / gl_context.h
blob323b373347b0625c88382f355a79ae5507ec0c86
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 class FlushEvent : public base::RefCountedThreadSafe<FlushEvent> {
38 public:
39 bool IsSignaled();
41 private:
42 friend class base::RefCountedThreadSafe<FlushEvent>;
43 friend class GLContext;
44 FlushEvent();
45 virtual ~FlushEvent();
46 void Signal();
48 base::CancellationFlag flag_;
51 // Needs to be called with this context current. It will return a FlushEvent
52 // that is initially unsignaled, but will transition to signaled after the
53 // next glFlush() or glFinish() occurs in this context.
54 scoped_refptr<FlushEvent> SignalFlush();
56 // Destroys the GL context.
57 virtual void Destroy() = 0;
59 // Makes the GL context and a surface current on the current thread.
60 virtual bool MakeCurrent(GLSurface* surface) = 0;
62 // Releases this GL context and surface as current on the current thread.
63 virtual void ReleaseCurrent(GLSurface* surface) = 0;
65 // Returns true if this context and surface is current. Pass a null surface
66 // if the current surface is not important.
67 virtual bool IsCurrent(GLSurface* surface) = 0;
69 // Get the underlying platform specific GL context "handle".
70 virtual void* GetHandle() = 0;
72 // Gets the GLStateRestorer for the context.
73 GLStateRestorer* GetGLStateRestorer();
75 // Sets the GLStateRestorer for the context (takes ownership).
76 void SetGLStateRestorer(GLStateRestorer* state_restorer);
78 // Set swap interval. This context must be current.
79 virtual void SetSwapInterval(int interval) = 0;
81 // Returns space separated list of extensions. The context must be current.
82 virtual std::string GetExtensions();
84 // Returns in bytes the total amount of GPU memory for the GPU which this
85 // context is currently rendering on. Returns false if no extension exists
86 // to get the exact amount of GPU memory.
87 virtual bool GetTotalGpuMemory(size_t* bytes);
89 // Indicate that it is safe to force this context to switch GPUs, since
90 // transitioning can cause corruption and hangs (OS X only).
91 virtual void SetSafeToForceGpuSwitch();
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 // Called when glFlush()/glFinish() is called with this context current.
139 void OnFlush();
141 protected:
142 virtual ~GLContext();
144 // Will release the current context when going out of scope, unless canceled.
145 class ScopedReleaseCurrent {
146 public:
147 ScopedReleaseCurrent();
148 ~ScopedReleaseCurrent();
150 void Cancel();
152 private:
153 bool canceled_;
156 // Sets the GL api to the real hardware API (vs the VirtualAPI)
157 static void SetRealGLApi();
158 virtual void SetCurrent(GLSurface* surface);
160 // Initialize function pointers to functions where the bound version depends
161 // on GL version or supported extensions. Should be called immediately after
162 // this context is made current.
163 bool InitializeDynamicBindings();
165 // Returns the last real (non-virtual) GLContext made current.
166 static GLContext* GetRealCurrent();
168 private:
169 friend class base::RefCounted<GLContext>;
171 // For GetRealCurrent.
172 friend class VirtualGLApi;
174 scoped_refptr<GLShareGroup> share_group_;
175 scoped_ptr<VirtualGLApi> virtual_gl_api_;
176 scoped_ptr<GLStateRestorer> state_restorer_;
177 scoped_ptr<GLVersionInfo> version_info_;
179 std::vector<scoped_refptr<FlushEvent> > flush_events_;
181 DISALLOW_COPY_AND_ASSIGN(GLContext);
184 class GL_EXPORT GLContextReal : public GLContext {
185 public:
186 explicit GLContextReal(GLShareGroup* share_group);
188 protected:
189 virtual ~GLContextReal();
191 virtual void SetCurrent(GLSurface* surface) OVERRIDE;
193 private:
194 DISALLOW_COPY_AND_ASSIGN(GLContextReal);
197 } // namespace gfx
199 #endif // UI_GL_GL_CONTEXT_H_