|
@@ -2,23 +2,26 @@ using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
using Avalonia.Media.Imaging;
|
|
using Avalonia.Media.Imaging;
|
|
using Avalonia.Platform;
|
|
using Avalonia.Platform;
|
|
|
|
+using Avalonia.Threading;
|
|
using Avalonia.Visuals.Media.Imaging;
|
|
using Avalonia.Visuals.Media.Imaging;
|
|
|
|
|
|
namespace Avalonia.Media
|
|
namespace Avalonia.Media
|
|
{
|
|
{
|
|
public sealed class DrawingContext : IDisposable
|
|
public sealed class DrawingContext : IDisposable
|
|
{
|
|
{
|
|
|
|
+ private readonly bool _ownsImpl;
|
|
private int _currentLevel;
|
|
private int _currentLevel;
|
|
|
|
|
|
|
|
|
|
- static readonly Stack<Stack<PushedState>> StateStackPool = new Stack<Stack<PushedState>>();
|
|
|
|
- static readonly Stack<Stack<TransformContainer>> TransformStackPool = new Stack<Stack<TransformContainer>>();
|
|
|
|
|
|
+ private static ThreadSafeObjectPool<Stack<PushedState>> StateStackPool { get; } =
|
|
|
|
+ ThreadSafeObjectPool<Stack<PushedState>>.Default;
|
|
|
|
|
|
- private Stack<PushedState> _states = StateStackPool.Count == 0 ? new Stack<PushedState>() : StateStackPool.Pop();
|
|
|
|
|
|
+ private static ThreadSafeObjectPool<Stack<TransformContainer>> TransformStackPool { get; } =
|
|
|
|
+ ThreadSafeObjectPool<Stack<TransformContainer>>.Default;
|
|
|
|
|
|
- private Stack<TransformContainer> _transformContainers = TransformStackPool.Count == 0
|
|
|
|
- ? new Stack<TransformContainer>()
|
|
|
|
- : TransformStackPool.Pop();
|
|
|
|
|
|
+ private Stack<PushedState> _states = StateStackPool.Get();
|
|
|
|
+
|
|
|
|
+ private Stack<TransformContainer> _transformContainers = TransformStackPool.Get();
|
|
|
|
|
|
readonly struct TransformContainer
|
|
readonly struct TransformContainer
|
|
{
|
|
{
|
|
@@ -35,6 +38,13 @@ namespace Avalonia.Media
|
|
public DrawingContext(IDrawingContextImpl impl)
|
|
public DrawingContext(IDrawingContextImpl impl)
|
|
{
|
|
{
|
|
PlatformImpl = impl;
|
|
PlatformImpl = impl;
|
|
|
|
+ _ownsImpl = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public DrawingContext(IDrawingContextImpl impl, bool ownsImpl)
|
|
|
|
+ {
|
|
|
|
+ _ownsImpl = ownsImpl;
|
|
|
|
+ PlatformImpl = impl;
|
|
}
|
|
}
|
|
|
|
|
|
public IDrawingContextImpl PlatformImpl { get; }
|
|
public IDrawingContextImpl PlatformImpl { get; }
|
|
@@ -299,11 +309,14 @@ namespace Avalonia.Media
|
|
{
|
|
{
|
|
while (_states.Count != 0)
|
|
while (_states.Count != 0)
|
|
_states.Peek().Dispose();
|
|
_states.Peek().Dispose();
|
|
- StateStackPool.Push(_states);
|
|
|
|
|
|
+ StateStackPool.Return(_states);
|
|
_states = null;
|
|
_states = null;
|
|
- TransformStackPool.Push(_transformContainers);
|
|
|
|
|
|
+ if (_transformContainers.Count != 0)
|
|
|
|
+ throw new InvalidOperationException("Transform container stack is non-empty");
|
|
|
|
+ TransformStackPool.Return(_transformContainers);
|
|
_transformContainers = null;
|
|
_transformContainers = null;
|
|
- PlatformImpl.Dispose();
|
|
|
|
|
|
+ if (_ownsImpl)
|
|
|
|
+ PlatformImpl.Dispose();
|
|
}
|
|
}
|
|
|
|
|
|
private static bool PenIsVisible(Pen pen)
|
|
private static bool PenIsVisible(Pen pen)
|