Skip to content
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

[net7.0] Fixed Android's StreamImageSourceService.LoadDrawableAsync() #16640

Merged
merged 9 commits into from
Sep 20, 2023
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
3 changes: 3 additions & 0 deletions src/Controls/tests/DeviceTests/Controls.DeviceTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

<ItemGroup>
<MauiImage Include="Resources\Images\*" />
<MauiImage Remove="Resources\Images\red-embedded.png" />
<None Remove="Resources\Images\red-embedded.png" />
<EmbeddedResource Include="Resources\Images\red-embedded.png" LogicalName="red-embedded.png" />
<MauiIcon Include="Resources\appicon.svg" ForegroundFile="Resources\appiconfg.svg" Color="#512BD4" />
<MauiSplashScreen Include="Resources\appiconfg.svg" Color="#512BD4" BaseSize="128,128" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Threading.Tasks;
using Android.Graphics.Drawables;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Xunit;

namespace Microsoft.Maui.DeviceTests
Expand Down Expand Up @@ -37,6 +39,45 @@ await InvokeOnMainThreadAsync(async () =>
await bitmapDrawable1.Bitmap.AssertNotEqualAsync(bitmapDrawable2.Bitmap);
});
}

[Fact]
public async Task ImageSetFromStreamRenders()
{
SetupBuilder();
var layout = new VerticalStackLayout()
{
HeightRequest = 100,
Copy link
Member

Choose a reason for hiding this comment

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

@mattleibow @jstedfast I modified this test to just set the height/width because I don't think the remeasuring of the layout is relevant to the test here.

I also don't think this test completely accurately embodies the original issue. I rolled back the changes from this PR and this test still passes without throwing a UIThread exception

Copy link
Member

Choose a reason for hiding this comment

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

I haven't been able to reproduce the original issue outside the sample repository.

I started a branch here to attempt reproducing
https://github.com/dotnet/maui/tree/test_for_Issue14052 but haven't had any luck yet.

I did test this PR directly against the repro and was able to reproduce the crash and then verified this PR fixes the crash.

This fix has been on NET8 since preview5 so I feel alright to merge this Backport

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

D'oh, this Width/HeightRequest change makes total sense. I was assuming the issue was that the screenshot was happening before layout had finished or something and so the width/height of the element wasn't yet known (hence the -1 values).

Copy link
Member

Choose a reason for hiding this comment

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

FWIW, I think the test only ever randomly failed - I don't think it failed every run.

WidthRequest = 100
};

using var stream = GetType().Assembly.GetManifestResourceStream("red-embedded.png");

var image = new Image
{
Source = ImageSource.FromStream(() => stream)
};

layout.Add(image);

await InvokeOnMainThreadAsync(async () =>
{
var handler = CreateHandler<LayoutHandler>(layout);

await handler.ToPlatform().AttachAndRun(async () =>
{
await image.Wait();
await handler.ToPlatform().AssertContainsColor(Colors.Red
#if WINDOWS
, handler.MauiContext
#endif
);
}
#if WINDOWS
, handler.MauiContext
#endif
);
});
}
}

// This subclass of memory stream is deliberately set up to trick Glide into using the cached image
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? s
return;
this.completed = true;

// set the image
this.view.setImageDrawable(resource);
post(() -> {
// set the image
this.view.setImageDrawable(resource);

// trigger the callback out of this target
post(() -> callback.onComplete(true, resource, this::clear));
// trigger the callback out of this target
callback.onComplete(true, resource, this::clear);
});
}

private void post(Runnable runnable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public partial class StreamImageSourceService
Stream? stream = null;
try
{
stream = await streamImageSource.GetStreamAsync(cancellationToken).ConfigureAwait(false);
stream = await streamImageSource.GetStreamAsync(cancellationToken);

var callback = new ImageLoaderCallback();

Expand Down
Binary file modified src/Core/src/maui.aar
Binary file not shown.