summaryrefslogtreecommitdiffstatsci
path: root/src/cairo-tests/mask.cpp
blob: feb1db1780d2ba898987742fadd21a8eac6256f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright 2014 © Samsung Research America, Silicon Valley
 *
 * Use of this source code is governed by the 3-Clause BSD license
 * specified in the COPYING file included with this source code.
 */
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <cairo.h>
#if USE_EGL
# include <cairo-gl.h>
#endif

#include "caskbench.h"
#include "caskbench_context.h"
#include "cairo-shapes.h"

static cairo_pattern_t *mask;

int
ca_setup_mask(caskbench_context_t *ctx)
{
    // See https://git.gnome.org/browse/gtk+/tree/gtk/gtkcolorbutton.c?id=804c80097995bb0026544118a32b59e23ee2fbe3#n241
    static unsigned char data[16] = { 0xFF, 0x00, 0x00, 0xFF,
                                      0x00, 0xFF, 0xFF, 0x00,
                                      0x00, 0xFF, 0xFF, 0x00,
                                      0xFF, 0x00, 0x00, 0xFF, };
    cairo_surface_t *stippled = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_A8, 2, 2, 4);
#if USE_EGL
    if (cairo_surface_get_type (ctx->cairo_surface) == CAIRO_SURFACE_TYPE_GL) {
        cairo_surface_t *cairo_surface;
        cairo_device_t *cairo_device = cairo_surface_get_device (ctx->cairo_surface);

        /* create a gl surface similar to image surface */
        cairo_surface = cairo_gl_surface_create (cairo_device, CAIRO_CONTENT_COLOR_ALPHA, 2, 2);
        cairo_t *gl_ctx = cairo_create (cairo_surface);
        cairo_pattern_t *pattern = cairo_pattern_create_for_surface (stippled);
        cairo_set_source (gl_ctx, pattern);

        /* paint image surface to gl surface as a pattern */
        cairo_paint (gl_ctx);
        cairo_destroy (gl_ctx);

        /* use gl surface as mask */
        mask = cairo_pattern_create_for_surface (cairo_surface);
    }
    else
#endif
        mask = cairo_pattern_create_for_surface (stippled);
    cairo_pattern_set_extend (mask, CAIRO_EXTEND_REPEAT);
    cairo_pattern_set_filter (mask, CAIRO_FILTER_NEAREST);

    return 1;
}

void
ca_teardown_mask(void)
{
    cairo_pattern_destroy (mask);
}

int
ca_test_mask(caskbench_context_t *ctx)
{
    int i;
    cairo_t *cr = ctx->cairo_cr;

    for (i=0; i<ctx->size; i++) {
        // Apply mask on a circle
        cairoRandomizeColor(ctx);

        cairo_arc (cr, 40*i, 40, 30, 0, 2*M_PI);
        cairo_save (cr);
        cairo_clip (cr);
        cairo_mask (cr, mask);
        cairo_fill (cr);
        cairo_restore (cr);
    }
    return 1;
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :