4 * An OpenGL based 'interactive canvas' library.
6 * Copyright (C) 2010 Intel Corporation.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 * Emmanuele Bassi <ebassi@linux.intel.com>
26 * SECTION:clutter-colorize-effect
27 * @short_description: A colorization effect
28 * @see_also: #ClutterEffect, #ClutterOffscreenEffect
30 * #ClutterColorizeEffect is a sub-class of #ClutterEffect that
31 * colorizes an actor with the given tint.
33 * #ClutterColorizeEffect is available since Clutter 1.4
36 #define CLUTTER_COLORIZE_EFFECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_COLORIZE_EFFECT, ClutterColorizeEffectClass))
37 #define CLUTTER_IS_COLORIZE_EFFECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_COLORIZE_EFFECT))
38 #define CLUTTER_COLORIZE_EFFECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_COLORIZE_EFFECT, ClutterColorizeEffectClass))
44 #define CLUTTER_ENABLE_EXPERIMENTAL_API
46 #include "clutter-colorize-effect.h"
48 #include "cogl/cogl.h"
50 #include "clutter-debug.h"
51 #include "clutter-enum-types.h"
52 #include "clutter-offscreen-effect.h"
53 #include "clutter-private.h"
55 struct _ClutterColorizeEffect
57 ClutterOffscreenEffect parent_instance;
59 /* the tint of the colorization */
67 CoglPipeline *pipeline;
70 struct _ClutterColorizeEffectClass
72 ClutterOffscreenEffectClass parent_class;
74 CoglPipeline *base_pipeline;
77 /* the magic gray vec3 has been taken from the NTSC conversion weights
80 * "OpenGL Superbible, 4th Edition"
81 * -- Richard S. Wright Jr, Benjamin Lipchak, Nicholas Haemel
84 static const gchar *colorize_glsl_declarations =
85 "uniform vec3 tint;\n";
87 static const gchar *colorize_glsl_source =
88 "float gray = dot (cogl_color_out.rgb, vec3 (0.299, 0.587, 0.114));\n"
89 "cogl_color_out.rgb = gray * tint;\n";
92 static const ClutterColor default_tint = { 255, 204, 153, 255 };
103 static GParamSpec *obj_props[PROP_LAST];
105 G_DEFINE_TYPE (ClutterColorizeEffect,
106 clutter_colorize_effect,
107 CLUTTER_TYPE_OFFSCREEN_EFFECT);
110 clutter_colorize_effect_pre_paint (ClutterEffect *effect)
112 ClutterColorizeEffect *self = CLUTTER_COLORIZE_EFFECT (effect);
113 ClutterEffectClass *parent_class;
115 if (!clutter_actor_meta_get_enabled (CLUTTER_ACTOR_META (effect)))
118 if (!clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
120 /* if we don't have support for GLSL shaders then we
121 * forcibly disable the ActorMeta
123 g_warning ("Unable to use the ShaderEffect: the graphics hardware "
124 "or the current GL driver does not implement support "
125 "for the GLSL shading language.");
126 clutter_actor_meta_set_enabled (CLUTTER_ACTOR_META (effect), FALSE);
130 parent_class = CLUTTER_EFFECT_CLASS (clutter_colorize_effect_parent_class);
131 if (parent_class->pre_paint (effect))
133 ClutterOffscreenEffect *offscreen_effect =
134 CLUTTER_OFFSCREEN_EFFECT (effect);
137 texture = clutter_offscreen_effect_get_texture (offscreen_effect);
138 self->tex_width = cogl_texture_get_width (texture);
139 self->tex_height = cogl_texture_get_height (texture);
141 cogl_pipeline_set_layer_texture (self->pipeline, 0, texture);
150 clutter_colorize_effect_paint_target (ClutterOffscreenEffect *effect)
152 ClutterColorizeEffect *self = CLUTTER_COLORIZE_EFFECT (effect);
154 guint8 paint_opacity;
156 actor = clutter_actor_meta_get_actor (CLUTTER_ACTOR_META (effect));
157 paint_opacity = clutter_actor_get_paint_opacity (actor);
159 cogl_pipeline_set_color4ub (self->pipeline,
164 cogl_push_source (self->pipeline);
166 cogl_rectangle (0, 0, self->tex_width, self->tex_height);
172 clutter_colorize_effect_dispose (GObject *gobject)
174 ClutterColorizeEffect *self = CLUTTER_COLORIZE_EFFECT (gobject);
176 if (self->pipeline != NULL)
178 cogl_object_unref (self->pipeline);
179 self->pipeline = NULL;
182 G_OBJECT_CLASS (clutter_colorize_effect_parent_class)->dispose (gobject);
186 clutter_colorize_effect_set_property (GObject *gobject,
191 ClutterColorizeEffect *effect = CLUTTER_COLORIZE_EFFECT (gobject);
196 clutter_colorize_effect_set_tint (effect,
197 clutter_value_get_color (value));
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
207 clutter_colorize_effect_get_property (GObject *gobject,
212 ClutterColorizeEffect *effect = CLUTTER_COLORIZE_EFFECT (gobject);
217 clutter_value_set_color (value, &effect->tint);
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
227 clutter_colorize_effect_class_init (ClutterColorizeEffectClass *klass)
229 ClutterEffectClass *effect_class = CLUTTER_EFFECT_CLASS (klass);
230 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
231 ClutterOffscreenEffectClass *offscreen_class;
233 offscreen_class = CLUTTER_OFFSCREEN_EFFECT_CLASS (klass);
234 offscreen_class->paint_target = clutter_colorize_effect_paint_target;
236 effect_class->pre_paint = clutter_colorize_effect_pre_paint;
238 gobject_class->set_property = clutter_colorize_effect_set_property;
239 gobject_class->get_property = clutter_colorize_effect_get_property;
240 gobject_class->dispose = clutter_colorize_effect_dispose;
243 * ClutterColorizeEffect:tint:
245 * The tint to apply to the actor
249 obj_props[PROP_TINT] =
250 clutter_param_spec_color ("tint",
252 P_("The tint to apply"),
254 CLUTTER_PARAM_READWRITE);
256 g_object_class_install_properties (gobject_class, PROP_LAST, obj_props);
260 update_tint_uniform (ClutterColorizeEffect *self)
262 if (self->tint_uniform > -1)
265 self->tint.red / 255.0,
266 self->tint.green / 255.0,
267 self->tint.blue / 255.0
270 cogl_pipeline_set_uniform_float (self->pipeline,
272 3, /* n_components */
279 clutter_colorize_effect_init (ClutterColorizeEffect *self)
281 ClutterColorizeEffectClass *klass = CLUTTER_COLORIZE_EFFECT_GET_CLASS (self);
283 if (G_UNLIKELY (klass->base_pipeline == NULL))
285 CoglSnippet *snippet;
287 clutter_backend_get_cogl_context (clutter_get_default_backend ());
289 klass->base_pipeline = cogl_pipeline_new (ctx);
291 snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_FRAGMENT,
292 colorize_glsl_declarations,
293 colorize_glsl_source);
294 cogl_pipeline_add_snippet (klass->base_pipeline, snippet);
295 cogl_object_unref (snippet);
297 cogl_pipeline_set_layer_null_texture (klass->base_pipeline,
298 0, /* layer number */
299 COGL_TEXTURE_TYPE_2D);
302 self->pipeline = cogl_pipeline_copy (klass->base_pipeline);
305 cogl_pipeline_get_uniform_location (self->pipeline, "tint");
307 self->tint = default_tint;
309 update_tint_uniform (self);
313 * clutter_colorize_effect_new:
314 * @tint: the color to be used
316 * Creates a new #ClutterColorizeEffect to be used with
317 * clutter_actor_add_effect()
319 * Return value: the newly created #ClutterColorizeEffect or %NULL
324 clutter_colorize_effect_new (const ClutterColor *tint)
326 return g_object_new (CLUTTER_TYPE_COLORIZE_EFFECT,
332 * clutter_colorize_effect_set_tint:
333 * @effect: a #ClutterColorizeEffect
334 * @tint: the color to be used
336 * Sets the tint to be used when colorizing
341 clutter_colorize_effect_set_tint (ClutterColorizeEffect *effect,
342 const ClutterColor *tint)
344 g_return_if_fail (CLUTTER_IS_COLORIZE_EFFECT (effect));
346 effect->tint = *tint;
348 update_tint_uniform (effect);
350 clutter_effect_queue_repaint (CLUTTER_EFFECT (effect));
352 g_object_notify_by_pspec (G_OBJECT (effect), obj_props[PROP_TINT]);
356 * clutter_colorize_effect_get_tint:
357 * @effect: a #ClutterColorizeEffect
358 * @tint: (out caller-allocates): return location for the color used
360 * Retrieves the tint used by @effect
365 clutter_colorize_effect_get_tint (ClutterColorizeEffect *effect,
368 g_return_if_fail (CLUTTER_IS_COLORIZE_EFFECT (effect));
369 g_return_if_fail (tint != NULL);
371 *tint = effect->tint;