-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Support blitting in webagg backend #19059
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,7 +118,7 @@ def _handle_key(key): | |
|
||
|
||
class FigureCanvasWebAggCore(backend_agg.FigureCanvasAgg): | ||
supports_blit = False | ||
supports_blit = True | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
@@ -153,6 +153,10 @@ def draw(self): | |
finally: | ||
self.manager.refresh_all() # Swap the frames. | ||
|
||
def blit(self, bbox=None): | ||
self._png_is_old = True | ||
self.manager.refresh_all() | ||
|
||
def draw_idle(self): | ||
self.send_event("draw") | ||
|
||
|
@@ -189,18 +193,14 @@ def get_diff_image(self): | |
output = buff | ||
else: | ||
self.set_image_mode('diff') | ||
last_buffer = (np.frombuffer(self._last_renderer.buffer_rgba(), | ||
dtype=np.uint32) | ||
.reshape((renderer.height, renderer.width))) | ||
diff = buff != last_buffer | ||
diff = buff != self._last_buff | ||
output = np.where(diff, buff, 0) | ||
|
||
buf = BytesIO() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I notice that there are both Making it clearer now will make it easier for the next developer who looks at this to understand what's going on :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't left a comment because I'm not 100% sure this is the reason. But here's the idea of what I might leave: We differentiate between There may also be something happening with the save where the buffer gets converted from whatever the renderer uses into a png buffer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #19117. |
||
data = output.view(dtype=np.uint8).reshape((*output.shape, 4)) | ||
Image.fromarray(data).save(buf, format="png") | ||
# Swap the renderer frames | ||
self._renderer, self._last_renderer = ( | ||
self._last_renderer, renderer) | ||
# store the current buffer so we can compute the next diff | ||
np.copyto(self._last_buff, buff) | ||
self._force_full = False | ||
self._png_is_old = False | ||
return buf.getvalue() | ||
|
@@ -220,9 +220,10 @@ def get_renderer(self, cleared=None): | |
if need_new_renderer: | ||
self._renderer = backend_agg.RendererAgg( | ||
w, h, self.figure.dpi) | ||
self._last_renderer = backend_agg.RendererAgg( | ||
w, h, self.figure.dpi) | ||
self._lastKey = key | ||
self._last_buff = np.copy(np.frombuffer( | ||
self._renderer.buffer_rgba(), dtype=np.uint32 | ||
).reshape((self._renderer.height, self._renderer.width))) | ||
|
||
elif cleared: | ||
self._renderer.clear() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied this from #4290 so I assume it was done correctly by @tacaswell, but I find it odd that this accepts a
bbox
argument but doesn't do anything with it. Is that a mistake?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's to match the super class; it should blit only the section in the bbox if it's not
None
. Just re-blitting everything is fine, but perhaps a bit under-optimized. I think you would have to add to the current wire protocol to be able to do bbox'd blitting.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense. Should a docstring be added explaining something to that effect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I see how to implement more selective blitting for the ipympl frontend, but would/can the backend Agg renderer also needing blitting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other consideration is that if any part of the image is transparent it will be forced to be a full image. That could also be optimized a bit with frontend blitting. Discussed far in the past here: #5419 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sure it could be possible, but would require sending a different message probably. Doesn't have to be done in this PR though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed I'm happy with this PR as is. Better blitting seems like a good 2021 goal.