Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Backport PR #21294 on branch v3.5.x (Disable blitting on GTK4 backends) #21304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/matplotlib/backends/backend_gtk4.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def _mpl_to_gtk_cursor(mpl_cursor):

class FigureCanvasGTK4(Gtk.DrawingArea, FigureCanvasBase):
required_interactive_framework = "gtk4"
supports_blit = False
_timer_cls = TimerGTK4
_context_is_scaled = False

Expand Down
56 changes: 12 additions & 44 deletions lib/matplotlib/backends/backend_gtk4agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,35 @@
from . import backend_agg, backend_gtk4
from .backend_cairo import cairo
from .backend_gtk4 import Gtk, _BackendGTK4
from matplotlib import transforms


class FigureCanvasGTK4Agg(backend_gtk4.FigureCanvasGTK4,
backend_agg.FigureCanvasAgg):
def __init__(self, figure):
backend_gtk4.FigureCanvasGTK4.__init__(self, figure)
self._bbox_queue = []

def on_draw_event(self, widget, ctx):
scale = self.device_pixel_ratio
allocation = self.get_allocation()
w = allocation.width * scale
h = allocation.height * scale

if not len(self._bbox_queue):
Gtk.render_background(
self.get_style_context(), ctx,
allocation.x, allocation.y,
allocation.width, allocation.height)
bbox_queue = [transforms.Bbox([[0, 0], [w, h]])]
else:
bbox_queue = self._bbox_queue
Gtk.render_background(
self.get_style_context(), ctx,
allocation.x, allocation.y,
allocation.width, allocation.height)

ctx = backend_cairo._to_context(ctx)

for bbox in bbox_queue:
x = int(bbox.x0)
y = h - int(bbox.y1)
width = int(bbox.x1) - int(bbox.x0)
height = int(bbox.y1) - int(bbox.y0)

buf = cbook._unmultiplied_rgba8888_to_premultiplied_argb32(
np.asarray(self.copy_from_bbox(bbox)))
image = cairo.ImageSurface.create_for_data(
buf.ravel().data, cairo.FORMAT_ARGB32, width, height)
image.set_device_scale(scale, scale)
ctx.set_source_surface(image, x / scale, y / scale)
ctx.paint()

if len(self._bbox_queue):
self._bbox_queue = []
buf = cbook._unmultiplied_rgba8888_to_premultiplied_argb32(
np.asarray(self.renderer.buffer_rgba()))
height, width, _ = buf.shape
image = cairo.ImageSurface.create_for_data(
buf.ravel().data, cairo.FORMAT_ARGB32, width, height)
image.set_device_scale(scale, scale)
ctx.set_source_surface(image, 0, 0)
ctx.paint()

return False

def blit(self, bbox=None):
# If bbox is None, blit the entire canvas to gtk. Otherwise
# blit only the area defined by the bbox.
if bbox is None:
bbox = self.figure.bbox

scale = self.device_pixel_ratio
allocation = self.get_allocation()
x = int(bbox.x0 / scale)
y = allocation.height - int(bbox.y1 / scale)
width = (int(bbox.x1) - int(bbox.x0)) // scale
height = (int(bbox.y1) - int(bbox.y0)) // scale

self._bbox_queue.append(bbox)
self.queue_draw_area(x, y, width, height)

def draw(self):
backend_agg.FigureCanvasAgg.draw(self)
super().draw()
Expand Down