For some reason, the SKSurface.Create(...) method returns null on the Android emulators on the mac. The device works, and "normal" OpenGL works on the emulators. I think skia is doing some check for something internally.
I'm facing the same issue on Android emulators on Windows (on a real device it works).
When loading an SKGLView in a Xamarin Forms App, I get the following exception:
android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
at SkiaSharp.SKSurface.Create (SkiaSharp.GRContext context, SkiaSharp.GRBackendRenderTargetDesc desc) [0x00000] in <087d0388435e4cd4ad8ad2862d6df448>:0
at SkiaSharp.Views.Android.SKGLSurfaceViewRenderer.OnDrawFrame (Javax.Microedition.Khronos.Opengles.IGL10 gl) [0x0000a] in <6ce3918488584aadaec5804bc552de4c>:0
at Android.Opengl.GLSurfaceView+IRendererInvoker.n_OnDrawFrame_Ljavax_microedition_khronos_opengles_GL10_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_gl) [0x00011] in <160ba97e03fe445783263e418f95ab21>:0
at (wrapper dynamic-method) System.Object:ca2dddba-5388-417b-b50a-03c33314115e (intptr,intptr,intptr)
at md50da5022b67416fd2ee3728f02525aaf6.SKGLSurfaceViewRenderer.n_onDrawFrame(Native Method)
at md50da5022b67416fd2ee3728f02525aaf6.SKGLSurfaceViewRenderer.onDrawFrame(SKGLSurfaceViewRenderer.java:32)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1523)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
As the exception comes from SkiaSharp.SKSurface.Create, maybe it's the context that is not initialized yet?
The issue (surface null in emulator on mac) is still present in 1.59.1.
Any update on this?
@mattleibow Is there already a schedule for this issue?
@mattleibow,
If there isn't a schedule for this issue atm, what would you consider the best workaround to be?
Do you think we'd be able to catch the exception somewhere, then swap out the SKGLView for an SKCanvasView?
I have similar problem. In SKGLView after upgrading to Xamarin.Forms 2.5. When OnCanvasViewPaintSurface is called on android the args.Surface = null. It also works fine on the real device - only emulator has problems (it has GL enabled). It worked fine with Forms 2.3 and 2.4.
As a "workaround", we are now using SKCanvasView instead of SKGLView when we detect an android emulator.
That one would be much easier, if SKCanavsView and SKGLView would share a decent base class or at least interface (if anyone knows some decent base class or interface that both share, please let me know).
The good thing is, that SKGLView and SKCanvasView can be easily exchanged, because methods and properties are (almost) compatible.
This workaround should only be considered as a temporary solution to at least be able to view the rendered canvas. This should definitely not be the solution to go for permanently.
I managed to locate the cause of the issue. It appears that GLES 2.0 sometimes does not support the RGBA8888 color format on some older emulators. Typically the devices have the extension to support this (and I found the new Google emulators now do). If the device has GLES 3.0 then this is not an issue.
So the bottom line, the older emulators did not support the color format.
From https://developer.android.com/guide/topics/graphics/opengl.html:
OpenGL ES 1.0 and 1.1 - This API specification is supported by Android 1.0 and higher.
OpenGL ES 2.0 - This API specification is supported by Android 2.2 (API level 8) and higher.
OpenGL ES 3.0 - This API specification is supported by Android 4.3 (API level 18) and higher.
OpenGL ES 3.1 - This API specification is supported by Android 5.0 (API level 21) and higher.
Basically, Android 4.3+ should never have any issues, and below that should be just a few emulators. I updated to the latest HAXM and now all my emulators are good.
Thanks for looking into this! I'll see if we need to update emulators over here.