Merge pull request #5113 from AvaloniaUI/thread-safe-SKTextBlobBuilder

Thread safe SKTextBlobBuilder
This commit is contained in:
Max Katz
2020-11-28 01:53:14 -05:00
committed by GitHub
@@ -3,6 +3,8 @@ using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Avalonia.Controls.Platform.Surfaces;
using Avalonia.Media;
using Avalonia.OpenGL;
@@ -166,12 +168,13 @@ namespace Avalonia.Skia
LinearMetrics = true
};
private static readonly SKTextBlobBuilder s_textBlobBuilder = new SKTextBlobBuilder();
private static readonly ThreadLocal<SKTextBlobBuilder> s_textBlobBuilderThreadLocal = new ThreadLocal<SKTextBlobBuilder>(() => new SKTextBlobBuilder());
/// <inheritdoc />
public IGlyphRunImpl CreateGlyphRun(GlyphRun glyphRun, out double width)
{
var count = glyphRun.GlyphIndices.Length;
var textBlobBuilder = s_textBlobBuilderThreadLocal.Value;
var glyphTypeface = (GlyphTypefaceImpl)glyphRun.GlyphTypeface.PlatformImpl;
@@ -191,15 +194,15 @@ namespace Avalonia.Skia
{
if (glyphTypeface.IsFixedPitch)
{
s_textBlobBuilder.AddRun(glyphRun.GlyphIndices.Buffer.Span, s_font);
textBlobBuilder.AddRun(glyphRun.GlyphIndices.Buffer.Span, s_font);
textBlob = s_textBlobBuilder.Build();
textBlob = textBlobBuilder.Build();
width = glyphTypeface.GetGlyphAdvance(glyphRun.GlyphIndices[0]) * scale * glyphRun.GlyphIndices.Length;
}
else
{
var buffer = s_textBlobBuilder.AllocateHorizontalRun(s_font, count, 0);
var buffer = textBlobBuilder.AllocateHorizontalRun(s_font, count, 0);
var positions = buffer.GetPositionSpan();
@@ -219,12 +222,12 @@ namespace Avalonia.Skia
buffer.SetGlyphs(glyphRun.GlyphIndices.Buffer.Span);
textBlob = s_textBlobBuilder.Build();
textBlob = textBlobBuilder.Build();
}
}
else
{
var buffer = s_textBlobBuilder.AllocatePositionedRun(s_font, count);
var buffer = textBlobBuilder.AllocatePositionedRun(s_font, count);
var glyphPositions = buffer.GetPositionSpan();
@@ -250,7 +253,7 @@ namespace Avalonia.Skia
width = currentX;
textBlob = s_textBlobBuilder.Build();
textBlob = textBlobBuilder.Build();
}
return new GlyphRunImpl(textBlob);