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
mosaic_region_iter_init(struct mosaic_region_iter * iter,int do_curves)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
mosaic_next_path(cairo_t * cr,struct mosaic_region_iter * iter)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
mosaic_perform(cairo_t * cr,unsigned flags,int width,int height,int loops)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 while (loops--) {
122 mosaic_region_iter_init (&iter, flags & MOSAIC_CURVE_TO);
123 while (mosaic_next_path (cr, &iter)) {
124 if (flags & MOSAIC_FILL) {
125 cairo_fill (cr);
126 }
127 else {
128 double x, y;
129 cairo_get_current_point (cr, &x, &y);
130 cairo_in_fill (cr, x, y);
131 }
132 }
133 }
134 cairo_perf_timer_stop ();
135
136 return cairo_perf_timer_elapsed ();
137 }
138
139 static cairo_time_t
mosaic_fill_curves(cairo_t * cr,int width,int height,int loops)140 mosaic_fill_curves (cairo_t *cr, int width, int height, int loops)
141 {
142 return mosaic_perform (cr, MOSAIC_FILL | MOSAIC_CURVE_TO, width, height, loops);
143 }
144
145 static cairo_time_t
mosaic_fill_lines(cairo_t * cr,int width,int height,int loops)146 mosaic_fill_lines (cairo_t *cr, int width, int height, int loops)
147 {
148 return mosaic_perform (cr, MOSAIC_FILL | MOSAIC_LINE_TO, width, height, loops);
149 }
150
151 static cairo_time_t
mosaic_tessellate_lines(cairo_t * cr,int width,int height,int loops)152 mosaic_tessellate_lines (cairo_t *cr, int width, int height, int loops)
153 {
154 return mosaic_perform (cr, MOSAIC_TESSELLATE | MOSAIC_LINE_TO, width, height, loops);
155 }
156
157 static cairo_time_t
mosaic_tessellate_curves(cairo_t * cr,int width,int height,int loops)158 mosaic_tessellate_curves (cairo_t *cr, int width, int height, int loops)
159 {
160 return mosaic_perform (cr, MOSAIC_TESSELLATE | MOSAIC_CURVE_TO, width, height, loops);
161 }
162
163 cairo_bool_t
mosaic_enabled(cairo_perf_t * perf)164 mosaic_enabled (cairo_perf_t *perf)
165 {
166 return cairo_perf_can_run (perf, "mosaic", NULL);
167 }
168
169 void
mosaic(cairo_perf_t * perf,cairo_t * cr,int width,int height)170 mosaic (cairo_perf_t *perf, cairo_t *cr, int width, int height)
171 {
172 cairo_perf_run (perf, "mosaic-fill-curves", mosaic_fill_curves, NULL);
173 cairo_perf_run (perf, "mosaic-fill-lines", mosaic_fill_lines, NULL);
174 cairo_perf_run (perf, "mosaic-tessellate-curves", mosaic_tessellate_curves, NULL);
175 cairo_perf_run (perf, "mosaic-tessellate-lines", mosaic_tessellate_lines, NULL);
176 }
177