SkiaSharp
SkiaSharp copied to clipboard
[BUG] [WinUI 3 / MAUI, 9.0 Preview 6, SkiaSharp 3.0-preview4.1] SKGLView crashes WinUI 3 MAUI App when the app is built in the unpackaged mode
Description
Technology stack
- .NET 9.0 Preview 6
- .NET MAUI 9.0 Preview 6
- SkiaSharp 3.0-preview4.1
- Windows / WinUI 3 1.5.x
Bug
SKGLView crashes the app at runtime when .NET MAUI App is built in the unpackaged mode (-p:WindowsPackageType=None).
Code
Steps to reproduce the bug
- Clone the public reproduction repository (link) and open the solution in Visual Studio 2022 Preview (latest).
- First, let's check that things work in the packaged mode.
- Select Windows Machine and click the green Debug button.
- The app should start.
- Click the Click Me button. A new page with some text should open. The app will not crash.
- Close the app and go back to Visual Studio 2022 Preview.
- Open the Developer PowerShell and check that you are in the solution folder.
- Delete bin and obj folders. (Otherwise the unpackaged app will not start.)
- Build the unpackaged app with
dotnet build -f:net9.0-windows10.0.19041.0 -c:Debug -p:WindowsPackageType=None -p:RuntimeIdentifierOverride=win10-x64 -p:WindowsAppSDKSelfContained=true - Open File Explorer and go to
bin\Debug\net9.0-windows10.0.19041.0\win10-x64. The unpackaged app should be there. - Click on WindowsMauiUnpackagedSKGLViewError.exe.
- The app will start.
- Click on the Click me button.
- The app will crash.
- Now, open Event Viewer and see the Windows Logs → Application error messages.
Expected Behavior
The app would not crash at runtime.
Actual Behavior
The app crashes in the OnLoaded event of AngleSwapChainPanel at runtime.
Version of SkiaSharp
3.x (Alpha)
Last Known Good Version of SkiaSharp
Other (Please indicate in the description)
IDE / Editor
Visual Studio (Windows)
Platform / Operating System
Windows
Platform / Operating System Version
Edition Windows 11 Pro Version 23H2 OS build 22631.3958 Experience Windows Feature Experience Pack 1000.22700.1026.0
Devices
Processor 13th Gen Intel(R) Core(TM) i9-13980HX 2.20 GHz Installed RAM 32,0 GB (31,6 GB usable) System type 64-bit operating system, x64-based processor
Relevant Screenshots
No response
Relevant Log Output
Log Name: Application
Source: .NET Runtime
Date: 20.7.2024 21.57.32
Event ID: 1026
Task Category: None
Level: Error
Keywords: Classic
User: N/A
Description:
Application: WindowsMauiUnpackagedSKGLViewError.exe
CoreCLR Version: 9.0.24.32707
.NET Version: 9.0.0-preview.6.24327.7
Description: The process was terminated due to an unhandled exception.
Stack:
at SkiaSharp.Views.GlesInterop.Egl.eglGetPlatformDisplayEXT(UInt32, IntPtr, Int32[])
at SkiaSharp.Views.GlesInterop.Egl.eglGetPlatformDisplayEXT(UInt32, IntPtr, Int32[])
at SkiaSharp.Views.GlesInterop.GlesContext.InitializeDisplay()
at SkiaSharp.Views.GlesInterop.GlesContext..ctor()
at SkiaSharp.Views.Windows.AngleSwapChainPanel.OnLoaded(System.Object, Microsoft.UI.Xaml.RoutedEventArgs)
at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler+EventState.<GetEventInvoke>b__1_0(System.Object, Microsoft.UI.Xaml.RoutedEventArgs)
at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr, IntPtr, IntPtr)
at ABI.Microsoft.UI.Xaml.IApplicationStaticsMethods.Start(WinRT.IObjectReference, Microsoft.UI.Xaml.ApplicationInitializationCallback)
at Microsoft.UI.Xaml.Application.Start(Microsoft.UI.Xaml.ApplicationInitializationCallback)
at WindowsMauiUnpackagedSKGLViewError.WinUI.Program.Main(System.String[])
Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name=".NET Runtime" />
<EventID Qualifiers="0">1026</EventID>
<Version>0</Version>
<Level>2</Level>
<Task>0</Task>
<Opcode>0</Opcode>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2024-07-20T18:57:32.8237381Z" />
<EventRecordID>82274</EventRecordID>
<Correlation />
<Execution ProcessID="2176" ThreadID="0" />
<Channel>Application</Channel>
</System>
<EventData>
<Data>Application: WindowsMauiUnpackagedSKGLViewError.exe
CoreCLR Version: 9.0.24.32707
.NET Version: 9.0.0-preview.6.24327.7
Description: The process was terminated due to an unhandled exception.
Stack:
at SkiaSharp.Views.GlesInterop.Egl.eglGetPlatformDisplayEXT(UInt32, IntPtr, Int32[])
at SkiaSharp.Views.GlesInterop.Egl.eglGetPlatformDisplayEXT(UInt32, IntPtr, Int32[])
at SkiaSharp.Views.GlesInterop.GlesContext.InitializeDisplay()
at SkiaSharp.Views.GlesInterop.GlesContext..ctor()
at SkiaSharp.Views.Windows.AngleSwapChainPanel.OnLoaded(System.Object, Microsoft.UI.Xaml.RoutedEventArgs)
at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler+EventState.<GetEventInvoke>b__1_0(System.Object, Microsoft.UI.Xaml.RoutedEventArgs)
at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr, IntPtr, IntPtr)
at ABI.Microsoft.UI.Xaml.IApplicationStaticsMethods.Start(WinRT.IObjectReference, Microsoft.UI.Xaml.ApplicationInitializationCallback)
at Microsoft.UI.Xaml.Application.Start(Microsoft.UI.Xaml.ApplicationInitializationCallback)
at WindowsMauiUnpackagedSKGLViewError.WinUI.Program.Main(System.String[])
</Data>
</EventData>
</Event>
Log Name: Application
Source: Application Error
Date: 20.7.2024 21.57.33
Event ID: 1000
Task Category: Application Crashing Events
Level: Error
Keywords:
Description:
Faulting application name: WindowsMauiUnpackagedSKGLViewError.exe, version: 1.0.0.0, time stamp: 0x667d0000
Faulting module name: coreclr.dll, version: 9.0.24.32707, time stamp: 0x667de227
Exception code: 0xc0000005
Fault offset: 0x00000000002de54e
Faulting process ID: 0x0x880
Faulting application start time: 0x0x1DADAD6AC829340
Faulting application path: C:\hmp\WindowsMauiUnpackagedSKGLViewError\WindowsMauiUnpackagedSKGLViewError\WindowsMauiUnpackagedSKGLViewError\bin\Debug\net9.0-windows10.0.19041.0\win10-x64\WindowsMauiUnpackagedSKGLViewError.exe
Faulting module path: C:\Program Files\dotnet\shared\Microsoft.NETCore.App\9.0.0-preview.6.24327.7\coreclr.dll
Report ID: 3e1585be-c9dd-41c0-aa20-9933b81efeae
Faulting package full name:
Faulting package-relative application ID:
Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Application Error" Guid="{a0e9b465-b939-57d7-b27d-95d8e925ff57}" />
<EventID>1000</EventID>
<Version>0</Version>
<Level>2</Level>
<Task>100</Task>
<Opcode>0</Opcode>
<Keywords>0x8000000000000000</Keywords>
<TimeCreated SystemTime="2024-07-20T18:57:33.1772821Z" />
<EventRecordID>82275</EventRecordID>
<Correlation />
<Execution ProcessID="52744" ThreadID="31284" />
<Channel>Application</Channel>
</System>
<EventData>
<Data Name="AppName">WindowsMauiUnpackagedSKGLViewError.exe</Data>
<Data Name="AppVersion">1.0.0.0</Data>
<Data Name="AppTimeStamp">667d0000</Data>
<Data Name="ModuleName">coreclr.dll</Data>
<Data Name="ModuleVersion">9.0.24.32707</Data>
<Data Name="ModuleTimeStamp">667de227</Data>
<Data Name="ExceptionCode">c0000005</Data>
<Data Name="FaultingOffset">00000000002de54e</Data>
<Data Name="ProcessId">0x880</Data>
<Data Name="ProcessCreationTime">0x1dadad6ac829340</Data>
<Data Name="AppPath">C:\hmp\WindowsMauiUnpackagedSKGLViewError\WindowsMauiUnpackagedSKGLViewError\WindowsMauiUnpackagedSKGLViewError\bin\Debug\net9.0-windows10.0.19041.0\win10-x64\WindowsMauiUnpackagedSKGLViewError.exe</Data>
<Data Name="ModulePath">C:\Program Files\dotnet\shared\Microsoft.NETCore.App\9.0.0-preview.6.24327.7\coreclr.dll</Data>
<Data Name="IntegratorReportId">3e1585be-c9dd-41c0-aa20-9933b81efeae</Data>
<Data Name="PackageFullName">
</Data>
<Data Name="PackageRelativeAppId">
</Data>
</EventData>
</Event>
Code of Conduct
- [X] I agree to follow this project's Code of Conduct
@mattleibow I ran analysis on the dump using WinDbg. It says the following:
0:000> !analyze -v
*******************************************************************************
* *
* Exception Analysis *
* *
*******************************************************************************
Failed to request MethodData, not in JIT code range
KEY_VALUES_STRING: 1
Key : AV.Dereference
Value: NullPtr
Key : AV.Fault
Value: Read
Key : Analysis.CPU.mSec
Value: 1890
Key : Analysis.Elapsed.mSec
Value: 12794
Key : Analysis.IO.Other.Mb
Value: 9
Key : Analysis.IO.Read.Mb
Value: 2
Key : Analysis.IO.Write.Mb
Value: 29
Key : Analysis.Init.CPU.mSec
Value: 2781
Key : Analysis.Init.Elapsed.mSec
Value: 19018
Key : Analysis.Memory.CommitPeak.Mb
Value: 377
Key : Analysis.Version.DbgEng
Value: 10.0.27725.1000
Key : Analysis.Version.Description
Value: 10.2408.27.01 amd64fre
Key : Analysis.Version.Ext
Value: 1.2408.27.1
Key : CLR.Engine
Value: CORECLR
Key : CLR.Version
Value: 9.0.24.47305
Key : Failure.Bucket
Value: NULL_POINTER_READ_c0000005_coreclr.dll!ProcessCLRExceptionNew
Key : Failure.Hash
Value: {c7c8b0e4-8662-6d89-0847-b939a6a3bc0f}
Key : Failure.Source.FileLine
Value: 952
Key : Failure.Source.FilePath
Value: D:\a\_work\1\s\src\coreclr\vm\exceptionhandling.cpp
Key : Failure.Source.SourceServerCommand
Value: raw.githubusercontent.com/dotnet/runtime/990ebf52fc408ca45929fd176d2740675a67fab8/src/coreclr/vm/exceptionhandling.cpp
Key : Timeline.OS.Boot.DeltaSec
Value: 923469
Key : Timeline.Process.Start.DeltaSec
Value: 7
Key : WER.OS.Branch
Value: ni_release
Key : WER.OS.Version
Value: 10.0.22621.1
Key : WER.Process.Version
Value: 1.0.0.0
FILE_IN_CAB: WindowsMauiUnpackagedSKGLViewError.exe.31924.dmp
NTGLOBALFLAG: 0
APPLICATION_VERIFIER_FLAGS: 0
CONTEXT: 000001d80dfd45b8 -- (.cxr 0x1d80dfd45b8)
rax=0000000000000000 rbx=0000000000000000 rcx=0000000000000000
rdx=0000000000000000 rsi=0000000000000000 rdi=0000000100000000
rip=3f80000000000000 rsp=0000000000000000 rbp=0000000000000000
r8=0000000000000000 r9=0000000000000000 r10=0000000000000000
r11=0000000000000000 r12=0000000000000000 r13=3f8000003f800000
r14=3f8000003f800000 r15=0000000000000000
iopl=0 nv up di pl nz na pe nc
cs=4df1 ss=0000 ds=0e08 es=01d8 fs=0000 gs=0001 efl=00000000
3f800000`00000000 ?? ???
Resetting default scope
EXCEPTION_RECORD: 000001d819de2c70 -- (.exr 0x1d819de2c70)
ExceptionAddress: 0000000000000000
ExceptionCode: 7fb4fd98
ExceptionFlags: 00007ffd
NumberParameters: 0
PROCESS_NAME: WindowsMauiUnpackagedSKGLViewError.dll
READ_ADDRESS: 0000000000000000
ERROR_CODE: (NTSTATUS) 0xc0000005 - The instruction at 0x%p referenced memory at 0x%p. The memory could not be %s.
EXCEPTION_CODE_STR: c0000005
FAULTING_THREAD: ffffffff
STACK_TEXT:
00007ffe`6e67be7f 00007ffe`6e67be7f coreclr!ProcessCLRExceptionNew+0xfb
STACK_COMMAND: ** Pseudo Context ** Pseudo ** Value: 3 ** ; kb
FAULTING_SOURCE_LINE: D:\a\_work\1\s\src\coreclr\vm\exceptionhandling.cpp
FAULTING_SOURCE_FILE: D:\a\_work\1\s\src\coreclr\vm\exceptionhandling.cpp
FAULTING_SOURCE_LINE_NUMBER: 952
FAULTING_SOURCE_SRV_COMMAND: https://raw.githubusercontent.com/dotnet/runtime/990ebf52fc408ca45929fd176d2740675a67fab8/src/coreclr/vm/exceptionhandling.cpp
FAULTING_SOURCE_CODE:
No source found for 'D:\a\_work\1\s\src\coreclr\vm\exceptionhandling.cpp'
SYMBOL_NAME: coreclr!ProcessCLRExceptionNew+fb
MODULE_NAME: coreclr
IMAGE_NAME: coreclr.dll
FAILURE_BUCKET_ID: NULL_POINTER_READ_c0000005_coreclr.dll!ProcessCLRExceptionNew
OS_VERSION: 10.0.22621.1
BUILDLAB_STR: ni_release
OSPLATFORM_TYPE: x64
OSNAME: Windows 10
IMAGE_VERSION: 9.0.24.47305
FAILURE_ID_HASH: {c7c8b0e4-8662-6d89-0847-b939a6a3bc0f}
Followup: MachineOwner
---------
At this point, I am using Visual Studio 2022 Community Preview 17.12.0 Preview 5.0, .NET MAUI 9.0 RC2, SkiaSharp 3.118.0-Preview1.2. I have also activated Full Dump (2) in Registry Editor for the test app.
A quick guess: before net9 we would never expect an unpackaged mode on Windows, seems it was implemented to speed up development (no idea why, windows was already the fastest platform to debug a MAUI app haha).
Anyway it may be that skiasharp is calling some code present in packaged mode only, resulting in this crash.
https://x.com/dotMorten/status/1849193454414573679
@mattleibow Since Direct3D support is coming to SkiaSharp (#2823), would it be possible to add a Direct3D-accelerated view to WinUI/MAUI views to replace SKGLView, which does not work in the unpackaged mode on Windows? Or make it possible to choose a Direct3D renderer for SKGLView, which would work in the unpackaged mode (which is now default for .NET 9 apps)?
WinUI is technically using Direct3D via the ANGLE library. ANGLE is basically a wrapper for DirectX that looks like OpenGL ES.
However, we still can add views to skip this layer and draw directly.
But, is this issue still occurring in unpackaged? I see the view does not have a width and height, so maybe the view is 0 height and causes a crash.
I will have a look as it should be working.
Thanks for clarifying things up.
I updated the test application to .NET SDK 9.0.201, .NET MAUI 9.0.50 and SkiaSharp 3.116.1. And it's still crashing in the unpackaged mode. Is there a SkiaSharp nuget package available that uses Direct3D, since 3.116.1 is crashing when AngleSwapChainPanel is accessing SkiaSharp.Views.GlesInterop.GlesContext?
I also added WidthRequest and HeightRequest to SKGLView, but they didn't help.
Can confirm SKGLView is crashing on Windows if accessed in unpackaged mode, tried on Debug. skiasharp 3.116.1 maui.controls 9.0.22 @TommiGustafsson-HMP what is the "unpackaged mode" use case? thought that was to speed up deployment on debug only?
Can confirm SKGLView is crashing on Windows if accessed in unpackaged mode, tried on Debug. skiasharp 3.116.1 maui.controls 9.0.22 @TommiGustafsson-HMP what is the "unpackaged mode" use case? thought that was to speed up deployment on debug only?
Our app is a game and Steam does not support MSIX, so we must upload it there as an unpackaged version.
OK, I will investigate.
It should be working, and if not then it is a horrible case of a bug fest.
That's much appreciated! Thank you!
The crash is still present in:
- .NET SDK 9.0.203
- .NET MAUI 9.0.60
- SkiaSharp 3.119.0-preview.1.2
I reported a similar issue (maybe the same at the core): https://github.com/mono/SkiaSharp/issues/3233
It has a very simple minimal reproducible sample: https://github.com/pauldendulk/skiasharpcrash
I reported a similar issue (maybe the same at the core): #3233
It has a very simple minimal reproducible sample: https://github.com/pauldendulk/skiasharpcrash
Yes, it's the same issue. The thing is that when I reported this issue on August 13, 2024, the default WindowsPackageType was MSIX. They changed it to None, when .NET 9 was released, and therefore the crash is now the default behavior with SKGLView on the Windows platform, which it was not back in August 2024.
@mattleibow I was able to debug this a bit further. I copied libEGL.dll and libGLESv2.dll from Google Chrome to the output directory, which resolved the System.ExecutionEngineException. However, then the next error was:
surface = Egl.eglCreateWindowSurface(eglDisplay, eglConfig, surfaceCreationProperties.As<IInspectable>().ThisPtr, surfaceAttributes);
if (surface == Egl.EGL_NO_SURFACE)
{
throw new Exception("Failed to create EGL surface");
}
failing with: EGL_BAD_NATIVE_WINDOW, 0x300B, 12299, A NativeWindowType is invalid, which I was able to get with eglGetError() in Immediate Window.
But apparently it was able to at least initialize the EGL.
So, I guess the problem in the unpackaged mode are the ANGLE libraries, and something else that it doesn't get the right native window.
I was able to fix this problem by using libEGL.dll from Google Chrome (137.0.7151.120) and libGLESv2.dll from SkiaSharp 3.119.0. That combination worked.
The problem at the moment is that in the current ANGLE build process:
- libEGL.dll must be compiled with
angle_is_winappsdk=false - libGLESv2.dll must be compiled with
angle_is_winappsdk=true
Or things do not work in the unpackaged mode.
We have 2 options to fix this.
Option 1
Fix the ANGLE build process, so that libEGL.dll works when compiled with angle_is_winappsdk=true.
Option 2
Compile ANGLE 2 times. Essentially you'd need to add something like this task to https://github.com/mono/SkiaSharp/blob/main/native/winui-angle/build.cake
Task("ANGLE2")
.IsDependentOn("sync-ANGLE")
.IsDependentOn("git-sync-deps")
.WithCriteria(IsRunningOnWindows())
.Does(() =>
{
Build("x86");
Build("x64");
Build("arm64");
void Build(string arch)
{
if (Skip(arch)) return;
try {
System.Environment.SetEnvironmentVariable("DEPOT_TOOLS_WIN_TOOLCHAIN", "0");
RunGn(ANGLE_PATH, $"out/winui/{arch}",
$"target_cpu='{arch}' " +
$"is_component_build=false " +
$"is_debug=false " +
$"is_clang=false " +
$"angle_is_winappsdk=false " +
$"winappsdk_dir='{WINAPPSDK_PATH}' " +
$"enable_precompiled_headers=false " +
$"angle_enable_null=false " +
$"angle_enable_wgpu=false " +
$"angle_enable_gl_desktop_backend=false " +
$"angle_enable_vulkan=false");
RunNinja(ANGLE_PATH, $"out/winui2/{arch}", "libEGL");
} finally {
System.Environment.SetEnvironmentVariable("DEPOT_TOOLS_WIN_TOOLCHAIN", "");
}
var outDir = OUTPUT_PATH.Combine(arch);
EnsureDirectoryExists(outDir);
CopyFileToDirectory(ANGLE_PATH.CombineWithFilePath($"out/winui/{arch}/libEGL.dll"), outDir);
CopyFileToDirectory(ANGLE_PATH.CombineWithFilePath($"out/winui/{arch}/libEGL.pdb"), outDir);
}
});
And of course remove libEGL.dll compiling from Task("ANGLE").
@TommiGustafsson-HMP But building it twice will also cause a distribution problem right? How would that work? Two different nuget packages? Or two versions in one nuget package? If so, how would the correct one be selected? This could cause dodgy problems.
And do you know what it would take to fix the ANGLE build for option 1?
No, it doesn't cause distribution problems, because if you build it twice, it works in both packaged (MSIX) and unpackaged modes. At least in x64 architecture.
Thanks will try today!
I built working ANGLE libraries for unpackaged WinAppSDK:
@TommiGustafsson-HMP Thanks again for you research! I used those and they work fine for both Un-packaged and MSIX-packaged. Is this same for you regarding the packaged mode?
Yes, they work well in the MSIX-packaged mode for me, too. So, they work in both modes.
@TommiGustafsson-HMP Thank you so much for this! The new DrawnUi preview nuget is now "Steam-friendly" with your dlls.
The two DLLs that I built are for the x64 architecture. SkiaSharp obviously needs also DLLs for the x86 and arm64 architectures, but I won't build them, because I have no way of testing that they work as intended, so I leave it to others.
@TommiGustafsson-HMP Well the fact that Windows x64 doesn't require MSIX mode anymore is really a huge deal!
For libs that when consumed on Windows just crash, then people have to search, read docs, to find out that they have to enable MSIX blabla, so no more of that, thanks to you. 🥇