1
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2
/* cairo - a vector graphics library with display and print output
3
 *
4
 * Copyright (c) 2011 Intel Corporation
5
 *
6
 * Permission is hereby granted, free of charge, to any person
7
 * obtaining a copy of this software and associated documentation
8
 * files (the "Software"), to deal in the Software without
9
 * restriction, including without limitation the rights to use,
10
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the
12
 * Software is furnished to do so, subject to the following
13
 * conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be
16
 * included in all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
 * OTHER DEALINGS IN THE SOFTWARE.
26
 */
27
#include "cairo-perf.h"
28
#include <assert.h>
29

            
30

            
31
#define STEP	5
32
#define WIDTH	100
33
#define HEIGHT	100
34

            
35
static void path (cairo_t *cr, unsigned int width, unsigned int height)
36
{
37
    unsigned int i;
38

            
39
    for (i = 0; i < width+1; i += STEP) {
40
	cairo_rectangle (cr, i-1, -1, 2, height+2);
41
	cairo_rectangle (cr, -1, i-1, width+2, 2);
42
    }
43
}
44

            
45
static void aa (cairo_t *cr)
46
{
47
    cairo_set_antialias (cr, CAIRO_ANTIALIAS_DEFAULT);
48
}
49

            
50
static void mono (cairo_t *cr)
51
{
52
    cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
53
}
54

            
55
static void aligned (cairo_t *cr, int width, int height)
56
{
57
}
58

            
59
static void misaligned (cairo_t *cr, int width, int height)
60
{
61
    cairo_translate (cr, 0.25, 0.25);
62
}
63

            
64
static void rotated (cairo_t *cr, int width, int height)
65
{
66
    cairo_translate (cr, width/2, height/2);
67
    cairo_rotate (cr, M_PI/4);
68
    cairo_translate (cr, -width/2, -height/2);
69
}
70

            
71
static void clip (cairo_t *cr)
72
{
73
    cairo_clip (cr);
74
    cairo_paint (cr);
75
}
76

            
77
static void clip_alpha (cairo_t *cr)
78
{
79
    cairo_clip (cr);
80
    cairo_paint_with_alpha (cr, .5);
81
}
82

            
83
static cairo_time_t
84
draw (cairo_t *cr,
85
      void (*prepare) (cairo_t *cr),
86
      void (*transform) (cairo_t *cr, int width, int height),
87
      void (*op) (cairo_t *cr),
88
      int width, int height, int loops)
89
{
90
    cairo_save (cr);
91
    cairo_set_source_rgb (cr, 1, 1, 1);
92
    cairo_paint (cr);
93
    cairo_set_source_rgb (cr, 1, 0, 0);
94

            
95
    prepare (cr);
96

            
97
    cairo_perf_timer_start ();
98
    while (loops--) {
99
	cairo_save (cr);
100
	transform (cr, width, height);
101
	path (cr, width, height);
102
	op (cr);
103
	cairo_restore (cr);
104
    }
105
    cairo_perf_timer_stop ();
106

            
107
    cairo_restore (cr);
108

            
109
    return cairo_perf_timer_elapsed ();
110
}
111

            
112
static cairo_time_t
113
draw_aligned_aa (cairo_t *cr, int width, int height, int loops)
114
{
115
    return draw(cr, aa, aligned, cairo_fill,
116
		width, height, loops);
117
}
118

            
119
static cairo_time_t
120
draw_misaligned_aa (cairo_t *cr, int width, int height, int loops)
121
{
122
    return draw(cr, aa, misaligned, cairo_fill,
123
		width, height, loops);
124
}
125

            
126
static cairo_time_t
127
draw_rotated_aa (cairo_t *cr, int width, int height, int loops)
128
{
129
    return draw(cr, aa, rotated, cairo_fill,
130
		width, height, loops);
131
}
132

            
133
static cairo_time_t
134
draw_aligned_mono (cairo_t *cr, int width, int height, int loops)
135
{
136
    return draw(cr, mono, aligned, cairo_fill,
137
		width, height, loops);
138
}
139

            
140
static cairo_time_t
141
draw_misaligned_mono (cairo_t *cr, int width, int height, int loops)
142
{
143
    return draw(cr, mono, misaligned, cairo_fill,
144
		width, height, loops);
145
}
146

            
147
static cairo_time_t
148
draw_rotated_mono (cairo_t *cr, int width, int height, int loops)
149
{
150
    return draw(cr, mono, rotated, cairo_fill,
151
		width, height, loops);
152
}
153

            
154
#define F(name, op,transform,aa) \
155
static cairo_time_t \
156
draw_##name (cairo_t *cr, int width, int height, int loops) \
157
{ return draw(cr, (aa), (transform), (op), width, height, loops); }
158

            
159
F(clip_aligned, clip, aligned, aa)
160
F(clip_misaligned, clip, misaligned, aa)
161
F(clip_rotated, clip, rotated, aa)
162
F(clip_aligned_mono, clip, aligned, mono)
163
F(clip_misaligned_mono, clip, misaligned, mono)
164
F(clip_rotated_mono, clip, rotated, mono)
165

            
166
F(clip_alpha_aligned, clip_alpha, aligned, aa)
167
F(clip_alpha_misaligned, clip_alpha, misaligned, aa)
168
F(clip_alpha_rotated, clip_alpha, rotated, aa)
169
F(clip_alpha_aligned_mono, clip_alpha, aligned, mono)
170
F(clip_alpha_misaligned_mono, clip_alpha, misaligned, mono)
171
F(clip_alpha_rotated_mono, clip_alpha, rotated, mono)
172

            
173
cairo_bool_t
174
hatching_enabled (cairo_perf_t *perf)
175
{
176
    return cairo_perf_can_run (perf, "hatching", NULL);
177
}
178

            
179
void
180
hatching (cairo_perf_t *perf, cairo_t *cr, int width, int height)
181
{
182
    cairo_perf_run (perf, "hatching-aligned-aa", draw_aligned_aa, NULL);
183
    cairo_perf_run (perf, "hatching-misaligned-aa", draw_misaligned_aa, NULL);
184
    cairo_perf_run (perf, "hatching-rotated-aa", draw_rotated_aa, NULL);
185
    cairo_perf_run (perf, "hatching-aligned-mono", draw_aligned_mono, NULL);
186
    cairo_perf_run (perf, "hatching-misaligned-mono", draw_misaligned_mono, NULL);
187
    cairo_perf_run (perf, "hatching-rotated-mono", draw_rotated_mono, NULL);
188

            
189
    cairo_perf_run (perf, "hatching-clip-aligned-aa", draw_clip_aligned, NULL);
190
    cairo_perf_run (perf, "hatching-clip-misaligned-aa", draw_clip_misaligned, NULL);
191
    cairo_perf_run (perf, "hatching-clip-rotated-aa", draw_clip_rotated, NULL);
192
    cairo_perf_run (perf, "hatching-clip-aligned-mono", draw_clip_aligned_mono, NULL);
193
    cairo_perf_run (perf, "hatching-clip-misaligned-mono", draw_clip_misaligned_mono, NULL);
194
    cairo_perf_run (perf, "hatching-clip-rotated-mono", draw_clip_rotated_mono, NULL);
195

            
196
    cairo_perf_run (perf, "hatching-clip-alpha-aligned-aa", draw_clip_alpha_aligned, NULL);
197
    cairo_perf_run (perf, "hatching-clip-alpha-misaligned-aa", draw_clip_alpha_misaligned, NULL);
198
    cairo_perf_run (perf, "hatching-clip-alpha-rotated-aa", draw_clip_alpha_rotated, NULL);
199
    cairo_perf_run (perf, "hatching-clip-alpha-aligned-mono", draw_clip_alpha_aligned_mono, NULL);
200
    cairo_perf_run (perf, "hatching-clip-alpha-misaligned-mono", draw_clip_alpha_misaligned_mono, NULL);
201
    cairo_perf_run (perf, "hatching-clip-alpha-rotated-mono", draw_clip_alpha_rotated_mono, NULL);
202
}