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
Textureobject 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/Restorearound each frame keeps that from leaking (orcanvas.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.