From 6c08b72c36630182401453f5c9e338cf23489382 Mon Sep 17 00:00:00 2001 From: suyambulingamrm Date: Tue, 27 May 2014 16:35:38 +0530 Subject: Made cairo fill to use its functions from cairo-shapes and removed unwanted code. --- src/cairo-tests/fill.cpp | 294 +++-------------------------------------------- 1 file changed, 19 insertions(+), 275 deletions(-) (limited to 'src/cairo-tests/fill.cpp') diff --git a/src/cairo-tests/fill.cpp b/src/cairo-tests/fill.cpp index e6c8982..b268318 100644 --- a/src/cairo-tests/fill.cpp +++ b/src/cairo-tests/fill.cpp @@ -5,260 +5,19 @@ * specified in the COPYING file included with this source code. */ #include - -#include -#include -#include -#include -#include - -#include "cairo-shapes.h" +#include "forward.h" #include "caskbench.h" -#include "kinetics.h" -#include "unistd.h" +#include "cairo-shapes.h" static int element_spacing; static int num_x_elements; static int num_y_elements; -static kinetics_t *cairo_particles; - -void drawshapes(caskbench_context_t *ctx,kinetics_t *particles) -{ - int i, j, x, y, r, p; - shape_type_t shape; - cairo_t *cr = ctx->cairo_cr; - r = 0.9 * element_spacing /2; - int old_x, old_y,old_width, old_height; - cairo_pattern_t *pattern; - cairo_surface_t *image = NULL; - double red, green, blue, alpha; - static const double dashed3[] = {1.0}; - static const double dashed2[] = {14.0, 6.0}; - cairo_path_t *newPath; - shape = ctx->shape_defaults.shape_type; - if(shape == CB_SHAPE_NONE) - { - shape = generate_random_shape(); - if(!ctx->shape_defaults.multi_shapes) - shape = CB_SHAPE_RECTANGLE; - } else { - if(!(ctx->shape_defaults.x && ctx->shape_defaults.y)) - { - ctx->shape_defaults.x = x = ctx->canvas_width/2; - ctx->shape_defaults.y = y = ctx->canvas_height/2; - } - if(!ctx->shape_defaults.multi_shapes) - if(!(ctx->shape_defaults.width && ctx->shape_defaults.height)) - { - ctx->shape_defaults.width = 100; - ctx->shape_defaults.height = 50; - } - ctx->shape_defaults.radius = r; - } - - - if(!ctx->shape_defaults.animation && !ctx->shape_defaults.multi_shapes) - { - num_x_elements = 1; - num_y_elements = 1; - - r = 40; - old_x = ctx->shape_defaults.x; - old_y = ctx->shape_defaults.y; - old_width = ctx->shape_defaults.width; - old_height = ctx->shape_defaults.height; - } - - for (j=0; jy : j * element_spacing; - for (i=0; ix : i * element_spacing; - - //Options for fill,gradient and transparency - if (ctx->shape_defaults.fill_type == CB_FILL_NONE) - { - if(ctx->shape_defaults.red > 0 || ctx->shape_defaults.blue > 0 || ctx->shape_defaults.green > 0 || ctx->shape_defaults.alpha > 0) - { - cairo_set_source_rgba(cr, (double)ctx->shape_defaults.red, (double)ctx->shape_defaults.green ? (double)ctx->shape_defaults.green:(double)0, ctx->shape_defaults.blue ? (double)ctx->shape_defaults.blue:(double)0, (double)ctx->shape_defaults.alpha ? (double)ctx->shape_defaults.alpha:(double)1); - } - else - cairoRandomizeColor(ctx); - } - else if (ctx->shape_defaults.fill_type == CB_FILL_SOLID) - { - if(ctx->shape_defaults.red > 0 || ctx->shape_defaults.blue > 0 || ctx->shape_defaults.green > 0 || ctx->shape_defaults.alpha > 0) - { - cairo_set_source_rgba(cr, (double)ctx->shape_defaults.red, (double)ctx->shape_defaults.green ? (double)ctx->shape_defaults.green:(double)0, ctx->shape_defaults.blue ? (double)ctx->shape_defaults.blue:(double)0, (double)ctx->shape_defaults.alpha ? (double)ctx->shape_defaults.alpha:(double)1); - } - else - cairoRandomizeColor(ctx); - } - else if (ctx->shape_defaults.fill_type == CB_FILL_LINEAR_GRADIENT || ctx->shape_defaults.fill_type == CB_FILL_RADIAL_GRADIENT) - { - red = (double)rand()/RAND_MAX; - green = (double)rand()/RAND_MAX; - blue = (double)rand()/RAND_MAX; - alpha = (double)rand()/RAND_MAX; - - } - else if(ctx->shape_defaults.fill_type == CB_FILL_HERRINGBONE_PATTERN){} - else if(ctx->shape_defaults.fill_type == CB_FILL_IMAGE_PATTERN && (ctx->stock_image_path)) - { - cairo_matrix_t matrix; - - image = cairo_image_surface_create_from_png (ctx->stock_image_path); - int w = cairo_image_surface_get_width (image); - int h = cairo_image_surface_get_height (image); - - pattern = cairo_pattern_create_for_surface (image); - cairo_set_source (cr, pattern); - } - if (!ctx->shape_defaults.multi_shapes && !ctx->shape_defaults.animation) { - x = ctx->shape_defaults.x; - y = ctx->shape_defaults.y; - } - - double cx, cy, rr, x1, x2; - shape_type_t shape_type; - switch (shape) { - case 1: - // Circle - shape_type = CB_SHAPE_CIRCLE; - ctx->shape_defaults.x = x; - ctx->shape_defaults.y = y; - ctx->shape_defaults.radius = r; - - cx = x; - cy = y; - rr = r; - x1 = y-r; - x2 = y+r; - - break; - - case 2: - // Rectangle - shape_type = CB_SHAPE_RECTANGLE; - ctx->shape_defaults.x = x; - ctx->shape_defaults.y = y; - ctx->shape_defaults.width = (ctx->shape_defaults.width)?ctx->shape_defaults.width:2*r; - ctx->shape_defaults.height = (ctx->shape_defaults.height)?ctx->shape_defaults.height:2*r; - - cx = x+(ctx->shape_defaults.width/2); - cy = y+(ctx->shape_defaults.height/2); - rr = ctx->shape_defaults.height/2; - x1 = y; - x2 = y+ctx->shape_defaults.height; - - break; - - case 3: - // Triangle - shape_type = CB_SHAPE_TRIANGLE; - - cx = x; - cy = y + 2*r; - rr = r; - x1 = y + 2*r; - x2 = y + 2*r + 4*r; - - break; - - case 4: - // Star - shape_type = CB_SHAPE_STAR; - - cx = x + 2*r * star_points[0][0]/200.0; - cy = y + 2*r * star_points[0][1]/200.0; - rr = r; - x1 = x + 2*r * star_points[0][0]/200.0 - r; - x2 = y + 2*r * star_points[0][1]/200.0 + r; - - break; - - default: - // TODO: Should never reach here - break; - } - - if (ctx->shape_defaults.fill_type != CB_FILL_NONE) - { - if (ctx->shape_defaults.fill_type == CB_FILL_RADIAL_GRADIENT) { - pattern = cairo_pattern_create_radial (cx, cy, rr, cx, cy, 0); - cairo_pattern_add_color_stop_rgba (pattern, 0, red, green, blue, alpha); - cairo_pattern_add_color_stop_rgba (pattern, 1, 0.78, 0.78, 0.78, 0.78); - cairo_set_source (cr, pattern); - } else if (ctx->shape_defaults.fill_type == CB_FILL_LINEAR_GRADIENT) { - pattern = cairo_pattern_create_linear (0, x1, 0, x2); - cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT); - cairo_pattern_add_color_stop_rgba (pattern, 0, red, green, blue, alpha); - cairo_pattern_add_color_stop_rgba (pattern, 1, 0.78, 0.78, 0.78, 0.78); - cairo_set_source (cr, pattern); - } - } - - cairoShapes[shape_type] (ctx,&ctx->shape_defaults); - - // Reset - ctx->shape_defaults.width = old_width; - ctx->shape_defaults.height = old_height; - - newPath = cairo_copy_path(cr); - cairo_fill (cr); - if(ctx->shape_defaults.stroke_width) - { - cairo_new_path(cr); - cairo_append_path(cr, newPath); - cairo_set_line_width(cr, ctx->shape_defaults.stroke_width); - cairo_set_line_cap(cr, (cairo_line_cap_t)ctx->shape_defaults.cap_style); - if (ctx->shape_defaults.dash_style == 1) - { - cairo_set_dash(cr, dashed3, 1, 0); - } - else if (ctx->shape_defaults.dash_style == 2) - { - cairo_set_dash(cr, dashed2, 2, 0); - } - - cairo_set_line_join(cr, (cairo_line_join_t)ctx->shape_defaults.join_style); - if (ctx->shape_defaults.red > 0 || ctx->shape_defaults.blue > 0 || ctx->shape_defaults.green > 0 || ctx->shape_defaults.alpha > 0) - { - cairo_set_source_rgba(cr, (double)ctx->shape_defaults.red, (double)ctx->shape_defaults.green ? (double)ctx->shape_defaults.green:(double)0, ctx->shape_defaults.blue ? (double)ctx->shape_defaults.blue:(double)0, (double)ctx->shape_defaults.alpha ? (double)ctx->shape_defaults.alpha:(double)1); - } - else - cairoRandomizeColor(ctx); - - cairo_stroke(cr); - } - if (ctx->shape_defaults.fill_type != CB_FILL_NONE) - { - if (pattern != NULL) - cairo_pattern_destroy(pattern); - if (image != NULL) - cairo_surface_destroy (image); - } - } - } - - if (!ctx->shape_defaults.animation && !ctx->shape_defaults.multi_shapes) - { - ctx->shape_defaults.x = old_x; - ctx->shape_defaults.y = old_y; - ctx->shape_defaults.width = old_width; - ctx->shape_defaults.height = old_height; - } -} int ca_setup_fill(caskbench_context_t *ctx) { - if (ctx->size < 0) + if (ctx->size <= 0) return 0; - - element_spacing = sqrt( ((double)ctx->canvas_width * ctx->canvas_height) / ctx->size); - num_x_elements = ctx->canvas_width / element_spacing; - num_y_elements = ctx->canvas_height / element_spacing; - return 1; } @@ -270,41 +29,26 @@ ca_teardown_fill(void) int ca_test_fill(caskbench_context_t *ctx) { - cairo_t *cr = ctx->cairo_cr; - for(int i = 0;i < ctx->size; i++) { - //Animation / Kinematics of single or multi shape - if(ctx->shape_defaults.animation) - { - int num_particles = ctx->shape_defaults.animation; - double start_frame, stop_frame, delta; - cairo_particles = (kinetics_t *) malloc (sizeof (kinetics_t) * num_particles); - int i,j; - for (i = 0; i < num_particles; i++) - kinetics_init (&cairo_particles[i]); - delta = 0; - - for (j=0;jcanvas_width ,ctx->canvas_height); - cairo_fill (cr); - - for (i = 0; i < num_particles; i++) { - kinetics_update(&cairo_particles[i], 0.1); - drawshapes(ctx,&cairo_particles[i]); - } - stop_frame = get_tick (); - delta = stop_frame - start_frame; - } + for (int i = 0; i < ctx->size; i++) + { + shapes_t shape; + shape_copy(&ctx->shape_defaults, &shape); + if (!(shape.x && shape.y)) + { + shape.x = ctx->canvas_width/2; + shape.y = ctx->canvas_height/2; } - - // Drawing of multishape on a grid - else + if (!(shape.width && shape.height)) { - drawshapes(ctx,NULL); + shape.width = 100; + shape.height = 50; } + if (!shape.radius) + shape.radius = 40; + + shape.shape_type = CB_SHAPE_RECTANGLE; + cairoDrawRandomizedShape(ctx, &shape); } return 1; } -- cgit v1.3.1-4-g156e From 8e77b0d253afadcc543acc0463c062b9bd7857df Mon Sep 17 00:00:00 2001 From: suyambulingamrm Date: Tue, 27 May 2014 19:55:45 +0530 Subject: Minor issue fixes which produced incorrect output with respect to cairo and skia shapes. --- src/cairo-shapes.cpp | 2 +- src/cairo-tests/fill.cpp | 4 ++-- src/caskbench.cpp | 1 + src/skia-shapes.cpp | 5 +++-- src/skia-tests/fill.cpp | 2 ++ 5 files changed, 9 insertions(+), 5 deletions(-) (limited to 'src/cairo-tests/fill.cpp') diff --git a/src/cairo-shapes.cpp b/src/cairo-shapes.cpp index 5016909..5597b26 100644 --- a/src/cairo-shapes.cpp +++ b/src/cairo-shapes.cpp @@ -121,7 +121,7 @@ cairoDrawRoundedRectangle (caskbench_context_t *ctx, shapes_t *args) double w = args->width; double x = args->x; double y = args->y; - double r = args->radius; + double r = 4.0; /* To make the output in sync with skia */ cairo_move_to (ctx->cairo_cr, x + r, y); cairo_arc (ctx->cairo_cr, x + w - r, y + r, r, M_PI + M_PI / 2, M_PI * 2 ); cairo_arc (ctx->cairo_cr, x + w - r, y + h - r, r, 0, M_PI / 2 ); diff --git a/src/cairo-tests/fill.cpp b/src/cairo-tests/fill.cpp index b268318..919ee13 100644 --- a/src/cairo-tests/fill.cpp +++ b/src/cairo-tests/fill.cpp @@ -46,8 +46,8 @@ ca_test_fill(caskbench_context_t *ctx) } if (!shape.radius) shape.radius = 40; - - shape.shape_type = CB_SHAPE_RECTANGLE; + /* Use rectangle as default shape */ + shape.shape_type = shape.shape_type ? shape.shape_type:CB_SHAPE_RECTANGLE; cairoDrawRandomizedShape(ctx, &shape); } return 1; diff --git a/src/caskbench.cpp b/src/caskbench.cpp index a77d3c3..82c080d 100644 --- a/src/caskbench.cpp +++ b/src/caskbench.cpp @@ -340,6 +340,7 @@ shape_defaults_init(shapes *shape_defaults, caskbench_options_t *opt) shape_defaults->cap_style = opt->cap_style; shape_defaults->join_style = opt->join_style; shape_defaults->dash_style = opt->dash_style; + shape_defaults->radius = 0.0; } void diff --git a/src/skia-shapes.cpp b/src/skia-shapes.cpp index 06cc96c..6888714 100644 --- a/src/skia-shapes.cpp +++ b/src/skia-shapes.cpp @@ -106,6 +106,7 @@ void skiaDrawTriangle(caskbench_context_t *ctx, shapes_t *args) path.moveTo(args->x, args->y + 2*args->radius); path.rLineTo(2*args->radius, 0); path.rLineTo(-args->radius, -2*args->radius); + path.close(); ctx->skia_canvas->drawPath(path, *(ctx->skia_paint)); @@ -124,6 +125,7 @@ void skiaDrawStar(caskbench_context_t *ctx, shapes_t *args) py = args->y + 2*args->radius * star_points[p][1]/200.0; path.lineTo(px, py); } + path.close(); ctx->skia_canvas->drawPath(path, *(ctx->skia_paint)); } @@ -131,8 +133,7 @@ void skiaDrawStar(caskbench_context_t *ctx, shapes_t *args) void skiaDrawRoundedRectangle (caskbench_context_t *ctx, shapes_t *args) { SkRect rect; - rect.set(args->x, args->y, args->width, args->height); - + rect.set(args->x, args->y, args->x + args->width, args->y + args->height); ctx->skia_canvas->drawRoundRect(rect, 4.0, 4.0, *(ctx->skia_paint)); } diff --git a/src/skia-tests/fill.cpp b/src/skia-tests/fill.cpp index f1fc951..859a639 100644 --- a/src/skia-tests/fill.cpp +++ b/src/skia-tests/fill.cpp @@ -54,6 +54,8 @@ sk_test_fill(caskbench_context_t *ctx) if (!shape.radius) shape.radius = 40; + /* Use rectangle as default shape */ + shape.shape_type = shape.shape_type ? shape.shape_type:CB_SHAPE_RECTANGLE; skiaDrawRandomizedShape(ctx, &shape); } -- cgit v1.3.1-4-g156e