WaylandOutputViewport.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. #include "WaylandOutputViewport.h"
  2. #include "WallpaperEngine/Desktop/GnomeShell.h"
  3. #include "WallpaperEngine/Logging/Log.h"
  4. #include <unistd.h>
  5. #define class _class
  6. #define namespace _namespace
  7. #define static
  8. extern "C" {
  9. #include "color-management-v1-protocol.h"
  10. #include "fractional-scale-v1-protocol.h"
  11. #include "viewporter-protocol.h"
  12. #include "wlr-layer-shell-unstable-v1-protocol.h"
  13. #include "xdg-output-unstable-v1-protocol.h"
  14. #include "xdg-shell-protocol.h"
  15. #ifdef ENABLE_KDE_FEATURES
  16. #include "plasma-shell-protocol.h"
  17. #endif
  18. }
  19. #undef class
  20. #undef namespace
  21. #undef static
  22. using namespace WallpaperEngine::Render::Drivers;
  23. using namespace WallpaperEngine::Render::Drivers::Output;
  24. static void handleLSConfigure (void* data, zwlr_layer_surface_v1* surface, uint32_t serial, uint32_t w, uint32_t h) {
  25. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  26. viewport->size = { w, h };
  27. viewport->logicalSize = { w, h };
  28. viewport->updateBufferSize ();
  29. zwlr_layer_surface_v1_ack_configure (surface, serial);
  30. }
  31. static void handleLSClosed (void* data, zwlr_layer_surface_v1* surface) {
  32. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  33. viewport->closed = true;
  34. viewport->rendering = false;
  35. }
  36. static void handleXdgSurfaceConfigure (void* data, xdg_surface* surface, uint32_t serial) {
  37. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  38. const auto pending = viewport->pendingToplevelSize;
  39. xdg_surface_ack_configure (surface, serial);
  40. viewport->toplevelConfigured = true;
  41. if (pending.x <= 0 || pending.y <= 0 || pending == viewport->size) {
  42. return;
  43. }
  44. viewport->size = pending;
  45. viewport->logicalSize = pending;
  46. viewport->updateBufferSize ();
  47. }
  48. static void
  49. handleXdgToplevelConfigure (void* data, xdg_toplevel*, int32_t width, int32_t height, wl_array* /* states */) {
  50. static_cast<WaylandOutputViewport*> (data)->pendingToplevelSize = { width, height };
  51. }
  52. // ignored, a wallpaper closed by Alt+F4 (focusable on GNOME 45) would never come back
  53. static void handleXdgToplevelClose (void*, xdg_toplevel*) { }
  54. static void handleXdgToplevelBounds (void*, xdg_toplevel*, int32_t, int32_t) { }
  55. static void handleXdgToplevelCapabilities (void*, xdg_toplevel*, wl_array*) { }
  56. constexpr xdg_surface_listener xdgSurfaceListener = { .configure = handleXdgSurfaceConfigure };
  57. constexpr xdg_toplevel_listener xdgToplevelListener = {
  58. .configure = handleXdgToplevelConfigure,
  59. .close = handleXdgToplevelClose,
  60. .configure_bounds = handleXdgToplevelBounds,
  61. .wm_capabilities = handleXdgToplevelCapabilities,
  62. };
  63. static void geometry (
  64. void* data, wl_output* output, int32_t x, int32_t y, int32_t width_mm, int32_t height_mm, int32_t subpixel,
  65. const char* make, const char* model, int32_t transform
  66. ) {
  67. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  68. // only use geometry position as fallback if xdg-output hasn't provided one
  69. if (!viewport->hasXdgLogicalPosition) {
  70. viewport->globalPosition = { x, y };
  71. }
  72. sLog.debug ("SPAN DEBUG geometry: output '", viewport->name, "' position=(", x, ",", y, ") transform=", transform);
  73. }
  74. static void mode (void* data, wl_output* output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
  75. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  76. // a mode change alone sends a toplevel no new configure
  77. if (viewport->xdgToplevel) {
  78. if (viewport->initialized) {
  79. viewport->getDriver ()->getOutput ().reset ();
  80. }
  81. return;
  82. }
  83. // physical pixels, only a placeholder until the layer shell configure brings the logical size; a mode change
  84. // on a live surface comes with a new configure
  85. if (!viewport->hasShellSurface ()) {
  86. viewport->size = { width, height };
  87. viewport->viewport = { 0, 0, width, height };
  88. }
  89. if (viewport->initialized) {
  90. viewport->getDriver ()->getOutput ().reset ();
  91. }
  92. }
  93. static void done (void* data, wl_output* wl_output) { static_cast<WaylandOutputViewport*> (data)->initialized = true; }
  94. static void scale (void* data, wl_output* wl_output, int32_t scale) {
  95. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  96. viewport->scale = scale;
  97. if (viewport->hasShellSurface ()) {
  98. viewport->updateBufferSize ();
  99. }
  100. if (viewport->initialized) {
  101. viewport->getDriver ()->getOutput ().reset ();
  102. }
  103. }
  104. static void name (void* data, wl_output* wl_output, const char* name) {
  105. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  106. if (name) {
  107. viewport->name = name;
  108. }
  109. viewport->getDriver ()->getOutput ().reset ();
  110. }
  111. static void description (void* data, wl_output* wl_output, const char* description) {
  112. // ignored
  113. }
  114. static void surfaceFrameCallback (void* data, struct wl_callback* cb, uint32_t time) {
  115. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  116. wl_callback_destroy (cb);
  117. viewport->frameCallback = nullptr;
  118. viewport->renderFrame ();
  119. }
  120. constexpr struct wl_callback_listener frameListener = { .done = surfaceFrameCallback };
  121. static void handlePreferredScale (void* data, wp_fractional_scale_v1*, uint32_t scale) {
  122. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  123. if (scale == viewport->preferredScale120) {
  124. return;
  125. }
  126. sLog.out ("Output ", viewport->name, ": drawing at scale ", scale / 120.0);
  127. viewport->preferredScale120 = scale;
  128. viewport->updateBufferSize ();
  129. }
  130. constexpr wp_fractional_scale_v1_listener fractionalScaleListener = { .preferred_scale = handlePreferredScale };
  131. constexpr wl_output_listener outputListener
  132. = { .geometry = geometry, .mode = mode, .done = done, .scale = scale, .name = name, .description = description };
  133. constexpr struct zwlr_layer_surface_v1_listener layerSurfaceListener = {
  134. .configure = handleLSConfigure,
  135. .closed = handleLSClosed,
  136. };
  137. static void xdgOutputLogicalPosition (void* data, struct zxdg_output_v1* xdg_output, int32_t x, int32_t y) {
  138. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  139. viewport->globalPosition = { x, y };
  140. viewport->hasXdgLogicalPosition = true;
  141. sLog.debug ("SPAN DEBUG xdg-output logical_position: '", viewport->name, "' position=(", x, ",", y, ")");
  142. }
  143. static void xdgOutputLogicalSize (void* data, struct zxdg_output_v1* xdg_output, int32_t width, int32_t height) {
  144. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  145. viewport->logicalSize = { width, height };
  146. if (viewport->initialized) {
  147. viewport->getDriver ()->getOutput ().reset ();
  148. }
  149. }
  150. static void xdgOutputDone (void* data, struct zxdg_output_v1* xdg_output) {
  151. // deprecated since xdg-output v3, compositor uses wl_output.done instead
  152. }
  153. static void xdgOutputName (void* data, struct zxdg_output_v1* xdg_output, const char* name) {
  154. // already handled by wl_output.name
  155. }
  156. static void xdgOutputDescription (void* data, struct zxdg_output_v1* xdg_output, const char* description) {
  157. // ignored
  158. }
  159. constexpr struct zxdg_output_v1_listener xdgOutputListener = {
  160. .logical_position = xdgOutputLogicalPosition,
  161. .logical_size = xdgOutputLogicalSize,
  162. .done = xdgOutputDone,
  163. .name = xdgOutputName,
  164. .description = xdgOutputDescription,
  165. };
  166. WaylandOutputViewport::WaylandOutputViewport (
  167. WaylandOpenGLDriver* driver, uint32_t waylandName, struct wl_registry* registry
  168. ) : OutputViewport ({ 0, 0, 0, 0 }, "", true), size ({ 0, 0 }), waylandName (waylandName), m_driver (driver) {
  169. this->output = static_cast<wl_output*> (wl_registry_bind (registry, waylandName, &wl_output_interface, 4));
  170. wl_output_add_listener (output, &outputListener, this);
  171. }
  172. void WaylandOutputViewport::setupXdgOutput (zxdg_output_manager_v1* manager) {
  173. this->xdgOutput = zxdg_output_manager_v1_get_xdg_output (manager, this->output);
  174. zxdg_output_v1_add_listener (this->xdgOutput, &xdgOutputListener, this);
  175. }
  176. static void descriptionInfoDone (void* data, wp_image_description_info_v1* info) {
  177. auto* viewport = static_cast<WaylandOutputViewport*> (data);
  178. const auto& description = viewport->pendingDescription;
  179. const uint32_t peak = description.targetMaxLuminance ? description.targetMaxLuminance : description.maxLuminance;
  180. // KWin describes an HDR output as PQ; a plain SDR output peaks at about its reference white
  181. const bool hdr = description.tf == WP_COLOR_MANAGER_V1_TRANSFER_FUNCTION_ST2084_PQ
  182. || (description.referenceLuminance > 0 && peak * 2 > description.referenceLuminance * 3);
  183. wp_image_description_info_v1_destroy (info);
  184. viewport->outputLuminance = glm::vec2 (description.referenceLuminance, peak);
  185. if (hdr != viewport->outputHDR) {
  186. viewport->outputHDR = hdr;
  187. sLog.out ("Output ", viewport->name, hdr ? " switched to HDR, drawing PQ" : " switched to SDR");
  188. viewport->applyImageDescription ();
  189. }
  190. }
  191. static void descriptionInfoIcc (void*, wp_image_description_info_v1*, int32_t fd, uint32_t) { close (fd); }
  192. static void descriptionInfoPrimaries (
  193. void*, wp_image_description_info_v1*, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t
  194. ) { }
  195. static void descriptionInfoPrimariesNamed (void*, wp_image_description_info_v1*, uint32_t) { }
  196. static void descriptionInfoTransferPower (void*, wp_image_description_info_v1*, uint32_t) { }
  197. static void descriptionInfoTransferNamed (void* data, wp_image_description_info_v1*, uint32_t tf) {
  198. static_cast<WaylandOutputViewport*> (data)->pendingDescription.tf = tf;
  199. }
  200. static void
  201. descriptionInfoLuminances (void* data, wp_image_description_info_v1*, uint32_t, uint32_t max, uint32_t reference) {
  202. auto& description = static_cast<WaylandOutputViewport*> (data)->pendingDescription;
  203. description.maxLuminance = max;
  204. description.referenceLuminance = reference;
  205. }
  206. static void descriptionInfoTargetLuminance (void* data, wp_image_description_info_v1*, uint32_t, uint32_t max) {
  207. static_cast<WaylandOutputViewport*> (data)->pendingDescription.targetMaxLuminance = max;
  208. }
  209. static void descriptionInfoTargetMaxCll (void*, wp_image_description_info_v1*, uint32_t) { }
  210. static void descriptionInfoTargetMaxFall (void*, wp_image_description_info_v1*, uint32_t) { }
  211. constexpr wp_image_description_info_v1_listener descriptionInfoListener = {
  212. .done = descriptionInfoDone,
  213. .icc_file = descriptionInfoIcc,
  214. .primaries = descriptionInfoPrimaries,
  215. .primaries_named = descriptionInfoPrimariesNamed,
  216. .tf_power = descriptionInfoTransferPower,
  217. .tf_named = descriptionInfoTransferNamed,
  218. .luminances = descriptionInfoLuminances,
  219. .target_primaries = descriptionInfoPrimaries,
  220. .target_luminance = descriptionInfoTargetLuminance,
  221. .target_max_cll = descriptionInfoTargetMaxCll,
  222. .target_max_fall = descriptionInfoTargetMaxFall,
  223. };
  224. static void outputDescriptionFailed (void* data, wp_image_description_v1* description, uint32_t, const char* message) {
  225. sLog.error ("Cannot tell whether ", static_cast<WaylandOutputViewport*> (data)->name, " runs in HDR: ", message);
  226. wp_image_description_v1_destroy (description);
  227. }
  228. static void outputDescriptionReady (void* data, wp_image_description_v1* description, uint32_t) {
  229. auto* viewport = static_cast<WaylandOutputViewport*> (data);
  230. viewport->pendingDescription = {};
  231. wp_image_description_info_v1_add_listener (
  232. wp_image_description_v1_get_information (description), &descriptionInfoListener, viewport
  233. );
  234. wp_image_description_v1_destroy (description);
  235. }
  236. constexpr wp_image_description_v1_listener outputDescriptionListener = {
  237. .failed = outputDescriptionFailed,
  238. .ready = outputDescriptionReady,
  239. };
  240. static void outputDescriptionChanged (void* data, wp_color_management_output_v1*) {
  241. static_cast<WaylandOutputViewport*> (data)->queryOutputDescription ();
  242. }
  243. constexpr wp_color_management_output_v1_listener colorOutputListener = {
  244. .image_description_changed = outputDescriptionChanged,
  245. };
  246. void WaylandOutputViewport::setupColorManagement () {
  247. if (this->colorOutput != nullptr || !m_driver->isHDRAvailable ()) {
  248. return;
  249. }
  250. this->colorOutput = wp_color_manager_v1_get_output (m_driver->getWaylandContext ()->colorManager, this->output);
  251. wp_color_management_output_v1_add_listener (this->colorOutput, &colorOutputListener, this);
  252. this->queryOutputDescription ();
  253. }
  254. void WaylandOutputViewport::queryOutputDescription () {
  255. wp_image_description_v1_add_listener (
  256. wp_color_management_output_v1_get_image_description (this->colorOutput), &outputDescriptionListener, this
  257. );
  258. }
  259. void WaylandOutputViewport::applyImageDescription () {
  260. if (this->colorSurface == nullptr) {
  261. return;
  262. }
  263. // takes effect with the next frame's commit
  264. if (this->outputHDR) {
  265. wp_color_management_surface_v1_set_image_description (
  266. this->colorSurface, m_driver->getHDRDescription (), WP_COLOR_MANAGER_V1_RENDER_INTENT_PERCEPTUAL
  267. );
  268. } else {
  269. wp_color_management_surface_v1_unset_image_description (this->colorSurface);
  270. }
  271. }
  272. bool WaylandOutputViewport::isHDR () const { return this->colorSurface != nullptr && this->outputHDR; }
  273. void WaylandOutputViewport::setupLS () {
  274. surface = wl_compositor_create_surface (m_driver->getWaylandContext ()->compositor);
  275. zwlr_layer_shell_v1_layer wlrLayer;
  276. switch (m_driver->getApp ().getContext ().settings.render.wayland.layer) {
  277. case WallpaperEngine::Application::ApplicationContext::WAYLAND_LAYER_BACKGROUND:
  278. wlrLayer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND;
  279. break;
  280. case WallpaperEngine::Application::ApplicationContext::WAYLAND_LAYER_TOP:
  281. wlrLayer = ZWLR_LAYER_SHELL_V1_LAYER_TOP;
  282. break;
  283. case WallpaperEngine::Application::ApplicationContext::WAYLAND_LAYER_OVERLAY:
  284. wlrLayer = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY;
  285. break;
  286. case WallpaperEngine::Application::ApplicationContext::WAYLAND_LAYER_BOTTOM:
  287. default:
  288. wlrLayer = ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
  289. break;
  290. }
  291. // KWin treats the "desktop" namespace as a desktop window, so 'show desktop' leaves it alone
  292. layerSurface = zwlr_layer_shell_v1_get_layer_surface (
  293. m_driver->getWaylandContext ()->layerShell, surface, output, wlrLayer, "desktop"
  294. );
  295. if (!layerSurface) {
  296. sLog.exception ("Failed to get a layer surface");
  297. }
  298. zwlr_layer_surface_v1_set_size (layerSurface, 0, 0);
  299. zwlr_layer_surface_v1_set_anchor (
  300. layerSurface,
  301. ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
  302. | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM
  303. );
  304. zwlr_layer_surface_v1_set_keyboard_interactivity (layerSurface, false);
  305. zwlr_layer_surface_v1_add_listener (layerSurface, &layerSurfaceListener, this);
  306. zwlr_layer_surface_v1_set_exclusive_zone (layerSurface, -1);
  307. #ifdef ENABLE_KDE_FEATURES
  308. if (m_driver->getWaylandContext ()->plasmaShell) {
  309. plasmaSurface = org_kde_plasma_shell_get_surface (m_driver->getWaylandContext ()->plasmaShell, surface);
  310. if (plasmaSurface) {
  311. org_kde_plasma_surface_set_role (plasmaSurface, ORG_KDE_PLASMA_SURFACE_ROLE_DESKTOP);
  312. org_kde_plasma_surface_set_skip_taskbar (plasmaSurface, 1);
  313. org_kde_plasma_surface_set_skip_switcher (plasmaSurface, 1);
  314. }
  315. }
  316. #endif
  317. this->prepareSurface ();
  318. wl_surface_commit (surface);
  319. wl_display_roundtrip (m_driver->getWaylandContext ()->display);
  320. // the roundtrip can already deliver closed (river without a layout)
  321. if (closed) {
  322. return;
  323. }
  324. this->createEGLSurface ();
  325. }
  326. void WaylandOutputViewport::setupToplevel () {
  327. auto* context = m_driver->getWaylandContext ();
  328. surface = wl_compositor_create_surface (context->compositor);
  329. xdgSurface = xdg_wm_base_get_xdg_surface (context->xdgWmBase, surface);
  330. xdg_surface_add_listener (xdgSurface, &xdgSurfaceListener, this);
  331. xdgToplevel = xdg_surface_get_toplevel (xdgSurface);
  332. xdg_toplevel_add_listener (xdgToplevel, &xdgToplevelListener, this);
  333. // the extension matches on these, set before the first commit
  334. const std::string title = std::string (WallpaperEngine::Desktop::GnomeShell::TITLE_PREFIX) + this->name;
  335. xdg_toplevel_set_app_id (xdgToplevel, WallpaperEngine::Desktop::GnomeShell::APP_ID);
  336. xdg_toplevel_set_title (xdgToplevel, title.c_str ());
  337. // not fullscreen, Mutter would hide the top bar; the extension places it by the title's output
  338. this->prepareSurface ();
  339. wl_surface_commit (surface);
  340. for (int i = 0; i < 10 && !toplevelConfigured && !closed; i++) {
  341. if (wl_display_roundtrip (context->display) == -1) {
  342. break;
  343. }
  344. }
  345. if (closed) {
  346. return;
  347. }
  348. if (!toplevelConfigured) {
  349. sLog.error ("Output ", this->name, ": the compositor never configured the wallpaper window");
  350. }
  351. // 0x0 configure: use the output's logical size
  352. if (this->size.x <= 0 || this->size.y <= 0 || this->pendingToplevelSize.x <= 0) {
  353. this->size = this->logicalSize.x > 0 ? this->logicalSize : this->size / glm::max (this->scale, 1);
  354. this->logicalSize = this->size;
  355. this->viewport = { 0, 0, this->bufferSize () };
  356. }
  357. this->createEGLSurface ();
  358. }
  359. void WaylandOutputViewport::setupFractionalScale () {
  360. auto* context = m_driver->getWaylandContext ();
  361. // a fractional buffer needs the viewport to tell the compositor its logical size
  362. if (!context->viewporter) {
  363. return;
  364. }
  365. this->surfaceViewport = wp_viewporter_get_viewport (context->viewporter, this->surface);
  366. if (context->fractionalScaleManager) {
  367. this->fractionalScale
  368. = wp_fractional_scale_manager_v1_get_fractional_scale (context->fractionalScaleManager, this->surface);
  369. wp_fractional_scale_v1_add_listener (this->fractionalScale, &fractionalScaleListener, this);
  370. }
  371. }
  372. void WaylandOutputViewport::prepareSurface () {
  373. this->setupFractionalScale ();
  374. this->setInputRegion (m_driver->getApp ().getContext ().settings.mouse.enabled);
  375. // fully opaque, the compositor can skip blending
  376. wl_region* opaqueRegion = wl_compositor_create_region (m_driver->getWaylandContext ()->compositor);
  377. wl_region_add (opaqueRegion, 0, 0, INT32_MAX, INT32_MAX);
  378. wl_surface_set_opaque_region (surface, opaqueRegion);
  379. wl_region_destroy (opaqueRegion);
  380. if (m_driver->isHDRAvailable ()) {
  381. this->setupColorManagement ();
  382. this->colorSurface = wp_color_manager_v1_get_surface (m_driver->getWaylandContext ()->colorManager, surface);
  383. this->applyImageDescription ();
  384. }
  385. }
  386. void WaylandOutputViewport::createEGLSurface () {
  387. const glm::ivec2 buffer = this->bufferSize ();
  388. this->viewport = { 0, 0, buffer };
  389. eglWindow = wl_egl_window_create (surface, buffer.x, buffer.y);
  390. eglSurface = m_driver->getEGLContext ()->eglCreatePlatformWindowSurfaceEXT (
  391. m_driver->getEGLContext ()->display, m_driver->getEGLContext ()->config, eglWindow, nullptr
  392. );
  393. // Mutter flags an empty commit between a toplevel's configure ack and its first buffer
  394. if (!this->xdgToplevel) {
  395. wl_surface_commit (surface);
  396. }
  397. wl_display_roundtrip (m_driver->getWaylandContext ()->display);
  398. wl_display_flush (m_driver->getWaylandContext ()->display);
  399. if (closed) {
  400. return;
  401. }
  402. static const auto XCURSORSIZE = getenv ("XCURSOR_SIZE") ? std::stoi (getenv ("XCURSOR_SIZE")) : 24;
  403. cursorTheme
  404. = wl_cursor_theme_load (getenv ("XCURSOR_THEME"), XCURSORSIZE * scale, m_driver->getWaylandContext ()->shm);
  405. if (!cursorTheme) {
  406. sLog.exception ("Failed to get a cursor theme");
  407. }
  408. pointer = wl_cursor_theme_get_cursor (cursorTheme, "left_ptr");
  409. cursorSurface = wl_compositor_create_surface (m_driver->getWaylandContext ()->compositor);
  410. if (!cursorSurface) {
  411. sLog.exception ("Failed to get a cursor surface");
  412. }
  413. if (eglMakeCurrent (
  414. m_driver->getEGLContext ()->display, eglSurface, eglSurface, m_driver->getEGLContext ()->context
  415. )
  416. == EGL_FALSE) {
  417. sLog.exception ("Failed to make egl current");
  418. }
  419. // paced by our frame callbacks; with interval 1 Mesa blocks in eglSwapBuffers while the surface isn't presented,
  420. // stalling the main loop
  421. eglSwapInterval (m_driver->getEGLContext ()->display, 0);
  422. this->m_driver->getOutput ().reset ();
  423. }
  424. WaylandOpenGLDriver* WaylandOutputViewport::getDriver () const { return this->m_driver; }
  425. void WaylandOutputViewport::makeCurrent () {
  426. const EGLBoolean result = eglMakeCurrent (
  427. m_driver->getEGLContext ()->display, eglSurface, eglSurface, m_driver->getEGLContext ()->context
  428. );
  429. if (result == EGL_FALSE) {
  430. sLog.error ("Couldn't make egl current");
  431. }
  432. }
  433. void WaylandOutputViewport::setInputRegion (bool everything) {
  434. wl_region* region = wl_compositor_create_region (m_driver->getWaylandContext ()->compositor);
  435. if (everything) {
  436. wl_region_add (region, 0, 0, INT32_MAX, INT32_MAX);
  437. }
  438. wl_surface_set_input_region (this->surface, region);
  439. wl_region_destroy (region);
  440. }
  441. void WaylandOutputViewport::setWantsPointerButtons (bool wants) {
  442. if (this->surface == nullptr || !m_driver->getApp ().getContext ().settings.mouse.enabled) {
  443. return;
  444. }
  445. // without a global cursor source, hover and parallax need the pointer on our surface
  446. const bool passthrough = !wants && m_driver->hasGlobalCursor ();
  447. if (passthrough == this->inputPassthrough) {
  448. return;
  449. }
  450. this->inputPassthrough = passthrough;
  451. // applied by the next commit
  452. this->setInputRegion (!passthrough);
  453. // the release would go to the desktop now
  454. this->leftClick = WallpaperEngine::Input::MouseClickStatus::Released;
  455. this->rightClick = WallpaperEngine::Input::MouseClickStatus::Released;
  456. sLog.out (
  457. "Output ", this->name,
  458. passthrough ? ": the wallpaper does not use clicks, they go to the desktop below"
  459. : ": the wallpaper uses clicks, the pointer stays on it"
  460. );
  461. }
  462. void WaylandOutputViewport::renderFrame () {
  463. // the driver frees closed viewports on its next pass
  464. if (this->closed || this->m_driver->isDispatchingWhilePaused ()) {
  465. return;
  466. }
  467. this->rendering = true;
  468. this->m_driver->getApp ().update (this);
  469. this->rendering = false;
  470. }
  471. void WaylandOutputViewport::swapOutput () {
  472. this->callbackInitialized = true;
  473. this->lastSwap = std::chrono::steady_clock::now ();
  474. this->makeCurrent ();
  475. // a forced frame replaces the pending callback, two live callbacks would double the frame rate
  476. if (frameCallback) {
  477. wl_callback_destroy (frameCallback);
  478. }
  479. frameCallback = wl_surface_frame (surface);
  480. wl_callback_add_listener (frameCallback, &frameListener, this);
  481. // latched by the commit inside eglSwapBuffers together with the new buffer
  482. if (this->surfaceViewport) {
  483. wl_surface_set_buffer_scale (surface, 1);
  484. wp_viewport_set_destination (this->surfaceViewport, this->size.x, this->size.y);
  485. } else {
  486. wl_surface_set_buffer_scale (surface, scale);
  487. }
  488. eglSwapBuffers (m_driver->getEGLContext ()->display, this->eglSurface);
  489. wl_surface_damage_buffer (surface, 0, 0, INT32_MAX, INT32_MAX);
  490. wl_surface_commit (surface);
  491. }
  492. double WaylandOutputViewport::renderScale () const {
  493. if (this->surfaceViewport && this->preferredScale120 > 0) {
  494. return this->preferredScale120 / 120.0;
  495. }
  496. return this->scale;
  497. }
  498. glm::ivec2 WaylandOutputViewport::bufferSize () const {
  499. // fractional-scale-v1: the buffer is the logical size times the scale, rounded half away from zero
  500. if (this->surfaceViewport && this->preferredScale120 > 0) {
  501. return (this->size * static_cast<int> (this->preferredScale120) + 60) / 120;
  502. }
  503. return this->size * this->scale;
  504. }
  505. void WaylandOutputViewport::updateBufferSize () {
  506. this->viewport = { 0, 0, this->bufferSize () };
  507. this->resize ();
  508. }
  509. void WaylandOutputViewport::resize () {
  510. if (!this->eglWindow) {
  511. return;
  512. }
  513. const glm::ivec2 buffer = this->bufferSize ();
  514. wl_egl_window_resize (this->eglWindow, buffer.x, buffer.y, 0, 0);
  515. this->getDriver ()->getOutput ().reset ();
  516. }