summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2012-02-28 13:09:12 +0000
committerChris Wilson <chris@chris-wilson.co.uk>2012-02-28 13:09:58 +0000
commit9da24443937cc82bc2f603cc57e56bd302d2afc8 (patch)
treec38876d81d17f05f235e123ec0622e07581abde9
parent846807b5b82e8741fe657969cb9f566a59da53c9 (diff)
Add a waterfall demo
Repeatedly draw the same line of text at different sizes to stress glyph creation as well as rendering.
-rw-r--r--.gitignore1
-rw-r--r--Makefile4
-rw-r--r--list.h151
-rw-r--r--waterfall-demo.c192
4 files changed, 347 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index bac3ee5..116f3c0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,3 +8,4 @@ slideshow-demo
spinner-demo
spiral-demo
tiger-demo
+waterfall-demo
diff --git a/Makefile b/Makefile
index 5d5e596..b172918 100644
--- a/Makefile
+++ b/Makefile
@@ -38,7 +38,7 @@ else
DEFINES+=-DHAVE_COGL=0
endif
-all: spinner-demo spiral-demo slideshow-demo tiger-demo fish-demo flowers-demo gears-demo gradient-demo chart-demo
+all: spinner-demo spiral-demo slideshow-demo tiger-demo fish-demo flowers-demo gears-demo gradient-demo chart-demo waterfall-demo
ifeq ($(shell pkg-config --exists poppler-glib && echo 1), 1)
all: poppler-demo
@@ -70,6 +70,8 @@ gradient-demo: gradient-demo.c $(SOURCES) demo.h Makefile
$(CC) $(DEFINES) $(CFLAGS) -o $@ gradient-demo.c $(SOURCES) $(LIBS)
chart-demo: chart-demo.c $(SOURCES) demo.h Makefile
$(CC) $(DEFINES) $(CFLAGS) -o $@ chart-demo.c $(SOURCES) $(LIBS)
+waterfall-demo: waterfall-demo.c $(SOURCES) demo.h list.h Makefile
+ $(CC) $(DEFINES) $(CFLAGS) -o $@ waterfall-demo.c $(SOURCES) $(LIBS)
clean:
rm -f *-demo
diff --git a/list.h b/list.h
new file mode 100644
index 0000000..cec3f16
--- /dev/null
+++ b/list.h
@@ -0,0 +1,151 @@
+/*
+ * Copyright © 2010-2012 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#ifndef LIST_H
+#define LIST_H
+
+#include <stdbool.h>
+
+struct list {
+ struct list *next, *prev;
+};
+
+static void
+list_init(struct list *list)
+{
+ list->next = list->prev = list;
+}
+
+static inline void
+__list_add(struct list *entry,
+ struct list *prev,
+ struct list *next)
+{
+ next->prev = entry;
+ entry->next = next;
+ entry->prev = prev;
+ prev->next = entry;
+}
+
+static inline void
+list_add(struct list *entry, struct list *head)
+{
+ __list_add(entry, head, head->next);
+}
+
+static inline void
+list_add_tail(struct list *entry, struct list *head)
+{
+ __list_add(entry, head->prev, head);
+}
+
+static inline void list_replace(struct list *old,
+ struct list *new)
+{
+ new->next = old->next;
+ new->next->prev = new;
+ new->prev = old->prev;
+ new->prev->next = new;
+}
+
+static inline void
+list_append(struct list *entry, struct list *head)
+{
+ __list_add(entry, head->prev, head);
+}
+
+
+static inline void
+__list_del(struct list *prev, struct list *next)
+{
+ next->prev = prev;
+ prev->next = next;
+}
+
+static inline void
+_list_del(struct list *entry)
+{
+ __list_del(entry->prev, entry->next);
+}
+
+static inline void
+list_del(struct list *entry)
+{
+ _list_del(entry);
+ list_init(entry);
+}
+
+static inline void list_move(struct list *list, struct list *head)
+{
+ if (list->prev != head) {
+ _list_del(list);
+ list_add(list, head);
+ }
+}
+
+static inline void list_move_tail(struct list *list, struct list *head)
+{
+ _list_del(list);
+ list_add_tail(list, head);
+}
+
+static inline bool
+list_is_empty(struct list *head)
+{
+ return head->next == head;
+}
+
+#undef container_of
+#define container_of(ptr, type, member) \
+ ((type *)((char *)(ptr) - (char *) &((type *)0)->member))
+
+#define __container_of(ptr, sample, member) \
+ (void *)((char *)(ptr) \
+ - ((char *)&(sample)->member - (char *)(sample)))
+
+#define list_entry(ptr, type, member) \
+ container_of(ptr, type, member)
+
+#define list_first_entry(ptr, type, member) \
+ list_entry((ptr)->next, type, member)
+
+#define list_last_entry(ptr, type, member) \
+ list_entry((ptr)->prev, type, member)
+
+#define list_for_each(pos, head) \
+ for (pos = (head)->next; pos != (head); pos = pos->next)
+
+#define list_for_each_entry(pos, head, member) \
+ for (pos = __container_of((head)->next, pos, member); \
+ &pos->member != (head); \
+ pos = __container_of(pos->member.next, pos, member))
+
+#define list_for_each_entry_safe(pos, tmp, head, member) \
+ for (pos = __container_of((head)->next, pos, member), \
+ tmp = __container_of(pos->member.next, pos, member); \
+ &pos->member != (head); \
+ pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
+
+#endif /* LIST_H */
+
diff --git a/waterfall-demo.c b/waterfall-demo.c
new file mode 100644
index 0000000..c89773d
--- /dev/null
+++ b/waterfall-demo.c
@@ -0,0 +1,192 @@
+#include <cairo.h>
+#include <sys/time.h>
+#include <stdlib.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;
+};
+
+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 = 4;
+ 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;
+ 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.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;
+ }
+
+ 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("chart: %.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);
+ fb->show (fb);
+ fb->destroy (fb);
+ pause();
+ }
+
+ return 0;
+}