Debian Codegraph @

tests/micro-bench/test-text-perf.c

raw
1 #include <clutter/clutter.h>
2
3 #include <stdlib.h>
4 #include <string.h>
5
6 #define STAGE_WIDTH 800
7 #define STAGE_HEIGHT 600
8
9 static int font_size;
10 static int n_chars;
11 static int rows, cols;
12
13 static void
14 on_paint (ClutterActor *actor, gconstpointer *data)
15 {
16 static GTimer *timer = NULL;
17 static int fps = 0;
18
19 if (!timer)
20 {
21 timer = g_timer_new ();
22 g_timer_start (timer);
23 }
24
25 if (g_timer_elapsed (timer, NULL) >= 1)
26 {
27 printf ("fps=%d, strings/sec=%d, chars/sec=%d\n",
28 fps,
29 fps * rows * cols,
30 fps * rows * cols * n_chars);
31 g_timer_start (timer);
32 fps = 0;
33 }
34
35 ++fps;
36 }
37
38 static gboolean
39 queue_redraw (gpointer stage)
40 {
41 clutter_actor_queue_redraw (CLUTTER_ACTOR (stage));
42
43 return G_SOURCE_CONTINUE;
44 }
45
46 static gunichar
47 get_character (int ch)
48 {
49 int total_letters = 0;
50 int i;
51
52 static const struct
53 {
54 gunichar first_letter;
55 int n_letters;
56 }
57 ranges[] =
58 {
59 { 'a', 26 }, /* lower case letters */
60 { 'A', 26 }, /* upper case letters */
61 { '0', 10 }, /* digits */
62 { 0x410, 0x40 }, /* cyrillic alphabet */
63 { 0x3b1, 18 } /* greek alphabet */
64 };
65
66 for (i = 0; i < G_N_ELEMENTS (ranges); i++)
67 total_letters += ranges[i].n_letters;
68
69 ch %= total_letters;
70
71 for (i = 0; i < G_N_ELEMENTS (ranges) - 1; i++)
72 if (ch < ranges[i].n_letters)
73 return ch + ranges[i].first_letter;
74 else
75 ch -= ranges[i].n_letters;
76
77 return ch + ranges[i].first_letter;
78 }
79
80 static ClutterActor *
81 create_label (void)
82 {
83 ClutterColor label_color = { 0xff, 0xff, 0xff, 0xff };
84 ClutterActor *label;
85 char *font_name;
86 GString *str;
87 int i;
88
89 font_name = g_strdup_printf ("Monospace %dpx", font_size);
90
91 str = g_string_new (NULL);
92 for (i = 0; i < n_chars; i++)
93 g_string_append_unichar (str, get_character (i));
94
95 label = clutter_text_new_with_text (font_name, str->str);
96 clutter_text_set_color (CLUTTER_TEXT (label), &label_color);
97
98 g_free (font_name);
99 g_string_free (str, TRUE);
100
101 return label;
102 }
103
104 int
105 main (int argc, char *argv[])
106 {
107 ClutterActor *stage;
108 ClutterActor *label;
109 int w, h;
110 int row, col;
111 float scale = 1.0f;
112
113 g_setenv ("CLUTTER_VBLANK", "none", FALSE);
114 g_setenv ("CLUTTER_DEFAULT_FPS", "1000", FALSE);
115
116 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
117 return 1;
118
119 if (argc != 3)
120 {
121 g_printerr ("Usage test-text-perf FONT_SIZE N_CHARS\n");
122 exit (1);
123 }
124
125 font_size = atoi (argv[1]);
126 n_chars = atoi (argv[2]);
127
128 g_print ("Monospace %dpx, string length = %d\n", font_size, n_chars);
129
130 stage = clutter_stage_new ();
131 clutter_actor_set_size (stage, STAGE_WIDTH, STAGE_HEIGHT);
132 clutter_stage_set_color (CLUTTER_STAGE (stage), CLUTTER_COLOR_Black);
133 clutter_stage_set_title (CLUTTER_STAGE (stage), "Text Performance");
134
135 g_signal_connect (stage, "paint", G_CALLBACK (on_paint), NULL);
136
137 label = create_label ();
138 w = clutter_actor_get_width (label);
139 h = clutter_actor_get_height (label);
140
141 /* If the label is too big to fit on the stage then scale it so that
142 it will fit */
143 if (w > STAGE_WIDTH || h > STAGE_HEIGHT)
144 {
145 float x_scale = STAGE_WIDTH / (float) w;
146 float y_scale = STAGE_HEIGHT / (float) h;
147
148 if (x_scale < y_scale)
149 {
150 scale = x_scale;
151 cols = 1;
152 rows = STAGE_HEIGHT / (h * scale);
153 }
154 else
155 {
156 scale = y_scale;
157 cols = STAGE_WIDTH / (w * scale);
158 rows = 1;
159 }
160
161 g_print ("Text scaled by %f to fit on the stage\n", scale);
162 }
163 else
164 {
165 cols = STAGE_WIDTH / w;
166 rows = STAGE_HEIGHT / h;
167 }
168
169 clutter_actor_destroy (label);
170
171 for (row=0; row<rows; row++)
172 for (col=0; col<cols; col++)
173 {
174 label = create_label();
175 clutter_actor_set_scale (label, scale, scale);
176 clutter_actor_set_position (label, w * col * scale, h * row * scale);
177 clutter_container_add_actor (CLUTTER_CONTAINER (stage), label);
178 }
179
180 clutter_actor_show_all (stage);
181
182 clutter_threads_add_idle (queue_redraw, stage);
183
184 clutter_main ();
185
186 return 0;
187 }