WaylandOpenGLDriver.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #pragma once
  2. #ifdef ENABLE_WAYLAND
  3. #include <EGL/egl.h>
  4. #include <EGL/eglext.h>
  5. #include <GL/glew.h>
  6. #include <wayland-client.h>
  7. #include <wayland-cursor.h>
  8. #include <wayland-egl.h>
  9. #include "WallpaperEngine/Application/ApplicationContext.h"
  10. #include "WallpaperEngine/Application/WallpaperApplication.h"
  11. #include "WallpaperEngine/Desktop/GnomeShell.h"
  12. #include "WallpaperEngine/Input/Drivers/WaylandMouseInput.h"
  13. #include "WallpaperEngine/Render/Drivers/Detectors/WaylandFullScreenDetector.h"
  14. #include "WallpaperEngine/Render/Drivers/Output/WaylandOutput.h"
  15. #include "WallpaperEngine/Render/Drivers/Output/WaylandOutputViewport.h"
  16. #include "WallpaperEngine/Render/Drivers/VideoDriver.h"
  17. namespace WallpaperEngine::Application {
  18. class ApplicationContext;
  19. class WallpaperApplication;
  20. }
  21. struct zwlr_layer_shell_v1;
  22. struct zwlr_layer_surface_v1;
  23. struct zxdg_output_manager_v1;
  24. struct xdg_wm_base;
  25. struct wp_color_manager_v1;
  26. struct wp_image_description_v1;
  27. struct wp_viewporter;
  28. struct wp_fractional_scale_manager_v1;
  29. struct ext_output_image_capture_source_manager_v1;
  30. struct ext_image_copy_capture_manager_v1;
  31. #ifdef ENABLE_KDE_FEATURES
  32. struct org_kde_plasma_shell;
  33. #endif
  34. namespace WallpaperEngine::Render::Drivers {
  35. using namespace WallpaperEngine::Application;
  36. using namespace WallpaperEngine::Input::Drivers;
  37. class WaylandOpenGLDriver;
  38. namespace Output {
  39. class WaylandOutputViewport;
  40. class WaylandOutput;
  41. }
  42. class WaylandOpenGLDriver final : public VideoDriver {
  43. friend class Output::WaylandOutput;
  44. friend class CWaylandMouseInput;
  45. public:
  46. struct SEGLContext {
  47. EGLDisplay display = nullptr;
  48. EGLConfig config = nullptr;
  49. EGLContext context = nullptr;
  50. PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC eglCreatePlatformWindowSurfaceEXT = nullptr;
  51. };
  52. struct WaylandContext {
  53. wl_display* display = nullptr;
  54. wl_registry* registry = nullptr;
  55. wl_compositor* compositor = nullptr;
  56. wl_shm* shm = nullptr;
  57. zwlr_layer_shell_v1* layerShell = nullptr;
  58. xdg_wm_base* xdgWmBase = nullptr;
  59. wl_seat* seat = nullptr;
  60. wl_pointer* pointer = nullptr;
  61. zxdg_output_manager_v1* xdgOutputManager = nullptr;
  62. ext_output_image_capture_source_manager_v1* captureSourceManager = nullptr;
  63. ext_image_copy_capture_manager_v1* copyCaptureManager = nullptr;
  64. wp_color_manager_v1* colorManager = nullptr;
  65. wp_viewporter* viewporter = nullptr;
  66. wp_fractional_scale_manager_v1* fractionalScaleManager = nullptr;
  67. #ifdef ENABLE_KDE_FEATURES
  68. org_kde_plasma_shell* plasmaShell = nullptr;
  69. #endif
  70. };
  71. struct ColorSupport {
  72. bool parametric = false;
  73. bool pq = false;
  74. bool bt2020 = false;
  75. };
  76. explicit WaylandOpenGLDriver (ApplicationContext& context, WallpaperApplication& app);
  77. ~WaylandOpenGLDriver ();
  78. [[nodiscard]] Output::Output& getOutput () override;
  79. float getRenderTime () const override;
  80. bool closeRequested () override;
  81. void resizeWindow (glm::ivec2 size) override;
  82. void resizeWindow (glm::ivec4 sizeandpos) override;
  83. void showWindow () override;
  84. void hideWindow () override;
  85. glm::ivec2 getFramebufferSize () const override;
  86. uint32_t getFrameCounter () const override;
  87. void dispatchEventQueue () override;
  88. void dispatchWhilePaused () override;
  89. /** frame callbacks don't draw while dispatchWhilePaused runs */
  90. [[nodiscard]] bool isDispatchingWhilePaused () const { return this->m_dispatchingWhilePaused; }
  91. [[nodiscard]] void* getProcAddress (const char* name) const override;
  92. [[nodiscard]] void* getWaylandDisplay () const override;
  93. [[nodiscard]] bool isHDRAvailable () const override;
  94. /** PQ with BT.2020 primaries (and the PQ default luminances, 203 nits reference white), shared by every HDR surface
  95. */
  96. [[nodiscard]] wp_image_description_v1* getHDRDescription () const;
  97. void releaseSurface (Output::WaylandOutputViewport*);
  98. void removeOutput (Output::WaylandOutputViewport*);
  99. void onSurfaceClosed (Output::WaylandOutputViewport*);
  100. void makeSurfacelessCurrent ();
  101. [[nodiscard]] bool hasGlobalCursor () const { return this->m_mouseInput.hasGlobalCursor (); }
  102. [[nodiscard]] Desktop::GnomeShell* getGnomeShell () const { return this->m_gnomeShell.get (); }
  103. [[nodiscard]] bool hasCapturedCursor () const;
  104. [[nodiscard]] std::optional<glm::dvec2> capturedCursorPosition () const;
  105. /** runs outside event dispatch */
  106. void reapClosedOutputs ();
  107. Output::WaylandOutputViewport* surfaceToViewport (const wl_surface*) const;
  108. [[nodiscard]] std::vector<std::string> drawnOutputs () const;
  109. Output::WaylandOutputViewport* viewportInFocus = nullptr;
  110. [[nodiscard]] SEGLContext* getEGLContext ();
  111. [[nodiscard]] WaylandContext* getWaylandContext ();
  112. std::vector<Output::WaylandOutputViewport*> m_screens = {};
  113. ColorSupport colorSupport = {};
  114. bool hdrDescriptionReady = false;
  115. private:
  116. Output::WaylandOutput m_output;
  117. SEGLContext m_eglContext = {};
  118. WaylandContext m_waylandContext = {};
  119. mutable bool m_requestedExit;
  120. void initWaylandRegistry ();
  121. void initGnomeShell ();
  122. void setupOutputLayerSurfaces ();
  123. void setupLateOutputs ();
  124. void initEGL ();
  125. void initGLEW ();
  126. void finishEGL () const;
  127. void initColorManagement ();
  128. wp_image_description_v1* m_hdrDescription = nullptr;
  129. uint32_t m_frameCounter = 0;
  130. bool m_layerSurfacesReady = false;
  131. bool m_dispatchingWhilePaused = false;
  132. bool m_waitingLogged = false;
  133. ApplicationContext& m_context;
  134. std::unique_ptr<Desktop::GnomeShell> m_gnomeShell;
  135. bool m_cursorCapture = false;
  136. WaylandMouseInput m_mouseInput;
  137. std::chrono::high_resolution_clock::time_point renderStart = std::chrono::high_resolution_clock::now ();
  138. };
  139. }
  140. #endif