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
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
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
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
62 template<typename T>
63 void set(T* ptr)
64 {
65 const std::type_info* id(&typeid(T));
66 _managerMap[id] = ptr;
67 }
68
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
78 virtual void flushDeletedGLObjects(double currentTime, double& availableTime);
79
83
86 virtual void deleteAllGLObjects();
87
93 virtual void discardAllGLObjects();
94
95 public:
96
99 static unsigned int createNewContextID();
100
102 static unsigned int getMaxContextID();
103
105 static void incrementContextIDUsageCount(unsigned int contextID);
106
108 static void decrementContextIDUsageCount(unsigned int contextID);
109
111
114
116 static GraphicsContexts getRegisteredGraphicsContexts(unsigned int contextID);
117
119 static void setCompileContext(unsigned int contextID, GraphicsContext* gc);
120
122 static GraphicsContext* getOrCreateCompileContext(unsigned int contextID);
123
125 static GraphicsContext* getCompileContext(unsigned int contextID);
126
129
132
133 protected:
134 virtual ~ContextData();
135
136 unsigned int _numContexts;
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;
142};
143
144
146extern OSG_EXPORT ContextData* getContextData(unsigned int contextID);
147
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
Definition ContextData:22
virtual void reportStats(std::ostream &out)
void setNumContexts(unsigned int numContexts)
Definition ContextData:29
virtual ~ContextData()
unsigned int getNumContexts() const
Definition ContextData:30
static void decrementContextIDUsageCount(unsigned int contextID)
void set(T *ptr)
Definition ContextData:63
unsigned int _numContexts
Definition ContextData:136
virtual void deleteAllGLObjects()
virtual void resetStats()
virtual void flushAllDeletedGLObjects()
virtual void recomputeStats(std::ostream &out) const
static void incrementContextIDUsageCount(unsigned int contextID)
static GraphicsContext * getCompileContext(unsigned int contextID)
std::map< const std::type_info *, osg::ref_ptr< osg::Referenced > > ManagerMap
Definition ContextData:140
const T * get() const
Definition ContextData:53
virtual void discardAllGLObjects()
osg::GraphicsContext * getCompileContext()
Definition ContextData:33
static void registerGraphicsContext(GraphicsContext *gc)
virtual void newFrame(osg::FrameStamp *)
virtual void flushDeletedGLObjects(double currentTime, double &availableTime)
ManagerMap _managerMap
Definition ContextData:141
osg::ref_ptr< osg::GraphicsContext > _compileContext
Definition ContextData:137
void setCompileContext(osg::GraphicsContext *gc)
Definition ContextData:32
static GraphicsContexts getAllRegisteredGraphicsContexts()
static void unregisterGraphicsContext(GraphicsContext *gc)
void decrementUsageCount()
Definition ContextData:27
static unsigned int createNewContextID()
static GraphicsContexts getRegisteredGraphicsContexts(unsigned int contextID)
static unsigned int getMaxContextID()
ContextData(unsigned int contextID)
T * get()
Definition ContextData:38
GraphicsContext::GraphicsContexts GraphicsContexts
Definition ContextData:110
static void setCompileContext(unsigned int contextID, GraphicsContext *gc)
static GraphicsContext * getOrCreateCompileContext(unsigned int contextID)
void incrementUsageCount()
Definition ContextData:26
Definition FrameStamp:35
Definition GraphicsContext:30
std::vector< GraphicsContext * > GraphicsContexts
Definition GraphicsContext:272
Definition GLObjects:57
Definition ref_ptr:32
T * get() const
Definition ref_ptr:117
author: Julien Valentin 2017 (mp3butcher@hotmail.com)
Definition AlphaFunc:19
ContextData * get< ContextData >(unsigned int contextID)
Definition ContextData:159
OSG_EXPORT ContextData * getContextData(unsigned int contextID)
OSG_EXPORT ContextData * getOrCreateContextData(unsigned int contextID)
T * get(unsigned int contextID)
Definition ContextData:152
#define OSG_EXPORT
Definition osg/Export:39