WebHostSharedMemory.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #pragma once
  2. #include <atomic>
  3. #include <cstddef>
  4. #include <cstdint>
  5. #include <optional>
  6. #include <string>
  7. namespace WallpaperEngine::WebBrowser::IPC {
  8. // shared memory between the main process (reads frames, writes input/size) and the web host (owns CEF, writes frames),
  9. // one segment per CWeb; pixel buffers have maxWidth x maxHeight capacity, the browser resizes within it via
  10. // desiredWidth/Height, the painted size is in slotWidth/slotHeight
  11. struct WebHostSharedMemory {
  12. // triple buffer: the host paints its back slot, the main process uploads its front slot, the third sits in
  13. // frameSlot; publishing and taking are single atomic swaps bits 0-1 are the slot, FRAME_DIRTY marks a published
  14. // frame not taken yet. Starts as slot 1 clean (host on 0, main on 2)
  15. static constexpr uint32_t FRAME_SLOTS = 3;
  16. static constexpr uint32_t FRAME_SLOT_MASK = 3;
  17. static constexpr uint32_t FRAME_DIRTY = 4;
  18. std::atomic<uint32_t> frameSlot { 1 };
  19. // Size of the content painted into each slot, written before the slot is published
  20. std::atomic<uint32_t> slotWidth[FRAME_SLOTS] {};
  21. std::atomic<uint32_t> slotHeight[FRAME_SLOTS] {};
  22. // Main process -> host process: bumped whenever the page should produce its next frame (CEF runs with
  23. // external begin frames). Doubles as a futex word, see waitForFrameRequest().
  24. std::atomic<uint32_t> frameRequestSeq { 0 };
  25. // desired viewport size, the host calls WasResized() when it changes
  26. std::atomic<uint32_t> desiredWidth { 0 };
  27. std::atomic<uint32_t> desiredHeight { 0 };
  28. // Host process -> main process
  29. std::atomic<bool> helperReady { false };
  30. std::atomic<bool> helperFailed { false };
  31. // Main process -> host process
  32. std::atomic<bool> quitRequested { false };
  33. // Whether the browser's audio should be muted, see --audio-screen/--ambient-volume
  34. std::atomic<bool> audioMuted { false };
  35. std::atomic<double> mouseX { 0.0 };
  36. std::atomic<double> mouseY { 0.0 };
  37. // Matches WallpaperEngine::Input::MouseClickStatus (Released = 0, Clicked = 1)
  38. std::atomic<int32_t> leftClick { 0 };
  39. std::atomic<int32_t> rightClick { 0 };
  40. // Main -> host: latest [left 64 | right 64] bands, unprocessed. Torn reads are harmless here, no seqlock
  41. static constexpr std::size_t AUDIO_BANDS = 128;
  42. std::atomic<float> audioBands[AUDIO_BANDS] {};
  43. // the desktop media player (MPRIS) mirrored for the host, which has no D-Bus; seqlock, odd mediaSeq while writing
  44. static constexpr std::size_t MEDIA_TEXT = 256;
  45. static constexpr std::size_t MEDIA_PATH = 1024;
  46. std::atomic<uint32_t> mediaSeq { 0 };
  47. // Bumped only when the cover changed, so the host doesn't reload and re-encode it for every progress update
  48. uint32_t mediaCoverVersion = 0;
  49. // Matches WallpaperEngine::Media::MediaSource::PlaybackState
  50. int32_t mediaState = 0;
  51. bool mediaAvailable = false;
  52. double mediaPosition = 0.0;
  53. double mediaDuration = 0.0;
  54. char mediaTitle[MEDIA_TEXT] = {};
  55. char mediaArtist[MEDIA_TEXT] = {};
  56. char mediaAlbum[MEDIA_TEXT] = {};
  57. // Local file of the cover, empty if there is none (or it isn't a file:// URL)
  58. char mediaCoverPath[MEDIA_PATH] = {};
  59. // primary, secondary, tertiary, text and high contrast as RGB8, see Media::ThumbnailPalette
  60. uint8_t mediaPalette[5][3] = {};
  61. // name=value lines, odd sequence numbers while writing
  62. static constexpr std::size_t PROPERTIES_TEXT = 65536;
  63. std::atomic<uint32_t> propertiesSeq { 0 };
  64. char propertiesText[PROPERTIES_TEXT] = {};
  65. uint32_t maxWidth = 0;
  66. uint32_t maxHeight = 0;
  67. // FRAME_SLOTS BGRA8 slots of maxWidth * maxHeight follow this header, only slotWidth x slotHeight is live
  68. [[nodiscard]] uint8_t* frameBuffer (uint32_t slot) {
  69. return reinterpret_cast<uint8_t*> (this) + sizeof (WebHostSharedMemory)
  70. + static_cast<std::size_t> (slot) * maxWidth * maxHeight * 4;
  71. }
  72. // host: publish the painted slot, get the next one
  73. uint32_t publishFrame (uint32_t backSlot) {
  74. return frameSlot.exchange (backSlot | FRAME_DIRTY, std::memory_order_acq_rel) & FRAME_SLOT_MASK;
  75. }
  76. // main process: swap the front slot for the latest frame, nullopt when none
  77. std::optional<uint32_t> takeFrame (uint32_t frontSlot) {
  78. if ((frameSlot.load (std::memory_order_acquire) & FRAME_DIRTY) == 0) {
  79. return std::nullopt;
  80. }
  81. return frameSlot.exchange (frontSlot, std::memory_order_acq_rel) & FRAME_SLOT_MASK;
  82. }
  83. // Main process: asks the host to produce a frame and wakes it if it's sleeping in waitForFrameRequest()
  84. void requestFrame ();
  85. // host: sleeps until frameRequestSeq differs from lastSeen or the timeout, returns the current value
  86. uint32_t waitForFrameRequest (uint32_t lastSeen, uint32_t timeoutMs);
  87. [[nodiscard]] static std::size_t totalSize (uint32_t maxWidth, uint32_t maxHeight) {
  88. return sizeof (WebHostSharedMemory) + FRAME_SLOTS * static_cast<std::size_t> (maxWidth) * maxHeight * 4;
  89. }
  90. };
  91. // a zeroed segment for maxWidth*maxHeight BGRA8, outName is what the host attaches to; nullptr on failure
  92. WebHostSharedMemory* createSharedMemory (std::string& outName, uint32_t maxWidth, uint32_t maxHeight);
  93. // removes what dead engines' web hosts left: "lwe-web-<tag>-<n>" segments and "lwe-cef-<tag>-<uuid>" profiles
  94. // (Desktop::processTag); users hold a shared flock (CEF_PROFILE_LOCK for profiles), unlocked ones are stale unless the
  95. // tag is a running pid. Each segment is 3 frames, hundreds of MB add up
  96. void removeStaleWebHostFiles ();
  97. // The file in a CEF profile directory its host process keeps locked
  98. inline constexpr const char* CEF_PROFILE_LOCK = "lwe-host.lock";
  99. // Whether some process holds a flock on the file
  100. bool isLockedByAnyone (const std::string& path);
  101. // attaches to a segment from createSharedMemory() in another process, same max size; nullptr on failure
  102. WebHostSharedMemory* attachSharedMemory (const std::string& name, uint32_t maxWidth, uint32_t maxHeight);
  103. // only the creator unlinks the shm name
  104. void closeSharedMemory (
  105. WebHostSharedMemory* mem, const std::string& name, uint32_t maxWidth, uint32_t maxHeight, bool unlinkSegment
  106. );
  107. } // namespace WallpaperEngine::WebBrowser::IPC