Loading...
Searching...
No Matches
glContext.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_IMAGING_GLF_GL_CONTEXT_H
8#define PXR_IMAGING_GLF_GL_CONTEXT_H
9
10#include "pxr/pxr.h"
11#include "pxr/imaging/glf/api.h"
13
14#include <memory>
15
16PXR_NAMESPACE_OPEN_SCOPE
17
18
19typedef std::shared_ptr<class GlfGLContext> GlfGLContextSharedPtr;
20
36{
37public:
38 GLF_API
39 virtual ~GlfGLContext();
40
41 // Disallow copies
42 GlfGLContext(const GlfGLContext&) = delete;
43 GlfGLContext& operator=(const GlfGLContext&) = delete;
44
46 GLF_API
47 static GlfGLContextSharedPtr GetCurrentGLContext();
48
50 GLF_API
51 static GlfGLContextSharedPtr GetSharedGLContext();
52
54 GLF_API
55 static void MakeCurrent(const GlfGLContextSharedPtr& context);
56
58 GLF_API
59 static bool AreSharing(GlfGLContextSharedPtr const & context1,
60 GlfGLContextSharedPtr const & context2);
61
63 GLF_API
64 static bool IsInitialized();
65
67 GLF_API
68 bool IsCurrent() const;
69
70private:
74 void MakeCurrent();
75
76public:
78 GLF_API
79 static void DoneCurrent();
80
82 GLF_API
83 bool IsSharing(GlfGLContextSharedPtr const & otherContext);
84
86 virtual bool IsValid() const = 0;
87
99 virtual GlfGLContextSharedPtr CreateSharingContext() {return nullptr;}
100
101protected:
102 GLF_API
103 GlfGLContext();
104
106 virtual void _MakeCurrent() = 0;
107
109 virtual bool _IsSharing(const GlfGLContextSharedPtr& rhs) const = 0;
110
112 virtual bool _IsEqual(const GlfGLContextSharedPtr& rhs) const = 0;
113};
114
154{
155public:
158 GLF_API
159 explicit GlfGLContextScopeHolder(const GlfGLContextSharedPtr& newContext);
160
161 GLF_API
163
165 GlfGLContextScopeHolder& operator=(const GlfGLContextScopeHolder) = delete;
166
167protected:
168 GLF_API
169 void _MakeNewContextCurrent();
170 GLF_API
171 void _RestoreOldContext();
172
173private:
174 GlfGLContextSharedPtr _newContext;
175 GlfGLContextSharedPtr _oldContext;
176};
177
220{
221public:
223 GlfGLContextScopeHolder(_GetSharedContext())
224 {
225 // Do nothing
226 }
227
228private:
229 static GlfGLContextSharedPtr _GetSharedContext()
230 {
233 }
234 return GlfGLContextSharedPtr();
235 }
236};
237
238#define API_GLF_HAS_ANY_GL_CONTEXT_SCOPE_HOLDER 1
239
287{
288public:
290 GlfGLContextScopeHolder(_GetAnyContext())
291 {
292 // Do nothing
293 }
294
295private:
296 static GlfGLContextSharedPtr _GetAnyContext()
297 {
299 GlfGLContextSharedPtr const current =
301 if (!(current &&
302 current->IsValid() &&
303 current->IsSharing(GlfGLContext::GetSharedGLContext()))) {
305 }
306 }
307 return GlfGLContextSharedPtr();
308 }
309};
310
319{
320public:
321 GLF_API
323
324 // Disallow copies
326 const GlfGLContextRegistrationInterface&) = delete;
328 const GlfGLContextRegistrationInterface&) = delete;
329
332 virtual GlfGLContextSharedPtr GetShared() = 0;
333
339 virtual GlfGLContextSharedPtr GetCurrent() = 0;
340
341protected:
342 GLF_API
344};
345
346
347PXR_NAMESPACE_CLOSE_SCOPE
348
349#endif
Architecture-specific thread function calls.
Helper class to make the shared GL context current if there is no valid current context,...
Definition: glContext.h:287
Provides window system independent access to GL contexts.
Definition: glContext.h:36
static GLF_API GlfGLContextSharedPtr GetCurrentGLContext()
Returns an instance for the current GL context.
virtual bool _IsEqual(const GlfGLContextSharedPtr &rhs) const =0
Returns true if this context is equal to rhs.
virtual void _MakeCurrent()=0
Makes this context current.
GLF_API bool IsSharing(GlfGLContextSharedPtr const &otherContext)
Returns true if this context is sharing with otherContext.
GLF_API bool IsCurrent() const
Returns true if this context is current.
static GLF_API bool AreSharing(GlfGLContextSharedPtr const &context1, GlfGLContextSharedPtr const &context2)
Returns true if context1 and context2 are sharing.
static GLF_API GlfGLContextSharedPtr GetSharedGLContext()
Returns an instance for the shared GL context.
virtual GlfGLContextSharedPtr CreateSharingContext()
Creates a new GlfContext that shares GL resources with this context.
Definition: glContext.h:99
virtual bool _IsSharing(const GlfGLContextSharedPtr &rhs) const =0
Returns true if this context is sharing with rhs.
static GLF_API bool IsInitialized()
Returns whether this interface has been initialized.
static GLF_API void MakeCurrent(const GlfGLContextSharedPtr &context)
Makes context current if valid, otherwise makes no context current.
static GLF_API void DoneCurrent()
Makes no context current.
virtual bool IsValid() const =0
Returns true if this context is valid.
Interface for registering a GlfGLContext system.
Definition: glContext.h:319
virtual GlfGLContextSharedPtr GetCurrent()=0
Whatever your GLContext system thinks is the current GL context may not really be the current context...
virtual GlfGLContextSharedPtr GetShared()=0
If this GLContext system supports a shared context this should return it.
Helper class to make a GL context current.
Definition: glContext.h:154
GLF_API GlfGLContextScopeHolder(const GlfGLContextSharedPtr &newContext)
Make the given context current and restore the current context when this object is destroyed.
Helper class to make the shared GL context current.
Definition: glContext.h:220
ARCH_API bool ArchIsMainThread()
Return true if the calling thread is the main thread, false otherwise.