unoplatform / uno

Build Mobile, Desktop and WebAssembly apps with C# and XAML. Today. Open source and professionally supported.
https://platform.uno
Apache License 2.0
8.76k stars 706 forks source link

[WASM][.NET 7.0] Enabling Threads and trying to draw on an SKXamlCanvas throws System.InvalidOperationException #10965

Closed Rytisgit closed 1 year ago

Rytisgit commented 1 year ago

Current behavior

Trying to use the new threading functionality which was merged into skiasharp, it always throws an exception when trying to draw something.

      Dispatcher unhandled exception
System.InvalidOperationException: Error #1 "TypeError: SkiaSharp.Views.Windows.SKXamlCanvas.clearCanvas is not a function
    at eval (eval at closedEval (http://localhost:8000/package_1969990d6435f002e3a34a58c011fff87b809961/dotnet.js:5:107383), <anonymous>:1:38)
    at closedEval (http://localhost:8000/package_1969990d6435f002e3a34a58c011fff87b809961/dotnet.js:5:107383)
    at Object.mono_wasm_invoke_js (http://localhost:8000/package_1969990d6435f002e3a34a58c011fff87b809961/dotnet.js:5:107399)
    at _mono_wasm_invoke_js (http://localhost:8000/package_1969990d6435f002e3a34a58c011fff87b809961/dotnet.js:9516:63)
    at http://localhost:8000/package_1969990d6435f002e3a34a58c011fff87b809961/dotnet.wasm:wasm-function[14600]:0x50f581
    at http://localhost:8000/package_1969990d6435f002e3a34a58c011fff87b809961/dotnet.wasm:wasm-function[14545]:0x50e44d
    at http://localhost:8000/package_1969990d6435f002e3a34a58c011fff87b809961/dotnet.wasm:wasm-function[14474]:0x4fec5a
    at http://localhost:8000/package_1969990d6435f002e3a34a58c011fff87b809961/dotnet.wasm:wasm-function[14473]:0x4fda84
    at http://localhost:8000/package_1969990d6435f002e3a34a58c011fff87b809961/dotnet.wasm:wasm-function[20857]:0x5dd5a6
    at http://localhost:8000/package_1969990d6435f002e3a34a58c011fff87b809961/dotnet.wasm:wasm-function[17508]:0x581050" executing javascript: "SkiaSharp.Views.Windows.SKXamlCanvas.clearCanvas("56738");"
   at WebAssembly.Runtime.InvokeJS(String str) in C:\a\1\s\src\Uno.Foundation.Runtime.WebAssembly\Interop\WebAssembly.Runtime.cs:line 44
   at Uno.Foundation.WebAssemblyRuntime.InnerInvokeJS(String str) in C:\a\1\s\src\Uno.Foundation.Runtime.WebAssembly\Interop\Runtime.wasm.cs:line 260
   at Uno.Foundation.WebAssemblyRuntime.InvokeJS(String str) in C:\a\1\s\src\Uno.Foundation.Runtime.WebAssembly\Interop\Runtime.wasm.cs:line 231
   at SkiaSharp.Views.Windows.SKXamlCanvas.FreeBitmap() in /home/vsts/work/1/s/source/SkiaSharp.Views.Uno/SkiaSharp.Views.Uno.Wasm/SKXamlCanvas.Wasm.cs:line 93
   at SkiaSharp.Views.Windows.SKXamlCanvas.CreateBitmap(SKSizeI& unscaledSize, Single& dpi) in /home/vsts/work/1/s/source/SkiaSharp.Views.Uno/SkiaSharp.Views.Uno.Wasm/SKXamlCanvas.Wasm.cs:line 80
   at SkiaSharp.Views.Windows.SKXamlCanvas.DoInvalidate() in /home/vsts/work/1/s/source/SkiaSharp.Views.Uno/SkiaSharp.Views.Uno.Wasm/SKXamlCanvas.Wasm.cs:line 53
   at SkiaSharp.Views.Windows.SKXamlCanvas.Invalidate() in /home/vsts/work/1/s/source/SkiaSharp.Views.Uno/SkiaSharp.Views.Uno/SKXamlCanvas.cs:line 131
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state) in D:\a\Uno.DotnetRuntime.WebAssembly\Uno.DotnetRuntime.WebAssembly\runtime\src\libraries\System.Private.CoreLib\src\System\Threading\Tasks\Task.cs:line 1914
   at Uno.UI.Dispatching.CoreDispatcherSynchronizationContext.<>c__DisplayClass3_0.<Post>b__0() in /home/vsts/work/1/s/src/Uno.UI.Dispatching/Core/CoreDispatcherSynchronizationContext.cs:line 25
   at Uno.UI.Dispatching.CoreDispatcher.InvokeOperationSafe(UIAsyncOperation operation) in /home/vsts/work/1/s/src/Uno.UI.Dispatching/Core/CoreDispatcher.cs:line 342 

Expected behavior

Threads work with Skiasharp and SKXamlCanvas.

How to reproduce it (as minimally and precisely as possible)

skxamlcanvas_thread_bug.zip

Workaround

None that I know.

Works on UWP/WinUI

None

Environment

No response

NuGet package version(s)

<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="6.0.0" />
<PackageReference Include="SkiaSharp.Views.Uno.WinUI" Version="2.88.4-preview.11" />
<PackageReference Include="Uno.Extensions.Logging.WebAssembly.Console" Version="1.4.0" />
<PackageReference Include="Uno.WinUI.WebAssembly" Version="4.7.0-dev.908" />
<PackageReference Include="Uno.WinUI.RemoteControl" Version="4.7.0-dev.908" Condition="'$(Configuration)'=='Debug'" />
<PackageReference Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="4.7.0-dev.908" />
<PackageReference Include="Uno.Wasm.Bootstrap" Version="8.0.0-dev.65" />
<PackageReference Include="Uno.Wasm.Bootstrap.DevServer" Version="8.0.0-dev.65" />
<PackageReference Include="Uno.WinUI.Lottie" Version="4.7.0-dev.908" />

The "SkiaSharp.Views.Uno.WinUI" Version="2.88.4-preview.11" is from the Skiasharp preview Nuget Source: https://aka.ms/skiasharp-eap/index.json

Affected platforms

WebAssembly

IDE

Visual Studio 2022

IDE version

Version 17.4.3

Relevant plugins

No response

Anything else we need to know?

hosted with dotnet serve as per the threading documentation: dotnet serve -p 8000 -h "Cross-Origin-Embedder-Policy: require-corp" -h "Cross-Origin-Opener-Policy: same-origin"

Rytisgit commented 1 year ago

I tested a bit more and it looks like it's not actually the threading setting that's broken, but the skiasharp nuget, with "SkiaSharp.Views.Uno.WinUI" Version="2.88.4-preview.3" working, but not having threading available, and "SkiaSharp.Views.Uno.WinUI" Version="2.88.4-preview.4" failing both with threads and without

jeromelaban commented 1 year ago

Latest builds of SkiaSharp 2.88.4 should be fixing this issue.