[gtk/path-work-rebased: 28/149] snapshot: Add gtk_snapshot_push_fill()




commit 92d5dcb37e3e22cf10d2e87e80688894d33857d1
Author: Benjamin Otte <otte redhat com>
Date:   Wed Nov 11 01:17:42 2020 +0100

    snapshot: Add gtk_snapshot_push_fill()

 gtk/gtksnapshot.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 gtk/gtksnapshot.h |  4 +++
 2 files changed, 76 insertions(+), 3 deletions(-)
---
diff --git a/gtk/gtksnapshot.c b/gtk/gtksnapshot.c
index cdf61845d5..dff33a5b13 100644
--- a/gtk/gtksnapshot.c
+++ b/gtk/gtksnapshot.c
@@ -109,6 +109,10 @@ struct _GtkSnapshotState {
     struct {
       GskRoundedRect bounds;
     } rounded_clip;
+    struct {
+      GskPath *path;
+      GskFillRule fill_rule;
+    } fill;
     struct {
       gsize n_shadows;
       GskShadow *shadows;
@@ -761,7 +765,7 @@ gtk_snapshot_ensure_translate (GtkSnapshot *snapshot,
       gtk_snapshot_autopush_transform (snapshot);
       state = gtk_snapshot_get_current_state (snapshot);
     }
-  
+
   gsk_transform_to_translate (state->transform, dx, dy);
 }
 
@@ -850,7 +854,7 @@ gtk_snapshot_push_clip (GtkSnapshot           *snapshot,
 {
   GtkSnapshotState *state;
   float scale_x, scale_y, dx, dy;
- 
+
   gtk_snapshot_ensure_affine (snapshot, &scale_x, &scale_y, &dx, &dy);
 
   state = gtk_snapshot_push_state (snapshot,
@@ -1096,6 +1100,71 @@ gtk_snapshot_push_rounded_clip (GtkSnapshot          *snapshot,
   gsk_rounded_rect_scale_affine (&state->data.rounded_clip.bounds, bounds, scale_x, scale_y, dx, dy);
 }
 
+static GskRenderNode *
+gtk_snapshot_collect_fill (GtkSnapshot      *snapshot,
+                           GtkSnapshotState *state,
+                           GskRenderNode   **nodes,
+                           guint             n_nodes)
+{
+  GskRenderNode *node, *fill_node;
+
+  node = gtk_snapshot_collect_default (snapshot, state, nodes, n_nodes);
+  if (node == NULL)
+    return NULL;
+
+  fill_node = gsk_fill_node_new (node,
+                                 state->data.fill.path,
+                                 state->data.fill.fill_rule);
+
+  if (fill_node->bounds.size.width == 0 ||
+      fill_node->bounds.size.height == 0)
+    {
+      gsk_render_node_unref (node);
+      gsk_render_node_unref (fill_node);
+      return NULL;
+    }
+
+  gsk_render_node_unref (node);
+
+  return fill_node;
+}
+
+static void
+gtk_snapshot_clear_fill (GtkSnapshotState *state)
+{
+  gsk_path_unref (state->data.fill.path);
+}
+
+/**
+ * gtk_snapshot_push_fill:
+ * @snapshot: a `GtkSnapshot`
+ * @path: The path describing the area to fill
+ * @fill_rule: The fill rule to use
+ *
+ * Fills the area given by @path and @fill_rule with an image and discards everything
+ * outside of it.
+ *
+ * The image is recorded until the next call to [method Gtk Snapshot pop].
+ */
+void
+gtk_snapshot_push_fill (GtkSnapshot *snapshot,
+                        GskPath     *path,
+                        GskFillRule  fill_rule)
+{
+  GtkSnapshotState *state;
+
+  /* FIXME: Is it worth calling ensure_affine() and transforming the path here? */
+  gtk_snapshot_ensure_identity (snapshot);
+
+  state = gtk_snapshot_push_state (snapshot,
+                                   gtk_snapshot_get_current_state (snapshot)->transform,
+                                   gtk_snapshot_collect_fill,
+                                   gtk_snapshot_clear_fill);
+
+  state->data.fill.path = gsk_path_ref (path);
+  state->data.fill.fill_rule = fill_rule;
+}
+
 static GskRenderNode *
 gtk_snapshot_collect_shadow (GtkSnapshot      *snapshot,
                              GtkSnapshotState *state,
@@ -2517,7 +2586,7 @@ gtk_snapshot_append_border (GtkSnapshot          *snapshot,
   gsk_rounded_rect_scale_affine (&real_outline, outline, scale_x, scale_y, dx, dy);
 
   node = gsk_border_node_new (&real_outline,
-                              (float[4]) { 
+                              (float[4]) {
                                 border_width[0] * scale_y,
                                 border_width[1] * scale_x,
                                 border_width[2] * scale_y,
diff --git a/gtk/gtksnapshot.h b/gtk/gtksnapshot.h
index 6125e44ecc..45592c963c 100644
--- a/gtk/gtksnapshot.h
+++ b/gtk/gtksnapshot.h
@@ -89,6 +89,10 @@ GDK_AVAILABLE_IN_ALL
 void            gtk_snapshot_push_rounded_clip          (GtkSnapshot            *snapshot,
                                                          const GskRoundedRect   *bounds);
 GDK_AVAILABLE_IN_ALL
+void            gtk_snapshot_push_fill                  (GtkSnapshot            *snapshot,
+                                                         GskPath                *path,
+                                                         GskFillRule             fill_rule);
+GDK_AVAILABLE_IN_ALL
 void            gtk_snapshot_push_shadow                (GtkSnapshot            *snapshot,
                                                          const GskShadow        *shadow,
                                                          gsize                   n_shadows);


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