visual_server_raster.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*************************************************************************/
  2. /* visual_server_raster.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_raster.h"
  31. #include "default_mouse_cursor.xpm"
  32. #include "io/marshalls.h"
  33. #include "os/os.h"
  34. #include "project_settings.h"
  35. #include "sort.h"
  36. #include "visual_server_canvas.h"
  37. #include "visual_server_global.h"
  38. #include "visual_server_scene.h"
  39. // careful, these may run in different threads than the visual server
  40. int VisualServerRaster::changes = 0;
  41. /* BLACK BARS */
  42. void VisualServerRaster::black_bars_set_margins(int p_left, int p_top, int p_right, int p_bottom) {
  43. black_margin[MARGIN_LEFT] = p_left;
  44. black_margin[MARGIN_TOP] = p_top;
  45. black_margin[MARGIN_RIGHT] = p_right;
  46. black_margin[MARGIN_BOTTOM] = p_bottom;
  47. }
  48. void VisualServerRaster::black_bars_set_images(RID p_left, RID p_top, RID p_right, RID p_bottom) {
  49. black_image[MARGIN_LEFT] = p_left;
  50. black_image[MARGIN_TOP] = p_top;
  51. black_image[MARGIN_RIGHT] = p_right;
  52. black_image[MARGIN_BOTTOM] = p_bottom;
  53. }
  54. void VisualServerRaster::_draw_margins() {
  55. VSG::canvas_render->draw_window_margins(black_margin, black_image);
  56. };
  57. /* FREE */
  58. void VisualServerRaster::free(RID p_rid) {
  59. if (VSG::storage->free(p_rid))
  60. return;
  61. if (VSG::canvas->free(p_rid))
  62. return;
  63. if (VSG::viewport->free(p_rid))
  64. return;
  65. if (VSG::scene->free(p_rid))
  66. return;
  67. }
  68. /* EVENT QUEUING */
  69. void VisualServerRaster::request_frame_drawn_callback(Object *p_where, const StringName &p_method, const Variant &p_userdata) {
  70. ERR_FAIL_NULL(p_where);
  71. FrameDrawnCallbacks fdc;
  72. fdc.object = p_where->get_instance_id();
  73. fdc.method = p_method;
  74. fdc.param = p_userdata;
  75. frame_drawn_callbacks.push_back(fdc);
  76. }
  77. void VisualServerRaster::draw(bool p_swap_buffers) {
  78. changes = 0;
  79. VSG::rasterizer->begin_frame();
  80. VSG::scene->update_dirty_instances(); //update scene stuff
  81. VSG::viewport->draw_viewports();
  82. VSG::scene->render_probes();
  83. _draw_margins();
  84. VSG::rasterizer->end_frame(p_swap_buffers);
  85. while (frame_drawn_callbacks.front()) {
  86. Object *obj = ObjectDB::get_instance(frame_drawn_callbacks.front()->get().object);
  87. if (obj) {
  88. Variant::CallError ce;
  89. const Variant *v = &frame_drawn_callbacks.front()->get().param;
  90. obj->call(frame_drawn_callbacks.front()->get().method, &v, 1, ce);
  91. if (ce.error != Variant::CallError::CALL_OK) {
  92. String err = Variant::get_call_error_text(obj, frame_drawn_callbacks.front()->get().method, &v, 1, ce);
  93. ERR_PRINTS("Error calling frame drawn function: " + err);
  94. }
  95. }
  96. frame_drawn_callbacks.pop_front();
  97. }
  98. emit_signal("frame_drawn_in_thread");
  99. }
  100. void VisualServerRaster::sync() {
  101. }
  102. bool VisualServerRaster::has_changed() const {
  103. return changes > 0;
  104. }
  105. void VisualServerRaster::init() {
  106. VSG::rasterizer->initialize();
  107. }
  108. void VisualServerRaster::finish() {
  109. if (test_cube.is_valid()) {
  110. free(test_cube);
  111. }
  112. VSG::rasterizer->finalize();
  113. }
  114. /* STATUS INFORMATION */
  115. int VisualServerRaster::get_render_info(RenderInfo p_info) {
  116. return VSG::storage->get_render_info(p_info);
  117. }
  118. /* TESTING */
  119. void VisualServerRaster::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale) {
  120. redraw_request();
  121. VSG::rasterizer->set_boot_image(p_image, p_color, p_scale);
  122. }
  123. void VisualServerRaster::set_default_clear_color(const Color &p_color) {
  124. }
  125. bool VisualServerRaster::has_feature(Features p_feature) const {
  126. return false;
  127. }
  128. RID VisualServerRaster::get_test_cube() {
  129. if (!test_cube.is_valid()) {
  130. test_cube = _make_test_cube();
  131. }
  132. return test_cube;
  133. }
  134. bool VisualServerRaster::has_os_feature(const String &p_feature) const {
  135. return VSG::storage->has_os_feature(p_feature);
  136. }
  137. void VisualServerRaster::set_debug_generate_wireframes(bool p_generate) {
  138. VSG::storage->set_debug_generate_wireframes(p_generate);
  139. }
  140. void VisualServerRaster::call_set_use_vsync(bool p_enable) {
  141. OS::get_singleton()->_set_use_vsync(p_enable);
  142. }
  143. VisualServerRaster::VisualServerRaster() {
  144. VSG::canvas = memnew(VisualServerCanvas);
  145. VSG::viewport = memnew(VisualServerViewport);
  146. VSG::scene = memnew(VisualServerScene);
  147. VSG::rasterizer = Rasterizer::create();
  148. VSG::storage = VSG::rasterizer->get_storage();
  149. VSG::canvas_render = VSG::rasterizer->get_canvas();
  150. VSG::scene_render = VSG::rasterizer->get_scene();
  151. for (int i = 0; i < 4; i++)
  152. black_margin[i] = 0;
  153. }
  154. VisualServerRaster::~VisualServerRaster() {
  155. memdelete(VSG::canvas);
  156. memdelete(VSG::viewport);
  157. memdelete(VSG::rasterizer);
  158. memdelete(VSG::scene);
  159. }