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; }
|
2022-12-13 08:52:35 +00:00
|
|
|
|
public IntPtr Handle { get; internal set; }
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public bool IsDisposed { get; private set; }
|
2022-03-04 01:16:39 +00:00
|
|
|
|
protected abstract Action<IntPtr, IntPtr> QueueDestroyFunction { get; }
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
2023-03-07 23:28:57 +00:00
|
|
|
|
internal WeakReference<GraphicsResource> weakReference;
|
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)
|
|
|
|
|
{
|
2023-03-07 23:28:57 +00:00
|
|
|
|
weakReference = new WeakReference<GraphicsResource>(this);
|
|
|
|
|
Device.AddResourceReference(weakReference);
|
2022-03-02 07:21:42 +00:00
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
2023-03-07 23:28:57 +00:00
|
|
|
|
if (weakReference != null)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
2022-03-04 01:16:39 +00:00
|
|
|
|
QueueDestroyFunction(Device.Handle, Handle);
|
2023-03-07 23:28:57 +00:00
|
|
|
|
Device.RemoveResourceReference(weakReference);
|
2023-05-12 00:56:40 +00:00
|
|
|
|
weakReference.SetTarget(null);
|
2023-03-07 23:28:57 +00:00
|
|
|
|
weakReference = null;
|
2022-02-23 05:14:32 +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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-20 03:33:27 +00:00
|
|
|
|
}
|