SkiaSharp icon indicating copy to clipboard operation
SkiaSharp copied to clipboard

[QUESTION] GPU acceleration makes canvas background go black

Open davwat opened this issue 3 years ago • 2 comments

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): image After the second render: image

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.

davwat avatar Nov 29 '21 20:11 davwat

I'm now using SKSurface.Draw() instead of Snapshot but the problems persist.

davwat avatar Nov 30 '21 07:11 davwat

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..

taublast avatar Feb 26 '24 06:02 taublast