2019-01-14 17:20:36 -05:00
|
|
|
// 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.
|
|
|
|
|
2019-04-30 22:13:28 -04:00
|
|
|
use crate::builder::SceneBuilder;
|
|
|
|
use crate::concurrent::executor::Executor;
|
2019-06-05 17:35:46 -04:00
|
|
|
use crate::options::{BuildOptions, PreparedBuildOptions};
|
|
|
|
use crate::options::{PreparedRenderTransform, RenderCommandListener};
|
2019-05-14 21:09:01 -04:00
|
|
|
use crate::paint::{Paint, PaintId};
|
2019-01-14 17:20:36 -05:00
|
|
|
use hashbrown::HashMap;
|
2019-06-21 13:06:19 -04:00
|
|
|
use pathfinder_geometry::vector::Vector2F;
|
|
|
|
use pathfinder_geometry::rect::RectF;
|
|
|
|
use pathfinder_geometry::transform2d::Transform2DF;
|
|
|
|
use pathfinder_content::color::ColorU;
|
|
|
|
use pathfinder_content::outline::Outline;
|
2019-01-14 17:20:36 -05:00
|
|
|
|
2019-01-29 17:50:15 -05:00
|
|
|
#[derive(Clone)]
|
2019-01-14 17:20:36 -05:00
|
|
|
pub struct Scene {
|
2019-05-10 15:03:38 -04:00
|
|
|
pub(crate) paths: Vec<PathObject>,
|
2019-05-14 17:28:21 -04:00
|
|
|
pub(crate) paints: Vec<Paint>,
|
2019-04-30 22:13:28 -04:00
|
|
|
paint_cache: HashMap<Paint, PaintId>,
|
2019-05-29 22:13:42 -04:00
|
|
|
bounds: RectF,
|
|
|
|
view_box: RectF,
|
2019-01-14 17:20:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Scene {
|
|
|
|
#[inline]
|
|
|
|
pub fn new() -> Scene {
|
|
|
|
Scene {
|
2019-05-10 15:03:38 -04:00
|
|
|
paths: vec![],
|
2019-01-14 17:20:36 -05:00
|
|
|
paints: vec![],
|
|
|
|
paint_cache: HashMap::new(),
|
2019-05-29 22:13:42 -04:00
|
|
|
bounds: RectF::default(),
|
|
|
|
view_box: RectF::default(),
|
2019-01-14 17:20:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 15:03:38 -04:00
|
|
|
pub fn push_path(&mut self, path: PathObject) {
|
|
|
|
self.bounds = self.bounds.union_rect(path.outline.bounds());
|
|
|
|
self.paths.push(path);
|
2019-04-30 22:13:28 -04:00
|
|
|
}
|
|
|
|
|
2019-01-14 17:20:36 -05:00
|
|
|
#[allow(clippy::trivially_copy_pass_by_ref)]
|
|
|
|
pub fn push_paint(&mut self, paint: &Paint) -> PaintId {
|
|
|
|
if let Some(paint_id) = self.paint_cache.get(paint) {
|
|
|
|
return *paint_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
let paint_id = PaintId(self.paints.len() as u16);
|
|
|
|
self.paint_cache.insert(*paint, paint_id);
|
|
|
|
self.paints.push(*paint);
|
|
|
|
paint_id
|
|
|
|
}
|
|
|
|
|
2019-04-30 22:13:28 -04:00
|
|
|
#[inline]
|
2019-05-10 15:03:38 -04:00
|
|
|
pub fn path_count(&self) -> usize {
|
|
|
|
self.paths.len()
|
2019-04-30 22:13:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn bounds(&self) -> RectF {
|
2019-04-30 22:13:28 -04:00
|
|
|
self.bounds
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn set_bounds(&mut self, new_bounds: RectF) {
|
2019-04-30 22:13:28 -04:00
|
|
|
self.bounds = new_bounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn view_box(&self) -> RectF {
|
2019-04-30 22:13:28 -04:00
|
|
|
self.view_box
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn set_view_box(&mut self, new_view_box: RectF) {
|
2019-04-30 22:13:28 -04:00
|
|
|
self.view_box = new_view_box;
|
2019-04-18 18:09:37 -04:00
|
|
|
}
|
|
|
|
|
2019-04-29 19:45:29 -04:00
|
|
|
pub(crate) fn apply_render_options(
|
|
|
|
&self,
|
|
|
|
original_outline: &Outline,
|
2019-06-05 17:35:46 -04:00
|
|
|
options: &PreparedBuildOptions,
|
2019-04-29 19:45:29 -04:00
|
|
|
) -> Outline {
|
2019-03-25 19:13:56 -04:00
|
|
|
let effective_view_box = self.effective_view_box(options);
|
|
|
|
|
2019-02-09 22:24:30 -05:00
|
|
|
let mut outline;
|
2019-02-06 21:09:37 -05:00
|
|
|
match options.transform {
|
2019-04-29 19:45:29 -04:00
|
|
|
PreparedRenderTransform::Perspective {
|
|
|
|
ref perspective,
|
|
|
|
ref clip_polygon,
|
|
|
|
..
|
|
|
|
} => {
|
2019-02-09 22:24:30 -05:00
|
|
|
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);
|
2019-03-20 16:40:48 -04:00
|
|
|
|
2019-03-25 19:13:56 -04:00
|
|
|
// TODO(pcwalton): Support subpixel AA in 3D.
|
2019-02-09 22:24:30 -05:00
|
|
|
}
|
2019-01-30 17:42:06 -05:00
|
|
|
}
|
2019-03-25 19:13:56 -04:00
|
|
|
_ => {
|
2019-02-09 22:24:30 -05:00
|
|
|
// TODO(pcwalton): Short circuit.
|
|
|
|
outline = (*original_outline).clone();
|
2019-03-25 19:13:56 -04:00
|
|
|
if options.transform.is_2d() || options.subpixel_aa_enabled {
|
|
|
|
let mut transform = match options.transform {
|
|
|
|
PreparedRenderTransform::Transform2D(transform) => transform,
|
2019-05-29 22:13:42 -04:00
|
|
|
PreparedRenderTransform::None => Transform2DF::default(),
|
2019-03-25 19:13:56 -04:00
|
|
|
PreparedRenderTransform::Perspective { .. } => unreachable!(),
|
|
|
|
};
|
|
|
|
if options.subpixel_aa_enabled {
|
2019-04-29 19:45:29 -04:00
|
|
|
transform = transform
|
2019-06-03 15:39:29 -04:00
|
|
|
.post_mul(&Transform2DF::from_scale(Vector2F::new(3.0, 1.0)))
|
2019-03-25 19:13:56 -04:00
|
|
|
}
|
|
|
|
outline.transform(&transform);
|
|
|
|
}
|
|
|
|
outline.clip_against_rect(effective_view_box);
|
2019-02-02 14:36:42 -05:00
|
|
|
}
|
2019-01-30 17:42:06 -05:00
|
|
|
}
|
2019-02-06 21:09:37 -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.
|
2019-03-25 19:13:56 -04:00
|
|
|
outline.prepare_for_tiling(self.effective_view_box(options));
|
2019-02-02 14:36:42 -05:00
|
|
|
outline
|
2019-01-16 19:53:10 -05:00
|
|
|
}
|
2019-03-06 22:35:57 -05:00
|
|
|
|
2019-03-22 17:28:18 -04:00
|
|
|
pub fn monochrome_color(&self) -> Option<ColorU> {
|
2019-05-10 15:03:38 -04:00
|
|
|
if self.paths.is_empty() {
|
2019-03-22 17:28:18 -04:00
|
|
|
return None;
|
2019-03-06 22:35:57 -05:00
|
|
|
}
|
2019-05-10 15:03:38 -04:00
|
|
|
|
|
|
|
let first_paint_id = self.paths[0].paint;
|
2019-04-29 19:45:29 -04:00
|
|
|
if self
|
2019-05-10 15:03:38 -04:00
|
|
|
.paths
|
2019-04-29 19:45:29 -04:00
|
|
|
.iter()
|
|
|
|
.skip(1)
|
2019-05-10 15:03:38 -04:00
|
|
|
.any(|path_object| path_object.paint != first_paint_id) {
|
2019-03-22 17:28:18 -04:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(self.paints[first_paint_id.0 as usize].color)
|
2019-03-06 22:35:57 -05:00
|
|
|
}
|
2019-03-25 19:13:56 -04:00
|
|
|
|
|
|
|
#[inline]
|
2019-06-05 17:35:46 -04:00
|
|
|
pub(crate) fn effective_view_box(&self, render_options: &PreparedBuildOptions) -> RectF {
|
2019-03-25 19:13:56 -04:00
|
|
|
if render_options.subpixel_aa_enabled {
|
2019-06-03 15:39:29 -04:00
|
|
|
self.view_box.scale_xy(Vector2F::new(3.0, 1.0))
|
2019-03-25 19:13:56 -04:00
|
|
|
} else {
|
|
|
|
self.view_box
|
|
|
|
}
|
|
|
|
}
|
2019-04-30 22:13:28 -04:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn build<E>(&self,
|
2019-06-05 17:35:46 -04:00
|
|
|
options: BuildOptions,
|
2019-04-30 22:13:28 -04:00
|
|
|
listener: Box<dyn RenderCommandListener>,
|
|
|
|
executor: &E)
|
|
|
|
where E: Executor {
|
2019-05-03 15:35:19 -04:00
|
|
|
let prepared_options = options.prepare(self.bounds);
|
|
|
|
SceneBuilder::new(self, &prepared_options, listener).build(executor)
|
2019-04-30 22:13:28 -04:00
|
|
|
}
|
2019-06-24 15:41:34 -04:00
|
|
|
|
2019-06-15 10:50:25 -04:00
|
|
|
pub fn paths<'a>(&'a self) -> PathIter {
|
|
|
|
PathIter {
|
|
|
|
scene: self,
|
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub struct PathIter<'a> {
|
|
|
|
scene: &'a Scene,
|
|
|
|
pos: usize
|
|
|
|
}
|
|
|
|
impl<'a> Iterator for PathIter<'a> {
|
2019-06-25 04:26:37 -04:00
|
|
|
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| {
|
2019-06-25 04:26:37 -04:00
|
|
|
(
|
|
|
|
self.scene.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-29 17:50:15 -05:00
|
|
|
}
|
2019-01-15 14:42:25 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2019-01-14 17:20:36 -05:00
|
|
|
pub struct PathObject {
|
|
|
|
outline: Outline,
|
|
|
|
paint: PaintId,
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PathObject {
|
|
|
|
#[inline]
|
2019-05-03 15:35:19 -04:00
|
|
|
pub fn new(outline: Outline, paint: PaintId, name: String) -> PathObject {
|
|
|
|
PathObject { outline, paint, name }
|
2019-01-14 17:20:36 -05:00
|
|
|
}
|
2019-01-29 17:50:15 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn outline(&self) -> &Outline {
|
|
|
|
&self.outline
|
|
|
|
}
|
2019-05-14 17:28:21 -04:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn paint(&self) -> PaintId {
|
|
|
|
self.paint
|
|
|
|
}
|
2019-01-14 17:20:36 -05:00
|
|
|
}
|