diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2018-10-03 06:00:50 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-03 06:00:50 -0400 |
commit | 74e7736720f457abc62ffe82cdda1db230320747 (patch) | |
tree | 6d4532f210773257b3db5a67e833f7d321fbfc83 /components/script | |
parent | 7fa2b2c879923a8b1f05be59ec9511ba8d5928c8 (diff) | |
parent | 5efbeea61ca619cbded91acb4b1b468aaa5e50b0 (diff) | |
download | servo-74e7736720f457abc62ffe82cdda1db230320747.tar.gz servo-74e7736720f457abc62ffe82cdda1db230320747.zip |
Auto merge of #21850 - servo:webgl, r=jdm
Don't use an intermediate PNG buffer in HTMLCanvasElement::ToDataURL
<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21850)
<!-- Reviewable:end -->
Diffstat (limited to 'components/script')
-rw-r--r-- | components/script/dom/bindings/trace.rs | 15 | ||||
-rw-r--r-- | components/script/dom/canvaspattern.rs | 8 | ||||
-rw-r--r-- | components/script/dom/canvasrenderingcontext2d.rs | 10 | ||||
-rw-r--r-- | components/script/dom/htmlcanvaselement.rs | 40 | ||||
-rw-r--r-- | components/script/dom/imagedata.rs | 4 | ||||
-rw-r--r-- | components/script/dom/paintrenderingcontext2d.rs | 3 | ||||
-rw-r--r-- | components/script/dom/webgl2renderingcontext.rs | 6 | ||||
-rw-r--r-- | components/script/dom/webglrenderingcontext.rs | 23 |
8 files changed, 60 insertions, 49 deletions
diff --git a/components/script/dom/bindings/trace.rs b/components/script/dom/bindings/trace.rs index 2c395094e7f..a3c4c7814ea 100644 --- a/components/script/dom/bindings/trace.rs +++ b/components/script/dom/bindings/trace.rs @@ -261,7 +261,13 @@ unsafe impl<T: JSTraceable> JSTraceable for VecDeque<T> { } } -unsafe impl<T: JSTraceable> JSTraceable for (T, T, T, T) { +unsafe impl<A, B, C, D> JSTraceable for (A, B, C, D) +where + A: JSTraceable, + B: JSTraceable, + C: JSTraceable, + D: JSTraceable, +{ unsafe fn trace(&self, trc: *mut JSTracer) { self.0.trace(trc); self.1.trace(trc); @@ -616,6 +622,13 @@ unsafe impl<U> JSTraceable for TypedSize2D<f32, U> { } } +unsafe impl<U> JSTraceable for TypedSize2D<u32, U> { + #[inline] + unsafe fn trace(&self, _trc: *mut JSTracer) { + // Do nothing + } +} + unsafe impl JSTraceable for StyleLocked<FontFaceRule> { unsafe fn trace(&self, _trc: *mut JSTracer) { // Do nothing. diff --git a/components/script/dom/canvaspattern.rs b/components/script/dom/canvaspattern.rs index ced7a6bb11b..bcc6e35f5f5 100644 --- a/components/script/dom/canvaspattern.rs +++ b/components/script/dom/canvaspattern.rs @@ -16,7 +16,7 @@ use euclid::Size2D; pub struct CanvasPattern { reflector_: Reflector, surface_data: Vec<u8>, - surface_size: Size2D<i32>, + surface_size: Size2D<u32>, repeat_x: bool, repeat_y: bool, origin_clean: bool, @@ -25,7 +25,7 @@ pub struct CanvasPattern { impl CanvasPattern { fn new_inherited( surface_data: Vec<u8>, - surface_size: Size2D<i32>, + surface_size: Size2D<u32>, repeat: RepetitionStyle, origin_clean: bool, ) -> CanvasPattern { @@ -39,7 +39,7 @@ impl CanvasPattern { CanvasPattern { reflector_: Reflector::new(), surface_data: surface_data, - surface_size: surface_size, + surface_size, repeat_x: x, repeat_y: y, origin_clean: origin_clean, @@ -48,7 +48,7 @@ impl CanvasPattern { pub fn new( global: &GlobalScope, surface_data: Vec<u8>, - surface_size: Size2D<i32>, + surface_size: Size2D<u32>, repeat: RepetitionStyle, origin_clean: bool, ) -> DomRoot<CanvasPattern> { diff --git a/components/script/dom/canvasrenderingcontext2d.rs b/components/script/dom/canvasrenderingcontext2d.rs index e3ca01c311e..2b364c3702d 100644 --- a/components/script/dom/canvasrenderingcontext2d.rs +++ b/components/script/dom/canvasrenderingcontext2d.rs @@ -128,7 +128,7 @@ impl CanvasRenderingContext2D { canvas: Option<&HTMLCanvasElement>, image_cache: Arc<ImageCache>, base_url: ServoUrl, - size: Size2D<i32>, + size: Size2D<u32>, ) -> CanvasRenderingContext2D { debug!("Creating new canvas rendering context."); let (sender, receiver) = @@ -157,7 +157,7 @@ impl CanvasRenderingContext2D { pub fn new( global: &GlobalScope, canvas: &HTMLCanvasElement, - size: Size2D<i32>, + size: Size2D<u32>, ) -> DomRoot<CanvasRenderingContext2D> { let window = window_from_node(canvas); let image_cache = window.image_cache(); @@ -173,7 +173,7 @@ impl CanvasRenderingContext2D { } // https://html.spec.whatwg.org/multipage/#concept-canvas-set-bitmap-dimensions - pub fn set_bitmap_dimensions(&self, size: Size2D<i32>) { + pub fn set_bitmap_dimensions(&self, size: Size2D<u32>) { self.reset_to_initial_state(); self.ipc_renderer .send(CanvasMsg::Recreate(size, self.get_canvas_id())) @@ -456,7 +456,7 @@ impl CanvasRenderingContext2D { Ok(()) } - fn fetch_image_data(&self, url: ServoUrl) -> Option<(Vec<u8>, Size2D<i32>)> { + fn fetch_image_data(&self, url: ServoUrl) -> Option<(Vec<u8>, Size2D<u32>)> { let img = match self.request_image_from_cache(url) { ImageResponse::Loaded(img, _) => img, ImageResponse::PlaceholderLoaded(_, _) | @@ -466,7 +466,7 @@ impl CanvasRenderingContext2D { }, }; - let image_size = Size2D::new(img.width as i32, img.height as i32); + let image_size = Size2D::new(img.width, img.height); let image_data = match img.format { PixelFormat::BGRA8 => img.bytes.to_vec(), PixelFormat::K8 => panic!("K8 color type not supported"), diff --git a/components/script/dom/htmlcanvaselement.rs b/components/script/dom/htmlcanvaselement.rs index 00b04db7ee8..09114a95d0a 100644 --- a/components/script/dom/htmlcanvaselement.rs +++ b/components/script/dom/htmlcanvaselement.rs @@ -98,8 +98,8 @@ impl HTMLCanvasElement { } } - pub fn get_size(&self) -> Size2D<i32> { - Size2D::new(self.Width() as i32, self.Height() as i32) + pub fn get_size(&self) -> Size2D<u32> { + Size2D::new(self.Width(), self.Height()) } pub fn origin_is_clean(&self) -> bool { @@ -277,7 +277,7 @@ impl HTMLCanvasElement { self.Height() != 0 && self.Width() != 0 } - pub fn fetch_all_data(&self) -> Option<(Vec<u8>, Size2D<i32>)> { + pub fn fetch_all_data(&self) -> Option<(Vec<u8>, Size2D<u32>)> { let size = self.get_size(); if size.width == 0 || size.height == 0 { @@ -383,12 +383,11 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement { None => return Ok(USVString("data:,".into())), } }, - Some(CanvasContext::WebGL2(ref context)) => match context - .base_context() - .get_image_data(self.Width(), self.Height()) - { - Some(data) => data, - None => return Ok(USVString("data:,".into())), + Some(CanvasContext::WebGL2(ref context)) => { + match context.base_context().get_image_data(self.Width(), self.Height()) { + Some(data) => data, + None => return Ok(USVString("data:,".into())), + } }, None => { // Each pixel is fully-transparent black. @@ -396,19 +395,16 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement { }, }; - // Only handle image/png for now. - let mime_type = "image/png"; - - let mut encoded = Vec::new(); - { - let encoder: PNGEncoder<&mut Vec<u8>> = PNGEncoder::new(&mut encoded); - encoder - .encode(&raw_data, self.Width(), self.Height(), ColorType::RGBA(8)) - .unwrap(); - } - - let encoded = base64::encode(&encoded); - Ok(USVString(format!("data:{};base64,{}", mime_type, encoded))) + // FIXME: Only handle image/png for now. + let mut png = Vec::new(); + PNGEncoder::new(&mut png) + .encode(&raw_data, self.Width(), self.Height(), ColorType::RGBA(8)) + .unwrap(); + let mut url = "data:image/png;base64,".to_owned(); + // FIXME(nox): Should this use base64::URL_SAFE? + // FIXME(nox): https://github.com/alicemaz/rust-base64/pull/56 + base64::encode_config_buf(&png, base64::STANDARD, &mut url); + Ok(USVString(url)) } } diff --git a/components/script/dom/imagedata.rs b/components/script/dom/imagedata.rs index d37e0092ba4..ea65d533ffe 100644 --- a/components/script/dom/imagedata.rs +++ b/components/script/dom/imagedata.rs @@ -149,8 +149,8 @@ impl ImageData { } } - pub fn get_size(&self) -> Size2D<i32> { - Size2D::new(self.Width() as i32, self.Height() as i32) + pub fn get_size(&self) -> Size2D<u32> { + Size2D::new(self.Width(), self.Height()) } } diff --git a/components/script/dom/paintrenderingcontext2d.rs b/components/script/dom/paintrenderingcontext2d.rs index 43124e30c76..1389dddcf3a 100644 --- a/components/script/dom/paintrenderingcontext2d.rs +++ b/components/script/dom/paintrenderingcontext2d.rs @@ -85,8 +85,7 @@ impl PaintRenderingContext2D { ) { let size = size * device_pixel_ratio; self.device_pixel_ratio.set(device_pixel_ratio); - self.context - .set_bitmap_dimensions(size.to_untyped().to_i32()); + self.context.set_bitmap_dimensions(size.to_untyped().to_u32()); self.scale_by_device_pixel_ratio(); } diff --git a/components/script/dom/webgl2renderingcontext.rs b/components/script/dom/webgl2renderingcontext.rs index a674cd719a6..ec839bf3fbf 100644 --- a/components/script/dom/webgl2renderingcontext.rs +++ b/components/script/dom/webgl2renderingcontext.rs @@ -49,7 +49,7 @@ impl WebGL2RenderingContext { fn new_inherited( window: &Window, canvas: &HTMLCanvasElement, - size: Size2D<i32>, + size: Size2D<u32>, attrs: GLContextAttributes, ) -> Option<WebGL2RenderingContext> { let base = WebGLRenderingContext::new(window, canvas, WebGLVersion::WebGL2, size, attrs)?; @@ -63,7 +63,7 @@ impl WebGL2RenderingContext { pub fn new( window: &Window, canvas: &HTMLCanvasElement, - size: Size2D<i32>, + size: Size2D<u32>, attrs: GLContextAttributes, ) -> Option<DomRoot<WebGL2RenderingContext>> { WebGL2RenderingContext::new_inherited(window, canvas, size, attrs).map(|ctx| { @@ -73,7 +73,7 @@ impl WebGL2RenderingContext { } impl WebGL2RenderingContext { - pub fn recreate(&self, size: Size2D<i32>) { + pub fn recreate(&self, size: Size2D<u32>) { self.base.recreate(size) } diff --git a/components/script/dom/webglrenderingcontext.rs b/components/script/dom/webglrenderingcontext.rs index 754637ad52b..7972201b9ce 100644 --- a/components/script/dom/webglrenderingcontext.rs +++ b/components/script/dom/webglrenderingcontext.rs @@ -157,7 +157,7 @@ pub struct WebGLRenderingContext { #[ignore_malloc_size_of = "Because it's small"] current_vertex_attrib_0: Cell<(f32, f32, f32, f32)>, #[ignore_malloc_size_of = "Because it's small"] - current_scissor: Cell<(i32, i32, i32, i32)>, + current_scissor: Cell<(i32, i32, u32, u32)>, #[ignore_malloc_size_of = "Because it's small"] current_clear_color: Cell<(f32, f32, f32, f32)>, extension_manager: WebGLExtensions, @@ -172,7 +172,7 @@ impl WebGLRenderingContext { window: &Window, canvas: &HTMLCanvasElement, webgl_version: WebGLVersion, - size: Size2D<i32>, + size: Size2D<u32>, attrs: GLContextAttributes, ) -> Result<WebGLRenderingContext, String> { if let Some(true) = PREFS @@ -229,7 +229,7 @@ impl WebGLRenderingContext { window: &Window, canvas: &HTMLCanvasElement, webgl_version: WebGLVersion, - size: Size2D<i32>, + size: Size2D<u32>, attrs: GLContextAttributes, ) -> Option<DomRoot<WebGLRenderingContext>> { match WebGLRenderingContext::new_inherited(window, canvas, webgl_version, size, attrs) { @@ -266,7 +266,7 @@ impl WebGLRenderingContext { }) } - pub fn recreate(&self, size: Size2D<i32>) { + pub fn recreate(&self, size: Size2D<u32>) { let (sender, receiver) = webgl_channel().unwrap(); self.webgl_sender.send_resize(size, sender).unwrap(); @@ -517,7 +517,7 @@ impl WebGLRenderingContext { fn get_image_pixels( &self, source: TexImageSource, - ) -> Fallible<Option<(Vec<u8>, Size2D<i32>, bool)>> { + ) -> Fallible<Option<(Vec<u8>, Size2D<u32>, bool)>> { Ok(Some(match source { TexImageSource::ImageData(image_data) => { (image_data.get_data_array(), image_data.get_size(), false) @@ -542,7 +542,7 @@ impl WebGLRenderingContext { ImageResponse::MetadataLoaded(_) => return Ok(None), }; - let size = Size2D::new(img.width as i32, img.height as i32); + let size = Size2D::new(img.width, img.height); // For now Servo's images are all stored as BGRA8 internally. let mut data = match img.format { @@ -2940,6 +2940,9 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { return self.webgl_error(InvalidValue); } + let width = width as u32; + let height = height as u32; + self.current_scissor.set((x, y, width, height)); self.send_command(WebGLCommand::Scissor(x, y, width, height)); } @@ -3791,8 +3794,8 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { target, level, internal_format, - size.width, - size.height, + size.width as i32, + size.height as i32, 0, format, data_type, @@ -4003,8 +4006,8 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext { target, level, format, - size.width, - size.height, + size.width as i32, + size.height as i32, 0, format, data_type, |