Upload Tizen2.0 source
[framework/graphics/cairo.git] / perf / micro / pixel.c
1 /*
2  * Copyright © 2011 Intel Corporation
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: Chris Wilson <chris@chris-wilson.co.uk>
25  */
26
27 /* Measure the overhead in setting a single pixel */
28
29 #include "cairo-perf.h"
30
31 #include <pixman.h>
32
33 static cairo_time_t
34 pixel_direct (cairo_t *cr, int width, int height, int loops)
35 {
36     cairo_surface_t *surface, *image;
37     uint32_t *data;
38     int stride, bpp;
39
40     surface = cairo_get_target (cr);
41     image = cairo_surface_map_to_image (surface, NULL);
42     data = (uint32_t *) cairo_image_surface_get_data (image);
43     stride = cairo_image_surface_get_stride (image) / sizeof (uint32_t);
44
45     switch (cairo_image_surface_get_format (image)) {
46     default:
47     case CAIRO_FORMAT_INVALID:
48     case CAIRO_FORMAT_A1: bpp = 0; break;
49     case CAIRO_FORMAT_A8: bpp = 8; break;
50     case CAIRO_FORMAT_RGB16_565: bpp = 16; break;
51     case CAIRO_FORMAT_RGB24:
52     case CAIRO_FORMAT_RGB30:
53     case CAIRO_FORMAT_ARGB32: bpp = 32; break;
54     }
55
56     cairo_perf_timer_start ();
57
58     while (loops--)
59         pixman_fill (data, stride, bpp, 0, 0, 1, 1, -1);
60
61     cairo_perf_timer_stop ();
62
63     cairo_surface_unmap_image (surface, image);
64
65     return cairo_perf_timer_elapsed ();
66 }
67
68 static cairo_time_t
69 pixel_paint (cairo_t *cr, int width, int height, int loops)
70 {
71     cairo_perf_timer_start ();
72
73     while (loops--)
74         cairo_paint (cr);
75
76     cairo_perf_timer_stop ();
77
78     return cairo_perf_timer_elapsed ();
79 }
80
81 static cairo_time_t
82 pixel_mask (cairo_t *cr, int width, int height, int loops)
83 {
84     cairo_surface_t *mask;
85     cairo_t *cr2;
86
87     mask = cairo_surface_create_similar (cairo_get_target (cr),
88                                          CAIRO_CONTENT_ALPHA,
89                                          1, 1);
90     cr2 = cairo_create (mask);
91     cairo_set_source_rgb (cr2, 1,1,1);
92     cairo_paint (cr2);
93     cairo_destroy (cr2);
94
95     cairo_perf_timer_start ();
96
97     while (loops--)
98         cairo_mask_surface (cr, mask, 0, 0);
99
100     cairo_perf_timer_stop ();
101
102     cairo_surface_destroy (mask);
103
104     return cairo_perf_timer_elapsed ();
105 }
106
107 static cairo_time_t
108 pixel_rectangle (cairo_t *cr, int width, int height, int loops)
109 {
110     cairo_new_path (cr);
111     cairo_rectangle (cr, 0, 0, 1, 1);
112
113     cairo_perf_timer_start ();
114
115     while (loops--)
116         cairo_fill_preserve (cr);
117
118     cairo_perf_timer_stop ();
119
120     cairo_new_path (cr);
121     return cairo_perf_timer_elapsed ();
122 }
123
124 static cairo_time_t
125 pixel_subrectangle (cairo_t *cr, int width, int height, int loops)
126 {
127     cairo_new_path (cr);
128     cairo_rectangle (cr, 0.1, 0.1, .8, .8);
129
130     cairo_perf_timer_start ();
131
132     while (loops--)
133         cairo_fill_preserve (cr);
134
135     cairo_perf_timer_stop ();
136
137     cairo_new_path (cr);
138     return cairo_perf_timer_elapsed ();
139 }
140
141 static cairo_time_t
142 pixel_triangle (cairo_t *cr, int width, int height, int loops)
143 {
144     cairo_new_path (cr);
145     cairo_move_to (cr, 0, 0);
146     cairo_line_to (cr, 1, 1);
147     cairo_line_to (cr, 0, 1);
148
149     cairo_perf_timer_start ();
150
151     while (loops--)
152         cairo_fill_preserve (cr);
153
154     cairo_perf_timer_stop ();
155
156     cairo_new_path (cr);
157     return cairo_perf_timer_elapsed ();
158 }
159
160 static cairo_time_t
161 pixel_circle (cairo_t *cr, int width, int height, int loops)
162 {
163     cairo_new_path (cr);
164     cairo_arc (cr, 0.5, 0.5, 0.5, 0, 2 * M_PI);
165
166     cairo_perf_timer_start ();
167
168     while (loops--)
169         cairo_fill_preserve (cr);
170
171     cairo_perf_timer_stop ();
172
173     cairo_new_path (cr);
174     return cairo_perf_timer_elapsed ();
175 }
176
177 static cairo_time_t
178 pixel_stroke (cairo_t *cr, int width, int height, int loops)
179 {
180     cairo_set_line_width (cr, 1.);
181     cairo_new_path (cr);
182     cairo_move_to (cr, 0, 0.5);
183     cairo_line_to (cr, 1, 0.5);
184
185     cairo_perf_timer_start ();
186
187     while (loops--)
188         cairo_stroke_preserve (cr);
189
190     cairo_perf_timer_stop ();
191
192     cairo_new_path (cr);
193     return cairo_perf_timer_elapsed ();
194 }
195
196 cairo_bool_t
197 pixel_enabled (cairo_perf_t *perf)
198 {
199     return cairo_perf_can_run (perf, "pixel", NULL);
200 }
201
202 void
203 pixel (cairo_perf_t *perf, cairo_t *cr, int width, int height)
204 {
205     cairo_set_source_rgb (cr, 1., 1., 1.);
206
207     cairo_perf_run (perf, "pixel-direct", pixel_direct, NULL);
208     cairo_perf_run (perf, "pixel-paint", pixel_paint, NULL);
209     cairo_perf_run (perf, "pixel-mask", pixel_mask, NULL);
210     cairo_perf_run (perf, "pixel-rectangle", pixel_rectangle, NULL);
211     cairo_perf_run (perf, "pixel-subrectangle", pixel_subrectangle, NULL);
212     cairo_perf_run (perf, "pixel-triangle", pixel_triangle, NULL);
213     cairo_perf_run (perf, "pixel-circle", pixel_circle, NULL);
214     cairo_perf_run (perf, "pixel-stroke", pixel_stroke, NULL);
215 }
216
217 cairo_bool_t
218 a1_pixel_enabled (cairo_perf_t *perf)
219 {
220     return cairo_perf_can_run (perf, "a1-pixel", NULL);
221 }
222
223 void
224 a1_pixel (cairo_perf_t *perf, cairo_t *cr, int width, int height)
225 {
226     cairo_set_source_rgb (cr, 1., 1., 1.);
227     cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
228
229     cairo_perf_run (perf, "a1-pixel-direct", pixel_direct, NULL);
230     cairo_perf_run (perf, "a1-pixel-paint", pixel_paint, NULL);
231     cairo_perf_run (perf, "a1-pixel-mask", pixel_mask, NULL);
232     cairo_perf_run (perf, "a1-pixel-rectangle", pixel_rectangle, NULL);
233     cairo_perf_run (perf, "a1-pixel-subrectangle", pixel_subrectangle, NULL);
234     cairo_perf_run (perf, "a1-pixel-triangle", pixel_triangle, NULL);
235     cairo_perf_run (perf, "a1-pixel-circle", pixel_circle, NULL);
236     cairo_perf_run (perf, "a1-pixel-stroke", pixel_stroke, NULL);
237 }