summaryrefslogtreecommitdiff
path: root/src/tests/evas/evas_test_mask.c
blob: b3dd17b42c59a64f2af9c8c51e3f694720f91ed9 (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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef BUILD_ENGINE_BUFFER

#include <Evas.h>
#include <Ecore_Evas.h>

#include "evas_suite.h"
#include "evas_tests_helpers.h"

#define TEST_FONT_NAME "DejaVuSans,UnDotum"
#define TEST_FONT_SOURCE TESTS_SRC_DIR "/TestFont.eet"

#define START_MASK_TEST(w, h) \
   Ecore_Evas *ee; Evas *e; \
   evas_init(); \
   ecore_evas_init(); \
   ee = ecore_evas_buffer_new(w, h); \
   ecore_evas_show(ee); \
   ecore_evas_manual_render_set(ee, EINA_TRUE); \
   e = ecore_evas_get(ee); \
   Eina_List *tofree = NULL; \
   do {} while (0)

#define END_MASK_TEST() do { \
   Evas_Object *o; \
   EINA_LIST_FREE(tofree, o) { evas_object_del(o); } \
   ecore_evas_free(ee); \
   ecore_evas_shutdown(); \
   evas_shutdown(); \
   } while (0)

#define AUTODEL(o) do { tofree = eina_list_prepend(tofree, o); } while (0)

static int
_bgra_compare(unsigned int *data, unsigned int *ref, int w, int h)
{
   int i, j, ret = 0;
   for (j = 0; j < h; j++)
     {
#if 0
        printf("data: ");
        for (i = 0; i < w; i++) printf("%#x ", data[i+j*w]);
        printf("\nref:  ");
        for (i = 0; i < w; i++) printf("%#x ", ref[i+j*w]);
        printf("\n\n");
#endif
        for (i = 0; i < w; i++)
          if (data[i+j*w] != ref[i+j*w])
            {
               printf("Pixel %2dx%-2d differ: %#x vs. %#x\n", i, j, data[i+j*w], ref[i+j*w]);
               fflush(stdout);
               ret = 1;
            }
     }
   return ret;
}

// The usual useless unit test
START_TEST(evas_mask_test_setget)
{
   Evas *e = _setup_evas();
   Evas_Object *obj = NULL, *mask = NULL;
   const Eina_List *list, *l;
   Eina_Iterator *it;
   Evas_Object *o;
   int i;

   obj = evas_object_text_add(e);
   fail_if(evas_object_clip_get(obj) != NULL);

   mask = evas_object_image_filled_add(e);
   evas_object_clip_set(obj, mask);
   fail_if(evas_object_clip_get(obj) != mask);

   list = evas_object_clipees_get(mask);
   i = 0;
   EINA_LIST_FOREACH(list, l, o)
     {
        if (!i) fail_if(o != obj);
        i++;
     }
   fail_if(i != 1);

   it = efl_canvas_object_clipees_get(mask);
   i = 0;
   EINA_ITERATOR_FOREACH(it, o)
     {
        if (!i) fail_if(o != obj);
        i++;
     }
   fail_if(i != 1);
   eina_iterator_free(it);

   evas_object_clip_unset(obj);
   fail_if(evas_object_clip_get(obj) != NULL);

   evas_object_del(mask);
   evas_object_del(obj);

   evas_free(e);
   evas_shutdown();
}
END_TEST

// This test is also very basic but will check the pixels
START_TEST(evas_mask_test_compare_clip)
{
   Evas_Object *obj, *mask, *rect, *bg, *clip, *text;
   unsigned int *data, *refdata[3];
   const int W = 64;
   const int H = 64;
   int i, tw, th;

   static unsigned int mask_data[] = {
      0x00000000, 0x00000000, 0x00000000, 0x00000000,
      0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000,
      0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000,
      0x00000000, 0x00000000, 0x00000000, 0x00000000,
   };

   static unsigned int ref_data[2][16] = {
      // blue masked with alpha 0xFF over green bg
      {
         0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00,
         0xFF00FF00, 0xFF0000FF, 0xFF0000FF, 0xFF00FF00,
         0xFF00FF00, 0xFF0000FF, 0xFF0000FF, 0xFF00FF00,
         0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00,
      },
      // blue masked with alpha 0x80 over green bg
      {
         0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00,
         0xFF00FF00, 0xFF007F80, 0xFF007F80, 0xFF00FF00,
         0xFF00FF00, 0xFF007F80, 0xFF007F80, 0xFF00FF00,
         0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00,
      }
   };

   START_MASK_TEST(W, H);
   printf("Testing basic mask render... ");

   // Create reference data -- scaled up images
   obj = evas_object_image_filled_add(e);
   evas_object_image_smooth_scale_set(obj, 0);
   evas_object_image_size_set(obj, 4, 4);
   evas_object_image_colorspace_set(obj, EVAS_COLORSPACE_ARGB8888);
   evas_object_image_data_copy_set(obj, ref_data[0]);
   evas_object_geometry_set(obj, 0, 0, W, H);
   evas_object_show(obj);
   AUTODEL(obj);

   ecore_evas_manual_render(ee);
   refdata[0] = calloc(W * H, 4);
   memcpy(refdata[0], ecore_evas_buffer_pixels_get(ee), W * H * 4);

   evas_object_image_data_copy_set(obj, ref_data[1]);
   ecore_evas_manual_render(ee);
   refdata[1] = calloc(W * H, 4);
   memcpy(refdata[1], ecore_evas_buffer_pixels_get(ee), W * H * 4);

   evas_object_hide(obj);

   // Green background
   bg = evas_object_rectangle_add(e);
   evas_object_geometry_set(bg, 0, 0, W, H);
   evas_object_color_set(bg, 0, 0xFF, 0, 0xFF);
   evas_object_show(bg);
   AUTODEL(bg);

   // Blue rectangle
   rect = evas_object_rectangle_add(e);
   evas_object_geometry_set(rect, 0, 0, W, H);
   evas_object_color_set(rect, 0, 0, 0xFF, 0xFF);
   evas_object_show(rect);
   AUTODEL(rect);

   // Mask image
   mask = evas_object_image_filled_add(e);
   evas_object_image_smooth_scale_set(mask, 0);
   evas_object_image_size_set(mask, 4, 4);
   evas_object_image_colorspace_set(mask, EVAS_COLORSPACE_ARGB8888);
   evas_object_image_data_copy_set(mask, mask_data);
   evas_object_geometry_set(mask, 0, 0, W, H);
   evas_object_clip_set(rect, mask);
   evas_object_show(mask);
   AUTODEL(mask);

   // Compare dump data
   data = calloc(W * H, 4);
   ecore_evas_manual_render(ee);
   memcpy(data, ecore_evas_buffer_pixels_get(ee), W * H * 4);
   fail_if(_bgra_compare(data, refdata[0], W,  H) != 0);

   // Try again with mask alpha = 0x80
   evas_object_color_set(mask, 0x80, 0x80, 0x80, 0x80);
   ecore_evas_manual_render(ee);
   memcpy(data, ecore_evas_buffer_pixels_get(ee), W * H * 4);

   evas_object_image_data_copy_set(obj, ref_data[1]);
   evas_object_show(obj);
   ecore_evas_manual_render(ee);
   refdata[1] = calloc(W * H, 4);
   memcpy(refdata[1], ecore_evas_buffer_pixels_get(ee), W * H * 4);
   fail_if(_bgra_compare(data, refdata[1], W, H) != 0);

   // Now try again with a clip instead - this verifies clip == mask
   clip = evas_object_rectangle_add(e);
   evas_object_geometry_set(clip, W / 4, H / 4, W / 2, H / 2);
   evas_object_color_set(clip, 0xFF, 0xFF, 0xFF, 0xFF);
   evas_object_clip_set(rect, clip);
   evas_object_hide(mask);
   evas_object_show(clip);

   ecore_evas_manual_render(ee);
   memcpy(data, ecore_evas_buffer_pixels_get(ee), W * H * 4);
   fail_if(_bgra_compare(data, refdata[0], W, H));

   evas_object_color_set(clip, 0x80, 0x80, 0x80, 0x80);
   ecore_evas_manual_render(ee);
   memcpy(data, ecore_evas_buffer_pixels_get(ee), W * H * 4);
   fail_if(_bgra_compare(data, refdata[1], W, H));

   // Reset objects
   evas_object_hide(rect);
   evas_object_hide(clip);
   evas_object_hide(mask);

   // Text masking test
   text = evas_object_text_add(e);
   evas_object_text_font_source_set(text, TEST_FONT_SOURCE);
   evas_object_text_font_set(text, TEST_FONT_NAME, 20);
   evas_object_text_text_set(text, "TEXT MASKING SHOULD CUT");
   evas_object_color_set(text, 0xFF, 0xFF, 0xFF, 0xFF);
   evas_object_geometry_get(text, NULL, NULL, &tw, &th);
   evas_object_geometry_set(text, W/2 - tw/2, H/2 - th/2, tw, th);
   evas_object_show(text);
   evas_object_show(clip);
   evas_object_color_set(clip, 0xFF, 0xFF, 0xFF, 0xFF);
   evas_object_clip_set(text, clip);
   ecore_evas_manual_render(ee);
   refdata[2] = calloc(W * H, 4);
   memcpy(refdata[2], ecore_evas_buffer_pixels_get(ee), W * H * 4);

   evas_object_color_set(mask, 0xFF, 0xFF, 0xFF, 0xFF);
   evas_object_clip_set(text, mask);
   evas_object_hide(clip);
   evas_object_show(mask);
   ecore_evas_manual_render(ee);
   memcpy(data, ecore_evas_buffer_pixels_get(ee), W * H * 4);

   fail_if(_bgra_compare(data, refdata[2], W, H));

   printf("PASSED!\n");
   for (i = 0; i < 3; i++) free(refdata[i]);
   free(data);
   END_MASK_TEST();
}
END_TEST

// This will simply check that a mask is recursively applied to children
START_TEST(evas_mask_test_mask_of_mask)
{
   Evas_Object *bg, *tbl, *rect0, *mask0, *mask1, *obj;
   unsigned int *data, *refdata;
   const int W = 16;
   const int H = 16;

   // Note: This test isn't great as a mask will hijack the clipper
   // set by the table to its children.

   static unsigned int mask_data[3][16] =
   {
      // Table's mask
      {
         0x00000000, 0x00000000, 0x00000000, 0x00000000,
         0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000,
         0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000,
         0x00000000, 0x00000000, 0x00000000, 0x00000000,
      },
      // Rect's mask
      {
         0x00000000, 0x00000000, 0x00000000, 0x00000000,
         0x00000000, 0x00000000, 0x00000000, 0x00000000,
         0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
         0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
      },
      // Reference image with colors
      {
         0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00,
         0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00,
         0xFF00FF00, 0xFF00FF00, 0xFFFF00FF, 0xFF00FF00, // look here!
         0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00,
      }
   };

   START_MASK_TEST(W, H);
   printf("Testing basic masks of masks... ");

   // Green background
   bg = evas_object_rectangle_add(e);
   evas_object_geometry_set(bg, 0, 0, W, H);
   evas_object_color_set(bg, 0, 0xFF, 0, 0xFF);
   evas_object_show(bg);
   AUTODEL(bg);

   // Table
   tbl = evas_object_table_add(e);
   evas_object_geometry_set(tbl, 0, 0, W, H);
   evas_object_table_homogeneous_set(tbl, EVAS_OBJECT_TABLE_HOMOGENEOUS_TABLE);
   evas_object_show(tbl);
   AUTODEL(tbl);

   // Table's mask
   mask0 = evas_object_image_filled_add(e);
   evas_object_image_smooth_scale_set(mask0, 0);
   evas_object_image_size_set(mask0, 4, 4);
   evas_object_image_colorspace_set(mask0, EVAS_COLORSPACE_ARGB8888);
   evas_object_image_data_copy_set(mask0, mask_data[0]);
   evas_object_geometry_set(mask0, 0, 0, W, H);
   evas_object_show(mask0);
   AUTODEL(mask0);

   evas_object_clip_set(tbl, mask0);

   // Rect is table's content
   rect0 = evas_object_rectangle_add(e);
   evas_object_color_set(rect0, 255, 0, 255, 255);
   evas_object_show(rect0);

   evas_object_size_hint_expand_set(rect0, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   evas_object_size_hint_fill_set(rect0, EVAS_HINT_FILL, EVAS_HINT_FILL);
   evas_object_table_pack(tbl, rect0, 0, 0, 1, 1);

   // mask1 is also table content
   mask1 = evas_object_image_filled_add(e);
   evas_object_image_smooth_scale_set(mask1, 0);
   evas_object_image_size_set(mask1, 4, 4);
   evas_object_image_colorspace_set(mask1, EVAS_COLORSPACE_ARGB8888);
   evas_object_image_data_copy_set(mask1, mask_data[1]);
   evas_object_show(mask1);

   evas_object_size_hint_expand_set(mask1, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   evas_object_size_hint_fill_set(mask1, EVAS_HINT_FILL, EVAS_HINT_FILL);
   evas_object_table_pack(tbl, mask1, 0, 0, 1, 1);

   // BAAAAD: Hijack rect0's clipper
   obj = evas_object_clip_get(rect0);
   evas_object_clip_set(rect0, mask1);
   if (!evas_object_clipees_has(obj))
     evas_object_hide(obj);

   // Screenshot mask of mask
   data = calloc(W * H, 4);
   ecore_evas_manual_render(ee);
   memcpy(data, ecore_evas_buffer_pixels_get(ee), W * H * 4);

   // Render reference image
   obj = evas_object_image_filled_add(e);
   evas_object_image_smooth_scale_set(obj, 0);
   evas_object_image_size_set(obj, 4, 4);
   evas_object_image_colorspace_set(obj, EVAS_COLORSPACE_ARGB8888);
   evas_object_image_data_copy_set(obj, mask_data[2]);
   evas_object_geometry_set(obj, 0, 0, W, H);
   evas_object_show(obj);
   AUTODEL(obj);

   refdata = calloc(W * H, 4);
   ecore_evas_manual_render(ee);
   memcpy(refdata, ecore_evas_buffer_pixels_get(ee), W * H * 4);
   fail_if(_bgra_compare(data, refdata, W, H) != 0);

   printf("PASSED!\n");
   free(refdata);
   free(data);
   END_MASK_TEST();
}
END_TEST

// NOTE: Much more extensive tests are required. But they should
// be based on "exactness" or a pixel similarity tool.
// The GL engine is not tested at all. Even masking images is not tested...

void evas_test_mask(TCase *tc)
{
   tcase_add_test(tc, evas_mask_test_setget);
   tcase_add_test(tc, evas_mask_test_compare_clip);
   tcase_add_test(tc, evas_mask_test_mask_of_mask);
}

#endif // BUILD_ENGINE_BUFFER