From 981d9392984d71bcea9dedd00a3f9a743dca643a Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 24 Feb 2018 23:05:20 -0800 Subject: [PATCH] More gracefully handle running out of texture space I manually checked this by setting TEX_SIZE to a small value and watching the log reallocate progressively larger sizes. --- Cargo.toml | 1 + src/gliumwindows.rs | 23 +++++++++++++++++++++-- src/main.rs | 2 ++ src/opengl/render.rs | 23 +++++++++-------------- src/opengl/textureatlas.rs | 16 +++++++++++++--- 5 files changed, 46 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 18a7026ca..e353dbebe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ bitflags = "1.0.1" clap = "2.30.0" euclid = "0.17.0" failure = "0.1.1" +failure_derive = "0.1.1" gl = "0.10.0" glium = "0.20.0" harfbuzz-sys = "0.1.15" diff --git a/src/gliumwindows.rs b/src/gliumwindows.rs index 91c551396..a09765a28 100644 --- a/src/gliumwindows.rs +++ b/src/gliumwindows.rs @@ -7,6 +7,7 @@ use font::FontConfiguration; use glium::{self, glutin}; use glium::glutin::{ElementState, MouseCursor}; use opengl::render::Renderer; +use opengl::textureatlas::OutOfTextureSpace; use pty::MasterPty; use std::io; use std::io::{Read, Write}; @@ -139,8 +140,26 @@ impl TerminalWindow { // Ensure that we finish() the target before we let the // error bubble up, otherwise we lose the context. target.finish().unwrap(); - res?; - Ok(()) + + // The only error we want to catch is texture space related; + // when that happens we need to blow our glyph cache and + // allocate a newer bigger texture. + match res { + Err(err) => { + match err.downcast_ref::() { + Some(&OutOfTextureSpace { size }) => { + eprintln!("out of texture space, allocating {}", size); + self.renderer.recreate_atlas(&self.host.display, size)?; + self.terminal.make_all_lines_dirty(); + // Recursively initiate a new paint + return self.paint(); + } + _ => (), + }; + Err(err) + } + Ok(_) => Ok(()), + } } pub fn try_read_pty(&mut self) -> Result<(), Error> { diff --git a/src/main.rs b/src/main.rs index 195436a66..58c7128db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,8 @@ extern crate core_text; extern crate euclid; #[macro_use] extern crate failure; +#[macro_use] +extern crate failure_derive; #[cfg(any(target_os = "android", all(unix, not(target_os = "macos"))))] extern crate fontconfig; // from servo-fontconfig #[cfg(any(target_os = "android", all(unix, not(target_os = "macos"))))] diff --git a/src/opengl/render.rs b/src/opengl/render.rs index c022465d7..83dbd086a 100644 --- a/src/opengl/render.rs +++ b/src/opengl/render.rs @@ -422,6 +422,13 @@ impl Renderer { }) } + pub fn recreate_atlas(&mut self, facade: &F, size: u32) -> Result<(), Error> { + let atlas = RefCell::new(Atlas::new(facade, size)?); + self.atlas = atlas; + self.glyph_cache = RefCell::new(HashMap::new()); + Ok(()) + } + pub fn resize(&mut self, facade: &F, width: u16, height: u16) -> Result<(), Error> { debug!("Renderer resize {},{}", width, height); @@ -504,21 +511,9 @@ impl Renderer { (glyph.width as u32, glyph.height as u32), ); - let tex = match self.atlas + let tex = self.atlas .borrow_mut() - .allocate(raw_im.width, raw_im.height, raw_im) - { - Ok(tex) => tex, - Err(size) => { - // TODO: this is a little tricky. We need to replace the texture - // atlas with a larger one, blow the font cache (that's the more - // tricky part) and arrange to re-render everything. - bail!( - "Ran out of space in the Atlas! Need to make another one of size {}", - size - ); - } - }; + .allocate(raw_im.width, raw_im.height, raw_im)?; let bearing_x = (glyph.bearing_x as f64 * scale) as isize; let bearing_y = (glyph.bearing_y as f64 * scale) as isize; diff --git a/src/opengl/textureatlas.rs b/src/opengl/textureatlas.rs index 94cd423d8..4d8a71675 100644 --- a/src/opengl/textureatlas.rs +++ b/src/opengl/textureatlas.rs @@ -8,6 +8,12 @@ use std::rc::Rc; pub const TEX_SIZE: u32 = 4096; +#[derive(Debug, Fail)] +#[fail(display = "Texture Size exceeded, need {}", size)] +pub struct OutOfTextureSpace { + pub size: u32, +} + /// Atlases are bitmaps of srgba data that are sized as a power of 2. /// We allocate sprites out of the available space, starting from the /// bottom left corner and working to the right until we run out of @@ -59,10 +65,12 @@ impl Atlas { width: u32, height: u32, data: T, - ) -> Result { + ) -> Result { if width > self.side || height > self.side { // It's not possible to satisfy that request - return Err(width.max(height).next_power_of_two()); + return Err(OutOfTextureSpace { + size: width.max(height).next_power_of_two(), + }); } let x_left = self.side - self.left; if x_left < width { @@ -76,7 +84,9 @@ impl Atlas { let y_left = self.side - self.bottom; if y_left < height { // No room at the inn. - return Err(self.side); + return Err(OutOfTextureSpace { + size: (self.side + width.max(height)).next_power_of_two(), + }); } let rect = Rect {