mono / SkiaSharp

SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
MIT License
4.24k stars 525 forks source link

.NET MAUI and/or WinUI 3 `SKGLView` is not transparent #2871

Open janne-hmp opened 1 month ago

janne-hmp commented 1 month ago

Description

SKGLView always draws an opaque gray background instead of a transparent background when specified to be transparent. SKCanvasView works fine.

Code

https://github.com/hyvanmielenpelit/GnollHack

Expected Behavior

Both SKGLView and SKCanvasView draw a transparent background upon canvas.Clear().

Actual Behavior

SKCanvasView draws a transparent background upon canvas.Clear() but SKGLView draws an opaque gray background.

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

Windows 11 Home, version 10.0.22631

Devices

Micro-Star International Co., Ltd. Pulse GL66 11UDK

Relevant Screenshots

image

Relevant Log Output

No response

Code of Conduct

TommiGustafsson-HMP commented 1 month ago

This is most likely because SwapChainPanel does not support transparency:

https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/composition

SKGLView uses it on MAUI Windows.

taublast commented 1 month ago

Indeed this is not a bug but unfortunately by design as windows GL hardware-accelerated view cannot be transparent.

TommiGustafsson-HMP commented 1 month ago

This is surely the limitation of the WinUI 3 composition engine, because WinUI 2 and UWP supported transparent backgrounds for swap chains.

janne-hmp commented 1 month ago

Indeed this is not a bug but unfortunately by design as windows GL hardware-accelerated view cannot be transparent.

Would you be able to make a feature request for the WinUI3 team to add support for transparency?

Without transparency, Windows SKGLView is pretty useless. Both Android and iOS support transparency for SKGLView. If transparency worked in WinUI2, I don't see why it would be impossible to implement for WinUI3.

TommiGustafsson-HMP commented 1 month ago

It's already been done here:

https://github.com/microsoft/microsoft-ui-xaml/issues/5025

It's a long standing issue requested by many users already.

TommiGustafsson-HMP commented 1 month ago

A similar thread is here:

https://github.com/microsoft/microsoft-ui-xaml/issues/8130#issuecomment-1817217110

TommiGustafsson-HMP commented 1 month ago

An interesting thing is that some of the use-cases, such as a transparent WebView2 over a MediaPlayerElement, could be done with the current composition engine, if there were a way to activate transparency for external content that is over other external content.

taublast commented 1 month ago

Well, we all would sure want to render an accelerated canvas over a usual MAUI content, so.. Not until we have a different backend for WinUI what would be close to a miracle. =)

mattleibow commented 1 month ago

Maybe this PR will expose Direct3D and then we can add a new D3D view to be used as a backing element:

Anyone that can help test or review will make things go faster. Also, if there are any folks that know how to hook up the WinUI + Direct3D + Composition, then please feel very free to help in any way.

TommiGustafsson-HMP commented 1 month ago

I'm not sure how we could best help here, but at least we could test new views, if they are available as a NuGet package for .NET MAUI on Windows.

TommiGustafsson-HMP commented 1 month ago

Here's an example in C++ how to make a transparent DirectX SwapChainPanel:

https://github.com/brabebhin/MayazucMediaPlayer/blob/main/source/MayazucNativeFramework/SubtitleRenderer.cpp

Info from here: https://github.com/microsoft/microsoft-ui-xaml/discussions/9684#discussioncomment-9640311