mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Use FLoat32Array in GamepadPose (#31106)
* Use FLoat32Array in GamepadPose Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Remove unused create_typed_array Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> --------- Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>
This commit is contained in:
parent
9a698b7bfb
commit
9d2c102fa0
4 changed files with 38 additions and 84 deletions
|
@ -2,13 +2,10 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::ptr;
|
||||
use std::ptr::NonNull;
|
||||
|
||||
use dom_struct::dom_struct;
|
||||
use js::jsapi::{Heap, JSObject};
|
||||
use js::typedarray::{CreateWith, Float32Array};
|
||||
use js::typedarray::Float32Array;
|
||||
|
||||
use super::bindings::typedarrays::HeapFloat32Array;
|
||||
use crate::dom::bindings::codegen::Bindings::GamepadPoseBinding::GamepadPoseMethods;
|
||||
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
|
||||
use crate::dom::bindings::root::DomRoot;
|
||||
|
@ -19,55 +16,17 @@ use crate::script_runtime::JSContext;
|
|||
pub struct GamepadPose {
|
||||
reflector_: Reflector,
|
||||
#[ignore_malloc_size_of = "mozjs"]
|
||||
position: Heap<*mut JSObject>,
|
||||
position: HeapFloat32Array,
|
||||
#[ignore_malloc_size_of = "mozjs"]
|
||||
orientation: Heap<*mut JSObject>,
|
||||
orientation: HeapFloat32Array,
|
||||
#[ignore_malloc_size_of = "mozjs"]
|
||||
linear_vel: Heap<*mut JSObject>,
|
||||
linear_vel: HeapFloat32Array,
|
||||
#[ignore_malloc_size_of = "mozjs"]
|
||||
angular_vel: Heap<*mut JSObject>,
|
||||
angular_vel: HeapFloat32Array,
|
||||
#[ignore_malloc_size_of = "mozjs"]
|
||||
linear_acc: Heap<*mut JSObject>,
|
||||
linear_acc: HeapFloat32Array,
|
||||
#[ignore_malloc_size_of = "mozjs"]
|
||||
angular_acc: Heap<*mut JSObject>,
|
||||
}
|
||||
|
||||
// TODO: support gamepad discovery
|
||||
#[allow(dead_code)]
|
||||
#[allow(unsafe_code)]
|
||||
fn update_or_create_typed_array(cx: JSContext, src: Option<&[f32]>, dst: &Heap<*mut JSObject>) {
|
||||
match src {
|
||||
Some(data) => {
|
||||
if dst.get().is_null() {
|
||||
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>());
|
||||
let _ = unsafe {
|
||||
Float32Array::create(*cx, CreateWith::Slice(data), array.handle_mut())
|
||||
};
|
||||
(*dst).set(array.get());
|
||||
} else {
|
||||
typedarray!(in(*cx) let array: Float32Array = dst.get());
|
||||
if let Ok(mut array) = array {
|
||||
unsafe { array.update(data) };
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
if !dst.get().is_null() {
|
||||
dst.set(ptr::null_mut());
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(unsafe_code)]
|
||||
fn heap_to_option(heap: &Heap<*mut JSObject>) -> Option<NonNull<JSObject>> {
|
||||
let js_object = heap.get();
|
||||
if js_object.is_null() {
|
||||
None
|
||||
} else {
|
||||
unsafe { Some(NonNull::new_unchecked(js_object)) }
|
||||
}
|
||||
angular_acc: HeapFloat32Array,
|
||||
}
|
||||
|
||||
// TODO: support gamepad discovery
|
||||
|
@ -76,12 +35,12 @@ impl GamepadPose {
|
|||
fn new_inherited() -> GamepadPose {
|
||||
GamepadPose {
|
||||
reflector_: Reflector::new(),
|
||||
position: Heap::default(),
|
||||
orientation: Heap::default(),
|
||||
linear_vel: Heap::default(),
|
||||
angular_vel: Heap::default(),
|
||||
linear_acc: Heap::default(),
|
||||
angular_acc: Heap::default(),
|
||||
position: HeapFloat32Array::default(),
|
||||
orientation: HeapFloat32Array::default(),
|
||||
linear_vel: HeapFloat32Array::default(),
|
||||
angular_vel: HeapFloat32Array::default(),
|
||||
linear_acc: HeapFloat32Array::default(),
|
||||
angular_acc: HeapFloat32Array::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,42 +51,42 @@ impl GamepadPose {
|
|||
|
||||
impl GamepadPoseMethods for GamepadPose {
|
||||
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-position
|
||||
fn GetPosition(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
|
||||
heap_to_option(&self.position)
|
||||
fn GetPosition(&self, _cx: JSContext) -> Option<Float32Array> {
|
||||
self.position.internal_to_option()
|
||||
}
|
||||
|
||||
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-hasposition
|
||||
fn HasPosition(&self) -> bool {
|
||||
!self.position.get().is_null()
|
||||
self.position.is_initialized()
|
||||
}
|
||||
|
||||
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-linearvelocity
|
||||
fn GetLinearVelocity(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
|
||||
heap_to_option(&self.linear_vel)
|
||||
fn GetLinearVelocity(&self, _cx: JSContext) -> Option<Float32Array> {
|
||||
self.linear_vel.internal_to_option()
|
||||
}
|
||||
|
||||
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-linearacceleration
|
||||
fn GetLinearAcceleration(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
|
||||
heap_to_option(&self.linear_acc)
|
||||
fn GetLinearAcceleration(&self, _cx: JSContext) -> Option<Float32Array> {
|
||||
self.linear_acc.internal_to_option()
|
||||
}
|
||||
|
||||
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-orientation
|
||||
fn GetOrientation(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
|
||||
heap_to_option(&self.orientation)
|
||||
fn GetOrientation(&self, _cx: JSContext) -> Option<Float32Array> {
|
||||
self.orientation.internal_to_option()
|
||||
}
|
||||
|
||||
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-orientation
|
||||
fn HasOrientation(&self) -> bool {
|
||||
!self.orientation.get().is_null()
|
||||
self.orientation.is_initialized()
|
||||
}
|
||||
|
||||
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-angularvelocity
|
||||
fn GetAngularVelocity(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
|
||||
heap_to_option(&self.angular_vel)
|
||||
fn GetAngularVelocity(&self, _cx: JSContext) -> Option<Float32Array> {
|
||||
self.angular_vel.internal_to_option()
|
||||
}
|
||||
|
||||
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-angularacceleration
|
||||
fn GetAngularAcceleration(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
|
||||
heap_to_option(&self.angular_acc)
|
||||
fn GetAngularAcceleration(&self, _cx: JSContext) -> Option<Float32Array> {
|
||||
self.angular_acc.internal_to_option()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue