Skip to content

Commit e4e8077

Browse files
committed
Make the caches shared between containers thread safe
1 parent 6ff1dfc commit e4e8077

7 files changed

Lines changed: 207 additions & 105 deletions

File tree

‎Source/HtmlRenderer/Adapters/RAdapter.cs‎

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ public abstract class RAdapter
4545
/// <summary>
4646
/// cache of brush color to brush instance
4747
/// </summary>
48+
/// <remarks>The adapter is shared by every container, the caches are locked on themselves.</remarks>
4849
private readonly Dictionary<RColor, RBrush> _brushesCache = new Dictionary<RColor, RBrush>();
4950

5051
/// <summary>
51-
/// cache of pen color to pen instance
52+
/// cache of pens, a cached pen is never changed, since another thread may be drawing with it
5253
/// </summary>
53-
private readonly Dictionary<RColor, RPen> _penCache = new Dictionary<RColor, RPen>();
54+
private readonly Dictionary<(RColor Color, double Width, RDashStyle DashStyle), RPen> _penCache = new Dictionary<(RColor, double, RDashStyle), RPen>();
5455

5556
/// <summary>
5657
/// cache of all the font used not to create same font again and again
@@ -256,18 +257,36 @@ public RColor GetColor(string colorName)
256257
}
257258

258259
/// <summary>
259-
/// Get cached pen instance for the given color.
260+
/// Get cached solid pen instance for the given color, 1 wide.
260261
/// </summary>
261262
/// <param name="color">the color to get pen for</param>
262-
/// <returns>pen instance</returns>
263+
/// <returns>pen instance, don't change it</returns>
263264
public RPen GetPen(RColor color)
264265
{
265-
RPen pen;
266-
if (!_penCache.TryGetValue(color, out pen))
266+
return GetPen(color, 1, RDashStyle.Solid);
267+
}
268+
269+
/// <summary>
270+
/// Get cached pen instance for the given color, width and dash style.
271+
/// </summary>
272+
/// <param name="color">the color to get pen for</param>
273+
/// <param name="width">the width of the pen</param>
274+
/// <param name="dashStyle">the dash style of the pen</param>
275+
/// <returns>pen instance, don't change it</returns>
276+
public RPen GetPen(RColor color, double width, RDashStyle dashStyle)
277+
{
278+
lock (_penCache)
267279
{
268-
_penCache[color] = pen = CreatePen(color);
280+
RPen pen;
281+
if (!_penCache.TryGetValue((color, width, dashStyle), out pen))
282+
{
283+
pen = CreatePen(color);
284+
pen.Width = width;
285+
pen.DashStyle = dashStyle;
286+
_penCache[(color, width, dashStyle)] = pen;
287+
}
288+
return pen;
269289
}
270-
return pen;
271290
}
272291

273292
/// <summary>
@@ -277,12 +296,15 @@ public RPen GetPen(RColor color)
277296
/// <returns>brush instance</returns>
278297
public RBrush GetSolidBrush(RColor color)
279298
{
280-
RBrush brush;
281-
if (!_brushesCache.TryGetValue(color, out brush))
299+
lock (_brushesCache)
282300
{
283-
_brushesCache[color] = brush = CreateSolidBrush(color);
301+
RBrush brush;
302+
if (!_brushesCache.TryGetValue(color, out brush))
303+
{
304+
_brushesCache[color] = brush = CreateSolidBrush(color);
305+
}
306+
return brush;
284307
}
285-
return brush;
286308
}
287309

288310
/// <summary>
@@ -372,7 +394,13 @@ public void AddFontFamilyMapping(string fromFamily, string toFamily)
372394
public virtual async Task<bool> AddFontFace(string familyName, RUri uri, int weight, bool isItalic, int stretch, IReadOnlyList<CodepointRange> ranges)
373395
{
374396
RFontFamily fontFamily;
375-
if (!_fontFaceCache.TryGetValue(uri.AbsoluteUri, out fontFamily))
397+
bool cached;
398+
lock (_fontFaceCache)
399+
{
400+
cached = _fontFaceCache.TryGetValue(uri.AbsoluteUri, out fontFamily);
401+
}
402+
403+
if (!cached)
376404
{
377405
var networkResponse = await GetResourceStream(uri).ConfigureAwait(false);
378406
if (networkResponse == null || networkResponse.ResourceStream == null)
@@ -404,7 +432,14 @@ public virtual async Task<bool> AddFontFace(string familyName, RUri uri, int wei
404432
return false;
405433
}
406434

407-
_fontFaceCache[uri.AbsoluteUri] = fontFamily;
435+
lock (_fontFaceCache)
436+
{
437+
// Another container may have loaded the same face in the mean time, keep the first
438+
if (_fontFaceCache.TryGetValue(uri.AbsoluteUri, out var existing))
439+
fontFamily = existing;
440+
else
441+
_fontFaceCache[uri.AbsoluteUri] = fontFamily;
442+
}
408443
}
409444

410445
_fontsHandler.AddFontFace(familyName, fontFamily, weight, isItalic, stretch, ranges);

‎Source/HtmlRenderer/Adapters/RGraphics.cs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,27 @@ protected RGraphics(RAdapter adapter, RRect initialClip)
5656
}
5757

5858
/// <summary>
59-
/// Get color pen.
59+
/// Get solid color pen, 1 wide.
6060
/// </summary>
6161
/// <param name="color">the color to get the pen for</param>
62-
/// <returns>pen instance</returns>
62+
/// <returns>pen instance, shared, don't change it</returns>
6363
public RPen GetPen(RColor color)
6464
{
6565
return _adapter.GetPen(color);
6666
}
6767

68+
/// <summary>
69+
/// Get color pen with the given width and dash style.
70+
/// </summary>
71+
/// <param name="color">the color to get the pen for</param>
72+
/// <param name="width">the width of the pen</param>
73+
/// <param name="dashStyle">the dash style of the pen</param>
74+
/// <returns>pen instance, shared, don't change it</returns>
75+
public RPen GetPen(RColor color, double width, RDashStyle dashStyle)
76+
{
77+
return _adapter.GetPen(color, width, dashStyle);
78+
}
79+
6880
/// <summary>
6981
/// Get solid color brush.
7082
/// </summary>

‎Source/HtmlRenderer/Core/Dom/CssBox.cs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,8 +1958,6 @@ internal void PaintDecoration(RGraphics g, RRect rectangle, bool isFirst, bool i
19581958

19591959
var bottomInset = ActualPaddingBottom - ActualBorderBottomWidth;
19601960
var pen = g.GetPen(ActualColor);
1961-
pen.Width = 1;
1962-
pen.DashStyle = RDashStyle.Solid;
19631961

19641962
// CSS 2.1 §16.3.1, the value can list several lines.
19651963
foreach (var line in TextDecoration.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))

‎Source/HtmlRenderer/Core/Handlers/BordersDrawHandler.cs‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,21 +282,15 @@ private static RGraphicsPath GetRoundedBorderPath(RGraphics g, Border border, Cs
282282
/// </summary>
283283
private static RPen GetPen(RGraphics g, string style, RColor color, double width)
284284
{
285-
var p = g.GetPen(color);
286-
p.Width = width;
287285
switch (style)
288286
{
289-
case "solid":
290-
p.DashStyle = RDashStyle.Solid;
291-
break;
292287
case "dotted":
293-
p.DashStyle = RDashStyle.Dot;
294-
break;
288+
return g.GetPen(color, width, RDashStyle.Dot);
295289
case "dashed":
296-
p.DashStyle = RDashStyle.Dash;
297-
break;
290+
return g.GetPen(color, width, RDashStyle.Dash);
291+
default:
292+
return g.GetPen(color, width, RDashStyle.Solid);
298293
}
299-
return p;
300294
}
301295

302296
/// <summary>

0 commit comments

Comments
 (0)