CWeb.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #pragma once
  2. #include <chrono>
  3. #include <glm/ext.hpp>
  4. #include <glm/glm.hpp>
  5. #include <algorithm>
  6. #include <filesystem>
  7. #include <memory>
  8. #include <string>
  9. #include <sys/types.h>
  10. #include <utility>
  11. #include <vector>
  12. #include "WallpaperEngine/Audio/AudioStream.h"
  13. #include "WallpaperEngine/Media/MediaSource.h"
  14. #include "WallpaperEngine/Render/CWallpaper.h"
  15. #include "WallpaperEngine/WebBrowser/IPC/WebHostSharedMemory.h"
  16. #include "WallpaperEngine/Data/Model/Wallpaper.h"
  17. namespace WallpaperEngine::Render::Wallpapers {
  18. // Web wallpapers are rendered by CEF, which this never embeds directly (CEF only supports one
  19. // CefInitialize()/CefShutdown() pair per process, so switching a running process between hosting
  20. // and not hosting a web wallpaper isn't safe). Instead CWeb spawns a disposable child process
  21. // (WallpaperApplication::runWebHost(), a self re-exec with --web-host) that owns CEF for exactly
  22. // one browser, and reads its rendered frames back through shared memory - matching how upstream
  23. // Wallpaper Engine on Windows delegates web wallpapers to a separate webwallpaper.exe process
  24. // rather than hosting CEF in the main process.
  25. class CWeb : public CWallpaper {
  26. public:
  27. CWeb (
  28. const Wallpaper& wallpaper, RenderContext& context, AudioContext& audioContext,
  29. const std::filesystem::path& resolvedBackgroundPath, const WallpaperState::TextureUVsScaling& scalingMode,
  30. const uint32_t& clampMode, const glm::ivec2& maxRenderSize
  31. );
  32. ~CWeb () override;
  33. [[nodiscard]] int getWidth () const override { return this->m_width; }
  34. [[nodiscard]] int getHeight () const override { return this->m_height; }
  35. void setSize (int width, int height);
  36. /** ambientVolume==0 (or muted) mutes the host's CEF browser; CEF exposes no analog volume control */
  37. void setAudioPolicy (bool muted, std::optional<int> ambientVolume) override;
  38. /** The host passes changes to the page's applyUserProperties */
  39. void setPropertyOverrides (const std::map<std::string, std::string>& overrides);
  40. /** Painted something not flat, the host failed, or it took too long */
  41. [[nodiscard]] bool hasContent () const override;
  42. protected:
  43. void renderFrame (const glm::ivec4& viewport) override;
  44. void updateMouse (const glm::ivec4& viewport);
  45. /** Mirrors the media player state into the shared segment for the host process to forward to the page */
  46. void updateMedia ();
  47. const Web& getWeb () const { return *this->getWallpaperData ().as<Web> (); }
  48. friend class CWallpaper;
  49. private:
  50. void spawnHost (const std::filesystem::path& resolvedBackgroundPath);
  51. // asks the host for the next page frame, at most once per m_frameRequestDivisor renders
  52. void requestPageFrame ();
  53. WallpaperEngine::WebBrowser::IPC::WebHostSharedMemory* m_shm = nullptr;
  54. std::string m_shmName;
  55. uint32_t m_shmMaxWidth = 0;
  56. uint32_t m_shmMaxHeight = 0;
  57. // slot of the shared triple buffer this side owns, the one uploaded to the GL texture
  58. uint32_t m_frontSlot = 2;
  59. // size of the texture as currently allocated on the GPU, so renderFrame() can tell whether it
  60. // needs to reallocate storage (glTexImage2D) or can just update pixels in place (glTexSubImage2D)
  61. uint32_t m_uploadedTextureWidth = 0;
  62. uint32_t m_uploadedTextureHeight = 0;
  63. // Renders come at whatever rate the display and --fps allow, but the page only needs about 60 frames a second:
  64. // on a 240Hz display every 4th render asks for one, which keeps the cadence even instead of 4x the paint and copy
  65. // work. Derived from the measured render interval.
  66. std::chrono::steady_clock::time_point m_lastRender {};
  67. double m_renderInterval = 0.0;
  68. uint32_t m_frameRequestDivisor = 1;
  69. uint32_t m_rendersSinceRequest = 0;
  70. // LWE_WEB_STATS=1 logs how many renders and page frames per second this wallpaper gets, to tell a slow page from a
  71. // slow engine when web wallpapers degrade
  72. bool m_statsEnabled = false;
  73. std::chrono::steady_clock::time_point m_statsStart {};
  74. uint32_t m_statsRenders = 0;
  75. uint32_t m_statsFrames = 0;
  76. pid_t m_hostPid = -1;
  77. // registered with the audio recorder so spectra are pushed to the host as soon as they're computed instead of
  78. // being sampled once per rendered frame
  79. int m_spectrumListenerId = 0;
  80. // the application can swap recorders on a hotswap, the listener has to come off the one it went on
  81. Audio::Drivers::Recorders::PlaybackRecorder* m_spectrumRecorder = nullptr;
  82. Media::MediaSource::MediaInfo m_lastMedia {};
  83. bool m_mediaPublished = false;
  84. uint32_t m_coverVersion = 0;
  85. bool m_helperFailureLogged = false;
  86. std::chrono::steady_clock::time_point m_created = std::chrono::steady_clock::now ();
  87. bool m_pageHasContent = false;
  88. int m_width = 16;
  89. int m_height = 17;
  90. glm::vec2 m_mousePosition = {};
  91. glm::vec2 m_mousePositionLast = {};
  92. };
  93. }