mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-05 19:12:08 -10:00
Fix the 'Clipping Mask' node not clipping endless color, gradient, and generator layers (#5072)
This commit is contained in:
1 parent
41586d6967
commit
01c5455ecf
3 files changed
+160
-10
No files matched your search
@@ -5,10 +5,14 @@ use crate::test_utils::gpu_executor;
|
||||
use crate::test_utils::test_prelude::*;
|
||||
use glam::{DAffine2, UVec2};
|
||||
use graph_craft::document::value::TaggedValue;
|
||||
use graphene_std::application_io::Texture;
|
||||
use graphene_std::blending_nodes::effects::{blur, drop_shadow};
|
||||
use graphene_std::graphic::{IntoGraphicList, extend};
|
||||
use graphene_std::list::{Item, List};
|
||||
use graphene_std::math_nodes::{color_value, gradient_value};
|
||||
use graphene_std::raster_nodes::std_nodes::noise_pattern;
|
||||
use graphene_std::renderer::{Render, RenderParams, RenderSvgSegmentList, SvgRender};
|
||||
use graphene_std::vector::style::RenderMode;
|
||||
use graphene_std::{Graphic, NodeParameter, ProtoNodeIdentifier, Vector};
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
@@ -136,6 +140,111 @@ fn assert_clip(name: &str, graphic: &Graphic, blurred: bool) {
|
||||
assert_eq!(pixel(170, 90)[3], 0, "{name}: the red is clipped away well past the base");
|
||||
}
|
||||
|
||||
/// Endless content a layer can hold, which fills the whole canvas unless something clips it.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum Endless {
|
||||
Color,
|
||||
Gradient,
|
||||
NoisePattern,
|
||||
}
|
||||
|
||||
/// A document viewed in the `render_mode` with a blue rectangle filling (50, 50) to (130, 130).
|
||||
async fn document_with_a_rectangle(render_mode: RenderMode) -> EditorTestUtils {
|
||||
let mut editor = EditorTestUtils::create();
|
||||
editor.new_document().await;
|
||||
editor.handle_message(DocumentMessage::SetRenderMode { render_mode }).await;
|
||||
|
||||
let blue = Color::from_rgbaf32_unchecked(0., 0., 1., 1.);
|
||||
editor.select_primary_color(blue).await;
|
||||
editor.select_secondary_color(blue).await;
|
||||
editor.draw_rect(50., 50., 130., 130.).await;
|
||||
|
||||
editor
|
||||
}
|
||||
|
||||
/// Adds a new layer of `endless` content above the selected layer, clipped to it as by an Alt-click between them.
|
||||
async fn add_clipped_endless_layer(editor: &mut EditorTestUtils, endless: Endless) {
|
||||
editor.handle_message(DocumentMessage::CreateEmptyFolder).await;
|
||||
let layer = editor.get_selected_layer().await.expect("the new layer is selected");
|
||||
let node = match endless {
|
||||
Endless::Color => color_value::IDENTIFIER,
|
||||
Endless::Gradient => gradient_value::IDENTIFIER,
|
||||
Endless::NoisePattern => noise_pattern::IDENTIFIER,
|
||||
};
|
||||
let node_type = DefinitionIdentifier::ProtoNode(node);
|
||||
editor.handle_message(NodeGraphMessage::CreateNodeInLayerWithTransaction { node_type, layer }).await;
|
||||
editor.handle_message(DocumentMessage::ClipLayer { id: layer.to_node() }).await;
|
||||
}
|
||||
|
||||
/// Evaluates the document for a viewport of `size` as the app does, returning what the frontend is sent and the frame the runtime presents.
|
||||
async fn evaluate_viewport(editor: &mut EditorTestUtils, size: UVec2) -> (Vec<FrontendMessage>, Option<Texture>) {
|
||||
let portfolio = editor.portfolio_message_handler_mut();
|
||||
let document_id = portfolio.active_document_id.expect("a document is open");
|
||||
let (executor, documents) = (&mut portfolio.executor, &mut portfolio.documents);
|
||||
let document = documents.get_mut(&document_id).expect("the active document exists");
|
||||
let submitted = executor
|
||||
.submit_node_graph_evaluation(document, document_id, size, 1., Default::default(), Vec::new(), true, DVec2::ZERO)
|
||||
.expect("the evaluation is submitted");
|
||||
|
||||
let mut messages = editor.dispatch(submitted);
|
||||
editor.runtime.update();
|
||||
let frame = editor.runtime.run().await;
|
||||
messages.extend(editor.dispatch(Message::Wake));
|
||||
|
||||
(messages, frame)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn endless_layers_clip_to_the_shape_beneath_them() {
|
||||
let size = UVec2::new(256, 256);
|
||||
|
||||
let frame_pixels = async |editor: &mut EditorTestUtils| {
|
||||
let (_, frame) = evaluate_viewport(editor, size).await;
|
||||
let frame = frame.expect("the viewport presents a frame");
|
||||
gpu_executor().read_texture_pixels(&frame).expect("the frame reads back")
|
||||
};
|
||||
let pixel = |pixels: &[u8], x: u32, y: u32| {
|
||||
let index = (y * size.x + x) as usize * 4;
|
||||
[pixels[index], pixels[index + 1], pixels[index + 2], pixels[index + 3]]
|
||||
};
|
||||
|
||||
for endless in [Endless::Color, Endless::Gradient, Endless::NoisePattern] {
|
||||
// In the viewport's Vello render, the endless content covers the rectangle but leaves the canvas past it as the rectangle alone left it
|
||||
let mut editor = document_with_a_rectangle(RenderMode::Normal).await;
|
||||
let rectangle_alone = frame_pixels(&mut editor).await;
|
||||
add_clipped_endless_layer(&mut editor, endless).await;
|
||||
let clipped = frame_pixels(&mut editor).await;
|
||||
|
||||
assert_ne!(pixel(&clipped, 90, 90), pixel(&rectangle_alone, 90, 90), "{endless:?}: the content shows within the rectangle");
|
||||
for (x, y) in [(20, 20), (200, 90), (90, 200), (230, 230)] {
|
||||
assert_eq!(
|
||||
pixel(&clipped, x, y),
|
||||
pixel(&rectangle_alone, x, y),
|
||||
"{endless:?}: the content is clipped away at ({x}, {y}), past the rectangle"
|
||||
);
|
||||
}
|
||||
|
||||
// In SVG Preview, the endless content is drawn only within the rectangle's mask
|
||||
let mut editor = document_with_a_rectangle(RenderMode::SvgPreview).await;
|
||||
add_clipped_endless_layer(&mut editor, endless).await;
|
||||
let (messages, _) = evaluate_viewport(&mut editor, size).await;
|
||||
let svg = messages
|
||||
.into_iter()
|
||||
.find_map(|message| match message {
|
||||
FrontendMessage::UpdateDocumentArtwork { svg } => Some(svg),
|
||||
_ => None,
|
||||
})
|
||||
.expect("the viewport's artwork is sent as SVG");
|
||||
|
||||
let (unmasked, masked) = svg.split_once(r#"mask="url(#mask-"#).unwrap_or_else(|| panic!("{endless:?}: the content is masked: {svg}"));
|
||||
let draws_endless_content = |markup: &str| ["<polyline", "<foreignObject"].iter().any(|tag| markup.contains(tag));
|
||||
assert!(
|
||||
!draws_endless_content(unmasked) && draws_endless_content(masked),
|
||||
"{endless:?}: the content is drawn within the mask: {svg}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn clipping_bases_clip_by_their_filters_but_not_the_shadows_they_cast() {
|
||||
for (effect_name, effect, blurred) in [("Blur", blur::IDENTIFIER, true), ("Drop Shadow", drop_shadow::IDENTIFIER, false)] {
|
||||
|
||||
@@ -3732,21 +3732,22 @@ fn render_graphic_item_to_vello<'a>(
|
||||
*mask_element_and_transform = None;
|
||||
}
|
||||
|
||||
if let Some(rect) = layer_rect {
|
||||
scene.push_layer(peniko::Fill::NonZero, peniko::Mix::Normal, 1., kurbo::Affine::IDENTITY, &rect);
|
||||
// Content without edges, like a color layer, is clipped in layers covering the whole render
|
||||
let push_layer = |scene: &mut VelloSceneBuilder, blend_mode: peniko::BlendMode| match layer_rect {
|
||||
Some(rect) => scene.push_layer(peniko::Fill::NonZero, blend_mode, 1., kurbo::Affine::IDENTITY, &rect),
|
||||
None => scene.push_viewport_layer(blend_mode, 1.),
|
||||
};
|
||||
let clipped = layer_rect.is_some() || bounds == RenderBoundingBox::Infinite;
|
||||
|
||||
if clipped {
|
||||
push_layer(scene, peniko::Mix::Normal.into());
|
||||
mask_element.render_to_vello(scene, transform_mask, &render_params.for_clipper());
|
||||
scene.push_layer(
|
||||
peniko::Fill::NonZero,
|
||||
peniko::BlendMode::new(peniko::Mix::Normal, peniko::Compose::SrcIn),
|
||||
1.,
|
||||
kurbo::Affine::IDENTITY,
|
||||
&rect,
|
||||
);
|
||||
push_layer(scene, peniko::BlendMode::new(peniko::Mix::Normal, peniko::Compose::SrcIn));
|
||||
}
|
||||
|
||||
draw_content(scene);
|
||||
|
||||
if layer_rect.is_some() {
|
||||
if clipped {
|
||||
scene.pop_layer();
|
||||
scene.pop_layer();
|
||||
}
|
||||
|
||||
@@ -1376,6 +1376,46 @@ fn a_clipping_base_clips_by_its_blur_but_not_by_its_shadow() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn endless_layers_clip_to_the_shape_beneath_them() {
|
||||
use core_types::list::{ATTR_APPEARANCE, Item, List};
|
||||
use core_types::{ATTR_CLIPPING_MASK, Color as GraphicColor};
|
||||
use graphic_types::generator::Generator;
|
||||
use graphic_types::vector_types::gradient::Gradient;
|
||||
use graphic_types::{Appearance, Coverage, Graphic, Vector};
|
||||
use rendering::{Render, RenderParams};
|
||||
use vello_gpu::kurbo::Shape;
|
||||
|
||||
let size = UVec2::new(128, 128);
|
||||
let mut square = Vector::item_from_bezpath(&Rect::new(30., 30., 80., 80.).to_path(0.1));
|
||||
square.set_attribute(
|
||||
ATTR_APPEARANCE,
|
||||
Appearance::new_single(Coverage::new_fill(), Graphic::ColorList(List::new_from_element(GraphicColor::WHITE))),
|
||||
);
|
||||
let base = Item::new_from_element(Graphic::VectorList(List::new_from_item(square)));
|
||||
|
||||
// Layers of a color, a gradient, and noise, each clipped to a square beneath it
|
||||
let layers = [
|
||||
("color", Graphic::from(Item::new_from_element(GraphicColor::RED).with_attribute(ATTR_CLIPPING_MASK, true))),
|
||||
(
|
||||
"gradient",
|
||||
Graphic::from(Item::new_from_element(Gradient::from(vec![GraphicColor::RED, GraphicColor::BLUE])).with_attribute(ATTR_CLIPPING_MASK, true)),
|
||||
),
|
||||
("noise", Graphic::from(Item::new_from_element(Generator::Noise).with_attribute(ATTR_CLIPPING_MASK, true))),
|
||||
];
|
||||
for (name, layer) in layers {
|
||||
let graphic = Graphic::GraphicList([base.clone(), Item::new_from_element(layer)].into_iter().collect());
|
||||
let Some(pixels) = render(size, |scene| graphic.render_to_vello(scene, DAffine2::IDENTITY, &RenderParams::default())) else {
|
||||
return;
|
||||
};
|
||||
|
||||
assert_eq!(pixel(&pixels, size, 55, 55)[3], 255, "{name}: the layer shows within the square");
|
||||
for (x, y) in [(10, 10), (100, 55), (55, 100), (120, 120)] {
|
||||
assert_eq!(pixel(&pixels, size, x, y)[3], 0, "{name}: the layer is clipped away at ({x}, {y}), past the square");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn feathers_fade_the_content_to_nothing_at_its_edges() {
|
||||
use graphic_types::EffectOperation;
|
||||
|
||||
Reference in new issue
Block a user