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.55k stars 543 forks source link

[FEATURE] Xamarin.Forms Android: Add an option to SKGLView to paint on the UI thread #1465

Open daltonks opened 4 years ago

daltonks commented 4 years ago

Is your feature request related to a problem? Please describe.

I am currently working on a UI framework in SkiaSharp. SKGLView on iOS and UWP paints the surface on the main thread, and Android does not. Because of this, I have concurrency issues when updating my state on the main thread on Android.

Describe the solution you'd like

An option on SKGLView to ensure that painting gets called on the UI thread.

Describe alternatives you've considered

Calling InvokeOnMainThreadAsync and waiting for it, but that deadlocks when rotating the screen.

gmurray81 commented 4 years ago

@mattleibow was there any progress on this? This makes it a bit difficult to render into the GL views on android from logic that requires serialized access, would be nice if there was an option for SKGLView to just marshal all the rendering onto the UI thread. I've achieved this, but in a way that makes me feel pretty unclean.

maiad commented 4 years ago

Hallo, I am in the same situation with my UI. Is it possible to synchronize PaintSurface with the main UI thread in Android ? I have tried SKGLSurfaceView & SKGLTextureView, both are rendering in the separate thread. When I use this thread I get many problems, because I cannot directly use AndroidActivity and Views, I cannot use awaitng tasks, because there is no task sheduler in the render task. I split the UI to animation part(on main UI thread) and paint part (on render thread) but there are so many complications with this...

........ Update ...

I have changed my UI. It can be painted inside another thread.

tmijieux commented 3 years ago

Any update on this ? I understand that drawing on another thread could maybe help the rest of xamarin forms draw other things while the SKGLView thread is drawing on its surface, but the main thread should definitely not let the control back to user code before the drawing is finished

Here is a small reproduction that illustrate the problem https://github.com/tmijieux/TestForMapsui/tree/skglview-thread-android

expected behavior: after tapping any of the button no NullReferenceException happens(after 5secs) actual behavior: there is a NullReferenceException