summaryrefslogtreecommitdiff
path: root/perf/micro/mosaic.c
blob: d76b8400210c226cac66ab46a0b25121bf977d5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright © 2006 Joonas Pihlaja
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Author: Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
 */
#include "cairo-perf.h"

/* Options passed in flags to mosaic_perform(): */
#define MOSAIC_FILL 1		/* do rasterise */
#define MOSAIC_TESSELLATE 0	/* just tessellate */
#define MOSAIC_CURVE_TO 2	/* use curve bounded regions */
#define MOSAIC_LINE_TO 0	/* use line bounded regions */

struct mosaic_region {
    unsigned rgb;		/* colour of this region in 0xRRGGBB format */
    unsigned ncurves;		/* number of boundary curves. */
};

struct mosaic_region_iter {
    int do_curves;
    struct mosaic_region const *region;
    double const *points;
};

#include "mosaic.h"

static void
mosaic_region_iter_init (struct mosaic_region_iter *iter, int do_curves)
{
    iter->region = mosaic_regions;
    iter->points = mosaic_curve_points;
    iter->do_curves = do_curves;
}

/* Create the next closed region as a path. */
static int
mosaic_next_path (cairo_t *cr, struct mosaic_region_iter *iter)
{
    double const *points = iter->points;
    unsigned i;
    unsigned ncurves = iter->region->ncurves;
    if (0 == ncurves) {
	return 0;
    }

    cairo_new_path (cr);
    cairo_move_to (cr, points[0], points[1]);
    points += 2;
    for (i=0; i < ncurves; i++, points += 6) {
	if (iter->do_curves) {
	    cairo_curve_to (cr,
			    points[0], points[1],
			    points[2], points[3],
			    points[4], points[5]);
	}
	else {
	    cairo_line_to (cr,
			    points[4], points[5]);
	}
    }
    cairo_close_path (cr);
    {
	unsigned rgb = iter->region->rgb;
	double r = ((rgb >> 16) & 255) / 255.0;
	double g = ((rgb >>  8) & 255) / 255.0;
	double b = ((rgb >>  0) & 255) / 255.0;
	cairo_set_source_rgb (cr, r, g, b);
    }

    iter->points = iter->points + 2*(1 + 3*iter->region->ncurves);
    iter->region++;
    return 1;
}

static cairo_time_t
mosaic_perform(cairo_t *cr, unsigned flags, int width, int height, int loops)
{
    struct mosaic_region_iter iter;

    /* Scale to fit the window.*/
    double minx = -40.7;
    double maxx = 955.1;
    double miny = -88.4;
    double maxy = 884.5;

    cairo_identity_matrix (cr);

    if (flags & MOSAIC_FILL) {
	cairo_set_source_rgb (cr, 1, 1, 1);
	cairo_rectangle (cr, 0, 0, width, height);
	cairo_fill (cr);
    }

    cairo_scale (cr, width / (maxx - minx) , height / (maxy - miny));
    cairo_translate (cr, -minx, -miny);

    /* Iterate over all closed regions in the mosaic filling or
     * tessellating them as dictated by the flags.  */

    cairo_perf_timer_start ();
    while (loops--) {
	mosaic_region_iter_init (&iter, flags & MOSAIC_CURVE_TO);
	while (mosaic_next_path (cr, &iter)) {
	    if (flags & MOSAIC_FILL) {
		cairo_fill (cr);
	    }
	    else {
		double x, y;
		cairo_get_current_point (cr, &x, &y);
		cairo_in_fill (cr, x, y);
	    }
	}
    }
    cairo_perf_timer_stop ();

    return cairo_perf_timer_elapsed ();
}

static cairo_time_t
mosaic_fill_curves (cairo_t *cr, int width, int height, int loops)
{
    return mosaic_perform (cr, MOSAIC_FILL | MOSAIC_CURVE_TO, width, height, loops);
}

static cairo_time_t
mosaic_fill_lines (cairo_t *cr, int width, int height, int loops)
{
    return mosaic_perform (cr, MOSAIC_FILL | MOSAIC_LINE_TO, width, height, loops);
}

static cairo_time_t
mosaic_tessellate_lines (cairo_t *cr, int width, int height, int loops)
{
    return mosaic_perform (cr, MOSAIC_TESSELLATE | MOSAIC_LINE_TO, width, height, loops);
}

static cairo_time_t
mosaic_tessellate_curves (cairo_t *cr, int width, int height, int loops)
{
    return mosaic_perform (cr, MOSAIC_TESSELLATE | MOSAIC_CURVE_TO, width, height, loops);
}

void
mosaic (cairo_perf_t *perf, cairo_t *cr, int width, int height)
{
    if (! cairo_perf_can_run (perf, "mosaic", NULL))
	return;

    cairo_perf_run (perf, "mosaic-fill-curves", mosaic_fill_curves, NULL);
    cairo_perf_run (perf, "mosaic-fill-lines", mosaic_fill_lines, NULL);
    cairo_perf_run (perf, "mosaic-tessellate-curves", mosaic_tessellate_curves, NULL);
    cairo_perf_run (perf, "mosaic-tessellate-lines", mosaic_tessellate_lines, NULL);
}