Skip to content

Commit 51dd33b

Browse files
authored
Finetune behavior for bare canvas usage (#464)
* Finetune behavior for bare canvas usage * warn
1 parent a96079d commit 51dd33b

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

tests/test_gui_base.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"""
44

55
import gc
6+
import sys
7+
import subprocess
68

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

120122

123+
@mark.skipif(not can_use_wgpu_lib, reason="Needs wgpu lib")
124+
def test_run_bare_canvas():
125+
"""Test that a bare canvas does not error."""
126+
127+
# This is (more or less) the equivalent of:
128+
#
129+
# from wgpu.gui.auto import WgpuCanvas, run
130+
# canvas = WgpuCanvas()
131+
# run()
132+
#
133+
# Note: run() calls _draw_frame_and_present() in event loop.
134+
135+
canvas = MyOffscreenCanvas()
136+
canvas._draw_frame_and_present()
137+
138+
139+
def test_canvas_context_not_base():
140+
"""Check that it is prevented that canvas context is instance of base context class."""
141+
code = "from wgpu.gui import WgpuCanvasBase; canvas = WgpuCanvasBase(); canvas.get_context()"
142+
143+
result = subprocess.run(
144+
[sys.executable, "-c", code],
145+
stdout=subprocess.PIPE,
146+
stderr=subprocess.STDOUT,
147+
universal_newlines=True,
148+
)
149+
out = result.stdout.rstrip()
150+
151+
assert "RuntimeError" in out
152+
assert "backend must be selected" in out.lower()
153+
assert "canvas.get_context" in out.lower()
154+
155+
121156
@mark.skipif(not can_use_wgpu_lib, reason="Needs wgpu lib")
122157
def test_offscreen_canvas():
123158
canvas = MyOffscreenCanvas()

wgpu/backends/wgpu_native/_api.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,10 +650,9 @@ def _create_python_texture(self, texture_id):
650650

651651
def present(self):
652652
if not self._texture:
653-
msg = "present() is called without a preceeding call to "
654-
msg += "get_current_texture(). Note that present() is usually "
655-
msg += "called automatically after the draw function returns."
656-
raise RuntimeError(msg)
653+
# Log warning but don't raise exception
654+
msg = "No texture to present, missing call to get_current_texture()?"
655+
logger.warning(msg)
657656
else:
658657
# Present the texture, then destroy it
659658
# H: void f(WGPUSurface surface)

wgpu/gui/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,13 @@ def get_context(self, kind="webgpu"):
126126
if self._canvas_context is None:
127127
# Get the active wgpu backend module
128128
backend_module = sys.modules["wgpu"].gpu.__module__
129+
if backend_module == "wgpu._classes":
130+
raise RuntimeError(
131+
"A backend must be selected (e.g. with request_adapter()) before canvas.get_context() can be called."
132+
)
129133
# Instantiate the context
130-
PC = sys.modules[backend_module].GPUCanvasContext # noqa: N806
131-
self._canvas_context = PC(self)
134+
CC = sys.modules[backend_module].GPUCanvasContext # noqa: N806
135+
self._canvas_context = CC(self)
132136
return self._canvas_context
133137

134138

0 commit comments

Comments
 (0)