/
opts.rs
194 lines (162 loc) · 7.26 KB
/
opts.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Configuration options for a single run of the servo application. Created
//! from command line arguments.
use geometry::{DevicePixel, ScreenPx};
use azure::azure_hl::{BackendType, CairoBackend, CoreGraphicsBackend};
use azure::azure_hl::{CoreGraphicsAcceleratedBackend, Direct2DBackend, SkiaBackend};
use geom::scale_factor::ScaleFactor;
use getopts;
use std::cmp;
use std::io;
use std::os;
use std::rt;
/// Global flags for Servo, currently set on the command line.
#[deriving(Clone)]
pub struct Opts {
/// The initial URLs to load.
pub urls: Vec<String>,
/// The rendering backend to use (`-r`).
pub render_backend: BackendType,
/// How many threads to use for CPU rendering (`-t`).
///
/// FIXME(pcwalton): This is not currently used. All rendering is sequential.
pub n_render_threads: uint,
/// True to use CPU painting, false to use GPU painting via Skia-GL (`-c`). Note that
/// compositing is always done on the GPU.
pub cpu_painting: bool,
/// The maximum size of each tile in pixels (`-s`).
pub tile_size: uint,
/// The ratio of device pixels per px at the default scale. If unspecified, will use the
/// platform default setting.
pub device_pixels_per_px: Option<ScaleFactor<ScreenPx, DevicePixel, f32>>,
/// `None` to disable the profiler or `Some` with an interval in seconds to enable it and cause
/// it to produce output on that interval (`-p`).
pub profiler_period: Option<f64>,
/// The number of threads to use for layout (`-y`). Defaults to 1, which results in a recursive
/// sequential algorithm.
pub layout_threads: uint,
/// True to exit after the page load (`-x`).
pub exit_after_load: bool,
pub output_file: Option<String>,
pub headless: bool,
pub hard_fail: bool,
/// True if we should bubble intrinsic widths sequentially (`-b`). If this is true, then
/// intrinsic widths are computed as a separate pass instead of during flow construction. You
/// may wish to turn this flag on in order to benchmark style recalculation against other
/// browser engines.
pub bubble_widths_separately: bool,
/// Use native threads instead of green threads
pub native_threading: bool
}
fn print_usage(app: &str, opts: &[getopts::OptGroup]) {
let message = format!("Usage: {} [ options ... ] [URL]\n\twhere options include", app);
println!("{}", getopts::usage(message.as_slice(), opts));
}
fn args_fail(msg: &str) {
io::stderr().write_line(msg).unwrap();
os::set_exit_status(1);
}
pub fn from_cmdline_args(args: &[String]) -> Option<Opts> {
let app_name = args[0].to_str();
let args = args.tail();
let opts = vec![
getopts::optflag("c", "cpu", "CPU rendering"),
getopts::optopt("o", "output", "Output file", "output.png"),
getopts::optopt("r", "rendering", "Rendering backend", "direct2d|core-graphics|core-graphics-accelerated|cairo|skia."),
getopts::optopt("s", "size", "Size of tiles", "512"),
getopts::optopt("", "device-pixel-ratio", "Device pixels per px", ""),
getopts::optflagopt("p", "profile", "Profiler flag and output interval", "10"),
getopts::optflag("x", "exit", "Exit after load flag"),
getopts::optopt("t", "threads", "Number of render threads", "[n-cores]"),
getopts::optopt("y", "layout-threads", "Number of layout threads", "1"),
getopts::optflag("z", "headless", "Headless mode"),
getopts::optflag("f", "hard-fail", "Exit on task failure instead of displaying about:failure"),
getopts::optflag("b", "bubble-widths", "Bubble intrinsic widths separately like other engines"),
getopts::optflag("n", "native-threading", "Use native threading instead of green threading"),
getopts::optflag("h", "help", "Print this message")
];
let opt_match = match getopts::getopts(args, opts.as_slice()) {
Ok(m) => m,
Err(f) => {
args_fail(f.to_err_msg().as_slice());
return None;
}
};
if opt_match.opt_present("h") || opt_match.opt_present("help") {
print_usage(app_name.as_slice(), opts.as_slice());
return None;
};
let urls = if opt_match.free.is_empty() {
print_usage(app_name.as_slice(), opts.as_slice());
args_fail("servo asks that you provide 1 or more URLs");
return None;
} else {
opt_match.free.clone()
};
let render_backend = match opt_match.opt_str("r") {
Some(backend_str) => {
if "direct2d" == backend_str.as_slice() {
Direct2DBackend
} else if "core-graphics" == backend_str.as_slice() {
CoreGraphicsBackend
} else if "core-graphics-accelerated" == backend_str.as_slice() {
CoreGraphicsAcceleratedBackend
} else if "cairo" == backend_str.as_slice() {
CairoBackend
} else if "skia" == backend_str.as_slice() {
SkiaBackend
} else {
fail!("unknown backend type")
}
}
None => SkiaBackend
};
let tile_size: uint = match opt_match.opt_str("s") {
Some(tile_size_str) => from_str(tile_size_str.as_slice()).unwrap(),
None => 512,
};
let device_pixels_per_px = opt_match.opt_str("device-pixel-ratio").map(|dppx_str|
ScaleFactor(from_str(dppx_str.as_slice()).unwrap())
);
let n_render_threads: uint = match opt_match.opt_str("t") {
Some(n_render_threads_str) => from_str(n_render_threads_str.as_slice()).unwrap(),
None => {
// FIXME (rust/14707): This still isn't exposed publicly via std::rt??
// FIXME (rust/14704): Terrible name for this lint, which here is allowing
// Rust types, not C types
#[allow(ctypes)]
extern {
fn rust_get_num_cpus() -> uint;
}
unsafe { rust_get_num_cpus() as uint }
}
};
// if only flag is present, default to 5 second period
let profiler_period = opt_match.opt_default("p", "5").map(|period| {
from_str(period.as_slice()).unwrap()
});
let cpu_painting = opt_match.opt_present("c");
let layout_threads: uint = match opt_match.opt_str("y") {
Some(layout_threads_str) => from_str(layout_threads_str.as_slice()).unwrap(),
None => cmp::max(rt::default_sched_threads() * 3 / 4, 1),
};
let native_threading = opt_match.opt_present("h") || opt_match.opt_present("help");
Some(Opts {
urls: urls,
render_backend: render_backend,
n_render_threads: n_render_threads,
cpu_painting: cpu_painting,
tile_size: tile_size,
device_pixels_per_px: device_pixels_per_px,
profiler_period: profiler_period,
layout_threads: layout_threads,
exit_after_load: opt_match.opt_present("x"),
output_file: opt_match.opt_str("o"),
headless: opt_match.opt_present("z"),
hard_fail: opt_match.opt_present("f"),
bubble_widths_separately: opt_match.opt_present("b"),
native_threading: native_threading
})
}