Git init
[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
75     while (loops--) {
76         y = 0;
77         do {
78             x = 0;
79             do {
80                 for (n = 0; n < num_glyphs; n++) {
81                     glyphs_copy[n] = glyphs[n];
82                     glyphs_copy[n].x += x;
83                     glyphs_copy[n].y += y;
84                 }
85                 cairo_show_glyphs (cr, glyphs_copy, num_glyphs);
86
87                 x += extents.width;
88             } while (x < width);
89             y += extents.height;
90         } while (y < height);
91     }
92
93     cairo_perf_timer_stop ();
94
95     cairo_glyph_free (glyphs);
96     cairo_glyph_free (glyphs_copy);
97
98     return cairo_perf_timer_elapsed ();
99 }
100
101 static double
102 count_glyphs (double font_size,
103               cairo_antialias_t antialias,
104               cairo_t *cr, int width, int height)
105 {
106     const char text[] = "the jay, pig, fox, zebra and my wolves quack";
107     cairo_scaled_font_t *scaled_font;
108     cairo_glyph_t *glyphs = NULL;
109     cairo_text_extents_t extents;
110     cairo_font_options_t *options;
111     cairo_status_t status;
112     int num_glyphs;
113     int glyphs_per_line, lines_per_loop;
114
115     options = cairo_font_options_create ();
116     cairo_font_options_set_antialias (options, antialias);
117     cairo_set_font_options (cr, options);
118     cairo_font_options_destroy (options);
119
120     cairo_select_font_face (cr,
121                             "@cairo:",
122                             CAIRO_FONT_SLANT_NORMAL,
123                             CAIRO_FONT_WEIGHT_NORMAL);
124     cairo_set_font_size (cr, font_size);
125     scaled_font = cairo_get_scaled_font (cr);
126     status = cairo_scaled_font_text_to_glyphs (scaled_font, 0., 0.,
127                                                text, -1,
128                                                &glyphs, &num_glyphs,
129                                                NULL, NULL,
130                                                NULL);
131     if (status)
132         return 0;
133
134     cairo_scaled_font_glyph_extents (scaled_font,
135                                      glyphs, num_glyphs,
136                                      &extents);
137     cairo_glyph_free (glyphs);
138
139     glyphs_per_line = num_glyphs * width / extents.width + 1;
140     lines_per_loop = height / extents.height + 1;
141     return glyphs_per_line * lines_per_loop / 1000.; /* kiloglyphs */
142 }
143
144 #define DECL(name,size, aa) \
145 static cairo_time_t \
146 do_glyphs##name (cairo_t *cr, int width, int height, int loops) \
147 { \
148     return do_glyphs (size, aa, cr, width, height, loops); \
149 } \
150 \
151 static double \
152 count_glyphs##name (cairo_t *cr, int width, int height) \
153 { \
154     return count_glyphs (size, aa, cr, width, height); \
155 }
156
157 DECL(8, 8, CAIRO_ANTIALIAS_GRAY)
158 DECL(10, 10, CAIRO_ANTIALIAS_GRAY)
159 DECL(12, 12, CAIRO_ANTIALIAS_GRAY)
160 DECL(16, 16, CAIRO_ANTIALIAS_GRAY)
161 DECL(20, 20, CAIRO_ANTIALIAS_GRAY)
162 DECL(24, 24, CAIRO_ANTIALIAS_GRAY)
163 DECL(32, 32, CAIRO_ANTIALIAS_GRAY)
164 DECL(40, 40, CAIRO_ANTIALIAS_GRAY)
165 DECL(48, 48, CAIRO_ANTIALIAS_GRAY)
166
167 DECL(8ca, 8, CAIRO_ANTIALIAS_SUBPIXEL)
168 DECL(48ca, 48, CAIRO_ANTIALIAS_SUBPIXEL)
169
170 DECL(8mono, 8, CAIRO_ANTIALIAS_NONE)
171 DECL(48mono, 48, CAIRO_ANTIALIAS_NONE)
172
173 cairo_bool_t
174 glyphs_enabled (cairo_perf_t *perf)
175 {
176     return cairo_perf_can_run (perf, "glyphs", NULL);
177 }
178
179 void
180 glyphs (cairo_perf_t *perf, cairo_t *cr, int width, int height)
181 {
182     cairo_perf_cover_sources_and_operators (perf, "glyphs8mono", do_glyphs8mono, count_glyphs8mono);
183     cairo_perf_cover_sources_and_operators (perf, "glyphs8", do_glyphs8, count_glyphs8);
184     cairo_perf_cover_sources_and_operators (perf, "glyphs8ca", do_glyphs8ca, count_glyphs8ca);
185
186     cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
187     cairo_set_source_rgb (cr, 0, 0, 0);
188
189     cairo_perf_run (perf, "glyphs8", do_glyphs8, count_glyphs8);
190     cairo_perf_run (perf, "glyphs10", do_glyphs10, count_glyphs10);
191     cairo_perf_run (perf, "glyphs12", do_glyphs12, count_glyphs12);
192     cairo_perf_run (perf, "glyphs16", do_glyphs16, count_glyphs16);
193     cairo_perf_run (perf, "glyphs20", do_glyphs20, count_glyphs20);
194     cairo_perf_run (perf, "glyphs24", do_glyphs24, count_glyphs24);
195     cairo_perf_run (perf, "glyphs32", do_glyphs32, count_glyphs32);
196     cairo_perf_run (perf, "glyphs40", do_glyphs40, count_glyphs40);
197     cairo_perf_run (perf, "glyphs48", do_glyphs48, count_glyphs48);
198
199     cairo_perf_cover_sources_and_operators (perf, "glyphs48mono", do_glyphs48mono, count_glyphs48mono);
200     cairo_perf_cover_sources_and_operators (perf, "glyphs48", do_glyphs48, count_glyphs48);
201     cairo_perf_cover_sources_and_operators (perf, "glyphs48ca", do_glyphs48ca, count_glyphs48ca);
202 }