editor_profiler.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**************************************************************************/
  2. /* editor_profiler.h */
  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. #ifndef EDITOR_PROFILER_H
  31. #define EDITOR_PROFILER_H
  32. #include "scene/gui/box_container.h"
  33. #include "scene/gui/button.h"
  34. #include "scene/gui/check_button.h"
  35. #include "scene/gui/label.h"
  36. #include "scene/gui/option_button.h"
  37. #include "scene/gui/spin_box.h"
  38. #include "scene/gui/split_container.h"
  39. #include "scene/gui/texture_rect.h"
  40. #include "scene/gui/tree.h"
  41. class ImageTexture;
  42. class EditorProfiler : public VBoxContainer {
  43. GDCLASS(EditorProfiler, VBoxContainer);
  44. public:
  45. struct Metric {
  46. bool valid = false;
  47. int frame_number = 0;
  48. float frame_time = 0;
  49. float process_time = 0;
  50. float physics_time = 0;
  51. float physics_frame_time = 0;
  52. struct Category {
  53. StringName signature;
  54. String name;
  55. float total_time = 0; //total for category
  56. struct Item {
  57. StringName signature;
  58. String name;
  59. String script;
  60. int line = 0;
  61. float self = 0;
  62. float total = 0;
  63. float internal = 0;
  64. int calls = 0;
  65. };
  66. Vector<Item> items;
  67. };
  68. Vector<Category> categories;
  69. HashMap<StringName, Category *> category_ptrs;
  70. HashMap<StringName, Category::Item *> item_ptrs;
  71. };
  72. enum DisplayMode {
  73. DISPLAY_FRAME_TIME,
  74. DISPLAY_AVERAGE_TIME,
  75. DISPLAY_FRAME_PERCENT,
  76. DISPLAY_PHYSICS_FRAME_PERCENT,
  77. };
  78. enum DisplayTime {
  79. DISPLAY_TOTAL_TIME,
  80. DISPLAY_SELF_TIME,
  81. };
  82. private:
  83. struct ThemeCache {
  84. Color seek_line_color;
  85. Color seek_line_hover_color;
  86. } theme_cache;
  87. Button *activate = nullptr;
  88. Button *clear_button = nullptr;
  89. TextureRect *graph = nullptr;
  90. Ref<ImageTexture> graph_texture;
  91. Vector<uint8_t> graph_image;
  92. Tree *variables = nullptr;
  93. HSplitContainer *h_split = nullptr;
  94. HashSet<StringName> plot_sigs;
  95. OptionButton *display_mode = nullptr;
  96. OptionButton *display_time = nullptr;
  97. CheckButton *display_internal_profiles = nullptr;
  98. SpinBox *cursor_metric_edit = nullptr;
  99. Vector<Metric> frame_metrics;
  100. int total_metrics = 0;
  101. int last_metric = -1;
  102. int max_functions = 0;
  103. bool updating_frame = false;
  104. int hover_metric = -1;
  105. float graph_height = 1.0f;
  106. bool seeking = false;
  107. Timer *frame_delay = nullptr;
  108. Timer *plot_delay = nullptr;
  109. void _update_button_text();
  110. void _update_frame();
  111. void _activate_pressed();
  112. void _clear_pressed();
  113. void _autostart_toggled(bool p_toggled_on);
  114. void _internal_profiles_pressed();
  115. String _get_time_as_text(const Metric &m, float p_time, int p_calls);
  116. void _make_metric_ptrs(Metric &m);
  117. void _item_edited();
  118. void _update_plot();
  119. void _graph_tex_mouse_exit();
  120. void _graph_tex_draw();
  121. void _graph_tex_input(const Ref<InputEvent> &p_ev);
  122. Color _get_color_from_signature(const StringName &p_signature) const;
  123. void _cursor_metric_changed(double);
  124. void _combo_changed(int);
  125. Metric _get_frame_metric(int index);
  126. protected:
  127. void _notification(int p_what);
  128. static void _bind_methods();
  129. public:
  130. void add_frame_metric(const Metric &p_metric, bool p_final = false);
  131. void set_enabled(bool p_enable, bool p_clear = true);
  132. void set_profiling(bool p_pressed);
  133. bool is_profiling();
  134. bool is_seeking() { return seeking; }
  135. void disable_seeking();
  136. void clear();
  137. Vector<Vector<String>> get_data_as_csv() const;
  138. EditorProfiler();
  139. };
  140. #endif // EDITOR_PROFILER_H