[GTKUI] Fix cairo crashes by not storing current context

Windows users have reported Deluge crashes when resizing the window with
Piecesbar or Stats plugins enabled:

    Expression: CAIRO_REFERENCE_COUNT_HAS_REFERENCE(&surface->ref_count)

This is similar to issues fixed in GNU Radio which is a problem due to
storing the current cairo context which is then being destroyed and
recreated within GTK causing a reference count error

Fixes: https://dev.deluge-torrent.org/ticket/3339
Refs: https://github.com/gnuradio/gnuradio/pull/6352
Closes: https://github.com/deluge-torrent/deluge/pull/431
This commit is contained in:
Calum Lind 2023-08-27 12:57:32 +01:00
commit 18dca70084
No known key found for this signature in database
GPG key ID: 90597A687B836BA3
2 changed files with 110 additions and 114 deletions

View file

@ -104,20 +104,19 @@ class Graph:
def set_interval(self, interval): def set_interval(self, interval):
self.interval = interval self.interval = interval
def draw_to_context(self, context, width, height): def draw_to_context(self, ctx, width, height):
self.ctx = context
self.width, self.height = width, height self.width, self.height = width, height
self.draw_rect(white, 0, 0, self.width, self.height) self.draw_rect(ctx, white, 0, 0, self.width, self.height)
self.draw_graph() self.draw_graph(ctx)
return self.ctx
def draw(self, width, height): def draw(self, width, height):
"""Create surface with context for use in tests"""
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(surface) ctx = cairo.Context(surface)
self.draw_to_context(ctx, width, height) self.draw_to_context(ctx, width, height)
return surface return surface
def draw_x_axis(self, bounds): def draw_x_axis(self, ctx, bounds):
(left, top, right, bottom) = bounds (left, top, right, bottom) = bounds
duration = self.length * self.interval duration = self.length * self.interval
start = self.last_update - duration start = self.last_update - duration
@ -142,13 +141,13 @@ class Graph:
) )
# + 0.5 to allign x to nearest pixel # + 0.5 to allign x to nearest pixel
x = int(ratio * (seconds_to_step + i * x_step) + left) + 0.5 x = int(ratio * (seconds_to_step + i * x_step) + left) + 0.5
self.draw_x_text(text, x, bottom) self.draw_x_text(ctx, text, x, bottom)
self.draw_dotted_line(gray, x, top - 0.5, x, bottom + 0.5) self.draw_dotted_line(ctx, gray, x, top - 0.5, x, bottom + 0.5)
self.draw_line(gray, left, bottom + 0.5, right, bottom + 0.5) self.draw_line(ctx, gray, left, bottom + 0.5, right, bottom + 0.5)
def draw_graph(self): def draw_graph(self, ctx):
font_extents = self.ctx.font_extents() font_extents = ctx.font_extents()
x_axis_space = font_extents[2] + 2 + self.line_size / 2 x_axis_space = font_extents[2] + 2 + self.line_size / 2
plot_height = self.height - x_axis_space plot_height = self.height - x_axis_space
# lets say we need 2n-1*font height pixels to plot the y ticks # lets say we need 2n-1*font height pixels to plot the y ticks
@ -171,18 +170,18 @@ class Graph:
# find the width of the y_ticks # find the width of the y_ticks
y_tick_text = [self.left_axis['formatter'](tick) for tick in y_ticks] y_tick_text = [self.left_axis['formatter'](tick) for tick in y_ticks]
def space_required(text): def space_required(ctx, text):
te = self.ctx.text_extents(text) te = ctx.text_extents(text)
return math.ceil(te[4] - te[0]) return math.ceil(te[4] - te[0])
y_tick_width = max(space_required(text) for text in y_tick_text) y_tick_width = max(space_required(ctx, text) for text in y_tick_text)
top = font_extents[2] / 2 top = font_extents[2] / 2
# bounds(left, top, right, bottom) # bounds(left, top, right, bottom)
bounds = (y_tick_width + 4, top + 2, self.width, self.height - x_axis_space) bounds = (y_tick_width + 4, top + 2, self.width, self.height - x_axis_space)
self.draw_x_axis(bounds) self.draw_x_axis(ctx, bounds)
self.draw_left_axis(bounds, y_ticks, y_tick_text) self.draw_left_axis(ctx, bounds, y_ticks, y_tick_text)
def intervalise(self, x, limit=None): def intervalise(self, x, limit=None):
"""Given a value x create an array of tick points to got with the graph """Given a value x create an array of tick points to got with the graph
@ -229,7 +228,7 @@ class Graph:
] ]
return intervals return intervals
def draw_left_axis(self, bounds, y_ticks, y_tick_text): def draw_left_axis(self, ctx, bounds, y_ticks, y_tick_text):
(left, top, right, bottom) = bounds (left, top, right, bottom) = bounds
stats = {} stats = {}
for stat in self.stat_info: for stat in self.stat_info:
@ -246,29 +245,36 @@ class Graph:
for i, y_val in enumerate(y_ticks): for i, y_val in enumerate(y_ticks):
y = int(bottom - y_val * ratio) - 0.5 y = int(bottom - y_val * ratio) - 0.5
if i != 0: if i != 0:
self.draw_dotted_line(gray, left, y, right, y) self.draw_dotted_line(ctx, gray, left, y, right, y)
self.draw_y_text(y_tick_text[i], left, y) self.draw_y_text(ctx, y_tick_text[i], left, y)
self.draw_line(gray, left, top, left, bottom) self.draw_line(ctx, gray, left, top, left, bottom)
for stat, info in stats.items(): for stat, info in stats.items():
if len(info['values']) > 0: if len(info['values']) > 0:
self.draw_value_poly(info['values'], info['color'], max_value, bounds)
self.draw_value_poly( self.draw_value_poly(
info['values'], info['fill_color'], max_value, bounds, info['fill'] ctx, info['values'], info['color'], max_value, bounds
)
self.draw_value_poly(
ctx,
info['values'],
info['fill_color'],
max_value,
bounds,
info['fill'],
) )
def draw_legend(self): def draw_legend(self):
pass pass
def trace_path(self, values, max_value, bounds): def trace_path(self, ctx, values, max_value, bounds):
(left, top, right, bottom) = bounds (left, top, right, bottom) = bounds
ratio = (bottom - top) / max_value ratio = (bottom - top) / max_value
line_width = self.line_size line_width = self.line_size
self.ctx.set_line_width(line_width) ctx.set_line_width(line_width)
self.ctx.move_to(right, bottom) ctx.move_to(right, bottom)
self.ctx.line_to(right, int(bottom - values[0] * ratio)) ctx.line_to(right, int(bottom - values[0] * ratio))
x = right x = right
step = (right - left) / (self.length - 1) step = (right - left) / (self.length - 1)
@ -276,64 +282,62 @@ class Graph:
if i == self.length - 1: if i == self.length - 1:
x = left x = left
self.ctx.line_to(x, int(bottom - value * ratio)) ctx.line_to(x, int(bottom - value * ratio))
x -= step x -= step
self.ctx.line_to(int(right - (len(values) - 1) * step), bottom) ctx.line_to(int(right - (len(values) - 1) * step), bottom)
self.ctx.close_path() ctx.close_path()
def draw_value_poly(self, values, color, max_value, bounds, fill=False): def draw_value_poly(self, ctx, values, color, max_value, bounds, fill=False):
self.trace_path(values, max_value, bounds) self.trace_path(ctx, values, max_value, bounds)
self.ctx.set_source_rgba(*color) ctx.set_source_rgba(*color)
if fill: if fill:
self.ctx.fill() ctx.fill()
else: else:
self.ctx.stroke() ctx.stroke()
def draw_x_text(self, text, x, y): def draw_x_text(self, ctx, text, x, y):
"""Draws text below and horizontally centered about x,y""" """Draws text below and horizontally centered about x,y"""
fe = self.ctx.font_extents() fe = ctx.font_extents()
te = self.ctx.text_extents(text) te = ctx.text_extents(text)
height = fe[2] height = fe[2]
x_bearing = te[0] x_bearing = te[0]
width = te[2] width = te[2]
self.ctx.move_to(int(x - width / 2 + x_bearing), int(y + height)) ctx.move_to(int(x - width / 2 + x_bearing), int(y + height))
self.ctx.set_source_rgba(*self.black) ctx.set_source_rgba(*self.black)
self.ctx.show_text(text) ctx.show_text(text)
def draw_y_text(self, text, x, y): def draw_y_text(self, ctx, text, x, y):
"""Draws text left of and vertically centered about x,y""" """Draws text left of and vertically centered about x,y"""
fe = self.ctx.font_extents() fe = ctx.font_extents()
te = self.ctx.text_extents(text) te = ctx.text_extents(text)
descent = fe[1] descent = fe[1]
ascent = fe[0] ascent = fe[0]
x_bearing = te[0] x_bearing = te[0]
width = te[4] width = te[4]
self.ctx.move_to( ctx.move_to(int(x - width - x_bearing - 2), int(y + (ascent - descent) / 2))
int(x - width - x_bearing - 2), int(y + (ascent - descent) / 2) ctx.set_source_rgba(*self.black)
) ctx.show_text(text)
self.ctx.set_source_rgba(*self.black)
self.ctx.show_text(text)
def draw_rect(self, color, x, y, height, width): def draw_rect(self, ctx, color, x, y, height, width):
self.ctx.set_source_rgba(*color) ctx.set_source_rgba(*color)
self.ctx.rectangle(x, y, height, width) ctx.rectangle(x, y, height, width)
self.ctx.fill() ctx.fill()
def draw_line(self, color, x1, y1, x2, y2): def draw_line(self, ctx, color, x1, y1, x2, y2):
self.ctx.set_source_rgba(*color) ctx.set_source_rgba(*color)
self.ctx.set_line_width(1) ctx.set_line_width(1)
self.ctx.move_to(x1, y1) ctx.move_to(x1, y1)
self.ctx.line_to(x2, y2) ctx.line_to(x2, y2)
self.ctx.stroke() ctx.stroke()
def draw_dotted_line(self, color, x1, y1, x2, y2): def draw_dotted_line(self, ctx, color, x1, y1, x2, y2):
self.ctx.set_source_rgba(*color) ctx.set_source_rgba(*color)
self.ctx.set_line_width(1) ctx.set_line_width(1)
dash, offset = self.ctx.get_dash() dash, offset = ctx.get_dash()
self.ctx.set_dash(self.dash_length, 0) ctx.set_dash(self.dash_length, 0)
self.ctx.move_to(x1, y1) ctx.move_to(x1, y1)
self.ctx.line_to(x2, y2) ctx.line_to(x2, y2)
self.ctx.stroke() ctx.stroke()
self.ctx.set_dash(dash, offset) ctx.set_dash(dash, offset)

View file

@ -51,7 +51,6 @@ class PiecesBar(DrawingArea):
self.text = self.prev_text = '' self.text = self.prev_text = ''
self.fraction = self.prev_fraction = 0 self.fraction = self.prev_fraction = 0
self.progress_overlay = self.text_overlay = self.pieces_overlay = None self.progress_overlay = self.text_overlay = self.pieces_overlay = None
self.cr = None
self.connect('size-allocate', self.do_size_allocate_event) self.connect('size-allocate', self.do_size_allocate_event)
self.show() self.show()
@ -63,34 +62,30 @@ class PiecesBar(DrawingArea):
self.height = size.height self.height = size.height
# Handle the draw by drawing # Handle the draw by drawing
def do_draw(self, event): def do_draw(self, ctx):
# Create cairo context ctx.set_line_width(max(ctx.device_to_user_distance(0.5, 0.5)))
self.cr = self.props.window.cairo_create()
self.cr.set_line_width(max(self.cr.device_to_user_distance(0.5, 0.5)))
# Restrict Cairo to the exposed area; avoid extra work # Restrict Cairo to the exposed area; avoid extra work
self.roundcorners_clipping() self.roundcorners_clipping(ctx)
self.draw_pieces() self.draw_pieces(ctx)
self.draw_progress_overlay() self.draw_progress_overlay(ctx)
self.write_text() self.write_text(ctx)
self.roundcorners_border() self.roundcorners_border(ctx)
# Drawn once, update width, height # Drawn once, update width, height
if self.resized(): if self.resized():
self.prev_width = self.width self.prev_width = self.width
self.prev_height = self.height self.prev_height = self.height
def roundcorners_clipping(self): def roundcorners_clipping(self, ctx):
self.create_roundcorners_subpath(self.cr, 0, 0, self.width, self.height) self.create_roundcorners_subpath(ctx, 0, 0, self.width, self.height)
self.cr.clip() ctx.clip()
def roundcorners_border(self): def roundcorners_border(self, ctx):
self.create_roundcorners_subpath( self.create_roundcorners_subpath(ctx, 0.5, 0.5, self.width - 1, self.height - 1)
self.cr, 0.5, 0.5, self.width - 1, self.height - 1 ctx.set_source_rgba(0, 0, 0, 0.9)
) ctx.stroke()
self.cr.set_source_rgba(0, 0, 0, 0.9)
self.cr.stroke()
@staticmethod @staticmethod
def create_roundcorners_subpath(ctx, x, y, width, height): def create_roundcorners_subpath(ctx, x, y, width, height):
@ -106,11 +101,9 @@ class PiecesBar(DrawingArea):
ctx.arc(x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees) ctx.arc(x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees)
ctx.arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees) ctx.arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees)
ctx.close_path() ctx.close_path()
return ctx
def draw_pieces(self): def draw_pieces(self, ctx):
if not self.num_pieces: if not self.num_pieces:
# Nothing to draw.
return return
if ( if (
@ -122,7 +115,7 @@ class PiecesBar(DrawingArea):
self.pieces_overlay = cairo.ImageSurface( self.pieces_overlay = cairo.ImageSurface(
cairo.FORMAT_ARGB32, self.width, self.height cairo.FORMAT_ARGB32, self.width, self.height
) )
ctx = cairo.Context(self.pieces_overlay) pieces_ctx = cairo.Context(self.pieces_overlay)
if self.pieces: if self.pieces:
pieces = self.pieces pieces = self.pieces
@ -139,17 +132,16 @@ class PiecesBar(DrawingArea):
for state in COLOR_STATES for state in COLOR_STATES
] ]
for state in pieces: for state in pieces:
ctx.set_source_rgb(*pieces_colors[state]) pieces_ctx.set_source_rgb(*pieces_colors[state])
ctx.rectangle(start_pos, 0, piece_width, self.height) pieces_ctx.rectangle(start_pos, 0, piece_width, self.height)
ctx.fill() pieces_ctx.fill()
start_pos += piece_width start_pos += piece_width
self.cr.set_source_surface(self.pieces_overlay) ctx.set_source_surface(self.pieces_overlay)
self.cr.paint() ctx.paint()
def draw_progress_overlay(self): def draw_progress_overlay(self, ctx):
if not self.text: if not self.text:
# Nothing useful to draw, return now!
return return
if ( if (
@ -161,16 +153,15 @@ class PiecesBar(DrawingArea):
self.progress_overlay = cairo.ImageSurface( self.progress_overlay = cairo.ImageSurface(
cairo.FORMAT_ARGB32, self.width, self.height cairo.FORMAT_ARGB32, self.width, self.height
) )
ctx = cairo.Context(self.progress_overlay) progress_ctx = cairo.Context(self.progress_overlay)
ctx.set_source_rgba(0.1, 0.1, 0.1, 0.3) # Transparent progress_ctx.set_source_rgba(0.1, 0.1, 0.1, 0.3) # Transparent
ctx.rectangle(0, 0, self.width * self.fraction, self.height) progress_ctx.rectangle(0, 0, self.width * self.fraction, self.height)
ctx.fill() progress_ctx.fill()
self.cr.set_source_surface(self.progress_overlay) ctx.set_source_surface(self.progress_overlay)
self.cr.paint() ctx.paint()
def write_text(self): def write_text(self, ctx):
if not self.text: if not self.text:
# Nothing useful to draw, return now!
return return
if self.resized() or self.text != self.prev_text or self.text_overlay is None: if self.resized() or self.text != self.prev_text or self.text_overlay is None:
@ -178,8 +169,8 @@ class PiecesBar(DrawingArea):
self.text_overlay = cairo.ImageSurface( self.text_overlay = cairo.ImageSurface(
cairo.FORMAT_ARGB32, self.width, self.height cairo.FORMAT_ARGB32, self.width, self.height
) )
ctx = cairo.Context(self.text_overlay) text_ctx = cairo.Context(self.text_overlay)
pl = PangoCairo.create_layout(ctx) pl = PangoCairo.create_layout(text_ctx)
pl.set_font_description(self.text_font) pl.set_font_description(self.text_font)
pl.set_width(-1) # No text wrapping pl.set_width(-1) # No text wrapping
pl.set_text(self.text, -1) pl.set_text(self.text, -1)
@ -188,12 +179,14 @@ class PiecesBar(DrawingArea):
text_height = plsize[1] // SCALE text_height = plsize[1] // SCALE
area_width_without_text = self.width - text_width area_width_without_text = self.width - text_width
area_height_without_text = self.height - text_height area_height_without_text = self.height - text_height
ctx.move_to(area_width_without_text // 2, area_height_without_text // 2) text_ctx.move_to(
ctx.set_source_rgb(1, 1, 1) area_width_without_text // 2, area_height_without_text // 2
PangoCairo.update_layout(ctx, pl) )
PangoCairo.show_layout(ctx, pl) text_ctx.set_source_rgb(1, 1, 1)
self.cr.set_source_surface(self.text_overlay) PangoCairo.update_layout(text_ctx, pl)
self.cr.paint() PangoCairo.show_layout(text_ctx, pl)
ctx.set_source_surface(self.text_overlay)
ctx.paint()
def resized(self): def resized(self):
return self.prev_width != self.width or self.prev_height != self.height return self.prev_width != self.width or self.prev_height != self.height
@ -226,7 +219,6 @@ class PiecesBar(DrawingArea):
self.text = self.prev_text = '' self.text = self.prev_text = ''
self.fraction = self.prev_fraction = 0 self.fraction = self.prev_fraction = 0
self.progress_overlay = self.text_overlay = self.pieces_overlay = None self.progress_overlay = self.text_overlay = self.pieces_overlay = None
self.cr = None
self.update() self.update()
def update(self): def update(self):