openscenegraph
ContextData
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSG_CONTEXTDATA
15#define OSG_CONTEXTDATA 1
16
17#include <osg/GraphicsContext>
18
19namespace osg {
20
21class OSG_EXPORT ContextData : public GraphicsObjectManager
22{
23 public:
24 ContextData(unsigned int contextID);
25
26 void incrementUsageCount() { ++_numContexts; }
27 void decrementUsageCount() { --_numContexts; }
28
29 void setNumContexts(unsigned int numContexts) { _numContexts = numContexts; }
30 unsigned int getNumContexts() const { return _numContexts; }
31
32 void setCompileContext(osg::GraphicsContext* gc) { _compileContext = gc; }
33 osg::GraphicsContext* getCompileContext() { return _compileContext.get(); }
34
35 /** Get a specific GL extensions object or GraphicsObjectManager, initialize if not already present.
36 * Note, must only be called from a the graphics context thread associated with this osg::State. */
37 template<typename T>
38 T* get()
39 {
40 const std::type_info* id(&typeid(T));
41 osg::ref_ptr<osg::Referenced>& ptr = _managerMap[id];
42 if (!ptr)
43 {
44 ptr = new T(_contextID);
45 }
46 return static_cast<T*>(ptr.get());
47 }
48
49 /** Get a specific GL extensions object or GraphicsObjectManager if it already exists in the extension map.
50 * Note, safe to call outwith a the graphics context thread associated with this osg::State.
51 * Returns NULL if the desired extension object has not been created yet.*/
52 template<typename T>
53 const T* get() const
54 {
55 const std::type_info* id(&typeid(T));
56 ManagerMap::const_iterator itr = _managerMap.find(id);
57 if (itr==_managerMap.end()) return 0;
58 else return itr->second.get();
59 }
60
61 /** Set a specific GL extensions object pr GraphicsObjectManager. */
62 template<typename T>
63 void set(T* ptr)
64 {
65 const std::type_info* id(&typeid(T));
66 _managerMap[id] = ptr;
67 }
68
69 /** Signal that a new frame has started.*/
70 virtual void newFrame(osg::FrameStamp*);
71
72 virtual void resetStats();
73 virtual void reportStats(std::ostream& out);
74 virtual void recomputeStats(std::ostream& out) const;
75
76 /** Flush all deleted OpenGL objects within the specified availableTime.
77 * Note, must be called from a thread which has current the graphics context associated with contextID. */
78 virtual void flushDeletedGLObjects(double currentTime, double& availableTime);
79
80 /** Flush all deleted OpenGL objects.
81 * Note, must be called from a thread which has current the graphics context associated with contextID. */
82 virtual void flushAllDeletedGLObjects();
83
84 /** Do a GL delete all OpenGL objects.
85 * Note, must be called from a thread which has current the graphics context associated with contextID. */
86 virtual void deleteAllGLObjects();
87
88 /** Discard all OpenGL objects.
89 * Note, unlike deleteAllGLjects discard does not
90 * do any OpenGL calls so can be called from any thread, but as a consequence it
91 * also doesn't remove the associated OpenGL resource so discard should only be
92 * called when the associated graphics context is being/has been closed. */
93 virtual void discardAllGLObjects();
94
95 public:
96
97 /** Create a contextID for a new graphics context, this contextID is used to set up the osg::State associate with context.
98 * Automatically increments the usage count of the contextID to 1.*/
99 static unsigned int createNewContextID();
100
101 /** Get the current max ContextID.*/
102 static unsigned int getMaxContextID();
103
104 /** Increment the usage count associate with a contextID. The usage count specifies how many graphics contexts a specific contextID is shared between.*/
105 static void incrementContextIDUsageCount(unsigned int contextID);
106
107 /** Decrement the usage count associate with a contextID. Once the contextID goes to 0 the contextID is then free to be reused.*/
108 static void decrementContextIDUsageCount(unsigned int contextID);
109
110 typedef GraphicsContext::GraphicsContexts GraphicsContexts;
111
112 /** Get all the registered graphics contexts.*/
113 static GraphicsContexts getAllRegisteredGraphicsContexts();
114
115 /** Get all the registered graphics contexts associated with a specific contextID.*/
116 static GraphicsContexts getRegisteredGraphicsContexts(unsigned int contextID);
117
118 /** Get the GraphicsContext for doing background compilation for GraphicsContexts associated with specified contextID.*/
119 static void setCompileContext(unsigned int contextID, GraphicsContext* gc);
120
121 /** Get existing or create a new GraphicsContext to do background compilation for GraphicsContexts associated with specified contextID.*/
122 static GraphicsContext* getOrCreateCompileContext(unsigned int contextID);
123
124 /** Get the GraphicsContext for doing background compilation for GraphicsContexts associated with specified contextID.*/
125 static GraphicsContext* getCompileContext(unsigned int contextID);
126
127 /** Register a GraphicsContext.*/
128 static void registerGraphicsContext(GraphicsContext* gc);
129
130 /** Unregister a GraphicsContext.*/
131 static void unregisterGraphicsContext(GraphicsContext* gc);
132
133 protected:
134 virtual ~ContextData();
135
136 unsigned int _numContexts;
137 osg::ref_ptr<osg::GraphicsContext> _compileContext;
138
139 // ManagerMap contains GL Extentsions objects used by StateAttribue to call OpenGL extensions/advanced features
140 typedef std::map<const std::type_info*, osg::ref_ptr<osg::Referenced> > ManagerMap;
141 ManagerMap _managerMap;
142};
143
144
145/** Get the ContextData for a specific contextID.*/
146extern OSG_EXPORT ContextData* getContextData(unsigned int contextID);
147
148/** Get or create the ContextData for a specific contextID.*/
149extern OSG_EXPORT ContextData* getOrCreateContextData(unsigned int contextID);
150
151template<typename T>
152inline T* get(unsigned int contextID)
153{
154 ContextData* gc = getOrCreateContextData(contextID);
155 return gc->get<T>();
156}
157
158// specialize for ContextData to avoid ContextData being nested within itself.
159template<> inline ContextData* get<ContextData>(unsigned int contextID) { return getOrCreateContextData(contextID); }
160
161}
162
163#endif