summaryrefslogtreecommitdiff
path: root/src/lib/evas/filters/evas_filter_mask.c
blob: 8c900d04a66322a31b9ce71b2eee72909b5388c2 (plain) (blame)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* Implementation of some masking functions for the software engine */

#include "evas_filter_private.h"
#include "evas_blend_private.h"


// Naming convention: _func_engine_incolor_maskcolor_outcolor()
static Eina_Bool _mask_cpu_alpha_alpha_alpha(Evas_Filter_Command *cmd);
static Eina_Bool _mask_cpu_alpha_rgba_rgba(Evas_Filter_Command *cmd);
static Eina_Bool _mask_cpu_alpha_alpha_rgba(Evas_Filter_Command *cmd);
static Eina_Bool _mask_cpu_rgba_alpha_rgba(Evas_Filter_Command *cmd);
static Eina_Bool _mask_cpu_rgba_rgba_rgba(Evas_Filter_Command *cmd);

Evas_Filter_Apply_Func
evas_filter_mask_cpu_func_get(Evas_Filter_Command *cmd)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(cmd, NULL);
   EINA_SAFETY_ON_NULL_RETURN_VAL(cmd->input, NULL);
   EINA_SAFETY_ON_NULL_RETURN_VAL(cmd->output, NULL);
   EINA_SAFETY_ON_NULL_RETURN_VAL(cmd->mask, NULL);

   EINA_SAFETY_ON_NULL_RETURN_VAL(cmd->input->backing, NULL);
   EINA_SAFETY_ON_NULL_RETURN_VAL(cmd->output->backing, NULL);
   EINA_SAFETY_ON_NULL_RETURN_VAL(cmd->mask->backing, NULL);

   EINA_SAFETY_ON_FALSE_RETURN_VAL(cmd->input->w == cmd->output->w, NULL);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(cmd->input->h == cmd->output->h, NULL);

   if (cmd->input->alpha_only)
     {
        if (cmd->mask->alpha_only && cmd->output->alpha_only)
          return _mask_cpu_alpha_alpha_alpha;
        else if (!cmd->mask->alpha_only && !cmd->output->alpha_only)
          return _mask_cpu_alpha_rgba_rgba;
        else if (cmd->mask->alpha_only && !cmd->output->alpha_only)
          return _mask_cpu_alpha_alpha_rgba;
     }
   else
     {
        if (cmd->mask->alpha_only && !cmd->output->alpha_only)
          return _mask_cpu_rgba_alpha_rgba;
        else if (!cmd->mask->alpha_only && !cmd->output->alpha_only)
          return _mask_cpu_rgba_rgba_rgba;
     }

   CRI("If input or mask is RGBA, then output must also be RGBA: %s [%s] %s",
       cmd->input->alpha_only ? "alpha" : "rgba",
       cmd->mask->alpha_only ? "alpha" : "rgba",
       cmd->output->alpha_only ? "alpha" : "rgba");
   return NULL;
}

static Eina_Bool
_mask_cpu_alpha_alpha_alpha(Evas_Filter_Command *cmd)
{
   Alpha_Gfx_Func func;
   RGBA_Image *in, *out, *mask;
   DATA8 *src, *dst, *msk;
   int render_op = cmd->draw.render_op;
   int w, h, mw, mh, x, y, my;
   int stepsize, stepcount, step;

   /* Mechanism:
    * 1. Stretch mask as requested in fillmode
    * 2. Copy source to destination
    * 3. Render mask into destination using alpha function
    *
    * FIXME: Could probably be optimized into a single op :)
    */

   in = (RGBA_Image *) cmd->input->backing;
   out = (RGBA_Image *) cmd->output->backing;
   mask = (RGBA_Image *) cmd->mask->backing;

   w = cmd->input->w;
   h = cmd->input->h;
   mw = cmd->mask->w;
   mh = cmd->mask->h;
   src = in->image.data8;
   dst = out->image.data8;

   EINA_SAFETY_ON_FALSE_RETURN_VAL((w > 0) && (mw > 0), EINA_FALSE);

   stepsize  = MIN(mw, w);
   stepcount = w / stepsize;

   // Stretch if necessary.
   if ((mw != w || mh != h) && (cmd->draw.fillmode & EVAS_FILTER_FILL_MODE_STRETCH_XY))
     {
        Evas_Filter_Buffer *fb;

        if (cmd->draw.fillmode & EVAS_FILTER_FILL_MODE_STRETCH_X)
          mw = w;
        if (cmd->draw.fillmode & EVAS_FILTER_FILL_MODE_STRETCH_Y)
          mh = h;

        BUFFERS_LOCK();
        fb = evas_filter_buffer_scaled_get(cmd->ctx, cmd->mask, mw, mh);
        BUFFERS_UNLOCK();

        EINA_SAFETY_ON_NULL_RETURN_VAL(fb, EINA_FALSE);
        fb->locked = EINA_FALSE;
        mask = fb->backing;
     }

   msk = mask->image.data8;

   // First pass: copy to dest
   if (src != dst)
     memcpy(dst, src, w * h * sizeof(DATA8));

   // Second pass: apply render op
   func = evas_common_alpha_func_get(render_op);
   for (y = 0, my = 0; y < h; y++, my++, msk += mw)
     {
        if (my >= mh)
          {
             my = 0;
             msk = mask->image.data8;
          }

        for (step = 0; step < stepcount; step++, dst += stepsize)
          func(msk, dst, stepsize);

        x = stepsize * stepcount;
        if (x < w)
          {
             func(msk, dst, w - x);
             dst += w - x;
          }
     }

   return EINA_TRUE;
}

static Eina_Bool
_mask_cpu_rgba_alpha_rgba(Evas_Filter_Command *cmd)
{
   Evas_Filter_Buffer *fb;
   Eina_Bool ok;

   /* Mechanism:
    * 1. Swap input and mask
    * 2. Apply mask operation for alpha+rgba+rgba
    * 3. Swap input and mask
    */

   fb = cmd->input;
   cmd->input = cmd->mask;
   cmd->mask = fb;

   ok = _mask_cpu_alpha_rgba_rgba(cmd);

   fb = cmd->input;
   cmd->input = cmd->mask;
   cmd->mask = fb;

   return ok;
}

static Eina_Bool
_mask_cpu_alpha_rgba_rgba(Evas_Filter_Command *cmd)
{
   RGBA_Gfx_Func func1, func2;
   RGBA_Image *in, *out, *mask;
   DATA8 *src;
   DATA32 *dst, *msk, *span;
   int op = cmd->draw.render_op;
   int w, h, mw, mh, y, my, r;
   int stepsize, stepcount, step;
   DATA32 color2;

   /* Mechanism:
    * 1. Stretch mask as requested in fillmode
    * 2. Render mask to span using input as mask
    * 3. Render span into destination
    *
    * FIXME: Could probably be optimized into a single op :)
    */

   in = (RGBA_Image *) cmd->input->backing;
   out = (RGBA_Image *) cmd->output->backing;
   mask = (RGBA_Image *) cmd->mask->backing;

   w = cmd->input->w;
   h = cmd->input->h;
   mw = cmd->mask->w;
   mh = cmd->mask->h;
   src = in->image.data8;
   dst = out->image.data;

   // Stretch if necessary.
   if ((mw != w || mh != h) && (cmd->draw.fillmode & EVAS_FILTER_FILL_MODE_STRETCH_XY))
     {
        Evas_Filter_Buffer *fb;

        if (cmd->draw.fillmode & EVAS_FILTER_FILL_MODE_STRETCH_X)
          mw = w;
        if (cmd->draw.fillmode & EVAS_FILTER_FILL_MODE_STRETCH_Y)
          mh = h;

        BUFFERS_LOCK();
        fb = evas_filter_buffer_scaled_get(cmd->ctx, cmd->mask, mw, mh);
        BUFFERS_UNLOCK();

        EINA_SAFETY_ON_NULL_RETURN_VAL(fb, EINA_FALSE);
        fb->locked = EINA_FALSE;
        mask = fb->backing;
     }

   color2 = ARGB_JOIN(cmd->draw.A, cmd->draw.R, cmd->draw.G, cmd->draw.B);
   msk = mask->image.data;

   EINA_SAFETY_ON_NULL_RETURN_VAL(src, EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(dst, EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(msk, EINA_FALSE);
   EINA_SAFETY_ON_FALSE_RETURN_VAL((w > 0) && (mw > 0), EINA_FALSE);

   stepsize  = MIN(mw, w);
   stepcount = w / stepsize;
   span = malloc(stepsize * sizeof(DATA32));

   func1 = evas_common_gfx_func_composite_pixel_mask_span_get(mask->cache_entry.flags.alpha, mask->cache_entry.flags.alpha_sparse, out->cache_entry.flags.alpha, 1, EVAS_RENDER_COPY);
   func2 = evas_common_gfx_func_composite_pixel_color_span_get(mask->cache_entry.flags.alpha, mask->cache_entry.flags.alpha_sparse, color2, out->cache_entry.flags.alpha, 1, op);

   // Apply mask using Gfx functions
   for (y = 0, my = 0; y < h; y++, my++, msk += mw)
     {
        if (my >= mh)
          {
             my = 0;
             msk = mask->image.data;
          }

        for (step = 0; step < stepcount; step++, dst += stepsize, src += stepsize)
          {
             memset(span, 0, stepsize * sizeof(DATA32));
             func1(msk, src, 0, span, stepsize);
             func2(span, NULL, color2, dst, stepsize);
          }

        r = w - (stepsize * stepcount);
        if (r > 0)
          {
             memset(span, 0, r * sizeof(DATA32));
             func1(msk, src, 0, span, r);
             func2(span, NULL, color2, dst, r);
             dst += r;
             src += r;
          }
     }

   free(span);
   return EINA_TRUE;
}

static Eina_Bool
_mask_cpu_alpha_alpha_rgba(Evas_Filter_Command *cmd)
{
   RGBA_Gfx_Func func;
   Alpha_Gfx_Func span_func;
   RGBA_Image *in, *out, *mask;
   DATA8 *src, *msk, *span;
   DATA32 *dst;
   DATA32 color;
   int op = cmd->draw.render_op;
   int w, h, mw, mh, y, my, r;
   int stepsize, stepcount, step;

   /* Mechanism:
    * 1. Copy mask to span buffer (1 line)
    * 2. Multiply source by span (so that: span = mask * source)
    * 3. Render span to destination using color (blend)
    *
    * FIXME: Could probably be optimized into a single op :)
    */

   in = (RGBA_Image *) cmd->input->backing;
   out = (RGBA_Image *) cmd->output->backing;
   mask = (RGBA_Image *) cmd->mask->backing;

   w = cmd->input->w;
   h = cmd->input->h;
   mw = cmd->mask->w;
   mh = cmd->mask->h;
   src = in->image.data8;
   dst = out->image.data;
   color = ARGB_JOIN(cmd->draw.A, cmd->draw.R, cmd->draw.G, cmd->draw.B);

   EINA_SAFETY_ON_FALSE_RETURN_VAL((w > 0) && (mw > 0), EINA_FALSE);

   // Stretch if necessary.
   if ((mw != w || mh != h) && (cmd->draw.fillmode & EVAS_FILTER_FILL_MODE_STRETCH_XY))
     {
        Evas_Filter_Buffer *fb;

        if (cmd->draw.fillmode & EVAS_FILTER_FILL_MODE_STRETCH_X)
          mw = w;
        if (cmd->draw.fillmode & EVAS_FILTER_FILL_MODE_STRETCH_Y)
          mh = h;

        BUFFERS_LOCK();
        fb = evas_filter_buffer_scaled_get(cmd->ctx, cmd->mask, mw, mh);
        BUFFERS_UNLOCK();

        EINA_SAFETY_ON_NULL_RETURN_VAL(fb, EINA_FALSE);
        fb->locked = EINA_FALSE;
        mask = fb->backing;
     }

   msk = mask->image.data8;
   stepsize  = MIN(mw, w);
   stepcount = w / stepsize;
   span = malloc(stepsize * sizeof(DATA8));

   func = evas_common_gfx_func_composite_mask_color_span_get(color, out->cache_entry.flags.alpha, 1, op);
   span_func = evas_common_alpha_func_get(EVAS_RENDER_MASK);

   for (y = 0, my = 0; y < h; y++, my++, msk += mw)
     {
        if (my >= mh)
          {
             my = 0;
             msk = mask->image.data8;
          }

        for (step = 0; step < stepcount; step++, dst += stepsize, src += stepsize)
          {
             memcpy(span, msk, stepsize * sizeof(DATA8));
             span_func(src, span, stepsize);
             func(NULL, span, color, dst, stepsize);
          }

        r = w - (stepsize * stepcount);
        if (r > 0)
          {
             memcpy(span, msk, r * sizeof(DATA8));
             span_func(src, span, r);
             func(NULL, span, color, dst, r);
             dst += r;
             src += r;
          }
     }

   free(span);
   return EINA_TRUE;
}

static Eina_Bool
_mask_cpu_rgba_rgba_rgba(Evas_Filter_Command *cmd)
{
   Evas_Filter_Command fake_cmd;
   Evas_Filter_Fill_Mode fillmode;
   Evas_Filter_Apply_Func blend;
   Evas_Filter_Buffer *fb;
   Eina_Bool ret;
   int w, h;

   fake_cmd = *cmd;
   w = cmd->input->w;
   h = cmd->input->h;

   /* Blend 2 rgba images into rgba destination.
    * Mechanism:
    * 1. Copy input to temp (COPY)
    * 2. Blend mask to temp (MUL)
    * 3. Blend temp to output (render_op)
    */

   // Copy
   BUFFERS_LOCK();
   fb = evas_filter_buffer_scaled_get(cmd->ctx, cmd->input, w, h);
   BUFFERS_UNLOCK();
   EINA_SAFETY_ON_NULL_RETURN_VAL(fb, EINA_FALSE);
   fb->locked = EINA_TRUE;

   // Repeat mask if unspecified - NONE is not possible
   fillmode = cmd->draw.fillmode;
   if ((fillmode & (EVAS_FILTER_FILL_MODE_REPEAT_X | EVAS_FILTER_FILL_MODE_STRETCH_X)) == 0)
     {
        DBG("X fillmode not specified: defaults to repeat");
        fillmode |= EVAS_FILTER_FILL_MODE_REPEAT_X;
     }
   if ((fillmode & (EVAS_FILTER_FILL_MODE_REPEAT_Y | EVAS_FILTER_FILL_MODE_STRETCH_Y)) == 0)
     {
        DBG("Y fillmode not specified: defaults to repeat");
        fillmode |= EVAS_FILTER_FILL_MODE_REPEAT_Y;
     }

   // Mask --> Temp
   fake_cmd.input = cmd->mask;
   fake_cmd.mask = NULL;
   fake_cmd.output = fb;
   fake_cmd.draw.render_op = EVAS_RENDER_MUL;
   fake_cmd.draw.fillmode = fillmode;
   blend = evas_filter_blend_cpu_func_get(&fake_cmd);
   EINA_SAFETY_ON_NULL_RETURN_VAL(blend, EINA_FALSE);
   ret = blend(&fake_cmd);
   if (!ret) goto finish;

   // Temp --> Output
   fake_cmd.draw.render_op = EVAS_RENDER_BLEND;
   fake_cmd.input = fb;
   fake_cmd.output = cmd->output;
   fake_cmd.draw.fillmode = EVAS_FILTER_FILL_MODE_NONE;
   blend = evas_filter_blend_cpu_func_get(&fake_cmd);
   EINA_SAFETY_ON_NULL_RETURN_VAL(blend, EINA_FALSE);
   ret = blend(&fake_cmd);

finish:
   fb->locked = EINA_FALSE;
   return ret;
}