xlib-expose-event.c [plain text]
#include <stdio.h>
#include <stdlib.h>
#include "cairo.h"
#include "cairo-test.h"
#include "buffer-diff.h"
#define SIZE 160
#define NLOOPS 10
static const char *png_filename = "romedalen.png";
static void
draw_mask (cairo_t *cr)
{
cairo_surface_t *surface;
cairo_t *cr2;
surface = cairo_surface_create_similar (cairo_get_group_target (cr),
CAIRO_CONTENT_ALPHA,
50, 50);
cr2 = cairo_create (surface);
cairo_surface_destroy (surface);
cairo_rectangle (cr2,
0, 0,
40, 40);
cairo_rectangle (cr2,
10, 10,
40, 40);
cairo_clip (cr2);
cairo_move_to (cr2, 0, 25);
cairo_line_to (cr2, 50, 25);
cairo_move_to (cr2, 25, 0);
cairo_line_to (cr2, 25, 50);
cairo_set_source_rgb (cr2, 1, 1, 1);
cairo_stroke (cr2);
cairo_set_source_rgb (cr, 1, 0, 0);
cairo_mask_surface (cr, cairo_get_target (cr2), 50, 50);
cairo_destroy (cr2);
}
static cairo_surface_t *
clone_similar_surface (cairo_surface_t * target, cairo_surface_t *surface)
{
cairo_t *cr;
cairo_surface_t *similar;
similar = cairo_surface_create_similar (target,
cairo_surface_get_content (surface),
cairo_image_surface_get_width (surface),
cairo_image_surface_get_height (surface));
cr = cairo_create (similar);
cairo_surface_destroy (similar);
cairo_set_source_surface (cr, surface, 0, 0);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_paint (cr);
similar = cairo_surface_reference (cairo_get_target (cr));
cairo_destroy (cr);
return similar;
}
static void
draw_image (const cairo_test_context_t *ctx, cairo_t *cr)
{
cairo_surface_t *surface, *similar;
surface = cairo_test_create_surface_from_png (ctx, png_filename);
similar = clone_similar_surface (cairo_get_group_target (cr), surface);
cairo_surface_destroy (surface);
cairo_set_source_surface (cr, similar, 0, 0);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_paint (cr);
cairo_surface_destroy (similar);
}
static void
draw (const cairo_test_context_t *ctx,
cairo_t *cr,
cairo_rectangle_t *region,
int n_regions)
{
cairo_save (cr);
if (region != NULL) {
int i;
for (i = 0; i < n_regions; i++) {
cairo_rectangle (cr,
region[i].x, region[i].y,
region[i].width, region[i].height);
}
cairo_clip (cr);
}
cairo_push_group (cr);
draw_image (ctx, cr);
draw_mask (cr);
cairo_pop_group_to_source (cr);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_paint (cr);
cairo_restore (cr);
}
static cairo_test_status_t
draw_func (cairo_t *cr, int width, int height)
{
cairo_rectangle_t region[4];
const cairo_test_context_t *ctx;
int i, j;
ctx = cairo_test_get_context (cr);
cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR);
draw (ctx, cr, NULL, 0);
for (i = 0; i < NLOOPS; i++) {
for (j = 0; j < NLOOPS; j++) {
region[0].x = i * SIZE / NLOOPS;
region[0].y = i * SIZE / NLOOPS;
region[0].width = SIZE / 4;
region[0].height = SIZE / 4;
region[1].x = j * SIZE / NLOOPS;
region[1].y = j * SIZE / NLOOPS;
region[1].width = SIZE / 4;
region[1].height = SIZE / 4;
region[2].x = i * SIZE / NLOOPS;
region[2].y = j * SIZE / NLOOPS;
region[2].width = SIZE / 4;
region[2].height = SIZE / 4;
region[3].x = j * SIZE / NLOOPS;
region[3].y = i * SIZE / NLOOPS;
region[3].width = SIZE / 4;
region[3].height = SIZE / 4;
draw (ctx, cr, region, 4);
}
}
cairo_pop_group_to_source (cr);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_paint (cr);
return CAIRO_TEST_SUCCESS;
}
CAIRO_TEST (xlib_expose_event,
"Emulate a typical expose event",
"xlib",
NULL,
SIZE, SIZE,
NULL, draw_func)