css: Pass boxes to the render functions

Instead of style + rect_of_one_box, pass the new GtkCssBoxes object.
This has the nice side effect that when drawing background + border +
outline, we only compute all the boxes we need once.
This commit is contained in:
Benjamin Otte 2019-02-15 06:15:50 +01:00
parent 7ad0f7fc52
commit c44c44ee25
6 changed files with 107 additions and 197 deletions

View File

@ -270,12 +270,9 @@ gtk_theming_background_snapshot_layer (GtkCssBoxes *bg,
}
void
gtk_css_style_snapshot_background (GtkCssStyle *style,
GtkSnapshot *snapshot,
gdouble width,
gdouble height)
gtk_css_style_snapshot_background (GtkCssBoxes *boxes,
GtkSnapshot *snapshot)
{
GtkCssBoxes boxes;
gint idx;
GtkCssValue *background_image;
GtkCssValue *box_shadow;
@ -284,9 +281,9 @@ gtk_css_style_snapshot_background (GtkCssStyle *style,
gint number_of_layers;
GskBlendMode *blend_mode_values;
background_image = gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
bg_color = _gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BACKGROUND_COLOR));
box_shadow = gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BOX_SHADOW);
background_image = gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
bg_color = _gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BACKGROUND_COLOR));
box_shadow = gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BOX_SHADOW);
/* This is the common default case of no background */
if (gdk_rgba_is_clear (bg_color) &&
@ -295,15 +292,13 @@ gtk_css_style_snapshot_background (GtkCssStyle *style,
_gtk_css_shadows_value_is_none (box_shadow))
return;
gtk_css_boxes_init_border_box (&boxes, style, 0, 0, width, height);
gtk_snapshot_push_debug (snapshot, "CSS background");
gtk_css_shadows_value_snapshot_outset (box_shadow,
snapshot,
gtk_css_boxes_get_border_box (&boxes));
gtk_css_boxes_get_border_box (boxes));
blend_modes = gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BACKGROUND_BLEND_MODE);
blend_modes = gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BACKGROUND_BLEND_MODE);
number_of_layers = _gtk_css_array_value_get_n_values (background_image);
blend_mode_values = g_alloca (sizeof (GskBlendMode) * number_of_layers);
@ -316,25 +311,25 @@ gtk_css_style_snapshot_background (GtkCssStyle *style,
}
if (!gdk_rgba_is_clear (bg_color))
gtk_theming_background_snapshot_color (&boxes, snapshot, bg_color, background_image);
gtk_theming_background_snapshot_color (boxes, snapshot, bg_color, background_image);
for (idx = number_of_layers - 1; idx >= 0; idx--)
{
if (blend_mode_values[idx] == GSK_BLEND_MODE_DEFAULT)
{
gtk_theming_background_snapshot_layer (&boxes, idx, snapshot);
gtk_theming_background_snapshot_layer (boxes, idx, snapshot);
}
else
{
gtk_snapshot_pop (snapshot);
gtk_theming_background_snapshot_layer (&boxes, idx, snapshot);
gtk_theming_background_snapshot_layer (boxes, idx, snapshot);
gtk_snapshot_pop (snapshot);
}
}
gtk_css_shadows_value_snapshot_inset (box_shadow,
snapshot,
gtk_css_boxes_get_padding_box (&boxes));
gtk_css_boxes_get_padding_box (boxes));
gtk_snapshot_pop (snapshot);
}

View File

@ -24,14 +24,13 @@
#include <cairo.h>
#include "gtkcsstypesprivate.h"
#include "gtkcssboxesprivate.h"
#include "gtktypes.h"
G_BEGIN_DECLS
void gtk_css_style_snapshot_background (GtkCssStyle *style,
GtkSnapshot *snapshot,
gdouble width,
gdouble height);
void gtk_css_style_snapshot_background (GtkCssBoxes *boxes,
GtkSnapshot *snapshot);

View File

@ -251,13 +251,10 @@ gtk_border_image_compute_slice_size (GtkBorderImageSliceSize sizes[3],
}
static void
gtk_border_image_render (GtkBorderImage *image,
const double border_width[4],
cairo_t *cr,
gdouble x,
gdouble y,
gdouble width,
gdouble height)
gtk_border_image_render (GtkBorderImage *image,
const float border_width[4],
cairo_t *cr,
const graphene_rect_t *rect)
{
cairo_surface_t *surface, *slice;
GtkBorderImageSliceSize vertical_slice[3], horizontal_slice[3];
@ -267,7 +264,7 @@ gtk_border_image_render (GtkBorderImage *image,
_gtk_css_image_get_concrete_size (image->source,
0, 0,
width, height,
rect->size.width, rect->size.height,
&source_width, &source_height);
/* XXX: Optimize for (source_width == width && source_height == height) */
@ -285,15 +282,15 @@ gtk_border_image_render (GtkBorderImage *image,
_gtk_css_number_value_get (_gtk_css_border_value_get_top (image->slice), source_height),
_gtk_css_number_value_get (_gtk_css_border_value_get_bottom (image->slice), source_height));
gtk_border_image_compute_border_size (horizontal_border,
x,
width,
rect->origin.x,
rect->size.width,
border_width[GTK_CSS_LEFT],
border_width[GTK_CSS_RIGHT],
_gtk_css_border_value_get_left (image->width),
_gtk_css_border_value_get_right (image->width));
gtk_border_image_compute_border_size (vertical_border,
y,
height,
rect->origin.y,
rect->size.height,
border_width[GTK_CSS_TOP],
border_width[GTK_CSS_BOTTOM],
_gtk_css_border_value_get_top (image->width),
@ -423,12 +420,12 @@ set_stroke_style (cairo_t *cr,
}
static void
render_frame_stroke (cairo_t *cr,
GskRoundedRect *border_box,
const double border_width[4],
GdkRGBA colors[4],
guint hidden_side,
GtkBorderStyle stroke_style)
render_frame_stroke (cairo_t *cr,
const GskRoundedRect *border_box,
const double border_width[4],
GdkRGBA colors[4],
guint hidden_side,
GtkBorderStyle stroke_style)
{
gboolean different_colors, different_borders;
GskRoundedRect stroke_box;
@ -510,12 +507,12 @@ render_frame_stroke (cairo_t *cr,
}
static void
snapshot_frame_stroke (GtkSnapshot *snapshot,
GskRoundedRect *outline,
const float border_width[4],
GdkRGBA colors[4],
guint hidden_side,
GtkBorderStyle stroke_style)
snapshot_frame_stroke (GtkSnapshot *snapshot,
const GskRoundedRect *outline,
const float border_width[4],
GdkRGBA colors[4],
guint hidden_side,
GtkBorderStyle stroke_style)
{
double double_width[4] = { border_width[0], border_width[1], border_width[2], border_width[3] };
cairo_t *cr;
@ -539,11 +536,11 @@ color_shade (const GdkRGBA *color,
}
static void
snapshot_border (GtkSnapshot *snapshot,
GskRoundedRect *border_box,
const float border_width[4],
GdkRGBA colors[4],
GtkBorderStyle border_style[4])
snapshot_border (GtkSnapshot *snapshot,
const GskRoundedRect *border_box,
const float border_width[4],
GdkRGBA colors[4],
GtkBorderStyle border_style[4])
{
guint hidden_side = 0;
guint i, j;
@ -661,136 +658,86 @@ snapshot_border (GtkSnapshot *snapshot,
}
void
gtk_css_style_snapshot_border (GtkCssStyle *style,
GtkSnapshot *snapshot,
gdouble width,
gdouble height)
gtk_css_style_snapshot_border (GtkCssBoxes *boxes,
GtkSnapshot *snapshot)
{
GtkBorderImage border_image;
float border_width[4];
border_width[0] = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_TOP_WIDTH), 100);
border_width[1] = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_RIGHT_WIDTH), 100);
border_width[2] = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_BOTTOM_WIDTH), 100);
border_width[3] = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_LEFT_WIDTH), 100);
border_width[0] = _gtk_css_number_value_get (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BORDER_TOP_WIDTH), 100);
border_width[1] = _gtk_css_number_value_get (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BORDER_RIGHT_WIDTH), 100);
border_width[2] = _gtk_css_number_value_get (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BORDER_BOTTOM_WIDTH), 100);
border_width[3] = _gtk_css_number_value_get (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BORDER_LEFT_WIDTH), 100);
if (gtk_border_image_init (&border_image, style))
if (gtk_border_image_init (&border_image, boxes->style))
{
cairo_t *cr;
graphene_rect_t bounds;
double double_width[4] = { border_width[0], border_width[1], border_width[2], border_width[3] };
const graphene_rect_t *bounds;
graphene_rect_init (&bounds, 0, 0, width, height);
bounds = gtk_css_boxes_get_border_rect (boxes);
gtk_snapshot_push_debug (snapshot, "CSS border image");
cr = gtk_snapshot_append_cairo (snapshot,
&bounds);
gtk_border_image_render (&border_image, double_width, cr, 0, 0, width, height);
cr = gtk_snapshot_append_cairo (snapshot, bounds);
gtk_border_image_render (&border_image, border_width, cr, bounds);
cairo_destroy (cr);
gtk_snapshot_pop (snapshot);
}
else
{
GtkBorderStyle border_style[4];
GskRoundedRect border_box;
GdkRGBA colors[4];
graphene_simd4f_t alpha_test_vector;
/* Optimize the most common case of "This widget has no border" */
if (border_width[0] == 0 &&
border_width[1] == 0 &&
border_width[2] == 0 &&
border_width[3] == 0)
if (graphene_rect_equal (gtk_css_boxes_get_border_rect (boxes),
gtk_css_boxes_get_padding_rect (boxes)))
return;
colors[0] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_TOP_COLOR));
colors[1] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_RIGHT_COLOR));
colors[2] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_BOTTOM_COLOR));
colors[3] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_LEFT_COLOR));
colors[0] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BORDER_TOP_COLOR));
colors[1] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BORDER_RIGHT_COLOR));
colors[2] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BORDER_BOTTOM_COLOR));
colors[3] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BORDER_LEFT_COLOR));
alpha_test_vector = graphene_simd4f_init (colors[0].alpha, colors[1].alpha, colors[2].alpha, colors[3].alpha);
if (graphene_simd4f_is_zero4 (alpha_test_vector))
return;
border_style[0] = _gtk_css_border_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_TOP_STYLE));
border_style[1] = _gtk_css_border_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_RIGHT_STYLE));
border_style[2] = _gtk_css_border_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_BOTTOM_STYLE));
border_style[3] = _gtk_css_border_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_LEFT_STYLE));
gtk_rounded_boxes_init_for_style (&border_box, NULL, NULL, style, 0, 0, width, height);
border_style[0] = _gtk_css_border_style_value_get (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BORDER_TOP_STYLE));
border_style[1] = _gtk_css_border_style_value_get (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BORDER_RIGHT_STYLE));
border_style[2] = _gtk_css_border_style_value_get (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BORDER_BOTTOM_STYLE));
border_style[3] = _gtk_css_border_style_value_get (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_BORDER_LEFT_STYLE));
gtk_snapshot_push_debug (snapshot, "CSS border");
snapshot_border (snapshot, &border_box, border_width, colors, border_style);
snapshot_border (snapshot,
gtk_css_boxes_get_border_box (boxes),
border_width,
colors,
border_style);
gtk_snapshot_pop (snapshot);
}
}
static void
compute_outline_rect (GtkCssStyle *style,
gdouble x,
gdouble y,
gdouble width,
gdouble height,
cairo_rectangle_t *out_rect)
{
double offset, owidth;
owidth = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_WIDTH), 100);
offset = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_OFFSET), 100);
if (width <= -2 * offset)
{
x += width / 2;
out_rect->x = x - owidth;
out_rect->width = 2 * owidth;
}
else
{
out_rect->x = x - offset - owidth;
out_rect->width = width + 2 * (offset + owidth);
}
if (height <= -2 * offset)
{
y += height / 2;
out_rect->y = y - owidth;
out_rect->height = 2 * owidth;
}
else
{
out_rect->y = y - offset - owidth;
out_rect->height = height + 2 * (offset + owidth);
}
}
void
gtk_css_style_snapshot_outline (GtkCssStyle *style,
GtkSnapshot *snapshot,
gdouble width,
gdouble height)
gtk_css_style_snapshot_outline (GtkCssBoxes *boxes,
GtkSnapshot *snapshot)
{
GtkBorderStyle border_style[4];
GskRoundedRect border_box;
float border_width[4];
GdkRGBA colors[4];
border_style[0] = _gtk_css_border_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_STYLE));
border_style[0] = _gtk_css_border_style_value_get (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_OUTLINE_STYLE));
if (border_style[0] != GTK_BORDER_STYLE_NONE)
{
cairo_rectangle_t rect;
compute_outline_rect (style, 0, 0, width, height, &rect);
border_style[1] = border_style[2] = border_style[3] = border_style[0];
border_width[0] = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_WIDTH), 100);
border_width[0] = _gtk_css_number_value_get (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_OUTLINE_WIDTH), 100);
border_width[3] = border_width[2] = border_width[1] = border_width[0];
colors[0] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_COLOR));
colors[0] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (boxes->style, GTK_CSS_PROPERTY_OUTLINE_COLOR));
colors[3] = colors[2] = colors[1] = colors[0];
_gtk_rounded_box_init_rect (&border_box, rect.x, rect.y, rect.width, rect.height);
_gtk_rounded_box_apply_outline_radius_for_style (&border_box, style);
snapshot_border (snapshot, &border_box, border_width, colors, border_style);
snapshot_border (snapshot,
gtk_css_boxes_get_outline_box (boxes),
border_width,
colors,
border_style);
}
}

View File

@ -23,21 +23,16 @@
#define __GTK_RENDER_BORDER_H__
#include "gtkborder.h"
#include "gtkcssimageprivate.h"
#include "gtkcssvalueprivate.h"
#include "gtkcssboxesprivate.h"
#include "gtktypes.h"
G_BEGIN_DECLS
void gtk_css_style_snapshot_border (GtkCssStyle *style,
GtkSnapshot *snapshot,
gdouble width,
gdouble height);
void gtk_css_style_snapshot_border (GtkCssBoxes *boxes,
GtkSnapshot *snapshot);
void gtk_css_style_snapshot_outline (GtkCssStyle *style,
GtkSnapshot *snapshot,
gdouble width,
gdouble height);
void gtk_css_style_snapshot_outline (GtkCssBoxes *boxes,
GtkSnapshot *snapshot);
G_END_DECLS

View File

@ -1263,14 +1263,15 @@ gtk_snapshot_render_background (GtkSnapshot *snapshot,
gdouble width,
gdouble height)
{
GtkCssBoxes boxes;
g_return_if_fail (snapshot != NULL);
g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
gtk_snapshot_offset (snapshot, x, y);
gtk_css_style_snapshot_background (gtk_style_context_lookup_style (context),
snapshot,
width, height);
gtk_snapshot_offset (snapshot, -x, -y);
gtk_css_boxes_init_border_box (&boxes,
gtk_style_context_lookup_style (context),
x, y, width, height);
gtk_css_style_snapshot_background (&boxes, snapshot);
}
/**
@ -1294,14 +1295,15 @@ gtk_snapshot_render_frame (GtkSnapshot *snapshot,
gdouble width,
gdouble height)
{
GtkCssBoxes boxes;
g_return_if_fail (snapshot != NULL);
g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
gtk_snapshot_offset (snapshot, x, y);
gtk_css_style_snapshot_border (gtk_style_context_lookup_style (context),
snapshot,
width, height);
gtk_snapshot_offset (snapshot, -x, -y);
gtk_css_boxes_init_border_box (&boxes,
gtk_style_context_lookup_style (context),
x, y, width, height);
gtk_css_style_snapshot_border (&boxes, snapshot);
}
/**
@ -1325,14 +1327,15 @@ gtk_snapshot_render_focus (GtkSnapshot *snapshot,
gdouble width,
gdouble height)
{
GtkCssBoxes boxes;
g_return_if_fail (snapshot != NULL);
g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
gtk_snapshot_offset (snapshot, x, y);
gtk_css_style_snapshot_outline (gtk_style_context_lookup_style (context),
snapshot,
width, height);
gtk_snapshot_offset (snapshot, -x, -y);
gtk_css_boxes_init_border_box (&boxes,
gtk_style_context_lookup_style (context),
x, y, width, height);
gtk_css_style_snapshot_outline (&boxes, snapshot);
}
/**

View File

@ -13053,72 +13053,43 @@ gtk_widget_create_render_node (GtkWidget *widget,
{
GtkWidgetClass *klass = GTK_WIDGET_GET_CLASS (widget);
GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
GtkCssBoxes boxes;
GtkCssValue *filter_value;
double opacity;
GtkCssStyle *style;
GtkAllocation allocation;
GtkBorder margin, border, padding;
GtkSnapshot *snapshot;
opacity = priv->alpha / 255.0;
if (opacity <= 0.0)
return NULL;
gtk_css_boxes_init (&boxes, widget);
snapshot = gtk_snapshot_new_with_parent (parent_snapshot);
gtk_widget_get_allocation (widget, &allocation);
gtk_snapshot_push_debug (snapshot,
"RenderNode for %s %p @ %d x %d",
G_OBJECT_TYPE_NAME (widget), widget,
allocation.width, allocation.height);
"RenderNode for %s %p",
G_OBJECT_TYPE_NAME (widget), widget);
filter_value = _gtk_style_context_peek_property (_gtk_widget_get_style_context (widget), GTK_CSS_PROPERTY_FILTER);
gtk_css_filter_value_push_snapshot (filter_value, snapshot);
style = gtk_css_node_get_style (priv->cssnode);
get_box_margin (style, &margin);
get_box_border (style, &border);
get_box_padding (style, &padding);
if (opacity < 1.0)
gtk_snapshot_push_opacity (snapshot, opacity);
if (!GTK_IS_WINDOW (widget))
{
gtk_snapshot_offset (snapshot, - padding.left - border.left, - border.top - padding.top);
gtk_css_style_snapshot_background (style,
snapshot,
allocation.width - margin.left - margin.right,
allocation.height - margin.top - margin.bottom);
gtk_css_style_snapshot_border (style,
snapshot,
allocation.width - margin.left - margin.right,
allocation.height - margin.top - margin.bottom);
gtk_snapshot_offset (snapshot, padding.left + border.left, border.top + padding.top);
gtk_css_style_snapshot_background (&boxes, snapshot);
gtk_css_style_snapshot_border (&boxes, snapshot);
}
if (priv->overflow == GTK_OVERFLOW_HIDDEN)
{
gtk_snapshot_push_clip (snapshot,
&GRAPHENE_RECT_INIT (- padding.left,
- padding.top,
allocation.width - margin.left - margin.right - border.left - border.right,
allocation.height - margin.top - margin.bottom - border.top - border.bottom));
}
gtk_snapshot_push_clip (snapshot, gtk_css_boxes_get_padding_rect (&boxes));
klass->snapshot (widget, snapshot);
if (priv->overflow == GTK_OVERFLOW_HIDDEN)
gtk_snapshot_pop (snapshot);
gtk_snapshot_offset (snapshot, - (padding.left + border.left), -(border.top + padding.top));
gtk_css_style_snapshot_outline (style,
snapshot,
allocation.width - margin.left - margin.right,
allocation.height - margin.top - margin.bottom);
gtk_snapshot_offset (snapshot, padding.left + border.left, border.top + padding.top);
gtk_css_style_snapshot_outline (&boxes, snapshot);
if (opacity < 1.0)
gtk_snapshot_pop (snapshot);