Skip to content

Commit c8d82c3

Browse files
author
bors-servo
authored
Auto merge of #1764 - jrmuizel:list-resources, r=kvark
Add a command to list the resources used by a yaml file I'd like to split this up into commits but am putting it here for now in case someone else wants it. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/webrender/1764) <!-- Reviewable:end -->
2 parents 582b0ac + c6c908c commit c8d82c3

File tree

3 files changed

+171
-151
lines changed

3 files changed

+171
-151
lines changed

wrench/src/args.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ subcommands:
7878
help: Include the given element type. Can be specified multiple times. (rect/image/text/glyphs/border)
7979
multiple: true
8080
takes_value: true
81+
- list-resources:
82+
long: list-resources
83+
help: List the resources used by this YAML file
8184
- watch:
8285
short: w
8386
long: watch

wrench/src/wrench.rs

+1-146
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ use dwrote;
1111
#[cfg(any(target_os = "linux", target_os = "macos"))]
1212
use font_loader::system_fonts;
1313
use glutin::WindowProxy;
14-
use image;
15-
use image::GenericImage;
1614
use json_frame_writer::JsonFrameWriter;
17-
use parse_function::parse_function;
18-
use premultiply::premultiply;
1915
use std::collections::HashMap;
20-
use std::path::{Path, PathBuf};
16+
use std::path::PathBuf;
2117
use time;
2218
use webrender;
2319
use webrender::api::*;
@@ -119,8 +115,6 @@ pub struct Wrench {
119115

120116
window_title_to_set: Option<String>,
121117

122-
image_map: HashMap<(PathBuf, Option<i64>), (ImageKey, LayoutSize)>,
123-
124118
graphics_api: webrender::GraphicsApiInfo,
125119

126120
pub rebuild_display_lists: bool,
@@ -198,8 +192,6 @@ impl Wrench {
198192
verbose,
199193
device_pixel_ratio: dp_ratio,
200194

201-
image_map: HashMap::new(),
202-
203195
root_pipeline_id: PipelineId(0, 0),
204196

205197
graphics_api,
@@ -385,76 +377,6 @@ impl Wrench {
385377
key
386378
}
387379

388-
pub fn add_or_get_image(&mut self, file: &Path, tiling: Option<i64>) -> (ImageKey, LayoutSize) {
389-
let key = (file.to_owned(), tiling);
390-
if let Some(k) = self.image_map.get(&key) {
391-
return *k;
392-
}
393-
394-
let (descriptor, image_data) = match image::open(file) {
395-
Ok(image) => {
396-
let image_dims = image.dimensions();
397-
let format = match image {
398-
image::ImageLuma8(_) => ImageFormat::A8,
399-
image::ImageRgb8(_) => ImageFormat::RGB8,
400-
image::ImageRgba8(_) => ImageFormat::BGRA8,
401-
_ => panic!("We don't support whatever your crazy image type is, come on"),
402-
};
403-
let mut bytes = image.raw_pixels();
404-
if format == ImageFormat::BGRA8 {
405-
premultiply(bytes.as_mut_slice());
406-
}
407-
let descriptor = ImageDescriptor::new(
408-
image_dims.0,
409-
image_dims.1,
410-
format,
411-
is_image_opaque(format, &bytes[..]),
412-
);
413-
let data = ImageData::new(bytes);
414-
(descriptor, data)
415-
}
416-
_ => {
417-
// This is a hack but it is convenient when generating test cases and avoids
418-
// bloating the repository.
419-
match parse_function(
420-
file.components()
421-
.last()
422-
.unwrap()
423-
.as_os_str()
424-
.to_str()
425-
.unwrap(),
426-
) {
427-
("xy-gradient", args, _) => generate_xy_gradient_image(
428-
args.get(0).unwrap_or(&"1000").parse::<u32>().unwrap(),
429-
args.get(1).unwrap_or(&"1000").parse::<u32>().unwrap(),
430-
),
431-
("solid-color", args, _) => generate_solid_color_image(
432-
args.get(0).unwrap_or(&"255").parse::<u8>().unwrap(),
433-
args.get(1).unwrap_or(&"255").parse::<u8>().unwrap(),
434-
args.get(2).unwrap_or(&"255").parse::<u8>().unwrap(),
435-
args.get(3).unwrap_or(&"255").parse::<u8>().unwrap(),
436-
args.get(4).unwrap_or(&"1000").parse::<u32>().unwrap(),
437-
args.get(5).unwrap_or(&"1000").parse::<u32>().unwrap(),
438-
),
439-
_ => {
440-
panic!("Failed to load image {:?}", file.to_str());
441-
}
442-
}
443-
}
444-
};
445-
let tiling = tiling.map(|tile_size| tile_size as u16);
446-
let image_key = self.api.generate_image_key();
447-
let mut resources = ResourceUpdates::new();
448-
resources.add_image(image_key, descriptor, image_data, tiling);
449-
self.api.update_resources(resources);
450-
let val = (
451-
image_key,
452-
LayoutSize::new(descriptor.width as f32, descriptor.height as f32),
453-
);
454-
self.image_map.insert(key, val);
455-
val
456-
}
457-
458380
pub fn update(&mut self, dim: DeviceUintSize) {
459381
if dim != self.window_size {
460382
self.window_size = dim;
@@ -538,70 +460,3 @@ impl Wrench {
538460
}
539461
}
540462
}
541-
542-
fn is_image_opaque(format: ImageFormat, bytes: &[u8]) -> bool {
543-
match format {
544-
ImageFormat::BGRA8 => {
545-
let mut is_opaque = true;
546-
for i in 0 .. (bytes.len() / 4) {
547-
if bytes[i * 4 + 3] != 255 {
548-
is_opaque = false;
549-
break;
550-
}
551-
}
552-
is_opaque
553-
}
554-
ImageFormat::RGB8 => true,
555-
ImageFormat::RG8 => true,
556-
ImageFormat::A8 => false,
557-
ImageFormat::Invalid | ImageFormat::RGBAF32 => unreachable!(),
558-
}
559-
}
560-
561-
fn generate_xy_gradient_image(w: u32, h: u32) -> (ImageDescriptor, ImageData) {
562-
let mut pixels = Vec::with_capacity((w * h * 4) as usize);
563-
for y in 0 .. h {
564-
for x in 0 .. w {
565-
let grid = if x % 100 < 3 || y % 100 < 3 { 0.9 } else { 1.0 };
566-
pixels.push((y as f32 / h as f32 * 255.0 * grid) as u8);
567-
pixels.push(0);
568-
pixels.push((x as f32 / w as f32 * 255.0 * grid) as u8);
569-
pixels.push(255);
570-
}
571-
}
572-
573-
(
574-
ImageDescriptor::new(w, h, ImageFormat::BGRA8, true),
575-
ImageData::new(pixels),
576-
)
577-
}
578-
579-
fn generate_solid_color_image(
580-
r: u8,
581-
g: u8,
582-
b: u8,
583-
a: u8,
584-
w: u32,
585-
h: u32,
586-
) -> (ImageDescriptor, ImageData) {
587-
let buf_size = (w * h * 4) as usize;
588-
let mut pixels = Vec::with_capacity(buf_size);
589-
// Unsafely filling the buffer is horrible. Unfortunately doing this idiomatically
590-
// is terribly slow in debug builds to the point that reftests/image/very-big.yaml
591-
// takes more than 20 seconds to run on a recent laptop.
592-
unsafe {
593-
pixels.set_len(buf_size);
594-
let color: u32 = ::std::mem::transmute([b, g, r, a]);
595-
let mut ptr: *mut u32 = ::std::mem::transmute(&mut pixels[0]);
596-
let end = ptr.offset((w * h) as isize);
597-
while ptr < end {
598-
*ptr = color;
599-
ptr = ptr.offset(1);
600-
}
601-
}
602-
603-
(
604-
ImageDescriptor::new(w, h, ImageFormat::BGRA8, a == 255),
605-
ImageData::new(pixels),
606-
)
607-
}

0 commit comments

Comments
 (0)