As per the tittle, I am unable to get the EGL14.eglGetCurrentContext() inside an Android Java class. More precisely, the returned context is equal to EGL14.EGL_NO_CONTEXT.
My interpretation is that the code is called from the main Unity thread yet the code is not able to get the OpenGL context.
Multithreaded rendering is disabled. The project is a Unity project exported to Android. This C# code calls the Java initSurface method from an Update function of a MonoBehavior:
private void Update()
{
if (_camMonitorInstance == null)
{
// it is important to call this in update method. Single Threaded Rendering will run in UnityMain Thread
InitAndroidSurface(camWidth, camHeight);
}
else
{
_camMonitorInstance.Call("updateSurfaceTexture");
}
}
This should ensure that it will be called from the UnityMain thread. I am very confident that the thread is UnityMain because in updateSurfaceTexture Thread.currentThread().getName().equals(“UnityMain”) is true.
private void initSurface() {
unityContext = EGL14.eglGetCurrentContext();
unityDisplay = EGL14.eglGetCurrentDisplay();
unityDrawSurface = EGL14.eglGetCurrentSurface(EGL14.EGL_DRAW);
unityReadSurface = EGL14.eglGetCurrentSurface(EGL14.EGL_READ);
if (unityContext == EGL14.EGL_NO_CONTEXT) {
Log.e(TAG, "UnityEGLContext is invalid -> Most probably wrong thread");
}
EGL14.eglMakeCurrent(unityDisplay, unityDrawSurface, unityReadSurface, unityContext);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, _unityTextureId);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
_surfaceTexture = new SurfaceTexture(_unityTextureId);
_surfaceTexture.setDefaultBufferSize(_textureWidth, _textureHeight);
_surface = new Surface(_surfaceTexture);
_surfaceTexture.setOnFrameAvailableListener(this);
}