Browse Source

Merge pull request #11777 from Gillibald/fixes/concurrentGlyphRunImpl

Use a thread safe collection for SKTextBlob caching
pull/11798/head
Max Katz 3 years ago
committed by GitHub
parent
commit
32b3827615
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 35
      src/Skia/Avalonia.Skia/GlyphRunImpl.cs

35
src/Skia/Avalonia.Skia/GlyphRunImpl.cs

@ -1,5 +1,6 @@
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@ -14,7 +15,7 @@ namespace Avalonia.Skia
private readonly ushort[] _glyphIndices;
private readonly SKPoint[] _glyphPositions;
private readonly Dictionary<SKFontEdging, SKTextBlob> _textBlobCache = new(1);
private readonly ConcurrentDictionary<SKFontEdging, SKTextBlob> _textBlobCache = new();
public GlyphRunImpl(IGlyphTypeface glyphTypeface, double fontRenderingEmSize,
IReadOnlyList<GlyphInfo> glyphInfos, Point baselineOrigin)
@ -100,32 +101,28 @@ namespace Avalonia.Skia
break;
}
if (_textBlobCache.TryGetValue(edging, out var textBlob))
return _textBlobCache.GetOrAdd(edging, (_) =>
{
return textBlob;
}
var font = _glyphTypefaceImpl.SKFont;
var font = _glyphTypefaceImpl.SKFont;
font.Hinting = SKFontHinting.Full;
font.Subpixel = edging == SKFontEdging.SubpixelAntialias;
font.Edging = edging;
font.Size = (float)FontRenderingEmSize;
font.Hinting = SKFontHinting.Full;
font.Subpixel = edging == SKFontEdging.SubpixelAntialias;
font.Edging = edging;
font.Size = (float)FontRenderingEmSize;
var builder = SKTextBlobBuilderCache.Shared.Get();
var builder = SKTextBlobBuilderCache.Shared.Get();
var runBuffer = builder.AllocatePositionedRun(font, _glyphIndices.Length);
var runBuffer = builder.AllocatePositionedRun(font, _glyphIndices.Length);
runBuffer.SetPositions(_glyphPositions);
runBuffer.SetGlyphs(_glyphIndices);
runBuffer.SetPositions(_glyphPositions);
runBuffer.SetGlyphs(_glyphIndices);
textBlob = builder.Build();
var textBlob = builder.Build();
SKTextBlobBuilderCache.Shared.Return(builder);
SKTextBlobBuilderCache.Shared.Return(builder);
_textBlobCache.Add(edging, textBlob);
return textBlob;
return textBlob;
});
}
public void Dispose()

Loading…
Cancel
Save