[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 5 | #ifndef UI_GL_GL_CONTEXT_H_ |
| 6 | #define UI_GL_GL_CONTEXT_H_ |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 7 | |
[email protected] | 1c0585d | 2010-10-07 23:19:16 | [diff] [blame] | 8 | #include <string> |
| 9 | |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
[email protected] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 13 | #include "ui/gl/gl_share_group.h" |
[email protected] | 21700451 | 2013-05-10 21:25:55 | [diff] [blame] | 14 | #include "ui/gl/gl_state_restorer.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 15 | #include "ui/gl/gpu_preference.h" |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 16 | |
[email protected] | 5a6db6c | 2010-04-22 18:32:06 | [diff] [blame] | 17 | namespace gfx { |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 18 | |
[email protected] | a412772 | 2011-04-27 23:13:52 | [diff] [blame] | 19 | class GLSurface; |
[email protected] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 20 | class VirtualGLApi; |
[email protected] | a412772 | 2011-04-27 23:13:52 | [diff] [blame] | 21 | |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 22 | // Encapsulates an OpenGL context, hiding platform specific management. |
[email protected] | fe377e1 | 2011-08-18 17:37:36 | [diff] [blame] | 23 | class GL_EXPORT GLContext : public base::RefCounted<GLContext> { |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 24 | public: |
[email protected] | 7196e01 | 2011-06-16 20:54:53 | [diff] [blame] | 25 | explicit GLContext(GLShareGroup* share_group); |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 26 | |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 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. |
[email protected] | 276f8906 | 2011-10-13 22:55:50 | [diff] [blame] | 31 | virtual bool Initialize( |
| 32 | GLSurface* compatible_surface, GpuPreference gpu_preference) = 0; |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 33 | |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 34 | // Destroys the GL context. |
| 35 | virtual void Destroy() = 0; |
| 36 | |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 37 | // Makes the GL context and a surface current on the current thread. |
| 38 | virtual bool MakeCurrent(GLSurface* surface) = 0; |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 39 | |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 40 | // Releases this GL context and surface as current on the current thread. |
| 41 | virtual void ReleaseCurrent(GLSurface* surface) = 0; |
[email protected] | a412772 | 2011-04-27 23:13:52 | [diff] [blame] | 42 | |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 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; |
[email protected] | a412772 | 2011-04-27 23:13:52 | [diff] [blame] | 46 | |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 47 | // Get the underlying platform specific GL context "handle". |
[email protected] | 4bedba7 | 2010-04-20 22:08:54 | [diff] [blame] | 48 | virtual void* GetHandle() = 0; |
| 49 | |
[email protected] | 21700451 | 2013-05-10 21:25:55 | [diff] [blame] | 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); |
[email protected] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 55 | |
[email protected] | 1c0585d | 2010-10-07 23:19:16 | [diff] [blame] | 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 | |
[email protected] | 631e6c9b | 2012-09-21 00:24:02 | [diff] [blame] | 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 | |
[email protected] | 97419c0 | 2013-04-10 02:52:38 | [diff] [blame] | 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 | |
[email protected] | 9b75399 | 2013-04-27 02:04:41 | [diff] [blame] | 71 | // Indicate that the real context switches should unbind the FBO first |
[email protected] | 85cb468 | 2013-04-20 00:54:24 | [diff] [blame] | 72 | // (For an Android work-around only). |
[email protected] | 9b75399 | 2013-04-27 02:04:41 | [diff] [blame] | 73 | virtual void SetUnbindFboOnMakeCurrent(); |
[email protected] | 85cb468 | 2013-04-20 00:54:24 | [diff] [blame] | 74 | |
[email protected] | 876f6fee | 2010-08-02 23:10:32 | [diff] [blame] | 75 | // Returns whether the current context supports the named extension. The |
| 76 | // context must be current. |
[email protected] | 1c0585d | 2010-10-07 23:19:16 | [diff] [blame] | 77 | bool HasExtension(const char* name); |
[email protected] | 876f6fee | 2010-08-02 23:10:32 | [diff] [blame] | 78 | |
[email protected] | 7196e01 | 2011-06-16 20:54:53 | [diff] [blame] | 79 | GLShareGroup* share_group(); |
| 80 | |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 81 | // Create a GL context that is compatible with the given surface. |
[email protected] | 7196e01 | 2011-06-16 20:54:53 | [diff] [blame] | 82 | // |share_group|, if non-NULL, is a group of contexts which the |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 83 | // internally created OpenGL context shares textures and other resources. |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 84 | static scoped_refptr<GLContext> CreateGLContext( |
[email protected] | 7196e01 | 2011-06-16 20:54:53 | [diff] [blame] | 85 | GLShareGroup* share_group, |
[email protected] | 276f8906 | 2011-10-13 22:55:50 | [diff] [blame] | 86 | GLSurface* compatible_surface, |
| 87 | GpuPreference gpu_preference); |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 88 | |
[email protected] | 0fc3574 | 2011-04-13 17:57:54 | [diff] [blame] | 89 | static bool LosesAllContextsOnContextLost(); |
| 90 | |
[email protected] | 1e9c0c8 | 2013-06-06 14:59:24 | [diff] [blame] | 91 | // Returns the last GLContext made current, virtual or real. |
[email protected] | c777de5 | 2011-09-09 23:08:56 | [diff] [blame] | 92 | static GLContext* GetCurrent(); |
| 93 | |
[email protected] | 706b69f | 2012-07-27 04:59:30 | [diff] [blame] | 94 | virtual bool WasAllocatedUsingRobustnessExtension(); |
[email protected] | 38d139d | 2011-07-14 00:38:43 | [diff] [blame] | 95 | |
[email protected] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 96 | // Use this context for virtualization. |
| 97 | void SetupForVirtualization(); |
| 98 | |
| 99 | // Make this context current when used for context virtualization. |
[email protected] | b3cbad1 | 2012-12-05 19:56:36 | [diff] [blame] | 100 | bool MakeVirtuallyCurrent(GLContext* virtual_context, GLSurface* surface); |
| 101 | |
| 102 | // Notify this context that |virtual_context|, that was using us, is |
[email protected] | 063121b8 | 2013-05-31 03:48:14 | [diff] [blame] | 103 | // being released or destroyed. |
| 104 | void OnReleaseVirtuallyCurrent(GLContext* virtual_context); |
[email protected] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 105 | |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 106 | protected: |
| 107 | virtual ~GLContext(); |
[email protected] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 108 | |
| 109 | // Sets the GL api to the real hardware API (vs the VirtualAPI) |
| 110 | static void SetRealGLApi(); |
[email protected] | 1e9c0c8 | 2013-06-06 14:59:24 | [diff] [blame] | 111 | virtual void SetCurrent(GLSurface* surface); |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 112 | |
[email protected] | 6494823b | 2011-10-27 18:30:44 | [diff] [blame] | 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 | |
[email protected] | 1e9c0c8 | 2013-06-06 14:59:24 | [diff] [blame] | 118 | // Returns the last real (non-virtual) GLContext made current. |
| 119 | static GLContext* GetRealCurrent(); |
| 120 | |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 121 | private: |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 122 | friend class base::RefCounted<GLContext>; |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 123 | |
[email protected] | 1e9c0c8 | 2013-06-06 14:59:24 | [diff] [blame] | 124 | // For GetRealCurrent. |
| 125 | friend class VirtualGLApi; |
| 126 | |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 127 | scoped_refptr<GLShareGroup> share_group_; |
[email protected] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 128 | scoped_ptr<VirtualGLApi> virtual_gl_api_; |
[email protected] | 21700451 | 2013-05-10 21:25:55 | [diff] [blame] | 129 | scoped_ptr<GLStateRestorer> state_restorer_; |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 130 | |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 131 | DISALLOW_COPY_AND_ASSIGN(GLContext); |
| 132 | }; |
| 133 | |
[email protected] | 1e9c0c8 | 2013-06-06 14:59:24 | [diff] [blame] | 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 | |
[email protected] | 5a6db6c | 2010-04-22 18:32:06 | [diff] [blame] | 147 | } // namespace gfx |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 148 | |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 149 | #endif // UI_GL_GL_CONTEXT_H_ |