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

Implement SKGLView for .NET MAUI #2598

Merged
merged 1 commit into from
Feb 4, 2024
Merged

Implement SKGLView for .NET MAUI #2598

merged 1 commit into from
Feb 4, 2024

Conversation

mattleibow
Copy link
Contributor

@mattleibow mattleibow commented Sep 4, 2023

Description of Change

  • Implemented SKGLView for Android, iOS and Windows
  • Mac Catalyst will not get support - a new Metal-based view needs to be created
    • Maybe I can use a metal view instead of GL, or bind the GL view myself...
  • Tizen first needs the support in the native view

Bugs Fixed

API Changes

None.

Behavioral Changes

None.

Required skia PR

None.

PR Checklist

  • Has tests (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Merged related skia PRs
  • Changes adhere to coding standard
  • Updated documentation

@mattleibow mattleibow force-pushed the dev/glview branch 2 times, most recently from 0df2a39 to 1fb1354 Compare September 17, 2023 09:35
@marcojak
Copy link

Hi Matthew, are there any plans to have a common hw accelerated view for a MAUI app for Android, iOS, Windows and Mac?

If not possible, how could I use an hw accelerated view on Windows? (and maybe a new one for Mac)?

@TopperDEL
Copy link

TopperDEL commented Oct 23, 2023

@marcojak This issue might be related for you: #1893

@r2d2Proton
Copy link

Definitely would like this to work on all the platforms being targeted. Windows, Mac, etc.

@bogdan799
Copy link

@mattleibow I've sort of tested implementation of SKGLView on ios and android. I needed metal on iOS, so I've just replaced native view for SKGLViewHandler.iOS and seem to be fine, could be considered while implementing universal HW accelerated view. All seems to be much smoother than CPU rendering for my usecase, thank you for the solution.

However I've noticed following issue on android:

Here when setting Rendermode to RenderMode.WhenDirty, repaint doesn't happen althouth I'm invalidating view many times:

SKGLViewHandler.Android.cs

public static void MapHasRenderLoop(SKGLViewHandler handler, ISKGLView view)
{
	handler.PlatformView.RenderMode = view.HasRenderLoop
		? Rendermode.Continuously
		: Rendermode.WhenDirty;
}

I checked following handler function, and seems you're using wrong method here:

// Mapper actions / properties

public static void OnInvalidateSurface(SKGLViewHandler handler, ISKGLView view, object? args)
{
	handler.PlatformView?.Invalidate();
}

According to MSDN:

Continuously 1 The renderer is called continuously to re-render the scene.
WhenDirty 0 The renderer only renders when the surface is created, or when RequestRender() is called.

Here's the change I've applied according to MSDN and it seem to work fine:

public static void OnInvalidateSurface(SKGLViewHandler handler, ISKGLView view, object? args)
{
	handler.PlatformView?.RequestRender();
}

@mattleibow
Copy link
Contributor Author

Thanks @bogdan799! Your findings helped.

- Mac Catalyst does not support OpenGL and will need a
  new Metal-based view
- Tizen needs the support in the native view
@mattleibow mattleibow merged commit 04ff977 into main Feb 4, 2024
2 checks passed
@mattleibow mattleibow deleted the dev/glview branch February 4, 2024 11:32
@levinli303
Copy link

Mac Catalyst has CAOpenGLLayer. You can have a UIView backed by CAOpenGLLayer and render OpenGL in the layer so that you don't have to mess with Metal.

@mattleibow
Copy link
Contributor Author

I am not sure if it is exposed in the .NET bindings. They left out all the deprecated APIs for Mac Catalyst :(

@ToolmakerSteve
Copy link

ToolmakerSteve commented May 15, 2024

@mattleibow I am getting the exception "Handler not found for view SkiaSharp.Views.Maui.Controls.SKGLView." in a fresh .Net Maui App project, targetting "Windows Machine", with an SKGLView added.

The exception occurs at run time. Commenting out the SKGLView line, the app runs w/o error.


Added these two lines in appropriate places in MainPage.xaml:
xmlns:sk="clr-namespace:SkiaSharp.Views.Maui.Controls;assembly=SkiaSharp.Views.Maui.Controls"

<sk:SKGLView WidthRequest="500" HeightRequest="300" BackgroundColor="Green" />


Or alternatively adding in code behind after a delay:

        public MainPage()
        {
            InitializeComponent();

            MainThread.BeginInvokeOnMainThread(async () =>
            {
                await Task.Delay(2000);
                var skv = new SKGLView() { WidthRequest = 300, HeightRequest = 300 };
                layout.Children.Add(skv);   // "Handler not found..." EXCEPTION HERE
            });
        }

-- Other details --

.csproj targetframework: net8.0-windows10.0.19041.0

VS Community 17.9.7 on Windows 11 Home

Tested with Microsoft.Maui.Controls 8.0.40 +

SkiaSharp.Views & SkiaSharp.Views.Maui Controls:

  • latest stable 2.88.8, then tried
  • latest preview 3.0.0-preview.3.1

Is this expected to work in Maui 8, or do I need a preview version of .Net Maui 9?

Should I create a new issue and/or upload a sample project?

@mattleibow
Copy link
Contributor Author

Please open a new issue with a repro sample. GL should be working.

@ToolmakerSteve
Copy link

#2860

@ToolmakerSteve
Copy link

@mattleibow - is there a plan to backport #2598 (Maui SKGLView fix) on Windows to 2.88.x?

Updating my client from .Net Framework 4.5 to .Net 8.

We've successfully updated on iOS and Android.
(WITHOUT Maui; these are "platform native" .Net 8 apps, with GUI rendered via SkiaSharp. Plus some OpenGL.)

On Windows .Net 8, SkiaSharp 2.88.8 or .9, we've got everything EXCEPT OpenGL working. Using Maui SKCanvasView.

I made a new Maui app. Added SkiaSharp 2.88.9. Remembered to do "UseSkiaSharp()". Still get "Handler not found for view SkiaSharp.Views.Maui.Controls.SKGLView.".

Updating to SkiaSharp 3.118.0.0 preview, it works. BUT this app is used in a production situation where failures must be very rare. So I can't use anything that isn't considered a stable release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SKGLView on .NET MAUI is missing a handler
7 participants