CWallpaper.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include <GL/glew.h>
  3. #include <GLFW/glfw3.h>
  4. #include <filesystem>
  5. #include <limits>
  6. #include <optional>
  7. #include "WallpaperEngine/Audio/AudioContext.h"
  8. #include "WallpaperEngine/Render/CFBO.h"
  9. #include "WallpaperEngine/Render/Helpers/ContextAware.h"
  10. #include "WallpaperEngine/Render/RenderContext.h"
  11. #include "WallpaperEngine/Data/Model/Wallpaper.h"
  12. #include "WallpaperEngine/Media/MediaSource.h"
  13. #include "FBOProvider.h"
  14. #include "WallpaperState.h"
  15. namespace WallpaperEngine::Application {
  16. class WallpaperApplication;
  17. }
  18. namespace WallpaperEngine::Render {
  19. namespace Helpers {
  20. class ContextAware;
  21. }
  22. using namespace WallpaperEngine::Render;
  23. using namespace WallpaperEngine::Audio;
  24. using namespace WallpaperEngine::Data::Model;
  25. using namespace WallpaperEngine::FileSystem;
  26. class CWallpaper : public Helpers::ContextAware, public FBOProvider, public TypeCaster {
  27. friend class WallpaperEngine::Application::WallpaperApplication;
  28. public:
  29. /** One wallpaper shared and rendered across multiple viewports */
  30. struct SpanInfo {
  31. /** Bounding box of the entire span group (x, y, width, height) in global desktop coordinates */
  32. glm::ivec4 totalBounds;
  33. };
  34. virtual ~CWallpaper () override;
  35. void render (
  36. const glm::ivec4& viewport, const bool vflip, const glm::ivec2& globalPosition = { 0, 0 },
  37. const glm::ivec2& logicalSize = { 0, 0 }
  38. );
  39. virtual void setPause (bool newState);
  40. [[nodiscard]] const AssetLocator& getAssetLocator () const;
  41. AudioContext& getAudioContext () const;
  42. [[nodiscard]] const WallpaperState& getState () const;
  43. /** Changes the scaling mode live, without reloading the wallpaper (used by the hotswap control file) */
  44. void setScalingMode (WallpaperState::TextureUVsScaling mode);
  45. /** Changes the manual zoom factor live, without reloading the wallpaper (used by the hotswap control file) */
  46. void setZoom (float zoom);
  47. /**
  48. * Changes the color shown outside the wallpaper's bounds (Center/Fit letterboxing, zoomed-out
  49. * scaling) live, without reloading the wallpaper. Only visible when the clamp mode is border
  50. * (the default) rather than clamp/repeat.
  51. */
  52. void setCornerColor (const glm::vec4& color);
  53. [[nodiscard]] virtual GLuint getWallpaperFramebuffer () const;
  54. [[nodiscard]] virtual GLuint getWallpaperTexture () const;
  55. [[nodiscard]] std::shared_ptr<const CFBO> findFBO (const std::string& name) const;
  56. [[nodiscard]] std::shared_ptr<const CFBO> getFBO () const;
  57. void updateUVs (const glm::ivec4& viewport, const bool vflip);
  58. void setDestinationFramebuffer (GLuint framebuffer);
  59. void setSpanInfo (const SpanInfo& spanInfo);
  60. [[nodiscard]] const SpanInfo* getSpanInfo () const;
  61. [[nodiscard]] virtual int getWidth () const = 0;
  62. [[nodiscard]] virtual int getHeight () const = 0;
  63. // maxRenderSize is only meaningful for Web wallpapers (see CWeb) - the largest resolution this
  64. // wallpaper instance will ever be asked to render at: a single screen's size normally, or a span
  65. // group's combined bounding box when spanned. Ignored for Scene/Video.
  66. static std::unique_ptr<CWallpaper> fromWallpaper (
  67. const Wallpaper& wallpaper, RenderContext& context, AudioContext& audioContext,
  68. const std::filesystem::path& resolvedBackgroundPath, const WallpaperState::TextureUVsScaling& scalingMode,
  69. const uint32_t& clampMode, const glm::ivec2& maxRenderSize
  70. );
  71. protected:
  72. CWallpaper (
  73. const Wallpaper& wallpaperData, RenderContext& context, AudioContext& audioContext,
  74. const WallpaperState::TextureUVsScaling& scalingMode, const uint32_t& clampMode
  75. );
  76. virtual void renderFrame (const glm::ivec4& viewport) = 0;
  77. void setupFramebuffers ();
  78. const Wallpaper& m_wallpaperData;
  79. [[nodiscard]] const Wallpaper& getWallpaperData () const;
  80. std::shared_ptr<const CFBO> m_sceneFBO = nullptr;
  81. GLuint m_vaoBuffer = GL_NONE;
  82. private:
  83. GLuint m_texCoordBuffer = GL_NONE;
  84. GLuint m_positionBuffer = GL_NONE;
  85. GLuint m_shader = GL_NONE;
  86. GLint g_Texture0 = GL_NONE;
  87. GLint a_Position = GL_NONE;
  88. GLint a_TexCoord = GL_NONE;
  89. GLuint m_destFramebuffer = GL_NONE;
  90. void setupShaders ();
  91. // UVs last uploaded to m_texCoordBuffer - render() skips the upload when these haven't
  92. // changed. NaN so the first render() call always uploads.
  93. float m_uploadedUstart = std::numeric_limits<float>::quiet_NaN ();
  94. float m_uploadedUend = std::numeric_limits<float>::quiet_NaN ();
  95. float m_uploadedVstart = std::numeric_limits<float>::quiet_NaN ();
  96. float m_uploadedVend = std::numeric_limits<float>::quiet_NaN ();
  97. std::map<std::string, std::shared_ptr<const CFBO>> m_fbos = {};
  98. AudioContext& m_audioContext;
  99. WallpaperState m_state;
  100. glm::vec4 m_cornerColor = { 0.0f, 0.0f, 0.0f, 1.0f };
  101. std::optional<SpanInfo> m_spanInfo = std::nullopt;
  102. // Avoids redundant renderFrame calls when the same wallpaper is shared across viewports (span mode)
  103. uint32_t m_lastRenderedFrame = UINT32_MAX;
  104. };
  105. } // namespace WallpaperEngine::Render