summaryrefslogtreecommitdiff
path: root/test/map-to-image.c
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2011-07-22 00:36:03 +0100
committerChris Wilson <chris@chris-wilson.co.uk>2011-07-26 14:55:58 +0100
commita69335a84eb9225b477cc8c753470eb3805b852c (patch)
tree7a28492ebf390e513391af587f00c017e4ca6301 /test/map-to-image.c
parentc6812c6a3679c3b8b9584e119e0d7fd93e09ae49 (diff)
downloadcairo-a69335a84eb9225b477cc8c753470eb3805b852c.tar.gz
API: map-to-image and create-similar-image
A common requirement is the fast upload of pixel data. In order to allocate the most appropriate image buffer, we need knowledge of the destination. The most obvious example is that we could use a shared-memory region for the image to avoid the transfer cost of uploading the pixels to the X server. Similarly, gl, win32, quartz... The other side of the equation is that for manual modification of a remote surface, it would be more efficient if we can create a similar image to reduce the transfer costs. This strategy is already followed for the destination fallbacks and this merely exposes the same capability for the application fallbacks. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Diffstat (limited to 'test/map-to-image.c')
-rw-r--r--test/map-to-image.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/test/map-to-image.c b/test/map-to-image.c
new file mode 100644
index 000000000..8528fa0d9
--- /dev/null
+++ b/test/map-to-image.c
@@ -0,0 +1,126 @@
+/*
+ * Copyright © 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Author: Chris Wilson <chris@chris-wilson.co.uk>
+ */
+
+#include "cairo-test.h"
+
+#define WIDTH 3
+#define HEIGHT 3
+
+/* A single, black pixel */
+static const uint32_t black_pixel = 0xff000000;
+
+static cairo_test_status_t
+all (cairo_t *cr, int width, int height)
+{
+ cairo_surface_t *surface;
+ uint8_t *data;
+ int stride;
+ int i, j;
+
+ /* Fill background white */
+ cairo_set_source_rgb (cr, 1, 1, 1);
+ cairo_paint (cr);
+
+ surface = cairo_surface_map_to_image (cairo_get_target (cr), NULL);
+ cairo_surface_flush (surface);
+ stride = cairo_image_surface_get_stride (surface);
+ data = cairo_image_surface_get_data (surface);
+ if (data) {
+ for (j = 0; j < HEIGHT; j++)
+ for (i = 0; i < WIDTH; i++)
+ *(uint32_t *)(data + j * stride + 4*i) = black_pixel;
+ }
+ cairo_surface_mark_dirty (surface);
+ cairo_surface_unmap_image (cairo_get_target (cr), surface);
+
+ return CAIRO_TEST_SUCCESS;
+}
+
+static cairo_test_status_t
+bit (cairo_t *cr, int width, int height)
+{
+ cairo_surface_t *surface;
+ cairo_rectangle_t extents;
+ uint8_t *data;
+
+ extents.x = extents.y = extents.width = extents.height = 1;
+
+ /* Fill background white */
+ cairo_set_source_rgb (cr, 1, 1, 1);
+ cairo_paint (cr);
+
+ surface = cairo_surface_map_to_image (cairo_get_target (cr), &extents);
+ cairo_surface_flush (surface);
+ data = cairo_image_surface_get_data (surface);
+ if (data)
+ *(uint32_t *)data = black_pixel;
+ cairo_surface_mark_dirty (surface);
+ cairo_surface_unmap_image (cairo_get_target (cr), surface);
+
+ return CAIRO_TEST_SUCCESS;
+}
+
+static cairo_test_status_t
+fill (cairo_t *cr, int width, int height)
+{
+ cairo_surface_t *surface;
+ cairo_rectangle_t extents;
+ cairo_t *cr2;
+
+ extents.x = extents.y = extents.width = extents.height = 1;
+
+ /* Fill background white */
+ cairo_set_source_rgb (cr, 1, 1, 1);
+ cairo_paint (cr);
+
+ surface = cairo_surface_map_to_image (cairo_get_target (cr), &extents);
+ cr2 = cairo_create (surface);
+ cairo_set_source_rgb (cr2, 1, 0, 0);
+ cairo_paint (cr2);
+ cairo_destroy (cr2);
+ cairo_surface_unmap_image (cairo_get_target (cr), surface);
+
+ return CAIRO_TEST_SUCCESS;
+}
+
+CAIRO_TEST (map_all_to_image,
+ "Test maping a surface to an image and modifying it externally",
+ "image", /* keywords */
+ "target=raster", /* requirements */
+ WIDTH, HEIGHT,
+ NULL, all)
+CAIRO_TEST (map_bit_to_image,
+ "Test maping a surface to an image and modifying it externally",
+ "image", /* keywords */
+ "target=raster", /* requirements */
+ WIDTH, HEIGHT,
+ NULL, bit)
+CAIRO_TEST (map_to_image_fill,
+ "Test maping a surface to an image and modifying it externally",
+ "image", /* keywords */
+ "target=raster", /* requirements */
+ WIDTH, HEIGHT,
+ NULL, fill)