From fd10ef0f630409e6fbd758fd1b5a9c316234c2c7 Mon Sep 17 00:00:00 2001 From: Pekka Paalanen Date: Fri, 27 May 2016 13:54:35 +0300 Subject: [PATCH] tests: convert image saver to pixman 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 Reviewed-by: Daniel Stone --- 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 1e3c17ff..dfa7ac1a 100644 --- a/tests/internal-screenshot-test.c +++ b/tests/internal-screenshot-test.c @@ -146,7 +146,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 67ef4b4b..377508f5 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 e74e8356..adeeee35 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);