2021-01-20 03:33:27 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Graphics
|
|
|
|
|
{
|
|
|
|
|
public abstract class GraphicsResource : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public GraphicsDevice Device { get; }
|
|
|
|
|
public IntPtr Handle { get; protected set; }
|
|
|
|
|
|
|
|
|
|
public bool IsDisposed { get; private set; }
|
|
|
|
|
protected abstract Action<IntPtr, IntPtr> QueueDestroyFunction { get; }
|
|
|
|
|
|
2021-01-27 05:06:15 +00:00
|
|
|
|
private WeakReference selfReference;
|
|
|
|
|
|
2021-01-20 03:33:27 +00:00
|
|
|
|
public GraphicsResource(GraphicsDevice device)
|
|
|
|
|
{
|
|
|
|
|
Device = device;
|
2021-01-27 05:06:15 +00:00
|
|
|
|
|
|
|
|
|
selfReference = new WeakReference(this);
|
|
|
|
|
Device.AddResourceReference(selfReference);
|
2021-01-20 03:33:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (!IsDisposed)
|
|
|
|
|
{
|
|
|
|
|
QueueDestroyFunction(Device.Handle, Handle);
|
2021-01-27 05:06:15 +00:00
|
|
|
|
|
|
|
|
|
if (selfReference != null)
|
|
|
|
|
{
|
|
|
|
|
Device.RemoveResourceReference(selfReference);
|
|
|
|
|
selfReference = null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-20 03:33:27 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|