Improve WebGLBuffer::buffer_data

It now checks the usage argument itself and use generics for the data vector.
This commit is contained in:
Anthony Ramine 2018-04-04 12:42:38 +02:00
parent 34b13dac66
commit 605da95076
2 changed files with 16 additions and 24 deletions

View file

@ -7,6 +7,7 @@ use canvas_traits::webgl::{WebGLBufferId, WebGLCommand, WebGLError, WebGLMsgSend
use canvas_traits::webgl::webgl_channel; use canvas_traits::webgl::webgl_channel;
use dom::bindings::cell::DomRefCell; use dom::bindings::cell::DomRefCell;
use dom::bindings::codegen::Bindings::WebGLBufferBinding; use dom::bindings::codegen::Bindings::WebGLBufferBinding;
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants;
use dom::bindings::reflector::reflect_dom_object; use dom::bindings::reflector::reflect_dom_object;
use dom::bindings::root::DomRoot; use dom::bindings::root::DomRoot;
use dom::webglobject::WebGLObject; use dom::webglobject::WebGLObject;
@ -86,18 +87,25 @@ impl WebGLBuffer {
Ok(()) Ok(())
} }
pub fn buffer_data(&self, target: u32, data: &[u8], usage: u32) -> WebGLResult<()> { pub fn buffer_data<T>(&self, target: u32, data: T, usage: u32) -> WebGLResult<()>
where
T: Into<Vec<u8>>,
{
match usage {
WebGLRenderingContextConstants::STREAM_DRAW |
WebGLRenderingContextConstants::STATIC_DRAW |
WebGLRenderingContextConstants::DYNAMIC_DRAW => (),
_ => return Err(WebGLError::InvalidEnum),
}
if let Some(previous_target) = self.target.get() { if let Some(previous_target) = self.target.get() {
if target != previous_target { if target != previous_target {
return Err(WebGLError::InvalidOperation); return Err(WebGLError::InvalidOperation);
} }
} }
let data = data.into();
self.capacity.set(data.len()); self.capacity.set(data.len());
self.renderer.send(WebGLCommand::BufferData( self.renderer.send(WebGLCommand::BufferData(target, data.into(), usage)).unwrap();
target,
data.to_vec().into(),
usage,
)).unwrap();
Ok(()) Ok(())
} }

View file

@ -1704,15 +1704,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
None => return Ok(self.webgl_error(InvalidValue)), None => return Ok(self.webgl_error(InvalidValue)),
}; };
match usage { handle_potential_webgl_error!(self, bound_buffer.buffer_data(target, data_vec, usage));
constants::STREAM_DRAW |
constants::STATIC_DRAW |
constants::DYNAMIC_DRAW => (),
_ => return Ok(self.webgl_error(InvalidEnum)),
}
handle_potential_webgl_error!(self, bound_buffer.buffer_data(target, &data_vec, usage));
Ok(()) Ok(())
} }
@ -1733,18 +1725,10 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
return Ok(self.webgl_error(InvalidValue)); return Ok(self.webgl_error(InvalidValue));
} }
match usage {
constants::STREAM_DRAW |
constants::STATIC_DRAW |
constants::DYNAMIC_DRAW => (),
_ => return Ok(self.webgl_error(InvalidEnum)),
}
// FIXME: Allocating a buffer based on user-requested size is // FIXME: Allocating a buffer based on user-requested size is
// not great, but we don't have a fallible allocation to try. // not great, but we don't have a fallible allocation to try.
let data = vec![0u8; size as usize]; let data = vec![0u8; size as usize];
handle_potential_webgl_error!(self, bound_buffer.buffer_data(target, &data, usage)); handle_potential_webgl_error!(self, bound_buffer.buffer_data(target, data, usage));
Ok(()) Ok(())
} }