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
|
@ -29,7 +29,7 @@ use url::Host;
|
|||
use webrender_api::ImageKey;
|
||||
|
||||
use super::validations::types::TexImageTarget;
|
||||
use crate::canvas_context::CanvasContext;
|
||||
use crate::canvas_context::{CanvasContext, LayoutCanvasRenderingContextHelpers};
|
||||
use crate::dom::bindings::codegen::Bindings::WebGL2RenderingContextBinding::{
|
||||
WebGL2RenderingContextConstants as constants, WebGL2RenderingContextMethods,
|
||||
};
|
||||
|
@ -38,15 +38,14 @@ use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::{
|
|||
};
|
||||
use crate::dom::bindings::codegen::UnionTypes::{
|
||||
ArrayBufferViewOrArrayBuffer, Float32ArrayOrUnrestrictedFloatSequence,
|
||||
HTMLCanvasElementOrOffscreenCanvas, Int32ArrayOrLongSequence,
|
||||
Uint32ArrayOrUnsignedLongSequence,
|
||||
HTMLCanvasElementOrOffscreenCanvas as RootedHTMLCanvasElementOrOffscreenCanvas,
|
||||
Int32ArrayOrLongSequence, Uint32ArrayOrUnsignedLongSequence,
|
||||
};
|
||||
use crate::dom::bindings::error::{ErrorResult, Fallible};
|
||||
use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
|
||||
use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom, MutNullableDom, ToLayout};
|
||||
use crate::dom::bindings::str::DOMString;
|
||||
use crate::dom::globalscope::GlobalScope;
|
||||
use crate::dom::html::htmlcanvaselement::LayoutCanvasRenderingContextHelpers;
|
||||
#[cfg(feature = "webxr")]
|
||||
use crate::dom::promise::Promise;
|
||||
use crate::dom::webgl::validations::WebGLValidator;
|
||||
|
@ -133,7 +132,7 @@ struct ReadPixelsSizes {
|
|||
impl WebGL2RenderingContext {
|
||||
fn new_inherited(
|
||||
window: &Window,
|
||||
canvas: &HTMLCanvasElementOrOffscreenCanvas,
|
||||
canvas: &RootedHTMLCanvasElementOrOffscreenCanvas,
|
||||
size: Size2D<u32>,
|
||||
attrs: GLContextAttributes,
|
||||
can_gc: CanGc,
|
||||
|
@ -179,10 +178,9 @@ impl WebGL2RenderingContext {
|
|||
})
|
||||
}
|
||||
|
||||
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
|
||||
pub(crate) fn new(
|
||||
window: &Window,
|
||||
canvas: &HTMLCanvasElementOrOffscreenCanvas,
|
||||
canvas: &RootedHTMLCanvasElementOrOffscreenCanvas,
|
||||
size: Size2D<u32>,
|
||||
attrs: GLContextAttributes,
|
||||
can_gc: CanGc,
|
||||
|
@ -973,8 +971,8 @@ impl CanvasContext for WebGL2RenderingContext {
|
|||
self.base.context_id()
|
||||
}
|
||||
|
||||
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
|
||||
self.base.canvas().clone()
|
||||
fn canvas(&self) -> Option<RootedHTMLCanvasElementOrOffscreenCanvas> {
|
||||
self.base.canvas()
|
||||
}
|
||||
|
||||
fn resize(&self) {
|
||||
|
@ -1000,7 +998,7 @@ impl CanvasContext for WebGL2RenderingContext {
|
|||
|
||||
impl WebGL2RenderingContextMethods<crate::DomTypeHolder> for WebGL2RenderingContext {
|
||||
/// <https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.1>
|
||||
fn Canvas(&self) -> HTMLCanvasElementOrOffscreenCanvas {
|
||||
fn Canvas(&self) -> RootedHTMLCanvasElementOrOffscreenCanvas {
|
||||
self.base.Canvas()
|
||||
}
|
||||
|
||||
|
|
|
@ -35,7 +35,9 @@ use serde::{Deserialize, Serialize};
|
|||
use servo_config::pref;
|
||||
use webrender_api::ImageKey;
|
||||
|
||||
use crate::canvas_context::CanvasContext;
|
||||
use crate::canvas_context::{
|
||||
CanvasContext, HTMLCanvasElementOrOffscreenCanvas, LayoutCanvasRenderingContextHelpers,
|
||||
};
|
||||
use crate::dom::bindings::cell::{DomRefCell, Ref, RefMut};
|
||||
use crate::dom::bindings::codegen::Bindings::ANGLEInstancedArraysBinding::ANGLEInstancedArraysConstants;
|
||||
use crate::dom::bindings::codegen::Bindings::EXTBlendMinmaxBinding::EXTBlendMinmaxConstants;
|
||||
|
@ -47,7 +49,8 @@ use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::{
|
|||
};
|
||||
use crate::dom::bindings::codegen::UnionTypes::{
|
||||
ArrayBufferViewOrArrayBuffer, Float32ArrayOrUnrestrictedFloatSequence,
|
||||
HTMLCanvasElementOrOffscreenCanvas, Int32ArrayOrLongSequence,
|
||||
HTMLCanvasElementOrOffscreenCanvas as RootedHTMLCanvasElementOrOffscreenCanvas,
|
||||
Int32ArrayOrLongSequence,
|
||||
};
|
||||
use crate::dom::bindings::conversions::DerivedFrom;
|
||||
use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
|
||||
|
@ -56,7 +59,6 @@ use crate::dom::bindings::reflector::{DomGlobal, DomObject, Reflector, reflect_d
|
|||
use crate::dom::bindings::root::{DomOnceCell, DomRoot, LayoutDom, MutNullableDom};
|
||||
use crate::dom::bindings::str::DOMString;
|
||||
use crate::dom::event::{Event, EventBubbles, EventCancelable};
|
||||
use crate::dom::html::htmlcanvaselement::LayoutCanvasRenderingContextHelpers;
|
||||
use crate::dom::node::{Node, NodeDamage, NodeTraits};
|
||||
#[cfg(feature = "webxr")]
|
||||
use crate::dom::promise::Promise;
|
||||
|
@ -216,9 +218,10 @@ pub(crate) struct WebGLRenderingContext {
|
|||
}
|
||||
|
||||
impl WebGLRenderingContext {
|
||||
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
|
||||
pub(crate) fn new_inherited(
|
||||
window: &Window,
|
||||
canvas: &HTMLCanvasElementOrOffscreenCanvas,
|
||||
canvas: HTMLCanvasElementOrOffscreenCanvas,
|
||||
webgl_version: WebGLVersion,
|
||||
size: Size2D<u32>,
|
||||
attrs: GLContextAttributes,
|
||||
|
@ -248,7 +251,7 @@ impl WebGLRenderingContext {
|
|||
webgl_version,
|
||||
glsl_version: ctx_data.glsl_version,
|
||||
limits: ctx_data.limits,
|
||||
canvas: canvas.clone(),
|
||||
canvas,
|
||||
last_error: Cell::new(None),
|
||||
texture_packing_alignment: Cell::new(4),
|
||||
texture_unpacking_settings: Cell::new(TextureUnpacking::CONVERT_COLORSPACE),
|
||||
|
@ -285,13 +288,19 @@ impl WebGLRenderingContext {
|
|||
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
|
||||
pub(crate) fn new(
|
||||
window: &Window,
|
||||
canvas: &HTMLCanvasElementOrOffscreenCanvas,
|
||||
canvas: &RootedHTMLCanvasElementOrOffscreenCanvas,
|
||||
webgl_version: WebGLVersion,
|
||||
size: Size2D<u32>,
|
||||
attrs: GLContextAttributes,
|
||||
can_gc: CanGc,
|
||||
) -> Option<DomRoot<WebGLRenderingContext>> {
|
||||
match WebGLRenderingContext::new_inherited(window, canvas, webgl_version, size, attrs) {
|
||||
match WebGLRenderingContext::new_inherited(
|
||||
window,
|
||||
HTMLCanvasElementOrOffscreenCanvas::from(canvas),
|
||||
webgl_version,
|
||||
size,
|
||||
attrs,
|
||||
) {
|
||||
Ok(ctx) => Some(reflect_dom_object(Box::new(ctx), window, can_gc)),
|
||||
Err(msg) => {
|
||||
error!("Couldn't create WebGLRenderingContext: {}", msg);
|
||||
|
@ -304,10 +313,10 @@ impl WebGLRenderingContext {
|
|||
can_gc,
|
||||
);
|
||||
match canvas {
|
||||
HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(canvas) => {
|
||||
RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(canvas) => {
|
||||
event.upcast::<Event>().fire(canvas.upcast(), can_gc);
|
||||
},
|
||||
HTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(canvas) => {
|
||||
RootedHTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(canvas) => {
|
||||
event.upcast::<Event>().fire(canvas.upcast(), can_gc);
|
||||
},
|
||||
}
|
||||
|
@ -1930,8 +1939,8 @@ impl CanvasContext for WebGLRenderingContext {
|
|||
self.webgl_sender.context_id()
|
||||
}
|
||||
|
||||
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
|
||||
Some(self.canvas.clone())
|
||||
fn canvas(&self) -> Option<RootedHTMLCanvasElementOrOffscreenCanvas> {
|
||||
Some(RootedHTMLCanvasElementOrOffscreenCanvas::from(&self.canvas))
|
||||
}
|
||||
|
||||
fn resize(&self) {
|
||||
|
@ -2075,8 +2084,8 @@ impl Drop for WebGLRenderingContext {
|
|||
|
||||
impl WebGLRenderingContextMethods<crate::DomTypeHolder> for WebGLRenderingContext {
|
||||
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.1
|
||||
fn Canvas(&self) -> HTMLCanvasElementOrOffscreenCanvas {
|
||||
self.canvas.clone()
|
||||
fn Canvas(&self) -> RootedHTMLCanvasElementOrOffscreenCanvas {
|
||||
RootedHTMLCanvasElementOrOffscreenCanvas::from(&self.canvas)
|
||||
}
|
||||
|
||||
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.11
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue