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
180 
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 
304 {
305 public:
307  GlfGLContextScopeHolder(_GetAnyContext())
308  {
309  // Do nothing
310  }
311 
312 private:
313  static GlfGLContextSharedPtr _GetAnyContext()
314  {
316  GlfGLContextSharedPtr const current =
318  if (!(current &&
319  current->IsValid() &&
320  current->IsSharing(GlfGLContext::GetSharedGLContext()))) {
322  }
323  }
324  return GlfGLContextSharedPtr();
325  }
326 };
327 
336 {
337 public:
338  GLF_API
340 
341  // Disallow copies
343  const GlfGLContextRegistrationInterface&) = delete;
345  const GlfGLContextRegistrationInterface&) = delete;
346 
349  virtual GlfGLContextSharedPtr GetShared() = 0;
350 
356  virtual GlfGLContextSharedPtr GetCurrent() = 0;
357 
358 protected:
359  GLF_API
361 };
362 
363 
364 PXR_NAMESPACE_CLOSE_SCOPE
365 
366 #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
GLF_API bool IsCurrent() const
Returns true if this context is current.
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
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:303
Interface for registering a GlfGLContext system.
Definition: glContext.h:335
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.