Skip to content
Merged
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
26 changes: 26 additions & 0 deletions AutoMask/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;

namespace AutoSplit_AutoMask;

Expand All @@ -16,6 +17,31 @@ public override void OnFrameworkInitializationCompleted()
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow();

// Catches uncaught exceptions raised on the UI dispatcher (including async-void
// event handlers' continuations). Setting Handled = true keeps the app running
// instead of bubbling to AppDomain.UnhandledException and terminating; the user
// gets a MessageBox and can retry the action that failed.
Dispatcher.UIThread.UnhandledException += (_, e) =>
{
Utils.LogCrashToDisk(e.Exception, "Dispatcher.UnhandledException");

try
{
if (desktop.MainWindow is { IsVisible: true } mainWindow)
{
_ = MessageBox.Show(mainWindow, "Something went wrong", e.Exception.Message);
}
}
catch
{
// Avalonia's documentation explicitly warns against allocating or
// performing resource-heavy work in this handler — secondary failures
// here can't be reported anywhere useful.
}

e.Handled = true;
};
}

base.OnFrameworkInitializationCompleted();
Expand Down
6 changes: 6 additions & 0 deletions AutoMask/AppJsonContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

namespace AutoSplit_AutoMask;

// Nested generic and element types are registered explicitly so trimming/AOT can't remove
// reflection metadata the source-gen serializer relies on. Keep this in sync with any new
// model field types.
[JsonSerializable(typeof(SplitPreset))]
[JsonSerializable(typeof(Split))]
[JsonSerializable(typeof(List<Split>))]
[JsonSerializable(typeof(PremadeSplitsFile))]
[JsonSerializable(typeof(PremadeSplit))]
[JsonSerializable(typeof(List<PremadeSplit>))]
[JsonSerializable(typeof(CapturePreferences))]
[JsonSourceGenerationOptions(PropertyNameCaseInsensitive = true)]
internal partial class AppJsonContext : JsonSerializerContext;
2 changes: 1 addition & 1 deletion AutoMask/AutoMask.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);AVLN3001</NoWarn>
<ApplicationIcon>Assets/icon.ico</ApplicationIcon>
<Version>0.9.2-alpha</Version>
<Version>0.9.3-alpha</Version>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
</PropertyGroup>

Expand Down
72 changes: 54 additions & 18 deletions AutoMask/Capture/BitBltCaptureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

namespace AutoSplit_AutoMask.Capture;

// Shared plumbing for window + region capture. Each BitBlt grabs the current subject rect into
// a reusable GDI compatible bitmap, then GetDIBits copies it into a SkiaSharp-backed buffer.
[SupportedOSPlatform("windows")]
public abstract class BitBltCaptureBase : ICaptureSource
{
Expand Down Expand Up @@ -42,22 +40,33 @@ public bool TryGrabFrame(out SKBitmap? frame)
return false;
}

if (w <= 0 || h <= 0)
// try/finally so any throw between acquire and release (EnsureBuffers GDI exhaustion,
// BitBlt fault, allocator OOM) doesn't strand srcDc. ReleaseDC must run for caret-DC
// and window-DC paths or the system DC pool fills up.
bool blitOk;
try
{
Win32.ReleaseDC(owningHwnd, srcDc);
return false;
}

SourceWidth = w;
SourceHeight = h;
if (w <= 0 || h <= 0)
{
return false;
}

EnsureBuffers(srcDc, w, h);
SourceWidth = w;
SourceHeight = h;

bool ok = Win32.BitBlt(_memDc, 0, 0, w, h, srcDc, ox, oy, Win32.SRCCOPY | Win32.CAPTUREBLT);
if (!EnsureBuffers(srcDc, w, h))
{
return false;
}

Win32.ReleaseDC(owningHwnd, srcDc);
blitOk = Win32.BitBlt(_memDc, 0, 0, w, h, srcDc, ox, oy, Win32.SRCCOPY | Win32.CAPTUREBLT);
}
finally
{
Win32.ReleaseDC(owningHwnd, srcDc);
}

if (!ok)
if (!blitOk)
{
return false;
}
Expand Down Expand Up @@ -87,27 +96,54 @@ public bool TryGrabFrame(out SKBitmap? frame)

if (scans == 0)
{
// Drop the bitmap so a transient GetDIBits failure (e.g. driver glitch on
// resolution change) doesn't leave a half-populated buffer that a later
// size-match call would skip re-allocating.
_frame.Dispose();
_frame = null;
return false;
}

frame = _frame;
return true;
}

private void EnsureBuffers(IntPtr srcDc, int w, int h)
private bool EnsureBuffers(IntPtr srcDc, int w, int h)
{
if (_memDc != IntPtr.Zero && w == _allocatedWidth && h == _allocatedHeight)
{
return;
return true;
}

ReleaseGdiHandles();

_memDc = Win32.CreateCompatibleDC(srcDc);
_gdiBitmap = Win32.CreateCompatibleBitmap(srcDc, w, h);
_oldObject = Win32.SelectObject(_memDc, _gdiBitmap);
IntPtr memDc = Win32.CreateCompatibleDC(srcDc);
if (memDc == IntPtr.Zero)
{
return false;
}

IntPtr gdiBitmap = Win32.CreateCompatibleBitmap(srcDc, w, h);
if (gdiBitmap == IntPtr.Zero)
{
Win32.DeleteDC(memDc);
return false;
}

IntPtr oldObject = Win32.SelectObject(memDc, gdiBitmap);
if (oldObject == IntPtr.Zero)
{
Win32.DeleteObject(gdiBitmap);
Win32.DeleteDC(memDc);
return false;
}

_memDc = memDc;
_gdiBitmap = gdiBitmap;
_oldObject = oldObject;
_allocatedWidth = w;
_allocatedHeight = h;
return true;
}

private void ReleaseGdiHandles()
Expand Down
Loading