Repository navigation
Dev/rg/skia headless host - #23779
Conversation
Add a new cross-platform Skia host (Uno.UI.Runtime.Skia.Headless) that runs an Uno app without a display. It keeps the full window lifecycle intact and always drives the two-phase render cycle; rasterization is optional. - UseHeadless() builder with WithSize/WithScale/WithDpi/WithOrientation. - RenderTo(buffer, ...) renders each frame zero-copy into a caller-owned native buffer and raises a frame-rendered callback. With no buffer, the paint walk is skipped via FeatureConfiguration.Rendering.SkipVisualTreePainting. - Grant InternalsVisibleTo to the new assembly and register it in the solution + Skia filter. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Turn the headless backend from single-window into multi-window: - Each window is now its own HeadlessWindowWrapper that owns its renderer, acts as its own IXamlRootHost and IDisplayInformationExtension, and is registered in XamlRootMap (dropping the singleton). SupportsMultipleWindows is now true. - Add HeadlessHostBuilder.ConfigureWindow(ctx => HeadlessWindowOptions) so each window gets its own size/scale/orientation and optional dedicated buffer; builder-level WithSize/WithScale/WithDpi/RenderTo remain the defaults. - Host resolves per-view DisplayInformation by window id, disposes all window renderers on shutdown (so render threads never outlive their buffers), and only enables the global SkipVisualTreePainting when no window can have a buffer. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Replace the SkiaSharp SKColorType exposed by HeadlessWindowOptions.PixelFormat and HeadlessHostBuilder.RenderTo with a dedicated engine-agnostic HeadlessPixelFormat enum, mapped to SKColorType internally. The public API no longer references SkiaSharp. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Hand the caller a HeadlessWindow handle once via OnWindowCreated instead of passing it into a per-frame callback. The handle exposes: - NewFrameReady: a bare EventHandler signal raised when the window has new content (no obligation to render), and - RenderIntoAsync(buffer, rowBytes, pixelFormat, ct): callable independently, at any time, from any thread. The host schedules the render on its render thread and completes the returned task once the caller's buffer is filled. The keep-alive cycle always runs underneath (drawing into a null surface); the pull path is layered on top of it rather than replacing it, so the lifecycle, composition animations and RenderTargetBitmap keep advancing whether or not the caller pulls frames. Signalling and rendering are fully independent. Public API exposes no Skia types (only the caller's own IntPtr). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Use the configured scale directly; remove the environment-variable scale override and its now-unused helper. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Remove the pixel-output hook (HeadlessWindow, RenderIntoAsync, NewFrameReady, OnWindowCreated, HeadlessPixelFormat) — it isn't required. Pixels are read on demand with the standard WinUI RenderTargetBitmap; the render cycle keeps ticking as keep-alive, with SkipVisualTreePainting always on. Honor the standard AppWindow.Resize (override HeadlessWindowWrapper.Resize) for window sizing; HeadlessWindowOptions is now scale-only (WithSize sets the initial default). The public API exposes no SkiaSharp types. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Add doc/articles/features/using-skia-headless.md describing UseHeadless, the builder options (WithSize/WithScale/WithDpi/ConfigureWindow), sizing via AppWindow.Resize, reading pixels with RenderTargetBitmap, and input injection; register it in the TOC next to the Linux Framebuffer entry. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR introduces a new Skia “headless/offscreen” host runtime, wiring it into the Uno solution and granting it internal access to the necessary framework assemblies so it can implement windowing, rendering, and display-information abstractions without a native window system.
Changes:
- Added a new
Uno.UI.Runtime.Skia.Headlessruntime project (NuGet:Uno.WinUI.Runtime.Skia.Headless) with buildTransitive props/targets and solution inclusion. - Implemented a headless window wrapper + renderer to drive the Skia render cycle on a dedicated thread and expose a
HeadlessWindowhandle for pulling rendered frames. - Updated multiple assemblies’
InternalsVisibleTolists to allow the new headless runtime to use internal platform hooks.
Reviewed changes
Copilot reviewed 25 out of 25 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Uno.UWP/AssemblyInfo.skia.cs | Adds InternalsVisibleTo for the new headless Skia runtime. |
| src/Uno.UWP/AssemblyInfo.cs | Adds InternalsVisibleTo for the new headless Skia runtime. |
| src/Uno.UI/AssemblyInfo.skia.cs | Adds InternalsVisibleTo for the new headless Skia runtime. |
| src/Uno.UI.slnx | Includes the new headless runtime project in solution build configuration. |
| src/Uno.UI.Runtime.Skia.Headless/Uno.UI.Runtime.Skia.Headless.csproj | New headless runtime project packaging and references. |
| src/Uno.UI.Runtime.Skia.Headless/UI/NativeWindowFactoryExtension.cs | Creates headless window wrappers and disposes them on shutdown. |
| src/Uno.UI.Runtime.Skia.Headless/UI/HeadlessWindowWrapper.cs | Implements per-window XamlRoot host + display metrics and renderer ownership. |
| src/Uno.UI.Runtime.Skia.Headless/Rendering/HeadlessRenderer.cs | Dedicated render thread, keep-alive rendering, and “render into buffer” request handling. |
| src/Uno.UI.Runtime.Skia.Headless/Rendering/HeadlessPixelFormatExtensions.cs | Maps public headless pixel formats to Skia color types/stride info. |
| src/Uno.UI.Runtime.Skia.Headless/Hosting/HeadlessHost.cs | New Skia host implementation for headless/offscreen operation and API extensibility registrations. |
| src/Uno.UI.Runtime.Skia.Headless/buildTransitive/Uno.WinUI.Runtime.Skia.Headless.targets | Adds compile constants and content-copy behavior for consuming apps. |
| src/Uno.UI.Runtime.Skia.Headless/buildTransitive/Uno.WinUI.Runtime.Skia.Headless.props | Sets runtime identifier and headless-specific MSBuild flags. |
| src/Uno.UI.Runtime.Skia.Headless/Builder/HostBuilder.cs | Adds UseHeadless(...) host builder extension methods. |
| src/Uno.UI.Runtime.Skia.Headless/Builder/HeadlessWindowOptions.cs | Public per-window sizing/orientation/callback options. |
| src/Uno.UI.Runtime.Skia.Headless/Builder/HeadlessWindowContext.cs | Public context passed to per-window configuration delegate. |
| src/Uno.UI.Runtime.Skia.Headless/Builder/HeadlessWindow.cs | Public handle to subscribe for “new frame” signals and request rendering into a buffer. |
| src/Uno.UI.Runtime.Skia.Headless/Builder/HeadlessPixelFormat.cs | Public pixel-format enum for headless rendering buffers. |
| src/Uno.UI.Runtime.Skia.Headless/Builder/HeadlessHostBuilder.cs | Public builder configuring size/scale/DPI/orientation and per-window options. |
| src/Uno.UI.Runtime.Skia.Headless/ApplicationModel/Core/CoreApplicationExtension.cs | Headless core application exit plumbing. |
| src/Uno.UI.Dispatching/AssemblyInfo.cs | Adds InternalsVisibleTo for the new headless Skia runtime. |
| src/Uno.UI.Composition/AssemblyInfo.skia.cs | Adds InternalsVisibleTo for the new headless Skia runtime. |
| src/Uno.UI.Composition/AssemblyInfo.cs | Adds InternalsVisibleTo for the new headless Skia runtime. |
| src/Uno.UI-Skia-only.slnf | Adds the headless runtime project to the Skia-only solution filter. |
| src/Uno.Foundation/AssemblyInfo.cs | Adds InternalsVisibleTo for the new headless Skia runtime. |
| src/Uno.Foundation.Logging/AssemblyInfo.cs | Adds InternalsVisibleTo for the new headless Skia runtime. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The public single-arg constructor was unused and only offered unconfigurable direct construction; the builder uses the internal constructor. HeadlessHost is now obtained solely via UseHeadless(). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23779/wasm-skia-net9/index.html |
- WithSize doc no longer references the removed RenderTo API. - WithScale/WithDpi reject non-finite (NaN/Infinity) values. - Validate HeadlessWindowOptions.Scale at the consumption point so a per-window configurator can't feed 0/NaN/Infinity into the size/scale division. - Dispose only frees the render surface/event once the render thread has actually stopped, avoiding a use-after-dispose race. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23779/wasm-skia-net9/index.html |
|
The build 222534 found UI Test snapshots differences: Details
|
- Restore the process-wide FeatureConfiguration.Rendering.SkipVisualTreePainting to its prior value on shutdown instead of leaving it forced on. - Tear down windows on shutdown (unregister from XamlRootMap, then stop the renderer) and ignore invalidations after the renderer is disposed, so late invalidations can't hit a disposed renderer and mappings don't leak. - Rename the host-builder extension class to HeadlessHostBuilderExtensions so its type name doesn't collide with the other Skia hosts' Uno.UI.Hosting.HostBuilder (avoids CS0433 when referenced together). Extension-method usage is unchanged. - Clarify in the docs that host selection is by registration order, not presence. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 24 out of 24 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (3)
src/Uno.UI.Runtime.Skia.Headless/Hosting/HeadlessHost.cs:66
- RunLoop executes on the thread that called host.Run/RunAsync (see Uno.UI.Hosting.UnoPlatformHost.RunCore), not on the EventLoop/dispatcher thread. Calling _windowFactory.TearDownWindows() here will Unregister XamlRoots from XamlRootMap off-dispatcher, but XamlRootMap uses unsynchronized static Dictionaries (no locks), so this can race with UI-thread Register/Invalidate and corrupt the map.
Schedule window teardown onto the EventLoop thread (same thread used by CoreDispatcher.DispatchOverride) and wait for it to complete before returning from RunLoop.
// Tear down every window (stop its render thread, unregister from XamlRootMap) before returning.
_windowFactory?.TearDownWindows();
src/Uno.UI.Runtime.Skia.Headless/Rendering/HeadlessRenderer.cs:68
- HeadlessRenderer.Invalidate() can still throw ObjectDisposedException due to a race: another thread may dispose _renderInvalidationEvent after the _disposed check but before Set(). This is especially likely during shutdown when late invalidations arrive.
Wrap Set() in a try/catch (or use an Interlocked-swapped event reference) so Invalidate is truly safe after/during disposal.
// A late invalidation can arrive after disposal (the event is disposed once the thread stops).
if (_disposed)
{
return;
}
_renderInvalidationEvent.Set();
}
src/Uno.UI.Runtime.Skia.Headless/Hosting/HeadlessHost.cs:48
- HeadlessHost creates a dedicated EventLoop thread, but neither RunLoop nor Dispose ever disposes the EventLoop or the ManualResetEvent. Since the headless host is explicitly documented to return to the caller when Application.Exit() is called, this can leak a background thread/WaitHandle in long-running processes (e.g., test runners that spin up multiple headless runs in-process).
Consider implementing an idempotent disposal/cleanup path that (1) tears down windows on the dispatcher thread, (2) restores any overridden CoreDispatcher dispatch delegates if applicable, and (3) disposes EventLoop + termination gate once the run terminates.
_eventLoop = new EventLoop();
_coreApplicationExtension = new CoreApplicationExtension(_terminationGate);
}
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-23779/docs/index.html |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23779/wasm-skia-net9/index.html |
|
The build 224989 found UI Test snapshots differences: Details
|
- Replace the thread-static _isDispatcherThread flag (written from an instance method) with a captured dispatcher-thread id. - Drop the needless try/catch around Thread.Join in HeadlessRenderer.Dispose (a valid timeout on a started thread cannot throw). Co-Authored-By: Claude Opus 4.8 <[email protected]>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 24 out of 24 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (2)
src/Uno.UI.Runtime.Skia.Headless/UI/NativeWindowFactoryExtension.cs:41
- TearDownWindows iterates over _windows while CreateWindow can add to the same list. Enumerating a List concurrently with writes can throw 'Collection was modified' and may skip windows. Snapshot + clear under the same gate used for writes makes teardown deterministic and thread-safe.
internal void TearDownWindows()
{
foreach (var window in _windows)
{
window.TearDown();
src/Uno.UI.Runtime.Skia.Headless/UI/NativeWindowFactoryExtension.cs:34
- CreateWindow updates _windowIndex and adds to _windows without synchronization. If windows can be created concurrently (or overlap with teardown), this can lead to duplicate indices or list corruption. Using Interlocked for the index and guarding list writes avoids races.
This issue also appears on line 37 of the same file.
public INativeWindowWrapper CreateWindow(Window window, XamlRoot xamlRoot)
{
var index = _windowIndex++;
var options = _builder.ResolveWindowOptions(index, window);
// Initial size is the builder default (same for every window); apps resize per window via AppWindow.Resize.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23779/wasm-skia-net9/index.html |
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-23779/docs/index.html |
|
The build 225080 found UI Test snapshots differences: Details
|
|
|
|
Tick the box to add this pull request to the merge queue (same as
|
GitHub Issue: https://github.com/unoplatform/kahua-private/issues/505
PR Type:
What changed? 🚀
PR Checklist ✅
Screenshots Compare Test Runresults.