desaturate-effect: Use macros for subclassing boilerplate

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3387>
This commit is contained in:
Bilal Elmoussaoui 2023-11-11 16:49:57 +01:00
parent 0c46978188
commit 5fecb069a0
3 changed files with 49 additions and 40 deletions

View File

@ -32,7 +32,6 @@
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ClutterActor, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ClutterBrightnessContrastEffect, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ClutterClone, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ClutterDesaturateEffect, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ClutterInputDevice, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ClutterScrollActor, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ClutterStage, g_object_unref)

View File

@ -24,7 +24,7 @@
/**
* ClutterDesaturateEffect:
*
*
* A desaturation effect
*
* #ClutterDesaturateEffect is a sub-class of #ClutterEffect that
@ -33,10 +33,6 @@
* the #ClutterDesaturateEffect:factor property.
*/
#define CLUTTER_DESATURATE_EFFECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_DESATURATE_EFFECT, ClutterDesaturateEffectClass))
#define CLUTTER_IS_DESATURATE_EFFECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_DESATURATE_EFFECT))
#define CLUTTER_DESATURATE_EFFECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_DESATURATE_EFFECT, ClutterDesaturateEffectClass))
#include "config.h"
#include <math.h>
@ -47,13 +43,10 @@
#include "clutter/clutter-debug.h"
#include "clutter/clutter-enum-types.h"
#include "clutter/clutter-offscreen-effect.h"
#include "clutter/clutter-private.h"
struct _ClutterDesaturateEffect
typedef struct _ClutterDesaturateEffectPrivate
{
ClutterOffscreenEffect parent_instance;
/* the desaturation factor, also known as "strength" */
gdouble factor;
@ -63,14 +56,8 @@ struct _ClutterDesaturateEffect
gint tex_height;
CoglPipeline *pipeline;
};
} ClutterDesaturateEffectPrivate;
struct _ClutterDesaturateEffectClass
{
ClutterOffscreenEffectClass parent_class;
CoglPipeline *base_pipeline;
};
/* the magic gray vec3 has been taken from the NTSC conversion weights
* as defined by:
@ -103,9 +90,9 @@ enum
static GParamSpec *obj_props[PROP_LAST];
G_DEFINE_TYPE (ClutterDesaturateEffect,
clutter_desaturate_effect,
CLUTTER_TYPE_OFFSCREEN_EFFECT);
G_DEFINE_TYPE_WITH_PRIVATE (ClutterDesaturateEffect,
clutter_desaturate_effect,
CLUTTER_TYPE_OFFSCREEN_EFFECT);
static CoglPipeline *
clutter_desaturate_effect_create_pipeline (ClutterOffscreenEffect *effect,
@ -113,18 +100,22 @@ clutter_desaturate_effect_create_pipeline (ClutterOffscreenEffect *effect,
{
ClutterDesaturateEffect *desaturate_effect =
CLUTTER_DESATURATE_EFFECT (effect);
ClutterDesaturateEffectPrivate *priv =
clutter_desaturate_effect_get_instance_private (desaturate_effect);
cogl_pipeline_set_layer_texture (desaturate_effect->pipeline, 0, texture);
cogl_pipeline_set_layer_texture (priv->pipeline, 0, texture);
return g_object_ref (desaturate_effect->pipeline);
return g_object_ref (priv->pipeline);
}
static void
clutter_desaturate_effect_dispose (GObject *gobject)
{
ClutterDesaturateEffect *self = CLUTTER_DESATURATE_EFFECT (gobject);
ClutterDesaturateEffectPrivate *priv =
clutter_desaturate_effect_get_instance_private (self);
g_clear_object (&self->pipeline);
g_clear_object (&priv->pipeline);
G_OBJECT_CLASS (clutter_desaturate_effect_parent_class)->dispose (gobject);
}
@ -157,11 +148,13 @@ clutter_desaturate_effect_get_property (GObject *gobject,
GParamSpec *pspec)
{
ClutterDesaturateEffect *effect = CLUTTER_DESATURATE_EFFECT (gobject);
ClutterDesaturateEffectPrivate *priv =
clutter_desaturate_effect_get_instance_private (effect);
switch (prop_id)
{
case PROP_FACTOR:
g_value_set_double (value, effect->factor);
g_value_set_double (value, priv->factor);
break;
default:
@ -173,10 +166,13 @@ clutter_desaturate_effect_get_property (GObject *gobject,
static void
update_factor_uniform (ClutterDesaturateEffect *self)
{
if (self->factor_uniform > -1)
cogl_pipeline_set_uniform_1f (self->pipeline,
self->factor_uniform,
self->factor);
ClutterDesaturateEffectPrivate *priv =
clutter_desaturate_effect_get_instance_private (self);
if (priv->factor_uniform > -1)
cogl_pipeline_set_uniform_1f (priv->pipeline,
priv->factor_uniform,
priv->factor);
}
static void
@ -212,6 +208,8 @@ static void
clutter_desaturate_effect_init (ClutterDesaturateEffect *self)
{
ClutterDesaturateEffectClass *klass = CLUTTER_DESATURATE_EFFECT_GET_CLASS (self);
ClutterDesaturateEffectPrivate *priv =
clutter_desaturate_effect_get_instance_private (self);
if (G_UNLIKELY (klass->base_pipeline == NULL))
{
@ -230,12 +228,12 @@ clutter_desaturate_effect_init (ClutterDesaturateEffect *self)
cogl_pipeline_set_layer_null_texture (klass->base_pipeline, 0);
}
self->pipeline = cogl_pipeline_copy (klass->base_pipeline);
priv->pipeline = cogl_pipeline_copy (klass->base_pipeline);
self->factor_uniform =
cogl_pipeline_get_uniform_location (self->pipeline, "factor");
priv->factor_uniform =
cogl_pipeline_get_uniform_location (priv->pipeline, "factor");
self->factor = 1.0;
priv->factor = 1.0;
update_factor_uniform (self);
}
@ -271,12 +269,15 @@ void
clutter_desaturate_effect_set_factor (ClutterDesaturateEffect *effect,
double factor)
{
ClutterDesaturateEffectPrivate *priv;
g_return_if_fail (CLUTTER_IS_DESATURATE_EFFECT (effect));
g_return_if_fail (factor >= 0.0 && factor <= 1.0);
if (fabs (effect->factor - factor) >= 0.00001)
priv = clutter_desaturate_effect_get_instance_private (effect);
if (fabs (priv->factor - factor) >= 0.00001)
{
effect->factor = factor;
priv->factor = factor;
update_factor_uniform (effect);
clutter_effect_queue_repaint (CLUTTER_EFFECT (effect));
@ -296,7 +297,10 @@ clutter_desaturate_effect_set_factor (ClutterDesaturateEffect *effect,
gdouble
clutter_desaturate_effect_get_factor (ClutterDesaturateEffect *effect)
{
ClutterDesaturateEffectPrivate *priv;
g_return_val_if_fail (CLUTTER_IS_DESATURATE_EFFECT (effect), 0.0);
return effect->factor;
priv = clutter_desaturate_effect_get_instance_private (effect);
return priv->factor;
}

View File

@ -29,18 +29,24 @@
#endif
#include "clutter/clutter-effect.h"
#include "clutter/clutter-offscreen-effect.h"
G_BEGIN_DECLS
#define CLUTTER_TYPE_DESATURATE_EFFECT (clutter_desaturate_effect_get_type ())
#define CLUTTER_DESATURATE_EFFECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CLUTTER_TYPE_DESATURATE_EFFECT, ClutterDesaturateEffect))
#define CLUTTER_IS_DESATURATE_EFFECT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CLUTTER_TYPE_DESATURATE_EFFECT))
typedef struct _ClutterDesaturateEffect ClutterDesaturateEffect;
typedef struct _ClutterDesaturateEffectClass ClutterDesaturateEffectClass;
struct _ClutterDesaturateEffectClass
{
ClutterOffscreenEffectClass parent_class;
CoglPipeline *base_pipeline;
};
CLUTTER_EXPORT
GType clutter_desaturate_effect_get_type (void) G_GNUC_CONST;
G_DECLARE_DERIVABLE_TYPE (ClutterDesaturateEffect,
clutter_desaturate_effect,
CLUTTER, DESATURATE_EFFECT,
ClutterOffscreenEffect)
CLUTTER_EXPORT
ClutterEffect *clutter_desaturate_effect_new (gdouble factor);