From 7e714a5ca164bc6e818acab0638e450ef38865fc Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 21 Feb 2020 09:10:13 -0800 Subject: [PATCH] x11: avoid changing the mouse cursor glyph on each move @kalgynirae showed me weirdly laggy behavior when moving the mouse in front of his x11 window. My suspicion was that this is somehow related to updating the mouse cursor glyph, and looking at this code there were two things that might influence this: * We weren't saving the newly applied cursor value, so we'd create a new cursor every time the mouse moved (doh!) * We'd create a new cursor id each time it changed, and then destroy it (which isn't that bad, but if it contributes to lag, maybe it is?) This commit addresses both of these by making a little cache map from cursor type to cursor id. I can't observe a difference on my system, so I wonder if this might also be partially related to graphics drivers and hardware/software cursors? --- window/src/lib.rs | 2 +- window/src/os/x11/window.rs | 75 +++++++++++++++++++++++++------------ 2 files changed, 53 insertions(+), 24 deletions(-) diff --git a/window/src/lib.rs b/window/src/lib.rs index 5242c26cc..c3dd0d7a2 100644 --- a/window/src/lib.rs +++ b/window/src/lib.rs @@ -91,7 +91,7 @@ pub trait PaintContext { fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator); } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum MouseCursor { Arrow, Hand, diff --git a/window/src/os/x11/window.rs b/window/src/os/x11/window.rs index 06f4388a4..736762d07 100644 --- a/window/src/os/x11/window.rs +++ b/window/src/os/x11/window.rs @@ -11,10 +11,22 @@ use crate::{ use anyhow::anyhow; use promise::{Future, Promise}; use std::any::Any; -use std::collections::VecDeque; +use std::collections::{HashMap, VecDeque}; use std::convert::TryInto; use std::rc::Rc; use std::sync::{Arc, Mutex}; +use xcb::ffi::xcb_cursor_t; + +struct XcbCursor { + id: xcb_cursor_t, + conn: Rc, +} + +impl Drop for XcbCursor { + fn drop(&mut self) { + xcb::free_cursor(&self.conn, self.id); + } +} #[derive(Default)] struct CopyAndPaste { @@ -34,6 +46,7 @@ pub(crate) struct XWindowInner { paint_all: bool, buffer_image: BufferImage, cursor: Option, + cursors: HashMap, XcbCursor>, copy_and_paste: CopyAndPaste, #[cfg(feature = "opengl")] gl_state: Option>, @@ -217,28 +230,43 @@ impl XWindowInner { return Ok(()); } - let id_no = match cursor.unwrap_or(MouseCursor::Arrow) { - // `/usr/include/X11/cursorfont.h` - MouseCursor::Arrow => 132, - MouseCursor::Hand => 58, - MouseCursor::Text => 152, - }; + let cursor_id = match self.cursors.get(&cursor) { + Some(cursor) => cursor.id, + None => { + let id_no = match cursor.unwrap_or(MouseCursor::Arrow) { + // `/usr/include/X11/cursorfont.h` + MouseCursor::Arrow => 132, + MouseCursor::Hand => 58, + MouseCursor::Text => 152, + }; - let cursor_id: xcb::ffi::xcb_cursor_t = self.conn.generate_id(); - xcb::create_glyph_cursor( - &self.conn, - cursor_id, - self.conn.cursor_font_id, - self.conn.cursor_font_id, - id_no, - id_no + 1, - 0xffff, - 0xffff, - 0xffff, - 0, - 0, - 0, - ); + let cursor_id: xcb::ffi::xcb_cursor_t = self.conn.generate_id(); + xcb::create_glyph_cursor( + &self.conn, + cursor_id, + self.conn.cursor_font_id, + self.conn.cursor_font_id, + id_no, + id_no + 1, + 0xffff, + 0xffff, + 0xffff, + 0, + 0, + 0, + ); + + self.cursors.insert( + cursor, + XcbCursor { + id: cursor_id, + conn: Rc::clone(&self.conn), + }, + ); + + cursor_id + } + }; xcb::change_window_attributes( &self.conn, @@ -246,7 +274,7 @@ impl XWindowInner { &[(xcb::ffi::XCB_CW_CURSOR, cursor_id)], ); - xcb::free_cursor(&self.conn, cursor_id); + self.cursor = cursor; Ok(()) } @@ -687,6 +715,7 @@ impl XWindow { copy_and_paste: CopyAndPaste::default(), buffer_image, cursor: None, + cursors: HashMap::new(), #[cfg(feature = "opengl")] gl_state: None, }))