Skip to content

Commit

Permalink
Merge commit '9d5a3e984466813f9dfc8ecfbd122d2cbd631c0d' into fsundvik…
Browse files Browse the repository at this point in the history
…/glamour
  • Loading branch information
fredizzimo committed Jul 13, 2024
2 parents 72cd7d9 + 9d5a3e9 commit ee3329a
Show file tree
Hide file tree
Showing 15 changed files with 509 additions and 417 deletions.
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ use renderer::{cursor_renderer::CursorSettings, RendererSettings};
#[cfg_attr(target_os = "windows", allow(unused_imports))]
use settings::SETTINGS;
use window::{
create_event_loop, create_window, determine_window_size, main_loop, UserEvent, WindowSettings,
WindowSize,
create_event_loop, determine_window_size, main_loop, UserEvent, WindowSettings, WindowSize,
};

pub use channel_utils::*;
Expand Down Expand Up @@ -97,8 +96,7 @@ fn main() -> NeovideExitCode {
Err(err) => handle_startup_errors(err, event_loop).into(),
Ok((window_size, font_settings, _runtime)) => {
clipboard::init(&event_loop);
let window = create_window(&event_loop, &window_size);
main_loop(window, window_size, font_settings, event_loop).into()
main_loop(window_size, font_settings, event_loop).into()
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/renderer/d3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ use winit::{
use super::{vsync::VSyncWinSwapChain, RendererSettings, SkiaRenderer, VSync};
#[cfg(feature = "gpu_profiling")]
use crate::profiling::{d3d::create_d3d_gpu_context, GpuCtx};
use crate::{profiling::tracy_gpu_zone, settings::SETTINGS, window::UserEvent};
use crate::{
profiling::{tracy_gpu_zone, tracy_zone},
settings::SETTINGS,
window::UserEvent,
};

fn get_hardware_adapter(factory: &IDXGIFactory4) -> Result<IDXGIAdapter1> {
tracy_zone!("get_hardware_adapter");
for i in 0.. {
let adapter = unsafe { factory.EnumAdapters1(i)? };
let mut desc = Default::default();
Expand Down Expand Up @@ -97,6 +102,7 @@ pub struct D3DSkiaRenderer {

impl D3DSkiaRenderer {
pub fn new(window: Window) -> Self {
tracy_zone!("D3DSkiaRenderer::new");
#[cfg(feature = "d3d_debug")]
unsafe {
let mut debug_controller: Option<ID3D12Debug> = None;
Expand All @@ -117,6 +123,7 @@ impl D3DSkiaRenderer {

let mut device: Option<ID3D12Device> = None;
unsafe {
tracy_zone!("create_device");
D3D12CreateDevice(&adapter, D3D_FEATURE_LEVEL_11_0, &mut device)
.expect("Failed to create a Direct3D 12 device");
}
Expand Down Expand Up @@ -167,6 +174,7 @@ impl D3DSkiaRenderer {
};

let swap_chain = unsafe {
tracy_zone!("create swap_chain");
dxgi_factory
.CreateSwapChainForComposition(&command_queue, &swap_chain_desc, None)
.expect("Failed to create the Direct3D swap chain")
Expand Down Expand Up @@ -232,6 +240,7 @@ impl D3DSkiaRenderer {
protected_context: Protected::No,
};
let gr_context = unsafe {
tracy_zone!("create skia context");
DirectContext::new_d3d(&backend_context, None).expect("Failed to create Skia context")
};

Expand Down Expand Up @@ -314,6 +323,7 @@ impl D3DSkiaRenderer {
}

fn setup_surfaces(&mut self) {
tracy_zone!("setup_surfaces");
let size = self.window.inner_size();
let size = (
size.width.try_into().expect("Could not convert width"),
Expand Down
8 changes: 7 additions & 1 deletion src/renderer/fonts/caching_shaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl CachingShaper {
}

fn reset_font_loader(&mut self) {
tracy_zone!("reset_font_loader");
self.font_info = None;
let font_size = self.current_size();

Expand Down Expand Up @@ -210,7 +211,12 @@ impl CachingShaper {
}

pub fn underline_position(&mut self) -> f32 {
self.metrics().underline_offset
let metrics = self.metrics();
metrics.ascent - metrics.underline_offset
}

pub fn stroke_size(&mut self) -> f32 {
self.metrics().stroke_size
}

pub fn y_adjustment(&mut self) -> f32 {
Expand Down
12 changes: 8 additions & 4 deletions src/renderer/fonts/font_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ use log::trace;
use lru::LruCache;
use skia_safe::{font::Edging as SkiaEdging, Data, Font, FontHinting as SkiaHinting, FontMgr};

use crate::renderer::fonts::font_options::{FontEdging, FontHinting};
use crate::renderer::fonts::swash_font::SwashFont;

use super::font_options::{CoarseStyle, FontDescription};
use crate::{
profiling::tracy_zone,
renderer::fonts::{
font_options::{CoarseStyle, FontDescription, FontEdging, FontHinting},
swash_font::SwashFont,
},
};

static DEFAULT_FONT: &[u8] = include_bytes!("../../../assets/fonts/FiraCodeNerdFont-Regular.ttf");
static LAST_RESORT_FONT: &[u8] = include_bytes!("../../../assets/fonts/LastResort-Regular.ttf");
Expand Down Expand Up @@ -87,6 +90,7 @@ impl FontLoader {
}

fn load(&mut self, font_key: FontKey) -> Option<FontPair> {
tracy_zone!("load_font");
trace!("Loading font {:?}", font_key);
if let Some(desc) = &font_key.font_desc {
let (family, style) = desc.as_family_and_font_style();
Expand Down
46 changes: 26 additions & 20 deletions src/renderer/grid_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,12 @@ impl GridRenderer {
let region = self.compute_text_region(clip_position, cell_width + 2);

if let Some(underline_style) = style.underline {
let line_position = self.grid_scale.height() - self.shaper.underline_position();
let p1 = pos + PixelVec::new(0.0, line_position);
let p2 = pos + PixelVec::new(width, line_position);
let stroke_size = self.shaper.stroke_size();
let underline_position = self.shaper.underline_position();
let p1 = pos + PixelVec::new(0.0, underline_position);
let p2 = pos + PixelVec::new(width, underline_position);

self.draw_underline(canvas, style, underline_style, p1, p2);
self.draw_underline(canvas, style, underline_style, stroke_size, p1, p2);
drawn = true;
}

Expand Down Expand Up @@ -231,6 +232,7 @@ impl GridRenderer {
canvas: &Canvas,
style: &Arc<Style>,
underline_style: UnderlineStyle,
stroke_size: f32,
p1: PixelPos<f32>,
p2: PixelPos<f32>,
) {
Expand All @@ -241,9 +243,13 @@ impl GridRenderer {
underline_paint.set_anti_alias(false);
underline_paint.set_blend_mode(BlendMode::SrcOver);
let underline_stroke_scale = SETTINGS.get::<RendererSettings>().underline_stroke_scale;
// If the stroke width is less than one, clamp it to one otherwise we get nasty aliasing
// issues
let stroke_width = (self.shaper.current_size() * underline_stroke_scale / 10.).max(1.);
// clamp to 1 and round to avoid aliasing issues
let stroke_width = (stroke_size * underline_stroke_scale).max(1.).round();

// offset y by width / 2 to align the *top* of the underline with p1 and p2
// also round to avoid aliasing issues
let p1 = (p1.x.round(), (p1.y + stroke_width / 2.).round());
let p2 = (p2.x.round(), (p2.y + stroke_width / 2.).round());

underline_paint
.set_color(style.special(&self.default_style.colors).to_color())
Expand All @@ -252,31 +258,31 @@ impl GridRenderer {
match underline_style {
UnderlineStyle::Underline => {
underline_paint.set_path_effect(None);
canvas.draw_line(to_skia_point(p1), to_skia_point(p2), &underline_paint);
canvas.draw_line(p1, p2, &underline_paint);
}
UnderlineStyle::UnderDouble => {
underline_paint.set_path_effect(None);
canvas.draw_line(to_skia_point(p1), to_skia_point(p2), &underline_paint);
let p1 = (p1.x, p1.y - 2.);
let p2 = (p2.x, p2.y - 2.);
canvas.draw_line(p1, p2, &underline_paint);
let p1 = (p1.0, p1.1 + 2. * stroke_width);
let p2 = (p2.0, p2.1 + 2. * stroke_width);
canvas.draw_line(p1, p2, &underline_paint);
}
UnderlineStyle::UnderCurl => {
let p1 = (p1.x, p1.y - 3. + stroke_width);
let p2 = (p2.x, p2.y - 3. + stroke_width);
let p1 = (p1.0, p1.1 + stroke_width);
let p2 = (p2.0, p2.1 + stroke_width);
underline_paint
.set_path_effect(None)
.set_anti_alias(true)
.set_style(skia_safe::paint::Style::Stroke);
let mut path = Path::default();
path.move_to(p1);
let mut i = p1.0;
let mut sin = -2. * stroke_width;
let increment = self.grid_scale.width() / 2.;
while i < p2.0 {
let dx = self.grid_scale.width() / 2.;
let count = ((p2.0 - p1.0) / dx).round();
let dy = (p2.1 - p1.1) / count;
for _ in 0..(count as i32) {
sin *= -1.;
i += increment;
path.quad_to((i - (increment / 2.), p1.1 + sin), (i, p1.1));
path.r_quad_to((dx / 2., sin), (dx, dy));
}
canvas.draw_path(&path, &underline_paint);
}
Expand All @@ -285,14 +291,14 @@ impl GridRenderer {
&[6.0 * stroke_width, 2.0 * stroke_width],
0.0,
));
canvas.draw_line(to_skia_point(p1), to_skia_point(p2), &underline_paint);
canvas.draw_line(p1, p2, &underline_paint);
}
UnderlineStyle::UnderDot => {
underline_paint.set_path_effect(dash_path_effect::new(
&[1.0 * stroke_width, 1.0 * stroke_width],
0.0,
));
canvas.draw_line(to_skia_point(p1), to_skia_point(p2), &underline_paint);
canvas.draw_line(p1, p2, &underline_paint);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use skia_safe::Canvas;

use winit::{
event::Event,
event_loop::{EventLoop, EventLoopProxy},
event_loop::{EventLoopProxy, EventLoopWindowTarget},
window::{Window, WindowBuilder},
};

Expand Down Expand Up @@ -526,7 +526,7 @@ pub struct WindowConfig {

pub fn build_window_config<TE>(
winit_window_builder: WindowBuilder,
event_loop: &EventLoop<TE>,
event_loop: &EventLoopWindowTarget<TE>,
) -> WindowConfig {
#[cfg(target_os = "windows")]
{
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/opengl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use skia_safe::{
};
use winit::{
dpi::PhysicalSize,
event_loop::{EventLoop, EventLoopProxy},
event_loop::{EventLoopProxy, EventLoopWindowTarget},
window::{Window, WindowBuilder},
};

Expand Down Expand Up @@ -243,7 +243,7 @@ fn gen_config(mut config_iterator: Box<dyn Iterator<Item = Config> + '_>) -> Con

pub fn build_window<TE>(
winit_window_builder: WindowBuilder,
event_loop: &EventLoop<TE>,
event_loop: &EventLoopWindowTarget<TE>,
) -> WindowConfig {
let template_builder = ConfigTemplateBuilder::new()
.with_stencil_size(8)
Expand Down
5 changes: 4 additions & 1 deletion src/settings/window_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ pub fn load_last_window_settings() -> Result<PersistentWindowSettings, String> {
}

pub fn save_window_size(window_wrapper: &WinitWindowWrapper) {
let window = window_wrapper.skia_renderer.window();
if window_wrapper.skia_renderer.is_none() {
return;
}
let window = window_wrapper.skia_renderer.as_ref().unwrap().window();
// Don't save the window size when the window is minimized, since the size can be 0
// Note wayland can't determine this
if window.is_minimized() == Some(true) {
Expand Down
Loading

0 comments on commit ee3329a

Please sign in to comment.