Skip to content

Commit db79820

Browse files
committed
make shared glyph cache the default
1 parent b96887f commit db79820

4 files changed

Lines changed: 17 additions & 185 deletions

File tree

‎src/ImageSharp.Drawing/Processing/DrawingCanvas{TPixel}.cs‎

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,11 @@ public sealed class DrawingCanvas<TPixel> : DrawingCanvas
5959
/// </summary>
6060
private readonly Stack<DrawingCanvasState> savedStates = new();
6161

62-
// Per-canvas glyph-outline cache: when enabled, hoists RichTextGlyphRenderer's per-glyph
63-
// outline cache from per-DrawText-call scope up to the whole canvas, so a glyph outline
64-
// built once is reused by every DrawText call on this canvas (across a frame's many text
65-
// runs) instead of being rebuilt for every run on a text-heavy page. Lazily created and only
66-
// used when sharing is opted in via Configuration.SetSharedGlyphCache; see shareGlyphCache.
67-
private Dictionary<RichTextGlyphRenderer.CacheKey, List<RichTextGlyphRenderer.GlyphRenderData>>? sharedGlyphCacheStore;
68-
69-
// Whether this canvas shares its glyph-outline cache across DrawText calls. Resolved once at
70-
// construction from the configuration (Configuration.SetSharedGlyphCache); defaults to false,
71-
// in which case each DrawText uses RichTextGlyphRenderer's private per-call cache.
72-
private readonly bool shareGlyphCache;
62+
// Per-canvas glyph-outline cache: hoists RichTextGlyphRenderer's per-glyph outline cache from
63+
// per-DrawText-call scope up to the whole canvas, so a glyph outline built once is reused by
64+
// every DrawText call on this canvas (across a frame's many text runs) instead of being
65+
// rebuilt for every run on a text-heavy page. Lazily created on first text draw.
66+
private Dictionary<RichTextGlyphRenderer.CacheKey, List<RichTextGlyphRenderer.GlyphRenderData>>? glyphCache;
7367

7468
/// <summary>
7569
/// Initializes a new instance of the <see cref="DrawingCanvas{TPixel}"/> class.
@@ -161,7 +155,6 @@ private DrawingCanvas(
161155
this.targetFrame = targetFrame;
162156
this.batcher = batcher;
163157
this.ownsBatcher = ownsBatcher;
164-
this.shareGlyphCache = configuration.GetSharedGlyphCache();
165158

166159
// Canvas coordinates are local to the current frame; origin stays at (0,0).
167160
this.Bounds = new Rectangle(0, 0, targetFrame.Bounds.Width, targetFrame.Bounds.Height);
@@ -450,10 +443,9 @@ public override void DrawText(
450443
this.DrawTextCore(textOptions, text, path, brush, pen);
451444
}
452445

453-
// Returns the per-canvas shared glyph-outline cache (lazily created) when sharing is enabled,
454-
// or null so the renderer falls back to its private per-call cache (the historical default).
455-
private Dictionary<RichTextGlyphRenderer.CacheKey, List<RichTextGlyphRenderer.GlyphRenderData>>? ResolveGlyphCache()
456-
=> this.shareGlyphCache ? this.sharedGlyphCacheStore ??= [] : null;
446+
// Returns the per-canvas glyph-outline cache, lazily creating it on first use.
447+
private Dictionary<RichTextGlyphRenderer.CacheKey, List<RichTextGlyphRenderer.GlyphRenderData>> ResolveGlyphCache()
448+
=> this.glyphCache ??= [];
457449

458450
private void DrawTextCore(
459451
RichTextOptions textOptions,
@@ -1162,8 +1154,8 @@ public override void Dispose()
11621154
this.DisposePendingImageResources();
11631155
}
11641156

1165-
// Release the per-canvas shared glyph-outline cache.
1166-
this.sharedGlyphCacheStore = null;
1157+
// Release the per-canvas glyph-outline cache.
1158+
this.glyphCache = null;
11671159

11681160
this.isDisposed = true;
11691161
}

‎src/ImageSharp.Drawing/Processing/GlyphCacheDefaultsExtensions.cs‎

Lines changed: 0 additions & 42 deletions
This file was deleted.

‎src/ImageSharp.Drawing/Processing/RichTextGlyphRenderer.cs‎

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,10 @@ internal sealed partial class RichTextGlyphRenderer : BaseGlyphBuilder, IDisposa
7373
private const float AccuracyMultiple = 8;
7474

7575
/// <summary>Maps cache keys to their list of <see cref="GlyphRenderData"/> entries (one per layer).
76-
/// May be a per-canvas shared cache injected via the constructor so glyph outlines persist across
77-
/// every DrawText call in a frame; otherwise a private per-renderer cache (the historical behavior).</summary>
76+
/// Owned by the enclosing <see cref="DrawingCanvas{TPixel}"/> and shared across every DrawText
77+
/// call on that canvas, so glyph outlines persist beyond a single text draw.</summary>
7878
private readonly Dictionary<CacheKey, List<GlyphRenderData>> glyphCache;
7979

80-
/// <summary><see langword="true"/> when <see cref="glyphCache"/> is owned by this renderer and
81-
/// cleared on dispose; <see langword="false"/> for an injected shared (per-canvas) cache.</summary>
82-
private readonly bool ownsCache;
83-
8480
/// <summary>Read cursor into the cached layer list for layered cache hits.</summary>
8581
private int cacheReadIndex;
8682

@@ -113,22 +109,20 @@ internal sealed partial class RichTextGlyphRenderer : BaseGlyphBuilder, IDisposa
113109
/// <param name="path">Optional path to draw the text along.</param>
114110
/// <param name="pen">Default pen for outlined text, or <see langword="null"/> for fill-only.</param>
115111
/// <param name="brush">Default brush for filled text, or <see langword="null"/> for outline-only.</param>
116-
/// <param name="sharedGlyphCache">Optional caller-owned glyph cache shared across renderer
117-
/// instances (e.g. per-canvas) so glyph outlines persist beyond a single text draw; when
118-
/// <see langword="null"/> the renderer uses a private cache cleared on dispose.</param>
112+
/// <param name="glyphCache">Caller-owned per-canvas glyph cache shared across renderer
113+
/// instances so glyph outlines persist beyond a single text draw.</param>
119114
public RichTextGlyphRenderer(
120115
DrawingOptions drawingOptions,
121116
IPath? path,
122117
Pen? pen,
123118
Brush? brush,
124-
Dictionary<CacheKey, List<GlyphRenderData>>? sharedGlyphCache = null)
119+
Dictionary<CacheKey, List<GlyphRenderData>> glyphCache)
125120
: base(drawingOptions.Transform)
126121
{
127122
this.drawingOptions = drawingOptions;
128123
this.defaultPen = pen;
129124
this.defaultBrush = brush;
130-
this.glyphCache = sharedGlyphCache ?? [];
131-
this.ownsCache = sharedGlyphCache is null;
125+
this.glyphCache = glyphCache;
132126
this.DrawingOperations = [];
133127
this.currentCompositionMode = drawingOptions.GraphicsOptions.AlphaCompositionMode;
134128
this.currentBlendingMode = drawingOptions.GraphicsOptions.ColorBlendingMode;
@@ -794,12 +788,7 @@ private void Dispose(bool disposing)
794788
{
795789
if (disposing)
796790
{
797-
// A shared (per-canvas) cache outlives this renderer; only clear a private one.
798-
if (this.ownsCache)
799-
{
800-
this.glyphCache.Clear();
801-
}
802-
791+
// The glyph cache is owned by the canvas and outlives this renderer.
803792
this.DrawingOperations.Clear();
804793
}
805794

‎tests/ImageSharp.Drawing.Benchmarks/Drawing/DrawTextSharedGlyphCache.cs‎

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)