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

Skip to content

Commit 4264233

Browse files
committed
FIX: Update blitting and drawing on the macosx backend
This inherits some of the drawing and blitting code from the AGG canvas and leverages that to handle when a full AGG re-render should happen. The macosx code always grabs the full AGG buffer when doing the draw to the GUI window.
1 parent 940cc2c commit 4264233

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

lib/matplotlib/backends/backend_macosx.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,37 @@ def __init__(self, figure):
3030
FigureCanvasBase.__init__(self, figure)
3131
width, height = self.get_width_height()
3232
_macosx.FigureCanvas.__init__(self, width, height)
33+
self._draw_pending = False
34+
self._is_drawing = False
3335

3436
def set_cursor(self, cursor):
3537
# docstring inherited
3638
_macosx.set_cursor(cursor)
3739

38-
def _draw(self):
39-
renderer = self.get_renderer()
40-
if self.figure.stale:
41-
renderer.clear()
42-
self.figure.draw(renderer)
43-
return renderer
44-
4540
def draw(self):
46-
# docstring inherited
47-
self._draw()
48-
self.flush_events()
41+
"""Render the figure and update the macosx canvas."""
42+
# The renderer draw is done here; delaying causes problems with code
43+
# that uses the result of the draw() to update plot elements.
44+
if self._is_drawing:
45+
return
46+
with cbook._setattr_cm(self, _is_drawing=True):
47+
super().draw()
48+
self.update()
4949

50-
# draw_idle is provided by _macosx.FigureCanvas
50+
def draw_idle(self):
51+
# docstring inherited
52+
if (getattr(self, '_draw_pending', False) or
53+
getattr(self, '_is_drawing', False)):
54+
return
55+
self._draw_pending = True
56+
with self._idle_draw_cntx():
57+
self._draw_pending = False
58+
self.draw()
5159

5260
def blit(self, bbox=None):
53-
self.draw_idle()
61+
# docstring inherited
62+
super().blit(bbox)
63+
self.update()
5464

5565
def resize(self, width, height):
5666
# Size from macOS is logical pixels, dpi is physical.

src/_macosx.m

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
345345
}
346346

347347
static PyObject*
348-
FigureCanvas_draw(FigureCanvas* self)
349-
{
350-
[self->view display];
351-
Py_RETURN_NONE;
352-
}
353-
354-
static PyObject*
355-
FigureCanvas_draw_idle(FigureCanvas* self)
348+
FigureCanvas_update(FigureCanvas* self)
356349
{
357350
[self->view setNeedsDisplay: YES];
358351
Py_RETURN_NONE;
@@ -485,12 +478,8 @@ static CGFloat _get_device_scale(CGContextRef cr)
485478
.tp_new = (newfunc)FigureCanvas_new,
486479
.tp_doc = "A FigureCanvas object wraps a Cocoa NSView object.",
487480
.tp_methods = (PyMethodDef[]){
488-
{"draw",
489-
(PyCFunction)FigureCanvas_draw,
490-
METH_NOARGS,
491-
NULL}, // docstring inherited
492-
{"draw_idle",
493-
(PyCFunction)FigureCanvas_draw_idle,
481+
{"update",
482+
(PyCFunction)FigureCanvas_update,
494483
METH_NOARGS,
495484
NULL}, // docstring inherited
496485
{"flush_events",
@@ -1263,7 +1252,7 @@ -(void)drawRect:(NSRect)rect
12631252

12641253
CGContextRef cr = [[NSGraphicsContext currentContext] CGContext];
12651254

1266-
if (!(renderer = PyObject_CallMethod(canvas, "_draw", ""))
1255+
if (!(renderer = PyObject_CallMethod(canvas, "get_renderer", ""))
12671256
|| !(renderer_buffer = PyObject_GetAttrString(renderer, "_renderer"))) {
12681257
PyErr_Print();
12691258
goto exit;

0 commit comments

Comments
 (0)