diff options
author | Anthony Ramine <n.oxyde@gmail.com> | 2018-10-11 15:46:42 +0200 |
---|---|---|
committer | Anthony Ramine <n.oxyde@gmail.com> | 2018-10-11 15:46:42 +0200 |
commit | bb2101f540f542b51c431d694b09fc1da5a0fac3 (patch) | |
tree | b354d2e7f9ceacbc714eb8f8247e9e04edc13d13 | |
parent | 35bf180dcb588f02652b2031316713f72d12be62 (diff) | |
download | servo-bb2101f540f542b51c431d694b09fc1da5a0fac3.tar.gz servo-bb2101f540f542b51c431d694b09fc1da5a0fac3.zip |
Simplify ctx.drawImage a bit
There is no need to swap between RGBA and BGRA twice.
-rw-r--r-- | components/canvas/canvas_paint_thread.rs | 12 | ||||
-rw-r--r-- | components/script/dom/canvasrenderingcontext2d.rs | 33 |
2 files changed, 9 insertions, 36 deletions
diff --git a/components/canvas/canvas_paint_thread.rs b/components/canvas/canvas_paint_thread.rs index 21ea434a369..bdd70fbb8ef 100644 --- a/components/canvas/canvas_paint_thread.rs +++ b/components/canvas/canvas_paint_thread.rs @@ -7,7 +7,6 @@ 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; @@ -139,13 +138,10 @@ impl<'a> CanvasPaintThread <'a> { source_rect, smoothing_enabled, ) => { - let data = match imagedata { - None => vec![0; image_size.width as usize * image_size.height as usize * 4], - Some(mut data) => { - pixels::byte_swap_colors_inplace(&mut data); - data.into() - }, - }; + let data = imagedata.map_or_else( + || vec![0; image_size.width as usize * image_size.height as usize * 4], + |bytes| bytes.into(), + ); self.canvas(canvas_id).draw_image( data, image_size, diff --git a/components/script/dom/canvasrenderingcontext2d.rs b/components/script/dom/canvasrenderingcontext2d.rs index dacabcccce5..f88ae42e452 100644 --- a/components/script/dom/canvasrenderingcontext2d.rs +++ b/components/script/dom/canvasrenderingcontext2d.rs @@ -403,39 +403,16 @@ impl CanvasRenderingContext2D { dh: Option<f64>, ) -> ErrorResult { debug!("Fetching image {}.", url); - // https://html.spec.whatwg.org/multipage/#img-error - // If the image argument is an HTMLImageElement object that is in the broken state, - // then throw an InvalidStateError exception - let (image_data, image_size) = match self.fetch_image_data(url) { - Some((mut data, size)) => { - // Pixels come from cache in BGRA order and drawImage expects RGBA so we - // have to swap the color values - pixels::byte_swap_and_premultiply_inplace(&mut data); - let size = Size2D::new(size.width as f64, size.height as f64); - (data, size) - }, - None => return Err(Error::InvalidState), - }; + let (mut image_data, image_size) = + self.fetch_image_data(url).ok_or(Error::InvalidState)?; + pixels::premultiply_inplace(&mut image_data); + let image_size = image_size.to_f64(); + let dw = dw.unwrap_or(image_size.width); let dh = dh.unwrap_or(image_size.height); let sw = sw.unwrap_or(image_size.width); let sh = sh.unwrap_or(image_size.height); - self.draw_image_data(image_data, image_size, sx, sy, sw, sh, dx, dy, dw, dh) - } - fn draw_image_data( - &self, - image_data: Vec<u8>, - image_size: Size2D<f64>, - sx: f64, - sy: f64, - sw: f64, - sh: f64, - dx: f64, - dy: f64, - dw: f64, - dh: f64, - ) -> ErrorResult { // Establish the source and destination rectangles let (source_rect, dest_rect) = self.adjust_source_dest_rects(image_size, sx, sy, sw, sh, dx, dy, dw, dh); |