2010-04-13 19:40:24 -04:00
|
|
|
/*
|
|
|
|
* Clutter.
|
|
|
|
*
|
|
|
|
* An OpenGL based 'interactive canvas' library.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Intel Corporation.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Emmanuele Bassi <ebassi@linux.intel.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-06-03 09:34:41 -04:00
|
|
|
* SECTION:clutter-colorize-effect
|
|
|
|
* @short_description: A colorization effect
|
2010-04-13 19:40:24 -04:00
|
|
|
* @see_also: #ClutterEffect, #ClutterOffscreenEffect
|
|
|
|
*
|
|
|
|
* #ClutterColorizeEffect is a sub-class of #ClutterEffect that
|
|
|
|
* colorizes an actor with the given tint.
|
|
|
|
*
|
|
|
|
* #ClutterColorizeEffect is available since Clutter 1.4
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CLUTTER_COLORIZE_EFFECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_COLORIZE_EFFECT, ClutterColorizeEffectClass))
|
|
|
|
#define CLUTTER_IS_COLORIZE_EFFECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_COLORIZE_EFFECT))
|
|
|
|
#define CLUTTER_COLORIZE_EFFECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_COLORIZE_EFFECT, ClutterColorizeEffectClass))
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-02-21 08:22:17 -05:00
|
|
|
#define CLUTTER_ENABLE_EXPERIMENTAL_API
|
|
|
|
|
2010-04-13 19:40:24 -04:00
|
|
|
#include "clutter-colorize-effect.h"
|
|
|
|
|
|
|
|
#include "cogl/cogl.h"
|
|
|
|
|
|
|
|
#include "clutter-debug.h"
|
|
|
|
#include "clutter-enum-types.h"
|
2010-08-11 12:43:15 -04:00
|
|
|
#include "clutter-offscreen-effect.h"
|
2010-04-13 19:40:24 -04:00
|
|
|
#include "clutter-private.h"
|
|
|
|
|
|
|
|
struct _ClutterColorizeEffect
|
|
|
|
{
|
2010-08-11 12:43:15 -04:00
|
|
|
ClutterOffscreenEffect parent_instance;
|
2010-04-13 19:40:24 -04:00
|
|
|
|
|
|
|
/* the tint of the colorization */
|
|
|
|
ClutterColor tint;
|
2010-08-11 12:43:15 -04:00
|
|
|
|
|
|
|
gint tint_uniform;
|
|
|
|
|
2011-11-28 08:46:30 -05:00
|
|
|
gint tex_width;
|
|
|
|
gint tex_height;
|
|
|
|
|
|
|
|
CoglPipeline *pipeline;
|
2010-04-13 19:40:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _ClutterColorizeEffectClass
|
|
|
|
{
|
2010-08-11 12:43:15 -04:00
|
|
|
ClutterOffscreenEffectClass parent_class;
|
2011-11-28 08:46:30 -05:00
|
|
|
|
|
|
|
CoglPipeline *base_pipeline;
|
2010-04-13 19:40:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* the magic gray vec3 has been taken from the NTSC conversion weights
|
|
|
|
* as defined by:
|
|
|
|
*
|
|
|
|
* "OpenGL Superbible, 4th Edition"
|
|
|
|
* -- Richard S. Wright Jr, Benjamin Lipchak, Nicholas Haemel
|
|
|
|
* Addison-Wesley
|
|
|
|
*/
|
2011-11-28 08:46:30 -05:00
|
|
|
static const gchar *colorize_glsl_declarations =
|
|
|
|
"uniform vec3 tint;\n";
|
|
|
|
|
|
|
|
static const gchar *colorize_glsl_source =
|
|
|
|
"float gray = dot (cogl_color_out.rgb, vec3 (0.299, 0.587, 0.114));\n"
|
|
|
|
"cogl_color_out.rgb = gray * tint;\n";
|
2010-04-13 19:40:24 -04:00
|
|
|
|
|
|
|
/* a lame sepia */
|
|
|
|
static const ClutterColor default_tint = { 255, 204, 153, 255 };
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
|
2010-06-21 05:20:32 -04:00
|
|
|
PROP_TINT,
|
|
|
|
|
|
|
|
PROP_LAST
|
2010-04-13 19:40:24 -04:00
|
|
|
};
|
|
|
|
|
2010-06-21 05:20:32 -04:00
|
|
|
static GParamSpec *obj_props[PROP_LAST];
|
|
|
|
|
2010-04-13 19:40:24 -04:00
|
|
|
G_DEFINE_TYPE (ClutterColorizeEffect,
|
|
|
|
clutter_colorize_effect,
|
2010-08-11 12:43:15 -04:00
|
|
|
CLUTTER_TYPE_OFFSCREEN_EFFECT);
|
2010-04-13 19:40:24 -04:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
clutter_colorize_effect_pre_paint (ClutterEffect *effect)
|
|
|
|
{
|
|
|
|
ClutterColorizeEffect *self = CLUTTER_COLORIZE_EFFECT (effect);
|
|
|
|
ClutterEffectClass *parent_class;
|
|
|
|
|
2010-05-17 07:55:46 -04:00
|
|
|
if (!clutter_actor_meta_get_enabled (CLUTTER_ACTOR_META (effect)))
|
|
|
|
return FALSE;
|
2010-04-13 19:40:24 -04:00
|
|
|
|
2010-08-11 12:43:15 -04:00
|
|
|
if (!clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
|
|
|
|
{
|
|
|
|
/* if we don't have support for GLSL shaders then we
|
|
|
|
* forcibly disable the ActorMeta
|
|
|
|
*/
|
|
|
|
g_warning ("Unable to use the ShaderEffect: the graphics hardware "
|
|
|
|
"or the current GL driver does not implement support "
|
|
|
|
"for the GLSL shading language.");
|
|
|
|
clutter_actor_meta_set_enabled (CLUTTER_ACTOR_META (effect), FALSE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-04-13 19:40:24 -04:00
|
|
|
|
2011-11-28 08:46:30 -05:00
|
|
|
parent_class = CLUTTER_EFFECT_CLASS (clutter_colorize_effect_parent_class);
|
|
|
|
if (parent_class->pre_paint (effect))
|
2010-08-11 12:43:15 -04:00
|
|
|
{
|
2011-11-28 08:46:30 -05:00
|
|
|
ClutterOffscreenEffect *offscreen_effect =
|
|
|
|
CLUTTER_OFFSCREEN_EFFECT (effect);
|
|
|
|
CoglHandle texture;
|
2010-04-13 19:40:24 -04:00
|
|
|
|
2011-11-28 08:46:30 -05:00
|
|
|
texture = clutter_offscreen_effect_get_texture (offscreen_effect);
|
|
|
|
self->tex_width = cogl_texture_get_width (texture);
|
|
|
|
self->tex_height = cogl_texture_get_height (texture);
|
2010-08-11 12:43:15 -04:00
|
|
|
|
2011-11-28 08:46:30 -05:00
|
|
|
cogl_pipeline_set_layer_texture (self->pipeline, 0, texture);
|
2010-08-11 12:43:15 -04:00
|
|
|
|
2011-11-28 08:46:30 -05:00
|
|
|
return TRUE;
|
2010-08-11 12:43:15 -04:00
|
|
|
}
|
2011-11-28 08:46:30 -05:00
|
|
|
else
|
|
|
|
return FALSE;
|
2010-04-13 19:40:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-11 12:43:15 -04:00
|
|
|
clutter_colorize_effect_paint_target (ClutterOffscreenEffect *effect)
|
|
|
|
{
|
|
|
|
ClutterColorizeEffect *self = CLUTTER_COLORIZE_EFFECT (effect);
|
2011-11-28 08:46:30 -05:00
|
|
|
ClutterActor *actor;
|
|
|
|
guint8 paint_opacity;
|
2010-08-11 12:43:15 -04:00
|
|
|
|
2011-11-28 08:46:30 -05:00
|
|
|
actor = clutter_actor_meta_get_actor (CLUTTER_ACTOR_META (effect));
|
|
|
|
paint_opacity = clutter_actor_get_paint_opacity (actor);
|
2010-08-11 12:43:15 -04:00
|
|
|
|
2011-11-28 08:46:30 -05:00
|
|
|
cogl_pipeline_set_color4ub (self->pipeline,
|
|
|
|
paint_opacity,
|
|
|
|
paint_opacity,
|
|
|
|
paint_opacity,
|
|
|
|
paint_opacity);
|
|
|
|
cogl_push_source (self->pipeline);
|
2010-08-11 12:43:15 -04:00
|
|
|
|
2011-11-28 08:46:30 -05:00
|
|
|
cogl_rectangle (0, 0, self->tex_width, self->tex_height);
|
2010-08-11 12:43:15 -04:00
|
|
|
|
2011-11-28 08:46:30 -05:00
|
|
|
cogl_pop_source ();
|
2010-08-11 12:43:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_colorize_effect_dispose (GObject *gobject)
|
2010-04-13 19:40:24 -04:00
|
|
|
{
|
2010-08-11 12:43:15 -04:00
|
|
|
ClutterColorizeEffect *self = CLUTTER_COLORIZE_EFFECT (gobject);
|
|
|
|
|
2011-11-28 08:46:30 -05:00
|
|
|
if (self->pipeline != NULL)
|
2010-08-11 12:43:15 -04:00
|
|
|
{
|
2011-11-28 08:46:30 -05:00
|
|
|
cogl_object_unref (self->pipeline);
|
|
|
|
self->pipeline = NULL;
|
2010-08-11 12:43:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (clutter_colorize_effect_parent_class)->dispose (gobject);
|
2010-04-13 19:40:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_colorize_effect_set_property (GObject *gobject,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
ClutterColorizeEffect *effect = CLUTTER_COLORIZE_EFFECT (gobject);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_TINT:
|
|
|
|
clutter_colorize_effect_set_tint (effect,
|
|
|
|
clutter_value_get_color (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_colorize_effect_get_property (GObject *gobject,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
ClutterColorizeEffect *effect = CLUTTER_COLORIZE_EFFECT (gobject);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_TINT:
|
|
|
|
clutter_value_set_color (value, &effect->tint);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_colorize_effect_class_init (ClutterColorizeEffectClass *klass)
|
|
|
|
{
|
|
|
|
ClutterEffectClass *effect_class = CLUTTER_EFFECT_CLASS (klass);
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
2010-08-11 12:43:15 -04:00
|
|
|
ClutterOffscreenEffectClass *offscreen_class;
|
2010-04-13 19:40:24 -04:00
|
|
|
|
2010-08-11 12:43:15 -04:00
|
|
|
offscreen_class = CLUTTER_OFFSCREEN_EFFECT_CLASS (klass);
|
|
|
|
offscreen_class->paint_target = clutter_colorize_effect_paint_target;
|
|
|
|
|
2010-04-13 19:40:24 -04:00
|
|
|
effect_class->pre_paint = clutter_colorize_effect_pre_paint;
|
|
|
|
|
|
|
|
gobject_class->set_property = clutter_colorize_effect_set_property;
|
|
|
|
gobject_class->get_property = clutter_colorize_effect_get_property;
|
2010-08-11 12:43:15 -04:00
|
|
|
gobject_class->dispose = clutter_colorize_effect_dispose;
|
2010-04-13 19:40:24 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ClutterColorizeEffect:tint:
|
|
|
|
*
|
|
|
|
* The tint to apply to the actor
|
|
|
|
*
|
|
|
|
* Since: 1.4
|
|
|
|
*/
|
2010-10-15 10:24:27 -04:00
|
|
|
obj_props[PROP_TINT] =
|
|
|
|
clutter_param_spec_color ("tint",
|
|
|
|
P_("Tint"),
|
|
|
|
P_("The tint to apply"),
|
|
|
|
&default_tint,
|
|
|
|
CLUTTER_PARAM_READWRITE);
|
|
|
|
|
2012-02-15 04:30:18 -05:00
|
|
|
g_object_class_install_properties (gobject_class, PROP_LAST, obj_props);
|
2011-11-28 08:46:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
update_tint_uniform (ClutterColorizeEffect *self)
|
|
|
|
{
|
|
|
|
if (self->tint_uniform > -1)
|
|
|
|
{
|
|
|
|
float tint[3] = {
|
|
|
|
self->tint.red / 255.0,
|
|
|
|
self->tint.green / 255.0,
|
|
|
|
self->tint.blue / 255.0
|
|
|
|
};
|
|
|
|
|
|
|
|
cogl_pipeline_set_uniform_float (self->pipeline,
|
|
|
|
self->tint_uniform,
|
|
|
|
3, /* n_components */
|
|
|
|
1, /* count */
|
|
|
|
tint);
|
|
|
|
}
|
2010-04-13 19:40:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_colorize_effect_init (ClutterColorizeEffect *self)
|
|
|
|
{
|
2012-02-15 04:30:18 -05:00
|
|
|
ClutterColorizeEffectClass *klass = CLUTTER_COLORIZE_EFFECT_GET_CLASS (self);
|
|
|
|
|
|
|
|
if (G_UNLIKELY (klass->base_pipeline == NULL))
|
|
|
|
{
|
|
|
|
CoglSnippet *snippet;
|
2012-02-21 08:22:17 -05:00
|
|
|
CoglContext *ctx =
|
|
|
|
clutter_backend_get_cogl_context (clutter_get_default_backend ());
|
2012-02-15 04:30:18 -05:00
|
|
|
|
2012-02-21 08:22:17 -05:00
|
|
|
klass->base_pipeline = cogl_pipeline_new (ctx);
|
2012-02-15 04:30:18 -05:00
|
|
|
|
|
|
|
snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_FRAGMENT,
|
|
|
|
colorize_glsl_declarations,
|
|
|
|
colorize_glsl_source);
|
|
|
|
cogl_pipeline_add_snippet (klass->base_pipeline, snippet);
|
|
|
|
cogl_object_unref (snippet);
|
|
|
|
|
|
|
|
cogl_pipeline_set_layer_null_texture (klass->base_pipeline,
|
|
|
|
0, /* layer number */
|
|
|
|
COGL_TEXTURE_TYPE_2D);
|
|
|
|
}
|
2011-11-28 08:46:30 -05:00
|
|
|
|
2012-02-15 04:30:18 -05:00
|
|
|
self->pipeline = cogl_pipeline_copy (klass->base_pipeline);
|
2011-11-28 08:46:30 -05:00
|
|
|
|
|
|
|
self->tint_uniform =
|
|
|
|
cogl_pipeline_get_uniform_location (self->pipeline, "tint");
|
|
|
|
|
2010-04-13 19:40:24 -04:00
|
|
|
self->tint = default_tint;
|
2011-11-28 08:46:30 -05:00
|
|
|
|
|
|
|
update_tint_uniform (self);
|
2010-04-13 19:40:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clutter_colorize_effect_new:
|
|
|
|
* @tint: the color to be used
|
|
|
|
*
|
|
|
|
* Creates a new #ClutterColorizeEffect to be used with
|
2010-08-17 09:54:20 -04:00
|
|
|
* clutter_actor_add_effect()
|
2010-04-13 19:40:24 -04:00
|
|
|
*
|
|
|
|
* Return value: the newly created #ClutterColorizeEffect or %NULL
|
|
|
|
*
|
|
|
|
* Since: 1.4
|
|
|
|
*/
|
|
|
|
ClutterEffect *
|
|
|
|
clutter_colorize_effect_new (const ClutterColor *tint)
|
|
|
|
{
|
|
|
|
return g_object_new (CLUTTER_TYPE_COLORIZE_EFFECT,
|
|
|
|
"tint", tint,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clutter_colorize_effect_set_tint:
|
|
|
|
* @effect: a #ClutterColorizeEffect
|
|
|
|
* @tint: the color to be used
|
|
|
|
*
|
|
|
|
* Sets the tint to be used when colorizing
|
|
|
|
*
|
|
|
|
* Since: 1.4
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
clutter_colorize_effect_set_tint (ClutterColorizeEffect *effect,
|
|
|
|
const ClutterColor *tint)
|
|
|
|
{
|
|
|
|
g_return_if_fail (CLUTTER_IS_COLORIZE_EFFECT (effect));
|
|
|
|
|
|
|
|
effect->tint = *tint;
|
|
|
|
|
2011-11-28 08:46:30 -05:00
|
|
|
update_tint_uniform (effect);
|
|
|
|
|
2011-11-28 09:19:28 -05:00
|
|
|
clutter_effect_queue_repaint (CLUTTER_EFFECT (effect));
|
2010-04-13 19:40:24 -04:00
|
|
|
|
2011-03-03 06:35:46 -05:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (effect), obj_props[PROP_TINT]);
|
2010-04-13 19:40:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clutter_colorize_effect_get_tint:
|
|
|
|
* @effect: a #ClutterColorizeEffect
|
2011-02-14 13:36:51 -05:00
|
|
|
* @tint: (out caller-allocates): return location for the color used
|
2010-04-13 19:40:24 -04:00
|
|
|
*
|
|
|
|
* Retrieves the tint used by @effect
|
|
|
|
*
|
|
|
|
* Since: 1.4
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
clutter_colorize_effect_get_tint (ClutterColorizeEffect *effect,
|
|
|
|
ClutterColor *tint)
|
|
|
|
{
|
|
|
|
g_return_if_fail (CLUTTER_IS_COLORIZE_EFFECT (effect));
|
|
|
|
g_return_if_fail (tint != NULL);
|
|
|
|
|
|
|
|
*tint = effect->tint;
|
|
|
|
}
|