[librsvg: 8/14] Make into_image_surface() not panic



commit 8c049721a3cc6193b867ee12a81a88befef68b93
Author: Ivan Molodetskikh <yalterz gmail com>
Date:   Sun Jul 15 14:03:16 2018 +0300

    Make into_image_surface() not panic

 rsvg_internals/src/filters/mod.rs                  | 14 +++++---------
 rsvg_internals/src/surface_utils/shared_surface.rs | 21 ++++++++++++++-------
 2 files changed, 19 insertions(+), 16 deletions(-)
---
diff --git a/rsvg_internals/src/filters/mod.rs b/rsvg_internals/src/filters/mod.rs
index b815d614..515e18f8 100644
--- a/rsvg_internals/src/filters/mod.rs
+++ b/rsvg_internals/src/filters/mod.rs
@@ -329,13 +329,9 @@ pub fn render(
             }
         });
 
-    match filter_ctx.into_output() {
-        Ok(surface) => surface.into_image_surface(),
-        Err(err) => {
-            panic!(
-                "Could not create an empty surface to return from a filter: {}",
-                err
-            );
-        }
-    }
+    filter_ctx
+        .into_output()
+        .expect("could not create an empty surface to return from a filter")
+        .into_image_surface()
+        .expect("could not convert filter output into an ImageSurface")
 }
diff --git a/rsvg_internals/src/surface_utils/shared_surface.rs 
b/rsvg_internals/src/surface_utils/shared_surface.rs
index 23cc45be..eaa1c7cf 100644
--- a/rsvg_internals/src/surface_utils/shared_surface.rs
+++ b/rsvg_internals/src/surface_utils/shared_surface.rs
@@ -106,17 +106,24 @@ impl SharedImageSurface {
     }
 
     /// Converts this `SharedImageSurface` back into a Cairo image surface.
-    ///
-    /// # Panics
-    /// Panics if the underlying Cairo image surface is not unique, that is, there are other
-    /// instances of `SharedImageSurface` pointing at the same Cairo image surface.
     #[inline]
-    pub fn into_image_surface(self) -> ImageSurface {
+    pub fn into_image_surface(self) -> Result<ImageSurface, cairo::Status> {
         let reference_count =
             unsafe { cairo_sys::cairo_surface_get_reference_count(self.surface.to_raw_none()) };
-        assert_eq!(reference_count, 1);
 
-        self.surface
+        if reference_count == 1 {
+            Ok(self.surface)
+        } else {
+            // If there are any other references, copy the underlying surface.
+            let bounds = IRect {
+                x0: 0,
+                y0: 0,
+                x1: self.width,
+                y1: self.height,
+            };
+
+            self.copy_surface(bounds)
+        }
     }
 
     /// Returns the surface width.


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