Skip to content

Commit

Permalink
Finetune behavior for bare canvas usage (#464)
Browse files Browse the repository at this point in the history
* Finetune behavior for bare canvas usage

* warn
  • Loading branch information
almarklein authored Feb 13, 2024
1 parent a96079d commit 51dd33b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
35 changes: 35 additions & 0 deletions tests/test_gui_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""

import gc
import sys
import subprocess

import numpy as np
import wgpu.gui # noqa
Expand Down Expand Up @@ -118,6 +120,39 @@ def present(self, texture):
self.array = np.frombuffer(data, np.uint8).reshape(size[1], size[0], 4)


@mark.skipif(not can_use_wgpu_lib, reason="Needs wgpu lib")
def test_run_bare_canvas():
"""Test that a bare canvas does not error."""

# This is (more or less) the equivalent of:
#
# from wgpu.gui.auto import WgpuCanvas, run
# canvas = WgpuCanvas()
# run()
#
# Note: run() calls _draw_frame_and_present() in event loop.

canvas = MyOffscreenCanvas()
canvas._draw_frame_and_present()


def test_canvas_context_not_base():
"""Check that it is prevented that canvas context is instance of base context class."""
code = "from wgpu.gui import WgpuCanvasBase; canvas = WgpuCanvasBase(); canvas.get_context()"

result = subprocess.run(
[sys.executable, "-c", code],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
)
out = result.stdout.rstrip()

assert "RuntimeError" in out
assert "backend must be selected" in out.lower()
assert "canvas.get_context" in out.lower()


@mark.skipif(not can_use_wgpu_lib, reason="Needs wgpu lib")
def test_offscreen_canvas():
canvas = MyOffscreenCanvas()
Expand Down
7 changes: 3 additions & 4 deletions wgpu/backends/wgpu_native/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,9 @@ def _create_python_texture(self, texture_id):

def present(self):
if not self._texture:
msg = "present() is called without a preceeding call to "
msg += "get_current_texture(). Note that present() is usually "
msg += "called automatically after the draw function returns."
raise RuntimeError(msg)
# Log warning but don't raise exception
msg = "No texture to present, missing call to get_current_texture()?"
logger.warning(msg)
else:
# Present the texture, then destroy it
# H: void f(WGPUSurface surface)
Expand Down
8 changes: 6 additions & 2 deletions wgpu/gui/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ def get_context(self, kind="webgpu"):
if self._canvas_context is None:
# Get the active wgpu backend module
backend_module = sys.modules["wgpu"].gpu.__module__
if backend_module == "wgpu._classes":
raise RuntimeError(
"A backend must be selected (e.g. with request_adapter()) before canvas.get_context() can be called."
)
# Instantiate the context
PC = sys.modules[backend_module].GPUCanvasContext # noqa: N806
self._canvas_context = PC(self)
CC = sys.modules[backend_module].GPUCanvasContext # noqa: N806
self._canvas_context = CC(self)
return self._canvas_context


Expand Down

0 comments on commit 51dd33b

Please sign in to comment.