RenderManager.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/RenderManager.h"
  29. #include "../../Include/RmlUi/Core/Core.h"
  30. #include "../../Include/RmlUi/Core/Geometry.h"
  31. #include "../../Include/RmlUi/Core/RenderInterface.h"
  32. namespace Rml {
  33. RenderManager::RenderManager() : render_interface(GetRenderInterface())
  34. {
  35. RMLUI_ASSERT(render_interface);
  36. }
  37. void RenderManager::BeginRender()
  38. {
  39. #ifdef RMLUI_DEBUG
  40. const RenderState default_state;
  41. RMLUI_ASSERT(state.clip_mask_list == default_state.clip_mask_list);
  42. RMLUI_ASSERT(state.scissor_region == state.scissor_region);
  43. RMLUI_ASSERT(state.transform == state.transform);
  44. #endif
  45. }
  46. void RenderManager::SetViewport(Vector2i dimensions)
  47. {
  48. viewport_dimensions = dimensions;
  49. }
  50. Vector2i RenderManager::GetViewport() const
  51. {
  52. return viewport_dimensions;
  53. }
  54. void RenderManager::DisableScissorRegion()
  55. {
  56. SetScissorRegion(Rectanglei::MakeInvalid());
  57. }
  58. void RenderManager::SetScissorRegion(Rectanglei new_region)
  59. {
  60. const bool old_scissor_enable = state.scissor_region.Valid();
  61. const bool new_scissor_enable = new_region.Valid();
  62. if (new_scissor_enable != old_scissor_enable)
  63. render_interface->EnableScissorRegion(new_scissor_enable);
  64. if (new_scissor_enable)
  65. {
  66. new_region.Intersect(Rectanglei::FromSize(viewport_dimensions));
  67. if (new_region != state.scissor_region)
  68. render_interface->SetScissorRegion(new_region.Left(), new_region.Top(), new_region.Width(), new_region.Height());
  69. }
  70. state.scissor_region = new_region;
  71. }
  72. void RenderManager::DisableClipMask()
  73. {
  74. if (!state.clip_mask_list.empty())
  75. {
  76. state.clip_mask_list.clear();
  77. ApplyClipMask(state.clip_mask_list);
  78. }
  79. }
  80. void RenderManager::SetClipMask(ClipMaskOperation operation, Geometry* geometry, Vector2f translation)
  81. {
  82. RMLUI_ASSERT(geometry);
  83. state.clip_mask_list = {ClipMaskGeometry{operation, geometry, translation, nullptr}};
  84. ApplyClipMask(state.clip_mask_list);
  85. }
  86. void RenderManager::SetClipMask(ClipMaskGeometryList in_clip_elements)
  87. {
  88. if (state.clip_mask_list != in_clip_elements)
  89. {
  90. state.clip_mask_list = std::move(in_clip_elements);
  91. ApplyClipMask(state.clip_mask_list);
  92. }
  93. }
  94. void RenderManager::SetTransform(const Matrix4f* p_new_transform)
  95. {
  96. static const Matrix4f identity_transform = Matrix4f::Identity();
  97. const Matrix4f& new_transform = (p_new_transform ? *p_new_transform : identity_transform);
  98. if (state.transform != new_transform)
  99. {
  100. render_interface->SetTransform(p_new_transform);
  101. state.transform = new_transform;
  102. }
  103. }
  104. void RenderManager::ApplyClipMask(const ClipMaskGeometryList& clip_elements)
  105. {
  106. const bool clip_mask_enabled = !clip_elements.empty();
  107. render_interface->EnableClipMask(clip_mask_enabled);
  108. if (clip_mask_enabled)
  109. {
  110. const Matrix4f initial_transform = state.transform;
  111. for (const ClipMaskGeometry& element_clip : clip_elements)
  112. {
  113. SetTransform(element_clip.transform);
  114. element_clip.geometry->RenderToClipMask(element_clip.operation, element_clip.absolute_offset);
  115. }
  116. // Apply the initially set transform in case it was changed.
  117. SetTransform(&initial_transform);
  118. }
  119. }
  120. void RenderManager::SetState(const RenderState& next)
  121. {
  122. SetScissorRegion(next.scissor_region);
  123. SetClipMask(next.clip_mask_list);
  124. SetTransform(&next.transform);
  125. }
  126. void RenderManager::ResetState()
  127. {
  128. SetState(RenderState{});
  129. }
  130. } // namespace Rml