|
1 | | -""" |
2 | | -Provide a simple context class to support ``canvas.get_context('bitmap')``. |
3 | | -""" |
4 | | - |
5 | 1 | import sys |
6 | 2 |
|
7 | 3 | from .basecontext import BaseContext |
8 | 4 |
|
| 5 | +__all__ = ["BitmapContext", "BitmapContextPlain", "BitmapContextToWgpu"] |
| 6 | + |
9 | 7 |
|
10 | 8 | class BitmapContext(BaseContext): |
11 | | - """A context that supports rendering by generating grayscale or rgba images. |
| 9 | + """A context that provides an API that takes a (grayscale or rgba) images bitmap. |
| 10 | +
|
| 11 | + This is loosely inspired by JS' ``ImageBitmapRenderingContext``. Rendering |
| 12 | + bitmaps is a simple way to use ``rendercanvas``, but usually not as |
| 13 | + performant as a wgpu context. |
12 | 14 |
|
13 | | - This is inspired by JS ``get_context('bitmaprenderer')`` which returns a ``ImageBitmapRenderingContext``. |
14 | | - It is a relatively simple context to implement, and provides a easy entry to using ``rendercanvas``. |
| 15 | + Users typically don't instantiate contexts directly, but use ``canvas.get_context("bitmap")``, |
| 16 | + which returns a subclass of this class, depending on the needs of the canvas. |
15 | 17 | """ |
16 | 18 |
|
17 | 19 | def __new__(cls, canvas: object, present_info: dict): |
| 20 | + # Instantiating this class actually produces a subclass |
18 | 21 | present_method = present_info["method"] |
19 | 22 | if present_method == "bitmap": |
20 | | - return super().__new__(BitmapToBitmapContext) |
| 23 | + return super().__new__(BitmapContextPlain) |
21 | 24 | elif present_method == "wgpu": |
22 | | - return super().__new__(BitmapToWgpuContext) |
| 25 | + return super().__new__(BitmapContextToWgpu) |
23 | 26 | else: |
24 | 27 | raise TypeError("Unexpected present_method {present_method!r}") |
25 | 28 |
|
@@ -66,8 +69,8 @@ def set_bitmap(self, bitmap): |
66 | 69 | self._bitmap_and_format = m, format |
67 | 70 |
|
68 | 71 |
|
69 | | -class BitmapToBitmapContext(BitmapContext): |
70 | | - """A BitmapContext that presents a bitmap to the canvas.""" |
| 72 | +class BitmapContextPlain(BitmapContext): |
| 73 | + """A BitmapContext that just presents the bitmap to the canvas.""" |
71 | 74 |
|
72 | 75 | def __init__(self, canvas, present_info): |
73 | 76 | super().__init__(canvas, present_info) |
@@ -97,11 +100,10 @@ def _rc_present(self): |
97 | 100 | } |
98 | 101 |
|
99 | 102 |
|
100 | | -class BitmapToWgpuContext(BitmapContext): |
101 | | - """A BitmapContext that presents via a wgpu.GPUCanvasContext. |
| 103 | +class BitmapContextToWgpu(BitmapContext): |
| 104 | + """A BitmapContext that uploads to a texture and present that to a ``wgpu.GPUCanvasContext``. |
102 | 105 |
|
103 | | - This adapter can be used by context objects that want to present a bitmap, when the |
104 | | - canvas only supports presenting to screen. |
| 106 | + This is uses for canvases that do not support presenting a bitmap. |
105 | 107 | """ |
106 | 108 |
|
107 | 109 | def __init__(self, canvas, present_info): |
|
0 commit comments