mutter/tests/conform/test-utils.c

236 lines
6.4 KiB
C
Raw Normal View History

#define COGL_ENABLE_EXPERIMENTAL_2_0_API
#include <cogl/cogl.h>
#include <stdlib.h>
#include "test-utils.h"
#define FB_WIDTH 512
#define FB_HEIGHT 512
static CoglBool cogl_test_is_verbose;
CoglContext *ctx;
CoglFramebuffer *fb;
void
test_utils_init (TestFlags flags)
{
static int counter = 0;
Adds CoglError api Although we use GLib internally in Cogl we would rather not leak GLib api through Cogl's own api, except through explicitly namespaced cogl_glib_ / cogl_gtype_ feature apis. One of the benefits we see to not leaking GLib through Cogl's public API is that documentation for Cogl won't need to first introduce the Glib API to newcomers, thus hopefully lowering the barrier to learning Cogl. This patch provides a Cogl specific typedef for reporting runtime errors which by no coincidence matches the typedef for GError exactly. If Cogl is built with --enable-glib (default) then developers can even safely assume that a CoglError is a GError under the hood. This patch also enforces a consistent policy for when NULL is passed as an error argument and an error is thrown. In this case we log the error and abort the application, instead of silently ignoring it. In common cases where nothing has been implemented to handle a particular error and/or where applications are just printing the error and aborting themselves then this saves some typing. This also seems more consistent with language based exceptions which usually cause a program to abort if they are not explicitly caught (which passing a non-NULL error signifies in this case) Since this policy for NULL error pointers is stricter than the standard GError convention, there is a clear note in the documentation to warn developers that are used to using the GError api. Reviewed-by: Neil Roberts <neil@linux.intel.com> (cherry picked from commit b068d5ea09ab32c37e8c965fc8582c85d1b2db46) Note: Since we can't change the Cogl 1.x api the patch was changed to not rename _error_quark() functions to be _error_domain() functions and although it's a bit ugly, instead of providing our own CoglError type that's compatible with GError we simply #define CoglError to GError unless Cogl is built with glib disabled. Note: this patch does technically introduce an API break since it drops the cogl_error_get_type() symbol generated by glib-mkenum (Since the CoglError enum was replaced by a CoglSystemError enum) but for now we are assuming that this will not affect anyone currently using the Cogl API. If this does turn out to be a problem in practice then we would be able to fix this my manually copying an implementation of cogl_error_get_type() generated by glib-mkenum into a compatibility source file and we could also define the original COGL_ERROR_ enums for compatibility too. Note: another minor concern with cherry-picking this patch to the 1.14 branch is that an api scanner would be lead to believe that some APIs have changed, and for example the gobject-introspection parser which understands the semantics of GError will not understand the semantics of CoglError. We expect most people that have tried to use gobject-introspection with Cogl already understand though that it is not well suited to generating bindings of the Cogl api anyway and we aren't aware or anyone depending on such bindings for apis involving GErrors. (GnomeShell only makes very-very minimal use of Cogl via the gjs bindings for the cogl_rectangle and cogl_color apis.) The main reason we have cherry-picked this patch to the 1.14 branch even given the above concerns is that without it it would become very awkward for us to cherry-pick other beneficial patches from master.
2012-08-31 14:28:27 -04:00
CoglError *error = NULL;
CoglOnscreen *onscreen = NULL;
CoglDisplay *display;
CoglRenderer *renderer;
CoglBool missing_requirement = FALSE;
if (counter != 0)
g_critical ("We don't support running more than one test at a time\n"
"in a single test run due to the state leakage that can\n"
"cause subsequent tests to fail.\n"
"\n"
"If you want to run all the tests you should run\n"
"$ make test-report");
counter++;
if (g_getenv ("COGL_TEST_VERBOSE") || g_getenv ("V"))
cogl_test_is_verbose = TRUE;
if (g_getenv ("G_DEBUG"))
{
char *debug = g_strconcat (g_getenv ("G_DEBUG"), ",fatal-warnings", NULL);
g_setenv ("G_DEBUG", debug, TRUE);
g_free (debug);
}
else
g_setenv ("G_DEBUG", "fatal-warnings", TRUE);
g_setenv ("COGL_X11_SYNC", "1", 0);
ctx = cogl_context_new (NULL, &error);
if (!ctx)
g_critical ("Failed to create a CoglContext: %s", error->message);
display = cogl_context_get_display (ctx);
renderer = cogl_display_get_renderer (display);
if (flags & TEST_REQUIREMENT_GL &&
Add a GL 3 driver This adds a new CoglDriver for GL 3 called COGL_DRIVER_GL3. When requested, the GLX, EGL and SDL2 winsyss will set the necessary attributes to request a forward-compatible core profile 3.1 context. That means it will have no deprecated features. To simplify the explosion of checks for specific combinations of context->driver, many of these conditionals have now been replaced with private feature flags that are checked instead. The GL and GLES drivers now initialise these private feature flags depending on which driver is used. The fixed function backends now explicitly check whether the fixed function private feature is available which means the GL3 driver will fall back to always using the GLSL progend. Since Rob's latest patches the GLSL progend no longer uses any fixed function API anyway so it should just work. The driver is currently lower priority than COGL_DRIVER_GL so it will not be used unless it is specificly requested. We may want to change this priority at some point because apparently Mesa can make some memory savings if a core profile context is used. In GL 3, getting the combined extensions string with glGetString is deprecated so this patch changes it to use glGetStringi to build up an array of extensions instead. _cogl_context_get_gl_extensions now returns this array instead of trying to return a const string. The caller is expected to free the array. Some issues with this patch: • GL 3 does not support GL_ALPHA format textures. We should probably make this a feature flag or something. Cogl uses this to render text which currently just throws a GL error and breaks so it's pretty important to do something about this before considering the GL3 driver to be stable. • GL 3 doesn't support client side vertex buffers. This probably doesn't matter because CoglBuffer won't normally use malloc'd buffers if VBOs are available, but it might but worth making malloc'd buffers a private feature and forcing it not to use them. • GL 3 doesn't support the default vertex array object. This patch just makes it create and bind a single non-default vertex array object which gets used just like the normal default object. Ideally it would be good to use vertex array objects properly and attach them to a CoglPrimitive to cache the state. Reviewed-by: Robert Bragg <robert@linux.intel.com> (cherry picked from commit 66c9db993595b3a22e63f4c201ea468bc9b88cb6)
2012-09-26 15:32:36 -04:00
cogl_renderer_get_driver (renderer) != COGL_DRIVER_GL &&
cogl_renderer_get_driver (renderer) != COGL_DRIVER_GL3)
{
missing_requirement = TRUE;
}
if (flags & TEST_REQUIREMENT_NPOT &&
!cogl_has_feature (ctx, COGL_FEATURE_ID_TEXTURE_NPOT))
{
missing_requirement = TRUE;
}
if (flags & TEST_REQUIREMENT_TEXTURE_3D &&
!cogl_has_feature (ctx, COGL_FEATURE_ID_TEXTURE_3D))
{
missing_requirement = TRUE;
}
if (flags & TEST_REQUIREMENT_POINT_SPRITE &&
!cogl_has_feature (ctx, COGL_FEATURE_ID_POINT_SPRITE))
{
missing_requirement = TRUE;
}
if (flags & TEST_REQUIREMENT_GLES2_CONTEXT &&
!cogl_has_feature (ctx, COGL_FEATURE_ID_GLES2_CONTEXT))
{
missing_requirement = TRUE;
}
if (flags & TEST_KNOWN_FAILURE)
{
missing_requirement = TRUE;
}
if (getenv ("COGL_TEST_ONSCREEN"))
{
onscreen = cogl_onscreen_new (ctx, 640, 480);
fb = COGL_FRAMEBUFFER (onscreen);
}
else
{
CoglOffscreen *offscreen;
CoglTexture2D *tex = cogl_texture_2d_new_with_size (ctx,
FB_WIDTH, FB_HEIGHT,
COGL_PIXEL_FORMAT_ANY,
&error);
if (!tex)
g_critical ("Failed to allocate texture: %s", error->message);
offscreen = cogl_offscreen_new_to_texture (COGL_TEXTURE (tex));
fb = COGL_FRAMEBUFFER (offscreen);
}
if (!cogl_framebuffer_allocate (fb, &error))
g_critical ("Failed to allocate framebuffer: %s", error->message);
if (onscreen)
cogl_onscreen_show (onscreen);
cogl_framebuffer_clear4f (fb,
COGL_BUFFER_BIT_COLOR |
COGL_BUFFER_BIT_DEPTH |
COGL_BUFFER_BIT_STENCIL,
0, 0, 0, 1);
if (missing_requirement)
g_print ("WARNING: Missing required feature[s] for this test\n");
}
void
test_utils_fini (void)
{
if (fb)
cogl_object_unref (fb);
if (ctx)
cogl_object_unref (ctx);
}
static CoglBool
compare_component (int a, int b)
{
return ABS (a - b) <= 1;
}
void
test_utils_compare_pixel (const uint8_t *screen_pixel, uint32_t expected_pixel)
{
/* Compare each component with a small fuzz factor */
if (!compare_component (screen_pixel[0], expected_pixel >> 24) ||
!compare_component (screen_pixel[1], (expected_pixel >> 16) & 0xff) ||
!compare_component (screen_pixel[2], (expected_pixel >> 8) & 0xff))
{
uint32_t screen_pixel_num = GUINT32_FROM_BE (*(uint32_t *) screen_pixel);
char *screen_pixel_string =
g_strdup_printf ("#%06x", screen_pixel_num >> 8);
char *expected_pixel_string =
g_strdup_printf ("#%06x", expected_pixel >> 8);
g_assert_cmpstr (screen_pixel_string, ==, expected_pixel_string);
g_free (screen_pixel_string);
g_free (expected_pixel_string);
}
}
void
test_utils_check_pixel (CoglFramebuffer *fb,
int x, int y, uint32_t expected_pixel)
{
uint8_t pixel[4];
cogl_framebuffer_read_pixels (fb,
x, y, 1, 1,
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
pixel);
test_utils_compare_pixel (pixel, expected_pixel);
}
void
test_utils_check_pixel_rgb (CoglFramebuffer *fb,
int x, int y, int r, int g, int b)
{
test_utils_check_pixel (fb, x, y, (r << 24) | (g << 16) | (b << 8));
}
void
test_utils_check_region (CoglFramebuffer *fb,
int x, int y,
int width, int height,
uint32_t expected_rgba)
{
uint8_t *pixels, *p;
pixels = p = g_malloc (width * height * 4);
cogl_framebuffer_read_pixels (fb,
x,
y,
width,
height,
COGL_PIXEL_FORMAT_RGBA_8888,
p);
/* Check whether the center of each division is the right color */
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
{
test_utils_compare_pixel (p, expected_rgba);
p += 4;
}
g_free (pixels);
}
CoglTexture *
test_utils_create_color_texture (CoglContext *context,
uint32_t color)
{
CoglTexture2D *tex_2d;
color = GUINT32_TO_BE (color);
tex_2d = cogl_texture_2d_new_from_data (context,
1, 1, /* width/height */
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
4, /* rowstride */
(uint8_t *) &color,
NULL);
return COGL_TEXTURE (tex_2d);
}
CoglBool
cogl_test_verbose (void)
{
return cogl_test_is_verbose;
}