ebabd9a18faf62ded1319b099a155b749b1828b1
[framework/graphics/cairo.git] / perf / micro / glyphs.c
1 /*
2  * Copyright © 2006 Red Hat, Inc.
3  * Copyright © 2009 Chris Wilson
4  *
5  * Permission to use, copy, modify, distribute, and sell this software
6  * and its documentation for any purpose is hereby granted without
7  * fee, provided that the above copyright notice appear in all copies
8  * and that both that copyright notice and this permission notice
9  * appear in supporting documentation, and that the name of
10  * Red Hat, Inc. not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. Red Hat, Inc. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as
14  * is" without express or implied warranty.
15  *
16  * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
19  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Author: Carl D. Worth <cworth@cworth.org>
25  *         Chris Wilson <chris@chris-wilson.co.uk>
26  */
27
28 #include "cairo-perf.h"
29
30 static cairo_time_t
31 do_glyphs (double font_size,
32            cairo_antialias_t antialias,
33            cairo_t *cr, int width, int height, int loops)
34 {
35     const char text[] = "the jay, pig, fox, zebra and my wolves quack";
36     cairo_scaled_font_t *scaled_font;
37     cairo_glyph_t *glyphs = NULL, *glyphs_copy;
38     cairo_text_extents_t extents;
39     cairo_font_options_t *options;
40     cairo_status_t status;
41     double x, y;
42     int num_glyphs, n;
43
44     options = cairo_font_options_create ();
45     cairo_font_options_set_antialias (options, antialias);
46     cairo_set_font_options (cr, options);
47     cairo_font_options_destroy (options);
48
49     cairo_select_font_face (cr,
50                             "@cairo:",
51                             CAIRO_FONT_SLANT_NORMAL,
52                             CAIRO_FONT_WEIGHT_NORMAL);
53     cairo_set_font_size (cr, font_size);
54     scaled_font = cairo_get_scaled_font (cr);
55     status = cairo_scaled_font_text_to_glyphs (scaled_font, 0., 0.,
56                                                text, -1,
57                                                &glyphs, &num_glyphs,
58                                                NULL, NULL,
59                                                NULL);
60     if (status)
61         return 0;
62
63     glyphs_copy = cairo_glyph_allocate (num_glyphs);
64     if (glyphs_copy == NULL) {
65         cairo_glyph_free (glyphs);
66         return 0;
67     }
68
69     cairo_scaled_font_glyph_extents (scaled_font,
70                                      glyphs, num_glyphs,
71                                      &extents);
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         y = 0;
80         do {
81             x = 0;
82             do {
83                 for (n = 0; n < num_glyphs; n++) {
84                     glyphs_copy[n] = glyphs[n];
85                     glyphs_copy[n].x += x;
86                     glyphs_copy[n].y += y;
87                 }
88                 cairo_show_glyphs (cr, glyphs_copy, num_glyphs);
89
90                 x += extents.width;
91             } while (x < width);
92             y += extents.height;
93         } while (y < height);
94     }
95
96     cairo_perf_timer_stop ();
97
98     cairo_glyph_free (glyphs);
99     cairo_glyph_free (glyphs_copy);
100
101     return cairo_perf_timer_elapsed ();
102 }
103
104 static double
105 count_glyphs (double font_size,
106               cairo_antialias_t antialias,
107               cairo_t *cr, int width, int height)
108 {
109     const char text[] = "the jay, pig, fox, zebra and my wolves quack";
110     cairo_scaled_font_t *scaled_font;
111     cairo_glyph_t *glyphs = NULL;
112     cairo_text_extents_t extents;
113     cairo_font_options_t *options;
114     cairo_status_t status;
115     int num_glyphs;
116     int glyphs_per_line, lines_per_loop;
117
118     options = cairo_font_options_create ();
119     cairo_font_options_set_antialias (options, antialias);
120     cairo_set_font_options (cr, options);
121     cairo_font_options_destroy (options);
122
123     cairo_select_font_face (cr,
124                             "@cairo:",
125                             CAIRO_FONT_SLANT_NORMAL,
126                             CAIRO_FONT_WEIGHT_NORMAL);
127     cairo_set_font_size (cr, font_size);
128     scaled_font = cairo_get_scaled_font (cr);
129     status = cairo_scaled_font_text_to_glyphs (scaled_font, 0., 0.,
130                                                text, -1,
131                                                &glyphs, &num_glyphs,
132                                                NULL, NULL,
133                                                NULL);
134     if (status)
135         return 0;
136
137     cairo_scaled_font_glyph_extents (scaled_font,
138                                      glyphs, num_glyphs,
139                                      &extents);
140     cairo_glyph_free (glyphs);
141
142     glyphs_per_line = num_glyphs * width / extents.width + 1;
143     lines_per_loop = height / extents.height + 1;
144     return glyphs_per_line * lines_per_loop / 1000.; /* kiloglyphs */
145 }
146
147 #define DECL(name,size, aa) \
148 static cairo_time_t \
149 do_glyphs##name (cairo_t *cr, int width, int height, int loops) \
150 { \
151     return do_glyphs (size, aa, cr, width, height, loops); \
152 } \
153 \
154 static double \
155 count_glyphs##name (cairo_t *cr, int width, int height) \
156 { \
157     return count_glyphs (size, aa, cr, width, height); \
158 }
159
160 DECL(8, 8, CAIRO_ANTIALIAS_GRAY)
161 DECL(10, 10, CAIRO_ANTIALIAS_GRAY)
162 DECL(12, 12, CAIRO_ANTIALIAS_GRAY)
163 DECL(16, 16, CAIRO_ANTIALIAS_GRAY)
164 DECL(20, 20, CAIRO_ANTIALIAS_GRAY)
165 DECL(24, 24, CAIRO_ANTIALIAS_GRAY)
166 DECL(32, 32, CAIRO_ANTIALIAS_GRAY)
167 DECL(40, 40, CAIRO_ANTIALIAS_GRAY)
168 DECL(48, 48, CAIRO_ANTIALIAS_GRAY)
169
170 DECL(8ca, 8, CAIRO_ANTIALIAS_SUBPIXEL)
171 DECL(48ca, 48, CAIRO_ANTIALIAS_SUBPIXEL)
172
173 DECL(8mono, 8, CAIRO_ANTIALIAS_NONE)
174 DECL(48mono, 48, CAIRO_ANTIALIAS_NONE)
175
176 cairo_bool_t
177 glyphs_enabled (cairo_perf_t *perf)
178 {
179     return cairo_perf_can_run (perf, "glyphs", NULL);
180 }
181
182 void
183 glyphs (cairo_perf_t *perf, cairo_t *cr, int width, int height)
184 {
185     cairo_perf_cover_sources_and_operators (perf, "glyphs8mono", do_glyphs8mono, count_glyphs8mono);
186     cairo_perf_cover_sources_and_operators (perf, "glyphs8", do_glyphs8, count_glyphs8);
187     cairo_perf_cover_sources_and_operators (perf, "glyphs8ca", do_glyphs8ca, count_glyphs8ca);
188
189     cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
190     cairo_set_source_rgb (cr, 0, 0, 0);
191
192     cairo_perf_run (perf, "glyphs8", do_glyphs8, count_glyphs8);
193     cairo_perf_run (perf, "glyphs10", do_glyphs10, count_glyphs10);
194     cairo_perf_run (perf, "glyphs12", do_glyphs12, count_glyphs12);
195     cairo_perf_run (perf, "glyphs16", do_glyphs16, count_glyphs16);
196     cairo_perf_run (perf, "glyphs20", do_glyphs20, count_glyphs20);
197     cairo_perf_run (perf, "glyphs24", do_glyphs24, count_glyphs24);
198     cairo_perf_run (perf, "glyphs32", do_glyphs32, count_glyphs32);
199     cairo_perf_run (perf, "glyphs40", do_glyphs40, count_glyphs40);
200     cairo_perf_run (perf, "glyphs48", do_glyphs48, count_glyphs48);
201
202     cairo_perf_cover_sources_and_operators (perf, "glyphs48mono", do_glyphs48mono, count_glyphs48mono);
203     cairo_perf_cover_sources_and_operators (perf, "glyphs48", do_glyphs48, count_glyphs48);
204     cairo_perf_cover_sources_and_operators (perf, "glyphs48ca", do_glyphs48ca, count_glyphs48ca);
205 }