Skip to content

Commit f6a0472

Browse files
authored
Implement own context classes (#127)
1 parent 05e22df commit f6a0472

28 files changed

+874
-524
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ jobs:
125125
"
126126
pytest -v tests
127127
128-
test-examples-build:
128+
test-examples:
129129
name: Test examples ${{ matrix.pyversion }}
130130
runs-on: ${{ matrix.os }}
131131
strategy:
@@ -183,8 +183,8 @@ jobs:
183183
pushd $HOME
184184
pytest -v --pyargs rendercanvas.__pyinstaller
185185
186-
release:
187-
name: Build release on ubuntu-latest
186+
build-release:
187+
name: Build release artifacts
188188
runs-on: ubuntu-latest
189189
strategy:
190190
fail-fast: false
@@ -227,7 +227,7 @@ jobs:
227227
publish:
228228
name: Publish release to Github and Pypi
229229
runs-on: ubuntu-latest
230-
needs: [tests, release]
230+
needs: [tests, build-release]
231231
if: success() && startsWith(github.ref, 'refs/tags/v')
232232
environment:
233233
name: pypi

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import numpy as np
6161
from rendercanvas.auto import RenderCanvas, loop
6262

6363
canvas = RenderCanvas(update_mode="continuous")
64-
context = canvas.get_context("bitmap")
64+
context = canvas.get_bitmap_context()
6565

6666
@canvas.request_draw
6767
def animate():

docs/api.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
API
2-
===
1+
Canvas API
2+
==========
33

44
These are the classes that make up the rendercanvas API:
55

docs/conf.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
# Load wgpu so autodoc can query docstrings
2626
import rendercanvas # noqa: E402
2727
import rendercanvas.stub # noqa: E402 - we use the stub backend to generate docs
28-
import rendercanvas._context # noqa: E402 - we use the ContextInterface to generate docs
29-
import rendercanvas.utils.bitmappresentadapter # noqa: E402
28+
3029

3130
# -- Project information -----------------------------------------------------
3231

docs/contextapi.rst

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
How context objects work
22
========================
33

4-
This page documents the working bentween the ``RenderCanvas`` and the context object.
4+
This page documents the inner working between the ``RenderCanvas`` and the context object.
55

66

77
Introduction
@@ -23,16 +23,13 @@ then present the result to the screen. For this, the canvas provides one or more
2323
│ │ ──bitmap──► │ │
2424
└─────────┘ └────────┘
2525
26-
This means that for the context to be able to present to any canvas, it must
27-
support *both* the 'bitmap' and 'screen' present-methods. If the context prefers
28-
presenting to the screen, and the canvas supports that, all is well. Similarly,
29-
if the context has a bitmap to present, and the canvas supports the
30-
bitmap-method, there's no problem.
26+
If the context is a ``BitmapContext``, and the canvas supports the bitmap present-method,
27+
things are easy. Similarly, if the context is a ``WgpuContext``, and the canvas
28+
supports the screen present-method, the presenting is simply delegated to wgpu.
3129

32-
It get's a little trickier when there's a mismatch, but we can deal with these
33-
cases too. When the context prefers presenting to screen, the rendered result is
34-
probably a texture on the GPU. This texture must then be downloaded to a bitmap
35-
on the CPU. All GPU API's have ways to do this.
30+
When there's a mismatch, we use different context sub-classes that handle the conversion.
31+
With the ``WgpuContextToBitmap`` context, the rendered result is inside a texture on the GPU.
32+
This texture is then downloaded to a bitmap on the CPU that can be passed to the canvas.
3633

3734
.. code-block::
3835
@@ -41,11 +38,10 @@ on the CPU. All GPU API's have ways to do this.
4138
──render──► | Context │ | │ Canvas │
4239
│ │ └─bitmap──► │ |
4340
└─────────┘ └────────┘
44-
download from gpu to cpu
41+
download to CPU
4542
46-
If the context has a bitmap to present, and the canvas only supports presenting
47-
to screen, you can use a small utility: the ``BitmapPresentAdapter`` takes a
48-
bitmap and presents it to the screen.
43+
With the ``BitmapContextToScreen`` context, the bitmap is uploaded to a GPU texture,
44+
which is then rendered to screen using the lower-level canvas-context from ``wgpu``.
4945

5046
.. code-block::
5147
@@ -54,46 +50,6 @@ bitmap and presents it to the screen.
5450
──render──► | Context │ │ │ Canvas │
5551
│ │ ──bitmap─┘ │ |
5652
└─────────┘ └────────┘
57-
use BitmapPresentAdapter
53+
upload to GPU
5854
5955
This way, contexts can be made to work with all canvas backens.
60-
61-
Canvases may also provide additionally present-methods. If a context knows how to
62-
use that present-method, it can make use of it. Examples could be presenting
63-
diff images or video streams.
64-
65-
.. code-block::
66-
67-
┌─────────┐ ┌────────┐
68-
│ │ │ │
69-
──render──► | Context │ ──special-present-method──► │ Canvas │
70-
│ │ │ |
71-
└─────────┘ └────────┘
72-
73-
74-
Context detection
75-
-----------------
76-
77-
Anyone can make a context that works with ``rendercanvas``. In order for ``rendercanvas`` to find, it needs a little hook.
78-
79-
.. autofunction:: rendercanvas._context.rendercanvas_context_hook
80-
:no-index:
81-
82-
83-
Context API
84-
-----------
85-
86-
The class below describes the API and behavior that is expected of a context object.
87-
Also see https://github.com/pygfx/rendercanvas/blob/main/rendercanvas/_context.py.
88-
89-
.. autoclass:: rendercanvas._context.ContextInterface
90-
:members:
91-
:no-index:
92-
93-
94-
Adapter
95-
-------
96-
97-
.. autoclass:: rendercanvas.utils.bitmappresentadapter.BitmapPresentAdapter
98-
:members:
99-
:no-index:

docs/contexts.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Context API
2+
===========
3+
4+
.. automodule:: rendercanvas.contexts
5+
6+
.. autoclass:: rendercanvas.contexts.BaseContext
7+
:members:
8+
9+
.. autoclass:: rendercanvas.contexts.BitmapContext
10+
:members:
11+
12+
.. autoclass:: rendercanvas.contexts.WgpuContext
13+
:members:

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Welcome to the rendercanvas docs!
99

1010
start
1111
api
12+
contexts
1213
backends
1314
utils
1415
Gallery <gallery/index.rst>

docs/start.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Rendering using bitmaps:
5050

5151
.. code-block:: py
5252
53-
context = canvas.get_context("bitmap")
53+
context = canvas.get_bitmap_context()
5454
5555
@canvas.request_draw
5656
def animate():
@@ -61,7 +61,7 @@ Rendering with wgpu:
6161

6262
.. code-block:: py
6363
64-
context = canvas.get_context("wgpu")
64+
context = canvas.get_wgpu_context()
6565
context.configure(device)
6666
6767
@canvas.request_draw

docs/utils.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,3 @@ Utils
77

88
utils_asyncs
99
utils_cube
10-
utils_bitmappresentadapter.rst
11-
utils_bitmaprenderingcontext.rst

docs/utils_bitmappresentadapter.rst

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)