visual_server_wrap_mt.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*************************************************************************/
  2. /* visual_server_wrap_mt.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 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. #include "project_settings.h"
  32. void VisualServerWrapMT::thread_exit() {
  33. exit = true;
  34. }
  35. void VisualServerWrapMT::thread_draw() {
  36. draw_mutex->lock();
  37. draw_pending--;
  38. bool draw = (draw_pending == 0); // only draw when no more flushes are pending
  39. draw_mutex->unlock();
  40. if (draw) {
  41. visual_server->draw();
  42. }
  43. }
  44. void VisualServerWrapMT::thread_flush() {
  45. draw_mutex->lock();
  46. draw_pending--;
  47. draw_mutex->unlock();
  48. }
  49. void VisualServerWrapMT::_thread_callback(void *_instance) {
  50. VisualServerWrapMT *vsmt = reinterpret_cast<VisualServerWrapMT *>(_instance);
  51. vsmt->thread_loop();
  52. }
  53. void VisualServerWrapMT::thread_loop() {
  54. server_thread = Thread::get_caller_id();
  55. OS::get_singleton()->make_rendering_thread();
  56. visual_server->init();
  57. exit = false;
  58. draw_thread_up = true;
  59. while (!exit) {
  60. // flush commands one by one, until exit is requested
  61. command_queue.wait_and_flush_one();
  62. }
  63. command_queue.flush_all(); // flush all
  64. visual_server->finish();
  65. }
  66. /* EVENT QUEUING */
  67. void VisualServerWrapMT::sync() {
  68. if (create_thread) {
  69. /* TODO: sync with the thread */
  70. /*
  71. ERR_FAIL_COND(!draw_mutex);
  72. draw_mutex->lock();
  73. draw_pending++; //cambiar por un saferefcount
  74. draw_mutex->unlock();
  75. */
  76. //command_queue.push( this, &VisualServerWrapMT::thread_flush);
  77. } else {
  78. command_queue.flush_all(); //flush all pending from other threads
  79. }
  80. }
  81. void VisualServerWrapMT::draw() {
  82. if (create_thread) {
  83. /* TODO: Make it draw
  84. ERR_FAIL_COND(!draw_mutex);
  85. draw_mutex->lock();
  86. draw_pending++; //cambiar por un saferefcount
  87. draw_mutex->unlock();
  88. command_queue.push( this, &VisualServerWrapMT::thread_draw);
  89. */
  90. } else {
  91. visual_server->draw();
  92. }
  93. }
  94. void VisualServerWrapMT::init() {
  95. if (create_thread) {
  96. draw_mutex = Mutex::create();
  97. print_line("CREATING RENDER THREAD");
  98. OS::get_singleton()->release_rendering_thread();
  99. if (create_thread) {
  100. thread = Thread::create(_thread_callback, this);
  101. print_line("STARTING RENDER THREAD");
  102. }
  103. while (!draw_thread_up) {
  104. OS::get_singleton()->delay_usec(1000);
  105. }
  106. print_line("DONE RENDER THREAD");
  107. } else {
  108. visual_server->init();
  109. }
  110. }
  111. void VisualServerWrapMT::finish() {
  112. if (thread) {
  113. command_queue.push(this, &VisualServerWrapMT::thread_exit);
  114. Thread::wait_to_finish(thread);
  115. memdelete(thread);
  116. texture_free_cached_ids();
  117. //mesh_free_cached_ids();
  118. thread = NULL;
  119. } else {
  120. visual_server->finish();
  121. }
  122. if (draw_mutex)
  123. memdelete(draw_mutex);
  124. }
  125. VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_thread)
  126. : command_queue(p_create_thread) {
  127. visual_server = p_contained;
  128. create_thread = p_create_thread;
  129. thread = NULL;
  130. draw_mutex = NULL;
  131. draw_pending = 0;
  132. draw_thread_up = false;
  133. alloc_mutex = Mutex::create();
  134. pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc");
  135. if (!p_create_thread) {
  136. server_thread = Thread::get_caller_id();
  137. } else {
  138. server_thread = 0;
  139. }
  140. }
  141. VisualServerWrapMT::~VisualServerWrapMT() {
  142. memdelete(visual_server);
  143. memdelete(alloc_mutex);
  144. //finish();
  145. }