Skip to content

Commit 9517b00

Browse files
authored
New node: 'Background Blur' (#5063)
Add a Background Blur effect that blurs what lies beneath the content
1 parent cfb45f2 commit 9517b00

18 files changed

Lines changed: 3700 additions & 185 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎node-graph/libraries/graphic-types/src/effect_layers.rs‎

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub enum PiecePlace {
2222
IntoContent { over: bool },
2323
/// The part of an interior piece blending with what shows through the content's fill, in a layer of its mode beneath the content's, or with `over`, over it.
2424
Through { over: bool },
25+
/// What lies beneath the content, filtered and cut to its silhouette, at the bottom of the content's own layer.
26+
Backdrop,
2527
}
2628

2729
/// One coverage of an effect that draws as a piece of its own, named like the map of it.
@@ -115,10 +117,16 @@ impl EffectLayers {
115117
.chain([(LayerPart::Content, blend_mode)])
116118
.collect();
117119

118-
// A shadow not knocked out in the content's layer, flattened or layered, would hide a part placed beneath that layer
120+
// A blurred backdrop, or a shadow not knocked out (flattened or layered), in the content's layer would hide a part placed beneath that layer
121+
let backdrop: Vec<LayerPart> = pieces_from(0, operations.len())
122+
.into_iter()
123+
.filter(|(piece, _)| piece.place == PiecePlace::Backdrop)
124+
.map(|(piece, _)| LayerPart::Piece(piece))
125+
.collect();
119126
let shows_through = |piece: &EffectPiece| piece.place == PiecePlace::Beneath && !piece.knocks_out;
120127
let mut content_run = parts.iter().rev().skip(1).take_while(|(_, mode)| *mode == BlendMode::Normal);
121-
let covered_beneath = content_run.any(|(part, _)| matches!(part, LayerPart::Piece(piece) if shows_through(piece))) || unmerged.iter().any(|(piece, _)| shows_through(piece));
128+
let covered_beneath =
129+
!backdrop.is_empty() || content_run.any(|(part, _)| matches!(part, LayerPart::Piece(piece) if shows_through(piece))) || unmerged.iter().any(|(piece, _)| shows_through(piece));
122130

123131
// Interior pieces of normal content blend within its share, in its layer. Below a full fill, a blended one splits off its part through the fill into a layer beneath
124132
// the content's, or over it when its mode isn't straight along what lies beneath or a shadow covers that, as every later piece then does to keep their order.
@@ -166,8 +174,8 @@ impl EffectLayers {
166174
layers.splice(content_layer..content_layer, through);
167175
}
168176

169-
// One normal layer at a full fill draws the same with all its effects flattened into the content's image, which takes fewer passes
170-
if blends_into_content && matches!(layers.as_slice(), [layer] if layer.blend_mode == BlendMode::Normal) {
177+
// One normal layer at a full fill draws the same with all its effects flattened into the content's image, which takes fewer passes, unless it blurs what lies beneath
178+
if blends_into_content && backdrop.is_empty() && matches!(layers.as_slice(), [layer] if layer.blend_mode == BlendMode::Normal) {
171179
let content = EffectLayer {
172180
blend_mode: BlendMode::Normal,
173181
parts: vec![LayerPart::Content],
@@ -179,6 +187,11 @@ impl EffectLayers {
179187
};
180188
}
181189

190+
// What lies beneath, filtered, goes at the bottom of the content's own layer, which fades and blends it with the content, even from a row flattened into the content
191+
if let Some(content_layer) = layers.iter_mut().find(|layer| layer.holds_content()) {
192+
content_layer.parts.splice(0..0, backdrop);
193+
}
194+
182195
Self { flattened, layers, trailing_masks }
183196
}
184197

@@ -201,6 +214,11 @@ impl EffectLayers {
201214
planned
202215
}
203216

217+
/// Whether a layered piece reads what lies beneath the content.
218+
pub fn reads_backdrop(&self) -> bool {
219+
(self.layers.iter().flat_map(|layer| &layer.parts)).any(|part| matches!(part, LayerPart::Piece(piece) if piece.place == PiecePlace::Backdrop))
220+
}
221+
204222
/// The index of the layer holding the content.
205223
pub fn content_layer(&self) -> Option<usize> {
206224
self.layers.iter().position(EffectLayer::holds_content)
@@ -263,6 +281,13 @@ impl EffectLayers {
263281
}
264282
}
265283

284+
/// Whether any of the effects reads what lies beneath the content, like a background blur.
285+
pub fn effects_read_backdrop(effects: &Effects) -> bool {
286+
effects
287+
.operations()
288+
.any(|operation| matches!(operation, EffectOperation::BackgroundBlur { .. }) && !operation.changes_nothing())
289+
}
290+
266291
/// Whether an effect row can draw as layered pieces, rather than being carried out within the content's image.
267292
/// Blurs, adjustments, masks, and feathers process the pixels of everything before them, and blend modes CSS lacks can't blend against the backdrop in SVG or a GPU layer,
268293
/// though interior pieces that `blend_into_content` blend with the content alone, which the renderers do themselves.
@@ -392,6 +417,7 @@ fn pieces(index: usize, operation: &EffectOperation) -> Vec<(EffectPiece, BlendM
392417
inside.into_iter().chain(outside).flatten().collect()
393418
}
394419
EffectOperation::PaintOverlay { blend_mode, .. } => vec![piece(0, Within, None, *blend_mode)],
420+
EffectOperation::BackgroundBlur { .. } => vec![piece(0, Backdrop, None, BlendMode::Normal)],
395421
EffectOperation::Adjustment(Adjustment::ColorOverlay { color, blend_mode, .. }) => vec![piece(0, Within, Some(*color), *blend_mode)],
396422
EffectOperation::Blur { .. }
397423
| EffectOperation::MotionBlur { .. }
@@ -568,6 +594,11 @@ mod tests {
568594
fn interior_pieces_go_over_the_content_where_its_layer_covers_what_lies_beneath_it() {
569595
let red = RED.with_alpha(0.75);
570596
let through_over = PiecePlace::Through { over: true };
597+
let background_blur = EffectOperation::BackgroundBlur {
598+
radius: 4.,
599+
box_blur: false,
600+
gamma: false,
601+
};
571602

572603
let with_knockout = |knocks_out: bool| {
573604
let mut shadow = drop_shadow(Color::BLACK.with_alpha(0.75), BlendMode::Multiply);
@@ -577,10 +608,12 @@ mod tests {
577608
shadow
578609
};
579610

580-
// A shadow not knocked out beneath the content in its layer would hide even a straight piece's part beneath that layer
581-
let layers = EffectLayers::plan(&Effects::from_operations([with_knockout(false), inner_shadow(red, BlendMode::Screen)]), BlendMode::Normal, 0.5, true);
582-
assert_eq!(modes(&layers), [BlendMode::Normal, BlendMode::Screen]);
583-
assert_eq!(parts(&layers)[1], [Some((1, 0, through_over))]);
611+
// A blurred backdrop, or a shadow not knocked out, beneath the content in its layer would hide even a straight piece's part beneath that layer
612+
for beneath in [background_blur, with_knockout(false)] {
613+
let layers = EffectLayers::plan(&Effects::from_operations([beneath.clone(), inner_shadow(red, BlendMode::Screen)]), BlendMode::Normal, 0.5, true);
614+
assert_eq!(modes(&layers), [BlendMode::Normal, BlendMode::Screen], "{beneath:?}");
615+
assert_eq!(parts(&layers)[1], [Some((1, 0, through_over))], "{beneath:?}");
616+
}
584617

585618
// A knocked-out shadow doesn't cover it there
586619
let layers = EffectLayers::plan(&Effects::from_operations([with_knockout(true), inner_shadow(red, BlendMode::Screen)]), BlendMode::Normal, 0.5, true);
@@ -758,6 +791,30 @@ mod tests {
758791
assert_eq!(kinds(filtered), [EffectKind::Mask, EffectKind::InnerShadow, EffectKind::Blur, EffectKind::Mask]);
759792
}
760793

794+
#[test]
795+
fn a_background_blur_lies_at_the_bottom_of_the_contents_layer() {
796+
let blue = Color::from_rgbaf32_unchecked(0., 0., 1., 1.);
797+
let background_blur = EffectOperation::BackgroundBlur {
798+
radius: 4.,
799+
box_blur: false,
800+
gamma: false,
801+
};
802+
let blur = EffectOperation::Blur {
803+
radius_mask: Default::default(),
804+
radius: 2.,
805+
box_blur: false,
806+
gamma: false,
807+
};
808+
809+
// Even beside a blended shadow, and flattened by a later blur, it fades and blends with the content
810+
let effects = Effects::from_operations([drop_shadow(blue, BlendMode::Multiply), background_blur, blur]);
811+
let layers = EffectLayers::plan(&effects, BlendMode::Screen, 1., true);
812+
assert!(layers.reads_backdrop());
813+
let content_layer = layers.content_layer().unwrap();
814+
assert_eq!(layers.layers[content_layer].blend_mode, BlendMode::Screen);
815+
assert_eq!(parts(&layers)[content_layer], [Some((1, 0, PiecePlace::Backdrop)), None]);
816+
}
817+
761818
#[test]
762819
fn knocked_out_pieces_hide_behind_however_the_content_covers_them() {
763820
let same_layer = EffectLayers::plan(&Effects::from_operations([drop_shadow(RED, BlendMode::Normal)]), BlendMode::Normal, 0.5, true);

‎node-graph/libraries/graphic-types/src/effects.rs‎

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ pub enum EffectKind {
9191
Pixelate,
9292
/// Prints each color channel of the content as dots on its own turned screen, as a press prints ink.
9393
ColorHalftone,
94+
/// Blurs what lies beneath the content within its silhouette, as if seen through frosted glass.
95+
BackgroundBlur,
9496
// The adjustments, which change each color of the content on its own, as their nodes of the same names do
9597
Desaturate,
9698
GammaCorrection,
@@ -148,6 +150,7 @@ impl std::fmt::Display for EffectKind {
148150
Self::Displace => "Displace",
149151
Self::Pixelate => "Pixelate",
150152
Self::ColorHalftone => "Color Halftone",
153+
Self::BackgroundBlur => "Background Blur",
151154
Self::Desaturate => "Desaturate",
152155
Self::GammaCorrection => "Gamma Correction",
153156
Self::MakeOpaque => "Make Opaque",
@@ -452,7 +455,7 @@ impl Effects {
452455
self.effects().skip(rows.start).take(rows.len()).map(Effect::operation)
453456
}
454457

455-
/// How far past the content the effects together can paint, in the effect space of a run of them.
458+
/// How far past the content the effects together can paint, or a background blur among them reads what lies beneath, in the effect space of a run of them.
456459
/// It leaves out what [`Effects::reach_around`] adds from the content's bounds and what it paints past its frame, for the effects relative to its extent.
457460
pub fn reach(&self) -> f64 {
458461
self.operations().map(|operation| operation.painted_reach()).sum()
@@ -1228,6 +1231,15 @@ pub enum EffectOperation {
12281231
/// Percent of the spacing between dots that each screen's dots lie past the `offset` along both of its axes.
12291232
phase: f64,
12301233
},
1234+
/// Blurs the backdrop beneath the content within the content's silhouette, beneath the content and its effects.
1235+
BackgroundBlur {
1236+
/// The standard deviation of the Gaussian, or the half width of the box.
1237+
radius: f64,
1238+
/// Whether to average a box of pixels rather than weighting them by a Gaussian.
1239+
box_blur: bool,
1240+
/// Whether to blend the colors with their gamma encoding rather than in linear light.
1241+
gamma: bool,
1242+
},
12311243
Adjustment(Adjustment),
12321244
}
12331245

@@ -1285,6 +1297,7 @@ impl EffectOperation {
12851297
Self::Displace { .. } => EffectKind::Displace,
12861298
Self::Pixelate { .. } => EffectKind::Pixelate,
12871299
Self::ColorHalftone { .. } => EffectKind::ColorHalftone,
1300+
Self::BackgroundBlur { .. } => EffectKind::BackgroundBlur,
12881301
Self::Adjustment(adjustment) => adjustment.kind(),
12891302
}
12901303
}
@@ -1315,7 +1328,7 @@ impl EffectOperation {
13151328
/// Whether this operation leaves the content as it is, like a blur of no radius or a shadow of no opacity.
13161329
pub fn changes_nothing(&self) -> bool {
13171330
match self {
1318-
Self::Blur { radius, .. } => *radius <= 0.,
1331+
Self::Blur { radius, .. } | Self::BackgroundBlur { radius, .. } => *radius <= 0.,
13191332
Self::DropShadow { color, .. } | Self::InnerShadow { color, .. } | Self::Satin { color, .. } => color.a() <= 0.,
13201333
Self::OuterGlow { color, noise, .. } | Self::InnerGlow { color, noise, .. } => GlowFill::of(color).is_none_or(|fill| fill.is_invisible(*noise)),
13211334
Self::Outline { paint, weight, contour_gradient, .. } => *weight <= 0. || OutlineFill::of(paint, *contour_gradient).is_none_or(|fill| fill.is_invisible()),
@@ -1375,6 +1388,16 @@ impl EffectOperation {
13751388
matches!(self, Self::Blur { radius_mask, .. } if !radius_mask.draws_nothing())
13761389
}
13771390

1391+
/// Whether this operation paints nothing past the content, whatever it reads there, like an inner shadow finding the edges it casts from.
1392+
pub fn paints_only_within_content(&self) -> bool {
1393+
match self {
1394+
Self::InnerShadow { .. } | Self::InnerGlow { .. } | Self::Satin { .. } | Self::Mask { .. } | Self::Feather { .. } | Self::BackgroundBlur { .. } => true,
1395+
Self::Outline { align, .. } => *align == StrokeAlign::Inside,
1396+
Self::BevelAndEmboss { style, .. } => *style == BevelStyle::InnerBevel,
1397+
_ => false,
1398+
}
1399+
}
1400+
13781401
/// Whether this operation reads the pixels around each one of the image the effects before it leave, like a blur or a warp, rather than casting from the content's shape or changing each pixel alone.
13791402
pub fn reads_pixels_around(&self) -> bool {
13801403
match self {
@@ -1383,7 +1406,7 @@ impl EffectOperation {
13831406
true
13841407
}
13851408
Self::ConvolutionKernel { .. } | Self::Emboss { .. } | Self::FindEdges | Self::MinimumMaximum { .. } | Self::Sharpen { .. } | Self::HighPass { .. } => true,
1386-
// Cast from the content's shape, reading a mask's own image or a feather's map, or changing each pixel alone
1409+
// Cast from the content's shape, reading a mask's or the backdrop's own image or a feather's map, or changing each pixel alone
13871410
Self::DropShadow { .. }
13881411
| Self::InnerShadow { .. }
13891412
| Self::OuterGlow { .. }
@@ -1392,7 +1415,7 @@ impl EffectOperation {
13921415
| Self::Outline { .. }
13931416
| Self::BevelAndEmboss { .. }
13941417
| Self::Feather { .. } => false,
1395-
Self::PaintOverlay { .. } | Self::Mask { .. } | Self::AddNoise { .. } | Self::Dissolve { .. } | Self::Adjustment(_) => false,
1418+
Self::PaintOverlay { .. } | Self::Mask { .. } | Self::BackgroundBlur { .. } | Self::AddNoise { .. } | Self::Dissolve { .. } | Self::Adjustment(_) => false,
13961419
}
13971420
}
13981421

@@ -1429,8 +1452,9 @@ impl EffectOperation {
14291452
pub fn reach(&self) -> f64 {
14301453
match self {
14311454
// A box reaches its half width, and the pixel past it for a fractional one, while a Gaussian fades out over three standard deviations
1432-
Self::Blur { radius, box_blur: true, .. } => radius.max(0.) + 1.,
1433-
Self::Blur { radius, .. } => 3. * radius.max(0.),
1455+
Self::Blur { radius, box_blur: true, .. } | Self::BackgroundBlur { radius, box_blur: true, .. } => radius.max(0.) + 1.,
1456+
// A background blur reads as far past the content as its Gaussian reaches
1457+
Self::Blur { radius, .. } | Self::BackgroundBlur { radius, .. } => 3. * radius.max(0.),
14341458
// An inner shadow paints within the content but reads this far out to find the edges it casts from.
14351459
// Shadows and glows end within their size, past which a precise glow fades over one more unit.
14361460
Self::DropShadow { distance, size, .. } | Self::InnerShadow { distance, size, .. } => distance.abs() + size.max(0.) + 1.,
@@ -1516,7 +1540,7 @@ impl EffectOperation {
15161540
};
15171541

15181542
match &mut self {
1519-
Self::Blur { radius, .. } => finite(radius),
1543+
Self::Blur { radius, .. } | Self::BackgroundBlur { radius, .. } => finite(radius),
15201544
Self::DropShadow { angle, distance, spread, size, .. }
15211545
| Self::InnerShadow {
15221546
angle, distance, choke: spread, size, ..
@@ -1801,6 +1825,11 @@ impl EffectOperation {
18011825
offset: DVec2::ZERO,
18021826
phase: 0.,
18031827
},
1828+
EffectKind::BackgroundBlur => Self::BackgroundBlur {
1829+
radius: 0.,
1830+
box_blur: false,
1831+
gamma: false,
1832+
},
18041833
EffectKind::Desaturate => Self::Adjustment(Adjustment::Desaturate { method: Default::default() }),
18051834
EffectKind::GammaCorrection => Self::Adjustment(Adjustment::GammaCorrection { gamma: 0., inverse: false }),
18061835
EffectKind::MakeOpaque => Self::Adjustment(Adjustment::MakeOpaque { strength: 0. }),
@@ -1853,6 +1882,11 @@ impl EffectOperation {
18531882
visitor.parameter("box_blur", box_blur, false);
18541883
visitor.parameter("gamma", gamma, false);
18551884
}
1885+
Self::BackgroundBlur { radius, box_blur, gamma } => {
1886+
visitor.parameter("radius", radius, 10.);
1887+
visitor.parameter("box_blur", box_blur, false);
1888+
visitor.parameter("gamma", gamma, false);
1889+
}
18561890
Self::DropShadow {
18571891
color,
18581892
blend_mode,
@@ -2193,6 +2227,23 @@ mod tests {
21932227
use super::*;
21942228
use vector_types::{Gradient, GradientStop};
21952229

2230+
#[test]
2231+
fn box_blurs_reach_their_half_width_and_a_pixel_while_gaussians_reach_three_radii() {
2232+
let blur = |box_blur| EffectOperation::Blur {
2233+
radius_mask: Graphic::default(),
2234+
radius: 6.,
2235+
box_blur,
2236+
gamma: false,
2237+
};
2238+
let background_blur = |box_blur| EffectOperation::BackgroundBlur { radius: 6., box_blur, gamma: false };
2239+
2240+
// Whether it blurs the content or what lies beneath it, a blur reaches as far as its kernel reads
2241+
for (box_blur, reach) in [(true, 7.), (false, 18.)] {
2242+
assert_eq!(blur(box_blur).reach(), reach, "box: {box_blur}");
2243+
assert_eq!(background_blur(box_blur).reach(), reach, "box: {box_blur}");
2244+
}
2245+
}
2246+
21962247
#[test]
21972248
fn rows_round_trip_their_operations_eliding_defaults() {
21982249
let operations = [
@@ -2780,6 +2831,16 @@ mod tests {
27802831
assert_eq!(runs, vec![0..3, 3..4]);
27812832
}
27822833

2834+
#[test]
2835+
fn effects_painting_only_within_the_content_paint_nothing_past_it() {
2836+
// An inner shadow reads past the content to find the edges it casts from, but paints only within it
2837+
let (inner_shadow, drop_shadow) = (EffectOperation::default_for(EffectKind::InnerShadow), EffectOperation::default_for(EffectKind::DropShadow));
2838+
let painted = |effects: Effects| effects.reach_around_by(DAffine2::IDENTITY, [DVec2::ZERO; 2], 0., |operation| !operation.paints_only_within_content());
2839+
assert!(Effects::from_operations([inner_shadow.clone()]).reach() > 0.);
2840+
assert_eq!(painted(Effects::from_operations([inner_shadow.clone()])), DVec2::ZERO);
2841+
assert_eq!(painted(Effects::from_operations([inner_shadow, drop_shadow.clone()])), DVec2::splat(drop_shadow.reach()));
2842+
}
2843+
27832844
#[test]
27842845
fn adjustment_rows_elide_their_defaults_and_name_grouped_parameters() {
27852846
use EffectKind::*;

‎node-graph/libraries/graphic-types/src/resampling.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,7 @@ impl EffectOperation {
17451745
| Self::HighPass { .. }
17461746
| Self::AddNoise { .. }
17471747
| Self::Dissolve { .. }
1748+
| Self::BackgroundBlur { .. }
17481749
| Self::Adjustment(_) => false,
17491750
}
17501751
}

‎node-graph/libraries/raster-kernels/src/raster_effects.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ fn evaluate(raster: &Raster<CPU>, transform: DAffine2, effects: &Effects, fill:
511511
};
512512
place_within(&mut pieces, piece(0), &mut full, &mut filled, painted, blend_mode);
513513
}
514+
// An image of its own has nothing beneath it to blur
515+
EffectOperation::BackgroundBlur { .. } => {}
514516
// A solid paint overlay paints within the content alone, like any other paint overlay
515517
EffectOperation::Adjustment(Adjustment::ColorOverlay { color, blend_mode, opacity }) => {
516518
let painted = premultiplied_gamma_channels(color.with_alpha(color.a() * (opacity / 100.) as f32));
@@ -955,7 +957,7 @@ fn composite_layers(
955957
PiecePlace::Within => SharePart::Whole,
956958
PiecePlace::IntoContent { over: false } => SharePart::IntoContent,
957959
PiecePlace::IntoContent { over: true } => SharePart::IntoContentBeneathPartOver { opacity: OPACITY, fade },
958-
PiecePlace::Beneath | PiecePlace::Through { .. } => continue,
960+
PiecePlace::Beneath | PiecePlace::Through { .. } | PiecePlace::Backdrop => continue,
959961
};
960962
content = composite_in_share(&content, tinted, full_content, piece.blend_mode, part);
961963
}
@@ -977,7 +979,8 @@ fn composite_layers(
977979
PiecePlace::Beneath => tinted.clone(),
978980
PiecePlace::Within if !holds_content => tinted.zip(full_content, |tinted, content| tinted.map(|channel| channel * content[3])),
979981
PiecePlace::Through { .. } => tinted.zip(&through_shares(full_content, &content, OPACITY, fade), |tinted, share| tinted.map(|channel| channel * share)),
980-
PiecePlace::Within | PiecePlace::IntoContent { .. } => continue,
982+
// What lies beneath, for a backdrop the image doesn't have
983+
PiecePlace::Within | PiecePlace::IntoContent { .. } | PiecePlace::Backdrop => continue,
981984
};
982985
stack = Some(stack_over(stack, upper));
983986
}

‎node-graph/libraries/rendering/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ log = { workspace = true }
3434
num-traits = { workspace = true }
3535
usvg = { workspace = true }
3636
kurbo = { workspace = true }
37+
linesweeper = { workspace = true }
3738
vector-types = { workspace = true }
3839
graphic-types = { workspace = true }
3940
raster-kernels = { workspace = true }

0 commit comments

Comments
 (0)