diff options
author | sagudev <16504129+sagudev@users.noreply.github.com> | 2025-05-01 19:49:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-01 17:49:59 +0000 |
commit | 3648525fe8f461190869a81c75643890b21e6565 (patch) | |
tree | 0ee9a68fbdd01db875c52370aacd7499709e5950 | |
parent | 1a3f10bba43f0bc051de1baac1ee17ab26db6143 (diff) | |
download | servo-3648525fe8f461190869a81c75643890b21e6565.tar.gz servo-3648525fe8f461190869a81c75643890b21e6565.zip |
Remove `HTMLCanvasDataSource` and `CanvasSource` (#36794)
All canvases return `Option<ImageKey>`.
Testing: Just refactor without behavior changes
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
-rw-r--r-- | components/layout/dom.rs | 12 | ||||
-rw-r--r-- | components/layout/replaced.rs | 35 | ||||
-rw-r--r-- | components/script/canvas_context.rs | 6 | ||||
-rw-r--r-- | components/script/dom/canvasrenderingcontext2d.rs | 8 | ||||
-rw-r--r-- | components/script/dom/gpucanvascontext.rs | 3 | ||||
-rw-r--r-- | components/script/dom/htmlcanvaselement.rs | 4 | ||||
-rw-r--r-- | components/script/dom/webgl2renderingcontext.rs | 4 | ||||
-rw-r--r-- | components/script/dom/webglrenderingcontext.rs | 8 | ||||
-rw-r--r-- | components/script/dom/webgpu/gpucanvascontext.rs | 9 | ||||
-rw-r--r-- | components/shared/script_layout/lib.rs | 10 |
10 files changed, 28 insertions, 71 deletions
diff --git a/components/layout/dom.rs b/components/layout/dom.rs index add4b3ac2d5..8f2697e670a 100644 --- a/components/layout/dom.rs +++ b/components/layout/dom.rs @@ -15,8 +15,7 @@ use script_layout_interface::wrapper_traits::{ LayoutDataTrait, LayoutNode, ThreadSafeLayoutElement, ThreadSafeLayoutNode, }; use script_layout_interface::{ - GenericLayoutDataTrait, HTMLCanvasDataSource, LayoutElementType, - LayoutNodeType as ScriptLayoutNodeType, + GenericLayoutDataTrait, LayoutElementType, LayoutNodeType as ScriptLayoutNodeType, }; use servo_arc::Arc as ServoArc; use style::properties::ComputedValues; @@ -29,7 +28,7 @@ use crate::flow::BlockLevelBox; use crate::flow::inline::InlineItem; use crate::fragment_tree::Fragment; use crate::geom::PhysicalSize; -use crate::replaced::{CanvasInfo, CanvasSource}; +use crate::replaced::CanvasInfo; use crate::table::TableLevelBox; use crate::taffy::TaffyItemBox; @@ -220,12 +219,7 @@ where fn as_canvas(self) -> Option<(CanvasInfo, PhysicalSize<f64>)> { let node = self.to_threadsafe(); let canvas_data = node.canvas_data()?; - let source = match canvas_data.source { - HTMLCanvasDataSource::WebGL(texture_id) => CanvasSource::WebGL(texture_id), - HTMLCanvasDataSource::Image(image_key) => CanvasSource::Image(image_key), - HTMLCanvasDataSource::WebGPU(image_key) => CanvasSource::WebGPU(image_key), - HTMLCanvasDataSource::Empty => CanvasSource::Empty, - }; + let source = canvas_data.source; Some(( CanvasInfo { source }, PhysicalSize::new(canvas_data.width.into(), canvas_data.height.into()), diff --git a/components/layout/replaced.rs b/components/layout/replaced.rs index b82fb947074..bbebc57aa97 100644 --- a/components/layout/replaced.rs +++ b/components/layout/replaced.rs @@ -3,7 +3,6 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::cell::LazyCell; -use std::fmt; use std::sync::Arc; use app_units::Au; @@ -96,33 +95,9 @@ impl NaturalSizes { } } -#[derive(MallocSizeOf)] -pub(crate) enum CanvasSource { - WebGL(ImageKey), - Image(ImageKey), - WebGPU(ImageKey), - /// transparent black - Empty, -} - -impl fmt::Debug for CanvasSource { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{}", - match *self { - CanvasSource::WebGL(_) => "WebGL", - CanvasSource::Image(_) => "Image", - CanvasSource::WebGPU(_) => "WebGPU", - CanvasSource::Empty => "Empty", - } - ) - } -} - #[derive(Debug, MallocSizeOf)] pub(crate) struct CanvasInfo { - pub source: CanvasSource, + pub source: Option<ImageKey>, } #[derive(Debug, MallocSizeOf)] @@ -388,12 +363,10 @@ impl ReplacedContents { return vec![]; } - let image_key = match canvas_info.source { - CanvasSource::WebGL(image_key) => image_key, - CanvasSource::WebGPU(image_key) => image_key, - CanvasSource::Image(image_key) => image_key, - CanvasSource::Empty => return vec![], + let Some(image_key) = canvas_info.source else { + return vec![]; }; + vec![Fragment::Image(ArcRefCell::new(ImageFragment { base: self.base_fragment_info.into(), style: style.clone(), diff --git a/components/script/canvas_context.rs b/components/script/canvas_context.rs index d85877c0f41..0a7545e9594 100644 --- a/components/script/canvas_context.rs +++ b/components/script/canvas_context.rs @@ -6,8 +6,9 @@ use euclid::default::Size2D; use script_bindings::root::Dom; -use script_layout_interface::{HTMLCanvasData, HTMLCanvasDataSource}; +use script_layout_interface::HTMLCanvasData; use snapshot::Snapshot; +use webrender_api::ImageKey; use crate::dom::bindings::codegen::UnionTypes::HTMLCanvasElementOrOffscreenCanvas; use crate::dom::bindings::inheritance::Castable; @@ -19,7 +20,8 @@ use crate::dom::types::{ }; pub(crate) trait LayoutCanvasRenderingContextHelpers { - fn canvas_data_source(self) -> HTMLCanvasDataSource; + /// `None` is rendered as transparent black (cleared canvas) + fn canvas_data_source(self) -> Option<ImageKey>; } pub(crate) trait LayoutHTMLCanvasElementHelpers { diff --git a/components/script/dom/canvasrenderingcontext2d.rs b/components/script/dom/canvasrenderingcontext2d.rs index 38bd38ad511..046d478e49d 100644 --- a/components/script/dom/canvasrenderingcontext2d.rs +++ b/components/script/dom/canvasrenderingcontext2d.rs @@ -7,9 +7,9 @@ use dom_struct::dom_struct; use euclid::default::Size2D; use profile_traits::ipc; use script_bindings::inheritance::Castable; -use script_layout_interface::HTMLCanvasDataSource; use servo_url::ServoUrl; use snapshot::Snapshot; +use webrender_api::ImageKey; use crate::canvas_context::{CanvasContext, CanvasHelpers, LayoutCanvasRenderingContextHelpers}; use crate::canvas_state::CanvasState; @@ -98,13 +98,13 @@ impl CanvasRenderingContext2D { } impl LayoutCanvasRenderingContextHelpers for LayoutDom<'_, CanvasRenderingContext2D> { - fn canvas_data_source(self) -> HTMLCanvasDataSource { + fn canvas_data_source(self) -> Option<ImageKey> { let canvas_state = &self.unsafe_get().canvas_state; if canvas_state.is_paintable() { - HTMLCanvasDataSource::Image(canvas_state.image_key()) + Some(canvas_state.image_key()) } else { - HTMLCanvasDataSource::Empty + None } } } diff --git a/components/script/dom/gpucanvascontext.rs b/components/script/dom/gpucanvascontext.rs index 5304d0f5d3b..2bdabf3e0ab 100644 --- a/components/script/dom/gpucanvascontext.rs +++ b/components/script/dom/gpucanvascontext.rs @@ -3,7 +3,6 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use dom_struct::dom_struct; -use script_layout_interface::HTMLCanvasDataSource; use crate::dom::bindings::codegen::Bindings::GPUCanvasContextBinding::GPUCanvasContextMethods; use crate::dom::bindings::codegen::UnionTypes; @@ -31,7 +30,7 @@ impl GPUCanvasContextMethods<crate::DomTypeHolder> for GPUCanvasContext { } impl LayoutCanvasRenderingContextHelpers for LayoutDom<'_, GPUCanvasContext> { - fn canvas_data_source(self) -> HTMLCanvasDataSource { + fn canvas_data_source(self) -> Option<ImageKey> { unimplemented!() } } diff --git a/components/script/dom/htmlcanvaselement.rs b/components/script/dom/htmlcanvaselement.rs index cc6df183f42..56e008839ba 100644 --- a/components/script/dom/htmlcanvaselement.rs +++ b/components/script/dom/htmlcanvaselement.rs @@ -21,7 +21,7 @@ use image::{ColorType, ImageEncoder}; use ipc_channel::ipc::{self as ipcchan}; use js::error::throw_type_error; use js::rust::{HandleObject, HandleValue}; -use script_layout_interface::{HTMLCanvasData, HTMLCanvasDataSource}; +use script_layout_interface::HTMLCanvasData; use servo_media::streams::MediaStreamType; use servo_media::streams::registry::MediaStreamId; use snapshot::Snapshot; @@ -201,7 +201,7 @@ impl LayoutHTMLCanvasElementHelpers for LayoutDom<'_, HTMLCanvasElement> { Some(RenderingContext::WebGL2(context)) => context.to_layout().canvas_data_source(), #[cfg(feature = "webgpu")] Some(RenderingContext::WebGPU(context)) => context.to_layout().canvas_data_source(), - Some(RenderingContext::Placeholder(_)) | None => HTMLCanvasDataSource::Empty, + Some(RenderingContext::Placeholder(_)) | None => None, } }; diff --git a/components/script/dom/webgl2renderingcontext.rs b/components/script/dom/webgl2renderingcontext.rs index 416454d8719..5e538b53b5f 100644 --- a/components/script/dom/webgl2renderingcontext.rs +++ b/components/script/dom/webgl2renderingcontext.rs @@ -22,10 +22,10 @@ use js::jsval::{BooleanValue, DoubleValue, Int32Value, NullValue, ObjectValue, U use js::rust::{CustomAutoRooterGuard, HandleObject, MutableHandleValue}; use js::typedarray::{ArrayBufferView, CreateWith, Float32, Int32Array, Uint32, Uint32Array}; use script_bindings::interfaces::WebGL2RenderingContextHelpers; -use script_layout_interface::HTMLCanvasDataSource; use servo_config::pref; use snapshot::Snapshot; use url::Host; +use webrender_api::ImageKey; use crate::canvas_context::CanvasContext; use crate::dom::bindings::codegen::Bindings::WebGL2RenderingContextBinding::{ @@ -4702,7 +4702,7 @@ impl WebGL2RenderingContextMethods<crate::DomTypeHolder> for WebGL2RenderingCont impl LayoutCanvasRenderingContextHelpers for LayoutDom<'_, WebGL2RenderingContext> { #[allow(unsafe_code)] - fn canvas_data_source(self) -> HTMLCanvasDataSource { + fn canvas_data_source(self) -> Option<ImageKey> { let this = self.unsafe_get(); unsafe { (*this.base.to_layout().unsafe_get()).layout_handle() } } diff --git a/components/script/dom/webglrenderingcontext.rs b/components/script/dom/webglrenderingcontext.rs index 9996a3cf504..98170f9655b 100644 --- a/components/script/dom/webglrenderingcontext.rs +++ b/components/script/dom/webglrenderingcontext.rs @@ -31,7 +31,6 @@ use js::typedarray::{ }; use net_traits::image_cache::ImageResponse; use pixels::{self, PixelFormat}; -use script_layout_interface::HTMLCanvasDataSource; use serde::{Deserialize, Serialize}; use servo_config::pref; use snapshot::Snapshot; @@ -875,9 +874,8 @@ impl WebGLRenderingContext { receiver.recv().unwrap() } - pub(crate) fn layout_handle(&self) -> HTMLCanvasDataSource { - let image_key = self.webrender_image; - HTMLCanvasDataSource::WebGL(image_key) + pub(crate) fn layout_handle(&self) -> Option<ImageKey> { + Some(self.webrender_image) } // https://www.khronos.org/registry/webgl/extensions/ANGLE_instanced_arrays/ @@ -4829,7 +4827,7 @@ impl WebGLRenderingContextMethods<crate::DomTypeHolder> for WebGLRenderingContex } impl LayoutCanvasRenderingContextHelpers for LayoutDom<'_, WebGLRenderingContext> { - fn canvas_data_source(self) -> HTMLCanvasDataSource { + fn canvas_data_source(self) -> Option<ImageKey> { (*self.unsafe_get()).layout_handle() } } diff --git a/components/script/dom/webgpu/gpucanvascontext.rs b/components/script/dom/webgpu/gpucanvascontext.rs index c81f96f651f..359b1b14003 100644 --- a/components/script/dom/webgpu/gpucanvascontext.rs +++ b/components/script/dom/webgpu/gpucanvascontext.rs @@ -8,7 +8,6 @@ use std::cell::RefCell; use arrayvec::ArrayVec; use dom_struct::dom_struct; use ipc_channel::ipc::{self}; -use script_layout_interface::HTMLCanvasDataSource; use snapshot::Snapshot; use webgpu_traits::{ ContextConfiguration, PRESENTATION_BUFFER_COUNT, WebGPU, WebGPUContextId, WebGPURequest, @@ -227,11 +226,11 @@ impl GPUCanvasContext { // Internal helper methods impl GPUCanvasContext { - fn layout_handle(&self) -> HTMLCanvasDataSource { + fn layout_handle(&self) -> Option<ImageKey> { if self.drawing_buffer.borrow().cleared { - HTMLCanvasDataSource::Empty + None } else { - HTMLCanvasDataSource::WebGPU(self.webrender_image) + Some(self.webrender_image) } } @@ -301,7 +300,7 @@ impl CanvasContext for GPUCanvasContext { } impl LayoutCanvasRenderingContextHelpers for LayoutDom<'_, GPUCanvasContext> { - fn canvas_data_source(self) -> HTMLCanvasDataSource { + fn canvas_data_source(self) -> Option<ImageKey> { (*self.unsafe_get()).layout_handle() } } diff --git a/components/shared/script_layout/lib.rs b/components/shared/script_layout/lib.rs index a40b8c403c1..66baccd5147 100644 --- a/components/shared/script_layout/lib.rs +++ b/components/shared/script_layout/lib.rs @@ -117,16 +117,8 @@ pub enum LayoutElementType { SVGSVGElement, } -pub enum HTMLCanvasDataSource { - WebGL(ImageKey), - Image(ImageKey), - WebGPU(ImageKey), - /// transparent black - Empty, -} - pub struct HTMLCanvasData { - pub source: HTMLCanvasDataSource, + pub source: Option<ImageKey>, pub width: u32, pub height: u32, } |