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 PXR_IMAGING_GLF_GL_CONTEXT_H
25 #define PXR_IMAGING_GLF_GL_CONTEXT_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/glf/api.h"
29 #include "pxr/base/arch/threads.h"
30 
31 #include <memory>
32 
33 PXR_NAMESPACE_OPEN_SCOPE
34 
35 
36 typedef std::shared_ptr<class GlfGLContext> GlfGLContextSharedPtr;
37 
53 {
54 public:
55  GLF_API
56  virtual ~GlfGLContext();
57 
58  // Disallow copies
59  GlfGLContext(const GlfGLContext&) = delete;
60  GlfGLContext& operator=(const GlfGLContext&) = delete;
61 
63  GLF_API
64  static GlfGLContextSharedPtr GetCurrentGLContext();
65 
67  GLF_API
68  static GlfGLContextSharedPtr GetSharedGLContext();
69 
71  GLF_API
72  static void MakeCurrent(const GlfGLContextSharedPtr& context);
73 
75  GLF_API
76  static bool AreSharing(GlfGLContextSharedPtr const & context1,
77  GlfGLContextSharedPtr const & context2);
78 
80  GLF_API
81  static bool IsInitialized();
82 
84  GLF_API
85  bool IsCurrent() const;
86 
87 private:
91  void MakeCurrent();
92 
93 public:
95  GLF_API
96  static void DoneCurrent();
97 
99  GLF_API
100  bool IsSharing(GlfGLContextSharedPtr const & otherContext);
101 
103  virtual bool IsValid() const = 0;
104 
116  virtual GlfGLContextSharedPtr CreateSharingContext() {return nullptr;}
117 
118 protected:
119  GLF_API
120  GlfGLContext();
121 
123  virtual void _MakeCurrent() = 0;
124 
126  virtual bool _IsSharing(const GlfGLContextSharedPtr& rhs) const = 0;
127 
129  virtual bool _IsEqual(const GlfGLContextSharedPtr& rhs) const = 0;
130 };
131 
171 {
172 public:
175  GLF_API
176  explicit GlfGLContextScopeHolder(const GlfGLContextSharedPtr& newContext);
177 
178  GLF_API
179  ~GlfGLContextScopeHolder();
180 
181  GlfGLContextScopeHolder(const GlfGLContextScopeHolder&) = delete;
182  GlfGLContextScopeHolder& operator=(const GlfGLContextScopeHolder) = delete;
183 
184 protected:
185  GLF_API
186  void _MakeNewContextCurrent();
187  GLF_API
188  void _RestoreOldContext();
189 
190 private:
191  GlfGLContextSharedPtr _newContext;
192  GlfGLContextSharedPtr _oldContext;
193 };
194 
237 {
238 public:
240  GlfGLContextScopeHolder(_GetSharedContext())
241  {
242  // Do nothing
243  }
244 
245 private:
246  static GlfGLContextSharedPtr _GetSharedContext()
247  {
250  }
251  return GlfGLContextSharedPtr();
252  }
253 };
254 
255 #define API_GLF_HAS_ANY_GL_CONTEXT_SCOPE_HOLDER 1
256 
300 {
301 public:
303  GlfGLContextScopeHolder(_GetAnyContext())
304  {
305  // Do nothing
306  }
307 
308 private:
309  static GlfGLContextSharedPtr _GetAnyContext()
310  {
312  GlfGLContextSharedPtr const current =
314  if (!(current && current->IsValid())) {
316  }
317  }
318  return GlfGLContextSharedPtr();
319  }
320 };
321 
330 {
331 public:
332  GLF_API
334 
335  // Disallow copies
337  const GlfGLContextRegistrationInterface&) = delete;
339  const GlfGLContextRegistrationInterface&) = delete;
340 
343  virtual GlfGLContextSharedPtr GetShared() = 0;
344 
350  virtual GlfGLContextSharedPtr GetCurrent() = 0;
351 
352 protected:
353  GLF_API
355 };
356 
357 
358 PXR_NAMESPACE_CLOSE_SCOPE
359 
360 #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:116
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:236
static GLF_API GlfGLContextSharedPtr GetCurrentGLContext()
Returns an instance for the current GL context.
Architecture-specific thread function calls.
static GLF_API void DoneCurrent()
Makes no context current.
Helper class to make the shared GL context current if there is no valid current context.
Definition: glContext.h:299
Interface for registering a GlfGLContext system.
Definition: glContext.h:329
Helper class to make a GL context current.
Definition: glContext.h:170
virtual bool _IsSharing(const GlfGLContextSharedPtr &rhs) const =0
Returns true if this context is sharing with rhs.