Skip to content

Commit

Permalink
Sync sizes (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
yutannihilation authored Oct 18, 2024
1 parent a53535c commit 4be02cc
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 201 deletions.
4 changes: 2 additions & 2 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ macro_rules! add_tracing_point {

#[savvy]
fn vellogd_impl(filename: &str, width: f64, height: f64) -> savvy::Result<()> {
let device_driver = VelloGraphicsDevice::new(filename, height)?;
let device_driver = VelloGraphicsDevice::new(filename, width, height)?;

// TODO: the actual width and height is kept on the server's side.
let device_descriptor = DeviceDescriptor::new(width, height);
Expand All @@ -46,7 +46,7 @@ fn vellogd_with_server_impl(
height: f64,
server: Option<&str>,
) -> savvy::Result<()> {
let device_driver = VelloGraphicsDeviceWithServer::new(filename, server)?;
let device_driver = VelloGraphicsDeviceWithServer::new(filename, server, width, height)?;

// TODO: the actual width and height is kept on the server's side.
let device_descriptor = DeviceDescriptor::new(width, height);
Expand Down
44 changes: 24 additions & 20 deletions src/rust/src/vello_device/default.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::atomic::Ordering;

use super::xy_to_path;
use super::WindowController;
use crate::add_tracing_point;
Expand All @@ -10,34 +12,33 @@ use vellogd_shared::protocol::Response;
use vellogd_shared::protocol::StrokeParams;
use vellogd_shared::text_layouter::TextLayouter;
use vellogd_shared::text_layouter::TextMetric;
use vellogd_shared::winit_app::EVENT_LOOP;
use vellogd_shared::winit_app::VELLO_APP_PROXY;

pub struct VelloGraphicsDevice {
filename: String,
layout: parley::Layout<peniko::Brush>,
height: f64, // TODO
}

impl VelloGraphicsDevice {
pub(crate) fn new(filename: &str, height: f64) -> savvy::Result<Self> {
pub(crate) fn new(filename: &str, width: f64, height: f64) -> savvy::Result<Self> {
VELLO_APP_PROXY.set_size(width as u32, height as u32);
Ok(Self {
filename: filename.into(),
layout: parley::Layout::new(),
height,
})
}
}

impl WindowController for VelloGraphicsDevice {
fn send_event(&self, event: Request) -> savvy::Result<()> {
EVENT_LOOP
.event_loop
VELLO_APP_PROXY
.tx
.send_event(event)
.map_err(|e| format!("Failed to send event {e:?}").into())
}

fn recv_response(&self) -> savvy::Result<Response> {
EVENT_LOOP
VELLO_APP_PROXY
.rx
.lock()
.unwrap()
Expand Down Expand Up @@ -99,7 +100,7 @@ impl DeviceDriver for VelloGraphicsDevice {
let fill_params = FillParams::from_gc(gc);
let stroke_params = StrokeParams::from_gc(gc);
if fill_params.is_some() || stroke_params.is_some() {
EVENT_LOOP
VELLO_APP_PROXY
.scene
.draw_circle(center.into(), r, fill_params, stroke_params);
}
Expand All @@ -109,7 +110,7 @@ impl DeviceDriver for VelloGraphicsDevice {
add_tracing_point!();

if let Some(stroke_params) = StrokeParams::from_gc(gc) {
EVENT_LOOP
VELLO_APP_PROXY
.scene
.draw_line(from.into(), to.into(), stroke_params);
}
Expand All @@ -121,7 +122,7 @@ impl DeviceDriver for VelloGraphicsDevice {
let fill_params = FillParams::from_gc(gc);
let stroke_params = StrokeParams::from_gc(gc);
if fill_params.is_some() || stroke_params.is_some() {
EVENT_LOOP
VELLO_APP_PROXY
.scene
.draw_polygon(xy_to_path(x, y, true), fill_params, stroke_params);
}
Expand All @@ -132,7 +133,7 @@ impl DeviceDriver for VelloGraphicsDevice {

let stroke_params = StrokeParams::from_gc(gc);
if let Some(stroke_params) = stroke_params {
EVENT_LOOP
VELLO_APP_PROXY
.scene
.draw_polyline(xy_to_path(x, y, false), stroke_params);
}
Expand All @@ -144,7 +145,7 @@ impl DeviceDriver for VelloGraphicsDevice {
let fill_params = FillParams::from_gc(gc);
let stroke_params = StrokeParams::from_gc(gc);
if fill_params.is_some() || stroke_params.is_some() {
EVENT_LOOP
VELLO_APP_PROXY
.scene
.draw_rect(from.into(), to.into(), fill_params, stroke_params);
}
Expand Down Expand Up @@ -177,10 +178,12 @@ impl DeviceDriver for VelloGraphicsDevice {
let lineheight = gc.lineheight as f32;
self.build_layout(text, size, lineheight);

let width = self.layout.width() as f64;
let transform = vello::kurbo::Affine::translate((-(width * hadj), 0.0))
let layout_width = self.layout.width() as f64;
let window_height = VELLO_APP_PROXY.height.load(Ordering::Relaxed) as f64;

let transform = vello::kurbo::Affine::translate((-(layout_width * hadj), 0.0))
.then_rotate(-angle.to_radians())
.then_translate((pos.0, self.height - pos.1).into()); // Y-axis is flipped
.then_translate((pos.0, window_height - pos.1).into()); // Y-axis is flipped

for line in self.layout.lines() {
let vadj = line.metrics().ascent * 0.5;
Expand All @@ -191,7 +194,7 @@ impl DeviceDriver for VelloGraphicsDevice {
};

// TODO: do not lock per glyph
EVENT_LOOP
VELLO_APP_PROXY
.scene
.draw_glyph(glyph_run, color, transform, vadj);
}
Expand Down Expand Up @@ -229,12 +232,13 @@ impl DeviceDriver for VelloGraphicsDevice {
// unsafe { R_NilValue }
// }

fn size(&mut self, dd: DevDesc) -> (f64, f64, f64, f64) {
fn size(&mut self, _: DevDesc) -> (f64, f64, f64, f64) {
add_tracing_point!();

// let sizes = self.get_window_sizes().unwrap_or((0, 0));
// (0.0, sizes.0 as _, 0.0, sizes.1 as _)
(dd.left, dd.right, dd.bottom, dd.top)
let width = VELLO_APP_PROXY.width.load(Ordering::Relaxed) as f64;
let height = VELLO_APP_PROXY.height.load(Ordering::Relaxed) as f64;

(0.0, width, 0.0, height)
}

fn char_metric(&mut self, c: char, gc: R_GE_gcontext, _: DevDesc) -> TextMetric {
Expand Down
2 changes: 1 addition & 1 deletion src/rust/src/vello_device/no_winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::graphics::DeviceDriver;
pub struct VelloGraphicsDevice {}

impl VelloGraphicsDevice {
pub(crate) fn new(filename: &str, height: f64) -> savvy::Result<Self> {
pub(crate) fn new(filename: &str, _width: f64, _height: f64) -> savvy::Result<Self> {
Err("This method is not supported on macOS".into())
}
}
Expand Down
42 changes: 38 additions & 4 deletions src/rust/src/vello_device/with_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use vellogd_shared::{
text_layouter::{TextLayouter, TextMetric},
};

use crate::graphics::DeviceDriver;
use crate::{add_tracing_point, graphics::DeviceDriver};

use super::{xy_to_path, WindowController};

Expand All @@ -26,15 +26,23 @@ impl Drop for VelloGraphicsDeviceWithServer {
}

impl VelloGraphicsDeviceWithServer {
pub(crate) fn new(filename: &str, server: Option<&str>) -> savvy::Result<Self> {
pub(crate) fn new(
filename: &str,
server: Option<&str>,
width: f64,
height: f64,
) -> savvy::Result<Self> {
// server -> controller
let (rx_server, rx_server_name) = IpcOneShotServer::<Response>::new().unwrap();

let server_process = if let Some(server_bin) = server {
// spawn a server process
let res = std::process::Command::new(server_bin)
.arg(rx_server_name)
// .stdout(std::process::Stdio::piped())
.args([
rx_server_name,
(width as u32).to_string(),
(height as u32).to_string(),
])
.spawn();

match res {
Expand Down Expand Up @@ -97,10 +105,14 @@ impl TextLayouter for VelloGraphicsDeviceWithServer {

impl DeviceDriver for VelloGraphicsDeviceWithServer {
fn activate(&mut self, _: DevDesc) {
add_tracing_point!();

self.request_new_window().unwrap();
}

fn close(&mut self, _: DevDesc) {
add_tracing_point!();

self.request_close_window().unwrap();
}

Expand All @@ -111,13 +123,17 @@ impl DeviceDriver for VelloGraphicsDeviceWithServer {
// fn mode(&mut self, mode: i32, _: DevDesc) {}

fn new_page(&mut self, _: R_GE_gcontext, _: DevDesc) {
add_tracing_point!();

self.request_new_page().unwrap();
}

// TODO
// fn clip(&mut self, from: (f64, f64), to: (f64, f64), _: DevDesc) {}

fn circle(&mut self, center: (f64, f64), r: f64, gc: R_GE_gcontext, _: DevDesc) {
add_tracing_point!();

let fill_params = FillParams::from_gc(gc);
let stroke_params = StrokeParams::from_gc(gc);
if fill_params.is_some() || stroke_params.is_some() {
Expand All @@ -132,6 +148,8 @@ impl DeviceDriver for VelloGraphicsDeviceWithServer {
}

fn line(&mut self, from: (f64, f64), to: (f64, f64), gc: R_GE_gcontext, _: DevDesc) {
add_tracing_point!();

if let Some(stroke_params) = StrokeParams::from_gc(gc) {
self.send_event(Request::DrawLine {
p0: from.into(),
Expand All @@ -143,6 +161,8 @@ impl DeviceDriver for VelloGraphicsDeviceWithServer {
}

fn polygon(&mut self, x: &[f64], y: &[f64], gc: R_GE_gcontext, _: DevDesc) {
add_tracing_point!();

let fill_params = FillParams::from_gc(gc);
let stroke_params = StrokeParams::from_gc(gc);
if fill_params.is_some() || stroke_params.is_some() {
Expand All @@ -156,6 +176,8 @@ impl DeviceDriver for VelloGraphicsDeviceWithServer {
}

fn polyline(&mut self, x: &[f64], y: &[f64], gc: R_GE_gcontext, _: DevDesc) {
add_tracing_point!();

let stroke_params = StrokeParams::from_gc(gc);
if let Some(stroke_params) = stroke_params {
self.send_event(Request::DrawPolyline {
Expand All @@ -167,6 +189,8 @@ impl DeviceDriver for VelloGraphicsDeviceWithServer {
}

fn rect(&mut self, from: (f64, f64), to: (f64, f64), gc: R_GE_gcontext, _: DevDesc) {
add_tracing_point!();

let fill_params = FillParams::from_gc(gc);
let stroke_params = StrokeParams::from_gc(gc);
if fill_params.is_some() || stroke_params.is_some() {
Expand All @@ -189,6 +213,8 @@ impl DeviceDriver for VelloGraphicsDeviceWithServer {
gc: R_GE_gcontext,
_: DevDesc,
) {
add_tracing_point!();

let [r, g, b, a] = gc.col.to_ne_bytes();
let color = peniko::Color::rgba8(r, g, b, a);
let family = unsafe {
Expand Down Expand Up @@ -246,15 +272,23 @@ impl DeviceDriver for VelloGraphicsDeviceWithServer {
// }

fn size(&mut self, _: DevDesc) -> (f64, f64, f64, f64) {
add_tracing_point!();

// TODO: cache result? (for example, for 1 second)

let sizes = self.get_window_sizes().unwrap_or((0, 0));
(0.0, sizes.0 as _, 0.0, sizes.1 as _)
}

fn char_metric(&mut self, c: char, gc: R_GE_gcontext, _: DevDesc) -> TextMetric {
add_tracing_point!();

self.get_char_metric(c, gc)
}

fn text_width(&mut self, text: &str, gc: R_GE_gcontext, _: DevDesc) -> f64 {
add_tracing_point!();

self.get_text_width(text, gc)
}

Expand Down
Loading

0 comments on commit 4be02cc

Please sign in to comment.