Add support for static SVG images using resvg crate (#36721)

This change adds support for rendering static SVG images using the
`resvg` crate, allowing svg sources in the `img` tag and in CSS
`background` and `content` properties. There are some limitations in
using resvg:

1. There is no support for animations or interactivity as these would
require implementing the full DOM layer of SVG specification.
2. Only system fonts can be used for text rendering. There is some
mechanism to provide a custom font resolver to usvg, but that is not
explored in this change.
3. resvg's handling of certain edge cases involving lack of explicit
`width` and `height` on the root svg element deviates from what the
specification expects from browsers. For example, resvg uses the values
in `viewBox` to derive the missing width or height dimension, but
without scaling that dimension to preserve the aspect ratio. It also
doesn't allow overriding this behavior.

Demo screenshot:
![servo - resvg
img](https://github.com/user-attachments/assets/8ecb2de2-ab7c-48e2-9f08-2d09d2cb8791)

<details>
<summary>Source</summary>

```
<style>
 #svg1 {
   border: 1px solid red;
 }

 #svg2 {
   border: 1px solid red;
   width: 300px;
 }
 #svg3 {
   border: 1px solid red;
   width: 300px;
   height: 200px;
   object-fit: contain;
 }
 #svg4 {
   border: 1px solid red;
   width: 300px;
   height: 200px;
   object-fit: cover;
 }
 #svg5 {
   border: 1px solid red;
   width: 300px;
   height: 200px;
   object-fit: fill;
 }
 #svg6 {
   border: 1px solid red;
   width: 300px;
   height: 200px;
   object-fit: none;
 }
</style>
</head>
<body>
        <div>
          <img id="svg1" src="https://raw.githubusercontent.com/servo/servo/refs/heads/main/resources/servo.svg" alt="Servo logo">
        </div>
        <div>
          <img id="svg2" src="https://raw.githubusercontent.com/servo/servo/refs/heads/main/resources/servo.svg" alt="Servo logo">
          <img id="svg3" src="https://raw.githubusercontent.com/servo/servo/refs/heads/main/resources/servo.svg" alt="Servo logo">
          <img id="svg4" src="https://raw.githubusercontent.com/servo/servo/refs/heads/main/resources/servo.svg" alt="Servo logo">
        </div>
        <div>
          <img id="svg5" src="https://raw.githubusercontent.com/servo/servo/refs/heads/main/resources/servo.svg" alt="Servo logo">
          <img id="svg6" src="https://raw.githubusercontent.com/servo/servo/refs/heads/main/resources/servo.svg" alt="Servo logo">
        </div>
</body>
```

</details>

---------

Signed-off-by: Mukilan Thiyagarajan <mukilan@igalia.com>
Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Mukilan Thiyagarajan 2025-05-27 16:32:40 +05:30 committed by GitHub
parent 324196351e
commit 8a20e42de4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
267 changed files with 2374 additions and 544 deletions

View file

@ -328,7 +328,15 @@ impl CanvasState {
cors_setting: Option<CorsSettings>,
) -> Option<snapshot::Snapshot> {
let img = match self.request_image_from_cache(url, cors_setting) {
ImageResponse::Loaded(img, _) => img,
ImageResponse::Loaded(image, _) => {
if let Some(image) = image.as_raster_image() {
image
} else {
// TODO: https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
warn!("Vector images are not supported as image source in canvas2d");
return None;
}
},
ImageResponse::PlaceholderLoaded(_, _) |
ImageResponse::None |
ImageResponse::MetadataLoaded(_) => {
@ -336,7 +344,7 @@ impl CanvasState {
},
};
let size = Size2D::new(img.width, img.height);
let size = Size2D::new(img.metadata.width, img.metadata.height);
let format = match img.format {
PixelFormat::BGRA8 => snapshot::PixelFormat::BGRA,
PixelFormat::RGBA8 => snapshot::PixelFormat::RGBA,

View file

@ -7,6 +7,7 @@ use std::rc::Rc;
use dom_struct::dom_struct;
use js::rust::{HandleObject, MutableHandleValue};
use net_traits::image_cache::Image;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::DataTransferBinding::DataTransferMethods;
@ -155,7 +156,10 @@ impl DataTransferMethods<crate::DomTypeHolder> for DataTransfer {
// Step 3
if let Some(image) = image.downcast::<HTMLImageElement>() {
data_store.set_bitmap(image.image_data(), x, y);
match image.image_data().as_ref().and_then(Image::as_raster_image) {
Some(image) => data_store.set_bitmap(Some(image), x, y),
None => warn!("Vector images are not yet supported in setDragImage"),
}
}
}

View file

@ -20,8 +20,8 @@ use js::rust::HandleObject;
use mime::{self, Mime};
use net_traits::http_status::HttpStatus;
use net_traits::image_cache::{
ImageCache, ImageCacheResult, ImageOrMetadataAvailable, ImageResponder, ImageResponse,
PendingImageId, UsePlaceholder,
Image, ImageCache, ImageCacheResult, ImageLoadListener, ImageOrMetadataAvailable,
ImageResponse, PendingImageId, UsePlaceholder,
};
use net_traits::request::{Destination, Initiator, RequestId};
use net_traits::{
@ -29,7 +29,7 @@ use net_traits::{
ResourceFetchTiming, ResourceTimingType,
};
use num_traits::ToPrimitive;
use pixels::{CorsStatus, Image, ImageMetadata};
use pixels::{CorsStatus, ImageMetadata};
use servo_url::ServoUrl;
use servo_url::origin::MutableOrigin;
use style::attr::{AttrValue, LengthOrPercentageOrAuto, parse_integer, parse_length};
@ -146,9 +146,8 @@ struct ImageRequest {
parsed_url: Option<ServoUrl>,
source_url: Option<USVString>,
blocker: DomRefCell<Option<LoadBlocker>>,
#[conditional_malloc_size_of]
#[no_trace]
image: Option<Arc<Image>>,
image: Option<Image>,
#[no_trace]
metadata: Option<ImageMetadata>,
#[no_trace]
@ -177,7 +176,8 @@ impl HTMLImageElement {
pub(crate) fn is_usable(&self) -> Fallible<bool> {
// If image has an intrinsic width or intrinsic height (or both) equal to zero, then return bad.
if let Some(image) = &self.current_request.borrow().image {
if image.width == 0 || image.height == 0 {
let intrinsic_size = image.metadata();
if intrinsic_size.width == 0 || intrinsic_size.height == 0 {
return Ok(false);
}
}
@ -191,7 +191,7 @@ impl HTMLImageElement {
}
}
pub(crate) fn image_data(&self) -> Option<Arc<Image>> {
pub(crate) fn image_data(&self) -> Option<Image> {
self.current_request.borrow().image.clone()
}
}
@ -341,10 +341,12 @@ impl HTMLImageElement {
is_placeholder,
}) => {
if is_placeholder {
self.process_image_response(
ImageResponse::PlaceholderLoaded(image, url),
can_gc,
)
if let Some(raster_image) = image.as_raster_image() {
self.process_image_response(
ImageResponse::PlaceholderLoaded(raster_image, url),
can_gc,
)
}
} else {
self.process_image_response(ImageResponse::Loaded(image, url), can_gc)
}
@ -403,7 +405,7 @@ impl HTMLImageElement {
window
.image_cache()
.add_listener(ImageResponder::new(sender, window.pipeline_id(), id));
.add_listener(ImageLoadListener::new(sender, window.pipeline_id(), id));
}
fn fetch_request(&self, img_url: &ServoUrl, id: PendingImageId) {
@ -448,11 +450,8 @@ impl HTMLImageElement {
}
// Steps common to when an image has been loaded.
fn handle_loaded_image(&self, image: Arc<Image>, url: ServoUrl, can_gc: CanGc) {
self.current_request.borrow_mut().metadata = Some(ImageMetadata {
height: image.height,
width: image.width,
});
fn handle_loaded_image(&self, image: Image, url: ServoUrl, can_gc: CanGc) {
self.current_request.borrow_mut().metadata = Some(image.metadata());
self.current_request.borrow_mut().final_url = Some(url);
self.current_request.borrow_mut().image = Some(image);
self.current_request.borrow_mut().state = State::CompletelyAvailable;
@ -471,7 +470,7 @@ impl HTMLImageElement {
(true, false)
},
(ImageResponse::PlaceholderLoaded(image, url), ImageRequestPhase::Current) => {
self.handle_loaded_image(image, url, can_gc);
self.handle_loaded_image(Image::Raster(image), url, can_gc);
(false, true)
},
(ImageResponse::Loaded(image, url), ImageRequestPhase::Pending) => {
@ -483,7 +482,7 @@ impl HTMLImageElement {
(ImageResponse::PlaceholderLoaded(image, url), ImageRequestPhase::Pending) => {
self.abort_request(State::Unavailable, ImageRequestPhase::Pending, can_gc);
self.image_request.set(ImageRequestPhase::Current);
self.handle_loaded_image(image, url, can_gc);
self.handle_loaded_image(Image::Raster(image), url, can_gc);
(false, true)
},
(ImageResponse::MetadataLoaded(meta), ImageRequestPhase::Current) => {
@ -536,11 +535,15 @@ impl HTMLImageElement {
can_gc: CanGc,
) {
match image {
ImageResponse::Loaded(image, url) | ImageResponse::PlaceholderLoaded(image, url) => {
self.pending_request.borrow_mut().metadata = Some(ImageMetadata {
height: image.height,
width: image.width,
});
ImageResponse::Loaded(image, url) => {
self.pending_request.borrow_mut().metadata = Some(image.metadata());
self.pending_request.borrow_mut().final_url = Some(url);
self.pending_request.borrow_mut().image = Some(image);
self.finish_reacting_to_environment_change(src, generation, selected_pixel_density);
},
ImageResponse::PlaceholderLoaded(image, url) => {
let image = Image::Raster(image);
self.pending_request.borrow_mut().metadata = Some(image.metadata());
self.pending_request.borrow_mut().final_url = Some(url);
self.pending_request.borrow_mut().image = Some(image);
self.finish_reacting_to_environment_change(src, generation, selected_pixel_density);
@ -1020,10 +1023,7 @@ impl HTMLImageElement {
// set on this element.
self.generation.set(self.generation.get() + 1);
// Step 6.3
let metadata = ImageMetadata {
height: image.height,
width: image.width,
};
let metadata = image.metadata();
// Step 6.3.2 abort requests
self.abort_request(
State::CompletelyAvailable,
@ -1033,7 +1033,7 @@ impl HTMLImageElement {
self.abort_request(State::Unavailable, ImageRequestPhase::Pending, can_gc);
let mut current_request = self.current_request.borrow_mut();
current_request.final_url = Some(img_url.clone());
current_request.image = Some(image.clone());
current_request.image = Some(image);
current_request.metadata = Some(metadata);
// Step 6.3.6
current_request.current_pixel_density = pixel_density;
@ -1360,7 +1360,7 @@ impl HTMLImageElement {
pub(crate) fn same_origin(&self, origin: &MutableOrigin) -> bool {
if let Some(ref image) = self.current_request.borrow().image {
return image.cors_status == CorsStatus::Safe;
return image.cors_status() == CorsStatus::Safe;
}
self.current_request
@ -1432,7 +1432,7 @@ impl MicrotaskRunnable for ImageElementMicrotask {
pub(crate) trait LayoutHTMLImageElementHelpers {
fn image_url(self) -> Option<ServoUrl>;
fn image_density(self) -> Option<f64>;
fn image_data(self) -> (Option<Arc<Image>>, Option<ImageMetadata>);
fn image_data(self) -> (Option<Image>, Option<ImageMetadata>);
fn get_width(self) -> LengthOrPercentageOrAuto;
fn get_height(self) -> LengthOrPercentageOrAuto;
}
@ -1449,12 +1449,9 @@ impl LayoutHTMLImageElementHelpers for LayoutDom<'_, HTMLImageElement> {
self.current_request().parsed_url.clone()
}
fn image_data(self) -> (Option<Arc<Image>>, Option<ImageMetadata>) {
fn image_data(self) -> (Option<Image>, Option<ImageMetadata>) {
let current_request = self.current_request();
(
current_request.image.clone(),
current_request.metadata.clone(),
)
(current_request.image.clone(), current_request.metadata)
}
fn image_density(self) -> Option<f64> {

View file

@ -27,7 +27,7 @@ use net_traits::{
FetchMetadata, FetchResponseListener, Metadata, NetworkError, ResourceFetchTiming,
ResourceTimingType,
};
use pixels::Image;
use pixels::RasterImage;
use script_bindings::codegen::GenericBindings::TimeRangesBinding::TimeRangesMethods;
use script_bindings::codegen::InheritTypes::{
ElementTypeId, HTMLElementTypeId, HTMLMediaElementTypeId, NodeTypeId,
@ -186,12 +186,12 @@ impl MediaFrameRenderer {
}
}
fn render_poster_frame(&mut self, image: Arc<Image>) {
fn render_poster_frame(&mut self, image: Arc<RasterImage>) {
if let Some(image_key) = image.id {
self.current_frame = Some(MediaFrame {
image_key,
width: image.width as i32,
height: image.height as i32,
width: image.metadata.width as i32,
height: image.metadata.height as i32,
});
self.show_poster = true;
}
@ -1358,13 +1358,13 @@ impl HTMLMediaElement {
}
/// <https://html.spec.whatwg.org/multipage/#poster-frame>
pub(crate) fn process_poster_image_loaded(&self, image: Arc<Image>) {
pub(crate) fn process_poster_image_loaded(&self, image: Arc<RasterImage>) {
if !self.show_poster.get() {
return;
}
// Step 6.
self.handle_resize(Some(image.width), Some(image.height));
self.handle_resize(Some(image.metadata.width), Some(image.metadata.height));
self.video_renderer
.lock()
.unwrap()

View file

@ -7,7 +7,7 @@ use std::default::Default;
use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix, local_name, ns};
use js::rust::HandleObject;
use pixels::Image;
use pixels::RasterImage;
use servo_arc::Arc;
use crate::dom::attr::Attr;
@ -31,7 +31,7 @@ pub(crate) struct HTMLObjectElement {
htmlelement: HTMLElement,
#[ignore_malloc_size_of = "Arc"]
#[no_trace]
image: DomRefCell<Option<Arc<Image>>>,
image: DomRefCell<Option<Arc<RasterImage>>>,
form_owner: MutNullableDom<HTMLFormElement>,
validity_state: MutNullableDom<ValidityState>,
}

View file

@ -12,7 +12,7 @@ use html5ever::{LocalName, Prefix, local_name, ns};
use ipc_channel::ipc;
use js::rust::HandleObject;
use net_traits::image_cache::{
ImageCache, ImageCacheResult, ImageOrMetadataAvailable, ImageResponder, ImageResponse,
ImageCache, ImageCacheResult, ImageLoadListener, ImageOrMetadataAvailable, ImageResponse,
PendingImageId, UsePlaceholder,
};
use net_traits::request::{CredentialsMode, Destination, RequestBuilder, RequestId};
@ -217,7 +217,7 @@ impl HTMLVideoElement {
element.process_image_response(response.response, CanGc::note());
});
image_cache.add_listener(ImageResponder::new(sender, window.pipeline_id(), id));
image_cache.add_listener(ImageLoadListener::new(sender, window.pipeline_id(), id));
}
/// <https://html.spec.whatwg.org/multipage/#poster-frame>
@ -262,7 +262,10 @@ impl HTMLVideoElement {
match response {
ImageResponse::Loaded(image, url) => {
debug!("Loaded poster image for video element: {:?}", url);
self.htmlmediaelement.process_poster_image_loaded(image);
match image.as_raster_image() {
Some(image) => self.htmlmediaelement.process_poster_image_loaded(image),
None => warn!("Vector images are not yet supported in video poster"),
}
LoadBlocker::terminate(&self.load_blocker, can_gc);
},
ImageResponse::MetadataLoaded(..) => {},

View file

@ -10,7 +10,6 @@ use std::default::Default;
use std::f64::consts::PI;
use std::ops::Range;
use std::slice::from_ref;
use std::sync::Arc as StdArc;
use std::{cmp, fmt, iter};
use app_units::Au;
@ -26,7 +25,8 @@ use js::jsapi::JSObject;
use js::rust::HandleObject;
use libc::{self, c_void, uintptr_t};
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use pixels::{Image, ImageMetadata};
use net_traits::image_cache::Image;
use pixels::ImageMetadata;
use script_bindings::codegen::InheritTypes::DocumentFragmentTypeId;
use script_layout_interface::{
GenericLayoutData, HTMLCanvasData, HTMLMediaData, LayoutElementType, LayoutNodeType, QueryMsg,
@ -1618,7 +1618,7 @@ pub(crate) trait LayoutNodeHelpers<'dom> {
fn selection(self) -> Option<Range<usize>>;
fn image_url(self) -> Option<ServoUrl>;
fn image_density(self) -> Option<f64>;
fn image_data(self) -> Option<(Option<StdArc<Image>>, Option<ImageMetadata>)>;
fn image_data(self) -> Option<(Option<Image>, Option<ImageMetadata>)>;
fn canvas_data(self) -> Option<HTMLCanvasData>;
fn media_data(self) -> Option<HTMLMediaData>;
fn svg_data(self) -> Option<SVGSVGData>;
@ -1831,7 +1831,7 @@ impl<'dom> LayoutNodeHelpers<'dom> for LayoutDom<'dom, Node> {
.image_url()
}
fn image_data(self) -> Option<(Option<StdArc<Image>>, Option<ImageMetadata>)> {
fn image_data(self) -> Option<(Option<Image>, Option<ImageMetadata>)> {
self.downcast::<HTMLImageElement>().map(|e| e.image_data())
}

View file

@ -21,15 +21,15 @@ use js::jsval::JSVal;
use js::rust::{HandleObject, MutableHandleValue};
use net_traits::http_status::HttpStatus;
use net_traits::image_cache::{
ImageCache, ImageCacheResult, ImageOrMetadataAvailable, ImageResponder, ImageResponse,
PendingImageId, PendingImageResponse, UsePlaceholder,
ImageCache, ImageCacheResponseMessage, ImageCacheResult, ImageLoadListener,
ImageOrMetadataAvailable, ImageResponse, PendingImageId, UsePlaceholder,
};
use net_traits::request::{RequestBuilder, RequestId};
use net_traits::{
FetchMetadata, FetchResponseListener, FetchResponseMsg, NetworkError, ResourceFetchTiming,
ResourceTimingType,
};
use pixels::Image;
use pixels::RasterImage;
use servo_url::{ImmutableOrigin, ServoUrl};
use uuid::Uuid;
@ -114,15 +114,15 @@ pub(crate) struct Notification {
/// <https://notifications.spec.whatwg.org/#image-resource>
#[ignore_malloc_size_of = "Arc"]
#[no_trace]
image_resource: DomRefCell<Option<Arc<Image>>>,
image_resource: DomRefCell<Option<Arc<RasterImage>>>,
/// <https://notifications.spec.whatwg.org/#icon-resource>
#[ignore_malloc_size_of = "Arc"]
#[no_trace]
icon_resource: DomRefCell<Option<Arc<Image>>>,
icon_resource: DomRefCell<Option<Arc<RasterImage>>>,
/// <https://notifications.spec.whatwg.org/#badge-resource>
#[ignore_malloc_size_of = "Arc"]
#[no_trace]
badge_resource: DomRefCell<Option<Arc<Image>>>,
badge_resource: DomRefCell<Option<Arc<RasterImage>>>,
}
impl Notification {
@ -561,7 +561,7 @@ struct Action {
/// <https://notifications.spec.whatwg.org/#action-icon-resource>
#[ignore_malloc_size_of = "Arc"]
#[no_trace]
icon_resource: DomRefCell<Option<Arc<Image>>>,
icon_resource: DomRefCell<Option<Arc<RasterImage>>>,
}
/// <https://notifications.spec.whatwg.org/#create-a-notification-with-a-settings-object>
@ -886,7 +886,11 @@ impl Notification {
ImageCacheResult::Available(ImageOrMetadataAvailable::ImageAvailable {
image, ..
}) => {
self.set_resource_and_show_when_ready(request_id, &resource_type, Some(image));
let image = image.as_raster_image();
if image.is_none() {
warn!("Vector images are not supported in notifications yet");
};
self.set_resource_and_show_when_ready(request_id, &resource_type, image);
},
ImageCacheResult::Available(ImageOrMetadataAvailable::MetadataAvailable(
_,
@ -925,8 +929,7 @@ impl Notification {
pending_image_id: PendingImageId,
resource_type: ResourceType,
) {
let (sender, receiver) =
ipc::channel::<PendingImageResponse>().expect("ipc channel failure");
let (sender, receiver) = ipc::channel().expect("ipc channel failure");
let global: &GlobalScope = &self.global();
@ -942,7 +945,11 @@ impl Notification {
task_source.queue(task!(handle_response: move || {
let this = trusted_this.root();
if let Ok(response) = response {
this.handle_image_cache_response(request_id, response.response, resource_type);
let ImageCacheResponseMessage::NotifyPendingImageLoadStatus(status) = response else {
warn!("Received unexpected message from image cache: {response:?}");
return;
};
this.handle_image_cache_response(request_id, status.response, resource_type);
} else {
this.handle_image_cache_response(request_id, ImageResponse::None, resource_type);
}
@ -950,7 +957,7 @@ impl Notification {
}),
);
global.image_cache().add_listener(ImageResponder::new(
global.image_cache().add_listener(ImageLoadListener::new(
sender,
global.pipeline_id(),
pending_image_id,
@ -965,7 +972,11 @@ impl Notification {
) {
match response {
ImageResponse::Loaded(image, _) => {
self.set_resource_and_show_when_ready(request_id, &resource_type, Some(image));
let image = image.as_raster_image();
if image.is_none() {
warn!("Vector images are not yet supported in notification attribute");
};
self.set_resource_and_show_when_ready(request_id, &resource_type, image);
},
ImageResponse::PlaceholderLoaded(image, _) => {
self.set_resource_and_show_when_ready(request_id, &resource_type, Some(image));
@ -981,7 +992,7 @@ impl Notification {
&self,
request_id: RequestId,
resource_type: &ResourceType,
image: Option<Arc<Image>>,
image: Option<Arc<RasterImage>>,
) {
match resource_type {
ResourceType::Image => {

View file

@ -609,15 +609,31 @@ impl WebGLRenderingContext {
};
let cors_setting = cors_setting_for_element(image.upcast());
let img =
match canvas_utils::request_image_from_cache(&window, img_url, cors_setting) {
ImageResponse::Loaded(img, _) => img,
ImageResponse::PlaceholderLoaded(_, _) |
ImageResponse::None |
ImageResponse::MetadataLoaded(_) => return Ok(None),
};
let img = match canvas_utils::request_image_from_cache(
&window,
img_url,
cors_setting,
) {
ImageResponse::Loaded(image, _) => {
match image.as_raster_image() {
Some(image) => image,
None => {
// Vector images are not currently supported here and there are some open questions
// in the specification about how to handle them:
// See https://github.com/KhronosGroup/WebGL/issues/1503.
warn!(
"Vector images as are not yet supported as WebGL texture source"
);
return Ok(None);
},
}
},
ImageResponse::PlaceholderLoaded(_, _) |
ImageResponse::None |
ImageResponse::MetadataLoaded(_) => return Ok(None),
};
let size = Size2D::new(img.width, img.height);
let size = Size2D::new(img.metadata.width, img.metadata.height);
let data = IpcSharedMemory::from_bytes(img.first_frame().bytes);
TexPixels::new(data, size, img.format, false)

View file

@ -55,7 +55,8 @@ use malloc_size_of::MallocSizeOf;
use media::WindowGLContext;
use net_traits::ResourceThreads;
use net_traits::image_cache::{
ImageCache, ImageResponder, ImageResponse, PendingImageId, PendingImageResponse,
ImageCache, ImageCacheResponseMessage, ImageLoadListener, ImageResponse, PendingImageId,
PendingImageResponse, RasterizationCompleteResponse,
};
use net_traits::storage_thread::StorageType;
use num_traits::ToPrimitive;
@ -87,7 +88,7 @@ use style_traits::CSSPixel;
use stylo_atoms::Atom;
use url::Position;
use webrender_api::ExternalScrollId;
use webrender_api::units::{DevicePixel, LayoutPixel};
use webrender_api::units::{DeviceIntSize, DevicePixel, LayoutPixel};
use super::bindings::codegen::Bindings::MessagePortBinding::StructuredSerializeOptions;
use super::bindings::trace::HashMapTracedValues;
@ -218,6 +219,8 @@ impl LayoutBlocker {
}
}
type PendingImageRasterizationKey = (PendingImageId, DeviceIntSize);
#[dom_struct]
pub(crate) struct Window {
globalscope: GlobalScope,
@ -240,7 +243,7 @@ pub(crate) struct Window {
#[no_trace]
image_cache: Arc<dyn ImageCache>,
#[no_trace]
image_cache_sender: IpcSender<PendingImageResponse>,
image_cache_sender: IpcSender<ImageCacheResponseMessage>,
window_proxy: MutNullableDom<WindowProxy>,
document: MutNullableDom<Document>,
location: MutNullableDom<Location>,
@ -343,11 +346,17 @@ pub(crate) struct Window {
pending_image_callbacks: DomRefCell<HashMap<PendingImageId, Vec<PendingImageCallback>>>,
/// All of the elements that have an outstanding image request that was
/// initiated by layout during a reflow. They are stored in the script thread
/// initiated by layout during a reflow. They are stored in the [`ScriptThread`]
/// to ensure that the element can be marked dirty when the image data becomes
/// available at some point in the future.
pending_layout_images: DomRefCell<HashMapTracedValues<PendingImageId, Vec<Dom<Node>>>>,
/// Vector images for which layout has intiated rasterization at a specific size
/// and whose results are not yet available. They are stored in the [`ScriptThread`]
/// so that the element can be marked dirty once the rasterization is completed.
pending_images_for_rasterization:
DomRefCell<HashMapTracedValues<PendingImageRasterizationKey, Vec<Dom<Node>>>>,
/// Directory to store unminified css for this window if unminify-css
/// opt is enabled.
unminified_css_dir: DomRefCell<Option<String>>,
@ -568,7 +577,7 @@ impl Window {
&self,
id: PendingImageId,
callback: impl Fn(PendingImageResponse) + 'static,
) -> IpcSender<PendingImageResponse> {
) -> IpcSender<ImageCacheResponseMessage> {
self.pending_image_callbacks
.borrow_mut()
.entry(id)
@ -597,6 +606,22 @@ impl Window {
}
}
pub(crate) fn handle_image_rasterization_complete_notification(
&self,
response: RasterizationCompleteResponse,
) {
let mut images = self.pending_images_for_rasterization.borrow_mut();
let nodes = images.entry((response.image_id, response.requested_size));
let nodes = match nodes {
Entry::Occupied(nodes) => nodes,
Entry::Vacant(_) => return,
};
for node in nodes.get() {
node.dirty(NodeDamage::OtherNodeDamage);
}
nodes.remove();
}
pub(crate) fn pending_image_notification(&self, response: PendingImageResponse) {
// We take the images here, in order to prevent maintaining a mutable borrow when
// image callbacks are called. These, in turn, can trigger garbage collection.
@ -2224,8 +2249,11 @@ impl Window {
.pending_layout_image_notification(response);
});
self.image_cache
.add_listener(ImageResponder::new(sender, self.pipeline_id(), id));
self.image_cache.add_listener(ImageLoadListener::new(
sender,
self.pipeline_id(),
id,
));
}
let nodes = images.entry(id).or_default();
@ -2234,6 +2262,25 @@ impl Window {
}
}
for image in results.pending_rasterization_images {
let node = unsafe { from_untrusted_node_address(image.node) };
let mut images = self.pending_images_for_rasterization.borrow_mut();
if !images.contains_key(&(image.id, image.size)) {
self.image_cache.add_rasterization_complete_listener(
pipeline_id,
image.id,
image.size,
self.image_cache_sender.clone(),
);
}
let nodes = images.entry((image.id, image.size)).or_default();
if !nodes.iter().any(|n| std::ptr::eq(&**n, &*node)) {
nodes.push(Dom::from_ref(&*node));
}
}
let size_messages = self
.Document()
.iframes_mut()
@ -2325,12 +2372,13 @@ impl Window {
});
let has_sent_idle_message = self.has_sent_idle_message.get();
let pending_images = !self.pending_layout_images.borrow().is_empty();
let no_pending_images = self.pending_layout_images.borrow().is_empty() &&
self.pending_images_for_rasterization.borrow().is_empty();
if !has_sent_idle_message &&
is_ready_state_complete &&
!reftest_wait &&
!pending_images &&
no_pending_images &&
!waiting_for_web_fonts_to_load
{
debug!(
@ -3005,7 +3053,7 @@ impl Window {
script_chan: Sender<MainThreadScriptMsg>,
layout: Box<dyn Layout>,
font_context: Arc<FontContext>,
image_cache_sender: IpcSender<PendingImageResponse>,
image_cache_sender: IpcSender<ImageCacheResponseMessage>,
image_cache: Arc<dyn ImageCache>,
resource_threads: ResourceThreads,
#[cfg(feature = "bluetooth")] bluetooth_thread: IpcSender<BluetoothRequest>,
@ -3104,6 +3152,7 @@ impl Window {
webxr_registry,
pending_image_callbacks: Default::default(),
pending_layout_images: Default::default(),
pending_images_for_rasterization: Default::default(),
unminified_css_dir: Default::default(),
local_script_source,
test_worklet: Default::default(),

View file

@ -6,7 +6,7 @@ use std::sync::Arc;
use constellation_traits::BlobImpl;
use indexmap::IndexMap;
use pixels::Image;
use pixels::RasterImage;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::root::DomRoot;
@ -70,7 +70,7 @@ impl Kind {
/// <https://html.spec.whatwg.org/multipage/#drag-data-store-bitmap>
#[allow(dead_code)] // TODO this used by DragEvent.
struct Bitmap {
image: Option<Arc<Image>>,
image: Option<Arc<RasterImage>>,
x: i32,
y: i32,
}
@ -126,7 +126,7 @@ impl DragDataStore {
self.mode = mode;
}
pub(crate) fn set_bitmap(&mut self, image: Option<Arc<Image>>, x: i32, y: i32) {
pub(crate) fn set_bitmap(&mut self, image: Option<Arc<RasterImage>>, x: i32, y: i32) {
self.bitmap = Some(Bitmap { image, x, y });
}

View file

@ -77,7 +77,10 @@ impl ImageAnimationManager {
image.id.unwrap(),
ImageDescriptor {
format: ImageFormat::BGRA8,
size: DeviceIntSize::new(image.width as i32, image.height as i32),
size: DeviceIntSize::new(
image.metadata.width as i32,
image.metadata.height as i32,
),
stride: None,
offset: 0,
flags: ImageDescriptorFlags::ALLOW_MIPMAPS,

View file

@ -6,12 +6,12 @@
use std::borrow::Cow;
use std::fmt;
use std::sync::Arc as StdArc;
use base::id::{BrowsingContextId, PipelineId};
use fonts_traits::ByteIndex;
use html5ever::{local_name, ns};
use pixels::{Image, ImageMetadata};
use net_traits::image_cache::Image;
use pixels::ImageMetadata;
use range::Range;
use script_layout_interface::wrapper_traits::{LayoutDataTrait, LayoutNode, ThreadSafeLayoutNode};
use script_layout_interface::{
@ -371,7 +371,7 @@ impl<'dom> ThreadSafeLayoutNode<'dom> for ServoThreadSafeLayoutNode<'dom> {
this.image_density()
}
fn image_data(&self) -> Option<(Option<StdArc<Image>>, Option<ImageMetadata>)> {
fn image_data(&self) -> Option<(Option<Image>, Option<ImageMetadata>)> {
let this = unsafe { self.get_jsmanaged() };
this.image_data()
}

View file

@ -16,7 +16,7 @@ use crossbeam_channel::{Receiver, SendError, Sender, select};
use devtools_traits::{DevtoolScriptControlMsg, ScriptToDevtoolsControlMsg};
use ipc_channel::ipc::IpcSender;
use net_traits::FetchResponseMsg;
use net_traits::image_cache::PendingImageResponse;
use net_traits::image_cache::ImageCacheResponseMessage;
use profile_traits::mem::{self as profile_mem, OpaqueSender, ReportsChan};
use profile_traits::time::{self as profile_time};
use script_traits::{Painter, ScriptThreadMessage};
@ -40,7 +40,7 @@ pub(crate) enum MixedMessage {
FromConstellation(ScriptThreadMessage),
FromScript(MainThreadScriptMsg),
FromDevtools(DevtoolScriptControlMsg),
FromImageCache(PendingImageResponse),
FromImageCache(ImageCacheResponseMessage),
#[cfg(feature = "webgpu")]
FromWebGPUServer(WebGPUMsg),
TimerFired,
@ -104,7 +104,14 @@ impl MixedMessage {
MainThreadScriptMsg::Inactive => None,
MainThreadScriptMsg::WakeUp => None,
},
MixedMessage::FromImageCache(response) => Some(response.pipeline_id),
MixedMessage::FromImageCache(response) => match response {
ImageCacheResponseMessage::NotifyPendingImageLoadStatus(response) => {
Some(response.pipeline_id)
},
ImageCacheResponseMessage::VectorImageRasterizationComplete(response) => {
Some(response.pipeline_id)
},
},
MixedMessage::FromDevtools(_) | MixedMessage::TimerFired => None,
#[cfg(feature = "webgpu")]
MixedMessage::FromWebGPUServer(..) => None,
@ -326,7 +333,7 @@ pub(crate) struct ScriptThreadSenders {
/// messages on this channel are routed to crossbeam [`Sender`] on the router thread, which
/// in turn sends messages to [`ScriptThreadReceivers::image_cache_receiver`].
#[no_trace]
pub(crate) image_cache_sender: IpcSender<PendingImageResponse>,
pub(crate) image_cache_sender: IpcSender<ImageCacheResponseMessage>,
/// For providing contact with the time profiler.
#[no_trace]
@ -355,7 +362,7 @@ pub(crate) struct ScriptThreadReceivers {
/// The [`Receiver`] which receives incoming messages from the `ImageCache`.
#[no_trace]
pub(crate) image_cache_receiver: Receiver<PendingImageResponse>,
pub(crate) image_cache_receiver: Receiver<ImageCacheResponseMessage>,
/// For receiving commands from an optional devtools server. Will be ignored if no such server
/// exists. When devtools are not active this will be [`crossbeam_channel::never()`].

View file

@ -71,7 +71,7 @@ use js::jsval::UndefinedValue;
use js::rust::ParentRuntime;
use media::WindowGLContext;
use metrics::MAX_TASK_NS;
use net_traits::image_cache::{ImageCache, PendingImageResponse};
use net_traits::image_cache::{ImageCache, ImageCacheResponseMessage};
use net_traits::request::{Referrer, RequestId};
use net_traits::response::ResponseInit;
use net_traits::storage_thread::StorageType;
@ -2095,11 +2095,24 @@ impl ScriptThread {
}
}
fn handle_msg_from_image_cache(&self, response: PendingImageResponse) {
let window = self.documents.borrow().find_window(response.pipeline_id);
if let Some(ref window) = window {
window.pending_image_notification(response);
}
fn handle_msg_from_image_cache(&self, response: ImageCacheResponseMessage) {
match response {
ImageCacheResponseMessage::NotifyPendingImageLoadStatus(pending_image_response) => {
let window = self
.documents
.borrow()
.find_window(pending_image_response.pipeline_id);
if let Some(ref window) = window {
window.pending_image_notification(pending_image_response);
}
},
ImageCacheResponseMessage::VectorImageRasterizationComplete(response) => {
let window = self.documents.borrow().find_window(response.pipeline_id);
if let Some(ref window) = window {
window.handle_image_rasterization_complete_notification(response);
}
},
};
}
fn handle_webdriver_msg(