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