CmCommandQueue.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "CmCommandQueue.h"
  2. #include "CmException.h"
  3. #include "CmRenderSystem.h"
  4. #include "CmDebug.h"
  5. namespace CamelotEngine
  6. {
  7. CommandQueue::CommandQueue(CM_THREAD_ID_TYPE threadId, bool allowAllThreads)
  8. :mMyThreadId(threadId), mAllowAllThreads(allowAllThreads)
  9. {
  10. mCommands = new vector<Command>::type();
  11. }
  12. CommandQueue::~CommandQueue()
  13. {
  14. #if CM_DEBUG_MODE
  15. #if CM_THREAD_SUPPORT != 0
  16. if(!mAllowAllThreads && CM_THREAD_CURRENT_ID != mMyThreadId)
  17. {
  18. CM_EXCEPT(InternalErrorException, "Command queue accessed outside of its creation thread.");
  19. }
  20. #endif
  21. #endif
  22. if(mCommands != nullptr)
  23. delete mCommands;
  24. }
  25. AsyncOp CommandQueue::queueReturn(boost::function<void(AsyncOp&)> commandCallback, bool _notifyWhenComplete, UINT32 _callbackId)
  26. {
  27. #if CM_DEBUG_MODE
  28. #if CM_THREAD_SUPPORT != 0
  29. if(!mAllowAllThreads && CM_THREAD_CURRENT_ID != mMyThreadId)
  30. {
  31. CM_EXCEPT(InternalErrorException, "Command queue accessed outside of its creation thread.");
  32. }
  33. #endif
  34. #endif
  35. Command newCommand(commandCallback, _notifyWhenComplete, _callbackId);
  36. mCommands->push_back(newCommand);
  37. return newCommand.asyncOp;
  38. }
  39. void CommandQueue::queue(boost::function<void()> commandCallback, bool _notifyWhenComplete, UINT32 _callbackId)
  40. {
  41. #if CM_DEBUG_MODE
  42. #if CM_THREAD_SUPPORT != 0
  43. if(!mAllowAllThreads && CM_THREAD_CURRENT_ID != mMyThreadId)
  44. {
  45. CM_EXCEPT(InternalErrorException, "Command queue accessed outside of its creation thread.");
  46. }
  47. #endif
  48. #endif
  49. Command newCommand(commandCallback, _notifyWhenComplete, _callbackId);
  50. mCommands->push_back(newCommand);
  51. }
  52. vector<CommandQueue::Command>::type* CommandQueue::flush()
  53. {
  54. vector<Command>::type* oldCommands = nullptr;
  55. {
  56. CM_LOCK_MUTEX(mCommandBufferMutex);
  57. oldCommands = mCommands;
  58. mCommands = new vector<Command>::type();
  59. }
  60. return oldCommands;
  61. }
  62. void CommandQueue::playback(vector<CommandQueue::Command>::type* commands, boost::function<void(UINT32)> notifyCallback)
  63. {
  64. #if CM_DEBUG_MODE
  65. RenderSystem* rs = RenderSystem::instancePtr();
  66. if(rs->getRenderThreadId() != CM_THREAD_CURRENT_ID)
  67. CM_EXCEPT(InternalErrorException, "This method should only be called from the render thread.");
  68. #endif
  69. if(commands == nullptr)
  70. return;
  71. for(auto iter = commands->begin(); iter != commands->end(); ++iter)
  72. {
  73. Command& command = (*iter);
  74. if(command.returnsValue)
  75. {
  76. command.callbackWithReturnValue(command.asyncOp);
  77. if(!command.asyncOp.hasCompleted())
  78. {
  79. LOGDBG("Async operation return value wasn't resolved properly. Resolving automatically to nullptr. " \
  80. "Make sure to complete the operation before returning from the command callback method.");
  81. command.asyncOp.completeOperation(nullptr);
  82. }
  83. }
  84. else
  85. {
  86. command.callback();
  87. }
  88. if(command.notifyWhenComplete && !notifyCallback.empty())
  89. {
  90. notifyCallback(command.callbackId);
  91. }
  92. }
  93. delete commands;
  94. }
  95. void CommandQueue::playback(vector<Command>::type* commands)
  96. {
  97. playback(commands, boost::function<void(UINT32)>());
  98. }
  99. bool CommandQueue::isEmpty()
  100. {
  101. CM_LOCK_MUTEX(mCommandBufferMutex);
  102. if(mCommands != nullptr && mCommands->size() > 0)
  103. return false;
  104. return true;
  105. }
  106. }