visual_server_wrap_mt.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*************************************************************************/
  2. /* visual_server_wrap_mt.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "visual_server_wrap_mt.h"
  30. #include "os/os.h"
  31. void VisualServerWrapMT::thread_exit() {
  32. exit=true;
  33. }
  34. void VisualServerWrapMT::thread_draw() {
  35. draw_mutex->lock();
  36. draw_pending--;
  37. bool draw=(draw_pending==0);// only draw when no more flushes are pending
  38. draw_mutex->unlock();
  39. if (draw) {
  40. visual_server->draw();
  41. }
  42. }
  43. void VisualServerWrapMT::thread_flush() {
  44. draw_mutex->lock();
  45. draw_pending--;
  46. draw_mutex->unlock();
  47. }
  48. void VisualServerWrapMT::_thread_callback(void *_instance) {
  49. VisualServerWrapMT *vsmt = reinterpret_cast<VisualServerWrapMT*>(_instance);
  50. vsmt->thread_loop();
  51. }
  52. void VisualServerWrapMT::thread_loop() {
  53. server_thread=Thread::get_caller_ID();
  54. OS::get_singleton()->make_rendering_thread();
  55. visual_server->init();
  56. exit=false;
  57. draw_thread_up=true;
  58. while(!exit) {
  59. // flush commands one by one, until exit is requested
  60. command_queue.wait_and_flush_one();
  61. }
  62. command_queue.flush_all(); // flush all
  63. visual_server->finish();
  64. }
  65. /* EVENT QUEUING */
  66. void VisualServerWrapMT::flush() {
  67. if (create_thread) {
  68. ERR_FAIL_COND(!draw_mutex);
  69. draw_mutex->lock();
  70. draw_pending++; //cambiar por un saferefcount
  71. draw_mutex->unlock();
  72. command_queue.push( this, &VisualServerWrapMT::thread_flush);
  73. } else {
  74. command_queue.flush_all(); //flush all pending from other threads
  75. }
  76. }
  77. void VisualServerWrapMT::draw() {
  78. if (create_thread) {
  79. ERR_FAIL_COND(!draw_mutex);
  80. draw_mutex->lock();
  81. draw_pending++; //cambiar por un saferefcount
  82. draw_mutex->unlock();
  83. command_queue.push( this, &VisualServerWrapMT::thread_draw);
  84. } else {
  85. command_queue.flush_all(); //flush all pending from other threads
  86. visual_server->draw();
  87. }
  88. }
  89. void VisualServerWrapMT::init() {
  90. if (create_thread) {
  91. draw_mutex = Mutex::create();
  92. print_line("CREATING RENDER THREAD");
  93. OS::get_singleton()->release_rendering_thread();
  94. if (create_thread) {
  95. thread = Thread::create( _thread_callback, this );
  96. print_line("STARTING RENDER THREAD");
  97. }
  98. while(!draw_thread_up) {
  99. OS::get_singleton()->delay_usec(1000);
  100. }
  101. print_line("DONE RENDER THREAD");
  102. } else {
  103. visual_server->init();
  104. }
  105. }
  106. void VisualServerWrapMT::finish() {
  107. if (thread) {
  108. command_queue.push( this, &VisualServerWrapMT::thread_exit);
  109. Thread::wait_to_finish( thread );
  110. memdelete(thread);
  111. thread=NULL;
  112. } else {
  113. visual_server->finish();
  114. }
  115. if (draw_mutex)
  116. memdelete(draw_mutex);
  117. }
  118. VisualServerWrapMT::VisualServerWrapMT(VisualServer* p_contained,bool p_create_thread) : command_queue(p_create_thread) {
  119. visual_server=p_contained;
  120. create_thread=p_create_thread;
  121. thread=NULL;
  122. draw_mutex=NULL;
  123. draw_pending=0;
  124. draw_thread_up=false;
  125. if (!p_create_thread) {
  126. server_thread=Thread::get_caller_ID();
  127. } else {
  128. server_thread=0;
  129. }
  130. }
  131. VisualServerWrapMT::~VisualServerWrapMT() {
  132. memdelete(visual_server);
  133. //finish();
  134. }