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>
This commit is contained in:
sagudev 2025-05-01 19:49:59 +02:00 committed by GitHub
parent 1a3f10bba4
commit 3648525fe8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 28 additions and 71 deletions

View file

@ -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()),

View file

@ -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(),

View file

@ -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 {

View file

@ -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
}
}
}

View file

@ -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!()
}
}

View file

@ -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,
}
};

View file

@ -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() }
}

View file

@ -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()
}
}

View file

@ -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()
}
}

View file

@ -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,
}