ContextData 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #ifndef OSG_CONTEXTDATA
  14. #define OSG_CONTEXTDATA 1
  15. #include <osg/GraphicsContext>
  16. namespace osg {
  17. class OSG_EXPORT ContextData : public GraphicsObjectManager
  18. {
  19. public:
  20. ContextData(unsigned int contextID);
  21. void incrementUsageCount() { ++_numContexts; }
  22. void decrementUsageCount() { --_numContexts; }
  23. void setNumContexts(unsigned int numContexts) { _numContexts = numContexts; }
  24. unsigned int getNumContexts() const { return _numContexts; }
  25. void setCompileContext(osg::GraphicsContext* gc) { _compileContext = gc; }
  26. osg::GraphicsContext* getCompileContext() { return _compileContext.get(); }
  27. /** Get a specific GL extensions object or GraphicsObjectManager, initialize if not already present.
  28. * Note, must only be called from a the graphics context thread associated with this osg::State. */
  29. template<typename T>
  30. T* get()
  31. {
  32. const std::type_info* id(&typeid(T));
  33. osg::ref_ptr<osg::Referenced>& ptr = _managerMap[id];
  34. if (!ptr)
  35. {
  36. ptr = new T(_contextID);
  37. }
  38. return static_cast<T*>(ptr.get());
  39. }
  40. /** Get a specific GL extensions object or GraphicsObjectManager if it already exists in the extension map.
  41. * Note, safe to call outwith a the graphics context thread associated with this osg::State.
  42. * Returns NULL if the desired extension object has not been created yet.*/
  43. template<typename T>
  44. const T* get() const
  45. {
  46. const std::type_info* id(&typeid(T));
  47. ManagerMap::const_iterator itr = _managerMap.find(id);
  48. if (itr==_managerMap.end()) return 0;
  49. else return itr->second.get();
  50. }
  51. /** Set a specific GL extensions object pr GraphicsObjectManager. */
  52. template<typename T>
  53. void set(T* ptr)
  54. {
  55. const std::type_info* id(&typeid(T));
  56. _managerMap[id] = ptr;
  57. }
  58. /** Signal that a new frame has started.*/
  59. virtual void newFrame(osg::FrameStamp*);
  60. virtual void resetStats();
  61. virtual void reportStats(std::ostream& out);
  62. virtual void recomputeStats(std::ostream& out) const;
  63. /** Flush all deleted OpenGL objects within the specified availableTime.
  64. * Note, must be called from a thread which has current the graphics context associated with contextID. */
  65. virtual void flushDeletedGLObjects(double currentTime, double& availableTime);
  66. /** Flush all deleted OpenGL objects.
  67. * Note, must be called from a thread which has current the graphics context associated with contextID. */
  68. virtual void flushAllDeletedGLObjects();
  69. /** Do a GL delete all OpenGL objects.
  70. * Note, must be called from a thread which has current the graphics context associated with contextID. */
  71. virtual void deleteAllGLObjects();
  72. /** Discard all OpenGL objects.
  73. * Note, unlike deleteAllGLjects discard does not
  74. * do any OpenGL calls so can be called from any thread, but as a consequence it
  75. * also doesn't remove the associated OpenGL resource so discard should only be
  76. * called when the associated graphics context is being/has been closed. */
  77. virtual void discardAllGLObjects();
  78. public:
  79. /** Create a contextID for a new graphics context, this contextID is used to set up the osg::State associate with context.
  80. * Automatically increments the usage count of the contextID to 1.*/
  81. static unsigned int createNewContextID();
  82. /** Get the current max ContextID.*/
  83. static unsigned int getMaxContextID();
  84. /** Increment the usage count associate with a contextID. The usage count specifies how many graphics contexts a specific contextID is shared between.*/
  85. static void incrementContextIDUsageCount(unsigned int contextID);
  86. /** Decrement the usage count associate with a contextID. Once the contextID goes to 0 the contextID is then free to be reused.*/
  87. static void decrementContextIDUsageCount(unsigned int contextID);
  88. typedef GraphicsContext::GraphicsContexts GraphicsContexts;
  89. /** Get all the registered graphics contexts.*/
  90. static GraphicsContexts getAllRegisteredGraphicsContexts();
  91. /** Get all the registered graphics contexts associated with a specific contextID.*/
  92. static GraphicsContexts getRegisteredGraphicsContexts(unsigned int contextID);
  93. /** Get the GraphicsContext for doing background compilation for GraphicsContexts associated with specified contextID.*/
  94. static void setCompileContext(unsigned int contextID, GraphicsContext* gc);
  95. /** Get existing or create a new GraphicsContext to do background compilation for GraphicsContexts associated with specified contextID.*/
  96. static GraphicsContext* getOrCreateCompileContext(unsigned int contextID);
  97. /** Get the GraphicsContext for doing background compilation for GraphicsContexts associated with specified contextID.*/
  98. static GraphicsContext* getCompileContext(unsigned int contextID);
  99. /** Register a GraphicsContext.*/
  100. static void registerGraphicsContext(GraphicsContext* gc);
  101. /** Unregister a GraphicsContext.*/
  102. static void unregisterGraphicsContext(GraphicsContext* gc);
  103. protected:
  104. virtual ~ContextData();
  105. unsigned int _numContexts;
  106. osg::ref_ptr<osg::GraphicsContext> _compileContext;
  107. // ManagerMap contains GL Extentsions objects used by StateAttribue to call OpenGL extensions/advanced features
  108. typedef std::map<const std::type_info*, osg::ref_ptr<osg::Referenced> > ManagerMap;
  109. ManagerMap _managerMap;
  110. };
  111. /** Get the ContextData for a specific contextID.*/
  112. extern OSG_EXPORT ContextData* getContextData(unsigned int contextID);
  113. /** Get or create the ContextData for a specific contextID.*/
  114. extern OSG_EXPORT ContextData* getOrCreateContextData(unsigned int contextID);
  115. template<typename T>
  116. inline T* get(unsigned int contextID)
  117. {
  118. ContextData* gc = getOrCreateContextData(contextID);
  119. return gc->get<T>();
  120. }
  121. // specialize for ContextData to avoid ContextData being nested within itself.
  122. template<> inline ContextData* get<ContextData>(unsigned int contextID) { return getOrCreateContextData(contextID); }
  123. }
  124. #endif