[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" |
| 14 | #include "ui/gl/gpu_preference.h" |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 15 | |
[email protected] | 5a6db6c | 2010-04-22 18:32:06 | [diff] [blame] | 16 | namespace gfx { |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 17 | |
[email protected] | a412772 | 2011-04-27 23:13:52 | [diff] [blame] | 18 | class GLSurface; |
[email protected] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 19 | class VirtualGLApi; |
| 20 | class GLStateRestorer; |
[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] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 50 | // Gets the GLStateRestore for the context. |
| 51 | virtual GLStateRestorer* GetGLStateRestorer(); |
| 52 | |
[email protected] | 1c0585d | 2010-10-07 23:19:16 | [diff] [blame] | 53 | // Set swap interval. This context must be current. |
| 54 | virtual void SetSwapInterval(int interval) = 0; |
| 55 | |
| 56 | // Returns space separated list of extensions. The context must be current. |
| 57 | virtual std::string GetExtensions(); |
| 58 | |
[email protected] | 631e6c9b | 2012-09-21 00:24:02 | [diff] [blame] | 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); |
| 63 | |
[email protected] | 97419c0 | 2013-04-10 02:52:38 | [diff] [blame] | 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(); |
| 67 | |
[email protected] | 9b75399 | 2013-04-27 02:04:41 | [diff] [blame^] | 68 | // Indicate that the real context switches should unbind the FBO first |
[email protected] | 85cb468 | 2013-04-20 00:54:24 | [diff] [blame] | 69 | // (For an Android work-around only). |
[email protected] | 9b75399 | 2013-04-27 02:04:41 | [diff] [blame^] | 70 | virtual void SetUnbindFboOnMakeCurrent(); |
[email protected] | 85cb468 | 2013-04-20 00:54:24 | [diff] [blame] | 71 | |
[email protected] | 876f6fee | 2010-08-02 23:10:32 | [diff] [blame] | 72 | // Returns whether the current context supports the named extension. The |
| 73 | // context must be current. |
[email protected] | 1c0585d | 2010-10-07 23:19:16 | [diff] [blame] | 74 | bool HasExtension(const char* name); |
[email protected] | 876f6fee | 2010-08-02 23:10:32 | [diff] [blame] | 75 | |
[email protected] | 7196e01 | 2011-06-16 20:54:53 | [diff] [blame] | 76 | GLShareGroup* share_group(); |
| 77 | |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 78 | // Create a GL context that is compatible with the given surface. |
[email protected] | 7196e01 | 2011-06-16 20:54:53 | [diff] [blame] | 79 | // |share_group|, if non-NULL, is a group of contexts which the |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 80 | // internally created OpenGL context shares textures and other resources. |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 81 | static scoped_refptr<GLContext> CreateGLContext( |
[email protected] | 7196e01 | 2011-06-16 20:54:53 | [diff] [blame] | 82 | GLShareGroup* share_group, |
[email protected] | 276f8906 | 2011-10-13 22:55:50 | [diff] [blame] | 83 | GLSurface* compatible_surface, |
| 84 | GpuPreference gpu_preference); |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 85 | |
[email protected] | 0fc3574 | 2011-04-13 17:57:54 | [diff] [blame] | 86 | static bool LosesAllContextsOnContextLost(); |
| 87 | |
[email protected] | c777de5 | 2011-09-09 23:08:56 | [diff] [blame] | 88 | static GLContext* GetCurrent(); |
| 89 | |
[email protected] | 706b69f | 2012-07-27 04:59:30 | [diff] [blame] | 90 | virtual bool WasAllocatedUsingRobustnessExtension(); |
[email protected] | 38d139d | 2011-07-14 00:38:43 | [diff] [blame] | 91 | |
[email protected] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 92 | // Use this context for virtualization. |
| 93 | void SetupForVirtualization(); |
| 94 | |
| 95 | // Make this context current when used for context virtualization. |
[email protected] | b3cbad1 | 2012-12-05 19:56:36 | [diff] [blame] | 96 | bool MakeVirtuallyCurrent(GLContext* virtual_context, GLSurface* surface); |
| 97 | |
| 98 | // Notify this context that |virtual_context|, that was using us, is |
| 99 | // being destroyed. |
| 100 | void OnDestroyVirtualContext(GLContext* virtual_context); |
[email protected] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 101 | |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 102 | protected: |
| 103 | virtual ~GLContext(); |
[email protected] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 104 | |
| 105 | // Sets the GL api to the real hardware API (vs the VirtualAPI) |
| 106 | static void SetRealGLApi(); |
[email protected] | c777de5 | 2011-09-09 23:08:56 | [diff] [blame] | 107 | static void SetCurrent(GLContext* context, GLSurface* surface); |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 108 | |
[email protected] | 6494823b | 2011-10-27 18:30:44 | [diff] [blame] | 109 | // Initialize function pointers to extension functions in the GL |
| 110 | // implementation. Should be called immediately after this context is made |
| 111 | // current. |
| 112 | bool InitializeExtensionBindings(); |
| 113 | |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 114 | private: |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 115 | friend class base::RefCounted<GLContext>; |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 116 | |
| 117 | scoped_refptr<GLShareGroup> share_group_; |
[email protected] | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 118 | scoped_ptr<VirtualGLApi> virtual_gl_api_; |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 119 | |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 120 | DISALLOW_COPY_AND_ASSIGN(GLContext); |
| 121 | }; |
| 122 | |
[email protected] | 5a6db6c | 2010-04-22 18:32:06 | [diff] [blame] | 123 | } // namespace gfx |
[email protected] | d37231fa | 2010-04-09 21:16:02 | [diff] [blame] | 124 | |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 125 | #endif // UI_GL_GL_CONTEXT_H_ |