GraphicsThread 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_GRAPHICSTHREAD
  14. #define OSG_GRAPHICSTHREAD 1
  15. #include <osg/OperationThread>
  16. #include <osg/State>
  17. namespace osg {
  18. class GraphicsContext;
  19. /** GraphicsThread is a helper class for running OpenGL GraphicsOperation within a single thread assigned to a specific GraphicsContext.*/
  20. class OSG_EXPORT GraphicsThread : public osg::OperationThread
  21. {
  22. public:
  23. GraphicsThread();
  24. /** Run does the graphics thread run loop.*/
  25. virtual void run();
  26. };
  27. struct OSG_EXPORT GraphicsOperation : public Operation
  28. {
  29. GraphicsOperation(const std::string& name, bool keep):
  30. Operation(name,keep) {}
  31. /** Override the standard Operation operator and dynamic cast object to a GraphicsContext,
  32. * on success call operation()(GraphicsContext*).*/
  33. virtual void operator () (Object* object);
  34. virtual void operator () (GraphicsContext* context) = 0;
  35. /** Resize any per context GLObject buffers to specified size. */
  36. virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
  37. /** If State is non-zero, this function releases any associated OpenGL objects for
  38. * the specified graphics context. Otherwise, releases OpenGL objects
  39. * for all graphics contexts. */
  40. virtual void releaseGLObjects(osg::State* = 0) const {}
  41. };
  42. /** SwapBufferOperation calls swap buffers on the GraphicsContext.*/
  43. struct OSG_EXPORT SwapBuffersOperation : public GraphicsOperation
  44. {
  45. SwapBuffersOperation():
  46. osg::Referenced(true),
  47. GraphicsOperation("SwapBuffers",true) {}
  48. virtual void operator () (GraphicsContext* context);
  49. };
  50. /** BarrierOperation allows one to synchronize multiple GraphicsThreads with each other.*/
  51. struct OSG_EXPORT BarrierOperation : public Operation, public OpenThreads::Barrier
  52. {
  53. enum PreBlockOp
  54. {
  55. NO_OPERATION,
  56. GL_FLUSH,
  57. GL_FINISH
  58. };
  59. BarrierOperation(int numThreads, PreBlockOp op=NO_OPERATION, bool keep=true):
  60. osg::Referenced(true),
  61. Operation("Barrier", keep),
  62. OpenThreads::Barrier(numThreads),
  63. _preBlockOp(op) {}
  64. virtual void release();
  65. virtual void operator () (Object* object);
  66. PreBlockOp _preBlockOp;
  67. };
  68. /** ReleaseContext_Block_MakeCurrentOperation releases the context for another thread to acquire,
  69. * then blocks waiting for context to be released, once the block is release the context is re-acquired.*/
  70. struct OSG_EXPORT ReleaseContext_Block_MakeCurrentOperation : public GraphicsOperation, public RefBlock
  71. {
  72. ReleaseContext_Block_MakeCurrentOperation():
  73. osg::Referenced(true),
  74. GraphicsOperation("ReleaseContext_Block_MakeCurrent", false) {}
  75. virtual void release();
  76. virtual void operator () (GraphicsContext* context);
  77. };
  78. struct OSG_EXPORT BlockAndFlushOperation : public GraphicsOperation, public OpenThreads::Block
  79. {
  80. BlockAndFlushOperation();
  81. virtual void release();
  82. virtual void operator () (GraphicsContext*);
  83. };
  84. struct OSG_EXPORT FlushDeletedGLObjectsOperation : public GraphicsOperation
  85. {
  86. FlushDeletedGLObjectsOperation(double availableTime, bool keep=false);
  87. virtual void operator () (GraphicsContext*);
  88. double _availableTime;
  89. };
  90. class OSG_EXPORT RunOperations : public osg::GraphicsOperation
  91. {
  92. public:
  93. RunOperations():
  94. osg::GraphicsOperation("RunOperation",true) {}
  95. virtual void operator () (osg::GraphicsContext* context);
  96. };
  97. class OSG_EXPORT EndOfDynamicDrawBlock : public OpenThreads::BlockCount, public osg::State::DynamicObjectRenderingCompletedCallback
  98. {
  99. public:
  100. EndOfDynamicDrawBlock(unsigned int);
  101. void completed(osg::State* state);
  102. protected:
  103. ~EndOfDynamicDrawBlock() {}
  104. };
  105. }
  106. #endif