texture: Export gdk_memory_texture_new() and GdkMemoryFormat

Also add tests for all these newfangled formats.
This commit is contained in:
Benjamin Otte 2018-03-12 05:12:54 +01:00
parent 5fe14e06da
commit e5813b3ae7
8 changed files with 328 additions and 37 deletions

View File

@ -720,13 +720,14 @@ gdk_event_get_type
GdkTexture
gdk_texture_new_for_data
gdk_texture_new_for_pixbuf
gdk_texture_new_for_gl
gdk_texture_new_from_resource
gdk_texture_new_from_file
gdk_texture_get_width
gdk_texture_get_height
gdk_texture_download
gdk_texture_release_gl
gdk_memory_texture_new
gdk_gl_texture_new
gdk_gl_texture_release
<SUBSECTION Standard>
GdkTextureClass

View File

@ -54,6 +54,7 @@
#include <gdk/gdkgltexture.h>
#include <gdk/gdkkeys.h>
#include <gdk/gdkkeysyms.h>
#include <gdk/gdkmemorytexture.h>
#include <gdk/gdkmonitor.h>
#include <gdk/gdkpango.h>
#include <gdk/gdkpixbuf.h>

View File

@ -241,8 +241,8 @@ static ConversionFunc converters[GDK_MEMORY_N_FORMATS][2] =
{ convert_swizzle_premultiply_3210_0123, convert_swizzle_premultiply_0123_0123 },
{ convert_swizzle_premultiply_3210_3012, convert_swizzle_premultiply_0123_3012 },
{ convert_swizzle_premultiply_3210_0321, convert_swizzle_premultiply_0123_0321 },
{ convert_swizzle_opaque_3210, convert_swizzle_opaque_3012 },
{ convert_swizzle_opaque_0123, convert_swizzle_opaque_0321 }
{ convert_swizzle_opaque_3210, convert_swizzle_opaque_0123 },
{ convert_swizzle_opaque_3012, convert_swizzle_opaque_0321 }
};
void

81
gdk/gdkmemorytexture.h Normal file
View File

@ -0,0 +1,81 @@
/*
* Copyright © 2018 Benjamin Otte
*
* 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.1 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/>.
*
* Authors: Benjamin Otte <otte@gnome.org>
*/
#ifndef __GDK_MEMORY_TEXTURE__H__
#define __GDK_MEMORY_TEXTURE__H__
#if !defined (__GDK_H_INSIDE__) && !defined (GDK_COMPILATION)
#error "Only <gdk/gdk.h> can be included directly."
#endif
#include <gdk/gdktexture.h>
G_BEGIN_DECLS
/*
* GdkMemoryFormat:
* @GDK_MEMORY_B8G8R8A8_PREMULTIPLIED: 4 bytes; for blue, green, red, alpha.
* The color values are premultiplied with the alpha value.
* @GDK_MEMORY_A8R8G8B8_PREMULTIPLIED: 4 bytes; for alpha, red, green, blue.
* The color values are premultiplied with the alpha value.
* @GDK_MEMORY_B8G8R8A8: 4 bytes; for blue, green, red, alpha.
* @GDK_MEMORY_A8R8G8B8: 4 bytes; for alpha, red, green, blue.
* @GDK_MEMORY_R8G8B8A8: 4 bytes; for red, green, blue, alpha.
* @GDK_MEMORY_A8B8G8R8: 4 bytes; for alpha, blue, green, red.
* @GDK_MEMORY_R8G8B8: 3 bytes; for red, green, blue. The data is opaque.
* @GDK_MEMORY_B8G8R8: 3 bytes; for blue, green, red. The data is opaque.
* @GDK_MEMORY_N_FORMATS: The number of formats. This value will change as
* more formats get added, so do not rely on its concrete integer.
*
* #GdkMemoryFormat describes a format that bytes can have in memory.
*
* It describes formats by listing the contents of the memory passed to it.
* So GDK_MEMORY_A8R8G8B8 will be 1 byte (8 bits) of alpha, followed by a
* byte each of red, green and blue. It is not endian-dependant, so
* CAIRO_FORMAT_ARGB32 is represented by different #GdkMemoryFormats on
* architectures with different endiannesses.
*
* Its naming is modelled after VkFormat (see
* https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#VkFormat
* for details).
*/
typedef enum {
GDK_MEMORY_B8G8R8A8_PREMULTIPLIED,
GDK_MEMORY_A8R8G8B8_PREMULTIPLIED,
GDK_MEMORY_B8G8R8A8,
GDK_MEMORY_A8R8G8B8,
GDK_MEMORY_R8G8B8A8,
GDK_MEMORY_A8B8G8R8,
GDK_MEMORY_R8G8B8,
GDK_MEMORY_B8G8R8,
GDK_MEMORY_N_FORMATS
} GdkMemoryFormat;
GDK_AVAILABLE_IN_ALL
GdkTexture * gdk_memory_texture_new (int width,
int height,
GdkMemoryFormat format,
GBytes *bytes,
gsize stride);
G_END_DECLS
#endif /* __GDK_MEMORY_TEXTURE_H__ */

View File

@ -20,37 +20,12 @@
#ifndef __GDK_MEMORY_TEXTURE_PRIVATE_H__
#define __GDK_MEMORY_TEXTURE_PRIVATE_H__
#include "gdkmemorytexture.h"
#include "gdktextureprivate.h"
G_BEGIN_DECLS
/*
* GdkMemoryFormat:
*
* #GdkMemroyFormat describes a format that bytes can have in memory.
*
* It describes formats by listing the contents of the memory passed to it.
* So GDK_MEMORY_A8R8G8B8 will be 8 bits of alpha, followed by 8 bites of each
* blue, green and red. It is not endian-dependant, so CAIRO_FORMAT_ARGB32 is
* represented by 2 different GdkMemoryFormats.
*
* Its naming is modelled after VkFormat (see
* https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#VkFormat
* for details).
*/
typedef enum {
GDK_MEMORY_B8G8R8A8_PREMULTIPLIED,
GDK_MEMORY_A8R8G8B8_PREMULTIPLIED,
GDK_MEMORY_B8G8R8A8,
GDK_MEMORY_A8R8G8B8,
GDK_MEMORY_R8G8B8A8,
GDK_MEMORY_A8B8G8R8,
GDK_MEMORY_R8G8B8,
GDK_MEMORY_B8G8R8,
GDK_MEMORY_N_FORMATS
} GdkMemoryFormat;
#define GDK_MEMORY_GDK_PIXBUF_OPAQUE GDK_MEMORY_R8G8B8
#define GDK_MEMORY_GDK_PIXBUF_ALPHA GDK_MEMORY_R8G8B8A8
@ -66,12 +41,6 @@ typedef enum {
G_DECLARE_FINAL_TYPE (GdkMemoryTexture, gdk_memory_texture, GDK, MEMORY_TEXTURE, GdkTexture)
GdkTexture * gdk_memory_texture_new (int width,
int height,
GdkMemoryFormat format,
GBytes *bytes,
gsize stride);
GdkMemoryFormat gdk_memory_texture_get_format (GdkMemoryTexture *self);
const guchar * gdk_memory_texture_get_data (GdkMemoryTexture *self);
gsize gdk_memory_texture_get_stride (GdkMemoryTexture *self);

View File

@ -71,6 +71,7 @@ gdk_public_headers = files([
'gdkgltexture.h',
'gdkkeys.h',
'gdkkeysyms.h',
'gdkmemorytexture.h',
'gdkmonitor.h',
'gdkpango.h',
'gdkpixbuf.h',

View File

@ -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 ();
}

View File

@ -6,6 +6,7 @@ tests = [
'display',
'encoding',
'keysyms',
'memorytexture',
'rectangle',
'rgba',
'seat',