web_main.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**************************************************************************/
  2. /* web_main.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "display_server_web.h"
  31. #include "godot_js.h"
  32. #include "os_web.h"
  33. #include "core/config/engine.h"
  34. #include "core/io/file_access.h"
  35. #include "core/io/resource_loader.h"
  36. #include "core/profiling/profiling.h"
  37. #include "main/main.h"
  38. #include "scene/main/scene_tree.h"
  39. #include "scene/main/window.h" // SceneTree only forward declares it.
  40. #ifdef TOOLS_ENABLED
  41. #include "editor/web_tools_editor_plugin.h"
  42. #endif
  43. #include <emscripten/emscripten.h>
  44. #include <cstdlib>
  45. static OS_Web *os = nullptr;
  46. #ifndef PROXY_TO_PTHREAD_ENABLED
  47. static uint64_t target_ticks = 0;
  48. #endif
  49. static bool main_started = false;
  50. static bool shutdown_complete = false;
  51. void exit_callback() {
  52. if (!shutdown_complete) {
  53. return; // Still waiting.
  54. }
  55. if (main_started) {
  56. Main::cleanup();
  57. main_started = false;
  58. }
  59. int exit_code = OS_Web::get_singleton()->get_exit_code();
  60. memdelete(os);
  61. os = nullptr;
  62. godot_cleanup_profiler();
  63. emscripten_force_exit(exit_code); // Exit runtime.
  64. }
  65. void cleanup_after_sync() {
  66. shutdown_complete = true;
  67. }
  68. void main_loop_callback() {
  69. #ifndef PROXY_TO_PTHREAD_ENABLED
  70. uint64_t current_ticks = os->get_ticks_usec();
  71. #endif
  72. bool force_draw = DisplayServerWeb::get_singleton()->check_size_force_redraw();
  73. if (force_draw) {
  74. Main::force_redraw();
  75. #ifndef PROXY_TO_PTHREAD_ENABLED
  76. } else if (current_ticks < target_ticks) {
  77. return; // Skip frame.
  78. #endif
  79. }
  80. #ifndef PROXY_TO_PTHREAD_ENABLED
  81. int max_fps = Engine::get_singleton()->get_max_fps();
  82. if (max_fps > 0) {
  83. if (current_ticks - target_ticks > 1000000) {
  84. // When the window loses focus, we stop getting updates and accumulate delay.
  85. // For this reason, if the difference is too big, we reset target ticks to the current ticks.
  86. target_ticks = current_ticks;
  87. }
  88. target_ticks += (uint64_t)(1000000 / max_fps);
  89. }
  90. #endif
  91. if (os->main_loop_iterate()) {
  92. emscripten_cancel_main_loop(); // Cancel current loop and set the cleanup one.
  93. emscripten_set_main_loop(exit_callback, -1, false);
  94. godot_js_os_finish_async(cleanup_after_sync);
  95. }
  96. }
  97. void print_web_header() {
  98. // Emscripten.
  99. char *emscripten_version_char = godot_js_emscripten_get_version();
  100. String emscripten_version = vformat("Emscripten %s", emscripten_version_char);
  101. // `free()` is used here because it's not memory that was allocated by Godot.
  102. free(emscripten_version_char);
  103. // Build features.
  104. String thread_support = OS::get_singleton()->has_feature("threads")
  105. ? "multi-threaded"
  106. : "single-threaded";
  107. String extensions_support = OS::get_singleton()->has_feature("web_extensions")
  108. ? "GDExtension support"
  109. : "no GDExtension support";
  110. Vector<String> build_configuration = { emscripten_version, thread_support, extensions_support };
  111. print_line(vformat("Build configuration: %s.", String(", ").join(build_configuration)));
  112. }
  113. /// When calling main, it is assumed FS is setup and synced.
  114. extern EMSCRIPTEN_KEEPALIVE int godot_web_main(int argc, char *argv[]) {
  115. godot_init_profiler();
  116. os = new OS_Web();
  117. #ifdef TOOLS_ENABLED
  118. WebToolsEditorPlugin::initialize();
  119. #endif
  120. // We must override main when testing is enabled
  121. TEST_MAIN_OVERRIDE
  122. Error err = Main::setup(argv[0], argc - 1, &argv[1]);
  123. // Proper shutdown in case of setup failure.
  124. if (err != OK) {
  125. // Will only exit after sync.
  126. emscripten_set_main_loop(exit_callback, -1, false);
  127. godot_js_os_finish_async(cleanup_after_sync);
  128. if (err == ERR_HELP) { // Returned by --help and --version, so success.
  129. return EXIT_SUCCESS;
  130. }
  131. return EXIT_FAILURE;
  132. }
  133. print_web_header();
  134. main_started = true;
  135. // Ease up compatibility.
  136. ResourceLoader::set_abort_on_missing_resources(false);
  137. int ret = Main::start();
  138. os->set_exit_code(ret);
  139. os->get_main_loop()->initialize();
  140. #ifdef TOOLS_ENABLED
  141. if (Engine::get_singleton()->is_project_manager_hint() && FileAccess::exists("/tmp/preload.zip")) {
  142. PackedStringArray ps;
  143. ps.push_back("/tmp/preload.zip");
  144. SceneTree::get_singleton()->get_root()->emit_signal(SNAME("files_dropped"), ps);
  145. }
  146. #endif
  147. emscripten_set_main_loop(main_loop_callback, -1, false);
  148. // Immediately run the first iteration.
  149. // We are inside an animation frame, we want to immediately draw on the newly setup canvas.
  150. main_loop_callback();
  151. return os->get_exit_code();
  152. }