[librsvg: 2/5] filters: use OnceCell for the background surface
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 2/5] filters: use OnceCell for the background surface
- Date: Mon, 4 Jan 2021 17:44:55 +0000 (UTC)
commit 75cca6c352dc691080172ca440ff1aee36a3626a
Author: Paolo Borelli <pborelli gnome org>
Date: Wed Dec 30 19:31:41 2020 +0100
filters: use OnceCell for the background surface
src/filters/context.rs | 34 +++++++---------------------------
1 file changed, 7 insertions(+), 27 deletions(-)
---
diff --git a/src/filters/context.rs b/src/filters/context.rs
index fec42c84..5c7fd269 100644
--- a/src/filters/context.rs
+++ b/src/filters/context.rs
@@ -1,4 +1,4 @@
-use std::cell::RefCell;
+use once_cell::sync::OnceCell;
use std::collections::HashMap;
use std::f64;
@@ -58,7 +58,7 @@ pub struct FilterContext {
/// Surfaces of the previous filter primitives by name.
previous_results: HashMap<CustomIdent, FilterOutput>,
/// The background surface. Computed lazily.
- background_surface: RefCell<Option<Result<SharedImageSurface, FilterError>>>,
+ background_surface: OnceCell<Result<SharedImageSurface, FilterError>>,
/// Primtive units
primitive_units: CoordUnits,
/// The filter effects region.
@@ -162,7 +162,7 @@ impl FilterContext {
source_surface,
last_result: None,
previous_results: HashMap::new(),
- background_surface: RefCell::new(None),
+ background_surface: OnceCell::new(),
primitive_units,
effects_region,
processing_linear_rgb: false,
@@ -188,34 +188,14 @@ impl FilterContext {
&self,
draw_ctx: &DrawingCtx,
) -> Result<SharedImageSurface, FilterError> {
- {
- // At this point either no, or only immutable references to background_surface exist, so
- // it's ok to make an immutable reference.
- let bg = self.background_surface.borrow();
-
- // If background_surface was already computed, return the immutable reference. It will
- // get bound to the &self lifetime by the function return type.
- if let Some(ref result) = *bg {
- return result.clone();
- }
- }
-
- // If we got here, then background_surface hasn't been computed yet. This means there are
- // no references to it and we can create a mutable reference.
- let mut bg = self.background_surface.borrow_mut();
-
- *bg = Some(
+ let res = self.background_surface.get_or_init(|| {
draw_ctx
.get_snapshot(self.source_surface.width(), self.source_surface.height())
- .map_err(FilterError::CairoError),
- );
+ .map_err(FilterError::CairoError)
+ });
// Return the only existing reference as immutable.
- bg.as_ref()
- .unwrap()
- .as_ref()
- .map(|surf| surf.clone())
- .map_err(|e| e.clone())
+ res.as_ref().map(|s| s.clone()).map_err(|e| e.clone())
}
/// Converts this `FilterContext` into the surface corresponding to the output of the filter
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]