[librsvg: 5/14] drawing_ctx: add get_snapshot method



commit ef77408ff9bae036f49d6bb3bab6b05b86e83fb8
Author: Paolo Borelli <pborelli gnome org>
Date:   Sat Jan 5 11:34:50 2019 +0100

    drawing_ctx: add get_snapshot method
    
    This allows to remove get_cr_stack and get_raw_offset and to make
    is_cairo_context_nested private.

 rsvg_internals/src/drawing_ctx.rs     | 30 ++++++++++++++--------
 rsvg_internals/src/filters/context.rs | 47 ++++++++---------------------------
 2 files changed, 30 insertions(+), 47 deletions(-)
---
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 34e14adf..c31fbc33 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -193,14 +193,6 @@ impl DrawingCtx {
         self.cr = cr.clone();
     }
 
-    pub fn is_cairo_context_nested(&self, cr: &cairo::Context) -> bool {
-        cr.to_raw_none() != self.initial_cr.to_raw_none()
-    }
-
-    pub fn get_cr_stack(&self) -> &Vec<cairo::Context> {
-        &self.cr_stack
-    }
-
     pub fn get_width(&self) -> f64 {
         self.rect.width
     }
@@ -209,8 +201,8 @@ impl DrawingCtx {
         self.rect.height
     }
 
-    pub fn get_raw_offset(&self) -> (f64, f64) {
-        (self.rect.x, self.rect.y)
+    fn is_cairo_context_nested(&self, cr: &cairo::Context) -> bool {
+        cr.to_raw_none() != self.initial_cr.to_raw_none()
     }
 
     pub fn get_offset(&self) -> (f64, f64) {
@@ -699,6 +691,24 @@ impl DrawingCtx {
         cr.set_matrix(save_affine);
     }
 
+    pub fn get_snapshot(&self, surface: &cairo::ImageSurface) {
+        let (x, y) = (self.rect.x, self.rect.y);
+
+        // TODO: as far as I can tell this should not render elements past the last (topmost) one
+        // with enable-background: new (because technically we shouldn't have been caching them).
+        // Right now there are no enable-background checks whatsoever.
+        let cr = cairo::Context::new(&surface);
+        for draw in self.cr_stack.iter() {
+            let nested = self.is_cairo_context_nested(&draw);
+            cr.set_source_surface(
+                &draw.get_target(),
+                if nested { 0f64 } else { -x },
+                if nested { 0f64 } else { -y },
+            );
+            cr.paint();
+        }
+    }
+
     pub fn draw_node_on_surface(
         &mut self,
         node: &RsvgNode,
diff --git a/rsvg_internals/src/filters/context.rs b/rsvg_internals/src/filters/context.rs
index 33f425d3..4231b42d 100644
--- a/rsvg_internals/src/filters/context.rs
+++ b/rsvg_internals/src/filters/context.rs
@@ -263,37 +263,6 @@ impl FilterContext {
             .map_err(FilterError::CairoError)
     }
 
-    /// Computes and returns the background image snapshot.
-    fn compute_background_image(
-        &self,
-        draw_ctx: &DrawingCtx,
-    ) -> Result<cairo::ImageSurface, cairo::Status> {
-        let surface = cairo::ImageSurface::create(
-            cairo::Format::ARgb32,
-            self.source_surface.width(),
-            self.source_surface.height(),
-        )?;
-
-        let (x, y) = draw_ctx.get_raw_offset();
-        let stack = draw_ctx.get_cr_stack();
-
-        // TODO: as far as I can tell this should not render elements past the last (topmost) one
-        // with enable-background: new (because technically we shouldn't have been caching them).
-        // Right now there are no enable-background checks whatsoever.
-        let cr = cairo::Context::new(&surface);
-        for draw in stack.into_iter() {
-            let nested = draw_ctx.is_cairo_context_nested(&draw);
-            cr.set_source_surface(
-                &draw.get_target(),
-                if nested { 0f64 } else { -x },
-                if nested { 0f64 } else { -y },
-            );
-            cr.paint();
-        }
-
-        Ok(surface)
-    }
-
     /// Returns the surface corresponding to the background image snapshot.
     pub fn background_image(
         &self,
@@ -316,12 +285,16 @@ impl FilterContext {
         let bg = unsafe { &mut *self.background_surface.get() };
 
         *bg = Some(
-            self.compute_background_image(draw_ctx)
-                .map_err(FilterError::CairoError)
-                .and_then(|surface| {
-                    SharedImageSurface::new(surface, SurfaceType::SRgb)
-                        .map_err(FilterError::CairoError)
-                }),
+            cairo::ImageSurface::create(
+                cairo::Format::ARgb32,
+                self.source_surface.width(),
+                self.source_surface.height(),
+            )
+            .map_err(FilterError::CairoError)
+            .and_then(|s| {
+                draw_ctx.get_snapshot(&s);
+                SharedImageSurface::new(s, SurfaceType::SRgb).map_err(FilterError::CairoError)
+            }),
         );
 
         // Return the only existing reference as immutable.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]