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.
This commit is contained in:
Wez Furlong committed 2018-02-24 23:05:20 -08:00
1 parent b81fb73c37
commit 981d939298
5 files changed
+46 -19

No files matched your search

+1
View File
@@ -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"
+21 -2
View File
@@ -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::<OutOfTextureSpace>() {
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> {
+2
View File
@@ -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"))))]
+9 -14
View File
@@ -422,6 +422,13 @@ impl Renderer {
})
}
pub fn recreate_atlas<F: Facade>(&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<F: Facade>(&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;
+13 -3
View File
@@ -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<Sprite, u32> {
) -> Result<Sprite, OutOfTextureSpace> {
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 {