diff options
author | Fernando Jiménez Moreno <ferjmoreno@gmail.com> | 2020-03-05 19:16:28 +0100 |
---|---|---|
committer | Fernando Jiménez Moreno <ferjmoreno@gmail.com> | 2020-03-06 13:23:07 +0100 |
commit | f0e30f5b0ccdf262d65507702935ae3dd4a698b5 (patch) | |
tree | f55ab1d78bbff880558946fe61113a2ed6d0468f /components | |
parent | 0bd794aed783260652313144ec3e74590db12b7b (diff) | |
download | servo-f0e30f5b0ccdf262d65507702935ae3dd4a698b5.tar.gz servo-f0e30f5b0ccdf262d65507702935ae3dd4a698b5.zip |
Canvas rendering for layout 2020
Update test expectations with layout 2020 canvas support
Diffstat (limited to 'components')
-rw-r--r-- | components/layout_2020/Cargo.toml | 1 | ||||
-rw-r--r-- | components/layout_2020/dom_traversal.rs | 24 | ||||
-rw-r--r-- | components/layout_2020/replaced.rs | 106 |
3 files changed, 110 insertions, 21 deletions
diff --git a/components/layout_2020/Cargo.toml b/components/layout_2020/Cargo.toml index 51c08c19e34..7d2f9e3311e 100644 --- a/components/layout_2020/Cargo.toml +++ b/components/layout_2020/Cargo.toml @@ -15,6 +15,7 @@ doctest = false [dependencies] app_units = "0.7" atomic_refcell = "0.1" +canvas_traits = {path = "../canvas_traits"} cssparser = "0.27" embedder_traits = {path = "../embedder_traits"} euclid = "0.20" diff --git a/components/layout_2020/dom_traversal.rs b/components/layout_2020/dom_traversal.rs index e67f06eb7f9..1a3a969919c 100644 --- a/components/layout_2020/dom_traversal.rs +++ b/components/layout_2020/dom_traversal.rs @@ -5,7 +5,7 @@ use crate::context::LayoutContext; use crate::element_data::{LayoutBox, LayoutDataForElement}; use crate::geom::PhysicalSize; -use crate::replaced::ReplacedContent; +use crate::replaced::{CanvasInfo, CanvasSource, ReplacedContent}; use crate::style_ext::{Display, DisplayGeneratingBox, DisplayInside, DisplayOutside}; use crate::wrapper::GetRawData; use atomic_refcell::{AtomicRefCell, AtomicRefMut}; @@ -14,9 +14,10 @@ use net_traits::image::base::Image as NetImage; use script_layout_interface::wrapper_traits::{ LayoutNode, ThreadSafeLayoutElement, ThreadSafeLayoutNode, }; +use script_layout_interface::HTMLCanvasDataSource; use servo_arc::Arc as ServoArc; use std::marker::PhantomData as marker; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use style::dom::{OpaqueNode, TNode}; use style::properties::ComputedValues; use style::selector_parser::PseudoElement; @@ -354,6 +355,7 @@ pub(crate) trait NodeExt<'dom>: 'dom + Copy + LayoutNode + Send + Sync { /// Returns the image if it’s loaded, and its size in image pixels /// adjusted for `image_density`. fn as_image(self) -> Option<(Option<Arc<NetImage>>, PhysicalSize<f64>)>; + fn as_canvas(self) -> Option<(CanvasInfo, PhysicalSize<f64>)>; fn first_child(self) -> Option<Self>; fn next_sibling(self) -> Option<Self>; fn parent_node(self) -> Option<Self>; @@ -399,6 +401,24 @@ where Some((resource, PhysicalSize::new(width, height))) } + 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(ipc_sender) => { + CanvasSource::Image(ipc_sender.map(|renderer| Arc::new(Mutex::new(renderer)))) + }, + }; + Some(( + CanvasInfo { + source, + canvas_id: canvas_data.canvas_id, + }, + PhysicalSize::new(canvas_data.width.into(), canvas_data.height.into()), + )) + } + fn first_child(self) -> Option<Self> { TNode::first_child(&self) } diff --git a/components/layout_2020/replaced.rs b/components/layout_2020/replaced.rs index b9e53bec772..1d7557b0641 100644 --- a/components/layout_2020/replaced.rs +++ b/components/layout_2020/replaced.rs @@ -10,15 +10,19 @@ use crate::geom::PhysicalSize; use crate::sizing::ContentSizes; use crate::style_ext::ComputedValuesExt; use crate::ContainingBlock; +use canvas_traits::canvas::{CanvasId, CanvasMsg, FromLayoutMsg}; +use ipc_channel::ipc::{self, IpcSender}; use net_traits::image::base::Image; use net_traits::image_cache::{ImageOrMetadataAvailable, UsePlaceholder}; use servo_arc::Arc as ServoArc; -use std::sync::Arc; +use std::fmt; +use std::sync::{Arc, Mutex}; use style::properties::ComputedValues; use style::servo::url::ComputedUrl; use style::values::computed::{Length, LengthOrAuto}; use style::values::CSSFloat; use style::Zero; +use webrender_api::ImageKey; #[derive(Debug, Serialize)] pub(crate) struct ReplacedContent { @@ -44,33 +48,69 @@ pub(crate) struct IntrinsicSizes { pub ratio: Option<CSSFloat>, } +#[derive(Serialize)] +pub(crate) enum CanvasSource { + WebGL(ImageKey), + Image(Option<Arc<Mutex<IpcSender<CanvasMsg>>>>), +} + +impl fmt::Debug for CanvasSource { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "{}", + match *self { + CanvasSource::WebGL(_) => "WebGL", + CanvasSource::Image(_) => "Image", + } + ) + } +} + +#[derive(Debug, Serialize)] +pub(crate) struct CanvasInfo { + pub source: CanvasSource, + pub canvas_id: CanvasId, +} + #[derive(Debug, Serialize)] pub(crate) enum ReplacedContentKind { Image(Option<Arc<Image>>), + Canvas(CanvasInfo), } impl ReplacedContent { pub fn for_element<'dom>(element: impl NodeExt<'dom>) -> Option<Self> { - if let Some((image, intrinsic_size_in_dots)) = element.as_image() { - // FIXME: should 'image-resolution' (when implemented) be used *instead* of - // `script::dom::htmlimageelement::ImageRequest::current_pixel_density`? + let (kind, intrinsic_size_in_dots) = { + if let Some((image, intrinsic_size_in_dots)) = element.as_image() { + (ReplacedContentKind::Image(image), intrinsic_size_in_dots) + } else if let Some((canvas_info, intrinsic_size_in_dots)) = element.as_canvas() { + ( + ReplacedContentKind::Canvas(canvas_info), + intrinsic_size_in_dots, + ) + } else { + return None; + } + }; - // https://drafts.csswg.org/css-images-4/#the-image-resolution - let dppx = 1.0; + // FIXME: should 'image-resolution' (when implemented) be used *instead* of + // `script::dom::htmlimageelement::ImageRequest::current_pixel_density`? - let width = (intrinsic_size_in_dots.width as CSSFloat) / dppx; - let height = (intrinsic_size_in_dots.height as CSSFloat) / dppx; - return Some(Self { - kind: ReplacedContentKind::Image(image), - intrinsic: IntrinsicSizes { - width: Some(Length::new(width)), - height: Some(Length::new(height)), - // FIXME https://github.com/w3c/csswg-drafts/issues/4572 - ratio: Some(width / height), - }, - }); - } - None + // https://drafts.csswg.org/css-images-4/#the-image-resolution + let dppx = 1.0; + + let width = (intrinsic_size_in_dots.width as CSSFloat) / dppx; + let height = (intrinsic_size_in_dots.height as CSSFloat) / dppx; + return Some(Self { + kind, + intrinsic: IntrinsicSizes { + width: Some(Length::new(width)), + height: Some(Length::new(height)), + // FIXME https://github.com/w3c/csswg-drafts/issues/4572 + ratio: Some(width / height), + }, + }); } pub fn from_image_url<'dom>( @@ -160,6 +200,34 @@ impl ReplacedContent { }) .into_iter() .collect(), + ReplacedContentKind::Canvas(canvas_info) => { + let image_key = match canvas_info.source { + CanvasSource::WebGL(image_key) => image_key, + CanvasSource::Image(ref ipc_renderer) => match *ipc_renderer { + Some(ref ipc_renderer) => { + let ipc_renderer = ipc_renderer.lock().unwrap(); + let (sender, receiver) = ipc::channel().unwrap(); + ipc_renderer + .send(CanvasMsg::FromLayout( + FromLayoutMsg::SendData(sender), + canvas_info.canvas_id, + )) + .unwrap(); + receiver.recv().unwrap().image_key + }, + None => return vec![], + }, + }; + vec![Fragment::Image(ImageFragment { + debug_id: DebugId::new(), + style: style.clone(), + rect: Rect { + start_corner: Vec2::zero(), + size, + }, + image_key, + })] + }, } } |