Skip to content

Commit

Permalink
Use FnvHashMap in WebGL implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro committed Aug 24, 2017
1 parent 6eb46b1 commit 0a64455
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/canvas/Cargo.toml
Expand Up @@ -15,6 +15,7 @@ canvas_traits = {path = "../canvas_traits"}
compositing = {path = "../compositing"}
cssparser = "0.19"
euclid = "0.15"
fnv = "1.0"
gleam = "0.4"
ipc-channel = "0.8"
log = "0.3.5"
Expand Down
1 change: 1 addition & 0 deletions components/canvas/lib.rs
Expand Up @@ -9,6 +9,7 @@ extern crate canvas_traits;
extern crate compositing;
extern crate cssparser;
extern crate euclid;
extern crate fnv;
extern crate gleam;
extern crate ipc_channel;
#[macro_use] extern crate log;
Expand Down
14 changes: 7 additions & 7 deletions components/canvas/webgl_thread.rs
Expand Up @@ -5,9 +5,9 @@
use canvas_traits::canvas::byte_swap;
use canvas_traits::webgl::*;
use euclid::Size2D;
use fnv::FnvHashMap;
use gleam::gl;
use offscreen_gl_context::{GLContext, GLContextAttributes, GLLimits, NativeGLContextMethods};
use std::collections::HashMap;
use std::mem;
use std::thread;
use super::gl_context::{GLContextFactory, GLContextWrapper};
Expand All @@ -26,9 +26,9 @@ pub struct WebGLThread<VR: WebVRRenderHandler + 'static, OB: WebGLThreadObserver
/// Channel used to generate/update or delete `webrender_api::ImageKey`s.
webrender_api: webrender_api::RenderApi,
/// Map of live WebGLContexts.
contexts: HashMap<WebGLContextId, GLContextWrapper>,
contexts: FnvHashMap<WebGLContextId, GLContextWrapper>,
/// Cached information for WebGLContexts.
cached_context_info: HashMap<WebGLContextId, WebGLContextInfo>,
cached_context_info: FnvHashMap<WebGLContextId, WebGLContextInfo>,
/// Current bound context.
bound_context_id: Option<WebGLContextId>,
/// Id generator for new WebGLContexts.
Expand All @@ -47,8 +47,8 @@ impl<VR: WebVRRenderHandler + 'static, OB: WebGLThreadObserver> WebGLThread<VR,
WebGLThread {
gl_factory,
webrender_api: webrender_api_sender.create_api(),
contexts: HashMap::new(),
cached_context_info: HashMap::new(),
contexts: Default::default(),
cached_context_info: Default::default(),
bound_context_id: None,
next_webgl_id: 0,
webvr_compositor,
Expand Down Expand Up @@ -335,7 +335,7 @@ impl<VR: WebVRRenderHandler + 'static, OB: WebGLThreadObserver> WebGLThread<VR,

/// Gets a reference to a GLContextWrapper for a given WebGLContextId and makes it current if required.
fn make_current_if_needed<'a>(context_id: WebGLContextId,
contexts: &'a HashMap<WebGLContextId, GLContextWrapper>,
contexts: &'a FnvHashMap<WebGLContextId, GLContextWrapper>,
bound_id: &mut Option<WebGLContextId>) -> Option<&'a GLContextWrapper> {
contexts.get(&context_id).and_then(|ctx| {
if Some(context_id) != *bound_id {
Expand All @@ -349,7 +349,7 @@ impl<VR: WebVRRenderHandler + 'static, OB: WebGLThreadObserver> WebGLThread<VR,

/// Gets a mutable reference to a GLContextWrapper for a WebGLContextId and makes it current if required.
fn make_current_if_needed_mut<'a>(context_id: WebGLContextId,
contexts: &'a mut HashMap<WebGLContextId, GLContextWrapper>,
contexts: &'a mut FnvHashMap<WebGLContextId, GLContextWrapper>,
bound_id: &mut Option<WebGLContextId>) -> &'a mut GLContextWrapper {
let ctx = contexts.get_mut(&context_id).expect("WebGLContext not found!");
if Some(context_id) != *bound_id {
Expand Down
29 changes: 15 additions & 14 deletions components/script/dom/webgl_extensions/extensions.rs
Expand Up @@ -12,13 +12,14 @@ use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderi
use dom::bindings::js::Root;
use dom::bindings::trace::JSTraceable;
use dom::webglrenderingcontext::WebGLRenderingContext;
use fnv::{FnvHashMap, FnvHashSet};
use gleam::gl::GLenum;
use heapsize::HeapSizeOf;
use js::jsapi::{JSContext, JSObject};
use js::jsval::JSVal;
use ref_filter_map::ref_filter_map;
use std::cell::Ref;
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use super::{ext, WebGLExtension};
use super::wrapper::{WebGLExtensionWrapper, TypedWebGLExtensionWrapper};

Expand Down Expand Up @@ -46,26 +47,26 @@ const DEFAULT_DISABLED_GET_PARAMETER_NAMES: [GLenum; 1] = [
/// WebGL features that are enabled/disabled by WebGL Extensions.
#[derive(HeapSizeOf, JSTraceable)]
struct WebGLExtensionFeatures {
gl_extensions: HashSet<String>,
disabled_tex_types: HashSet<GLenum>,
not_filterable_tex_types: HashSet<GLenum>,
effective_tex_internal_formats: HashMap<TexFormatType, u32>,
query_parameter_handlers: HashMap<GLenum, WebGLQueryParameterHandler>,
gl_extensions: FnvHashSet<String>,
disabled_tex_types: FnvHashSet<GLenum>,
not_filterable_tex_types: FnvHashSet<GLenum>,
effective_tex_internal_formats: FnvHashMap<TexFormatType, u32>,
query_parameter_handlers: FnvHashMap<GLenum, WebGLQueryParameterHandler>,
/// WebGL Hint() targets enabled by extensions.
hint_targets: HashSet<GLenum>,
hint_targets: FnvHashSet<GLenum>,
/// WebGL GetParameter() names enabled by extensions.
disabled_get_parameter_names: HashSet<GLenum>,
disabled_get_parameter_names: FnvHashSet<GLenum>,
}

impl Default for WebGLExtensionFeatures {
fn default() -> WebGLExtensionFeatures {
WebGLExtensionFeatures {
gl_extensions: HashSet::new(),
gl_extensions: Default::default(),
disabled_tex_types: DEFAULT_DISABLED_TEX_TYPES.iter().cloned().collect(),
not_filterable_tex_types: DEFAULT_NOT_FILTERABLE_TEX_TYPES.iter().cloned().collect(),
effective_tex_internal_formats: HashMap::new(),
query_parameter_handlers: HashMap::new(),
hint_targets: HashSet::new(),
effective_tex_internal_formats: Default::default(),
query_parameter_handlers: Default::default(),
hint_targets: Default::default(),
disabled_get_parameter_names: DEFAULT_DISABLED_GET_PARAMETER_NAMES.iter().cloned().collect(),
}
}
Expand All @@ -90,8 +91,8 @@ impl WebGLExtensions {
pub fn init_once<F>(&self, cb: F) where F: FnOnce() -> String {
if self.extensions.borrow().len() == 0 {
let gl_str = cb();
self.features.borrow_mut().gl_extensions = HashSet::from_iter(gl_str.split(&[',', ' '][..])
.map(|s| s.into()));
self.features.borrow_mut().gl_extensions = FnvHashSet::from_iter(gl_str.split(&[',', ' '][..])
.map(|s| s.into()));
self.register_all_extensions();
}
}
Expand Down
10 changes: 5 additions & 5 deletions components/script/dom/webglrenderingcontext.rs
Expand Up @@ -44,6 +44,7 @@ use dom::webgluniformlocation::WebGLUniformLocation;
use dom::window::Window;
use dom_struct::dom_struct;
use euclid::Size2D;
use fnv::FnvHashMap;
use half::f16;
use js::conversions::ConversionBehavior;
use js::jsapi::{JSContext, JSObject, Type, Rooted};
Expand All @@ -55,7 +56,6 @@ use offscreen_gl_context::{GLContextAttributes, GLLimits};
use script_layout_interface::HTMLCanvasDataSource;
use servo_config::prefs::PREFS;
use std::cell::Cell;
use std::collections::HashMap;
use webrender_api;

type ImagePixelResult = Result<(Vec<u8>, Size2D<i32>, bool), ()>;
Expand Down Expand Up @@ -151,7 +151,7 @@ pub struct WebGLRenderingContext {
bound_texture_cube_map: MutNullableJS<WebGLTexture>,
bound_buffer_array: MutNullableJS<WebGLBuffer>,
bound_buffer_element_array: MutNullableJS<WebGLBuffer>,
bound_attrib_buffers: DOMRefCell<HashMap<u32, JS<WebGLBuffer>>>,
bound_attrib_buffers: DOMRefCell<FnvHashMap<u32, JS<WebGLBuffer>>>,
current_program: MutNullableJS<WebGLProgram>,
#[ignore_heap_size_of = "Because it's small"]
current_vertex_attrib_0: Cell<(f32, f32, f32, f32)>,
Expand Down Expand Up @@ -194,7 +194,7 @@ impl WebGLRenderingContext {
bound_texture_cube_map: MutNullableJS::new(None),
bound_buffer_array: MutNullableJS::new(None),
bound_buffer_element_array: MutNullableJS::new(None),
bound_attrib_buffers: DOMRefCell::new(HashMap::new()),
bound_attrib_buffers: DOMRefCell::new(Default::default()),
bound_renderbuffer: MutNullableJS::new(None),
current_program: MutNullableJS::new(None),
current_vertex_attrib_0: Cell::new((0f32, 0f32, 0f32, 1f32)),
Expand Down Expand Up @@ -239,12 +239,12 @@ impl WebGLRenderingContext {
}
}

pub fn borrow_bound_attrib_buffers(&self) -> Ref<HashMap<u32, JS<WebGLBuffer>>> {
pub fn borrow_bound_attrib_buffers(&self) -> Ref<FnvHashMap<u32, JS<WebGLBuffer>>> {
self.bound_attrib_buffers.borrow()
}

pub fn set_bound_attrib_buffers<'a, T>(&self, iter: T) where T: Iterator<Item=(u32, &'a WebGLBuffer)> {
*self.bound_attrib_buffers.borrow_mut() = HashMap::from_iter(iter.map(|(k,v)| (k, JS::from_ref(v))));
*self.bound_attrib_buffers.borrow_mut() = FnvHashMap::from_iter(iter.map(|(k,v)| (k, JS::from_ref(v))));
}

pub fn bound_buffer_element_array(&self) -> Option<Root<WebGLBuffer>> {
Expand Down

0 comments on commit 0a64455

Please sign in to comment.