summarylogtreecommitdiffstats
path: root/meta_clip_effect.c
blob: 247af632f48dcff4a551be1b24807a504234fd04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// for 40.4

#include "meta_clip_effect.h"
#include "meta/prefs.h"

typedef struct {
  CoglPipeline *pipeline;
  CoglTexture *corner_texture;
  ClutterActor *actor;
  cairo_rectangle_int_t bounds;
} MetaClipEffectPrivate;

G_DEFINE_TYPE_WITH_PRIVATE(MetaClipEffect, meta_clip_effect, CLUTTER_TYPE_OFFSCREEN_EFFECT)

/*
 * THIS shader copied from the kwin-plugin of CutefishOS project
 * see https://github.com/cutefishos/kwin-plugins/blob/main/plugins/roundedwindow/roundedwindow.cpp
 */
#define ROUNDED_CLIP_FRAGMENT_SHADER_DECLARATIONS                            \
"uniform vec2 corner_scale;                                                             \n"\
"uniform vec2 offset_lt;   // left top                                                  \n"\
"uniform vec2 offset_rt;   // right top                                                 \n"\
"uniform vec2 offset_lb;   // left bottom                                               \n"\
"uniform vec2 offset_rb;   // right bottom                                              \n"\
"uniform vec4 bounds;                                                                   \n"\
"uniform float opacity;                                                                 \n"\
"uniform int skip;                                                                      \n"\
"                                                                                       \n"\
"float cal_alpha(void) {                                                                \n"\
"  vec2 scale = corner_scale;                                                           \n"\
"  vec2 coord = cogl_tex_coord0_in.xy;                                                  \n"\
"  vec4 fg;                                                                             \n"\
"                                                                                       \n"\
"  if(coord.x < bounds.x || coord.y < bounds.y ||                                       \n"\
"    coord.x > bounds.z || coord.y > bounds.w) {                                        \n"\
"    return 0.0;                                                                        \n"\
"  }                                                                                    \n"\
"                                                                                       \n"\
"  if(coord.x < 0.5) {                                                                  \n"\
"    if(coord.y < 0.5) {                                                                \n"\
"      vec2 cornerCoord = vec2(coord.x * scale.x, coord.y * scale.y);                   \n"\
"      fg = texture2D(cogl_sampler1, cornerCoord - offset_lt);                          \n"\
"    } else {                                                                           \n"\
"      vec2 cornerCoordBL = vec2(coord.x * scale.x, (1.0 - coord.y) * scale.y);         \n"\
"      fg = texture2D(cogl_sampler1, cornerCoordBL - offset_lb);                        \n"\
"    }                                                                                  \n"\
"  } else {                                                                             \n"\
"    if(coord.y < 0.5) {                                                                \n"\
"      vec2 cornerCoordTR = vec2((1.0 - coord.x) * scale.x, coord.y * scale.y);         \n"\
"      fg = texture2D(cogl_sampler1, cornerCoordTR - offset_rt);                        \n"\
"    } else {                                                                           \n"\
"      vec2 cornerCoordBR = vec2((1.0 - coord.x) * scale.x, (1.0 - coord.y) * scale.y); \n"\
"      fg = texture2D(cogl_sampler1, cornerCoordBR - offset_rb);                        \n"\
"    }                                                                                  \n"\
"  }                                                                                    \n"\
"                                                                                       \n"\
"  return fg.r;                                                                         \n"\
"}                                                                                      \n"

#define ROUNDED_CLIP_FRAGMENT_SHADER_CODE                                    \
"vec4 bg = texture2D(cogl_sampler0, cogl_tex_coord0_in.xy) * opacity;         \n"\
"if (skip == 1)                                                               \n"\
"  cogl_color_out = bg;                                                       \n"\
"else                                                                         \n"\
"  cogl_color_out = bg * cal_alpha();                                         \n"

// "cogl_color_out = bg * 0.6 + (vec4(0.4) * cal_alpha());  // for_debug         \n"




static CoglPipeline *
meta_clip_effect_class_create_pipeline(ClutterOffscreenEffect *effect,
                                       CoglTexture            *texture)
{
  MetaClipEffect *clip_effect = META_CLIP_EFFECT (effect);
  MetaClipEffectPrivate *priv = meta_clip_effect_get_instance_private(clip_effect);
  cogl_pipeline_set_layer_texture (priv->pipeline, 0, texture);
  cogl_pipeline_set_layer_texture(priv->pipeline, 1, priv->corner_texture);

  return cogl_object_ref (priv->pipeline);
}

static void
meta_clip_effect_set_actor(ClutterActorMeta *meta,
                           ClutterActor     *actor)
{
  ClutterActorMetaClass *meta_class 
    = CLUTTER_ACTOR_META_CLASS(meta_clip_effect_parent_class);
  MetaClipEffectPrivate *priv = 
    meta_clip_effect_get_instance_private(META_CLIP_EFFECT(meta));
  meta_class->set_actor(meta, actor);
  priv->actor = clutter_actor_meta_get_actor(meta);
}

static void
meta_clip_effect_dispose(GObject *gobject)
{
  MetaClipEffect*effect = META_CLIP_EFFECT(gobject);
  MetaClipEffectPrivate *priv = 
    meta_clip_effect_get_instance_private(META_CLIP_EFFECT(effect));

  if (priv->pipeline != NULL)
  {
    g_clear_pointer(&priv->pipeline, cogl_object_unref);
  }

  G_OBJECT_CLASS (meta_clip_effect_parent_class)->dispose (gobject);
}

static
CoglTexture *gen_texture(void)
{
  int radius = meta_prefs_get_round_corner_radius();
  int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, radius);
  uint8_t *pixel = g_malloc0(stride * radius);
  ClutterBackend *backend = clutter_get_default_backend ();
  CoglContext *ctx = clutter_backend_get_cogl_context (backend);
  cairo_surface_t *image = 
    cairo_image_surface_create_for_data(pixel,
                                        CAIRO_FORMAT_ARGB32,
                                        radius,
                                        radius,
                                        stride);
  cairo_t *cr = cairo_create(image);

  /* draw a 1 / 4 circel, a small texture sized with radius x radius
   * texture will be look like this:
   * 
   *                 XXXX
   *              XXXXXXX
   *            XXXXXXXXX
   *           XXXXXXXXXX
   *          XXXXXXXXXXX
   *          XXXXXXXXXXX
   *  
   */

  cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
  cairo_fill(cr);
  cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
  cairo_move_to(cr, 0, radius);
  cairo_arc(cr, radius, radius, radius, M_PI, 1.5 * M_PI);
  cairo_line_to(cr, radius, radius);
  cairo_line_to(cr, 0, radius);
  cairo_close_path (cr);
  cairo_fill(cr);

  cairo_surface_flush(image);

  GError *error = NULL;
  CoglTexture2D *texture =
    cogl_texture_2d_new_from_data(ctx, radius, radius,
                                  COGL_PIXEL_FORMAT_ARGB_8888,
                                  stride, pixel, &error);
  if (error)
  {
    g_warning ("Failed to allocate mask texture: %s", error->message);
    g_error_free (error);
    g_clear_pointer(&texture, cogl_object_unref);
  }
  g_free(pixel);
  cairo_destroy(cr);
  cairo_surface_destroy (image);
  return COGL_TEXTURE(texture);
}

static void
meta_clip_effect_class_init(MetaClipEffectClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  ClutterOffscreenEffectClass *offscreen_class = CLUTTER_OFFSCREEN_EFFECT_CLASS (klass);
  ClutterActorMetaClass *meta_class = CLUTTER_ACTOR_META_CLASS(klass);

  meta_class->set_actor = meta_clip_effect_set_actor;
  offscreen_class->create_pipeline = meta_clip_effect_class_create_pipeline;
  gobject_class->dispose = meta_clip_effect_dispose;
}

static void
meta_clip_effect_init(MetaClipEffect *self)
{
  MetaClipEffectClass *klass = META_CLIP_EFFECT_GET_CLASS (self);
  MetaClipEffectPrivate *priv = meta_clip_effect_get_instance_private(self);

  if (G_UNLIKELY (klass->base_pipeline == NULL))
    {
      CoglSnippet *snippet;
      CoglContext *ctx =
        clutter_backend_get_cogl_context (clutter_get_default_backend ());

      klass->base_pipeline = cogl_pipeline_new (ctx);
      klass->base_corner_texture = gen_texture();

      snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_FRAGMENT,
                                  ROUNDED_CLIP_FRAGMENT_SHADER_DECLARATIONS,
                                  ROUNDED_CLIP_FRAGMENT_SHADER_CODE);
      cogl_pipeline_add_snippet (klass->base_pipeline, snippet);
      cogl_object_unref (snippet);

      cogl_pipeline_set_layer_null_texture (klass->base_pipeline, 0);
    }

  priv->pipeline = cogl_pipeline_copy (klass->base_pipeline);
  priv->corner_texture = klass->base_corner_texture;
  // g_print("corner ref: %ld\n", COGL_OBJECT(priv->corner_texture)->ref_count);
  priv->actor = NULL;
}

MetaClipEffect *meta_clip_effect_new(void)
{
  return g_object_new(META_TYPE_CLIP_EFFECT, NULL);
}

void
meta_clip_effect_set_bounds(MetaClipEffect        *effect, 
                            cairo_rectangle_int_t *_bounds,
                            int                   padding[4])
{
  // padding: [left, right, top, bottom]

  MetaClipEffectPrivate *priv = meta_clip_effect_get_instance_private(effect);

  g_return_if_fail(priv->pipeline && priv->actor);
  float radius = meta_prefs_get_round_corner_radius();

  priv->bounds.x = _bounds->x + padding[0];
  priv->bounds.y = _bounds->y + padding[2];
  priv->bounds.width =  _bounds->width  - padding[1] - padding[0];
  priv->bounds.height = _bounds->height - padding[2] - padding[3];
  float w, h;

  clutter_actor_get_size(priv->actor, &w, &h);

  int location_skip = 
    cogl_pipeline_get_uniform_location(priv->pipeline, "skip");
  int location_opacity = 
    cogl_pipeline_get_uniform_location(priv->pipeline, "opacity");
  int location_offset_lt = 
    cogl_pipeline_get_uniform_location(priv->pipeline, "offset_lt");
  int location_offset_rt =
    cogl_pipeline_get_uniform_location(priv->pipeline, "offset_rt");
  int location_offset_lb =
    cogl_pipeline_get_uniform_location(priv->pipeline, "offset_lb");
  int location_offset_rb =
    cogl_pipeline_get_uniform_location(priv->pipeline, "offset_rb");
  int location_corner_scale = 
    cogl_pipeline_get_uniform_location(priv->pipeline, "corner_scale");
  int location_bounds = 
    cogl_pipeline_get_uniform_location(priv->pipeline, "bounds");

  float bounds[] = {
    priv->bounds.x / w,
    priv->bounds.y / h,
    (priv->bounds.x + priv->bounds.width) / w,
    (priv->bounds.y + priv->bounds.height) / h
  };
  float corner_scale[] = { w / radius , h / radius };
  float top = priv->bounds.y;
  float left = priv->bounds.x;
  float right = w - priv->bounds.width - priv->bounds.x;
  float bottom = h - priv->bounds.height - priv->bounds.y;

  float offset_lt[] = { left / (float) radius, top / (float)radius };
  float offset_rt[] = { right / (float) radius, top / (float)radius };
  float offset_lb[] = { left / (float) radius, bottom / (float)radius };
  float offset_rb[] = { right / (float) radius, bottom / (float)radius };

  cogl_pipeline_set_uniform_float(priv->pipeline,
                                  location_offset_lt, 2, 1, offset_lt);
  cogl_pipeline_set_uniform_float(priv->pipeline,
                                  location_offset_rt, 2, 1, offset_rt);
  cogl_pipeline_set_uniform_float(priv->pipeline,
                                  location_offset_lb, 2, 1, offset_lb);
  cogl_pipeline_set_uniform_float(priv->pipeline,
                                  location_offset_rb, 2, 1, offset_rb);
  cogl_pipeline_set_uniform_float(priv->pipeline,
                                  location_corner_scale, 2, 1, corner_scale);
  cogl_pipeline_set_uniform_float(priv->pipeline,
                                  location_bounds, 4, 1, bounds);
  cogl_pipeline_set_uniform_1f(priv->pipeline,
                               location_opacity,
                               clutter_actor_get_opacity(priv->actor) / 255.0);
  cogl_pipeline_set_uniform_1i(priv->pipeline, location_skip, 0);
}

void
meta_clip_effect_skip(MetaClipEffect *effect)
{
  MetaClipEffectPrivate *priv = meta_clip_effect_get_instance_private(effect);

  g_return_if_fail(priv->pipeline && priv->actor);

  int location_opacity = 
    cogl_pipeline_get_uniform_location(priv->pipeline, "opacity");
  int location_skip = 
    cogl_pipeline_get_uniform_location(priv->pipeline, "skip");

  cogl_pipeline_set_uniform_1f(priv->pipeline,
                               location_opacity,
                               clutter_actor_get_opacity(priv->actor) / 255.0);
  cogl_pipeline_set_uniform_1i(priv->pipeline, location_skip, 1);
}

void
meta_clip_effect_get_bounds(MetaClipEffect        *effect,
                            cairo_rectangle_int_t *bounds)
{
  MetaClipEffectPrivate *priv = meta_clip_effect_get_instance_private(effect);
  *bounds = priv->bounds;
}

void
meta_clip_effect_update_corner_texture(MetaClipEffect *effect)
{
  MetaClipEffectClass *klass = META_CLIP_EFFECT_GET_CLASS(effect);
  MetaClipEffectPrivate *priv = meta_clip_effect_get_instance_private(effect);

  if (klass->base_corner_texture == priv->corner_texture)
  {
    CoglTexture *new_texture = gen_texture();
    if (new_texture != NULL)
    {
      cogl_object_unref(klass->base_corner_texture);
      klass->base_corner_texture = new_texture;
      priv->corner_texture = klass->base_corner_texture;

      cogl_pipeline_set_layer_texture(priv->pipeline, 1, priv->corner_texture);
    }
  } else {
    // it's need-not call `cogl_object_unref` to the old texture
    priv->corner_texture = klass->base_corner_texture;
    // because this function will call `cogl_object_unref` to the old texture
    // at the same time, this function will add the reference count of new texture
    cogl_pipeline_set_layer_texture(priv->pipeline, 1, priv->corner_texture);
  }
}