All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
glContext.h
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef GLF_GLCONTEXT_H
25 #define GLF_GLCONTEXT_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/glf/api.h"
29 #include "pxr/base/arch/threads.h"
30 #include <boost/noncopyable.hpp>
31 #include <boost/shared_ptr.hpp>
32 
33 PXR_NAMESPACE_OPEN_SCOPE
34 
35 
36 typedef boost::shared_ptr<class GlfGLContext> GlfGLContextSharedPtr;
37 
52 class GlfGLContext : public boost::noncopyable {
53 public:
54  GLF_API
55  virtual ~GlfGLContext();
56 
58  GLF_API
59  static GlfGLContextSharedPtr GetCurrentGLContext();
60 
62  GLF_API
63  static GlfGLContextSharedPtr GetSharedGLContext();
64 
66  GLF_API
67  static void MakeCurrent(const GlfGLContextSharedPtr& context);
68 
70  GLF_API
71  static bool AreSharing(GlfGLContextSharedPtr const & context1,
72  GlfGLContextSharedPtr const & context2);
73 
75  GLF_API
76  static bool IsInitialized();
77 
79  GLF_API
80  bool IsCurrent() const;
81 
82 private:
86  void MakeCurrent();
87 
88 public:
90  GLF_API
91  static void DoneCurrent();
92 
94  GLF_API
95  bool IsSharing(GlfGLContextSharedPtr const & otherContext);
96 
98  virtual bool IsValid() const = 0;
99 
111  virtual GlfGLContextSharedPtr CreateSharingContext() {return nullptr;}
112 
113 protected:
114  GLF_API
115  GlfGLContext();
116 
118  virtual void _MakeCurrent() = 0;
119 
121  virtual bool _IsSharing(const GlfGLContextSharedPtr& rhs) const = 0;
122 
124  virtual bool _IsEqual(const GlfGLContextSharedPtr& rhs) const = 0;
125 };
126 
165 class GlfGLContextScopeHolder : boost::noncopyable {
166 public:
169  GLF_API
170  explicit GlfGLContextScopeHolder(const GlfGLContextSharedPtr& newContext);
171 
172  GLF_API
173  ~GlfGLContextScopeHolder();
174 
175 protected:
176  GLF_API
177  void _MakeNewContextCurrent();
178  GLF_API
179  void _RestoreOldContext();
180 
181 private:
182  GlfGLContextSharedPtr _newContext;
183  GlfGLContextSharedPtr _oldContext;
184 };
185 
228 public:
230  GlfGLContextScopeHolder(_GetSharedContext())
231  {
232  // Do nothing
233  }
234 
235 private:
236  static GlfGLContextSharedPtr _GetSharedContext()
237  {
240  }
241  return GlfGLContextSharedPtr();
242  }
243 };
244 
252 class GlfGLContextRegistrationInterface : boost::noncopyable {
253 public:
254  GLF_API
256 
259  virtual GlfGLContextSharedPtr GetShared() = 0;
260 
266  virtual GlfGLContextSharedPtr GetCurrent() = 0;
267 
268 protected:
269  GLF_API
271 };
272 
273 
274 PXR_NAMESPACE_CLOSE_SCOPE
275 
276 #endif
GLF_API GlfGLContextScopeHolder(const GlfGLContextSharedPtr &newContext)
Make the given context current and restore the current context when this object is destroyed...
static GLF_API bool AreSharing(GlfGLContextSharedPtr const &context1, GlfGLContextSharedPtr const &context2)
Returns true if context1 and context2 are sharing.
GLF_API bool IsSharing(GlfGLContextSharedPtr const &otherContext)
Returns true if this context is sharing with otherContext.
virtual GlfGLContextSharedPtr GetShared()=0
If this GLContext system supports a shared context this should return it.
virtual bool IsValid() const =0
Returns true if this context is valid.
static GLF_API bool IsInitialized()
Returns whether this interface has been initialized.
virtual bool _IsEqual(const GlfGLContextSharedPtr &rhs) const =0
Returns true if this context is equal to rhs.
virtual GlfGLContextSharedPtr CreateSharingContext()
Creates a new GlfContext that shares GL resources with this context.
Definition: glContext.h:111
static GLF_API GlfGLContextSharedPtr GetSharedGLContext()
Returns an instance for the shared GL context.
virtual void _MakeCurrent()=0
Makes this context current.
Provides window system independent access to GL contexts.
Definition: glContext.h:52
GLF_API bool IsCurrent() const
Returns true if this context is current.
virtual GlfGLContextSharedPtr GetCurrent()=0
Whatever your GLContext system thinks is the current GL context may not really be the current context...
ARCH_API bool ArchIsMainThread()
Return true if the calling thread is the main thread, false otherwise.
static GLF_API void MakeCurrent(const GlfGLContextSharedPtr &context)
Makes context current if valid, otherwise makes no context current.
Helper class to make the shared GL context current.
Definition: glContext.h:227
static GLF_API GlfGLContextSharedPtr GetCurrentGLContext()
Returns an instance for the current GL context.
static GLF_API void DoneCurrent()
Makes no context current.
Interface for registering a GlfGLContext system.
Definition: glContext.h:252
Helper class to make a GL context current.
Definition: glContext.h:165
virtual bool _IsSharing(const GlfGLContextSharedPtr &rhs) const =0
Returns true if this context is sharing with rhs.