1
/*
2
 * Copyright © 2006 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: Carl D. Worth <cworth@cworth.org>
25
 */
26

            
27
#include "cairo-perf.h"
28

            
29
/* This test case is designed to illustrate a performance bug in
30
 * drawing very long lines, where most of the line is out of bounds of
31
 * the destination surface, (but some portion of the line is
32
 * visible). These results are in the "long-lines-uncropped" report.
33
 *
34
 * For comparison, this test also renders the visible portions of the
35
 * same lines, (this is the "long-lines-cropped" report).
36
 */
37

            
38
typedef enum {
39
    LONG_LINES_CROPPED = 0x1,
40
    LONG_LINES_ONCE = 0x2,
41
} long_lines_crop_t;
42
#define NUM_LINES    20
43
#define LONG_FACTOR  50.0
44

            
45
static cairo_time_t
46
do_long_lines (cairo_t *cr, int width, int height, int loops, long_lines_crop_t crop)
47
{
48
    int i;
49
    double x, y, dx, dy, min_x, min_y, max_x, max_y;
50
    double outer_width, outer_height;
51

            
52
    cairo_save (cr);
53

            
54
    cairo_translate (cr, width / 2, height / 2);
55

            
56
    if (crop & LONG_LINES_CROPPED) {
57
	outer_width = width;
58
	outer_height = height;
59
	cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); /* green */
60
    } else {
61
	outer_width = LONG_FACTOR * width;
62
	outer_height = LONG_FACTOR * height;
63
	cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); /* red */
64
    }
65

            
66
    min_x = x = - outer_width / 2.0;
67
    min_y = y = - outer_height / 2.0;
68
    max_x = outer_width / 2.0;
69
    max_y = outer_height / 2.0;
70
    dx = outer_width / NUM_LINES;
71
    dy = outer_height / NUM_LINES;
72

            
73
    cairo_perf_timer_start ();
74

            
75
    while (loops--) {
76
	for (i = 0; i <= NUM_LINES; i++) {
77
	    cairo_move_to (cr, 0, 0);
78
	    cairo_line_to (cr, x, min_y);
79
	    if ((crop & LONG_LINES_ONCE) == 0)
80
		cairo_stroke (cr);
81

            
82
	    cairo_move_to (cr, 0, 0);
83
	    cairo_line_to (cr, x, max_y);
84
	    if ((crop & LONG_LINES_ONCE) == 0)
85
		cairo_stroke (cr);
86

            
87
	    cairo_move_to (cr, 0, 0);
88
	    cairo_line_to (cr, min_x, y);
89
	    if ((crop & LONG_LINES_ONCE) == 0)
90
		cairo_stroke (cr);
91

            
92
	    cairo_move_to (cr, 0, 0);
93
	    cairo_line_to (cr, max_x, y);
94
	    if ((crop & LONG_LINES_ONCE) == 0)
95
		cairo_stroke (cr);
96

            
97
	    x += dx;
98
	    y += dy;
99
	}
100
	if (crop & LONG_LINES_ONCE)
101
	    cairo_stroke (cr);
102
    }
103

            
104
    cairo_perf_timer_stop ();
105

            
106
    cairo_restore (cr);
107

            
108
    return cairo_perf_timer_elapsed ();
109
}
110

            
111
static cairo_time_t
112
long_lines_uncropped (cairo_t *cr, int width, int height, int loops)
113
{
114
    return do_long_lines (cr, width, height, loops, 0);
115
}
116

            
117
static cairo_time_t
118
long_lines_uncropped_once (cairo_t *cr, int width, int height, int loops)
119
{
120
    return do_long_lines (cr, width, height, loops, LONG_LINES_ONCE);
121
}
122

            
123
static cairo_time_t
124
long_lines_cropped (cairo_t *cr, int width, int height, int loops)
125
{
126
    return do_long_lines (cr, width, height, loops, LONG_LINES_CROPPED);
127
}
128

            
129
static cairo_time_t
130
long_lines_cropped_once (cairo_t *cr, int width, int height, int loops)
131
{
132
    return do_long_lines (cr, width, height, loops, LONG_LINES_CROPPED | LONG_LINES_ONCE);
133
}
134

            
135
cairo_bool_t
136
long_lines_enabled (cairo_perf_t *perf)
137
{
138
    return cairo_perf_can_run (perf, "long-lines", NULL);
139
}
140

            
141
void
142
long_lines (cairo_perf_t *perf, cairo_t *cr, int width, int height)
143
{
144
    cairo_perf_run (perf, "long-lines-uncropped", long_lines_uncropped, NULL);
145
    cairo_perf_run (perf, "long-lines-uncropped-once", long_lines_uncropped_once, NULL);
146
    cairo_perf_run (perf, "long-lines-cropped", long_lines_cropped, NULL);
147
    cairo_perf_run (perf, "long-lines-cropped-once", long_lines_cropped_once, NULL);
148
}