-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor inter-context-canvas api (#21)
- Loading branch information
1 parent
a8ddf97
commit 8ae174f
Showing
29 changed files
with
1,266 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Advanced | ||
======== | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
:caption: Contents: | ||
|
||
backendapi | ||
contextapi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
How context objects work | ||
======================== | ||
|
||
This page documents the working bentween the ``RenderCanvas`` and the context object. | ||
|
||
|
||
Introduction | ||
------------ | ||
|
||
The process of rendering to a canvas can be separated in two parts: *rendering* | ||
and *presenting*. The role of the context is to facilitate the rendering, and to | ||
then present the result to the screen. For this, the canvas provides one or more | ||
*present-methods*. Each canvas backend must provide at least the 'screen' or | ||
'bitmap' present-method. | ||
|
||
.. code-block:: | ||
Rendering Presenting | ||
┌─────────┐ ┌────────┐ | ||
│ │ ──screen──► │ │ | ||
──render──► | Context │ or │ Canvas │ | ||
│ │ ──bitmap──► │ │ | ||
└─────────┘ └────────┘ | ||
This means that for the context to be able to present to any canvas, it must | ||
support *both* the 'image' and 'screen' present-methods. If the context prefers | ||
presenting to the screen, and the canvas supports that, all is well. Similarly, | ||
if the context has a bitmap to present, and the canvas supports the | ||
bitmap-method, there's no problem. | ||
|
||
It get's a little trickier when there's a mismatch, but we can deal with these | ||
cases too. When the context prefers presenting to screen, the rendered result is | ||
probably a texture on the GPU. This texture must then be downloaded to a bitmap | ||
on the CPU. All GPU API's have ways to do this. | ||
|
||
.. code-block:: | ||
┌─────────┐ ┌────────┐ | ||
│ │ ──tex─┐ │ │ | ||
──render──► | Context │ | │ Canvas │ | ||
│ │ └─bitmap──► │ | | ||
└─────────┘ └────────┘ | ||
download from gpu to cpu | ||
If the context has a bitmap to present, and the canvas only supports presenting | ||
to screen, you can usse a small utility: the ``BitmapPresentAdapter`` takes a | ||
bitmap and presents it to the screen. | ||
|
||
.. code-block:: | ||
┌─────────┐ ┌────────┐ | ||
│ │ ┌─screen──► │ │ | ||
──render──► | Context │ │ │ Canvas │ | ||
│ │ ──bitmap─┘ │ | | ||
└─────────┘ └────────┘ | ||
use BitmapPresentAdapter | ||
This way, contexts can be made to work with all canvas backens. | ||
|
||
Canvases may also provide additionaly present-methods. If a context knows how to | ||
use that present-method, it can make use of it. Examples could be presenting | ||
diff images or video streams. | ||
|
||
.. code-block:: | ||
┌─────────┐ ┌────────┐ | ||
│ │ │ │ | ||
──render──► | Context │ ──special-present-method──► │ Canvas │ | ||
│ │ │ | | ||
└─────────┘ └────────┘ | ||
Context detection | ||
----------------- | ||
|
||
Anyone can make a context that works with ``rendercanvas``. In order for ``rendercanvas`` to find, it needs a little hook. | ||
|
||
.. autofunction:: rendercanvas._context.rendercanvas_context_hook | ||
:no-index: | ||
|
||
|
||
Context API | ||
----------- | ||
|
||
The class below describes the API and behavior that is expected of a context object. | ||
Also see https://github.com/pygfx/rendercanvas/blob/main/rendercanvas/_context.py. | ||
|
||
.. autoclass:: rendercanvas._context.ContextInterface | ||
:members: | ||
:no-index: | ||
|
||
|
||
Adapter | ||
------- | ||
|
||
.. autoclass:: rendercanvas.utils.bitmappresentadapter.BitmapPresentAdapter | ||
:members: | ||
:no-index: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,8 @@ Welcome to the rendercanvas docs! | |
start | ||
api | ||
backends | ||
backendapi | ||
utils | ||
advanced | ||
|
||
|
||
Indices and tables | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Utils | ||
===== | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
:caption: Contents: | ||
|
||
utils_cube | ||
utils_bitmappresentadapter.rst | ||
utils_bitmaprenderingcontext.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Bitmap present adapter | ||
====================== | ||
|
||
.. automodule:: rendercanvas.utils.bitmappresentadapter | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Bitmap rendering context | ||
======================== | ||
|
||
.. automodule:: rendercanvas.utils.bitmaprenderingcontext | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Code for wgpu cube example | ||
========================== | ||
|
||
.. automodule:: rendercanvas.utils.cube | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
Simple example that uses the bitmap-context to show images of noise. | ||
""" | ||
|
||
import numpy as np | ||
from rendercanvas.auto import RenderCanvas, loop | ||
|
||
|
||
canvas = RenderCanvas(update_mode="continuous") | ||
context = canvas.get_context("bitmap") | ||
|
||
|
||
@canvas.request_draw | ||
def animate(): | ||
w, h = canvas.get_logical_size() | ||
shape = int(h) // 4, int(w) // 4 | ||
|
||
bitmap = np.random.uniform(0, 255, shape).astype(np.uint8) | ||
context.set_bitmap(bitmap) | ||
|
||
|
||
loop.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
Simple snake game based on bitmap rendering. Work in progress. | ||
""" | ||
|
||
from collections import deque | ||
|
||
import numpy as np | ||
|
||
from rendercanvas.auto import RenderCanvas, loop | ||
|
||
|
||
canvas = RenderCanvas(present_method=None, update_mode="continuous") | ||
|
||
context = canvas.get_context("bitmap") | ||
|
||
world = np.zeros((120, 160), np.uint8) | ||
pos = [100, 100] | ||
direction = [1, 0] | ||
q = deque() | ||
|
||
|
||
@canvas.add_event_handler("key_down") | ||
def on_key(event): | ||
key = event["key"] | ||
if key == "ArrowLeft": | ||
direction[0] = -1 | ||
direction[1] = 0 | ||
elif key == "ArrowRight": | ||
direction[0] = 1 | ||
direction[1] = 0 | ||
elif key == "ArrowUp": | ||
direction[0] = 0 | ||
direction[1] = -1 | ||
elif key == "ArrowDown": | ||
direction[0] = 0 | ||
direction[1] = 1 | ||
|
||
|
||
@canvas.request_draw | ||
def animate(): | ||
pos[0] += direction[0] | ||
pos[1] += direction[1] | ||
|
||
if pos[0] < 0: | ||
pos[0] = world.shape[1] - 1 | ||
elif pos[0] >= world.shape[1]: | ||
pos[0] = 0 | ||
if pos[1] < 0: | ||
pos[1] = world.shape[0] - 1 | ||
elif pos[1] >= world.shape[0]: | ||
pos[1] = 0 | ||
|
||
q.append(tuple(pos)) | ||
world[pos[1], pos[0]] = 255 | ||
|
||
while len(q) > 20: | ||
old_pos = q.popleft() | ||
world[old_pos[1], old_pos[0]] = 0 | ||
|
||
context.set_bitmap(world) | ||
|
||
|
||
loop.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.