From 10f3ba2b2c63cf564664ad5ed719fb38c0262ecd Mon Sep 17 00:00:00 2001 From: "Alfred E. Heggestad" Date: Fri, 24 Mar 2017 16:20:24 +0100 Subject: cairo: add text with resolution and framerate --- modules/cairo/cairo.c | 67 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/modules/cairo/cairo.c b/modules/cairo/cairo.c index 438e654..0b86304 100644 --- a/modules/cairo/cairo.c +++ b/modules/cairo/cairo.c @@ -19,12 +19,22 @@ #endif -/* +/** + * @defgroup cairo cairo + * + * Cairo video-source module is a video generator for testing + * and demo purposes. + * * Note: This module is very experimental! * * Use Cairo library to draw graphics into a frame buffer */ + +enum { + FONT_SIZE = 18 +}; + struct vidsrc_st { const struct vidsrc *vs; /* inheritance */ @@ -63,9 +73,7 @@ static void draw_gradient(cairo_t *cr, double step, int width, int height) { cairo_pattern_t *pat; double r, g, b; - double x, y, tx, ty; - char buf[128]; - double fontsize = 20.0; + double x, y; r = 0.1 + fabs(sin(5 * step)); g = 0.0; @@ -74,10 +82,6 @@ static void draw_gradient(cairo_t *cr, double step, int width, int height) x = width * (sin(10 * step) + 1)/2; y = height * (1 - fabs(sin(30 * step))); - tx = width/2 * (sin(5 * step) + 1)/2; - ty = fontsize + (height - fontsize) * (1 - fabs(sin(20 * step))); - - pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, height); cairo_pattern_add_color_stop_rgba (pat, 1, r, g, b, 1); cairo_pattern_add_color_stop_rgba (pat, 0, 0, 0, 0, 1); @@ -95,15 +99,6 @@ static void draw_gradient(cairo_t *cr, double step, int width, int height) cairo_fill (cr); cairo_pattern_destroy (pat); - /* Draw text */ - cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, - CAIRO_FONT_WEIGHT_NORMAL); - cairo_set_font_size (cr, fontsize); - - re_snprintf(buf, sizeof(buf), "%H", fmt_gmtime, NULL); - - cairo_move_to (cr, tx, ty); - cairo_text_path (cr, buf); cairo_set_source_rgb (cr, 1, 1, 1); cairo_fill_preserve (cr); cairo_set_source_rgb (cr, 0, 0, 0); @@ -112,11 +107,36 @@ static void draw_gradient(cairo_t *cr, double step, int width, int height) } +static void draw_text(struct vidsrc_st *st, int x, int y, + const char *fmt, ...) +{ + char buf[4096] = ""; + va_list ap; + + va_start(ap, fmt); + (void)re_vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + + cairo_set_source_rgb(st->cr, 1.0, 1.0, 1.0); /* white */ + + cairo_set_font_size(st->cr, FONT_SIZE); + cairo_move_to(st->cr, x, y); + cairo_show_text(st->cr, buf); +} + + static void process(struct vidsrc_st *st) { struct vidframe f; + unsigned xoffs = 2, yoffs = 24; draw_gradient(st->cr, st->step, st->size.w, st->size.h); + + draw_text(st, xoffs, yoffs + FONT_SIZE, "%H", fmt_gmtime, NULL); + + draw_text(st, xoffs, yoffs + FONT_SIZE*2, "%u x %u @ %d fps", + st->size.w, st->size.h, st->prm.fps); + st->step += 0.02 / st->prm.fps; vidframe_init_buf(&f, VID_FMT_RGB32, &st->size, @@ -182,7 +202,20 @@ static int alloc(struct vidsrc_st **stp, const struct vidsrc *vs, st->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size->w, size->h); + if (!st->surface) { + err = ENOMEM; + goto out; + } + st->cr = cairo_create(st->surface); + if (!st->cr) { + err = ENOMEM; + goto out; + } + + cairo_select_font_face(st->cr, "Sans", + CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_BOLD); info("cairo: surface with format %d (%d x %d) stride=%d\n", cairo_image_surface_get_format(st->surface), -- cgit v1.2.3