visual_server_canvas_helper.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**************************************************************************/
  2. /* visual_server_canvas_helper.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 VISUAL_SERVER_CANVAS_HELPER_H
  31. #define VISUAL_SERVER_CANVAS_HELPER_H
  32. #include "core/color.h"
  33. #include "core/fixed_array.h"
  34. #include "core/local_vector.h"
  35. #include "core/math/rect2.h"
  36. #include "core/rid.h"
  37. class MultiRect;
  38. class VisualServerCanvasHelper {
  39. public:
  40. struct State {
  41. RID item;
  42. RID texture;
  43. Color modulate;
  44. RID normal_map;
  45. uint32_t flags = 0;
  46. bool operator==(const State &p_state) const {
  47. return ((item == p_state.item) &&
  48. (texture == p_state.texture) &&
  49. (modulate == p_state.modulate) &&
  50. (normal_map == p_state.normal_map) &&
  51. (flags == p_state.flags));
  52. }
  53. bool operator!=(const State &p_state) const { return !(*this == p_state); }
  54. };
  55. private:
  56. // There is a single mutex for tilemaps, only one quadrant can be adding
  57. // at a time.
  58. static LocalVector<MultiRect> _tilemap_multirects;
  59. static Mutex _tilemap_mutex;
  60. public:
  61. static void tilemap_begin();
  62. static void tilemap_add_rect(RID p_canvas_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, RID p_normal_map = RID(), bool p_clip_uv = false);
  63. static void tilemap_end();
  64. static bool _multirect_enabled;
  65. };
  66. class MultiRect {
  67. friend class VisualServerCanvasHelper;
  68. public:
  69. enum { MAX_RECTS = 2048 };
  70. private:
  71. VisualServerCanvasHelper::State state;
  72. bool state_set = false;
  73. FixedArray<Rect2, MAX_RECTS, true> rects;
  74. FixedArray<Rect2, MAX_RECTS, true> sources;
  75. static uint32_t flags_from_rects(Rect2 &r_rect, Rect2 &r_source);
  76. bool overlaps(const Rect2 &p_rect) const {
  77. for (uint32_t n = 0; n < rects.size(); n++) {
  78. if (rects[n].intersects(p_rect)) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. bool add_pre_flipped(const Rect2 &p_rect, const Rect2 &p_src_rect);
  85. public:
  86. // Simple API
  87. void add_rect(RID p_canvas_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, RID p_normal_map = RID(), bool p_clip_uv = false);
  88. // Efficient API
  89. void begin(const VisualServerCanvasHelper::State &p_state);
  90. bool add(const Rect2 &p_rect, const Rect2 &p_src_rect, bool p_commit_on_flip_change = true);
  91. bool is_empty() const { return rects.is_empty(); }
  92. bool is_full() const { return rects.is_full(); }
  93. // Always called in destructor, but can be used explicitly
  94. // for multiple draws, or to time the flush.
  95. void flush();
  96. ~MultiRect() { flush(); }
  97. };
  98. #endif // VISUAL_SERVER_CANVAS_HELPER_H