Skip to content

Commit

Permalink
optimize cairo drawing (use ints)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-tee committed Jul 8, 2020
1 parent d471379 commit 544699e
Show file tree
Hide file tree
Showing 8 changed files with 395 additions and 221 deletions.
150 changes: 110 additions & 40 deletions inc/utils/cairo.h
Expand Up @@ -64,50 +64,39 @@ typedef struct CairoCaches
void
z_cairo_draw_selection (
cairo_t * cr,
double start_x,
double start_y,
double offset_x,
double offset_y);
int start_x,
int start_y,
int offset_x,
int offset_y);

void
z_cairo_draw_horizontal_line (
cairo_t * cr,
double y,
double from_x,
double to_x,
int y,
int from_x,
int to_x,
double alpha);

void
z_cairo_draw_vertical_line (
cairo_t * cr,
double x,
double from_y,
double to_y);
int x,
int from_y,
int to_y);

/**
* @param aspect Aspect ratio.
* @param corner_radius Corner curvature radius.
*/
static inline void
void
z_cairo_rounded_rectangle (
cairo_t * cr,
double x,
double y,
double width,
double height,
int x,
int y,
int width,
int height,
double aspect,
double corner_radius)
{
double radius = corner_radius / aspect;
double degrees = G_PI / 180.0;

cairo_new_sub_path (cr);
cairo_arc (cr, x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees);
cairo_arc (cr, x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees);
cairo_arc (cr, x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees);
cairo_arc (cr, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
cairo_close_path (cr);
}
double corner_radius);

#define z_cairo_get_text_extents_for_widget( \
_widget,_layout,_text,_width,_height) \
Expand Down Expand Up @@ -157,21 +146,13 @@ z_cairo_draw_text_full (
/**
* Draws a diamond shape.
*/
static inline void
void
z_cairo_diamond (
cairo_t * cr,
double x,
double y,
double width,
double height)
{
cairo_move_to (cr, x, height / 2);
cairo_line_to (cr, width / 2, y);
cairo_line_to (cr, width, height / 2);
cairo_line_to (cr, width / 2, height);
cairo_line_to (cr, x, height / 2);
cairo_close_path (cr);
}
int x,
int y,
int width,
int height);

/**
* Returns a surface for the icon name.
Expand Down Expand Up @@ -227,6 +208,95 @@ z_cairo_reset_caches (
int height,
cairo_t * new_cr);

/**
* Wrapper using integers to prevent extra
* calculations in cairo. According to GTK devs,
* cairo will attempt to draw details when drawing
* non-integer coordinates.
*/
static inline void
z_cairo_move_to (
cairo_t * cr,
int x,
int y)
{
cairo_move_to (cr, (double) x, (double) y);
}

static inline void
z_cairo_line_to (
cairo_t * cr,
int x,
int y)
{
cairo_line_to (cr, (double) x, (double) y);
}

static inline void
z_cairo_rel_line_to (
cairo_t * cr,
int x,
int y)
{
cairo_rel_line_to (cr, (double) x, (double) y);
}

static inline void
z_cairo_rectangle (
cairo_t * cr,
int x,
int y,
int width,
int height)
{
cairo_rectangle (
cr, (double) x, (double) y,
(double) width, (double) height);
}

static inline void
z_cairo_arc (
cairo_t * cr,
int xc,
int yc,
int radius,
int angle1,
int angle2)
{
cairo_arc (
cr, (double) xc, (double) yc,
(double) radius, (double) angle1,
(double) angle2);
}

static inline void
z_cairo_translate (
cairo_t * cr,
int x,
int y)
{
cairo_translate (cr, (double) x, (double) y);
}

static inline void
z_cairo_set_source_surface (
cairo_t * cr,
cairo_surface_t * surface,
int x,
int y)
{
cairo_set_source_surface (
cr, surface, (double) x, (double) y);
}

static inline void
z_cairo_set_line_width (
cairo_t * cr,
int width)
{
cairo_set_line_width (cr, (double) width);
}

CairoCaches *
z_cairo_caches_new (void);

Expand Down
6 changes: 3 additions & 3 deletions src/gui/widgets/arranger.c
Expand Up @@ -126,9 +126,9 @@ draw_selections (
case UI_OVERLAY_ACTION_SELECTING:
case UI_OVERLAY_ACTION_DELETE_SELECTING:
z_cairo_draw_selection (
cr, self->start_x - rect->x,
self->start_y - rect->y,
offset_x, offset_y);
cr, (int) (self->start_x - rect->x),
(int) (self->start_y - rect->y),
(int) offset_x, (int) offset_y);
break;
case UI_OVERLAY_ACTION_RAMPING:
cairo_set_source_rgba (
Expand Down
27 changes: 16 additions & 11 deletions src/gui/widgets/automation_mode.c
Expand Up @@ -184,8 +184,10 @@ draw_bg (
cr, 1, 1, 1, 0.4);
cairo_set_line_width (cr, 0.5);
z_cairo_rounded_rectangle (
cr, x, y, self->width, self->height,
self->aspect, self->corner_radius);
cr, (int) x, (int) y,
(int) self->width, (int) self->height,
(int) self->aspect,
(int) self->corner_radius);
cairo_stroke (cr);
}

Expand Down Expand Up @@ -253,13 +255,16 @@ draw_bg (
if (rounded)
{
z_cairo_rounded_rectangle (
cr, new_x, y, new_width, self->height,
self->aspect, self->corner_radius);
cr, (int) new_x, (int) y,
(int) new_width, (int) self->height,
(int) self->aspect,
(int) self->corner_radius);
}
else
{
cairo_rectangle (
cr, new_x, y, new_width, self->height);
z_cairo_rectangle (
cr, (int) new_x, (int) y,
(int) new_width, (int) self->height);
}
cairo_fill (cr);
}
Expand Down Expand Up @@ -325,13 +330,13 @@ automation_mode_widget_draw (
PangoLayout * layout = self->layout;
cairo_set_source_rgba (
cr, 1, 1, 1, 1);
cairo_move_to (
z_cairo_move_to (
cr,
x + AUTOMATION_MODE_HPADDING +
(int) (x + AUTOMATION_MODE_HPADDING +
i * (2 * AUTOMATION_MODE_HPADDING) +
total_text_widths,
(y + self->height / 2) -
self->text_heights[i] / 2);
total_text_widths),
(int) ((y + self->height / 2) -
self->text_heights[i] / 2));
char mode_str[400];
if (i == AUTOMATION_MODE_RECORD)
{
Expand Down
20 changes: 12 additions & 8 deletions src/gui/widgets/custom_button.c
Expand Up @@ -111,10 +111,12 @@ draw_bg (
{
cairo_set_source_rgba (
cr, 1, 1, 1, 0.4);
cairo_set_line_width (cr, 0.5);
cairo_set_line_width (cr, 1);
z_cairo_rounded_rectangle (
cr, x, y, self->size, self->size,
self->aspect, self->corner_radius);
cr, (int) x, (int) y,
(int) self->size, (int) self->size,
(int) self->aspect,
(int) self->corner_radius);
cairo_stroke (cr);
}

Expand Down Expand Up @@ -143,8 +145,9 @@ draw_bg (
self->last_color = c;

z_cairo_rounded_rectangle (
cr, x, y, width, self->size, self->aspect,
self->corner_radius);
cr, (int) x, (int) y, (int) width,
(int) self->size, (int) self->aspect,
(int) self->corner_radius);
cairo_fill (cr);
}

Expand Down Expand Up @@ -205,9 +208,10 @@ custom_button_widget_draw_with_text (
/* draw text */
cairo_set_source_rgba (
cr, 1, 1, 1, 1);
cairo_move_to (
cr, x + self->size + 2,
(y + self->size / 2) - self->text_height / 2);
z_cairo_move_to (
cr, (int) (x + self->size + 2),
(int)
((y + self->size / 2) - self->text_height / 2));
PangoLayout * layout = self->layout;
pango_layout_set_text (
layout, self->text, -1);
Expand Down
33 changes: 18 additions & 15 deletions src/gui/widgets/midi_note.c
Expand Up @@ -109,9 +109,10 @@ draw_midi_note_bg (
}

z_cairo_rounded_rectangle (
cr, draw_x, full_rect->y - arr_rect->y,
draw_width, full_rect->height, 1.0,
full_rect->height / 8.0f);
cr, (int) draw_x,
(int) (full_rect->y - arr_rect->y),
(int) draw_width, (int) full_rect->height, 1,
(int) (full_rect->height / 8.0f));
/*cairo_rectangle (*/
/*cr, draw_x, full_rect->y - rect->y,*/
/*draw_width, full_rect->height);*/
Expand Down Expand Up @@ -202,10 +203,10 @@ midi_note_draw (
if (PIANO_ROLL->drum_mode)
{
z_cairo_diamond (
cr, draw_rect.x - arr_rect->x,
draw_rect.y - arr_rect->y,
draw_rect.width,
draw_rect.height);
cr, (int) (draw_rect.x - arr_rect->x),
(int) (draw_rect.y - arr_rect->y),
(int) draw_rect.width,
(int) draw_rect.height);
}
else
{
Expand All @@ -232,9 +233,10 @@ midi_note_draw (
if (PIANO_ROLL->drum_mode)
{
z_cairo_diamond (
cr, draw_rect.x - arr_rect->x,
draw_rect.y - arr_rect->y,
draw_rect.width, draw_rect.height);
cr, (int) (draw_rect.x - arr_rect->x),
(int) (draw_rect.y - arr_rect->y),
(int) draw_rect.width,
(int) draw_rect.height);
}
else
{
Expand Down Expand Up @@ -275,12 +277,13 @@ midi_note_draw (
recreate_pango_layouts (
self,
MIN (full_rect.width, 400));
cairo_move_to (
z_cairo_move_to (
cr,
REGION_NAME_BOX_PADDING +
(full_rect.x - arr_rect->x),
fontsize_ratio * REGION_NAME_BOX_PADDING +
(full_rect.y - arr_rect->y));
(int) (REGION_NAME_BOX_PADDING +
(full_rect.x - arr_rect->x)),
(int)
(fontsize_ratio * REGION_NAME_BOX_PADDING +
(full_rect.y - arr_rect->y)));
PangoLayout * layout = self->layout;
z_cairo_draw_text (
cr,
Expand Down

0 comments on commit 544699e

Please sign in to comment.