HeadlessOpenGLDriver.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include "WallpaperEngine/Application/ApplicationContext.h"
  3. #include "WallpaperEngine/Application/WallpaperApplication.h"
  4. #include "WallpaperEngine/Input/MouseInput.h"
  5. #include "WallpaperEngine/Render/Drivers/VideoDriver.h"
  6. #include <EGL/egl.h>
  7. #include <GL/glew.h>
  8. #include <chrono>
  9. #include <vector>
  10. namespace WallpaperEngine::Render::Drivers {
  11. using namespace WallpaperEngine::Application;
  12. /**
  13. * Renders into an offscreen EGL pbuffer on a GPU render node, no window system involved. Picked with
  14. * XDG_SESSION_TYPE=headless together with --window, meant for screenshots and regression runs on
  15. * machines without a display (Xvfb only offers software rendering). LWE_HEADLESS_DEVICE=/dev/dri/renderDN
  16. * chooses the GPU, otherwise the first one EGL lists is used.
  17. */
  18. class HeadlessOpenGLDriver final : public VideoDriver {
  19. public:
  20. explicit HeadlessOpenGLDriver (ApplicationContext& context, WallpaperApplication& app);
  21. ~HeadlessOpenGLDriver () override;
  22. [[nodiscard]] Output::Output& getOutput () override;
  23. [[nodiscard]] float getRenderTime () const override;
  24. bool closeRequested () override;
  25. void resizeWindow (glm::ivec2 size) override;
  26. void resizeWindow (glm::ivec4 sizeandpos) override;
  27. void showWindow () override;
  28. void hideWindow () override;
  29. [[nodiscard]] glm::ivec2 getFramebufferSize () const override;
  30. [[nodiscard]] uint32_t getFrameCounter () const override;
  31. void dispatchEventQueue () override;
  32. [[nodiscard]] void* getProcAddress (const char* name) const override;
  33. private:
  34. class FixedMouse final : public Input::MouseInput {
  35. public:
  36. explicit FixedMouse (const HeadlessOpenGLDriver& driver);
  37. void update () override;
  38. // output center, or LWE_HEADLESS_CURSOR=x,y as fractions. LWE_HEADLESS_CURSOR_PATH="seconds:x,y[,d];..." moves
  39. // it over time, d holds the left button
  40. [[nodiscard]] glm::dvec2 position () const override { return glm::dvec2 (m_driver.m_size) * m_fraction; }
  41. [[nodiscard]] Input::MouseClickStatus leftClick () const override {
  42. return m_down ? Input::Clicked : Input::Released;
  43. }
  44. [[nodiscard]] Input::MouseClickStatus rightClick () const override { return Input::Released; }
  45. private:
  46. struct Step {
  47. double time;
  48. glm::dvec2 fraction;
  49. bool down;
  50. };
  51. const HeadlessOpenGLDriver& m_driver;
  52. glm::dvec2 m_fraction = glm::dvec2 (0.5);
  53. bool m_down = false;
  54. std::vector<Step> m_path;
  55. };
  56. void createSurface ();
  57. FixedMouse m_mouseInput;
  58. Output::Output* m_output = nullptr;
  59. EGLDisplay m_display = EGL_NO_DISPLAY;
  60. EGLConfig m_config = nullptr;
  61. EGLContext m_eglContext = EGL_NO_CONTEXT;
  62. EGLSurface m_surface = EGL_NO_SURFACE;
  63. GLsync m_lastFrame = nullptr;
  64. glm::ivec2 m_size = { 640, 480 };
  65. std::chrono::steady_clock::time_point m_started = std::chrono::steady_clock::now ();
  66. uint32_t m_frameCounter = 0;
  67. };
  68. } // namespace WallpaperEngine::Render::Drivers