Checkpoint

This commit is contained in:
Nathan Sobo 2023-10-03 20:19:59 -06:00
parent da211bef96
commit 1e0ff65337
7 changed files with 57 additions and 46 deletions

View file

@ -6,8 +6,8 @@ mod mac;
mod test;
use crate::{
AnyWindowHandle, Bounds, DevicePixels, Font, FontId, FontMetrics, GlyphId, GlyphRasterParams,
Pixels, Point, Result, Scene, ShapedLine, SharedString, Size,
AnyWindowHandle, Bounds, DevicePixels, Font, FontId, FontMetrics, GlyphId, Pixels, Point,
RenderGlyphParams, Result, Scene, ShapedLine, SharedString, Size,
};
use anyhow::anyhow;
use async_task::Runnable;
@ -147,7 +147,7 @@ pub trait PlatformWindow {
fn is_topmost_for_position(&self, position: Point<Pixels>) -> bool;
fn draw(&self, scene: Scene);
fn glyph_atlas(&self) -> Arc<dyn PlatformAtlas<GlyphRasterParams>>;
fn glyph_atlas(&self) -> Arc<dyn PlatformAtlas>;
}
pub trait PlatformDispatcher: Send + Sync {
@ -163,8 +163,8 @@ pub trait PlatformTextSystem: Send + Sync {
fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Bounds<f32>>;
fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>>;
fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
fn glyph_raster_bounds(&self, params: &GlyphRasterParams) -> Result<Bounds<DevicePixels>>;
fn rasterize_glyph(&self, params: &GlyphRasterParams) -> Result<(Size<DevicePixels>, Vec<u8>)>;
fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>>;
fn rasterize_glyph(&self, params: &RenderGlyphParams) -> Result<(Size<DevicePixels>, Vec<u8>)>;
fn layout_line(&self, text: &str, font_size: Pixels, runs: &[(usize, FontId)]) -> ShapedLine;
fn wrap_line(
&self,
@ -175,10 +175,22 @@ pub trait PlatformTextSystem: Send + Sync {
) -> Vec<usize>;
}
pub trait PlatformAtlas<Key>: Send + Sync {
#[derive(PartialEq, Eq, Hash, Clone)]
pub enum AtlasKey {
Glyph(RenderGlyphParams),
// Svg(RenderSvgParams),
}
impl From<RenderGlyphParams> for AtlasKey {
fn from(params: RenderGlyphParams) -> Self {
Self::Glyph(params)
}
}
pub trait PlatformAtlas: Send + Sync {
fn get_or_insert_with(
&self,
key: &Key,
key: &AtlasKey,
build: &mut dyn FnMut() -> Result<(Size<DevicePixels>, Vec<u8>)>,
) -> Result<AtlasTile>;

View file

@ -1,4 +1,6 @@
use crate::{AtlasTextureId, AtlasTile, Bounds, DevicePixels, PlatformAtlas, Point, Size};
use crate::{
AtlasKey, AtlasTextureId, AtlasTile, Bounds, DevicePixels, PlatformAtlas, Point, Size,
};
use anyhow::{anyhow, Result};
use collections::HashMap;
use derive_more::{Deref, DerefMut};
@ -7,11 +9,10 @@ use foreign_types::ForeignType;
use metal::{Device, TextureDescriptor};
use objc::{msg_send, sel, sel_impl};
use parking_lot::Mutex;
use std::hash::Hash;
pub struct MetalAtlas<Key>(Mutex<MetalAtlasState<Key>>);
pub struct MetalAtlas(Mutex<MetalAtlasState>);
impl<Key> MetalAtlas<Key> {
impl MetalAtlas {
pub fn new(
size: Size<DevicePixels>,
pixel_format: metal::MTLPixelFormat,
@ -34,20 +35,17 @@ impl<Key> MetalAtlas<Key> {
}
}
struct MetalAtlasState<Key> {
struct MetalAtlasState {
device: AssertSend<Device>,
texture_descriptor: AssertSend<TextureDescriptor>,
textures: Vec<MetalAtlasTexture>,
tiles_by_key: HashMap<Key, AtlasTile>,
tiles_by_key: HashMap<AtlasKey, AtlasTile>,
}
impl<Key> PlatformAtlas<Key> for MetalAtlas<Key>
where
Key: Clone + Eq + Hash + Send,
{
impl PlatformAtlas for MetalAtlas {
fn get_or_insert_with(
&self,
key: &Key,
key: &AtlasKey,
build: &mut dyn FnMut() -> Result<(Size<DevicePixels>, Vec<u8>)>,
) -> Result<AtlasTile> {
let mut lock = self.0.lock();
@ -75,7 +73,7 @@ where
}
}
impl<Key> MetalAtlasState<Key> {
impl MetalAtlasState {
fn push_texture(&mut self, min_size: Size<DevicePixels>) -> &mut MetalAtlasTexture {
let default_atlas_size = Size {
width: self.texture_descriptor.width().into(),

View file

@ -1,6 +1,5 @@
use crate::{
point, size, AtlasTextureId, DevicePixels, GlyphRasterParams, MetalAtlas, MonochromeSprite,
Quad, Scene, Size,
point, size, AtlasTextureId, DevicePixels, MetalAtlas, MonochromeSprite, Quad, Scene, Size,
};
use cocoa::{
base::{NO, YES},
@ -21,7 +20,7 @@ pub struct MetalRenderer {
sprites_pipeline_state: metal::RenderPipelineState,
unit_vertices: metal::Buffer,
instances: metal::Buffer,
glyph_atlas: Arc<MetalAtlas<GlyphRasterParams>>,
glyph_atlas: Arc<MetalAtlas>,
}
impl MetalRenderer {
@ -124,7 +123,7 @@ impl MetalRenderer {
&*self.layer
}
pub fn glyph_atlas(&self) -> &Arc<MetalAtlas<GlyphRasterParams>> {
pub fn glyph_atlas(&self) -> &Arc<MetalAtlas> {
&self.glyph_atlas
}

View file

@ -1,6 +1,6 @@
use crate::{
point, px, size, Bounds, DevicePixels, Font, FontFeatures, FontId, FontMetrics, FontStyle,
FontWeight, GlyphId, GlyphRasterParams, Pixels, PlatformTextSystem, Point, Result, ShapedGlyph,
FontWeight, GlyphId, Pixels, PlatformTextSystem, Point, RenderGlyphParams, Result, ShapedGlyph,
ShapedLine, ShapedRun, SharedString, Size, SUBPIXEL_VARIANTS,
};
use anyhow::anyhow;
@ -134,13 +134,13 @@ impl PlatformTextSystem for MacTextSystem {
self.0.read().glyph_for_char(font_id, ch)
}
fn glyph_raster_bounds(&self, params: &GlyphRasterParams) -> Result<Bounds<DevicePixels>> {
fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
self.0.read().raster_bounds(params)
}
fn rasterize_glyph(
&self,
glyph_id: &GlyphRasterParams,
glyph_id: &RenderGlyphParams,
) -> Result<(Size<DevicePixels>, Vec<u8>)> {
self.0.read().rasterize_glyph(glyph_id)
}
@ -234,7 +234,7 @@ impl MacTextSystemState {
})
}
fn raster_bounds(&self, params: &GlyphRasterParams) -> Result<Bounds<DevicePixels>> {
fn raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
let font = &self.fonts[params.font_id.0];
let scale = Transform2F::from_scale(params.scale_factor);
Ok(font
@ -248,7 +248,7 @@ impl MacTextSystemState {
.into())
}
fn rasterize_glyph(&self, params: &GlyphRasterParams) -> Result<(Size<DevicePixels>, Vec<u8>)> {
fn rasterize_glyph(&self, params: &RenderGlyphParams) -> Result<(Size<DevicePixels>, Vec<u8>)> {
let glyph_bounds = self.raster_bounds(params)?;
if glyph_bounds.size.width.0 == 0 || glyph_bounds.size.height.0 == 0 {
Err(anyhow!("glyph bounds are empty"))

View file

@ -1,10 +1,10 @@
use super::{ns_string, MetalRenderer, NSRange};
use crate::{
point, px, size, AnyWindowHandle, Bounds, Event, GlyphRasterParams, KeyDownEvent, Keystroke,
MacScreen, Modifiers, ModifiersChangedEvent, MouseButton, MouseDownEvent, MouseMovedEvent,
MouseUpEvent, NSRectExt, Pixels, Platform, PlatformAtlas, PlatformDispatcher,
PlatformInputHandler, PlatformScreen, PlatformWindow, Point, Scene, Size, Timer,
WindowAppearance, WindowBounds, WindowKind, WindowOptions, WindowPromptLevel,
point, px, size, AnyWindowHandle, Bounds, Event, KeyDownEvent, Keystroke, MacScreen, Modifiers,
ModifiersChangedEvent, MouseButton, MouseDownEvent, MouseMovedEvent, MouseUpEvent, NSRectExt,
Pixels, Platform, PlatformAtlas, PlatformDispatcher, PlatformInputHandler, PlatformScreen,
PlatformWindow, Point, Scene, Size, Timer, WindowAppearance, WindowBounds, WindowKind,
WindowOptions, WindowPromptLevel,
};
use block::ConcreteBlock;
use cocoa::{
@ -886,7 +886,7 @@ impl PlatformWindow for MacWindow {
}
}
fn glyph_atlas(&self) -> Arc<dyn PlatformAtlas<GlyphRasterParams>> {
fn glyph_atlas(&self) -> Arc<dyn PlatformAtlas> {
self.0.lock().renderer.glyph_atlas().clone()
}
}

View file

@ -215,13 +215,13 @@ impl TextSystem {
})
}
pub fn raster_bounds(&self, params: &GlyphRasterParams) -> Result<Bounds<DevicePixels>> {
pub fn raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
self.platform_text_system.glyph_raster_bounds(params)
}
pub fn rasterize_glyph(
&self,
glyph_id: &GlyphRasterParams,
glyph_id: &RenderGlyphParams,
) -> Result<(Size<DevicePixels>, Vec<u8>)> {
self.platform_text_system.rasterize_glyph(glyph_id)
}
@ -384,7 +384,7 @@ pub struct ShapedGlyph {
}
#[derive(Clone, Debug, PartialEq)]
pub struct GlyphRasterParams {
pub struct RenderGlyphParams {
pub(crate) font_id: FontId,
pub(crate) glyph_id: GlyphId,
pub(crate) font_size: Pixels,
@ -392,9 +392,9 @@ pub struct GlyphRasterParams {
pub(crate) scale_factor: f32,
}
impl Eq for GlyphRasterParams {}
impl Eq for RenderGlyphParams {}
impl Hash for GlyphRasterParams {
impl Hash for RenderGlyphParams {
fn hash<H: Hasher>(&self, state: &mut H) {
self.font_id.0.hash(state);
self.glyph_id.0.hash(state);

View file

@ -1,9 +1,9 @@
use crate::{
px, AnyView, AppContext, AvailableSpace, BorrowAppContext, Bounds, Context, Corners, Effect,
Element, EntityId, FontId, GlyphId, GlyphRasterParams, Handle, Hsla, IsZero, LayerId, LayoutId,
MainThread, MainThreadOnly, MonochromeSprite, Pixels, PlatformAtlas, PlatformWindow, Point,
Reference, ScaledPixels, Scene, Size, Style, TaffyLayoutEngine, WeakHandle, WindowOptions,
SUBPIXEL_VARIANTS,
Element, EntityId, FontId, GlyphId, Handle, Hsla, IsZero, LayerId, LayoutId, MainThread,
MainThreadOnly, MonochromeSprite, Pixels, PlatformAtlas, PlatformWindow, Point, Reference,
RenderGlyphParams, ScaledPixels, Scene, Size, Style, TaffyLayoutEngine, WeakHandle,
WindowOptions, SUBPIXEL_VARIANTS,
};
use anyhow::Result;
use futures::Future;
@ -16,7 +16,7 @@ pub struct AnyWindow {}
pub struct Window {
handle: AnyWindowHandle,
platform_window: MainThreadOnly<Box<dyn PlatformWindow>>,
glyph_atlas: Arc<dyn PlatformAtlas<GlyphRasterParams>>,
glyph_atlas: Arc<dyn PlatformAtlas>,
rem_size: Pixels,
content_size: Size<Pixels>,
layout_engine: TaffyLayoutEngine,
@ -222,7 +222,7 @@ impl<'a, 'w> WindowContext<'a, 'w> {
x: (glyph_origin.x.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8,
y: (glyph_origin.y.0.fract() * SUBPIXEL_VARIANTS as f32).floor() as u8,
};
let params = GlyphRasterParams {
let params = RenderGlyphParams {
font_id,
glyph_id,
font_size,
@ -236,7 +236,9 @@ impl<'a, 'w> WindowContext<'a, 'w> {
let tile = self
.window
.glyph_atlas
.get_or_insert_with(&params, &mut || self.text_system().rasterize_glyph(&params))?;
.get_or_insert_with(&params.clone().into(), &mut || {
self.text_system().rasterize_glyph(&params)
})?;
let bounds = Bounds {
origin: glyph_origin.map(|px| px.floor()) + raster_bounds.origin.map(Into::into),
size: tile.bounds.size.map(Into::into),