CallbackTexture.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "../../Include/RmlUi/Core/CallbackTexture.h"
  2. #include "../../Include/RmlUi/Core/Texture.h"
  3. #include "RenderManagerAccess.h"
  4. namespace Rml {
  5. void CallbackTexture::Release()
  6. {
  7. if (resource_handle != StableVectorIndex::Invalid)
  8. {
  9. RenderManagerAccess::ReleaseResource(render_manager, *this);
  10. Clear();
  11. }
  12. }
  13. Rml::CallbackTexture::operator Texture() const
  14. {
  15. return Texture(render_manager, resource_handle);
  16. }
  17. CallbackTextureInterface::CallbackTextureInterface(RenderManager& render_manager, RenderInterface& render_interface, TextureHandle& texture_handle,
  18. Vector2i& dimensions) : render_manager(render_manager), render_interface(render_interface), texture_handle(texture_handle), dimensions(dimensions)
  19. {}
  20. bool CallbackTextureInterface::GenerateTexture(Span<const byte> source, Vector2i new_dimensions) const
  21. {
  22. if (texture_handle)
  23. {
  24. RMLUI_ERRORMSG("Texture already set");
  25. return false;
  26. }
  27. texture_handle = render_interface.GenerateTexture(source, new_dimensions);
  28. if (texture_handle)
  29. dimensions = new_dimensions;
  30. return texture_handle != TextureHandle{};
  31. }
  32. void CallbackTextureInterface::SaveLayerAsTexture() const
  33. {
  34. if (texture_handle)
  35. {
  36. RMLUI_ERRORMSG("Texture already set");
  37. return;
  38. }
  39. const Rectanglei region = render_manager.GetScissorRegion();
  40. if (!region.Valid())
  41. {
  42. RMLUI_ERRORMSG("Save layer as texture requires a scissor region to be set first");
  43. return;
  44. }
  45. texture_handle = render_interface.SaveLayerAsTexture();
  46. if (texture_handle)
  47. dimensions = region.Size();
  48. }
  49. void CallbackTextureInterface::SetTextureHandle(TextureHandle handle, Vector2i new_dimensions) const
  50. {
  51. if (texture_handle)
  52. {
  53. RMLUI_ERRORMSG("Texture already set");
  54. return;
  55. }
  56. texture_handle = handle;
  57. dimensions = new_dimensions;
  58. }
  59. RenderManager& CallbackTextureInterface::GetRenderManager() const
  60. {
  61. return render_manager;
  62. }
  63. CallbackTextureSource::CallbackTextureSource(CallbackTextureFunction&& callback) : callback(std::move(callback)) {}
  64. CallbackTextureSource::CallbackTextureSource(CallbackTextureSource&& other) noexcept :
  65. callback(std::move(other.callback)), textures(std::move(other.textures))
  66. {}
  67. CallbackTextureSource& CallbackTextureSource::operator=(CallbackTextureSource&& other) noexcept
  68. {
  69. callback = std::move(other.callback);
  70. textures = std::move(other.textures);
  71. return *this;
  72. }
  73. Texture CallbackTextureSource::GetTexture(RenderManager& render_manager) const
  74. {
  75. CallbackTexture& texture = textures[&render_manager];
  76. if (!texture)
  77. texture = render_manager.MakeCallbackTexture(callback);
  78. return Texture(texture);
  79. }
  80. } // namespace Rml