pathfinder/renderer/src/scene.rs

289 lines
8.0 KiB
Rust
Raw Normal View History

// pathfinder/renderer/src/scene.rs
//
// Copyright © 2019 The Pathfinder Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! A set of paths to be rendered.
use crate::builder::SceneBuilder;
use crate::concurrent::executor::Executor;
use crate::options::{BuildOptions, PreparedBuildOptions};
use crate::options::{PreparedRenderTransform, RenderCommandListener};
use crate::paint::{Paint, PaintId, PaintInfo, Palette};
use pathfinder_content::effects::Effects;
2020-02-17 17:44:48 -05:00
use pathfinder_content::fill::FillRule;
use pathfinder_geometry::vector::Vector2F;
use pathfinder_geometry::rect::RectF;
use pathfinder_geometry::transform2d::Transform2F;
use pathfinder_content::outline::Outline;
#[derive(Clone)]
pub struct Scene {
pub(crate) display_list: Vec<DisplayItem>,
pub(crate) paths: Vec<DrawPath>,
pub(crate) clip_paths: Vec<ClipPath>,
palette: Palette,
bounds: RectF,
view_box: RectF,
}
impl Scene {
#[inline]
pub fn new() -> Scene {
Scene {
display_list: vec![],
paths: vec![],
clip_paths: vec![],
palette: Palette::new(),
bounds: RectF::default(),
view_box: RectF::default(),
}
}
pub fn push_path(&mut self, path: DrawPath) {
self.bounds = self.bounds.union_rect(path.outline.bounds());
self.paths.push(path);
let new_path_count = self.paths.len() as u32;
if let Some(DisplayItem::DrawPaths {
start_index: _,
ref mut end_index
}) = self.display_list.last_mut() {
*end_index = new_path_count;
} else {
self.display_list.push(DisplayItem::DrawPaths {
start_index: new_path_count - 1,
end_index: new_path_count,
});
}
}
pub fn push_clip_path(&mut self, clip_path: ClipPath) -> ClipPathId {
self.bounds = self.bounds.union_rect(clip_path.outline.bounds());
let clip_path_id = ClipPathId(self.clip_paths.len() as u32);
self.clip_paths.push(clip_path);
clip_path_id
}
pub fn push_layer(&mut self, effects: Effects) {
self.display_list.push(DisplayItem::PushLayer { effects });
}
pub fn pop_layer(&mut self) {
self.display_list.push(DisplayItem::PopLayer);
}
#[inline]
pub fn build_paint_info(&self) -> PaintInfo {
self.palette.build_paint_info(self.view_box.size().to_i32())
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn push_paint(&mut self, paint: &Paint) -> PaintId {
self.palette.push_paint(paint)
}
#[inline]
pub fn path_count(&self) -> usize {
self.paths.len()
}
#[inline]
pub fn bounds(&self) -> RectF {
self.bounds
}
#[inline]
pub fn set_bounds(&mut self, new_bounds: RectF) {
self.bounds = new_bounds;
}
#[inline]
pub fn view_box(&self) -> RectF {
self.view_box
}
#[inline]
pub fn set_view_box(&mut self, new_view_box: RectF) {
self.view_box = new_view_box;
}
2019-04-29 19:45:29 -04:00
pub(crate) fn apply_render_options(
&self,
original_outline: &Outline,
options: &PreparedBuildOptions,
2019-04-29 19:45:29 -04:00
) -> Outline {
let effective_view_box = self.effective_view_box(options);
let mut outline;
match options.transform {
2019-04-29 19:45:29 -04:00
PreparedRenderTransform::Perspective {
ref perspective,
ref clip_polygon,
..
} => {
if original_outline.is_outside_polygon(clip_polygon) {
outline = Outline::new();
} else {
outline = (*original_outline).clone();
outline.clip_against_polygon(clip_polygon);
outline.apply_perspective(perspective);
// TODO(pcwalton): Support subpixel AA in 3D.
}
2019-01-30 17:42:06 -05:00
}
_ => {
// TODO(pcwalton): Short circuit.
outline = (*original_outline).clone();
if options.transform.is_2d() || options.subpixel_aa_enabled {
let mut transform = match options.transform {
PreparedRenderTransform::Transform2D(transform) => transform,
PreparedRenderTransform::None => Transform2F::default(),
PreparedRenderTransform::Perspective { .. } => unreachable!(),
};
if options.subpixel_aa_enabled {
transform *= Transform2F::from_scale(Vector2F::new(3.0, 1.0))
}
outline.transform(&transform);
}
outline.clip_against_rect(effective_view_box);
}
2019-01-30 17:42:06 -05:00
}
if !options.dilation.is_zero() {
outline.dilate(options.dilation);
}
2019-02-12 11:57:49 -05:00
// TODO(pcwalton): Fold this into previous passes to avoid unnecessary clones during
// monotonic conversion.
outline.prepare_for_tiling(self.effective_view_box(options));
outline
2019-01-16 19:53:10 -05:00
}
#[inline]
pub(crate) fn effective_view_box(&self, render_options: &PreparedBuildOptions) -> RectF {
if render_options.subpixel_aa_enabled {
self.view_box.scale_xy(Vector2F::new(3.0, 1.0))
} else {
self.view_box
}
}
#[inline]
pub fn build<E>(&self,
options: BuildOptions,
listener: Box<dyn RenderCommandListener>,
executor: &E)
where E: Executor {
let prepared_options = options.prepare(self.bounds);
SceneBuilder::new(self, &prepared_options, listener).build(executor)
}
2019-06-15 10:50:25 -04:00
pub fn paths<'a>(&'a self) -> PathIter {
PathIter {
scene: self,
pos: 0
}
}
}
2019-06-15 10:50:25 -04:00
pub struct PathIter<'a> {
scene: &'a Scene,
pos: usize
}
2019-06-15 10:50:25 -04:00
impl<'a> Iterator for PathIter<'a> {
type Item = (&'a Paint, &'a Outline, &'a str);
2019-06-15 10:50:25 -04:00
fn next(&mut self) -> Option<Self::Item> {
let item = self.scene.paths.get(self.pos).map(|path_object| {
(
self.scene.palette.paints.get(path_object.paint.0 as usize).unwrap(),
&path_object.outline,
&*path_object.name
)
2019-06-15 10:50:25 -04:00
});
self.pos += 1;
item
}
}
2019-01-15 14:42:25 -05:00
#[derive(Clone, Debug)]
pub struct DrawPath {
outline: Outline,
paint: PaintId,
clip_path: Option<ClipPathId>,
2020-02-17 17:44:48 -05:00
fill_rule: FillRule,
name: String,
}
#[derive(Clone, Debug)]
pub struct ClipPath {
outline: Outline,
2020-02-17 17:44:48 -05:00
fill_rule: FillRule,
name: String,
}
#[derive(Clone, Copy, Debug)]
pub struct ClipPathId(pub u32);
#[derive(Clone, Debug)]
pub enum DisplayItem {
DrawPaths { start_index: u32, end_index: u32 },
PushLayer { effects: Effects },
PopLayer,
}
impl DrawPath {
#[inline]
2020-02-17 17:44:48 -05:00
pub fn new(outline: Outline,
paint: PaintId,
clip_path: Option<ClipPathId>,
fill_rule: FillRule,
name: String)
-> DrawPath {
2020-02-17 17:44:48 -05:00
DrawPath { outline, paint, clip_path, fill_rule, name }
}
#[inline]
pub fn outline(&self) -> &Outline {
&self.outline
}
#[inline]
pub(crate) fn clip_path(&self) -> Option<ClipPathId> {
self.clip_path
}
#[inline]
pub(crate) fn paint(&self) -> PaintId {
self.paint
}
2020-02-17 17:44:48 -05:00
#[inline]
pub(crate) fn fill_rule(&self) -> FillRule {
self.fill_rule
}
}
impl ClipPath {
#[inline]
2020-02-17 17:44:48 -05:00
pub fn new(outline: Outline, fill_rule: FillRule, name: String) -> ClipPath {
ClipPath { outline, fill_rule, name }
}
#[inline]
pub fn outline(&self) -> &Outline {
&self.outline
}
2020-02-17 17:44:48 -05:00
#[inline]
pub(crate) fn fill_rule(&self) -> FillRule {
self.fill_rule
}
}