Skip to content

Commit

Permalink
Use TypedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
yysung1123 authored and jdm committed Feb 10, 2017
1 parent aea6797 commit 6c4ce72
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 92 deletions.
55 changes: 1 addition & 54 deletions components/script/dom/bindings/conversions.rs
Expand Up @@ -47,7 +47,7 @@ use js::glue::{GetProxyPrivate, IsWrapper};
use js::glue::{RUST_JSID_IS_INT, RUST_JSID_TO_INT};
use js::glue::{RUST_JSID_IS_STRING, RUST_JSID_TO_STRING, UnwrapObject};
use js::jsapi::{HandleId, HandleObject, HandleValue, JSContext};
use js::jsapi::{JSObject, JSString, JS_GetArrayBufferViewType};
use js::jsapi::{JSObject, JSString};
use js::jsapi::{JS_GetLatin1StringCharsAndLength, JS_GetObjectAsArrayBuffer, JS_GetObjectAsArrayBufferView};
use js::jsapi::{JS_GetReservedSlot, JS_GetTwoByteStringCharsAndLength};
use js::jsapi::{JS_IsArrayObject, JS_NewStringCopyN, JS_StringHasLatin1Chars};
Expand Down Expand Up @@ -572,59 +572,6 @@ pub unsafe fn array_buffer_view_data<'a, T>(abv: *mut JSObject) -> Option<&'a mu
Some(slice::from_raw_parts_mut(ptr as *mut T, byte_length as usize / mem::size_of::<T>()))
}

/// Returns a copy of the ArrayBufferView data, viewed as T, without checking
/// the real type of it.
pub unsafe fn array_buffer_view_to_vec<T>(abv: *mut JSObject) -> Option<Vec<T>>
where T: ArrayBufferViewContents
{
array_buffer_view_data(abv).map(|data| data.to_vec())
}

/// Returns a mutable slice of the Array Buffer View data, viewed as T, checking
/// that the real type of it is ty.
pub unsafe fn array_buffer_view_data_checked<'a, T>(abv: *mut JSObject) -> Option<&'a mut [T]>
where T: ArrayBufferViewContents
{
array_buffer_view_data::<T>(abv).and_then(|data| {
if T::is_type_compatible(JS_GetArrayBufferViewType(abv)) {
Some(data)
} else {
None
}
})
}

/// Returns a copy of the ArrayBufferView data, viewed as T, checking that the
/// real type of it is ty.
pub unsafe fn array_buffer_view_to_vec_checked<T>(abv: *mut JSObject) -> Option<Vec<T>>
where T: ArrayBufferViewContents
{
array_buffer_view_data_checked(abv).map(|data| data.to_vec())
}

/// Similar API as the array_buffer_view_xxx functions, but for ArrayBuffer
/// objects.
pub unsafe fn array_buffer_data<'a, T>(ab: *mut JSObject) -> Option<&'a mut [T]>
where T: ArrayBufferViewContents
{
assert!(!ab.is_null());

let mut byte_length = 0;
let mut ptr = ptr::null_mut();
let ret = JS_GetObjectAsArrayBuffer(ab, &mut byte_length, &mut ptr);
if ret.is_null() {
return None;
}
Some(slice::from_raw_parts_mut(ptr as *mut T, byte_length as usize / mem::size_of::<T>()))
}

/// Similar API to array_buffer_view_to_vec, but for ArrayBuffer objects.
pub unsafe fn array_buffer_to_vec<T>(ab: *mut JSObject) -> Option<Vec<T>>
where T: ArrayBufferViewContents
{
array_buffer_data(ab).map(|data| data.to_vec())
}

/// Returns whether `value` is an array-like object.
/// Note: Currently only Arrays are supported.
/// TODO: Expand this to support sequences and other array-like objects
Expand Down
74 changes: 36 additions & 38 deletions components/script/dom/webglrenderingcontext.rs
Expand Up @@ -10,8 +10,6 @@ use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderi
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextMethods;
use dom::bindings::codegen::UnionTypes::ImageDataOrHTMLImageElementOrHTMLCanvasElementOrHTMLVideoElement;
use dom::bindings::conversions::{ArrayBufferViewContents, ConversionResult, FromJSValConvertible, ToJSValConvertible};
use dom::bindings::conversions::{array_buffer_to_vec, array_buffer_view_data, array_buffer_view_data_checked};
use dom::bindings::conversions::{array_buffer_view_to_vec, array_buffer_view_to_vec_checked};
use dom::bindings::error::{Error, Fallible};
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, LayoutJS, MutNullableJS, Root};
Expand Down Expand Up @@ -499,7 +497,8 @@ impl WebGLRenderingContext {
format: TexFormat,
data_type: TexDataType,
unpacking_alignment: u32,
data: *mut JSObject)
data: *mut JSObject,
cx: *mut JSContext)
-> Result<u32, ()> {
let element_size = data_type.element_size();
let components_per_element = data_type.components_per_element();
Expand All @@ -511,12 +510,14 @@ impl WebGLRenderingContext {
// if it is UNSIGNED_SHORT_5_6_5, UNSIGNED_SHORT_4_4_4_4,
// or UNSIGNED_SHORT_5_5_5_1, a Uint16Array must be supplied.
// If the types do not match, an INVALID_OPERATION error is generated.
typedarray!(in(cx) let typedarray_u8: Uint8Array = data);
typedarray!(in(cx) let typedarray_u16: Uint16Array = data);
let received_size = if data.is_null() {
element_size
} else {
if array_buffer_view_data_checked::<u16>(data).is_some() {
if typedarray_u16.is_ok() {
2
} else if array_buffer_view_data_checked::<u8>(data).is_some() {
} else if typedarray_u8.is_ok() {
1
} else {
self.webgl_error(InvalidOperation);
Expand Down Expand Up @@ -779,10 +780,6 @@ unsafe fn typed_array_or_sequence_to_vec<T>(cx: *mut JSContext,
<T as FromJSValConvertible>::Config: Clone,
{
assert!(!sequence_or_abv.is_null());
if let Some(v) = array_buffer_view_to_vec_checked::<T>(sequence_or_abv) {
return Ok(v);
}

rooted!(in(cx) let mut val = UndefinedValue());
sequence_or_abv.to_jsval(cx, val.handle_mut());

Expand All @@ -796,13 +793,13 @@ unsafe fn typed_array_or_sequence_to_vec<T>(cx: *mut JSContext,
}

#[allow(unsafe_code)]
unsafe fn fallible_array_buffer_view_to_vec<T>(abv: *mut JSObject) -> Result<Vec<T>, Error>
where T: ArrayBufferViewContents
unsafe fn fallible_array_buffer_view_to_vec(cx: *mut JSContext, abv: *mut JSObject) -> Result<Vec<u8>, Error>
{
assert!(!abv.is_null());
match array_buffer_view_to_vec::<T>(abv) {
Some(v) => Ok(v),
None => Err(Error::Type("Not an ArrayBufferView".to_owned())),
typedarray!(in(cx) let array_buffer_view: ArrayBufferView = abv);
match array_buffer_view {
Ok(mut v) => Ok(v.as_slice().to_vec()),
Err(_) => Err(Error::Type("Not an ArrayBufferView".to_owned())),
}
}

Expand Down Expand Up @@ -1181,15 +1178,15 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {

#[allow(unsafe_code)]
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.5
unsafe fn BufferData(&self, _cx: *mut JSContext, target: u32, data: *mut JSObject, usage: u32) -> Fallible<()> {
unsafe fn BufferData(&self, cx: *mut JSContext, target: u32, data: *mut JSObject, usage: u32) -> Fallible<()> {
if data.is_null() {
return Ok(self.webgl_error(InvalidValue));
}

let data_vec = match array_buffer_to_vec::<u8>(data) {
Some(data) => data,
// Not an ArrayBuffer object, maybe an ArrayBufferView?
None => try!(fallible_array_buffer_view_to_vec::<u8>(data)),
typedarray!(in(cx) let array_buffer: ArrayBuffer = data);
let data_vec = match array_buffer {
Ok(mut data) => data.as_slice().to_vec(),
Err(_) => try!(fallible_array_buffer_view_to_vec(cx, data)),
};

let bound_buffer = match target {
Expand Down Expand Up @@ -1249,15 +1246,15 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {

#[allow(unsafe_code)]
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.5
unsafe fn BufferSubData(&self, _cx: *mut JSContext, target: u32, offset: i64, data: *mut JSObject) -> Fallible<()> {
unsafe fn BufferSubData(&self, cx: *mut JSContext, target: u32, offset: i64, data: *mut JSObject) -> Fallible<()> {
if data.is_null() {
return Ok(self.webgl_error(InvalidValue));
}

let data_vec = match array_buffer_to_vec::<u8>(data) {
Some(data) => data,
// Not an ArrayBuffer object, maybe an ArrayBufferView?
None => try!(fallible_array_buffer_view_to_vec::<u8>(data)),
typedarray!(in(cx) let array_buffer: ArrayBuffer = data);
let data_vec = match array_buffer {
Ok(mut data) => data.as_slice().to_vec(),
Err(_) => try!(fallible_array_buffer_view_to_vec(cx, data)),
};

let bound_buffer = match target {
Expand Down Expand Up @@ -1287,9 +1284,9 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {

#[allow(unsafe_code)]
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
unsafe fn CompressedTexImage2D(&self, _cx: *mut JSContext, _target: u32, _level: i32, _internal_format: u32,
unsafe fn CompressedTexImage2D(&self, cx: *mut JSContext, _target: u32, _level: i32, _internal_format: u32,
_width: i32, _height: i32, _border: i32, pixels: *mut JSObject) -> Fallible<()> {
let _data = try!(fallible_array_buffer_view_to_vec::<u8>(pixels) );
let _data = try!(fallible_array_buffer_view_to_vec(cx, pixels) );
// FIXME: No compressed texture format is currently supported, so error out as per
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#COMPRESSED_TEXTURE_SUPPORT
self.webgl_error(InvalidEnum);
Expand All @@ -1298,10 +1295,10 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {

#[allow(unsafe_code)]
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
unsafe fn CompressedTexSubImage2D(&self, _cx: *mut JSContext, _target: u32, _level: i32,
unsafe fn CompressedTexSubImage2D(&self, cx: *mut JSContext, _target: u32, _level: i32,
_xoffset: i32, _yoffset: i32, _width: i32, _height: i32,
_format: u32, pixels: *mut JSObject) -> Fallible<()> {
let _data = try!(fallible_array_buffer_view_to_vec::<u8>(pixels));
let _data = try!(fallible_array_buffer_view_to_vec(cx, pixels));
// FIXME: No compressed texture format is currently supported, so error out as per
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#COMPRESSED_TEXTURE_SUPPORT
self.webgl_error(InvalidEnum);
Expand Down Expand Up @@ -2066,15 +2063,16 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {

#[allow(unsafe_code)]
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.12
unsafe fn ReadPixels(&self, _cx: *mut JSContext, x: i32, y: i32, width: i32, height: i32,
unsafe fn ReadPixels(&self, cx: *mut JSContext, x: i32, y: i32, width: i32, height: i32,
format: u32, pixel_type: u32, pixels: *mut JSObject) -> Fallible<()> {
if pixels.is_null() {
return Ok(self.webgl_error(InvalidValue));
}

let mut data = match { array_buffer_view_data::<u8>(pixels) } {
Some(data) => data,
None => return Err(Error::Type("Not an ArrayBufferView".to_owned())),
typedarray!(in(cx) let mut pixels_data: ArrayBufferView = pixels);
let mut data = match { pixels_data.as_mut() } {
Ok(data) => data.as_mut_slice(),
Err(_) => return Err(Error::Type("Not an ArrayBufferView".to_owned())),
};

if !self.validate_framebuffer_complete() {
Expand Down Expand Up @@ -2749,7 +2747,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
#[allow(unsafe_code)]
unsafe fn TexImage2D(&self,
_cx: *mut JSContext,
cx: *mut JSContext,
target: u32,
level: i32,
internal_format: u32,
Expand All @@ -2762,7 +2760,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
let data = if data_ptr.is_null() {
None
} else {
Some(try!(fallible_array_buffer_view_to_vec::<u8>(data_ptr)))
Some(try!(fallible_array_buffer_view_to_vec(cx, data_ptr)))
};

let validator = TexImage2DValidator::new(self, target, level,
Expand All @@ -2788,7 +2786,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
let expected_byte_length =
match { self.validate_tex_image_2d_data(width, height,
format, data_type,
unpacking_alignment, data_ptr) } {
unpacking_alignment, data_ptr, cx) } {
Ok(byte_length) => byte_length,
Err(()) => return Ok(()),
};
Expand Down Expand Up @@ -2859,7 +2857,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
#[allow(unsafe_code)]
unsafe fn TexSubImage2D(&self,
_cx: *mut JSContext,
cx: *mut JSContext,
target: u32,
level: i32,
xoffset: i32,
Expand All @@ -2872,7 +2870,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
let data = if data_ptr.is_null() {
None
} else {
Some(try!(fallible_array_buffer_view_to_vec::<u8>(data_ptr)))
Some(try!(fallible_array_buffer_view_to_vec(cx, data_ptr)))
};


Expand All @@ -2898,7 +2896,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
let expected_byte_length =
match { self.validate_tex_image_2d_data(width, height,
format, data_type,
unpacking_alignment, data_ptr) } {
unpacking_alignment, data_ptr, cx) } {
Ok(byte_length) => byte_length,
Err(()) => return Ok(()),
};
Expand Down

0 comments on commit 6c4ce72

Please sign in to comment.