2021-01-20 03:33:27 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Graphics
|
|
|
|
|
{
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public abstract class GraphicsResource : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public GraphicsDevice Device { get; }
|
|
|
|
|
public IntPtr Handle { get; protected set; }
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public bool IsDisposed { get; private set; }
|
|
|
|
|
protected abstract Action<IntPtr, IntPtr, IntPtr> QueueDestroyFunction { get; }
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
private WeakReference<GraphicsResource> selfReference;
|
2021-01-27 05:06:15 +00:00
|
|
|
|
|
2022-03-02 07:21:42 +00:00
|
|
|
|
public GraphicsResource(GraphicsDevice device, bool trackResource = true)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
Device = device;
|
2021-01-27 05:06:15 +00:00
|
|
|
|
|
2022-03-02 07:21:42 +00:00
|
|
|
|
if (trackResource)
|
|
|
|
|
{
|
|
|
|
|
selfReference = new WeakReference<GraphicsResource>(this);
|
|
|
|
|
Device.AddResourceReference(selfReference);
|
|
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (!IsDisposed)
|
|
|
|
|
{
|
2022-02-19 05:02:16 +00:00
|
|
|
|
Device.PrepareDestroyResource(this, QueueDestroyFunction);
|
2021-01-27 05:06:15 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
if (selfReference != null)
|
|
|
|
|
{
|
|
|
|
|
Device.RemoveResourceReference(selfReference);
|
|
|
|
|
selfReference = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsDisposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~GraphicsResource()
|
|
|
|
|
{
|
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
|
Dispose(disposing: false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
|
Dispose(disposing: true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-20 03:33:27 +00:00
|
|
|
|
}
|