tests: convert reference image loader to pixman

This rewrites load_surface_from_png() to load_image_from_png(), to
return a pixman_image_t instead of a struct surface.

A loaded image has no need for wl_buffer or wl_surface or any of the
associated attributes. This is part of unifying to make everything use
pixman_image_t.

cairo_surface_flush() is added, because Cairo documentation for
cairo_image_surface_get_data() says you have to flush after drawing,
before using the data. It is unclear if loading a PNG counts as drawing,
so stay on the safe side.

load_image_from_png() now pays attention to the pixel format returned by
Cairo, which seems to come out as CAIRO_FORMAT_RGB24 in
internal-screenshot-test, not as CAIRO_FORMAT_ARGB32 as expected. I do
not know if Cairo actually guarantees the x8/a8 channel to be 0xff for
RGB24, but better to not trust it. Therefore the image is explicitly
converted to a8r8g8b8 as needed. This also adds support for loading A8
and RGB16_565 images, provided that Cairo delivers them.

The cairo surface is now wrapped directly into a pixman_image_t. If the
pixel format conversion is not needed, this eliminates a copy of the
image data. The Cairo surface will get automatically destroyed with the
Pixman image.

Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
Reviewed-by: Daniel Stone <daniels@collabora.com>
diff --git a/tests/internal-screenshot-test.c b/tests/internal-screenshot-test.c
index ce41181..1e3c17f 100644
--- a/tests/internal-screenshot-test.c
+++ b/tests/internal-screenshot-test.c
@@ -66,8 +66,8 @@
 	struct client *client;
 	struct wl_surface *surface;
 	struct surface *screenshot = NULL;
-	struct surface *reference_good = NULL;
-	struct surface *reference_bad = NULL;
+	pixman_image_t *reference_good = NULL;
+	pixman_image_t *reference_bad = NULL;
 	struct rectangle clip;
 	const char *fname;
 	bool match = false;
@@ -111,24 +111,23 @@
 	/* Load good reference image */
 	fname = screenshot_reference_filename("internal-screenshot-good", 0);
 	printf("Loading good reference image %s\n", fname);
-	reference_good = load_surface_from_png(fname);
+	reference_good = load_image_from_png(fname);
 	assert(reference_good);
 
 	/* Load bad reference image */
 	fname = screenshot_reference_filename("internal-screenshot-bad", 0);
 	printf("Loading bad reference image %s\n", fname);
-	reference_bad = load_surface_from_png(fname);
+	reference_bad = load_image_from_png(fname);
 	assert(reference_bad);
 
 	/* Test check_images_match() without a clip.
 	 * We expect this to fail since we use a bad reference image
 	 */
 	match = check_images_match(screenshot->buffer->image,
-				   reference_bad->buffer->image, NULL);
+				   reference_bad, NULL);
 	printf("Screenshot %s reference image\n", match? "equal to" : "different from");
 	assert(!match);
-	buffer_destroy(reference_bad->buffer);
-	free(reference_bad);
+	pixman_image_unref(reference_bad);
 
 	/* Test check_images_match() with clip.
 	 * Alpha-blending and other effects can cause irrelevant discrepancies, so look only
@@ -140,11 +139,9 @@
 	clip.height = 100;
 	printf("Clip: %d,%d %d x %d\n", clip.x, clip.y, clip.width, clip.height);
 	match = check_images_match(screenshot->buffer->image,
-				   reference_good->buffer->image,
-				   &clip);
+				   reference_good, &clip);
 	printf("Screenshot %s reference image in clipped area\n", match? "matches" : "doesn't match");
-	buffer_destroy(reference_good->buffer);
-	free(reference_good);
+	pixman_image_unref(reference_good);
 
 	/* Test dumping of non-matching images */
 	if (!match || dump_all_images) {