diff --git a/src/xgfx.rs b/src/xgfx.rs index 8f1832a97..ab173ebbb 100644 --- a/src/xgfx.rs +++ b/src/xgfx.rs @@ -2,9 +2,6 @@ use egli; use gl; use glium; use glium::backend::Backend; -use libc; -use std::convert::From; -use std::io; use std::mem; use std::ops::Deref; use std::os; @@ -21,10 +18,6 @@ use failure::{self, Error}; pub type Result = result::Result; pub use xkeysyms::*; -use super::term::color::RgbColor; -use palette::Blend; -use palette::pixel::Srgb; - pub struct Connection { pub display: *mut x11::xlib::Display, conn: xcb::Connection, @@ -35,7 +28,6 @@ pub struct Connection { pub atom_xsel_data: xcb::Atom, pub atom_targets: xcb::Atom, keysyms: *mut xcb_key_symbols_t, - shm_available: bool, egl_display: Rc, egl_config: egli::FrameBufferConfigRef, } @@ -86,9 +78,6 @@ 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(); - let egl_display = egli::Display::from_display_id(display as *mut _).map_err( egli_err, )?; @@ -121,7 +110,6 @@ impl Connection { atom_protocols, atom_delete, keysyms, - shm_available, atom_utf8_string, atom_xsel_data, atom_targets, @@ -273,8 +261,8 @@ impl<'a> Window<'a> { // we're single threaded, so no need to check contexts false, if cfg!(debug_assertions) { - glium::debug::DebugCallbackBehavior::PrintAll - //glium::debug::DebugCallbackBehavior::DebugMessageOnError + //glium::debug::DebugCallbackBehavior::PrintAll + glium::debug::DebugCallbackBehavior::DebugMessageOnError } else { glium::debug::DebugCallbackBehavior::Ignore }, @@ -350,487 +338,3 @@ unsafe impl glium::backend::Backend for GlState { .expect("make_current failed"); } } - -/// A color stored as big endian bgra32 -#[derive(Copy, Clone, Debug)] -pub struct Color(u32); - -impl From for Color { - #[inline] - fn from(s: Srgb) -> Color { - let b: [u8; 4] = s.to_pixel(); - Color::rgba(b[0], b[1], b[2], b[3]) - } -} - -impl From for Srgb { - #[inline] - fn from(c: Color) -> Srgb { - Srgb::from_pixel(&c.as_rgba()) - } -} - -impl Color { - #[inline] - pub fn rgb(red: u8, green: u8, blue: u8) -> Color { - Color::rgba(red, green, blue, 0xff) - } - - #[inline] - pub fn rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Color { - let word = (blue as u32) << 24 | (green as u32) << 16 | (red as u32) << 8 | alpha as u32; - Color(word.to_be()) - } - - #[inline] - pub fn as_rgba(&self) -> (u8, u8, u8, u8) { - let host = u32::from_be(self.0); - ( - (host >> 8) as u8, - (host >> 16) as u8, - (host >> 24) as u8, - (host & 0xff) as u8, - ) - } - - /// Compute the composite of two colors according to the supplied operator. - /// self is the src operand, dest is the dest operand. - #[inline] - pub fn composite(&self, dest: Color, operator: &Operator) -> Color { - match operator { - &Operator::Over => { - let src: Srgb = (*self).into(); - let dest: Srgb = dest.into(); - Srgb::from_linear(src.to_linear().over(dest.to_linear())).into() - } - - &Operator::Source => *self, - - &Operator::Multiply => { - let src: Srgb = (*self).into(); - let dest: Srgb = dest.into(); - let result: Color = Srgb::from_linear(src.to_linear().multiply(dest.to_linear())) - .into(); - result.into() - } - - &Operator::MultiplyThenOver(ref tint) => { - // First multiply by the tint color. This colorizes the glyph. - let src: Srgb = (*self).into(); - let tint: Srgb = (*tint).into(); - let mut tinted = src.to_linear().multiply(tint.to_linear()); - // We take the alpha from the source. This is important because - // we're using Multiply to tint the glyph and if we don't reset the - // alpha we tend to end up with a background square of the tint color. - tinted.alpha = src.alpha; - - // Then blend the tinted glyph over the destination background - let dest: Srgb = dest.into(); - Srgb::from_linear(tinted.over(dest.to_linear())).into() - } - } - } -} - -impl From for Color { - #[inline] - fn from(color: RgbColor) -> Self { - Color::rgb(color.red, color.green, color.blue) - } -} - -/// Compositing operator. -/// We implement a small subset of possible compositing operators. -/// More information on these and their temrinology can be found -/// in the Cairo documentation here: -/// https://www.cairographics.org/operators/ -#[derive(Debug, Clone, Copy)] -pub enum Operator { - /// Apply the alpha channel of src and combine src with dest, - /// according to the classic OVER composite operator - Over, - /// Ignore dest; take src as the result of the operation - Source, - /// Multiply src x dest. The result is at least as dark as - /// the darker of the two input colors. This is used to - /// apply a color tint. - Multiply, - /// Multiply src with the provided color, then apply the - /// Over operator on the result with the dest as the dest. - /// This is used to colorize the src and then blend the - /// result into the destination. - MultiplyThenOver(Color), -} - -/// A bitmap in big endian bgra32 color format, with storage -/// in a Vec. -pub struct Image { - data: Vec, - width: usize, - height: usize, -} - -/// A bitmap in big endian bgra32 color format with abstract -/// storage filled in by the trait implementation. -pub trait BitmapImage { - /// Obtain a read only pointer to the pixel data - unsafe fn pixel_data(&self) -> *const u8; - - /// Obtain a mutable pointer to the pixel data - unsafe fn pixel_data_mut(&mut self) -> *mut u8; - - /// Return the pair (width, height) of the image, measured in pixels - fn image_dimensions(&self) -> (usize, usize); - - #[inline] - /// Obtain a mutable reference to the raw bgra pixel at the specified coordinates - fn pixel_mut(&mut self, x: usize, y: usize) -> &mut u32 { - let (width, height) = self.image_dimensions(); - assert!(x < width && y < height); - unsafe { - let offset = (y * width * 4) + (x * 4); - &mut *(self.pixel_data_mut().offset(offset as isize) as *mut u32) - } - } - - #[inline] - /// Read the raw bgra pixel at the specified coordinates - fn pixel(&self, x: usize, y: usize) -> u32 { - let (width, height) = self.image_dimensions(); - assert!(x < width && y < height); - unsafe { - let offset = (y * width * 4) + (x * 4); - *(self.pixel_data().offset(offset as isize) as *const u32) - } - } - - /// Clear the entire image to the specific color - fn clear(&mut self, color: Color) { - let (width, height) = self.image_dimensions(); - self.clear_rect(0, 0, width, height, color); - } - - fn clear_rect( - &mut self, - dest_x: isize, - dest_y: isize, - width: usize, - height: usize, - color: Color, - ) { - let (dim_width, dim_height) = self.image_dimensions(); - for y in 0..height { - let dest_y = y as isize + dest_y; - if dest_y < 0 { - continue; - } - if dest_y as usize >= dim_height { - break; - } - for x in 0..width { - let dest_x = x as isize + dest_x; - if dest_x < 0 { - continue; - } - if dest_x as usize >= dim_width { - break; - } - - *self.pixel_mut(dest_x as usize, dest_y as usize) = color.0; - } - } - } - - fn draw_vertical_line( - &mut self, - dest_x: isize, - dest_y: isize, - height: usize, - color: Color, - operator: Operator, - ) { - let (dim_width, dim_height) = self.image_dimensions(); - if dest_x < 0 || dest_x >= dim_width as isize { - return; - } - for y in 0..height { - let dest_y = y as isize + dest_y; - if dest_y < 0 { - continue; - } - if dest_y >= dim_height as isize { - break; - } - - let pix = self.pixel_mut(dest_x as usize, dest_y as usize); - *pix = color.composite(Color(*pix), &operator).0; - } - } - - fn draw_horizontal_line( - &mut self, - dest_x: isize, - dest_y: isize, - width: usize, - color: Color, - operator: Operator, - ) { - let (dim_width, dim_height) = self.image_dimensions(); - - if dest_y < 0 || dest_y >= dim_height as isize { - return; - } - - for x in 0..width { - let dest_x = x as isize + dest_x; - - if dest_x < 0 { - continue; - } - if dest_x >= dim_width as isize { - break; - } - - let pix = self.pixel_mut(dest_x as usize, dest_y as usize); - *pix = color.composite(Color(*pix), &operator).0; - } - } - - /// Draw a 1-pixel wide rectangle - fn draw_rect( - &mut self, - dest_x: isize, - dest_y: isize, - width: usize, - height: usize, - color: Color, - operator: Operator, - ) { - // Draw the vertical lines down either side - self.draw_vertical_line(dest_x, dest_y, height, color, operator); - self.draw_vertical_line(dest_x + width as isize, dest_y, height, color, operator); - - // And the horizontals for the top and bottom - self.draw_horizontal_line(dest_x, dest_y, width, color, operator); - self.draw_horizontal_line(dest_x, dest_y + height as isize, width, color, operator); - } - - fn draw_image(&mut self, dest_x: isize, dest_y: isize, im: &BitmapImage, operator: Operator) { - let (dest_width, dest_height) = im.image_dimensions(); - self.draw_image_subset(dest_x, dest_y, 0, 0, dest_width, dest_height, im, operator) - } - - fn draw_image_subset( - &mut self, - dest_x: isize, - dest_y: isize, - src_x: usize, - src_y: usize, - width: usize, - height: usize, - im: &BitmapImage, - operator: Operator, - ) { - let (dest_width, dest_height) = im.image_dimensions(); - let (dim_width, dim_height) = self.image_dimensions(); - assert!(width <= dest_width && height <= dest_height); - for y in src_y..src_y + height { - let dest_y = y as isize + dest_y - src_y as isize; - if dest_y < 0 { - continue; - } - if dest_y as usize >= dim_height { - break; - } - for x in src_x..src_x + width { - let dest_x = x as isize + dest_x - src_x as isize; - if dest_x < 0 { - continue; - } - if dest_x as usize >= dim_width { - break; - } - - let src = Color(im.pixel(x, y)); - let dst = self.pixel_mut(dest_x as usize, dest_y as usize); - *dst = src.composite(Color(*dst), &operator).0; - } - } - } -} - - -impl Image { - /// Create a new bgra32 image buffer with the specified dimensions. - /// The buffer is initialized to all zeroes. - pub fn new(width: usize, height: usize) -> Image { - let size = height * width * 4; - let mut data = Vec::with_capacity(size); - - data.resize(size, 0); - - Image { - data, - width, - height, - } - } -} - -impl BitmapImage for Image { - unsafe fn pixel_data(&self) -> *const u8 { - self.data.as_ptr() - } - - unsafe fn pixel_data_mut(&mut self) -> *mut u8 { - self.data.as_mut_ptr() - } - - fn image_dimensions(&self) -> (usize, usize) { - (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 8e36794a0..15a457fb8 100644 --- a/src/xwin.rs +++ b/src/xwin.rs @@ -19,7 +19,7 @@ use term::color::RgbColor; use term::hyperlink::Hyperlink; use xcb; use xcb_util; -use xgfx::{self, BitmapImage, Connection, Drawable}; +use xgfx::{self, Connection, Drawable}; use xkeysyms; type Transform2D = euclid::Transform2D; @@ -108,52 +108,6 @@ void main() { } "#; -/// 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(), - } - } -} - /// Holds the information we need to implement TerminalHost struct Host<'a> { window: xgfx::Window<'a>, @@ -171,7 +125,6 @@ pub struct TerminalWindow<'a> { cell_height: usize, cell_width: usize, descender: isize, - buffer_image: RefCell>, terminal: term::Terminal, process: Child, glyph_cache: RefCell>>, @@ -319,9 +272,6 @@ impl<'a> TerminalWindow<'a> { let window = xgfx::Window::new(&conn, width, height)?; window.set_title("wezterm"); - 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 } else { @@ -387,7 +337,6 @@ impl<'a> TerminalWindow<'a> { fill_program, glyph_vertex_buffer, line_vertex_buffer, - buffer_image: RefCell::new(buffer_image), conn, width, height, @@ -423,20 +372,6 @@ impl<'a> TerminalWindow<'a> { if width != self.width || height != self.height { debug!("resize {},{}", width, height); - let mut buffer = BufferImage::new( - self.conn, - self.host.window.as_drawable(), - width as usize, - height as usize, - ); - buffer.draw_image( - 0, - 0, - &*self.buffer_image.borrow_mut(), - xgfx::Operator::Source, - ); - self.buffer_image = RefCell::new(buffer); - self.width = width; self.height = height; self.projection = Self::compute_projection(width as f32, height as f32); @@ -450,32 +385,6 @@ impl<'a> TerminalWindow<'a> { self.host.pty.resize(rows, cols, width, height)?; self.terminal.resize(rows as usize, cols as usize); - // If we have partial rows or columns to the bottom or right, - // clear those out as they may contains artifacts from prior to - // the resize. - let background_color = self.palette.resolve( - &term::color::ColorAttribute::Background, - ); - self.buffer_image.borrow_mut().clear_rect( - cols as isize * self.cell_width as isize, - 0, - (width as usize).saturating_sub( - cols as usize * self.cell_width as usize, - ), - self.height as usize, - background_color.into(), - ); - self.buffer_image.borrow_mut().clear_rect( - 0, - rows as isize * self.cell_height as isize, - width as usize, - (height as usize).saturating_sub( - rows as usize * - self.cell_height as usize, - ), - background_color.into(), - ); - Ok(true) } else { debug!("ignoring extra resize");