blob: 3eb390fce5cb03f4dcca9e43cc2eda7d12f35f93 [file] [log] [blame]
[email protected]c9e2cbbb2012-05-12 21:17:271// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d37231fa2010-04-09 21:16:022// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]c9e2cbbb2012-05-12 21:17:275#ifndef UI_GL_GL_CONTEXT_H_
6#define UI_GL_GL_CONTEXT_H_
[email protected]d37231fa2010-04-09 21:16:027
[email protected]1c0585d2010-10-07 23:19:168#include <string>
9
[email protected]f62a5ab2011-05-23 20:34:1510#include "base/basictypes.h"
[email protected]fbe20372011-06-01 01:46:3811#include "base/memory/ref_counted.h"
[email protected]1868a342012-11-07 15:56:0212#include "base/memory/scoped_ptr.h"
[email protected]c9e2cbbb2012-05-12 21:17:2713#include "ui/gl/gl_share_group.h"
[email protected]217004512013-05-10 21:25:5514#include "ui/gl/gl_state_restorer.h"
[email protected]c9e2cbbb2012-05-12 21:17:2715#include "ui/gl/gpu_preference.h"
[email protected]d37231fa2010-04-09 21:16:0216
[email protected]5a6db6c2010-04-22 18:32:0617namespace gfx {
[email protected]d37231fa2010-04-09 21:16:0218
[email protected]a4127722011-04-27 23:13:5219class GLSurface;
[email protected]1868a342012-11-07 15:56:0220class VirtualGLApi;
[email protected]a4127722011-04-27 23:13:5221
[email protected]d37231fa2010-04-09 21:16:0222// Encapsulates an OpenGL context, hiding platform specific management.
[email protected]fe377e12011-08-18 17:37:3623class GL_EXPORT GLContext : public base::RefCounted<GLContext> {
[email protected]d37231fa2010-04-09 21:16:0224 public:
[email protected]7196e012011-06-16 20:54:5325 explicit GLContext(GLShareGroup* share_group);
[email protected]d37231fa2010-04-09 21:16:0226
[email protected]f62a5ab2011-05-23 20:34:1527 // 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]276f89062011-10-13 22:55:5031 virtual bool Initialize(
32 GLSurface* compatible_surface, GpuPreference gpu_preference) = 0;
[email protected]f62a5ab2011-05-23 20:34:1533
[email protected]d37231fa2010-04-09 21:16:0234 // Destroys the GL context.
35 virtual void Destroy() = 0;
36
[email protected]f62a5ab2011-05-23 20:34:1537 // Makes the GL context and a surface current on the current thread.
38 virtual bool MakeCurrent(GLSurface* surface) = 0;
[email protected]d37231fa2010-04-09 21:16:0239
[email protected]f62a5ab2011-05-23 20:34:1540 // Releases this GL context and surface as current on the current thread.
41 virtual void ReleaseCurrent(GLSurface* surface) = 0;
[email protected]a4127722011-04-27 23:13:5242
[email protected]f62a5ab2011-05-23 20:34:1543 // 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]a4127722011-04-27 23:13:5246
[email protected]d37231fa2010-04-09 21:16:0247 // Get the underlying platform specific GL context "handle".
[email protected]4bedba72010-04-20 22:08:5448 virtual void* GetHandle() = 0;
49
[email protected]217004512013-05-10 21:25:5550 // 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]1868a342012-11-07 15:56:0255
[email protected]1c0585d2010-10-07 23:19:1656 // 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]631e6c9b2012-09-21 00:24:0262 // 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]97419c02013-04-10 02:52:3867 // 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]9b753992013-04-27 02:04:4171 // Indicate that the real context switches should unbind the FBO first
[email protected]85cb4682013-04-20 00:54:2472 // (For an Android work-around only).
[email protected]9b753992013-04-27 02:04:4173 virtual void SetUnbindFboOnMakeCurrent();
[email protected]85cb4682013-04-20 00:54:2474
[email protected]876f6fee2010-08-02 23:10:3275 // Returns whether the current context supports the named extension. The
76 // context must be current.
[email protected]1c0585d2010-10-07 23:19:1677 bool HasExtension(const char* name);
[email protected]876f6fee2010-08-02 23:10:3278
[email protected]7196e012011-06-16 20:54:5379 GLShareGroup* share_group();
80
[email protected]ffae4022011-05-12 22:54:2981 // Create a GL context that is compatible with the given surface.
[email protected]7196e012011-06-16 20:54:5382 // |share_group|, if non-NULL, is a group of contexts which the
[email protected]ffae4022011-05-12 22:54:2983 // internally created OpenGL context shares textures and other resources.
[email protected]fbe20372011-06-01 01:46:3884 static scoped_refptr<GLContext> CreateGLContext(
[email protected]7196e012011-06-16 20:54:5385 GLShareGroup* share_group,
[email protected]276f89062011-10-13 22:55:5086 GLSurface* compatible_surface,
87 GpuPreference gpu_preference);
[email protected]d37231fa2010-04-09 21:16:0288
[email protected]0fc35742011-04-13 17:57:5489 static bool LosesAllContextsOnContextLost();
90
[email protected]1e9c0c82013-06-06 14:59:2491 // Returns the last GLContext made current, virtual or real.
[email protected]c777de52011-09-09 23:08:5692 static GLContext* GetCurrent();
93
[email protected]706b69f2012-07-27 04:59:3094 virtual bool WasAllocatedUsingRobustnessExtension();
[email protected]38d139d2011-07-14 00:38:4395
[email protected]1868a342012-11-07 15:56:0296 // Use this context for virtualization.
97 void SetupForVirtualization();
98
99 // Make this context current when used for context virtualization.
[email protected]b3cbad12012-12-05 19:56:36100 bool MakeVirtuallyCurrent(GLContext* virtual_context, GLSurface* surface);
101
102 // Notify this context that |virtual_context|, that was using us, is
[email protected]063121b82013-05-31 03:48:14103 // being released or destroyed.
104 void OnReleaseVirtuallyCurrent(GLContext* virtual_context);
[email protected]1868a342012-11-07 15:56:02105
[email protected]fbe20372011-06-01 01:46:38106 protected:
107 virtual ~GLContext();
[email protected]1868a342012-11-07 15:56:02108
109 // Sets the GL api to the real hardware API (vs the VirtualAPI)
110 static void SetRealGLApi();
[email protected]1e9c0c82013-06-06 14:59:24111 virtual void SetCurrent(GLSurface* surface);
[email protected]fbe20372011-06-01 01:46:38112
[email protected]6494823b2011-10-27 18:30:44113 // 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]1e9c0c82013-06-06 14:59:24118 // Returns the last real (non-virtual) GLContext made current.
119 static GLContext* GetRealCurrent();
120
[email protected]d37231fa2010-04-09 21:16:02121 private:
[email protected]fbe20372011-06-01 01:46:38122 friend class base::RefCounted<GLContext>;
[email protected]c9e2cbbb2012-05-12 21:17:27123
[email protected]1e9c0c82013-06-06 14:59:24124 // For GetRealCurrent.
125 friend class VirtualGLApi;
126
[email protected]c9e2cbbb2012-05-12 21:17:27127 scoped_refptr<GLShareGroup> share_group_;
[email protected]1868a342012-11-07 15:56:02128 scoped_ptr<VirtualGLApi> virtual_gl_api_;
[email protected]217004512013-05-10 21:25:55129 scoped_ptr<GLStateRestorer> state_restorer_;
[email protected]c9e2cbbb2012-05-12 21:17:27130
[email protected]d37231fa2010-04-09 21:16:02131 DISALLOW_COPY_AND_ASSIGN(GLContext);
132};
133
[email protected]1e9c0c82013-06-06 14:59:24134class 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]5a6db6c2010-04-22 18:32:06147} // namespace gfx
[email protected]d37231fa2010-04-09 21:16:02148
[email protected]c9e2cbbb2012-05-12 21:17:27149#endif // UI_GL_GL_CONTEXT_H_