Skip to content

Commit e2610b6

Browse files
chore: Move streamed-render protocols out of server_renderer (#232)
Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
1 parent a0f6400 commit e2610b6

8 files changed

Lines changed: 129 additions & 42 deletions

File tree

‎doc/changelog.d/232.maintenance.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Move streamed-render protocols out of server_renderer

‎src/ansys/visor/viewer/app/trame/server_loop_bridge.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from trame_rca.view_adapter import RcaViewAdapter
1616

1717
from ansys.visor.viewer.app.trame.async_runner import AsyncRunner
18-
from ansys.visor.viewer.renderer.server_renderer import FrameRequester
18+
from ansys.visor.viewer.renderer.streamed_protocols import FrameRequester
1919

2020

2121
class ServerLoopBridge(FrameRequester):

‎src/ansys/visor/viewer/app/trame/server_state_publisher.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from ansys.visor.viewer.app.trame.async_runner import AsyncRunner
1717
from ansys.visor.viewer.models.common.visor_camera_state import VisorCameraState
1818
from ansys.visor.viewer.models.common.visor_cross_section_state import VisorCrossSectionState
19-
from ansys.visor.viewer.renderer.server_renderer import ViewRecordPublisher
19+
from ansys.visor.viewer.renderer.streamed_protocols import ViewRecordPublisher
2020

2121
SERVER_CAMERA_STATE_KEY: str = "server_camera_state"
2222
"""State key carrying the server's camera record, as its camelCase fields, or None."""

‎src/ansys/visor/viewer/renderer/server_renderer.py‎

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,18 @@
1313

1414
from __future__ import annotations
1515

16-
from typing import Protocol
17-
1816
# Registers the OpenGL2 factory overrides, so vtkRenderWindow() is a
1917
# concrete off-screen-capable window rather than the abstract base.
2018
import vtkmodules.vtkRenderingOpenGL2 # noqa: F401
2119
from vtkmodules.vtkCommonCore import vtkObject
2220
from vtkmodules.vtkRenderingCore import vtkRenderWindow
2321

24-
from ansys.visor.viewer.models.common.visor_camera_state import VisorCameraState
25-
from ansys.visor.viewer.models.common.visor_cross_section_state import VisorCrossSectionState
2622
from ansys.visor.viewer.renderer.pipeline_renderer import VtkPipelineRenderer
27-
28-
INITIAL_WINDOW_SIZE: tuple[int, int] = (300, 300)
29-
"""Window size before the client reports its canvas; trame-rca's pre-resize image size."""
30-
31-
32-
class FrameRequester(Protocol):
33-
"""Where a ServerRenderer asks for a streamed frame."""
34-
35-
def request_frame(self) -> None:
36-
"""Ask for a streamed frame; callable from any thread."""
37-
38-
39-
class ViewRecordPublisher(Protocol):
40-
"""Where a ServerRenderer publishes its camera and cross-section plane records."""
41-
42-
def publish_camera(self, camera_state: VisorCameraState | None) -> None:
43-
"""Publish the camera record to clients, or None when there is none; callable from any thread."""
44-
45-
def publish_cross_section(self, cross_section_state: VisorCrossSectionState | None) -> None:
46-
"""Publish the plane record to clients, or None when there is none; callable from any thread."""
23+
from ansys.visor.viewer.renderer.streamed_protocols import (
24+
INITIAL_WINDOW_SIZE,
25+
FrameRequester,
26+
ViewRecordPublisher,
27+
)
4728

4829

4930
class ServerRenderer(VtkPipelineRenderer):

‎src/ansys/visor/viewer/renderer/server_window.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
VisorRcaWindow
33
==============
4-
trame-rca remote-controlled area over a :class:`ServerRenderer`.
4+
trame-rca remote-controlled area over an :class:`RcaRenderTarget`.
55
66
trame-rca's scheduler reads :attr:`VisorRcaWindow.img_cols_rows` on the event
77
loop thread, and its resize and interaction RPCs arrive on the same thread.
@@ -25,7 +25,7 @@
2525
from trame_rca.rca.protocol import RemoteControlledAreaProtocol
2626

2727
from ansys.visor.viewer.renderer.gl_context_lock import SceneLock
28-
from ansys.visor.viewer.renderer.server_renderer import INITIAL_WINDOW_SIZE, ServerRenderer
28+
from ansys.visor.viewer.renderer.streamed_protocols import INITIAL_WINDOW_SIZE, RcaRenderTarget
2929

3030
GESTURE_END_EVENT_TYPES: frozenset[str] = frozenset(
3131
{"LeftButtonRelease", "MiddleButtonRelease", "RightButtonRelease", "MouseWheel"}
@@ -38,10 +38,10 @@
3838

3939

4040
class VisorRcaWindow:
41-
"""trame-rca area over a ServerRenderer that touches VTK only while holding the scene lock."""
41+
"""trame-rca area over an RcaRenderTarget that touches VTK only while holding the scene lock."""
4242

4343
_capture: RemoteControlledAreaProtocol
44-
_renderer: ServerRenderer
44+
_renderer: RcaRenderTarget
4545
_vtk_lock: SceneLock
4646
_reschedule: Callable[[], None]
4747
_pending_size: tuple[int, int] | None
@@ -53,7 +53,7 @@ class VisorRcaWindow:
5353
def __init__(
5454
self,
5555
capture: RemoteControlledAreaProtocol,
56-
renderer: ServerRenderer,
56+
renderer: RcaRenderTarget,
5757
vtk_lock: SceneLock,
5858
reschedule: Callable[[], None],
5959
) -> None:
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
Streamed-render protocols
3+
=========================
4+
The names a streamed view's renderer, window, loop bridge and state publisher
5+
share, kept apart from :mod:`ansys.visor.viewer.renderer.server_renderer` so
6+
that importing them does not import the off-screen VTK pipeline.
7+
"""
8+
9+
from __future__ import annotations
10+
11+
from typing import TYPE_CHECKING, Protocol
12+
13+
from ansys.visor.viewer.models.common.visor_camera_state import VisorCameraState
14+
from ansys.visor.viewer.models.common.visor_cross_section_state import VisorCrossSectionState
15+
16+
if TYPE_CHECKING:
17+
from vtkmodules.vtkRenderingCore import vtkRenderWindow
18+
19+
INITIAL_WINDOW_SIZE: tuple[int, int] = (300, 300)
20+
"""Window size before the client reports its canvas; trame-rca's pre-resize image size."""
21+
22+
BACKGROUND_RGB: dict[bool, tuple[float, float, float]] = {
23+
False: (247 / 255, 247 / 255, 247 / 255),
24+
True: (54 / 255, 60 / 255, 67 / 255),
25+
}
26+
"""Viewer background per theme, keyed on dark.
27+
28+
The values of ``--visor-viewer-background`` in the client's
29+
``public/css/theme-light.css`` and ``public/css/theme-dark.css``.
30+
"""
31+
32+
33+
class FrameRequester(Protocol):
34+
"""Where a ServerRenderer asks for a streamed frame."""
35+
36+
def request_frame(self) -> None:
37+
"""Ask for a streamed frame; callable from any thread."""
38+
39+
40+
class ViewRecordPublisher(Protocol):
41+
"""Where a ServerRenderer publishes its camera and cross-section plane records."""
42+
43+
def publish_camera(self, camera_state: VisorCameraState | None) -> None:
44+
"""Publish the camera record to clients, or None when there is none; callable from any thread."""
45+
46+
def publish_cross_section(self, cross_section_state: VisorCrossSectionState | None) -> None:
47+
"""Publish the plane record to clients, or None when there is none; callable from any thread."""
48+
49+
50+
class RcaRenderTarget(Protocol):
51+
"""The renderer surface VisorRcaWindow captures from and resizes."""
52+
53+
@property
54+
def render_window(self) -> vtkRenderWindow:
55+
"""The window the capture reads and releases."""
56+
57+
@property
58+
def background_rgb(self) -> tuple[float, float, float]:
59+
"""The colour the placeholder frame is filled with."""
60+
61+
def set_viewport_size(self, width: int, height: int) -> None:
62+
"""Resize to the client canvas in device pixels."""
63+
64+
def set_pixel_scale(self, pixel_scale: float) -> None:
65+
"""Take the client's device pixels per CSS pixel."""
66+
67+
def record_pipeline_camera(self) -> None:
68+
"""Record and publish the camera after a gesture; caller holds the lock."""
69+
70+
def record_cross_section_plane(self) -> None:
71+
"""Record and publish a dragged plane after a gesture; caller holds the lock."""

‎src/ansys/visor/viewer/vtk/scene/server_scene.py‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,14 @@
1919
from ansys.visor.viewer.renderer.gl_context_lock import GlContextLock
2020
from ansys.visor.viewer.renderer.server_renderer import ServerRenderer
2121
from ansys.visor.viewer.renderer.server_window import VisorRcaWindow
22+
from ansys.visor.viewer.renderer.streamed_protocols import BACKGROUND_RGB
2223
from ansys.visor.viewer.vtk.scene.base import VisorSceneBase
2324

2425
if TYPE_CHECKING:
2526
from ansys.visor.viewer.app.trame.server_loop_bridge import ServerLoopBridge
2627
from ansys.visor.viewer.models.runtime.picking.geometry_pick import GeometryPick
2728
from ansys.visor.viewer.models.runtime.scene.runtime_app_state import RuntimeAppState
28-
from ansys.visor.viewer.renderer.server_renderer import ViewRecordPublisher
29-
30-
BACKGROUND_RGB: dict[bool, tuple[float, float, float]] = {
31-
False: (247 / 255, 247 / 255, 247 / 255),
32-
True: (54 / 255, 60 / 255, 67 / 255),
33-
}
34-
"""Viewer background per theme, keyed on dark.
35-
36-
The values of ``--visor-viewer-background`` in the client's
37-
``public/css/theme-light.css`` and ``public/css/theme-dark.css``.
38-
"""
29+
from ansys.visor.viewer.renderer.streamed_protocols import ViewRecordPublisher
3930

4031

4132
class VisorServerScene(VisorSceneBase):
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Unit tests for what the streamed modules load on import.
3+
4+
A fresh interpreter imports the trame app and the state publisher a streamed
5+
session uses, then reports every loaded module name. Neither may pull in the
6+
off-screen VTK pipeline, nor trame-vtklocal through it.
7+
"""
8+
9+
from __future__ import annotations
10+
11+
import json
12+
import subprocess
13+
import sys
14+
15+
import pytest
16+
17+
_IMPORT_SCRIPT = (
18+
"import json, sys\n"
19+
"import ansys.visor.viewer.app.trame.server_app\n"
20+
"import ansys.visor.viewer.app.trame.server_state_publisher\n"
21+
"print(json.dumps(sorted(sys.modules)))\n"
22+
)
23+
24+
25+
@pytest.fixture(scope="module")
26+
def loaded_modules() -> set[str]:
27+
"""Module names loaded by a fresh interpreter after importing the streamed modules."""
28+
result = subprocess.run(
29+
[sys.executable, "-c", _IMPORT_SCRIPT],
30+
capture_output=True,
31+
text=True,
32+
check=True,
33+
)
34+
# Settings loading may print first; the module list is the last line.
35+
return set(json.loads(result.stdout.strip().splitlines()[-1]))
36+
37+
38+
def test_streamed_imports_leave_server_renderer_unloaded(loaded_modules: set[str]) -> None:
39+
assert "ansys.visor.viewer.renderer.server_renderer" not in loaded_modules
40+
41+
42+
def test_streamed_imports_leave_trame_vtklocal_unloaded(loaded_modules: set[str]) -> None:
43+
assert "trame_vtklocal" not in loaded_modules

0 commit comments

Comments
 (0)