Port ClutterText to ClutterPaintNode

The port to use ClutterPaintNodes basically mirrors what
it currently does, with the major benefit of not depending
on cogl_get_draw_framebuffer() anymore.

There are various factors that influence the number of
subnodes:

 * The background color adds a color subnode;
 * The selection adds a clip subnode, and a color subnode,
   or a color and a text subnode;

The simplest case is when the text does not overflow and
has no background color nor selection. In that case, the
render tree is simply:

    [ Dummy ]
        ↓
    [ Text ]

In contrast, the most complex case is when drawing the text
with selection, in which case the render tree looks like:

    [ Dummy ]
        ↓
    [ Clip ]
        ↓
    [ Text ] → [ Clip ]
                  ↓
               [ Color ] → [ Text ]

Since the selection may have another text color, the selected
text must be rendered again, but clipped to only cover the
selection rectangle. This is suboptimal, but it's what the
current code already does anyway.
This commit is contained in:
Georges Basile Stavracas Neto 2018-09-26 02:05:28 -03:00
parent cc75fc88ee
commit 5a4ade857a
No known key found for this signature in database
GPG Key ID: 886C17EE170D1385

View File

@ -1746,7 +1746,8 @@ add_selection_rectangle_to_path (ClutterText *text,
/* Draws the selected text, its background, and the cursor */ /* Draws the selected text, its background, and the cursor */
static void static void
selection_paint (ClutterText *self) paint_selection (ClutterText *self,
ClutterPaintNode *node)
{ {
ClutterTextPrivate *priv = self->priv; ClutterTextPrivate *priv = self->priv;
ClutterActor *actor = CLUTTER_ACTOR (self); ClutterActor *actor = CLUTTER_ACTOR (self);
@ -1758,33 +1759,59 @@ selection_paint (ClutterText *self)
if (priv->position == priv->selection_bound) if (priv->position == priv->selection_bound)
{ {
g_autoptr(ClutterPaintNode) cursor_node = NULL;
/* No selection, just draw the cursor */ /* No selection, just draw the cursor */
if (priv->cursor_color_set) if (priv->cursor_color_set)
color = &priv->cursor_color; color = &priv->cursor_color;
else else
color = &priv->text_color; color = &priv->text_color;
cogl_set_source_color4ub (color->red, cursor_node = clutter_color_node_new (&(ClutterColor) {
color->green, color->red,
color->blue, color->green,
paint_opacity * color->alpha / 255); color->blue,
paint_opacity * color->alpha / 255,
});
clutter_paint_node_set_name (cursor_node, "ClutterText.selection-background");
clutter_paint_node_add_child (node, cursor_node);
cogl_rectangle (priv->cursor_rect.origin.x, clutter_paint_node_add_rectangle (cursor_node,
priv->cursor_rect.origin.y, &(ClutterActorBox) {
priv->cursor_rect.origin.x + priv->cursor_rect.size.width, priv->cursor_rect.origin.x,
priv->cursor_rect.origin.y + priv->cursor_rect.size.height); priv->cursor_rect.origin.y,
priv->cursor_rect.origin.x + priv->cursor_rect.size.width,
priv->cursor_rect.origin.y + priv->cursor_rect.size.height
});
} }
else else
{ {
/* Paint selection background first */ /* Paint selection background first */
g_autoptr(ClutterPaintNode) selection_background_node = NULL;
g_autoptr(ClutterPaintNode) selection_text_node = NULL;
g_autoptr(ClutterPaintNode) selection_clip_node = NULL;
PangoLayout *layout = clutter_text_get_layout (self); PangoLayout *layout = clutter_text_get_layout (self);
ClutterActorBox alloc = { 0, };
CoglPath *selection_path = cogl_path_new (); CoglPath *selection_path = cogl_path_new ();
CoglColor cogl_color = { 0, };
CoglFramebuffer *fb;
fb = cogl_get_draw_framebuffer (); clutter_text_foreach_selection_rectangle (self,
if (G_UNLIKELY (fb == NULL)) add_selection_rectangle_to_path,
return; selection_path);
/* Clip against both the actor allocation - we don't want to draw
* the selection rectangle outside the actor - and the selection
* path - we don't want to render the text over the unselected
* text either.
*/
selection_clip_node = clutter_clip_node_new ();
clutter_paint_node_set_name (selection_clip_node, "ClutterText.clip-selection");
clutter_paint_node_add_child (node, selection_clip_node);
clutter_actor_get_allocation_box (CLUTTER_ACTOR (self), &alloc);
clutter_actor_box_set_origin (&alloc, 0, 0);
clutter_paint_node_add_rectangle (selection_clip_node, &alloc);
clutter_paint_node_add_path (selection_clip_node, selection_path);
/* Paint selection background */ /* Paint selection background */
if (priv->selection_color_set) if (priv->selection_color_set)
@ -1794,35 +1821,40 @@ selection_paint (ClutterText *self)
else else
color = &priv->text_color; color = &priv->text_color;
cogl_set_source_color4ub (color->red, selection_background_node = clutter_color_node_new (&(ClutterColor) {
color->green, color->red,
color->blue, color->green,
paint_opacity * color->alpha / 255); color->blue,
paint_opacity * color->alpha / 255,
});
clutter_paint_node_set_name (selection_background_node, "ClutterText.selection-background");
clutter_paint_node_add_child (selection_clip_node, selection_background_node);
clutter_text_foreach_selection_rectangle (self, clutter_paint_node_add_path (selection_background_node, selection_path);
add_selection_rectangle_to_path,
selection_path);
cogl_path_fill (selection_path);
/* Paint selected text */ /* Paint selected text */
cogl_framebuffer_push_path_clip (fb, selection_path);
cogl_object_unref (selection_path);
if (priv->selected_text_color_set) if (priv->selected_text_color_set)
color = &priv->selected_text_color; color = &priv->selected_text_color;
else else
color = &priv->text_color; color = &priv->text_color;
cogl_color_init_from_4ub (&cogl_color, selection_text_node = clutter_text_node_new (layout,
color->red, &(ClutterColor) {
color->green, color->red,
color->blue, color->green,
paint_opacity * color->alpha / 255); color->blue,
paint_opacity * color->alpha / 255,
});
cogl_pango_render_layout (layout, priv->text_x, 0, &cogl_color, 0); clutter_paint_node_set_name (selection_text_node, "ClutterText.selection-text");
clutter_paint_node_add_child (selection_clip_node, selection_text_node);
cogl_framebuffer_pop_clip (fb); clutter_paint_node_add_texture_rectangle (selection_text_node,
&alloc,
priv->text_x, priv->text_y,
1, 1);
cogl_object_unref (selection_path);
} }
} }
@ -2373,26 +2405,43 @@ clutter_text_compute_layout_offsets (ClutterText *self,
#define TEXT_PADDING 2 #define TEXT_PADDING 2
static void static inline ClutterPaintNode *
clutter_text_paint (ClutterActor *self) create_clip_node (ClutterPaintNode *node,
float width,
float height)
{ {
g_autoptr(ClutterPaintNode) clip_node = NULL;
clip_node = clutter_clip_node_new ();
clutter_paint_node_set_name (clip_node, "ClutterText.clip");
clutter_paint_node_add_child (node, clip_node);
clutter_paint_node_add_rectangle (clip_node,
&(ClutterActorBox) {
0, 0,
width, height,
});
return g_steal_pointer (&clip_node);
}
static void
clutter_text_paint_node (ClutterActor *self,
ClutterPaintNode *node)
{
g_autoptr(ClutterPaintNode) text_node = NULL;
ClutterText *text = CLUTTER_TEXT (self); ClutterText *text = CLUTTER_TEXT (self);
ClutterTextPrivate *priv = text->priv; ClutterTextPrivate *priv = text->priv;
CoglFramebuffer *fb;
PangoLayout *layout; PangoLayout *layout;
ClutterActorBox alloc = { 0, }; ClutterActorBox alloc = { 0, };
CoglColor color = { 0, };
guint8 real_opacity; guint8 real_opacity;
gint text_x = priv->text_x; gint text_x = priv->text_x;
gint text_y = priv->text_y; gint text_y = priv->text_y;
gboolean clip_set = FALSE;
gboolean bg_color_set = FALSE; gboolean bg_color_set = FALSE;
guint n_chars; guint n_chars;
float alloc_width; float alloc_width;
float alloc_height; float alloc_height;
fb = cogl_get_draw_framebuffer ();
/* Note that if anything in this paint method changes it needs to be /* Note that if anything in this paint method changes it needs to be
reflected in the get_paint_volume implementation which is tightly reflected in the get_paint_volume implementation which is tightly
tied to the workings of this function */ tied to the workings of this function */
@ -2405,6 +2454,7 @@ clutter_text_paint (ClutterActor *self)
g_object_get (self, "background-color-set", &bg_color_set, NULL); g_object_get (self, "background-color-set", &bg_color_set, NULL);
if (bg_color_set) if (bg_color_set)
{ {
g_autoptr(ClutterPaintNode) background_color_node = NULL;
ClutterColor bg_color; ClutterColor bg_color;
clutter_actor_get_background_color (self, &bg_color); clutter_actor_get_background_color (self, &bg_color);
@ -2412,11 +2462,11 @@ clutter_text_paint (ClutterActor *self)
* bg_color.alpha * bg_color.alpha
/ 255; / 255;
cogl_set_source_color4ub (bg_color.red, background_color_node = clutter_color_node_new (&bg_color);
bg_color.green, clutter_paint_node_set_name (background_color_node, "ClutterText.background-color");
bg_color.blue, clutter_paint_node_add_child (node, background_color_node);
bg_color.alpha);
cogl_rectangle (0, 0, alloc_width, alloc_height); clutter_paint_node_add_rectangle (background_color_node, &alloc);
} }
/* don't bother painting an empty text actor, unless it's /* don't bother painting an empty text actor, unless it's
@ -2469,8 +2519,7 @@ clutter_text_paint (ClutterActor *self)
pango_layout_get_extents (layout, NULL, &logical_rect); pango_layout_get_extents (layout, NULL, &logical_rect);
cogl_framebuffer_push_rectangle_clip (fb, 0, 0, alloc_width, alloc_height); node = create_clip_node (node, alloc_width, alloc_height);
clip_set = TRUE;
actor_width = alloc_width - 2 * TEXT_PADDING; actor_width = alloc_width - 2 * TEXT_PADDING;
text_width = logical_rect.width / PANGO_SCALE; text_width = logical_rect.width / PANGO_SCALE;
@ -2513,12 +2562,8 @@ clutter_text_paint (ClutterActor *self)
pango_layout_get_pixel_extents (layout, NULL, &logical_rect); pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
/* don't clip if the layout managed to fit inside our allocation */ /* don't clip if the layout managed to fit inside our allocation */
if (logical_rect.width > alloc_width || if (logical_rect.width > alloc_width || logical_rect.height > alloc_height)
logical_rect.height > alloc_height) node = create_clip_node (node, alloc_width, alloc_height);
{
cogl_framebuffer_push_rectangle_clip (fb, 0, 0, alloc_width, alloc_height);
clip_set = TRUE;
}
clutter_text_compute_layout_offsets (text, layout, &alloc, &text_x, &text_y); clutter_text_compute_layout_offsets (text, layout, &alloc, &text_x, &text_y);
} }
@ -2541,17 +2586,25 @@ clutter_text_paint (ClutterActor *self)
CLUTTER_NOTE (PAINT, "painting text (text: '%s')", CLUTTER_NOTE (PAINT, "painting text (text: '%s')",
clutter_text_buffer_get_text (get_buffer (text))); clutter_text_buffer_get_text (get_buffer (text)));
cogl_color_init_from_4ub (&color, text_node = clutter_text_node_new (layout,
priv->text_color.red, &(ClutterColor) {
priv->text_color.green, priv->text_color.red,
priv->text_color.blue, priv->text_color.green,
real_opacity); priv->text_color.blue,
cogl_pango_render_layout (layout, priv->text_x, priv->text_y, &color, 0); real_opacity
});
selection_paint (text); clutter_paint_node_set_name (text_node, "ClutterText.text");
clutter_paint_node_add_child (node, text_node);
if (clip_set) clutter_actor_box_set_origin (&alloc, 0, 0);
cogl_framebuffer_pop_clip (fb); clutter_paint_node_add_texture_rectangle (text_node,
&alloc,
priv->text_x,
priv->text_y,
1, 1);
paint_selection (text, node);
} }
static void static void
@ -3548,7 +3601,7 @@ clutter_text_class_init (ClutterTextClass *klass)
gobject_class->dispose = clutter_text_dispose; gobject_class->dispose = clutter_text_dispose;
gobject_class->finalize = clutter_text_finalize; gobject_class->finalize = clutter_text_finalize;
actor_class->paint = clutter_text_paint; actor_class->paint_node = clutter_text_paint_node;
actor_class->get_paint_volume = clutter_text_get_paint_volume; actor_class->get_paint_volume = clutter_text_get_paint_volume;
actor_class->get_preferred_width = clutter_text_get_preferred_width; actor_class->get_preferred_width = clutter_text_get_preferred_width;
actor_class->get_preferred_height = clutter_text_get_preferred_height; actor_class->get_preferred_height = clutter_text_get_preferred_height;