1use 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#[derive(Copy, Clone, Default, Debug)]
30pub struct Drawable {
31 pub fill_verts: Option<(usize, usize)>,
33 pub stroke_verts: Option<(usize, usize)>,
35}
36
37#[derive(Debug)]
39pub enum CommandType {
40 SetRenderTarget(RenderTarget),
42 ClearRect {
44 x: u32,
46 y: u32,
48 width: u32,
50 height: u32,
52 color: Color,
54 },
55 ConvexFill {
57 params: Params,
59 },
60 ConcaveFill {
62 stencil_params: Params,
64 fill_params: Params,
66 },
67 Stroke {
69 params: Params,
71 },
72 StencilStroke {
74 params1: Params,
76 params2: Params,
78 },
79 Triangles {
81 params: Params,
83 },
84 RenderFilteredImage {
86 target_image: ImageId,
88 filter: ImageFilter,
90 },
91}
92
93pub struct Command {
95 pub cmd_type: CommandType,
97 pub drawables: Vec<Drawable>,
99 pub triangles_verts: Option<(usize, usize)>,
101 pub image: Option<ImageId>,
103 pub glyph_texture: GlyphTexture,
105 pub fill_rule: FillRule,
107 pub composite_operation: CompositeOperationState,
109}
110
111impl Command {
112 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#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
128pub enum RenderTarget {
129 Screen,
131 Image(ImageId),
133}
134
135pub trait Renderer {
137 type Image;
139
140 fn set_size(&mut self, width: u32, height: u32, dpi: f32);
142
143 fn render(&mut self, images: &mut ImageStore<Self::Image>, verts: &[Vertex], commands: Vec<Command>);
145
146 fn alloc_image(&mut self, info: ImageInfo) -> Result<Self::Image, ErrorKind>;
148
149 fn update_image(&mut self, image: &mut Self::Image, data: ImageSource, x: usize, y: usize)
151 -> Result<(), ErrorKind>;
152
153 fn delete_image(&mut self, image: Self::Image, image_id: ImageId);
155
156 fn screenshot(&mut self) -> Result<ImgVec<RGBA8>, ErrorKind>;
158}
159
160#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Default)]
162#[repr(C)]
163pub struct Vertex {
164 pub x: f32,
166 pub y: f32,
168 pub u: f32,
170 pub v: f32,
172}
173
174impl Vertex {
175 pub fn new(x: f32, y: f32, u: f32, v: f32) -> Self {
177 Self { x, y, u, v }
178 }
179
180 pub fn set(&mut self, x: f32, y: f32, u: f32, v: f32) {
182 *self = Self { x, y, u, v };
183 }
184}
185
186#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
188pub enum ShaderType {
189 FillGradient,
191 FillImage,
193 Stencil,
195 FillImageGradient,
197 FilterImage,
199}
200
201impl Default for ShaderType {
202 fn default() -> Self {
203 Self::FillGradient
204 }
205}
206
207impl ShaderType {
208 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}