summaryrefslogtreecommitdiff
path: root/src/tests/meta-ref-test.c
blob: 4861f3b959cfd64e5f3e03d25f2040fed689819d (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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
 * Copyright (C) 2021 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * The image difference code is originally a reformatted and simplified
 * copy of weston-test-client-helper.c from the weston repository, with
 * the following copyright and license note:
 *
 * Copyright © 2012 Intel Corporation
 * Copyright © 2015 Samsung Electronics Co., Ltd
 * Copyright 2016, 2017 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*
 * To update or initialize reference images for tests, set the
 * MUTTER_REF_TEST_UPDATE environment variable.
 *
 * The MUTTER_REF_TEST_UPDATE is interpreted as a comma seperated list of
 * regular expressions. If the test path matches any of the regular
 * expressions, the test reference image will be updated, unless the
 * existing reference image is pixel identical to the newly created one.
 *
 * Updating test reference images also requires using a software OpenGL
 * renderer, which can be achieved using MESA_LOADER_DRIVER_OVERRIDE=swrast
 *
 * For example, for the test case '/path/to/test/case', run the test
 * inside
 *
 * ```
 * env MESA_LOADER_DRIVER_OVERRIDE=swrast MUTTER_REF_TEST_UPDATE='/path/to/test/case`
 * ```
 *
 */

#include "config.h"

#include "tests/meta-ref-test.h"

#include <cairo.h>
#include <glib.h>

#include "backends/meta-backend-private.h"
#include "backends/meta-crtc.h"
#include "backends/meta-gpu.h"
#include "backends/meta-stage-private.h"
#include "clutter/clutter/clutter-stage-view-private.h"

typedef struct _Range
{
  int a;
  int b;
} Range;

typedef struct _ImageIterator
{
  uint8_t *data;
  int stride;
} ImageIterator;

typedef struct _PixelDiffStat
{
  /* Pixel diff stat channel */
  struct {
    int min_diff;
    int max_diff;
  } ch[4];
} PixelDiffStat;

/**
 * range_get:
 * @range: Range to validate or NULL.
 *
 * Validate and get range.
 *
 * Returns the given range, or {0, 0} for NULL.
 *
 * Will abort if range is invalid, that is a > b.
 */
static Range
range_get (const Range *range)
{
  if (!range)
    return (Range) { 0, 0 };

  g_assert_cmpint (range->a, <=, range->b);
  return *range;
}

static void
image_iterator_init (ImageIterator   *it,
                     cairo_surface_t *image)
{
  it->stride = cairo_image_surface_get_stride (image);
  it->data = cairo_image_surface_get_data (image);

  g_assert_cmpint (cairo_image_surface_get_format (image), ==,
                   CAIRO_FORMAT_ARGB32);
}

static uint32_t *
image_iterator_get_row (ImageIterator *it,
                        int            y)
{
  return (uint32_t *) (it->data + y * it->stride);
}

static gboolean
fuzzy_match_pixels (uint32_t       pix_a,
                    uint32_t       pix_b,
                    const Range   *fuzz,
                    PixelDiffStat *diff_stat)
{
  gboolean ret = TRUE;
  int shift;
  int i;

  for (shift = 0, i = 0; i < 4; shift += 8, i++)
    {
      int val_a = (pix_a >> shift) & 0xffu;
      int val_b = (pix_b >> shift) & 0xffu;
      int d = val_b - val_a;

      if (diff_stat)
        {
          diff_stat->ch[i].min_diff = MIN (diff_stat->ch[i].min_diff, d);
          diff_stat->ch[i].max_diff = MAX (diff_stat->ch[i].max_diff, d);
        }

      if (d < fuzz->a || d > fuzz->b)
        ret = FALSE;
    }

  return ret;
}

static gboolean
compare_images (cairo_surface_t *ref_image,
                cairo_surface_t *result_image,
                const Range     *precision,
                PixelDiffStat   *diff_stat)
{
  Range fuzz = range_get (precision);
  ImageIterator it_ref;
  ImageIterator it_result;
  int x, y;
  uint32_t *pix_ref;
  uint32_t *pix_result;

  g_assert_cmpint (cairo_image_surface_get_width (ref_image), ==,
                   cairo_image_surface_get_width (result_image));
  g_assert_cmpint (cairo_image_surface_get_height (ref_image), ==,
                   cairo_image_surface_get_height (result_image));

  image_iterator_init (&it_ref, ref_image);
  image_iterator_init (&it_result, result_image);

  for (y = 0; y < cairo_image_surface_get_height (ref_image); y++)
    {
      pix_ref = image_iterator_get_row (&it_ref, y);
      pix_result = image_iterator_get_row (&it_result, y);

      for (x = 0; x < cairo_image_surface_get_width (ref_image); x++)
        {
          if (!fuzzy_match_pixels (*pix_ref, *pix_result,
                                   &fuzz, diff_stat))
            return FALSE;

          pix_ref++;
          pix_result++;
        }
    }

  return TRUE;
}

static void
assert_software_rendered (ClutterStageView *stage_view)
{
  MetaRendererView *view = META_RENDERER_VIEW (stage_view);
  MetaCrtc *crtc = meta_renderer_view_get_crtc (view);
  MetaGpu *gpu = meta_crtc_get_gpu (crtc);
  MetaBackend *backend = meta_gpu_get_backend (gpu);

  g_assert_false (meta_backend_is_rendering_hardware_accelerated (backend));
}

static void
capture_view_into (ClutterStageView *view,
                   MetaRectangle    *rect,
                   uint8_t          *buffer,
                   int               stride)
{
  CoglFramebuffer *framebuffer;
  ClutterBackend *backend;
  CoglContext *context;
  CoglBitmap *bitmap;
  cairo_rectangle_int_t view_layout;
  float view_scale;
  float texture_width;
  float texture_height;
  int x, y;

  framebuffer = clutter_stage_view_get_framebuffer (view);

  view_scale = clutter_stage_view_get_scale (view);
  texture_width = roundf (rect->width * view_scale);
  texture_height = roundf (rect->height * view_scale);

  backend = clutter_get_default_backend ();
  context = clutter_backend_get_cogl_context (backend);
  bitmap = cogl_bitmap_new_for_data (context,
                                     texture_width, texture_height,
                                     CLUTTER_CAIRO_FORMAT_ARGB32,
                                     stride,
                                     buffer);

  clutter_stage_view_get_layout (view, &view_layout);

  x = roundf ((rect->x - view_layout.x) * view_scale);
  y = roundf ((rect->y - view_layout.y) * view_scale);
  cogl_framebuffer_read_pixels_into_bitmap (framebuffer,
                                            x, y,
                                            COGL_READ_PIXELS_COLOR_BUFFER,
                                            bitmap);

  cogl_object_unref (bitmap);
}

typedef struct
{
  MetaStageWatch *watch;
  GMainLoop *loop;

  cairo_surface_t *out_image;
} CaptureViewData;

static void
on_after_paint (MetaStage           *stage,
                ClutterStageView    *view,
                ClutterPaintContext *paint_context,
                ClutterFrame        *frame,
                gpointer             user_data)
{
  CaptureViewData *data = user_data;
  MetaRectangle rect;
  float view_scale;
  int texture_width, texture_height;
  cairo_surface_t *image;
  uint8_t *buffer;
  int stride;

  meta_stage_remove_watch (stage, data->watch);
  data->watch = NULL;

  clutter_stage_view_get_layout (view, &rect);
  view_scale = clutter_stage_view_get_scale (view);
  texture_width = roundf (rect.width * view_scale);
  texture_height = roundf (rect.height * view_scale);
  image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                      texture_width, texture_height);
  cairo_surface_set_device_scale (image, view_scale, view_scale);

  buffer = cairo_image_surface_get_data (image);
  stride = cairo_image_surface_get_stride (image);

  capture_view_into (view, &rect, buffer, stride);

  data->out_image = image;

  cairo_surface_mark_dirty (data->out_image);

  g_main_loop_quit (data->loop);
}

static cairo_surface_t *
capture_view (ClutterStageView *stage_view)
{
  MetaRendererView *view = META_RENDERER_VIEW (stage_view);
  MetaCrtc *crtc = meta_renderer_view_get_crtc (view);
  MetaBackend *backend = meta_crtc_get_backend (crtc);
  MetaStage *stage = META_STAGE (meta_backend_get_stage (backend));
  CaptureViewData data = { 0 };

  data.loop = g_main_loop_new (NULL, FALSE);
  data.watch = meta_stage_watch_view (stage, stage_view,
                                      META_STAGE_WATCH_AFTER_PAINT,
                                      on_after_paint,
                                      &data);
  clutter_stage_view_add_redraw_clip (stage_view, NULL);
  clutter_stage_view_schedule_update (stage_view);

  g_main_loop_run (data.loop);
  g_main_loop_unref (data.loop);

  g_assert_null (data.watch);
  g_assert_nonnull (data.out_image);

  return data.out_image;
}

static void
depathify (char *path)
{
  int len = strlen (path);
  int i;

  for (i = 0; i < len; i++)
    {
      if (path[i] == '/')
        path[i] = '_';
    }
}

static void
ensure_expected_format (cairo_surface_t **ref_image)
{
  int width, height;
  cairo_surface_t *target;
  cairo_t *cr;

  if (cairo_image_surface_get_format (*ref_image) ==
      CAIRO_FORMAT_ARGB32)
    return;

  width = cairo_image_surface_get_width (*ref_image);
  height = cairo_image_surface_get_height (*ref_image);
  target = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);

  cr = cairo_create (target);
  cairo_set_source_surface (cr, *ref_image, 0.0, 0.0);
  cairo_paint (cr);
  cairo_destroy (cr);

  cairo_surface_destroy (*ref_image);
  *ref_image = target;
}

/*
 * Tint a color:
 * @src Source pixel as x8r8g8b8.
 * @add The tint as x8r8g8b8, x8 must be zero; r8, g8 and b8 must be
 * no greater than 0xc0 to avoid overflow to another channel.
 * Returns: The tinted pixel color as x8r8g8b8, x8 guaranteed to be 0xff.
 *
 * The source pixel RGB values are divided by 4, and then the tint is added.
 * To achieve colors outside of the range of src, a tint color channel must be
 * at least 0x40. (0xff / 4 = 0x3f, 0xff - 0x3f = 0xc0)
 */
static uint32_t
tint (uint32_t src,
      uint32_t add)
{
  uint32_t v;

  v = ((src & 0xfcfcfcfc) >> 2) | 0xff000000;

  return v + add;
}

static cairo_surface_t *
visualize_difference (cairo_surface_t *ref_image,
                      cairo_surface_t *result_image,
                      const Range     *precision)
{
  Range fuzz = range_get (precision);
  int width, height;
  cairo_surface_t *diff_image;
  cairo_t *cr;
  ImageIterator it_ref;
  ImageIterator it_result;
  ImageIterator it_diff;
  int y;

  width = cairo_image_surface_get_width (ref_image);
  height = cairo_image_surface_get_height (ref_image);

  diff_image = cairo_surface_create_similar_image (ref_image,
                                                   CAIRO_FORMAT_ARGB32,
                                                   width,
                                                   height);
  cr = cairo_create (diff_image);
  cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
  cairo_paint (cr);
  cairo_set_source_surface (cr, ref_image, 0.0, 0.0);
  cairo_set_operator (cr, CAIRO_OPERATOR_HSL_LUMINOSITY);
  cairo_paint (cr);
  cairo_destroy (cr);

  image_iterator_init (&it_ref, ref_image);
  image_iterator_init (&it_result, result_image);
  image_iterator_init (&it_diff, diff_image);

  for (y = 0; y < cairo_image_surface_get_height (ref_image); y++)
    {
      uint32_t *ref_pixel;
      uint32_t *result_pixel;
      uint32_t *diff_pixel;
      int x;

      ref_pixel = image_iterator_get_row (&it_ref, y);
      result_pixel = image_iterator_get_row (&it_result, y);
      diff_pixel = image_iterator_get_row (&it_diff, y);

      for (x = 0; x < cairo_image_surface_get_width (ref_image); x++)
        {
          if (fuzzy_match_pixels (*ref_pixel, *result_pixel,
                                  &fuzz, NULL))
            *diff_pixel = tint (*diff_pixel, 0x00008000); /* green */
          else
            *diff_pixel = tint (*diff_pixel, 0x00c00000); /* red */

          ref_pixel++;
          result_pixel++;
          diff_pixel++;
        }
    }

  return diff_image;
}

void
meta_ref_test_verify_view (ClutterStageView *view,
                           const char       *test_name_unescaped,
                           int               test_seq_no,
                           MetaReftestFlag   flags)
{
  cairo_surface_t *view_image;
  const char *dist_dir;
  g_autofree char *test_name = NULL;
  g_autofree char *ref_image_path = NULL;
  cairo_surface_t *ref_image;
  cairo_status_t ref_status;

  if (flags & META_REFTEST_FLAG_UPDATE_REF)
    assert_software_rendered (view);

  view_image = capture_view (view);

  test_name = g_strdup (test_name_unescaped + 1);
  depathify (test_name);

  dist_dir = g_test_get_dir (G_TEST_DIST);
  ref_image_path = g_strdup_printf ("%s/tests/ref-tests/%s_%d.ref.png",
                                    dist_dir,
                                    test_name, test_seq_no);

  ref_image = cairo_image_surface_create_from_png (ref_image_path);
  g_assert_nonnull (ref_image);
  ref_status = cairo_surface_status (ref_image);

  if (flags & META_REFTEST_FLAG_UPDATE_REF)
    {
      g_assert (ref_status == CAIRO_STATUS_FILE_NOT_FOUND ||
                ref_status == CAIRO_STATUS_SUCCESS);

      if (ref_status == CAIRO_STATUS_SUCCESS)
        ensure_expected_format (&ref_image);

      if (ref_status == CAIRO_STATUS_SUCCESS &&
          cairo_image_surface_get_width (ref_image) ==
          cairo_image_surface_get_width (view_image) &&
          cairo_image_surface_get_height (ref_image) ==
          cairo_image_surface_get_height (view_image) &&
          compare_images (ref_image, view_image, NULL, NULL))
        {
          g_message ("Not updating '%s', it didn't change.", ref_image_path);
        }
      else
        {
          g_message ("Updating '%s'.", ref_image_path);
          g_assert_cmpint (cairo_surface_write_to_png (view_image, ref_image_path),
                           ==,
                           CAIRO_STATUS_SUCCESS);
        }
    }
  else
    {
      const Range gl_fuzz = { -3, 4 };
      PixelDiffStat diff_stat = {};

      g_assert_cmpint (ref_status, ==, CAIRO_STATUS_SUCCESS);
      ensure_expected_format (&ref_image);

      if (!compare_images (ref_image, view_image, &gl_fuzz,
                           &diff_stat))
        {
          cairo_surface_t *diff_image;
          const char *build_dir;
          g_autofree char *ref_image_copy_path = NULL;
          g_autofree char *result_image_path = NULL;
          g_autofree char *diff_image_path = NULL;

          diff_image = visualize_difference (ref_image, view_image,
                                             &gl_fuzz);

          build_dir = g_test_get_dir (G_TEST_BUILT);
          ref_image_copy_path =
            g_strdup_printf ("%s/meson-logs/tests/ref-tests/%s_%d.ref.png",
                             build_dir,
                             test_name, test_seq_no);
          result_image_path =
            g_strdup_printf ("%s/meson-logs/tests/ref-tests/%s_%d.result.png",
                             build_dir,
                             test_name, test_seq_no);
          diff_image_path =
            g_strdup_printf ("%s/meson-logs/tests/ref-tests/%s_%d.diff.png",
                             build_dir,
                             test_name, test_seq_no);

          g_mkdir_with_parents (g_path_get_dirname (ref_image_copy_path),
                                0755);

          g_assert_cmpint (cairo_surface_write_to_png (ref_image,
                                                       ref_image_copy_path),
                           ==,
                           CAIRO_STATUS_SUCCESS);
          g_assert_cmpint (cairo_surface_write_to_png (view_image,
                                                       result_image_path),
                           ==,
                           CAIRO_STATUS_SUCCESS);
          g_assert_cmpint (cairo_surface_write_to_png (diff_image,
                                                       diff_image_path),
                           ==,
                           CAIRO_STATUS_SUCCESS);

          g_critical ("Pixel difference exceeds limits "
                      "(min: [%d, %d, %d, %d], "
                      "max: [%d, %d, %d, %d])\n"
                      "See %s, %s, and %s for details.",
                      diff_stat.ch[0].min_diff,
                      diff_stat.ch[1].min_diff,
                      diff_stat.ch[2].min_diff,
                      diff_stat.ch[3].min_diff,
                      diff_stat.ch[0].max_diff,
                      diff_stat.ch[1].max_diff,
                      diff_stat.ch[2].max_diff,
                      diff_stat.ch[3].max_diff,
                      ref_image_copy_path,
                      result_image_path,
                      diff_image_path);
        }
    }

  cairo_surface_destroy (view_image);
  cairo_surface_destroy (ref_image);
}

MetaReftestFlag
meta_ref_test_determine_ref_test_flag (void)
{
  const char *update_tests;
  char **update_test_rules;
  int n_update_test_rules;
  MetaReftestFlag flags;
  int i;

  update_tests = g_getenv ("MUTTER_REF_TEST_UPDATE");
  if (!update_tests)
    return META_REFTEST_FLAG_NONE;

  if (strcmp (update_tests, "all") == 0)
    return META_REFTEST_FLAG_UPDATE_REF;

  update_test_rules = g_strsplit (update_tests, ",", -1);
  n_update_test_rules = g_strv_length (update_test_rules);
  g_assert_cmpint (n_update_test_rules, >, 0);

  flags = META_REFTEST_FLAG_NONE;
  for (i = 0; i < n_update_test_rules; i++)
    {
      char *rule = update_test_rules[i];

      if (g_regex_match_simple (rule, g_test_get_path (), 0, 0))
        {
          flags |= META_REFTEST_FLAG_UPDATE_REF;
          break;
        }
    }

  g_strfreev (update_test_rules);

  return flags;
}