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. 4 5 #ifndef UI_GL_GL_CONTEXT_H_ 6 #define UI_GL_GL_CONTEXT_H_ 7 8 #include <string> 9 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" 16 17 namespace gfx { 18 19 class GLSurface; 20 class VirtualGLApi; 21 22 // Encapsulates an OpenGL context, hiding platform specific management. 23 class GL_EXPORT GLContext : public base::RefCounted<GLContext> { 24 public: 25 explicit GLContext(GLShareGroup* share_group); 26 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; 33 34 // Destroys the GL context. 35 virtual void Destroy() = 0; 36 37 // Makes the GL context and a surface current on the current thread. 38 virtual bool MakeCurrent(GLSurface* surface) = 0; 39 40 // Releases this GL context and surface as current on the current thread. 41 virtual void ReleaseCurrent(GLSurface* surface) = 0; 42 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; 46 47 // Get the underlying platform specific GL context "handle". 48 virtual void* GetHandle() = 0; 49 50 // Gets the GLStateRestorer for the context. 51 GLStateRestorer* GetGLStateRestorer(); 52 53 // Sets the GLStateRestorer for the context (takes ownership). 54 void SetGLStateRestorer(GLStateRestorer* state_restorer); 55 56 // Set swap interval. This context must be current. 57 virtual void SetSwapInterval(int interval) = 0; 58 59 // Returns space separated list of extensions. The context must be current. 60 virtual std::string GetExtensions(); 61 62 // Returns in bytes the total amount of GPU memory for the GPU which this 63 // context is currently rendering on. Returns false if no extension exists 64 // to get the exact amount of GPU memory. 65 virtual bool GetTotalGpuMemory(size_t* bytes); 66 67 // Indicate that it is safe to force this context to switch GPUs, since 68 // transitioning can cause corruption and hangs (OS X only). 69 virtual void SetSafeToForceGpuSwitch(); 70 71 // Indicate that the real context switches should unbind the FBO first 72 // (For an Android work-around only). 73 virtual void SetUnbindFboOnMakeCurrent(); 74 75 // Returns whether the current context supports the named extension. The 76 // context must be current. 77 bool HasExtension(const char* name); 78 79 GLShareGroup* share_group(); 80 81 // Create a GL context that is compatible with the given surface. 82 // |share_group|, if non-NULL, is a group of contexts which the 83 // internally created OpenGL context shares textures and other resources. 84 static scoped_refptr<GLContext> CreateGLContext( 85 GLShareGroup* share_group, 86 GLSurface* compatible_surface, 87 GpuPreference gpu_preference); 88 89 static bool LosesAllContextsOnContextLost(); 90 91 // Returns the last GLContext made current, virtual or real. 92 static GLContext* GetCurrent(); 93 94 virtual bool WasAllocatedUsingRobustnessExtension(); 95 96 // Use this context for virtualization. 97 void SetupForVirtualization(); 98 99 // Make this context current when used for context virtualization. 100 bool MakeVirtuallyCurrent(GLContext* virtual_context, GLSurface* surface); 101 102 // Notify this context that |virtual_context|, that was using us, is 103 // being released or destroyed. 104 void OnReleaseVirtuallyCurrent(GLContext* virtual_context); 105 106 protected: 107 virtual ~GLContext(); 108 109 // Sets the GL api to the real hardware API (vs the VirtualAPI) 110 static void SetRealGLApi(); 111 virtual void SetCurrent(GLSurface* surface); 112 113 // Initialize function pointers to extension functions in the GL 114 // implementation. Should be called immediately after this context is made 115 // current. 116 bool InitializeExtensionBindings(); 117 118 // Returns the last real (non-virtual) GLContext made current. 119 static GLContext* GetRealCurrent(); 120 121 private: 122 friend class base::RefCounted<GLContext>; 123 124 // For GetRealCurrent. 125 friend class VirtualGLApi; 126 127 scoped_refptr<GLShareGroup> share_group_; 128 scoped_ptr<VirtualGLApi> virtual_gl_api_; 129 scoped_ptr<GLStateRestorer> state_restorer_; 130 131 DISALLOW_COPY_AND_ASSIGN(GLContext); 132 }; 133 134 class GL_EXPORT GLContextReal : public GLContext { 135 public: 136 explicit GLContextReal(GLShareGroup* share_group); 137 138 protected: 139 virtual ~GLContextReal(); 140 141 virtual void SetCurrent(GLSurface* surface) OVERRIDE; 142 143 private: 144 DISALLOW_COPY_AND_ASSIGN(GLContextReal); 145 }; 146 147 } // namespace gfx 148 149 #endif // UI_GL_GL_CONTEXT_H_ 150