summaryrefslogtreecommitdiff
path: root/tests/conform/test-atlas-migration.c
blob: 8b6cd985507a8dee2f0be3df68a8ef169cfb6172 (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
#include <clutter/clutter.h>

#include "test-conform-common.h"

#define N_TEXTURES 128

#define OPACITY_FOR_ROW(y) \
  (0xff - ((y) & 0xf) * 0x10)

#define COLOR_FOR_SIZE(size) \
  (colors + (size) % 3)

static const ClutterColor colors[] =
  { { 0xff, 0x00, 0x00, 0xff },
    { 0x00, 0xff, 0x00, 0xff },
    { 0x00, 0x00, 0xff, 0xff } };

static CoglHandle
create_texture (int size)
{
  CoglHandle texture;
  const ClutterColor *color;
  guint8 *data, *p;
  int x, y;

  /* Create a red, green or blue texture depending on the size */
  color = COLOR_FOR_SIZE (size);

  p = data = g_malloc (size * size * 4);

  /* Fill the data with the color but fade the opacity out with
     increasing y coordinates so that we can see the blending it the
     atlas migration accidentally blends with garbage in the
     texture */
  for (y = 0; y < size; y++)
    {
      int opacity = OPACITY_FOR_ROW (y);

      for (x = 0; x < size; x++)
        {
          /* Store the colors premultiplied */
          p[0] = color->red * opacity / 255;
          p[1] = color->green * opacity / 255;
          p[2] = color->blue * opacity / 255;
          p[3] = opacity;

          p += 4;
        }
    }

  texture = cogl_texture_new_from_data (size, /* width */
                                        size, /* height */
                                        COGL_TEXTURE_NONE, /* flags */
                                        /* format */
                                        COGL_PIXEL_FORMAT_RGBA_8888,
                                        /* internal format */
                                        COGL_PIXEL_FORMAT_RGBA_8888,
                                        /* rowstride */
                                        size * 4,
                                        data);

  g_free (data);

  return texture;
}

static void
verify_texture (CoglHandle texture, int size)
{
  guint8 *data, *p;
  int x, y;
  const ClutterColor *color;

  color = COLOR_FOR_SIZE (size);

  p = data = g_malloc (size * size * 4);

  cogl_texture_get_data (texture,
                         COGL_PIXEL_FORMAT_RGBA_8888,
                         size * 4,
                         data);

  for (y = 0; y < size; y++)
    {
      int opacity = OPACITY_FOR_ROW (y);

      for (x = 0; x < size; x++)
        {
          g_assert_cmpint (p[0], ==, color->red * opacity / 255);
          g_assert_cmpint (p[1], ==, color->green * opacity / 255);
          g_assert_cmpint (p[2], ==, color->blue * opacity / 255);
          g_assert_cmpint (p[3], ==, opacity);

          p += 4;
        }
    }

  g_free (data);
}

void
test_cogl_atlas_migration (TestUtilsGTestFixture *fixture,
                           void *data)
{
  CoglHandle textures[N_TEXTURES];
  int i, tex_num;

  /* Create and destroy all of the textures a few times to increase
     the chances that we'll end up reusing the buffers for previously
     discarded atlases */
  for (i = 0; i < 5; i++)
    {
      for (tex_num = 0; tex_num < N_TEXTURES; tex_num++)
        textures[tex_num] = create_texture (tex_num + 1);
      for (tex_num = 0; tex_num < N_TEXTURES; tex_num++)
        cogl_object_unref (textures[tex_num]);
    }

  /* Create all the textures again */
  for (tex_num = 0; tex_num < N_TEXTURES; tex_num++)
    textures[tex_num] = create_texture (tex_num + 1);

  /* Verify that they all still have the right data */
  for (tex_num = 0; tex_num < N_TEXTURES; tex_num++)
    verify_texture (textures[tex_num], tex_num + 1);

  /* Destroy them all */
  for (tex_num = 0; tex_num < N_TEXTURES; tex_num++)
    cogl_object_unref (textures[tex_num]);

  if (g_test_verbose ())
    g_print ("OK\n");
}