diff --git a/Cargo.toml b/Cargo.toml index baf4a70cf..557a97967 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,10 @@ servo-fontconfig = "0.4.0" unicode-segmentation = "1.2.0" unicode-width = "0.1.4" vte = "0.3.2" -xcb = "0.8.1" + +[dependencies.xcb] +features = ["shm"] +version = "0.8.1" [dependencies.xcb-util] features = [ diff --git a/src/xgfx.rs b/src/xgfx.rs index 439a2c389..9513aca58 100644 --- a/src/xgfx.rs +++ b/src/xgfx.rs @@ -1,6 +1,9 @@ +use libc; use resize; use std::convert::From; +use std::io; use std::ops::Deref; +use std::ptr; use std::result; use xcb; use xcb_util; @@ -19,6 +22,7 @@ pub struct Connection { atom_protocols: xcb::Atom, atom_delete: xcb::Atom, keysyms: *mut xcb_key_symbols_t, + shm_available: bool, } impl Deref for Connection { @@ -41,12 +45,16 @@ impl Connection { let keysyms = unsafe { xcb_key_symbols_alloc(conn.get_raw_conn()) }; + let reply = xcb::shm::query_version(&conn).get_reply()?; + let shm_available = reply.shared_pixmaps(); + Ok(Connection { conn, screen_num, atom_protocols, atom_delete, keysyms, + shm_available, }) } @@ -170,43 +178,6 @@ impl<'a> Drop for Window<'a> { } } -pub struct Pixmap<'a> { - pixmap_id: xcb::xproto::Pixmap, - conn: &'a Connection, -} - -impl<'a> Drawable for Pixmap<'a> { - fn as_drawable(&self) -> xcb::xproto::Drawable { - self.pixmap_id - } - - fn get_conn(&self) -> &Connection { - self.conn - } -} - -impl<'a> Pixmap<'a> { - pub fn new(drawable: &Drawable, depth: u8, width: u16, height: u16) -> Result { - let conn = drawable.get_conn(); - let pixmap_id = conn.conn().generate_id(); - xcb::create_pixmap( - conn.conn(), - depth, - pixmap_id, - drawable.as_drawable(), - width, - height, - ).request_check()?; - Ok(Pixmap { conn, pixmap_id }) - } -} - -impl<'a> Drop for Pixmap<'a> { - fn drop(&mut self) { - xcb::free_pixmap(self.conn.conn(), self.pixmap_id); - } -} - pub struct Context<'a> { gc_id: xcb::xproto::Gcontext, conn: &'a Connection, @@ -625,3 +596,147 @@ impl BitmapImage for Image { (self.width, self.height) } } + +/// Holder for a shared memory segment id. +/// We hold on to the id only until the server has attached +/// (or failed to attach) to the segment. +/// The id is removed on Drop. +struct ShmId { + id: libc::c_int, +} + +/// Holder for a shared memory mapping. +/// The mapping is removed on Drop. +struct ShmData { + /// the base address of the mapping + data: *mut u8, +} + +impl ShmId { + /// Create a new private shared memory segment of the specified size + fn new(size: usize) -> Result { + let id = unsafe { libc::shmget(libc::IPC_PRIVATE, size, libc::IPC_CREAT | 0o600) }; + + if id == -1 { + bail!( + "shmget failed for {} bytes: {:?}", + size, + io::Error::last_os_error() + ); + } + + Ok(ShmId { id }) + } + + /// Attach the segment to our address space + fn attach(&self) -> Result { + let data = unsafe { libc::shmat(self.id, ptr::null(), 0) }; + if data as usize == !0 { + bail!("shmat failed: {:?} {}", data, io::Error::last_os_error()); + } + Ok(ShmData { data: data as *mut u8 }) + } +} + +impl Drop for ShmId { + fn drop(&mut self) { + unsafe { + libc::shmctl(self.id, libc::IPC_RMID, ptr::null_mut()); + } + } +} + +impl Drop for ShmData { + fn drop(&mut self) { + unsafe { + libc::shmdt(self.data as *const _); + } + } +} + +/// An image implementation backed by shared memory. +/// This also has an associated pixmap on the server side, +/// so we implement both BitmapImage and Drawable. +pub struct ShmImage<'a> { + data: ShmData, + seg_id: xcb::shm::Seg, + draw_id: u32, + conn: &'a Connection, + width: usize, + height: usize, +} + +impl<'a> ShmImage<'a> { + pub fn new( + conn: &Connection, + drawable: xcb::xproto::Drawable, + width: usize, + height: usize, + ) -> Result { + if !conn.shm_available { + bail!("SHM not available"); + } + + // Allocate and attach memory of the desired size + let id = ShmId::new(width * height * 4)?; + let data = id.attach()?; + + // Tell the server to attach to it + let seg_id = conn.generate_id(); + xcb::shm::attach_checked(conn, seg_id, id.id as u32, false) + .request_check()?; + + // Now create a pixmap that references it + let draw_id = conn.generate_id(); + xcb::shm::create_pixmap_checked( + conn, + draw_id, + drawable, + width as u16, + height as u16, + 24, // TODO: we need to get this from the conn->screen + seg_id, + 0, + ).request_check()?; + + Ok(ShmImage { + data, + seg_id, + draw_id, + conn, + width, + height, + }) + } +} + +impl<'a> BitmapImage for ShmImage<'a> { + unsafe fn pixel_data(&self) -> *const u8 { + self.data.data as *const u8 + } + + unsafe fn pixel_data_mut(&mut self) -> *mut u8 { + self.data.data + } + + fn image_dimensions(&self) -> (usize, usize) { + (self.width, self.height) + } +} + +impl<'a> Drop for ShmImage<'a> { + fn drop(&mut self) { + xcb::free_pixmap(self.conn, self.draw_id); + xcb::shm::detach(self.conn, self.seg_id); + } +} + +impl<'a> Drawable for ShmImage<'a> { + fn as_drawable(&self) -> xcb::xproto::Drawable { + self.draw_id + } + + fn get_conn(&self) -> &Connection { + self.conn + } +} diff --git a/src/xwin.rs b/src/xwin.rs index 3da01c3b2..ff8e87925 100644 --- a/src/xwin.rs +++ b/src/xwin.rs @@ -7,9 +7,55 @@ use std::process::Child; use std::slice; use term::{self, KeyCode, KeyModifiers}; use xcb; -use xgfx::{self, BitmapImage, Connection}; +use xgfx::{self, BitmapImage, Connection, Drawable}; use xkeysyms; +/// BufferImage is used to hold the bitmap of our rendered screen. +/// If SHM is available we store it there and save the overhead of +/// sending the bitmap to the server each time something is rendered. +/// Otherwise, we will send up portions of the bitmap each time something +/// on the screen changes. +enum BufferImage<'a> { + Shared(xgfx::ShmImage<'a>), + Image(xgfx::Image), +} + +impl<'a> BufferImage<'a> { + fn new(conn: &Connection, drawable: xcb::Drawable, width: usize, height: usize) -> BufferImage { + match xgfx::ShmImage::new(conn, drawable, width, height) { + Ok(shm) => BufferImage::Shared(shm), + Err(err) => { + debug!("falling back to local image because SHM says: {:?}", err); + BufferImage::Image(xgfx::Image::new(width, height)) + } + } + } +} + +/// Implement BitmapImage that delegates to the underlying image +impl<'a> BitmapImage for BufferImage<'a> { + unsafe fn pixel_data(&self) -> *const u8 { + match self { + &BufferImage::Shared(ref shm) => shm.pixel_data(), + &BufferImage::Image(ref im) => im.pixel_data(), + } + } + + unsafe fn pixel_data_mut(&mut self) -> *mut u8 { + match self { + &mut BufferImage::Shared(ref mut shm) => shm.pixel_data_mut(), + &mut BufferImage::Image(ref mut im) => im.pixel_data_mut(), + } + } + + fn image_dimensions(&self) -> (usize, usize) { + match self { + &BufferImage::Shared(ref shm) => shm.image_dimensions(), + &BufferImage::Image(ref im) => im.image_dimensions(), + } + } +} + pub struct TerminalWindow<'a> { window: xgfx::Window<'a>, conn: &'a Connection, @@ -20,7 +66,7 @@ pub struct TerminalWindow<'a> { cell_width: f64, descender: isize, window_context: xgfx::Context<'a>, - buffer_image: xgfx::Image, + buffer_image: BufferImage<'a>, need_paint: bool, terminal: term::Terminal, pty: MasterPty, @@ -43,7 +89,8 @@ impl<'a> TerminalWindow<'a> { window.set_title("wterm"); let window_context = xgfx::Context::new(conn, &window); - let buffer_image = xgfx::Image::new(width as usize, height as usize); + let buffer_image = + BufferImage::new(conn, window.as_drawable(), width as usize, height as usize); let descender = if descender.is_positive() { ((descender as f64) / 64.0).ceil() as isize @@ -76,9 +123,16 @@ impl<'a> TerminalWindow<'a> { pub fn resize_surfaces(&mut self, width: u16, height: u16) -> Result { if width != self.width || height != self.height { debug!("resize {},{}", width, height); - let mut buffer = xgfx::Image::new(width as usize, height as usize); + + let mut buffer = BufferImage::new( + self.conn, + self.window.as_drawable(), + width as usize, + height as usize, + ); buffer.draw_image(0, 0, &self.buffer_image, xgfx::Operator::Source); self.buffer_image = buffer; + self.width = width; self.height = height; @@ -97,21 +151,38 @@ impl<'a> TerminalWindow<'a> { pub fn expose(&mut self, x: u16, y: u16, width: u16, height: u16) -> Result<(), Error> { debug!("expose {},{}, {},{}", x, y, width, height); - if x == 0 && y == 0 && width == self.width && height == self.height { - self.window_context.put_image(0, 0, &self.buffer_image); - } else { - let mut im = xgfx::Image::new(width as usize, height as usize); - im.draw_image_subset( - 0, - 0, - x as usize, - y as usize, - width as usize, - height as usize, - &self.buffer_image, - xgfx::Operator::Source, - ); - self.window_context.put_image(x as i16, y as i16, &im); + + match &self.buffer_image { + &BufferImage::Shared(ref shm) => { + self.window_context.copy_area( + shm, + x as i16, + y as i16, + &self.window, + x as i16, + y as i16, + width, + height, + ); + } + &BufferImage::Image(ref buffer) => { + if x == 0 && y == 0 && width == self.width && height == self.height { + self.window_context.put_image(0, 0, buffer); + } else { + let mut im = xgfx::Image::new(width as usize, height as usize); + im.draw_image_subset( + 0, + 0, + x as usize, + y as usize, + width as usize, + height as usize, + buffer, + xgfx::Operator::Source, + ); + self.window_context.put_image(x as i16, y as i16, &im); + } + } } self.conn.flush(); @@ -285,8 +356,25 @@ impl<'a> TerminalWindow<'a> { // FIXME: we have to push the render to the server in case it // was the result of output from the process on the pty. It would // be nice to make this paint function only re-render the changed - // portions and send only those to the X server here. - self.window_context.put_image(0, 0, &self.buffer_image); + // portions and send only those to the X server here. This isn't + // so terrible when we have SHM available. + match &self.buffer_image { + &BufferImage::Shared(ref shm) => { + self.window_context.copy_area( + shm, + 0, + 0, + &self.window, + 0, + 0, + self.width, + self.height, + ); + } + &BufferImage::Image(ref buffer) => { + self.window_context.put_image(0, 0, buffer); + } + } Ok(()) }