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
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 {
41 struct mosaic_region const *region;
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. */
57 mosaic_next_path (cairo_t *cr, struct mosaic_region_iter *iter)
59 double const *points = iter->points;
61 unsigned ncurves = iter->region->ncurves;
67 cairo_move_to (cr, points[0], points[1]);
69 for (i=0; i < ncurves; i++, points += 6) {
70 if (iter->do_curves) {
74 points[4], points[5]);
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);
96 mosaic_perform(cairo_t *cr, unsigned flags, int width, int height, int loops)
98 struct mosaic_region_iter iter;
100 /* Scale to fit the window.*/
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);
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. */
120 cairo_perf_timer_start ();
121 cairo_perf_set_thread_aware (cr, FALSE);
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) {
132 cairo_get_current_point (cr, &x, &y);
133 cairo_in_fill (cr, x, y);
138 cairo_perf_timer_stop ();
140 return cairo_perf_timer_elapsed ();
144 mosaic_fill_curves (cairo_t *cr, int width, int height, int loops)
146 return mosaic_perform (cr, MOSAIC_FILL | MOSAIC_CURVE_TO, width, height, loops);
150 mosaic_fill_lines (cairo_t *cr, int width, int height, int loops)
152 return mosaic_perform (cr, MOSAIC_FILL | MOSAIC_LINE_TO, width, height, loops);
156 mosaic_tessellate_lines (cairo_t *cr, int width, int height, int loops)
158 return mosaic_perform (cr, MOSAIC_TESSELLATE | MOSAIC_LINE_TO, width, height, loops);
162 mosaic_tessellate_curves (cairo_t *cr, int width, int height, int loops)
164 return mosaic_perform (cr, MOSAIC_TESSELLATE | MOSAIC_CURVE_TO, width, height, loops);
168 mosaic_enabled (cairo_perf_t *perf)
170 return cairo_perf_can_run (perf, "mosaic", NULL);
174 mosaic (cairo_perf_t *perf, cairo_t *cr, int width, int height)
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);