Merge branch 'wip/matthiasc/snapshot' into 'master'

Implement builder pattern for GtkSnapshot

See merge request GNOME/gtk!52
This commit is contained in:
Matthias Clasen 2018-03-11 05:39:00 +00:00
commit 527d265bea
17 changed files with 233 additions and 98 deletions

View File

@ -4166,6 +4166,13 @@ gtk_volume_button_get_type
<FILE>gtksnapshot</FILE>
<TITLE>GtkSnapshot</TITLE>
GtkSnapshot
gtk_snapshot_new
gtk_snapshot_ref
gtk_snapshot_unref
gtk_snapshot_to_node
gtk_snapshot_free_to_node
gtk_snapshot_get_renderer
gtk_snapshot_get_record_names
gtk_snapshot_push
gtk_snapshot_push_transform
gtk_snapshot_push_opacity

View File

@ -146,6 +146,7 @@ gtk_shortcuts_section_get_type
gtk_shortcuts_group_get_type
gtk_shortcuts_shortcut_get_type
gtk_size_group_get_type
gtk_snapshot_get_type
gtk_spin_button_get_type
gtk_spinner_get_type
gtk_stack_get_type

View File

@ -21,7 +21,7 @@
#include "gsk/gsk.h"
#include "gskpango.h"
#include "gtksnapshotprivate.h"
#include "gtksnapshot.h"
#include <math.h>
@ -155,7 +155,7 @@ gsk_pango_renderer_show_text_glyphs (PangoRenderer *renderer,
if (node == NULL)
return;
if (crenderer->snapshot->record_names)
if (gtk_snapshot_get_record_names (crenderer->snapshot))
{
char name[64];
g_snprintf (name, sizeof (name), "Glyphs<%d>", glyphs->num_glyphs);

View File

@ -22,7 +22,7 @@
#include "gtkcssimageprivate.h"
#include "gtkcssstyleprivate.h"
#include "gtksnapshotprivate.h"
#include "gtksnapshot.h"
/* for the types only */
#include "gtk/gtkcssimagecrossfadeprivate.h"
@ -219,7 +219,7 @@ _gtk_css_image_draw (GtkCssImage *image,
double width,
double height)
{
GtkSnapshot snapshot;
GtkSnapshot *snapshot;
GskRenderNode *node;
cairo_region_t *clip;
@ -231,9 +231,9 @@ _gtk_css_image_draw (GtkCssImage *image,
cairo_save (cr);
clip = cairo_region_create_rectangle (&(cairo_rectangle_int_t) { 0, 0, width, height });
gtk_snapshot_init (&snapshot, NULL, FALSE, clip, "Fallback<%s>", G_OBJECT_TYPE_NAME (image));
gtk_css_image_snapshot (image, &snapshot, width, height);
node = gtk_snapshot_finish (&snapshot);
snapshot = gtk_snapshot_new (NULL, FALSE, clip, "Fallback<%s>", G_OBJECT_TYPE_NAME (image));
gtk_css_image_snapshot (image, snapshot, width, height);
node = gtk_snapshot_free_to_node (snapshot);
if (node != NULL)
{

View File

@ -25,7 +25,7 @@
#include "gtk/gtkcssparserprivate.h"
#include "gtk/gtkcsstypesprivate.h"
#include "gtk/gtksnapshotprivate.h"
#include "gtk/gtksnapshot.h"
G_BEGIN_DECLS

View File

@ -24,7 +24,7 @@
#include "gtkcsscolorvalueprivate.h"
#include "gtkcssnumbervalueprivate.h"
#include "gtkcssrgbavalueprivate.h"
#include "gtksnapshotprivate.h"
#include "gtksnapshot.h"
#include "gtkstylecontextprivate.h"
#include "gtkpango.h"
@ -1053,7 +1053,7 @@ gtk_css_shadow_value_snapshot_outset (const GtkCssValue *shadow,
_gtk_css_number_value_get (shadow->voffset, 0),
_gtk_css_number_value_get (shadow->spread, 0),
_gtk_css_number_value_get (shadow->radius, 0));
if (snapshot->record_names)
if (gtk_snapshot_get_record_names (snapshot))
gsk_render_node_set_name (node, "Outset Shadow");
gtk_snapshot_append_node (snapshot, node);
gsk_render_node_unref (node);
@ -1084,7 +1084,7 @@ gtk_css_shadow_value_snapshot_inset (const GtkCssValue *shadow,
_gtk_css_number_value_get (shadow->voffset, 0),
_gtk_css_number_value_get (shadow->spread, 0),
_gtk_css_number_value_get (shadow->radius, 0));
if (snapshot->record_names)
if (gtk_snapshot_get_record_names (snapshot))
gsk_render_node_set_name (node, "Inset Shadow");
gtk_snapshot_append_node (snapshot, node);
gsk_render_node_unref (node);

View File

@ -41,7 +41,7 @@
#include "gtkprivate.h"
#include "gtkscrollable.h"
#include "gtksizerequest.h"
#include "gtksnapshotprivate.h"
#include "gtksnapshot.h"
#include "gtkstylecontextprivate.h"
#include "gtktreednd.h"
#include "gtktypebuiltins.h"
@ -6689,7 +6689,7 @@ gtk_icon_view_create_drag_icon (GtkIconView *icon_view,
GtkTreePath *path)
{
GtkWidget *widget;
GtkSnapshot snapshot;
GtkSnapshot *snapshot;
GskRenderNode *node;
cairo_t *cr;
cairo_surface_t *surface;
@ -6724,15 +6724,15 @@ gtk_icon_view_create_drag_icon (GtkIconView *icon_view,
rect.width,
rect.height);
gtk_snapshot_init (&snapshot, NULL, FALSE, NULL, "IconView DragIcon");
gtk_icon_view_snapshot_item (icon_view, &snapshot, item,
icon_view->priv->item_padding,
icon_view->priv->item_padding,
FALSE);
node = gtk_snapshot_finish (&snapshot);
snapshot = gtk_snapshot_new (NULL, FALSE, NULL, "IconView DragIcon");
gtk_icon_view_snapshot_item (icon_view, snapshot, item,
icon_view->priv->item_padding,
icon_view->priv->item_padding,
FALSE);
node = gtk_snapshot_free_to_node (snapshot);
cr = cairo_create (surface);
gsk_render_node_draw (node, cr);
gsk_render_node_draw (node, cr);
cairo_destroy (cr);
return surface;

View File

@ -19,7 +19,7 @@
#include "gtk/gtk.h"
#include "gtkmagnifierprivate.h"
#include "gtkwidgetprivate.h"
#include "gtksnapshotprivate.h"
#include "gtksnapshot.h"
#include "gtkintl.h"
enum {
@ -103,7 +103,7 @@ gtk_magnifier_snapshot (GtkWidget *widget,
{
GtkMagnifier *magnifier = GTK_MAGNIFIER (widget);
GtkMagnifierPrivate *priv = _gtk_magnifier_get_instance_private (magnifier);
GtkSnapshot inspected_snapshot;
GtkSnapshot *inspected_snapshot;
GskRenderNode *inspected_node;
graphene_matrix_t transform;
@ -115,14 +115,13 @@ gtk_magnifier_snapshot (GtkWidget *widget,
g_signal_handler_block (priv->inspected, priv->draw_handler);
gtk_snapshot_init (&inspected_snapshot,
gtk_snapshot_get_renderer (snapshot),
snapshot->record_names,
NULL,
"MagnifierSnapshot");
inspected_snapshot = gtk_snapshot_new (gtk_snapshot_get_renderer (snapshot),
gtk_snapshot_get_record_names (snapshot),
NULL,
"MagnifierSnapshot");
gtk_widget_snapshot (priv->inspected, &inspected_snapshot);
inspected_node = gtk_snapshot_finish (&inspected_snapshot);
gtk_widget_snapshot (priv->inspected, inspected_snapshot);
inspected_node = gtk_snapshot_free_to_node (inspected_snapshot);
if (inspected_node != NULL)
{

View File

@ -25,7 +25,7 @@
#include "gtkscrolledwindow.h"
#include "gtkwidgetprivate.h"
#include "gtkmarshalers.h"
#include "gtksnapshotprivate.h"
#include "gtksnapshot.h"
#include "gtkprivate.h"
#include "gtkintl.h"
@ -673,17 +673,16 @@ gtk_overlay_snapshot (GtkWidget *widget,
if (main_widget_node == NULL)
{
GtkSnapshot child_snapshot;
GtkSnapshot *child_snapshot;
gtk_snapshot_init (&child_snapshot,
gtk_snapshot_get_renderer (snapshot),
snapshot->record_names,
NULL,
"OverlayCaptureMainChild");
gtk_snapshot_offset (&child_snapshot, main_alloc.x, main_alloc.y);
gtk_widget_snapshot (main_widget, &child_snapshot);
gtk_snapshot_offset (&child_snapshot, -main_alloc.x, -main_alloc.y);
main_widget_node = gtk_snapshot_finish (&child_snapshot);
child_snapshot = gtk_snapshot_new (gtk_snapshot_get_renderer (snapshot),
gtk_snapshot_get_record_names (snapshot),
NULL,
"OverlayCaptureMainChild");
gtk_snapshot_offset (child_snapshot, main_alloc.x, main_alloc.y);
gtk_widget_snapshot (main_widget, child_snapshot);
gtk_snapshot_offset (child_snapshot, -main_alloc.x, -main_alloc.y);
main_widget_node = gtk_snapshot_free_to_node (child_snapshot);
graphene_matrix_init_translate (&translate, &GRAPHENE_POINT3D_INIT (main_alloc.x,main_alloc.y, 0));
}

View File

@ -438,7 +438,7 @@ snapshot_frame_fill (GtkSnapshot *snapshot,
gsk_rounded_rect_offset (&offset_outline, off_x, off_y);
node = gsk_border_node_new (&offset_outline, border_width, colors);
if (snapshot->record_names)
if (gtk_snapshot_get_record_names (snapshot))
gsk_render_node_set_name (node, "Border");
gtk_snapshot_append_node (snapshot, node);
gsk_render_node_unref (node);

View File

@ -28,7 +28,7 @@
#include "gtkcssstyleprivate.h"
#include "gtkcsstransformvalueprivate.h"
#include "gtkiconthemeprivate.h"
#include "gtksnapshotprivate.h"
#include "gtksnapshot.h"
#include <math.h>

View File

@ -46,10 +46,57 @@
* operates on. Use the gtk_snapshot_push() and gtk_snapshot_pop() functions to
* change the current node.
*
* The only way to obtain a #GtkSnapshot object is as an argument to
* the #GtkWidget::snapshot vfunc.
* The typical way to obtain a #GtkSnapshot object is as an argument to
* the #GtkWidget::snapshot vfunc. If you need to create your own GtkSnapshot,
* use gtk_snapshot_new().
*/
G_DEFINE_BOXED_TYPE (GtkSnapshot, gtk_snapshot, gtk_snapshot_ref, gtk_snapshot_unref)
/**
* gtk_snapshot_ref:
* @snapshot: a #GtkSnapshot
*
* Increase the reference count of @snapshot by 1.
*
* Returns: the @snapshot
*/
GtkSnapshot *
gtk_snapshot_ref (GtkSnapshot *snapshot)
{
g_assert (snapshot->ref_count > 0);
snapshot->ref_count += 1;
return snapshot;
}
/**
* gtk_snapshot_unref:
* @snapshot: a #GtkSnapshot
*
* Decrease the reference count of @snapshot by 1 and
* free the object if the count drops to 0.
*/
void
gtk_snapshot_unref (GtkSnapshot *snapshot)
{
g_assert (snapshot->ref_count > 0);
snapshot->ref_count -= 1;
if (snapshot->ref_count > 0)
return;
if (snapshot->state_stack)
gsk_render_node_unref (gtk_snapshot_to_node (snapshot));
g_assert (snapshot->state_stack == NULL);
g_assert (snapshot->nodes == NULL);
g_free (snapshot);
}
static GskRenderNode *
gtk_snapshot_collect_default (GtkSnapshot *snapshot,
GtkSnapshotState *state,
@ -125,22 +172,28 @@ gtk_snapshot_state_clear (GtkSnapshotState *state)
g_clear_pointer (&state->name, g_free);
}
void
gtk_snapshot_init (GtkSnapshot *snapshot,
GskRenderer *renderer,
gboolean record_names,
const cairo_region_t *clip,
const char *name,
...)
/**
* gtk_snapshot_new:
* @renderer: the #GskRenderer to create nodes for
* @record_names: whether to keep node names (for debugging purposes)
* @clip: the clip region to use, or %NULL
* @name: a printf-style format string to create the node name
* @...: arguments for @name
*
* Creates a new #GtkSnapshot.
*
* Returns: a newly-allocated #GtkSnapshot
*/
GtkSnapshot *
gtk_snapshot_new (GskRenderer *renderer,
gboolean record_names,
const cairo_region_t *clip,
const char *name,
...)
{
GtkSnapshot *snapshot;
char *str;
snapshot->record_names = record_names;
snapshot->renderer = renderer;
snapshot->state_stack = g_array_new (FALSE, TRUE, sizeof (GtkSnapshotState));
g_array_set_clear_func (snapshot->state_stack, (GDestroyNotify)gtk_snapshot_state_clear);
snapshot->nodes = g_ptr_array_new_with_free_func ((GDestroyNotify)gsk_render_node_unref);
if (name && record_names)
{
va_list args;
@ -152,11 +205,42 @@ gtk_snapshot_init (GtkSnapshot *snapshot,
else
str = NULL;
snapshot = g_new (GtkSnapshot, 1);
snapshot->ref_count = 1;
snapshot->record_names = record_names;
snapshot->renderer = renderer;
snapshot->state_stack = g_array_new (FALSE, TRUE, sizeof (GtkSnapshotState));
g_array_set_clear_func (snapshot->state_stack, (GDestroyNotify)gtk_snapshot_state_clear);
snapshot->nodes = g_ptr_array_new_with_free_func ((GDestroyNotify)gsk_render_node_unref);
gtk_snapshot_push_state (snapshot,
str,
(cairo_region_t *) clip,
0, 0,
gtk_snapshot_collect_default);
return snapshot;
}
/**
* gtk_snapshot_free_to_node:
* @snapshot: a #GtkSnapshot
*
* Returns the node that was constructed by @snapshot
* and frees @snapshot.
*
* Returns: a newly-created #GskRenderNode
*/
GskRenderNode *
gtk_snapshot_free_to_node (GtkSnapshot *snapshot)
{
GskRenderNode *result;
result = gtk_snapshot_to_node (snapshot);
gtk_snapshot_unref (snapshot);
return result;
}
/**
@ -1067,8 +1151,20 @@ gtk_snapshot_pop_internal (GtkSnapshot *snapshot)
return node;
}
/**
* gtk_snapshot_to_node:
* @snapshot: a #GtkSnapshot
*
* Returns the render node that was constructed
* by @snapshot. After calling this function, it
* is no longer possible to add more nodes to
* @snapshot. The only function that should be
* called after this is gtk_snapshot_unref().
*
* Returns: the constructed #GskRenderNode
*/
GskRenderNode *
gtk_snapshot_finish (GtkSnapshot *snapshot)
gtk_snapshot_to_node (GtkSnapshot *snapshot)
{
GskRenderNode *result;
@ -1089,7 +1185,11 @@ gtk_snapshot_finish (GtkSnapshot *snapshot)
result = gtk_snapshot_pop_internal (snapshot);
g_array_free (snapshot->state_stack, TRUE);
snapshot->state_stack = NULL;
g_ptr_array_free (snapshot->nodes, TRUE);
snapshot->nodes = NULL;
return result;
}
@ -1123,11 +1223,26 @@ gtk_snapshot_pop (GtkSnapshot *snapshot)
* Returns: (transfer none): the #GskRenderer
*/
GskRenderer *
gtk_snapshot_get_renderer (const GtkSnapshot *snapshot)
gtk_snapshot_get_renderer (GtkSnapshot *snapshot)
{
return snapshot->renderer;
}
/**
* gtk_snapshot_get_record_names:
* @snapshot: a #GtkSnapshot
*
* Obtains whether the snapshot is recording names
* for debugging.
*
* Returns: whether the snapshot records names
*/
gboolean
gtk_snapshot_get_record_names (GtkSnapshot *snapshot)
{
return snapshot->record_names;
}
/**
* gtk_snapshot_offset:
* @snapshot: a $GtkSnapshot

View File

@ -36,6 +36,31 @@
G_BEGIN_DECLS
GDK_AVAILABLE_IN_ALL
GType gtk_snapshot_get_type (void) G_GNUC_CONST;
GDK_AVAILABLE_IN_ALL
GtkSnapshot * gtk_snapshot_ref (GtkSnapshot *snapshot);
GDK_AVAILABLE_IN_ALL
void gtk_snapshot_unref (GtkSnapshot *snapshot);
GDK_AVAILABLE_IN_ALL
GtkSnapshot * gtk_snapshot_new (GskRenderer *renderer,
gboolean record_names,
const cairo_region_t *clip,
const char *name,
...) G_GNUC_PRINTF (4, 5);
GDK_AVAILABLE_IN_ALL
GskRenderNode * gtk_snapshot_free_to_node (GtkSnapshot *snapshot);
GDK_AVAILABLE_IN_ALL
GskRenderNode * gtk_snapshot_to_node (GtkSnapshot *snapshot);
GDK_AVAILABLE_IN_ALL
GskRenderer * gtk_snapshot_get_renderer (GtkSnapshot *snapshot);
GDK_AVAILABLE_IN_ALL
gboolean gtk_snapshot_get_record_names (GtkSnapshot *snapshot);
GDK_AVAILABLE_IN_ALL
void gtk_snapshot_push (GtkSnapshot *snapshot,
gboolean keep_coordinates,

View File

@ -81,22 +81,13 @@ struct _GtkSnapshotState {
};
struct _GtkSnapshot {
int ref_count;
gboolean record_names;
GskRenderer *renderer;
GArray *state_stack;
GPtrArray *nodes;
};
void gtk_snapshot_init (GtkSnapshot *state,
GskRenderer *renderer,
gboolean record_names,
const cairo_region_t *clip,
const char *name,
...) G_GNUC_PRINTF (5, 6);
GskRenderNode * gtk_snapshot_finish (GtkSnapshot *state);
GskRenderer * gtk_snapshot_get_renderer (const GtkSnapshot *snapshot);
G_END_DECLS
#endif /* __GTK_SNAPSHOT_PRIVATE_H__ */

View File

@ -28,7 +28,7 @@
#include "gtkcontainerprivate.h"
#include "gtkprogresstrackerprivate.h"
#include "gtksettingsprivate.h"
#include "gtksnapshotprivate.h"
#include "gtksnapshot.h"
#include "gtkwidgetprivate.h"
#include "a11y/gtkstackaccessible.h"
#include "a11y/gtkstackaccessibleprivate.h"
@ -1952,17 +1952,16 @@ gtk_stack_snapshot (GtkWidget *widget,
if (priv->last_visible_node == NULL &&
priv->last_visible_child != NULL)
{
GtkSnapshot last_visible_snapshot;
GtkSnapshot *last_visible_snapshot;
gtk_widget_get_allocation (priv->last_visible_child->widget,
&priv->last_visible_surface_allocation);
gtk_snapshot_init (&last_visible_snapshot,
gtk_snapshot_get_renderer (snapshot),
snapshot->record_names,
NULL,
"StackCaptureLastVisibleChild");
gtk_widget_snapshot (priv->last_visible_child->widget, &last_visible_snapshot);
priv->last_visible_node = gtk_snapshot_finish (&last_visible_snapshot);
last_visible_snapshot = gtk_snapshot_new (gtk_snapshot_get_renderer (snapshot),
gtk_snapshot_get_record_names (snapshot),
NULL,
"StackCaptureLastVisibleChild");
gtk_widget_snapshot (priv->last_visible_child->widget, last_visible_snapshot);
priv->last_visible_node = gtk_snapshot_free_to_node (last_visible_snapshot);
}
gtk_snapshot_push_clip (snapshot,

View File

@ -49,7 +49,7 @@
#include "gtkrendericonprivate.h"
#include "gtkscrollable.h"
#include "gtksettingsprivate.h"
#include "gtksnapshotprivate.h"
#include "gtksnapshot.h"
#include "gtkstylecontextprivate.h"
#include "gtktooltip.h"
#include "gtktreednd.h"
@ -13821,7 +13821,7 @@ gtk_tree_view_create_row_drag_icon (GtkTreeView *tree_view,
GList *list;
GdkRectangle background_area;
GtkWidget *widget;
GtkSnapshot snapshot;
GtkSnapshot *snapshot;
GskRenderNode *rendernode;
gint depth;
/* start drawing inside the black outline */
@ -13871,9 +13871,9 @@ gtk_tree_view_create_row_drag_icon (GtkTreeView *tree_view,
bin_window_width + 2,
background_area.height + 2);
gtk_snapshot_init (&snapshot, NULL, FALSE, NULL, "TreeView DragIcon");
snapshot = gtk_snapshot_new (NULL, FALSE, NULL, "TreeView DragIcon");
gtk_snapshot_render_background (&snapshot, context,
gtk_snapshot_render_background (snapshot, context,
0, 0,
bin_window_width + 2,
background_area.height + 2);
@ -13924,7 +13924,7 @@ gtk_tree_view_create_row_drag_icon (GtkTreeView *tree_view,
gtk_style_context_add_class (context, GTK_STYLE_CLASS_SEPARATOR);
gtk_style_context_get_color (context, &color);
gtk_snapshot_append_color (&snapshot,
gtk_snapshot_append_color (snapshot,
&color,
&GRAPHENE_RECT_INIT(
cell_area.x,
@ -13939,7 +13939,7 @@ gtk_tree_view_create_row_drag_icon (GtkTreeView *tree_view,
else
{
gtk_tree_view_column_cell_snapshot (column,
&snapshot,
snapshot,
&background_area,
&cell_area,
0, FALSE);
@ -13948,7 +13948,7 @@ gtk_tree_view_create_row_drag_icon (GtkTreeView *tree_view,
cell_offset += gtk_tree_view_column_get_width (column);
}
rendernode = gtk_snapshot_finish (&snapshot);
rendernode = gtk_snapshot_free_to_node (snapshot);
cr = cairo_create (surface);

View File

@ -60,7 +60,7 @@
#include "gtkselection.h"
#include "gtksettingsprivate.h"
#include "gtksizegroup-private.h"
#include "gtksnapshotprivate.h"
#include "gtksnapshot.h"
#include "gtkstylecontextprivate.h"
#include "gtktooltipprivate.h"
#include "gtktypebuiltins.h"
@ -5459,7 +5459,7 @@ gtk_widget_draw_internal (GtkWidget *widget,
if (mode == RENDER_SNAPSHOT)
{
GskRenderer *renderer = gtk_widget_get_renderer (widget);
GtkSnapshot snapshot;
GtkSnapshot *snapshot;
cairo_region_t *clip;
GskRenderNode *node;
@ -5468,9 +5468,9 @@ gtk_widget_draw_internal (GtkWidget *widget,
widget->priv->clip.y - widget->priv->allocation.y,
widget->priv->clip.width,
widget->priv->clip.height});
gtk_snapshot_init (&snapshot, renderer, FALSE, clip, "Fallback<%s>", G_OBJECT_TYPE_NAME (widget));
gtk_widget_snapshot (widget, &snapshot);
node = gtk_snapshot_finish (&snapshot);
snapshot = gtk_snapshot_new (renderer, FALSE, clip, "Fallback<%s>", G_OBJECT_TYPE_NAME (widget));
gtk_widget_snapshot (widget, snapshot);
node = gtk_snapshot_free_to_node (snapshot);
if (node != NULL)
{
gsk_render_node_draw (node, cr);
@ -13981,7 +13981,7 @@ gtk_widget_render (GtkWidget *widget,
const cairo_region_t *region)
{
GdkDrawingContext *context;
GtkSnapshot snapshot;
GtkSnapshot *snapshot;
GskRenderer *renderer;
GskRenderNode *root;
cairo_region_t *clip;
@ -13997,14 +13997,13 @@ gtk_widget_render (GtkWidget *widget,
context = gsk_renderer_begin_draw_frame (renderer, region);
clip = gdk_drawing_context_get_clip (context);
gtk_snapshot_init (&snapshot,
renderer,
should_record_names (widget, renderer),
clip,
"Render<%s>", G_OBJECT_TYPE_NAME (widget));
snapshot = gtk_snapshot_new (renderer,
should_record_names (widget, renderer),
clip,
"Render<%s>", G_OBJECT_TYPE_NAME (widget));
cairo_region_destroy (clip);
gtk_widget_snapshot (widget, &snapshot);
root = gtk_snapshot_finish (&snapshot);
gtk_widget_snapshot (widget, snapshot);
root = gtk_snapshot_free_to_node (snapshot);
if (root != NULL)
{
gtk_inspector_record_render (widget,