Clear sprite cache when scale factor changes
This commit is contained in:
parent
23fbeaf978
commit
4002be882f
3 changed files with 31 additions and 10 deletions
|
@ -40,6 +40,7 @@ impl Renderer {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
device: metal::Device,
|
device: metal::Device,
|
||||||
pixel_format: metal::MTLPixelFormat,
|
pixel_format: metal::MTLPixelFormat,
|
||||||
|
scale_factor: f32,
|
||||||
fonts: Arc<dyn platform::FontSystem>,
|
fonts: Arc<dyn platform::FontSystem>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let library = device
|
let library = device
|
||||||
|
@ -64,7 +65,7 @@ impl Renderer {
|
||||||
MTLResourceOptions::StorageModeManaged,
|
MTLResourceOptions::StorageModeManaged,
|
||||||
);
|
);
|
||||||
|
|
||||||
let sprite_cache = SpriteCache::new(device.clone(), vec2i(1024, 768), fonts);
|
let sprite_cache = SpriteCache::new(device.clone(), vec2i(1024, 768), scale_factor, fonts);
|
||||||
let image_cache = ImageCache::new(device.clone(), vec2i(1024, 768));
|
let image_cache = ImageCache::new(device.clone(), vec2i(1024, 768));
|
||||||
let path_atlases =
|
let path_atlases =
|
||||||
AtlasAllocator::new(device.clone(), build_path_atlas_texture_descriptor());
|
AtlasAllocator::new(device.clone(), build_path_atlas_texture_descriptor());
|
||||||
|
@ -522,6 +523,8 @@ impl Renderer {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.sprite_cache.set_scale_factor(scale_factor);
|
||||||
|
|
||||||
let mut sprites_by_atlas = HashMap::new();
|
let mut sprites_by_atlas = HashMap::new();
|
||||||
|
|
||||||
for glyph in glyphs {
|
for glyph in glyphs {
|
||||||
|
@ -530,7 +533,6 @@ impl Renderer {
|
||||||
glyph.font_size,
|
glyph.font_size,
|
||||||
glyph.id,
|
glyph.id,
|
||||||
glyph.origin,
|
glyph.origin,
|
||||||
scale_factor,
|
|
||||||
) {
|
) {
|
||||||
// Snap sprite to pixel grid.
|
// Snap sprite to pixel grid.
|
||||||
let origin = (glyph.origin * scale_factor).floor() + sprite.offset.to_f32();
|
let origin = (glyph.origin * scale_factor).floor() + sprite.offset.to_f32();
|
||||||
|
|
|
@ -12,7 +12,6 @@ use std::{borrow::Cow, collections::HashMap, sync::Arc};
|
||||||
struct GlyphDescriptor {
|
struct GlyphDescriptor {
|
||||||
font_id: FontId,
|
font_id: FontId,
|
||||||
font_size: OrderedFloat<f32>,
|
font_size: OrderedFloat<f32>,
|
||||||
scale_factor: OrderedFloat<f32>,
|
|
||||||
glyph_id: GlyphId,
|
glyph_id: GlyphId,
|
||||||
subpixel_variant: (u8, u8),
|
subpixel_variant: (u8, u8),
|
||||||
}
|
}
|
||||||
|
@ -44,12 +43,14 @@ pub struct SpriteCache {
|
||||||
atlases: AtlasAllocator,
|
atlases: AtlasAllocator,
|
||||||
glyphs: HashMap<GlyphDescriptor, Option<GlyphSprite>>,
|
glyphs: HashMap<GlyphDescriptor, Option<GlyphSprite>>,
|
||||||
icons: HashMap<IconDescriptor, IconSprite>,
|
icons: HashMap<IconDescriptor, IconSprite>,
|
||||||
|
scale_factor: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SpriteCache {
|
impl SpriteCache {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
device: metal::Device,
|
device: metal::Device,
|
||||||
size: Vector2I,
|
size: Vector2I,
|
||||||
|
scale_factor: f32,
|
||||||
fonts: Arc<dyn platform::FontSystem>,
|
fonts: Arc<dyn platform::FontSystem>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let descriptor = TextureDescriptor::new();
|
let descriptor = TextureDescriptor::new();
|
||||||
|
@ -61,19 +62,29 @@ impl SpriteCache {
|
||||||
atlases: AtlasAllocator::new(device, descriptor),
|
atlases: AtlasAllocator::new(device, descriptor),
|
||||||
glyphs: Default::default(),
|
glyphs: Default::default(),
|
||||||
icons: Default::default(),
|
icons: Default::default(),
|
||||||
|
scale_factor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_scale_factor(&mut self, scale_factor: f32) {
|
||||||
|
if scale_factor != self.scale_factor {
|
||||||
|
self.icons.clear();
|
||||||
|
self.glyphs.clear();
|
||||||
|
self.atlases.clear();
|
||||||
|
}
|
||||||
|
self.scale_factor = scale_factor;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn render_glyph(
|
pub fn render_glyph(
|
||||||
&mut self,
|
&mut self,
|
||||||
font_id: FontId,
|
font_id: FontId,
|
||||||
font_size: f32,
|
font_size: f32,
|
||||||
glyph_id: GlyphId,
|
glyph_id: GlyphId,
|
||||||
target_position: Vector2F,
|
target_position: Vector2F,
|
||||||
scale_factor: f32,
|
|
||||||
) -> Option<GlyphSprite> {
|
) -> Option<GlyphSprite> {
|
||||||
const SUBPIXEL_VARIANTS: u8 = 4;
|
const SUBPIXEL_VARIANTS: u8 = 4;
|
||||||
|
|
||||||
|
let scale_factor = self.scale_factor;
|
||||||
let target_position = target_position * scale_factor;
|
let target_position = target_position * scale_factor;
|
||||||
let fonts = &self.fonts;
|
let fonts = &self.fonts;
|
||||||
let atlases = &mut self.atlases;
|
let atlases = &mut self.atlases;
|
||||||
|
@ -87,7 +98,6 @@ impl SpriteCache {
|
||||||
.entry(GlyphDescriptor {
|
.entry(GlyphDescriptor {
|
||||||
font_id,
|
font_id,
|
||||||
font_size: OrderedFloat(font_size),
|
font_size: OrderedFloat(font_size),
|
||||||
scale_factor: OrderedFloat(scale_factor),
|
|
||||||
glyph_id,
|
glyph_id,
|
||||||
subpixel_variant,
|
subpixel_variant,
|
||||||
})
|
})
|
||||||
|
|
|
@ -205,7 +205,12 @@ impl Window {
|
||||||
synthetic_drag_counter: 0,
|
synthetic_drag_counter: 0,
|
||||||
executor,
|
executor,
|
||||||
scene_to_render: Default::default(),
|
scene_to_render: Default::default(),
|
||||||
renderer: Renderer::new(device.clone(), PIXEL_FORMAT, fonts),
|
renderer: Renderer::new(
|
||||||
|
device.clone(),
|
||||||
|
PIXEL_FORMAT,
|
||||||
|
get_scale_factor(native_window),
|
||||||
|
fonts,
|
||||||
|
),
|
||||||
command_queue: device.new_command_queue(),
|
command_queue: device.new_command_queue(),
|
||||||
last_fresh_keydown: None,
|
last_fresh_keydown: None,
|
||||||
layer,
|
layer,
|
||||||
|
@ -405,10 +410,7 @@ impl platform::WindowContext for WindowState {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scale_factor(&self) -> f32 {
|
fn scale_factor(&self) -> f32 {
|
||||||
unsafe {
|
get_scale_factor(self.native_window)
|
||||||
let screen: id = msg_send![self.native_window, screen];
|
|
||||||
NSScreen::backingScaleFactor(screen) as f32
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn titlebar_height(&self) -> f32 {
|
fn titlebar_height(&self) -> f32 {
|
||||||
|
@ -427,6 +429,13 @@ impl platform::WindowContext for WindowState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_scale_factor(native_window: id) -> f32 {
|
||||||
|
unsafe {
|
||||||
|
let screen: id = msg_send![native_window, screen];
|
||||||
|
NSScreen::backingScaleFactor(screen) as f32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
unsafe fn get_window_state(object: &Object) -> Rc<RefCell<WindowState>> {
|
unsafe fn get_window_state(object: &Object) -> Rc<RefCell<WindowState>> {
|
||||||
let raw: *mut c_void = *object.get_ivar(WINDOW_STATE_IVAR);
|
let raw: *mut c_void = *object.get_ivar(WINDOW_STATE_IVAR);
|
||||||
let rc1 = Rc::from_raw(raw as *mut RefCell<WindowState>);
|
let rc1 = Rc::from_raw(raw as *mut RefCell<WindowState>);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue