2009-12-07 Rolf Bjarne Kvinge <RKvinge@novell.com>
[moon.git] / cairo / perf / mosaic.c
blob7172a9d3d038938163f94c9c6fa8ac598acd4409
1 /*
2 * Copyright © 2006 Joonas Pihlaja
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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.
24 * Author: Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
26 #include "cairo-perf.h"
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 */
34 struct mosaic_region {
35 unsigned rgb; /* colour of this region in 0xRRGGBB format */
36 unsigned ncurves; /* number of boundary curves. */
39 struct mosaic_region_iter {
40 int do_curves;
41 struct mosaic_region const *region;
42 double const *points;
45 #include "mosaic.h"
47 static void
48 mosaic_region_iter_init (struct mosaic_region_iter *iter, int do_curves)
50 iter->region = mosaic_regions;
51 iter->points = mosaic_curve_points;
52 iter->do_curves = do_curves;
55 /* Create the next closed region as a path. */
56 static int
57 mosaic_next_path (cairo_t *cr, struct mosaic_region_iter *iter)
59 double const *points = iter->points;
60 unsigned i;
61 unsigned ncurves = iter->region->ncurves;
62 if (0 == ncurves) {
63 return 0;
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]);
76 else {
77 cairo_line_to (cr,
78 points[4], points[5]);
81 cairo_close_path (cr);
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);
90 iter->points = iter->points + 2*(1 + 3*iter->region->ncurves);
91 iter->region++;
92 return 1;
95 static cairo_perf_ticks_t
96 mosaic_perform(cairo_t *cr, unsigned flags, int width, int height)
98 struct mosaic_region_iter iter;
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;
106 cairo_identity_matrix (cr);
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);
114 cairo_scale (cr, width / (maxx - minx) , height / (maxy - miny));
115 cairo_translate (cr, -minx, -miny);
117 /* Iterate over all closed regions in the mosaic filling or
118 * tessellating them as dictated by the flags. */
119 mosaic_region_iter_init (&iter, flags & MOSAIC_CURVE_TO);
121 cairo_perf_timer_start ();
122 while (mosaic_next_path (cr, &iter)) {
123 if (flags & MOSAIC_FILL) {
124 cairo_fill (cr);
126 else {
127 double x, y;
128 cairo_get_current_point (cr, &x, &y);
129 cairo_in_fill (cr, x, y);
132 cairo_perf_timer_stop ();
134 return cairo_perf_timer_elapsed ();
137 static cairo_perf_ticks_t
138 mosaic_fill_curves (cairo_t *cr, int width, int height)
140 return mosaic_perform (cr, MOSAIC_FILL | MOSAIC_CURVE_TO, width, height);
143 static cairo_perf_ticks_t
144 mosaic_fill_lines (cairo_t *cr, int width, int height)
146 return mosaic_perform (cr, MOSAIC_FILL | MOSAIC_LINE_TO, width, height);
149 static cairo_perf_ticks_t
150 mosaic_tessellate_lines (cairo_t *cr, int width, int height)
152 return mosaic_perform (cr, MOSAIC_TESSELLATE | MOSAIC_LINE_TO, width, height);
155 static cairo_perf_ticks_t
156 mosaic_tessellate_curves (cairo_t *cr, int width, int height)
158 return mosaic_perform (cr, MOSAIC_TESSELLATE | MOSAIC_CURVE_TO, width, height);
161 void
162 mosaic (cairo_perf_t *perf, cairo_t *cr, int width, int height)
164 cairo_perf_run (perf, "mosaic_fill_curves", mosaic_fill_curves);
165 cairo_perf_run (perf, "mosaic_fill_lines", mosaic_fill_lines);
166 cairo_perf_run (perf, "mosaic_tessellate_curves", mosaic_tessellate_curves);
167 cairo_perf_run (perf, "mosaic_tessellate_lines", mosaic_tessellate_lines);