Tizen 2.0 Release
[framework/graphics/cairo.git] / perf / micro / mosaic.c
1 /*
2  * Copyright © 2006 Joonas Pihlaja
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: Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
25  */
26 #include "cairo-perf.h"
27
28 /* Options passed in flags to mosaic_perform(): */
29 #define MOSAIC_FILL 1           /* do rasterise */
30 #define MOSAIC_TESSELLATE 0     /* just tessellate */
31 #define MOSAIC_CURVE_TO 2       /* use curve bounded regions */
32 #define MOSAIC_LINE_TO 0        /* use line bounded regions */
33
34 struct mosaic_region {
35     unsigned rgb;               /* colour of this region in 0xRRGGBB format */
36     unsigned ncurves;           /* number of boundary curves. */
37 };
38
39 struct mosaic_region_iter {
40     int do_curves;
41     struct mosaic_region const *region;
42     double const *points;
43 };
44
45 #include "mosaic.h"
46
47 static void
48 mosaic_region_iter_init (struct mosaic_region_iter *iter, int do_curves)
49 {
50     iter->region = mosaic_regions;
51     iter->points = mosaic_curve_points;
52     iter->do_curves = do_curves;
53 }
54
55 /* Create the next closed region as a path. */
56 static int
57 mosaic_next_path (cairo_t *cr, struct mosaic_region_iter *iter)
58 {
59     double const *points = iter->points;
60     unsigned i;
61     unsigned ncurves = iter->region->ncurves;
62     if (0 == ncurves) {
63         return 0;
64     }
65
66     cairo_new_path (cr);
67     cairo_move_to (cr, points[0], points[1]);
68     points += 2;
69     for (i=0; i < ncurves; i++, points += 6) {
70         if (iter->do_curves) {
71             cairo_curve_to (cr,
72                             points[0], points[1],
73                             points[2], points[3],
74                             points[4], points[5]);
75         }
76         else {
77             cairo_line_to (cr,
78                             points[4], points[5]);
79         }
80     }
81     cairo_close_path (cr);
82     {
83         unsigned rgb = iter->region->rgb;
84         double r = ((rgb >> 16) & 255) / 255.0;
85         double g = ((rgb >>  8) & 255) / 255.0;
86         double b = ((rgb >>  0) & 255) / 255.0;
87         cairo_set_source_rgb (cr, r, g, b);
88     }
89
90     iter->points = iter->points + 2*(1 + 3*iter->region->ncurves);
91     iter->region++;
92     return 1;
93 }
94
95 static cairo_time_t
96 mosaic_perform(cairo_t *cr, unsigned flags, int width, int height, int loops)
97 {
98     struct mosaic_region_iter iter;
99
100     /* Scale to fit the window.*/
101     double minx = -40.7;
102     double maxx = 955.1;
103     double miny = -88.4;
104     double maxy = 884.5;
105
106     cairo_identity_matrix (cr);
107
108     if (flags & MOSAIC_FILL) {
109         cairo_set_source_rgb (cr, 1, 1, 1);
110         cairo_rectangle (cr, 0, 0, width, height);
111         cairo_fill (cr);
112     }
113
114     cairo_scale (cr, width / (maxx - minx) , height / (maxy - miny));
115     cairo_translate (cr, -minx, -miny);
116
117     /* Iterate over all closed regions in the mosaic filling or
118      * tessellating them as dictated by the flags.  */
119
120     cairo_perf_timer_start ();
121     cairo_perf_set_thread_aware (cr, FALSE);
122     while (loops--) {
123         if (loops == 0)
124                 cairo_perf_set_thread_aware (cr, TRUE);
125         mosaic_region_iter_init (&iter, flags & MOSAIC_CURVE_TO);
126         while (mosaic_next_path (cr, &iter)) {
127             if (flags & MOSAIC_FILL) {
128                 cairo_fill (cr);
129             }
130             else {
131                 double x, y;
132                 cairo_get_current_point (cr, &x, &y);
133                 cairo_in_fill (cr, x, y);
134             }
135         }
136     }
137
138     cairo_perf_timer_stop ();
139
140     return cairo_perf_timer_elapsed ();
141 }
142
143 static cairo_time_t
144 mosaic_fill_curves (cairo_t *cr, int width, int height, int loops)
145 {
146     return mosaic_perform (cr, MOSAIC_FILL | MOSAIC_CURVE_TO, width, height, loops);
147 }
148
149 static cairo_time_t
150 mosaic_fill_lines (cairo_t *cr, int width, int height, int loops)
151 {
152     return mosaic_perform (cr, MOSAIC_FILL | MOSAIC_LINE_TO, width, height, loops);
153 }
154
155 static cairo_time_t
156 mosaic_tessellate_lines (cairo_t *cr, int width, int height, int loops)
157 {
158     return mosaic_perform (cr, MOSAIC_TESSELLATE | MOSAIC_LINE_TO, width, height, loops);
159 }
160
161 static cairo_time_t
162 mosaic_tessellate_curves (cairo_t *cr, int width, int height, int loops)
163 {
164     return mosaic_perform (cr, MOSAIC_TESSELLATE | MOSAIC_CURVE_TO, width, height, loops);
165 }
166
167 cairo_bool_t
168 mosaic_enabled (cairo_perf_t *perf)
169 {
170     return cairo_perf_can_run (perf, "mosaic", NULL);
171 }
172
173 void
174 mosaic (cairo_perf_t *perf, cairo_t *cr, int width, int height)
175 {
176     cairo_perf_run (perf, "mosaic-fill-curves", mosaic_fill_curves, NULL);
177     cairo_perf_run (perf, "mosaic-fill-lines", mosaic_fill_lines, NULL);
178     cairo_perf_run (perf, "mosaic-tessellate-curves", mosaic_tessellate_curves, NULL);
179     cairo_perf_run (perf, "mosaic-tessellate-lines", mosaic_tessellate_lines, NULL);
180 }