diff --git a/platform/android/surface.rs b/platform/android/surface.rs index c0777df..80f525f 100644 --- a/platform/android/surface.rs +++ b/platform/android/surface.rs @@ -52,8 +52,8 @@ impl NativeCompositingGraphicsContext { } pub struct NativeSurface { - image: Option, - bitmap: *c_void, + image: Option, // For GPU rendering + bitmap: Option>, // For CPU rendering will_leak: bool, } @@ -65,7 +65,7 @@ impl NativeSurface { } NativeSurface { image : _image, - bitmap : ptr::null(), + bitmap: None, will_leak: true, } } @@ -74,15 +74,13 @@ impl NativeSurface { impl NativeSurfaceMethods for NativeSurface { /// This may only be called on the case of CPU rendering. fn new(_native_context: &NativePaintingGraphicsContext, size: Size2D, _stride: i32) -> NativeSurface { - unsafe { - let len = size.width * size.height * 4; - let bitmap: Vec = Vec::from_elem(len as uint, 0 as u8); - - NativeSurface { - image: None, - bitmap: mem::transmute(bitmap.as_ptr()), - will_leak : true, - } + let len = size.width * size.height * 4; + let bitmap: Vec = Vec::from_elem(len as uint, 0 as u8); + + NativeSurface { + image: None, + bitmap: Some(bitmap), + will_leak : true, } } @@ -92,32 +90,34 @@ impl NativeSurfaceMethods for NativeSurface { texture: &Texture, _size: Size2D) { let _bound = texture.bind(); - - unsafe { - match self.image { - None => { - if self.bitmap != ptr::null() { - glTexImage2D(TEXTURE_2D, 0, BGRA as i32, _size.width as i32, _size.height as i32, 0, BGRA as u32, UNSIGNED_BYTE, self.bitmap); - } - else { - debug!("Cannot bind the buffer(CPU rendering), there is no bitmap"); + match self.image { + None => match self.bitmap { + Some(ref bitmap) => { + let data = bitmap.as_ptr() as *c_void; + unsafe { + glTexImage2D(TEXTURE_2D, 0, BGRA as i32, _size.width as i32, _size.height as i32, + 0, BGRA as u32, UNSIGNED_BYTE, data); } - }, - Some(image_khr) => { - egl_image_target_texture2d_oes(TEXTURE_2D, image_khr); } + None => { + debug!("Cannot bind the buffer(CPU rendering), there is no bitmap"); + } + }, + Some(image_khr) => { + egl_image_target_texture2d_oes(TEXTURE_2D, image_khr); } } } /// This may only be called on the painting side. - fn upload(&self, _graphics_context: &NativePaintingGraphicsContext, data: &[u8]) { - unsafe { - if self.bitmap != ptr::null() { - let dest:&mut [u8] = mem::transmute((self.bitmap, data.len())); - dest.copy_memory(data); + fn upload(&mut self, _graphics_context: &NativePaintingGraphicsContext, data: &[u8]) { + match self.bitmap { + Some(ref mut bitmap) => { + unsafe { + bitmap.as_mut_slice().copy_memory(data); + } } - else { + None => { debug!("Cannot upload the buffer(CPU rendering), there is no bitmap"); } } diff --git a/platform/linux/surface.rs b/platform/linux/surface.rs index d19a3bd..f858c1a 100644 --- a/platform/linux/surface.rs +++ b/platform/linux/surface.rs @@ -266,7 +266,7 @@ impl NativeSurfaceMethods for NativeSurface { } /// This may only be called on the painting side. - fn upload(&self, graphics_context: &NativePaintingGraphicsContext, data: &[u8]) { + fn upload(&mut self, graphics_context: &NativePaintingGraphicsContext, data: &[u8]) { unsafe { // Ensure that we're running on the render task. Take the display. let pixmap = self.pixmap; diff --git a/platform/macos/surface.rs b/platform/macos/surface.rs index fcd118b..2f1dd5a 100644 --- a/platform/macos/surface.rs +++ b/platform/macos/surface.rs @@ -206,7 +206,7 @@ impl NativeSurfaceMethods for NativeSurface { io_surface.bind_to_gl_texture(size) } - fn upload(&self, _: &NativePaintingGraphicsContext, data: &[u8]) { + fn upload(&mut self, _: &NativePaintingGraphicsContext, data: &[u8]) { let io_surface = io_surface::lookup(self.io_surface_id.unwrap()); io_surface.upload(data) } diff --git a/platform/surface.rs b/platform/surface.rs index 0397a77..7672127 100644 --- a/platform/surface.rs +++ b/platform/surface.rs @@ -54,7 +54,7 @@ pub trait NativeSurfaceMethods { size: Size2D); /// Uploads pixel data to the surface. Painting task only. - fn upload(&self, native_context: &NativePaintingGraphicsContext, data: &[u8]); + fn upload(&mut self, native_context: &NativePaintingGraphicsContext, data: &[u8]); /// Returns an opaque ID identifying the surface for debugging. fn get_id(&self) -> int;