Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crates/craft_renderer/src/render_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::rc::Weak;

use peniko::Color;

use craft_primitives::geometry::{BezPath, Rectangle, Vec2};
use craft_primitives::geometry::{Affine, BezPath, Rectangle, Vec2};

use craft_resource_manager::ResourceIdentifier;

Expand All @@ -12,6 +12,7 @@ use crate::text_renderer_data::{TextData, TextScroll};

#[derive(Clone)]
pub enum RenderCommand {
SetTransform(SetTransformCmd),
DrawRect(DrawRectCmd),
DrawRectOutline(DrawRectOutlineCmd),
DrawImage(DrawImageCmd),
Expand All @@ -25,6 +26,11 @@ pub enum RenderCommand {
BoxShadowCmd(BoxShadowCmd),
}

#[derive(Clone)]
pub struct SetTransformCmd {
pub transform: Affine,
}

#[derive(Clone)]
pub struct DrawRectCmd {
pub rect: Rectangle,
Expand Down
9 changes: 7 additions & 2 deletions crates/craft_renderer/src/render_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::rc::Weak;

use peniko::Color;

use craft_primitives::geometry::{BezPath, Rectangle, Shape};
use craft_primitives::geometry::{Affine, BezPath, Rectangle, Shape};
use craft_resource_manager::ResourceIdentifier;

use crate::render_command::{BoxShadowCmd, DrawImageCmd, DrawRectCmd, DrawRectOutlineCmd, DrawTextCmd, DrawTinyVgCmd, FillBezPathCmd, PushLayerCmd};
use crate::render_command::{BoxShadowCmd, DrawImageCmd, DrawRectCmd, DrawRectOutlineCmd, DrawTextCmd, DrawTinyVgCmd, FillBezPathCmd, PushLayerCmd, SetTransformCmd};
use crate::sort_commands::SortedCommands;
use crate::text_renderer_data::{TextData, TextScroll};
use crate::{Brush, RenderCommand, TargetItem};
Expand Down Expand Up @@ -43,6 +43,11 @@ impl RenderList {
self.overlay.children.clear();
}

#[inline(always)]
pub fn set_transform(&mut self, transform: Affine) {
self.commands.push(RenderCommand::SetTransform(SetTransformCmd { transform }));
}

#[inline(always)]
pub fn draw_rect(&mut self, rect: Rectangle, color: Color) {
if let Some(cull) = &self.cull
Expand Down
6 changes: 6 additions & 0 deletions crates/craft_renderer/src/sort_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ pub(crate) fn sort_and_cull_render_list_internal(surface_height: f32, render_lis
}
}

RenderCommand::SetTransform(_) => {
unsafe {
(*current).children.push(SortedItem::Other(index as u32));
}
}

_ => {
let bounding_rect = bounding_rect(command);
if !should_cull(&bounding_rect, window_height) {
Expand Down
36 changes: 23 additions & 13 deletions crates/craft_renderer/src/vello/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub struct VelloRenderer {
scene: Scene,
pub surface_clear_color: Color,
pub render_into_texture: bool,

transform: Affine
}

impl RenderSurface {
Expand Down Expand Up @@ -240,18 +242,19 @@ impl VelloRenderer {
scene: Scene::new(),
surface_clear_color: Color::WHITE,
render_into_texture,
transform: Default::default(),
}
}
}

fn vello_draw_rect(scene: &mut Scene, rectangle: Rectangle, fill_color: Color) {
fn vello_draw_rect(scene: &mut Scene, rectangle: Rectangle, fill_color: Color, transform: &Affine) {
let rect = Rect::new(
rectangle.x as f64,
rectangle.y as f64,
(rectangle.x + rectangle.width) as f64,
(rectangle.y + rectangle.height) as f64,
);
scene.fill(Fill::NonZero, Affine::IDENTITY, fill_color, None, &rect);
scene.fill(Fill::NonZero, *transform, fill_color, None, &rect);
}

impl Renderer for VelloRenderer {
Expand Down Expand Up @@ -281,12 +284,17 @@ impl Renderer for VelloRenderer {
resource_manager: Arc<ResourceManager>,
window: Rectangle,
) {
self.transform = Affine::IDENTITY;

SortedCommands::draw(render_list, &render_list.overlay, &mut |command: &RenderCommand| {
let scene = &mut self.scene;

match command {
RenderCommand::SetTransform(cmd) => {
self.transform = cmd.transform;
}
RenderCommand::DrawRect(cmd) => {
vello_draw_rect(scene, cmd.rect, cmd.color);
vello_draw_rect(scene, cmd.rect, cmd.color, &self.transform);
}
RenderCommand::DrawRectOutline(cmd) => {
self.scene.stroke(
Expand All @@ -312,23 +320,24 @@ impl Renderer for VelloRenderer {
width: image.width(),
height: image.height(),
};

let vello_image = vello::peniko::ImageBrush::new(vello_image);

let mut transform = Affine::IDENTITY;
transform = transform.with_translation(kurbo::Vec2::new(cmd.rect.x as f64, cmd.rect.y as f64));
transform = transform.pre_scale_non_uniform(
let mut image_transform = Affine::IDENTITY;
image_transform = image_transform.with_translation(kurbo::Vec2::new(cmd.rect.x as f64, cmd.rect.y as f64));
image_transform = image_transform.pre_scale_non_uniform(
cmd.rect.width as f64 / image.width() as f64,
cmd.rect.height as f64 / image.height() as f64,
);
scene.draw_image(&vello_image, transform);
image_transform = self.transform * image_transform;
scene.draw_image(&vello_image, image_transform);
}
}
RenderCommand::DrawText(cmd) => {
let text_transform =
Affine::default().with_translation(kurbo::Vec2::new(cmd.rect.x as f64, cmd.rect.y as f64));
let scroll = cmd.text_scroll.unwrap_or(TextScroll::default()).scroll_y;
let text_transform = text_transform.then_translate(kurbo::Vec2::new(0.0, -scroll as f64));
let mut text_transform = text_transform.then_translate(kurbo::Vec2::new(0.0, -scroll as f64));
text_transform = self.transform * text_transform;

let c = cmd.data.upgrade();
if c.is_none() {
Expand Down Expand Up @@ -376,7 +385,7 @@ impl Renderer for VelloRenderer {
width: background.width,
height: background.height,
};
vello_draw_rect(scene, background_rect, *color);
vello_draw_rect(scene, background_rect, *color, &self.transform);
}

for (selection, selection_color) in &line.selections {
Expand All @@ -386,7 +395,7 @@ impl Renderer for VelloRenderer {
width: selection.width,
height: selection.height,
};
vello_draw_rect(scene, selection_rect, *selection_color);
vello_draw_rect(scene, selection_rect, *selection_color, &self.transform);
}
});

Expand Down Expand Up @@ -433,7 +442,7 @@ impl Renderer for VelloRenderer {
width: cursor.width,
height: cursor.height,
};
vello_draw_rect(scene, cursor_rect, *cursor_color);
vello_draw_rect(scene, cursor_rect, *cursor_color, &self.transform);
}
}
RenderCommand::DrawTinyVg(cmd) => {
Expand All @@ -443,6 +452,7 @@ impl Renderer for VelloRenderer {
resource_manager.clone(),
cmd.resource_id.clone(),
&cmd.override_color,
&self.transform
);
}
RenderCommand::PushLayer(cmd) => {
Expand All @@ -465,7 +475,7 @@ impl Renderer for VelloRenderer {
scene.pop_layer();
}
RenderCommand::FillBezPath(cmd) => {
scene.fill(Fill::NonZero, Affine::IDENTITY, &cmd.brush, None, &cmd.path);
scene.fill(Fill::NonZero, self.transform, &cmd.brush, None, &cmd.path);
}
_ => {}
}
Expand Down
35 changes: 19 additions & 16 deletions crates/craft_renderer/src/vello/tinyvg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub(crate) fn draw_tiny_vg(
resource_manager: Arc<ResourceManager>,
resource_identifier: ResourceIdentifier,
override_color: &Option<Color>,
transform: &Affine
) {
let resource = resource_manager.get(&resource_identifier);
if let Some(resource) = resource
Expand All @@ -46,7 +47,7 @@ pub(crate) fn draw_tiny_vg(
}
let tiny_vg = resource.tinyvg.as_ref().unwrap();

let mut affine = Affine::IDENTITY;
let mut vg_transform = Affine::IDENTITY;
let mut svg_width = tiny_vg.header.width as f32;
let mut svg_height = tiny_vg.header.height as f32;

Expand All @@ -58,12 +59,14 @@ pub(crate) fn draw_tiny_vg(
svg_height = rectangle.height;
}

affine = affine.with_translation(kurbo::Vec2::new(rectangle.x as f64, rectangle.y as f64));
affine = affine.pre_scale_non_uniform(
vg_transform = vg_transform.with_translation(kurbo::Vec2::new(rectangle.x as f64, rectangle.y as f64));
vg_transform = vg_transform.pre_scale_non_uniform(
rectangle.width as f64 / svg_width as f64,
rectangle.height as f64 / svg_height as f64,
);

vg_transform = *transform * vg_transform;

for command in &tiny_vg.draw_commands {
match command {
DrawCommand::FillPolygon(data) => {
Expand All @@ -85,7 +88,7 @@ pub(crate) fn draw_tiny_vg(
&data.style,
None,
&tiny_vg.color_table,
&affine,
&vg_transform,
override_color,
);
}
Expand All @@ -94,7 +97,7 @@ pub(crate) fn draw_tiny_vg(
for rectangle in &data.rectangles {
let rectangle =
kurbo::Rect::new(rectangle.x.0, rectangle.y.0, rectangle.height.0, rectangle.height.0);
scene.fill(Fill::EvenOdd, affine, &brush, None, &rectangle);
scene.fill(Fill::EvenOdd, vg_transform, &brush, None, &rectangle);
}
}
DrawCommand::FillPath(data) => {
Expand All @@ -104,7 +107,7 @@ pub(crate) fn draw_tiny_vg(
&data.style,
None,
&tiny_vg.color_table,
&affine,
&vg_transform,
override_color,
);
}
Expand All @@ -116,7 +119,7 @@ pub(crate) fn draw_tiny_vg(
tinyvg_helpers::to_kurbo_point(line.start),
tinyvg_helpers::to_kurbo_point(line.end),
);
scene.stroke(&Stroke::new(data.line_width.0), affine, &brush, None, &line);
scene.stroke(&Stroke::new(data.line_width.0), vg_transform, &brush, None, &line);
}
}
DrawCommand::DrawLineLoop(data) => {
Expand All @@ -128,7 +131,7 @@ pub(crate) fn draw_tiny_vg(
tinyvg_helpers::to_kurbo_point(start),
tinyvg_helpers::to_kurbo_point(*point),
);
scene.stroke(&Stroke::new(data.line_width.0), affine, &brush, None, &line);
scene.stroke(&Stroke::new(data.line_width.0), vg_transform, &brush, None, &line);
start = *point;
}
}
Expand All @@ -141,7 +144,7 @@ pub(crate) fn draw_tiny_vg(
tinyvg_helpers::to_kurbo_point(start),
tinyvg_helpers::to_kurbo_point(*point),
);
scene.stroke(&Stroke::new(data.line_width.0), affine, &brush, None, &line);
scene.stroke(&Stroke::new(data.line_width.0), vg_transform, &brush, None, &line);
start = *point;
}
}
Expand All @@ -152,7 +155,7 @@ pub(crate) fn draw_tiny_vg(
&data.style,
Some(&data.line_width),
&tiny_vg.color_table,
&affine,
&vg_transform,
override_color,
);
}
Expand All @@ -175,7 +178,7 @@ pub(crate) fn draw_tiny_vg(
&data.fill_style,
None,
&tiny_vg.color_table,
&affine,
&vg_transform,
override_color,
);
draw_path(
Expand All @@ -184,7 +187,7 @@ pub(crate) fn draw_tiny_vg(
&data.line_style,
Some(&data.line_width),
&tiny_vg.color_table,
&affine,
&vg_transform,
override_color,
);
}
Expand All @@ -194,8 +197,8 @@ pub(crate) fn draw_tiny_vg(
for rectangle in &data.rectangles {
let rectangle =
kurbo::Rect::new(rectangle.x.0, rectangle.y.0, rectangle.height.0, rectangle.height.0);
scene.fill(Fill::EvenOdd, affine, &fill_brush, None, &rectangle);
scene.stroke(&Stroke::new(data.line_width.0), affine, &line_brush, None, &rectangle);
scene.fill(Fill::EvenOdd, vg_transform, &fill_brush, None, &rectangle);
scene.stroke(&Stroke::new(data.line_width.0), vg_transform, &line_brush, None, &rectangle);
}
}
DrawCommand::OutlineFillPath(data) => {
Expand All @@ -205,7 +208,7 @@ pub(crate) fn draw_tiny_vg(
&data.fill_style,
None,
&tiny_vg.color_table,
&affine,
&vg_transform,
override_color,
);
draw_path(
Expand All @@ -214,7 +217,7 @@ pub(crate) fn draw_tiny_vg(
&data.line_style,
Some(&data.line_width),
&tiny_vg.color_table,
&affine,
&vg_transform,
override_color,
);
}
Expand Down
Loading
Loading