summaryrefslogtreecommitdiff
path: root/perf/dragon.c
blob: 366b63c209a764f878be96f6f05beba2105b41ac (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright © 2007 Chris Wilson
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Chris Wilson not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. Chris Wilson makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Chris Wilson <chris@chris-wilson.co.uk>
 *
 * Inspiration (and path!) taken from
 * http://labs.trolltech.com/blogs/2007/08/31/rasterizing-dragons/
 */

#include "cairo-perf.h"

#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif

#ifndef MAX
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif

static inline int
next_pot (int v)
{
    v--;
    v |= v >> 1;
    v |= v >> 2;
    v |= v >> 4;
    v |= v >> 8;
    v |= v >> 16;
    v++;
    return v;
}

static cairo_bool_t
direction (int i)
{
    int pivot, np2;

    if (i < 2)
	return TRUE;

    np2 = next_pot (i + 1);
    if (np2 == i + 1)
	return TRUE;

    pivot = np2 / 2 - 1;
    return ! direction (2 * pivot - i);
}

static void
path (cairo_t *cr, int step, int dir, int iterations)
{
    double dx, dy;
    int i;

    switch (dir) {
	case 0: dx =  step; dy =  0; break;
	case 1: dx = -step; dy =  0; break;
	case 2: dx =  0; dy =  step; break;
	case 3: dx =  0; dy = -step; break;
    }

    for (i = 0; i < iterations; i++) {
	cairo_rel_line_to (cr, dx, dy);

	if (direction (i)) {
	    double t = dx;
	    dx = dy;
	    dy = -t;
	} else {
	    double t = dx;
	    dx = -dy;
	    dy = t;
	}
    }
}

static cairo_perf_ticks_t
do_dragon (cairo_t *cr, int width, int height, int loops)
{
    cairo_pattern_t *pattern;
    double cx, cy, r;

    cx = cy = .5 * MAX (width, height);
    r = .5 * MIN (width, height);

    cairo_perf_timer_start ();

    while (loops--) {
	pattern = cairo_pattern_create_radial (cx, cy, 0., cx, cy, r);
	cairo_pattern_add_color_stop_rgb (pattern, 0., .0, .0, .0);
	cairo_pattern_add_color_stop_rgb (pattern, 0.25, .5, .4, .4);
	cairo_pattern_add_color_stop_rgb (pattern, .5, .8, .8, .9);
	cairo_pattern_add_color_stop_rgb (pattern, 1., .9, .9, 1.);
	cairo_set_source (cr, pattern);
	cairo_pattern_destroy (pattern);
	cairo_paint (cr);

	cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
	cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
	cairo_set_line_width (cr, 4.);

	cairo_move_to (cr, cx, cy);
	path (cr, 12, 0, 2048);
	pattern = cairo_pattern_create_radial (cx, cy, 0., cx, cy, r);
	cairo_pattern_add_color_stop_rgb (pattern, 0., 1., 1., 1.);
	cairo_pattern_add_color_stop_rgb (pattern, 1., 0., 0., 0.);
	cairo_set_source (cr, pattern);
	cairo_pattern_destroy (pattern);
	cairo_stroke(cr);

	cairo_move_to (cr, cx, cy);
	path (cr, 12, 1, 2048);
	pattern = cairo_pattern_create_radial (cx, cy, 0., cx, cy, r);
	cairo_pattern_add_color_stop_rgb (pattern, 1., 1., 1., 0.);
	cairo_pattern_add_color_stop_rgb (pattern, 0., 1., 0., 0.);
	cairo_set_source (cr, pattern);
	cairo_pattern_destroy (pattern);
	cairo_stroke(cr);

	cairo_move_to (cr, cx, cy);
	path (cr, 12, 2, 2048);
	pattern = cairo_pattern_create_radial (cx, cy, 0., cx, cy, r);
	cairo_pattern_add_color_stop_rgb (pattern, 1., 0., 1., 1.);
	cairo_pattern_add_color_stop_rgb (pattern, 0., 0., 1., 0.);
	cairo_set_source (cr, pattern);
	cairo_pattern_destroy (pattern);
	cairo_stroke(cr);

	cairo_move_to (cr, cx, cy);
	path (cr, 12, 3, 2048);
	pattern = cairo_pattern_create_radial (cx, cy, 0., cx, cy, r);
	cairo_pattern_add_color_stop_rgb (pattern, 1., 1., 0., 1.);
	cairo_pattern_add_color_stop_rgb (pattern, 0., 0., 0., 1.);
	cairo_set_source (cr, pattern);
	cairo_pattern_destroy (pattern);
	cairo_stroke(cr);
    }

    cairo_perf_timer_stop ();

    return cairo_perf_timer_elapsed ();
}

static cairo_perf_ticks_t
do_dragon_solid (cairo_t *cr, int width, int height, int loops)
{
    double cx, cy, r;

    cx = cy = .5 * MAX (width, height);
    r = .5 * MIN (width, height);

    cairo_perf_timer_start ();

    while (loops--) {
	cairo_set_source_rgb (cr, 0, 0, 0);
	cairo_paint (cr);

	cairo_set_line_width (cr, 4.);

	cairo_move_to (cr, cx, cy);
	path (cr, 12, 0, 2048);
	cairo_set_source_rgb (cr, 1, 0, 0);
	cairo_stroke(cr);

	cairo_move_to (cr, cx, cy);
	path (cr, 12, 1, 2048);
	cairo_set_source_rgb (cr, 0, 1, 0);
	cairo_stroke(cr);

	cairo_move_to (cr, cx, cy);
	path (cr, 12, 2, 2048);
	cairo_set_source_rgb (cr, 0, 0, 1);
	cairo_stroke(cr);

	cairo_move_to (cr, cx, cy);
	path (cr, 12, 3, 2048);
	cairo_set_source_rgb (cr, 1, 1, 1);
	cairo_stroke(cr);
    }

    cairo_perf_timer_stop ();

    return cairo_perf_timer_elapsed ();
}

void
dragon (cairo_perf_t *perf, cairo_t *cr, int width, int height)
{
    if (! cairo_perf_can_run (perf, "dragon"))
	return;

    cairo_perf_run (perf, "dragon-solid", do_dragon_solid);
    cairo_perf_run (perf, "dragon", do_dragon);
}