1 /*
2  * Copyright © 2006 Red Hat, Inc.
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: Carl D. Worth <cworth@cworth.org>
25  */
26 
27 #include "cairo-perf.h"
28 
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 
33 typedef enum {
34     WM_NEW_PATH,
35     WM_MOVE_TO,
36     WM_LINE_TO,
37     WM_HLINE_TO,
38     WM_VLINE_TO,
39     WM_REL_LINE_TO,
40     WM_END
41 } wm_type_t;
42 
43 typedef struct _wm_element {
44     wm_type_t type;
45     double x;
46     double y;
47 } wm_element_t;
48 
49 #include "world-map.h"
50 
51 enum {
52     STROKE = 1,
53     FILL = 2,
54 };
55 
56 static cairo_time_t
do_world_map(cairo_t * cr,int width,int height,int loops,int mode)57 do_world_map (cairo_t *cr, int width, int height, int loops, int mode)
58 {
59     const wm_element_t *e;
60     double cx, cy;
61 
62     cairo_set_line_width (cr, 0.2);
63 
64     cairo_perf_timer_start ();
65 
66     while (loops--) {
67 	cairo_set_source_rgb (cr, .68, .85, .90); /* lightblue */
68 	cairo_rectangle (cr, 0, 0, 800, 400);
69 	cairo_fill (cr);
70 
71 	e = &countries[0];
72 	while (1) {
73 	    switch (e->type) {
74 	    case WM_NEW_PATH:
75 	    case WM_END:
76 		if (mode & FILL) {
77 		    cairo_set_source_rgb (cr, .75, .75, .75); /* silver */
78 		    cairo_fill_preserve (cr);
79 		}
80 		if (mode & STROKE) {
81 		    cairo_set_source_rgb (cr, .50, .50, .50); /* gray */
82 		    cairo_stroke (cr);
83 		}
84 		cairo_new_path (cr);
85 		cairo_move_to (cr, e->x, e->y);
86 		break;
87 	    case WM_MOVE_TO:
88 		cairo_close_path (cr);
89 		cairo_move_to (cr, e->x, e->y);
90 		break;
91 	    case WM_LINE_TO:
92 		cairo_line_to (cr, e->x, e->y);
93 		break;
94 	    case WM_HLINE_TO:
95 		cairo_get_current_point (cr, &cx, &cy);
96 		cairo_line_to (cr, e->x, cy);
97 		break;
98 	    case WM_VLINE_TO:
99 		cairo_get_current_point (cr, &cx, &cy);
100 		cairo_line_to (cr, cx, e->y);
101 		break;
102 	    case WM_REL_LINE_TO:
103 		cairo_rel_line_to (cr, e->x, e->y);
104 		break;
105 	    }
106 	    if (e->type == WM_END)
107 		break;
108 	    e++;
109 	}
110 
111 	cairo_new_path (cr);
112     }
113 
114     cairo_perf_timer_stop ();
115 
116     return cairo_perf_timer_elapsed ();
117 }
118 
119 static cairo_time_t
do_world_map_stroke(cairo_t * cr,int width,int height,int loops)120 do_world_map_stroke (cairo_t *cr, int width, int height, int loops)
121 {
122     return do_world_map (cr, width, height, loops, STROKE);
123 }
124 
125 static cairo_time_t
do_world_map_fill(cairo_t * cr,int width,int height,int loops)126 do_world_map_fill (cairo_t *cr, int width, int height, int loops)
127 {
128     return do_world_map (cr, width, height, loops, FILL);
129 }
130 
131 static cairo_time_t
do_world_map_both(cairo_t * cr,int width,int height,int loops)132 do_world_map_both (cairo_t *cr, int width, int height, int loops)
133 {
134     return do_world_map (cr, width, height, loops, FILL | STROKE);
135 }
136 
137 cairo_bool_t
world_map_enabled(cairo_perf_t * perf)138 world_map_enabled (cairo_perf_t *perf)
139 {
140     return cairo_perf_can_run (perf, "world-map", NULL);
141 }
142 
143 void
world_map(cairo_perf_t * perf,cairo_t * cr,int width,int height)144 world_map (cairo_perf_t *perf, cairo_t *cr, int width, int height)
145 {
146     cairo_perf_run (perf, "world-map-stroke", do_world_map_stroke, NULL);
147     cairo_perf_run (perf, "world-map-fill", do_world_map_fill, NULL);
148     cairo_perf_run (perf, "world-map", do_world_map_both, NULL);
149 }
150