Video rendering surface
The video rendering surface connects the playback engine to the application's player UI. It gives the engine a native composition target for video frames, keeps that target aligned with the view, and places a separate interaction layer over the rendered image.
Rendering surface
The Screenbox.VideoView/VideoView.cs control is the UI-facing video surface. It extends a XAML SwapChainPanel so the playback engine can present frames through a Direct3D swap chain instead of through a normal XAML image element.
The control owns the link between the panel and the graphics resources. It creates the target when the panel has a valid size, attaches it to the panel, and reports the native target settings through its Initialized event. This keeps the rendering contract between the UI and playback engine in one place.
References: Screenbox.VideoView/VideoView.cs• L21-47 Screenbox.VideoView/VideoView.cs• L107-187
Swap-chain attachment
The swap-chain attachment gives the XAML panel a native presentation target. Screenbox.VideoView/SwapChainPanelExtensions.cs bridges the managed panel to its native Windows interface so the current swap chain can be set or cleared during the surface lifecycle.
The surface first tries the hardware graphics path. It can fall back to software rendering when hardware setup fails, which lets the player continue to create a rendering target on systems with an unavailable or unstable graphics device.
References: Screenbox.VideoView/SwapChainPanelExtensions.cs• L8-36 Screenbox.VideoView/VideoView.cs• L120-193
Playback handoff
The rendering surface and the playback engine meet through the Initialized event. When VideoView is ready, PlayerElement passes the surface settings to PlayerElementViewModel, which uses them when it creates the VLC-backed media player.
This handoff makes the render target available before playback starts. A new surface can replace an old player, and the view model disposes the old player before it accepts the new one. Initialization runs away from the UI thread so player setup and teardown do not block the player page.
sequenceDiagram
participant Page as Player page
participant View as VideoView
participant Element as PlayerElement
participant VM as PlayerElementViewModel
participant VLC as VLC player
Page->>View: Host the video surface
View->>View: Create and attach swap chain
View-->>Element: Initialized(surface options)
Element->>VM: Initialize(surface options)
VM->>VLC: Create player with surface options
VLC-->>View: Present rendered frames
References: Screenbox/…/PlayerElement.xaml• L39-47 Screenbox/…/PlayerElement.xaml.cs• L58-66 Screenbox.Core/…/PlayerElementViewModel.cs• L98-190
Layout synchronization
The surface tracks both its arranged size and its composition scale. It uses these values to keep the swap-chain target at the right pixel size and to keep the rendered image aligned with the XAML coordinate space.
The view model also receives size changes from PlayerElement. It uses the current view size and the video's natural dimensions to update the media player's source rectangle. This provides the crop needed for the selected aspect ratio while leaving the rendering surface responsible for display scale and target size.
References: Screenbox.VideoView/VideoView.cs• L49-72 Screenbox.VideoView/VideoView.cs• L196-241 Screenbox/…/PlayerElement.xaml.cs• L63-66 Screenbox.Core/…/PlayerElementViewModel.cs• L193-207 Screenbox.Core/…/PlayerElementViewModel.cs• L500-527
Interaction layer
PlayerElement places a transparent interaction layer over the non-interactive VideoView. The rendered video receives frames, while the overlay receives taps, pointer movement, wheel input, swipes, and hold gestures.
The overlay sends those gestures to the view model. This separation prevents input handling from depending on the native swap-chain surface and allows the player page to keep its controls, context menu, and playback gestures above the video.
References: Screenbox/…/PlayerElement.xaml• L18-47 Screenbox/…/PlayerElement.xaml.cs• L68-189 Screenbox/…/PlayerPage.xaml• L284-296
Surface lifecycle
The surface creates its graphics target only after it has usable dimensions. Size and scale changes update the target while it is loaded; unloading clears the panel target and releases the graphics resources.
Graphics and disposal errors trigger one guarded rebuild attempt. The guard prevents repeated recovery calls from competing while the surface is already being recreated. If setup cannot succeed through either graphics path, the surface remains without a swap chain until a later valid lifecycle event allows creation again.
References: Screenbox.VideoView/VideoView.cs• L76-105 Screenbox.VideoView/VideoView.cs• L107-129 Screenbox.VideoView/VideoView.cs• L243-273
Related pages
- Media playback — playback system context.
- VLC playback engine — media engine behavior.
- Play queue and controls — playback commands and user controls.
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to the
VideoView.Initializedcontract must stay aligned with the player initialization path. The player needs the surface options before it creates the VLC player. - Changes to the panel's size or composition-scale handling must preserve both the swap-chain pixel size and the view model's aspect-ratio crop behavior.
- Changes to the interaction overlay must keep the rendered
VideoViewnon-interactive. Otherwise pointer input can bypass the player gesture path. - Changes to swap-chain teardown must clear the panel target before releasing its graphics resources.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The surface has no valid size or composition scale. | No swap chain is created, so the player has no video presentation target yet. | Wait for a valid layout event before starting surface setup. |
| Hardware graphics setup fails. | The video surface may not render through the preferred device path. | Retry with the software graphics path. |
| The graphics device is lost during resize or scale change. | The current swap chain becomes unusable. | Tear down and recreate the surface through the guarded recovery path. |
| The surface is unloaded while a player still uses its target. | Frames can be sent to a disposed panel or graphics resource. | Clear the panel target and release the graphics resources during unload. |
| Surface initialization is replaced by a newer initialization request. | A stale player can replace the current player or remain undisposed. | Cancel stale initialization and dispose the player owned by that request. |
Key files
| File | Why to read it |
|---|---|
| Screenbox.VideoView/VideoView.cs | Defines the video surface, graphics target lifecycle, sizing, scale handling, and initialization contract. |
| Screenbox.VideoView/SwapChainPanelExtensions.cs | Shows how the swap chain is attached to and removed from the native XAML panel. |
Screenbox/Controls/PlayerElement.xaml | Shows the relationship between the rendered surface and the transparent interaction layer. |
| Screenbox/…/PlayerElement.xaml.cs | Connects surface initialization and size changes to the playback view model. |
| Screenbox.Core/…/PlayerElementViewModel.cs | Coordinates surface handoff, player replacement, and video crop behavior. |
Screenbox/Pages/PlayerPage.xaml | Places the player element in the application playback page. |