Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #48 from linebender/hello-vello
Browse files Browse the repository at this point in the history
Update to use renamed vello crate
  • Loading branch information
dfrg authored Dec 13, 2022
2 parents d576fcb + 2408eb5 commit 64e19b3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ static_assertions = "1.1.0"
test-log = { version = "0.2.5", features = ["trace"], default-features = false }
tracing-subscriber = { version = "0.3.2", features = ["env-filter"] }
unicode-segmentation = "1.7.0"
piet-wgsl = { git = "https://github.com/linebender/piet-gpu" }
piet-scene = { git = "https://github.com/linebender/piet-gpu" }
vello = { git = "https://github.com/linebender/vello" }
parley = { git = "https://github.com/dfrg/parley" }
pollster = "0.2.5"

Expand Down
80 changes: 43 additions & 37 deletions examples/shello.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use glazier::kurbo::Size;
use glazier::{
Application, Cursor, FileDialogToken, FileInfo, IdleToken, KeyEvent, MouseEvent, Region,
TimerToken, WinHandler, WindowHandle,
Scalable, TimerToken, WinHandler, WindowHandle,
};
use parley::{FontContext, Layout};
use piet_scene::{
use std::any::Any;
use vello::util::{RenderContext, RenderSurface};
use vello::Renderer;
use vello::{
glyph::{
pinot::{types::Tag, FontRef},
GlyphContext,
},
kurbo::{Affine, PathEl, Point, Rect},
Brush, Color, Fill, Mix, Scene, SceneBuilder, Stroke,
peniko::{Brush, Color, Fill, Mix, Stroke},
Scene, SceneBuilder,
};
use piet_wgsl::util::{RenderContext, RenderSurface};
use piet_wgsl::Renderer;
use std::any::Any;

const WIDTH: usize = 2048;
const HEIGHT: usize = 1536;
Expand All @@ -33,6 +34,7 @@ fn main() {
struct WindowState {
handle: WindowHandle,
render: RenderContext,
renderer: Renderer,
surface: Option<RenderSurface>,
scene: Scene,
size: Size,
Expand All @@ -43,10 +45,12 @@ struct WindowState {
impl WindowState {
pub fn new() -> Self {
let render = pollster::block_on(RenderContext::new()).unwrap();
let renderer = Renderer::new(&render.device).unwrap();
Self {
handle: Default::default(),
surface: None,
render,
renderer,
scene: Default::default(),
font_context: FontContext::new(),
counter: 0,
Expand All @@ -67,43 +71,42 @@ impl WindowState {
self.handle.invalidate();
}

fn new_surface(&mut self) {
self.surface = Some(self.render.create_surface(
&self.handle,
self.size.width as u32,
self.size.height as u32,
));
fn surface_size(&self) -> (u32, u32) {
let handle = &self.handle;
let scale = handle.get_scale().unwrap_or_default();
let insets = handle.content_insets().to_px(scale);
let mut size = handle.get_size().to_px(scale);
size.width -= insets.x_value();
size.height -= insets.y_value();
(size.width as u32, size.height as u32)
}

fn render(&mut self) {
let width = self.size.width as u32;
let height = self.size.height as u32;
let (width, height) = self.surface_size();
if self.surface.is_none() {
self.new_surface();
self.surface = Some(self.render.create_surface(&self.handle, width, height));
}

let mut renderer = Renderer::new(&self.render.device).unwrap();
render_anim_frame(&mut self.scene, &mut self.font_context, self.counter);
self.counter += 1;

let surface_texture = self
.surface
.as_ref()
.unwrap()
.surface
.get_current_texture()
.unwrap();
renderer
.render_to_surface(
&self.render.device,
&self.render.queue,
&self.scene,
&surface_texture,
width,
height,
)
.unwrap();
surface_texture.present();
if let Some(surface) = self.surface.as_mut() {
if surface.config.width != width || surface.config.height != height {
self.render.resize_surface(surface, width, height);
}
let surface_texture = surface.surface.get_current_texture().unwrap();
self.renderer
.render_to_surface(
&self.render.device,
&self.render.queue,
&self.scene,
&surface_texture,
width,
height,
)
.unwrap();
surface_texture.present();
}
}
}

Expand Down Expand Up @@ -167,7 +170,6 @@ impl WinHandler for WindowState {

fn size(&mut self, size: Size) {
self.size = size;
self.new_surface();
}

fn got_focus(&mut self) {
Expand Down Expand Up @@ -252,13 +254,17 @@ pub fn render_anim_frame(scene: &mut Scene, fcx: &mut FontContext, i: u64) {
let scale = (i as f64 * 0.01).sin() * 0.5 + 1.5;
let mut lcx = parley::LayoutContext::new();
let mut layout_builder =
lcx.ranged_builder(fcx, "Hello piet-gpu! ഹലോ ਸਤ ਸ੍ਰੀ ਅਕਾਲ مرحبا!", scale as f32);
lcx.ranged_builder(fcx, "Hello vello! ഹലോ ਸਤ ਸ੍ਰੀ ਅਕਾਲ مرحبا!", scale as f32);
layout_builder.push_default(&parley::style::StyleProperty::FontSize(34.0));
layout_builder.push(
&parley::style::StyleProperty::Brush(ParleyBrush(Brush::Solid(Color::rgb8(255, 255, 0)))),
6..10,
);
layout_builder.push(&parley::style::StyleProperty::FontSize(48.0), 6..10);
layout_builder.push(&parley::style::StyleProperty::FontSize(48.0), 6..12);
layout_builder.push(
&parley::style::StyleProperty::Brush(ParleyBrush(Color::YELLOW.into())),
6..12,
);
layout_builder.push_default(&parley::style::StyleProperty::Brush(ParleyBrush(
Brush::Solid(Color::rgb8(255, 255, 255)),
)));
Expand Down

0 comments on commit 64e19b3

Please sign in to comment.