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
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
using System;
using System.Threading.Tasks;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Graphics.Win2D;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public partial class GraphicsViewHandlerTests
{
PlatformTouchGraphicsView GetPlatformGraphicsView(GraphicsViewHandler graphicsViewHandler) =>
graphicsViewHandler.PlatformView;

[Fact(DisplayName = "GraphicsView releases Win2D drawing sessions after repeated drawing")]
public async Task GraphicsViewReleasesWin2DDrawingSessionsAfterRepeatedDrawing()
{
await InvokeOnMainThreadAsync(async () =>
{
var drawable = new CapturingDrawable();
var graphicsView = new W2DGraphicsView
{
Width = 100,
Height = 100,
Drawable = drawable,
};

await graphicsView.AttachAndRun(
async () =>
{
var canvas = await drawable.NextDraw.WaitAsync(TimeSpan.FromSeconds(5));
Assert.Null(canvas.Session);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

💡 Suggestion — regression test is not discriminating: The only assertion is Assert.Null(canvas.Session) observed after the draw completes. This passes both for the intended fix and for a regression in which _canvas.Session is never assigned at all (or OnDraw early-returns before assignment), which would silently break all Windows GraphicsView rendering while keeping this test green. Add a positive signal captured during the draw — e.g. have CapturingDrawable.Draw record ((W2DCanvas)canvas).Session is not null (or perform a real draw call) and assert that recorded flag alongside the post-draw null check — so the test can distinguish "session released" from "session never set".


for (int i = 0; i < 100; i++)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

💡 Suggestion — device-test runtime/flake budget: The loop repeats the identical Assert.Null(canvas.Session) 100 times, each with a 5s WaitAsync. Iterations 2..100 add no new signal for this fix (the session is cleared in a finally on every OnDraw), but they do add up to 100 invalidate/present round-trips on a contended Windows CI agent, and any single dropped/coalesced CanvasControl.Invalidate() turns into a 5s TimeoutException. If the intent is to catch cumulative retention, assert on that directly (e.g. a WeakReference to a previously captured session being collected, or a memory threshold) and drop the iteration count to a handful; otherwise 2–3 iterations are sufficient to prove per-frame release.

{
var nextDraw = drawable.PrepareNextDraw();
graphicsView.Invalidate();
canvas = await nextDraw.WaitAsync(TimeSpan.FromSeconds(5));

Assert.Null(canvas.Session);
}
},
MauiContext);
});
}

sealed class CapturingDrawable : IDrawable
{
TaskCompletionSource<W2DCanvas> _nextDraw = CreateCompletionSource();

public Task<W2DCanvas> NextDraw => _nextDraw.Task;

public Task<W2DCanvas> PrepareNextDraw()
{
_nextDraw = CreateCompletionSource();
return _nextDraw.Task;
}

public void Draw(ICanvas canvas, RectF dirtyRect)
{
_nextDraw.TrySetResult(Assert.IsType<W2DCanvas>(canvas));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

⚠️ Warning — test failure mode: Assert.IsType<W2DCanvas>(canvas) runs inside IDrawable.Draw, i.e. inside PlatformGraphicsView.OnDraw, which is invoked from Win2D's CanvasControl.Draw event on the UI thread. If the assertion fails, the XunitException propagates out of the draw callback into Win2D/WinUI rather than into the test, and — because TrySetResult is never reached — the awaiting side additionally blocks until the 5s WaitAsync timeout. The result is an unhandled-exception host crash or an opaque TimeoutException instead of a readable assertion failure. Capture the canvas (_nextDraw.TrySetResult(canvas as W2DCanvas) or store the raw ICanvas) and perform the type assertion on the awaiting side, where xunit can report it.

}

static TaskCompletionSource<W2DCanvas> CreateCompletionSource() =>
new(TaskCreationOptions.RunContinuationsAsynchronously);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,15 @@ private void OnDraw(CanvasControl sender, CanvasDrawEventArgs args)
}
finally
{
_canvas.ResetState();
PlatformGraphicsService.ThreadLocalCreator = null;
try
{
_canvas.ResetState();
}
finally
{
_canvas.Session = null;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

💡 Suggestion — Image/Graphics (Windows) resource retention: Clearing _canvas.Session releases the per-frame CanvasDrawingSession, but PlatformCanvas also caches _patternContext and _effectContext (CanvasRenderTarget instances constructed from that session, see GetOrCreatePatternContext/GetOrCreateEffectContext). Those are only disposed when the requested size changes, are never disposed on UserControl_Unloaded, and the _canvas field is readonly and lives for the lifetime of the view — so for drawables that use shadow, blur, or pattern fills the Win2D device resources are still pinned after the view is unloaded. Concrete scenario: a GraphicsView whose drawable calls SetShadow/SetBlur is navigated away from; the session is now released but the effect render target keeps the device surface alive. Consider disposing the cached render targets/effects at the same point (or from UserControl_Unloaded) so the release is complete.

PlatformGraphicsService.ThreadLocalCreator = null;
}
}
}
}
Expand Down
Loading