SkiaSharp
SkiaSharp copied to clipboard
[QUESTION] GPU acceleration makes canvas background go black
This is quite a long question.
Drawing with GPU acceleration makes canvas background black (regardless of flushed or not), and SKSurface.Snapshot() is empty after second SKCanvas.Flush().
I am creating an SKCanvas like this:
private GRGlInterface grGlInterface;
private GRContext grContext;
private GRBackendRenderTarget renderTarget;
private SKSurface surface;
protected SKCanvas canvas;
...
grGlInterface = GRGlInterface.Create();
grContext = GRContext.CreateGl(grGlInterface);
renderTarget = new((int)size.x, (int)size.y, 0, 8, new GRGlFramebufferInfo(0, 0x8058));
surface = SKSurface.Create(grContext, renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888);
canvas = surface.Canvas;
And rendering to it like this:
canvas.Clear();
using SKPaint primary = new SKPaint();
primary.Color = SKColor.Parse("#1abc9c");
primary.IsAntialias = true;
using SKRoundRect rect = new SKRoundRect(new(0, 0, 140, 40), 15f);
canvas.DrawRoundRect(rect, primary);
canvas.Flush();
Then finally drawing it to another canvas that uses a Silk.NET GLFW window GLContext:
using SKImage image = surface.Snapshot();
target.DrawImage(image, position);
After the first render (the red circled area is the entire canvas is having the problem. the other canvas is the entire window):
After the second render:

If I take SKSnapshot straight after second flush, the snapshot is empty, but if I do the drawing-and-flushing process multiple times at some other point in the program and not directly before the snapshot, there isn't an issue.
I'm now using SKSurface.Draw() instead of Snapshot but the problems persist.
Just a quick thought maybe in some specific cases when canvas.Clear(); passes a transparent color somewhere internally it treats it as Black instead of Transparent.
That empty second frame makes me think of double-buffering where the second buffer was shown but not filled..