Upload Tizen2.0 source
[framework/graphics/cairo.git] / perf / micro / long-lines.c
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  * visibile). 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     cairo_perf_set_thread_aware (cr, FALSE);
75
76     while (loops--) {
77         if (loops == 0)
78                 cairo_perf_set_thread_aware (cr, TRUE);
79         for (i = 0; i <= NUM_LINES; i++) {
80             cairo_move_to (cr, 0, 0);
81             cairo_line_to (cr, x, min_y);
82             if ((crop & LONG_LINES_ONCE) == 0)
83                 cairo_stroke (cr);
84
85             cairo_move_to (cr, 0, 0);
86             cairo_line_to (cr, x, max_y);
87             if ((crop & LONG_LINES_ONCE) == 0)
88                 cairo_stroke (cr);
89
90             cairo_move_to (cr, 0, 0);
91             cairo_line_to (cr, min_x, y);
92             if ((crop & LONG_LINES_ONCE) == 0)
93                 cairo_stroke (cr);
94
95             cairo_move_to (cr, 0, 0);
96             cairo_line_to (cr, max_x, y);
97             if ((crop & LONG_LINES_ONCE) == 0)
98                 cairo_stroke (cr);
99
100             x += dx;
101             y += dy;
102         }
103         if (crop & LONG_LINES_ONCE)
104             cairo_stroke (cr);
105     }
106
107     cairo_perf_timer_stop ();
108
109     cairo_restore (cr);
110
111     return cairo_perf_timer_elapsed ();
112 }
113
114 static cairo_time_t
115 long_lines_uncropped (cairo_t *cr, int width, int height, int loops)
116 {
117     return do_long_lines (cr, width, height, loops, 0);
118 }
119
120 static cairo_time_t
121 long_lines_uncropped_once (cairo_t *cr, int width, int height, int loops)
122 {
123     return do_long_lines (cr, width, height, loops, LONG_LINES_ONCE);
124 }
125
126 static cairo_time_t
127 long_lines_cropped (cairo_t *cr, int width, int height, int loops)
128 {
129     return do_long_lines (cr, width, height, loops, LONG_LINES_CROPPED);
130 }
131
132 static cairo_time_t
133 long_lines_cropped_once (cairo_t *cr, int width, int height, int loops)
134 {
135     return do_long_lines (cr, width, height, loops, LONG_LINES_CROPPED | LONG_LINES_ONCE);
136 }
137
138 cairo_bool_t
139 long_lines_enabled (cairo_perf_t *perf)
140 {
141     return cairo_perf_can_run (perf, "long-lines", NULL);
142 }
143
144 void
145 long_lines (cairo_perf_t *perf, cairo_t *cr, int width, int height)
146 {
147     cairo_perf_run (perf, "long-lines-uncropped", long_lines_uncropped, NULL);
148     cairo_perf_run (perf, "long-lines-uncropped-once", long_lines_uncropped_once, NULL);
149     cairo_perf_run (perf, "long-lines-cropped", long_lines_cropped, NULL);
150     cairo_perf_run (perf, "long-lines-cropped-once", long_lines_cropped_once, NULL);
151 }