summaryrefslogtreecommitdiff
path: root/testsuite/gdk/memorytexture.c
blob: 950de41b0a94f460ece3c2a4aa3d545939f91b45 (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
#include <locale.h>
#include <gdk/gdk.h>

/* maximum bytes per pixel */
#define MAX_BPP 4

typedef enum {
  BLUE,
  GREEN,
  RED,
  TRANSPARENT,
  ALMOST_OPAQUE_REBECCAPURPLE,
  N_COLORS
} Color;

const char * color_names[N_COLORS] = {
  "blue",
  "green",
  "red",
  "transparent",
  "almost_opaque_rebeccapurple"
};

typedef struct _MemoryData {
  gsize bytes_per_pixel;
  guint opaque : 1;
  guchar data[N_COLORS][MAX_BPP];
} MemoryData;

typedef struct _TestData {
  GdkMemoryFormat format;
  Color color;
} TestData;

#define RGBA(a, b, c, d) { 0x ## a, 0x ## b, 0x ## c, 0x ## d }

static MemoryData tests[GDK_MEMORY_N_FORMATS] = {
  { 4, FALSE, { RGBA(FF,00,00,FF), RGBA(00,FF,00,FF), RGBA(00,00,FF,FF), RGBA(00,00,00,00), RGBA(66,22,44,AA) } },
  { 4, FALSE, { RGBA(FF,00,00,FF), RGBA(FF,00,FF,00), RGBA(FF,FF,00,00), RGBA(00,00,00,00), RGBA(AA,44,22,66) } },
  { 4, FALSE, { RGBA(00,00,FF,FF), RGBA(00,FF,00,FF), RGBA(FF,00,00,FF), RGBA(00,00,00,00), RGBA(44,22,66,AA) } },
  { 4, FALSE, { RGBA(FF,00,00,FF), RGBA(00,FF,00,FF), RGBA(00,00,FF,FF), RGBA(00,00,00,00), RGBA(99,33,66,AA) } },
  { 4, FALSE, { RGBA(FF,00,00,FF), RGBA(FF,00,FF,00), RGBA(FF,FF,00,00), RGBA(00,00,00,00), RGBA(AA,66,33,99) } },
  { 4, FALSE, { RGBA(00,00,FF,FF), RGBA(00,FF,00,FF), RGBA(FF,00,00,FF), RGBA(00,00,00,00), RGBA(66,33,99,AA) } },
  { 4, FALSE, { RGBA(FF,FF,00,00), RGBA(FF,00,FF,00), RGBA(FF,00,00,FF), RGBA(00,00,00,00), RGBA(AA,99,33,66) } },
  { 3, TRUE,  { RGBA(00,00,FF,00), RGBA(00,FF,00,00), RGBA(FF,00,00,00), RGBA(00,00,00,00), RGBA(44,22,66,00) } },
  { 3, TRUE,  { RGBA(FF,00,00,00), RGBA(00,FF,00,00), RGBA(00,00,FF,00), RGBA(00,00,00,00), RGBA(66,22,44,00) } },
};

static void
compare_textures (GdkTexture *expected,
                  GdkTexture *test,
                  gboolean    ignore_alpha)
{
  guchar *expected_data, *test_data;
  int width, height;
  int x, y;

  g_assert_cmpint (gdk_texture_get_width (expected), ==, gdk_texture_get_width (test));
  g_assert_cmpint (gdk_texture_get_height (expected), ==, gdk_texture_get_height (test));

  width = gdk_texture_get_width (expected);
  height = gdk_texture_get_height (expected);

  expected_data = g_malloc (width * height * 4);
  gdk_texture_download (expected, expected_data, width * 4);

  test_data = g_malloc (width * height * 4);
  gdk_texture_download (test, test_data, width * 4);

  for (y = 0; y < height; y++)
    {
      for (x = 0; x < width; x++)
        {
          if (ignore_alpha)
            g_assert_cmphex (*(guint32 *) &expected_data[y * width + x * 4] & 0xFFFFFF, ==, *(guint32 *) &test_data[y * width + x * 4] & 0xFFFFFF);
          else
            g_assert_cmphex (*(guint32 *) &expected_data[y * width + x * 4], ==, *(guint32 *) &test_data[y * width + x * 4]);
        }
    }

  g_free (expected_data);
  g_free (test_data);
}

static GdkTexture *
create_texture (GdkMemoryFormat  format,
                Color            color,
                int              width,
                int              height,
                gsize            stride)
{
  GdkTexture *texture;
  GBytes *bytes;
  guchar *data;
  int x, y;

  data = g_malloc (height * MAX (stride, tests[format].bytes_per_pixel));
  for (y = 0; y < height; y++)
    for (x = 0; x < width; x++)
      {
        memcpy (&data[y * stride + x * tests[format].bytes_per_pixel],
                &tests[format].data[color],
                tests[format].bytes_per_pixel);
      }

  bytes = g_bytes_new_take (data, height * MAX (stride, tests[format].bytes_per_pixel));
  texture = gdk_memory_texture_new (width, height,
                                    format,
                                    bytes,
                                    stride);
  g_bytes_unref (bytes);

  return texture;
}

static void
test_download_1x1 (gconstpointer data)
{
  const TestData *test_data = data;
  GdkTexture *expected, *test;

  expected = create_texture (GDK_MEMORY_DEFAULT, test_data->color, 1, 1, tests[test_data->format].bytes_per_pixel);
  test = create_texture (test_data->format, test_data->color, 1, 1, tests[test_data->format].bytes_per_pixel);

  compare_textures (expected, test, tests[test_data->format].opaque);

  g_object_unref (expected);
  g_object_unref (test);
}

static void
test_download_1x1_with_stride (gconstpointer data)
{
  const TestData *test_data = data;
  GdkTexture *expected, *test;

  expected = create_texture (GDK_MEMORY_DEFAULT, test_data->color, 1, 1, 4);
  test = create_texture (test_data->format, test_data->color, 1, 1, 2 * MAX_BPP);

  compare_textures (expected, test, tests[test_data->format].opaque);

  g_object_unref (expected);
  g_object_unref (test);
}

static void
test_download_4x4 (gconstpointer data)
{
  const TestData *test_data = data;
  GdkTexture *expected, *test;

  expected = create_texture (GDK_MEMORY_DEFAULT, test_data->color, 4, 4, 16);
  test = create_texture (test_data->format, test_data->color, 4, 4, 4 * tests[test_data->format].bytes_per_pixel);

  compare_textures (expected, test, tests[test_data->format].opaque);

  g_object_unref (expected);
  g_object_unref (test);
}

static void
test_download_4x4_with_stride (gconstpointer data)
{
  const TestData *test_data = data;
  GdkTexture *expected, *test;

  expected = create_texture (GDK_MEMORY_DEFAULT, test_data->color, 4, 4, 16);
  test = create_texture (test_data->format, test_data->color, 4, 4, 4 * MAX_BPP);

  compare_textures (expected, test, tests[test_data->format].opaque);

  g_object_unref (expected);
  g_object_unref (test);
}

int
main (int argc, char *argv[])
{
  GdkMemoryFormat format;
  Color color;
  GEnumClass *enum_class;

  g_test_init (&argc, &argv, NULL);

  enum_class = g_type_class_ref (GDK_TYPE_MEMORY_FORMAT);

  for (format = 0; format < GDK_MEMORY_N_FORMATS; format++)
    {
      for (color = 0; color < N_COLORS; color++)
        {
          TestData *test_data = g_new (TestData, 1);
          char *test_name = g_strdup_printf ("/memorytexture/download_1x1/%s/%s",
                                             g_enum_get_value (enum_class, format)->value_nick,
                                             color_names[color]);
          test_data->format = format;
          test_data->color = color;
          g_test_add_data_func_full (test_name, test_data, test_download_1x1, g_free);
          g_free (test_name);

          test_data = g_new (TestData, 1);
          test_name = g_strdup_printf ("/memorytexture/download_1x1_with_stride/%s/%s",
                                       g_enum_get_value (enum_class, format)->value_nick,
                                       color_names[color]);
          test_data->format = format;
          test_data->color = color;
          g_test_add_data_func_full (test_name, test_data, test_download_1x1_with_stride, g_free);
          g_free (test_name);

          test_data = g_new (TestData, 1);
          test_name = g_strdup_printf ("/memorytexture/download_4x4/%s/%s",
                                       g_enum_get_value (enum_class, format)->value_nick,
                                       color_names[color]);
          test_data->format = format;
          test_data->color = color;
          g_test_add_data_func_full (test_name, test_data, test_download_4x4, g_free);
          g_free (test_name);

          test_data = g_new (TestData, 1);
          test_name = g_strdup_printf ("/memorytexture/download_4x4_with_stride/%s/%s",
                                       g_enum_get_value (enum_class, format)->value_nick,
                                       color_names[color]);
          test_data->format = format;
          test_data->color = color;
          g_test_add_data_func_full (test_name, test_data, test_download_4x4_with_stride, g_free);
          g_free (test_name);
        }
    }

  return g_test_run ();
}