diff options
-rw-r--r-- | components/script/dom/htmlcanvaselement.rs | 61 |
1 files changed, 30 insertions, 31 deletions
diff --git a/components/script/dom/htmlcanvaselement.rs b/components/script/dom/htmlcanvaselement.rs index d71c86a3879..2fffe51134f 100644 --- a/components/script/dom/htmlcanvaselement.rs +++ b/components/script/dom/htmlcanvaselement.rs @@ -4,13 +4,14 @@ use canvas_traits::{CanvasMsg, FromLayoutMsg}; use dom::attr::Attr; +use dom::bindings::cell::DOMRefCell; use dom::bindings::codegen::Bindings::HTMLCanvasElementBinding; use dom::bindings::codegen::Bindings::HTMLCanvasElementBinding::HTMLCanvasElementMethods; use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLContextAttributes; use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLElementCast}; use dom::bindings::codegen::UnionTypes::CanvasRenderingContext2DOrWebGLRenderingContext; use dom::bindings::global::GlobalRef; -use dom::bindings::js::{HeapGCValue, JS, LayoutJS, MutNullableHeap, Root}; +use dom::bindings::js::{HeapGCValue, JS, LayoutJS, Root}; use dom::bindings::utils::{Reflectable}; use dom::canvasrenderingcontext2d::{CanvasRenderingContext2D, LayoutCanvasRenderingContext2DHelpers}; use dom::document::Document; @@ -24,7 +25,6 @@ use ipc_channel::ipc::{self, IpcSender}; use js::jsapi::{HandleValue, JSContext}; use offscreen_gl_context::GLContextAttributes; use std::cell::Cell; -use std::default::Default; use std::iter::repeat; use util::str::{DOMString, parse_unsigned_integer}; @@ -43,7 +43,7 @@ impl HeapGCValue for CanvasContext {} #[dom_struct] pub struct HTMLCanvasElement { htmlelement: HTMLElement, - context: MutNullableHeap<CanvasContext>, + context: DOMRefCell<Option<CanvasContext>>, width: Cell<u32>, height: Cell<u32>, } @@ -59,9 +59,8 @@ impl HTMLCanvasElement { prefix: Option<DOMString>, document: &Document) -> HTMLCanvasElement { HTMLCanvasElement { - htmlelement: - HTMLElement::new_inherited(localName, prefix, document), - context: Default::default(), + htmlelement: HTMLElement::new_inherited(localName, prefix, document), + context: DOMRefCell::new(None), width: Cell::new(DEFAULT_WIDTH), height: Cell::new(DEFAULT_HEIGHT), } @@ -77,10 +76,10 @@ impl HTMLCanvasElement { fn recreate_contexts(&self) { let size = self.get_size(); - if let Some(context) = self.context.get() { - match context { - CanvasContext::Context2d(context) => context.root().r().recreate(size), - CanvasContext::WebGL(context) => context.root().r().recreate(size), + if let Some(ref context) = *self.context.borrow() { + match *context { + CanvasContext::Context2d(ref context) => context.root().r().recreate(size), + CanvasContext::WebGL(ref context) => context.root().r().recreate(size), } } } @@ -105,10 +104,10 @@ impl LayoutHTMLCanvasElementHelpers for LayoutJS<HTMLCanvasElement> { #[allow(unsafe_code)] unsafe fn get_renderer_id(&self) -> Option<usize> { let ref canvas = *self.unsafe_get(); - canvas.context.get().map(|context| { - match context { - CanvasContext::Context2d(context) => context.to_layout().get_renderer_id(), - CanvasContext::WebGL(context) => context.to_layout().get_renderer_id(), + canvas.context.borrow_for_layout().as_ref().map(|context| { + match *context { + CanvasContext::Context2d(ref context) => context.to_layout().get_renderer_id(), + CanvasContext::WebGL(ref context) => context.to_layout().get_renderer_id(), } }) } @@ -116,10 +115,10 @@ impl LayoutHTMLCanvasElementHelpers for LayoutJS<HTMLCanvasElement> { #[allow(unsafe_code)] unsafe fn get_ipc_renderer(&self) -> Option<IpcSender<CanvasMsg>> { let ref canvas = *self.unsafe_get(); - canvas.context.get().map(|context| { - match context { - CanvasContext::Context2d(context) => context.to_layout().get_ipc_renderer(), - CanvasContext::WebGL(context) => context.to_layout().get_ipc_renderer(), + canvas.context.borrow_for_layout().as_ref().map(|context| { + match *context { + CanvasContext::Context2d(ref context) => context.to_layout().get_ipc_renderer(), + CanvasContext::WebGL(ref context) => context.to_layout().get_ipc_renderer(), } }) } @@ -138,24 +137,24 @@ impl LayoutHTMLCanvasElementHelpers for LayoutJS<HTMLCanvasElement> { impl HTMLCanvasElement { pub fn ipc_renderer(&self) -> Option<IpcSender<CanvasMsg>> { - self.context.get().map(|context| { - match context { - CanvasContext::Context2d(context) => context.root().r().ipc_renderer(), - CanvasContext::WebGL(context) => context.root().r().ipc_renderer(), + self.context.borrow().as_ref().map(|context| { + match *context { + CanvasContext::Context2d(ref context) => context.root().r().ipc_renderer(), + CanvasContext::WebGL(ref context) => context.root().r().ipc_renderer(), } }) } pub fn get_or_init_2d_context(&self) -> Option<Root<CanvasRenderingContext2D>> { - if self.context.get().is_none() { + if self.context.borrow().is_none() { let window = window_from_node(self); let size = self.get_size(); let context = CanvasRenderingContext2D::new(GlobalRef::Window(window.r()), self, size); - self.context.set(Some(CanvasContext::Context2d(JS::from_rooted(&context)))); + *self.context.borrow_mut() = Some(CanvasContext::Context2d(JS::from_rooted(&context))); } - match self.context.get().unwrap() { - CanvasContext::Context2d(context) => Some(context.root()), + match *self.context.borrow().as_ref().unwrap() { + CanvasContext::Context2d(ref context) => Some(context.root()), _ => None, } } @@ -163,7 +162,7 @@ impl HTMLCanvasElement { pub fn get_or_init_webgl_context(&self, cx: *mut JSContext, attrs: Option<HandleValue>) -> Option<Root<WebGLRenderingContext>> { - if self.context.get().is_none() { + if self.context.borrow().is_none() { let window = window_from_node(self); let size = self.get_size(); @@ -180,12 +179,12 @@ impl HTMLCanvasElement { let maybe_ctx = WebGLRenderingContext::new(GlobalRef::Window(window.r()), self, size, attrs); - self.context.set(maybe_ctx.map( |ctx| CanvasContext::WebGL(JS::from_rooted(&ctx)))); + *self.context.borrow_mut() = maybe_ctx.map( |ctx| CanvasContext::WebGL(JS::from_rooted(&ctx))); } - if let Some(context) = self.context.get() { - match context { - CanvasContext::WebGL(context) => Some(context.root()), + if let Some(ref context) = *self.context.borrow() { + match *context { + CanvasContext::WebGL(ref context) => Some(context.root()), _ => None, } } else { |