clutter-text: Replace cogl_rectangle()

Because ClutterText has a somewhat convoluted drawing routine,
replacing cogl_rectangle() here isn't as straightfoward as the
effects were.

A new CoglPipeline is now part of the ClutterText struct, and
is used to set the color of the  background or the selection.

Another change is paint_selection() now receives a framebuffer
to draw into. The check for NULL framebuffer does not make
sense here, since there is always a draw framebuffer set
when in the drawing function. Because of that, the check is
now gone.
This commit is contained in:
Georges Basile Stavracas Neto 2018-11-08 20:30:51 -02:00
parent 4c3d9fccc1
commit cabcad1856
No known key found for this signature in database
GPG Key ID: 886C17EE170D1385

View File

@ -277,6 +277,8 @@ static const ClutterColor default_selection_color = { 0, 0, 0, 255 };
static const ClutterColor default_text_color = { 0, 0, 0, 255 }; static const ClutterColor default_text_color = { 0, 0, 0, 255 };
static const ClutterColor default_selected_text_color = { 0, 0, 0, 255 }; static const ClutterColor default_selected_text_color = { 0, 0, 0, 255 };
static CoglPipeline *default_color_pipeline = NULL;
static ClutterAnimatableIface *parent_animatable_iface = NULL; static ClutterAnimatableIface *parent_animatable_iface = NULL;
static ClutterScriptableIface *parent_scriptable_iface = NULL; static ClutterScriptableIface *parent_scriptable_iface = NULL;
@ -1744,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) selection_paint (ClutterText *self,
CoglFramebuffer *fb)
{ {
ClutterTextPrivate *priv = self->priv; ClutterTextPrivate *priv = self->priv;
ClutterActor *actor = CLUTTER_ACTOR (self); ClutterActor *actor = CLUTTER_ACTOR (self);
@ -1756,18 +1759,27 @@ selection_paint (ClutterText *self)
if (priv->position == priv->selection_bound) if (priv->position == priv->selection_bound)
{ {
CoglPipeline *color_pipeline = cogl_pipeline_copy (default_color_pipeline);
CoglColor cogl_color;
/* 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,
cogl_color_init_from_4ub (&cogl_color,
color->red,
color->green, color->green,
color->blue, color->blue,
paint_opacity * color->alpha / 255); paint_opacity * color->alpha / 255);
cogl_color_premultiply (&cogl_color);
cogl_pipeline_set_color (color_pipeline, &cogl_color);
cogl_rectangle (priv->cursor_rect.origin.x, cogl_framebuffer_draw_rectangle (fb,
color_pipeline,
priv->cursor_rect.origin.x,
priv->cursor_rect.origin.y, priv->cursor_rect.origin.y,
priv->cursor_rect.origin.x + priv->cursor_rect.size.width, priv->cursor_rect.origin.x + priv->cursor_rect.size.width,
priv->cursor_rect.origin.y + priv->cursor_rect.size.height); priv->cursor_rect.origin.y + priv->cursor_rect.size.height);
@ -1778,11 +1790,6 @@ selection_paint (ClutterText *self)
PangoLayout *layout = clutter_text_get_layout (self); PangoLayout *layout = clutter_text_get_layout (self);
CoglPath *selection_path = cogl_path_new (); CoglPath *selection_path = cogl_path_new ();
CoglColor cogl_color = { 0, }; CoglColor cogl_color = { 0, };
CoglFramebuffer *fb;
fb = cogl_get_draw_framebuffer ();
if (G_UNLIKELY (fb == NULL))
return;
/* Paint selection background */ /* Paint selection background */
if (priv->selection_color_set) if (priv->selection_color_set)
@ -1792,11 +1799,6 @@ selection_paint (ClutterText *self)
else else
color = &priv->text_color; color = &priv->text_color;
cogl_set_source_color4ub (color->red,
color->green,
color->blue,
paint_opacity * color->alpha / 255);
clutter_text_foreach_selection_rectangle (self, clutter_text_foreach_selection_rectangle (self,
add_selection_rectangle_to_path, add_selection_rectangle_to_path,
selection_path); selection_path);
@ -2400,9 +2402,19 @@ clutter_text_paint (ClutterActor *self)
alloc_width = alloc.x2 - alloc.x1; alloc_width = alloc.x2 - alloc.x1;
alloc_height = alloc.y2 - alloc.y1; alloc_height = alloc.y2 - alloc.y1;
if (G_UNLIKELY (default_color_pipeline == NULL))
{
CoglContext *ctx =
clutter_backend_get_cogl_context (clutter_get_default_backend ());
default_color_pipeline = cogl_pipeline_new (ctx);
}
g_assert (default_color_pipeline != NULL);
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)
{ {
CoglPipeline *color_pipeline = cogl_pipeline_copy (default_color_pipeline);
ClutterColor bg_color; ClutterColor bg_color;
clutter_actor_get_background_color (self, &bg_color); clutter_actor_get_background_color (self, &bg_color);
@ -2410,11 +2422,20 @@ clutter_text_paint (ClutterActor *self)
* bg_color.alpha * bg_color.alpha
/ 255; / 255;
cogl_set_source_color4ub (bg_color.red, cogl_color_init_from_4ub (&color,
bg_color.red,
bg_color.green, bg_color.green,
bg_color.blue, bg_color.blue,
bg_color.alpha); bg_color.alpha);
cogl_rectangle (0, 0, alloc_width, alloc_height); cogl_color_premultiply (&color);
cogl_pipeline_set_color (color_pipeline, &color);
cogl_framebuffer_draw_rectangle (fb,
color_pipeline,
0, 0,
alloc_width, alloc_height);
cogl_object_unref (color_pipeline);
} }
/* don't bother painting an empty text actor, unless it's /* don't bother painting an empty text actor, unless it's
@ -2546,7 +2567,7 @@ clutter_text_paint (ClutterActor *self)
real_opacity); real_opacity);
cogl_pango_render_layout (layout, priv->text_x, priv->text_y, &color, 0); cogl_pango_render_layout (layout, priv->text_x, priv->text_y, &color, 0);
selection_paint (text); selection_paint (text, fb);
if (clip_set) if (clip_set)
cogl_framebuffer_pop_clip (fb); cogl_framebuffer_pop_clip (fb);