vg/
renderer.rs

1//! Module containing renderer implementations.
2
3use imgref::ImgVec;
4use rgb::RGBA8;
5
6use crate::{
7    paint::GlyphTexture,
8    Color,
9    CompositeOperationState,
10    ErrorKind,
11    FillRule,
12    ImageFilter,
13    ImageId,
14    ImageInfo,
15    ImageSource,
16    ImageStore,
17};
18
19mod opengl;
20pub use opengl::OpenGl;
21
22mod void;
23pub use void::Void;
24
25mod params;
26pub use params::Params;
27
28/// Represents drawable
29#[derive(Copy, Clone, Default, Debug)]
30pub struct Drawable {
31    /// Fill vertices
32    pub fill_verts: Option<(usize, usize)>,
33    /// Stroke vertices
34    pub stroke_verts: Option<(usize, usize)>,
35}
36
37/// Represents command type
38#[derive(Debug)]
39pub enum CommandType {
40    /// Set render target
41    SetRenderTarget(RenderTarget),
42    /// Clear rectangle
43    ClearRect {
44        /// Rectangle x position
45        x: u32,
46        /// Rectangle y position
47        y: u32,
48        /// Rectangle width
49        width: u32,
50        /// Rectangle height
51        height: u32,
52        /// Clear color
53        color: Color,
54    },
55    /// Convex fill
56    ConvexFill {
57        /// Fill parameters
58        params: Params,
59    },
60    /// Concave fill
61    ConcaveFill {
62        /// Stencil parameters
63        stencil_params: Params,
64        /// Fill parameters
65        fill_params: Params,
66    },
67    /// Stroke
68    Stroke {
69        /// Stroke parameters
70        params: Params,
71    },
72    /// Stencil stroke
73    StencilStroke {
74        /// First parameters
75        params1: Params,
76        /// Second parameters
77        params2: Params,
78    },
79    /// Triangles
80    Triangles {
81        /// Triangle parameters
82        params: Params,
83    },
84    /// Render filtered image
85    RenderFilteredImage {
86        /// Target image id
87        target_image: ImageId,
88        /// Image filter
89        filter: ImageFilter,
90    },
91}
92
93/// Represents command
94pub struct Command {
95    /// Command type
96    pub cmd_type: CommandType,
97    /// Drawables
98    pub drawables: Vec<Drawable>,
99    /// Triangle vertices
100    pub triangles_verts: Option<(usize, usize)>,
101    /// Image
102    pub image: Option<ImageId>,
103    /// Glyph texture
104    pub glyph_texture: GlyphTexture,
105    /// Fill rule
106    pub fill_rule: FillRule,
107    /// Compsite operation
108    pub composite_operation: CompositeOperationState,
109}
110
111impl Command {
112    /// Create new Command with specified flavor
113    pub fn new(flavor: CommandType) -> Self {
114        Self {
115            cmd_type: flavor,
116            drawables: Default::default(),
117            triangles_verts: Default::default(),
118            image: Default::default(),
119            glyph_texture: Default::default(),
120            fill_rule: Default::default(),
121            composite_operation: Default::default(),
122        }
123    }
124}
125
126/// Represents rendering target
127#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
128pub enum RenderTarget {
129    /// Render to screen
130    Screen,
131    /// Render into image
132    Image(ImageId),
133}
134
135/// This is the main renderer trait that the [Canvas](../struct.Canvas.html) draws to.
136pub trait Renderer {
137    /// Renderer image
138    type Image;
139
140    /// Set size
141    fn set_size(&mut self, width: u32, height: u32, dpi: f32);
142
143    /// Renderer image
144    fn render(&mut self, images: &mut ImageStore<Self::Image>, verts: &[Vertex], commands: Vec<Command>);
145
146    /// Alloc image
147    fn alloc_image(&mut self, info: ImageInfo) -> Result<Self::Image, ErrorKind>;
148
149    /// Update image
150    fn update_image(&mut self, image: &mut Self::Image, data: ImageSource, x: usize, y: usize)
151        -> Result<(), ErrorKind>;
152
153    /// Delete image
154    fn delete_image(&mut self, image: Self::Image, image_id: ImageId);
155
156    /// Create screenchot
157    fn screenshot(&mut self) -> Result<ImgVec<RGBA8>, ErrorKind>;
158}
159
160/// Vertex struct for specifying triangle geometry
161#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Default)]
162#[repr(C)]
163pub struct Vertex {
164    /// Represents x
165    pub x: f32,
166    /// Represents y
167    pub y: f32,
168    /// Represents u
169    pub u: f32,
170    /// Represents v
171    pub v: f32,
172}
173
174impl Vertex {
175    /// Create new Vertex with specified params
176    pub fn new(x: f32, y: f32, u: f32, v: f32) -> Self {
177        Self { x, y, u, v }
178    }
179
180    /// Set params
181    pub fn set(&mut self, x: f32, y: f32, u: f32, v: f32) {
182        *self = Self { x, y, u, v };
183    }
184}
185
186/// Represents Shader Type
187#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
188pub enum ShaderType {
189    /// Represents Fill Gradient
190    FillGradient,
191    /// Represents Fill Image
192    FillImage,
193    /// Represents Stencil
194    Stencil,
195    /// Represents Fill Image Gradient
196    FillImageGradient,
197    /// Represents Filter Image
198    FilterImage,
199}
200
201impl Default for ShaderType {
202    fn default() -> Self {
203        Self::FillGradient
204    }
205}
206
207impl ShaderType {
208    /// Represents shader type as f32
209    pub fn to_f32(self) -> f32 {
210        match self {
211            Self::FillGradient => 0.0,
212            Self::FillImage => 1.0,
213            Self::Stencil => 2.0,
214            Self::FillImageGradient => 3.0,
215            Self::FilterImage => 4.0,
216        }
217    }
218}