#include #include #define NUM_RUNS 100 #define M_PI 3.14159265358979323846 static gboolean run_benchmark = FALSE; static gboolean square = TRUE; static void run_bench_square (gpointer data, gpointer user_data) { run_benchmark = TRUE; gtk_widget_queue_draw (GTK_WIDGET (user_data)); } static void run_bench_round (gpointer data, gpointer user_data) { square = FALSE; run_benchmark = TRUE; gtk_widget_queue_draw (GTK_WIDGET (user_data)); } 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 draw_cairo (gpointer data, gpointer user_data) { struct timeval before; struct timeval after; int i; int runs = 1; GtkWidget *widget = GTK_WIDGET (data); int width = widget->allocation.width; int height = widget->allocation.height; cairo_t *cr; cairo_pattern_t *pattern; if (run_benchmark) { printf("Running benchmark...\n"); runs = NUM_RUNS; } gettimeofday (&before, NULL); for (i=0; iwindow); 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_fill (cr); cairo_destroy (cr); } gettimeofday (&after, NULL); if (run_benchmark) fprintf (stderr, "%g msec\n", (after.tv_sec - before.tv_sec) * 1000. + (after.tv_usec - before.tv_usec) / 1000.); run_benchmark = FALSE; } GtkWidget* create_window (void) { GtkWidget *window; GtkWidget *vbox; GtkWidget *hbox; GtkWidget *drawingarea; GtkWidget *button_rect; GtkWidget *button_round; window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_widget_set_size_request (window, 400, 300); vbox = gtk_vbox_new (FALSE, 0); gtk_container_add (GTK_CONTAINER (window), vbox); drawingarea = gtk_drawing_area_new (); gtk_box_pack_start (GTK_BOX (vbox), drawingarea, TRUE, TRUE, 0); hbox = gtk_hbox_new (FALSE, 0); gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0); button_rect = gtk_button_new_with_label ("Squared"); gtk_box_pack_start (GTK_BOX (hbox), button_rect, TRUE, TRUE, 0); button_round = gtk_button_new_with_label ("Rounded"); gtk_box_pack_start (GTK_BOX (hbox), button_round, TRUE, TRUE, 0); g_signal_connect ((gpointer) drawingarea, "expose_event", G_CALLBACK (draw_cairo), NULL); g_signal_connect ((gpointer) button_rect, "clicked", G_CALLBACK (run_bench_square), drawingarea); g_signal_connect ((gpointer) button_round, "clicked", G_CALLBACK (run_bench_round), drawingarea); g_signal_connect ((gpointer) window, "destroy", G_CALLBACK (gtk_main_quit), NULL); return window; } int main (int argc, char *argv[]) { GtkWidget *window; gtk_init (&argc, &argv); window = create_window (); gtk_widget_show_all (window); gtk_main (); return 0; }