Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/BannedSymbols.BannedApiAnalyzers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ M:System.Diagnostics.Debug.Assert(System.Boolean);include a unique message
M:System.Diagnostics.Debug.WriteLine(System.String);use Util.DebugWriteLine (first-party) because apparently the BCL's version doesn't work properly in some circumstances?
M:System.Diagnostics.Trace.Assert(System.Boolean);include a unique message
M:System.Diagnostics.Trace.Assert(System.Boolean,System.String);just do the check and throw a specific exception
M:System.Drawing.Bitmap.#ctor(System.Int32,System.Int32);use BitmapBuffer.CreateBitmapObject
M:System.Drawing.Bitmap.#ctor(System.Int32,System.Int32,System.Drawing.Imaging.PixelFormat);use BitmapBuffer.CreateBitmapObject
M:System.Random.#ctor;this is a source of nondeterminism
M:System.String.GetHashCode;use StableStringHash extension
P:System.StringComparer.InvariantCultureIgnoreCase;use OrdinalIgnoreCase
Expand Down
9 changes: 8 additions & 1 deletion src/BizHawk.Bizware.Graphics/BitmapBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ namespace BizHawk.Bizware.Graphics
/// </summary>
public unsafe class BitmapBuffer : IDisposable
{
public static Bitmap CreateBitmapObject(Size size, PixelFormat format = PixelFormat.Format32bppArgb)
=> size.Width is >= 1 and <= 0xFFFF || size.Height is >= 1 and <= 0xFFFF
#pragma warning disable RS0030 // this is the sanctioned call-site
? new Bitmap(width: size.Width, height: size.Height, format)
#pragma warning restore RS0030
: throw new ArgumentOutOfRangeException(paramName: nameof(size), size, message: "width and height must each be in the range 1..<65536");

public int Width, Height;
public int[] Pixels;

Expand Down Expand Up @@ -568,7 +575,7 @@ public Bitmap ToSysdrawingBitmap()
}

var pf = HasAlpha ? PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb;
var bmp = new Bitmap(Width, Height, pf);
var bmp = CreateBitmapObject(Size, pf);
ToSysdrawingBitmap(bmp);
return bmp;
}
Expand Down
3 changes: 1 addition & 2 deletions src/BizHawk.Bizware.Graphics/GDIPlus/GDIPlusTexture2D.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Drawing;
using System.Drawing.Imaging;

namespace BizHawk.Bizware.Graphics
{
Expand All @@ -16,7 +15,7 @@ public GDIPlusTexture2D(int width, int height)
{
Width = width;
Height = height;
SDBitmap = new(width, height, PixelFormat.Format32bppArgb);
SDBitmap = BitmapBuffer.CreateBitmapObject(new(width: width, height: height));
}

public virtual void Dispose()
Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Bizware.Graphics/Renderers/ImGui2DRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public ITexture2D Render(int width, int height)
_stringTexture?.Dispose();
_stringGraphics?.Dispose();
_stringOutput?.Dispose();
_stringOutput = new(width, height, PixelFormat.Format32bppArgb);
_stringOutput = BitmapBuffer.CreateBitmapObject(new(width: width, height: height));
_stringGraphics = SDGraphics.FromImage(_stringOutput);
_stringTexture = _igl.CreateTexture(width, height);
}
Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Client.Common/Api/Classes/GuiApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed class GuiApi : IGuiApi

private readonly Dictionary<string, Bitmap> _imageCache = new();

private readonly Bitmap _nullGraphicsBitmap = new(1, 1);
private readonly Bitmap _nullGraphicsBitmap = BitmapBuffer.CreateBitmapObject(new(1, 1));

private CompositingMode _compositingMode = CompositingMode.SourceOver;

Expand Down
3 changes: 2 additions & 1 deletion src/BizHawk.Client.EmuHawk/AVOut/GifWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO;
using System.Drawing;

using BizHawk.Bizware.Graphics;
using BizHawk.Client.Common;
using BizHawk.Emulation.Common;

Expand Down Expand Up @@ -167,7 +168,7 @@ public void AddFrame(IVideoProvider source)
return; // skip this frame
}

using var bmp = new Bitmap(source.BufferWidth, source.BufferHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using var bmp = BitmapBuffer.CreateBitmapObject(new(width: source.BufferWidth, height: source.BufferHeight));
var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
System.Runtime.InteropServices.Marshal.Copy(source.GetVideoBuffer(), 0, data.Scan0, bmp.Width * bmp.Height);
bmp.UnlockBits(data);
Expand Down
4 changes: 3 additions & 1 deletion src/BizHawk.Client.EmuHawk/CustomControls/ViewportPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Drawing.Drawing2D;
using System.Windows.Forms;

using BizHawk.Bizware.Graphics;

namespace BizHawk.Client.EmuHawk
{
/// <summary>
Expand Down Expand Up @@ -38,7 +40,7 @@ public RetainedViewportPanel(bool doubleBuffer = false)
SetStyle(ControlStyles.Opaque, true);
SetStyle(ControlStyles.UserMouse, true);

SetBitmap(new Bitmap(2, 2));
SetBitmap(BitmapBuffer.CreateBitmapObject(new(2, 2)));
}


Expand Down
3 changes: 2 additions & 1 deletion src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Text;
using System.Windows.Forms;

using BizHawk.Bizware.Graphics;
using BizHawk.Client.Common;
using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
Expand Down Expand Up @@ -318,7 +319,7 @@ public static void Set(this DragEventArgs e, DragDropEffects effect)

public static Bitmap ToBitMap(this Control control)
{
var b = new Bitmap(control.Width, control.Height);
var b = BitmapBuffer.CreateBitmapObject(control.Size);
var rect = new Rectangle(new Point(0, 0), control.Size);
control.DrawToBitmap(b, rect);
return b;
Expand Down
3 changes: 2 additions & 1 deletion src/BizHawk.Client.EmuHawk/FormBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Drawing;
using System.Windows.Forms;

using BizHawk.Bizware.Graphics;
using BizHawk.Client.Common;
using BizHawk.Common;

Expand All @@ -16,7 +17,7 @@ public class FormBase : Form
/// <summary>removes transparency from an image by combining it with a solid background</summary>
public static Image FillImageBackground(Image img, Color c)
{
Bitmap result = new(width: img.Width, height: img.Height);
var result = BitmapBuffer.CreateBitmapObject(img.Size);
using var g = Graphics.FromImage(result);
g.Clear(c);
g.DrawImage(img, x: 0, y: 0, width: img.Width, height: img.Height);
Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Client.EmuHawk/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3353,7 +3353,7 @@ private void RecordAvBase(string videoWriterName, string filename, bool unattend

bbIn.DiscardAlpha();

Bitmap bmpOut = new(width: Config.AVWriterResizeWidth, height: Config.AVWriterResizeHeight, PixelFormat.Format32bppArgb);
var bmpOut = BitmapBuffer.CreateBitmapObject(new(width: Config.AVWriterResizeWidth, height: Config.AVWriterResizeHeight));
bmpIn = bbIn.ToSysdrawingBitmap();
using (var g = Graphics.FromImage(bmpOut))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Drawing.Drawing2D;
using System.Windows.Forms;

using BizHawk.Bizware.Graphics;

namespace BizHawk.Client.EmuHawk
{
/// <summary>
Expand Down Expand Up @@ -43,7 +45,7 @@ public RCheevosAchievementForm(RCheevos.Cheevo cheevo, Func<uint, string> getChe

private static Bitmap UpscaleBadge(Bitmap src)
{
var ret = new Bitmap(120, 120);
var ret = BitmapBuffer.CreateBitmapObject(new(120, 120));
using var g = Graphics.FromImage(ret);
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.PixelOffsetMode = PixelOffsetMode.Half;
Expand Down
4 changes: 3 additions & 1 deletion src/BizHawk.Client.EmuHawk/config/GB/BmpView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Drawing;
using System.Windows.Forms;

using BizHawk.Bizware.Graphics;

namespace BizHawk.Client.EmuHawk
{
public class BmpView : Control
Expand Down Expand Up @@ -81,7 +83,7 @@ public void ChangeBitmapSize(int w, int h)
}


Bmp = new Bitmap(w, h, PixelFormat.Format32bppArgb);
Bmp = BitmapBuffer.CreateBitmapObject(new(width: w, height: h));
BmpView_SizeChanged(null, EventArgs.Empty);
Refresh();
}
Expand Down
3 changes: 2 additions & 1 deletion src/BizHawk.Client.EmuHawk/config/NES/NESGraphicsConfig.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Drawing;
using System.Windows.Forms;

using BizHawk.Bizware.Graphics;
using BizHawk.Common;
using BizHawk.Client.Common;
using BizHawk.Emulation.Common;
Expand Down Expand Up @@ -75,7 +76,7 @@ private void SetPaletteImage()
int w = pictureBoxPalette.Size.Width;
int h = pictureBoxPalette.Size.Height;

var bmp = new Bitmap(w, h);
var bmp = BitmapBuffer.CreateBitmapObject(pictureBoxPalette.Size);
for (int j = 0; j < h; j++)
{
int cy = j * 4 / h;
Expand Down
3 changes: 2 additions & 1 deletion src/BizHawk.Client.EmuHawk/config/NES/QuickNesConfig.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Drawing;
using System.Windows.Forms;

using BizHawk.Bizware.Graphics;
using BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES;
using BizHawk.Client.Common;
using BizHawk.Common;
Expand Down Expand Up @@ -38,7 +39,7 @@ private void SetPaletteImage()
{
int w = pictureBox1.Size.Width;
int h = pictureBox1.Size.Height;
var bmp = new Bitmap(w, h);
var bmp = BitmapBuffer.CreateBitmapObject(pictureBox1.Size);
var pal = _settings.Palette;

for (int j = 0; j < h; j++)
Expand Down
18 changes: 16 additions & 2 deletions src/BizHawk.Client.EmuHawk/tools/GB/GBPrinterView.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using BizHawk.Bizware.Graphics;
using BizHawk.Client.Common;
using BizHawk.Common;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.Consoles.Nintendo.Gameboy;

Expand Down Expand Up @@ -86,9 +89,19 @@ private void OnPrint(IntPtr image, byte height, byte topMargin, byte bottomMargi
// In this implementation:
// the bottom margin and top margin are just white lines at the top and bottom
// exposure is ignored
// height of 0 is treated as a height of 1 but zeroed data
var shouldFreePointer = false;
if (height is 0)
{
var sizeBytes = PaperWidth * sizeof(uint);
image = Marshal.AllocCoTaskMem(sizeBytes);
shouldFreePointer = true;
Util.UnsafeSpanFromPointer(image, length: sizeBytes).Fill(0);
height = 1;
}

// The page received image
var page = new Bitmap(PaperWidth, height);
var page = BitmapBuffer.CreateBitmapObject(new(width: PaperWidth, height: height));

var bmp = page.LockBits(new Rectangle(0, 0, PaperWidth, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

Expand All @@ -107,6 +120,7 @@ private void OnPrint(IntPtr image, byte height, byte topMargin, byte bottomMargi
}
}

if (shouldFreePointer) Marshal.FreeCoTaskMem(image);
page.UnlockBits(bmp);

// add it to the bottom of the history
Expand Down Expand Up @@ -147,7 +161,7 @@ private void ClearPaper()
private void ResizeHistory(int height)
{
// copy to a new image of height
var newHistory = new Bitmap(PaperWidth, height);
var newHistory = BitmapBuffer.CreateBitmapObject(new(width: PaperWidth, height: height));
using (var g = Graphics.FromImage(newHistory))
{
g.Clear(Color.FromArgb((int)PaperColor));
Expand Down
3 changes: 2 additions & 1 deletion src/BizHawk.Client.EmuHawk/tools/Lua/LuaCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Windows.Forms;
using System.IO;

using BizHawk.Bizware.Graphics;
using BizHawk.Client.Common;
using BizHawk.Common.PathExtensions;

Expand Down Expand Up @@ -74,7 +75,7 @@ public LuaCanvas(
// was this done after reflow for a reason? --yoshi
luaPictureBox.Width = width;
luaPictureBox.Height = height;
luaPictureBox.Image = new Bitmap(width, height);
luaPictureBox.Image = BitmapBuffer.CreateBitmapObject(luaPictureBox.Size);
PerformLayout();
}

Expand Down
5 changes: 3 additions & 2 deletions src/BizHawk.Client.EmuHawk/tools/Lua/LuaPictureBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Windows.Forms;

using BizHawk.Bizware.Graphics;
using BizHawk.Client.Common;
using BizHawk.Common.CollectionExtensions;

Expand Down Expand Up @@ -40,7 +41,7 @@ private Pen GetPen([LuaColorParam] object color)

public LuaPictureBox(NLuaTableHelper tableHelper, Action<string> logOutputCallback)
{
Image = new Bitmap(Width, Height);
Image = BitmapBuffer.CreateBitmapObject(Size);
LogOutputCallback = logOutputCallback;
TableHelper = tableHelper;
}
Expand All @@ -49,7 +50,7 @@ public void LuaResize(int width, int height)
{
Width = width;
Height = height;
Image = new Bitmap(width, height);
Image = BitmapBuffer.CreateBitmapObject(Size);
}

public void Clear([LuaColorParam] object color)
Expand Down
9 changes: 5 additions & 4 deletions src/BizHawk.Client.EmuHawk/tools/NES/NESPPU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Drawing.Imaging;
using System.Windows.Forms;

using BizHawk.Bizware.Graphics;
using BizHawk.Client.Common;
using BizHawk.Emulation.Cores.Nintendo.NES;
using BizHawk.Emulation.Common;
Expand All @@ -24,7 +25,7 @@ public static Icon ToolIcon

private int _scanline;

private Bitmap _zoomBoxDefaultImage = new Bitmap(64, 64);
private Bitmap _zoomBoxDefaultImage = BitmapBuffer.CreateBitmapObject(new(64, 64));
private bool _forceChange;

[RequiredService]
Expand Down Expand Up @@ -300,7 +301,7 @@ private void UpdatePaletteSelection()
private static Bitmap Section(Image srcBitmap, Rectangle section, bool is8x16)
{
// Create the new bitmap and associated graphics object
var bmp = new Bitmap(64, 64);
var bmp = BitmapBuffer.CreateBitmapObject(new(64, 64));
var g = Graphics.FromImage(bmp);

// Draw the specified section of the source bitmap to the new one
Expand Down Expand Up @@ -473,7 +474,7 @@ private void SpriteRefreshMenuItem_Click(object sender, EventArgs e)

private void NesPPU_MouseClick(object sender, MouseEventArgs e)
{
ZoomBox.Image = new Bitmap(64, 64);
ZoomBox.Image = BitmapBuffer.CreateBitmapObject(new(64, 64));
}

private void NesPPU_KeyDown(object sender, KeyEventArgs e)
Expand Down Expand Up @@ -666,7 +667,7 @@ private void HandlePaletteViewMouseMove(Point e)
AddressLabel.Text = $"Address: 0x{addr:X4}";
int val;

var bmp = new Bitmap(64, 64);
var bmp = BitmapBuffer.CreateBitmapObject(new(64, 64));
var g = Graphics.FromImage(bmp);

byte[] palRam = _ppu.GetPalRam();
Expand Down
6 changes: 4 additions & 2 deletions src/BizHawk.Client.EmuHawk/tools/NES/NameTableViewer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Drawing;
using System.Windows.Forms;

using BizHawk.Bizware.Graphics;

namespace BizHawk.Client.EmuHawk
{
public sealed class NameTableViewer : Control
Expand All @@ -10,7 +12,7 @@ public sealed class NameTableViewer : Control
public NameTableViewer()
{
var pSize = new Size(512, 480);
Nametables = new Bitmap(pSize.Width, pSize.Height);
Nametables = BitmapBuffer.CreateBitmapObject(pSize);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
Expand Down Expand Up @@ -70,7 +72,7 @@ private void NameTableViewer_Paint(object sender, PaintEventArgs e)

public void ScreenshotToClipboard()
{
using var b = new Bitmap(Width, Height);
using var b = BitmapBuffer.CreateBitmapObject(Size);
var rect = new Rectangle(new Point(0, 0), Size);
DrawToBitmap(b, rect);
Clipboard.SetImage(b);
Expand Down
4 changes: 3 additions & 1 deletion src/BizHawk.Client.EmuHawk/tools/NES/PaletteViewer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Drawing;
using System.Windows.Forms;

using BizHawk.Bizware.Graphics;

namespace BizHawk.Client.EmuHawk
{
public sealed class PaletteViewer : Control
Expand Down Expand Up @@ -76,7 +78,7 @@ public bool HasChanged()

public void ScreenshotToClipboard()
{
var b = new Bitmap(Width, Height);
var b = BitmapBuffer.CreateBitmapObject(Size);
var rect = new Rectangle(new Point(0, 0), Size);
DrawToBitmap(b, rect);

Expand Down
Loading