diff options
author | Benjamin Otte <otte@redhat.com> | 2018-03-12 05:12:54 +0100 |
---|---|---|
committer | Benjamin Otte <otte@redhat.com> | 2018-03-12 17:21:45 +0100 |
commit | e5813b3ae76c7a75ed42a932cf3b93a15596ff44 (patch) | |
tree | 79cf7d9c838017c7f6545d6d47a6f8c72e64c249 /testsuite | |
parent | 5fe14e06da49af376ea84d4e81eba1a531700f73 (diff) | |
download | gtk+-e5813b3ae76c7a75ed42a932cf3b93a15596ff44.tar.gz |
texture: Export gdk_memory_texture_new() and GdkMemoryFormat
Also add tests for all these newfangled formats.
Diffstat (limited to 'testsuite')
-rw-r--r-- | testsuite/gdk/memorytexture.c | 237 | ||||
-rw-r--r-- | testsuite/gdk/meson.build | 1 |
2 files changed, 238 insertions, 0 deletions
diff --git a/testsuite/gdk/memorytexture.c b/testsuite/gdk/memorytexture.c new file mode 100644 index 0000000000..e4cf451174 --- /dev/null +++ b/testsuite/gdk/memorytexture.c @@ -0,0 +1,237 @@ +#include <locale.h> +#include <gdk/gdk.h> + +/* maximum bytes per pixel */ +#define MAX_BPP 4 + +#if G_BYTE_ORDER == G_LITTLE_ENDIAN +#define GDK_MEMORY_CAIRO_FORMAT_ARGB32 GDK_MEMORY_B8G8R8A8_PREMULTIPLIED +#elif G_BYTE_ORDER == G_BIG_ENDIAN +#define GDK_MEMORY_CAIRO_FORMAT_ARGB32 GDK_MEMORY_A8R8G8B8_PREMULTIPLIED +#endif + +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(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; + gint width, height; + gint 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 * stride); + 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_static (data, height * stride); + 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_CAIRO_FORMAT_ARGB32, 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_CAIRO_FORMAT_ARGB32, 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_CAIRO_FORMAT_ARGB32, 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_CAIRO_FORMAT_ARGB32, 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); + + g_test_bug_base ("http://bugzilla.gnome.org"); + + 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 (); +} diff --git a/testsuite/gdk/meson.build b/testsuite/gdk/meson.build index 227107b6bd..d7049af572 100644 --- a/testsuite/gdk/meson.build +++ b/testsuite/gdk/meson.build @@ -6,6 +6,7 @@ tests = [ 'display', 'encoding', 'keysyms', + 'memorytexture', 'rectangle', 'rgba', 'seat', |