1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14#ifndef OSG_CONTEXTDATA
15#define OSG_CONTEXTDATA 1
17#include <osg/GraphicsContext>
21class OSG_EXPORT ContextData : public GraphicsObjectManager
24 ContextData(unsigned int contextID);
26 void incrementUsageCount() { ++_numContexts; }
27 void decrementUsageCount() { --_numContexts; }
29 void setNumContexts(unsigned int numContexts) { _numContexts = numContexts; }
30 unsigned int getNumContexts() const { return _numContexts; }
32 void setCompileContext(osg::GraphicsContext* gc) { _compileContext = gc; }
33 osg::GraphicsContext* getCompileContext() { return _compileContext.get(); }
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. */
40 const std::type_info* id(&typeid(T));
41 osg::ref_ptr<osg::Referenced>& ptr = _managerMap[id];
44 ptr = new T(_contextID);
46 return static_cast<T*>(ptr.get());
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.*/
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();
61 /** Set a specific GL extensions object pr GraphicsObjectManager. */
65 const std::type_info* id(&typeid(T));
66 _managerMap[id] = ptr;
69 /** Signal that a new frame has started.*/
70 virtual void newFrame(osg::FrameStamp*);
72 virtual void resetStats();
73 virtual void reportStats(std::ostream& out);
74 virtual void recomputeStats(std::ostream& out) const;
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);
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();
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();
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();
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();
101 /** Get the current max ContextID.*/
102 static unsigned int getMaxContextID();
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);
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);
110 typedef GraphicsContext::GraphicsContexts GraphicsContexts;
112 /** Get all the registered graphics contexts.*/
113 static GraphicsContexts getAllRegisteredGraphicsContexts();
115 /** Get all the registered graphics contexts associated with a specific contextID.*/
116 static GraphicsContexts getRegisteredGraphicsContexts(unsigned int contextID);
118 /** Get the GraphicsContext for doing background compilation for GraphicsContexts associated with specified contextID.*/
119 static void setCompileContext(unsigned int contextID, GraphicsContext* gc);
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);
124 /** Get the GraphicsContext for doing background compilation for GraphicsContexts associated with specified contextID.*/
125 static GraphicsContext* getCompileContext(unsigned int contextID);
127 /** Register a GraphicsContext.*/
128 static void registerGraphicsContext(GraphicsContext* gc);
130 /** Unregister a GraphicsContext.*/
131 static void unregisterGraphicsContext(GraphicsContext* gc);
134 virtual ~ContextData();
136 unsigned int _numContexts;
137 osg::ref_ptr<osg::GraphicsContext> _compileContext;
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;
145/** Get the ContextData for a specific contextID.*/
146extern OSG_EXPORT ContextData* getContextData(unsigned int contextID);
148/** Get or create the ContextData for a specific contextID.*/
149extern OSG_EXPORT ContextData* getOrCreateContextData(unsigned int contextID);
152inline T* get(unsigned int contextID)
154 ContextData* gc = getOrCreateContextData(contextID);
158// specialize for ContextData to avoid ContextData being nested within itself.
159template<> inline ContextData* get<ContextData>(unsigned int contextID) { return getOrCreateContextData(contextID); }