2016-03-17 13:48:19 +00:00
|
|
|
/* GSK - The GTK Scene Kit
|
|
|
|
*
|
|
|
|
* Copyright 2016 Endless
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:GskRenderNode
|
2016-08-07 03:08:04 +00:00
|
|
|
* @Title: GskRenderNode
|
|
|
|
* @Short_description: Simple scene graph element
|
2016-03-17 13:48:19 +00:00
|
|
|
*
|
2016-07-03 23:23:19 +00:00
|
|
|
* #GskRenderNode is the basic block in a scene graph to be
|
|
|
|
* rendered using #GskRenderer.
|
|
|
|
*
|
|
|
|
* Each node has a parent, except the top-level node; each node may have
|
|
|
|
* children nodes.
|
|
|
|
*
|
|
|
|
* Each node has an associated drawing surface, which has the size of
|
|
|
|
* the rectangle set using gsk_render_node_set_bounds(). Nodes have an
|
|
|
|
* associated transformation matrix, which is used to position and
|
|
|
|
* transform the node on the scene graph; additionally, they also have
|
|
|
|
* a child transformation matrix, which will be applied to each child.
|
|
|
|
*
|
|
|
|
* Render nodes are meant to be transient; once they have been associated
|
|
|
|
* to a #GskRenderer it's safe to release any reference you have on them.
|
|
|
|
* Once a #GskRenderNode has been rendered, it is marked as immutable, and
|
|
|
|
* cannot be modified.
|
2016-03-17 13:48:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gskrendernodeprivate.h"
|
|
|
|
|
|
|
|
#include "gskdebugprivate.h"
|
2016-10-31 16:28:09 +00:00
|
|
|
#include "gskrendererprivate.h"
|
2016-11-07 16:59:38 +00:00
|
|
|
#include "gsktexture.h"
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
#include <graphene-gobject.h>
|
|
|
|
|
2016-11-01 02:40:01 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2016-06-29 18:05:33 +00:00
|
|
|
#include <gobject/gvaluecollector.h>
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-07-04 13:07:24 +00:00
|
|
|
/**
|
2016-12-10 14:33:16 +00:00
|
|
|
* GskRenderNode: (ref-func gsk_render_node_ref) (unref-func gsk_render_node_unref)
|
2016-07-04 13:07:24 +00:00
|
|
|
*
|
|
|
|
* The `GskRenderNode` structure contains only private data.
|
|
|
|
*
|
2016-10-18 10:37:10 +00:00
|
|
|
* Since: 3.90
|
2016-07-04 13:07:24 +00:00
|
|
|
*/
|
|
|
|
|
2016-12-10 14:33:16 +00:00
|
|
|
G_DEFINE_BOXED_TYPE (GskRenderNode, gsk_render_node,
|
|
|
|
gsk_render_node_ref,
|
|
|
|
gsk_render_node_unref)
|
2016-06-29 18:05:33 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_render_node_finalize (GskRenderNode *self)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2016-07-08 15:23:49 +00:00
|
|
|
self->is_mutable = TRUE;
|
|
|
|
|
2016-12-11 03:18:25 +00:00
|
|
|
self->node_class->finalize (self);
|
|
|
|
|
2016-10-20 19:12:36 +00:00
|
|
|
g_clear_pointer (&self->name, g_free);
|
2016-10-18 15:34:03 +00:00
|
|
|
|
2016-12-11 03:28:21 +00:00
|
|
|
g_slice_free1 (self->node_class->struct_size, self);
|
2016-06-29 18:05:33 +00:00
|
|
|
}
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-12-10 14:33:16 +00:00
|
|
|
/*< private >
|
|
|
|
* gsk_render_node_new:
|
2016-12-11 02:57:42 +00:00
|
|
|
* @node_class: class structure for this node
|
2016-12-10 14:33:16 +00:00
|
|
|
*
|
|
|
|
* Returns: (transfer full): the newly created #GskRenderNode
|
|
|
|
*/
|
|
|
|
GskRenderNode *
|
2016-12-11 02:57:42 +00:00
|
|
|
gsk_render_node_new (const GskRenderNodeClass *node_class)
|
2016-06-29 18:05:33 +00:00
|
|
|
{
|
2016-12-10 20:42:01 +00:00
|
|
|
GskRenderNode *self;
|
|
|
|
|
2016-12-11 02:57:42 +00:00
|
|
|
g_return_val_if_fail (node_class != NULL, NULL);
|
|
|
|
g_return_val_if_fail (node_class->node_type != GSK_NOT_A_RENDER_NODE, NULL);
|
2016-12-10 20:42:01 +00:00
|
|
|
|
2016-12-11 03:28:21 +00:00
|
|
|
self = g_slice_alloc0 (node_class->struct_size);
|
2016-12-10 20:42:01 +00:00
|
|
|
|
2016-12-11 02:57:42 +00:00
|
|
|
self->node_class = node_class;
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-06-29 18:05:33 +00:00
|
|
|
self->ref_count = 1;
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
self->opacity = 1.0;
|
2016-06-22 16:30:36 +00:00
|
|
|
|
2016-08-13 07:34:22 +00:00
|
|
|
self->min_filter = GSK_SCALING_FILTER_NEAREST;
|
|
|
|
self->mag_filter = GSK_SCALING_FILTER_NEAREST;
|
|
|
|
|
2016-06-22 16:30:36 +00:00
|
|
|
self->is_mutable = TRUE;
|
2016-07-27 15:23:44 +00:00
|
|
|
|
2016-12-10 14:33:16 +00:00
|
|
|
return self;
|
2016-06-29 18:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gsk_render_node_ref:
|
|
|
|
* @node: a #GskRenderNode
|
|
|
|
*
|
|
|
|
* Acquires a reference on the given #GskRenderNode.
|
|
|
|
*
|
|
|
|
* Returns: (transfer none): the #GskRenderNode with an additional reference
|
|
|
|
*
|
2016-10-18 10:37:10 +00:00
|
|
|
* Since: 3.90
|
2016-06-29 18:05:33 +00:00
|
|
|
*/
|
|
|
|
GskRenderNode *
|
|
|
|
gsk_render_node_ref (GskRenderNode *node)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GSK_IS_RENDER_NODE (node), NULL);
|
|
|
|
|
|
|
|
g_atomic_int_inc (&node->ref_count);
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gsk_render_node_unref:
|
|
|
|
* @node: a #GskRenderNode
|
|
|
|
*
|
|
|
|
* Releases a reference on the given #GskRenderNode.
|
|
|
|
*
|
|
|
|
* If the reference was the last, the resources associated to the @node are
|
|
|
|
* freed.
|
|
|
|
*
|
2016-10-18 10:37:10 +00:00
|
|
|
* Since: 3.90
|
2016-06-29 18:05:33 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gsk_render_node_unref (GskRenderNode *node)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GSK_IS_RENDER_NODE (node));
|
|
|
|
|
|
|
|
if (g_atomic_int_dec_and_test (&node->ref_count))
|
2016-12-10 14:33:16 +00:00
|
|
|
gsk_render_node_finalize (node);
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-10 20:42:01 +00:00
|
|
|
/**
|
|
|
|
* gsk_render_node_get_node_type:
|
|
|
|
* @node: a #GskRenderNode
|
|
|
|
*
|
|
|
|
* Returns the type of the @node.
|
|
|
|
*
|
|
|
|
* Returns: the type of the #GskRenderNode
|
|
|
|
*
|
|
|
|
* Since: 3.90
|
|
|
|
*/
|
|
|
|
GskRenderNodeType
|
|
|
|
gsk_render_node_get_node_type (GskRenderNode *node)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GSK_IS_RENDER_NODE (node), GSK_NOT_A_RENDER_NODE);
|
|
|
|
|
2016-12-11 02:57:42 +00:00
|
|
|
return node->node_class->node_type;
|
2016-12-10 20:42:01 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
/**
|
|
|
|
* gsk_render_node_get_bounds:
|
|
|
|
* @node: a #GskRenderNode
|
|
|
|
* @bounds: (out caller-allocates): return location for the boundaries
|
|
|
|
*
|
2016-12-12 05:29:04 +00:00
|
|
|
* Retrieves the boundaries of the @node. The node will not draw outside
|
|
|
|
* of its boundaries.
|
2016-03-17 13:48:19 +00:00
|
|
|
*
|
2016-10-18 10:37:10 +00:00
|
|
|
* Since: 3.90
|
2016-03-17 13:48:19 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gsk_render_node_get_bounds (GskRenderNode *node,
|
|
|
|
graphene_rect_t *bounds)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GSK_IS_RENDER_NODE (node));
|
|
|
|
g_return_if_fail (bounds != NULL);
|
|
|
|
|
2016-12-12 05:29:04 +00:00
|
|
|
node->node_class->get_bounds (node, bounds);
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gsk_render_node_set_opacity:
|
|
|
|
* @node: a #GskRenderNode
|
|
|
|
* @opacity: the opacity of the node, between 0 (fully transparent) and
|
|
|
|
* 1 (fully opaque)
|
|
|
|
*
|
|
|
|
* Sets the opacity of the @node.
|
|
|
|
*
|
2016-10-18 10:37:10 +00:00
|
|
|
* Since: 3.90
|
2016-03-17 13:48:19 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gsk_render_node_set_opacity (GskRenderNode *node,
|
|
|
|
double opacity)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GSK_IS_RENDER_NODE (node));
|
2016-06-22 16:30:36 +00:00
|
|
|
g_return_if_fail (node->is_mutable);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
node->opacity = CLAMP (opacity, 0.0, 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gsk_render_node_get_opacity:
|
|
|
|
* @node: a #GskRenderNode
|
|
|
|
*
|
|
|
|
* Retrieves the opacity set using gsk_render_node_set_opacity().
|
|
|
|
*
|
|
|
|
* Returns: the opacity of the #GskRenderNode
|
|
|
|
*
|
2016-10-18 10:37:10 +00:00
|
|
|
* Since: 3.90
|
2016-03-17 13:48:19 +00:00
|
|
|
*/
|
|
|
|
double
|
|
|
|
gsk_render_node_get_opacity (GskRenderNode *node)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GSK_IS_RENDER_NODE (node), 0.0);
|
|
|
|
|
|
|
|
return node->opacity;
|
|
|
|
}
|
|
|
|
|
2016-08-13 07:34:22 +00:00
|
|
|
void
|
|
|
|
gsk_render_node_set_scaling_filters (GskRenderNode *node,
|
|
|
|
GskScalingFilter min_filter,
|
|
|
|
GskScalingFilter mag_filter)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GSK_IS_RENDER_NODE (node));
|
|
|
|
|
|
|
|
if (node->min_filter != min_filter)
|
|
|
|
node->min_filter = min_filter;
|
|
|
|
if (node->mag_filter != mag_filter)
|
|
|
|
node->mag_filter = mag_filter;
|
|
|
|
}
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
/**
|
|
|
|
* gsk_render_node_set_name:
|
|
|
|
* @node: a #GskRenderNode
|
|
|
|
* @name: (nullable): a name for the node
|
|
|
|
*
|
|
|
|
* Sets the name of the node.
|
|
|
|
*
|
|
|
|
* A name is generally useful for debugging purposes.
|
|
|
|
*
|
2016-10-18 10:37:10 +00:00
|
|
|
* Since: 3.90
|
2016-03-17 13:48:19 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gsk_render_node_set_name (GskRenderNode *node,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GSK_IS_RENDER_NODE (node));
|
|
|
|
|
|
|
|
g_free (node->name);
|
|
|
|
node->name = g_strdup (name);
|
|
|
|
}
|
|
|
|
|
2016-10-29 16:08:11 +00:00
|
|
|
/**
|
|
|
|
* gsk_render_node_get_name:
|
|
|
|
* @node: a #GskRenderNode
|
|
|
|
*
|
|
|
|
* Retrieves the name previously set via gsk_render_node_set_name().
|
|
|
|
* If no name has been set, %NULL is returned.
|
|
|
|
*
|
2016-10-30 06:58:05 +00:00
|
|
|
* Returns: (nullable): The name previously set via
|
2016-10-29 16:08:11 +00:00
|
|
|
* gsk_render_node_set_name() or %NULL
|
|
|
|
*
|
|
|
|
* Since: 3.90
|
|
|
|
**/
|
|
|
|
const char *
|
|
|
|
gsk_render_node_get_name (GskRenderNode *node)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GSK_IS_RENDER_NODE (node), NULL);
|
|
|
|
|
|
|
|
return node->name;
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:12:30 +00:00
|
|
|
/**
|
|
|
|
* gsk_render_node_set_blend_mode:
|
|
|
|
* @node: a #GskRenderNode
|
|
|
|
* @blend_mode: the blend mode to be applied to the node's children
|
|
|
|
*
|
|
|
|
* Sets the blend mode to be used when rendering the children
|
|
|
|
* of the @node.
|
|
|
|
*
|
|
|
|
* The default value is %GSK_BLEND_MODE_DEFAULT.
|
|
|
|
*
|
2016-10-18 10:37:10 +00:00
|
|
|
* Since: 3.90
|
2016-07-01 11:12:30 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gsk_render_node_set_blend_mode (GskRenderNode *node,
|
|
|
|
GskBlendMode blend_mode)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GSK_IS_RENDER_NODE (node));
|
2016-07-08 15:24:35 +00:00
|
|
|
g_return_if_fail (node->is_mutable);
|
2016-07-01 11:12:30 +00:00
|
|
|
|
|
|
|
if (node->blend_mode == blend_mode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
node->blend_mode = blend_mode;
|
|
|
|
}
|
|
|
|
|
2016-08-24 13:07:26 +00:00
|
|
|
/*
|
2016-07-01 11:12:30 +00:00
|
|
|
* gsk_render_node_get_blend_mode:
|
|
|
|
* @node: a #GskRenderNode
|
|
|
|
*
|
|
|
|
* Retrieves the blend mode set by gsk_render_node_set_blend_mode().
|
|
|
|
*
|
|
|
|
* Returns: the blend mode
|
|
|
|
*/
|
|
|
|
GskBlendMode
|
|
|
|
gsk_render_node_get_blend_mode (GskRenderNode *node)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GSK_IS_RENDER_NODE (node), GSK_BLEND_MODE_DEFAULT);
|
|
|
|
|
|
|
|
return node->blend_mode;
|
|
|
|
}
|
|
|
|
|
2016-07-03 23:23:19 +00:00
|
|
|
/*< private >
|
|
|
|
* gsk_render_node_make_immutable:
|
|
|
|
* @node: a #GskRenderNode
|
|
|
|
*
|
|
|
|
* Marks @node, and all its children, as immutable.
|
|
|
|
*/
|
2016-06-22 16:30:36 +00:00
|
|
|
void
|
|
|
|
gsk_render_node_make_immutable (GskRenderNode *node)
|
|
|
|
{
|
|
|
|
if (!node->is_mutable)
|
|
|
|
return;
|
|
|
|
|
2016-12-11 10:53:17 +00:00
|
|
|
node->node_class->make_immutable (node);
|
2016-06-22 16:30:36 +00:00
|
|
|
|
2016-12-11 10:53:17 +00:00
|
|
|
node->is_mutable = FALSE;
|
2016-06-22 16:30:36 +00:00
|
|
|
}
|
2016-06-29 18:05:33 +00:00
|
|
|
|
2016-12-13 02:05:15 +00:00
|
|
|
/**
|
|
|
|
* gsk_render_node_draw:
|
|
|
|
* @node: a #GskRenderNode
|
|
|
|
* @cr: cairo context to draw to
|
|
|
|
*
|
|
|
|
* Draw the contents of @node to the given cairo context.
|
|
|
|
*
|
|
|
|
* Typically, you'll use this function to implement fallback rendering
|
|
|
|
* of #GskRenderNodes on an intermediate Cairo context, instead of using
|
|
|
|
* the drawing context associated to a #GdkWindow's rendering buffer.
|
|
|
|
*
|
|
|
|
* For advanced nodes that cannot be supported using Cairo, in particular
|
|
|
|
* for nodes doing 3D operations, this function may fail.
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
gsk_render_node_draw (GskRenderNode *node,
|
|
|
|
cairo_t *cr)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GSK_IS_RENDER_NODE (node));
|
|
|
|
g_return_if_fail (cr != NULL);
|
|
|
|
|
|
|
|
cairo_save (cr);
|
|
|
|
|
|
|
|
if (!GSK_RENDER_MODE_CHECK (GEOMETRY))
|
|
|
|
{
|
|
|
|
graphene_rect_t frame;
|
|
|
|
|
|
|
|
gsk_render_node_get_bounds (node, &frame);
|
|
|
|
GSK_NOTE (CAIRO, g_print ("CLIP = { .x = %g, .y = %g, .width = %g, .height = %g }\n",
|
|
|
|
frame.origin.x, frame.origin.y,
|
|
|
|
frame.size.width, frame.size.height));
|
|
|
|
|
|
|
|
cairo_rectangle (cr, frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
|
|
|
|
cairo_clip (cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
GSK_NOTE (CAIRO, g_print ("Rendering node %s[%p]\n",
|
|
|
|
node->name,
|
|
|
|
node));
|
|
|
|
|
|
|
|
node->node_class->draw (node, cr);
|
|
|
|
|
|
|
|
if (GSK_RENDER_MODE_CHECK (GEOMETRY))
|
|
|
|
{
|
|
|
|
graphene_rect_t frame;
|
|
|
|
|
|
|
|
gsk_render_node_get_bounds (node, &frame);
|
|
|
|
|
|
|
|
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
|
|
|
|
cairo_rectangle (cr, frame.origin.x - 1, frame.origin.y - 1, frame.size.width + 2, frame.size.height + 2);
|
|
|
|
cairo_set_line_width (cr, 2);
|
|
|
|
cairo_set_source_rgba (cr, 0, 0, 0, 0.5);
|
|
|
|
cairo_stroke (cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_restore (cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|