visual_server_wrap_mt.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "core/os/os.h"
  32. #include "core/project_settings.h"
  33. #include "servers/display_server.h"
  34. void VisualServerWrapMT::thread_exit() {
  35. exit = true;
  36. }
  37. void VisualServerWrapMT::thread_draw(bool p_swap_buffers, double frame_step) {
  38. if (!atomic_decrement(&draw_pending)) {
  39. visual_server->draw(p_swap_buffers, frame_step);
  40. }
  41. }
  42. void VisualServerWrapMT::thread_flush() {
  43. atomic_decrement(&draw_pending);
  44. }
  45. void VisualServerWrapMT::_thread_callback(void *_instance) {
  46. VisualServerWrapMT *vsmt = reinterpret_cast<VisualServerWrapMT *>(_instance);
  47. vsmt->thread_loop();
  48. }
  49. void VisualServerWrapMT::thread_loop() {
  50. server_thread = Thread::get_caller_id();
  51. DisplayServer::get_singleton()->make_rendering_thread();
  52. visual_server->init();
  53. exit = false;
  54. draw_thread_up = true;
  55. while (!exit) {
  56. // flush commands one by one, until exit is requested
  57. command_queue.wait_and_flush_one();
  58. }
  59. command_queue.flush_all(); // flush all
  60. visual_server->finish();
  61. }
  62. /* EVENT QUEUING */
  63. void VisualServerWrapMT::sync() {
  64. if (create_thread) {
  65. atomic_increment(&draw_pending);
  66. command_queue.push_and_sync(this, &VisualServerWrapMT::thread_flush);
  67. } else {
  68. command_queue.flush_all(); //flush all pending from other threads
  69. }
  70. }
  71. void VisualServerWrapMT::draw(bool p_swap_buffers, double frame_step) {
  72. if (create_thread) {
  73. atomic_increment(&draw_pending);
  74. command_queue.push(this, &VisualServerWrapMT::thread_draw, p_swap_buffers, frame_step);
  75. } else {
  76. visual_server->draw(p_swap_buffers, frame_step);
  77. }
  78. }
  79. void VisualServerWrapMT::init() {
  80. if (create_thread) {
  81. print_verbose("VisualServerWrapMT: Creating render thread");
  82. DisplayServer::get_singleton()->release_rendering_thread();
  83. if (create_thread) {
  84. thread = Thread::create(_thread_callback, this);
  85. print_verbose("VisualServerWrapMT: Starting render thread");
  86. }
  87. while (!draw_thread_up) {
  88. OS::get_singleton()->delay_usec(1000);
  89. }
  90. print_verbose("VisualServerWrapMT: Finished render thread");
  91. } else {
  92. visual_server->init();
  93. }
  94. }
  95. void VisualServerWrapMT::finish() {
  96. if (thread) {
  97. command_queue.push(this, &VisualServerWrapMT::thread_exit);
  98. Thread::wait_to_finish(thread);
  99. memdelete(thread);
  100. thread = NULL;
  101. } else {
  102. visual_server->finish();
  103. }
  104. sky_free_cached_ids();
  105. shader_free_cached_ids();
  106. material_free_cached_ids();
  107. mesh_free_cached_ids();
  108. multimesh_free_cached_ids();
  109. immediate_free_cached_ids();
  110. skeleton_free_cached_ids();
  111. directional_light_free_cached_ids();
  112. omni_light_free_cached_ids();
  113. spot_light_free_cached_ids();
  114. reflection_probe_free_cached_ids();
  115. gi_probe_free_cached_ids();
  116. lightmap_capture_free_cached_ids();
  117. particles_free_cached_ids();
  118. camera_free_cached_ids();
  119. viewport_free_cached_ids();
  120. environment_free_cached_ids();
  121. camera_effects_free_cached_ids();
  122. scenario_free_cached_ids();
  123. instance_free_cached_ids();
  124. canvas_free_cached_ids();
  125. canvas_item_free_cached_ids();
  126. canvas_light_occluder_free_cached_ids();
  127. canvas_occluder_polygon_free_cached_ids();
  128. }
  129. void VisualServerWrapMT::set_use_vsync_callback(bool p_enable) {
  130. singleton_mt->call_set_use_vsync(p_enable);
  131. }
  132. VisualServerWrapMT *VisualServerWrapMT::singleton_mt = NULL;
  133. VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_thread) :
  134. command_queue(p_create_thread) {
  135. singleton_mt = this;
  136. DisplayServer::switch_vsync_function = set_use_vsync_callback; //as this goes to another thread, make sure it goes properly
  137. visual_server = p_contained;
  138. create_thread = p_create_thread;
  139. thread = NULL;
  140. draw_pending = 0;
  141. draw_thread_up = false;
  142. pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc");
  143. if (!p_create_thread) {
  144. server_thread = Thread::get_caller_id();
  145. } else {
  146. server_thread = 0;
  147. }
  148. }
  149. VisualServerWrapMT::~VisualServerWrapMT() {
  150. memdelete(visual_server);
  151. //finish();
  152. }