1
/*
2
 * Copyright © 2007 Chris Wilson
3
 *
4
 * Permission to use, copy, modify, distribute, and sell this software
5
 * and its documentation for any purpose is hereby granted without
6
 * fee, provided that the above copyright notice appear in all copies
7
 * and that both that copyright notice and this permission notice
8
 * appear in supporting documentation, and that the name of
9
 * Chris Wilson not be used in advertising or publicity pertaining to
10
 * distribution of the software without specific, written prior
11
 * permission. Chris Wilson makes no representations about the
12
 * suitability of this software for any purpose.  It is provided "as
13
 * is" without express or implied warranty.
14
 *
15
 * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17
 * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
18
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
 *
23
 * Author: Chris Wilson <chris@chris-wilson.co.uk>
24
 */
25

            
26
#include "cairo-perf.h"
27
#define _USE_MATH_DEFINES /* for M_SQRT2 on win32 */
28
#include <math.h>
29

            
30
static void
31
add_rectangle (cairo_t *cr, double size)
32
{
33
    double x, y;
34

            
35
    if (size < 1)
36
	return;
37

            
38
    cairo_get_current_point (cr, &x, &y);
39

            
40
    cairo_rel_move_to (cr, -size/2., -size/2.);
41
    cairo_rel_line_to (cr, size, 0);
42
    cairo_rel_line_to (cr, 0, size);
43
    cairo_rel_line_to (cr, -size, 0);
44
    cairo_close_path (cr);
45

            
46
    cairo_save (cr);
47
    cairo_translate (cr, -size/2., size);
48
    cairo_move_to (cr, x, y);
49
    cairo_rotate (cr, M_PI/4);
50
    add_rectangle (cr, size / M_SQRT2);
51
    cairo_restore (cr);
52

            
53
    cairo_save (cr);
54
    cairo_translate (cr, size/2., size);
55
    cairo_move_to (cr, x, y);
56
    cairo_rotate (cr, -M_PI/4);
57
    add_rectangle (cr, size / M_SQRT2);
58
    cairo_restore (cr);
59
}
60

            
61
static cairo_time_t
62
do_pythagoras_tree (cairo_t *cr, int width, int height, int loops)
63
{
64
    double size = 128;
65

            
66
    cairo_perf_timer_start ();
67

            
68
    while (loops--) {
69
	cairo_save (cr);
70
	cairo_translate (cr, 0, height);
71
	cairo_scale (cr, 1, -1);
72

            
73
	cairo_move_to (cr, width/2, size/2);
74
	add_rectangle (cr, size);
75
	cairo_set_source_rgb (cr, 0., 0., 0.);
76
	cairo_fill (cr);
77
	cairo_restore (cr);
78
    }
79

            
80
    cairo_perf_timer_stop ();
81

            
82
    return cairo_perf_timer_elapsed ();
83
}
84

            
85
cairo_bool_t
86
pythagoras_tree_enabled (cairo_perf_t *perf)
87
{
88
    return cairo_perf_can_run (perf, "pythagoras-tree", NULL);
89
}
90

            
91
void
92
pythagoras_tree (cairo_perf_t *perf, cairo_t *cr, int width, int height)
93
{
94
    cairo_perf_run (perf, "pythagoras-tree", do_pythagoras_tree, NULL);
95
}