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?
This commit is contained in:
1 parent
ee3156888f
commit
7e714a5ca1
2 files changed
+53
-24
No files matched your search
+1
-1
@@ -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,
|
||||
|
||||
+52
-23
@@ -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<XConnection>,
|
||||
}
|
||||
|
||||
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<MouseCursor>,
|
||||
cursors: HashMap<Option<MouseCursor>, XcbCursor>,
|
||||
copy_and_paste: CopyAndPaste,
|
||||
#[cfg(feature = "opengl")]
|
||||
gl_state: Option<Rc<glium::backend::Context>>,
|
||||
@@ -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,
|
||||
}))
|
||||
|
||||
Reference in new issue
Block a user