Skip to content

Commit

Permalink
Merge some byte swap/premultiply functions in their own crate
Browse files Browse the repository at this point in the history
  • Loading branch information
nox committed Oct 5, 2018
1 parent a2e3dd4 commit 784fbb2
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 96 deletions.
8 changes: 8 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 components/canvas/Cargo.toml
Expand Up @@ -24,6 +24,7 @@ ipc-channel = "0.11"
log = "0.4"
num-traits = "0.2"
offscreen_gl_context = {version = "0.21", features = ["serde", "osmesa"]}
pixels = {path = "../pixels"}
serde_bytes = "0.10"
servo_config = {path = "../config"}
webrender = {git = "https://github.com/servo/webrender"}
Expand Down
3 changes: 2 additions & 1 deletion components/canvas/canvas_paint_thread.rs
Expand Up @@ -7,6 +7,7 @@ use canvas_data::*;
use canvas_traits::canvas::*;
use euclid::Size2D;
use ipc_channel::ipc::{self, IpcSender};
use pixels;
use std::borrow::ToOwned;
use std::collections::HashMap;
use std::thread;
Expand Down Expand Up @@ -141,7 +142,7 @@ impl<'a> CanvasPaintThread <'a> {
let data = match imagedata {
None => vec![0; image_size.width as usize * image_size.height as usize * 4],
Some(mut data) => {
byte_swap(&mut data);
pixels::byte_swap_colors_inplace(&mut data);
data.into()
},
};
Expand Down
1 change: 1 addition & 0 deletions components/canvas/lib.rs
Expand Up @@ -15,6 +15,7 @@ extern crate ipc_channel;
#[macro_use] extern crate log;
extern crate num_traits;
extern crate offscreen_gl_context;
extern crate pixels;
extern crate serde_bytes;
extern crate servo_config;
extern crate webrender;
Expand Down
4 changes: 2 additions & 2 deletions components/canvas/webgl_thread.rs
Expand Up @@ -2,13 +2,13 @@
* 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/. */

use canvas_traits::canvas::byte_swap;
use canvas_traits::webgl::*;
use euclid::Size2D;
use fnv::FnvHashMap;
use gleam::gl;
use ipc_channel::ipc::IpcBytesSender;
use offscreen_gl_context::{GLContext, GLContextAttributes, GLLimits, NativeGLContextMethods};
use pixels;
use std::thread;
use super::gl_context::{GLContextFactory, GLContextWrapper};
use webrender;
Expand Down Expand Up @@ -562,7 +562,7 @@ impl<VR: WebVRRenderHandler + 'static> WebGLThread<VR> {
let src_slice = &orig_pixels[src_start .. src_start + stride];
(&mut pixels[dst_start .. dst_start + stride]).clone_from_slice(&src_slice[..stride]);
}
byte_swap(&mut pixels);
pixels::byte_swap_colors_inplace(&mut pixels);
pixels
}

Expand Down
35 changes: 0 additions & 35 deletions components/canvas_traits/canvas.rs
Expand Up @@ -382,38 +382,3 @@ impl FromStr for CompositionOrBlending {
Err(())
}
}

// TODO(pcwalton): Speed up with SIMD, or better yet, find some way to not do this.
pub fn byte_swap(data: &mut [u8]) {
let length = data.len();
// FIXME(rust #27741): Range::step_by is not stable yet as of this writing.
let mut i = 0;
while i < length {
let r = data[i + 2];
data[i + 2] = data[i + 0];
data[i + 0] = r;
i += 4;
}
}

pub fn multiply_u8_pixel(a: u8, b: u8) -> u8 {
return (a as u32 * b as u32 / 255) as u8;
}

pub fn byte_swap_and_premultiply(data: &mut [u8]) {
let length = data.len();

let mut i = 0;
while i < length {
let r = data[i + 2];
let g = data[i + 1];
let b = data[i + 0];
let a = data[i + 3];

data[i + 0] = multiply_u8_pixel(r, a);
data[i + 1] = multiply_u8_pixel(g, a);
data[i + 2] = multiply_u8_pixel(b, a);

i += 4;
}
}
1 change: 1 addition & 0 deletions components/net/Cargo.toml
Expand Up @@ -34,6 +34,7 @@ mime_guess = "1.8.0"
msg = {path = "../msg"}
net_traits = {path = "../net_traits"}
openssl = "0.9"
pixels = {path = "../pixels"}
profile_traits = {path = "../profile_traits"}
serde = "1.0"
serde_json = "1.0"
Expand Down
27 changes: 2 additions & 25 deletions components/net/image_cache.rs
Expand Up @@ -9,6 +9,7 @@ use net_traits::image::base::{Image, ImageMetadata, PixelFormat, load_from_memor
use net_traits::image_cache::{CanRequestImages, ImageCache, ImageResponder};
use net_traits::image_cache::{ImageOrMetadataAvailable, ImageResponse, ImageState};
use net_traits::image_cache::{PendingImageId, UsePlaceholder};
use pixels;
use servo_url::ServoUrl;
use std::collections::HashMap;
use std::collections::hash_map::Entry::{Occupied, Vacant};
Expand Down Expand Up @@ -52,7 +53,7 @@ fn set_webrender_image_key(webrender_api: &webrender_api::RenderApi, image: &mut
let is_opaque = match image.format {
PixelFormat::BGRA8 => {
bytes.extend_from_slice(&*image.bytes);
premultiply(bytes.as_mut_slice())
pixels::premultiply_inplace(bytes.as_mut_slice())
}
PixelFormat::RGB8 => {
for bgr in image.bytes.chunks(3) {
Expand Down Expand Up @@ -86,30 +87,6 @@ fn set_webrender_image_key(webrender_api: &webrender_api::RenderApi, image: &mut
image.id = Some(image_key);
}

// Returns true if the image was found to be
// completely opaque.
fn premultiply(data: &mut [u8]) -> bool {
let mut is_opaque = true;
let length = data.len();

let mut i = 0;
while i < length {
let b = data[i + 0] as u32;
let g = data[i + 1] as u32;
let r = data[i + 2] as u32;
let a = data[i + 3] as u32;

data[i + 0] = (b * a / 255) as u8;
data[i + 1] = (g * a / 255) as u8;
data[i + 2] = (r * a / 255) as u8;

i += 4;
is_opaque = is_opaque && a == 255;
}

is_opaque
}

// ======================================================================
// Aux structs and enums.
// ======================================================================
Expand Down
1 change: 1 addition & 0 deletions components/net/lib.rs
Expand Up @@ -27,6 +27,7 @@ extern crate mime_guess;
extern crate msg;
extern crate net_traits;
extern crate openssl;
extern crate pixels;
#[macro_use]
extern crate profile_traits;
#[macro_use] extern crate serde;
Expand Down
1 change: 1 addition & 0 deletions components/net_traits/Cargo.toml
Expand Up @@ -24,6 +24,7 @@ malloc_size_of = { path = "../malloc_size_of" }
malloc_size_of_derive = { path = "../malloc_size_of_derive" }
msg = {path = "../msg"}
num-traits = "0.2"
pixels = {path = "../pixels"}
serde = "1.0"
servo_arc = {path = "../servo_arc"}
servo_config = {path = "../config"}
Expand Down
21 changes: 2 additions & 19 deletions components/net_traits/image/base.rs
Expand Up @@ -4,6 +4,7 @@

use ipc_channel::ipc::IpcSharedMemory;
use piston_image::{self, DynamicImage, ImageFormat};
use pixels;
use std::fmt;
use webrender_api;

Expand Down Expand Up @@ -46,24 +47,6 @@ pub struct ImageMetadata {
// FIXME: Images must not be copied every frame. Instead we should atomically
// reference count them.

// TODO(pcwalton): Speed up with SIMD, or better yet, find some way to not do this.
fn byte_swap_colors_inplace(data: &mut [u8]) {
let length = data.len();

let mut i = 0;
while i < length {
let r = data[i + 2];
let g = data[i + 1];
let b = data[i + 0];

data[i + 0] = r;
data[i + 1] = g;
data[i + 2] = b;

i += 4;
}
}

pub fn load_from_memory(buffer: &[u8]) -> Option<Image> {
if buffer.is_empty() {
return None;
Expand All @@ -82,7 +65,7 @@ pub fn load_from_memory(buffer: &[u8]) -> Option<Image> {
DynamicImage::ImageRgba8(rgba) => rgba,
image => image.to_rgba(),
};
byte_swap_colors_inplace(&mut *rgba);
pixels::byte_swap_colors_inplace(&mut *rgba);
Some(Image {
width: rgba.width(),
height: rgba.height(),
Expand Down
1 change: 1 addition & 0 deletions components/net_traits/lib.rs
Expand Up @@ -17,6 +17,7 @@ extern crate ipc_channel;
#[macro_use] extern crate malloc_size_of_derive;
extern crate msg;
extern crate num_traits;
extern crate pixels;
#[macro_use] extern crate serde;
extern crate servo_arc;
extern crate servo_url;
Expand Down
10 changes: 10 additions & 0 deletions components/pixels/Cargo.toml
@@ -0,0 +1,10 @@
[package]
name = "pixels"
version = "0.0.1"
authors = ["The Servo Project Developers"]
license = "MPL-2.0"
publish = false

[lib]
name = "pixels"
path = "lib.rs"
40 changes: 40 additions & 0 deletions components/pixels/lib.rs
@@ -0,0 +1,40 @@
/* 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/. */

// TODO(pcwalton): Speed up with SIMD, or better yet, find some way to not do this.
pub fn byte_swap_colors_inplace(pixels: &mut [u8]) {
assert!(pixels.len() % 4 == 0);
for rgba in pixels.chunks_mut(4) {
let b = rgba[0];
rgba[0] = rgba[2];
rgba[2] = b;
}
}

pub fn byte_swap_and_premultiply_inplace(pixels: &mut [u8]) {
assert!(pixels.len() % 4 == 0);
for rgba in pixels.chunks_mut(4) {
let b = rgba[0];
rgba[0] = multiply_u8_color(rgba[2], rgba[3]);
rgba[1] = multiply_u8_color(rgba[1], rgba[3]);
rgba[2] = multiply_u8_color(b, rgba[3]);
}
}

/// Returns true if the pixels were found to be completely opaque.
pub fn premultiply_inplace(pixels: &mut [u8]) -> bool {
assert!(pixels.len() % 4 == 0);
let mut is_opaque = true;
for rgba in pixels.chunks_mut(4) {
rgba[0] = multiply_u8_color(rgba[0], rgba[3]);
rgba[1] = multiply_u8_color(rgba[1], rgba[3]);
rgba[2] = multiply_u8_color(rgba[2], rgba[3]);
is_opaque = is_opaque && rgba[3] == 255;
}
is_opaque
}

pub fn multiply_u8_color(a: u8, b: u8) -> u8 {
return (a as u32 * b as u32 / 255) as u8;
}
1 change: 1 addition & 0 deletions components/script/Cargo.toml
Expand Up @@ -75,6 +75,7 @@ num-traits = "0.2"
offscreen_gl_context = {version = "0.21", features = ["serde"]}
parking_lot = "0.6"
phf = "0.7.18"
pixels = {path = "../pixels"}
profile_traits = {path = "../profile_traits"}
ref_filter_map = "1.0.1"
ref_slice = "1.0"
Expand Down
5 changes: 3 additions & 2 deletions components/script/dom/canvasrenderingcontext2d.rs
Expand Up @@ -5,7 +5,7 @@
use canvas_traits::canvas::{Canvas2dMsg, CanvasMsg, CanvasId};
use canvas_traits::canvas::{CompositionOrBlending, FillOrStrokeStyle, FillRule};
use canvas_traits::canvas::{LineCapStyle, LineJoinStyle, LinearGradientStyle};
use canvas_traits::canvas::{RadialGradientStyle, RepetitionStyle, byte_swap_and_premultiply};
use canvas_traits::canvas::{RadialGradientStyle, RepetitionStyle};
use cssparser::{Parser, ParserInput, RGBA};
use cssparser::Color as CSSColor;
use dom::bindings::cell::DomRefCell;
Expand Down Expand Up @@ -41,6 +41,7 @@ use net_traits::image_cache::ImageResponse;
use net_traits::image_cache::ImageState;
use net_traits::image_cache::UsePlaceholder;
use num_traits::ToPrimitive;
use pixels;
use profile_traits::ipc as profiled_ipc;
use script_traits::ScriptMsg;
use servo_url::ServoUrl;
Expand Down Expand Up @@ -410,7 +411,7 @@ impl CanvasRenderingContext2D {
Some((mut data, size)) => {
// Pixels come from cache in BGRA order and drawImage expects RGBA so we
// have to swap the color values
byte_swap_and_premultiply(&mut data);
pixels::byte_swap_and_premultiply_inplace(&mut data);
let size = Size2D::new(size.width as f64, size.height as f64);
(data, size)
},
Expand Down

0 comments on commit 784fbb2

Please sign in to comment.