gnome-shell-3.6.3.1/src/shell-invert-lightness-effect.c

No issues found

  1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
  2 /*
  3  * Copyright (C) 2010-2012 Inclusive Design Research Centre, OCAD University.
  4  *
  5  * This program is free software; you can redistribute it and/or
  6  * modify it under the terms of the GNU Lesser General Public
  7  * License as published by the Free Software Foundation; either
  8  * version 2 of the License, or (at your option) any later version.
  9  *
 10  * This library is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  * Lesser General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU Lesser General Public
 16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 17  *
 18  * Author:
 19  *   Joseph Scheuhammer <clown@alum.mit.edu>
 20  */
 21 
 22 /**
 23  * SECTION:shell-invert-lightness-effect
 24  * @short_description: A colorization effect where lightness is inverted but
 25  * color is not.
 26  * @see_also: #ClutterEffect, #ClutterOffscreenEffect
 27  *
 28  * #ShellInvertLightnessEffect is a sub-class of #ClutterEffect that enhances
 29  * the appearance of a clutter actor.  Specifically it inverts the lightness
 30  * of a #ClutterActor (e.g., darker colors become lighter, white becomes black,
 31  * and white, black).
 32  */
 33 
 34 #define SHELL_INVERT_LIGHTNESS_EFFECT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), SHELL_TYPE_INVERT_LIGHTNESS_EFFECT, ShellInvertLightnessEffectClass))
 35 #define SHELL_IS_INVERT_EFFECT_CLASS(klass)           (G_TYPE_CHECK_CLASS_TYPE ((klass), SHELL_TYPE_INVERT_LIGHTNESS_EFFECT))
 36 #define SHELL_INVERT_LIGHTNESS_EFFECT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), SHELL_TYPE_INVERT_LIGHTNESS_EFFEC, ShellInvertLightnessEffectClass))
 37 
 38 #define CLUTTER_ENABLE_EXPERIMENTAL_API
 39 
 40 #include "shell-invert-lightness-effect.h"
 41 
 42 #include <cogl/cogl.h>
 43 
 44 struct _ShellInvertLightnessEffect
 45 {
 46   ClutterOffscreenEffect parent_instance;
 47 
 48   gint tex_width;
 49   gint tex_height;
 50 
 51   CoglPipeline *pipeline;
 52 };
 53 
 54 struct _ShellInvertLightnessEffectClass
 55 {
 56   ClutterOffscreenEffectClass parent_class;
 57 
 58   CoglPipeline *base_pipeline;
 59 };
 60 
 61 /* Lightness inversion in GLSL.
 62  */
 63 static const gchar *invert_lightness_source =
 64   "cogl_texel = texture2D (cogl_sampler, cogl_tex_coord.st);\n"
 65   "vec3 effect = vec3 (cogl_texel);\n"
 66   "\n"
 67   "float maxColor = max (cogl_texel.r, max (cogl_texel.g, cogl_texel.b));\n"
 68   "float minColor = min (cogl_texel.r, min (cogl_texel.g, cogl_texel.b));\n"
 69   "float lightness = (maxColor + minColor) / 2.0;\n"
 70   "\n"
 71   "float delta = (1.0 - lightness) - lightness;\n"
 72   "effect.rgb = (effect.rgb + delta);\n"
 73   "\n"
 74   "cogl_texel = vec4 (effect, cogl_texel.a);\n";
 75 
 76 G_DEFINE_TYPE (ShellInvertLightnessEffect,
 77                shell_invert_lightness_effect,
 78                CLUTTER_TYPE_OFFSCREEN_EFFECT);
 79 
 80 static gboolean
 81 shell_invert_lightness_effect_pre_paint (ClutterEffect *effect)
 82 {
 83   ShellInvertLightnessEffect *self = SHELL_INVERT_LIGHTNESS_EFFECT (effect);
 84   ClutterEffectClass *parent_class;
 85 
 86   if (!clutter_actor_meta_get_enabled (CLUTTER_ACTOR_META (effect)))
 87     return FALSE;
 88 
 89   if (!clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
 90     {
 91       /* if we don't have support for GLSL shaders then we
 92        * forcibly disable the ActorMeta
 93        */
 94       g_warning ("Unable to use the ShellInvertLightnessEffect: the "
 95                  "graphics hardware or the current GL driver does not "
 96                  "implement support for the GLSL shading language.");
 97       clutter_actor_meta_set_enabled (CLUTTER_ACTOR_META (self), FALSE);
 98       return FALSE;
 99     }
100 
101   parent_class =
102     CLUTTER_EFFECT_CLASS (shell_invert_lightness_effect_parent_class);
103   if (parent_class->pre_paint (effect))
104     {
105       ClutterOffscreenEffect *offscreen_effect =
106         CLUTTER_OFFSCREEN_EFFECT (effect);
107       CoglHandle texture;
108 
109       texture = clutter_offscreen_effect_get_texture (offscreen_effect);
110       self->tex_width = cogl_texture_get_width (texture);
111       self->tex_height = cogl_texture_get_height (texture);
112 
113       cogl_pipeline_set_layer_texture (self->pipeline, 0, texture);
114 
115       return TRUE;
116     }
117   else
118     return FALSE;
119 }
120 
121 static void
122 shell_invert_lightness_effect_paint_target (ClutterOffscreenEffect *effect)
123 {
124   ShellInvertLightnessEffect *self = SHELL_INVERT_LIGHTNESS_EFFECT (effect);
125   ClutterActor *actor;
126   guint8 paint_opacity;
127 
128   actor = clutter_actor_meta_get_actor (CLUTTER_ACTOR_META (effect));
129   paint_opacity = clutter_actor_get_paint_opacity (actor);
130 
131   cogl_pipeline_set_color4ub (self->pipeline,
132                               paint_opacity,
133                               paint_opacity,
134                               paint_opacity,
135                               paint_opacity);
136   cogl_push_source (self->pipeline);
137 
138   cogl_rectangle (0, 0, self->tex_width, self->tex_height);
139 
140   cogl_pop_source ();
141 }
142 
143 static void
144 shell_invert_lightness_effect_dispose (GObject *gobject)
145 {
146   ShellInvertLightnessEffect *self = SHELL_INVERT_LIGHTNESS_EFFECT (gobject);
147 
148   if (self->pipeline != NULL)
149     {
150       cogl_object_unref (self->pipeline);
151       self->pipeline = NULL;
152     }
153 
154   G_OBJECT_CLASS (shell_invert_lightness_effect_parent_class)->dispose (gobject);
155 }
156 
157 static void
158 shell_invert_lightness_effect_class_init (ShellInvertLightnessEffectClass *klass)
159 {
160   ClutterEffectClass *effect_class = CLUTTER_EFFECT_CLASS (klass);
161   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
162   ClutterOffscreenEffectClass *offscreen_class;
163 
164   offscreen_class = CLUTTER_OFFSCREEN_EFFECT_CLASS (klass);
165   offscreen_class->paint_target = shell_invert_lightness_effect_paint_target;
166 
167   effect_class->pre_paint = shell_invert_lightness_effect_pre_paint;
168 
169   gobject_class->dispose = shell_invert_lightness_effect_dispose;
170 }
171 
172 static void
173 shell_invert_lightness_effect_init (ShellInvertLightnessEffect *self)
174 {
175   ShellInvertLightnessEffectClass *klass;
176   klass = SHELL_INVERT_LIGHTNESS_EFFECT_GET_CLASS (self);
177 
178   if (G_UNLIKELY (klass->base_pipeline == NULL))
179     {
180       CoglSnippet *snippet;
181       CoglContext *ctx =
182         clutter_backend_get_cogl_context (clutter_get_default_backend ());
183 
184       klass->base_pipeline = cogl_pipeline_new (ctx);
185 
186       snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_TEXTURE_LOOKUP,
187                                   NULL,
188                                   NULL);
189       cogl_snippet_set_replace (snippet, invert_lightness_source);
190       cogl_pipeline_add_layer_snippet (klass->base_pipeline, 0, snippet);
191       cogl_object_unref (snippet);
192 
193       cogl_pipeline_set_layer_null_texture (klass->base_pipeline,
194                                             0, /* layer number */
195                                             COGL_TEXTURE_TYPE_2D);
196     }
197 
198   self->pipeline = cogl_pipeline_copy (klass->base_pipeline);
199 }
200 
201 /**
202  * shell_invert_lightness_effect_new:
203  *
204  * Creates a new #ShellInvertLightnessEffect to be used with
205  * clutter_actor_add_effect()
206  *
207  * Return value: (transfer full): the newly created
208  * #ShellInvertLightnessEffect or %NULL.  Use g_object_unref() when done.
209  */
210 ClutterEffect *
211 shell_invert_lightness_effect_new (void)
212 {
213   return g_object_new (SHELL_TYPE_INVERT_LIGHTNESS_EFFECT, NULL);
214 }