update to 1.10.4
[profile/ivi/clutter.git] / clutter / clutter-colorize-effect.c
1 /*
2  * Clutter.
3  *
4  * An OpenGL based 'interactive canvas' library.
5  *
6  * Copyright (C) 2010  Intel Corporation.
7  *
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.
12  *
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.
17  *
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/>.
20  *
21  * Author:
22  *   Emmanuele Bassi <ebassi@linux.intel.com>
23  */
24
25 /**
26  * SECTION:clutter-colorize-effect
27  * @short_description: A colorization effect
28  * @see_also: #ClutterEffect, #ClutterOffscreenEffect
29  *
30  * #ClutterColorizeEffect is a sub-class of #ClutterEffect that
31  * colorizes an actor with the given tint.
32  *
33  * #ClutterColorizeEffect is available since Clutter 1.4
34  */
35
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))
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #define CLUTTER_ENABLE_EXPERIMENTAL_API
45
46 #include "clutter-colorize-effect.h"
47
48 #include "cogl/cogl.h"
49
50 #include "clutter-debug.h"
51 #include "clutter-enum-types.h"
52 #include "clutter-offscreen-effect.h"
53 #include "clutter-private.h"
54
55 struct _ClutterColorizeEffect
56 {
57   ClutterOffscreenEffect parent_instance;
58
59   /* the tint of the colorization */
60   ClutterColor tint;
61
62   gint tint_uniform;
63
64   gint tex_width;
65   gint tex_height;
66
67   CoglPipeline *pipeline;
68 };
69
70 struct _ClutterColorizeEffectClass
71 {
72   ClutterOffscreenEffectClass parent_class;
73
74   CoglPipeline *base_pipeline;
75 };
76
77 /* the magic gray vec3 has been taken from the NTSC conversion weights
78  * as defined by:
79  *
80  *   "OpenGL Superbible, 4th Edition"
81  *   -- Richard S. Wright Jr, Benjamin Lipchak, Nicholas Haemel
82  *   Addison-Wesley
83  */
84 static const gchar *colorize_glsl_declarations =
85 "uniform vec3 tint;\n";
86
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";
90
91 /* a lame sepia */
92 static const ClutterColor default_tint = { 255, 204, 153, 255 };
93
94 enum
95 {
96   PROP_0,
97
98   PROP_TINT,
99
100   PROP_LAST
101 };
102
103 static GParamSpec *obj_props[PROP_LAST];
104
105 G_DEFINE_TYPE (ClutterColorizeEffect,
106                clutter_colorize_effect,
107                CLUTTER_TYPE_OFFSCREEN_EFFECT);
108
109 static gboolean
110 clutter_colorize_effect_pre_paint (ClutterEffect *effect)
111 {
112   ClutterColorizeEffect *self = CLUTTER_COLORIZE_EFFECT (effect);
113   ClutterEffectClass *parent_class;
114
115   if (!clutter_actor_meta_get_enabled (CLUTTER_ACTOR_META (effect)))
116     return FALSE;
117
118   if (!clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
119     {
120       /* if we don't have support for GLSL shaders then we
121        * forcibly disable the ActorMeta
122        */
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);
127       return FALSE;
128     }
129
130   parent_class = CLUTTER_EFFECT_CLASS (clutter_colorize_effect_parent_class);
131   if (parent_class->pre_paint (effect))
132     {
133       ClutterOffscreenEffect *offscreen_effect =
134         CLUTTER_OFFSCREEN_EFFECT (effect);
135       CoglHandle texture;
136
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);
140
141       cogl_pipeline_set_layer_texture (self->pipeline, 0, texture);
142
143       return TRUE;
144     }
145   else
146     return FALSE;
147 }
148
149 static void
150 clutter_colorize_effect_paint_target (ClutterOffscreenEffect *effect)
151 {
152   ClutterColorizeEffect *self = CLUTTER_COLORIZE_EFFECT (effect);
153   ClutterActor *actor;
154   guint8 paint_opacity;
155
156   actor = clutter_actor_meta_get_actor (CLUTTER_ACTOR_META (effect));
157   paint_opacity = clutter_actor_get_paint_opacity (actor);
158
159   cogl_pipeline_set_color4ub (self->pipeline,
160                               paint_opacity,
161                               paint_opacity,
162                               paint_opacity,
163                               paint_opacity);
164   cogl_push_source (self->pipeline);
165
166   cogl_rectangle (0, 0, self->tex_width, self->tex_height);
167
168   cogl_pop_source ();
169 }
170
171 static void
172 clutter_colorize_effect_dispose (GObject *gobject)
173 {
174   ClutterColorizeEffect *self = CLUTTER_COLORIZE_EFFECT (gobject);
175
176   if (self->pipeline != NULL)
177     {
178       cogl_object_unref (self->pipeline);
179       self->pipeline = NULL;
180     }
181
182   G_OBJECT_CLASS (clutter_colorize_effect_parent_class)->dispose (gobject);
183 }
184
185 static void
186 clutter_colorize_effect_set_property (GObject      *gobject,
187                                       guint         prop_id,
188                                       const GValue *value,
189                                       GParamSpec   *pspec)
190 {
191   ClutterColorizeEffect *effect = CLUTTER_COLORIZE_EFFECT (gobject);
192
193   switch (prop_id)
194     {
195     case PROP_TINT:
196       clutter_colorize_effect_set_tint (effect,
197                                         clutter_value_get_color (value));
198       break;
199
200     default:
201       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
202       break;
203     }
204 }
205
206 static void
207 clutter_colorize_effect_get_property (GObject    *gobject,
208                                       guint       prop_id,
209                                       GValue     *value,
210                                       GParamSpec *pspec)
211 {
212   ClutterColorizeEffect *effect = CLUTTER_COLORIZE_EFFECT (gobject);
213
214   switch (prop_id)
215     {
216     case PROP_TINT:
217       clutter_value_set_color (value, &effect->tint);
218       break;
219
220     default:
221       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
222       break;
223     }
224 }
225
226 static void
227 clutter_colorize_effect_class_init (ClutterColorizeEffectClass *klass)
228 {
229   ClutterEffectClass *effect_class = CLUTTER_EFFECT_CLASS (klass);
230   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
231   ClutterOffscreenEffectClass *offscreen_class;
232
233   offscreen_class = CLUTTER_OFFSCREEN_EFFECT_CLASS (klass);
234   offscreen_class->paint_target = clutter_colorize_effect_paint_target;
235
236   effect_class->pre_paint = clutter_colorize_effect_pre_paint;
237
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;
241
242   /**
243    * ClutterColorizeEffect:tint:
244    *
245    * The tint to apply to the actor
246    *
247    * Since: 1.4
248    */
249   obj_props[PROP_TINT] =
250     clutter_param_spec_color ("tint",
251                               P_("Tint"),
252                               P_("The tint to apply"),
253                               &default_tint,
254                               CLUTTER_PARAM_READWRITE);
255
256   g_object_class_install_properties (gobject_class, PROP_LAST, obj_props);
257 }
258
259 static void
260 update_tint_uniform (ClutterColorizeEffect *self)
261 {
262   if (self->tint_uniform > -1)
263     {
264       float tint[3] = {
265         self->tint.red / 255.0,
266         self->tint.green / 255.0,
267         self->tint.blue / 255.0
268       };
269
270       cogl_pipeline_set_uniform_float (self->pipeline,
271                                        self->tint_uniform,
272                                        3, /* n_components */
273                                        1, /* count */
274                                        tint);
275     }
276 }
277
278 static void
279 clutter_colorize_effect_init (ClutterColorizeEffect *self)
280 {
281   ClutterColorizeEffectClass *klass = CLUTTER_COLORIZE_EFFECT_GET_CLASS (self);
282
283   if (G_UNLIKELY (klass->base_pipeline == NULL))
284     {
285       CoglSnippet *snippet;
286       CoglContext *ctx =
287         clutter_backend_get_cogl_context (clutter_get_default_backend ());
288
289       klass->base_pipeline = cogl_pipeline_new (ctx);
290
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);
296
297       cogl_pipeline_set_layer_null_texture (klass->base_pipeline,
298                                             0, /* layer number */
299                                             COGL_TEXTURE_TYPE_2D);
300     }
301
302   self->pipeline = cogl_pipeline_copy (klass->base_pipeline);
303
304   self->tint_uniform =
305     cogl_pipeline_get_uniform_location (self->pipeline, "tint");
306
307   self->tint = default_tint;
308
309   update_tint_uniform (self);
310 }
311
312 /**
313  * clutter_colorize_effect_new:
314  * @tint: the color to be used
315  *
316  * Creates a new #ClutterColorizeEffect to be used with
317  * clutter_actor_add_effect()
318  *
319  * Return value: the newly created #ClutterColorizeEffect or %NULL
320  *
321  * Since: 1.4
322  */
323 ClutterEffect *
324 clutter_colorize_effect_new (const ClutterColor *tint)
325 {
326   return g_object_new (CLUTTER_TYPE_COLORIZE_EFFECT,
327                        "tint", tint,
328                        NULL);
329 }
330
331 /**
332  * clutter_colorize_effect_set_tint:
333  * @effect: a #ClutterColorizeEffect
334  * @tint: the color to be used
335  *
336  * Sets the tint to be used when colorizing
337  *
338  * Since: 1.4
339  */
340 void
341 clutter_colorize_effect_set_tint (ClutterColorizeEffect *effect,
342                                   const ClutterColor    *tint)
343 {
344   g_return_if_fail (CLUTTER_IS_COLORIZE_EFFECT (effect));
345
346   effect->tint = *tint;
347
348   update_tint_uniform (effect);
349
350   clutter_effect_queue_repaint (CLUTTER_EFFECT (effect));
351
352   g_object_notify_by_pspec (G_OBJECT (effect), obj_props[PROP_TINT]);
353 }
354
355 /**
356  * clutter_colorize_effect_get_tint:
357  * @effect: a #ClutterColorizeEffect
358  * @tint: (out caller-allocates): return location for the color used
359  *
360  * Retrieves the tint used by @effect
361  *
362  * Since: 1.4
363  */
364 void
365 clutter_colorize_effect_get_tint (ClutterColorizeEffect *effect,
366                                   ClutterColor          *tint)
367 {
368   g_return_if_fail (CLUTTER_IS_COLORIZE_EFFECT (effect));
369   g_return_if_fail (tint != NULL);
370
371   *tint = effect->tint;
372 }