Extract interface from DisplaySurface

This commit is contained in:
YoshiRulz 2021-04-08 16:46:00 +10:00
parent bd9ec3c57d
commit 85eedb39fd
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
6 changed files with 39 additions and 23 deletions

View File

@ -39,9 +39,9 @@ namespace BizHawk.Client.Common
private Color? _defaultTextBackground = Color.FromArgb(128, 0, 0, 0);
private DisplaySurface _clientSurface;
private IDisplaySurface _clientSurface;
private DisplaySurface _GUISurface;
private IDisplaySurface _GUISurface;
private (int Left, int Top, int Right, int Bottom) _padding = (0, 0, 0, 0);
@ -73,7 +73,7 @@ namespace BizHawk.Client.Common
public void SetAttributes(ImageAttributes a) => _attributes = a;
private DisplaySurface GetRelevantSurface(DisplaySurfaceID? surfaceID)
private IDisplaySurface GetRelevantSurface(DisplaySurfaceID? surfaceID)
{
var nnID = surfaceID ?? _usingSurfaceID ?? throw new Exception();
void ThisIsTheLuaAutolockHack()

View File

@ -1,4 +1,3 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
@ -10,7 +9,7 @@ namespace BizHawk.Client.Common
/// This is a wrapper for a Bitmap, basically, which can also be a int[].
/// It should be phased out, in favor of BitmapBuffer and Texture2d's
/// </summary>
public unsafe class DisplaySurface : IDisposable
public unsafe class DisplaySurface : IDisplaySurface
{
private Bitmap _bmp;
private BitmapData _bmpData;
@ -28,9 +27,6 @@ namespace BizHawk.Client.Common
return _bmp;
}
/// <summary>
/// returns a Graphics object used to render to this surface. be sure to dispose it!
/// </summary>
public Graphics GetGraphics()
{
ToBitmap();

View File

@ -11,10 +11,10 @@ namespace BizHawk.Client.Common
/// <summary>locks the surface with ID <paramref name="surfaceID"/></summary>
/// <exception cref="InvalidOperationException">already locked, or unknown surface</exception>
DisplaySurface LockApiHawkSurface(DisplaySurfaceID surfaceID, bool clear = true);
IDisplaySurface LockApiHawkSurface(DisplaySurfaceID surfaceID, bool clear = true);
/// <summary>unlocks the given <paramref name="surface"/>, which must be a locked surface produced by <see cref="LockApiHawkSurface"/></summary>
/// <exception cref="InvalidOperationException">already unlocked</exception>
void UnlockApiHawkSurface(DisplaySurface surface);
void UnlockApiHawkSurface(IDisplaySurface surface);
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Drawing;
namespace BizHawk.Client.Common
{
public interface IDisplaySurface : IDisposable
{
int Height { get; }
int Width { get; }
void Clear();
/// <returns>a <see cref="Graphics"/> used to render to this surface; be sure to dispose it!</returns>
Graphics GetGraphics();
Bitmap PeekBitmap();
}
}

View File

@ -34,7 +34,7 @@ namespace BizHawk.Client.Common
private readonly IGL _gl;
private List<Texture2d> _currentTextures;
public Texture2d Get(DisplaySurface ds)
public Texture2d Get(IDisplaySurface ds)
{
using var bb = new BitmapBuffer(ds.PeekBitmap(), new BitmapLoadOptions());
return Get(bb);

View File

@ -1097,24 +1097,24 @@ namespace BizHawk.Client.EmuHawk
private bool? _lastVsyncSetting;
private GraphicsControl _lastVsyncSettingGraphicsControl;
private readonly Dictionary<DisplaySurfaceID, DisplaySurface> _apiHawkIDToSurface = new();
private readonly Dictionary<DisplaySurfaceID, IDisplaySurface> _apiHawkIDToSurface = new();
/// <remarks>Can't this just be a field/prop of <see cref="DisplaySurface"/>? --yoshi</remarks>
private readonly Dictionary<DisplaySurface, DisplaySurfaceID> _apiHawkSurfaceToID = new();
/// <remarks>Can't this just be a prop of <see cref="IDisplaySurface"/>? --yoshi</remarks>
private readonly Dictionary<IDisplaySurface, DisplaySurfaceID> _apiHawkSurfaceToID = new();
private readonly Dictionary<DisplaySurfaceID, SwappableDisplaySurfaceSet> _apiHawkSurfaceSets = new();
/// <summary>
/// Peeks a locked lua surface, or returns null if it isn't locked
/// </summary>
public DisplaySurface PeekApiHawkLockedSurface(DisplaySurfaceID surfaceID)
public IDisplaySurface PeekApiHawkLockedSurface(DisplaySurfaceID surfaceID)
{
if (_apiHawkIDToSurface.ContainsKey(surfaceID))
return _apiHawkIDToSurface[surfaceID];
return null;
}
public DisplaySurface LockApiHawkSurface(DisplaySurfaceID surfaceID, bool clear)
public IDisplaySurface LockApiHawkSurface(DisplaySurfaceID surfaceID, bool clear)
{
if (_apiHawkIDToSurface.ContainsKey(surfaceID))
{
@ -1141,7 +1141,7 @@ namespace BizHawk.Client.EmuHawk
_ => throw new ArgumentException(message: "not a valid enum member", paramName: nameof(surfaceID))
};
DisplaySurface ret = sdss.AllocateSurface(width, height, clear);
IDisplaySurface ret = sdss.AllocateSurface(width, height, clear);
_apiHawkIDToSurface[surfaceID] = ret;
_apiHawkSurfaceToID[ret] = surfaceID;
return ret;
@ -1167,19 +1167,20 @@ namespace BizHawk.Client.EmuHawk
}
}
/// <summary>unlocks this DisplaySurface which had better have been locked as a lua surface</summary>
/// <summary>unlocks this IDisplaySurface which had better have been locked as a lua surface</summary>
/// <exception cref="InvalidOperationException">already unlocked</exception>
public void UnlockApiHawkSurface(DisplaySurface surface)
public void UnlockApiHawkSurface(IDisplaySurface surface)
{
if (!_apiHawkSurfaceToID.ContainsKey(surface))
if (surface is not DisplaySurface dispSurfaceImpl) throw new ArgumentException("o noes", nameof(surface));
if (!_apiHawkSurfaceToID.ContainsKey(dispSurfaceImpl))
{
throw new InvalidOperationException("Surface was not locked as a lua surface");
}
var surfaceID = _apiHawkSurfaceToID[surface];
_apiHawkSurfaceToID.Remove(surface);
var surfaceID = _apiHawkSurfaceToID[dispSurfaceImpl];
_apiHawkSurfaceToID.Remove(dispSurfaceImpl);
_apiHawkIDToSurface.Remove(surfaceID);
_apiHawkSurfaceSets[surfaceID].SetPending(surface);
_apiHawkSurfaceSets[surfaceID].SetPending(dispSurfaceImpl);
}
// helper classes: