summaryrefslogtreecommitdiff
path: root/remenic-gradient.c
blob: d68c90356db55dac8f560e4423e6cf611b416a7f (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
#include <sys/time.h>
#include <cairo.h>
#include <stdlib.h>
#include <stdio.h>

#define WIDTH 400
#define HEIGHT 300
#define NUM_RUNS 100
#define M_PI 3.14159265358979323846

static int square = 1;

void
rounded_rectangle (cairo_t *cr,
		   double x, double y, double w, double h,
		   double radius)
{
    cairo_move_to (cr, x+radius, y);
    cairo_arc (cr, x+w-radius, y+radius,   radius, M_PI + M_PI / 2, M_PI * 2        );
    cairo_arc (cr, x+w-radius, y+h-radius, radius, 0,               M_PI / 2        );
    cairo_arc (cr, x+radius,   y+h-radius, radius, M_PI/2,          M_PI            );
    cairo_arc (cr, x+radius,   y+radius,   radius, M_PI,            270 * M_PI / 180);
}

static void
run_benchmark (cairo_surface_t *surface)
{
    struct timeval before;
    struct timeval after;
    int i;

    cairo_t *cr;
    cairo_pattern_t *pattern;

    gettimeofday (&before, NULL);
    for (i=0; i < NUM_RUNS; i++)
    {
	cr = cairo_create (surface);

	if (square)
	    cairo_rectangle (cr, 0, 0, WIDTH, HEIGHT);
	else
	    rounded_rectangle (cr, 0, 0, WIDTH, HEIGHT, 3.0);

	pattern = cairo_pattern_create_linear (0, 0, 0, HEIGHT);
	cairo_pattern_add_color_stop_rgb (pattern, 0, 1, 0, 0);
	cairo_pattern_add_color_stop_rgb (pattern, 1, 0, 0, 0);
	cairo_set_source (cr, pattern);
	cairo_pattern_destroy (pattern);

	cairo_fill (cr);
	cairo_destroy (cr);
    }
    gettimeofday (&after, NULL);

    fprintf (stderr, "%g msec\n",
	     (after.tv_sec - before.tv_sec) * 1000. +
	     (after.tv_usec - before.tv_usec) / 1000.);
}

int
main (int argc, char *argv[])
{
    cairo_surface_t *surface;
  
    if (argc != 2) {
	fprintf (stderr, "Usage: gradient-test [0/1] (0 == rounded, 1 == square)\n");
	exit (1);
    }

    square = atoi (argv[1]);

    surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, WIDTH, HEIGHT);

    run_benchmark (surface);

    return 0;
}