summaryrefslogtreecommitdiff
path: root/waterfall-demo.c
blob: 0d8496313d9de49ad7e24670bd707e453109598b (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <cairo.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <math.h>

#include "demo.h"
#include "list.h"

#define LOREM "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"

struct waterfall {
	struct device *device;
	struct list text;
	enum clip clip;
	int min_size, max_size;
};

struct text {
	struct list list;
	double rgba[4];
	int size;
	int y;
	int hue;
};

static void hue_to_rgba (int hue, double *rgba)
{
	double h, s = 1, v = .85;
	double m, n, f;
	int i;

	h = fmod (hue / 16., 6.);
	i = floor (h);
	f = h - i;
	if ((i & 1) == 0)
		f = 1 - f;

	m = v * (1 - s);
	n = v * (1 - s * f);
	switch(i){
	case 6:
	case 0: rgba[0] = v; rgba[1] = n; rgba[2] = m; break;
	case 1: rgba[0] = n; rgba[1] = v; rgba[2] = m; break;
	case 2: rgba[0] = m; rgba[1] = v; rgba[2] = n; break;
	case 3: rgba[0] = m; rgba[1] = n; rgba[2] = v; break;
	case 4: rgba[0] = n; rgba[1] = m; rgba[2] = v; break;
	case 5: rgba[0] = v; rgba[1] = m; rgba[2] = n; break;
	}
	rgba[3] = 1.;
}

static void waterfall_draw(struct waterfall *w, cairo_t *cr)
{
	struct text *t;

	cairo_set_source_rgb (cr, 1, 1, 1);
	cairo_paint (cr);

	device_apply_clip(w->device, cr, w->clip);

	list_for_each_entry(t, &w->text, list) {
		cairo_move_to (cr, 0, t->y);
		cairo_set_font_size (cr, t->size);
		cairo_set_source_rgba (cr, t->rgba[0], t->rgba[1], t->rgba[2], t->rgba[3]);
		cairo_show_text (cr, LOREM);
		--t->y;
	}

	if (list_is_empty(&w->text)) {
		t = malloc(sizeof (*t));
		list_add_tail(&t->list, &w->text);
		t->y = w->device->height;
		t->size = w->min_size;
		t->hue = 0;
		hue_to_rgba(t->hue, t->rgba);
	} else {
		t = list_first_entry(&w->text, struct text, list);
		if (t->y + t->size < 0) {
			list_del(&t->list);
			free (t);
		}

		t = list_last_entry(&w->text, struct text, list);
		if (t->y < w->device->height) {
			struct text *tt = malloc(sizeof (*t));
			list_add_tail(&tt->list, &w->text);
			tt->y = t->y + t->size + 2;
			if (t->size < w->device->height)
				tt->size = t->size + 1;
			else
				tt->size = 4;
			if (tt->size < w->min_size)
				tt->size = w->min_size;
			if (tt->size > w->max_size)
				tt->size = w->min_size;
			tt->hue = t->hue + 1;
			hue_to_rgba(tt->hue, tt->rgba);
		}
	}

	cairo_reset_clip (cr);
}

static int done;
static void signal_handler(int sig)
{
	done = sig;
}

int main (int argc, char **argv)
{
	struct waterfall waterfall;
	struct timeval start, last_tty, last_fps, now;

	double delta;
	int frame = 0;
	int frames = 0;
	int show_fps = 1;
	int benchmark;
	const char *version;

	int n;

	list_init(&waterfall.text);

	waterfall.min_size = 6;
	waterfall.max_size = 96;
	waterfall.device = device_open(argc, argv);
	version = device_show_version(argc, argv);
	waterfall.clip = device_get_clip(argc, argv);
	benchmark = device_get_benchmark(argc, argv);
	if (benchmark == 0)
		benchmark = 20;
	if (benchmark > 0)
		show_fps = 0;

	signal(SIGHUP, signal_handler);

	for (n = 1; n < argc; n++) {
		if (strcmp (argv[n], "--hide-fps") == 0)
			show_fps = 0;
		if (strncmp (argv[n], "--size=", 7) == 0) {
			int min, max;
			if (sscanf(argv[n]+7, "%d,%d", &min, &max) != 2)
				max = min = atoi(argv[n]+7);
			if (min != 0 && max >= min)
				waterfall.min_size = min, waterfall.max_size = max;
		}
	}

	gettimeofday(&start, 0); now = last_tty = last_fps = start;
	do {
		struct framebuffer *fb = waterfall.device->get_framebuffer (waterfall.device);
		cairo_t *cr = cairo_create(fb->surface);

		waterfall_draw(&waterfall, cr);

		gettimeofday(&now, NULL);
		if (show_fps && last_fps.tv_sec) {
			fps_draw(cr, waterfall.device->name, version, &last_fps, &now);
		}
		last_fps = now;

		cairo_destroy(cr);

		fb->show (fb);
		fb->destroy (fb);

		if (benchmark < 0 && 0) {
			delta = now.tv_sec - last_tty.tv_sec;
			delta += (now.tv_usec - last_tty.tv_usec)*1e-6;
			frames++;
			if (delta >  5) {
				printf("%.2f fps\n", frames/delta);
				last_tty = now;
				frames = 0;
			}
		}

		frame++;
		if (benchmark > 0) {
			delta = now.tv_sec - start.tv_sec;
			delta += (now.tv_usec - start.tv_usec)*1e-6;
			if (delta > benchmark) {
				printf("waterfall: %.2f fps\n", frame / delta);
				break;
			}
		}
	} while (!done);

	if (benchmark < 0) {
		struct framebuffer *fb = waterfall.device->get_framebuffer (waterfall.device);
		cairo_t *cr = cairo_create(fb->surface);

		waterfall_draw(&waterfall, cr);
		cairo_destroy(cr);

		fps_finish(fb, waterfall.device->name, version, "waterfall");
		fb->show (fb);
		fb->destroy (fb);
		pause();
	}

	return 0;
}