1
/*
2
 * Copyright © 2006 Dan Amelang
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
 * the authors not be used in advertising or publicity pertaining to
10
 * distribution of the software without specific, written prior
11
 * permission. The authors make 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
 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17
 * FITNESS, IN NO EVENT SHALL THE AUTHORS 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
 * Authors: Dan Amelang <dan@amelang.net>
24
 */
25
#include "cairo-perf.h"
26

            
27
#if 0
28
#define MODE cairo_perf_run
29
#else
30
#define MODE cairo_perf_cover_sources_and_operators
31
#endif
32

            
33
#define RECTANGLE_COUNT (1000)
34

            
35
static struct {
36
    double x;
37
    double y;
38
    double width;
39
    double height;
40
} rects[RECTANGLE_COUNT];
41

            
42
static cairo_time_t
43
do_rectangles (cairo_t *cr, int width, int height, int loops)
44
{
45
    int i;
46

            
47
    cairo_perf_timer_start ();
48

            
49
    while (loops--) {
50
	for (i = 0; i < RECTANGLE_COUNT; i++) {
51
	    cairo_rectangle (cr, rects[i].x, rects[i].y,
52
			     rects[i].width, rects[i].height);
53
	    cairo_fill (cr);
54
	}
55
    }
56

            
57
    cairo_perf_timer_stop ();
58

            
59
    return cairo_perf_timer_elapsed ();
60
}
61

            
62
static cairo_time_t
63
do_rectangles_once (cairo_t *cr, int width, int height, int loops)
64
{
65
    int i;
66

            
67
    cairo_perf_timer_start ();
68

            
69
    while (loops--) {
70
	for (i = 0; i < RECTANGLE_COUNT; i++) {
71
	    cairo_rectangle (cr, rects[i].x, rects[i].y,
72
			     rects[i].width, rects[i].height);
73
	}
74

            
75
	cairo_fill (cr);
76
    }
77

            
78
    cairo_perf_timer_stop ();
79

            
80
    return cairo_perf_timer_elapsed ();
81
}
82

            
83
static cairo_time_t
84
do_rectangle (cairo_t *cr, int width, int height, int loops)
85
{
86
    cairo_perf_timer_start ();
87

            
88
    while (loops--) {
89
	cairo_rectangle (cr, 0, 0, width, height);
90
	cairo_fill (cr);
91
    }
92

            
93
    cairo_perf_timer_stop ();
94

            
95
    return cairo_perf_timer_elapsed ();
96
}
97

            
98
cairo_bool_t
99
rectangles_enabled (cairo_perf_t *perf)
100
{
101
    return cairo_perf_can_run (perf, "rectangles", NULL);
102
}
103

            
104
void
105
rectangles (cairo_perf_t *perf, cairo_t *cr, int width, int height)
106
{
107
    int i;
108

            
109
    srand (8478232);
110
    for (i = 0; i < RECTANGLE_COUNT; i++)
111
    {
112
        rects[i].x = rand () % width;
113
        rects[i].y = rand () % height;
114
        rects[i].width  = (rand () % (width / 10)) + 1;
115
        rects[i].height = (rand () % (height / 10)) + 1;
116
    }
117

            
118
    MODE (perf, "one-rectangle", do_rectangle, NULL);
119
    MODE (perf, "rectangles", do_rectangles, NULL);
120
    MODE (perf, "rectangles-once", do_rectangles_once, NULL);
121
}