[PATCH weston 09/11] tests: convert image saver to pixman
Pekka Paalanen
ppaalanen at gmail.com
Thu Jun 16 14:13:28 UTC 2016
From: Pekka Paalanen <pekka.paalanen at collabora.co.uk>
This rewrites write_surface_as_png() into write_image_as_png(), which
operates on a pixman_image_t instead of a struct surface.
This is part of the migration to use pixman_image_t everywhere without
superfluous parameters/members.
Now the image saving handles more than just ARGB32 format, presumably.
At least it does not assume everything is always ARGB32.
Signed-off-by: Pekka Paalanen <pekka.paalanen at collabora.co.uk>
---
tests/internal-screenshot-test.c | 2 +-
tests/weston-test-client-helper.c | 53 ++++++++++++++++++++++++++-------------
tests/weston-test-client-helper.h | 2 +-
3 files changed, 38 insertions(+), 19 deletions(-)
diff --git a/tests/internal-screenshot-test.c b/tests/internal-screenshot-test.c
index 51072a6..5213891 100644
--- a/tests/internal-screenshot-test.c
+++ b/tests/internal-screenshot-test.c
@@ -128,7 +128,7 @@ TEST(internal_screenshot)
/* Test dumping of non-matching images */
if (!match || dump_all_images) {
fname = screenshot_output_filename("internal-screenshot", 0);
- write_surface_as_png(screenshot, fname);
+ write_image_as_png(screenshot->buffer->image, fname);
}
free(screenshot);
diff --git a/tests/weston-test-client-helper.c b/tests/weston-test-client-helper.c
index 67ef4b4..377508f 100644
--- a/tests/weston-test-client-helper.c
+++ b/tests/weston-test-client-helper.c
@@ -991,6 +991,18 @@ format_cairo2pixman(cairo_format_t fmt)
assert(0 && "unknown Cairo pixel format");
}
+static cairo_format_t
+format_pixman2cairo(pixman_format_code_t fmt)
+{
+ unsigned i;
+
+ for (i = 0; i < ARRAY_LENGTH(format_map); i++)
+ if (format_map[i].pixman == fmt)
+ return format_map[i].cairo;
+
+ assert(0 && "unknown Pixman pixel format");
+}
+
/**
* Compute the ROI for image comparisons
*
@@ -1117,36 +1129,43 @@ check_images_match(pixman_image_t *img_a, pixman_image_t *img_b,
return true;
}
-/** write_surface_as_png()
+/**
+ * Write an image into a PNG file.
*
- * Writes out a given weston test surface to disk as a PNG image
- * using the provided filename (with path).
+ * \param image The image.
+ * \param fname The name and path for the file.
*
- * @returns true if successfully saved file; false otherwise.
+ * \returns true if successfully saved file; false otherwise.
+ *
+ * \note Only image formats directly supported by Cairo are accepted, not all
+ * Pixman formats.
*/
bool
-write_surface_as_png(const struct surface *weston_surface, const char *fname)
+write_image_as_png(pixman_image_t *image, const char *fname)
{
cairo_surface_t *cairo_surface;
cairo_status_t status;
- int bpp = 4; /* Assume ARGB */
- int stride = bpp * weston_surface->width;
- void *pixels;
+ cairo_format_t fmt;
+
+ fmt = format_pixman2cairo(pixman_image_get_format(image));
+
+ cairo_surface = cairo_image_surface_create_for_data(
+ (void *)pixman_image_get_data(image),
+ fmt,
+ pixman_image_get_width(image),
+ pixman_image_get_height(image),
+ pixman_image_get_stride(image));
- pixels = pixman_image_get_data(weston_surface->buffer->image);
- cairo_surface = cairo_image_surface_create_for_data(pixels,
- CAIRO_FORMAT_ARGB32,
- weston_surface->width,
- weston_surface->height,
- stride);
- printf("Writing PNG to disk\n");
status = cairo_surface_write_to_png(cairo_surface, fname);
if (status != CAIRO_STATUS_SUCCESS) {
- printf("Failed to save screenshot: %s\n",
- cairo_status_to_string(status));
+ fprintf(stderr, "Failed to save image '%s': %s\n", fname,
+ cairo_status_to_string(status));
+
return false;
}
+
cairo_surface_destroy(cairo_surface);
+
return true;
}
diff --git a/tests/weston-test-client-helper.h b/tests/weston-test-client-helper.h
index e74e835..adeeee3 100644
--- a/tests/weston-test-client-helper.h
+++ b/tests/weston-test-client-helper.h
@@ -202,7 +202,7 @@ check_images_match(pixman_image_t *img_a, pixman_image_t *img_b,
const struct rectangle *clip);
bool
-write_surface_as_png(const struct surface *weston_surface, const char *fname);
+write_image_as_png(pixman_image_t *image, const char *fname);
pixman_image_t *
load_image_from_png(const char *fname);
--
2.7.3
More information about the wayland-devel
mailing list