2021-01-20 03:33:27 +00:00
|
|
|
|
using System;
|
2023-11-20 23:18:06 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2023-12-09 00:07:38 +00:00
|
|
|
|
using System.Threading;
|
2021-01-20 03:33:27 +00:00
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Graphics
|
|
|
|
|
{
|
2023-12-09 00:07:38 +00:00
|
|
|
|
// TODO: give this a Name property for debugging use
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public abstract class GraphicsResource : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public GraphicsDevice Device { get; }
|
2023-12-09 00:07:38 +00:00
|
|
|
|
public IntPtr Handle { get => handle; internal set => handle = value; }
|
|
|
|
|
private nint handle;
|
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-11-21 01:09:22 +00:00
|
|
|
|
private GCHandle SelfReference;
|
|
|
|
|
protected GraphicsResource(GraphicsDevice device)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
Device = device;
|
2021-01-27 05:06:15 +00:00
|
|
|
|
|
2023-11-21 01:09:22 +00:00
|
|
|
|
SelfReference = GCHandle.Alloc(this, GCHandleType.Weak);
|
|
|
|
|
Device.AddResourceReference(SelfReference);
|
2023-10-04 21:45:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 01:09:22 +00:00
|
|
|
|
protected void Dispose(bool disposing)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
if (!IsDisposed)
|
|
|
|
|
{
|
2023-12-09 00:07:38 +00:00
|
|
|
|
if (disposing)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
2023-11-21 01:09:22 +00:00
|
|
|
|
Device.RemoveResourceReference(SelfReference);
|
|
|
|
|
SelfReference.Free();
|
2023-12-09 00:07:38 +00:00
|
|
|
|
}
|
2023-10-04 21:45:17 +00:00
|
|
|
|
|
2023-12-09 00:07:38 +00:00
|
|
|
|
// Atomically call destroy function in case this is called from the finalizer thread
|
|
|
|
|
var toDispose = Interlocked.Exchange(ref handle, IntPtr.Zero);
|
|
|
|
|
if (toDispose != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
QueueDestroyFunction(Device.Handle, toDispose);
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsDisposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~GraphicsResource()
|
|
|
|
|
{
|
2023-10-04 21:45:17 +00:00
|
|
|
|
#if DEBUG
|
2023-12-09 00:07:38 +00:00
|
|
|
|
// If you see this log message, you leaked a graphics resource without disposing it!
|
|
|
|
|
// We'll try to clean it up for you but you really should fix this.
|
|
|
|
|
Logger.LogWarn($"A resource of type {GetType().Name} was not Disposed.");
|
2023-10-04 21:45:17 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2023-12-09 00:07:38 +00:00
|
|
|
|
Dispose(false);
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|