-
-
Notifications
You must be signed in to change notification settings - Fork 864
/
Copy pathoptions.rs
417 lines (372 loc) · 13.3 KB
/
options.rs
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use crate::backends::TestAudioBackend;
use crate::environment::{Environment, RenderInterface};
use crate::image_trigger::ImageTrigger;
use crate::util::write_image;
use anyhow::{anyhow, Result};
use approx::relative_eq;
use image::ImageFormat;
use regex::Regex;
use ruffle_core::tag_utils::SwfMovie;
use ruffle_core::{PlayerBuilder, PlayerMode, PlayerRuntime, ViewportDimensions};
use ruffle_render::backend::RenderBackend;
use ruffle_render::quality::StageQuality;
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
use std::time::Duration;
use vfs::VfsPath;
#[derive(Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct TestOptions {
pub num_frames: Option<u32>,
pub num_ticks: Option<u32>,
pub tick_rate: Option<f64>,
pub output_path: String,
pub sleep_to_meet_frame_rate: bool,
pub image_comparisons: HashMap<String, ImageComparison>,
pub ignore: bool,
pub known_failure: bool,
pub approximations: Option<Approximations>,
pub player_options: PlayerOptions,
pub log_fetch: bool,
pub required_features: RequiredFeatures,
pub fonts: HashMap<String, FontOptions>,
}
impl Default for TestOptions {
fn default() -> Self {
Self {
num_frames: None,
num_ticks: None,
tick_rate: None,
output_path: "output.txt".to_string(),
sleep_to_meet_frame_rate: false,
image_comparisons: Default::default(),
ignore: false,
known_failure: false,
approximations: None,
player_options: PlayerOptions::default(),
log_fetch: false,
required_features: RequiredFeatures::default(),
fonts: Default::default(),
}
}
}
impl TestOptions {
pub fn read(path: &VfsPath) -> Result<Self> {
let result: Self = toml::from_str(&path.read_to_string()?)?;
result.validate()?;
Ok(result)
}
fn validate(&self) -> Result<()> {
if !self.image_comparisons.is_empty() {
let mut seen_triggers = HashSet::new();
for comparison in self.image_comparisons.values() {
if comparison.trigger != ImageTrigger::FsCommand
&& !seen_triggers.insert(comparison.trigger)
{
return Err(anyhow!(
"Multiple captures are set to trigger {:?}. This likely isn't intended!",
comparison.trigger
));
}
}
}
Ok(())
}
pub fn output_path(&self, test_directory: &VfsPath) -> Result<VfsPath> {
Ok(test_directory.join(&self.output_path)?)
}
}
#[derive(Clone, Deserialize, Default)]
#[serde(default, deny_unknown_fields)]
pub struct Approximations {
number_patterns: Vec<String>,
epsilon: Option<f64>,
max_relative: Option<f64>,
}
impl Approximations {
pub fn compare(&self, actual: f64, expected: f64) -> Result<()> {
let result = match (self.epsilon, self.max_relative) {
(Some(epsilon), Some(max_relative)) => relative_eq!(
actual,
expected,
epsilon = epsilon,
max_relative = max_relative
),
(Some(epsilon), None) => relative_eq!(actual, expected, epsilon = epsilon),
(None, Some(max_relative)) => {
relative_eq!(actual, expected, max_relative = max_relative)
}
(None, None) => relative_eq!(actual, expected),
};
if result {
Ok(())
} else {
Err(anyhow!(
"Approximation failed: expected {}, found {}. Episilon = {:?}, Max Relative = {:?}",
expected,
actual,
self.epsilon,
self.max_relative
))
}
}
pub fn number_patterns(&self) -> Vec<Regex> {
self.number_patterns
.iter()
.map(|p| Regex::new(p).unwrap())
.collect()
}
}
#[derive(Clone, Deserialize, Default)]
#[serde(default, deny_unknown_fields)]
pub struct RequiredFeatures {
lzma: bool,
jpegxr: bool,
}
impl RequiredFeatures {
pub fn can_run(&self) -> bool {
(!self.lzma || cfg!(feature = "lzma")) && (!self.jpegxr || cfg!(feature = "jpegxr"))
}
}
#[derive(Clone, Deserialize, Default)]
#[serde(default, deny_unknown_fields)]
pub struct PlayerOptions {
max_execution_duration: Option<Duration>,
viewport_dimensions: Option<ViewportDimensions>,
with_renderer: Option<RenderOptions>,
with_audio: bool,
with_video: bool,
runtime: PlayerRuntime,
mode: PlayerMode,
}
impl PlayerOptions {
pub fn setup(&self, mut player_builder: PlayerBuilder) -> Result<PlayerBuilder> {
if let Some(max_execution_duration) = self.max_execution_duration {
player_builder = player_builder.with_max_execution_duration(max_execution_duration);
}
if let Some(render_options) = &self.with_renderer {
player_builder = player_builder.with_quality(match render_options.sample_count {
16 => StageQuality::High16x16,
8 => StageQuality::High8x8,
4 => StageQuality::High,
2 => StageQuality::Medium,
_ => StageQuality::Low,
});
}
if self.with_audio {
player_builder = player_builder.with_audio(TestAudioBackend::default());
}
player_builder = player_builder
.with_player_runtime(self.runtime)
.with_player_mode(self.mode);
if self.with_video {
#[cfg(feature = "ruffle_video_external")]
{
let current_exe = std::env::current_exe()?;
let directory = current_exe.parent().expect("Executable parent dir");
use ruffle_video_external::{
backend::ExternalVideoBackend, decoder::openh264::OpenH264Codec,
};
let openh264 = OpenH264Codec::load(directory)
.map_err(|e| anyhow!("Couldn't load OpenH264: {}", e))?;
player_builder =
player_builder.with_video(ExternalVideoBackend::new_with_openh264(openh264));
}
#[cfg(all(
not(feature = "ruffle_video_external"),
feature = "ruffle_video_software"
))]
{
player_builder = player_builder
.with_video(ruffle_video_software::backend::SoftwareVideoBackend::new());
}
}
Ok(player_builder)
}
pub fn can_run(&self, check_renderer: bool, environment: &impl Environment) -> bool {
if let Some(render) = &self.with_renderer {
// If we don't actually want to check the renderer (ie we're just listing potential tests),
// don't spend the cost to create it
if check_renderer && !render.optional && !environment.is_render_supported(render) {
return false;
}
}
true
}
pub fn viewport_dimensions(&self, movie: &SwfMovie) -> ViewportDimensions {
self.viewport_dimensions
.unwrap_or_else(|| ViewportDimensions {
width: movie.width().to_pixels() as u32,
height: movie.height().to_pixels() as u32,
scale_factor: 1.0,
})
}
pub fn create_renderer(
&self,
environment: &impl Environment,
dimensions: ViewportDimensions,
) -> Option<(Box<dyn RenderInterface>, Box<dyn RenderBackend>)> {
if self.with_renderer.is_some() {
environment.create_renderer(dimensions.width, dimensions.height)
} else {
None
}
}
}
#[derive(Deserialize, Default, Clone, Debug)]
#[serde(default, deny_unknown_fields)]
pub struct ImageComparison {
tolerance: u8,
max_outliers: usize,
pub trigger: ImageTrigger,
}
fn calc_difference(lhs: u8, rhs: u8) -> u8 {
(lhs as i16 - rhs as i16).unsigned_abs() as u8
}
impl ImageComparison {
pub fn test(
&self,
name: &str,
actual_image: image::RgbaImage,
expected_image: image::RgbaImage,
test_path: &VfsPath,
environment_name: String,
known_failure: bool,
) -> Result<()> {
use anyhow::Context;
let save_actual_image = || {
if !known_failure {
// If we're expecting failure, spamming files isn't productive.
write_image(
&test_path.join(format!("{name}.actual-{environment_name}.png"))?,
&actual_image,
ImageFormat::Png,
)
} else {
Ok(())
}
};
if actual_image.width() != expected_image.width()
|| actual_image.height() != expected_image.height()
{
save_actual_image()?;
return Err(anyhow!(
"'{}' image is not the right size. Expected = {}x{}, actual = {}x{}.",
name,
expected_image.width(),
expected_image.height(),
actual_image.width(),
actual_image.height()
));
}
let mut is_alpha_different = false;
let difference_data: Vec<u8> = expected_image
.as_raw()
.chunks_exact(4)
.zip(actual_image.as_raw().chunks_exact(4))
.flat_map(|(cmp_chunk, data_chunk)| {
if cmp_chunk[3] != data_chunk[3] {
is_alpha_different = true;
}
[
calc_difference(cmp_chunk[0], data_chunk[0]),
calc_difference(cmp_chunk[1], data_chunk[1]),
calc_difference(cmp_chunk[2], data_chunk[2]),
calc_difference(cmp_chunk[3], data_chunk[3]),
]
})
.collect();
let outliers: usize = difference_data
.chunks_exact(4)
.map(|colors| {
(colors[0] > self.tolerance) as usize
+ (colors[1] > self.tolerance) as usize
+ (colors[2] > self.tolerance) as usize
+ (colors[3] > self.tolerance) as usize
})
.sum();
let max_difference = difference_data
.chunks_exact(4)
.map(|colors| colors[0].max(colors[1]).max(colors[2]).max(colors[3]))
.max()
.unwrap();
if outliers > self.max_outliers {
save_actual_image()?;
let mut difference_color = Vec::with_capacity(
actual_image.width() as usize * actual_image.height() as usize * 3,
);
for p in difference_data.chunks_exact(4) {
difference_color.extend_from_slice(&p[..3]);
}
if !known_failure {
// If we're expecting failure, spamming files isn't productive.
let difference_image = image::RgbImage::from_raw(
actual_image.width(),
actual_image.height(),
difference_color,
)
.context("Couldn't create color difference image")?;
write_image(
&test_path.join(format!("{name}.difference-color-{environment_name}.png"))?,
&difference_image,
ImageFormat::Png,
)?;
}
if is_alpha_different {
let mut difference_alpha = Vec::with_capacity(
actual_image.width() as usize * actual_image.height() as usize,
);
for p in difference_data.chunks_exact(4) {
difference_alpha.push(p[3])
}
if !known_failure {
// If we're expecting failure, spamming files isn't productive.
let difference_image = image::GrayImage::from_raw(
actual_image.width(),
actual_image.height(),
difference_alpha,
)
.context("Couldn't create alpha difference image")?;
write_image(
&test_path
.join(format!("{name}.difference-alpha-{environment_name}.png"))?,
&difference_image,
ImageFormat::Png,
)?;
}
}
return Err(anyhow!(
"Image '{}' failed: Number of outliers ({}) is bigger than allowed limit of {}. Max difference is {}",
name,
outliers,
self.max_outliers,
max_difference
));
} else {
println!("Image '{name}' succeeded: {outliers} outliers found, max difference {max_difference}",);
}
Ok(())
}
}
#[derive(Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct RenderOptions {
optional: bool,
pub sample_count: u32,
}
impl Default for RenderOptions {
fn default() -> Self {
Self {
optional: false,
sample_count: 1,
}
}
}
#[derive(Deserialize, Default, Clone)]
#[serde(default, deny_unknown_fields)]
pub struct FontOptions {
pub family: String,
pub path: String,
pub bold: bool,
pub italic: bool,
}