Skip to content

Commit dd529f4

Browse files
committed
fix(gtk): Fix OpenGL render surface flickering
For an unknown reason, matching the surface size for the SK render surface to the GLArea can cause render glitches. Adding a guard band avoids this flickering.
1 parent 919cd95 commit dd529f4

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/Uno.UI.Runtime.Skia.Gtk/GLRenderSurface.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ internal class GLRenderSurface : GLArea, IRenderSurface
2828
private const SKColorType colorType = SKColorType.Rgba8888;
2929
private const GRSurfaceOrigin surfaceOrigin = GRSurfaceOrigin.BottomLeft;
3030

31+
/// <summary>
32+
/// Include a guard band for the creation of the OpenGL surface to avoid
33+
/// incorrect renders when the surface is exactly the size of the GLArea.
34+
/// </summary>
35+
private const int GuardBand = 32;
36+
3137
private readonly DisplayInformation _displayInformation;
3238
private FocusManager? _focusManager;
3339

@@ -77,9 +83,6 @@ private void UnoGLDrawingArea_Render(object o, RenderArgs args)
7783
_grContext = GRContext.CreateGl(glInterface);
7884
}
7985

80-
_gl.Clear(ClearBufferMask.ColorBufferBit);
81-
_gl.ClearColor(1.0f, 1.0f, 1.0f, 1.0f);
82-
8386
// manage the drawing surface
8487
var res = (int)Math.Max(1.0, Screen.Resolution / 96.0);
8588
var w = Math.Max(0, AllocatedWidth * res);
@@ -102,24 +105,33 @@ private void UnoGLDrawingArea_Render(object o, RenderArgs args)
102105

103106
var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());
104107

105-
_renderTarget = new GRBackendRenderTarget(w, h, samples, stencil, glInfo);
108+
_renderTarget = new GRBackendRenderTarget(w + GuardBand, h + GuardBand, samples, stencil, glInfo);
106109

107110
// create the surface
108111
_surface?.Dispose();
109112
_surface = SKSurface.Create(_grContext, _renderTarget, surfaceOrigin, colorType);
113+
114+
if (this.Log().IsEnabled(LogLevel.Trace))
115+
{
116+
this.Log().Trace($"Recreate render surface {w}x{h} colorType:{colorType} sample:{samples}");
117+
}
110118
}
111119

112-
using (new SKAutoCanvasRestore(_surface.Canvas, true))
113-
{
114-
_surface.Canvas.Clear(SKColors.White);
120+
_gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.StencilBufferBit | ClearBufferMask.DepthBufferBit);
121+
_gl.ClearColor(1.0f, 1.0f, 1.0f, 1.0f);
115122

116-
// _surface.Canvas.Scale((float)(1/_dpi));
123+
var canvas = _surface.Canvas;
124+
125+
using (new SKAutoCanvasRestore(canvas, true))
126+
{
127+
canvas.Clear(SKColors.White);
128+
canvas.Translate(new SKPoint(0, GuardBand));
117129

118130
WUX.Window.Current.Compositor.Render(_surface);
119131
}
120132

121133
// update the control
122-
_surface.Canvas.Flush();
134+
canvas.Flush();
123135

124136
_gl.Flush();
125137
}

0 commit comments

Comments
 (0)