@@ -10,7 +10,7 @@ use fnv::FnvHashMap;
1010use fonts:: FontContext ;
1111use fxhash:: FxHashMap ;
1212use net_traits:: image_cache:: {
13- ImageCache , ImageCacheResult , ImageOrMetadataAvailable , PendingImageId , UsePlaceholder ,
13+ Image as CachedImage , ImageCache , ImageCacheResult , ImageOrMetadataAvailable , PendingImageId , UsePlaceholder
1414} ;
1515use parking_lot:: { Mutex , RwLock } ;
1616use pixels:: Image as PixelImage ;
@@ -49,20 +49,28 @@ pub struct LayoutContext<'a> {
4949 pub iframe_sizes : Mutex < IFrameSizes > ,
5050
5151 pub resolved_image_cache :
52- Arc < RwLock < FnvHashMap < ( ServoUrl , UsePlaceholder ) , Option < net_traits :: image_cache :: Image > > > > ,
52+ Arc < RwLock < FnvHashMap < ( ServoUrl , UsePlaceholder ) , Option < CachedImage > > > > ,
5353
5454 pub node_image_animation_map : Arc < RwLock < FxHashMap < OpaqueNode , ImageAnimationState > > > ,
5555}
5656
57+ pub struct LayoutImage {
58+ pub image : CachedImage ,
59+ // image-set images can override the natural resolution
60+ // and hence the final size for raster images
61+ pub size : DeviceIntSize
62+ }
63+
5764pub enum ResolvedImage < ' a > {
5865 Gradient ( & ' a Gradient ) ,
59- Image ( net_traits :: image_cache :: Image ) ,
66+ Image ( LayoutImage ) ,
6067}
6168
6269impl Drop for LayoutContext < ' _ > {
6370 fn drop ( & mut self ) {
6471 if !std:: thread:: panicking ( ) {
6572 assert ! ( self . pending_images. lock( ) . is_empty( ) ) ;
73+ assert ! ( self . pending_rasterization_images. lock( ) . is_empty( ) ) ;
6674 }
6775 }
6876}
@@ -156,7 +164,7 @@ impl LayoutContext<'_> {
156164 node : OpaqueNode ,
157165 url : ServoUrl ,
158166 use_placeholder : UsePlaceholder ,
159- ) -> Result < net_traits :: image_cache :: Image , ResolveImageError > {
167+ ) -> Result < CachedImage , ResolveImageError > {
160168 if let Some ( cached_image) = self
161169 . resolved_image_cache
162170 . read ( )
@@ -229,12 +237,13 @@ impl LayoutContext<'_> {
229237 // element and not just the node.
230238 let image_url = image_url. url ( ) . ok_or ( ResolveImageError :: InvalidUrl ) ?;
231239 let node = node. ok_or ( ResolveImageError :: MissingNode ) ?;
232- let webrender_info = self . get_webrender_image_for_url (
240+ let image = self . get_webrender_image_for_url (
233241 node,
234242 image_url. clone ( ) . into ( ) ,
235243 UsePlaceholder :: No ,
236244 ) ?;
237- Ok ( ResolvedImage :: Image ( webrender_info) )
245+ let size = Size2D :: new ( image. metadata ( ) . width as i32 , image. metadata ( ) . height as i32 ) ;
246+ Ok ( ResolvedImage :: Image ( LayoutImage { image, size } ) )
238247 } ,
239248 Image :: ImageSet ( image_set) => {
240249 image_set
@@ -244,17 +253,24 @@ impl LayoutContext<'_> {
244253 . and_then ( |image| {
245254 self . resolve_image ( node, & image. image )
246255 . map ( |info| match info {
247- ResolvedImage :: Image ( image_info ) => {
256+ ResolvedImage :: Image ( layout_image ) => {
248257 // From <https://drafts.csswg.org/css-images-4/#image-set-notation>:
249258 // > A <resolution> (optional). This is used to help the UA decide
250259 // > which <image-set-option> to choose. If the image reference is
251260 // > for a raster image, it also specifies the image’s natural
252261 // > resolution, overriding any other source of data that might
253262 // > supply a natural resolution.
254- // TODO: what is this
255- // image_info.size = (image_info.size.to_f32() / image.resolution.dppx())
256- // .to_u32();
257- ResolvedImage :: Image ( image_info)
263+ let image_metadata = layout_image. image . metadata ( ) ;
264+ let size = if layout_image. image . as_raster_image ( ) . is_some ( ) {
265+ let scale_factor = image. resolution . dppx ( ) ;
266+ Size2D :: new (
267+ image_metadata. width as f32 / scale_factor,
268+ image_metadata. height as f32 / scale_factor
269+ ) . to_i32 ( )
270+ } else {
271+ Size2D :: new ( image_metadata. width , image_metadata. height ) . to_i32 ( )
272+ } ;
273+ ResolvedImage :: Image ( LayoutImage { size, .. layout_image } )
258274 } ,
259275 _ => info,
260276 } )
0 commit comments