| 1 |
#include "config.h" |
| 2 |
|
| 3 |
#include <gtk/gtk.h> |
| 4 |
|
| 5 |
#include "gdk/gdkmemoryformatprivate.h" |
| 6 |
#include "gsk/gl/fp16private.h" |
| 7 |
#include "testsuite/gdk/gdktestutils.h" |
| 8 |
|
| 9 |
struct { |
| 10 |
const char *name; |
| 11 |
GskRenderer * (*create_func) (void); |
| 12 |
GskRenderer *renderer; |
| 13 |
} renderers[] = { |
| 14 |
{ |
| 15 |
"cairo", |
| 16 |
gsk_cairo_renderer_new, |
| 17 |
}, |
| 18 |
{ |
| 19 |
"vulkan", |
| 20 |
gsk_vulkan_renderer_new, |
| 21 |
}, |
| 22 |
{ |
| 23 |
"gl", |
| 24 |
gsk_gl_renderer_new, |
| 25 |
}, |
| 26 |
}; |
| 27 |
|
| 28 |
static gpointer |
| 29 |
encode_renderer_format (guint renderer, |
| 30 |
GdkMemoryFormat format) |
| 31 |
{ |
| 32 |
return GSIZE_TO_POINTER (format * G_N_ELEMENTS (renderers) + renderer); |
| 33 |
} |
| 34 |
|
| 35 |
static void |
| 36 |
decode_renderer_format (gconstpointer data, |
| 37 |
GskRenderer **renderer, |
| 38 |
GdkMemoryFormat *format) |
| 39 |
{ |
| 40 |
gsize value = GPOINTER_TO_SIZE (data); |
| 41 |
|
| 42 |
*renderer = renderers[value % G_N_ELEMENTS (renderers)].renderer; |
| 43 |
value /= G_N_ELEMENTS (renderers); |
| 44 |
|
| 45 |
*format = value; |
| 46 |
} |
| 47 |
|
| 48 |
#if 0 |
| 49 |
static GdkTexture * |
| 50 |
ensure_texture_format (GdkTexture *texture, |
| 51 |
GdkMemoryFormat format) |
| 52 |
{ |
| 53 |
GdkTextureDownloader *downloader; |
| 54 |
GdkTexture *result; |
| 55 |
GBytes *bytes; |
| 56 |
gsize stride; |
| 57 |
|
| 58 |
if (gdk_texture_get_format (texture) == format) |
| 59 |
return texture; |
| 60 |
|
| 61 |
downloader = gdk_texture_downloader_new (texture); |
| 62 |
gdk_texture_downloader_set_format (downloader, format); |
| 63 |
bytes = gdk_texture_downloader_download_bytes (downloader, &stride); |
| 64 |
gdk_texture_downloader_free (downloader); |
| 65 |
|
| 66 |
result = gdk_memory_texture_new (gdk_texture_get_width (texture), |
| 67 |
gdk_texture_get_height (texture), |
| 68 |
format, |
| 69 |
bytes, |
| 70 |
stride); |
| 71 |
g_bytes_unref (bytes); |
| 72 |
g_object_unref (texture); |
| 73 |
|
| 74 |
return result; |
| 75 |
} |
| 76 |
#endif |
| 77 |
|
| 78 |
static void |
| 79 |
color_make_opaque (GdkRGBA *result, |
| 80 |
const GdkRGBA *color) |
| 81 |
{ |
| 82 |
result->red *= color->alpha; |
| 83 |
result->green *= color->alpha; |
| 84 |
result->blue *= color->alpha; |
| 85 |
result->alpha = 1.0f; |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
color_make_gray (GdkRGBA *result, |
| 90 |
const GdkRGBA *color) |
| 91 |
{ |
| 92 |
result->red = (color->red + color->green + color->blue) / 3.0f; |
| 93 |
result->green = result->red; |
| 94 |
result->blue = result->red; |
| 95 |
result->alpha = color->alpha; |
| 96 |
} |
| 97 |
|
| 98 |
static void |
| 99 |
color_make_white (GdkRGBA *result, |
| 100 |
const GdkRGBA *color) |
| 101 |
{ |
| 102 |
result->red = 1.0f; |
| 103 |
result->green = 1.0f; |
| 104 |
result->blue = 1.0f; |
| 105 |
result->alpha = color->alpha; |
| 106 |
} |
| 107 |
|
| 108 |
/* Generate colors so that premultiplying will result in values in steps of 1/15th. |
| 109 |
* Also make sure that an averaged gray value fits in that range. */ |
| 110 |
static void |
| 111 |
create_random_color (GdkRGBA *color) |
| 112 |
{ |
| 113 |
int r, g, b; |
| 114 |
do |
| 115 |
{ |
| 116 |
r = g_test_rand_int_range (0, 6); |
| 117 |
g = g_test_rand_int_range (0, 6); |
| 118 |
b = g_test_rand_int_range (0, 6); |
| 119 |
} |
| 120 |
while ((r + g + b) % 3 != 0); |
| 121 |
color->red = r / 5.f; |
| 122 |
color->green = g / 5.f; |
| 123 |
color->blue = b / 5.f; |
| 124 |
color->alpha = g_test_rand_int_range (0, 4) / 3.f; |
| 125 |
} |
| 126 |
|
| 127 |
static void |
| 128 |
create_random_color_for_format (GdkRGBA *color, |
| 129 |
GdkMemoryFormat format) |
| 130 |
{ |
| 131 |
/* non-premultiplied can represet GdkRGBA (1, 1, 1, 0) |
| 132 |
* but premultiplied cannot. |
| 133 |
* Premultiplied will always represent this as (0, 0, 0, 0) |
| 134 |
*/ |
| 135 |
do |
| 136 |
{ |
| 137 |
create_random_color (color); |
| 138 |
} |
| 139 |
while (color->alpha == 0 && |
| 140 |
gdk_memory_format_alpha (format) == GDK_MEMORY_ALPHA_PREMULTIPLIED); |
| 141 |
|
| 142 |
/* If the format can't handle alpha, make things opaque |
| 143 |
*/ |
| 144 |
if (gdk_memory_format_alpha (format) == GDK_MEMORY_ALPHA_OPAQUE) |
| 145 |
color_make_opaque (color, color); |
| 146 |
|
| 147 |
/* If the format has fewer color channels than the |
| 148 |
* target, make sure the colors get adjusted. |
| 149 |
*/ |
| 150 |
if (gdk_memory_format_n_colors (format) == 1) |
| 151 |
color_make_gray (color, color); |
| 152 |
else if (gdk_memory_format_n_colors (format) == 0) |
| 153 |
color_make_white (color, color); |
| 154 |
} |
| 155 |
|
| 156 |
static GdkTexture * |
| 157 |
create_solid_color_texture (GdkMemoryFormat format, |
| 158 |
int width, |
| 159 |
int height, |
| 160 |
const GdkRGBA *color) |
| 161 |
{ |
| 162 |
TextureBuilder builder; |
| 163 |
|
| 164 |
texture_builder_init (&builder, format, width, height); |
| 165 |
texture_builder_fill (&builder, color); |
| 166 |
|
| 167 |
return texture_builder_finish (&builder); |
| 168 |
} |
| 169 |
|
| 170 |
/* randomly creates 4 colors with values that are multiples |
| 171 |
* of 8, so that averaging the colors works without rounding |
| 172 |
* errors, and then creates a stipple pattern like this: |
| 173 |
* |
| 174 |
* 1 2 1 2 1 2 ... |
| 175 |
* 3 4 3 4 3 4 |
| 176 |
* 1 2 1 2 1 2 |
| 177 |
* 3 4 3 4 3 4 |
| 178 |
* 1 2 1 2 1 2 |
| 179 |
* 3 4 3 4 3 4 |
| 180 |
* ⋮ |
| 181 |
*/ |
| 182 |
static GdkTexture * |
| 183 |
create_stipple_texture (GdkMemoryFormat format, |
| 184 |
gsize width, |
| 185 |
gsize height, |
| 186 |
float scale, |
| 187 |
GdkRGBA colors[2][2], |
| 188 |
GdkRGBA *average) |
| 189 |
{ |
| 190 |
TextureBuilder builder; |
| 191 |
GdkTexture *texture; |
| 192 |
int x, y; |
| 193 |
/* large enough */ |
| 194 |
float data[4 * 4 * 4]; |
| 195 |
GdkMemoryLayout layout; |
| 196 |
|
| 197 |
*average = (GdkRGBA) { 0, 0, 0, 0 }; |
| 198 |
|
| 199 |
for (y = 0; y < 2; y++) |
| 200 |
{ |
| 201 |
for (x = 0; x < 2; x++) |
| 202 |
{ |
| 203 |
create_random_color_for_format (&colors[x][y], format); |
| 204 |
if (gdk_memory_format_alpha (format) != GDK_MEMORY_ALPHA_OPAQUE) |
| 205 |
colors[x][y].alpha *= scale * 16.f/17.f; |
| 206 |
else |
| 207 |
{ |
| 208 |
colors[x][y].red *= scale * 16.f/17.f; |
| 209 |
colors[x][y].green *= scale * 16.f/17.f; |
| 210 |
colors[x][y].blue *= scale *16.f/17.f; |
| 211 |
} |
| 212 |
|
| 213 |
average->red += colors[x][y].red * colors[x][y].alpha; |
| 214 |
average->green += colors[x][y].green * colors[x][y].alpha; |
| 215 |
average->blue += colors[x][y].blue * colors[x][y].alpha; |
| 216 |
average->alpha += colors[x][y].alpha; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
if (average->alpha != 0.0f) |
| 221 |
{ |
| 222 |
average->red /= average->alpha; |
| 223 |
average->green /= average->alpha; |
| 224 |
average->blue /= average->alpha; |
| 225 |
average->alpha /= 4.0f; |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
/* Each component of the average has been multiplied by the alpha |
| 230 |
* already, so if the alpha is zero, all components should also |
| 231 |
* be zero */ |
| 232 |
g_assert_cmpfloat (average->red, ==, 0.0f); |
| 233 |
g_assert_cmpfloat (average->green, ==, 0.0f); |
| 234 |
g_assert_cmpfloat (average->blue, ==, 0.0f); |
| 235 |
} |
| 236 |
|
| 237 |
for (y = 0; y < 4; y++) |
| 238 |
{ |
| 239 |
for (x = 0; x < 4; x++) |
| 240 |
{ |
| 241 |
data[4 * (4 * y + x) + 0] = colors[x % 2][y % 2].red; |
| 242 |
data[4 * (4 * y + x) + 1] = colors[x % 2][y % 2].green; |
| 243 |
data[4 * (4 * y + x) + 2] = colors[x % 2][y % 2].blue; |
| 244 |
data[4 * (4 * y + x) + 3] = colors[x % 2][y % 2].alpha; |
| 245 |
} |
| 246 |
} |
| 247 |
gdk_memory_layout_init (&layout, |
| 248 |
GDK_MEMORY_R32G32B32A32_FLOAT, |
| 249 |
4, |
| 250 |
4, |
| 251 |
1); |
| 252 |
layout.width = MIN (width, layout.width); |
| 253 |
layout.height = MIN (height, layout.height); |
| 254 |
|
| 255 |
texture_builder_init (&builder, format, width, height); |
| 256 |
for (y = 0; y < height; y += layout.height) |
| 257 |
{ |
| 258 |
for (x = 0; x < width; x += layout.width) |
| 259 |
{ |
| 260 |
texture_builder_draw_data (&builder, |
| 261 |
x, y, |
| 262 |
(guchar *) data, |
| 263 |
&layout); |
| 264 |
} |
| 265 |
} |
| 266 |
texture = texture_builder_finish (&builder); |
| 267 |
|
| 268 |
return texture; |
| 269 |
} |
| 270 |
|
| 271 |
static void |
| 272 |
dump_scaling_input (const GdkRGBA colors[2][2], |
| 273 |
const GdkRGBA *average) |
| 274 |
{ |
| 275 |
int x, y; |
| 276 |
|
| 277 |
for (y = 0; y < 2; y++) |
| 278 |
{ |
| 279 |
for (x = 0; x < 2; x++) |
| 280 |
g_test_message ("input stipple texture (%d,%d) r=%f g=%f b=%f a=%f", |
| 281 |
x, y, |
| 282 |
colors[x][y].red, |
| 283 |
colors[x][y].green, |
| 284 |
colors[x][y].blue, |
| 285 |
colors[x][y].alpha); |
| 286 |
} |
| 287 |
|
| 288 |
g_test_message ("expected average r=%f g=%f b=%f a=%f", |
| 289 |
average->red, average->green, average->blue, average->alpha); |
| 290 |
} |
| 291 |
|
| 292 |
static void |
| 293 |
test_linear_filtering (gconstpointer data, |
| 294 |
gsize width, |
| 295 |
gsize height) |
| 296 |
{ |
| 297 |
GdkMemoryFormat format; |
| 298 |
GskRenderer *renderer; |
| 299 |
GdkTexture *input, *output, *expected; |
| 300 |
GskRenderNode *node; |
| 301 |
GdkRGBA colors[2][2]; |
| 302 |
GdkRGBA average_color; |
| 303 |
gboolean cant_math; |
| 304 |
|
| 305 |
decode_renderer_format (data, &renderer, &format); |
| 306 |
|
| 307 |
width = MAX (width, gdk_memory_format_get_block_width (format)); |
| 308 |
height = MAX (height, gdk_memory_format_get_block_height (format)); |
| 309 |
|
| 310 |
cant_math = GSK_IS_CAIRO_RENDERER (renderer); |
| 311 |
input = create_stipple_texture (format, width, height, cant_math ? 0.5 : 1, colors, &average_color); |
| 312 |
node = gsk_texture_scale_node_new (input, &GRAPHENE_RECT_INIT (0, 0, width / 2, height / 2), GSK_SCALING_FILTER_LINEAR); |
| 313 |
output = gsk_renderer_render_texture (renderer, node, NULL); |
| 314 |
expected = create_solid_color_texture (gdk_texture_get_format (output), width / 2, height / 2, &average_color); |
| 315 |
|
| 316 |
compare_textures (expected, output, FALSE); |
| 317 |
|
| 318 |
if (g_test_failed ()) |
| 319 |
dump_scaling_input (colors, &average_color); |
| 320 |
|
| 321 |
g_object_unref (expected); |
| 322 |
g_object_unref (output); |
| 323 |
gsk_render_node_unref (node); |
| 324 |
g_object_unref (input); |
| 325 |
} |
| 326 |
|
| 327 |
static void |
| 328 |
test_mipmaps (gconstpointer data) |
| 329 |
{ |
| 330 |
GdkMemoryFormat format; |
| 331 |
GskRenderer *renderer; |
| 332 |
GdkTexture *input, *output, *expected; |
| 333 |
GskRenderNode *node; |
| 334 |
GdkRGBA colors[2][2]; |
| 335 |
GdkRGBA average_color; |
| 336 |
gsize width, height; |
| 337 |
|
| 338 |
decode_renderer_format (data, &renderer, &format); |
| 339 |
|
| 340 |
width = MAX (2, gdk_memory_format_get_block_width (format)); |
| 341 |
height = MAX (2, gdk_memory_format_get_block_height (format)); |
| 342 |
|
| 343 |
input = create_stipple_texture (format, width, height, 1.0, colors, &average_color); |
| 344 |
node = gsk_texture_scale_node_new (input, &GRAPHENE_RECT_INIT (0, 0, 1, 1), GSK_SCALING_FILTER_TRILINEAR); |
| 345 |
output = gsk_renderer_render_texture (renderer, node, NULL); |
| 346 |
expected = create_solid_color_texture (gdk_texture_get_format (output), 1, 1, &average_color); |
| 347 |
|
| 348 |
compare_textures (expected, output, FALSE); |
| 349 |
|
| 350 |
if (g_test_failed ()) |
| 351 |
dump_scaling_input (colors, &average_color); |
| 352 |
|
| 353 |
g_object_unref (expected); |
| 354 |
g_object_unref (output); |
| 355 |
gsk_render_node_unref (node); |
| 356 |
g_object_unref (input); |
| 357 |
} |
| 358 |
|
| 359 |
static void |
| 360 |
test_linear_filtering_2x2 (gconstpointer data) |
| 361 |
{ |
| 362 |
test_linear_filtering (data, 2, 2); |
| 363 |
} |
| 364 |
|
| 365 |
static void |
| 366 |
test_linear_filtering_512x512 (gconstpointer data) |
| 367 |
{ |
| 368 |
test_linear_filtering (data, 512, 512); |
| 369 |
} |
| 370 |
|
| 371 |
static void |
| 372 |
add_format_test (const char *name, |
| 373 |
GTestDataFunc func) |
| 374 |
{ |
| 375 |
GdkMemoryFormat format; |
| 376 |
gsize renderer; |
| 377 |
GEnumClass *enum_class; |
| 378 |
|
| 379 |
enum_class = g_type_class_ref (GDK_TYPE_MEMORY_FORMAT); |
| 380 |
|
| 381 |
for (renderer = 0; renderer < G_N_ELEMENTS (renderers); renderer++) |
| 382 |
{ |
| 383 |
if (renderers[renderer].renderer == NULL) |
| 384 |
continue; |
| 385 |
|
| 386 |
for (format = 0; format < GDK_MEMORY_N_FORMATS; format++) |
| 387 |
{ |
| 388 |
char *test_name = g_strdup_printf ("%s/%s/%s", |
| 389 |
name, |
| 390 |
renderers[renderer].name, |
| 391 |
g_enum_get_value (enum_class, format)->value_nick); |
| 392 |
g_test_add_data_func_full (test_name, encode_renderer_format (renderer, format), func, NULL); |
| 393 |
g_free (test_name); |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
static void |
| 399 |
create_renderers (void) |
| 400 |
{ |
| 401 |
GError *error = NULL; |
| 402 |
gsize i; |
| 403 |
|
| 404 |
for (i = 0; i < G_N_ELEMENTS (renderers); i++) |
| 405 |
{ |
| 406 |
renderers[i].renderer = renderers[i].create_func (); |
| 407 |
if (!gsk_renderer_realize_for_display (renderers[i].renderer, gdk_display_get_default (), &error)) |
| 408 |
{ |
| 409 |
g_test_message ("Could not realize %s renderer: %s", renderers[i].name, error->message); |
| 410 |
g_clear_error (&error); |
| 411 |
g_clear_object (&renderers[i].renderer); |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
static void |
| 417 |
destroy_renderers (void) |
| 418 |
{ |
| 419 |
gsize i; |
| 420 |
|
| 421 |
for (i = 0; i < G_N_ELEMENTS (renderers); i++) |
| 422 |
{ |
| 423 |
if (renderers[i].renderer == NULL) |
| 424 |
continue; |
| 425 |
|
| 426 |
gsk_renderer_unrealize (renderers[i].renderer); |
| 427 |
g_clear_object (&renderers[i].renderer); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
int |
| 432 |
main (int argc, char *argv[]) |
| 433 |
{ |
| 434 |
int result; |
| 435 |
|
| 436 |
gtk_test_init (&argc, &argv, NULL); |
| 437 |
create_renderers (); |
| 438 |
|
| 439 |
add_format_test ("/scaling/linear-filtering", test_linear_filtering_2x2); |
| 440 |
add_format_test ("/scaling/linear-filtering-large", test_linear_filtering_512x512); |
| 441 |
add_format_test ("/scaling/mipmap", test_mipmaps); |
| 442 |
|
| 443 |
result = g_test_run (); |
| 444 |
|
| 445 |
/* So the context gets actually destroyed */ |
| 446 |
gdk_gl_context_clear_current (); |
| 447 |
|
| 448 |
destroy_renderers (); |
| 449 |
|
| 450 |
return result; |
| 451 |
} |