Repository navigation
[Windows] Fix GraphicsView crash during repeated drawing #37419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
|
|
||
| for (int i = 0; i < 100; i++) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
💡 Suggestion — device-test runtime/flake budget: The loop repeats the identical |
||
| { | ||
| 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)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| static TaskCompletionSource<W2DCanvas> CreateCompletionSource() => | ||
| new(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,8 +133,15 @@ private void OnDraw(CanvasControl sender, CanvasDrawEventArgs args) | |
| } | ||
| finally | ||
| { | ||
| _canvas.ResetState(); | ||
| PlatformGraphicsService.ThreadLocalCreator = null; | ||
| try | ||
| { | ||
| _canvas.ResetState(); | ||
| } | ||
| finally | ||
| { | ||
| _canvas.Session = null; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
💡 Suggestion — Image/Graphics (Windows) resource retention: Clearing |
||
| PlatformGraphicsService.ThreadLocalCreator = null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 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.Sessionis never assigned at all (orOnDrawearly-returns before assignment), which would silently break all WindowsGraphicsViewrendering while keeping this test green. Add a positive signal captured during the draw — e.g. haveCapturingDrawable.Drawrecord((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".