VL.Stride SkiaRenderer creates a new EGL surface and SKSurface every frame

Hi, I’m just debugging a complex project with a lot of skia textures in it with the help of Claude.
The AI has found an issue. I didn’t confirm this myself ( but can see the resulting cost ).
So I hope this is a real bug, I’ll post this here for your consideration. @Elias
Should we report things like that as a github issue?

VL.Stride SkiaRenderer creates a new EGL surface and SKSurface every frame

vvvv gamma 8.0-0143 · VL.Stride.Runtime 2026.8.0-0143 · Stride 4.3.0.2507 (D3D11) · Windows 11 · exported app

Summary

VL.Stride.SkiaRenderer.DrawCore (the renderer that draws a Skia layer directly into the current Stride render target) creates a new ANGLE/EGL surface from the render target’s D3D11 texture and a new SKSurface on top of it in every frame, and disposes both again at the end of the frame. The render target is normally the same texture from frame to frame, so this work could be cached. SkiaTexture already does exactly that: it keeps its EGL surface and SKSurface until size, format or device change.

Where

VL.Stride.SkiaRenderer.DrawCore(RenderDrawContext), decompiled from VL.Stride.Runtime 2026.8.0-0143 and shortened:

Texture renderTarget = context.CommandList.RenderTarget;
...
EglSurface eglSurface;
using (eglContext.MakeCurrent(false, null))
    eglSurface = eglContext.CreateSurfaceFromClientBuffer(nativeTexture.NativePointer);   // every frame
try
{
    using (renderContext.MakeCurrent(true, eglSurface))
    {
        SKSurface skSurface = CreateSkSurface(renderContext.SkiaContext, renderTarget);    // every frame
        try
        {
            canvas.ClipRect(viewport);
            ... layer.Render(CallerInfo.InRenderer(..., skSurface, ...));
            skSurface.Flush();
        }
        finally { skSurface.Dispose(); }
    }
}
finally { eglSurface.Dispose(); }

Measured cost

An exported app, fullscreen at 3840×2160, with one SkiaRenderer drawing a Skia layer into the window, profiled with dotnet-trace (SampleProfiler, 59 s):

Call Share of main-thread samples
EglContext.CreateSurfaceFromClientBuffer from SkiaRenderer.DrawCore 2.7%
SkiaRenderer.CreateSkSurface 1.1%

That’s about 0.9 ms per frame at ~40 fps on our machine, plus per-frame allocations of EglSurface, SKSurface and GRBackendRenderTarget (finalizable SkiaSharp objects) for the GC. The cost is the same whether the layer is complex or nearly empty.

Suggested fix

Cache the EGL surface and SKSurface per render target, as SkiaTexture does, and recreate them only when the target changes:

// fields
Texture? cachedTarget; IntPtr cachedNative; Int2 cachedSize; int cachedSamples;
EglContext? cachedEglContext; EglSurface? cachedEglSurface; SKSurface? cachedSkSurface;

// in DrawCore
var native = ((Texture2D)SharpDXInterop.GetNativeResource(renderTarget)).NativePointer;
var size = new Int2(renderTarget.Width, renderTarget.Height);
if (cachedSkSurface is null || renderTarget != cachedTarget || native != cachedNative || size != cachedSize
    || (int)renderTarget.MultisampleCount != cachedSamples || eglContext != cachedEglContext)
{
    DisposeCachedSurfaces();
    using (eglContext.MakeCurrent(false, null))
        cachedEglSurface = eglContext.CreateSurfaceFromClientBuffer(native);
    using (renderContext.MakeCurrent(true, cachedEglSurface))
        cachedSkSurface = CreateSkSurface(renderContext.SkiaContext, renderTarget);
    (cachedTarget, cachedNative, cachedSize, cachedSamples, cachedEglContext) =
        (renderTarget, native, size, (int)renderTarget.MultisampleCount, eglContext);
}
using (renderContext.MakeCurrent(true, cachedEglSurface))
{
    var canvas = cachedSkSurface.Canvas;
    canvas.Save();                      // the clip must not accumulate across frames
    canvas.ClipRect(viewport);
    ... layer.Render(...);
    canvas.Restore();
    cachedSkSurface.Flush();
}

// DisposeCachedSurfaces() in Dispose(), when the render target is destroyed,
// and on RenderContextProvider.OnDeviceLost (SkiaTexture already subscribes to that).

Points to watch:

  • Stale native pointers: a destroyed texture’s native pointer can be reused by a new texture. Comparing the Stride Texture object as well, and dropping the cache when that texture is destroyed, avoids drawing into a stale EGL surface.
  • Alternating targets: if a renderer draws into a few different targets in turn (several windows, swap-chain buffers on some setups), a small cache keyed by target (2–4 entries) keeps the benefit.
  • The MSAA path (msaaAwareEglContext) is part of the key, since the surface belongs to that context.
  • Canvas state: with a reused SKSurface, the clip, matrix and anything else a layer leaves on the canvas carry over to the next frame. Save/Restore around each frame keeps that from leaking (or canvas.ResetMatrix() plus a fresh clip).

Workaround until then

Render the layer through SkiaTexture, which caches its surfaces, and draw the texture with a fullscreen quad. This costs a full-size intermediate texture (32 MB at 4K) and an extra copy, so a fix in SkiaRenderer would be the better solution.

I remember having issues when resizing the window back then. The sksurface would hold on to the backbuffer leading to a crash when the swap chain owned by stride tried to resize.
Having said that, we can try this once more using a cached surface.

Surface objects are cached in upcoming.

Hi @Elias, thanks for the quick fix! Since updating, SkiaRenderer on a RenderWindow sometimes doesn’t draw, and with MSAA on the RenderWindow it doesn’t draw at all. Claude and I compared the decompiled SkiaRenderer from VL.Stride.Runtime 0143 and 0261. This is what we found; I haven’t confirmed each point myself yet.

1. MSAA: drawing happens on the MSAA EGL context, but the GRContext belongs to the main one

Before the change, the EGL surface was created with msaaAwareEglContext, but drawing ran inside RenderContext.MakeCurrent(true, surface), i.e. on the main EGL context that the shared GRContext was created on. Now it is:

using ((msaaAwareEglContext ?? renderContext.EglContext).MakeCurrent(true, eglSurface))
    ... layer.Render(... renderContext.SkiaContext ...)

So with MSAA, the shared GRContext issues its GL calls while a different context is current. The two contexts share programs and textures, but not FBOs or VAOs, and Skia’s cached GL state no longer matches. We think that’s why nothing shows up. Without MSAA, msaaAwareEglContext is null and nothing changes, which matches what we see.

2. CreateSkSurface runs with no surface current

CreateSkSurface reads GL_STENCIL_BITS and GL_SAMPLES from the current framebuffer. Before, it ran inside the scope where the target surface was current. Now it runs inside MakeCurrent(false, null), so it reads 0 samples and 0 stencil bits for an MSAA target.

3. One failed creation disables the renderer until the target changes

lastRenderTargetPointer = nativePointer is set before the EGL surface and SKSurface are created. If either one returns null once (for example during a resize), the next frames see a matching pointer and return early. Nothing is drawn until the render target changes. This could explain the occasional “not drawing” on non-MSAA windows.

4. The clip carries over between frames

canvas.ClipRect(viewport, Intersect) now runs on a reused canvas with no Save/Restore around it. If the viewport changes while the target stays the same, or a layer leaves state on the canvas, the clip keeps shrinking.

Suggested change (sketch):

if (lastRenderTargetPointer != nativePointer || surface == null)
{
    ReleaseSurface();
    // ... msaaAwareEglContext setup as now ...
    var surfaceContext = msaaAwareEglContext ?? renderContext.EglContext;
    using (surfaceContext.MakeCurrent(false, null))
        eglSurface = surfaceContext.CreateSurfaceFromClientBuffer(nativePointer);
    if (eglSurface == null) { ReleaseSurface(); return; }

    using (renderContext.MakeCurrent(true, eglSurface))       // main context, as before the change
        surface = CreateSkSurface(renderContext.SkiaContext, renderTarget);
    if (surface == null) { ReleaseSurface(); return; }

    lastRenderTargetPointer = nativePointer;                  // only once both exist
}

using (renderContext.MakeCurrent(true, eglSurface))           // main context, not msaaAwareEglContext
{
    var canvas = surface.Canvas;
    canvas.Save();
    canvas.ClipRect(viewportRect);
    // ... layer.Render(...)
    canvas.Restore();
    surface.Flush();
}

Until this is fixed, we’re working around it on MSAA windows by rendering through SkiaTexture and a fullscreen quad.

Tested with vvvv gamma 2026.8.0-0261 on Windows 11 with Stride D3D11.

If this is getting too complicated I would be happy to have the cache being reverted.

@mburk Indeed, the sample and stencil readings were all zero. I was able to repro it by switching between two renderers - simply using one was not enough (my initial test). Just pushed a fix, can you try with latest?

Seems fixed, thanks!