|
| 1 | +""" |
| 2 | +Support for rendering in a Jupyter widget. Provides a widget subclass that |
| 3 | +can be used as cell output, or embedded in a ipywidgets gui. |
| 4 | +""" |
| 5 | + |
| 6 | +from .offscreen import WgpuOffscreenCanvas |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +from jupyter_rfb import RemoteFrameBuffer |
| 10 | + |
| 11 | + |
| 12 | +class JupyterWgpuCanvas(WgpuOffscreenCanvas, RemoteFrameBuffer): |
| 13 | + """An ipywidgets widget providing a wgpu canvas. Needs the jupyter_rfb library.""" |
| 14 | + |
| 15 | + def __init__(self): |
| 16 | + super().__init__() |
| 17 | + self._pixel_ratio = 1 |
| 18 | + self._logical_size = 0, 0 |
| 19 | + self._is_closed = False |
| 20 | + |
| 21 | + # Implementation needed for RemoteFrameBuffer |
| 22 | + |
| 23 | + def handle_event(self, event): |
| 24 | + event_type = event.get("event_type", "") |
| 25 | + if event_type == "close": |
| 26 | + self._is_closed = True |
| 27 | + elif event_type == "resize": |
| 28 | + self._pixel_ratio = event["pixel_ratio"] |
| 29 | + self._logical_size = event["width"], event["height"] |
| 30 | + |
| 31 | + def get_frame(self): |
| 32 | + # The _draw_frame_and_present() does the drawing and then calls |
| 33 | + # present_context.present(), which calls our present() method. |
| 34 | + # The resuls is either a numpy array or None, and this matches |
| 35 | + # with what this method is expected to return. |
| 36 | + return self._draw_frame_and_present() |
| 37 | + |
| 38 | + # Implementation needed for WgpuCanvasBase |
| 39 | + |
| 40 | + def get_pixel_ratio(self): |
| 41 | + return self._pixel_ratio |
| 42 | + |
| 43 | + def get_logical_size(self): |
| 44 | + return self._logical_size |
| 45 | + |
| 46 | + def get_physical_size(self): |
| 47 | + return int(self._logical_size[0] * self._pixel_ratio), int( |
| 48 | + self._logical_size[1] * self._pixel_ratio |
| 49 | + ) |
| 50 | + |
| 51 | + def set_logical_size(self, width, height): |
| 52 | + self.css_width = f"{width}px" |
| 53 | + self.css_height = f"{height}px" |
| 54 | + |
| 55 | + def close(self): |
| 56 | + RemoteFrameBuffer.close(self) |
| 57 | + |
| 58 | + def is_closed(self): |
| 59 | + return self._is_closed |
| 60 | + |
| 61 | + def _request_draw(self): |
| 62 | + RemoteFrameBuffer.request_draw(self) |
| 63 | + |
| 64 | + # Implementation needed for WgpuOffscreenCanvas |
| 65 | + |
| 66 | + def present(self, texture_view): |
| 67 | + # This gets called at the end of a draw pass via GPUCanvasContextOffline |
| 68 | + device = texture_view._device |
| 69 | + size = texture_view.size |
| 70 | + bytes_per_pixel = 4 |
| 71 | + data = device.queue.read_texture( |
| 72 | + { |
| 73 | + "texture": texture_view.texture, |
| 74 | + "mip_level": 0, |
| 75 | + "origin": (0, 0, 0), |
| 76 | + }, |
| 77 | + { |
| 78 | + "offset": 0, |
| 79 | + "bytes_per_row": bytes_per_pixel * size[0], |
| 80 | + "rows_per_image": size[1], |
| 81 | + }, |
| 82 | + size, |
| 83 | + ) |
| 84 | + return np.frombuffer(data, np.uint8).reshape(size[1], size[0], 4) |
| 85 | + |
| 86 | + def get_preferred_format(self): |
| 87 | + # Use a format that maps well to PNG: rgba8norm. |
| 88 | + # Use srgb for perseptive color mapping. You probably want to |
| 89 | + # apply this before displaying on screen, but you do not want |
| 90 | + # to duplicate it. When a PNG is shown on screen in the browser |
| 91 | + # it's shown as-is (at least it was when I just tried). |
| 92 | + return "rgba8unorm-srgb" |
| 93 | + |
| 94 | + |
| 95 | +# Make available under a name that is the same for all gui backends |
| 96 | +WgpuCanvas = JupyterWgpuCanvas |
0 commit comments