Improve RefCell usage

Use a RefCell only where we need it, in the glyph cache. We don't need
it for the platform data.
This commit is contained in:
Simon Hausmann 2020-08-13 10:23:08 +02:00
parent 4606268eab
commit df2620838e
2 changed files with 10 additions and 14 deletions

View file

@ -10,22 +10,19 @@ type GlyphsByPixelSize = Vec<Rc<RefCell<CachedFontGlyphs>>>;
#[derive(Default)] #[derive(Default)]
pub(crate) struct GlyphCache { pub(crate) struct GlyphCache {
glyphs_by_font: HashMap<FontHandle, GlyphsByPixelSize>, glyphs_by_font: RefCell<HashMap<FontHandle, GlyphsByPixelSize>>,
} }
impl GlyphCache { impl GlyphCache {
pub fn find_font( pub fn find_font(&self, font_family: &str, pixel_size: f32) -> Rc<RefCell<CachedFontGlyphs>> {
&mut self,
font_family: &str,
pixel_size: f32,
) -> Rc<RefCell<CachedFontGlyphs>> {
let font = let font =
sixtyfps_corelib::font::FONT_CACHE.with(|fc| fc.find_font(font_family, pixel_size)); sixtyfps_corelib::font::FONT_CACHE.with(|fc| fc.find_font(font_family, pixel_size));
let font_handle = font.handle(); let font_handle = font.handle();
let mut glyphs_by_font = self.glyphs_by_font.borrow_mut();
let glyphs_by_pixel_size = let glyphs_by_pixel_size =
self.glyphs_by_font.entry(font_handle.clone()).or_insert(GlyphsByPixelSize::default()); glyphs_by_font.entry(font_handle.clone()).or_insert(GlyphsByPixelSize::default());
glyphs_by_pixel_size glyphs_by_pixel_size
.iter() .iter()

View file

@ -104,7 +104,7 @@ pub struct GLRenderer {
path_shader: PathShader, path_shader: PathShader,
image_shader: ImageShader, image_shader: ImageShader,
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
platform_data: Rc<RefCell<PlatformData>>, platform_data: Rc<PlatformData>,
texture_atlas: Rc<RefCell<TextureAtlas>>, texture_atlas: Rc<RefCell<TextureAtlas>>,
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
window: Rc<winit::window::Window>, window: Rc<winit::window::Window>,
@ -118,7 +118,7 @@ pub struct GLRenderingPrimitivesBuilder {
stroke_tesselator: StrokeTessellator, stroke_tesselator: StrokeTessellator,
texture_atlas: Rc<RefCell<TextureAtlas>>, texture_atlas: Rc<RefCell<TextureAtlas>>,
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
platform_data: Rc<RefCell<PlatformData>>, platform_data: Rc<PlatformData>,
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
window: Rc<winit::window::Window>, window: Rc<winit::window::Window>,
@ -131,7 +131,7 @@ pub struct GLFrame {
path_shader: PathShader, path_shader: PathShader,
image_shader: ImageShader, image_shader: ImageShader,
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
platform_data: Rc<RefCell<PlatformData>>, platform_data: Rc<PlatformData>,
root_matrix: cgmath::Matrix4<f32>, root_matrix: cgmath::Matrix4<f32>,
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
windowed_context: glutin::WindowedContext<glutin::PossiblyCurrent>, windowed_context: glutin::WindowedContext<glutin::PossiblyCurrent>,
@ -195,7 +195,7 @@ impl GLRenderer {
let path_shader = PathShader::new(&context); let path_shader = PathShader::new(&context);
let image_shader = ImageShader::new(&context); let image_shader = ImageShader::new(&context);
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
let platform_data = Rc::new(RefCell::new(PlatformData::new(&context))); let platform_data = Rc::new(PlatformData::new(&context));
GLRenderer { GLRenderer {
context, context,
@ -575,8 +575,7 @@ impl GLRenderingPrimitivesBuilder {
pixel_size: f32, pixel_size: f32,
color: Color, color: Color,
) -> GLRenderingPrimitive { ) -> GLRenderingPrimitive {
let mut pd = self.platform_data.borrow_mut(); let cached_glyphs = self.platform_data.glyph_cache.find_font(font_family, pixel_size);
let cached_glyphs = pd.glyph_cache.find_font(font_family, pixel_size);
let mut cached_glyphs = cached_glyphs.borrow_mut(); let mut cached_glyphs = cached_glyphs.borrow_mut();
let mut atlas = self.texture_atlas.borrow_mut(); let mut atlas = self.texture_atlas.borrow_mut();
let glyphs = cached_glyphs.layout_glyphs(&self.context, &mut atlas, text); let glyphs = cached_glyphs.layout_glyphs(&self.context, &mut atlas, text);
@ -791,7 +790,7 @@ impl GraphicsFrame for GLFrame {
let (r, g, b, a) = color.as_rgba_f32(); let (r, g, b, a) = color.as_rgba_f32();
for GlyphRun { vertices, texture_vertices, texture, vertex_count } in glyph_runs { for GlyphRun { vertices, texture_vertices, texture, vertex_count } in glyph_runs {
self.platform_data.borrow().glyph_shader.bind( self.platform_data.glyph_shader.bind(
&self.context, &self.context,
&to_gl_matrix(&matrix), &to_gl_matrix(&matrix),
&[r, g, b, a], &[r, g, b, a],