Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SVG renderer backend #1168

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"exporter",

"render/canvas",
"render/svg",
"render/wgpu",
"render/common_tess",
"render/webgl",
Expand Down
1 change: 1 addition & 0 deletions exporter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "MIT OR Apache-2.0"
clap = "3.0.0-beta.2"
ruffle_core = { path = "../core" }
ruffle_render_wgpu = { path = "../render/wgpu", features = ["clap"] }
ruffle_render_svg = { path = "../render/svg" }
env_logger = "0.8.2"
image = "0.23.12"
log = "0.4"
Expand Down
182 changes: 124 additions & 58 deletions exporter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@ use ruffle_core::backend::input::NullInputBackend;
use ruffle_core::backend::locale::NullLocaleBackend;
use ruffle_core::backend::log::NullLogBackend;
use ruffle_core::backend::navigator::NullNavigatorBackend;
use ruffle_core::backend::render::RenderBackend;
use ruffle_core::backend::storage::MemoryStorageBackend;
use ruffle_core::tag_utils::SwfMovie;
use ruffle_core::Player;
use ruffle_render_svg::SvgRenderBackend;
use ruffle_render_wgpu::clap::{GraphicsBackend, PowerPreference};
use ruffle_render_wgpu::target::TextureTarget;
use ruffle_render_wgpu::{wgpu, Descriptors, WgpuRenderBackend};
use std::error::Error;
use std::fs::create_dir_all;
use std::fs::{create_dir_all, File};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use walkdir::{DirEntry, WalkDir};

#[derive(Clap, Debug, Copy, Clone, Eq, PartialEq, Hash)]
enum Format {
Png,
Svg,
}

#[derive(Clap, Debug, Copy, Clone)]
struct SizeOpt {
/// The amount to scale the page size with
Expand Down Expand Up @@ -61,6 +70,10 @@ struct Opt {
#[clap(short, long)]
silent: bool,

/// Export file format
#[clap(long, arg_enum, default_value = "png")]
format: Format,

#[clap(flatten)]
size: SizeOpt,

Expand All @@ -86,25 +99,41 @@ struct Opt {
trace_path: Option<PathBuf>,
}

enum CaptureDescriptors {
Png { descriptors: Descriptors },
Svg,
}

enum FileData {
Png(RgbaImage),
Svg(String),
}

fn take_screenshot(
descriptors: Descriptors,
descriptors: CaptureDescriptors,
swf_path: &Path,
frames: u32,
skipframes: u32,
progress: &Option<ProgressBar>,
size: SizeOpt,
) -> Result<(Descriptors, Vec<RgbaImage>), Box<dyn std::error::Error>> {
) -> Result<(CaptureDescriptors, Vec<FileData>), Box<dyn std::error::Error>> {
let movie = SwfMovie::from_path(&swf_path)?;

let width = size.width.unwrap_or_else(|| movie.width());
let width = (width as f32 * size.scale).round() as u32;

let height = size.height.unwrap_or_else(|| movie.height());
let height = (height as f32 * size.scale).round() as u32;

let target = TextureTarget::new(&descriptors.device, (width, height));
let is_png = matches!(descriptors, CaptureDescriptors::Png {..});
let backend: Box<dyn RenderBackend> = match descriptors {
CaptureDescriptors::Png { descriptors } => {
let target = TextureTarget::new(&descriptors.device, (width, height));
Box::new(WgpuRenderBackend::new(descriptors, target)?)
}
CaptureDescriptors::Svg => Box::new(SvgRenderBackend::new(width, height)),
};
let player = Player::new(
Box::new(WgpuRenderBackend::new(descriptors, target)?),
backend,
Box::new(NullAudioBackend::new()),
Box::new(NullNavigatorBackend::new()),
Box::new(NullInputBackend::new()),
Expand Down Expand Up @@ -134,15 +163,23 @@ fn take_screenshot(
if i >= skipframes {
player.lock().unwrap().render();
let mut player = player.lock().unwrap();
let renderer = player
.renderer_mut()
.downcast_mut::<WgpuRenderBackend<TextureTarget>>()
.unwrap();
let target = renderer.target();
if let Some(image) = target.capture(renderer.device()) {
result.push(image);
if is_png {
let renderer = player
.renderer_mut()
.downcast_mut::<WgpuRenderBackend<TextureTarget>>()
.unwrap();
let target = renderer.target();
if let Some(image) = target.capture(renderer.device()) {
result.push(FileData::Png(image));
} else {
return Err(format!("Unable to capture frame {} of {:?}", i, swf_path).into());
}
} else {
return Err(format!("Unable to capture frame {} of {:?}", i, swf_path).into());
let renderer = player
.renderer_mut()
.downcast_mut::<SvgRenderBackend>()
.unwrap();
result.push(FileData::Svg(renderer.to_string()));
}
}

Expand All @@ -151,16 +188,20 @@ fn take_screenshot(
}
}

let descriptors = Arc::try_unwrap(player)
.ok()
.unwrap()
.into_inner()?
.destroy()
.downcast::<WgpuRenderBackend<TextureTarget>>()
.ok()
.unwrap()
.descriptors();
Ok((descriptors, result))
if is_png {
let descriptors = Arc::try_unwrap(player)
.ok()
.unwrap()
.into_inner()?
.destroy()
.downcast::<WgpuRenderBackend<TextureTarget>>()
.ok()
.unwrap()
.descriptors();
Ok((CaptureDescriptors::Png { descriptors }, result))
} else {
Ok((CaptureDescriptors::Svg, result))
}
}

fn find_files(root: &Path, with_progress: bool) -> Vec<DirEntry> {
Expand Down Expand Up @@ -193,12 +234,16 @@ fn find_files(root: &Path, with_progress: bool) -> Vec<DirEntry> {
results
}

fn capture_single_swf(descriptors: Descriptors, opt: &Opt) -> Result<(), Box<dyn Error>> {
fn capture_single_swf(descriptors: CaptureDescriptors, opt: &Opt) -> Result<(), Box<dyn Error>> {
let output = opt.output_path.clone().unwrap_or_else(|| {
let mut result = PathBuf::new();
if opt.frames == 1 {
result.set_file_name(opt.swf.file_stem().unwrap());
result.set_extension("png");
result.set_extension(if matches!(descriptors, CaptureDescriptors::Png {..}) {
"png"
} else {
"svg"
});
} else {
result.set_file_name(opt.swf.file_stem().unwrap());
}
Expand Down Expand Up @@ -237,12 +282,17 @@ fn capture_single_swf(descriptors: Descriptors, opt: &Opt) -> Result<(), Box<dyn
}

if frames.len() == 1 {
frames.get(0).unwrap().save(&output)?;
match &frames[0] {
FileData::Png(image) => image.save(&output)?,
FileData::Svg(data) => File::create(&output)?.write_all(data.as_bytes())?,
}
} else {
for (frame, image) in frames.iter().enumerate() {
let mut path = PathBuf::from(&output);
path.push(format!("{}.png", frame));
image.save(&path)?;
match image {
FileData::Png(image) => image.save(output.join(format!("{}.png", frame)))?,
FileData::Svg(data) => File::create(output.join(format!("{}.svg", frame)))?
.write_all(data.as_bytes())?,
}
}
}

Expand Down Expand Up @@ -270,7 +320,10 @@ fn capture_single_swf(descriptors: Descriptors, opt: &Opt) -> Result<(), Box<dyn
Ok(())
}

fn capture_multiple_swfs(mut descriptors: Descriptors, opt: &Opt) -> Result<(), Box<dyn Error>> {
fn capture_multiple_swfs(
mut descriptors: CaptureDescriptors,
opt: &Opt,
) -> Result<(), Box<dyn Error>> {
let output = opt.output_path.clone().unwrap();
let files = find_files(&opt.swf, !opt.silent);

Expand Down Expand Up @@ -310,22 +363,29 @@ fn capture_multiple_swfs(mut descriptors: Descriptors, opt: &Opt) -> Result<(),
.to_path_buf();

if frames.len() == 1 {
let mut destination = PathBuf::from(&output);
relative_path.set_extension("png");
destination.push(relative_path);
if let Some(parent) = destination.parent() {
let _ = create_dir_all(parent);
let _ = create_dir_all(&output);
let mut destination = output.join(relative_path);
match &frames[0] {
FileData::Png(image) => {
destination.set_extension("png");
image.save(&destination)?
}
FileData::Svg(data) => {
destination.set_extension("svg");
File::create(&destination)?.write_all(data.as_bytes())?
}
}
frames.get(0).unwrap().save(&destination)?;
} else {
let mut parent = PathBuf::from(&output);
relative_path.set_extension("");
parent.push(&relative_path);
let _ = create_dir_all(&parent);
for (frame, image) in frames.iter().enumerate() {
let mut destination = parent.clone();
destination.push(format!("{}.png", frame));
image.save(&destination)?;
match image {
FileData::Png(image) => image.save(parent.join(format!("{}.png", frame)))?,
FileData::Svg(data) => File::create(parent.join(format!("{}.svg", frame)))?
.write_all(data.as_bytes())?,
}
}
}
}
Expand Down Expand Up @@ -371,24 +431,30 @@ fn trace_path(_opt: &Opt) -> Option<&Path> {

fn main() -> Result<(), Box<dyn Error>> {
let opt: Opt = Opt::parse();
let instance = wgpu::Instance::new(opt.graphics.into());
let adapter = block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: opt.power.into(),
compatible_surface: None,
}))
.ok_or(
"This tool requires hardware acceleration, but no compatible graphics device was found.",
)?;

let (device, queue) = block_on(adapter.request_device(
&wgpu::DeviceDescriptor {
features: Default::default(),
limits: wgpu::Limits::default(),
shader_validation: false,
},
trace_path(&opt),
))?;
let descriptors = Descriptors::new(device, queue)?;
let descriptors = match opt.format {
Format::Png => {
let instance = wgpu::Instance::new(opt.graphics.into());
let adapter = block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: opt.power.into(),
compatible_surface: None,
}))
.ok_or(
"This tool requires hardware acceleration, but no compatible graphics device was found."
)?;

let (device, queue) = block_on(adapter.request_device(
&wgpu::DeviceDescriptor {
features: Default::default(),
limits: wgpu::Limits::default(),
shader_validation: false,
},
trace_path(&opt),
))?;
let descriptors = Descriptors::new(device, queue)?;
CaptureDescriptors::Png { descriptors }
}
Format::Svg => CaptureDescriptors::Svg,
};

if opt.swf.is_file() {
capture_single_swf(descriptors, &opt)?;
Expand Down
20 changes: 20 additions & 0 deletions render/svg/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "ruffle_render_svg"
version = "0.1.0"
authors = ["Mike Welsh <mwelsh@gmail.com>", "Ben Schattinger <developer@lights0123.com>"]
edition = "2018"
license = "MIT OR Apache-2.0"

[dependencies]
base64 = "0.12.3"
fnv = "1.0.7"
svg = "0.8.0"
png = "0.16.7"

[dependencies.jpeg-decoder]
version = "0.1.20"
#default-features = false # can't use rayon on web

[dependencies.ruffle_core]
path = "../../core"
#default-features = false
Loading