visual_server_wrap_mt.cpp 5.3 KB

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