From 95cc66e9c031ddf2fc8632387787ecb1ebdd433f Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 9 Aug 2019 14:53:29 -0700 Subject: [PATCH] x11: maintain the image buffer across paint calls --- window/src/os/x11/connection.rs | 3 +-- window/src/os/x11/window.rs | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/window/src/os/x11/connection.rs b/window/src/os/x11/connection.rs index 2c26ff119..6311dff38 100644 --- a/window/src/os/x11/connection.rs +++ b/window/src/os/x11/connection.rs @@ -128,7 +128,7 @@ impl Connection { let mut events = Events::with_capacity(8); poll.register(self, tok_xcb, Ready::readable(), PollOpt::level())?; - let paint_interval = Duration::from_millis(50); + let paint_interval = Duration::from_millis(25); let mut last_interval = Instant::now(); while !*self.should_terminate.borrow() { @@ -298,7 +298,6 @@ impl Connection { } /// Run through all of the windows and cause them to paint if they need it. - /// This happens ~50ms or so. fn do_paint(&self) { for window in self.windows.borrow().values() { window.paint_if_needed().unwrap(); diff --git a/window/src/os/x11/window.rs b/window/src/os/x11/window.rs index ba3350fd7..9229ce69f 100644 --- a/window/src/os/x11/window.rs +++ b/window/src/os/x11/window.rs @@ -63,6 +63,7 @@ struct WindowInner { height: u16, expose: VecDeque, paint_all: bool, + buffer_image: BufferImage, } impl Drop for WindowInner { @@ -129,6 +130,19 @@ impl WindowInner { self.paint_all = false; self.expose.clear(); self.expose.push_back(window_dimensions); + } else if self.expose.is_empty() { + return Ok(()); + } + + let (buf_width, buf_height) = self.buffer_image.image_dimensions(); + if buf_width != self.width.into() || buf_height != self.height.into() { + // Window was resized, so we need to update our buffer + self.buffer_image = BufferImage::new( + &self.conn, + self.window_id, + self.width as usize, + self.height as usize, + ); } for rect in self.expose.drain(..) { @@ -145,20 +159,13 @@ impl WindowInner { eprintln!("paint {:?}", rect); - let mut buffer = BufferImage::new( - &self.conn, - self.window_id, - self.width as usize, - self.height as usize, - ); - let mut context = X11GraphicsContext { - buffer: &mut buffer, + buffer: &mut self.buffer_image, }; self.callbacks.paint(&mut context); - match &buffer { + match &self.buffer_image { BufferImage::Shared(ref im) => { self.window_context.copy_area( im, @@ -386,6 +393,8 @@ impl Window { let window_context = Context::new(&conn, &window_id); + let buffer_image = BufferImage::new(&conn, window_id, width, height); + Arc::new(Mutex::new(WindowInner { window_id, conn: Arc::clone(&conn), @@ -395,6 +404,7 @@ impl Window { height: height.try_into()?, expose: VecDeque::new(), paint_all: true, + buffer_image, })) };