mirror of
https://github.com/servo/servo.git
synced 2025-09-30 08:39:16 +01:00
canvas: Use non rooted variant of HTMLCanvasElementOrOffscreenCanvas type (#38970)
Any RenderingContext/OffscreenRenderingContext type has readonly "canvas" attribute and associated native-code DOM context objects have reference to target DOM canvas objects. https://html.spec.whatwg.org/multipage/canvas.html#renderingcontext https://html.spec.whatwg.org/multipage/canvas.html#offscreenrenderingcontext And currently the reference to DOM canvas object is the rooting pointer on the stack, which leads to the circular reference problem. The SpiderMonkey's (SM) garbage collector will not be able to free the DOM canvas and context objects (unreacheble from JS) because of the rooting pointer on stack (see STACK_ROOTS). And these objects will be stored until the associated script runtime/thread will be terminated. SM -> JS Roots -> DOM Canvas* (on heap) -> DOM Context (on heap) SM -> Rust Roots -> Dom Canvas* (on stack) <- as "canvas" member field Let's replace the rooting pointer to the traceble pointer (DomRoot -> Dom) in the "canvas" member field of DOM context object, which allows to broke circular referencing problem. Testing: No changes in existed tests Signed-off-by: Andrei Volykhin <volykhin.andrei@huawei.com> Co-authored-by: Andrei Volykhin <volykhin.andrei@huawei.com>
This commit is contained in:
parent
2c7866eb24
commit
8a62984c2f
11 changed files with 191 additions and 116 deletions
|
@ -24,7 +24,7 @@ use servo_media::streams::MediaStreamType;
|
|||
use servo_media::streams::registry::MediaStreamId;
|
||||
use style::attr::AttrValue;
|
||||
|
||||
pub(crate) use crate::canvas_context::*;
|
||||
use crate::canvas_context::{CanvasContext, LayoutCanvasRenderingContextHelpers, RenderingContext};
|
||||
use crate::conversions::Convert;
|
||||
use crate::dom::attr::Attr;
|
||||
use crate::dom::bindings::callback::ExceptionHandling;
|
||||
|
@ -34,7 +34,7 @@ use crate::dom::bindings::codegen::Bindings::HTMLCanvasElementBinding::{
|
|||
};
|
||||
use crate::dom::bindings::codegen::Bindings::MediaStreamBinding::MediaStreamMethods;
|
||||
use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLContextAttributes;
|
||||
use crate::dom::bindings::codegen::UnionTypes::HTMLCanvasElementOrOffscreenCanvas;
|
||||
use crate::dom::bindings::codegen::UnionTypes::HTMLCanvasElementOrOffscreenCanvas as RootedHTMLCanvasElementOrOffscreenCanvas;
|
||||
use crate::dom::bindings::conversions::ConversionResult;
|
||||
use crate::dom::bindings::error::{Error, Fallible};
|
||||
use crate::dom::bindings::inheritance::Castable;
|
||||
|
@ -156,6 +156,10 @@ impl HTMLCanvasElement {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) trait LayoutHTMLCanvasElementHelpers {
|
||||
fn data(self) -> HTMLCanvasData;
|
||||
}
|
||||
|
||||
impl LayoutHTMLCanvasElementHelpers for LayoutDom<'_, HTMLCanvasElement> {
|
||||
#[allow(unsafe_code)]
|
||||
fn data(self) -> HTMLCanvasData {
|
||||
|
@ -227,11 +231,10 @@ impl HTMLCanvasElement {
|
|||
// Step 1. Let context be the result of running the
|
||||
// ImageBitmapRenderingContext creation algorithm given this and
|
||||
// options.
|
||||
let context = ImageBitmapRenderingContext::new(
|
||||
&self.owner_global(),
|
||||
HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self)),
|
||||
can_gc,
|
||||
);
|
||||
let canvas =
|
||||
RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
|
||||
|
||||
let context = ImageBitmapRenderingContext::new(&self.owner_global(), &canvas, can_gc);
|
||||
|
||||
// Step 2. Set this's context mode to bitmaprenderer.
|
||||
*self.context_mode.borrow_mut() =
|
||||
|
@ -254,9 +257,10 @@ impl HTMLCanvasElement {
|
|||
};
|
||||
}
|
||||
let window = self.owner_window();
|
||||
let canvas =
|
||||
RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
|
||||
let size = self.get_size();
|
||||
let attrs = Self::get_gl_attributes(cx, options)?;
|
||||
let canvas = HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
|
||||
let context = WebGLRenderingContext::new(
|
||||
&window,
|
||||
&canvas,
|
||||
|
@ -286,9 +290,10 @@ impl HTMLCanvasElement {
|
|||
};
|
||||
}
|
||||
let window = self.owner_window();
|
||||
let canvas =
|
||||
RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
|
||||
let size = self.get_size();
|
||||
let attrs = Self::get_gl_attributes(cx, options)?;
|
||||
let canvas = HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
|
||||
let context = WebGL2RenderingContext::new(&window, &canvas, size, attrs, can_gc)?;
|
||||
*self.context_mode.borrow_mut() = Some(RenderingContext::WebGL2(Dom::from_ref(&*context)));
|
||||
Some(context)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue