1
/*
2
 * Copyright 2011 Red Hat Inc.
3
 *
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use, copy,
8
 * modify, merge, publish, distribute, sublicense, and/or sell copies
9
 * of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 *
24
 * Author: Benjamin Otte <otte@redhat.com>
25
 */
26

            
27
#include "cairo-perf.h"
28

            
29
static cairo_surface_t *
30
generate_random_waveform(cairo_t *target, int width, int height)
31
{
32
    cairo_surface_t *surface;
33
    cairo_t *cr;
34
    int i, r;
35

            
36
    srand (0xdeadbeef);
37

            
38
    surface = cairo_surface_create_similar (cairo_get_target (target),
39
					    CAIRO_CONTENT_ALPHA,
40
					    width, height);
41
    cr = cairo_create (surface);
42

            
43
    r = height / 2;
44

            
45
    for (i = 0; i < width; i++)
46
    {
47
	r += rand () % (height / 4) - (height / 8);
48
	if (r < 0)
49
	    r = 0;
50
	else if (r > height)
51
	    r = height;
52
	cairo_rectangle (cr, i, (height - r) / 2, 1, r);
53
    }
54
    cairo_fill (cr);
55
    cairo_destroy (cr);
56

            
57
    return surface;
58
}
59

            
60
static cairo_time_t
61
do_wave (cairo_t *cr, int width, int height, int loops)
62
{
63
    cairo_surface_t *wave;
64
    cairo_pattern_t *mask;
65

            
66
    wave = generate_random_waveform (cr, width, height);
67

            
68
    cairo_perf_timer_start ();
69

            
70
    while (loops--) {
71
	/* paint outline (and contents) */
72
	cairo_set_source_rgb (cr, 1, 0, 0);
73
	cairo_mask_surface (cr, wave, 0, 0);
74

            
75
	/* overdraw inline */
76
	/* first, create a mask */
77
	cairo_push_group_with_content (cr, CAIRO_CONTENT_ALPHA);
78
	cairo_set_source_surface (cr, wave, 0, 0);
79
	cairo_paint (cr);
80
	cairo_set_operator (cr, CAIRO_OPERATOR_IN);
81
	cairo_set_source_surface (cr, wave, 1, 0);
82
	cairo_paint (cr);
83
	cairo_set_source_surface (cr, wave, -1, 0);
84
	cairo_paint (cr);
85
	cairo_set_source_surface (cr, wave, 0, 1);
86
	cairo_paint (cr);
87
	cairo_set_source_surface (cr, wave, 0, -1);
88
	cairo_paint (cr);
89
	mask = cairo_pop_group (cr);
90

            
91
	/* second, paint the mask */
92
	cairo_set_source_rgb (cr, 0, 1, 0);
93
	cairo_mask (cr, mask);
94

            
95
	cairo_pattern_destroy (mask);
96
    }
97

            
98
    cairo_perf_timer_stop ();
99

            
100
    cairo_surface_destroy (wave);
101

            
102
    return cairo_perf_timer_elapsed ();
103
}
104

            
105
cairo_bool_t
106
wave_enabled (cairo_perf_t *perf)
107
{
108
    return cairo_perf_can_run (perf, "wave", NULL);
109
}
110

            
111
void
112
wave (cairo_perf_t *perf, cairo_t *cr, int width, int height)
113
{
114
    cairo_perf_run (perf, "wave", do_wave, NULL);
115
}