summaryrefslogtreecommitdiff
path: root/perf/micro/long-lines.c
blob: a0d134c2da04eb53d7148a33155852c206590f9d (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
/*
 * Copyright © 2006 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Author: Carl D. Worth <cworth@cworth.org>
 */

#include "cairo-perf.h"

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

typedef enum {
    LONG_LINES_CROPPED = 0x1,
    LONG_LINES_ONCE = 0x2,
} long_lines_crop_t;
#define NUM_LINES    20
#define LONG_FACTOR  50.0

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

    cairo_save (cr);

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

    if (crop & LONG_LINES_CROPPED) {
	outer_width = width;
	outer_height = height;
	cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); /* green */
    } else {
	outer_width = LONG_FACTOR * width;
	outer_height = LONG_FACTOR * height;
	cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); /* red */
    }

    min_x = x = - outer_width / 2.0;
    min_y = y = - outer_height / 2.0;
    max_x = outer_width / 2.0;
    max_y = outer_height / 2.0;
    dx = outer_width / NUM_LINES;
    dy = outer_height / NUM_LINES;

    cairo_perf_timer_start ();

    while (loops--) {
	for (i = 0; i <= NUM_LINES; i++) {
	    cairo_move_to (cr, 0, 0);
	    cairo_line_to (cr, x, min_y);
	    if ((crop & LONG_LINES_ONCE) == 0)
		cairo_stroke (cr);

	    cairo_move_to (cr, 0, 0);
	    cairo_line_to (cr, x, max_y);
	    if ((crop & LONG_LINES_ONCE) == 0)
		cairo_stroke (cr);

	    cairo_move_to (cr, 0, 0);
	    cairo_line_to (cr, min_x, y);
	    if ((crop & LONG_LINES_ONCE) == 0)
		cairo_stroke (cr);

	    cairo_move_to (cr, 0, 0);
	    cairo_line_to (cr, max_x, y);
	    if ((crop & LONG_LINES_ONCE) == 0)
		cairo_stroke (cr);

	    x += dx;
	    y += dy;
	}
	if (crop & LONG_LINES_ONCE)
	    cairo_stroke (cr);
    }

    cairo_perf_timer_stop ();

    cairo_restore (cr);

    return cairo_perf_timer_elapsed ();
}

static cairo_time_t
long_lines_uncropped (cairo_t *cr, int width, int height, int loops)
{
    return do_long_lines (cr, width, height, loops, 0);
}

static cairo_time_t
long_lines_uncropped_once (cairo_t *cr, int width, int height, int loops)
{
    return do_long_lines (cr, width, height, loops, LONG_LINES_ONCE);
}

static cairo_time_t
long_lines_cropped (cairo_t *cr, int width, int height, int loops)
{
    return do_long_lines (cr, width, height, loops, LONG_LINES_CROPPED);
}

static cairo_time_t
long_lines_cropped_once (cairo_t *cr, int width, int height, int loops)
{
    return do_long_lines (cr, width, height, loops, LONG_LINES_CROPPED | LONG_LINES_ONCE);
}

cairo_bool_t
long_lines_enabled (cairo_perf_t *perf)
{
    return cairo_perf_can_run (perf, "long-lines", NULL);
}

void
long_lines (cairo_perf_t *perf, cairo_t *cr, int width, int height)
{
    cairo_perf_run (perf, "long-lines-uncropped", long_lines_uncropped, NULL);
    cairo_perf_run (perf, "long-lines-uncropped-once", long_lines_uncropped_once, NULL);
    cairo_perf_run (perf, "long-lines-cropped", long_lines_cropped, NULL);
    cairo_perf_run (perf, "long-lines-cropped-once", long_lines_cropped_once, NULL);
}