1
0
Fork 0
forked from wry/wry

all: implement hardware cursors

This commit is contained in:
Julian Orth 2022-06-01 21:46:31 +02:00
parent 6cc97ee56e
commit 3b8935cf55
23 changed files with 614 additions and 91 deletions

View file

@ -38,6 +38,7 @@ use {
},
leaks::Tracker,
object::Object,
rect::Rect,
state::State,
tree::{
generic_node_visitor, ContainerNode, ContainerSplit, Direction, FloatNode, FoundNode,
@ -150,6 +151,7 @@ pub struct WlSeatGlobal {
desired_known_cursor: Cell<Option<KnownCursor>>,
changes: NumCell<u32>,
cursor_size: Cell<u32>,
hardware_cursor: Cell<bool>,
}
const CHANGE_CURSOR_MOVED: u32 = 1 << 0;
@ -202,6 +204,7 @@ impl WlSeatGlobal {
desired_known_cursor: Cell::new(None),
changes: NumCell::new(CHANGE_CURSOR_MOVED | CHANGE_TREE),
cursor_size: Cell::new(DEFAULT_CURSOR_SIZE),
hardware_cursor: Cell::new(state.globals.seats.len() == 0),
});
state.add_cursor_size(DEFAULT_CURSOR_SIZE);
let seat = slf.clone();
@ -218,6 +221,78 @@ impl WlSeatGlobal {
slf
}
pub fn set_hardware_cursor(&self, hardware_cursor: bool) {
self.hardware_cursor.set(hardware_cursor);
}
pub fn hardware_cursor(&self) -> bool {
self.hardware_cursor.get()
}
fn update_hardware_cursor_position(&self) {
self.update_hardware_cursor_(false);
}
pub fn update_hardware_cursor(&self) {
self.update_hardware_cursor_(true);
}
fn update_hardware_cursor_(&self, render: bool) {
if !self.hardware_cursor.get() {
return;
}
let cursor = match self.get_cursor() {
Some(c) => c,
_ => {
self.state.disable_hardware_cursors();
return;
}
};
if render {
cursor.tick();
}
let (x, y) = self.get_position();
for output in self.state.root.outputs.lock().values() {
if let Some(hc) = output.hardware_cursor.get() {
let scale = output.preferred_scale.get();
let extents = cursor.extents_at_scale(scale);
if render {
let (max_width, max_height) = hc.max_size();
if extents.width() > max_width || extents.height() > max_height {
hc.set_enabled(false);
hc.commit();
continue;
}
}
let opos = output.global.pos.get();
let (x_rel, y_rel);
if scale == 1 {
x_rel = x.round_down() - opos.x1();
y_rel = y.round_down() - opos.y1();
} else {
let scalef = scale.to_f64();
x_rel = ((x - Fixed::from_int(opos.x1())).to_f64() * scalef).round() as i32;
y_rel = ((y - Fixed::from_int(opos.y1())).to_f64() * scalef).round() as i32;
}
let mode = output.global.mode.get();
if extents
.intersects(&Rect::new_sized(-x_rel, -y_rel, mode.width, mode.height).unwrap())
{
if render {
let buffer = hc.get_buffer();
buffer.render_hardware_cursor(cursor.deref(), &self.state, scale);
hc.swap_buffer();
}
hc.set_enabled(true);
hc.set_position(x_rel + extents.x1(), y_rel + extents.y1());
} else {
hc.set_enabled(false);
}
hc.commit();
}
}
}
pub fn set_cursor_size(&self, size: u32) {
let old = self.cursor_size.replace(size);
if size != old {
@ -361,6 +436,7 @@ impl WlSeatGlobal {
pub fn set_position(&self, x: i32, y: i32) {
self.pos.set((Fixed::from_int(x), Fixed::from_int(y)));
self.update_hardware_cursor_position();
self.trigger_tree_changed();
let output = 'set_output: {
let outputs = self.state.outputs.lock();
@ -681,7 +757,9 @@ impl WlSeatGlobal {
if let Some(cursor) = cursor.as_ref() {
cursor.set_output(&self.output.get());
}
self.cursor.set(cursor);
self.cursor.set(cursor.clone());
self.state.hardware_tick_cursor.push(cursor);
self.update_hardware_cursor();
}
pub fn dnd_icon(&self) -> Option<Rc<WlSurface>> {

View file

@ -480,6 +480,7 @@ impl WlSeatGlobal {
fn set_new_position(self: &Rc<Self>, time_usec: u64, x: Fixed, y: Fixed) {
self.pos_time_usec.set(time_usec);
self.pos.set((x, y));
self.update_hardware_cursor_position();
self.changes.or_assign(CHANGE_CURSOR_MOVED);
self.apply_changes();
}

View file

@ -804,9 +804,6 @@ impl WlSurface {
}
self.buffer_abs_pos
.set(self.buffer_abs_pos.get().with_size(width, height).unwrap());
for (_, cursor) in &self.cursors {
cursor.handle_buffer_change();
}
}
{
let mut pfr = self.pending.frame_request.borrow_mut();
@ -832,6 +829,11 @@ impl WlSurface {
if self.need_extents_update.get() {
self.calculate_extents();
}
if buffer_transform_changed || transform_changed {
for (_, cursor) in &self.cursors {
cursor.handle_buffer_change();
}
}
ext.post_commit();
self.client.state.damage();
Ok(())

View file

@ -31,10 +31,12 @@ impl CursorSurface {
}
fn update_extents(&self) {
let extents = self.extents.get();
let (hot_x, hot_y) = self.hotspot.get();
self.extents
.set(Rect::new_sized(-hot_x, -hot_y, extents.width(), extents.height()).unwrap());
.set(self.surface.extents.get().move_(-hot_x, -hot_y));
if self.seat.hardware_cursor() {
self.seat.update_hardware_cursor();
}
}
pub fn handle_surface_destroy(&self) {
@ -42,9 +44,6 @@ impl CursorSurface {
}
pub fn handle_buffer_change(&self) {
let (width, height) = self.surface.buffer_abs_pos.get().size();
self.extents
.set(Rect::new_sized(0, 0, width, height).unwrap());
self.update_extents();
}
@ -62,22 +61,44 @@ impl CursorSurface {
impl Cursor for CursorSurface {
fn render(&self, renderer: &mut Renderer, x: Fixed, y: Fixed) {
let extents = self.extents.get().move_(x.round_down(), y.round_down());
let x_int = x.round_down();
let y_int = y.round_down();
let extents = self.extents.get().move_(x_int, y_int);
if extents.intersects(&renderer.logical_extents()) {
let (hot_x, hot_y) = self.hotspot.get();
let scale = renderer.scale();
if scale != 1 {
let scale = scale.to_f64();
let (hot_x, hot_y) = self.hotspot.get();
let (hot_x, hot_y) = (Fixed::from_int(hot_x), Fixed::from_int(hot_y));
let x = ((x - hot_x).to_f64() * scale).round() as _;
let y = ((y - hot_y).to_f64() * scale).round() as _;
renderer.render_surface_scaled(&self.surface, x, y, None);
} else {
renderer.render_surface(&self.surface, extents.x1(), extents.y1());
renderer.render_surface(&self.surface, x_int - hot_x, y_int - hot_y);
}
}
}
fn render_hardware_cursor(&self, renderer: &mut Renderer) {
let extents = self.surface.extents.get();
renderer.render_surface(&self.surface, -extents.x1(), -extents.y1());
}
fn extents_at_scale(&self, scale: Fixed) -> Rect {
let rect = self.extents.get();
if scale == 1 {
return rect;
}
let scale = scale.to_f64();
Rect::new(
(rect.x1() as f64 * scale).ceil() as _,
(rect.y1() as f64 * scale).ceil() as _,
(rect.x2() as f64 * scale).ceil() as _,
(rect.y2() as f64 * scale).ceil() as _,
)
.unwrap()
}
fn set_output(&self, output: &Rc<OutputNode>) {
self.surface.set_output(output);
}